diff --git a/data/scripts/editor_main.lua b/data/scripts/editor_main.lua index 1032eba723..a5f3b523fe 100644 --- a/data/scripts/editor_main.lua +++ b/data/scripts/editor_main.lua @@ -1,11 +1,57 @@ local tpl = [[ declare ImGui: { + AlignTextToFramePadding : () -> (), Begin : (string) -> boolean, - End : () -> (), - Text : (string) -> (), + BeginChildFrame : (string, number, number) -> boolean, + BeginPopup : (string) -> boolean, Button : (string) -> boolean, + CalcTextSize : (string) -> (number, number), + Checkbox : (string, boolean) -> (boolean, boolean), + CollapsingHeader : (string) -> boolean, + Columns : (number) -> (), + DragFloat : (string, number) -> (boolean, number), + DragInt : (string, number) -> (boolean, number), + Dummy : (number, number) -> (), + End : () -> (), + EndChildFrame : () -> (), + EndCombo : () -> (), + EndPopup : () -> (), + GetColumnWidth : (number) -> number, + GetDisplayWidth : () -> number, + GetDisplayHeight : () -> number, + GetWindowWidth : () -> (), + GetWindowHeight : () -> (), + GetWindowPos : () -> any, + Indent : (number) -> (), + InputTextMultiline : (string, string) -> (boolean, string), + IsItemHovered : () -> boolean, + IsMouseClicked : (number) -> boolean, + IsMouseDown : (number) -> boolean, + LabelText : (string, string) -> (), + NewLine : () -> (), + NextColumn : () -> (), + OpenPopup : (string) -> (), + PopItemWidth : () -> (), + PopID : () -> (), + PopStyleColor : (number) -> (), + PopStyleVar : (number) -> (), + PopItemWidth : () -> (), + PushItemWidth : (number) -> (), + PushID : (number) -> (), + PushStyleColor : (number, any) -> (), + PushStyleVar : (number, number, number) -> () | (number, number) -> () , + Rect : (number, number, number) -> (), SameLine : () -> (), - InputTextMultiline : (string, string) -> (boolean, string) + Selectable : (string, boolean) -> boolean | (string) -> boolean, + Separator : () -> (), + SetCursorScreenPos : (number, number) -> (), + SetNextWindowPos : (number, number) -> (), + SetNextWindowPosCenter : () -> (), + SetNextWindowSize : (number, number) -> (), + SetStyleColor : (number, any) -> (), + SliderFloat : (string, number, number, number) -> (boolean, number), + Text : (string) -> (), + Unindent : (number) -> (), } declare class World @@ -67,6 +113,8 @@ declare LumixReflection: { getFunction : (ComponentBase, number) -> FunctionBase, getFunctionName : (FunctionBase) -> string, getFunctionArgCount : (FunctionBase) -> number, + getFunctionArgType : (FunctionBase, number) -> string, + getFunctionReturnType : (FunctionBase) -> string, getPropertyType : (PropertyBase) -> number, getPropertyName : (PropertyBase) -> string } @@ -99,7 +147,7 @@ type ButtonInputEvent = { export type InputEvent = ButtonInputEvent | AxisInputEvent ]] -if true then +if false then function typeToString(type : number) : string if type < 3 then return "number" end if type == 3 then return "Entity" end @@ -114,6 +162,13 @@ if true then return string.lower(s) end + function toLuaType(ctype : string) + if ctype == "float" then return "number" end + if ctype == "bool" then return "boolean" end + if ctype == "void" then return "()" end + return "any" + end + function refl() local num_cmp = LumixReflection.getNumComponents() local out = "" @@ -128,7 +183,7 @@ if true then local prop = LumixReflection.getProperty(cmp, j - 1) local prop_name = LumixReflection.getPropertyName(prop) local prop_type = LumixReflection.getPropertyType(prop) - if prop_name:match("3.*") then continue end + if prop_name:match("[0-9].*") then continue end out = out .. "\t" .. toLuaIdentifier(prop_name) .. ": " .. typeToString(prop_type) .. "\n" end local num_funcs = LumixReflection.getNumFunctions(cmp) @@ -136,11 +191,12 @@ if true then local func = LumixReflection.getFunction(cmp, j - 1) local func_name = LumixReflection.getFunctionName(func) local arg_count = LumixReflection.getFunctionArgCount(func) + local ret_type = LumixReflection.getFunctionReturnType(func) out = out .. `\t{func_name} : ({name}` for i = 2, arg_count do - out = out .. ", any" + out = out .. ", " .. toLuaType(LumixReflection.getFunctionArgType(func, i - 1)) end - out = out .. ") -> any\n" + out = out .. `) -> {toLuaType(ret_type)}\n` end out = out .. "end\n\n" end diff --git a/data/scripts/lumix.d.lua b/data/scripts/lumix.d.lua index 170ad883cd..02c2305b11 100644 --- a/data/scripts/lumix.d.lua +++ b/data/scripts/lumix.d.lua @@ -1,10 +1,56 @@ declare ImGui: { + AlignTextToFramePadding : () -> (), Begin : (string) -> boolean, - End : () -> (), - Text : (string) -> (), + BeginChildFrame : (string, number, number) -> boolean, + BeginPopup : (string) -> boolean, Button : (string) -> boolean, + CalcTextSize : (string) -> (number, number), + Checkbox : (string, boolean) -> (boolean, boolean), + CollapsingHeader : (string) -> boolean, + Columns : (number) -> (), + DragFloat : (string, number) -> (boolean, number), + DragInt : (string, number) -> (boolean, number), + Dummy : (number, number) -> (), + End : () -> (), + EndChildFrame : () -> (), + EndCombo : () -> (), + EndPopup : () -> (), + GetColumnWidth : (number) -> number, + GetDisplayWidth : () -> number, + GetDisplayHeight : () -> number, + GetWindowWidth : () -> (), + GetWindowHeight : () -> (), + GetWindowPos : () -> any, + Indent : (number) -> (), + InputTextMultiline : (string, string) -> (boolean, string), + IsItemHovered : () -> boolean, + IsMouseClicked : (number) -> boolean, + IsMouseDown : (number) -> boolean, + LabelText : (string, string) -> (), + NewLine : () -> (), + NextColumn : () -> (), + OpenPopup : (string) -> (), + PopItemWidth : () -> (), + PopID : () -> (), + PopStyleColor : (number) -> (), + PopStyleVar : (number) -> (), + PopItemWidth : () -> (), + PushItemWidth : (number) -> (), + PushID : (number) -> (), + PushStyleColor : (number, any) -> (), + PushStyleVar : (number, number, number) -> () | (number, number) -> () , + Rect : (number, number, number) -> (), SameLine : () -> (), - InputTextMultiline : (string, string) -> (boolean, string) + Selectable : (string, boolean) -> boolean | (string) -> boolean, + Separator : () -> (), + SetCursorScreenPos : (number, number) -> (), + SetNextWindowPos : (number, number) -> (), + SetNextWindowPosCenter : () -> (), + SetNextWindowSize : (number, number) -> (), + SetStyleColor : (number, any) -> (), + SliderFloat : (string, number, number, number) -> (boolean, number), + Text : (string) -> (), + Unindent : (number) -> (), } declare class World @@ -47,8 +93,8 @@ declare class terrain tesselation: number grid_resolution: number grass: any - getTerrainNormalAt : (terrain, any, any) -> any - getTerrainHeightAt : (terrain, any, any) -> any + getTerrainNormalAt : (terrain, number, number) -> any + getTerrainHeightAt : (terrain, number, number) -> number end declare class camera @@ -69,8 +115,6 @@ declare class curve_decal material: string half_extents: number uv_scale: any - bezier_p0: any - bezier_p2: any end declare class point_light @@ -141,12 +185,12 @@ declare class rigid_actor sphere_geometry: any mesh: string material: string - putToSleep : (rigid_actor) -> any - getSpeed : (rigid_actor) -> any + putToSleep : (rigid_actor) -> () + getSpeed : (rigid_actor) -> number getVelocity : (rigid_actor) -> any - applyForce : (rigid_actor, any) -> any - applyImpulse : (rigid_actor, any) -> any - addForceAtPos : (rigid_actor, any, any) -> any + applyForce : (rigid_actor, any) -> () + applyImpulse : (rigid_actor, any) -> () + addForceAtPos : (rigid_actor, any, any) -> () end declare class physical_heightfield @@ -163,9 +207,9 @@ declare class physical_controller use_root_motion: boolean use_custom_gravity: boolean custom_gravity_acceleration: number - move : (physical_controller, any) -> any - isCollisionDown : (physical_controller) -> any - getGravitySpeed : (physical_controller) -> any + move : (physical_controller, any) -> () + isCollisionDown : (physical_controller) -> boolean + getGravitySpeed : (physical_controller) -> number end declare class js_script @@ -237,8 +281,6 @@ declare class d6_joint x_motion: number y_motion: number z_motion: number - swing_1: number - swing_2: number twist: number linear_limit: number swing_limit: any @@ -258,9 +300,9 @@ declare class vehicle chassis: string chassis_layer: number wheels_layer: number - setAccel : (vehicle, any) -> any - setSteer : (vehicle, any) -> any - setBrake : (vehicle, any) -> any + setAccel : (vehicle, number) -> () + setSteer : (vehicle, number) -> () + setBrake : (vehicle, number) -> () end declare class wheel @@ -281,10 +323,10 @@ declare class navmesh_agent height: number move_entity: boolean speed: number - setActive : (navmesh_agent, any) -> any - navigate : (navmesh_agent, any, any, any) -> any - cancelNavigation : (navmesh_agent) -> any - drawPath : (navmesh_agent) -> any + setActive : (navmesh_agent, boolean) -> () + navigate : (navmesh_agent, any, number, number) -> boolean + cancelNavigation : (navmesh_agent) -> () + drawPath : (navmesh_agent) -> () end declare class navmesh_zone @@ -297,11 +339,11 @@ declare class navmesh_zone max_climb: number autoload: boolean detailed: boolean - load : (navmesh_zone) -> any - drawContours : (navmesh_zone) -> any - drawNavmesh : (navmesh_zone, any, any, any, any) -> any - drawCompactHeightfield : (navmesh_zone) -> any - drawHeightfield : (navmesh_zone) -> any + load : (navmesh_zone) -> boolean + drawContours : (navmesh_zone) -> () + drawNavmesh : (navmesh_zone, any, boolean, boolean, boolean) -> () + drawCompactHeightfield : (navmesh_zone) -> () + drawHeightfield : (navmesh_zone) -> () generateNavmesh : (navmesh_zone) -> any end @@ -325,8 +367,8 @@ declare class animator source: string default_set: number use_root_motion: boolean - setFloatInput : (animator, any, any) -> any - setBoolInput : (animator, any, any) -> any + setFloatInput : (animator, any, number) -> () + setBoolInput : (animator, any, boolean) -> () getInputIndex : (animator, any) -> any end @@ -345,8 +387,8 @@ end declare class ambient_sound sound: string - pause : (ambient_sound) -> any - resume : (ambient_sound) -> any + pause : (ambient_sound) -> () + resume : (ambient_sound) -> () end declare class echo_zone @@ -457,6 +499,8 @@ declare LumixReflection: { getFunction : (ComponentBase, number) -> FunctionBase, getFunctionName : (FunctionBase) -> string, getFunctionArgCount : (FunctionBase) -> number, + getFunctionArgType : (FunctionBase, number) -> string, + getFunctionReturnType : (FunctionBase) -> string, getPropertyType : (PropertyBase) -> number, getPropertyName : (PropertyBase) -> string } @@ -487,4 +531,3 @@ type ButtonInputEvent = { } export type InputEvent = ButtonInputEvent | AxisInputEvent - diff --git a/src/engine/lua_api.cpp b/src/engine/lua_api.cpp index 87f6a6119b..2740513912 100644 --- a/src/engine/lua_api.cpp +++ b/src/engine/lua_api.cpp @@ -59,16 +59,13 @@ int DragInt(lua_State* L) } -int PushStyleColor(lua_State* L) -{ - int var = LuaWrapper::checkArg(L, 1); +void PushStyleColor(i32 var, const Vec4& color) { ImVec4 v; - v.x = LuaWrapper::checkArg(L, 2); - v.y = LuaWrapper::checkArg(L, 3); - v.z = LuaWrapper::checkArg(L, 4); - v.w = LuaWrapper::checkArg(L, 5); + v.x = color.x; + v.y = color.y; + v.z = color.z; + v.w = color.w; ImGui::PushStyleColor(var, v); - return 0; } @@ -91,25 +88,17 @@ int PushStyleVar(lua_State* L) } -int PushID(lua_State* L) -{ - int id = LuaWrapper::checkArg(L, 1); - ImGui::PushID(id); - return 0; -} +void PushID(i32 id) { ImGui::PushID(id); } -int SetStyleColor(lua_State* L) -{ - auto& style = ImGui::GetStyle(); - int index = LuaWrapper::checkArg(L, 1); - ImVec4 color; - color.x = LuaWrapper::checkArg(L, 2); - color.y = LuaWrapper::checkArg(L, 3); - color.z = LuaWrapper::checkArg(L, 4); - color.w = LuaWrapper::checkArg(L, 5); - style.Colors[index] = color; - return 0; +void SetStyleColor(int color_index, const Vec4& color) { + ImGuiStyle& style = ImGui::GetStyle(); + ImVec4 v; + v.x = color.x; + v.y = color.y; + v.z = color.z; + v.w = color.w; + style.Colors[color_index] = v; } @@ -134,12 +123,8 @@ int Text(lua_State* L) } -int LabelText(lua_State* L) -{ - auto* label = LuaWrapper::checkArg(L, 1); - auto* text = LuaWrapper::checkArg(L, 2); +void LabelText(const char* label, const char* text) { ImGui::LabelText(label, "%s", text); - return 0; } @@ -188,11 +173,9 @@ int Checkbox(lua_State* L) } -int GetWindowPos(lua_State* L) -{ +Vec2 GetWindowPos() { ImVec2 pos = ImGui::GetWindowPos(); - LuaWrapper::push(L, Vec2(pos.x, pos.y)); - return 1; + return Vec2(pos.x, pos.y); } @@ -206,13 +189,6 @@ int SetNextWindowPos(lua_State* L) } -int AlignTextToFramePadding(lua_State* L) -{ - ImGui::AlignTextToFramePadding(); - return 0; -} - - int Selectable(lua_State* L) { auto* label = LuaWrapper::checkArg(L, 1); @@ -237,13 +213,6 @@ int SetCursorScreenPos(lua_State* L) } -int Separator(lua_State* L) -{ - ImGui::Separator(); - return 0; -} - - void Rect(float w, float h, u32 color) { ImGuiEx::Rect(w, h, color); @@ -289,11 +258,7 @@ int SetNextWindowSize(float w, float h) } -int OpenPopup(lua_State* L) { - auto* str_id = LuaWrapper::checkArg(L, 1); - ImGui::OpenPopup(str_id); - return 0; -} +void OpenPopup(const char* str_id) { ImGui::OpenPopup(str_id); } int Begin(lua_State* L) { @@ -332,37 +297,8 @@ int BeginPopup(lua_State* L) return 1; } - -int GetDisplayWidth(lua_State* L) -{ - float w = ImGui::GetIO().DisplaySize.x; - LuaWrapper::push(L, w); - return 1; -} - - -int GetDisplayHeight(lua_State* L) -{ - float w = ImGui::GetIO().DisplaySize.y; - LuaWrapper::push(L, w); - return 1; -} - - -int GetWindowWidth(lua_State* L) -{ - float w = ImGui::GetWindowWidth(); - LuaWrapper::push(L, w); - return 1; -} - - -int GetWindowHeight(lua_State* L) -{ - float w = ImGui::GetWindowHeight(); - LuaWrapper::push(L, w); - return 1; -} +float GetDisplayWidth() { return ImGui::GetIO().DisplaySize.x; } +float GetDisplayHeight() { return ImGui::GetIO().DisplaySize.y; } int SameLine(lua_State* L) @@ -536,6 +472,33 @@ static u32 LUA_getFunctionArgCount(reflection::FunctionBase* fnc) { return fnc->getArgCount(); } +static i32 LUA_getFunctionReturnType(lua_State* L) { + auto* fnc = LuaWrapper::checkArg(L, 1); + StringView name = fnc->getReturnTypeName(); + lua_pushlstring(L, name.begin, name.size()); + return 1; +} + +static const char* LUA_getFunctionArgType(reflection::FunctionBase* fnc, u32 arg_idx) { + const reflection::TypeDescriptor type = fnc->getArgType(arg_idx); + switch (type.type) { + case reflection::Variant::VOID: return "void"; + case reflection::Variant::PTR: return "void*"; + case reflection::Variant::BOOL: return "bool"; + case reflection::Variant::I32: return "i32"; + case reflection::Variant::U32: return "u32"; + case reflection::Variant::FLOAT: return "float"; + case reflection::Variant::CSTR: return "const char*"; + case reflection::Variant::ENTITY: return "EntityPtr"; + case reflection::Variant::VEC2: return "Vec2"; + case reflection::Variant::VEC3: return "Vec3"; + case reflection::Variant::DVEC3: return "DVec3"; + case reflection::Variant::COLOR: return "Color"; + } + ASSERT(false); + return ""; +} + static const char* LUA_getPropertyName(reflection::PropertyBase* prop) { return prop->name; } @@ -908,6 +871,8 @@ void registerEngineAPI(lua_State* L, Engine* engine) LuaWrapper::createSystemFunction(L, "LumixReflection", "getFunction", &LuaWrapper::wrap); LuaWrapper::createSystemFunction(L, "LumixReflection", "getFunctionName", &LuaWrapper::wrap); LuaWrapper::createSystemFunction(L, "LumixReflection", "getFunctionArgCount", &LuaWrapper::wrap); + LuaWrapper::createSystemFunction(L, "LumixReflection", "getFunctionReturnType", &LUA_getFunctionReturnType); + LuaWrapper::createSystemFunction(L, "LumixReflection", "getFunctionArgType", &LuaWrapper::wrap); LuaWrapper::createSystemFunction(L, "LumixReflection", "getPropertyType", &LuaWrapper::wrap); LuaWrapper::createSystemFunction(L, "LumixReflection", "getPropertyName", &LuaWrapper::wrap); @@ -946,7 +911,7 @@ void registerEngineAPI(lua_State* L, Engine* engine) LuaWrapper::createSystemVariable(L, "ImGui", "StyleVar_ItemSpacing", ImGuiStyleVar_ItemSpacing); LuaWrapper::createSystemVariable(L, "ImGui", "StyleVar_ItemInnerSpacing", ImGuiStyleVar_ItemInnerSpacing); LuaWrapper::createSystemVariable(L, "ImGui", "StyleVar_WindowPadding", ImGuiStyleVar_WindowPadding); - LuaImGui::registerCFunction(L, "AlignTextToFramePadding", &LuaImGui::AlignTextToFramePadding); + LuaImGui::registerCFunction(L, "AlignTextToFramePadding", &LuaWrapper::wrap); LuaImGui::registerCFunction(L, "Begin", &LuaImGui::Begin); LuaImGui::registerCFunction(L, "BeginChildFrame", &LuaImGui::BeginChildFrame); LuaImGui::registerCFunction(L, "BeginPopup", LuaImGui::BeginPopup); @@ -963,11 +928,11 @@ void registerEngineAPI(lua_State* L, Engine* engine) LuaImGui::registerCFunction(L, "EndCombo", &LuaWrapper::wrap<&ImGui::EndCombo>); LuaImGui::registerCFunction(L, "EndPopup", &LuaWrapper::wrap<&ImGui::EndPopup>); LuaImGui::registerCFunction(L, "GetColumnWidth", &LuaWrapper::wrap<&ImGui::GetColumnWidth>); - LuaImGui::registerCFunction(L, "GetDisplayWidth", &LuaImGui::GetDisplayWidth); - LuaImGui::registerCFunction(L, "GetDisplayHeight", &LuaImGui::GetDisplayHeight); - LuaImGui::registerCFunction(L, "GetWindowWidth", &LuaImGui::GetWindowWidth); - LuaImGui::registerCFunction(L, "GetWindowHeight", &LuaImGui::GetWindowHeight); - LuaImGui::registerCFunction(L, "GetWindowPos", &LuaImGui::GetWindowPos); + LuaImGui::registerCFunction(L, "GetDisplayWidth", &LuaWrapper::wrap); + LuaImGui::registerCFunction(L, "GetDisplayHeight", &LuaWrapper::wrap); + LuaImGui::registerCFunction(L, "GetWindowWidth", &LuaWrapper::wrap); + LuaImGui::registerCFunction(L, "GetWindowHeight", &LuaWrapper::wrap); + LuaImGui::registerCFunction(L, "GetWindowPos", &LuaWrapper::wrap); LuaImGui::registerCFunction(L, "Indent", &LuaWrapper::wrap<&ImGui::Indent>); LuaImGui::registerCFunction(L, "InputTextMultiline", &LuaImGui::InputTextMultiline); LuaImGui::registerCFunction(L, "IsItemHovered", &LuaWrapper::wrap<&LuaImGui::IsItemHovered>); @@ -975,28 +940,28 @@ void registerEngineAPI(lua_State* L, Engine* engine) LuaImGui::registerCFunction(L, "IsMouseDown", &LuaWrapper::wrap<&LuaImGui::IsMouseDown>); LuaImGui::registerCFunction(L, "NewLine", &LuaWrapper::wrap<&ImGui::NewLine>); LuaImGui::registerCFunction(L, "NextColumn", &LuaWrapper::wrap<&ImGui::NextColumn>); - LuaImGui::registerCFunction(L, "OpenPopup", &LuaImGui::OpenPopup); + LuaImGui::registerCFunction(L, "OpenPopup", &LuaWrapper::wrap); LuaImGui::registerCFunction(L, "PopItemWidth", &LuaWrapper::wrap<&ImGui::PopItemWidth>); LuaImGui::registerCFunction(L, "PopID", &LuaWrapper::wrap<&ImGui::PopID>); LuaImGui::registerCFunction(L, "PopStyleColor", &LuaWrapper::wrap<&ImGui::PopStyleColor>); LuaImGui::registerCFunction(L, "PopStyleVar", &LuaWrapper::wrap<&ImGui::PopStyleVar>); LuaImGui::registerCFunction(L, "PushItemWidth", &LuaWrapper::wrap<&ImGui::PushItemWidth>); - LuaImGui::registerCFunction(L, "PushID", &LuaImGui::PushID); - LuaImGui::registerCFunction(L, "PushStyleColor", &LuaImGui::PushStyleColor); + LuaImGui::registerCFunction(L, "PushID", &LuaWrapper::wrap); + LuaImGui::registerCFunction(L, "PushStyleColor", &LuaWrapper::wrap); LuaImGui::registerCFunction(L, "PushStyleVar", &LuaImGui::PushStyleVar); LuaImGui::registerCFunction(L, "Rect", &LuaWrapper::wrap<&LuaImGui::Rect>); LuaImGui::registerCFunction(L, "SameLine", &LuaImGui::SameLine); LuaImGui::registerCFunction(L, "Selectable", &LuaImGui::Selectable); - LuaImGui::registerCFunction(L, "Separator", &LuaImGui::Separator); + LuaImGui::registerCFunction(L, "Separator", &LuaWrapper::wrap); LuaImGui::registerCFunction(L, "SetCursorScreenPos", &LuaImGui::SetCursorScreenPos); LuaImGui::registerCFunction(L, "SetNextWindowPos", &LuaImGui::SetNextWindowPos); LuaImGui::registerCFunction(L, "SetNextWindowPosCenter", &LuaImGui::SetNextWindowPosCenter); LuaImGui::registerCFunction(L, "SetNextWindowSize", &LuaWrapper::wrap<&LuaImGui::SetNextWindowSize>); - LuaImGui::registerCFunction(L, "SetStyleColor", &LuaImGui::SetStyleColor); + LuaImGui::registerCFunction(L, "SetStyleColor", &LuaWrapper::wrap); LuaImGui::registerCFunction(L, "SliderFloat", &LuaImGui::SliderFloat); LuaImGui::registerCFunction(L, "Text", &LuaImGui::Text); LuaImGui::registerCFunction(L, "Unindent", &LuaWrapper::wrap<&ImGui::Unindent>); - LuaImGui::registerCFunction(L, "LabelText", &LuaImGui::LabelText); + LuaImGui::registerCFunction(L, "LabelText", &LuaWrapper::wrap); LuaWrapper::createSystemVariable(L, "LumixAPI", "INPUT_DEVICE_KEYBOARD", InputSystem::Device::KEYBOARD); LuaWrapper::createSystemVariable(L, "LumixAPI", "INPUT_DEVICE_MOUSE", InputSystem::Device::MOUSE);