fennec
Loading...
Searching...
No Matches
node2d.h
Go to the documentation of this file.
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
30
31#ifndef FENNEC_SCENE_COMPONENTS_TRANSFORM2D_H
32#define FENNEC_SCENE_COMPONENTS_TRANSFORM2D_H
33
34#include <fennec/math/matrix.h>
35#include <fennec/math/ext/transform.h>
36
37#include <fennec/core/event.h>
38
40
41
42namespace fennec
43{
44
45struct node2d;
46
47struct transform_update_2d : event {
48 node2d* node;
49
50 FENNEC_RTTI_CLASS_ENABLE(event) {
51
52 }
53};
54
55struct node2d : scene_node {
56// Definitions =========================================================================================================
57
58 enum mobility_ : bool {
59 mobility_static = false,
60 mobility_free = true
61 };
62
63
64// Constructors & Destructor ===========================================================================================
65public:
66
70 node2d(fennec::scene* scn, size_t id, const string& name)
71 : scene_node(scn, id, name)
72 , _mobility(mobility_free)
73 , _position(0, 0)
74 , _scale(1, 1)
75 , _shear(0, 0)
76 , _rotation(0) {
77 }
78
81 template<typename TypeT>
82 node2d(fennec::scene* scn, size_t id, const string& name, TypeT* type)
83 : scene_node(scn, id, name, type)
84 , _mobility(mobility_free)
85 , _position(0, 0)
86 , _scale(1, 1)
87 , _shear(0, 0)
88 , _rotation(0) {
89 }
90
91 node2d(const node2d&) = default;
92 node2d(node2d&&) noexcept = default;
93
94 ~node2d() = default;
95
96// Access ==============================================================================================================
97
98 constexpr const vec2& position() const {
99 return _position;
100 }
101
102 constexpr const vec2& scale() const {
103 return _scale;
104 }
105
106 constexpr float rotation() const {
107 return _rotation;
108 }
109
110 constexpr const vec2& shear() const {
111 return _shear;
112 }
113
114 constexpr bool mobility() const {
115 return _mobility;
116 }
117
118
119// Modifiers ===========================================================================================================
120
121 constexpr void translate(const vec2& x) {
122 if (_mobility) {
123 _position += x;
124 }
125 }
126
127 constexpr void scale(const vec2& s) {
128 if (_mobility) {
129 _scale *= s;
130 }
131 }
132
133 constexpr void rotate(float r) {
134 if (_mobility) {
135 _rotation += r;
136 }
137 }
138
139 constexpr void shear(const vec2& s) {
140 if (_mobility) {
141 _shear += s;
142 }
143 }
144
145 constexpr void commit() {
146 if (not _mobility) {
147 return;
148 }
149
150 // Get parent
151
152
153
154
155
156 // Propagate down
157
158 }
159
160 constexpr const mat3& local() {
161 return _local;
162 }
163
164 constexpr const mat3& global() {
165 return _global;
166 }
167
168// Fields ==============================================================================================================
169private:
170 bool _mobility;
171 vec2 _position;
172 vec2 _scale;
173 vec2 _shear;
174 float _rotation;
175 mat3 _local, _global;
176
177
178// Helpers =============================================================================================================
179
180 constexpr void _recalculate(const mat3& parent) {
181 _local = fennec::rotation(_rotation);
182 _local *= fennec::shear(_shear);
183 _local *= fennec::scaling(_scale);
184 _local *= fennec::translation(_position);
185 _global = parent * _local;
186 }
187};
188
189}
190
191
192#endif // FENNEC_SCENE_COMPONENTS_TRANSFORM2D_H
Main Scene Hierarchy.
Definition scene.h:36
the Matrices
tvec2< float_t > vec2
A two-component single-precision floating-point vector.
Definition vector.h:162