math_fieldwise.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*************************************************************************/
  2. /* math_fieldwise.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/helper/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. switch (p_source.get_type()) {
  42. case Variant::VECTOR2: {
  43. SETUP_TYPE(Vector2)
  44. /**/ TRY_TRANSFER_FIELD("x", x) else TRY_TRANSFER_FIELD("y", y)
  45. return target;
  46. }
  47. case Variant::RECT2: {
  48. SETUP_TYPE(Rect2)
  49. /**/ TRY_TRANSFER_FIELD("x", pos.x) else TRY_TRANSFER_FIELD("y", pos.y) else TRY_TRANSFER_FIELD("w", size.x) else TRY_TRANSFER_FIELD("h", size.y)
  50. return target;
  51. }
  52. case Variant::VECTOR3: {
  53. SETUP_TYPE(Vector3)
  54. /**/ TRY_TRANSFER_FIELD("x", x) else TRY_TRANSFER_FIELD("y", y) else TRY_TRANSFER_FIELD("z", z)
  55. return target;
  56. }
  57. case Variant::PLANE: {
  58. SETUP_TYPE(Plane)
  59. /**/ TRY_TRANSFER_FIELD("x", normal.x) else TRY_TRANSFER_FIELD("y", normal.y) else TRY_TRANSFER_FIELD("z", normal.z) else TRY_TRANSFER_FIELD("d", d)
  60. return target;
  61. }
  62. case Variant::QUAT: {
  63. SETUP_TYPE(Quat)
  64. /**/ TRY_TRANSFER_FIELD("x", x) else TRY_TRANSFER_FIELD("y", y) else TRY_TRANSFER_FIELD("z", z) else TRY_TRANSFER_FIELD("w", w)
  65. return target;
  66. }
  67. case Variant::_AABB: {
  68. SETUP_TYPE(AABB)
  69. /**/ TRY_TRANSFER_FIELD("px", pos.x) else TRY_TRANSFER_FIELD("py", pos.y) else TRY_TRANSFER_FIELD("pz", pos.z) else TRY_TRANSFER_FIELD("sx", size.x) else TRY_TRANSFER_FIELD("sy", size.y) else TRY_TRANSFER_FIELD("sz", size.z)
  70. return target;
  71. }
  72. case Variant::MATRIX32: {
  73. SETUP_TYPE(Matrix32)
  74. /**/ TRY_TRANSFER_FIELD("xx", elements[0][0]) else TRY_TRANSFER_FIELD("xy", elements[0][1]) else TRY_TRANSFER_FIELD("yx", elements[1][0]) else TRY_TRANSFER_FIELD("yy", elements[1][1]) else TRY_TRANSFER_FIELD("ox", elements[2][0]) else TRY_TRANSFER_FIELD("oy", elements[2][1])
  75. return target;
  76. }
  77. case Variant::MATRIX3: {
  78. SETUP_TYPE(Matrix3)
  79. /**/ TRY_TRANSFER_FIELD("xx", elements[0][0]) else TRY_TRANSFER_FIELD("xy", elements[0][1]) else TRY_TRANSFER_FIELD("xz", elements[0][2]) else TRY_TRANSFER_FIELD("yx", elements[1][0]) else TRY_TRANSFER_FIELD("yy", elements[1][1]) else TRY_TRANSFER_FIELD("yz", elements[1][2]) else TRY_TRANSFER_FIELD("zx", elements[2][0]) else TRY_TRANSFER_FIELD("zy", elements[2][1]) else TRY_TRANSFER_FIELD("zz", elements[2][2])
  80. return target;
  81. }
  82. case Variant::TRANSFORM: {
  83. SETUP_TYPE(Transform)
  84. /**/ TRY_TRANSFER_FIELD("xx", basis.elements[0][0]) else TRY_TRANSFER_FIELD("xy", basis.elements[0][1]) else TRY_TRANSFER_FIELD("xz", basis.elements[0][2]) else TRY_TRANSFER_FIELD("yx", basis.elements[1][0]) else TRY_TRANSFER_FIELD("yy", basis.elements[1][1]) else TRY_TRANSFER_FIELD("yz", basis.elements[1][2]) else TRY_TRANSFER_FIELD("zx", basis.elements[2][0]) else TRY_TRANSFER_FIELD("zy", basis.elements[2][1]) else TRY_TRANSFER_FIELD("zz", basis.elements[2][2]) else TRY_TRANSFER_FIELD("xo", origin.x) else TRY_TRANSFER_FIELD("yo", origin.y) else TRY_TRANSFER_FIELD("zo", origin.z)
  85. return target;
  86. }
  87. default: {
  88. ERR_FAIL_V(p_target);
  89. }
  90. }
  91. }
  92. #endif // TOOLS_ENABLED