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(
20 [](const entt::meta_any& i)
21 {
22 return false;
23 });
24 // Register math::transform class
25 entt::meta_factory<math::transform>{}
26 .type("transform"_hs)
28 entt::attribute{"name", "transform"},
29 entt::attribute{"pretty_name", "Transform"},
30 })
31 .data<entt::overload<void(const math::vec3&)>(&math::transform::set_translation),
32 entt::overload<const math::vec3&() const>(&math::transform::get_translation)>("position"_hs)
34 entt::attribute{"name", "position"},
35 entt::attribute{"pretty_name", "Position"},
36 })
37 .data<entt::overload<void(const math::quat&)>(&math::transform::set_rotation),
38 entt::overload<const math::quat&() const>(&math::transform::get_rotation)>("rotation"_hs)
40 entt::attribute{"name", "rotation"},
41 entt::attribute{"pretty_name", "Rotation"},
42 })
43 .data<entt::overload<void(const math::vec3&)>(&math::transform::set_scale),
44 entt::overload<const math::vec3&() const>(&math::transform::get_scale)>("scale"_hs)
46 entt::attribute{"name", "scale"},
47 entt::attribute{"pretty_name", "Scale"},
48 })
49 .data<entt::overload<void(const math::vec3&)>(&math::transform::set_skew),
50 entt::overload<const math::vec3&() const>(&math::transform::get_skew)>("skew"_hs)
52 entt::attribute{"name", "skew"},
53 entt::attribute{"pretty_name", "Skew"},
54 });
55
56 // Register transform_component class
57 entt::meta_factory<transform_component>{}
58 .type("transform_component"_hs)
60 entt::attribute{"name", "transform_component"},
61 entt::attribute{"category", "RENDERING"},
62 entt::attribute{"pretty_name", "Transform"},
63 })
64 .func<&component_meta<transform_component>::exists>("component_exists"_hs)
65 .func<&component_meta<transform_component>::add>("component_add"_hs)
66 .func<&component_meta<transform_component>::remove>("component_remove"_hs)
67 .func<&component_meta<transform_component>::save>("component_save"_hs)
68 .func<&component_meta<transform_component>::load>("component_load"_hs)
70 .custom<entt::attributes>(entt::attributes{
71 entt::attribute{"name", "local_transform"},
72 entt::attribute{"pretty_name", "Local"},
73 entt::attribute{"tooltip", "This is the local transformation.\n"
74 "It is relative to the parent."},
75 })
76 .data<&transform_component::set_transform_global, &transform_component::get_transform_global>("global_transform"_hs)
78 entt::attribute{"name", "global_transform"},
79 entt::attribute{"pretty_name", "Global"},
80 entt::attribute{"tooltip", "This is the global transformation.\n"
81 "Affected by parent transformation."},
82 })
83 .data<&transform_component::set_active, &transform_component::is_active>("active"_hs)
85 entt::attribute{"name", "active"},
86 entt::attribute{"pretty_name", "Active"},
87 entt::attribute{"tooltip", "This is the active state."},
88 entt::attribute{"predicate", invisible_predicate_entt},
89 });
90
91 // auto type = entt::resolve<transform_component>();
92 // auto datas = type.data();
93 // for(auto data : datas)
94 // {
95 // APPLOG_TRACE("Data: {}", data.second.arity());
96 // }
97}
98
100{
101 bool is_root = obj.get_owner().all_of<root_component>();
102
103 try_save(ar, ser20::make_nvp("local_transform", obj.get_transform_local()));
104 try_save(ar, ser20::make_nvp("parent", is_root ? entt::handle{} : obj.get_parent()));
105 try_save(ar, ser20::make_nvp("children", obj.get_children()));
106 try_save(ar, ser20::make_nvp("active", obj.is_active()));
107}
110
112{
113 math::transform local_transform = obj.get_transform_local();
114 if(try_load(ar, ser20::make_nvp("local_transform", local_transform)))
115 {
116 obj.set_transform_local(local_transform);
117 }
118
119 entt::handle parent;
120 try_load(ar, ser20::make_nvp("parent", parent));
121
122 obj.set_parent(parent, false);
123
124 // not really used but needed to preserve binary archive integrity
125 std::vector<entt::handle> children;
126 try_load(ar, ser20::make_nvp("children", children));
127
128 bool active{true};
129 if(try_load(ar, ser20::make_nvp("active", active)))
130 {
131 obj.set_active(active);
132 }
133}
136
137} // 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.
attributes::value_type attribute
Definition reflection.h:19
std::map< std::string, meta_any > attributes
Definition reflection.h:18
auto property_predicate(property_predicate_t predicate) -> property_predicate_t
BinaryInputArchive iarchive_binary_t
simd::JSONOutputArchive oarchive_associative_t
BinaryOutputArchive oarchive_binary_t
simd::JSONInputArchive iarchive_associative_t
#define REFLECT(cls)
Definition reflection.h:133
#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.