Lagrange
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 <entt/entt.hpp>
15#include <functional>
16#include <string>
17
18
19namespace lagrange {
20namespace ui {
21
22using Registry = entt::registry;
23using Entity = entt::entity;
24inline constexpr entt::null_t NullEntity{};
25
26using System = std::function<void(Registry& registry)>;
27using StringID = entt::id_type;
28
29/*
30 Globally used enums
31*/
32enum class IndexingMode { VERTEX, EDGE, FACE, CORNER, INDEXED };
33enum class PrimitiveType { POINTS, LINES, TRIANGLES };
34enum class SelectionBehavior { SET, ADD, ERASE };
35
36using namespace entt::literals;
37
38inline StringID string_id(const std::string& str)
39{
40 return entt::hashed_string(str.c_str());
41}
42inline StringID string_id(const char* str)
43{
44 return entt::hashed_string(str);
45}
46
47template <typename T>
48void component_clone(Registry* w, Entity src, Entity dst)
49{
50 w->emplace_or_replace<T>(dst, w->get<T>(src));
51}
52
53template <typename T>
54void component_move(Registry* w, Entity src, Entity dst)
55{
56 w->emplace_or_replace<T>(dst, std::move(w->get<T>(src)));
57 w->remove<T>(src);
58}
59
60template <typename T>
61void component_add_default(Registry* w, Entity dst)
62{
63 w->emplace_or_replace<T>(dst);
64}
65
68{
69 static const char* id() { return "PayloadEntity"; }
70 Entity entity;
71};
72
73
76{
77 static const char* id() { return "PayloadComponent"; }
78 entt::id_type component_hash;
79 Entity entity;
80};
81
82
83template <typename Component>
84void register_component(const std::string& display_name = entt::type_id<Component>().name().data())
85{
86 using namespace entt::literals;
87 entt::meta<Component>()
88 .template func<&component_clone<Component>>("component_clone"_hs)
89 .template func<&component_move<Component>>("component_move"_hs)
90 .template func<&component_add_default<Component>>("component_add_default"_hs)
91 .prop("display_name"_hs, display_name);
92}
93
94template <typename Component, auto Func>
95void register_component_widget()
96{
97 using namespace entt::literals;
98 entt::meta<Component>().template func<Func>("show_widget"_hs);
99}
100
101template <typename Component>
102void register_component_widget(const std::function<void(Registry*, Entity)>& fn)
103{
104 using namespace entt::literals;
105 entt::meta<Component>().prop("show_widget_lambda"_hs, fn);
106}
107
108inline void show_widget(Registry& w, Entity e, const entt::meta_type& meta_type)
109{
110 using namespace entt::literals;
111 auto fn = meta_type.func("show_widget"_hs);
112 if (fn) {
113 fn.invoke({}, &w, e);
114 } else {
115 auto lambda = meta_type.prop("show_widget_lambda"_hs);
116 if (lambda) {
117 lambda.value().cast<std::function<void(Registry*, Entity)>>()(&w, e);
118 }
119 }
120}
121
122template <typename Resolvable>
123void show_widget(Registry& r, Entity e, const Resolvable& resolvable)
124{
125 show_widget(r, e, entt::resolve(resolvable));
126}
127
128
129} // namespace ui
130} // namespace lagrange
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Payload for sending components through UI.
Definition: Entity.h:76
Payload for sending entities through UI.
Definition: Entity.h:68