Lagrange
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
33template <typename Event>
34void on(Registry& r, std::function<void(Event&)> listener)
35{
36 get_event_emitter(r).on<Event>(
37 [=](Event& event, EventEmitter& /*emitter*/) { listener(event); });
38}
39
40
45template <typename Event, typename... Args>
46void publish(Registry& r, Args&&... args)
47{
48 get_event_emitter(r).publish<Event>(Event{std::forward<Args>(args)...});
49}
50
52template <typename Event>
53void forward_entity_event(Registry& r, Entity e)
54{
55 ui::publish<Event>(r, e);
56}
57
60template <typename Component, typename ConstructEvent, typename DestroyEvent>
61void toggle_component_event(Registry& r)
62{
63 if (get_event_emitter(r).contains<ConstructEvent>()) {
64 r.on_construct<Component>().template connect<&forward_entity_event<ConstructEvent>>();
65 } else {
66 r.on_construct<Component>().template disconnect<&forward_entity_event<ConstructEvent>>();
67 }
68
69 if (get_event_emitter(r).contains<DestroyEvent>()) {
70 r.on_destroy<Component>().template connect<&forward_entity_event<DestroyEvent>>();
71 } else {
72 r.on_destroy<Component>().template disconnect<&forward_entity_event<DestroyEvent>>();
73 }
74}
75
76} // namespace ui
77} // namespace lagrange
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
void on(Registry &r, std::function< void(Event &)> listener)
Register a listener for Event.
Definition: events.h:34
void publish(Registry &r, Args &&... args)
Trigger an event of type Event.
Definition: events.h:46
void forward_entity_event(Registry &r, Entity e)
Utility function for forwarding events that contain only an entity identifier.
Definition: events.h:53
void toggle_component_event(Registry &r)
Enable/Disable on_construct and on_destroy events depending on whether ConstructEvent and DestroyEven...
Definition: events.h:61
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: project.cpp:27
Definition: EventEmitter.h:21