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

40 lines
1.9 KiB
Kotlin

package fr.username404.snowygui
import fr.username404.snowygui.gui.Element
import fr.username404.snowygui.gui.SnowyScreen
import fr.username404.snowygui.gui.elements.ClickBox
import fr.username404.snowygui.gui.elements.ClickButton
import fr.username404.snowygui.misc.addComponents
import net.minecraft.network.chat.TranslatableComponent
private var baseXAxis: Double = 4.0
internal fun newBox(translationKey: String): ClickBox {
val result = ClickBox(baseXAxis, 4.0, TranslatableComponent(translationKey))
baseXAxis += 86
return result
}
object ClickGui: SnowyScreen() {
private var GuiDragging: Boolean = false
override val components = mutableSetOf<Element>()
fun addComps(vararg toAdd: Element) = components.addAll(toAdd)
private inline fun boxContext(args: (ClickBox) -> Unit) = components.filterIsInstance<ClickBox>().forEach(args)
private inline fun buttonsContext(args: (ClickButton) -> Unit) = boxContext { it.buttons.values.forEach(args) }
override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { buttonsContext { it.mouseClicked(d, e, i) }; return false }
override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { buttonsContext { it.mouseReleased(d, e, i) }; return false }
override fun mouseScrolled(d: Double, e: Double, f: Double): Boolean { boxContext { it.scroll(f) }; return false }
override fun mouseDragged(d: Double, e: Double, i: Int, f: Double, g: Double): Boolean {
if (i == 0) {
components.forEach {
if (it.withinBounds(d, e) and !GuiDragging) {
GuiDragging = true
it.x = d.minus(f) + (d - (it.x + it.width))
it.y = e.minus(g) + (e - (it.y + it.height))
} else GuiDragging = false
}
}
return super.mouseDragged(d, e, i, f, g)
}
init { addComponents() }
}