Skip to content

Commit

Permalink
make imgui lua api more similar to native api
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Jan 18, 2024
1 parent 8061182 commit f775c00
Show file tree
Hide file tree
Showing 36 changed files with 961 additions and 991 deletions.
4 changes: 2 additions & 2 deletions clibs/imgui/imgui_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ void imgui_enum_init(lua_State* L) {
flag_gen(L, "Config", eConfigFlags);
flag_gen(L, "ColorEdit", eColorEditFlags);
flag_gen(L, "Slider", eSliderFlags);
lua_setfield(L, -2, "flags");
lua_setfield(L, -2, "Flags");

lua_newtable(L);
enum_gen(L, "TableBgTarget", eTableBgTarget);
Expand All @@ -681,5 +681,5 @@ void imgui_enum_init(lua_State* L) {
enum_gen(L, "MouseButton", eMouseButton);
enum_gen(L, "MouseCursor", eMouseCursor);
enum_gen(L, "Mod", eMod);
lua_setfield(L, -2, "enum");
lua_setfield(L, -2, "Enum");
}
113 changes: 42 additions & 71 deletions clibs/imgui/luaimgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,7 @@ read_field_vec4(lua_State *L, const char *field, ImVec4 def_val, int tidx = INDE
return def_val;
}


static int dSpace(lua_State* L) {
static int dDockSpace(lua_State* L) {
const char* str_id = luaL_checkstring(L, 1);
auto flags = lua_getflags<ImGuiDockNodeFlags>(L, 2);
float w = (float)luaL_optnumber(L, 3, 0);
Expand All @@ -338,7 +337,7 @@ static int dSpace(lua_State* L) {
return 0;
}

static int dBuilderGetCentralRect(lua_State * L) {
static int dDockBuilderGetCentralRect(lua_State * L) {
const char* str_id = luaL_checkstring(L, 1);
ImGuiDockNode* central_node = ImGui::DockBuilderGetCentralNode(ImGui::GetID(str_id));
lua_pushnumber(L, central_node->Pos.x);
Expand Down Expand Up @@ -2989,8 +2988,7 @@ static void ImGuiFree(void* ptr, void* /*user_data*/) {
free(ptr);
}

static int
lmemory(lua_State* L) {
static int util_memory(lua_State* L) {
lua_pushinteger(L, allocator_memory);
return 1;
}
Expand Down Expand Up @@ -3019,49 +3017,6 @@ luaopen_imgui(lua_State *L) {
{ "GetMainViewport", lGetMainViewport },
{ "InitFont", lInitFont },
{ "GetMouseCursor", lGetMouseCursor },
{ "memory", lmemory },
{ NULL, NULL },
};
luaL_newlib(L, l);

luaL_Reg io[] = {
{ "AddMouseButtonEvent", ioAddMouseButtonEvent },
{ "AddMouseWheelEvent", ioAddMouseWheelEvent },
{ "AddKeyEvent", ioAddKeyEvent },
{ "AddInputCharacter", ioAddInputCharacter },
{ "AddInputCharacterUTF16", ioAddInputCharacterUTF16 },
{ "AddFocusEvent", ioAddFocusEvent },
{ NULL, NULL },
};
luaL_Reg io_setter[] = {
{ "ConfigFlags", ioSetterConfigFlags },
{ NULL, NULL },
};
luaL_Reg io_getter[] = {
{ "WantCaptureMouse", ioGetterWantCaptureMouse },
{ "WantCaptureKeyboard", ioGetterWantCaptureKeyboard },
{ NULL, NULL },
};
luaL_newlib(L, io);
lua_newtable(L);
luaL_newlib(L, io_setter);
lua_pushcclosure(L, ioSetter, 1);
lua_setfield(L, -2, "__newindex");
luaL_newlib(L, io_getter);
lua_pushcclosure(L, ioGetter, 1);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "io");

luaL_Reg dock[] = {
{ "Space", dSpace },
{ "BuilderGetCentralRect", dBuilderGetCentralRect },
{ NULL, NULL },
};
luaL_newlib(L, dock);
lua_setfield(L, -2, "dock");

luaL_Reg widgets[] = {
{ "Button", wButton },
{ "SmallButton", wSmallButton },
{ "InvisibleButton", wInvisibleButton },
Expand Down Expand Up @@ -3124,12 +3079,6 @@ luaopen_imgui(lua_State *L) {
{ "PushTextWrapPos", wPushTextWrapPos },
{ "PopTextWrapPos", wPopTextWrapPos },
{ "SelectableInput", wSelectableInput },
{ NULL, NULL },
};
luaL_newlib(L, widgets);
lua_setfield(L, -2, "widget");

luaL_Reg cursor[] = {
{ "Separator", cSeparator },
{ "SameLine", cSameLine },
{ "NewLine", cNewLine },
Expand All @@ -3154,13 +3103,6 @@ luaopen_imgui(lua_State *L) {
{ "PushItemWidth", cPushItemWidth},
{ "PopItemWidth", cPopItemWidth},
{ "SetMouseCursor", cSetMouseCursor },
{ NULL, NULL },
};

luaL_newlib(L, cursor);
lua_setfield(L, -2, "cursor");

luaL_Reg windows[] = {
{ "Begin", winBegin },
{ "End", winEnd },
{ "BeginDisabled", winBeginDisabled },
Expand Down Expand Up @@ -3213,15 +3155,6 @@ luaopen_imgui(lua_State *L) {
{ "PopStyleColor", winPopStyleColor },
{ "PushStyleVar", winPushStyleVar },
{ "PopStyleVar", winPopStyleVar },
{ NULL, NULL },
};

luaL_newlib(L, windows);
lua_setfield(L, -2, "windows");

imgui::table::init(L);

luaL_Reg util[] = {
{ "SetColorEditOptions", uSetColorEditOptions },
{ "PushClipRect", uPushClipRect },
{ "PopClipRect", uPopClipRect },
Expand Down Expand Up @@ -3256,12 +3189,50 @@ luaopen_imgui(lua_State *L) {
{ "IsMouseDragging", cIsMouseDragging },
{ "GetMousePos", cGetMousePos },
{ "SetClipboardText", cSetClipboardText },
{ "DockSpace", dDockSpace },
{ "DockBuilderGetCentralRect", dDockBuilderGetCentralRect },
{ NULL, NULL },
};
luaL_newlib(L, l);

luaL_Reg util[] = {
{ "memory", util_memory },
{ NULL, NULL },
};
luaL_newlib(L, util);
lua_setfield(L, -2, "util");

imgui_enum_init(L);
imgui::table::init(L);

luaL_Reg io[] = {
{ "AddMouseButtonEvent", ioAddMouseButtonEvent },
{ "AddMouseWheelEvent", ioAddMouseWheelEvent },
{ "AddKeyEvent", ioAddKeyEvent },
{ "AddInputCharacter", ioAddInputCharacter },
{ "AddInputCharacterUTF16", ioAddInputCharacterUTF16 },
{ "AddFocusEvent", ioAddFocusEvent },
{ NULL, NULL },
};
luaL_Reg io_setter[] = {
{ "ConfigFlags", ioSetterConfigFlags },
{ NULL, NULL },
};
luaL_Reg io_getter[] = {
{ "WantCaptureMouse", ioGetterWantCaptureMouse },
{ "WantCaptureKeyboard", ioGetterWantCaptureKeyboard },
{ NULL, NULL },
};
luaL_newlib(L, io);
lua_newtable(L);
luaL_newlib(L, io_setter);
lua_pushcclosure(L, ioSetter, 1);
lua_setfield(L, -2, "__newindex");
luaL_newlib(L, io_getter);
lua_pushcclosure(L, ioGetter, 1);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "io");

imgui_enum_init(L);
return 1;
}
41 changes: 20 additions & 21 deletions clibs/imgui/luaimgui_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static int ClipperBegin(lua_State* L) {
return 4;
}

static int Clipper(lua_State* L) {
static int ListClipper(lua_State* L) {
ImGuiListClipper* clipper = (ImGuiListClipper*)lua_newuserdatauv(L, sizeof(ImGuiListClipper), 0);
new (clipper) ImGuiListClipper;
if (luaL_newmetatable(L, "IMGUI_CLIPPER")) {
Expand All @@ -205,26 +205,25 @@ static int Clipper(lua_State* L) {

void init(lua_State* L) {
luaL_Reg table[] = {
{ "Begin", BeginTable },
{ "End", EndTable },
{ "NextRow", TableNextRow },
{ "NextColumn", TableNextColumn },
{ "SetColumnIndex", TableSetColumnIndex },
{ "GetColumnIndex", TableGetColumnIndex },
{ "GetRowIndex", TableGetRowIndex },
{ "SetupColumn", TableSetupColumn },
{ "SetupScrollFreeze", TableSetupScrollFreeze },
{ "HeadersRow", TableHeadersRow },
{ "Header", TableHeader },
{ "GetColumnCount", TableGetColumnCount },
{ "GetColumnName", TableGetColumnName },
{ "GetColumnFlags", TableGetColumnFlags },
{ "SetColumnEnabled", TableSetColumnEnabled },
{ "GetSortSpecs", TableGetSortSpecs },
{ "SetBgColor", TableSetBgColor },
{ "Clipper", Clipper },
{ "TableBegin", BeginTable },
{ "TableEnd", EndTable },
{ "TableNextRow", TableNextRow },
{ "TableNextColumn", TableNextColumn },
{ "TableSetColumnIndex", TableSetColumnIndex },
{ "TableGetColumnIndex", TableGetColumnIndex },
{ "TableGetRowIndex", TableGetRowIndex },
{ "TableSetupColumn", TableSetupColumn },
{ "TableSetupScrollFreeze", TableSetupScrollFreeze },
{ "TableHeadersRow", TableHeadersRow },
{ "TableHeader", TableHeader },
{ "TableGetColumnCount", TableGetColumnCount },
{ "TableGetColumnName", TableGetColumnName },
{ "TableGetColumnFlags", TableGetColumnFlags },
{ "TableSetColumnEnabled", TableSetColumnEnabled },
{ "TableGetSortSpecs", TableGetSortSpecs },
{ "TableSetBgColor", TableSetBgColor },
{ "ListClipper", ListClipper },
{ NULL, NULL },
};
luaL_newlib(L, table);
lua_setfield(L, -2, "table");
luaL_setfuncs(L, table, 0);
}}
32 changes: 16 additions & 16 deletions pkg/ant.imgui/imgui_system.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local ecs = ...

local platform = require "bee.platform"
local imgui = import_package "ant.imgui"
local ImGui = import_package "ant.imgui"
local rhwi = import_package "ant.hwi"
local assetmgr = import_package "ant.asset"
local inputmgr = import_package "ant.inputmgr"
Expand All @@ -13,48 +13,48 @@ local ServiceWindow = ltask.queryservice "ant.window|window"
local m = ecs.system 'imgui_system'

function m:init_system()
imgui.CreateContext()
imgui.io.ConfigFlags = imgui.flags.Config {
ImGui.CreateContext()
ImGui.io.ConfigFlags = ImGui.Flags.Config {
"NavEnableKeyboard",
"DockingEnable",
"NavNoCaptureKeyboard",
"DpiEnableScaleViewports",
"DpiEnableScaleFonts",
"NoMouseCursorChange",
}
imgui.InitPlatform(rhwi.native_window())
ImGui.InitPlatform(rhwi.native_window())

local imgui_font = assetmgr.load_material "/pkg/ant.imgui/materials/font.material"
local imgui_image = assetmgr.load_material "/pkg/ant.imgui/materials/image.material"
assetmgr.material_mark(imgui_font.fx.prog)
assetmgr.material_mark(imgui_image.fx.prog)
local viewId = rhwi.viewid_generate("imgui_eidtor" .. 1, "uiruntime")
imgui.InitRender {
ImGui.InitRender {
fontProg = PM.program_get(imgui_font.fx.prog),
imageProg = PM.program_get(imgui_image.fx.prog),
fontUniform = imgui_font.fx.uniforms.s_tex.handle,
imageUniform = imgui_image.fx.uniforms.s_tex.handle,
viewIdPool = { viewId },
}
if platform.os == "windows" then
imgui.FontAtlasAddFont {
ImGui.FontAtlasAddFont {
SystemFont = "Segoe UI Emoji",
SizePixels = 18,
GlyphRanges = { 0x23E0, 0x329F, 0x1F000, 0x1FA9F }
}
imgui.FontAtlasAddFont {
ImGui.FontAtlasAddFont {
SystemFont = "黑体",
SizePixels = 18,
GlyphRanges = { 0x0020, 0xFFFF }
}
elseif platform.os == "macos" then
imgui.FontAtlasAddFont {
ImGui.FontAtlasAddFont {
SystemFont = "苹方-简",
SizePixels = 18,
GlyphRanges = { 0x0020, 0xFFFF }
}
else -- iOS
imgui.FontAtlasAddFont {
ImGui.FontAtlasAddFont {
SystemFont = "Heiti SC",
SizePixels = 18,
GlyphRanges = { 0x0020, 0xFFFF }
Expand All @@ -64,26 +64,26 @@ function m:init_system()
end

function m:init_world()
imgui.FontAtlasBuild()
ImGui.FontAtlasBuild()
end

function m:exit()
imgui.DestroyRenderer()
imgui.DestroyPlatform()
imgui.DestroyContext()
ImGui.DestroyRenderer()
ImGui.DestroyPlatform()
ImGui.DestroyContext()
end

local last_cursor = 0

function m:start_frame()
local cursor = imgui.GetMouseCursor()
local cursor = ImGui.GetMouseCursor()
if last_cursor ~= cursor then
last_cursor = cursor
ltask.call(ServiceWindow, "setcursor", cursor)
end
imgui.NewFrame()
ImGui.NewFrame()
end

function m:end_frame()
imgui.Render()
ImGui.Render()
end
8 changes: 4 additions & 4 deletions pkg/ant.imgui/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ function ImGuiEvent.mousewheel(e)
end

function ImGuiEvent.keyboard(e)
ImGuiIO.AddKeyEvent(ImGui.enum.Mod.Ctrl, e.state.CTRL ~= nil);
ImGuiIO.AddKeyEvent(ImGui.enum.Mod.Shift, e.state.SHIFT ~= nil);
ImGuiIO.AddKeyEvent(ImGui.enum.Mod.Alt, e.state.ALT ~= nil);
ImGuiIO.AddKeyEvent(ImGui.enum.Mod.Super, e.state.SYS ~= nil);
ImGuiIO.AddKeyEvent(ImGui.Enum.Mod.Ctrl, e.state.CTRL ~= nil);
ImGuiIO.AddKeyEvent(ImGui.Enum.Mod.Shift, e.state.SHIFT ~= nil);
ImGuiIO.AddKeyEvent(ImGui.Enum.Mod.Alt, e.state.ALT ~= nil);
ImGuiIO.AddKeyEvent(ImGui.Enum.Mod.Super, e.state.SYS ~= nil);
if e.press == 1 then
ImGuiIO.AddKeyEvent(e.key, true);
elseif e.press == 0 then
Expand Down
2 changes: 1 addition & 1 deletion pkg/ant.inputmgr/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local ImGui = require "imgui"

local keymap = {}

for name, index in pairs(ImGui.enum.Key) do
for name, index in pairs(ImGui.Enum.Key) do
keymap[index] = name
end

Expand Down
Loading

0 comments on commit f775c00

Please sign in to comment.