BuildingSettingsDlg.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. // BuildingSettingsDlg.cpp : implementation file
  5. //
  6. #include "stdafx.h"
  7. #include "resource.h"
  8. #include "BuildingSettingsDlg.h"
  9. #include "EditorObjects.h"
  10. #include "EditorObjectMgr.h"
  11. #include "EditorInterface.h" // just for the undo manager
  12. #include "../Code/unitdesg.h" /* just for definition of MIN_TERRAIN_PART_ID and MAX_MAP_CELL_WIDTH */
  13. /////////////////////////////////////////////////////////////////////////////
  14. // BuildingSettingsDlg dialog
  15. BuildingSettingsDlg::BuildingSettingsDlg( EList< EditorObject*, EditorObject* >& newList/*=NULL*/, ActionUndoMgr &undoMgr)
  16. : CDialog(BuildingSettingsDlg::IDD), units( newList )
  17. {
  18. //{{AFX_DATA_INIT(BuildingSettingsDlg)
  19. m_Alignment = -1;
  20. m_x = 0.0f;
  21. m_y = 0.0f;
  22. m_partID = 0;
  23. m_forestName = _T("");
  24. //}}AFX_DATA_INIT
  25. pUndoMgr = &undoMgr;
  26. pAction = NULL;
  27. }
  28. void BuildingSettingsDlg::DoDataExchange(CDataExchange* pDX)
  29. {
  30. CDialog::DoDataExchange(pDX);
  31. //{{AFX_DATA_MAP(BuildingSettingsDlg)
  32. DDX_Control(pDX, IDC_MECH, m_Mech);
  33. DDX_Control(pDX, IDC_GROUP, m_Group);
  34. DDX_Radio(pDX, IDC_ALIGN1, m_Alignment);
  35. DDX_Text(pDX, IDC_BS_X_EDIT, m_x);
  36. DDX_Text(pDX, IDC_BS_Y_EDIT, m_y);
  37. DDX_Text(pDX, IDC_BS_PARTID_EDIT, m_partID);
  38. DDX_Text(pDX, IDC_FOREST_NAME, m_forestName);
  39. //}}AFX_DATA_MAP
  40. }
  41. BEGIN_MESSAGE_MAP(BuildingSettingsDlg, CDialog)
  42. //{{AFX_MSG_MAP(BuildingSettingsDlg)
  43. ON_CBN_SELCHANGE(IDC_GROUP, OnSelchangeGroup)
  44. ON_CBN_SELCHANGE(IDC_MECH, OnSelchangeMech)
  45. //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47. /////////////////////////////////////////////////////////////////////////////
  48. // BuildingSettingsDlg message handlers
  49. void BuildingSettingsDlg::OnSelchangeGroup()
  50. {
  51. m_Mech.ResetContent();
  52. int group = m_Group.GetCurSel();
  53. group = m_Group.GetItemData( group );
  54. const char* MechNames[256];
  55. int count = 256;
  56. EditorObjectMgr::instance()->getBuildingNamesInGroup( group, MechNames, count );
  57. for ( int i = 0; i < count; ++i )
  58. {
  59. m_Mech.AddString( MechNames[i] );
  60. }
  61. m_Mech.SetCurSel( 0 );
  62. }
  63. void BuildingSettingsDlg::applyChanges()
  64. {
  65. // get the type info from the dlg box
  66. int index = m_Group.GetCurSel( );
  67. if ( index != -1 )
  68. {
  69. int group = m_Group.GetItemData( index );
  70. int indexInGroup = m_Mech.GetCurSel( );
  71. if ( indexInGroup != -1 )
  72. {
  73. for ( EDITOROBJECT_LIST::EIterator iter = units.Begin(); !iter.IsDone(); iter++ )
  74. {
  75. (*iter)->setAppearance( group, indexInGroup );
  76. }
  77. }
  78. }
  79. // now set the alignment
  80. UpdateData( true );
  81. index = m_Alignment;
  82. if ( index != -1 )
  83. {
  84. for ( EDITOROBJECT_LIST::EIterator iter = units.Begin(); !iter.IsDone(); iter++ )
  85. {
  86. (*iter)->setAlignment( index );
  87. }
  88. }
  89. unsigned long base=0, color1=0, color2=0;
  90. bool bBase = false;
  91. bool bColor1 = false;
  92. bool bColor2 = false;
  93. // now figure out the colors
  94. CWnd* pWnd = GetDlgItem( IDC_BASE );
  95. if ( pWnd )
  96. {
  97. CString tmpStr;
  98. pWnd->GetWindowText( tmpStr );
  99. if ( tmpStr.GetLength() )
  100. {
  101. bBase = true;
  102. tmpStr.Replace( "0x", "" );
  103. sscanf( tmpStr, "%x", &base );
  104. base |= 0xff000000;
  105. }
  106. }
  107. pWnd = GetDlgItem( IDC_HIGHLIGHT1 );
  108. if ( pWnd )
  109. {
  110. CString tmpStr;
  111. pWnd->GetWindowText( tmpStr );
  112. if ( tmpStr.GetLength() )
  113. {
  114. bColor1 = true;
  115. tmpStr.Replace( "0x", "" );
  116. sscanf( tmpStr, "%x", &color1 );
  117. color1 |= 0xff000000;
  118. }
  119. }
  120. pWnd = GetDlgItem( IDC_HIGHLIGHT2 );
  121. if ( pWnd )
  122. {
  123. CString tmpStr;
  124. pWnd->GetWindowText( tmpStr );
  125. if ( tmpStr.GetLength() )
  126. {
  127. bColor2 = true;
  128. tmpStr.Replace( "0x", "" );
  129. sscanf( tmpStr, "%x", &color2 );
  130. color2 |= 0xff000000;
  131. }
  132. }
  133. }
  134. void BuildingSettingsDlg::OnOK()
  135. {
  136. if (NULL != pUndoMgr)
  137. {
  138. pUndoMgr->AddAction(pAction);
  139. }
  140. else
  141. {
  142. delete pAction;
  143. }
  144. pAction = NULL;
  145. applyChanges();
  146. CDialog::OnOK();
  147. }
  148. BOOL BuildingSettingsDlg::OnInitDialog()
  149. {
  150. CDialog::OnInitDialog();
  151. pAction = new ModifyBuildingAction;
  152. EDITOROBJECT_LIST::EIterator iter;
  153. for ( iter = units.Begin(); !iter.IsDone(); iter++ )
  154. {
  155. pAction->addBuildingInfo(*(*iter));
  156. }
  157. updateMemberVariables();
  158. return TRUE; // return TRUE unless you set the focus to a control
  159. // EXCEPTION: OCX Property Pages should return FALSE
  160. }
  161. void BuildingSettingsDlg::OnSelchangeMech()
  162. {
  163. int group = m_Group.GetCurSel();
  164. group = m_Group.GetItemData( group );
  165. /*int indexInGroup =*/ m_Mech.GetCurSel();
  166. }
  167. void BuildingSettingsDlg::updateMemberVariables()
  168. {
  169. long forest = -1;
  170. bool bForests = true;
  171. EditorObject* pEditorObject = units.GetHead();
  172. m_Alignment = pEditorObject->getAlignment();
  173. if (m_Alignment == -1)
  174. m_Alignment = 8; //Move it to the neutral select button
  175. for ( EDITOROBJECT_LIST::EIterator iter = units.Begin(); !iter.IsDone(); iter++ )
  176. {
  177. if ( ((*iter)->getAlignment() != m_Alignment) && ((*iter)->getAlignment() != -1))
  178. {
  179. m_Alignment = -1;
  180. break;
  181. }
  182. if ( (*iter)->getForestID() != -1 )
  183. {
  184. if ( forest == -1 )
  185. forest = (*iter)->getForestID();
  186. else if ( forest != (*iter)->getForestID() )
  187. bForests = false;
  188. }
  189. }
  190. if ( forest != -1 )
  191. {
  192. const Forest* pForest = EditorObjectMgr::instance()->getForest( forest );
  193. if ( pForest )
  194. {
  195. m_forestName = pForest->getFileName();
  196. }
  197. }
  198. EditorObjectMgr* pMgr = EditorObjectMgr::instance();
  199. int groupCount = pMgr->getBuildingGroupCount();
  200. const char** pGroups = new const char*[groupCount];
  201. m_Group.ResetContent();
  202. pMgr->getBuildingGroupNames(pGroups, groupCount);
  203. int count = 0;
  204. for ( int i = 0; i < groupCount; ++i )
  205. {
  206. if ((4/*mech group*/ == i) || (6/*vehicle group*/ == i))
  207. {
  208. continue;
  209. }
  210. m_Group.AddString( pGroups[i] );
  211. m_Group.SetItemData( count, (DWORD)i );
  212. count += 1;
  213. }
  214. delete [] pGroups;
  215. // make sure all the units we are editing are in the same group
  216. int group = units.GetHead()->getGroup();
  217. for ( iter = units.Begin(); !iter.IsDone(); iter++ )
  218. {
  219. if ( (*iter)->getGroup() != group )
  220. {
  221. group = -1;
  222. break;
  223. }
  224. }
  225. if ( group != -1 ) // we found a valid group
  226. {
  227. const char* pGroupName = pMgr->getGroupName( group );
  228. int index = m_Group.FindString( -1, pGroupName );
  229. m_Group.SetCurSel( index );
  230. // OK, now fill in the index....
  231. const char* MechNames[256];
  232. int count = 256;
  233. m_Mech.ResetContent();
  234. pMgr->getBuildingNamesInGroup( group, MechNames, count );
  235. for ( int i = 0; i < count; ++i )
  236. {
  237. m_Mech.AddString( MechNames[i] );
  238. }
  239. // ok, now determine if all of the mechs are the same.
  240. int indexInGroup = units.GetHead()->getIndexInGroup();
  241. for ( iter = units.Begin(); !iter.IsDone(); iter++ )
  242. {
  243. if ( (*iter)->getIndexInGroup() != indexInGroup )
  244. {
  245. indexInGroup = -1;
  246. break;
  247. }
  248. }
  249. if ( indexInGroup != -1 )
  250. {
  251. const char* pName = units.GetHead()->getDisplayName();
  252. index = m_Mech.FindString( -1, pName );
  253. if ( index != -1 )
  254. {
  255. m_Mech.SetCurSel( index );
  256. }
  257. }
  258. }
  259. m_x = pEditorObject->getPosition().x;
  260. m_y = pEditorObject->getPosition().y;
  261. long row, column;
  262. pEditorObject->getCells(row, column);
  263. m_partID = MIN_TERRAIN_PART_ID + row * MAX_MAP_CELL_WIDTH + column;
  264. UpdateData( false );
  265. }
  266. void BuildingSettingsDlg::OnCancel()
  267. {
  268. pAction->undo();
  269. delete pAction;
  270. pAction = NULL;
  271. CDialog::OnCancel();
  272. }