-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathQuickStart.tsx
More file actions
89 lines (78 loc) · 2.11 KB
/
QuickStart.tsx
File metadata and controls
89 lines (78 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Rive React Native Quick Start (Nitro)
Resources:
- Getting Started: https://rive.app/docs/runtimes/react-native/react-native
- Data Binding: https://rive.app/docs/runtimes/data-binding
*/
import { Button, View, StyleSheet } from 'react-native';
import {
RiveView,
useRive,
useRiveFile,
useRiveNumber,
useRiveTrigger,
useViewModelInstance,
Fit,
} from '@rive-app/react-native';
import type { Metadata } from '../shared/metadata';
export default function QuickStart() {
const { riveFile } = useRiveFile(
require('../../assets/rive/quick_start.riv')
);
const { riveViewRef, setHybridRef } = useRive();
const { instance: viewModelInstance } = useViewModelInstance(riveFile, {
onInit: (vmi) => vmi.numberProperty('health')!.set(9),
});
const { setValue: setHealth } = useRiveNumber('health', viewModelInstance);
const { trigger: gameOverTrigger } = useRiveTrigger(
'gameOver',
viewModelInstance,
{ onTrigger: () => console.log('Game Over Triggered') }
);
const handleTakeDamage = () => {
setHealth((h) => (h ?? 0) - 7);
riveViewRef!.play();
};
const handleMaxHealth = () => {
setHealth(100);
riveViewRef!.play();
};
const handleGameOver = () => {
setHealth(0);
gameOverTrigger();
riveViewRef!.play();
};
return (
<View style={styles.container}>
{riveFile && viewModelInstance && (
<RiveView
hybridRef={setHybridRef}
file={riveFile}
fit={Fit.Layout}
style={styles.rive}
autoPlay={true}
dataBind={viewModelInstance}
/>
)}
<Button onPress={handleTakeDamage} title="Take Damage" />
<Button onPress={handleMaxHealth} title="Max Health" />
<Button onPress={handleGameOver} title="Game Over" />
</View>
);
}
QuickStart.metadata = {
name: 'Quick Start',
description: 'Basic data binding example with health and game over trigger',
order: 0,
} satisfies Metadata;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
rive: {
width: '100%',
height: 400,
},
});