-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDataBindingArtboardsExample.tsx
More file actions
261 lines (239 loc) · 6.5 KB
/
DataBindingArtboardsExample.tsx
File metadata and controls
261 lines (239 loc) · 6.5 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import {
View,
Text,
StyleSheet,
ActivityIndicator,
Pressable,
} from 'react-native';
import { useState, useEffect, useRef } from 'react';
import {
Fit,
RiveView,
useRiveFile,
useViewModelInstance,
type RiveFile,
type BindableArtboard,
} from '@rive-app/react-native';
import { type Metadata } from '../shared/metadata';
/**
* Data Binding Artboards Example
*
* Demonstrates swapping artboards at runtime using data binding.
* The main Rive file includes a view model with a property of type `Artboard`
* called "CharacterArtboard". This property can be set to any artboard from
* either the main file or an external file.
*
* Marketplace:
* - Main: https://rive.app/marketplace/24641-46042-data-binding-artboards/
* - Assets: https://rive.app/marketplace/24642-47536-data-binding-artboards/
*
* Docs: https://rive.app/docs/runtimes/data-binding#artboards
*/
export default function DataBindingArtboardsExample() {
// Main scene file - contains the Card view model with CharacterArtboard property
const {
riveFile: mainFile,
isLoading: mainLoading,
error: mainError,
} = useRiveFile(require('../../assets/swap_character_main.riv'));
// Assets file - contains "Character 1" and "Character 2" artboards
const {
riveFile: assetsFile,
isLoading: assetsLoading,
error: assetsError,
} = useRiveFile(require('../../assets/swap_character_assets.riv'));
const isLoading = mainLoading || assetsLoading;
const error = mainError || assetsError;
if (isLoading) {
return (
<View style={styles.container}>
<ActivityIndicator size="large" color="#0000ff" />
<Text style={styles.loadingText}>Loading Rive files...</Text>
</View>
);
}
if (error || !mainFile || !assetsFile) {
return (
<View style={styles.container}>
<Text style={styles.errorText}>
{error?.message || 'Failed to load Rive files'}
</Text>
</View>
);
}
return <ArtboardSwapper mainFile={mainFile} assetsFile={assetsFile} />;
}
function ArtboardSwapper({
mainFile,
assetsFile,
}: {
mainFile: RiveFile;
assetsFile: RiveFile;
}) {
const { instance, error } = useViewModelInstance(mainFile);
const [currentArtboard, setCurrentArtboard] = useState<string>('Dragon');
const initializedRef = useRef(false);
// Set initial artboard on mount
useEffect(() => {
if (initializedRef.current || !instance) return;
initializedRef.current = true;
const artboardProp = instance.artboardProperty('CharacterArtboard');
if (artboardProp) {
try {
const bindable = assetsFile.getBindableArtboard('Character 1');
artboardProp.set(bindable);
} catch (e) {
console.error(`Failed to set initial artboard: ${e}`);
}
}
}, [instance, assetsFile]);
if (error) {
console.error(error.message);
return <Text style={{ color: 'red' }}>{error.message}</Text>;
}
// Map display names to actual artboard names
const artboardOptions = [
{ label: 'Dragon', artboard: 'Character 1', fromAssets: true },
{ label: 'Gator', artboard: 'Character 2', fromAssets: true },
{ label: 'Placeholder', artboard: 'Placeholder', fromAssets: false },
];
const swapArtboard = (option: (typeof artboardOptions)[number]) => {
if (!instance) return;
const artboardProp = instance.artboardProperty('CharacterArtboard');
if (!artboardProp) {
console.error('Artboard property "CharacterArtboard" not found');
return;
}
try {
const sourceFile = option.fromAssets ? assetsFile : mainFile;
const bindable: BindableArtboard = sourceFile.getBindableArtboard(
option.artboard
);
artboardProp.set(bindable);
setCurrentArtboard(option.label);
} catch (e) {
console.error(`Failed to swap artboard: ${e}`);
}
};
if (!instance) {
return (
<View style={styles.container}>
<ActivityIndicator size="large" color="#0000ff" />
</View>
);
}
return (
<View style={styles.container}>
<Text style={styles.title}>Data Binding Artboards</Text>
<Text style={styles.subtitle}>
Swap artboards at runtime from different files
</Text>
<View style={styles.riveContainer}>
<RiveView
style={styles.rive}
autoPlay={true}
dataBind={instance}
fit={Fit.Layout}
layoutScaleFactor={1.0}
file={mainFile}
artboardName="Main"
stateMachineName="State Machine 1"
/>
</View>
<View style={styles.infoContainer}>
<Text style={styles.infoText}>Current: {currentArtboard}</Text>
</View>
<View style={styles.buttonContainer}>
{artboardOptions.map((option) => (
<Pressable
key={option.label}
style={[
styles.button,
currentArtboard === option.label && styles.buttonActive,
]}
onPress={() => swapArtboard(option)}
>
<Text style={styles.buttonText}>
{option.label}
{option.fromAssets ? ' (external)' : ' (internal)'}
</Text>
</Pressable>
))}
</View>
</View>
);
}
DataBindingArtboardsExample.metadata = {
name: 'Data Binding Artboards',
description: 'Swap artboards at runtime using data binding properties',
} satisfies Metadata;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
padding: 16,
},
title: {
fontSize: 20,
fontWeight: 'bold',
textAlign: 'center',
marginBottom: 4,
},
subtitle: {
fontSize: 14,
color: '#666',
textAlign: 'center',
marginBottom: 16,
},
riveContainer: {
flex: 1,
backgroundColor: '#f5f5f5',
borderRadius: 8,
overflow: 'hidden',
},
rive: {
flex: 1,
},
infoContainer: {
marginVertical: 12,
padding: 12,
backgroundColor: '#f0f0f0',
borderRadius: 8,
},
infoText: {
fontSize: 14,
color: '#333',
fontWeight: '600',
},
buttonContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
gap: 8,
},
button: {
paddingHorizontal: 16,
paddingVertical: 10,
backgroundColor: '#007AFF',
borderRadius: 8,
},
buttonActive: {
backgroundColor: '#34C759',
},
buttonText: {
fontSize: 14,
fontWeight: '600',
color: '#fff',
},
loadingText: {
marginTop: 12,
textAlign: 'center',
color: '#666',
},
errorText: {
color: 'red',
textAlign: 'center',
fontSize: 16,
fontWeight: 'bold',
marginBottom: 16,
},
});