Lua: can make new tabs from lua scripts, doc generation for available tabs to use (#1593)
* lua api: add globals.get_uint and globals.set_uint * lua doc: remove duplicate function check as we can overload so it doesn't make sense * lua doc gen: add support for parsing the tabs enum * gui: custom lua tabs don't have a `func` rendering function but can still have elements to draw * lua doc: update generated doc * chore: code style * chore: minor spelling mistake * chore: code style * gui_service: add runtime removal of tabs * refactor: make it so that it's less likely defining tabs and their translation key in a wrong way. * lua api: ability to add custom tabs to the gui from lua
This commit is contained in:
@ -17,6 +17,7 @@ class DocKind(Enum):
|
||||
Field = "field"
|
||||
Constructor = "constructor"
|
||||
Function = "function"
|
||||
Tabs = "tabs"
|
||||
|
||||
|
||||
class Table:
|
||||
@ -47,8 +48,6 @@ class Table:
|
||||
s += f"## Functions ({len(self.functions)})\n"
|
||||
s += "\n"
|
||||
|
||||
self.check_for_duplicate_function_names()
|
||||
|
||||
for func in self.functions:
|
||||
s += func.print_markdown(f"{self.name}.")
|
||||
|
||||
@ -65,15 +64,6 @@ class Table:
|
||||
print(dup)
|
||||
exit(1)
|
||||
|
||||
def check_for_duplicate_function_names(self):
|
||||
seen = set()
|
||||
duplicates = [x for x in self.functions if x.name in seen or seen.add(x.name)]
|
||||
if len(duplicates) > 0:
|
||||
print("Error while building lua doc. Duplicate function names:")
|
||||
for dup in duplicates:
|
||||
print(dup)
|
||||
exit(1)
|
||||
|
||||
|
||||
class Class:
|
||||
def __init__(self, name, inheritance, fields, constructors, functions, description):
|
||||
@ -117,8 +107,6 @@ class Class:
|
||||
s += f"## Functions ({len(self.functions)})\n"
|
||||
s += "\n"
|
||||
|
||||
self.check_for_duplicate_function_names()
|
||||
|
||||
for func in self.functions:
|
||||
s += func.print_markdown(f"{self.name}:")
|
||||
|
||||
@ -135,15 +123,6 @@ class Class:
|
||||
print(dup)
|
||||
exit(1)
|
||||
|
||||
def check_for_duplicate_function_names(self):
|
||||
seen = set()
|
||||
duplicates = [x for x in self.functions if x.name in seen or seen.add(x.name)]
|
||||
if len(duplicates) > 0:
|
||||
print("Error while building lua doc. Duplicate function names:")
|
||||
for dup in duplicates:
|
||||
print(dup)
|
||||
exit(1)
|
||||
|
||||
|
||||
class Field:
|
||||
def __init__(self, name, type_, description):
|
||||
@ -203,7 +182,7 @@ class Constructor:
|
||||
s += f"\n"
|
||||
s += "\n"
|
||||
|
||||
s += f"**Exemple Usage:**\n"
|
||||
s += f"**Example Usage:**\n"
|
||||
s += "```lua\n"
|
||||
|
||||
s += f"myInstance = {self.parent.name}:new({parameters_str})\n"
|
||||
@ -285,7 +264,7 @@ class Function:
|
||||
|
||||
s += "\n"
|
||||
|
||||
s += f"**Exemple Usage:**\n"
|
||||
s += f"**Example Usage:**\n"
|
||||
s += "```lua\n"
|
||||
if self.return_type is not None and len(self.return_type) > 0:
|
||||
s += self.return_type + " = "
|
||||
@ -345,7 +324,8 @@ def parse_lua_api_doc(folder_path):
|
||||
.lower()
|
||||
)
|
||||
|
||||
continue
|
||||
if doc_kind is not DocKind.Tabs:
|
||||
continue
|
||||
|
||||
if doc_kind is not None and "//" in line:
|
||||
match doc_kind:
|
||||
@ -387,6 +367,7 @@ def parse_lua_api_doc(folder_path):
|
||||
line,
|
||||
line_lower,
|
||||
)
|
||||
case DocKind.Tabs: parse_tabs_doc(file)
|
||||
case _:
|
||||
# print("unsupported doc kind: " + str(doc_kind))
|
||||
pass
|
||||
@ -503,6 +484,28 @@ def parse_constructor_doc(cur_constructor, cur_class, line, line_lower):
|
||||
return cur_constructor, cur_class
|
||||
|
||||
|
||||
tabs_enum = []
|
||||
def parse_tabs_doc(file):
|
||||
start_parsing = False
|
||||
for line in file:
|
||||
if "enum class" in line.lower():
|
||||
start_parsing = True
|
||||
continue
|
||||
|
||||
if start_parsing:
|
||||
if "};" in line.lower():
|
||||
return
|
||||
if "{" == line.lower().strip():
|
||||
continue
|
||||
if "//" in line.lower():
|
||||
continue
|
||||
if "" == line.lower().strip():
|
||||
continue
|
||||
else:
|
||||
tabs_enum.append(line.replace(",", "").strip())
|
||||
|
||||
|
||||
|
||||
def make_parameter_from_doc_line(line):
|
||||
param_info = line.split(lua_api_comment_separator, 3)[1:]
|
||||
param_name = param_type = param_desc = ""
|
||||
@ -542,6 +545,36 @@ for table_name, table in tables.items():
|
||||
f.write(str(table))
|
||||
f.close()
|
||||
|
||||
tabs_file_name = f"./tabs.md"
|
||||
if os.path.exists(tabs_file_name):
|
||||
os.remove(tabs_file_name)
|
||||
f = open(tabs_file_name, "a")
|
||||
|
||||
f.write("""# Tabs
|
||||
|
||||
All the tabs from the menu are listed below, used as parameter for adding gui elements to them.
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```lua
|
||||
missionsTab = gui.get_tab("GUI_TAB_MISSIONS")
|
||||
missionsTab:add_button("Click me", function ()
|
||||
log.info("You clicked!")
|
||||
end)
|
||||
```
|
||||
|
||||
For a complete list of available gui functions, please refer to the tab class documentation and the gui table documentation.
|
||||
|
||||
""")
|
||||
|
||||
f.write(f"## Tab Count: {len(tabs_enum)}\n\n")
|
||||
|
||||
# Minus the first, because it's the `NONE` tab, minus the last one because it's for runtime defined tabs.
|
||||
for i in range(1, len(tabs_enum) - 1 ):
|
||||
f.write("### `GUI_TAB_" + tabs_enum[i] + "`\n")
|
||||
|
||||
f.close()
|
||||
|
||||
try:
|
||||
os.makedirs("./classes/")
|
||||
except:
|
||||
|
Reference in New Issue
Block a user