Add components in another file called addComponents.kt

This commit is contained in:
Username404-59 2021-05-01 21:32:24 +02:00
parent 9dc097d32f
commit 1551d70e88
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
4 changed files with 37 additions and 14 deletions

View File

@ -3,25 +3,24 @@ 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: MutableSet<Element> = mutableSetOf(
ClickBox(4.0, 4.0, TranslatableComponent("snowy.clickbox.misc"), mutableListOf(
ClickButton(color = 0x6C9E9D, Title = "Fullbright"),
ClickButton(color = 0x6C9E9D)
)),
ClickBox(90.0, 4.0, TranslatableComponent("snowy.clickbox.rendering"), mutableListOf(
ClickButton(color = 0x6C9E9D),
ClickButton(color = 0x6C9E9D)
))
)
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)
override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { boxContext { it.buttons.forEach { elem -> elem.mouseClicked(d, e, i) } }; return false }
override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { boxContext { it.buttons.forEach { elem -> elem.mouseReleased(d, e, i) } }; return false }
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
@ -40,4 +39,5 @@ object ClickGui: SnowyScreen() {
}
return super.mouseDragged(d, e, i, f, g)
}
init { addComponents() }
}

View File

@ -6,7 +6,7 @@ import net.minecraft.network.chat.TranslatableComponent
import kotlin.properties.Delegates
abstract class SnowyScreen(translatableString: String = "screen.snowy.gui", private val willPauseScreen: Boolean = false): Screen(TranslatableComponent(translatableString)) {
open val components: MutableSet<Element> by Delegates.notNull()
protected open val components: MutableSet<Element> by Delegates.notNull()
override fun render(poseStack: PoseStack?, i: Int, j: Int, f: Float) {
if (poseStack != null) {
components.forEach {

View File

@ -18,6 +18,14 @@ class ClickBox(
private val name: TranslatableComponent? = null
): ColoredElement(x, y, 80, 10, 0x6C9E9D, 0.5F) {
val buttons = mutableMapOf<String, ClickButton>()
fun addButtons(vararg collect: Pair<String, () -> Unit>, color: Int = 0x6C9E9D): ClickBox {
buttons.putAll(
collect.map {
it.first to ClickButton(Title = it.first, action = it.second, color = color)
}
)
return this
}
@JvmField
val scrollBar: ColoredElement = object: ColoredElement(
(x + width), y + height + 3,

View File

@ -0,0 +1,15 @@
package fr.username404.snowygui.misc
import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.newBox
fun ClickGui.addComponents() {
addComps(
newBox("snowy.clickbox.misc").addButtons(
"Fullbright" to {
// TODO Fullbright
}
),
newBox("snowy.clickbox.rendering").addButtons()
)
}