SelectionBox.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. ///////////////////////////////////////////////////////////////////////////////////////
  19. //
  20. // SelectionBoxClass.cpp
  21. //
  22. // Class declaration for a selected item's bounding box.
  23. //
  24. #include "stdafx.h"
  25. #include "selectionbox.h"
  26. #include "matrix3d.h"
  27. #include "sceneeditor.h"
  28. #include "rendobj.h"
  29. #include "node.h"
  30. //////////////////////////////////////////////////////////////////////
  31. //
  32. // SelectionBoxClass::DecorationLineClass
  33. //
  34. //////////////////////////////////////////////////////////////////////
  35. SelectionBoxClass::DecorationLineClass::DecorationLineClass (void)
  36. : m_pLine3D (NULL)
  37. {
  38. m_pLine3D = new Line3DClass (Vector3 (0,0,0), Vector3 (10,10,10), 0.3F, 0.8F, 0.8F, 0.8F);
  39. // Don't do collision detections on us
  40. Inc_Ignore_Counter();
  41. // Let the base class know who the model is
  42. Set_Model (m_pLine3D);
  43. return ;
  44. }
  45. //////////////////////////////////////////////////////////////////////
  46. //
  47. // SelectionBoxClass::DecorationLineClass::~DecorationLineClass
  48. //
  49. //////////////////////////////////////////////////////////////////////
  50. SelectionBoxClass::DecorationLineClass::~DecorationLineClass (void)
  51. {
  52. MEMBER_RELEASE (m_pLine3D);
  53. return ;
  54. }
  55. //////////////////////////////////////////////////////////////////////
  56. //
  57. // SelectionBoxClass
  58. //
  59. //////////////////////////////////////////////////////////////////////
  60. SelectionBoxClass::SelectionBoxClass (void)
  61. : m_bIsAddedToScene (false)
  62. {
  63. //
  64. // Loop through and create all the line segments
  65. //
  66. for (int line = 0; line < 24; line ++) {
  67. m_pBoundingLines[line] = new SelectionBoxClass::DecorationLineClass;
  68. }
  69. return ;
  70. }
  71. //////////////////////////////////////////////////////////////////////
  72. //
  73. // ~SelectionBoxClass
  74. //
  75. //////////////////////////////////////////////////////////////////////
  76. SelectionBoxClass::~SelectionBoxClass (void)
  77. {
  78. // Remove this item from the scene
  79. Remove_From_Scene ();
  80. // Loop through and release all the line segments
  81. for (int line = 0; line < 24; line ++) {
  82. MEMBER_RELEASE (m_pBoundingLines[line]);
  83. }
  84. return ;
  85. }
  86. //////////////////////////////////////////////////////////////////////
  87. //
  88. // Display_Around_Node
  89. //
  90. //////////////////////////////////////////////////////////////////////
  91. void
  92. SelectionBoxClass::Display_Around_Node (const NodeClass &node)
  93. {
  94. RenderObjClass *render_obj = node.Peek_Render_Obj ();
  95. if (render_obj != NULL) {
  96. Display_Around_Node (*render_obj);
  97. }
  98. return ;
  99. }
  100. //////////////////////////////////////////////////////////////////////
  101. //
  102. // Display_Around_Node
  103. //
  104. //////////////////////////////////////////////////////////////////////
  105. void
  106. SelectionBoxClass::Display_Around_Node (const RenderObjClass &render_obj)
  107. {
  108. // Get the bounding box of the item
  109. AABoxClass bounding_box;
  110. render_obj.Get_Obj_Space_Bounding_Box (bounding_box);
  111. const Matrix3D &transform = render_obj.Get_Transform ();
  112. // Alias the extent vector
  113. Vector3 extent = bounding_box.Extent;
  114. extent.X += extent.X / 50.0F;
  115. extent.Y += extent.Y / 50.0F;
  116. extent.Z += extent.Z / 50.0F;
  117. //
  118. // Calculate the positions of the eight verticies
  119. //
  120. Vector3 verticies[8];
  121. verticies[0] = bounding_box.Center + Vector3 (extent.X, extent.Y, extent.Z);
  122. verticies[1] = bounding_box.Center + Vector3 (-extent.X, extent.Y, extent.Z);
  123. verticies[2] = bounding_box.Center + Vector3 (-extent.X, extent.Y, -extent.Z);
  124. verticies[3] = bounding_box.Center + Vector3 (extent.X, extent.Y, -extent.Z);
  125. verticies[4] = bounding_box.Center + Vector3 (extent.X, -extent.Y, extent.Z);
  126. verticies[5] = bounding_box.Center + Vector3 (-extent.X, -extent.Y, extent.Z);
  127. verticies[6] = bounding_box.Center + Vector3 (-extent.X, -extent.Y, -extent.Z);
  128. verticies[7] = bounding_box.Center + Vector3 (extent.X, -extent.Y, -extent.Z);
  129. //
  130. // Rotate these verticies using the item's current transform
  131. //
  132. for (int vertex = 0; vertex < 8; vertex ++) {
  133. verticies[vertex] = transform * verticies[vertex];
  134. }
  135. //
  136. // Determine the new size for each grab handle
  137. //
  138. float largest_dim = max (extent.X, extent.Y);
  139. largest_dim = max (largest_dim, extent.Z);
  140. float width = max (largest_dim / 55.0F, 0.02F);
  141. //
  142. // Set the positions of the line segments that make up the box
  143. //
  144. m_pBoundingLines[0]->Reset (verticies[0], verticies[0] + (verticies[1]-verticies[0])/4.00F, width);
  145. m_pBoundingLines[4]->Reset (verticies[0], verticies[0] + (verticies[4]-verticies[0])/4.00F, width);
  146. m_pBoundingLines[12]->Reset (verticies[0], verticies[0] + (verticies[3]-verticies[0])/4.00F, width);
  147. m_pBoundingLines[1]->Reset (verticies[1], verticies[1] + (verticies[2]-verticies[1])/4.00F, width);
  148. m_pBoundingLines[5]->Reset (verticies[1], verticies[1] + (verticies[5]-verticies[1])/4.00F, width);
  149. m_pBoundingLines[13]->Reset (verticies[1], verticies[1] + (verticies[0]-verticies[1])/4.00F, width);
  150. m_pBoundingLines[2]->Reset (verticies[2], verticies[2] + (verticies[3]-verticies[2])/4.00F, width);
  151. m_pBoundingLines[6]->Reset (verticies[2], verticies[2] + (verticies[6]-verticies[2])/4.00F, width);
  152. m_pBoundingLines[14]->Reset (verticies[2], verticies[2] + (verticies[1]-verticies[2])/4.00F, width);
  153. m_pBoundingLines[3]->Reset (verticies[3], verticies[3] + (verticies[0]-verticies[3])/4.00F, width);
  154. m_pBoundingLines[7]->Reset (verticies[3], verticies[3] + (verticies[7]-verticies[3])/4.00F, width);
  155. m_pBoundingLines[15]->Reset (verticies[3], verticies[3] + (verticies[2]-verticies[3])/4.00F, width);
  156. m_pBoundingLines[8]->Reset (verticies[4], verticies[4] + (verticies[5]-verticies[4])/4.00F, width);
  157. m_pBoundingLines[16]->Reset (verticies[4], verticies[4] + (verticies[0]-verticies[4])/4.00F, width);
  158. m_pBoundingLines[17]->Reset (verticies[4], verticies[4] + (verticies[7]-verticies[4])/4.00F, width);
  159. m_pBoundingLines[9]->Reset (verticies[5], verticies[5] + (verticies[6]-verticies[5])/4.00F, width);
  160. m_pBoundingLines[18]->Reset (verticies[5], verticies[5] + (verticies[4]-verticies[5])/4.00F, width);
  161. m_pBoundingLines[19]->Reset (verticies[5], verticies[5] + (verticies[1]-verticies[5])/4.00F, width);
  162. m_pBoundingLines[10]->Reset (verticies[6], verticies[6] + (verticies[7]-verticies[6])/4.00F, width);
  163. m_pBoundingLines[20]->Reset (verticies[6], verticies[6] + (verticies[5]-verticies[6])/4.00F, width);
  164. m_pBoundingLines[21]->Reset (verticies[6], verticies[6] + (verticies[2]-verticies[6])/4.00F, width);
  165. m_pBoundingLines[11]->Reset (verticies[7], verticies[7] + (verticies[4]-verticies[7])/4.00F, width);
  166. m_pBoundingLines[22]->Reset (verticies[7], verticies[7] + (verticies[6]-verticies[7])/4.00F, width);
  167. m_pBoundingLines[23]->Reset (verticies[7], verticies[7] + (verticies[3]-verticies[7])/4.00F, width);
  168. //
  169. // Add the lines to the scene if necessary
  170. //
  171. if (m_bIsAddedToScene == false) {
  172. SceneEditorClass *scene = ::Get_Scene_Editor ();
  173. if (scene != NULL) {
  174. // Loop through all the line segments and add them to the scene
  175. for (int line = 0; line < 24; line ++) {
  176. scene->Add_Dynamic_Object (m_pBoundingLines[line]);
  177. }
  178. // Success!
  179. m_bIsAddedToScene = true;
  180. }
  181. }
  182. return ;
  183. }
  184. //////////////////////////////////////////////////////////////////////
  185. //
  186. // Remove_From_Scene
  187. //
  188. //////////////////////////////////////////////////////////////////////
  189. void
  190. SelectionBoxClass::Remove_From_Scene (void)
  191. {
  192. // Get a pointer to the current scene
  193. SceneEditorClass *scene = ::Get_Scene_Editor ();
  194. if (scene != NULL) {
  195. // Remove all the lines from the scene
  196. for (int line = 0; line < 24; line ++) {
  197. scene->Remove_Object (m_pBoundingLines[line]);
  198. }
  199. // Success!
  200. m_bIsAddedToScene = true;
  201. }
  202. return ;
  203. }
  204. //////////////////////////////////////////////////////////////////////
  205. //
  206. // Set_Color
  207. //
  208. //////////////////////////////////////////////////////////////////////
  209. void
  210. SelectionBoxClass::Set_Color (const Vector3 &color)
  211. {
  212. // Change the color of all the lines
  213. for (int line = 0; line < 24; line ++) {
  214. m_pBoundingLines[line]->Set_Color (color);
  215. }
  216. return ;
  217. }