origin.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant 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 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #if !defined(INCLUDED_ORIGIN_H)
  18. #define INCLUDED_ORIGIN_H
  19. #include "ientity.h"
  20. #include "math/matrix.h"
  21. #include "generic/callback.h"
  22. #include "stringio.h"
  23. const Vector3 ORIGINKEY_IDENTITY = Vector3(0, 0, 0);
  24. inline void default_origin(Vector3& origin)
  25. {
  26. origin = ORIGINKEY_IDENTITY;
  27. }
  28. inline void read_origin(Vector3& origin, const char* value)
  29. {
  30. if(!string_parse_vector3(value, origin))
  31. {
  32. default_origin(origin);
  33. }
  34. }
  35. inline void write_origin(const Vector3& origin, Entity* entity, const char* key)
  36. {
  37. char value[64];
  38. sprintf(value, "%g %g %g", origin[0], origin[1], origin[2]);
  39. entity->setKeyValue(key, value);
  40. }
  41. inline Vector3 origin_translated(const Vector3& origin, const Vector3& translation)
  42. {
  43. return matrix4_get_translation_vec3(
  44. matrix4_multiplied_by_matrix4(
  45. matrix4_translation_for_vec3(origin),
  46. matrix4_translation_for_vec3(translation)
  47. )
  48. );
  49. }
  50. inline Vector3 origin_snapped(const Vector3& origin, float snap)
  51. {
  52. return vector3_snapped(origin, snap);
  53. }
  54. class OriginKey
  55. {
  56. Callback m_originChanged;
  57. public:
  58. Vector3 m_origin;
  59. OriginKey(const Callback& originChanged)
  60. : m_originChanged(originChanged), m_origin(ORIGINKEY_IDENTITY)
  61. {
  62. }
  63. void originChanged(const char* value)
  64. {
  65. read_origin(m_origin, value);
  66. m_originChanged();
  67. }
  68. typedef MemberCaller1<OriginKey, const char*, &OriginKey::originChanged> OriginChangedCaller;
  69. void write(Entity* entity) const
  70. {
  71. write_origin(m_origin, entity, "origin");
  72. }
  73. };
  74. #include "scenelib.h"
  75. inline BrushDoom3* Node_getBrushDoom3(scene::Node& node)
  76. {
  77. return NodeTypeCast<BrushDoom3>::cast(node);
  78. }
  79. inline void BrushDoom3_setDoom3GroupOrigin(scene::Node& node, const Vector3& origin)
  80. {
  81. BrushDoom3* brush = Node_getBrushDoom3(node);
  82. if(brush != 0)
  83. {
  84. brush->setDoom3GroupOrigin(origin);
  85. }
  86. }
  87. class SetDoom3GroupOriginWalker : public scene::Traversable::Walker
  88. {
  89. const Vector3& m_origin;
  90. public:
  91. SetDoom3GroupOriginWalker(const Vector3& origin) : m_origin(origin)
  92. {
  93. }
  94. bool pre(scene::Node& node) const
  95. {
  96. BrushDoom3_setDoom3GroupOrigin(node, m_origin);
  97. return true;
  98. }
  99. };
  100. class Doom3GroupOrigin : public scene::Traversable::Observer
  101. {
  102. scene::Traversable& m_set;
  103. const Vector3& m_origin;
  104. bool m_enabled;
  105. public:
  106. Doom3GroupOrigin(scene::Traversable& set, const Vector3& origin) : m_set(set), m_origin(origin), m_enabled(false)
  107. {
  108. }
  109. void enable()
  110. {
  111. m_enabled = true;
  112. originChanged();
  113. }
  114. void disable()
  115. {
  116. m_enabled = false;
  117. }
  118. void originChanged()
  119. {
  120. if(m_enabled)
  121. {
  122. m_set.traverse(SetDoom3GroupOriginWalker(m_origin));
  123. }
  124. }
  125. void insert(scene::Node& node)
  126. {
  127. if(m_enabled)
  128. {
  129. BrushDoom3_setDoom3GroupOrigin(node, m_origin);
  130. }
  131. }
  132. void erase(scene::Node& node)
  133. {
  134. if(m_enabled)
  135. {
  136. BrushDoom3_setDoom3GroupOrigin(node, Vector3(0, 0, 0));
  137. }
  138. }
  139. };
  140. #endif