BuildingBrush.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #define BUILDINGBRUSH_CPP
  2. /*************************************************************************************************\
  3. BuildingBrush.cpp : Implementation of the BuildingBrush component.
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. \*************************************************************************************************/
  8. #include "BuildingBrush.h"
  9. #ifndef EDITOROBJECTMGR_H
  10. #include "EditorObjectMgr.h"
  11. #endif
  12. #ifndef TERRAIN_H
  13. #include "Terrain.h"
  14. #endif
  15. #ifndef EDITOROBJECTS_H
  16. #include "EditorObjects.h"
  17. #endif
  18. #ifndef EDITORINTERFACE_H
  19. #include "EditorInterface.h"
  20. #endif
  21. #include "resource.h"
  22. BuildingBrush::BuildingBrush( int Group, int IndexInGroup, int Alignment )
  23. {
  24. group = Group;
  25. indexInGroup = IndexInGroup;
  26. pAction = NULL;
  27. pCursor = EditorObjectMgr::instance()->getAppearance( Group, IndexInGroup );
  28. pCursor->teamId = Alignment;
  29. pCursor->setInView(true);
  30. pCursor->setVisibility(true,true);
  31. pCursor->position = eye->getPosition();
  32. pCursor->update();
  33. alignment = Alignment;
  34. }
  35. BuildingBrush::BuildingBrush()
  36. {
  37. pCursor = NULL;
  38. group = indexInGroup = -1;
  39. pAction = NULL;
  40. }
  41. BuildingBrush::~BuildingBrush()
  42. {
  43. if ( pCursor )
  44. delete pCursor;
  45. }
  46. bool BuildingBrush::canPaint( Stuff::Vector3D& worldPos, int screenX, int screenY, int flags )
  47. {
  48. if ( !EditorObjectMgr::instance()->canAddBuilding( worldPos, pCursor->rotation, group, indexInGroup ) )
  49. {
  50. pCursor->setHighlightColor( 0x007f0000 );
  51. return false; // no two things on top of each other
  52. }
  53. pCursor->setHighlightColor( 0x00007f00 );
  54. return true;
  55. }
  56. bool BuildingBrush::beginPaint()
  57. {
  58. gosASSERT( !pAction );
  59. pAction = new BuildingAction;
  60. return true; // need to set up undo here
  61. }
  62. bool BuildingBrush::paint( Stuff::Vector3D& worldPos, int screenX, int screenY )
  63. {
  64. if ((AppearanceTypeList::appearanceHeap->totalCoreLeft() < 1000/*arbitrary*/)
  65. || (AppearanceTypeList::appearanceHeap->totalCoreLeft() < 0.01/*arbitrary*/ * AppearanceTypeList::appearanceHeap->size()))
  66. {
  67. AfxMessageBox(IDS_APPEARANCE_HEAP_EXHAUSTED);
  68. {
  69. /*The preceding message box will cause the app (EditorInterface) to miss the "left
  70. mouse button up" event, so we'll act on it here.*/
  71. POINT point;
  72. GetCursorPos(&point);
  73. EditorInterface::instance()->handleLeftButtonUp(point.x, point.y);
  74. ReleaseCapture();
  75. }
  76. return false;
  77. }
  78. else
  79. {
  80. EditorObject* pInfo = EditorObjectMgr::instance()->addBuilding( worldPos, group, indexInGroup, alignment, pCursor->rotation );
  81. {
  82. if ( pInfo && pAction )
  83. pAction->addBuildingInfo( *pInfo );
  84. return true;
  85. }
  86. }
  87. }
  88. Action* BuildingBrush::endPaint( )
  89. {
  90. if (pAction)
  91. {
  92. if ( !pAction->objInfoPtrList.Count() )
  93. {
  94. delete pAction;
  95. pAction = NULL;
  96. }
  97. }
  98. Action* pRetAction = pAction;
  99. pAction = NULL;
  100. return pRetAction;
  101. }
  102. bool BuildingBrush::BuildingAction::undo()
  103. {
  104. bool bRetVal = true;
  105. /*
  106. OBJ_APPEAR_LIST::EIterator iter3 = buildingAppearanceCopies.Begin();
  107. for ( OBJ_INFO_LIST::EIterator iter = positions.Begin();
  108. !iter.IsDone(); iter++ )
  109. {
  110. EditorObject* pObj = EditorObjectMgr::instance()->getObjectAtLocation((*iter3).position.x, (*iter3).position.y);
  111. if (pObj)
  112. {
  113. bRetVal = EditorObjectMgr::instance()->deleteBuilding( pObj ) && bRetVal;
  114. }
  115. else
  116. {
  117. gosASSERT(false);
  118. }
  119. iter3++;
  120. }
  121. */
  122. for ( OBJ_INFO_PTR_LIST::EIterator iter = objInfoPtrList.Begin(); !iter.IsDone(); iter++ )
  123. {
  124. ObjectAppearance *pAppearance = (*(*iter)).appearance();
  125. EditorObject* pObj = EditorObjectMgr::instance()->getObjectAtLocation(pAppearance->position.x, pAppearance->position.y);
  126. if (pObj)
  127. {
  128. bRetVal = EditorObjectMgr::instance()->deleteBuilding( pObj ) && bRetVal;
  129. }
  130. else
  131. {
  132. gosASSERT(false);
  133. }
  134. }
  135. return bRetVal;
  136. }
  137. bool BuildingBrush::BuildingAction::redo()
  138. {
  139. bool bRetVal = true;
  140. for ( OBJ_INFO_PTR_LIST::EIterator iter = objInfoPtrList.Begin(); !iter.IsDone(); iter++ )
  141. {
  142. ObjectAppearance *pAppearance = (*(*iter)).appearance();
  143. EditorObject* pObj = EditorObjectMgr::instance()->addBuilding( pAppearance->position, EditorObjectMgr::instance()->getGroup( (*(*iter)).getID() ), EditorObjectMgr::instance()->getIndexInGroup( (*(*iter)).getID() ), pAppearance->teamId, pAppearance->rotation );
  144. if (pObj)
  145. {
  146. (*pObj).CastAndCopy((*(*iter)));
  147. }
  148. else
  149. {
  150. gosASSERT(false);
  151. bRetVal = false;
  152. }
  153. }
  154. return bRetVal;
  155. }
  156. void BuildingBrush::BuildingAction::addBuildingInfo(EditorObject& info)
  157. {
  158. EditorObject *pCopy = info.Clone();
  159. gosASSERT(pCopy);
  160. objInfoPtrList.Append(pCopy);
  161. }
  162. void BuildingBrush::update( int ScreenMouseX, int ScreenMouseY )
  163. {
  164. if ( !pCursor )
  165. return;
  166. Stuff::Vector3D pos;
  167. Stuff::Vector2DOf<long> pt;
  168. pt.x = ScreenMouseX;
  169. pt.y = ScreenMouseY;
  170. eye->inverseProject( pt, pos );
  171. if ( !EditorObjectMgr::instance()->canAddBuilding( pos, pCursor->rotation, group, indexInGroup ) )
  172. pCursor->setHighlightColor( 0x00400000 );
  173. else
  174. pCursor->setHighlightColor( 0x00004000 );
  175. pCursor->position = pos;
  176. pCursor->recalcBounds();
  177. pCursor->update(); //Safe tp call here now because we run the first update in the constructor which caches in texture
  178. //NOT TRUE WITH RIA CODE!!!!! Must have a separate update or NO Triangles get added!!!
  179. pCursor->setVisibility( true, true );
  180. }
  181. void BuildingBrush::render( int ScreenMouseX, int ScreenMouseY )
  182. {
  183. if ( !pCursor )
  184. return;
  185. /*
  186. Stuff::Vector3D pos;
  187. Stuff::Vector2DOf<long> pt;
  188. pt.x = ScreenMouseX;
  189. pt.y = ScreenMouseY;
  190. eye->inverseProject( pt, pos );
  191. if ( !EditorObjectMgr::instance()->canAddBuilding( pos, group, indexInGroup ) )
  192. pCursor->setHighlightColor( 0x00400000 );
  193. else
  194. pCursor->setHighlightColor( 0x00004000 );
  195. pCursor->position = pos;
  196. pCursor->recalcBounds();
  197. pCursor->update(); //Safe tp call here now because we run the first update in the constructor which caches in texture
  198. //NOT TRUE WITH RIA CODE!!!!! Must have a separate update or NO Triangles get added!!!
  199. pCursor->setVisibility( true, true );
  200. */ //This may cause cursor to lag. Check it and see.
  201. pCursor->render();
  202. }
  203. void BuildingBrush::rotateBrush( int direction )
  204. {
  205. int ID = EditorObjectMgr::instance()->getID( group, indexInGroup );
  206. int fitID = EditorObjectMgr::instance()->getFitID(ID);
  207. if ((EditorObjectMgr::WALL == EditorObjectMgr::instance()->getSpecialType(ID)) || (33/*repair bay*/ == fitID))
  208. {
  209. pCursor->rotation += direction * 90;
  210. }
  211. else
  212. {
  213. pCursor->rotation += direction * 45;
  214. }
  215. }
  216. //*************************************************************************************************
  217. // end of file ( BuildingBrush.cpp )