Skip to content

Commit af4fc35

Browse files
committed
Tooltip: SetTooltip() is expanded immediately into a window, honoring current font / styling setting. Add internal mechanism to override tooltips (not exposed in BeginTooltip yet because bools are evil)
ocornut/imgui@138a9db
1 parent f7106f1 commit af4fc35

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

src/main/kotlin/imgui/Context.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ object Context {
160160
/** Distance between mouse and center of grab box, normalized in parent space. Use storage? */
161161
var scrollbarClickDeltaToGrabCenter = Vec2()
162162

163-
var tooltip = ""
163+
var tooltipOverrideCount = 0
164164
/** If no custom clipboard handler is defined */
165165
var privateClipboard = ""
166166
/** Cursor position request to the OS Input Method Editor */

src/main/kotlin/imgui/imgui/internal.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,6 @@ interface imgui_internal {
9999
assert(g.initialized) // Forgot to call ImGui::NewFrame()
100100
assert(g.frameCountEnded != g.frameCount) // ImGui::EndFrame() called multiple times, or forgot to call ImGui::NewFrame() again
101101

102-
// Render tooltip
103-
if (g.tooltip.isNotEmpty() && g.tooltip[0] != '\u0000') {
104-
ImGui.beginTooltip()
105-
ImGui.textUnformatted(g.tooltip)
106-
ImGui.endTooltip()
107-
}
108-
109102
// Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME)
110103
// if (IO.imeSetInputScreenPosFn && ImLengthSqr(g.OsImePosRequest - g.OsImePosSet) > 0.0001f) { TODO
111104
// g.IO.ImeSetInputScreenPosFn((int) g . OsImePosRequest . x, (int) g . OsImePosRequest . y)

src/main/kotlin/imgui/imgui/main.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interface imgui_main {
4343

4444
g.time += IO.deltaTime
4545
g.frameCount += 1
46-
g.tooltip = ""
46+
g.tooltipOverrideCount = 0
4747
g.overlayDrawList.clear()
4848
g.overlayDrawList.pushTextureId(IO.fonts.texId)
4949
g.overlayDrawList.pushClipRectFullScreen()
Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
package imgui.imgui
22

33
import imgui.*
4+
import imgui.ImGui.begin
45
import imgui.ImGui.currentWindowRead
5-
import imgui.Context.style
6+
import imgui.ImGui.end
7+
import imgui.ImGui.findWindowByName
8+
import imgui.ImGui.style
9+
import imgui.ImGui.textV
610
import imgui.Context as g
711

812
/** Tooltips */
913
interface imgui_tooltips {
1014

11-
/** set tooltip under mouse-cursor, typically use with ImGui::IsHovered(). last call wins
12-
*
13-
* Tooltip is stored and turned into a BeginTooltip()/EndTooltip() sequence at the end of the frame.
14-
* Each call override previous value.*/
15-
fun setTooltip(fmt: String, vararg values: Any) {
16-
g.tooltip = fmt.format(style.locale, *values)
15+
/** Not exposed publicly as BeginTooltip() because bool parameters are evil. Let's see if other needs arise first. */
16+
fun beginTooltipEx(overridePreviousTooltip: Boolean) {
17+
18+
var windowName = "##Tooltip%02d".format(style.locale, g.tooltipOverrideCount)
19+
if (overridePreviousTooltip)
20+
findWindowByName(windowName)?.let {
21+
if (it.active) {
22+
// Hide previous tooltips. We can't easily "reset" the content of a window so we create a new one.
23+
it.hiddenFrames = 1
24+
windowName = "##Tooltip%02d".format(++g.tooltipOverrideCount)
25+
}
26+
}
27+
begin(windowName, null, WindowFlags.Tooltip or WindowFlags.NoTitleBar or WindowFlags.NoMove or
28+
WindowFlags.NoResize or WindowFlags.NoSavedSettings or WindowFlags.AlwaysAutoResize)
1729
}
1830

19-
/** use to create full-featured tooltip windows that aren't just text */
20-
fun beginTooltip() {
21-
val flags = WindowFlags.Tooltip or WindowFlags.NoTitleBar or WindowFlags.NoMove or WindowFlags.NoResize or
22-
WindowFlags.NoSavedSettings or WindowFlags.AlwaysAutoResize
23-
ImGui.begin("##Tooltip", null, flags)
31+
fun setTooltipV(fmt: String, args: Array<out Any>) {
32+
beginTooltipEx(true)
33+
textV(fmt, args)
34+
endTooltip()
2435
}
2536

37+
fun setTooltip(fmt: String, vararg args: Any) = setTooltipV(fmt, args)
38+
39+
/** begin/append a tooltip window. to create full-featured tooltip (with any kind of contents). */
40+
fun beginTooltip() = beginTooltipEx(false)
41+
2642
fun endTooltip() {
2743
assert(currentWindowRead!!.flags has WindowFlags.Tooltip) // Mismatched BeginTooltip()/EndTooltip() calls
28-
ImGui.end()
44+
end()
2945
}
3046
}

src/main/kotlin/imgui/imgui/widgets.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import glm_.i
66
import glm_.vec2.Vec2
77
import glm_.vec4.Vec4
88
import imgui.*
9+
import imgui.Context.style
910
import imgui.ImGui.F32_TO_INT8_SAT
1011
import imgui.ImGui.F32_TO_INT8_UNBOUND
1112
import imgui.ImGui.beginGroup
@@ -58,7 +59,6 @@ import imgui.ImGui.spacing
5859
import imgui.ImGui.textLineHeight
5960
import imgui.internal.*
6061
import imgui.Context as g
61-
import imgui.Context.style
6262

6363
interface imgui_widgets {
6464

@@ -82,7 +82,7 @@ interface imgui_widgets {
8282
/** shortcut for PushStyleColor(ImGuiCol_Text, col); Text(fmt, ...); PopStyleColor(); */
8383
fun textColored(col: Vec4, fmt: String, vararg args: Any) {
8484
pushStyleColor(Col.Text, col)
85-
text(fmt, args)
85+
text(fmt, *args)
8686
popStyleColor()
8787
}
8888

0 commit comments

Comments
 (0)