-
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
Open
M1KUS3Q
wants to merge
7
commits into
main
Choose a base branch
from
feat/markeremitter-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 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 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 <algorithm> | ||
| #include <vector> | ||
|
|
||
| #include "events/EventListener.hpp" | ||
|
|
||
| struct Context; | ||
|
|
||
| class Delegate { | ||
| public: | ||
| Delegate() = default; | ||
| ~Delegate() = default; | ||
|
|
||
| Delegate(const Delegate&) = delete; | ||
| Delegate& operator=(const Delegate&) = delete; | ||
| Delegate(Delegate&&) = delete; | ||
| Delegate& operator=(Delegate&&) = delete; | ||
|
|
||
| void subscribe(EventListener* listener) { listeners.push_back(listener); } | ||
|
|
||
| void unsubscribe(EventListener* listener) { | ||
| listeners.erase(std::remove(listeners.begin(), listeners.end(), listener), listeners.end()); | ||
| } | ||
|
|
||
| void invoke(const Context& ctx) { | ||
| for (auto* listener : listeners) { | ||
| listener->onEventTriggered(ctx); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes) | ||
| std::vector<EventListener*> listeners; | ||
| }; | ||
|
|
||
| #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
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,52 @@ | ||
| #ifndef MARKEREMITTERCOMPONENT_HPP | ||
| #define MARKEREMITTERCOMPONENT_HPP | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "events/MarkerEventLink.hpp" | ||
| #include "scene/components/Component.hpp" | ||
|
|
||
| class Delegate; | ||
| 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; | ||
|
|
||
| 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 { | ||
| std::unique_ptr<MarkerEventLink> link; | ||
| Delegate* delegate = nullptr; | ||
| }; | ||
|
|
||
| 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,44 @@ | ||
| #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)) {} | ||
|
|
||
| MarkerEmitterComponent::~MarkerEmitterComponent() { | ||
| for (auto& sub : activeSubscriptions) { | ||
| sub.delegate->unsubscribe(sub.link.get()); | ||
| } | ||
| } | ||
|
|
||
| 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); | ||
| delegate.subscribe(link.get()); | ||
|
|
||
| activeSubscriptions.push_back({std::move(link), &delegate}); | ||
| } | ||
| } | ||
|
|
||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One decision worth talking about is storing "Binding"s and "ActiveSubscription"s separately. ActiveSubscription is a resolved binding, and I think we should think if the types shouldn't be combined, having uninitialized fields that get initialized onSceneReady. The downside of that approach is more complicated logic if any rebinding is to happen. I have chosen this one at first, as the clear separation of a POD type and more complicated resolved type easily mimics the config/runtime separation and allows for rebinding, refreshes etc. later