Skip to content

Commit

Permalink
Add ImGui tab bar to Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosine256 committed Jul 12, 2023
1 parent 7a21d99 commit aa4c154
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
38 changes: 37 additions & 1 deletion examples/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ local inputcolorrgba = Color:new(0.0, 0.0, 1.0, 0.5) -- 50% opacity blue

local inputtooltip = 'This input\'s value is used as its tooltip.'

local extratabs = {}
local nextextratabid = 1

local widgetopen = true
local closebutton = false

Expand Down Expand Up @@ -125,6 +128,39 @@ set_callback(function(draw_ctx)
draw_ctx:win_imagebutton('##coolbutton', loadingimage, 0.33, 0.11, 0, 0, 1, 1)
end

draw_ctx:win_separator_text('Tabs')

draw_ctx:win_tab_bar('ExampleTabBar', function()
draw_ctx:win_tab_item('Small Tab', false, function()
draw_ctx:win_text('Not much to see here.')
end)
draw_ctx:win_tab_item('Large Tab', false, function()
draw_ctx:win_text('This tab has a lot of content.')
for i=1,20 do
draw_ctx:win_text('Content #'..i)
draw_ctx:win_inline()
draw_ctx:win_button('Button #'..i)
end
end)
local i = 1
while i <= #extratabs do
local open = draw_ctx:win_tab_item(extratabs[i], true, function()
draw_ctx:win_text('This is '..extratabs[i]..'. It can be closed with the X.')
end)
if open then
i = i + 1
else
table.remove(extratabs, i)
end
end
if draw_ctx:win_tab_item_button('+') then
table.insert(extratabs, 'Extra Tab #'..nextextratabid)
nextextratabid = nextextratabid + 1
end
end)

draw_ctx:win_separator_text('Identical Input Labels')

-- remember to use unique labels on identical inputs
if draw_ctx:win_button('Test##FirstTest') then
message('First button')
Expand All @@ -150,7 +186,7 @@ set_callback(function(draw_ctx)
end

-- or with pushid (string)
local b_ids = { "first", "second", "third" }
local b_ids = { 'first', 'second', 'third' }
b = {}
for i, id in ipairs(b_ids) do
draw_ctx:win_pushid(id)
Expand Down
28 changes: 28 additions & 0 deletions src/game_api/script/usertypes/gui_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,31 @@ void GuiDrawContext::win_section(std::string title, sol::function callback)
if (ImGui::CollapsingHeader(title.c_str()))
handle_function<void>(backend, callback);
};
void GuiDrawContext::win_tab_bar(std::string id, sol::function callback)
{
// TODO: Which ImGuiTabBarFlags should be exposed? ImGuiTabBarFlags_Reorderable and ImGuiTabBarFlags_AutoSelectNewTabs seem useful.
if (ImGui::BeginTabBar(id.c_str()))
{
handle_function<void>(backend, callback);
ImGui::EndTabBar();
}
};
bool GuiDrawContext::win_tab_item(std::string label, bool closeable, sol::function callback)
{
// TODO: Which ImGuiTabItemFlags should be exposed? Many of them seem useful.
bool open = true;
if (ImGui::BeginTabItem(label.c_str(), closeable ? &open : NULL))
{
handle_function<void>(backend, callback);
ImGui::EndTabItem();
}
return open;
};
bool GuiDrawContext::win_tab_item_button(std::string label)
{
// TODO: Which ImGuiTabItemFlags should be exposed? Many of them seem useful.
return ImGui::TabItemButton(label.c_str());
};
void GuiDrawContext::win_indent(float width)
{
if (std::abs(width) < 1.0f)
Expand Down Expand Up @@ -651,6 +676,9 @@ void register_usertypes(sol::state& lua)
guidrawcontext_type["win_imagebutton"] = &GuiDrawContext::win_imagebutton;
guidrawcontext_type["win_tooltip"] = &GuiDrawContext::win_tooltip;
guidrawcontext_type["win_section"] = &GuiDrawContext::win_section;
guidrawcontext_type["win_tab_bar"] = &GuiDrawContext::win_tab_bar;
guidrawcontext_type["win_tab_item"] = &GuiDrawContext::win_tab_item;
guidrawcontext_type["win_tab_item_button"] = &GuiDrawContext::win_tab_item_button;
guidrawcontext_type["win_indent"] = &GuiDrawContext::win_indent;
guidrawcontext_type["win_width"] = &GuiDrawContext::win_width;

Expand Down
6 changes: 6 additions & 0 deletions src/game_api/script/usertypes/gui_lua.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class GuiDrawContext
void win_tooltip(std::string text);
/// Add a collapsing accordion section, put contents in the callback function.
void win_section(std::string title, sol::function callback);
/// Add a tab bar. Only create tab items inside the callback.
void win_tab_bar(std::string id, sol::function callback);
/// Add a tab item. Only use this inside a tab bar callback. Returns false if closed from the X.
bool win_tab_item(std::string label, bool closeable, sol::function callback);
/// Add a tab item button. Only use this inside a tab bar callback. Returns true when clicked.
bool win_tab_item_button(std::string label);
/// Indent contents, or unindent if negative
void win_indent(float width);
/// Sets next item width (width>1: width in pixels, width<0: to the right of window, -1<width<1: fractional, multiply by available window width)
Expand Down

0 comments on commit aa4c154

Please sign in to comment.