Skip to content

Commit 898ea8a

Browse files
committed
Refactor and fix various bugs in several files
In this commit, various bugs and issues were addressed in several different files. This includes removing superfluous conditionals, optimizing string formatting and implementing proper UTF-8 handling in text encoding. As part of this commit, redundant and repetitive function calls were also simplified for performance improvement, and some unclear variable names were changed to enhance readability. The changes were largely focused on streamlining the code for better optimization and understanding. Necessary adjustments were made for correct handling of tree node states and input disable frames in windows. Additionally, a potential bug concerning text display end in renderHelpers.kt was fixed.
1 parent c0d852d commit 898ea8a

File tree

9 files changed

+30
-35
lines changed

9 files changed

+30
-35
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +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}"
269-
.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)
270269
selectable(buf)
271270
if (isItemHovered())
272271
foregroundDrawList.addRect(r.min - 1, r.max + 1, COL32(255, 255, 0, 255), thickness = 2f)

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,14 @@ interface loggingCapture {
9292

9393
/** pass text data straight to log (without being displayed) */
9494
fun logTextV(g: Context, fmt: String, vararg args: Any) {
95-
if (g.logFile != null) {
96-
val writer = FileWriter(g.logFile!!, true)
97-
writer.write(String.format(fmt, *args))
98-
} else
99-
g.logBuffer.append(fmt.format(*args))
95+
val text = if (args.isEmpty()) fmt else fmt.format(*args)
96+
g.logFile?.appendText(text) ?: g.logBuffer.append(text)
10097
}
10198

10299
fun logText(fmt: String, vararg args: Any) {
103100
if (!g.logEnabled)
104101
return
105102

106-
logTextV(g, fmt, args)
107-
}
108-
109-
fun logTextV(fmt: String, vararg args: Any) {
110-
111-
if (!g.logEnabled)
112-
return
113-
114-
logTextV(g, fmt, args)
103+
logTextV(g, fmt, *args)
115104
}
116105
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ interface widgetsText {
8888

8989
val valueTextBegin = g.tempBuffer
9090
val valueTextEnd = formatStringToTempBuffer(fmt, args)
91-
val valueSize = calcTextSize(valueTextBegin, valueTextEnd, hideTextAfterDoubleHash = false)
91+
val valueSize = calcTextSize(valueTextBegin, 0, valueTextEnd, false)
9292
val labelSize = calcTextSize(label, hideTextAfterDoubleHash = true)
9393

9494
val pos = window.dc.cursorPos // [JVM] careful, same instance

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ import glm_.f
55
import glm_.max
66
import glm_.vec2.Vec2
77
import imgui.*
8-
import imgui.ImGui.bringToDisplayBehind
98
import imgui.ImGui.debugLocateItemResolveWithLastItem
109
import imgui.ImGui.endColumns
1110
import imgui.ImGui.errorCheckUsingSetCursorPosToExtendParentBoundaries
1211
import imgui.ImGui.findBestWindowPosForPopup
13-
import imgui.ImGui.findBlockingModal
1412
import imgui.ImGui.findWindowByName
1513
import imgui.ImGui.focusWindow
1614
import imgui.ImGui.gcAwakeTransientBuffers
1715
import imgui.ImGui.io
1816
import imgui.ImGui.isMouseHoveringRect
19-
import imgui.ImGui.isWithinBeginStackOf
2017
import imgui.ImGui.logFinish
2118
import imgui.ImGui.markIniSettingsDirty
2219
import imgui.ImGui.navInitWindow
@@ -29,7 +26,6 @@ import imgui.ImGui.setLastItemData
2926
import imgui.ImGui.setPos
3027
import imgui.ImGui.setSize
3128
import imgui.ImGui.style
32-
import imgui.ImGui.topMostPopupModal
3329
import imgui.ImGui.updateParentAndRootLinks
3430
import imgui.internal.classes.FocusRequestFlag
3531
import imgui.internal.classes.Rect
@@ -526,6 +522,8 @@ interface windows {
526522
window.scrollMax.y = max(0f, window.contentSize.y + window.windowPadding.y * 2f - window.innerRect.height)
527523

528524
// Apply scrolling
525+
if (g.frameCount >= 746 && name == "Dear ImGui Test Engine/Log_4C7FD7FA/Log_CAB00879")
526+
print("")
529527
window.scroll = window.calcNextScrollFromScrollTargetAndClamp()
530528
window.scrollTarget put Float.MAX_VALUE
531529
window.decoInnerSizeX1 = 0f; window.decoInnerSizeY1 = 0f
@@ -722,8 +720,8 @@ interface windows {
722720

723721
window.apply {
724722
// Update the Hidden flag
725-
val hiddenRegulare = hiddenFramesCanSkipItems > 0 || hiddenFramesCannotSkipItems > 0 || hiddenFramesForRenderOnly > 0
726-
hidden = hiddenRegulare || hiddenFramesForRenderOnly > 0
723+
val hiddenRegular = hiddenFramesCanSkipItems > 0 || hiddenFramesCannotSkipItems > 0 || hiddenFramesForRenderOnly > 0
724+
hidden = hiddenRegular || hiddenFramesForRenderOnly > 0
727725

728726
// Disable inputs for requested number of frames
729727
if (disableInputsFrames > 0) {
@@ -732,7 +730,7 @@ interface windows {
732730
}
733731

734732
// Update the SkipItems flag, used to early out of all items functions (no layout required)
735-
skipItems = (collapsed || !active || hiddenRegulare) && autoFitFrames allLessThanEqual 0 && hiddenFramesCannotSkipItems <= 0
733+
skipItems = (collapsed || !active || hiddenRegular) && autoFitFrames allLessThanEqual 0 && hiddenFramesCannotSkipItems <= 0
736734
}
737735
}
738736

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

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

35
import glm_.has
@@ -525,7 +527,7 @@ object ShowDemoWindowWidgets {
525527
operator fun invoke() {
526528
treeNode("Trees") {
527529
treeNode("Basic trees") {
528-
for (i in 0..4) {
530+
for (i in 0..<5) {
529531
// Use SetNextItemOpen() so set the default state of a node to be open. We could
530532
// also use TreeNodeEx() with the ImGuiTreeNodeFlags_DefaultOpen flag to achieve the same thing!
531533
if (i == 0)
@@ -632,7 +634,7 @@ object ShowDemoWindowWidgets {
632634

633635
object Text {
634636
var wrapWidth = 200f
635-
val buf = "日本語".toByteArray(32) // "nihongo"
637+
val buf = utf8(0xe6, 0x97, 0xa5, 0xe6, 0x9c, 0xac, 0xe8, 0xaa, 0x9e).toByteArray(32) // "nihongo"
636638
operator fun invoke() {
637639
treeNode("Text") {
638640
treeNode("Color Text") {
@@ -684,8 +686,8 @@ object ShowDemoWindowWidgets {
684686
"CJK text will only appear if the font was loaded with the appropriate CJK character ranges." +
685687
"Call io.Fonts->AddFontFromFileTTF() manually to load extra character ranges." +
686688
"Read docs/FONTS.txt for details.")
687-
text("Hiragana: \u304b\u304d\u304f\u3051\u3053 (kakikukeko)") // Normally we would use u8"blah blah" with the proper characters directly in the string.
688-
text("Kanjis: \u65e5\u672c\u8a9e (nihongo)")
689+
text("Hiragana: ${utf8(0xe3, 0x81, 0x8b, 0xe3, 0x81, 0x8d, 0xe3, 0x81, 0x8f, 0xe3, 0x81, 0x91, 0xe3, 0x81, 0x93)} (kakikukeko)") // Normally we would use u8"blah blah" with the proper characters directly in the string.
690+
text("Kanjis: ${utf8(0xe6, 0x97, 0xa5, 0xe6, 0x9c, 0xac, 0xe8, 0xaa, 0x9e)} (nihongo)")
689691
inputText("UTF-8 input", buf)
690692
}
691693
}

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

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

35
import glm_.vec2.Vec2
@@ -74,7 +76,8 @@ internal interface loggingCapture {
7476
if (lineStart != lineEnd || !isLastLine) {
7577
val lineLength = lineEnd - lineStart
7678
val indentation = if (g.logLineFirstItem) treeDepth * 4 else 1
77-
logText(" ".repeat(indentation) + text.substring(lineStart, lineStart + lineLength))
79+
val t = text.encodeToByteArray().sliceArray(lineStart ..< lineStart + lineLength)
80+
logText(" ".repeat(indentation) + t.decodeToString())
7881
g.logLineFirstItem = false
7982
if (text.getOrNul(lineEnd) == '\n') {
8083
logText("\n")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ internal interface renderHelpers {
290290
fun findRenderedTextEnd(text: ByteArray, textBegin: Int = 0, textEnd: Int = text.size): Int {
291291
var textDisplayEnd = textBegin
292292
while (textDisplayEnd < textEnd && text[textDisplayEnd] != 0.b &&
293-
(text[textDisplayEnd + 0] != '#'.b || text[textDisplayEnd + 1] != '#'.b))
293+
(text[textDisplayEnd + 0] != '#'.b || (textDisplayEnd + 1 < textEnd && text[textDisplayEnd + 1] != '#'.b)))
294294
textDisplayEnd++
295295
return textDisplayEnd
296296
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal interface scrolling {
1818
infix fun Window.setScrollX(scrollX: Float) {
1919
scrollTarget.x = scrollX
2020
scrollTargetCenterRatio.x = 0f
21-
scrollTargetEdgeSnapDist.x = 0.0f
21+
scrollTargetEdgeSnapDist.x = 0f
2222
}
2323

2424
/** ~SetScrollY(ImGuiWindow* window, float new_scroll_y) */

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package imgui.internal.api
33
import glm_.func.common.max
44
import glm_.glm
55
import glm_.has
6+
import glm_.i
67
import glm_.vec2.Vec2
78
import imgui.*
89
import imgui.ImGui.calcTextSize
@@ -605,19 +606,22 @@ internal interface widgetsLowLevelBehaviors {
605606
treeNodeSetOpen(id, isOpen)
606607
} else {
607608
// We treat ImGuiCond_Once and ImGuiCond_FirstUseEver the same because tree node state are not saved persistently.
608-
val storedValue = storage.getOrElse(id) { -1 }
609+
val storedValue = storage[id]?.i ?: -1
609610
if (storedValue == -1) {
610611
isOpen = g.nextItemData.openVal
611612
storage[id] = isOpen
612613
treeNodeSetOpen(id, isOpen)
613-
} else isOpen = storedValue != 0
614+
} else
615+
isOpen = storedValue != 0
614616
}
615-
} else isOpen = storage[id] ?: (flags has Tnf.DefaultOpen)
617+
} else
618+
isOpen = storage[id] ?: (flags has Tnf.DefaultOpen)
616619

617620
/* When logging is enabled, we automatically expand tree nodes (but *NOT* collapsing headers.. seems like
618621
sensible behavior).
619622
NB- If we are above max depth we still allow manually opened nodes to be logged. */
620-
if (g.logEnabled && flags hasnt Tnf.NoAutoOpenOnLog && (window.dc.treeDepth - g.logDepthRef) < g.logDepthToExpand) isOpen = true
623+
if (g.logEnabled && flags hasnt Tnf.NoAutoOpenOnLog && (window.dc.treeDepth - g.logDepthRef) < g.logDepthToExpand)
624+
isOpen = true
621625

622626
return isOpen
623627
}

0 commit comments

Comments
 (0)