Skip to content

Commit f90fb32

Browse files
authored
Merge pull request #103 from rive-app/102
102
2 parents cea1b9a + 92ef02d commit f90fb32

File tree

21 files changed

+436
-72
lines changed

21 files changed

+436
-72
lines changed

app/src/main/java/app/rive/runtime/example/AndroidPlayerActivity.kt

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class AndroidPlayerActivity : AppCompatActivity() {
2323
val animationResources = listOf(
2424
R.raw.artboard_animations,
2525
R.raw.basketball,
26+
R.raw.clipping,
2627
R.raw.explorer,
2728
R.raw.f22,
2829
R.raw.flux_capacitor,
@@ -101,30 +102,30 @@ class AndroidPlayerActivity : AppCompatActivity() {
101102
}
102103

103104
fun addAnimationControl(animationName: String): View {
104-
var layout = LinearLayout(this)
105+
val layout = LinearLayout(this)
105106
layout.orientation = LinearLayout.HORIZONTAL
106107
layout.gravity = Gravity.END
107108

108-
var text = TextView(this)
109+
val text = TextView(this)
109110
text.text = animationName
110111

111-
var playButton = AppCompatButton(this)
112+
val playButton = AppCompatButton(this)
112113
playButton.text = ">"
113114
playButton.background.setTint(Color.WHITE)
114115
playButton.setOnClickListener {
115116
animationView.play(animationName, loop, direction)
116117
}
117118
playButtonMap[animationName] = playButton
118119

119-
var pauseButton = AppCompatButton(this)
120+
val pauseButton = AppCompatButton(this)
120121
pauseButton.text = "||"
121122
pauseButton.background.setTint(Color.WHITE)
122123
pauseButton.setOnClickListener {
123124
animationView.pause(animationName)
124125
}
125126
pauseButtonMap[animationName] = pauseButton
126127

127-
var stopButton = AppCompatButton(this)
128+
val stopButton = AppCompatButton(this)
128129
stopButton.text = "[]"
129130
stopButton.background.setTint(Color.RED)
130131
stopButton.setOnClickListener {
@@ -184,13 +185,13 @@ class AndroidPlayerActivity : AppCompatActivity() {
184185

185186

186187
stateMachine.inputs.forEach {
187-
val layout = LinearLayout(this)
188-
layout.orientation = LinearLayout.HORIZONTAL
189-
layout.gravity = Gravity.END
188+
val innerLayout = LinearLayout(this)
189+
innerLayout.orientation = LinearLayout.HORIZONTAL
190+
innerLayout.gravity = Gravity.END
190191

191-
val text = TextView(this)
192-
text.text = it.name
193-
layout.addView(text)
192+
val innerText = TextView(this)
193+
innerText.text = it.name
194+
innerLayout.addView(innerText)
194195

195196
if (it.isTrigger) {
196197
val triggerButton = AppCompatButton(this)
@@ -199,7 +200,7 @@ class AndroidPlayerActivity : AppCompatActivity() {
199200
triggerButton.setOnClickListener { _ ->
200201
animationView.fireState(stateMachineName, it.name)
201202
}
202-
layout.addView(triggerButton)
203+
innerLayout.addView(triggerButton)
203204
}
204205

205206
if (it.isBoolean) {
@@ -210,7 +211,7 @@ class AndroidPlayerActivity : AppCompatActivity() {
210211
boolBox.setOnCheckedChangeListener { _, b ->
211212
animationView.setBooleanState(stateMachineName, it.name, b)
212213
}
213-
layout.addView(boolBox)
214+
innerLayout.addView(boolBox)
214215
}
215216

216217
if (it.isNumber) {
@@ -221,25 +222,25 @@ class AndroidPlayerActivity : AppCompatActivity() {
221222
editTriggerButton.background.setTint(Color.WHITE)
222223
editTriggerButton.setOnClickListener { _ ->
223224
try {
224-
var value = editText.text.toString().toFloat()
225+
val value = editText.text.toString().toFloat()
225226
animationView.setNumberState(stateMachineName, it.name, value)
226227
} catch (e: Error) {
227228

228229
}
229230
}
230231

231-
layout.addView(editText)
232-
layout.addView(editTriggerButton)
232+
innerLayout.addView(editText)
233+
innerLayout.addView(editTriggerButton)
233234
}
234235

235-
views.add(layout)
236+
views.add(innerLayout)
236237
}
237238

238239
return views
239240
}
240241

241242
fun loadArtboard(artboardName: String) {
242-
var controls = findViewById<LinearLayout>(R.id.controls)
243+
val controls = findViewById<LinearLayout>(R.id.controls)
243244
controls.removeAllViews()
244245
animationView.drawable.file?.artboard(artboardName)?.let { artboard ->
245246
if (artboard.stateMachineNames.size > 0) {
@@ -266,8 +267,8 @@ class AndroidPlayerActivity : AppCompatActivity() {
266267

267268
fun setSpinner() {
268269
animationView.drawable.file?.artboardNames?.let { artboardNames ->
269-
var dropdown = findViewById<Spinner>(R.id.artboards)
270-
var adapter = ArrayAdapter<String>(
270+
val dropdown = findViewById<Spinner>(R.id.artboards)
271+
val adapter = ArrayAdapter<String>(
271272
this,
272273
android.R.layout.simple_spinner_dropdown_item,
273274
artboardNames
@@ -294,8 +295,8 @@ class AndroidPlayerActivity : AppCompatActivity() {
294295

295296
fun setResourceSpinner() {
296297
animationResources.let { _ ->
297-
var dropdown = findViewById<Spinner>(R.id.resources)
298-
var adapter = ArrayAdapter<String>(
298+
val dropdown = findViewById<Spinner>(R.id.resources)
299+
val adapter = ArrayAdapter<String>(
299300
this,
300301
android.R.layout.simple_spinner_dropdown_item,
301302
resourceNames
@@ -332,19 +333,13 @@ class AndroidPlayerActivity : AppCompatActivity() {
332333
} else if (animation is StateMachineInstance) {
333334
text = animation.stateMachine.name
334335
}
335-
text?.let {
336+
text?.let { theText ->
336337
val textView = TextView(that)
337-
textView.text = "Play $text"
338+
textView.text = "Play $theText"
338339
events.addView(textView, 0)
339-
playButtonMap.get(text)?.let {
340-
it.background.setTint(Color.GREEN)
341-
}
342-
pauseButtonMap.get(text)?.let {
343-
it.background.setTint(Color.WHITE)
344-
}
345-
stopButtonMap.get(text)?.let {
346-
it.background.setTint(Color.WHITE)
347-
}
340+
playButtonMap[theText]?.background?.setTint(Color.GREEN)
341+
pauseButtonMap[theText]?.background?.setTint(Color.WHITE)
342+
stopButtonMap[theText]?.background?.setTint(Color.WHITE)
348343
}
349344
}
350345

@@ -359,15 +354,9 @@ class AndroidPlayerActivity : AppCompatActivity() {
359354
val textView = TextView(that)
360355
textView.text = "Pause $text"
361356
events.addView(textView, 0)
362-
playButtonMap.get(text)?.let {
363-
it.background.setTint(Color.WHITE)
364-
}
365-
pauseButtonMap.get(text)?.let {
366-
it.background.setTint(Color.BLUE)
367-
}
368-
stopButtonMap.get(text)?.let {
369-
it.background.setTint(Color.WHITE)
370-
}
357+
playButtonMap[text]?.background?.setTint(Color.WHITE)
358+
pauseButtonMap[text]?.background?.setTint(Color.BLUE)
359+
stopButtonMap[text]?.background?.setTint(Color.WHITE)
371360
}
372361
}
373362

@@ -382,15 +371,9 @@ class AndroidPlayerActivity : AppCompatActivity() {
382371
val textView = TextView(that)
383372
textView.text = "Stop $text"
384373
events.addView(textView, 0)
385-
playButtonMap.get(text)?.let {
386-
it.background.setTint(Color.WHITE)
387-
}
388-
pauseButtonMap.get(text)?.let {
389-
it.background.setTint(Color.WHITE)
390-
}
391-
stopButtonMap.get(text)?.let {
392-
it.background.setTint(Color.RED)
393-
}
374+
playButtonMap[text]?.background?.setTint(Color.WHITE)
375+
pauseButtonMap[text]?.background?.setTint(Color.WHITE)
376+
stopButtonMap[text]?.background?.setTint(Color.RED)
394377
}
395378
}
396379

@@ -401,6 +384,13 @@ class AndroidPlayerActivity : AppCompatActivity() {
401384
events.addView(text, 0)
402385
}
403386
}
387+
388+
override fun notifyStateChanged(layerState: LayerState) {
389+
390+
val text = TextView(that)
391+
text.text = "State Changed $layerState"
392+
events.addView(text, 0)
393+
}
404394
}
405395

406396
animationView.registerListener(listener)

app/src/main/res/raw/clipping.riv

253 Bytes
Binary file not shown.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "jni_refs.hpp"
2+
#include "helpers/general.hpp"
3+
4+
// From rive-cpp
5+
#include "animation/layer_state.hpp"
6+
#include "animation/exit_state.hpp"
7+
#include "animation/entry_state.hpp"
8+
#include "animation/any_state.hpp"
9+
#include "animation/animation_state.hpp"
10+
11+
#ifdef __cplusplus
12+
extern "C"
13+
{
14+
#endif
15+
16+
// ANIMATION
17+
JNIEXPORT jboolean JNICALL Java_app_rive_runtime_kotlin_core_LayerState_cppIsExitState(
18+
JNIEnv *env,
19+
jobject thisObj,
20+
jlong ref)
21+
{
22+
23+
rive::LayerState *layerState = (rive::LayerState *)ref;
24+
return layerState->is<rive::ExitState>();
25+
}
26+
27+
JNIEXPORT jboolean JNICALL Java_app_rive_runtime_kotlin_core_LayerState_cppIsAnyState(
28+
JNIEnv *env,
29+
jobject thisObj,
30+
jlong ref)
31+
{
32+
33+
rive::LayerState *layerState = (rive::LayerState *)ref;
34+
return layerState->is<rive::AnyState>();
35+
}
36+
37+
JNIEXPORT jboolean JNICALL Java_app_rive_runtime_kotlin_core_LayerState_cppIsEntryState(
38+
JNIEnv *env,
39+
jobject thisObj,
40+
jlong ref)
41+
{
42+
43+
rive::LayerState *layerState = (rive::LayerState *)ref;
44+
return layerState->is<rive::EntryState>();
45+
}
46+
47+
JNIEXPORT jboolean JNICALL Java_app_rive_runtime_kotlin_core_LayerState_cppIsAnimationState(
48+
JNIEnv *env,
49+
jobject thisObj,
50+
jlong ref)
51+
{
52+
53+
rive::LayerState *layerState = (rive::LayerState *)ref;
54+
return layerState->is<rive::AnimationState>();
55+
}
56+
57+
JNIEXPORT jlong JNICALL Java_app_rive_runtime_kotlin_core_AnimationState_cppAnimation(
58+
JNIEnv *env,
59+
jobject thisObj,
60+
jlong ref)
61+
{
62+
63+
rive::AnimationState *animationState = (rive::AnimationState *)ref;
64+
return (long)animationState->animation();
65+
}
66+
67+
#ifdef __cplusplus
68+
}
69+
#endif

cpp/src/bindings/bindings_linear_animation_instance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C"
2424
rive::LinearAnimation *animation = (rive::LinearAnimation *)animationRef;
2525

2626
// TODO: delete this object?
27-
auto animationInstance = new rive::LinearAnimationInstance(animation);
27+
rive::LinearAnimationInstance *animationInstance = new rive::LinearAnimationInstance(animation);
2828

2929
return (jlong)animationInstance;
3030
}
@@ -44,7 +44,7 @@ extern "C"
4444

4545
if (didLoop)
4646
{
47-
auto loopType = animationInstance->animation()->loop();
47+
rive::Loop loopType = animationInstance->animation()->loop();
4848
switch (loopType)
4949
{
5050
case rive::Loop::oneShot:

cpp/src/bindings/bindings_state_machine_instance.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern "C"
2222
rive::StateMachine *animation = (rive::StateMachine *)stateMachineRef;
2323

2424
// TODO: delete this object?
25-
auto stateMachineInstance = new rive::StateMachineInstance(animation);
25+
rive::StateMachineInstance *stateMachineInstance = new rive::StateMachineInstance(animation);
2626

2727
return (jlong)stateMachineInstance;
2828
}
@@ -37,6 +37,25 @@ extern "C"
3737
return stateMachineInstance->advance(elapsedTime);
3838
}
3939

40+
JNIEXPORT jint JNICALL Java_app_rive_runtime_kotlin_core_StateMachineInstance_cppStateChangedCount(
41+
JNIEnv *env,
42+
jobject thisObj,
43+
jlong ref)
44+
{
45+
rive::StateMachineInstance *stateMachineInstance = (rive::StateMachineInstance *)ref;
46+
return stateMachineInstance->stateChangedCount();
47+
}
48+
49+
JNIEXPORT jlong JNICALL Java_app_rive_runtime_kotlin_core_StateMachineInstance_cppStateChangedByIndex(
50+
JNIEnv *env,
51+
jobject thisObj,
52+
jlong ref,
53+
jint index)
54+
{
55+
rive::StateMachineInstance *stateMachineInstance = (rive::StateMachineInstance *)ref;
56+
return (jlong)stateMachineInstance->stateChangedByIndex(index);
57+
}
58+
4059
JNIEXPORT void JNICALL Java_app_rive_runtime_kotlin_core_StateMachineInstance_cppApply(
4160
JNIEnv *env,
4261
jobject thisObj,

0 commit comments

Comments
 (0)