@@ -4,52 +4,65 @@ import imgui.ImGui
4
4
import imgui.flag.ImGuiCol
5
5
import kotlin.reflect.KMutableProperty0
6
6
import me.zeroeightsix.kami.gui.ImguiDSL.addFrame
7
+ import me.zeroeightsix.kami.gui.ImguiDSL.calcTextSize
7
8
import me.zeroeightsix.kami.gui.ImguiDSL.colour
8
9
import me.zeroeightsix.kami.gui.ImguiDSL.cursorPosX
9
10
import me.zeroeightsix.kami.gui.ImguiDSL.get
10
11
import me.zeroeightsix.kami.gui.ImguiDSL.plus
12
+ import me.zeroeightsix.kami.gui.windows.Settings
13
+ import me.zeroeightsix.kami.util.Bind
11
14
12
- /* *
13
- * A button for entry of a single character. It will breathe while active to indicate that the button is consuming inputs.
14
- *
15
- * @return `true` if `char` was updated
16
- */
17
- fun charButton (strId : String , char : KMutableProperty0 <Char >, pressText : String? = "Press a key") {
18
- var value by char
15
+ private fun <T , R > inputButton (text : String , value : T , hoverText : String? , width : Float? = null, onHover : () -> R ): R ? {
19
16
val frameHeight = ImGui .getFrameHeight()
20
17
val framePaddingY = ImGui .getStyle().framePaddingY
21
18
val drawList = ImGui .getWindowDrawList()
19
+ @Suppress(" NAME_SHADOWING" ) val width = width ? : frameHeight
22
20
23
- ImguiDSL .withId(strId) {
24
- val cursorPos = ImguiDSL .cursorPos
25
- val (x, y) = ImguiDSL .windowPos + cursorPos
26
- cursorPosX + = (frameHeight - ImguiDSL .calcTextSize(value.toString()).x) / 2f
27
- ImGui .text(value.toString())
28
- var hovered = ImGui .isItemHovered()
29
- ImguiDSL .cursorPos = cursorPos
30
-
31
- ImguiDSL .invisibleButton(value.toString(), frameHeight, frameHeight) {}
32
-
33
- hovered = hovered || ImGui .isItemHovered()
34
- drawList.addFrame(
35
- x,
36
- y,
37
- x + frameHeight,
38
- y + frameHeight,
39
- (if (hovered) ImGui .getStyle()[ImGuiCol .FrameBgHovered ] else ImGui .getStyle()[ImGuiCol .FrameBg ]).colour
40
- )
21
+ val cursorPos = ImguiDSL .cursorPos
22
+ val (x, y) = ImguiDSL .windowPos + cursorPos
23
+ cursorPosX + = (width - calcTextSize(value.toString()).x) / 2f
24
+ ImGui .text(value.toString())
25
+ var hovered = ImGui .isItemHovered()
26
+ ImguiDSL .cursorPos = cursorPos
27
+
28
+ ImguiDSL .invisibleButton(value.toString(), width, frameHeight) {}
29
+
30
+ hovered = hovered || ImGui .isItemHovered()
31
+ drawList.addFrame(
32
+ x,
33
+ y,
34
+ x + width,
35
+ y + frameHeight,
36
+ (if (hovered) ImGui .getStyle()[ImGuiCol .FrameBgHovered ] else ImGui .getStyle()[ImGuiCol .FrameBg ]).colour
37
+ )
41
38
42
- ImGui .sameLine(0f , ImGui .getStyle().itemInnerSpacingX)
43
- ImguiDSL .cursorPosY + = (framePaddingY / 2f )
44
- ImGui .text(if (hovered) pressText else strId )
45
- ImguiDSL .cursorPosY - = (framePaddingY / 2f )
39
+ ImGui .sameLine(0f , ImGui .getStyle().itemInnerSpacingX)
40
+ ImguiDSL .cursorPosY + = (framePaddingY / 2f )
41
+ ImGui .text(if (hovered) hoverText else text )
42
+ ImguiDSL .cursorPosY - = (framePaddingY / 2f )
46
43
47
- if (hovered) {
48
- ImGui .captureKeyboardFromApp()
44
+ return if (hovered) onHover() else null
45
+ }
49
46
50
- val c = KamiImgui .charQueue.removeFirstOrNull() ? : return @withId
47
+ fun charButton (text : String , char : KMutableProperty0 <Char >, pressText : String? = "Type a character") {
48
+ inputButton(text, char.get(), pressText) {
49
+ ImGui .captureKeyboardFromApp()
51
50
52
- value = c.first
53
- }
51
+ char.set((KamiImgui .charQueue.removeFirstOrNull() ? : return @inputButton).first)
52
+ }
53
+ }
54
+
55
+ fun bindButton (text : String , bind : Bind , pressText : String? = "Press a key"): Bind ? {
56
+ val width = calcTextSize(bind.toString()).x.coerceAtLeast(20f )
57
+ return inputButton(text, bind, pressText, width + ImGui .getStyle().framePaddingX * 2f ) {
58
+ ImGui .captureKeyboardFromApp()
59
+ val c = KamiImgui .keyQueue.removeFirstOrNull() ? : return @inputButton null
60
+ val modifiers = Settings .modifiersEnabled
61
+ return @inputButton Bind (
62
+ ImGui .getIO().keyCtrl && modifiers,
63
+ ImGui .getIO().keyAlt && modifiers,
64
+ ImGui .getIO().keyShift && modifiers,
65
+ c
66
+ )
54
67
}
55
68
}
0 commit comments