math_fieldwise.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #ifdef TOOLS_ENABLED
  31. #include "math_fieldwise.h"
  32. #define SETUP_TYPE(m_type) \
  33. m_type source = p_source; \
  34. m_type target = p_target;
  35. #define TRY_TRANSFER_FIELD(m_name, m_member) \
  36. if (p_field == m_name) { \
  37. target.m_member = source.m_member; \
  38. }
  39. Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const String &p_field) {
  40. ERR_FAIL_COND_V(p_target.get_type() != p_source.get_type(), p_target);
  41. /* clang-format makes a mess of this macro usage */
  42. /* clang-format off */
  43. switch (p_source.get_type()) {
  44. case Variant::VECTOR2: {
  45. SETUP_TYPE(Vector2)
  46. /**/ TRY_TRANSFER_FIELD("x", x)
  47. else TRY_TRANSFER_FIELD("y", y)
  48. return target;
  49. }
  50. case Variant::VECTOR2I: {
  51. SETUP_TYPE(Vector2i)
  52. /**/ TRY_TRANSFER_FIELD("x", x)
  53. else TRY_TRANSFER_FIELD("y", y)
  54. return target;
  55. }
  56. case Variant::RECT2: {
  57. SETUP_TYPE(Rect2)
  58. /**/ TRY_TRANSFER_FIELD("x", position.x)
  59. else TRY_TRANSFER_FIELD("y", position.y)
  60. else TRY_TRANSFER_FIELD("w", size.x)
  61. else TRY_TRANSFER_FIELD("h", size.y)
  62. return target;
  63. }
  64. case Variant::RECT2I: {
  65. SETUP_TYPE(Rect2i)
  66. /**/ TRY_TRANSFER_FIELD("x", position.x)
  67. else TRY_TRANSFER_FIELD("y", position.y)
  68. else TRY_TRANSFER_FIELD("w", size.x)
  69. else TRY_TRANSFER_FIELD("h", size.y)
  70. return target;
  71. }
  72. case Variant::VECTOR3: {
  73. SETUP_TYPE(Vector3)
  74. /**/ TRY_TRANSFER_FIELD("x", x)
  75. else TRY_TRANSFER_FIELD("y", y)
  76. else TRY_TRANSFER_FIELD("z", z)
  77. return target;
  78. }
  79. case Variant::VECTOR3I: {
  80. SETUP_TYPE(Vector3i)
  81. /**/ TRY_TRANSFER_FIELD("x", x)
  82. else TRY_TRANSFER_FIELD("y", y)
  83. else TRY_TRANSFER_FIELD("z", z)
  84. return target;
  85. }
  86. case Variant::VECTOR4: {
  87. SETUP_TYPE(Vector4)
  88. /**/ TRY_TRANSFER_FIELD("x", x)
  89. else TRY_TRANSFER_FIELD("y", y)
  90. else TRY_TRANSFER_FIELD("z", z)
  91. else TRY_TRANSFER_FIELD("w", w)
  92. return target;
  93. }
  94. case Variant::VECTOR4I: {
  95. SETUP_TYPE(Vector4i)
  96. /**/ TRY_TRANSFER_FIELD("x", x)
  97. else TRY_TRANSFER_FIELD("y", y)
  98. else TRY_TRANSFER_FIELD("z", z)
  99. else TRY_TRANSFER_FIELD("w", w)
  100. return target;
  101. }
  102. case Variant::PLANE: {
  103. SETUP_TYPE(Plane)
  104. /**/ TRY_TRANSFER_FIELD("x", normal.x)
  105. else TRY_TRANSFER_FIELD("y", normal.y)
  106. else TRY_TRANSFER_FIELD("z", normal.z)
  107. else TRY_TRANSFER_FIELD("d", d)
  108. return target;
  109. }
  110. case Variant::QUATERNION: {
  111. SETUP_TYPE(Quaternion)
  112. /**/ TRY_TRANSFER_FIELD("x", x)
  113. else TRY_TRANSFER_FIELD("y", y)
  114. else TRY_TRANSFER_FIELD("z", z)
  115. else TRY_TRANSFER_FIELD("w", w)
  116. return target;
  117. }
  118. case Variant::AABB: {
  119. SETUP_TYPE(AABB)
  120. /**/ TRY_TRANSFER_FIELD("px", position.x)
  121. else TRY_TRANSFER_FIELD("py", position.y)
  122. else TRY_TRANSFER_FIELD("pz", position.z)
  123. else TRY_TRANSFER_FIELD("sx", size.x)
  124. else TRY_TRANSFER_FIELD("sy", size.y)
  125. else TRY_TRANSFER_FIELD("sz", size.z)
  126. return target;
  127. }
  128. case Variant::TRANSFORM2D: {
  129. SETUP_TYPE(Transform2D)
  130. /**/ TRY_TRANSFER_FIELD("xx", columns[0][0])
  131. else TRY_TRANSFER_FIELD("xy", columns[0][1])
  132. else TRY_TRANSFER_FIELD("yx", columns[1][0])
  133. else TRY_TRANSFER_FIELD("yy", columns[1][1])
  134. else TRY_TRANSFER_FIELD("ox", columns[2][0])
  135. else TRY_TRANSFER_FIELD("oy", columns[2][1])
  136. return target;
  137. }
  138. case Variant::BASIS: {
  139. SETUP_TYPE(Basis)
  140. /**/ TRY_TRANSFER_FIELD("xx", rows[0][0])
  141. else TRY_TRANSFER_FIELD("xy", rows[0][1])
  142. else TRY_TRANSFER_FIELD("xz", rows[0][2])
  143. else TRY_TRANSFER_FIELD("yx", rows[1][0])
  144. else TRY_TRANSFER_FIELD("yy", rows[1][1])
  145. else TRY_TRANSFER_FIELD("yz", rows[1][2])
  146. else TRY_TRANSFER_FIELD("zx", rows[2][0])
  147. else TRY_TRANSFER_FIELD("zy", rows[2][1])
  148. else TRY_TRANSFER_FIELD("zz", rows[2][2])
  149. return target;
  150. }
  151. case Variant::TRANSFORM3D: {
  152. SETUP_TYPE(Transform3D)
  153. /**/ TRY_TRANSFER_FIELD("xx", basis.rows[0][0])
  154. else TRY_TRANSFER_FIELD("xy", basis.rows[0][1])
  155. else TRY_TRANSFER_FIELD("xz", basis.rows[0][2])
  156. else TRY_TRANSFER_FIELD("yx", basis.rows[1][0])
  157. else TRY_TRANSFER_FIELD("yy", basis.rows[1][1])
  158. else TRY_TRANSFER_FIELD("yz", basis.rows[1][2])
  159. else TRY_TRANSFER_FIELD("zx", basis.rows[2][0])
  160. else TRY_TRANSFER_FIELD("zy", basis.rows[2][1])
  161. else TRY_TRANSFER_FIELD("zz", basis.rows[2][2])
  162. else TRY_TRANSFER_FIELD("xo", origin.x)
  163. else TRY_TRANSFER_FIELD("yo", origin.y)
  164. else TRY_TRANSFER_FIELD("zo", origin.z)
  165. return target;
  166. }
  167. case Variant::PROJECTION: {
  168. SETUP_TYPE(Projection)
  169. /**/ TRY_TRANSFER_FIELD("xx", columns[0].x)
  170. else TRY_TRANSFER_FIELD("xy", columns[0].y)
  171. else TRY_TRANSFER_FIELD("xz", columns[0].z)
  172. else TRY_TRANSFER_FIELD("xw", columns[0].w)
  173. else TRY_TRANSFER_FIELD("yx", columns[1].x)
  174. else TRY_TRANSFER_FIELD("yy", columns[1].y)
  175. else TRY_TRANSFER_FIELD("yz", columns[1].z)
  176. else TRY_TRANSFER_FIELD("yw", columns[1].w)
  177. else TRY_TRANSFER_FIELD("zx", columns[2].x)
  178. else TRY_TRANSFER_FIELD("zy", columns[2].y)
  179. else TRY_TRANSFER_FIELD("zz", columns[2].z)
  180. else TRY_TRANSFER_FIELD("zw", columns[2].w)
  181. else TRY_TRANSFER_FIELD("xo", columns[3].x)
  182. else TRY_TRANSFER_FIELD("yo", columns[3].y)
  183. else TRY_TRANSFER_FIELD("zo", columns[3].z)
  184. else TRY_TRANSFER_FIELD("wo", columns[3].w)
  185. return target;
  186. }
  187. default: {
  188. ERR_FAIL_V(p_target);
  189. }
  190. }
  191. /* clang-format on */
  192. }
  193. #endif // TOOLS_ENABLED