Skip to content

Commit

Permalink
Fixes & tests scene transitions parameters (#1184)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz authored Dec 31, 2022
1 parent 8820c03 commit 109d582
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class SceneContainer(
time: TimeSpan = 0.seconds,
transition: Transition = defaultTransition
): T {
return changeTo(T::class, gen, injects, time, transition, remap = true)
return changeTo(T::class, gen, injects = injects, time = time, transition = transition, remap = true)
}

suspend inline fun <T : Scene> changeTo(
Expand Down
22 changes: 15 additions & 7 deletions korge/src/commonMain/kotlin/com/soywiz/korge/scene/Transition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class TransitionView() : Container() {
}

/** [Transition] that will be used to render [prev] and [next] */
private var transition: Transition = AlphaTransition
internal var transition: Transition = AlphaTransition
private set
private lateinit var transitionProcess: TransitionProcess
val prev: View get() = this[0]
val next: View get() = this[1]
Expand All @@ -34,6 +35,8 @@ class TransitionView() : Container() {
this.transitionProcess.start(this.prev, this.next)
}

override fun toString(): String = super.toString() + ":ratio=$ratio:transition=$transition"

fun endTransition() {
this.ratio = 1.0
this.transitionProcess.end(this.prev, this.next)
Expand Down Expand Up @@ -66,20 +69,23 @@ interface Transition {
fun create(): TransitionProcess
}

fun TransitionProcess(render: (ctx: RenderContext, prev: View, next: View, ratio: Double) -> Unit): TransitionProcess =
fun TransitionProcess(name: String = "Transition", render: (ctx: RenderContext, prev: View, next: View, ratio: Double) -> Unit): TransitionProcess =
object : TransitionProcess {
override fun render(ctx: RenderContext, prev: View, next: View, ratio: Double) = render(ctx, prev, next, ratio)
override fun toString(): String = name
}

fun Transition(render: (ctx: RenderContext, prev: View, next: View, ratio: Double) -> Unit): Transition {
fun Transition(name: String = "Transition", render: (ctx: RenderContext, prev: View, next: View, ratio: Double) -> Unit): Transition {
return object : Transition, TransitionProcess {
override fun create(): TransitionProcess = this
override fun render(ctx: RenderContext, prev: View, next: View, ratio: Double) = render(ctx, prev, next, ratio)
override fun toString(): String = name
}
}

fun TransitionCreate(block: () -> TransitionProcess): Transition = object : Transition {
fun TransitionCreate(name: String = "Transition", block: () -> TransitionProcess): Transition = object : Transition {
override fun create(): TransitionProcess = block()
override fun toString(): String = name
}

/*
Expand All @@ -93,6 +99,8 @@ class Transition(

/** Creates a new [Transition] with an [Easing] specified */
fun Transition.withEasing(easing: Easing) = object : Transition {
override fun toString(): String = "${this@withEasing}.withEasing"

override fun create(): TransitionProcess {
val process = this@withEasing.create()
return object : TransitionProcess {
Expand All @@ -107,7 +115,7 @@ fun Transition.withEasing(easing: Easing) = object : Transition {

/** A [Transition] that will blend [prev] and [next] by adjusting its alphas */
@SharedImmutable
val AlphaTransition = Transition { ctx, prev, next, ratio ->
val AlphaTransition = Transition("AlphaTransition") { ctx, prev, next, ratio ->
val prevAlpha = prev.alpha
val nextAlpha = next.alpha
try {
Expand All @@ -129,9 +137,9 @@ fun MaskTransition(
reversed: Boolean = false,
spread: Double = 1.0,
filtering: Boolean = true,
) = TransitionCreate {
) = TransitionCreate("MaskTransition") {
val filter = TransitionFilter(transition, reversed, spread, filtering = filtering)
TransitionProcess { ctx, prev, next, ratio ->
TransitionProcess("MaskTransition") { ctx, prev, next, ratio ->
filter.ratio = ratio
prev.render(ctx)
next.renderFiltered(ctx, filter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.soywiz.korge.scene

import com.soywiz.korge.tests.ViewsForTesting
import com.soywiz.korge.view.SContainer
import com.soywiz.korge.view.SolidRect
import com.soywiz.korim.color.Colors
import com.soywiz.klock.*
import com.soywiz.korge.tests.*
import com.soywiz.korge.view.*
import com.soywiz.korge.view.filter.*
import com.soywiz.korim.color.*
import kotlin.test.*

class SceneContainerTest : ViewsForTesting() {
data class SceneInfo(val name: String)
Expand Down Expand Up @@ -55,4 +57,19 @@ class SceneContainerTest : ViewsForTesting() {
// mylog.joinToString(", ")
// )
//}

@Test
//fun testVerifyTransitionIsUsed() = suspendTest {
fun testVerifyTransitionIsUsed() = viewsTest {
//val log = ViewsLog(coroutineContext)
//log.injector.mapSingleton { ResourcesRoot() }
val sceneContainer = sceneContainer(views)
val startTime = time
sceneContainer.changeTo({ EmptyScene() }, time = 0.5.seconds, transition = MaskTransition(TransitionFilter.Transition.HORIZONTAL))
val transitionView = sceneContainer.firstChild as TransitionView
assertEquals("MaskTransition", transitionView.transition.toString())
assertEquals(1.0, transitionView.ratio)
val endTime = time
assertTrue { endTime - startTime in 0.5.seconds..0.75.seconds }
}
}

0 comments on commit 109d582

Please sign in to comment.