diff --git a/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt b/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt index 6106b10..0bf4aaf 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt @@ -14,17 +14,29 @@ object ClickGui: SnowyScreen() { private inline fun buttonsContext(args: ColoredElement.() -> Unit) = boxContext { buttons.forEach { if (it.y > y + height) it.args() } } - override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { buttonsContext { this.mouseClicked(d, e, i) }; return false } - override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { buttonsContext { this.mouseReleased(d, e, i) }; return false } + + private var draggingBox: String? = null + private inline fun currentBoxContext(args: ClickBox.() -> Unit): Unit? = draggingBox?.run { boxContext { if (name.string == draggingBox) args() } } + private var offsetX: Double = 0.0; + private var offsetY: Double = 0.0; + override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { + if (i == GLFW.GLFW_MOUSE_BUTTON_LEFT) { + clickBoxes.find { it.isWithinBounds(d, e) }?.let { draggingBox = it.name.string } + currentBoxContext { + offsetX = d - (x + width) + offsetY = e - (y + height) + } + } + buttonsContext { this.mouseClicked(d, e, i) }; return super.mouseClicked(d, e, i); + } + override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { draggingBox = null; buttonsContext { this.mouseReleased(d, e, i) }; return false } override fun mouseScrolled(d: Double, e: Double, f: Double): Boolean { boxContext { scroll(d, e, f) }; return false } + override fun mouseDragged(d: Double, e: Double, i: Int, f: Double, g: Double): Boolean { if (i == GLFW.GLFW_MOUSE_BUTTON_LEFT) { - components.forEach { - if (it.isWithinBounds(d.minus(f), e.minus(g)) && !isDragging) { - isDragging = true - it.x = d.minus(f) + (d - (it.x + it.width)) - it.y = e.minus(g) + (e - (it.y + it.height)) - } else isDragging = false + currentBoxContext { + x = (d + offsetX).coerceIn(0.0..this@ClickGui.width - width.toDouble()) + y = (e + offsetY).coerceIn(0.0..this@ClickGui.height - height.toDouble()) } } return super.mouseDragged(d, e, i, f, g)