package fr.username404.snowygui import fr.username404.snowygui.gui.ColoredElement import fr.username404.snowygui.gui.Element import fr.username404.snowygui.gui.SnowyScreen import fr.username404.snowygui.gui.elements.ClickBox import org.lwjgl.glfw.GLFW object ClickGui: SnowyScreen() { override val components = mutableSetOf() val clickBoxes get() = components.filterIsInstance() inline fun boxContext(args: ClickBox.() -> Unit) = clickBoxes.forEach(args) private inline fun buttonsContext(args: ColoredElement.() -> Unit) = boxContext { buttons.forEach { if (it.y > y + height) it.args() } } 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, scrollY: Double): Boolean { boxContext { scroll(d, e, scrollY) }; return false } override fun mouseDragged(d: Double, e: Double, i: Int, f: Double, g: Double): Boolean { if (i == GLFW.GLFW_MOUSE_BUTTON_LEFT) { 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) } }