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

63 lines
1.6 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
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 {
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
/**
* 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)
view.solver = solver
view.wasAdded()
return view
}
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) {
views.forEach(View::draw)
}
}