From efed58999624d806bfb82c8c066730b67e8552f3 Mon Sep 17 00:00:00 2001
From: Richard Bates <richard@dreamrealityinteractive.com>
Date: Wed, 29 Nov 2023 08:49:17 +0000
Subject: [PATCH] Item sizing and scrollbar additions (#37)

* Add item width, content region, scrollbar size

* Add tab6 with item width examples

* Set scrollbar size in example
---
 example/example.script        | 46 +++++++++++++++++++++++++++
 imgui/api/imgui.script_api    | 55 +++++++++++++++++++++++++++++++-
 imgui/src/extension_imgui.cpp | 59 +++++++++++++++++++++++++++++++++++
 3 files changed, 159 insertions(+), 1 deletion(-)

diff --git a/example/example.script b/example/example.script
index 86d0bf8..77b31af 100644
--- a/example/example.script
+++ b/example/example.script
@@ -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)
@@ -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
@@ -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()
diff --git a/imgui/api/imgui.script_api b/imgui/api/imgui.script_api
index 2e2aa54..f8d8baf 100644
--- a/imgui/api/imgui.script_api
+++ b/imgui/api/imgui.script_api
@@ -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
@@ -356,7 +367,6 @@
       - name: y
         type: number
 
-
 #*****************************************************************************************************
 
   - name: begin_child
@@ -1440,6 +1450,15 @@
       - name: rounding
         type: number
 
+#*****************************************************************************************************
+
+  - name: set_style_scrollbar_size
+    type: function
+
+    parameters:
+      - name: size
+        type: number
+
 #*****************************************************************************************************
 
   - name: set_style_color
@@ -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 ************************************************************************************
 #*****************************************************************************************************
diff --git a/imgui/src/extension_imgui.cpp b/imgui/src/extension_imgui.cpp
index 0a1b388..98e57b6 100644
--- a/imgui/src/extension_imgui.cpp
+++ b/imgui/src/extension_imgui.cpp
@@ -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);
@@ -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);
@@ -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 -----------------
@@ -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},
@@ -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},