Skip to content

Commit d4cb76c

Browse files
committed
HUD: Visual Grid, Locking and Visibility Toggle
1 parent be7b501 commit d4cb76c

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

src/main/kotlin/com/lambda/gui/MenuBar.kt

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.lambda.core.Loader
2626
import com.lambda.event.EventFlow
2727
import com.lambda.graphics.texture.TextureOwner.upload
2828
import com.lambda.gui.DearImGui.EXTERNAL_LINK
29+
import com.lambda.gui.components.HudGuiLayout
2930
import com.lambda.gui.components.QuickSearch
3031
import com.lambda.gui.dsl.ImGuiBuilder
3132
import com.lambda.module.ModuleRegistry
@@ -194,26 +195,11 @@ object MenuBar {
194195
}
195196

196197
private fun ImGuiBuilder.buildHudMenu() {
197-
menuItem("Open Editor", enabled = false) {
198-
// ToDo (HUD Editor Window):
199-
// - Full-screen canvas with grid; left "Elements" list; right "Properties" inspector.
200-
// - Drag & drop, snap grid, lock/unlock, safe margins, anchors, multi-select & alignment tools.
198+
menuItem(if (HudGuiLayout.isLocked) "Unlock" else "Lock") {
199+
HudGuiLayout.isLocked = !HudGuiLayout.isLocked
201200
}
202-
menu("Layouts") {
203-
// ToDo:
204-
// - New/Save/Save As/Load/Import/Export layout actions; Toggle "Autosave on change".
205-
menuItem("New...", enabled = false) {}
206-
menuItem("Save", enabled = false) {}
207-
menuItem("Save As...", enabled = false) {}
208-
menuItem("Load...", enabled = false) {}
209-
menuItem("Import...", enabled = false) {}
210-
menuItem("Export...", enabled = false) {}
211-
separator()
212-
menuItem("Autosave on change", selected = true, enabled = false) {}
213-
}
214-
menuItem("Toggle Edit Handles", selected = true, enabled = false) {
215-
// ToDo:
216-
// - Show/hide bounds, anchors, labels while in edit mode.
201+
menuItem(if (HudGuiLayout.isShownInGUI) "Hide" else "Show") {
202+
HudGuiLayout.isShownInGUI = !HudGuiLayout.isShownInGUI
217203
}
218204
}
219205

src/main/kotlin/com/lambda/gui/components/HudGuiLayout.kt

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import imgui.flag.ImDrawListFlags
3636
import imgui.flag.ImGuiWindowFlags
3737
import imgui.flag.ImGuiStyleVar
3838
import kotlin.math.PI
39+
import kotlin.math.max
3940

4041
object HudGuiLayout : Loadable {
4142
const val DEFAULT_HUD_FLAGS =
@@ -51,6 +52,9 @@ object HudGuiLayout : Loadable {
5152
private val pendingPositions = mutableMapOf<String, Pair<Float, Float>>()
5253
private val snapOverlays = mutableMapOf<String, SnapVisual>()
5354

55+
var isShownInGUI = true
56+
var isLocked = false
57+
5458
private data class SnapVisual(
5559
val snapX: Float?,
5660
val snapY: Float?,
@@ -65,6 +69,8 @@ object HudGuiLayout : Loadable {
6569

6670
init {
6771
listen<GuiEvent.NewFrame> {
72+
if (ClickGui.isEnabled && !isShownInGUI) return@listen
73+
6874
buildLayout {
6975
val vp = ImGui.getMainViewport()
7076
SnapManager.beginFrame(vp.sizeX, vp.sizeY, io.fontGlobalScale)
@@ -73,7 +79,8 @@ object HudGuiLayout : Loadable {
7379
val mousePressedThisFrame = mouseDown && !mouseWasDown
7480
val mouseReleasedThisFrame = !mouseDown && mouseWasDown
7581
mouseWasDown = mouseDown
76-
if (mouseReleasedThisFrame || !ClickGui.isEnabled) {
82+
83+
if (mouseReleasedThisFrame || !ClickGui.isEnabled || isLocked) {
7784
activeDragHudName = null
7885
}
7986

@@ -86,12 +93,11 @@ object HudGuiLayout : Loadable {
8693

8794
notShown.forEach { SnapManager.unregisterElement(it.name) }
8895

89-
if (ClickGui.isEnabled && activeDragHudName == null && mousePressedThisFrame) {
90-
tryBeginDrag(huds)
91-
}
92-
93-
if (ClickGui.isEnabled && activeDragHudName != null && mouseDown) {
94-
updateDragAndSnapping()
96+
val canDrag = ClickGui.isEnabled && !isLocked
97+
if (canDrag) {
98+
if (activeDragHudName == null && mousePressedThisFrame) { tryBeginDrag(huds) }
99+
if (activeDragHudName != null && mouseDown) updateDragAndSnapping()
100+
if (activeDragHudName != null) drawDragGrid()
95101
}
96102

97103
huds.forEach { hud ->
@@ -105,7 +111,7 @@ object HudGuiLayout : Loadable {
105111
val baseFlags = if (hasBg) {
106112
DEFAULT_HUD_FLAGS and ImGuiWindowFlags.NoBackground.inv()
107113
} else DEFAULT_HUD_FLAGS
108-
val hudFlags = if (!ClickGui.isEnabled) {
114+
val hudFlags = if (!ClickGui.isEnabled || isLocked) {
109115
baseFlags or ImGuiWindowFlags.NoMove
110116
} else baseFlags
111117

@@ -129,7 +135,7 @@ object HudGuiLayout : Loadable {
129135
)
130136
}
131137
with(hud) { buildLayout() }
132-
if (ClickGui.isEnabled) {
138+
if (canDrag) {
133139
drawHudCornerArcs(foregroundDrawList, windowPos.x, windowPos.y, windowSize.x, windowSize.y)
134140
}
135141
val rect = RectF(windowPos.x, windowPos.y, windowSize.x, windowSize.y)
@@ -176,11 +182,38 @@ object HudGuiLayout : Loadable {
176182
snapOverlays[id] = SnapVisual(snap.snapX, snap.snapY, snap.kindX, snap.kindY)
177183
}
178184

185+
private fun ImGuiBuilder.drawDragGrid() {
186+
if (!GuiSettings.snapEnabled || !GuiSettings.snapToGrid) return
187+
val vp = ImGui.getMainViewport()
188+
val step = max(4f, GuiSettings.gridSize * io.fontGlobalScale)
189+
if (step <= 0f) return
190+
191+
val x0 = vp.posX
192+
val y0 = vp.posY
193+
val x1 = vp.posX + vp.sizeX
194+
val y1 = vp.posY + vp.sizeY
195+
196+
val draw = backgroundDrawList
197+
val col = ImColor.rgba(255, 255, 255, 28)
198+
val thickness = 1f
199+
200+
var x = x0
201+
while (x <= x1 + 0.5f) {
202+
draw.addLine(x, y0, x, y1, col, thickness)
203+
x += step
204+
}
205+
var y = y0
206+
while (y <= y1 + 0.5f) {
207+
draw.addLine(x0, y, x1, y, col, thickness)
208+
y += step
209+
}
210+
}
211+
179212
private fun ImGuiBuilder.drawHudCornerArcs(draw: ImDrawList, x: Float, y: Float, w: Float, h: Float) {
180213
val baseRadius = GuiSettings.hudOutlineCornerRadius
181214
val rounding = if (baseRadius > 0f) baseRadius else style.windowRounding
182215
val inflate = GuiSettings.hudOutlineCornerInflate
183-
// Soft halo corners (gray, slightly smaller)
216+
// Soft halo corners
184217
drawCornerArcs(
185218
draw,
186219
x, y, w, h,

0 commit comments

Comments
 (0)