Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 57e2ac0

Browse files
committed
feat: Added a Bind button to all modules in GUI
The bind button works similarly to the char button for command prefix. This commit also introduces a `keyQueue`, just like charButton's `charQueue`.
1 parent 1b8ec00 commit 57e2ac0

File tree

5 files changed

+60
-47
lines changed

5 files changed

+60
-47
lines changed

src/main/kotlin/me/zeroeightsix/kami/feature/FullFeature.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ package me.zeroeightsix.kami.feature
55
import io.github.fablabsmc.fablabs.api.fiber.v1.annotation.Listener
66
import io.github.fablabsmc.fablabs.api.fiber.v1.annotation.Setting
77
import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigBranch
8+
import java.lang.Boolean as JavaBoolean
89
import me.zero.alpine.listener.EventHandler
910
import me.zero.alpine.listener.EventHook
1011
import me.zero.alpine.listener.Listenable
12+
import me.zero.alpine.listener.Listener as AlpineListener
1113
import me.zeroeightsix.kami.KamiMod
1214
import me.zeroeightsix.kami.event.BindEvent
1315
import me.zeroeightsix.kami.setting.SettingVisibility
1416
import me.zeroeightsix.kami.then
1517
import me.zeroeightsix.kami.util.Bind
16-
import me.zero.alpine.listener.Listener as AlpineListener
17-
import java.lang.Boolean as JavaBoolean
1818

1919
open class FullFeature(
2020
override var name: String = "No name",
@@ -48,7 +48,6 @@ open class FullFeature(
4848
}
4949

5050
@Setting(name = "Bind")
51-
@SettingVisibility.Constant(false)
5251
override var bind = Bind.none()
5352

5453
@Setting
@@ -110,4 +109,4 @@ open class FullFeature(
110109

111110
open fun onEnable() {}
112111
open fun onDisable() {}
113-
}
112+
}

src/main/kotlin/me/zeroeightsix/kami/gui/ImGuiScreen.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import imgui.ImGui
44
import imgui.flag.ImGuiConfigFlags
55
import me.zeroeightsix.kami.gui.ImguiDSL.without
66
import me.zeroeightsix.kami.mc
7+
import me.zeroeightsix.kami.util.Bind
78
import net.minecraft.client.gui.screen.Screen
9+
import net.minecraft.client.util.InputUtil
810
import net.minecraft.text.Text
911
import org.lwjgl.glfw.GLFW
1012

@@ -28,6 +30,8 @@ abstract class ImGuiScreen(title: Text) : Screen(title) {
2830
val returned = super.keyPressed(keyCode, scanCode, modifiers)
2931
if (!returned) {
3032
KamiImgui.imguiGlfw.keyCallback(mc.window.handle, keyCode, scanCode, GLFW.GLFW_PRESS, modifiers)
33+
34+
KamiImgui.keyQueue.add(Bind.Code(InputUtil.fromKeyCode(keyCode, scanCode)))
3135
}
3236
return returned
3337
}

src/main/kotlin/me/zeroeightsix/kami/gui/KImGui.kt

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,65 @@ import imgui.ImGui
44
import imgui.flag.ImGuiCol
55
import kotlin.reflect.KMutableProperty0
66
import me.zeroeightsix.kami.gui.ImguiDSL.addFrame
7+
import me.zeroeightsix.kami.gui.ImguiDSL.calcTextSize
78
import me.zeroeightsix.kami.gui.ImguiDSL.colour
89
import me.zeroeightsix.kami.gui.ImguiDSL.cursorPosX
910
import me.zeroeightsix.kami.gui.ImguiDSL.get
1011
import me.zeroeightsix.kami.gui.ImguiDSL.plus
12+
import me.zeroeightsix.kami.gui.windows.Settings
13+
import me.zeroeightsix.kami.util.Bind
1114

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? {
1916
val frameHeight = ImGui.getFrameHeight()
2017
val framePaddingY = ImGui.getStyle().framePaddingY
2118
val drawList = ImGui.getWindowDrawList()
19+
@Suppress("NAME_SHADOWING") val width = width ?: frameHeight
2220

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+
)
4138

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)
4643

47-
if (hovered) {
48-
ImGui.captureKeyboardFromApp()
44+
return if (hovered) onHover() else null
45+
}
4946

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()
5150

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+
)
5467
}
5568
}

src/main/kotlin/me/zeroeightsix/kami/gui/KamiImgui.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import me.zeroeightsix.kami.KamiMod
1414
import me.zeroeightsix.kami.gui.windows.Settings
1515
import me.zeroeightsix.kami.mc
1616
import me.zeroeightsix.kami.tryOrNull
17+
import me.zeroeightsix.kami.util.Bind
1718
import net.minecraft.client.util.math.MatrixStack
1819
import org.lwjgl.opengl.GL
1920

@@ -23,6 +24,7 @@ object KamiImgui {
2324
// Anything pasted in by the clipboard will not appear in this queue.
2425
// It is cleared after each `frame` call.
2526
val charQueue = mutableListOf<Pair<Char, Int/*keycode*/>>()
27+
val keyQueue = mutableListOf<Bind.Code>()
2628
val imguiGlfw: ImGuiImplGlfw = ImGuiImplGlfw()
2729
private val imguiGl: ImGuiImplGl3 = ImGuiImplGl3()
2830
private val postDrawStack: Stack<(MatrixStack) -> Unit> = Stack()
@@ -119,6 +121,7 @@ object KamiImgui {
119121
}
120122
}
121123
charQueue.clear()
124+
keyQueue.clear()
122125
}
123126
}
124127

src/main/kotlin/me/zeroeightsix/kami/setting/KamiConfig.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.mojang.authlib.GameProfile
44
import imgui.ImGui.colorEdit4
55
import imgui.ImGui.dragScalar
66
import imgui.ImGui.inputText
7-
import imgui.ImGui.sameLine
87
import imgui.ImGui.text
98
import imgui.flag.ImGuiColorEditFlags
109
import imgui.flag.ImGuiDataType
@@ -74,11 +73,11 @@ import me.zeroeightsix.kami.feature.FindSettings
7473
import me.zeroeightsix.kami.feature.FullFeature
7574
import me.zeroeightsix.kami.feature.HasConfig
7675
import me.zeroeightsix.kami.feature.module.Module
77-
import me.zeroeightsix.kami.gui.ImguiDSL.button
7876
import me.zeroeightsix.kami.gui.ImguiDSL.checkbox
7977
import me.zeroeightsix.kami.gui.ImguiDSL.combo
8078
import me.zeroeightsix.kami.gui.ImguiDSL.imgui
8179
import me.zeroeightsix.kami.gui.ImguiDSL.wrapImFloat
80+
import me.zeroeightsix.kami.gui.bindButton
8281
import me.zeroeightsix.kami.gui.text.CompiledText
8382
import me.zeroeightsix.kami.gui.text.VarMap
8483
import me.zeroeightsix.kami.gui.widgets.PinnableWidget
@@ -536,13 +535,8 @@ object KamiConfig {
536535
}
537536
)
538537
.extend(
539-
{ _, bind ->
540-
text("Bound to $bind") // TODO: Highlight bind in another color?
541-
sameLine(0f, -1f)
542-
button("Bind") { // TODO: Bind popup?
543-
// Maybe just display "Press a key" instead of the normal "Bound to ...", and wait for a key press.
544-
}
545-
null
538+
{ name, bind ->
539+
return@extend bindButton(name, bind)
546540
},
547541
{ _, b ->
548542
val range = 0..b.remaining.lastIndexOf('+')

0 commit comments

Comments
 (0)