SnowyGUI/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt

32 lines
1.5 KiB
Kotlin

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<Element>()
val clickBoxes get() = components.filterIsInstance<ClickBox>()
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()
} }
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 }
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, e) && !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
}
}
return super.mouseDragged(d, e, i, f, g)
}
}