From 5d5ea4b5ead79d88ad294d5fca5ea71179fe57c1 Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Sun, 2 May 2021 12:41:54 +0200 Subject: [PATCH] Add a buttonsContext method to ClickGui.kt, and a WIP scroll function to ClickBox.kt --- .../kotlin/fr/username404/snowygui/ClickGui.kt | 15 ++++++--------- .../kotlin/fr/username404/snowygui/gui/Element.kt | 7 +++++-- .../username404/snowygui/gui/elements/ClickBox.kt | 7 ++++++- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt b/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt index 0582507..a29475a 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt @@ -3,6 +3,7 @@ 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 @@ -16,17 +17,13 @@ internal fun newBox(translationKey: String): ClickBox { object ClickGui: SnowyScreen() { private var GuiDragging: Boolean = false override val components = mutableSetOf() - fun addComps(vararg toAdd: Element) { components.addAll(toAdd) } + fun addComps(vararg toAdd: Element) = components.addAll(toAdd) private inline fun boxContext(args: (ClickBox) -> Unit) = components.filterIsInstance().forEach(args) - override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { boxContext { it.buttons.values.forEach { elem -> elem.mouseClicked(d, e, i) } }; return false } - override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { boxContext { it.buttons.values.forEach { elem -> elem.mouseReleased(d, e, i) } }; return false } - override fun mouseScrolled(d: Double, e: Double, f: Double): Boolean { - boxContext { - it.scrollBar.height // TODO Implement scrolling using the f argument - } - return false - } + 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 { diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/Element.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/Element.kt index c54be97..ba17042 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/Element.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/Element.kt @@ -24,7 +24,7 @@ fun interface Renderable { abstract class Element( @JvmField val xOrigin: Double, @JvmField val yOrigin: Double, - val width: Int, val height: Int + var width: Int, var height: Int ): Renderable { open var x = xOrigin; open var y = yOrigin internal fun withinBounds(coordinateX: Double, coordinateY: Double): Boolean = @@ -52,7 +52,10 @@ abstract class Element( var hidden: Boolean = false; protected set } -abstract class ColoredElement(x: Double, y: Double, width: Int, height: Int, protected open var color: Int, protected var opacity: Float) : Element(x, y, width, height) { +abstract class ColoredElement( + x: Double, y: Double, width: Int, height: Int, + protected open var color: Int, protected var opacity: Float, +) : Element(x, y, width, height) { companion object { const val TransparentColor: Int = -0x1 @JvmStatic protected fun VertexConsumer.colorIt(color: Int, opacity: Float = 1F): VertexConsumer { diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt index bde5ebc..2200d11 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt @@ -18,7 +18,7 @@ class ClickBox( private val name: TranslatableComponent? = null ): ColoredElement(x, y, 80, 10, 0x6C9E9D, 0.5F) { val buttons = mutableMapOf() - fun addButtons(vararg collect: Pair Unit>, color: Int = 0x6C9E9D): ClickBox { + fun addButtons(vararg collect: Pair Unit)?>, color: Int = 0x6C9E9D): ClickBox { buttons.putAll( collect.map { it.first to ClickButton(Title = it.first, action = it.second, color = color) @@ -41,6 +41,11 @@ class ClickBox( RenderSystem.enableTexture() } } + fun scroll(supplied: Double) { + with(scrollBar) { + // TODO Implement scrolling + } + } private companion object { const val inclination: Double = 2.5