Lagrange
Loading...
Searching...
No Matches
Entity.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 <functional>
15#include <string>
16
17// clang-format off
18#include <lagrange/utils/warnoff.h>
19#include <entt/entt.hpp>
20#include <lagrange/utils/warnon.h>
21// clang-format on
22
23namespace lagrange {
24namespace ui {
25
26using Registry = entt::registry;
27using Entity = entt::entity;
28inline constexpr entt::null_t NullEntity{};
29
30using System = std::function<void(Registry& registry)>;
31using StringID = entt::id_type;
32
34{
35 std::string display_name;
36 std::string icon;
37 std::string keybind;
38 std::function<void(entt::registry*, Entity)> show_widget;
39
40 TypeData& set_display_name(const std::string& name)
41 {
42 display_name = name;
43 return *this;
44 }
45
46 TypeData& set_icon(const std::string& ic)
47 {
48 icon = ic;
49 return *this;
50 }
51
52 TypeData& set_keybind(const std::string& kb)
53 {
54 keybind = kb;
55 return *this;
56 }
57
58 TypeData& set_show_widget(const std::function<void(entt::registry*, Entity)>& fn)
59 {
60 show_widget = fn;
61 return *this;
62 }
63};
64
65/*
66 Globally used enums
67*/
68enum class IndexingMode { VERTEX, EDGE, FACE, CORNER, INDEXED };
69enum class PrimitiveType { POINTS, LINES, TRIANGLES };
70enum class SelectionBehavior { SET, ADD, ERASE };
71
72using namespace entt::literals;
73
74inline StringID string_id(const std::string& str)
75{
76 return entt::hashed_string(str.c_str());
77}
78inline StringID string_id(const char* str)
79{
80 return entt::hashed_string(str);
81}
82
83template <typename T>
84void component_clone(Registry* w, Entity src, Entity dst)
85{
86 w->emplace_or_replace<T>(dst, w->get<T>(src));
87}
88
89template <typename T>
90void component_move(Registry* w, Entity src, Entity dst)
91{
92 w->emplace_or_replace<T>(dst, std::move(w->get<T>(src)));
93 w->remove<T>(src);
94}
95
96template <typename T>
97void component_add_default(Registry* w, Entity dst)
98{
99 w->emplace_or_replace<T>(dst);
100}
101
104{
105 static const char* id() { return "PayloadEntity"; }
106 Entity entity;
107};
108
109
112{
113 static const char* id() { return "PayloadComponent"; }
114 entt::id_type component_hash;
115 Entity entity;
116};
117
118
119template <typename Component>
120void register_component(const std::string& display_name = entt::type_id<Component>().name().data())
121{
122 using namespace entt::literals;
123 entt::meta_factory<Component>()
124 .template func<&component_clone<Component>>("component_clone"_hs)
125 .template func<&component_move<Component>>("component_move"_hs)
126 .template func<&component_add_default<Component>>("component_add_default"_hs)
127 .template custom<TypeData>(TypeData().set_display_name(display_name));
128}
129
130template <typename Component, auto Func>
131void register_component_widget()
132{
133 using namespace entt::literals;
134 entt::meta_factory<Component>().template func<Func>("show_widget"_hs);
135}
136
137template <typename Component>
138void register_component_widget(const std::function<void(Registry*, Entity)>& fn)
139{
140 using namespace entt::literals;
141 entt::meta_factory<Component>().template custom<TypeData>(TypeData().set_show_widget(fn));
142}
143
144inline void show_widget(Registry& w, Entity e, const entt::meta_type& meta_type)
145{
146 using namespace entt::literals;
147 auto fn = meta_type.func("show_widget"_hs);
148 if (fn) {
149 fn.invoke({}, &w, e);
150 } else {
151 const TypeData* type_data = meta_type.custom();
152 if (type_data) {
153 type_data->show_widget(&w, e);
154 }
155 }
156}
157
158template <typename Resolvable>
159void show_widget(Registry& r, Entity e, const Resolvable& resolvable)
160{
161 show_widget(r, e, entt::resolve(resolvable));
162}
163
164
165} // namespace ui
166} // namespace lagrange
Lagrange UI Viewer and mini 3D engine.
Definition AcceleratedPicking.h:23
Main namespace for Lagrange.
Payload for sending components through UI.
Definition Entity.h:112
Payload for sending entities through UI.
Definition Entity.h:104
Definition Entity.h:34