CampaignDialog.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. // CampaignDialog.cpp : implementation file
  5. //
  6. #include "stdafx.h"
  7. #include "CampaignDialog.h"
  8. #include "CampaignData.h"
  9. #include "GroupDialog.h"
  10. #include "assert.h"
  11. #include "../MCLib/Utilities.h" /*for cLoadString*/
  12. #include "UserTextEdit.h"
  13. #include "../MCLib/Paths.h"
  14. #if 0 /*gos doesn't like this */
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. #endif /*gos doesn't like this */
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CCampaignDialog dialog
  23. CCampaignDialog::CCampaignDialog(CWnd* pParent /*=NULL*/)
  24. : CDialog(CCampaignDialog::IDD, pParent)
  25. {
  26. //{{AFX_DATA_INIT(CCampaignDialog)
  27. m_CBillsEdit = 0;
  28. m_FinalVideoEdit = _T("");
  29. m_NameEdit = _T("");
  30. m_NameResourceStringIDEdit = _T("");
  31. //}}AFX_DATA_INIT
  32. }
  33. void CCampaignDialog::DoDataExchange(CDataExchange* pDX)
  34. {
  35. CDialog::DoDataExchange(pDX);
  36. //{{AFX_DATA_MAP(CCampaignDialog)
  37. DDX_Control(pDX, IDC_CA_GROUP_LIST, m_GroupListControl);
  38. DDX_Text(pDX, IDC_CA_CBILLS_EDIT, m_CBillsEdit);
  39. DDX_Text(pDX, IDC_CA_FINAL_VIDEO_EDIT, m_FinalVideoEdit);
  40. DDX_Text(pDX, IDC_CA_NAME_EDIT, m_NameEdit);
  41. DDX_Text(pDX, IDC_CA_NAME_RESOURCE_STRING_ID_EDIT, m_NameResourceStringIDEdit);
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CCampaignDialog, CDialog)
  45. //{{AFX_MSG_MAP(CCampaignDialog)
  46. ON_BN_CLICKED(IDC_CA_FINAL_VIDEO_BROWSE_BUTTON, OnCaFinalVideoBrowseButton)
  47. ON_BN_CLICKED(ID_CA_EXIT, OnCaExit)
  48. ON_BN_CLICKED(IDC_CA_ADD_BUTTON, OnCaAddButton)
  49. ON_BN_CLICKED(ID_CA_SAVE_AS_BUTTON, OnCaSaveAsButton)
  50. ON_BN_CLICKED(ID_CA_LOAD_BUTTON, OnCaLoadButton)
  51. ON_BN_CLICKED(IDC_CA_EDIT_BUTTON, OnCaEditButton)
  52. ON_BN_CLICKED(IDC_CA_REMOVE_BUTTON, OnCaRemoveButton)
  53. ON_BN_CLICKED(IDC_CA_MOVE_UP_BUTTON, OnCaMoveUpButton)
  54. ON_BN_CLICKED(IDC_CA_MOVE_DOWN_BUTTON, OnCaMoveDownButton)
  55. ON_BN_CLICKED(IDC_CA_NAME_EDIT_BUTTON, OnCaNameEditButton)
  56. ON_BN_CLICKED(ID_CA_SAVE_BUTTON, OnCaSaveButton)
  57. ON_WM_CLOSE()
  58. //}}AFX_MSG_MAP
  59. END_MESSAGE_MAP()
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CCampaignDialog message handlers
  62. void CCampaignDialog::OnCaFinalVideoBrowseButton()
  63. {
  64. UpdateData(TRUE);
  65. while (true) {
  66. CFileDialog selectFileDialog(TRUE,_T("AVI"),_T("*.AVI"),
  67. OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR,
  68. _T("Movie (*.AVI)|*.AVI|"));
  69. selectFileDialog.m_ofn.lpstrInitialDir = moviePath;
  70. if( selectFileDialog.DoModal()==IDOK ) {
  71. m_FinalVideoEdit = selectFileDialog.GetFileTitle();
  72. break;
  73. } else {
  74. break;
  75. }
  76. }
  77. UpdateData(FALSE);
  78. }
  79. void CCampaignDialog::OnCaExit()
  80. {
  81. int res = PromptAndSaveIfNecessary();
  82. if (IDCANCEL != res) {
  83. EndDialog(IDOK);
  84. }
  85. }
  86. static void setGroupListBoxValues(CListBox &GroupListBox, const CGroupList &GroupList) {
  87. GroupListBox.ResetContent();
  88. CGroupList::EConstIterator it;
  89. for (it = GroupList.Begin(); !it.IsDone(); it++) {
  90. CString tmpCStr;
  91. tmpCStr = _TEXT("[");
  92. tmpCStr += ((*it).m_Label);
  93. tmpCStr += _TEXT("] ");
  94. GroupListBox.AddString(tmpCStr);
  95. }
  96. }
  97. static void setGroupDialogValues(CGroupDialog &groupDialog, const CGroupData &groupData) {
  98. groupDialog.m_LabelEdit = groupData.m_Label;
  99. groupDialog.m_NumMissionsToCompleteEdit = groupData.m_NumMissionsToComplete;
  100. groupDialog.m_OperationFileEdit = groupData.m_OperationFile;
  101. groupDialog.m_PreVideoFileEdit = groupData.m_PreVideoFile;
  102. groupDialog.m_VideoFileEdit = groupData.m_VideoFile;
  103. groupDialog.m_MissionList = groupData.m_MissionList;
  104. groupDialog.m_TuneNumber = groupData.m_TuneNumber;
  105. }
  106. static void setGroupDataValues(CGroupData &groupData, const CGroupDialog &groupDialog) {
  107. groupData.m_Label = groupDialog.m_LabelEdit;
  108. groupData.m_NumMissionsToComplete = groupDialog.m_NumMissionsToCompleteEdit;
  109. groupData.m_OperationFile = groupDialog.m_OperationFileEdit;
  110. groupData.m_PreVideoFile = groupDialog.m_PreVideoFileEdit;
  111. groupData.m_VideoFile = groupDialog.m_VideoFileEdit;
  112. groupData.m_MissionList = groupDialog.m_MissionList;
  113. groupData.m_TuneNumber = groupDialog.m_TuneNumber;
  114. }
  115. void CCampaignDialog::OnCaAddButton()
  116. {
  117. CGroupData groupData;
  118. CGroupDialog groupDialog;
  119. setGroupDialogValues(groupDialog, groupData);
  120. int ret = groupDialog.DoModal();
  121. if (IDOK == ret) {
  122. setGroupDataValues(groupData, groupDialog);
  123. m_CampaignData.m_GroupList.Append(groupData);
  124. UpdateData(TRUE);
  125. setGroupListBoxValues(m_GroupListControl, m_CampaignData.m_GroupList);
  126. m_GroupListControl.SetCurSel(m_GroupListControl.GetCount() - 1);
  127. UpdateData(FALSE);
  128. }
  129. }
  130. static void setCampaignDialogValues(CCampaignDialog &campaignDialog, const CCampaignData &campaignData) {
  131. campaignDialog.m_NameUnlocalizedText = campaignData.m_Name;
  132. campaignDialog.m_NameUseResourceString = campaignData.m_NameUseResourceString;
  133. campaignDialog.m_NameResourceStringID = campaignData.m_NameResourceStringID;
  134. campaignDialog.m_CBillsEdit = campaignData.m_CBills;
  135. campaignDialog.m_FinalVideoEdit = campaignData.m_FinalVideo;
  136. setGroupListBoxValues(campaignDialog.m_GroupListControl, campaignData.m_GroupList);
  137. campaignDialog.UpdateData(FALSE);
  138. }
  139. static void setCampaignDataValues(CCampaignData &campaignData, CCampaignDialog &campaignDialog) {
  140. campaignDialog.UpdateData();
  141. campaignData.m_Name = campaignDialog.m_NameUnlocalizedText;
  142. campaignData.m_NameUseResourceString = campaignDialog.m_NameUseResourceString;
  143. campaignData.m_NameResourceStringID = campaignDialog.m_NameResourceStringID;
  144. campaignData.m_CBills = campaignDialog.m_CBillsEdit;
  145. campaignData.m_FinalVideo = campaignDialog.m_FinalVideoEdit;
  146. }
  147. int CCampaignDialog::SaveAs()
  148. {
  149. CFileDialog selectFileDialog(FALSE,_T("FIT"),_T("*.FIT"),
  150. OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR,
  151. _T("Campaign File (*.FIT)|*.FIT|"));
  152. {
  153. /* if the mission directory doesn't exist, we attempt to create it */
  154. int curDirStrSize = GetCurrentDirectory(0, NULL);
  155. TCHAR *curDirStr = new TCHAR[curDirStrSize];
  156. GetCurrentDirectory(curDirStrSize, curDirStr);
  157. BOOL result = SetCurrentDirectory(campaignPath);
  158. SetCurrentDirectory(curDirStr);
  159. delete curDirStr; curDirStr = 0;
  160. if (0 == result) {
  161. CreateDirectory(campaignPath, NULL);
  162. }
  163. }
  164. selectFileDialog.m_ofn.lpstrInitialDir = campaignPath;
  165. int retval = selectFileDialog.DoModal();
  166. if( IDOK == retval ) {
  167. setCampaignDataValues(m_CampaignData, *this);
  168. m_CampaignData.m_PathName = selectFileDialog.GetPathName();
  169. m_CampaignData.Save(m_CampaignData.m_PathName);
  170. m_LastSavedCampaignData = m_CampaignData;
  171. }
  172. return retval;
  173. }
  174. void CCampaignDialog::OnCaSaveAsButton()
  175. {
  176. SaveAs();
  177. }
  178. void CCampaignDialog::OnCaLoadButton()
  179. {
  180. int res = PromptAndSaveIfNecessary();
  181. if (IDCANCEL == res) {
  182. return;
  183. }
  184. CFileDialog selectFileDialog(TRUE,_T("FIT"),_T("*.FIT"),
  185. OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR,
  186. _T("Campaign File (*.FIT)|*.FIT|"));
  187. selectFileDialog.m_ofn.lpstrInitialDir = campaignPath;
  188. if( selectFileDialog.DoModal()==IDOK ) {
  189. CString pathname = selectFileDialog.GetPathName();
  190. m_CampaignData.Clear();
  191. m_CampaignData.Read(pathname);
  192. m_CampaignData.m_PathName = pathname;
  193. m_LastSavedCampaignData = m_CampaignData;
  194. setCampaignDialogValues(*this, m_CampaignData);
  195. UpdateData(FALSE);
  196. UpdateNameDisplay();
  197. }
  198. }
  199. void CCampaignDialog::OnCaEditButton()
  200. {
  201. int selectedItemIndex = m_GroupListControl.GetCurSel();
  202. if ((0 <= selectedItemIndex) && ((int)m_CampaignData.m_GroupList.Count() > selectedItemIndex)) {
  203. CGroupData &groupDataRef = m_CampaignData.m_GroupList[selectedItemIndex];
  204. CGroupDialog groupDialog;
  205. setGroupDialogValues(groupDialog, groupDataRef);
  206. if (IDOK == groupDialog.DoModal()) {
  207. setGroupDataValues(groupDataRef, groupDialog);
  208. UpdateData(TRUE);
  209. setGroupListBoxValues(m_GroupListControl, m_CampaignData.m_GroupList);
  210. UpdateData(FALSE);
  211. }
  212. }
  213. }
  214. void CCampaignDialog::OnCaRemoveButton()
  215. {
  216. int selectedItemIndex = m_GroupListControl.GetCurSel();
  217. if ((0 <= selectedItemIndex) && ((int)m_CampaignData.m_GroupList.Count() > selectedItemIndex)) {
  218. // should put up confirmation box here
  219. CGroupList::EIterator it = m_CampaignData.m_GroupList.Begin();
  220. int index;
  221. for (index = 0; index < selectedItemIndex; index++) {
  222. it++;
  223. assert(!it.IsDone());
  224. }
  225. m_CampaignData.m_GroupList.Delete(it);
  226. UpdateData(TRUE);
  227. int selectedItemIndex = m_GroupListControl.GetCurSel();
  228. setGroupListBoxValues(m_GroupListControl, m_CampaignData.m_GroupList);
  229. if ((LB_ERR != selectedItemIndex) && (0 < m_GroupListControl.GetCount())) {
  230. if (m_GroupListControl.GetCount() <= (long)selectedItemIndex) {
  231. selectedItemIndex = m_GroupListControl.GetCount() - 1;
  232. }
  233. m_GroupListControl.SetCurSel(selectedItemIndex);
  234. }
  235. UpdateData(FALSE);
  236. }
  237. }
  238. void CCampaignDialog::OnCaMoveUpButton()
  239. {
  240. int selectedItemIndex = m_GroupListControl.GetCurSel();
  241. if ((1 <= selectedItemIndex) && ((int)m_CampaignData.m_GroupList.Count() > selectedItemIndex)) {
  242. CGroupData tmpGroupData;
  243. tmpGroupData = (*m_CampaignData.m_GroupList.Iterator(selectedItemIndex));
  244. m_CampaignData.m_GroupList.Delete(selectedItemIndex);
  245. m_CampaignData.m_GroupList.Insert(tmpGroupData, selectedItemIndex - 1);
  246. UpdateData(TRUE);
  247. setGroupListBoxValues(m_GroupListControl, m_CampaignData.m_GroupList);
  248. m_GroupListControl.SetCurSel(selectedItemIndex - 1);
  249. UpdateData(FALSE);
  250. }
  251. }
  252. void CCampaignDialog::OnCaMoveDownButton()
  253. {
  254. int selectedItemIndex = m_GroupListControl.GetCurSel();
  255. if ((0 <= selectedItemIndex) && ((int)m_CampaignData.m_GroupList.Count() - 1 > selectedItemIndex)) {
  256. CGroupData tmpGroupData;
  257. tmpGroupData = (*m_CampaignData.m_GroupList.Iterator(selectedItemIndex));
  258. m_CampaignData.m_GroupList.Delete(selectedItemIndex);
  259. if ((int)m_CampaignData.m_GroupList.Count() -1 == selectedItemIndex) {
  260. m_CampaignData.m_GroupList.Append(tmpGroupData);
  261. } else {
  262. m_CampaignData.m_GroupList.Insert(tmpGroupData, selectedItemIndex + 1);
  263. }
  264. UpdateData(TRUE);
  265. setGroupListBoxValues(m_GroupListControl, m_CampaignData.m_GroupList);
  266. m_GroupListControl.SetCurSel(selectedItemIndex + 1);
  267. UpdateData(FALSE);
  268. }
  269. }
  270. static BOOL CSLoadString(int resourceID, CString &targetStr) {
  271. char szTmp[16384/*max string length*/];
  272. cLoadString( resourceID, szTmp, 16384/*max string length*/ );
  273. targetStr = szTmp;
  274. CString tmpStr;
  275. tmpStr.Format("mc2res.dll:%d Not defined", resourceID);
  276. if (0 == strcmp(tmpStr.GetBuffer(0), szTmp)) {
  277. return (0);
  278. }
  279. return (!0);
  280. }
  281. void CCampaignDialog::UpdateNameDisplay() {
  282. UpdateData(TRUE);
  283. if (m_NameUseResourceString) {
  284. m_NameResourceStringIDEdit.Format("%d", m_NameResourceStringID);
  285. int ret = CSLoadString(m_NameResourceStringID, m_NameEdit);
  286. if (0 == ret) {
  287. m_NameEdit = _TEXT("");
  288. }
  289. } else {
  290. m_NameResourceStringIDEdit = _TEXT("");
  291. m_NameEdit = m_NameUnlocalizedText;
  292. }
  293. UpdateData(FALSE);
  294. }
  295. void CCampaignDialog::OnCaNameEditButton()
  296. {
  297. CUserTextEdit userTextEditDialog;
  298. userTextEditDialog.m_UnlocalizedText = m_NameUnlocalizedText;
  299. userTextEditDialog.m_UseResourceString = m_NameUseResourceString;
  300. userTextEditDialog.m_ResourceStringID = m_NameResourceStringID;
  301. int ret = userTextEditDialog.DoModal();
  302. if (IDOK == ret) {
  303. m_NameUnlocalizedText = userTextEditDialog.m_UnlocalizedText;
  304. m_NameUseResourceString = userTextEditDialog.m_UseResourceString;
  305. m_NameResourceStringID = userTextEditDialog.m_ResourceStringID;
  306. UpdateNameDisplay();
  307. }
  308. }
  309. int CCampaignDialog::Save()
  310. {
  311. int retVal = IDOK;
  312. if (m_CampaignData.m_PathName.IsEmpty()) {
  313. retVal = SaveAs();
  314. } else {
  315. m_CampaignData.Save(m_CampaignData.m_PathName);
  316. m_LastSavedCampaignData = m_CampaignData;
  317. retVal = IDOK;
  318. }
  319. return retVal;
  320. }
  321. void CCampaignDialog::OnCaSaveButton()
  322. {
  323. Save();
  324. }
  325. int CCampaignDialog::PromptAndSaveIfNecessary()
  326. {
  327. setCampaignDataValues(m_CampaignData, *this);
  328. int res = IDNO;
  329. bool endFlag = false;
  330. while (!endFlag) {
  331. endFlag = true;
  332. if (!(m_LastSavedCampaignData == m_CampaignData)) {
  333. res = AfxMessageBox(IDS_DO_YOU_WANT_TO_SAVE_YOUR_CHANGES, MB_YESNOCANCEL);
  334. }
  335. if (IDYES == res) {
  336. int saveRes = Save();
  337. if (IDCANCEL == saveRes) {
  338. endFlag = false;
  339. }
  340. }
  341. }
  342. return res;
  343. }
  344. BOOL CCampaignDialog::OnInitDialog()
  345. {
  346. CDialog::OnInitDialog();
  347. setCampaignDialogValues(*this, m_CampaignData);
  348. // TODO: Add extra initialization here
  349. return TRUE; // return TRUE unless you set the focus to a control
  350. // EXCEPTION: OCX Property Pages should return FALSE
  351. }
  352. void CCampaignDialog::OnClose()
  353. {
  354. int res = PromptAndSaveIfNecessary();
  355. if (IDCANCEL != res) {
  356. CDialog::OnClose();
  357. }
  358. }