From e28daf3b4ae5d1e74ff65d35a0afd666040db5e0 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 21 Jun 2019 19:02:08 -0400 Subject: [PATCH] Remove UI prefix, start Window implementation --- src/main/kotlin/net/shadowfacts/asmr/ASMR.kt | 2 +- .../kotlin/net/shadowfacts/asmr/TestScreen.kt | 52 ++++++++++++++++++ .../kotlin/net/shadowfacts/asmr/UITest.kt | 54 ------------------- .../shadowfacts/shadowui/LayoutVariable.kt | 2 +- .../kotlin/net/shadowfacts/shadowui/Screen.kt | 27 ++++++++++ .../net/shadowfacts/shadowui/UIScreen.kt | 28 ---------- .../shadowui/{UIView.kt => View.kt} | 24 +++++---- .../kotlin/net/shadowfacts/shadowui/Window.kt | 30 +++++++++++ .../shadowfacts/shadowui/geometry/Point.kt | 7 +++ .../shadowui/geometry/{UIRect.kt => Rect.kt} | 2 +- .../shadowfacts/shadowui/util/RenderHelper.kt | 4 +- 11 files changed, 134 insertions(+), 98 deletions(-) create mode 100644 src/main/kotlin/net/shadowfacts/asmr/TestScreen.kt delete mode 100644 src/main/kotlin/net/shadowfacts/asmr/UITest.kt create mode 100644 src/main/kotlin/net/shadowfacts/shadowui/Screen.kt delete mode 100644 src/main/kotlin/net/shadowfacts/shadowui/UIScreen.kt rename src/main/kotlin/net/shadowfacts/shadowui/{UIView.kt => View.kt} (77%) create mode 100644 src/main/kotlin/net/shadowfacts/shadowui/Window.kt create mode 100644 src/main/kotlin/net/shadowfacts/shadowui/geometry/Point.kt rename src/main/kotlin/net/shadowfacts/shadowui/geometry/{UIRect.kt => Rect.kt} (69%) diff --git a/src/main/kotlin/net/shadowfacts/asmr/ASMR.kt b/src/main/kotlin/net/shadowfacts/asmr/ASMR.kt index c20d431..2c514be 100644 --- a/src/main/kotlin/net/shadowfacts/asmr/ASMR.kt +++ b/src/main/kotlin/net/shadowfacts/asmr/ASMR.kt @@ -13,7 +13,7 @@ object ASMR: ModInitializer { CommandRegistry.INSTANCE.register(false) { dispatcher -> val command = CommandManager.literal("uitest").executes { try { - MinecraftClient.getInstance().openScreen(UITest()) + MinecraftClient.getInstance().openScreen(TestScreen()) } catch (e: Throwable) { e.printStackTrace() } diff --git a/src/main/kotlin/net/shadowfacts/asmr/TestScreen.kt b/src/main/kotlin/net/shadowfacts/asmr/TestScreen.kt new file mode 100644 index 0000000..f234109 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/asmr/TestScreen.kt @@ -0,0 +1,52 @@ +package net.shadowfacts.asmr + +import net.shadowfacts.kiwidsl.dsl +import net.shadowfacts.shadowui.Screen +import net.shadowfacts.shadowui.View +import net.shadowfacts.shadowui.Window +import net.shadowfacts.shadowui.util.Color + +class TestScreen: Screen() { + + init { + windows.add(Window().apply { + val red = addView(View().apply { + backgroundColor = Color(0xff0000) + }) + val green = addView(View().apply { + backgroundColor = Color(0x00ff00) + }) + val blue = addView(View().apply { + backgroundColor = Color(0x0000ff) + }) + val purple = green.addSubview(View().apply { + backgroundColor = Color(0x800080) + }) + + solver.dsl { + red.leftAnchor equalTo 0 + red.widthAnchor equalTo 200 + red.topAnchor equalTo 0 + red.heightAnchor equalTo 100 + + green.leftAnchor equalTo (red.leftAnchor + red.widthAnchor + 20) + green.widthAnchor equalTo red.widthAnchor + green.topAnchor equalTo 0 + green.heightAnchor equalTo (red.heightAnchor + 100) + + blue.leftAnchor equalTo green.leftAnchor + blue.widthAnchor equalTo green.widthAnchor + blue.topAnchor equalTo (green.topAnchor + green.heightAnchor) + blue.heightAnchor equalTo 50 + + purple.widthAnchor equalTo 100 + purple.heightAnchor equalTo 100 + purple.centerXAnchor equalTo green.centerXAnchor + purple.centerYAnchor equalTo green.centerYAnchor + } + + layout() + }) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/asmr/UITest.kt b/src/main/kotlin/net/shadowfacts/asmr/UITest.kt deleted file mode 100644 index 5c682af..0000000 --- a/src/main/kotlin/net/shadowfacts/asmr/UITest.kt +++ /dev/null @@ -1,54 +0,0 @@ -package net.shadowfacts.asmr - -import net.shadowfacts.kiwidsl.dsl -import net.shadowfacts.shadowui.UIScreen -import net.shadowfacts.shadowui.UIView -import net.shadowfacts.shadowui.util.Color - -class UITest: UIScreen() { - - init { - val red = UIView().apply { - backgroundColor = Color(0xff0000) - addView(this) - } - val green = UIView().apply { - backgroundColor = Color(0x00ff00) - addView(this) - } - val blue = UIView().apply { - backgroundColor = Color(0x0000ff) - addView(this) - } - - val purple = UIView().apply { - backgroundColor = Color(0x800080) - green.addSubview(this) - } - - solver.dsl { - red.leftAnchor equalTo 0 - red.widthAnchor equalTo 200 - red.topAnchor equalTo 0 - red.heightAnchor equalTo 100 - - green.leftAnchor equalTo (red.leftAnchor + red.widthAnchor + 20) - green.widthAnchor equalTo red.widthAnchor - green.topAnchor equalTo 0 - green.heightAnchor equalTo (red.heightAnchor + 100) - - blue.leftAnchor equalTo green.leftAnchor - blue.widthAnchor equalTo green.widthAnchor - blue.topAnchor equalTo (green.topAnchor + green.heightAnchor) - blue.heightAnchor equalTo 50 - - purple.widthAnchor equalTo 100 - purple.heightAnchor equalTo 100 - purple.centerXAnchor equalTo green.centerXAnchor - purple.centerYAnchor equalTo green.centerYAnchor - } - - layout() - } - -} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/shadowui/LayoutVariable.kt b/src/main/kotlin/net/shadowfacts/shadowui/LayoutVariable.kt index deff55b..57e0364 100644 --- a/src/main/kotlin/net/shadowfacts/shadowui/LayoutVariable.kt +++ b/src/main/kotlin/net/shadowfacts/shadowui/LayoutVariable.kt @@ -2,7 +2,7 @@ package net.shadowfacts.shadowui import no.birkett.kiwi.Variable -class LayoutVariable(val owner: UIView, val property: String): Variable("LayoutVariable") { +class LayoutVariable(val owner: View, val property: String): Variable("LayoutVariable") { override fun getName() = "$owner.$property" diff --git a/src/main/kotlin/net/shadowfacts/shadowui/Screen.kt b/src/main/kotlin/net/shadowfacts/shadowui/Screen.kt new file mode 100644 index 0000000..42e2818 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/shadowui/Screen.kt @@ -0,0 +1,27 @@ +package net.shadowfacts.shadowui + +import net.minecraft.client.gui.screen.Screen +import net.minecraft.network.chat.TextComponent +import net.shadowfacts.shadowui.geometry.Point +import no.birkett.kiwi.Solver + +open class Screen: Screen(TextComponent("Screen")) { + + val windows = mutableListOf() + + fun addWindow(window: Window, index: Int? = null) { + if (index != null) { + windows.add(index, window) + } else { + windows.add(window) + } + } + + override fun render(mouseX: Int, mouseY: Int, delta: Float) { + val mouse = Point(mouseX, mouseY) + windows.forEach { + it.draw(mouse, delta) + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/shadowui/UIScreen.kt b/src/main/kotlin/net/shadowfacts/shadowui/UIScreen.kt deleted file mode 100644 index d500acc..0000000 --- a/src/main/kotlin/net/shadowfacts/shadowui/UIScreen.kt +++ /dev/null @@ -1,28 +0,0 @@ -package net.shadowfacts.shadowui - -import net.minecraft.client.gui.screen.Screen -import net.minecraft.network.chat.TextComponent -import no.birkett.kiwi.Solver - -open class UIScreen: Screen(TextComponent("UIScreen")) { - - val solver = Solver() - - val views = mutableListOf() - - fun addView(view: UIView) { - views.add(view) - view.solver = solver - view.wasAdded() - } - - fun layout() { - solver.updateVariables() - views.forEach(UIView::didLayout) - } - - override fun render(mouseX: Int, mouseY: Int, delta: Float) { - views.forEach(UIView::draw) - } - -} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/shadowui/UIView.kt b/src/main/kotlin/net/shadowfacts/shadowui/View.kt similarity index 77% rename from src/main/kotlin/net/shadowfacts/shadowui/UIView.kt rename to src/main/kotlin/net/shadowfacts/shadowui/View.kt index 78e0028..944dfbb 100644 --- a/src/main/kotlin/net/shadowfacts/shadowui/UIView.kt +++ b/src/main/kotlin/net/shadowfacts/shadowui/View.kt @@ -2,12 +2,12 @@ package net.shadowfacts.shadowui import com.mojang.blaze3d.platform.GlStateManager import net.shadowfacts.kiwidsl.dsl -import net.shadowfacts.shadowui.geometry.UIRect +import net.shadowfacts.shadowui.geometry.Rect import net.shadowfacts.shadowui.util.Color import net.shadowfacts.shadowui.util.RenderHelper import no.birkett.kiwi.Solver -class UIView { +class View { lateinit var solver: Solver @@ -22,21 +22,23 @@ class UIView { val centerYAnchor = LayoutVariable(this, "centerY") // The rectangle for this view in the coordinate system of the parent view. - lateinit var frame: UIRect + lateinit var frame: Rect // The rectangle for this view in its own coordinate system. - lateinit var bounds: UIRect + lateinit var bounds: Rect var backgroundColor = Color(0xff0000) - var parent: UIView? = null - val subviews = mutableListOf() + var parent: View? = null + val subviews = mutableListOf() - fun addSubview(view: UIView) { + fun addSubview(view: View): View { subviews.add(view) view.parent = this view.solver = solver view.wasAdded() + + return view } fun wasAdded() { @@ -53,12 +55,12 @@ class UIView { } fun didLayout() { - subviews.forEach(UIView::didLayout) + subviews.forEach(View::didLayout) val parentLeft = parent?.leftAnchor?.value ?: 0.0 val parentTop = parent?.topAnchor?.value ?: 0.0 - frame = UIRect(leftAnchor.value - parentLeft, topAnchor.value - parentTop, widthAnchor.value, heightAnchor.value) - bounds = UIRect(0.0, 0.0, widthAnchor.value, heightAnchor.value) + frame = Rect(leftAnchor.value - parentLeft, topAnchor.value - parentTop, widthAnchor.value, heightAnchor.value) + bounds = Rect(0.0, 0.0, widthAnchor.value, heightAnchor.value) } fun draw() { @@ -69,7 +71,7 @@ class UIView { drawContent() - subviews.forEach(UIView::draw) + subviews.forEach(View::draw) GlStateManager.popMatrix() } diff --git a/src/main/kotlin/net/shadowfacts/shadowui/Window.kt b/src/main/kotlin/net/shadowfacts/shadowui/Window.kt new file mode 100644 index 0000000..4c38fd9 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/shadowui/Window.kt @@ -0,0 +1,30 @@ +package net.shadowfacts.shadowui + +import net.shadowfacts.shadowui.geometry.Point +import no.birkett.kiwi.Solver + +class Window { + + var solver = Solver() + + val views = mutableListOf() + + fun addView(view: T): T { + views.add(view) + view.solver = solver + + view.wasAdded() + + return view + } + + fun layout() { + solver.updateVariables() + views.forEach(View::didLayout) + } + + fun draw(mouse: Point, delta: Float) { + views.forEach(View::draw) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/shadowui/geometry/Point.kt b/src/main/kotlin/net/shadowfacts/shadowui/geometry/Point.kt new file mode 100644 index 0000000..9f1ca88 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/shadowui/geometry/Point.kt @@ -0,0 +1,7 @@ +package net.shadowfacts.shadowui.geometry + +class Point(val x: Double, val y: Double) { + + constructor(x: Int, y: Int): this(x.toDouble(), y.toDouble()) + +} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/shadowui/geometry/UIRect.kt b/src/main/kotlin/net/shadowfacts/shadowui/geometry/Rect.kt similarity index 69% rename from src/main/kotlin/net/shadowfacts/shadowui/geometry/UIRect.kt rename to src/main/kotlin/net/shadowfacts/shadowui/geometry/Rect.kt index 6d3876b..92c7e1e 100644 --- a/src/main/kotlin/net/shadowfacts/shadowui/geometry/UIRect.kt +++ b/src/main/kotlin/net/shadowfacts/shadowui/geometry/Rect.kt @@ -1,6 +1,6 @@ package net.shadowfacts.shadowui.geometry -data class UIRect(val left: Double, val top: Double, val width: Double, val height: Double) { +data class Rect(val left: Double, val top: Double, val width: Double, val height: Double) { val right: Double get() = left + width diff --git a/src/main/kotlin/net/shadowfacts/shadowui/util/RenderHelper.kt b/src/main/kotlin/net/shadowfacts/shadowui/util/RenderHelper.kt index 914e0f3..dc0db71 100644 --- a/src/main/kotlin/net/shadowfacts/shadowui/util/RenderHelper.kt +++ b/src/main/kotlin/net/shadowfacts/shadowui/util/RenderHelper.kt @@ -1,11 +1,11 @@ package net.shadowfacts.shadowui.util import net.minecraft.client.gui.DrawableHelper -import net.shadowfacts.shadowui.geometry.UIRect +import net.shadowfacts.shadowui.geometry.Rect object RenderHelper { - fun fill(rect: UIRect, color: Color) { + fun fill(rect: Rect, color: Color) { DrawableHelper.fill(rect.left.toInt(), rect.top.toInt(), rect.right.toInt(), rect.bottom.toInt(), color.argb) }