Unravel Engine C++ Reference
Loading...
Searching...
No Matches
transform_component.cpp
Go to the documentation of this file.
2
5#include "../entity.hpp"
6#include "entt/meta/meta.hpp"
7#include "entt/meta/resolve.hpp"
8#include "glm/fwd.hpp"
9
13
14namespace unravel
15{
16
18{
19 auto invisible_predicate_entt = entt::property_predicate<bool>(
20 [](const entt::meta_any& i)
21 {
22 return false;
23 });
24
25 // The global transform only differs from the local one when the entity is parented;
26 // for a root entity it is identical, so hide it to avoid a redundant editable block.
27 auto has_parent_predicate_entt = entt::property_predicate<bool>(
28 [](const entt::meta_any& i) -> bool
29 {
30 const auto* comp = i.try_cast<transform_component>();
31 return static_cast<bool>(comp && comp->get_parent());
32 });
33 // Register math::transform class
34 entt::meta_factory<math::transform>{}
35 .type("transform"_hs)
37 entt::attribute{"name", "transform"},
38 entt::attribute{"pretty_name", "Transform"},
39 })
40 .data<entt::overload<void(const math::vec3&)>(&math::transform::set_translation),
41 entt::overload<const math::vec3&() const>(&math::transform::get_translation)>("position"_hs)
43 entt::attribute{"name", "position"},
44 entt::attribute{"pretty_name", "Position"},
45 })
46 .data<entt::overload<void(const math::quat&)>(&math::transform::set_rotation),
47 entt::overload<const math::quat&() const>(&math::transform::get_rotation)>("rotation"_hs)
49 entt::attribute{"name", "rotation"},
50 entt::attribute{"pretty_name", "Rotation"},
51 })
52 .data<entt::overload<void(const math::vec3&)>(&math::transform::set_scale),
53 entt::overload<const math::vec3&() const>(&math::transform::get_scale)>("scale"_hs)
55 entt::attribute{"name", "scale"},
56 entt::attribute{"pretty_name", "Scale"},
57 })
58 .data<entt::overload<void(const math::vec3&)>(&math::transform::set_skew),
59 entt::overload<const math::vec3&() const>(&math::transform::get_skew)>("skew"_hs)
61 entt::attribute{"name", "skew"},
62 entt::attribute{"pretty_name", "Skew"},
63 });
64
65 // Register transform_component class
66 entt::meta_factory<transform_component>{}
67 .type("transform_component"_hs)
69 entt::attribute{"name", "transform_component"},
70 entt::attribute{"category", "RENDERING"},
71 entt::attribute{"pretty_name", "Transform"},
72 })
73 .func<&component_meta<transform_component>::exists>("component_exists"_hs)
74 .func<&component_meta<transform_component>::add>("component_add"_hs)
75 .func<&component_meta<transform_component>::remove>("component_remove"_hs)
76 .func<&component_meta<transform_component>::save>("component_save"_hs)
77 .func<&component_meta<transform_component>::load>("component_load"_hs)
79 .custom<entt::attributes>(entt::attributes{
80 entt::attribute{"name", "local_transform"},
81 entt::attribute{"pretty_name", "Local"},
82 entt::attribute{"tooltip", "This is the local transformation.\n"
83 "It is relative to the parent."},
84 })
85 .data<&transform_component::set_transform_global, &transform_component::get_transform_global>("global_transform"_hs)
87 entt::attribute{"name", "global_transform"},
88 entt::attribute{"pretty_name", "Global"},
89 entt::attribute{"tooltip", "This is the global transformation.\n"
90 "Affected by parent transformation."},
91 // Collapsed by default: it is a read-along view, secondary to the local transform.
92 entt::attribute{"collapsed", true},
93 // Only relevant for parented entities (root: global == local).
94 entt::attribute{"predicate", has_parent_predicate_entt},
95 })
96 .data<&transform_component::set_active, &transform_component::is_active>("active"_hs)
98 entt::attribute{"name", "active"},
99 entt::attribute{"pretty_name", "Active"},
100 entt::attribute{"tooltip", "This is the active state."},
101 entt::attribute{"predicate", invisible_predicate_entt},
102 });
103
104 // auto type = entt::resolve<transform_component>();
105 // auto datas = type.data();
106 // for(auto data : datas)
107 // {
108 // APPLOG_TRACE("Data: {}", data.second.arity());
109 // }
110}
111
113{
114 bool is_root = obj.get_owner().all_of<root_component>();
115
116 try_save(ar, ser20::make_nvp("local_transform", obj.get_transform_local()));
117 try_save(ar, ser20::make_nvp("parent", is_root ? entt::handle{} : obj.get_parent()));
118 try_save(ar, ser20::make_nvp("children", obj.get_children()));
119 try_save(ar, ser20::make_nvp("active", obj.is_active()));
120}
123
125{
126 math::transform local_transform = obj.get_transform_local();
127 if(try_load(ar, ser20::make_nvp("local_transform", local_transform)))
128 {
129 obj.set_transform_local(local_transform);
130 }
131
132 entt::handle parent;
133 try_load(ar, ser20::make_nvp("parent", parent));
134
135 obj.set_parent(parent, false);
136
137 // not really used but needed to preserve binary archive integrity
138 std::vector<entt::handle> children;
139 try_load(ar, ser20::make_nvp("children", children));
140
141 bool active{true};
142 if(try_load(ar, ser20::make_nvp("active", active)))
143 {
144 obj.set_active(active);
145 }
146}
149
150} // namespace unravel
General purpose transformation class designed to maintain each component of the transformation separa...
Definition transform.hpp:27
void set_scale(const vec3_t &scale) noexcept
Set the scale component.
void set_skew(const vec3_t &skew) noexcept
Set the skew component.
auto get_scale() const noexcept -> const vec3_t &
Get the scale component.
auto get_translation() const noexcept -> const vec3_t &
Get the translation component.
void set_translation(const vec3_t &position) noexcept
Set the translation component.
auto get_rotation() const noexcept -> const quat_t &
Get the rotation component.
auto get_skew() const noexcept -> const vec3_t &
Get the skew component.
void set_rotation(const quat_t &rotation) noexcept
Set the rotation component.
Component that handles transformations (position, rotation, scale, etc.) in the ACE framework.
void set_transform_local(const math::transform &trans) noexcept
Sets the local transform.
auto get_transform_local() const noexcept -> const math::transform &
Gets the local transform.
std::vector< render_pass_node > children
auto property_predicate(property_predicate_t< T > predicate) -> property_predicate_t< T >
Definition reflection.h:89
attributes::value_type attribute
Definition reflection.h:19
std::map< std::string, meta_any > attributes
Definition reflection.h:18
BinaryInputArchive iarchive_binary_t
simd::JSONOutputArchive oarchive_associative_t
BinaryOutputArchive oarchive_binary_t
simd::JSONInputArchive iarchive_associative_t
#define REFLECT(cls)
Definition reflection.h:149
#define SAVE_INSTANTIATE(cls, Archive)
#define LOAD(cls)
auto try_save(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool
#define LOAD_INSTANTIATE(cls, Archive)
#define SAVE(cls)
auto try_load(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool
Root component structure for the ACE framework, serves as the base component.