Cacao: Misc things

This commit is contained in:
Shadowfacts 2021-03-20 14:48:59 -04:00
parent 81ce590231
commit 2774cabfcc
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 83 additions and 4 deletions

View File

@ -57,6 +57,16 @@ open class CacaoHandledScreen<Handler: ScreenHandler>(
} }
} }
override fun onClose() {
super.onClose()
windows.forEach {
// todo: VC callbacks
it.firstResponder = null
}
}
override fun drawBackground(matrixStack: MatrixStack, delta: Float, mouseX: Int, mouseY: Int) { override fun drawBackground(matrixStack: MatrixStack, delta: Float, mouseX: Int, mouseY: Int) {
renderBackground(matrixStack) renderBackground(matrixStack)
} }
@ -95,6 +105,38 @@ open class CacaoHandledScreen<Handler: ScreenHandler>(
} }
} }
override fun mouseDragged(mouseX: Double, mouseY: Double, button: Int, deltaX: Double, deltaY: Double): Boolean {
val window = windows.lastOrNull()
val startPoint = Point(mouseX, mouseY)
val delta = Point(deltaX, deltaY)
val result = window?.mouseDragged(startPoint, delta, MouseButton.fromMC(button))
return if (result == true) {
true
} else if (window is ScreenHandlerWindow) {
return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)
} else {
false
}
}
override fun mouseReleased(mouseX: Double, mouseY: Double, button: Int): Boolean {
val window = windows.lastOrNull()
val result = window?.mouseReleased(Point(mouseX, mouseY), MouseButton.fromMC(button))
return if (result == true) {
true
} else if (window is ScreenHandlerWindow) {
super.mouseReleased(mouseX, mouseY, button)
} else {
false
}
}
override fun mouseScrolled(mouseX: Double, mouseY: Double, amount: Double): Boolean {
val window = windows.lastOrNull()
val result = window?.mouseScrolled(Point(mouseX, mouseY), amount)
return result == true
}
override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
val modifiersSet by lazy { KeyModifiers(modifiers) } val modifiersSet by lazy { KeyModifiers(modifiers) }
if (findResponder { it.keyPressed(keyCode, modifiersSet) }) { if (findResponder { it.keyPressed(keyCode, modifiersSet) }) {

View File

@ -119,6 +119,12 @@ open class CacaoScreen(title: Text = LiteralText("CacaoScreen")): Screen(title),
return result == true return result == true
} }
override fun mouseScrolled(mouseX: Double, mouseY: Double, amount: Double): Boolean {
val window = windows.lastOrNull()
val result = window?.mouseScrolled(Point(mouseX, mouseY), amount)
return result == true
}
override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
val modifiersSet by lazy { KeyModifiers(modifiers) } val modifiersSet by lazy { KeyModifiers(modifiers) }
if (findResponder { it.keyPressed(keyCode, modifiersSet) }) { if (findResponder { it.keyPressed(keyCode, modifiersSet) }) {

View File

@ -13,6 +13,7 @@ import no.birkett.kiwi.Solver
import java.lang.RuntimeException import java.lang.RuntimeException
import java.util.* import java.util.*
import kotlin.collections.HashSet import kotlin.collections.HashSet
import kotlin.math.floor
/** /**
* The base Cacao View class. Provides layout anchors, properties, and helper methods. * The base Cacao View class. Provides layout anchors, properties, and helper methods.
@ -368,7 +369,7 @@ open class View(): Responder {
*/ */
open fun draw(matrixStack: MatrixStack, mouse: Point, delta: Float) { open fun draw(matrixStack: MatrixStack, mouse: Point, delta: Float) {
RenderHelper.pushMatrix() RenderHelper.pushMatrix()
RenderHelper.translate(frame.left, frame.top) RenderHelper.translate(floor(frame.left), floor(frame.top))
RenderHelper.fill(matrixStack, bounds, backgroundColor) RenderHelper.fill(matrixStack, bounds, backgroundColor)
@ -446,6 +447,23 @@ open class View(): Responder {
return false return false
} }
open fun mouseDragEnded(point: Point, mouseButton: MouseButton) {
val view = subviewsAtPoint(point).maxByOrNull(View::zIndex)
if (view != null) {
val pointInView = convert(point, to = view)
return view.mouseDragEnded(pointInView, mouseButton)
}
}
open fun mouseScrolled(point: Point, amount: Double): Boolean {
val view = subviewsAtPoint(point).maxByOrNull(View::zIndex)
if (view != null) {
val pointInView = convert(point, to = view)
return view.mouseScrolled(pointInView, amount)
}
return false
}
/** /**
* Converts the given point in this view's coordinate system to the coordinate system of another view or the window. * Converts the given point in this view's coordinate system to the coordinate system of another view or the window.
* *

View File

@ -11,6 +11,7 @@ import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.util.RenderHelper import net.shadowfacts.cacao.util.RenderHelper
import net.shadowfacts.cacao.view.View import net.shadowfacts.cacao.view.View
import net.shadowfacts.phycon.mixin.client.TextFieldWidgetAccessor import net.shadowfacts.phycon.mixin.client.TextFieldWidgetAccessor
import org.lwjgl.glfw.GLFW
/** /**
* An abstract text field class. Cannot be constructed directly, use for creating other text fields with more specific * An abstract text field class. Cannot be constructed directly, use for creating other text fields with more specific
@ -159,7 +160,7 @@ abstract class AbstractTextField<Impl: AbstractTextField<Impl>>(
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
handler?.invoke(this as Impl) handler?.invoke(this as Impl)
} }
return result return result || (isFirstResponder && keyCode != GLFW.GLFW_KEY_ESCAPE)
} }
// todo: label for the TextFieldWidget? // todo: label for the TextFieldWidget?

View File

@ -234,7 +234,8 @@ open class Window(
fun mouseDragged(startPoint: Point, delta: Point, mouseButton: MouseButton): Boolean { fun mouseDragged(startPoint: Point, delta: Point, mouseButton: MouseButton): Boolean {
val currentlyDraggedView = this.currentDragReceiver val currentlyDraggedView = this.currentDragReceiver
if (currentlyDraggedView != null) { if (currentlyDraggedView != null) {
return currentlyDraggedView.mouseDragged(startPoint, delta, mouseButton) val pointInView = viewController.view.convert(startPoint, to = currentlyDraggedView)
return currentlyDraggedView.mouseDragged(pointInView, delta, mouseButton)
} else if (startPoint in viewController.view.frame) { } else if (startPoint in viewController.view.frame) {
val startInView = val startInView =
Point(startPoint.x - viewController.view.frame.left, startPoint.y - viewController.view.frame.top) Point(startPoint.x - viewController.view.frame.left, startPoint.y - viewController.view.frame.top)
@ -246,7 +247,12 @@ open class Window(
view = view.subviewsAtPoint(pointInView).maxByOrNull(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 if (this.currentDragReceiver != null) {
val pointInView = viewController.view.convert(startPoint, to = this.currentDragReceiver!!)
this.currentDragReceiver!!.mouseDragged(pointInView, delta, mouseButton)
} else {
false
}
} }
return false return false
} }
@ -254,10 +260,16 @@ open class Window(
fun mouseReleased(point: Point, mouseButton: MouseButton): Boolean { fun mouseReleased(point: Point, mouseButton: MouseButton): Boolean {
val currentlyDraggedView = this.currentDragReceiver val currentlyDraggedView = this.currentDragReceiver
if (currentlyDraggedView != null) { if (currentlyDraggedView != null) {
val pointInView = viewController.view.convert(point, to = currentlyDraggedView)
currentlyDraggedView.mouseDragEnded(pointInView, mouseButton)
this.currentDragReceiver = null this.currentDragReceiver = null
return true return true
} }
return false return false
} }
fun mouseScrolled(point: Point, amount: Double): Boolean {
return viewController.view.mouseScrolled(point, amount)
}
} }