Skip to content

Commit 5d50a65

Browse files
committed
Update varargs/table interactions and formatting
Updated the use of vararg and / operators in table interactions to improve support for resizable and precise width tables. Fixed layout and text formatting in debug utilities, detail viewers and demo windows. Replaced unclear string operations with more explicit byte transformations for cleaner and more accurate function executions. These changes improve debug performance and accuracy, especially for tables.
1 parent 5722cbc commit 5d50a65

File tree

13 files changed

+126
-121
lines changed

13 files changed

+126
-121
lines changed

core/src/main/kotlin/imgui/api/debugUtilities.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package imgui.api
22

3+
import glm_.b
34
import imgui.ImGui
45
import imgui.ImGui.sameLine
56
import imgui.ImGui.tableHeadersRow
@@ -8,6 +9,7 @@ import imgui.ImGui.tableSetupColumn
89
import imgui.ImGui.text
910
import imgui.ImGui.textUnformatted
1011
import imgui.TableFlag
12+
import imgui.div
1113
import imgui.dsl.table
1214
import imgui.internal.textCharFromUtf8
1315
import imgui.or
@@ -17,28 +19,28 @@ import uno.kotlin.NUL
1719
interface debugUtilities {
1820

1921
/** Helper tool to diagnose between text encoding issues and font loading issues. Pass your UTF-8 string and verify that there are correct. */
20-
fun debugTextEncoding(str: String) {
22+
fun debugTextEncoding(str: ByteArray) {
2123
text("Text: \"$str\"")
22-
table("##DebugTextEncoding", 4, TableFlag.Borders or TableFlag.RowBg or TableFlag.SizingFixedFit) {
24+
table("##DebugTextEncoding", 4, TableFlag.Borders / TableFlag.RowBg / TableFlag.SizingFixedFit / TableFlag.Resizable) {
2325
tableSetupColumn("Offset")
2426
tableSetupColumn("UTF-8")
2527
tableSetupColumn("Glyph")
2628
tableSetupColumn("Codepoint")
2729
tableHeadersRow()
2830
var p = 0
29-
while (str[p] != NUL) {
30-
val (c, cUtf8Len) = textCharFromUtf8(str.encodeToByteArray(), p, -1)
31+
while (p < str.size && str[p] != 0.b) {
32+
val (c, cUtf8Len) = textCharFromUtf8(str, p, -1)
3133
tableNextColumn()
3234
text("$p")
3335
tableNextColumn()
3436
for (byteIndex in 0 until cUtf8Len) {
3537
if (byteIndex > 0)
3638
sameLine()
37-
text("0x%02X", str[p + byteIndex].code)
39+
text("0x%02X", str[p + byteIndex])
3840
}
3941
tableNextColumn()
4042
if (ImGui.font.findGlyphNoFallback(Char(c)) != null)
41-
textUnformatted(str.drop(p), p + cUtf8Len)
43+
textUnformatted(str.copyOfRange(p, p + cUtf8Len), cUtf8Len)
4244
else
4345
textUnformatted("[missing]")
4446
tableNextColumn()

core/src/main/kotlin/imgui/api/demoDebugInformations.kt

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:OptIn(ExperimentalStdlibApi::class)
2+
13
package imgui.api
24

35
import glm_.*
@@ -10,7 +12,6 @@ import imgui.ImGui.beginChildFrame
1012
import imgui.ImGui.beginCombo
1113
import imgui.ImGui.beginItemTooltip
1214
import imgui.ImGui.beginTable
13-
import imgui.ImGui.beginTooltip
1415
import imgui.ImGui.bulletText
1516
import imgui.ImGui.button
1617
import imgui.ImGui.calcTextSize
@@ -93,14 +94,14 @@ import imgui.ImGui.treeNode
9394
import imgui.ImGui.treeNodeToLabelSpacing
9495
import imgui.ImGui.treePop
9596
import imgui.ImGui.unindent
96-
import imgui.classes.ListClipper
9797
import imgui.classes.Style
9898
import imgui.classes.listClipper
9999
import imgui.demo.DemoWindow
100100
import imgui.demo.showExampleApp.StyleEditor
101101
import imgui.dsl.indent
102102
import imgui.dsl.listBox
103103
import imgui.dsl.treeNode
104+
import imgui.dsl.withID
104105
import imgui.internal.DrawIdx
105106
import imgui.internal.DrawVert
106107
import imgui.internal.api.debugTools.Companion.metricsHelpMarker
@@ -176,7 +177,6 @@ interface demoDebugInformations {
176177
text("Application average %.3f ms/frame (%.1f FPS)", 1000f / io.framerate, io.framerate)
177178
text("${io.metricsRenderVertices} vertices, ${io.metricsRenderIndices} indices (${io.metricsRenderIndices / 3} triangles)")
178179
text("${io.metricsRenderWindows} visible windows, ${io.metricsActiveAllocations} active allocations")
179-
text("If your company uses this, please consider sponsoring the project!")
180180
//SameLine(); if (SmallButton("GC")) { g.GcCompactAll = true; }
181181

182182
separator()
@@ -205,7 +205,7 @@ interface demoDebugInformations {
205205
if (showEncodingViewer) {
206206
setNextItemWidth(-Float.MIN_VALUE)
207207
inputText("##Text", buf)
208-
if (buf.isNotEmpty())
208+
if (buf[0] != 0.b)
209209
debugTextEncoding(buf)
210210
treePop()
211211
}
@@ -265,7 +265,7 @@ interface demoDebugInformations {
265265
continue
266266
for (columnN in 0 until table.columnsCount) {
267267
val r = Funcs.getTableRect(table, rectN, columnN)
268-
val buf = "(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) Col $columnN ${rectN.name}" .format(r.min.x, r.min.y, r.max.x, r.max.y, r.width, r.height)
268+
val buf = "(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) Col $columnN ${rectN.name}".format(r.min.x, r.min.y, r.max.x, r.max.y, r.width, r.height)
269269
selectable(buf)
270270
if (isItemHovered())
271271
foregroundDrawList.addRect(r.min - 1, r.max + 1, COL32(255, 255, 0, 255), thickness = 2f)
@@ -339,14 +339,16 @@ interface demoDebugInformations {
339339
}
340340

341341
// Details for TabBars
342-
treeNode("TabBars", "Tab Bars (${g.tabBars.size})") {
343-
for (n in g.tabBars.indices)
344-
debugNodeTabBar(g.tabBars[n]!!, "TabBar")
342+
treeNode("TabBars", "Tab Bars (${g.tabBars.aliveCount.i})") {
343+
for (tabBar in g.tabBars)
344+
withID(tabBar) {
345+
debugNodeTabBar(tabBar, "TabBar")
346+
}
345347
}
346348

347-
treeNode("Tables", "Tables (${g.tables.size})") {
348-
for (n in 0 until g.tables.size)
349-
debugNodeTable(g.tables.getByIndex(n))
349+
treeNode("Tables", "Tables (${g.tables.aliveCount.i})") {
350+
for (table in g.tables)
351+
debugNodeTable(table)
350352
}
351353

352354
// Details for Fonts
@@ -362,10 +364,11 @@ interface demoDebugInformations {
362364

363365

364366
// Details for Docking
365-
// #ifdef IMGUI_HAS_DOCK
366-
treeNode("Dock nodes") {
367+
if (IMGUI_HAS_DOCK)
368+
treeNode("Dock nodes") {
367369

368-
} // #endif // #define IMGUI_HAS_DOCK
370+
}
371+
// #endif // #define IMGUI_HAS_DOCK
369372

370373
// Settings
371374
treeNode("Settings") {
@@ -455,7 +458,7 @@ interface demoDebugInformations {
455458
text("WheelingWindow: '${g.wheelingWindow?.name ?: "NULL"}'")
456459
text("WheelingWindowReleaseTimer: %.2f", g.wheelingWindowReleaseTimer)
457460
val axis = if (g.wheelingAxisAvg.x > g.wheelingAxisAvg.y) "X" else if (g.wheelingAxisAvg.x < g.wheelingAxisAvg.y) "Y" else "<none>"
458-
text("WheelingAxisAvg[] = { %.3f, %.3f }, Main Axis: %s", g.wheelingAxisAvg.x, g.wheelingAxisAvg.y)
461+
text("WheelingAxisAvg[] = { %.3f, %.3f }, Main Axis: $axis", g.wheelingAxisAvg.x, g.wheelingAxisAvg.y)
459462
}
460463

461464
text("KEY OWNERS")
@@ -486,7 +489,7 @@ interface demoDebugInformations {
486489
}
487490
}
488491
}
489-
text("(ActiveIdUsing: AllKeyboardKeys: %d, NavDirMask: 0x%X)", g.activeIdUsingAllKeyboardKeys, g.activeIdUsingNavDirMask)
492+
text("(ActiveIdUsing: AllKeyboardKeys: ${g.activeIdUsingAllKeyboardKeys.i}, NavDirMask: 0x%X)", g.activeIdUsingNavDirMask)
490493
}
491494
}
492495

@@ -524,7 +527,7 @@ interface demoDebugInformations {
524527
debugLocateItemOnHover(g.navId)
525528
text("NavInputSource: ${g.navInputSource}")
526529
text("NavActive: ${io.navActive}, NavVisible: ${io.navVisible}")
527-
text("NavActivateId/DownId/PressedId: %08X/%08X/%08X/%08X", g.navActivateId, g.navActivateDownId, g.navActivatePressedId)
530+
text("NavActivateId/DownId/PressedId: %08X/%08X/%08X", g.navActivateId, g.navActivateDownId, g.navActivatePressedId)
528531
text("NavActivateFlags: %04X", g.navActivateFlags)
529532
text("NavDisableHighlight: ${g.navDisableHighlight}, NavDisableMouseHover: ${g.navDisableMouseHover}")
530533
text("NavFocusScopeId = 0x%08X", g.navFocusScopeId)
@@ -730,6 +733,7 @@ interface demoDebugInformations {
730733
separator()
731734
text("By Omar Cornut and all Dear Imgui contributors.")
732735
text("Dear ImGui is licensed under the MIT License, see LICENSE for more information.")
736+
text("If your company uses this, please consider sponsoring the project!")
733737

734738
checkbox("Config/Build Information", ::showConfigInfo)
735739
if (showConfigInfo) {
@@ -1002,6 +1006,6 @@ interface demoDebugInformations {
10021006
return formatString(buf, "???")
10031007
}
10041008

1005-
val buf = ""
1009+
val buf = ByteArray(100)
10061010
}
10071011
}

core/src/main/kotlin/imgui/api/widgetsText.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface widgetsText {
3535
* A) doesn't require null terminated string if 'text_end' is specified,
3636
* B) it's faster, no memory copy is done, no buffer size limits, recommended for long chunks of text. */
3737
fun textUnformatted(text: String, textEnd: Int = -1) = textEx(text, textEnd, TextFlag.NoWidthForLargeClippedText)
38+
fun textUnformatted(text: ByteArray, textEnd: Int = -1) = textEx(text, textEnd, TextFlag.NoWidthForLargeClippedText)
3839

3940
/** formatted text */
4041
fun text(fmt: String, vararg args: Any) {

core/src/main/kotlin/imgui/classes/Context.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ class Context(sharedFontAtlas: FontAtlas? = null) {
503503
// Tab bars
504504

505505
var currentTabBar: TabBar? = null
506-
val tabBars = TabBarPool()
506+
val tabBars = Pool { TabBar() }
507507
val currentTabBarStack = Stack<PtrOrIndex>()
508508
val shrinkWidthBuffer = ArrayList<ShrinkWidthItem>()
509509

@@ -874,14 +874,14 @@ class Context(sharedFontAtlas: FontAtlas? = null) {
874874
companion object {
875875
// IMPORTANT: ###xxx suffixes must be same in ALL languages
876876
val gLocalizationEntriesEnUS = listOf(
877-
LocEntry(LocKey.VersionStr, "Dear ImGui $IMGUI_VERSION ($IMGUI_VERSION_NUM)"),
878-
LocEntry(LocKey.TableSizeOne, "Size column to fit###SizeOne"),
879-
LocEntry(LocKey.TableSizeAllFit, "Size all columns to fit###SizeAll"),
880-
LocEntry(LocKey.TableSizeAllDefault, "Size all columns to default###SizeAll"),
881-
LocEntry(LocKey.TableResetOrder, "Reset order###ResetOrder"),
882-
LocEntry(LocKey.WindowingMainMenuBar, "(Main menu bar)"),
883-
LocEntry(LocKey.WindowingPopup, "(Popup)"),
884-
LocEntry(LocKey.WindowingUntitled, "(Untitled)"))
877+
LocEntry(LocKey.VersionStr, "Dear ImGui $IMGUI_VERSION ($IMGUI_VERSION_NUM)"),
878+
LocEntry(LocKey.TableSizeOne, "Size column to fit###SizeOne"),
879+
LocEntry(LocKey.TableSizeAllFit, "Size all columns to fit###SizeAll"),
880+
LocEntry(LocKey.TableSizeAllDefault, "Size all columns to default###SizeAll"),
881+
LocEntry(LocKey.TableResetOrder, "Reset order###ResetOrder"),
882+
LocEntry(LocKey.WindowingMainMenuBar, "(Main menu bar)"),
883+
LocEntry(LocKey.WindowingPopup, "(Popup)"),
884+
LocEntry(LocKey.WindowingUntitled, "(Untitled)"))
885885
}
886886
}
887887

@@ -896,9 +896,9 @@ enum class ContextHookType { NewFramePre, NewFramePost, EndFramePre, EndFramePos
896896

897897
/** Hook for extensions like ImGuiTestEngine */
898898
class ContextHook(
899-
// A unique ID assigned by AddContextHook()
900-
var hookId: ID = 0,
901-
var type: ContextHookType = ContextHookType.NewFramePre,
902-
var owner: ID = 0,
903-
var callback: ContextHookCallback? = null,
904-
var userData: Any? = null)
899+
// A unique ID assigned by AddContextHook()
900+
var hookId: ID = 0,
901+
var type: ContextHookType = ContextHookType.NewFramePre,
902+
var owner: ID = 0,
903+
var callback: ContextHookCallback? = null,
904+
var userData: Any? = null)

core/src/main/kotlin/imgui/demo/ShowDemoWindowInputs.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ object ShowDemoWindowInputs {
149149
// IMGUI_DEMO_MARKER("Inputs & Focus/Dragging");
150150
treeNode("Dragging") {
151151
textWrapped("You can use getMouseDragDelta(0) to query for the dragged amount on any widget.")
152-
for (button in MouseButton.values())
152+
for (button in MouseButton.traditionalValues)
153153
if (button != MouseButton.None) {
154154
text("IsMouseDragging(${button.i}):")
155155
text(" w/ default threshold: ${button.isDragging().i},")

core/src/main/kotlin/imgui/demo/ShowDemoWindowLayout.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ object ShowDemoWindowLayout {
308308
run {
309309
var windowFlags: WindowFlags = none
310310
if (disableMouseWheel)
311-
windowFlags = windowFlags or Wf.NoScrollWithMouse
311+
windowFlags /= Wf.NoScrollWithMouse
312312
if (!disableMenu)
313-
windowFlags = windowFlags or Wf.MenuBar
313+
windowFlags /= Wf.MenuBar
314314
withStyleVar(StyleVar.ChildRounding, 5f) {
315315
child("ChildR", Vec2(0, 260), true, windowFlags) {
316316
if (!disableMenu && beginMenuBar()) {
@@ -321,7 +321,7 @@ object ShowDemoWindowLayout {
321321
endMenuBar()
322322
}
323323
columns(2)
324-
if (beginTable("split", 2, TableFlag.Resizable or TableFlag.NoSavedSettings)) {
324+
if (beginTable("split", 2, TableFlag.Resizable / TableFlag.NoSavedSettings)) {
325325
for (i in 0..99) {
326326
val text = "%03d".format(style.locale, i)
327327
tableNextColumn()

core/src/main/kotlin/imgui/demo/ShowDemoWindowTables.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ import imgui.api.demoDebugInformations.Companion.helpMarker
6969
import imgui.api.drag
7070
import imgui.api.slider
7171
import imgui.classes.DrawList
72-
import imgui.classes.ListClipper
7372
import imgui.classes.TableSortSpecs
7473
import imgui.classes.listClipper
7574
import imgui.dsl.popup
@@ -762,13 +761,13 @@ object ShowDemoWindowTables {
762761
}
763762

764763
object `Sizing policies` {
765-
var flags1 = Tf.BordersV or Tf.BordersOuterH or Tf.RowBg or Tf.ContextMenuInBody
764+
var flags1 = Tf.BordersV / Tf.BordersOuterH / Tf.RowBg / Tf.ContextMenuInBody
766765
val sizingPolicyFlags = flagArrayOf(Tf.SizingFixedFit, Tf.SizingFixedSame, Tf.SizingStretchProp, Tf.SizingStretchSame)
767766

768767
enum class ContentsType { ShowWidth, ShortText, LongText, Button, FillButton, InputText }
769768

770769
var flags = Tf.ScrollY / Tf.Borders / Tf.RowBg / Tf.Resizable
771-
var contentsType1 = ContentsType.ShowWidth
770+
var contentsType = ContentsType.ShowWidth
772771
var columnCount = 3
773772
val textBuf = ByteArray(32)
774773

@@ -779,7 +778,7 @@ object ShowDemoWindowTables {
779778
checkboxFlags("ImGuiTableFlags_NoHostExtendX", ::flags1, Tf.NoHostExtendX)
780779
}
781780

782-
for (tableN in 0..3)
781+
for (tableN in sizingPolicyFlags.indices)
783782
withID(tableN) {
784783
setNextItemWidth(TEXT_BASE_WIDTH * 30)
785784
editTableSizingFlags(sizingPolicyFlags mutablePropertyAt tableN)
@@ -813,19 +812,18 @@ object ShowDemoWindowTables {
813812
withID("Advanced") {
814813
withItemWidth(TEXT_BASE_WIDTH * 30) {
815814
editTableSizingFlags(::flags)
816-
val ordinalRef = contentsType1.ordinal.mutableReference
815+
val ordinalRef = contentsType.ordinal.mutableReference
817816
val ordinal by ordinalRef
818817
combo("Contents", ordinalRef, "Show width\u0000Short Text\u0000Long Text\u0000Button\u0000Fill Button\u0000InputText\u0000")
819-
contentsType1 = ContentsType.values()[ordinal]
820-
if (contentsType1 == ContentsType.FillButton) {
818+
contentsType = ContentsType.values()[ordinal]
819+
if (contentsType == ContentsType.FillButton) {
821820
sameLine()
822821
helpMarker("Be mindful that using right-alignment (e.g. size.x = -FLT_MIN) creates a feedback loop where contents width can feed into auto-column width can feed into contents width.")
823822
}
824823
drag("Columns", ::columnCount, 0.1f, 1, 64, "%d", SliderFlag.AlwaysClamp)
825824
checkboxFlags("ImGuiTableFlags_Resizable", ::flags, Tf.Resizable)
826825
checkboxFlags("ImGuiTableFlags_PreciseWidths", ::flags, Tf.PreciseWidths)
827826
sameLine(); helpMarker("Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth.")
828-
checkboxFlags("ImGuiTableFlags_Resizable", ::flags, Tf.Resizable)
829827
checkboxFlags("ImGuiTableFlags_ScrollX", ::flags, Tf.ScrollX)
830828
checkboxFlags("ImGuiTableFlags_ScrollY", ::flags, Tf.ScrollY)
831829
checkboxFlags("ImGuiTableFlags_NoClip", ::flags, Tf.NoClip)
@@ -841,7 +839,7 @@ object ShowDemoWindowTables {
841839

842840
withID(cell) {
843841
val label = "Hello $column,$row"
844-
when (contentsType1) {
842+
when (contentsType) {
845843
ContentsType.ShortText -> textUnformatted(label)
846844
ContentsType.LongText -> text("Some ${if (column == 0) "long" else "longeeer"} text $column,$row\nOver two lines..")
847845
ContentsType.ShowWidth -> text("W: %.1f", contentRegionAvail.x)

core/src/main/kotlin/imgui/demo/ShowDemoWindowWidgets.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -925,16 +925,16 @@ object ShowDemoWindowWidgets {
925925
selectable("Hello.h", selected1 mutablePropertyAt 2); sameLine(300); text(" 2,345 bytes")
926926
}
927927
treeNode("In columns") {
928-
if (beginTable("split1", 3, TableFlag.Resizable or TableFlag.NoSavedSettings)) {
928+
if (beginTable("split1", 3, TableFlag.Resizable / TableFlag.NoSavedSettings / TableFlag.Borders)) {
929929
for (i in 0..9) {
930930
val label = "Item $i"
931931
tableNextColumn()
932932
selectable(label, selected2 mutablePropertyAt i) // FIXME-TABLE: Selection overlap
933933
}
934934
endTable()
935935
}
936-
separator()
937-
if (beginTable("split2", 3, TableFlag.Resizable or TableFlag.NoSavedSettings)) {
936+
spacing()
937+
if (beginTable("split2", 3, TableFlag.Resizable / TableFlag.NoSavedSettings / TableFlag.Borders)) {
938938
for (i in 0..9) {
939939
val label = "Item $i"
940940
tableNextRow()

core/src/main/kotlin/imgui/flags & enumerations.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,7 @@ enum class MouseButton {
19391939
val i = ordinal - 1 // starts at -1
19401940

19411941
companion object {
1942+
val traditionalValues = values().drop(1).take(3)
19421943
val imguiValues = values().drop(1)
19431944
val COUNT = 5
19441945
infix fun of(i: Int): MouseButton = values()[1 + i]

core/src/main/kotlin/imgui/internal/api/basicHelpersForWidgetCode.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal interface basicHelpersForWidgetCode {
9191
g.lastItemData.id = id
9292
g.lastItemData.rect put bb
9393
g.lastItemData.navRect put (navBbArg ?: bb)
94-
g.lastItemData.inFlags = g.currentItemFlags or g.nextItemData.itemFlags or extraFlags
94+
g.lastItemData.inFlags = g.currentItemFlags / g.nextItemData.itemFlags / extraFlags
9595
g.lastItemData.statusFlags = none
9696

9797
// Directional navigation processing

0 commit comments

Comments
 (0)