Lagrange
Loading...
Searching...
No Matches
events.h
1/*
2 * Copyright 2021 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/ui/components/EventEmitter.h>
15
16// Wrapper for entt::emitter class functionality
17// The emitter is stored in registry's context variable
18// These function's are a short-hand for adding/triggering events
19
20namespace lagrange {
21namespace ui {
22
23inline EventEmitter& get_event_emitter(Registry& r)
24{
25 return r.ctx().emplace<EventEmitter>(EventEmitter());
26}
27
32template <typename Event>
33void on(Registry& r, std::function<void(Event&)> listener)
34{
35 get_event_emitter(r).on<Event>(
36 [=](Event& event, EventEmitter& /*emitter*/) { listener(event); });
37}
38
39
44template <typename Event, typename... Args>
45void publish(Registry& r, Args&&... args)
46{
47 get_event_emitter(r).publish<Event>(Event{std::forward<Args>(args)...});
48}
49
51template <typename Event>
52void forward_entity_event(Registry& r, Entity e)
53{
55}
56
59template <typename Component, typename ConstructEvent, typename DestroyEvent>
60void toggle_component_event(Registry& r)
61{
62 if (get_event_emitter(r).contains<ConstructEvent>()) {
63 r.on_construct<Component>().template connect<&forward_entity_event<ConstructEvent>>();
64 } else {
65 r.on_construct<Component>().template disconnect<&forward_entity_event<ConstructEvent>>();
66 }
67
68 if (get_event_emitter(r).contains<DestroyEvent>()) {
69 r.on_destroy<Component>().template connect<&forward_entity_event<DestroyEvent>>();
70 } else {
71 r.on_destroy<Component>().template disconnect<&forward_entity_event<DestroyEvent>>();
72 }
73}
74
75} // namespace ui
76} // namespace lagrange
Lagrange UI Viewer and mini 3D engine.
Definition AcceleratedPicking.h:23
void on(Registry &r, std::function< void(Event &)> listener)
Register a listener for Event.
Definition events.h:33
void publish(Registry &r, Args &&... args)
Trigger an event of type Event.
Definition events.h:45
void forward_entity_event(Registry &r, Entity e)
Utility function for forwarding events that contain only an entity identifier.
Definition events.h:52
void toggle_component_event(Registry &r)
Enable/Disable on_construct and on_destroy events depending on whether ConstructEvent and DestroyEven...
Definition events.h:60
Main namespace for Lagrange.
Definition project.cpp:27
Definition EventEmitter.h:21