Start adding CacaoHandledScreen

This commit is contained in:
Shadowfacts 2021-02-18 23:27:18 -05:00
parent 8f577598ff
commit 15f9b02041
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
19 changed files with 161 additions and 25 deletions

View File

@ -0,0 +1,17 @@
package net.shadowfacts.cacao
import net.shadowfacts.cacao.window.Window
/**
* @author shadowfacts
*/
interface AbstractCacaoScreen {
val windows: List<Window>
fun <T: Window> addWindow(window: T, index: Int): T
fun <T: Window> addWindow(window: T): T
fun removeWindow(window: Window)
}

View File

@ -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: ScreenHandler>(
handler: Handler,
playerInv: PlayerInventory,
title: Text,
): HandledScreen<Handler>(handler, playerInv, title), AbstractCacaoScreen {
private val _windows = LinkedList<Window>()
override val windows: List<Window> = _windows
override fun <T: Window> 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 <T : Window> 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
}
}
}

View File

@ -8,6 +8,7 @@ import net.minecraft.text.LiteralText
import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.util.MouseButton import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.util.RenderHelper import net.shadowfacts.cacao.util.RenderHelper
import net.shadowfacts.cacao.window.Window
import java.util.* import java.util.*
/** /**
@ -16,7 +17,7 @@ import java.util.*
* *
* @author shadowfacts * @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. // _windows is the internal, mutable object, since we only want it to by mutated by the add/removeWindow methods.
private val _windows = LinkedList<Window>() private val _windows = LinkedList<Window>()
@ -24,7 +25,7 @@ open class CacaoScreen: Screen(LiteralText("CacaoScreen")) {
* The list of windows that belong to this screen. * The list of windows that belong to this screen.
* This list should never be modified directly, only by using the [addWindow]/[removeWindow] methods. * This list should never be modified directly, only by using the [addWindow]/[removeWindow] methods.
*/ */
val windows: List<Window> = _windows override val windows: List<Window> = _windows
/** /**
* Adds the given window to this screen's window list. * 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. * @param index The index to insert the window into the window list at.
* @return The window that was added, as a convenience. * @return The window that was added, as a convenience.
*/ */
fun <T: Window> addWindow(window: T, index: Int = _windows.size): T { override fun <T: Window> addWindow(window: T, index: Int): T {
_windows.add(index, window) _windows.add(index, window)
window.screen = this window.screen = this
@ -45,10 +46,14 @@ open class CacaoScreen: Screen(LiteralText("CacaoScreen")) {
return window return window
} }
override fun <T : Window> addWindow(window: T): T {
return addWindow(window, _windows.size)
}
/** /**
* Removes the given window from this screen's window list. * Removes the given window from this screen's window list.
*/ */
fun removeWindow(window: Window) { override fun removeWindow(window: Window) {
_windows.remove(window) _windows.remove(window)
} }

View File

@ -1,6 +1,6 @@
package net.shadowfacts.cacao.view 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.geometry.Axis
import net.shadowfacts.cacao.util.Color import net.shadowfacts.cacao.util.Color
import net.shadowfacts.cacao.util.texture.NinePatchTexture import net.shadowfacts.cacao.util.texture.NinePatchTexture

View File

@ -3,7 +3,7 @@ package net.shadowfacts.cacao.view
import net.minecraft.client.util.math.MatrixStack import net.minecraft.client.util.math.MatrixStack
import net.shadowfacts.kiwidsl.dsl import net.shadowfacts.kiwidsl.dsl
import net.shadowfacts.cacao.LayoutVariable 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.geometry.*
import net.shadowfacts.cacao.util.* import net.shadowfacts.cacao.util.*
import net.shadowfacts.cacao.util.properties.ObservableLateInitProperty 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. * @return Whether the mouse click was handled by this view or any subviews.
*/ */
open fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean { 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) { if (view != null) {
val pointInView = convert(point, to = view) val pointInView = convert(point, to = view)
return view.mouseClicked(pointInView, mouseButton) return view.mouseClicked(pointInView, mouseButton)
@ -337,7 +337,7 @@ open class View() {
} }
open fun mouseDragged(startPoint: Point, delta: Point, mouseButton: MouseButton): Boolean { 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) { if (view != null) {
val startInView = convert(startPoint, to = view) val startInView = convert(startPoint, to = view)
return view.mouseDragged(startInView, delta, mouseButton) return view.mouseDragged(startInView, delta, mouseButton)

View File

@ -1,7 +1,7 @@
package net.shadowfacts.cacao.view.button package net.shadowfacts.cacao.view.button
import net.minecraft.util.Identifier 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.Axis
import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Rect

View File

@ -1,6 +1,6 @@
package net.shadowfacts.cacao.viewcontroller 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.util.properties.ObservableLazyProperty
import net.shadowfacts.cacao.view.View import net.shadowfacts.cacao.view.View
import net.shadowfacts.kiwidsl.dsl import net.shadowfacts.kiwidsl.dsl

View File

@ -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) {
}

View File

@ -1,6 +1,8 @@
package net.shadowfacts.cacao package net.shadowfacts.cacao.window
import net.minecraft.client.util.math.MatrixStack 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.geometry.Point
import net.shadowfacts.cacao.util.MouseButton import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.view.View import net.shadowfacts.cacao.view.View
@ -20,7 +22,7 @@ import no.birkett.kiwi.Variable
* *
* @param viewController The root view controller for this window. * @param viewController The root view controller for this window.
*/ */
class Window( open class Window(
/** /**
* The root view controller for this window. * The root view controller for this window.
*/ */
@ -31,7 +33,7 @@ class Window(
* The screen that this window belongs to. * 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. * 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. * 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 mouse The point in the coordinate system of the window.
* @param delta The time elapsed since the last frame. * @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) val mouseInView = Point(mouse.x - viewController.view.frame.left, mouse.y - viewController.view.frame.top)
viewController.view.draw(matrixStack, mouseInView, delta) viewController.view.draw(matrixStack, mouseInView, delta)
} }
@ -188,13 +190,14 @@ class Window(
if (currentlyDraggedView != null) { if (currentlyDraggedView != null) {
return currentlyDraggedView.mouseDragged(startPoint, delta, mouseButton) return currentlyDraggedView.mouseDragged(startPoint, delta, mouseButton)
} else if (startPoint in viewController.view.frame) { } 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 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) { while (view != null && !view.respondsToDragging) {
prevView = view prevView = view
val pointInView = viewController.view.convert(startInView, to = 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 this.currentDragReceiver = view ?: prevView
return this.currentDragReceiver?.mouseDragged(startPoint, delta, mouseButton) ?: false return this.currentDragReceiver?.mouseDragged(startPoint, delta, mouseButton) ?: false

View File

@ -9,6 +9,7 @@ import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.network.DeviceBlock import net.shadowfacts.phycon.network.DeviceBlock
import net.shadowfacts.phycon.network.DeviceBlockEntity import net.shadowfacts.phycon.network.DeviceBlockEntity
import net.shadowfacts.phycon.screen.DeviceConsoleScreen import net.shadowfacts.phycon.screen.DeviceConsoleScreen
import net.shadowfacts.phycon.screen.TestCacaoScreen
/** /**
* @author shadowfacts * @author shadowfacts
@ -36,7 +37,8 @@ class ConsoleItem: Item(Settings()) {
} }
private fun openScreen(be: DeviceBlockEntity) { private fun openScreen(be: DeviceBlockEntity) {
val screen = DeviceConsoleScreen(be) // val screen = DeviceConsoleScreen(be)
val screen = TestCacaoScreen()
MinecraftClient.getInstance().openScreen(screen) MinecraftClient.getInstance().openScreen(screen)
} }

View File

@ -2,7 +2,7 @@ package net.shadowfacts.phycon.screen
import net.minecraft.util.Identifier import net.minecraft.util.Identifier
import net.shadowfacts.cacao.CacaoScreen 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.Axis
import net.shadowfacts.cacao.geometry.Size import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.util.Color import net.shadowfacts.cacao.util.Color

View File

@ -4,6 +4,7 @@ import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.view.View import net.shadowfacts.cacao.view.View
import net.shadowfacts.cacao.viewcontroller.ViewController import net.shadowfacts.cacao.viewcontroller.ViewController
import net.shadowfacts.cacao.window.Window
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test

View File

@ -5,6 +5,7 @@ import net.shadowfacts.kiwidsl.dsl
import net.shadowfacts.cacao.geometry.Size import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.view.View import net.shadowfacts.cacao.view.View
import net.shadowfacts.cacao.viewcontroller.ViewController import net.shadowfacts.cacao.viewcontroller.ViewController
import net.shadowfacts.cacao.window.Window
import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test

View File

@ -2,7 +2,7 @@ package net.shadowfacts.cacao.view
import net.shadowfacts.cacao.CacaoScreen import net.shadowfacts.cacao.CacaoScreen
import net.shadowfacts.kiwidsl.dsl 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.Axis
import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.geometry.Size import net.shadowfacts.cacao.geometry.Size

View File

@ -1,7 +1,7 @@
package net.shadowfacts.cacao.view package net.shadowfacts.cacao.view
import net.shadowfacts.cacao.CacaoScreen 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.Point
import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.util.MouseButton import net.shadowfacts.cacao.util.MouseButton

View File

@ -2,7 +2,7 @@ package net.shadowfacts.cacao.view
import net.minecraft.client.util.math.MatrixStack import net.minecraft.client.util.math.MatrixStack
import net.shadowfacts.cacao.CacaoScreen 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.Point
import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.viewcontroller.ViewController import net.shadowfacts.cacao.viewcontroller.ViewController

View File

@ -1,7 +1,7 @@
package net.shadowfacts.cacao.view.button package net.shadowfacts.cacao.view.button
import net.shadowfacts.cacao.CacaoScreen 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.Point
import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.geometry.Size import net.shadowfacts.cacao.geometry.Size

View File

@ -1,7 +1,7 @@
package net.shadowfacts.cacao.view.button package net.shadowfacts.cacao.view.button
import net.shadowfacts.cacao.CacaoScreen 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.Point
import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.util.MouseButton import net.shadowfacts.cacao.util.MouseButton

View File

@ -1,7 +1,7 @@
package net.shadowfacts.cacao.view.button package net.shadowfacts.cacao.view.button
import net.shadowfacts.cacao.CacaoScreen 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.Point
import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.util.MouseButton import net.shadowfacts.cacao.util.MouseButton