-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/markeremitter component #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0e69178
feat: add event system primitives (EventListener, Delegate, MarkerEve…
M1KUS3Q a70d52b
feat: add onSceneReady lifecycle and getEvent/resolveEvents to Compon…
M1KUS3Q 99be3bf
feat: trigger onSceneReady lifecycle from Parser
M1KUS3Q d221afb
feat: add MarkerEmitterComponent protobuf message and extend Componen…
M1KUS3Q 2a00cc4
feat: implement MarkerEmitterComponent
M1KUS3Q 926523a
test: add unit tests for event system and MarkerEmitterComponent
M1KUS3Q 0a371fc
chore: format via clang-format
M1KUS3Q 1f462ce
feat: implement Subscription and ListenerStore for easier delegate ma…
M1KUS3Q be10c12
fix: format necessary changes
M1KUS3Q File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ CTestTestfile.cmake | |
| _deps/ | ||
|
|
||
| # IDEs and Editors | ||
| .clangd | ||
| .vscode/ | ||
| .idea/ | ||
| *.swp | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #ifndef DELEGATE_HPP | ||
| #define DELEGATE_HPP | ||
|
|
||
| #include <cstddef> | ||
| #include <memory> | ||
|
|
||
| #include "events/EventListener.hpp" | ||
| #include "events/Subscription.hpp" | ||
|
|
||
| struct Context; | ||
|
|
||
| class Delegate { | ||
| public: | ||
| Delegate() : store(std::make_shared<ListenerStore>()) {} | ||
| ~Delegate() = default; | ||
|
|
||
| Delegate(const Delegate&) = delete; | ||
| Delegate& operator=(const Delegate&) = delete; | ||
| Delegate(Delegate&&) = delete; | ||
| Delegate& operator=(Delegate&&) = delete; | ||
|
|
||
| Subscription subscribe(EventListener* listener) { | ||
| const std::size_t subscriptionId = store->nextId++; | ||
| store->listeners.push_back({listener, subscriptionId}); | ||
| return Subscription(store, subscriptionId); | ||
| } | ||
|
|
||
| void invoke(const Context& ctx) { | ||
| for (const auto& entry : store->listeners) { | ||
| entry.listener->onEventTriggered(ctx); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| std::shared_ptr<ListenerStore> store; | ||
| }; | ||
|
|
||
| #endif // DELEGATE_HPP |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #ifndef EVENTLISTENER_HPP | ||
| #define EVENTLISTENER_HPP | ||
|
|
||
| struct Context; | ||
|
|
||
| class EventListener { | ||
| public: | ||
| EventListener() = default; | ||
| virtual ~EventListener() = default; | ||
|
|
||
| EventListener(const EventListener&) = delete; | ||
| EventListener& operator=(const EventListener&) = delete; | ||
| EventListener(EventListener&&) = delete; | ||
| EventListener& operator=(EventListener&&) = delete; | ||
|
|
||
| virtual void onEventTriggered(const Context& ctx) = 0; | ||
| }; | ||
|
|
||
| #endif // EVENTLISTENER_HPP |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #ifndef MARKEREVENTLINK_HPP | ||
| #define MARKEREVENTLINK_HPP | ||
|
|
||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include "data_structures/Context.hpp" | ||
| #include "events/EventListener.hpp" | ||
|
|
||
| class MarkerEventLink : public EventListener { | ||
| public: | ||
| explicit MarkerEventLink(std::string name) : eventName(std::move(name)) {} | ||
|
|
||
| void onEventTriggered(const Context& ctx) override { | ||
| if (ctx.markers != nullptr) { | ||
| ctx.markers->push_back(eventName); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| std::string eventName; | ||
| }; | ||
|
|
||
| #endif // MARKEREVENTLINK_HPP |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #ifndef SUBSCRIPTION_HPP | ||
| #define SUBSCRIPTION_HPP | ||
|
|
||
| #include <cstddef> | ||
| #include <memory> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| class EventListener; | ||
|
|
||
| // Shared listener bookkeeping. A Delegate owns one of these via std::shared_ptr and hands a | ||
| // std::weak_ptr to each Subscription it creates, so a Subscription can detect when its Delegate | ||
| // has already been destroyed and skip unsubscribing (a no-op instead of UB). | ||
| struct ListenerStore { | ||
| struct Entry { | ||
| EventListener* listener; | ||
| std::size_t subscriptionId; | ||
| }; | ||
|
|
||
| std::vector<Entry> listeners; | ||
| std::size_t nextId = 0; | ||
| }; | ||
|
|
||
| // RAII handle returned by Delegate::subscribe. Unsubscribes from its Delegate on destruction; | ||
| // if the Delegate is already gone the operation is a no-op. | ||
| class Subscription { | ||
| public: | ||
| Subscription(std::weak_ptr<ListenerStore> store, std::size_t subscriptionId) | ||
| : store(std::move(store)), subscriptionId(subscriptionId) {} | ||
|
|
||
| ~Subscription() { | ||
| if (auto lockedStore = store.lock()) { | ||
| std::erase_if(lockedStore->listeners, | ||
| [subscriptionId = subscriptionId](const ListenerStore::Entry& entry) { | ||
| return entry.subscriptionId == subscriptionId; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| Subscription(const Subscription&) = delete; | ||
| Subscription& operator=(const Subscription&) = delete; | ||
| Subscription(Subscription&&) noexcept = default; | ||
| Subscription& operator=(Subscription&&) noexcept = default; | ||
|
|
||
| private: | ||
| std::weak_ptr<ListenerStore> store; | ||
| std::size_t subscriptionId; | ||
| }; | ||
|
|
||
| #endif // SUBSCRIPTION_HPP |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #ifndef MARKEREMITTERCOMPONENT_HPP | ||
| #define MARKEREMITTERCOMPONENT_HPP | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "events/MarkerEventLink.hpp" | ||
| #include "events/Subscription.hpp" | ||
| #include "scene/components/Component.hpp" | ||
|
|
||
| class Scene; | ||
|
|
||
| namespace NeuronIDE { | ||
| class Component; | ||
| } | ||
|
|
||
| class MarkerEmitterComponent : public Component { | ||
| public: | ||
| struct Binding { | ||
| std::string targetObject; | ||
| std::string targetEvent; | ||
| std::string markerName; | ||
| }; | ||
|
|
||
| MarkerEmitterComponent(const std::shared_ptr<SceneObject>& owner, | ||
| std::vector<Binding> bindings); | ||
| ~MarkerEmitterComponent() override = default; | ||
|
|
||
| MarkerEmitterComponent(const MarkerEmitterComponent&) = delete; | ||
| MarkerEmitterComponent& operator=(const MarkerEmitterComponent&) = delete; | ||
| MarkerEmitterComponent(MarkerEmitterComponent&&) = delete; | ||
| MarkerEmitterComponent& operator=(MarkerEmitterComponent&&) = delete; | ||
|
|
||
| void update(const Context& context) override; | ||
| void render(SDL_Renderer* renderer) override; | ||
| void onSceneReady(Scene& scene) override; | ||
|
|
||
| static std::unique_ptr<Component> createMarkerEmitter( | ||
| const NeuronIDE::Component& protoComp, const std::shared_ptr<SceneObject>& owner); | ||
|
|
||
| private: | ||
| struct ActiveSubscription { | ||
| // Declaration order matters: the Subscription is destroyed first, unsubscribing before | ||
| // the listener it points to is freed. | ||
| std::unique_ptr<MarkerEventLink> link; | ||
| Subscription subscription; | ||
| }; | ||
|
|
||
| std::vector<Binding> bindings; | ||
| std::vector<ActiveSubscription> activeSubscriptions; | ||
| }; | ||
|
|
||
| #endif // MARKEREMITTERCOMPONENT_HPP | ||
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #include "scene/components/MarkerEmitterComponent.hpp" | ||
|
|
||
| #include "events/Delegate.hpp" | ||
| #include "neuronide.pb.h" | ||
| #include "scene/Scene.hpp" | ||
| #include "scene/components/ComponentRegistry.hpp" | ||
|
|
||
| MarkerEmitterComponent::MarkerEmitterComponent(const std::shared_ptr<SceneObject>& owner, | ||
| std::vector<Binding> bindings) | ||
| : Component(owner), bindings(std::move(bindings)) {} | ||
|
|
||
| void MarkerEmitterComponent::update(const Context& context) { (void)context; } | ||
|
|
||
| void MarkerEmitterComponent::render(SDL_Renderer* renderer) { (void)renderer; } | ||
|
|
||
| void MarkerEmitterComponent::onSceneReady(Scene& scene) { | ||
| for (const auto& binding : bindings) { | ||
| auto& delegate = scene.resolveEvents(binding.targetObject, binding.targetEvent); | ||
|
|
||
| auto link = std::make_unique<MarkerEventLink>(binding.markerName); | ||
| auto sub = delegate.subscribe(link.get()); | ||
|
|
||
| activeSubscriptions.push_back({std::move(link), std::move(sub)}); | ||
| } | ||
| } | ||
|
|
||
| std::unique_ptr<Component> MarkerEmitterComponent::createMarkerEmitter( | ||
| const NeuronIDE::Component& protoComp, const std::shared_ptr<SceneObject>& owner) { | ||
| std::vector<Binding> bindings; | ||
| for (const auto& protoBinding : protoComp.marker_emitter().bindings()) { | ||
| bindings.push_back({protoBinding.target_object(), protoBinding.target_event(), | ||
| protoBinding.marker_name()}); | ||
| } | ||
| return std::make_unique<MarkerEmitterComponent>(owner, std::move(bindings)); | ||
| } | ||
|
|
||
| REGISTER_COMPONENT(NeuronIDE::Component::kMarkerEmitter, | ||
| MarkerEmitterComponent::createMarkerEmitter) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.