Skip to content

Commit

Permalink
fix: incorrect parameter method overloading on play
Browse files Browse the repository at this point in the history
Some of these methods unintentionally take in the incorrect parameter value. Specifying the parameter name is only needed for a couple, but figured it's better to be explicit here and avoid this problem again (this is the second time this has popped up)

Diffs=
c28dab8e9 fix: incorrect parameter method overloading on play (#8289)

Co-authored-by: Gordon <[email protected]>
  • Loading branch information
HayesGordon and HayesGordon committed Oct 7, 2024
1 parent 500cdba commit 20c9cb4
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5d5d418b7316aa555edb7f8ada7f20714430bdfe
c28dab8e9c98c516048ee36005754bbe8bdf4f2a
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import app.rive.runtime.kotlin.ChangedInput
import app.rive.runtime.kotlin.RiveAnimationView
import app.rive.runtime.kotlin.test.R
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -207,6 +208,26 @@ class RiveStateMachineStateResolutionTest {
}
}

@Test
fun settleInitialStateFalseDoesNotThrow() {
// See PR: https://github.com/rive-app/rive/pull/8289
// Some method overloading passed the wrong parameter. This test just makes sure the correct
// value is set.
UiThreadStatement.runOnUiThread {
val observer = TestUtils.Observer()
mockView.registerListener(observer)
mockView.setRiveResource(
R.raw.state_machine_state_resolution,
stateMachineName = "StateResolution",
autoplay = false
)
assertNotNull(mockView.controller.activeArtboard)
mockView.play(
settleInitialState = false,
)
}
}

@Test
fun setupMultipleStates() {
UiThreadStatement.runOnUiThread {
Expand Down
68 changes: 42 additions & 26 deletions kotlin/src/main/java/app/rive/runtime/kotlin/RiveAnimationView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
* [animationNames]
*/
fun pause(animationNames: List<String>, areStateMachines: Boolean = false) {
controller.pause(animationNames, areStateMachines)
controller.pause(animationNames = animationNames, areStateMachines = areStateMachines)
}


Expand All @@ -456,7 +456,7 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
* [animationName] Animation
*/
fun pause(animationName: String, isStateMachine: Boolean = false) {
controller.pause(animationName, isStateMachine)
controller.pause(animationName = animationName, isStateMachine = isStateMachine)
}

/**
Expand All @@ -482,7 +482,10 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
* Advanced: Multiple [animation instances][LinearAnimationInstance] can run the same animation
*/
fun stop(animationNames: List<String>, areStateMachines: Boolean = false) {
controller.stopAnimations(animationNames, areStateMachines)
controller.stopAnimations(
animationNames = animationNames,
areStateMachines = areStateMachines
)
}

/**
Expand All @@ -494,7 +497,7 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
* Advanced: Multiple [animation instances][LinearAnimationInstance] can run the same animation.
*/
fun stop(animationName: String, isStateMachine: Boolean = false) {
controller.stopAnimations(animationName, isStateMachine)
controller.stopAnimations(animationName = animationName, isStateMachine = isStateMachine)
}

/**
Expand All @@ -521,7 +524,7 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
rendererAttributes.apply {
this.loop = loop
}
controller.play(loop, direction, settleInitialState)
controller.play(loop = loop, direction = direction, settleInitialState = settleInitialState)
}

/**
Expand All @@ -541,11 +544,11 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
this.loop = loop
}
controller.play(
animationNames,
loop,
direction,
areStateMachines,
settleInitialState
animationNames = animationNames,
loop = loop,
direction = direction,
areStateMachines = areStateMachines,
settleInitialState = settleInitialState
)
}

Expand All @@ -567,11 +570,11 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
this.loop = loop
}
controller.play(
animationName,
loop,
direction,
isStateMachine,
settleInitialState
animationName = animationName,
loop = loop,
direction = direction,
isStateMachine = isStateMachine,
settleInitialState = settleInitialState
)
}

Expand All @@ -588,46 +591,54 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
* Fire the [SMITrigger] input called [inputName] on all active matching state machines
*/
fun fireState(stateMachineName: String, inputName: String) {
controller.fireState(stateMachineName, inputName)
controller.fireState(stateMachineName = stateMachineName, inputName = inputName)
}

/**
* Update the state of the [SMIBoolean] input called [inputName] on all active matching state machines
* to [value]
*/
fun setBooleanState(stateMachineName: String, inputName: String, value: Boolean) {
controller.setBooleanState(stateMachineName, inputName, value)
controller.setBooleanState(
stateMachineName = stateMachineName,
inputName = inputName,
value = value
)
}

/**
* Update the state of the [SMINumber] input called [inputName] on all active matching state machines
* to [value]
*/
fun setNumberState(stateMachineName: String, inputName: String, value: Float) {
controller.setNumberState(stateMachineName, inputName, value)
controller.setNumberState(
stateMachineName = stateMachineName,
inputName = inputName,
value = value
)
}

/**
* Fire the [SMITrigger] input called [inputName] on the nested artboard represented at [path]
*/
fun fireStateAtPath(inputName: String, path: String) {
controller.fireStateAtPath(inputName, path)
controller.fireStateAtPath(inputName = inputName, path = path)
}

/**
* Update the state of the [SMIBoolean] input called [inputName] on the nested artboard represented at [path]
* to [value]
*/
fun setBooleanStateAtPath(inputName: String, value: Boolean, path: String) {
controller.setBooleanStateAtPath(inputName, value, path)
controller.setBooleanStateAtPath(inputName = inputName, value = value, path = path)
}

/**
* Update the state of the [SMINumber] input called [inputName] on the nested artboard represented at [path]
* to [value]
*/
fun setNumberStateAtPath(inputName: String, value: Float, path: String) {
controller.setNumberStateAtPath(inputName, value, path)
controller.setNumberStateAtPath(inputName = inputName, value = value, path = path)
}

/**
Expand All @@ -641,15 +652,15 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
* Get the current value for a text run named [textRunName] on the active artboard if it exists.
*/
fun getTextRunValue(textRunName: String): String? {
return controller.getTextRunValue(textRunName)
return controller.getTextRunValue(textRunName = textRunName)
}

/**
* Get the text value for a text run named [textRunName] on the nested artboard
* represented at [path].
*/
fun getTextRunValue(textRunName: String, path: String): String? {
return controller.getTextRunValue(textRunName, path)
return controller.getTextRunValue(textRunName = textRunName, path = path)
}


Expand All @@ -658,7 +669,7 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
* @throws RiveException if the text run does not exist.
*/
fun setTextRunValue(textRunName: String, textValue: String) {
controller.setTextRunValue(textRunName, textValue)
controller.setTextRunValue(textRunName = textRunName, textValue = textValue)
}

/**
Expand All @@ -667,7 +678,7 @@ open class RiveAnimationView(context: Context, attrs: AttributeSet? = null) :
* @throws RiveException if the text run does not exist.
*/
fun setTextRunValue(textRunName: String, textValue: String, path: String) {
controller.setTextRunValue(textRunName, textValue, path)
controller.setTextRunValue(textRunName = textRunName, textValue = textValue, path = path)
}

/**
Expand Down Expand Up @@ -1151,4 +1162,9 @@ sealed class ResourceType {
* Wraps the data necessary for grabbing an input with [name] with [value]
* [value] is necessary when wrapping [SMINumber] and [SMIBoolean] inputs.
*/
data class ChangedInput(val stateMachineName: String, val name: String, val value: Any? = null, val nestedArtboardPath: String? = null)
data class ChangedInput(
val stateMachineName: String,
val name: String,
val value: Any? = null,
val nestedArtboardPath: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class RiveFileController(
resolveStateMachineAdvance(stateMachineInstance, elapsed)

if (!stillPlaying) {
pause(stateMachineInstance)
pause(stateMachine = stateMachineInstance)
}
}
}
Expand Down Expand Up @@ -389,7 +389,13 @@ class RiveFileController(
settleInitialState: Boolean = true,
) {
animationNames.forEach {
playAnimation(it, loop, direction, areStateMachines, settleInitialState)
playAnimation(
animationName = it,
loop = loop,
direction = direction,
isStateMachine = areStateMachines,
settleInitialState = settleInitialState
)
}
}

Expand All @@ -400,7 +406,13 @@ class RiveFileController(
isStateMachine: Boolean = false,
settleInitialState: Boolean = true,
) {
playAnimation(animationName, loop, direction, isStateMachine, settleInitialState)
playAnimation(
animationName = animationName,
loop = loop,
direction = direction,
isStateMachine = isStateMachine,
settleInitialState = settleInitialState
)
}

/**
Expand All @@ -415,23 +427,28 @@ class RiveFileController(
activeArtboard?.let { activeArtboard ->
if (pausedAnimations.isNotEmpty() || pausedStateMachines.isNotEmpty()) {
animations.forEach {
play(it, direction = direction, loop = loop)
play(animationInstance = it, direction = direction, loop = loop)
}
stateMachines.forEach {
play(it, settleInitialState)
play(stateMachineInstance = it, settleStateMachineState = settleInitialState)
}
} else {
val animationNames = activeArtboard.animationNames
if (animationNames.isNotEmpty()) {
playAnimation(animationNames.first(), loop, direction)
playAnimation(
animationName = animationNames.first(),
loop = loop,
direction = direction
)
}
val stateMachineNames = activeArtboard.stateMachineNames
if (stateMachineNames.isNotEmpty()) {
return playAnimation(
stateMachineNames.first(),
loop,
direction,
settleInitialState
animationName = stateMachineNames.first(),
loop = loop,
direction = direction,
isStateMachine = true,
settleInitialState = settleInitialState
)
}
}
Expand All @@ -454,9 +471,9 @@ class RiveFileController(

fun pause(animationName: String, isStateMachine: Boolean = false) {
if (isStateMachine) {
stateMachines(animationName).forEach { pause(it) }
stateMachines(animationName).forEach { pause(stateMachine = it) }
} else {
animations(animationName).forEach { pause(it) }
animations(animationName).forEach { pause(animation = it) }
}
}

Expand Down Expand Up @@ -497,8 +514,20 @@ class RiveFileController(
* It also tries to start the thread if it's not started, which will internally cause advance
* to happen at least once.
*/
private fun queueInput(stateMachineName: String, inputName: String, value: Any? = null, path: String? = null) {
queueInputs(ChangedInput(stateMachineName, inputName, value, path))
private fun queueInput(
stateMachineName: String,
inputName: String,
value: Any? = null,
path: String? = null
) {
queueInputs(
ChangedInput(
stateMachineName = stateMachineName,
name = inputName,
value = value,
nestedArtboardPath = path
)
)
}

internal fun queueInputs(vararg inputs: ChangedInput) {
Expand Down Expand Up @@ -553,12 +582,32 @@ class RiveFileController(
queueInput(stateMachineName = stateMachineName, inputName = inputName, path = path)
}

fun setBooleanState(stateMachineName: String, inputName: String, value: Boolean, path: String? = null) {
queueInput(stateMachineName = stateMachineName, inputName = inputName, value = value, path = path)
fun setBooleanState(
stateMachineName: String,
inputName: String,
value: Boolean,
path: String? = null
) {
queueInput(
stateMachineName = stateMachineName,
inputName = inputName,
value = value,
path = path
)
}

fun setNumberState(stateMachineName: String, inputName: String, value: Float, path: String? = null) {
queueInput(stateMachineName = stateMachineName, inputName = inputName, value = value, path = path)
fun setNumberState(
stateMachineName: String,
inputName: String,
value: Float,
path: String? = null
) {
queueInput(
stateMachineName = stateMachineName,
inputName = inputName,
value = value,
path = path
)
}

fun fireStateAtPath(inputName: String, path: String) {
Expand Down

0 comments on commit 20c9cb4

Please sign in to comment.