1
1
package app.rive.runtime.example
2
2
3
+ import android.graphics.Color
3
4
import android.os.Bundle
4
5
import android.view.Gravity
5
6
import android.view.View
6
7
import android.widget.*
7
8
import androidx.appcompat.app.AppCompatActivity
8
9
import androidx.appcompat.widget.AppCompatButton
10
+ import androidx.appcompat.widget.AppCompatCheckBox
11
+ import androidx.appcompat.widget.AppCompatEditText
9
12
import app.rive.runtime.kotlin.RiveAnimationView
10
13
import app.rive.runtime.kotlin.RiveDrawable.Listener
11
14
import app.rive.runtime.kotlin.core.*
12
15
13
16
class AndroidPlayerActivity : AppCompatActivity () {
14
17
var loop: Loop = Loop .NONE
15
18
var direction: Direction = Direction .AUTO
19
+ var playButtonMap: HashMap <String , View > = HashMap ()
20
+ var pauseButtonMap: HashMap <String , View > = HashMap ()
21
+ var stopButtonMap: HashMap <String , View > = HashMap ()
16
22
17
23
val animationResources = listOf (
18
24
R .raw.artboard_animations,
@@ -22,13 +28,16 @@ class AndroidPlayerActivity : AppCompatActivity() {
22
28
R .raw.flux_capacitor,
23
29
R .raw.loopy,
24
30
R .raw.mascot,
31
+ R .raw.neostream,
25
32
R .raw.off_road_car_blog,
26
33
R .raw.progress,
27
34
R .raw.pull,
28
35
R .raw.rope,
36
+ R .raw.skills,
29
37
R .raw.trailblaze,
38
+ R .raw.ui_swipe_left_to_delete,
30
39
R .raw.vader,
31
- R .raw.wacky
40
+ R .raw.wacky,
32
41
)
33
42
34
43
val animationView by lazy(LazyThreadSafetyMode .NONE ) {
@@ -44,6 +53,11 @@ class AndroidPlayerActivity : AppCompatActivity() {
44
53
animationView.artboardName
45
54
animationView.setRiveResource(animationResources[index], artboardName = null )
46
55
setSpinner()
56
+
57
+ playButtonMap.clear()
58
+ pauseButtonMap.clear()
59
+ stopButtonMap.clear()
60
+
47
61
animationView.drawable.file?.firstArtboard?.name?.let {
48
62
loadArtboard(it)
49
63
}
@@ -92,25 +106,31 @@ class AndroidPlayerActivity : AppCompatActivity() {
92
106
layout.gravity = Gravity .END
93
107
94
108
var text = TextView (this )
95
- text.setText( animationName)
109
+ text.text = animationName
96
110
97
111
var playButton = AppCompatButton (this )
98
- playButton.setText(" >" )
112
+ playButton.text = " >"
113
+ playButton.background.setTint(Color .WHITE )
99
114
playButton.setOnClickListener {
100
115
animationView.play(animationName, loop, direction)
101
116
}
117
+ playButtonMap[animationName] = playButton
102
118
103
119
var pauseButton = AppCompatButton (this )
104
- pauseButton.setText(" ||" )
120
+ pauseButton.text = " ||"
121
+ pauseButton.background.setTint(Color .WHITE )
105
122
pauseButton.setOnClickListener {
106
123
animationView.pause(animationName)
107
124
}
125
+ pauseButtonMap[animationName] = pauseButton
108
126
109
127
var stopButton = AppCompatButton (this )
110
- stopButton.setText(" []" )
128
+ stopButton.text = " []"
129
+ stopButton.background.setTint(Color .RED )
111
130
stopButton.setOnClickListener {
112
131
animationView.stop(animationName)
113
132
}
133
+ stopButtonMap[animationName] = stopButton
114
134
115
135
116
136
layout.addView(text)
@@ -120,12 +140,128 @@ class AndroidPlayerActivity : AppCompatActivity() {
120
140
return layout
121
141
}
122
142
143
+
144
+ fun addStateMachineControl (artboard : Artboard , stateMachineName : String ): List <View > {
145
+ val views = mutableListOf<View >()
146
+ val layout = LinearLayout (this )
147
+ layout.orientation = LinearLayout .HORIZONTAL
148
+ layout.gravity = Gravity .END
149
+
150
+ val text = TextView (this )
151
+ text.text = stateMachineName
152
+
153
+ val playButton = AppCompatButton (this )
154
+ playButton.text = " >"
155
+ playButton.background.setTint(Color .WHITE )
156
+ playButton.setOnClickListener {
157
+ animationView.play(stateMachineName, loop, direction, isStateMachine = true )
158
+ }
159
+ playButtonMap[stateMachineName] = playButton
160
+
161
+ val pauseButton = AppCompatButton (this )
162
+ pauseButton.text = " ||"
163
+ pauseButton.background.setTint(Color .WHITE )
164
+ pauseButton.setOnClickListener {
165
+ animationView.pause(stateMachineName, isStateMachine = true )
166
+ }
167
+ pauseButtonMap[stateMachineName] = pauseButton
168
+
169
+ val stopButton = AppCompatButton (this )
170
+ stopButton.text = " []"
171
+ stopButton.background.setTint(Color .RED )
172
+ stopButton.setOnClickListener {
173
+ animationView.stop(stateMachineName, isStateMachine = true )
174
+ }
175
+ stopButtonMap[stateMachineName] = stopButton
176
+
177
+ val stateMachine = artboard.stateMachine(stateMachineName)
178
+
179
+ layout.addView(text)
180
+ layout.addView(playButton)
181
+ layout.addView(pauseButton)
182
+ layout.addView(stopButton)
183
+ views.add(layout)
184
+
185
+
186
+ stateMachine.inputs.forEach {
187
+ val layout = LinearLayout (this )
188
+ layout.orientation = LinearLayout .HORIZONTAL
189
+ layout.gravity = Gravity .END
190
+
191
+ val text = TextView (this )
192
+ text.text = it.name
193
+ layout.addView(text)
194
+
195
+ if (it.isTrigger) {
196
+ val triggerButton = AppCompatButton (this )
197
+ triggerButton.text = " Fire"
198
+ triggerButton.background.setTint(Color .WHITE )
199
+ triggerButton.setOnClickListener { _ ->
200
+ animationView.fireState(stateMachineName, it.name)
201
+ }
202
+ layout.addView(triggerButton)
203
+ }
204
+
205
+ if (it.isBoolean) {
206
+ val boolBox = AppCompatCheckBox (this )
207
+ if ((it as StateMachineBooleanInput ).value) {
208
+ boolBox.isChecked = true
209
+ }
210
+ boolBox.setOnCheckedChangeListener { _, b ->
211
+ animationView.setBooleanState(stateMachineName, it.name, b)
212
+ }
213
+ layout.addView(boolBox)
214
+ }
215
+
216
+ if (it.isNumber) {
217
+ val editText = AppCompatEditText (this )
218
+ editText.setText((it as StateMachineNumberInput ).value.toString())
219
+ val editTriggerButton = AppCompatButton (this )
220
+ editTriggerButton.text = " Apply"
221
+ editTriggerButton.background.setTint(Color .WHITE )
222
+ editTriggerButton.setOnClickListener { _ ->
223
+ try {
224
+ var value = editText.text.toString().toFloat()
225
+ animationView.setNumberState(stateMachineName, it.name, value)
226
+ } catch (e: Error ) {
227
+
228
+ }
229
+ }
230
+
231
+ layout.addView(editText)
232
+ layout.addView(editTriggerButton)
233
+ }
234
+
235
+ views.add(layout)
236
+ }
237
+
238
+ return views
239
+ }
240
+
123
241
fun loadArtboard (artboardName : String ) {
124
242
var controls = findViewById<LinearLayout >(R .id.controls)
125
243
controls.removeAllViews()
126
- animationView.drawable.file?.artboard(artboardName)?.animationNames?.forEach {
127
- controls.addView(addAnimationControl(it))
244
+ animationView.drawable.file?.artboard(artboardName)?.let { artboard ->
245
+ if (artboard.stateMachineNames.size > 0 ) {
246
+ val stateMachineHeader = TextView (this )
247
+ stateMachineHeader.text = " State Machines:"
248
+ controls.addView(stateMachineHeader)
249
+ artboard.stateMachineNames.forEach {
250
+ addStateMachineControl(artboard, it).forEach {
251
+ controls.addView(it)
252
+ }
253
+ }
254
+ }
255
+ if (artboard.animationNames.size > 0 ) {
256
+ val animationsHeader = TextView (this )
257
+ animationsHeader.text = " Animations:"
258
+ controls.addView(animationsHeader)
259
+ artboard.animationNames.forEach {
260
+ controls.addView(addAnimationControl(it))
261
+ }
262
+ }
128
263
}
264
+
129
265
}
130
266
131
267
fun setSpinner () {
@@ -135,7 +271,7 @@ class AndroidPlayerActivity : AppCompatActivity() {
135
271
this ,
136
272
android.R .layout.simple_spinner_dropdown_item,
137
273
artboardNames
138
- );
274
+ )
139
275
dropdown.adapter = adapter
140
276
dropdown.onItemSelectedListener = object : AdapterView .OnItemSelectedListener {
141
277
override fun onItemSelected (
@@ -163,7 +299,7 @@ class AndroidPlayerActivity : AppCompatActivity() {
163
299
this ,
164
300
android.R .layout.simple_spinner_dropdown_item,
165
301
resourceNames
166
- );
302
+ )
167
303
dropdown.adapter = adapter
168
304
dropdown.onItemSelectedListener = object : AdapterView .OnItemSelectedListener {
169
305
override fun onItemSelected (
@@ -190,36 +326,92 @@ class AndroidPlayerActivity : AppCompatActivity() {
190
326
val events = findViewById<LinearLayout >(R .id.events)
191
327
val listener = object : Listener {
192
328
override fun notifyPlay (animation : PlayableInstance ) {
193
- val text = TextView (that)
194
- text.setText(" Play ${(animation as LinearAnimationInstance ).animation.name} " )
195
- events.addView(text, 0 )
329
+ var text: String? = null
330
+ if (animation is LinearAnimationInstance ) {
331
+ text = animation.animation.name
332
+ } else if (animation is StateMachineInstance ) {
333
+ text = animation.stateMachine.name
334
+ }
335
+ text?.let {
336
+ val textView = TextView (that)
337
+ textView.text = " Play $text "
338
+ 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
+ }
348
+ }
196
349
}
197
350
198
351
override fun notifyPause (animation : PlayableInstance ) {
199
- val text = TextView (that)
200
- text.setText(" Pause ${(animation as LinearAnimationInstance ).animation.name} " )
201
- events.addView(text, 0 )
352
+ var text: String? = null
353
+ if (animation is LinearAnimationInstance ) {
354
+ text = animation.animation.name
355
+ } else if (animation is StateMachineInstance ) {
356
+ text = animation.stateMachine.name
357
+ }
358
+ text?.let {
359
+ val textView = TextView (that)
360
+ textView.text = " Pause $text "
361
+ 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
+ }
371
+ }
202
372
}
203
373
204
374
override fun notifyStop (animation : PlayableInstance ) {
205
- val text = TextView (that)
206
- text.setText(" Stop ${(animation as LinearAnimationInstance ).animation.name} " )
207
- events.addView(text, 0 )
375
+ var text: String? = null
376
+ if (animation is LinearAnimationInstance ) {
377
+ text = animation.animation.name
378
+ } else if (animation is StateMachineInstance ) {
379
+ text = animation.stateMachine.name
380
+ }
381
+ text?.let {
382
+ val textView = TextView (that)
383
+ textView.text = " Stop $text "
384
+ 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
+ }
394
+ }
208
395
}
209
396
210
397
override fun notifyLoop (animation : PlayableInstance ) {
211
- val text = TextView (that)
212
- text.setText(" Loop ${(animation as LinearAnimationInstance ).animation.name} " )
213
- events.addView(text, 0 )
398
+ if (animation is LinearAnimationInstance ) {
399
+ val text = TextView (that)
400
+ text.text = " Loop ${animation.animation.name} "
401
+ events.addView(text, 0 )
402
+ }
214
403
}
215
404
}
216
405
217
406
animationView.registerListener(listener)
218
-
219
407
}
220
408
409
+
221
410
override fun onDestroy () {
222
411
super .onDestroy()
223
412
animationView.destroy()
224
413
}
414
+
415
+
225
416
}
417
+
0 commit comments