1 | /**************************************************************************/ |
2 | /* math_fieldwise.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifdef TOOLS_ENABLED |
32 | |
33 | #include "math_fieldwise.h" |
34 | |
35 | #define SETUP_TYPE(m_type) \ |
36 | m_type source = p_source; \ |
37 | m_type target = p_target; |
38 | #define TRY_TRANSFER_FIELD(m_name, m_member) \ |
39 | if (p_field == m_name) { \ |
40 | target.m_member = source.m_member; \ |
41 | } |
42 | |
43 | Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const String &p_field) { |
44 | ERR_FAIL_COND_V(p_target.get_type() != p_source.get_type(), p_target); |
45 | |
46 | /* clang-format makes a mess of this macro usage */ |
47 | /* clang-format off */ |
48 | |
49 | switch (p_source.get_type()) { |
50 | case Variant::VECTOR2: { |
51 | SETUP_TYPE(Vector2) |
52 | |
53 | /**/ TRY_TRANSFER_FIELD("x" , x) |
54 | else TRY_TRANSFER_FIELD("y" , y) |
55 | |
56 | return target; |
57 | } |
58 | |
59 | case Variant::VECTOR2I: { |
60 | SETUP_TYPE(Vector2i) |
61 | |
62 | /**/ TRY_TRANSFER_FIELD("x" , x) |
63 | else TRY_TRANSFER_FIELD("y" , y) |
64 | |
65 | return target; |
66 | } |
67 | |
68 | case Variant::RECT2: { |
69 | SETUP_TYPE(Rect2) |
70 | |
71 | /**/ TRY_TRANSFER_FIELD("x" , position.x) |
72 | else TRY_TRANSFER_FIELD("y" , position.y) |
73 | else TRY_TRANSFER_FIELD("w" , size.x) |
74 | else TRY_TRANSFER_FIELD("h" , size.y) |
75 | |
76 | return target; |
77 | } |
78 | |
79 | case Variant::RECT2I: { |
80 | SETUP_TYPE(Rect2i) |
81 | |
82 | /**/ TRY_TRANSFER_FIELD("x" , position.x) |
83 | else TRY_TRANSFER_FIELD("y" , position.y) |
84 | else TRY_TRANSFER_FIELD("w" , size.x) |
85 | else TRY_TRANSFER_FIELD("h" , size.y) |
86 | |
87 | return target; |
88 | } |
89 | |
90 | case Variant::VECTOR3: { |
91 | SETUP_TYPE(Vector3) |
92 | |
93 | /**/ TRY_TRANSFER_FIELD("x" , x) |
94 | else TRY_TRANSFER_FIELD("y" , y) |
95 | else TRY_TRANSFER_FIELD("z" , z) |
96 | |
97 | return target; |
98 | } |
99 | |
100 | case Variant::VECTOR3I: { |
101 | SETUP_TYPE(Vector3i) |
102 | |
103 | /**/ TRY_TRANSFER_FIELD("x" , x) |
104 | else TRY_TRANSFER_FIELD("y" , y) |
105 | else TRY_TRANSFER_FIELD("z" , z) |
106 | |
107 | return target; |
108 | } |
109 | |
110 | case Variant::VECTOR4: { |
111 | SETUP_TYPE(Vector4) |
112 | |
113 | /**/ TRY_TRANSFER_FIELD("x" , x) |
114 | else TRY_TRANSFER_FIELD("y" , y) |
115 | else TRY_TRANSFER_FIELD("z" , z) |
116 | else TRY_TRANSFER_FIELD("w" , w) |
117 | |
118 | return target; |
119 | } |
120 | |
121 | case Variant::VECTOR4I: { |
122 | SETUP_TYPE(Vector4i) |
123 | |
124 | /**/ TRY_TRANSFER_FIELD("x" , x) |
125 | else TRY_TRANSFER_FIELD("y" , y) |
126 | else TRY_TRANSFER_FIELD("z" , z) |
127 | else TRY_TRANSFER_FIELD("w" , w) |
128 | |
129 | return target; |
130 | } |
131 | |
132 | case Variant::PLANE: { |
133 | SETUP_TYPE(Plane) |
134 | |
135 | /**/ TRY_TRANSFER_FIELD("x" , normal.x) |
136 | else TRY_TRANSFER_FIELD("y" , normal.y) |
137 | else TRY_TRANSFER_FIELD("z" , normal.z) |
138 | else TRY_TRANSFER_FIELD("d" , d) |
139 | |
140 | return target; |
141 | } |
142 | |
143 | case Variant::QUATERNION: { |
144 | SETUP_TYPE(Quaternion) |
145 | |
146 | /**/ TRY_TRANSFER_FIELD("x" , x) |
147 | else TRY_TRANSFER_FIELD("y" , y) |
148 | else TRY_TRANSFER_FIELD("z" , z) |
149 | else TRY_TRANSFER_FIELD("w" , w) |
150 | |
151 | return target; |
152 | } |
153 | |
154 | case Variant::AABB: { |
155 | SETUP_TYPE(AABB) |
156 | |
157 | /**/ TRY_TRANSFER_FIELD("px" , position.x) |
158 | else TRY_TRANSFER_FIELD("py" , position.y) |
159 | else TRY_TRANSFER_FIELD("pz" , position.z) |
160 | else TRY_TRANSFER_FIELD("sx" , size.x) |
161 | else TRY_TRANSFER_FIELD("sy" , size.y) |
162 | else TRY_TRANSFER_FIELD("sz" , size.z) |
163 | |
164 | return target; |
165 | } |
166 | |
167 | case Variant::TRANSFORM2D: { |
168 | SETUP_TYPE(Transform2D) |
169 | |
170 | /**/ TRY_TRANSFER_FIELD("xx" , columns[0][0]) |
171 | else TRY_TRANSFER_FIELD("xy" , columns[0][1]) |
172 | else TRY_TRANSFER_FIELD("yx" , columns[1][0]) |
173 | else TRY_TRANSFER_FIELD("yy" , columns[1][1]) |
174 | else TRY_TRANSFER_FIELD("ox" , columns[2][0]) |
175 | else TRY_TRANSFER_FIELD("oy" , columns[2][1]) |
176 | |
177 | return target; |
178 | } |
179 | |
180 | case Variant::BASIS: { |
181 | SETUP_TYPE(Basis) |
182 | |
183 | /**/ TRY_TRANSFER_FIELD("xx" , rows[0][0]) |
184 | else TRY_TRANSFER_FIELD("xy" , rows[0][1]) |
185 | else TRY_TRANSFER_FIELD("xz" , rows[0][2]) |
186 | else TRY_TRANSFER_FIELD("yx" , rows[1][0]) |
187 | else TRY_TRANSFER_FIELD("yy" , rows[1][1]) |
188 | else TRY_TRANSFER_FIELD("yz" , rows[1][2]) |
189 | else TRY_TRANSFER_FIELD("zx" , rows[2][0]) |
190 | else TRY_TRANSFER_FIELD("zy" , rows[2][1]) |
191 | else TRY_TRANSFER_FIELD("zz" , rows[2][2]) |
192 | |
193 | return target; |
194 | } |
195 | |
196 | case Variant::TRANSFORM3D: { |
197 | SETUP_TYPE(Transform3D) |
198 | |
199 | /**/ TRY_TRANSFER_FIELD("xx" , basis.rows[0][0]) |
200 | else TRY_TRANSFER_FIELD("xy" , basis.rows[0][1]) |
201 | else TRY_TRANSFER_FIELD("xz" , basis.rows[0][2]) |
202 | else TRY_TRANSFER_FIELD("yx" , basis.rows[1][0]) |
203 | else TRY_TRANSFER_FIELD("yy" , basis.rows[1][1]) |
204 | else TRY_TRANSFER_FIELD("yz" , basis.rows[1][2]) |
205 | else TRY_TRANSFER_FIELD("zx" , basis.rows[2][0]) |
206 | else TRY_TRANSFER_FIELD("zy" , basis.rows[2][1]) |
207 | else TRY_TRANSFER_FIELD("zz" , basis.rows[2][2]) |
208 | else TRY_TRANSFER_FIELD("xo" , origin.x) |
209 | else TRY_TRANSFER_FIELD("yo" , origin.y) |
210 | else TRY_TRANSFER_FIELD("zo" , origin.z) |
211 | |
212 | return target; |
213 | } |
214 | |
215 | case Variant::PROJECTION: { |
216 | SETUP_TYPE(Projection) |
217 | |
218 | /**/ TRY_TRANSFER_FIELD("xx" , columns[0].x) |
219 | else TRY_TRANSFER_FIELD("xy" , columns[0].y) |
220 | else TRY_TRANSFER_FIELD("xz" , columns[0].z) |
221 | else TRY_TRANSFER_FIELD("xw" , columns[0].w) |
222 | else TRY_TRANSFER_FIELD("yx" , columns[1].x) |
223 | else TRY_TRANSFER_FIELD("yy" , columns[1].y) |
224 | else TRY_TRANSFER_FIELD("yz" , columns[1].z) |
225 | else TRY_TRANSFER_FIELD("yw" , columns[1].w) |
226 | else TRY_TRANSFER_FIELD("zx" , columns[2].x) |
227 | else TRY_TRANSFER_FIELD("zy" , columns[2].y) |
228 | else TRY_TRANSFER_FIELD("zz" , columns[2].z) |
229 | else TRY_TRANSFER_FIELD("zw" , columns[2].w) |
230 | else TRY_TRANSFER_FIELD("xo" , columns[3].x) |
231 | else TRY_TRANSFER_FIELD("yo" , columns[3].y) |
232 | else TRY_TRANSFER_FIELD("zo" , columns[3].z) |
233 | else TRY_TRANSFER_FIELD("wo" , columns[3].w) |
234 | |
235 | return target; |
236 | } |
237 | |
238 | default: { |
239 | ERR_FAIL_V(p_target); |
240 | } |
241 | } |
242 | /* clang-format on */ |
243 | } |
244 | |
245 | #endif // TOOLS_ENABLED |
246 | |