|
| 1 | +import * as React from 'react'; |
| 2 | +import { |
| 3 | + ActivityIndicator, |
| 4 | + SafeAreaView, |
| 5 | + ScrollView, |
| 6 | + StyleSheet, |
| 7 | + Text, |
| 8 | + View, |
| 9 | +} from 'react-native'; |
| 10 | +import { |
| 11 | + /*Rive, */ Fit, |
| 12 | + /*RNRiveError,*/ useRiveFile, |
| 13 | + RiveView, |
| 14 | +} from 'react-native-rive'; |
| 15 | +import { Picker } from '@react-native-picker/picker'; |
| 16 | + |
| 17 | +export default function StateMachine() { |
| 18 | + const [uri, setUri] = React.useState('https://picsum.photos/id/372/500/500'); |
| 19 | + const { riveFile, isLoading, error } = useRiveFile( |
| 20 | + require('../../assets/rive/out_of_band.riv'), |
| 21 | + { |
| 22 | + referencedAssets: { |
| 23 | + 'Inter-594377': { |
| 24 | + source: require('../../assets/fonts/Inter-594377.ttf'), |
| 25 | + // source: { |
| 26 | + // fileName: 'Inter-594377.ttf', |
| 27 | + // path: 'fonts', // only needed for Android assets |
| 28 | + // }, |
| 29 | + }, |
| 30 | + 'referenced-image-2929282': { |
| 31 | + source: { |
| 32 | + uri: uri, |
| 33 | + }, |
| 34 | + // source: { |
| 35 | + // fileName: 'referenced-image-2929282.png', |
| 36 | + // path: 'images', // only needed for Android assets |
| 37 | + // }, |
| 38 | + }, |
| 39 | + 'referenced_audio-2929340': { |
| 40 | + source: require('../../assets/audio/referenced_audio-2929340.wav'), |
| 41 | + // source: { |
| 42 | + // fileName: 'referenced_audio-2929340.wav', |
| 43 | + // path: 'audio', // only needed for Android assets |
| 44 | + // }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | + ); |
| 49 | + |
| 50 | + if (isLoading) { |
| 51 | + return <ActivityIndicator />; |
| 52 | + } else if (error != null) { |
| 53 | + return ( |
| 54 | + <SafeAreaView style={styles.safeAreaViewContainer}> |
| 55 | + <Text>Error loading Rive file</Text> |
| 56 | + </SafeAreaView> |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <SafeAreaView style={styles.safeAreaViewContainer}> |
| 62 | + <RiveView |
| 63 | + file={riveFile} |
| 64 | + autoplay={true} |
| 65 | + fit={Fit.Contain} |
| 66 | + style={styles.animation} |
| 67 | + stateMachineName="State Machine 1" |
| 68 | + // The `referencedAssets` prop allows you to load external assets from various sources: |
| 69 | + // - A URI |
| 70 | + // - A bundled asset on the native platform (iOS and Android) |
| 71 | + // - A source loaded directly from JavaScript. |
| 72 | + // |
| 73 | + // This example demonstrates multiple ways to load the same asset from different locations. |
| 74 | + // Note: It's not necessary to store the same asset in all these locations; this is for demonstration purposes. |
| 75 | + // |
| 76 | + // The key of the map is the unique asset identifier (as exported in the Editor), |
| 77 | + // which combines the asset name and its unique identifier. |
| 78 | + // You can optionally exclude the unique identifier, for example, instead of 'Inter-594377', you can use 'Inter'. |
| 79 | + // However, it is recommended to use the full identifier to avoid potential conflicts. |
| 80 | + // Using just the asset name allows you to avoid knowing the unique identifier and gives you more control over naming. |
| 81 | + |
| 82 | + artboardName="Artboard" |
| 83 | + resourceName={'out_of_band'} |
| 84 | + onError={(riveError: RNRiveError) => { |
| 85 | + console.log(riveError); |
| 86 | + }} |
| 87 | + /> |
| 88 | + {/* <Text> |
| 89 | + Load in an external asset from a URL, or bundled asset on the native |
| 90 | + platform, or as a source loaded directly from JavaScript. |
| 91 | + </Text> */} |
| 92 | + <ScrollView contentContainerStyle={styles.container}> |
| 93 | + <View style={styles.pickersWrapper}> |
| 94 | + <View style={styles.pickerWrapper}> |
| 95 | + <Picker |
| 96 | + selectedValue={uri} |
| 97 | + onValueChange={(value) => setUri(value)} |
| 98 | + mode={'dropdown'} |
| 99 | + style={styles.picker} |
| 100 | + > |
| 101 | + {[ |
| 102 | + 'https://picsum.photos/id/372/500/500', |
| 103 | + 'https://picsum.photos/id/373/500/500', |
| 104 | + ].map((key) => ( |
| 105 | + <Picker.Item key={key} value={key} label={key} /> |
| 106 | + ))} |
| 107 | + </Picker> |
| 108 | + </View> |
| 109 | + </View> |
| 110 | + </ScrollView> |
| 111 | + </SafeAreaView> |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +const styles = StyleSheet.create({ |
| 116 | + safeAreaViewContainer: { |
| 117 | + flex: 1, |
| 118 | + }, |
| 119 | + container: { |
| 120 | + flexGrow: 1, |
| 121 | + alignItems: 'center', |
| 122 | + justifyContent: 'flex-start', |
| 123 | + }, |
| 124 | + animation: { |
| 125 | + width: '100%', |
| 126 | + height: 400, |
| 127 | + }, |
| 128 | + picker: { |
| 129 | + flex: 1, |
| 130 | + width: '100%', |
| 131 | + }, |
| 132 | + pickerWrapper: { |
| 133 | + borderWidth: 1, |
| 134 | + borderColor: 'black', |
| 135 | + borderRadius: 5, |
| 136 | + alignItems: 'center', |
| 137 | + margin: 16, |
| 138 | + }, |
| 139 | + pickersWrapper: { |
| 140 | + flex: 1, |
| 141 | + padding: 16, |
| 142 | + alignSelf: 'stretch', |
| 143 | + }, |
| 144 | +}); |
0 commit comments