GroupMgr.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. // GroupMgrClass.cpp
  21. //
  22. // Class definition for managing groups.
  23. //
  24. #include "stdafx.h"
  25. #include "groupmgr.h"
  26. #include "leveleditdoc.h"
  27. #include "cameramgr.h"
  28. #include "node.h"
  29. #include "sceneeditor.h"
  30. #include "utils.h"
  31. #include "wwmath.h"
  32. #include "waypointnode.h"
  33. #include "waypathnode.h"
  34. /////////////////////////////////////////////////////////////
  35. //
  36. // Add_Node
  37. //
  38. /////////////////////////////////////////////////////////////
  39. const GroupMgrClass &
  40. GroupMgrClass::operator= (const GroupMgrClass &src)
  41. {
  42. m_BoundingSphere = src.m_BoundingSphere;
  43. m_BoundingBox = src.m_BoundingBox;
  44. m_LowZCenter = src.m_LowZCenter;
  45. m_AbsCenter = src.m_AbsCenter;
  46. m_Name = src.m_Name;
  47. m_bDirty = src.m_bDirty;
  48. Free_List ();
  49. Import_Nodes (src.m_GroupList);
  50. // Return a reference to ourselves
  51. return *this;
  52. }
  53. /////////////////////////////////////////////////////////////
  54. //
  55. // Export_Nodes
  56. //
  57. /////////////////////////////////////////////////////////////
  58. void
  59. GroupMgrClass::Export_Nodes (NODE_LIST &node_list)
  60. {
  61. //
  62. // Add all the nodes from this group to the list
  63. //
  64. for (int index = 0; index < m_GroupList.Count (); index ++) {
  65. NodeClass *node = m_GroupList[index];
  66. if (node != NULL) {
  67. node_list.Add_Unique (node);
  68. }
  69. }
  70. return ;
  71. }
  72. /////////////////////////////////////////////////////////////
  73. //
  74. // Import_Nodes
  75. //
  76. /////////////////////////////////////////////////////////////
  77. void
  78. GroupMgrClass::Import_Nodes (const NODE_LIST &node_list)
  79. {
  80. // Loop through all the entries in the list and add them
  81. // to our group.
  82. for (int index = 0; index < node_list.Count (); index ++) {
  83. Add_Node (node_list[index]);
  84. }
  85. return;
  86. }
  87. /////////////////////////////////////////////////////////////
  88. //
  89. // Add_Node
  90. //
  91. /////////////////////////////////////////////////////////////
  92. void
  93. GroupMgrClass::Add_Node (NodeClass *node)
  94. {
  95. if ((node != NULL) && (Is_Item_In_Group (node) == false)) {
  96. //
  97. // Add this item to the list
  98. //
  99. SAFE_ADD_REF (node);
  100. m_GroupList.Add (node);
  101. Recalc_Stats ();
  102. }
  103. return ;
  104. }
  105. /////////////////////////////////////////////////////////////
  106. //
  107. // Remove_Node
  108. //
  109. /////////////////////////////////////////////////////////////
  110. void
  111. GroupMgrClass::Remove_Node (NodeClass *node_to_remove)
  112. {
  113. bool found = false;
  114. for (int index = 0; !found && index < m_GroupList.Count (); index ++) {
  115. NodeClass *node = m_GroupList[index];
  116. //
  117. // If this is the node we were looking for, then remove it
  118. // from the list and free our hold on it.
  119. //
  120. if (node == node_to_remove) {
  121. m_GroupList.Delete (index);
  122. MEMBER_RELEASE (node);
  123. found = true;
  124. }
  125. }
  126. if (found) {
  127. Recalc_Stats ();
  128. }
  129. return ;
  130. }
  131. /////////////////////////////////////////////////////////////
  132. //
  133. // Reset
  134. //
  135. /////////////////////////////////////////////////////////////
  136. void
  137. GroupMgrClass::Reset (void)
  138. {
  139. Free_List ();
  140. return ;
  141. }
  142. /////////////////////////////////////////////////////////////
  143. //
  144. // Free_List
  145. //
  146. /////////////////////////////////////////////////////////////
  147. void
  148. GroupMgrClass::Free_List (void)
  149. {
  150. for (int index = 0; index < m_GroupList.Count (); index ++) {
  151. NodeClass *node = m_GroupList[index];
  152. MEMBER_RELEASE (node);
  153. }
  154. m_GroupList.Delete_All ();
  155. Recalc_Stats ();
  156. return ;
  157. }
  158. /////////////////////////////////////////////////////////////
  159. //
  160. // Is_Item_In_Group
  161. //
  162. /////////////////////////////////////////////////////////////
  163. bool
  164. GroupMgrClass::Is_Item_In_Group (NodeClass *node_to_find)
  165. {
  166. bool retval = false;
  167. //
  168. // Loop until we've found the node
  169. //
  170. for ( int index = 0;
  171. (retval == false) && (index < m_GroupList.Count ());
  172. index ++) {
  173. NodeClass *node = m_GroupList[index];
  174. retval = (node == node_to_find);
  175. }
  176. return retval;
  177. }
  178. /////////////////////////////////////////////////////////////
  179. //
  180. // Clone_Group
  181. //
  182. /////////////////////////////////////////////////////////////
  183. void
  184. GroupMgrClass::Clone_Group (void)
  185. {
  186. NODE_LIST temp_list;
  187. //
  188. // Duplicate the group list
  189. //
  190. for (int index = 0; index < m_GroupList.Count (); index ++) {
  191. NodeClass *node = m_GroupList[index];
  192. temp_list.Add (node);
  193. }
  194. // Remove all items from the group
  195. m_GroupList.Delete_All ();
  196. //
  197. // Loop through and create clones of the items
  198. //
  199. for (index = 0; index < temp_list.Count (); index ++) {
  200. NodeClass *node = temp_list[index];
  201. if (node->Get_Type () == NODE_TYPE_WAYPOINT) {
  202. //
  203. // Find where this waypoint is on the path
  204. //
  205. WaypointNodeClass *waypoint = (WaypointNodeClass *)node;
  206. WaypathNodeClass *path = waypoint->Peek_Waypath ();
  207. int index = path->Find_Index (waypoint);
  208. if (index >= 0) {
  209. //
  210. // Insert a new point one after the given point
  211. //
  212. path->Insert_Point (index + 1, waypoint->Get_Position ());
  213. //
  214. // Add this new point to our group
  215. //
  216. WaypointNodeClass *new_point = NULL;
  217. path->Get_Point (index + 1, &new_point);
  218. if (new_point != NULL) {
  219. SAFE_ADD_REF (new_point);
  220. m_GroupList.Add (new_point);
  221. }
  222. }
  223. } else if (node->Get_Parent_Node () != NULL) {
  224. if (node->Get_Parent_Node ()->Can_Add_Child_Nodes ()) {
  225. //
  226. // Ask the parent node to create us a new child node, then add
  227. // it to the list
  228. //
  229. NodeClass *new_node = node->Get_Parent_Node ()->Add_Child_Node (node->Get_Transform ());
  230. if (new_node != NULL) {
  231. SAFE_ADD_REF (new_node);
  232. m_GroupList.Add (new_node);
  233. }
  234. }
  235. } else {
  236. //
  237. // Create a new instance of this node, and add it to our list
  238. //
  239. NodeClass *new_node = ::Get_Scene_Editor ()->Clone_Node (node);
  240. if (new_node != NULL) {
  241. m_GroupList.Add (new_node);
  242. }
  243. }
  244. MEMBER_RELEASE (node);
  245. }
  246. Recalc_Stats ();
  247. return ;
  248. }
  249. /////////////////////////////////////////////////////////////
  250. //
  251. // Recalc_Stats
  252. //
  253. /////////////////////////////////////////////////////////////
  254. void
  255. GroupMgrClass::Recalc_Stats (void)
  256. {
  257. m_BoundingSphere.Init (Vector3 (0, 0, 0), 0);
  258. m_BoundingBox.Init (Vector3 (0, 0, 0), Vector3 (0, 0, 0));
  259. m_LowZCenter = Vector3 (0, 0, 0);
  260. float low_z = WWMATH_FLOAT_MAX;
  261. //
  262. // Loop through all the nodes in the group
  263. //
  264. for (int index = 0; index < m_GroupList.Count (); index ++) {
  265. NodeClass *node = m_GroupList[index];
  266. RenderObjClass *render_obj = node->Peek_Render_Obj ();
  267. if (render_obj != NULL) {
  268. if (index == 0) {
  269. m_BoundingBox = render_obj->Get_Bounding_Box ();
  270. m_BoundingSphere = render_obj->Get_Bounding_Sphere ();
  271. m_LowZCenter = render_obj->Get_Position ();
  272. low_z = m_LowZCenter.Z;
  273. } else {
  274. //
  275. // Add this node's stats to the pot
  276. //
  277. m_BoundingBox.Add_Box (render_obj->Get_Bounding_Box ());
  278. m_BoundingSphere += render_obj->Get_Bounding_Sphere ();
  279. m_LowZCenter += render_obj->Get_Position ();
  280. if (render_obj->Get_Position ().Z < low_z) {
  281. low_z = render_obj->Get_Position ().Z;
  282. }
  283. }
  284. }
  285. }
  286. m_LowZCenter = m_LowZCenter / (float)m_GroupList.Count ();
  287. m_AbsCenter = m_LowZCenter;
  288. m_LowZCenter.Z = low_z;
  289. m_bDirty = false;
  290. return ;
  291. }
  292. /////////////////////////////////////////////////////////////
  293. //
  294. // Hide
  295. //
  296. /////////////////////////////////////////////////////////////
  297. void
  298. GroupMgrClass::Hide (bool hide)
  299. {
  300. // Loop through all the nodes in the group
  301. for (int index = 0; index < m_GroupList.Count (); index ++) {
  302. NodeClass *node = m_GroupList[index];
  303. if (node != NULL) {
  304. //
  305. // Hide this node
  306. //
  307. node->Hide (hide);
  308. }
  309. }
  310. // Remember our 'state'
  311. m_bHidden = hide;
  312. return ;
  313. }
  314. //*********************************************************************************************//
  315. //
  316. // End of GroupMgrClass
  317. //
  318. //*********************************************************************************************//
  319. //*********************************************************************************************//
  320. //
  321. // Start of SelectionMgrClass
  322. //
  323. //*********************************************************************************************//
  324. /////////////////////////////////////////////////////////////
  325. //
  326. // Add_Node
  327. //
  328. /////////////////////////////////////////////////////////////
  329. void
  330. SelectionMgrClass::Add_Node (NodeClass *node)
  331. {
  332. if ((node != NULL) && (Is_Item_In_Group (node) == false)) {
  333. //
  334. // Show the selection box
  335. //
  336. node->Show_Selection_Box (true);
  337. }
  338. GroupMgrClass::Add_Node (node);
  339. return ;
  340. }
  341. /////////////////////////////////////////////////////////////
  342. //
  343. // Remove_Node
  344. //
  345. /////////////////////////////////////////////////////////////
  346. void
  347. SelectionMgrClass::Remove_Node (NodeClass *node)
  348. {
  349. //
  350. // Remove the selection box from this node
  351. //
  352. if (node != NULL) {
  353. node->Show_Selection_Box (false);
  354. }
  355. GroupMgrClass::Remove_Node (node);
  356. return ;
  357. }
  358. /////////////////////////////////////////////////////////////
  359. //
  360. // Reset
  361. //
  362. /////////////////////////////////////////////////////////////
  363. void
  364. SelectionMgrClass::Reset (void)
  365. {
  366. // Loop through all the nodes in the group
  367. for (int index = 0; index < m_GroupList.Count (); index ++) {
  368. NodeClass *node = m_GroupList[index];
  369. if (node != NULL) {
  370. //
  371. // Remove the selection item from this node
  372. //
  373. node->Show_Selection_Box (false);
  374. }
  375. }
  376. GroupMgrClass::Reset ();
  377. return ;
  378. }
  379. /////////////////////////////////////////////////////////////
  380. //
  381. // Clone_Group
  382. //
  383. /////////////////////////////////////////////////////////////
  384. void
  385. SelectionMgrClass::Clone_Group (void)
  386. {
  387. //
  388. // Remove the selection box from around all the items
  389. //
  390. for (int index = 0; index < m_GroupList.Count (); index ++) {
  391. NodeClass *node = m_GroupList[index];
  392. node->Show_Selection_Box (false);
  393. }
  394. //
  395. // Let the base class do the clone operation
  396. //
  397. GroupMgrClass::Clone_Group ();
  398. //
  399. // Show the selection box for all the cloned items
  400. //
  401. for (index = 0; index < m_GroupList.Count (); index ++) {
  402. NodeClass *node = m_GroupList[index];
  403. node->Show_Selection_Box (true);
  404. }
  405. return ;
  406. }
  407. //*********************************************************************************************//
  408. //
  409. // End of GroupMgrClass
  410. //
  411. //*********************************************************************************************//
  412. //*********************************************************************************************//
  413. //
  414. // Start of UserGroupMgrClass
  415. //
  416. //*********************************************************************************************//
  417. /////////////////////////////////////////////////////////////
  418. //
  419. // Recalc_Stats
  420. //
  421. /////////////////////////////////////////////////////////////
  422. void
  423. UserGroupMgrClass::Recalc_Stats (void)
  424. {
  425. // Allow the base class to process this call
  426. GroupMgrClass::Recalc_Stats ();
  427. // Remove this group from the scene if its empty
  428. Update_Global_Group_List ();
  429. return ;
  430. }
  431. /////////////////////////////////////////////////////////////
  432. //
  433. // Update_Global_Group_List
  434. //
  435. /////////////////////////////////////////////////////////////
  436. void
  437. UserGroupMgrClass::Update_Global_Group_List (void)
  438. {
  439. if (m_GroupList.Count () == 0) {
  440. ::Get_Scene_Editor ()->Remove_Global_Group (this);
  441. }
  442. return ;
  443. }
  444. /////////////////////////////////////////////////////////////
  445. //
  446. // Add_Node
  447. //
  448. /////////////////////////////////////////////////////////////
  449. void
  450. UserGroupMgrClass::Add_Node (NodeClass *node)
  451. {
  452. GroupMgrClass::Add_Node (node);
  453. if (node != NULL) {
  454. }
  455. return ;
  456. }
  457. /////////////////////////////////////////////////////////////
  458. //
  459. // Remove_Node
  460. //
  461. /////////////////////////////////////////////////////////////
  462. void
  463. UserGroupMgrClass::Remove_Node (NodeClass *node)
  464. {
  465. bool in_list = Is_Item_In_Group (node);
  466. GroupMgrClass::Remove_Node (node);
  467. // OK to process?
  468. if (in_list && node != NULL) {
  469. }
  470. return ;
  471. }
  472. /////////////////////////////////////////////////////////////
  473. //
  474. // ~UserGroupMgrClass
  475. //
  476. /////////////////////////////////////////////////////////////
  477. UserGroupMgrClass::~UserGroupMgrClass (void)
  478. {
  479. return ;
  480. }
  481. //*********************************************************************************************//
  482. //
  483. // End of UserGroupMgrClass
  484. //
  485. //*********************************************************************************************//