Skip to content

Commit

Permalink
Add menu bar API (#28)
Browse files Browse the repository at this point in the history
* Fix window function names in script API

* Add menu bar API

* Add spacing in menu bar script API

* Add menu bar examples

* Return `selected` value for menu item

Now matches the current checkbox bindings

* Better code for defaulting arguments to `true`
  • Loading branch information
JustAPotota authored May 2, 2022
1 parent 601624a commit 26a411b
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 3 deletions.
84 changes: 83 additions & 1 deletion example/example.script
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,89 @@ local function update_tab5(self)

end

local function main_menu_bar(self)
if imgui.begin_main_menu_bar() then
if imgui.begin_menu("File") then
if imgui.menu_item("Quit", "Ctrl+Q") then
sys.exit(0)
end

imgui.end_menu()
end

if imgui.begin_menu("View") then
local clicked = imgui.menu_item("Show Demo Window", nil, self.show_demo_window)
if clicked then
self.show_demo_window = not self.show_demo_window
end

imgui.end_menu()
end

if imgui.begin_menu("About") then
imgui.menu_item("Defold v" .. sys.get_engine_info().version, nil, nil, false)

imgui.end_menu()
end

imgui.end_main_menu_bar()
end
end

local function nested_menu(self)
if imgui.begin_menu("Nested Menu") then
imgui.menu_item("Nested Item")
nested_menu(self)
imgui.end_menu()
end
end

local function menu_bar(self)
if imgui.begin_menu_bar() then
if imgui.begin_menu("Menu") then
if imgui.menu_item("Menu Item") then
print("Clicked 'Menu Item'")
end

-- Shortcuts are only visual, ImGui doesn't currently use them
imgui.menu_item("Has Shortcut", "Ctrl+A")

local changed, active = imgui.menu_item("Toggleable Item", nil, self.toggle_item_active)
if changed then
self.toggle_item_active = active
print("'Toggleable Item' is " .. (self.toggle_item_active and "on" or "off"))
end

imgui.menu_item("Disabled Item", nil, nil, false)

if imgui.begin_menu("Nested Menu") then
imgui.menu_item("Item")
nested_menu(self)
imgui.end_menu()
end

if imgui.menu_item("Click Me!") then
sys.exit(0)
end

imgui.end_menu()
end

if imgui.begin_menu("Other Menu") then
if imgui.menu_item("Other Menu Item") then
print("Clicked 'Other Menu Item'")
end
imgui.end_menu()
end

imgui.end_menu_bar()
end
end


function update(self, dt)
main_menu_bar(self)

if self.show_demo_window then
imgui.demo()
end
Expand All @@ -336,7 +417,8 @@ function update(self, dt)
close_window(self)
end

imgui.begin_window("Hello, world!")
imgui.begin_window("Hello, world!", nil, imgui.WINDOWFLAGS_MENUBAR)
menu_bar(self)
imgui.begin_tab_bar("tabs")

local tab1_open = imgui.begin_tab_item("Tab1")
Expand Down
90 changes: 88 additions & 2 deletions imgui/api/imgui.script_api
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@

#*****************************************************************************************************

- name: window_begin
- name: begin_window
type: function

parameters:
Expand All @@ -291,7 +291,7 @@

#*****************************************************************************************************

- name: window_end
- name: end_window
type: function

#*****************************************************************************************************
Expand Down Expand Up @@ -812,6 +812,92 @@
- name: pushed
type: boolean

#*****************************************************************************************************
#***** MENU BAR **************************************************************************************
#*****************************************************************************************************

- name: begin_menu_bar
type: function


returns:
- name: result
type: boolean

#*****************************************************************************************************

- name: end_menu_bar
type: function

#*****************************************************************************************************

- name: begin_main_menu_bar
type: function


returns:
- name: result
type: boolean

#*****************************************************************************************************

- name: end_main_menu_bar
type: function

#*****************************************************************************************************

- name: begin_menu
type: function


parameters:
- name: label
type: string

- name: enabled
optional: true
type: bool


returns:
- name: result
type: boolean

#*****************************************************************************************************

- name: end_menu
type: function

#*****************************************************************************************************

- name: menu_item
type: function


parameters:
- name: label
type: string

- name: shortcut
type: string
optional: true

- name: selected
type: boolean
optional: true

- name: enabled
type: boolean
optional: true


returns:
- name: changed
type: boolean

- name: selected
type: boolean

#*****************************************************************************************************
#***** LAYOUT ****************************************************************************************
#*****************************************************************************************************
Expand Down
97 changes: 97 additions & 0 deletions imgui/src/extension_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,96 @@ static int imgui_Checkbox(lua_State* L)
return 2;
}

static int imgui_BeginMenuBar(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 1);
imgui_NewFrame();

bool result = ImGui::BeginMenuBar();
lua_pushboolean(L, result);

return 1;
}

static int imgui_EndMenuBar(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 0);
imgui_NewFrame();

ImGui::EndMenuBar();

return 0;
}

static int imgui_BeginMainMenuBar(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 1);
imgui_NewFrame();

bool result = ImGui::BeginMainMenuBar();
lua_pushboolean(L, result);

return 1;
}

static int imgui_EndMainMenuBar(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 0);
imgui_NewFrame();

ImGui::EndMainMenuBar();

return 0;
}

static int imgui_BeginMenu(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 1);
imgui_NewFrame();

const char* label = luaL_checkstring(L, 1);
bool enabled = true;

if (lua_isboolean(L, 2)) {
enabled = lua_toboolean(L, 2);
}

bool result = ImGui::BeginMenu(label, enabled);
lua_pushboolean(L, result);

return 1;
}

static int imgui_EndMenu(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 0);
imgui_NewFrame();

ImGui::EndMenu();

return 0;
}

static int imgui_MenuItem(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 2);
imgui_NewFrame();

const char* label = luaL_checkstring(L, 1);
const char* shortcut = lua_tostring(L, 2);
bool selected = lua_toboolean(L, 3);
bool enabled = true;

if (lua_isboolean(L, 4)) {
enabled = lua_toboolean(L, 4);
}

bool result = ImGui::MenuItem(label, shortcut, &selected, enabled);
lua_pushboolean(L, result);
lua_pushboolean(L, selected);

return 2;
}

// ----------------------------
// ----- LAYOUT ---------
Expand Down Expand Up @@ -1938,6 +2028,13 @@ static const luaL_reg Module_methods[] =
{"button", imgui_Button},
{"button_image", imgui_ButtonImage},
{"checkbox", imgui_Checkbox},
{"begin_menu_bar", imgui_BeginMenuBar},
{"end_menu_bar", imgui_EndMenuBar},
{"begin_main_menu_bar", imgui_BeginMainMenuBar},
{"end_main_menu_bar", imgui_EndMainMenuBar},
{"begin_menu", imgui_BeginMenu},
{"end_menu", imgui_EndMenu},
{"menu_item", imgui_MenuItem},

{"same_line", imgui_SameLine},
{"new_line", imgui_NewLine},
Expand Down

0 comments on commit 26a411b

Please sign in to comment.