Skip to content

Commit

Permalink
Make Camera view experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed May 6, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 5e70225 commit cb97cde
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions korge/src/commonMain/kotlin/korlibs/korge/view/Camera.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package korlibs.korge.view

import korlibs.korge.annotations.*
import korlibs.korge.tween.*
import korlibs.math.geom.*
import korlibs.math.interpolation.*
@@ -8,6 +9,7 @@ import korlibs.time.*
/**
* Creates a new [Camera] and attaches to [this] [Container]. The [callback] argument is called with the [Camera] injected as this to be able to configure the camera.
*/
@KorgeExperimental
inline fun Container.camera(callback: @ViewDslMarker Camera.() -> Unit = {}): Camera = Camera().addTo(this, callback)

/**
@@ -23,31 +25,36 @@ inline fun Container.camera(callback: @ViewDslMarker Camera.() -> Unit = {}): Ca
* The [ClipContainer] or the nearest [View.Reference] ancestor will determine the size of the [Camera]
*/
// @TODO: Do not require a [ClipContainer] by handling the [renderInternal] to use a transformed Camera. To support legacy we should do this in a separate class NewCamera? CameraContainer?.
class Camera : Container(), View.Reference {
@KorgeExperimental
open class Camera : Container(), View.Reference {
//private val globalTransform = parent?.globalMatrix?.toTransform() ?: MatrixTransform()

override var unscaledSize: Size
get() = Size(referenceParent?.width ?: 100f, referenceParent?.height ?: 100f)
//get() = Size((referenceParent?.width ?: 100f) * globalTransform.scaleAvg, (referenceParent?.height ?: 100f) * globalTransform.scaleAvg)
get() = Size((referenceParent?.width ?: 100f), (referenceParent?.height ?: 100f))
set(_) = Unit

override fun getLocalBoundsInternal() = Rectangle(0f, 0f, width, height)

fun getLocalMatrixFittingGlobalRect(rect: Rectangle): Matrix {
val destinationBounds = rect
return (this.parent?.globalMatrix ?: Matrix())
//return Matrix()
.translated(-destinationBounds.x, -destinationBounds.y)
.scaled(
widthD / destinationBounds.width,
heightD / destinationBounds.height
width / destinationBounds.width,
height / destinationBounds.height
)
}

fun getLocalMatrixFittingView(view: View?): Matrix =
getLocalMatrixFittingGlobalRect((view ?: stage)?.globalBounds ?: Rectangle(0, 0, 100, 100))

fun setTo(view: View?) { this.localMatrix = getLocalMatrixFittingView(view).immutable }
fun setTo(rect: Rectangle) { this.localMatrix = getLocalMatrixFittingGlobalRect(rect).immutable }
fun setTo(view: View?) { this.localMatrix = getLocalMatrixFittingView(view) }
fun setTo(rect: Rectangle) { this.localMatrix = getLocalMatrixFittingGlobalRect(rect) }

suspend fun tweenTo(view: View?, vararg vs: V2<*>, time: TimeSpan, easing: Easing = Easing.LINEAR) = this.tween(
this::localMatrix[this.localMatrix.clone(), getLocalMatrixFittingView(view).immutable],
this::localMatrix[this.localMatrix, getLocalMatrixFittingView(view)],
*vs,
time = time,
easing = easing

0 comments on commit cb97cde

Please sign in to comment.