diff --git a/src/main/kotlin/net/shadowfacts/cacao/AbstractCacaoScreen.kt b/src/main/kotlin/net/shadowfacts/cacao/AbstractCacaoScreen.kt new file mode 100644 index 0000000..9ec46a0 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/cacao/AbstractCacaoScreen.kt @@ -0,0 +1,17 @@ +package net.shadowfacts.cacao + +import net.shadowfacts.cacao.window.Window + +/** + * @author shadowfacts + */ +interface AbstractCacaoScreen { + + val windows: List + + fun addWindow(window: T, index: Int): T + fun addWindow(window: T): T + + fun removeWindow(window: Window) + +} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/cacao/CacaoHandledScreen.kt b/src/main/kotlin/net/shadowfacts/cacao/CacaoHandledScreen.kt new file mode 100644 index 0000000..ef10a71 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/cacao/CacaoHandledScreen.kt @@ -0,0 +1,93 @@ +package net.shadowfacts.cacao + +import net.minecraft.client.gui.screen.ingame.HandledScreen +import net.minecraft.client.util.math.MatrixStack +import net.minecraft.entity.player.PlayerInventory +import net.minecraft.screen.ScreenHandler +import net.minecraft.sound.SoundEvents +import net.minecraft.text.Text +import net.shadowfacts.cacao.geometry.Point +import net.shadowfacts.cacao.util.MouseButton +import net.shadowfacts.cacao.util.RenderHelper +import net.shadowfacts.cacao.window.ScreenHandlerWindow +import net.shadowfacts.cacao.window.Window +import java.util.* + +/** + * @author shadowfacts + */ +class CacaoHandledScreen( + handler: Handler, + playerInv: PlayerInventory, + title: Text, +): HandledScreen(handler, playerInv, title), AbstractCacaoScreen { + + private val _windows = LinkedList() + + override val windows: List = _windows + + override fun addWindow(window: T, index: Int): T { + if (window is ScreenHandlerWindow && window.screenHandler != handler) { + throw RuntimeException("Adding ScreenHandlerWindow to CacaoHandledScreen with different screen handler is not supported") + } + + _windows.add(index, window) + + window.screen = this + window.wasAdded() + window.resize(width, height) + + return window + } + + override fun addWindow(window: T): T { + return addWindow(window, _windows.size) + } + + override fun removeWindow(window: Window) { + _windows.remove(window) + } + + override fun init() { + super.init() + + windows.forEach { + it.resize(width, height) + } + } + + override fun drawBackground(matrixStack: MatrixStack, delta: Float, mouseX: Int, mouseY: Int) { + // no-op + } + + override fun render(matrixStack: MatrixStack, mouseX: Int, mouseY: Int, delta: Float) { + renderBackground(matrixStack) + + val mouse = Point(mouseX, mouseY) + windows.forEachIndexed { index, it -> + it.draw(matrixStack, mouse, delta) + if (it is ScreenHandlerWindow) { + if (index == windows.size - 1) { + super.render(matrixStack, mouseX, mouseY, delta) + } else { + // if the screen handler window is not the frontmost, we fake the mouse x/y to disable the slot mouseover effect + super.render(matrixStack, -1, -1, delta) + } + } + } + } + + override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean { + val window = windows.lastOrNull() + val result = window?.mouseClicked(Point(mouseX, mouseY), MouseButton.fromMC(button)) + return if (result == true) { + RenderHelper.playSound(SoundEvents.UI_BUTTON_CLICK) + true + } else if (window is ScreenHandlerWindow) { + super.mouseClicked(mouseX, mouseY, button) + } else { + false + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt b/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt index 9d8dd85..5b95805 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt @@ -8,6 +8,7 @@ import net.minecraft.text.LiteralText import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.util.MouseButton import net.shadowfacts.cacao.util.RenderHelper +import net.shadowfacts.cacao.window.Window import java.util.* /** @@ -16,7 +17,7 @@ import java.util.* * * @author shadowfacts */ -open class CacaoScreen: Screen(LiteralText("CacaoScreen")) { +open class CacaoScreen: Screen(LiteralText("CacaoScreen")), AbstractCacaoScreen { // _windows is the internal, mutable object, since we only want it to by mutated by the add/removeWindow methods. private val _windows = LinkedList() @@ -24,7 +25,7 @@ open class CacaoScreen: Screen(LiteralText("CacaoScreen")) { * The list of windows that belong to this screen. * This list should never be modified directly, only by using the [addWindow]/[removeWindow] methods. */ - val windows: List = _windows + override val windows: List = _windows /** * Adds the given window to this screen's window list. @@ -35,7 +36,7 @@ open class CacaoScreen: Screen(LiteralText("CacaoScreen")) { * @param index The index to insert the window into the window list at. * @return The window that was added, as a convenience. */ - fun addWindow(window: T, index: Int = _windows.size): T { + override fun addWindow(window: T, index: Int): T { _windows.add(index, window) window.screen = this @@ -45,10 +46,14 @@ open class CacaoScreen: Screen(LiteralText("CacaoScreen")) { return window } + override fun addWindow(window: T): T { + return addWindow(window, _windows.size) + } + /** * Removes the given window from this screen's window list. */ - fun removeWindow(window: Window) { + override fun removeWindow(window: Window) { _windows.remove(window) } diff --git a/src/main/kotlin/net/shadowfacts/cacao/view/DialogView.kt b/src/main/kotlin/net/shadowfacts/cacao/view/DialogView.kt index 378026e..3b9015f 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/view/DialogView.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/view/DialogView.kt @@ -1,6 +1,6 @@ package net.shadowfacts.cacao.view -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.geometry.Axis import net.shadowfacts.cacao.util.Color import net.shadowfacts.cacao.util.texture.NinePatchTexture diff --git a/src/main/kotlin/net/shadowfacts/cacao/view/View.kt b/src/main/kotlin/net/shadowfacts/cacao/view/View.kt index 41bfaf8..e1f3d3a 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/view/View.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/view/View.kt @@ -3,7 +3,7 @@ package net.shadowfacts.cacao.view import net.minecraft.client.util.math.MatrixStack import net.shadowfacts.kiwidsl.dsl import net.shadowfacts.cacao.LayoutVariable -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.geometry.* import net.shadowfacts.cacao.util.* import net.shadowfacts.cacao.util.properties.ObservableLateInitProperty @@ -328,7 +328,7 @@ open class View() { * @return Whether the mouse click was handled by this view or any subviews. */ open fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean { - val view = subviewsAtPoint(point).maxBy(View::zIndex) + val view = subviewsAtPoint(point).maxByOrNull(View::zIndex) if (view != null) { val pointInView = convert(point, to = view) return view.mouseClicked(pointInView, mouseButton) @@ -337,7 +337,7 @@ open class View() { } open fun mouseDragged(startPoint: Point, delta: Point, mouseButton: MouseButton): Boolean { - val view = subviewsAtPoint(startPoint).maxBy(View::zIndex) + val view = subviewsAtPoint(startPoint).maxByOrNull(View::zIndex) if (view != null) { val startInView = convert(startPoint, to = view) return view.mouseDragged(startInView, delta, mouseButton) diff --git a/src/main/kotlin/net/shadowfacts/cacao/view/button/DropdownButton.kt b/src/main/kotlin/net/shadowfacts/cacao/view/button/DropdownButton.kt index 0adb7b6..dec1296 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/view/button/DropdownButton.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/view/button/DropdownButton.kt @@ -1,7 +1,7 @@ package net.shadowfacts.cacao.view.button import net.minecraft.util.Identifier -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.geometry.Axis import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Rect diff --git a/src/main/kotlin/net/shadowfacts/cacao/viewcontroller/ViewController.kt b/src/main/kotlin/net/shadowfacts/cacao/viewcontroller/ViewController.kt index 5174919..a6bf752 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/viewcontroller/ViewController.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/viewcontroller/ViewController.kt @@ -1,6 +1,6 @@ package net.shadowfacts.cacao.viewcontroller -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.util.properties.ObservableLazyProperty import net.shadowfacts.cacao.view.View import net.shadowfacts.kiwidsl.dsl diff --git a/src/main/kotlin/net/shadowfacts/cacao/window/ScreenHandlerWindow.kt b/src/main/kotlin/net/shadowfacts/cacao/window/ScreenHandlerWindow.kt new file mode 100644 index 0000000..73842e1 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/cacao/window/ScreenHandlerWindow.kt @@ -0,0 +1,14 @@ +package net.shadowfacts.cacao.window + +import net.minecraft.screen.ScreenHandler +import net.shadowfacts.cacao.viewcontroller.ViewController + +/** + * @author shadowfacts + */ +class ScreenHandlerWindow( + val screenHandler: ScreenHandler, + viewController: ViewController +): Window(viewController) { + +} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/cacao/Window.kt b/src/main/kotlin/net/shadowfacts/cacao/window/Window.kt similarity index 93% rename from src/main/kotlin/net/shadowfacts/cacao/Window.kt rename to src/main/kotlin/net/shadowfacts/cacao/window/Window.kt index b5863df..4f1ee3b 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/Window.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/window/Window.kt @@ -1,6 +1,8 @@ -package net.shadowfacts.cacao +package net.shadowfacts.cacao.window import net.minecraft.client.util.math.MatrixStack +import net.shadowfacts.cacao.AbstractCacaoScreen +import net.shadowfacts.cacao.CacaoScreen import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.util.MouseButton import net.shadowfacts.cacao.view.View @@ -20,7 +22,7 @@ import no.birkett.kiwi.Variable * * @param viewController The root view controller for this window. */ -class Window( +open class Window( /** * The root view controller for this window. */ @@ -31,7 +33,7 @@ class Window( * The screen that this window belongs to. * Not initialized until this window is added to a screen, using it before that point will throw a runtime exception. */ - lateinit var screen: CacaoScreen + lateinit var screen: AbstractCacaoScreen /** * The constraint solver used by this window and all its views and subviews. @@ -157,7 +159,7 @@ class Window( * @param mouse The point in the coordinate system of the window. * @param delta The time elapsed since the last frame. */ - fun draw(matrixStack: MatrixStack, mouse: Point, delta: Float) { + open fun draw(matrixStack: MatrixStack, mouse: Point, delta: Float) { val mouseInView = Point(mouse.x - viewController.view.frame.left, mouse.y - viewController.view.frame.top) viewController.view.draw(matrixStack, mouseInView, delta) } @@ -188,13 +190,14 @@ class Window( if (currentlyDraggedView != null) { return currentlyDraggedView.mouseDragged(startPoint, delta, mouseButton) } else if (startPoint in viewController.view.frame) { - val startInView = Point(startPoint.x - viewController.view.frame.left, startPoint.y - viewController.view.frame.top) + val startInView = + Point(startPoint.x - viewController.view.frame.left, startPoint.y - viewController.view.frame.top) var prevView: View? = null - var view = viewController.view.subviewsAtPoint(startInView).maxBy(View::zIndex) + var view = viewController.view.subviewsAtPoint(startInView).maxByOrNull(View::zIndex) while (view != null && !view.respondsToDragging) { prevView = view val pointInView = viewController.view.convert(startInView, to = view) - view = view.subviewsAtPoint(pointInView).maxBy(View::zIndex) + view = view.subviewsAtPoint(pointInView).maxByOrNull(View::zIndex) } this.currentDragReceiver = view ?: prevView return this.currentDragReceiver?.mouseDragged(startPoint, delta, mouseButton) ?: false diff --git a/src/main/kotlin/net/shadowfacts/phycon/item/ConsoleItem.kt b/src/main/kotlin/net/shadowfacts/phycon/item/ConsoleItem.kt index a6570af..f5cb03a 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/item/ConsoleItem.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/item/ConsoleItem.kt @@ -9,6 +9,7 @@ import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.network.DeviceBlock import net.shadowfacts.phycon.network.DeviceBlockEntity import net.shadowfacts.phycon.screen.DeviceConsoleScreen +import net.shadowfacts.phycon.screen.TestCacaoScreen /** * @author shadowfacts @@ -36,7 +37,8 @@ class ConsoleItem: Item(Settings()) { } private fun openScreen(be: DeviceBlockEntity) { - val screen = DeviceConsoleScreen(be) +// val screen = DeviceConsoleScreen(be) + val screen = TestCacaoScreen() MinecraftClient.getInstance().openScreen(screen) } diff --git a/src/main/kotlin/net/shadowfacts/phycon/screen/TestCacaoScreen.kt b/src/main/kotlin/net/shadowfacts/phycon/screen/TestCacaoScreen.kt index 525073d..1ca0579 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/screen/TestCacaoScreen.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/screen/TestCacaoScreen.kt @@ -2,7 +2,7 @@ package net.shadowfacts.phycon.screen import net.minecraft.util.Identifier import net.shadowfacts.cacao.CacaoScreen -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.geometry.Axis import net.shadowfacts.cacao.geometry.Size import net.shadowfacts.cacao.util.Color diff --git a/src/test/kotlin/net/shadowfacts/cacao/CoordinateConversionTests.kt b/src/test/kotlin/net/shadowfacts/cacao/CoordinateConversionTests.kt index 5b752a9..787ad5b 100644 --- a/src/test/kotlin/net/shadowfacts/cacao/CoordinateConversionTests.kt +++ b/src/test/kotlin/net/shadowfacts/cacao/CoordinateConversionTests.kt @@ -4,6 +4,7 @@ import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.view.View import net.shadowfacts.cacao.viewcontroller.ViewController +import net.shadowfacts.cacao.window.Window import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test diff --git a/src/test/kotlin/net/shadowfacts/cacao/WindowLayoutTests.kt b/src/test/kotlin/net/shadowfacts/cacao/WindowLayoutTests.kt index ae6214e..b39976a 100644 --- a/src/test/kotlin/net/shadowfacts/cacao/WindowLayoutTests.kt +++ b/src/test/kotlin/net/shadowfacts/cacao/WindowLayoutTests.kt @@ -5,6 +5,7 @@ import net.shadowfacts.kiwidsl.dsl import net.shadowfacts.cacao.geometry.Size import net.shadowfacts.cacao.view.View import net.shadowfacts.cacao.viewcontroller.ViewController +import net.shadowfacts.cacao.window.Window import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test diff --git a/src/test/kotlin/net/shadowfacts/cacao/view/StackViewLayoutTests.kt b/src/test/kotlin/net/shadowfacts/cacao/view/StackViewLayoutTests.kt index 82dde34..4dea9bd 100644 --- a/src/test/kotlin/net/shadowfacts/cacao/view/StackViewLayoutTests.kt +++ b/src/test/kotlin/net/shadowfacts/cacao/view/StackViewLayoutTests.kt @@ -2,7 +2,7 @@ package net.shadowfacts.cacao.view import net.shadowfacts.cacao.CacaoScreen import net.shadowfacts.kiwidsl.dsl -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.geometry.Axis import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Size diff --git a/src/test/kotlin/net/shadowfacts/cacao/view/ViewClickTests.kt b/src/test/kotlin/net/shadowfacts/cacao/view/ViewClickTests.kt index 0d4b133..771a2bd 100644 --- a/src/test/kotlin/net/shadowfacts/cacao/view/ViewClickTests.kt +++ b/src/test/kotlin/net/shadowfacts/cacao/view/ViewClickTests.kt @@ -1,7 +1,7 @@ package net.shadowfacts.cacao.view import net.shadowfacts.cacao.CacaoScreen -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.util.MouseButton diff --git a/src/test/kotlin/net/shadowfacts/cacao/view/ViewHoverTests.kt b/src/test/kotlin/net/shadowfacts/cacao/view/ViewHoverTests.kt index 6a44b98..2f1f359 100644 --- a/src/test/kotlin/net/shadowfacts/cacao/view/ViewHoverTests.kt +++ b/src/test/kotlin/net/shadowfacts/cacao/view/ViewHoverTests.kt @@ -2,7 +2,7 @@ package net.shadowfacts.cacao.view import net.minecraft.client.util.math.MatrixStack import net.shadowfacts.cacao.CacaoScreen -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.viewcontroller.ViewController diff --git a/src/test/kotlin/net/shadowfacts/cacao/view/button/ButtonClickTests.kt b/src/test/kotlin/net/shadowfacts/cacao/view/button/ButtonClickTests.kt index a39f9e2..67ed7e1 100644 --- a/src/test/kotlin/net/shadowfacts/cacao/view/button/ButtonClickTests.kt +++ b/src/test/kotlin/net/shadowfacts/cacao/view/button/ButtonClickTests.kt @@ -1,7 +1,7 @@ package net.shadowfacts.cacao.view.button import net.shadowfacts.cacao.CacaoScreen -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Size diff --git a/src/test/kotlin/net/shadowfacts/cacao/view/button/EnumButtonTests.kt b/src/test/kotlin/net/shadowfacts/cacao/view/button/EnumButtonTests.kt index 70e6577..248f1aa 100644 --- a/src/test/kotlin/net/shadowfacts/cacao/view/button/EnumButtonTests.kt +++ b/src/test/kotlin/net/shadowfacts/cacao/view/button/EnumButtonTests.kt @@ -1,7 +1,7 @@ package net.shadowfacts.cacao.view.button import net.shadowfacts.cacao.CacaoScreen -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.util.MouseButton diff --git a/src/test/kotlin/net/shadowfacts/cacao/view/button/ToggleButtonTests.kt b/src/test/kotlin/net/shadowfacts/cacao/view/button/ToggleButtonTests.kt index f3c595b..88d22d1 100644 --- a/src/test/kotlin/net/shadowfacts/cacao/view/button/ToggleButtonTests.kt +++ b/src/test/kotlin/net/shadowfacts/cacao/view/button/ToggleButtonTests.kt @@ -1,7 +1,7 @@ package net.shadowfacts.cacao.view.button import net.shadowfacts.cacao.CacaoScreen -import net.shadowfacts.cacao.Window +import net.shadowfacts.cacao.window.Window import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.util.MouseButton