add support for view model trigger based listeners (#10323) e7ef71b568 Co-authored-by: hernan <hernan@rive.app>
diff --git a/.rive_head b/.rive_head index b1ecd35..a1a68fa 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -af18705be2294cf120d220bd89a6d64adcd17fd1 +e7ef71b56849da7b093cf7f10c47be71e0b8a754
diff --git a/dev/defs/animation/state_machine_listener.json b/dev/defs/animation/state_machine_listener.json index 72a25e1..b6494b1 100644 --- a/dev/defs/animation/state_machine_listener.json +++ b/dev/defs/animation/state_machine_listener.json
@@ -36,6 +36,17 @@ "string": "eventid" }, "description": "Event id for the associated event, if listenerType is event" + }, + "viewModelPathIds": { + "type": "List<Id>", + "typeRuntime": "Bytes", + "encoded": true, + "initialValue": "[]", + "key": { + "int": 868, + "string": "viewmodelpathids" + }, + "description": "Path to the selected view model trigger property if listenerType is a viewModel trigger." } } } \ No newline at end of file
diff --git a/include/rive/animation/state_machine_instance.hpp b/include/rive/animation/state_machine_instance.hpp index f53f129..f26059a 100644 --- a/include/rive/animation/state_machine_instance.hpp +++ b/include/rive/animation/state_machine_instance.hpp
@@ -37,6 +37,7 @@ class DataBind; class BindableProperty; class HitDrawable; +class ListenerViewModel; #ifdef WITH_RIVE_TOOLS class StateMachineInstance; @@ -162,11 +163,14 @@ NestedArtboard* parentNestedArtboard() { return m_parentNestedArtboard; } void notify(const std::vector<EventReport>& events, NestedArtboard* context) override; + void notifyListenerViewModels(); /// Tracks an event that reported, will be cleared at the end of the next /// advance. void reportEvent(Event* event, float secondsDelay = 0.0f) override; + void reportListenerViewModel(ListenerViewModel*); + /// Gets the number of events that reported since the last advance. std::size_t reportedEventCount() const; @@ -210,6 +214,8 @@ StateMachineInstance* m_parentStateMachineInstance = nullptr; NestedArtboard* m_parentNestedArtboard = nullptr; std::vector<DataBind*> m_dataBinds; + std::vector<ListenerViewModel*> m_listenerViewModels; + std::vector<ListenerViewModel*> m_reportedListenerViewModels; std::unordered_map<BindableProperty*, BindableProperty*> m_bindablePropertyInstances; std::unordered_map<BindableProperty*, DataBind*>
diff --git a/include/rive/animation/state_machine_listener.hpp b/include/rive/animation/state_machine_listener.hpp index 051bc7a..c068860 100644 --- a/include/rive/animation/state_machine_listener.hpp +++ b/include/rive/animation/state_machine_listener.hpp
@@ -30,6 +30,15 @@ void performChanges(StateMachineInstance* stateMachineInstance, Vec2D position, Vec2D previousPosition) const; + void decodeViewModelPathIds(Span<const uint8_t> value) override; + void copyViewModelPathIds(const StateMachineListenerBase& object) override; + std::vector<uint32_t> viewModelPathIdsBuffer() const + { + return m_viewModelPathIdsBuffer; + } + +protected: + std::vector<uint32_t> m_viewModelPathIdsBuffer; private: void addAction(std::unique_ptr<ListenerAction>);
diff --git a/include/rive/generated/animation/state_machine_listener_base.hpp b/include/rive/generated/animation/state_machine_listener_base.hpp index f65a966..dc80d46 100644 --- a/include/rive/generated/animation/state_machine_listener_base.hpp +++ b/include/rive/generated/animation/state_machine_listener_base.hpp
@@ -1,7 +1,9 @@ #ifndef _RIVE_STATE_MACHINE_LISTENER_BASE_HPP_ #define _RIVE_STATE_MACHINE_LISTENER_BASE_HPP_ #include "rive/animation/state_machine_component.hpp" +#include "rive/core/field_types/core_bytes_type.hpp" #include "rive/core/field_types/core_uint_type.hpp" +#include "rive/span.hpp" namespace rive { class StateMachineListenerBase : public StateMachineComponent @@ -31,6 +33,7 @@ static const uint16_t targetIdPropertyKey = 224; static const uint16_t listenerTypeValuePropertyKey = 225; static const uint16_t eventIdPropertyKey = 399; + static const uint16_t viewModelPathIdsPropertyKey = 868; protected: uint32_t m_TargetId = -1; @@ -71,12 +74,17 @@ eventIdChanged(); } + virtual void decodeViewModelPathIds(Span<const uint8_t> value) = 0; + virtual void copyViewModelPathIds( + const StateMachineListenerBase& object) = 0; + Core* clone() const override; void copy(const StateMachineListenerBase& object) { m_TargetId = object.m_TargetId; m_ListenerTypeValue = object.m_ListenerTypeValue; m_EventId = object.m_EventId; + copyViewModelPathIds(object); StateMachineComponent::copy(object); } @@ -93,6 +101,9 @@ case eventIdPropertyKey: m_EventId = CoreUintType::deserialize(reader); return true; + case viewModelPathIdsPropertyKey: + decodeViewModelPathIds(CoreBytesType::deserialize(reader)); + return true; } return StateMachineComponent::deserialize(propertyKey, reader); } @@ -101,6 +112,7 @@ virtual void targetIdChanged() {} virtual void listenerTypeValueChanged() {} virtual void eventIdChanged() {} + virtual void viewModelPathIdsChanged() {} }; } // namespace rive
diff --git a/include/rive/generated/core_registry.hpp b/include/rive/generated/core_registry.hpp index 07f5868..d465527 100644 --- a/include/rive/generated/core_registry.hpp +++ b/include/rive/generated/core_registry.hpp
@@ -3925,6 +3925,7 @@ case ExportAudioBase::volumePropertyKey: return CoreDoubleType::id; case NestedArtboardBase::dataBindPathIdsPropertyKey: + case StateMachineListenerBase::viewModelPathIdsPropertyKey: case MeshBase::triangleIndexBytesPropertyKey: case DataConverterOperationViewModelBase::sourcePathIdsPropertyKey: case DataBindContextBase::sourcePathIdsPropertyKey:
diff --git a/include/rive/listener_type.hpp b/include/rive/listener_type.hpp index 0cad98c..63e890a 100644 --- a/include/rive/listener_type.hpp +++ b/include/rive/listener_type.hpp
@@ -12,8 +12,10 @@ event = 5, click = 6, draggableConstraint = 7, - dragStart = 8, - dragEnd = 9, + textInput = 8, + dragStart = 9, + dragEnd = 10, + viewModel = 11, }; } #endif \ No newline at end of file
diff --git a/src/animation/state_machine_instance.cpp b/src/animation/state_machine_instance.cpp index 563430b..3382fae 100644 --- a/src/animation/state_machine_instance.cpp +++ b/src/animation/state_machine_instance.cpp
@@ -39,6 +39,7 @@ #include "rive/text/text.hpp" #include "rive/math/math_types.hpp" #include "rive/audio_event.hpp" +#include "rive/dirtyable.hpp" #include <unordered_map> #include <chrono> @@ -1033,6 +1034,8 @@ case ListenerType::event: case ListenerType::click: case ListenerType::draggableConstraint: + case ListenerType::textInput: + case ListenerType::viewModel: break; } } @@ -1052,6 +1055,8 @@ case ListenerType::event: case ListenerType::click: case ListenerType::draggableConstraint: + case ListenerType::textInput: + case ListenerType::viewModel: break; } } @@ -1145,6 +1150,8 @@ case ListenerType::event: case ListenerType::click: case ListenerType::draggableConstraint: + case ListenerType::textInput: + case ListenerType::viewModel: break; } } @@ -1164,6 +1171,8 @@ case ListenerType::event: case ListenerType::click: case ListenerType::draggableConstraint: + case ListenerType::textInput: + case ListenerType::viewModel: break; } } @@ -1186,6 +1195,45 @@ void prepareEvent(Vec2D position, ListenerType hitType) override {} }; +class ListenerViewModel : public Dirtyable +{ +public: + virtual ~ListenerViewModel() = default; + ListenerViewModel(StateMachineInstance* smInstance, + const StateMachineListener* listener) : + m_stateMachineInstance(smInstance), m_listener(listener) + {} + void clearDataContext() + { + if (m_viewModelInstanceValue != nullptr) + { + m_viewModelInstanceValue->removeDependent(this); + m_viewModelInstanceValue = nullptr; + } + } + void bindFromContext(DataContext* dataContext) + { + clearDataContext(); + auto path = m_listener->viewModelPathIdsBuffer(); + auto vmProp = dataContext->getViewModelProperty(path); + if (vmProp != nullptr) + { + m_viewModelInstanceValue = vmProp; + vmProp->addDependent(this); + } + } + void addDirt(ComponentDirt value, bool recurse) + { + m_stateMachineInstance->reportListenerViewModel(this); + } + const StateMachineListener* listener() { return m_listener; } + +private: + StateMachineInstance* m_stateMachineInstance = nullptr; + const StateMachineListener* m_listener = nullptr; + ViewModelInstanceValue* m_viewModelInstanceValue = nullptr; +}; + } // namespace rive HitResult StateMachineInstance::updateListeners(Vec2D position, @@ -1498,6 +1546,12 @@ { continue; } + if (listener->listenerType() == ListenerType::viewModel) + { + auto vmListener = new ListenerViewModel(this, listener); + m_listenerViewModels.push_back(vmListener); + continue; + } auto listenerGroup = rivestd::make_unique<ListenerGroup>(listener); auto target = m_artboardInstance->resolve(listener->targetId()); if (target != nullptr && target->is<Component>()) @@ -1618,6 +1672,10 @@ delete pair.second; pair.second = nullptr; } + for (auto& listenerViewModel : m_listenerViewModels) + { + delete listenerViewModel; + } if (m_ownsDataContext) { delete m_DataContext; @@ -1762,6 +1820,8 @@ { this->notifyEventListeners(m_reportedEvents, nullptr); m_reportedEvents.clear(); + notifyListenerViewModels(); + m_reportedListenerViewModels.clear(); m_needsAdvance = false; } updateDataBinds(); @@ -1921,6 +1981,10 @@ dataBind->as<DataBindContext>()->bindFromContext(dataContext); } } + for (auto listenerViewModel : m_listenerViewModels) + { + listenerViewModel->bindFromContext(dataContext); + } } void StateMachineInstance::clearDataContext() @@ -1930,6 +1994,10 @@ delete m_DataContext; m_DataContext = nullptr; } + for (auto& listenerViewModel : m_listenerViewModels) + { + listenerViewModel->clearDataContext(); + } m_ownsDataContext = false; } @@ -2009,6 +2077,12 @@ m_reportedEvents.push_back(EventReport(event, delaySeconds)); } +void StateMachineInstance::reportListenerViewModel( + ListenerViewModel* listenerViewModel) +{ + m_reportedListenerViewModels.push_back(listenerViewModel); +} + std::size_t StateMachineInstance::reportedEventCount() const { return m_reportedEvents.size(); @@ -2030,6 +2104,19 @@ updateDataBinds(); } +void StateMachineInstance::notifyListenerViewModels() +{ + if (m_reportedListenerViewModels.size() > 0) + { + for (auto& listenerViewModel : m_reportedListenerViewModels) + { + listenerViewModel->listener()->performChanges(this, + Vec2D(), + Vec2D()); + } + } +} + void StateMachineInstance::notifyEventListeners( const std::vector<EventReport>& events, NestedArtboard* source) @@ -2051,10 +2138,10 @@ ? artboard() : source->artboardInstance(); - // NOTE: this issue can't happen anymore because a new fix - // in the editor prevents selecting other artboard as - // target. But the fix is kept here to fix older files. - // listener->eventId() can point to an id from an + // NOTE: this issue can't happen anymore because a new + // fix in the editor prevents selecting other artboard + // as target. But the fix is kept here to fix older + // files. listener->eventId() can point to an id from an // event in the context of this artboard or the // context of a nested artboard. Because those ids // belong to different contexts, they can have the
diff --git a/src/animation/state_machine_listener.cpp b/src/animation/state_machine_listener.cpp index bc7cbaa..b113d85 100644 --- a/src/animation/state_machine_listener.cpp +++ b/src/animation/state_machine_listener.cpp
@@ -49,4 +49,21 @@ { action->perform(stateMachineInstance, position, previousPosition); } +} + +void StateMachineListener::decodeViewModelPathIds(Span<const uint8_t> value) +{ + BinaryReader reader(value); + while (!reader.reachedEnd()) + { + auto val = reader.readVarUintAs<uint32_t>(); + m_viewModelPathIdsBuffer.push_back(val); + } +} + +void StateMachineListener::copyViewModelPathIds( + const StateMachineListenerBase& object) +{ + m_viewModelPathIdsBuffer = + object.as<StateMachineListener>()->m_viewModelPathIdsBuffer; } \ No newline at end of file
diff --git a/tests/unit_tests/assets/trigger_based_listeners.riv b/tests/unit_tests/assets/trigger_based_listeners.riv new file mode 100644 index 0000000..8c95705 --- /dev/null +++ b/tests/unit_tests/assets/trigger_based_listeners.riv Binary files differ
diff --git a/tests/unit_tests/runtime/data_binding_test.cpp b/tests/unit_tests/runtime/data_binding_test.cpp index c8d5ac3..6dd843d 100644 --- a/tests/unit_tests/runtime/data_binding_test.cpp +++ b/tests/unit_tests/runtime/data_binding_test.cpp
@@ -1143,3 +1143,42 @@ CHECK(silver.matches("computed_root_transform-list")); } + +TEST_CASE("Trigger based listeners", "[data binding]") +{ + + rive::SerializingFactory silver; + auto file = ReadRiveFile("assets/trigger_based_listeners.riv", &silver); + + auto artboard = file->artboardNamed("main"); + + silver.frameSize(artboard->width(), artboard->height()); + + REQUIRE(artboard != nullptr); + auto stateMachine = artboard->stateMachineAt(0); + int viewModelId = artboard.get()->viewModelId(); + + auto vmi = viewModelId == -1 + ? file->createViewModelInstance(artboard.get()) + : file->createViewModelInstance(viewModelId, 0); + + stateMachine->bindViewModelInstance(vmi); + stateMachine->advanceAndApply(0.0f); + stateMachine->advanceAndApply(0.016f); + + auto renderer = silver.makeRenderer(); + artboard->draw(renderer.get()); + stateMachine->pointerDown(rive::Vec2D(25.0f, 25.0f)); + // Advance and apply twice to take the transition and apply the next state. + stateMachine->advanceAndApply(0.1f); + stateMachine->advanceAndApply(1.0f); + stateMachine->pointerUp(rive::Vec2D(25.0f, 25.0f)); + // Advance and apply twice to take the transition and apply the next state. + stateMachine->advanceAndApply(0.1f); + stateMachine->advanceAndApply(1.0f); + + silver.addFrame(); + artboard->draw(renderer.get()); + + CHECK(silver.matches("trigger_based_listeners")); +}
diff --git a/tests/unit_tests/silvers/trigger_based_listeners.sriv b/tests/unit_tests/silvers/trigger_based_listeners.sriv new file mode 100644 index 0000000..9146f36 --- /dev/null +++ b/tests/unit_tests/silvers/trigger_based_listeners.sriv Binary files differ