Rework text/event count/at. Storing the source artboard events and text runs leads to problems when using instanced artboards as the nested artboards use references to the source objects. Noesis ran into this when trying to use the API to change the text run's value but weren't seeing anything getting marked dirty as the returns runs effectively were owned by a different artboard. This dries up the API and fixes references by using two generalized count and indexAt methods. Further details here! https://github.com/rive-app/rive/pull/6043#issuecomment-1922305190 Diffs= f5e458f80 Rework text/event count/at. (#6548) Co-authored-by: Luigi Rosso <luigi-rosso@users.noreply.github.com>
diff --git a/.rive_head b/.rive_head index ab6d12c..5f86ca1 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -852b17bca7e7c2d3deb7f2d09876b3b548115200 +f5e458f807056bf2522620aafaa60a048cd38d47
diff --git a/include/rive/artboard.hpp b/include/rive/artboard.hpp index 2b1d1bc..6fe3baa 100644 --- a/include/rive/artboard.hpp +++ b/include/rive/artboard.hpp
@@ -42,8 +42,6 @@ std::vector<Core*> m_Objects; std::vector<LinearAnimation*> m_Animations; std::vector<StateMachine*> m_StateMachines; - std::vector<TextValueRun*> m_TextValueRuns; - std::vector<Event*> m_Events; std::vector<Component*> m_DependencyOrder; std::vector<Drawable*> m_Drawables; std::vector<DrawTarget*> m_DrawTargets; @@ -71,8 +69,6 @@ void addObject(Core* object); void addAnimation(LinearAnimation* object); void addStateMachine(StateMachine* object); - void addTextValueRun(TextValueRun* object); - void addEvent(Event* object); public: Artboard() {} @@ -136,6 +132,35 @@ return nullptr; } + template <typename T = Component> size_t count() + { + size_t count = 0; + for (auto object : m_Objects) + { + if (object != nullptr && object->is<T>()) + { + count++; + } + } + return count; + } + + template <typename T = Component> T* objectAt(size_t index) + { + size_t count = 0; + for (auto object : m_Objects) + { + if (object != nullptr && object->is<T>()) + { + if (count++ == index) + { + return static_cast<T*>(object); + } + } + } + return nullptr; + } + template <typename T = Component> std::vector<T*> find() { std::vector<T*> results; @@ -155,12 +180,6 @@ size_t stateMachineCount() const { return m_StateMachines.size(); } std::string stateMachineNameAt(size_t index) const; - size_t textValueRunCount() const { return m_TextValueRuns.size(); } - TextValueRun* textValueRunAt(size_t index) const; - - size_t eventCount() const { return m_Events.size(); } - Event* eventAt(size_t index) const; - LinearAnimation* firstAnimation() const { return animation(0); } LinearAnimation* animation(const std::string& name) const; LinearAnimation* animation(size_t index) const; @@ -206,14 +225,6 @@ { artboardClone->m_StateMachines.push_back(stateMachine); } - for (auto textRun : m_TextValueRuns) - { - artboardClone->m_TextValueRuns.push_back(textRun); - } - for (auto event : m_Events) - { - artboardClone->m_Events.push_back(event); - } if (artboardClone->initialize() != StatusCode::Ok) {
diff --git a/include/rive/event.hpp b/include/rive/event.hpp index 906f562..743c6c5 100644 --- a/include/rive/event.hpp +++ b/include/rive/event.hpp
@@ -8,7 +8,6 @@ { public: void trigger(const CallbackData& value) override; - StatusCode import(ImportStack& importStack) override; }; } // namespace rive
diff --git a/include/rive/importers/artboard_importer.hpp b/include/rive/importers/artboard_importer.hpp index b9fba12..1525803 100644 --- a/include/rive/importers/artboard_importer.hpp +++ b/include/rive/importers/artboard_importer.hpp
@@ -21,8 +21,6 @@ void addComponent(Core* object); void addAnimation(LinearAnimation* animation); void addStateMachine(StateMachine* stateMachine); - void addTextValueRun(TextValueRun* textValueRun); - void addEvent(Event* event); StatusCode resolve() override; const Artboard* artboard() const { return m_Artboard; }
diff --git a/include/rive/text/text_value_run.hpp b/include/rive/text/text_value_run.hpp index 0807b34..3bdca17 100644 --- a/include/rive/text/text_value_run.hpp +++ b/include/rive/text/text_value_run.hpp
@@ -10,7 +10,6 @@ public: StatusCode onAddedClean(CoreContext* context) override; StatusCode onAddedDirty(CoreContext* context) override; - StatusCode import(ImportStack& importStack) override; TextStyle* style() { return m_style; } uint32_t offset() const;
diff --git a/src/artboard.cpp b/src/artboard.cpp index 2b2ea3e..0aa29a3 100644 --- a/src/artboard.cpp +++ b/src/artboard.cpp
@@ -378,10 +378,6 @@ void Artboard::addStateMachine(StateMachine* object) { m_StateMachines.push_back(object); } -void Artboard::addTextValueRun(TextValueRun* object) { m_TextValueRuns.push_back(object); } - -void Artboard::addEvent(Event* object) { m_Events.push_back(object); } - Core* Artboard::resolve(uint32_t id) const { if (id >= static_cast<int>(m_Objects.size())) @@ -727,24 +723,6 @@ return index; } -TextValueRun* Artboard::textValueRunAt(size_t index) const -{ - if (index >= m_TextValueRuns.size()) - { - return nullptr; - } - return m_TextValueRuns[index]; -} - -Event* Artboard::eventAt(size_t index) const -{ - if (index >= m_Events.size()) - { - return nullptr; - } - return m_Events[index]; -} - // std::unique_ptr<ArtboardInstance> Artboard::instance() const // { // std::unique_ptr<ArtboardInstance> artboardClone(new ArtboardInstance);
diff --git a/src/event.cpp b/src/event.cpp index 39dade4..ad1582e 100644 --- a/src/event.cpp +++ b/src/event.cpp
@@ -8,15 +8,4 @@ void Event::trigger(const CallbackData& value) { value.context()->reportEvent(this, value.delaySeconds()); -} - -StatusCode Event::import(ImportStack& importStack) -{ - auto artboardImporter = importStack.latest<ArtboardImporter>(ArtboardBase::typeKey); - if (artboardImporter == nullptr) - { - return StatusCode::MissingObject; - } - artboardImporter->addEvent(this); - return Super::import(importStack); } \ No newline at end of file
diff --git a/src/importers/artboard_importer.cpp b/src/importers/artboard_importer.cpp index ccb524c..d0d93df 100644 --- a/src/importers/artboard_importer.cpp +++ b/src/importers/artboard_importer.cpp
@@ -22,13 +22,6 @@ m_Artboard->addStateMachine(stateMachine); } -void ArtboardImporter::addTextValueRun(TextValueRun* textValueRun) -{ - m_Artboard->addTextValueRun(textValueRun); -} - -void ArtboardImporter::addEvent(Event* event) { m_Artboard->addEvent(event); } - StatusCode ArtboardImporter::resolve() { return m_Artboard->initialize(); } bool ArtboardImporter::readNullObject()
diff --git a/src/text/text_value_run.cpp b/src/text/text_value_run.cpp index 5093ca2..3a86641 100644 --- a/src/text/text_value_run.cpp +++ b/src/text/text_value_run.cpp
@@ -44,17 +44,6 @@ return StatusCode::Ok; } -StatusCode TextValueRun::import(ImportStack& importStack) -{ - auto artboardImporter = importStack.latest<ArtboardImporter>(ArtboardBase::typeKey); - if (artboardImporter == nullptr) - { - return StatusCode::MissingObject; - } - artboardImporter->addTextValueRun(this); - return Super::import(importStack); -} - void TextValueRun::styleIdChanged() { auto coreObject = artboard()->resolve(styleId());
diff --git a/test/state_machine_event_test.cpp b/test/state_machine_event_test.cpp index 1b06681..3a6e2c2 100644 --- a/test/state_machine_event_test.cpp +++ b/test/state_machine_event_test.cpp
@@ -125,7 +125,7 @@ auto file = ReadRiveFile("../../test/assets/event_on_listener.riv"); auto artboard = file->artboard(); - auto eventCount = artboard->eventCount(); + auto eventCount = artboard->count<rive::Event>(); REQUIRE(eventCount == 4); } @@ -134,7 +134,7 @@ auto file = ReadRiveFile("../../test/assets/event_on_listener.riv"); auto artboard = file->artboard(); - auto event = artboard->eventAt(0); + auto event = artboard->objectAt<rive::Event>(0); REQUIRE(event->name() == "Somewhere.com"); }
diff --git a/test/text_test.cpp b/test/text_test.cpp index c009a05..1afaf93 100644 --- a/test/text_test.cpp +++ b/test/text_test.cpp
@@ -33,7 +33,7 @@ auto file = ReadRiveFile("../../test/assets/new_text.riv"); auto artboard = file->artboard(); - auto textRunCount = artboard->textValueRunCount(); + auto textRunCount = artboard->count<rive::TextValueRun>(); REQUIRE(textRunCount == 22); } @@ -42,7 +42,7 @@ auto file = ReadRiveFile("../../test/assets/hello_world.riv"); auto artboard = file->artboard(); - auto textRun = artboard->textValueRunAt(0); + auto textRun = artboard->objectAt<rive::TextValueRun>(0); REQUIRE(textRun->text() == "Hello World!"); }