ASMR/src/main/kotlin/net/shadowfacts/cacao/Window.kt

124 lines
3.7 KiB
Kotlin
Raw Normal View History

2019-06-22 19:02:17 +00:00
package net.shadowfacts.cacao
2019-06-22 19:02:17 +00:00
import net.shadowfacts.cacao.geometry.Point
2019-06-23 15:41:32 +00:00
import net.shadowfacts.cacao.util.MouseButton
2019-06-22 19:02:17 +00:00
import net.shadowfacts.cacao.view.View
import no.birkett.kiwi.Solver
2019-06-22 20:08:00 +00:00
import java.util.*
2019-06-22 14:59:18 +00:00
/**
2019-06-22 20:08:00 +00:00
* A Window is the object at the top of a Cacao view hierarchy. It occupies the entirety of the Minecraft screen size
* and provides the base coordinate system for its view hierarchy.
*
* The Window owns the Kiwi [Solver] object used for layout by all of its views.
*
2019-06-22 14:59:18 +00:00
* @author shadowfacts
*/
class Window {
2019-06-26 01:52:17 +00:00
/**
* 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
var solver = Solver()
2019-06-22 20:08:00 +00:00
// _views is the internal, mutable object, since we only want it to be mutated by the add/removeView methods
private val _views = LinkedList<View>()
/**
* The list of top-level views in this window.
* This list should never be modified directly, only by calling the [addView]/[removeView] methods.
*/
val views: List<View> = _views
2019-06-25 22:39:58 +00:00
private var viewsSortedByZIndex: List<View> = listOf()
2019-06-26 01:52:17 +00:00
/**
* Convenience method that removes this window from its [screen].
*/
fun removeFromScreen() {
screen.removeWindow(this)
}
2019-06-22 20:08:00 +00:00
/**
* Adds the given view as a top-level view in this window.
*
* @param view The view to add.
* @return The same view, as a convenience.
*/
fun <T: View> addView(view: T): T {
2019-06-22 20:08:00 +00:00
_views.add(view)
2019-06-25 22:39:58 +00:00
viewsSortedByZIndex = views.sortedBy(View::zIndex)
view.window = this
view.solver = solver
view.wasAdded()
return view
}
2019-06-25 22:39:58 +00:00
/**
* Finds all views in this window at the given point.
*
* @param point The point to find views at, in the coordinate system of the window.
* @return All views that contain the point.
*/
fun viewsAtPoint(point: Point): List<View> {
return views.filter { point in it.frame }
}
2019-06-23 15:41:32 +00:00
/**
* Attempts to find a top level view of this window that contains the given point.
* If there are multiple overlapping views, which one this method returns is undefined.
2019-06-25 22:39:58 +00:00
* [viewsAtPoint] may be used, and the resulting List sorted by [View.zIndex].
2019-06-23 15:41:32 +00:00
*
* @param point The point to find views at, in the coordinate system of the window.
2019-06-25 22:39:58 +00:00
* @return the Veiw, if any, that contain the given point.
2019-06-23 15:41:32 +00:00
*/
fun viewAtPoint(point: Point): View? {
return views.firstOrNull { point in it.frame }
}
2019-06-22 20:08:00 +00:00
/**
* Instructs the solver to solve all of the provided constraints.
* Should be called after the view hierarchy is setup.
*/
fun layout() {
solver.updateVariables()
views.forEach(View::didLayout)
}
2019-06-22 20:08:00 +00:00
/**
* Draws this window and all of its views.
* This method is called by [CacaoScreen] and generally shouldn't be called directly.
*
* @param mouse The point in the coordinate system of the window.
* @param delta The time elapsed since the last frame.
*/
fun draw(mouse: Point, delta: Float) {
2019-06-25 22:39:58 +00:00
viewsSortedByZIndex.forEach {
2019-06-23 15:41:32 +00:00
val mouseInView = Point(mouse.x - it.frame.left, mouse.y - it.frame.top)
it.draw(mouseInView, delta)
}
}
/**
* Called when a mouse button is clicked and this is the active window.
* This method is called by [CacaoScreen] and generally shouldn't be called directly.
*
* @param point The point in the window of the click.
* @param mouseButton The mouse button that was used to click.
2019-06-24 02:34:12 +00:00
* @return Whether the mouse click was handled by a view.
2019-06-23 15:41:32 +00:00
*/
2019-06-24 02:34:12 +00:00
fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
2019-06-26 01:52:17 +00:00
val view = viewsAtPoint(point).maxBy(View::zIndex)
2019-06-23 15:41:32 +00:00
if (view != null) {
val pointInView = Point(point.x - view.frame.left, point.y - view.frame.top)
2019-06-24 02:34:12 +00:00
return view.mouseClicked(pointInView, mouseButton)
2019-06-23 15:41:32 +00:00
}
2019-06-24 02:34:12 +00:00
return false
}
}