Add event handler extraction support to rive code generator#4
Open
dbellizzi wants to merge 1 commit intorive-app:mainfrom
Open
Add event handler extraction support to rive code generator#4dbellizzi wants to merge 1 commit intorive-app:mainfrom
dbellizzi wants to merge 1 commit intorive-app:mainfrom
Conversation
This enhancement enables the rive code generator to extract event handlers
from state machines, addressing a limitation where only inputs were extracted
but events that could be fired back to the host application were missing.
## Changes Made
### C++ Code Generator (src/main.cpp):
- Added headers for event system: rive/event.hpp, rive/animation/listener_fire_event.hpp
- Added EventInfo struct to hold event information
- Added state_machine_events field to ArtboardData struct
- Implemented get_state_machine_events_from_artboard() function that:
- Iterates through state machine listeners
- Checks for ListenerFireEvent actions
- Resolves event IDs to Event objects
- Extracts unique event names per state machine
- Integrated event extraction into template generation logic
### JSON Template (templates/json_template.mustache):
- Added "events" section within each state machine
- Events are formatted as key-value pairs: "eventCamelCase": "eventName"
- Maintains backward compatibility with existing JSON structure
## API Usage
The implementation uses the correct Rive API pattern:
1. StateMachine->listener(i) to access listeners
2. ListenerAction->is<ListenerFireEvent>() to check action type
3. ListenerFireEvent->eventId() to get event ID
4. ArtboardInstance->resolve(eventId) to get Event object
5. Event->name() to extract event name
## Example Output
Before (only inputs):
```json
"stateMachines": {
"monsterV1": {
"inputs": { ... }
}
}
```
After (inputs + events):
```json
"stateMachines": {
"monsterV1": {
"inputs": { ... },
"events": {
"monstertapped": "monsterTapped",
"enteropenmouth": "enterOpenMouth",
"exitopenmouth": "exitOpenMouth"
}
}
}
```
## Testing
Tested with multiple .riv files including complex state machines with events.
The enhancement correctly extracts events while maintaining all existing
functionality for inputs, animations, and other data.
Resolves the limitation where event handlers like "monsterTapped" were
present in Rive files but not accessible in generated code.
cerupcat
approved these changes
Aug 22, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I had claude code write this, but tested the result on all of our Rive files. Please have a look and see if you'd like to include it.
This enhancement enables the rive code generator to extract event handlers from state machines, addressing a limitation where only inputs were extracted but events that could be fired back to the host application were missing.
Changes Made
C++ Code Generator (src/main.cpp):
JSON Template (templates/json_template.mustache):
API Usage
The implementation uses the correct Rive API pattern:
Example Output
Before (only inputs):
After (inputs + events):
Testing
Tested with multiple .riv files including complex state machines with events. The enhancement correctly extracts events while maintaining all existing functionality for inputs, animations, and other data.
Resolves the limitation where event handlers like "monsterTapped" were present in Rive files but not accessible in generated code.