Skip to content

Commit

Permalink
Item sizing and scrollbar additions (#37)
Browse files Browse the repository at this point in the history
* Add item width, content region, scrollbar size

* Add tab6 with item width examples

* Set scrollbar size in example
  • Loading branch information
dri-richard authored Nov 29, 2023
1 parent 09530e5 commit efed589
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 1 deletion.
46 changes: 46 additions & 0 deletions example/example.script
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local function set_style()
imgui.set_style_window_rounding(6)
imgui.set_style_frame_rounding(3)
imgui.set_style_scrollbar_rounding(10)
imgui.set_style_scrollbar_size(10)
imgui.set_style_color(imgui.ImGuiCol_Text, 0.90, 0.90, 0.90, 0.90)
imgui.set_style_color(imgui.ImGuiCol_TextDisabled, 0.60, 0.60, 0.60, 1.00)
imgui.set_style_color(imgui.ImGuiCol_WindowBg, 0.09, 0.09, 0.15, 1.00)
Expand Down Expand Up @@ -368,6 +369,45 @@ local function update_tab5(self)

end

-- Loosely based on "Widgets Width" part of imgui_demo
local function update_tab6(self)
if not self.tab6_f then self.tab6_f = 0 end

imgui.text("SetNextItemWidth(200)")
imgui.same_line()
imgui.set_next_item_width(200)
local changed, f = imgui.input_float("float##1", self.tab6_f)
if changed then
self.tab6_f = f
end

imgui.text("SetNextItemWidth(GetWindowWidth() * 0.5f)")
imgui.same_line()
local w,h = imgui.get_window_size()
imgui.set_next_item_width(w * 0.5)
local changed, f = imgui.input_float("float##2", self.tab6_f)
if changed then
self.tab6_f = f
end

imgui.text("SetNextItemWidth(GetContentRegionAvail().x * 0.5f)")
imgui.same_line()
local aw, ah = imgui.get_content_region_avail()
imgui.set_next_item_width(aw * 0.5)
local changed, f = imgui.input_float("float##3", self.tab6_f)
if changed then
self.tab6_f = f
end

imgui.text("SetNextItemWidth(-200)")
imgui.same_line()
imgui.set_next_item_width(-200)
local changed, f = imgui.input_float("float##4", self.tab6_f)
if changed then
self.tab6_f = f
end
end

local function main_menu_bar(self)
if imgui.begin_main_menu_bar() then
if imgui.begin_menu("File") then
Expand Down Expand Up @@ -492,6 +532,12 @@ function update(self, dt)
update_tab5(self)
imgui.end_tab_item()
end

local tab6_open = imgui.begin_tab_item("Tab6")
if tab6_open then
update_tab6(self)
imgui.end_tab_item()
end

imgui.end_tab_bar()
imgui.end_window()
Expand Down
55 changes: 54 additions & 1 deletion imgui/api/imgui.script_api
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@
- name: focused
type: boolean

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

- name: get_content_region_avail
type: function

return:
- name: w
type: number
- name: h
type: number

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

- name: get_window_content_region_max
Expand All @@ -356,7 +367,6 @@
- name: y
type: number


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

- name: begin_child
Expand Down Expand Up @@ -1440,6 +1450,15 @@
- name: rounding
type: number

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

- name: set_style_scrollbar_size
type: function

parameters:
- name: size
type: number

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

- name: set_style_color
Expand Down Expand Up @@ -1524,6 +1543,40 @@
- name: scale
type: number

#*****************************************************************************************************
#***** ITEM WIDTH ************************************************************************************
#*****************************************************************************************************

- name: push_item_width
type: function

parameters:
- name: width
type: number

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

- name: pop_item_width
type: function

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

- name: set_next_item_width
type: function

parameters:
- name: width
type: number

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

- name: calc_item_width
type: function

return:
- name: width
type: number

#*****************************************************************************************************
#***** NAVIGATION ************************************************************************************
#*****************************************************************************************************
Expand Down
59 changes: 59 additions & 0 deletions imgui/src/extension_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,17 @@ static int imgui_IsWindowFocused(lua_State* L)
lua_pushboolean(L, focused);
return 1;
}

static int imgui_GetContentRegionAvail(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 2);
imgui_NewFrame();
ImVec2 region = ImGui::GetContentRegionAvail();
lua_pushnumber(L, region.x);
lua_pushnumber(L, region.y);
return 2;
}

static int imgui_GetWindowContentRegionMax(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 2);
Expand Down Expand Up @@ -1679,6 +1690,13 @@ static int imgui_SetStyleScrollbarRounding(lua_State* L)
style.ScrollbarRounding = luaL_checknumber(L, 1);
return 0;
}
static int imgui_SetStyleScrollbarSize(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 0);
ImGuiStyle& style = ImGui::GetStyle();
style.ScrollbarSize = luaL_checknumber(L, 1);
return 0;
}
static int imgui_SetStyleColor(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 0);
Expand Down Expand Up @@ -1758,6 +1776,40 @@ static int imgui_SetCursorPos(lua_State *L)
return 0;
}

// ----------------------------
// ----- ITEM WIDTH -----------
// ----------------------------

static int imgui_PushItemWidth(lua_State *L)
{
DM_LUA_STACK_CHECK(L, 0);
float width = luaL_checknumber(L, 1);
ImGui::PushItemWidth(width);
return 0;
}

static int imgui_PopItemWidth(lua_State *L)
{
DM_LUA_STACK_CHECK(L, 0);
ImGui::PopItemWidth();
return 0;
}

static int imgui_SetNextItemWidth(lua_State *L)
{
DM_LUA_STACK_CHECK(L, 0);
float width = luaL_checknumber(L, 1);
ImGui::SetNextItemWidth(width);
return 0;
}

static int imgui_CalcItemWidth(lua_State *L)
{
DM_LUA_STACK_CHECK(L, 1);
float width = ImGui::CalcItemWidth();
lua_pushnumber(L, width);
return 1;
}

// ----------------------------
// ----- NAVIGATION -----------------
Expand Down Expand Up @@ -2119,6 +2171,7 @@ static const luaL_reg Module_methods[] =
{"begin_window", imgui_Begin},
{"end_window", imgui_End},
{"is_window_focused", imgui_IsWindowFocused},
{"get_content_region_avail", imgui_GetContentRegionAvail},
{"get_window_content_region_max", imgui_GetWindowContentRegionMax},

{"begin_child", imgui_BeginChild},
Expand Down Expand Up @@ -2238,10 +2291,16 @@ static const luaL_reg Module_methods[] =
{"set_style_frame_rounding", imgui_SetStyleFrameRounding},
{"set_style_tab_rounding", imgui_SetStyleTabRounding},
{"set_style_scrollbar_rounding", imgui_SetStyleScrollbarRounding},
{"set_style_scrollbar_size", imgui_SetStyleScrollbarSize},
{"set_style_color", imgui_SetStyleColor},
{"push_style_color", imgui_PushStyleColor},
{"pop_style_color", imgui_PopStyleColor},
{"get_style_item_spacing", imgui_GetStyleItemSpacing},

{"push_item_width", imgui_PushItemWidth},
{"pop_item_width", imgui_PopItemWidth},
{"set_next_item_width", imgui_SetNextItemWidth},
{"calc_item_width", imgui_CalcItemWidth},

{"set_defaults", imgui_SetDefaults},
{"set_ini_filename", imgui_SetIniFilename},
Expand Down

0 comments on commit efed589

Please sign in to comment.