-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRiveEventsExample.tsx
More file actions
155 lines (149 loc) · 3.84 KB
/
RiveEventsExample.tsx
File metadata and controls
155 lines (149 loc) · 3.84 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
/* eslint-disable @typescript-eslint/no-deprecated */
import { View, Text, StyleSheet, ActivityIndicator } from 'react-native';
import { useState, useEffect } from 'react';
import {
Fit,
RiveView,
useRive,
useRiveFile,
type RiveEvent,
RiveEventType,
} from '@rive-app/react-native';
import { type Metadata } from '../shared/metadata';
/**
* @deprecated Rive events at runtime is deprecated. Use data binding instead.
*
* See https://rive.app/docs/runtimes/data-binding
*
* Demonstrates getting and handling Rive events
*/
export default function EventsExample() {
const { riveViewRef, setHybridRef } = useRive();
const { riveFile, isLoading, error } = useRiveFile(
require('../../assets/rive/rating.riv')
);
const [lastEvent, setLastEvent] = useState<RiveEvent | null>(null);
const handleRiveEvent = (event: any) => {
console.log('Rive Event:', event);
if (event.type === RiveEventType.General) {
setLastEvent(event);
}
};
// Add event listener when the ref is available
useEffect(() => {
if (riveViewRef) {
riveViewRef.onEventListener(handleRiveEvent);
}
return () => {
if (riveViewRef) {
riveViewRef.removeEventListeners();
}
};
}, [riveViewRef]);
return (
<View style={styles.container}>
<View style={styles.riveContainer}>
{isLoading ? (
<ActivityIndicator size="large" color="#0000ff" />
) : error ? (
<Text style={styles.errorText}>{error.message}</Text>
) : riveFile ? (
<RiveView
style={styles.rive}
autoPlay={true}
fit={Fit.Contain}
file={riveFile}
hybridRef={setHybridRef}
/>
) : null}
</View>
{lastEvent && (
<View style={styles.eventInfo}>
<Text style={styles.eventTitle}>Last Event:</Text>
<Text>Name: {lastEvent.name}</Text>
<Text>
Type:{' '}
{lastEvent.type === RiveEventType.General
? 'General Event'
: 'Open URL Event'}
</Text>
{lastEvent.type === RiveEventType.OpenUrl && lastEvent.url && (
<Text>URL: {lastEvent.url}</Text>
)}
{lastEvent.type === RiveEventType.OpenUrl && lastEvent.target && (
<Text>Target: {lastEvent.target}</Text>
)}
{lastEvent.properties &&
Object.keys(lastEvent.properties).length > 0 && (
<>
<Text style={styles.propertiesTitle}>Properties:</Text>
{Object.entries(lastEvent.properties).map(([key, value]) => (
<Text key={key}>
{key}: {String(value)}
</Text>
))}
</>
)}
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
padding: 20,
},
title: {
fontSize: 24,
fontWeight: 'bold',
textAlign: 'center',
marginTop: 20,
marginBottom: 10,
},
subtitle: {
fontSize: 16,
color: '#666',
textAlign: 'center',
marginBottom: 20,
},
riveContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f5f5f5',
borderRadius: 10,
overflow: 'hidden',
},
rive: {
width: '100%',
height: '100%',
},
errorText: {
color: 'red',
textAlign: 'center',
padding: 20,
},
eventInfo: {
marginTop: 20,
padding: 15,
backgroundColor: '#f0f0f0',
borderRadius: 8,
},
eventTitle: {
fontSize: 18,
fontWeight: 'bold',
marginBottom: 10,
},
propertiesTitle: {
fontSize: 16,
fontWeight: 'bold',
marginTop: 10,
marginBottom: 5,
},
});
EventsExample.metadata = {
name: 'Events',
description: 'Demonstrates how to listen to and handle Rive events',
} satisfies Metadata;