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