ScriptEditDialog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. // ScriptEditDialog.cpp : implementation file
  19. //
  20. #include "stdafx.h"
  21. #include "leveledit.h"
  22. #include "ScriptEditDialog.h"
  23. #include "ScriptMgr.h"
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29. /////////////////////////////////////////////////////////////////////////////
  30. //
  31. // ScriptEditDialogClass
  32. //
  33. /////////////////////////////////////////////////////////////////////////////
  34. ScriptEditDialogClass::ScriptEditDialogClass(CWnd* pParent /*=NULL*/)
  35. : m_CurrentParamType (PARAM_TYPE_STRING),
  36. m_CurrentParamIndex (0),
  37. m_ValueVector3Dlg (NULL),
  38. CDialog(ScriptEditDialogClass::IDD, pParent)
  39. {
  40. //{{AFX_DATA_INIT(ScriptEditDialogClass)
  41. // NOTE: the ClassWizard will add member initialization here
  42. //}}AFX_DATA_INIT
  43. return ;
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. //
  47. // ~ScriptEditDialogClass
  48. //
  49. /////////////////////////////////////////////////////////////////////////////
  50. ScriptEditDialogClass::~ScriptEditDialogClass (void)
  51. {
  52. SAFE_DELETE (m_ValueVector3Dlg);
  53. return ;
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. //
  57. // DoDataExchange
  58. //
  59. /////////////////////////////////////////////////////////////////////////////
  60. void
  61. ScriptEditDialogClass::DoDataExchange (CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(ScriptEditDialogClass)
  65. DDX_Control(pDX, IDC_PARAM_VALUE_STRING_EDIT, m_ValueStringEdit);
  66. DDX_Control(pDX, IDC_PARAM_VALUE_NUMBER_SPIN, m_ValueNumberSpin);
  67. DDX_Control(pDX, IDC_PARAM_VALUE_NUMBER_EDIT, m_ValueNumberEdit);
  68. DDX_Control(pDX, IDC_PARAM_VALUE_LIST_COMBO, m_ValueListCombo);
  69. DDX_Control(pDX, IDC_PARAM_VALUE_BOOL_CHECK, m_ValueBoolCheck);
  70. //}}AFX_DATA_MAP
  71. }
  72. BEGIN_MESSAGE_MAP(ScriptEditDialogClass, CDialog)
  73. //{{AFX_MSG_MAP(ScriptEditDialogClass)
  74. ON_CBN_SELCHANGE(IDC_PARAM_NAME_COMBO, OnSelChangeParamNameCombo)
  75. ON_CBN_SELCHANGE(IDC_SCRIPT_NAME, OnSelChangeScriptName)
  76. ON_NOTIFY(UDN_DELTAPOS, IDC_PARAM_VALUE_NUMBER_SPIN, OnDeltaposParamValueNumberSpin)
  77. //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()
  79. /////////////////////////////////////////////////////////////////////////////
  80. //
  81. // OnInitDialog
  82. //
  83. /////////////////////////////////////////////////////////////////////////////
  84. BOOL
  85. ScriptEditDialogClass::OnInitDialog (void)
  86. {
  87. CDialog::OnInitDialog();
  88. //
  89. // Add the total list of scripts to the combobox
  90. //
  91. for (int index = 0; index < ScriptMgrClass::Get_Count (); index ++) {
  92. EditScriptClass *script = ScriptMgrClass::Get_Script (index);
  93. if (script != NULL) {
  94. //
  95. // Add this script to the combobox
  96. //
  97. int item_index = SendDlgItemMessage (IDC_SCRIPT_NAME, CB_ADDSTRING, 0, (LPARAM)script->Get_Name ());
  98. SendDlgItemMessage (IDC_SCRIPT_NAME, CB_SETITEMDATA, (WPARAM)item_index, (LPARAM)script);
  99. //
  100. // Select this script if it is the default
  101. //
  102. if (::lstrcmpi (m_Script.Get_Name (), script->Get_Name ()) == 0) {
  103. SendDlgItemMessage (IDC_SCRIPT_NAME, CB_SETCURSEL, (WPARAM)item_index);
  104. }
  105. }
  106. }
  107. //
  108. // Reposition the hidden controls so they occupy the same area as
  109. // the default 'value' control.
  110. //
  111. CRect rect;
  112. m_ValueStringEdit.GetWindowRect (&rect);
  113. ScreenToClient (&rect);
  114. m_ValueNumberEdit.SetWindowPos (NULL, rect.left, rect.top, rect.Width (), rect.Height (), SWP_NOZORDER);
  115. m_ValueListCombo.SetWindowPos (NULL, rect.left, rect.top, rect.Width (), rect.Height () * 10, SWP_NOZORDER);
  116. m_ValueBoolCheck.SetWindowPos (NULL, rect.left, rect.top, rect.Width (), rect.Height (), SWP_NOZORDER);
  117. m_ValueNumberSpin.SetRange (-1000000, 1000000);
  118. m_ValueNumberSpin.SetBuddy (&m_ValueNumberEdit);
  119. //
  120. // Create the 'Vector3' dialog control
  121. //
  122. m_ValueVector3Dlg = new Vector3DialogClass;
  123. m_ValueVector3Dlg->Create (this);
  124. CRect v3_rect;
  125. m_ValueVector3Dlg->GetWindowRect (&v3_rect);
  126. m_ValueVector3Dlg->ShowWindow (SW_HIDE);
  127. m_ValueVector3Dlg->SetWindowPos (&m_ValueNumberEdit, rect.left, (rect.top + rect.Height () / 2) - (v3_rect.Height () / 2), 0, 0, SWP_NOSIZE);
  128. Fill_Param_Combo ();
  129. Switch_Value_Control_Type ();
  130. return TRUE;
  131. }
  132. /////////////////////////////////////////////////////////////////////////////
  133. //
  134. // Fill_Param_Combo
  135. //
  136. /////////////////////////////////////////////////////////////////////////////
  137. void
  138. ScriptEditDialogClass::Fill_Param_Combo (void)
  139. {
  140. // Clear the contents of the param combobox
  141. SendDlgItemMessage (IDC_PARAM_NAME_COMBO, CB_RESETCONTENT);
  142. // Add the param names to the combobox
  143. for (int index = 0; index < m_Script.Get_Param_Count (); index ++) {
  144. CString name = m_Script.Get_Param_Name (index);
  145. SendDlgItemMessage (IDC_PARAM_NAME_COMBO, CB_ADDSTRING, (WPARAM)0, (LPARAM)(LPCTSTR)name);
  146. }
  147. // Select the first parameter in the list by default
  148. SendDlgItemMessage (IDC_PARAM_NAME_COMBO, CB_SETCURSEL, (WPARAM)0);
  149. return ;
  150. }
  151. /////////////////////////////////////////////////////////////////////////////
  152. //
  153. // Switch_Value_Control_Type
  154. //
  155. /////////////////////////////////////////////////////////////////////////////
  156. void
  157. ScriptEditDialogClass::Switch_Value_Control_Type (void)
  158. {
  159. // Get the parameter index and use it to determine the new combo type
  160. int index = SendDlgItemMessage (IDC_PARAM_NAME_COMBO, CB_GETCURSEL);
  161. if (index != CB_ERR) {
  162. Switch_Value_Control_Type (index);
  163. }
  164. return ;
  165. }
  166. /////////////////////////////////////////////////////////////////////////////
  167. //
  168. // Switch_Value_Control_Type
  169. //
  170. /////////////////////////////////////////////////////////////////////////////
  171. void
  172. ScriptEditDialogClass::Switch_Value_Control_Type (int param_index)
  173. {
  174. // Is the new type different than the old one?
  175. PARAM_TYPES type = m_Script.Get_Param_Type (param_index);
  176. if (type != m_CurrentParamType) {
  177. //
  178. // What's the new type?
  179. //
  180. switch (type)
  181. {
  182. case PARAM_TYPE_FLOAT:
  183. case PARAM_TYPE_INT:
  184. {
  185. if (type == PARAM_TYPE_FLOAT) {
  186. ::Make_Edit_Float_Ctrl (m_ValueNumberEdit);
  187. } else {
  188. ::Make_Edit_Int_Ctrl (m_ValueNumberEdit);
  189. }
  190. m_ValueNumberEdit.ShowWindow (SW_SHOW);
  191. m_ValueNumberSpin.ShowWindow (SW_SHOW);
  192. }
  193. break;
  194. case PARAM_TYPE_BOOL:
  195. {
  196. m_ValueBoolCheck.ShowWindow (SW_SHOW);
  197. }
  198. break;
  199. case PARAM_TYPE_VECTOR3:
  200. {
  201. m_ValueVector3Dlg->ShowWindow (SW_SHOW);
  202. }
  203. break;
  204. case PARAM_TYPE_STRING:
  205. default:
  206. {
  207. m_ValueStringEdit.ShowWindow (SW_SHOW);
  208. }
  209. break;
  210. }
  211. //
  212. // Hide the old window
  213. //
  214. switch (m_CurrentParamType)
  215. {
  216. case PARAM_TYPE_FLOAT:
  217. case PARAM_TYPE_INT:
  218. {
  219. if ((type != PARAM_TYPE_FLOAT) &&
  220. (type != PARAM_TYPE_INT)) {
  221. m_ValueNumberEdit.ShowWindow (SW_HIDE);
  222. m_ValueNumberSpin.ShowWindow (SW_HIDE);
  223. }
  224. }
  225. break;
  226. case PARAM_TYPE_BOOL:
  227. {
  228. m_ValueBoolCheck.ShowWindow (SW_HIDE);
  229. }
  230. break;
  231. case PARAM_TYPE_VECTOR3:
  232. {
  233. m_ValueVector3Dlg->ShowWindow (SW_HIDE);
  234. }
  235. break;
  236. case PARAM_TYPE_STRING:
  237. default:
  238. {
  239. m_ValueStringEdit.ShowWindow (SW_HIDE);
  240. }
  241. break;
  242. }
  243. // Remember the new type
  244. m_CurrentParamType = type;
  245. }
  246. //
  247. // Make sure the value is correct
  248. //
  249. Fill_Value_Control (m_Script.Get_Param_Value (param_index));
  250. return ;
  251. }
  252. /////////////////////////////////////////////////////////////////////////////
  253. //
  254. // Fill_Value_Control
  255. //
  256. /////////////////////////////////////////////////////////////////////////////
  257. void
  258. ScriptEditDialogClass::Fill_Value_Control (LPCTSTR value)
  259. {
  260. // What's type?
  261. switch (m_CurrentParamType)
  262. {
  263. case PARAM_TYPE_FLOAT:
  264. {
  265. float float_value = ::atof (value);
  266. m_ValueNumberEdit.SetWindowText (value);
  267. m_ValueNumberSpin.SetPos (::atol (value));
  268. //m_ValueNumberSpin.SetPos (int(float_value * 100.00F));
  269. }
  270. break;
  271. case PARAM_TYPE_INT:
  272. {
  273. m_ValueNumberEdit.SetWindowText (value);
  274. m_ValueNumberSpin.SetPos (::atol (value));
  275. }
  276. break;
  277. case PARAM_TYPE_BOOL:
  278. {
  279. m_ValueBoolCheck.SetWindowText (m_Script.Get_Param_Name (m_CurrentParamIndex));
  280. m_ValueBoolCheck.SetCheck (::lstrcmpi (value, "true") == 0);
  281. }
  282. break;
  283. case PARAM_TYPE_VECTOR3:
  284. {
  285. //
  286. // Determine what the 3 components of the vector3 are
  287. //
  288. Vector3 new_value (0, 0, 0);
  289. ::sscanf (value, "%f %f %f", &new_value.X, &new_value.Y, &new_value.Z);
  290. //
  291. // Pass the new value onto the dialog
  292. //
  293. m_ValueVector3Dlg->Set_Default_Value (new_value);
  294. }
  295. break;
  296. case PARAM_TYPE_STRING:
  297. default:
  298. {
  299. m_ValueStringEdit.SetWindowText (value);
  300. }
  301. break;
  302. }
  303. return ;
  304. }
  305. /////////////////////////////////////////////////////////////////////////////
  306. //
  307. // Save_Current_Value
  308. //
  309. /////////////////////////////////////////////////////////////////////////////
  310. void
  311. ScriptEditDialogClass::Save_Current_Value (void)
  312. {
  313. // What's type?
  314. CString text_value;
  315. switch (m_CurrentParamType)
  316. {
  317. case PARAM_TYPE_FLOAT:
  318. {
  319. // Validate the float
  320. float float_value = ::GetDlgItemFloat (m_hWnd, IDC_PARAM_VALUE_NUMBER_EDIT, true);
  321. text_value.Format ("%.2f", float_value);
  322. }
  323. break;
  324. case PARAM_TYPE_INT:
  325. {
  326. // Validate the int
  327. int num_value = GetDlgItemInt (IDC_PARAM_VALUE_NUMBER_EDIT);
  328. text_value.Format ("%d", num_value);
  329. }
  330. break;
  331. case PARAM_TYPE_BOOL:
  332. {
  333. text_value = m_ValueBoolCheck.GetCheck () ? "true" : "false";
  334. }
  335. break;
  336. case PARAM_TYPE_VECTOR3:
  337. {
  338. //
  339. // Get the current value from the dialog and convert it to a string
  340. //
  341. Vector3 curr_value = m_ValueVector3Dlg->Get_Current_Value ();
  342. text_value.Format ("%.2f %.2f %.2f", curr_value.X, curr_value.Y, curr_value.Z);
  343. }
  344. break;
  345. case PARAM_TYPE_STRING:
  346. default:
  347. {
  348. m_ValueStringEdit.GetWindowText (text_value);
  349. }
  350. break;
  351. }
  352. // Pass the new value onto the property
  353. m_Script.Set_Param_Value (m_CurrentParamIndex, text_value);
  354. return ;
  355. }
  356. /////////////////////////////////////////////////////////////////////////////
  357. //
  358. // OnSelChangeParamNameCombo
  359. //
  360. /////////////////////////////////////////////////////////////////////////////
  361. void
  362. ScriptEditDialogClass::OnSelChangeParamNameCombo (void)
  363. {
  364. // Make sure we save the current value from the value control
  365. Save_Current_Value ();
  366. // Get the parameter index
  367. m_CurrentParamIndex = SendDlgItemMessage (IDC_PARAM_NAME_COMBO, CB_GETCURSEL);
  368. if (m_CurrentParamIndex != CB_ERR) {
  369. // Reset the dialog controls to reflect this parameter
  370. Switch_Value_Control_Type ();
  371. Fill_Value_Control (m_Script.Get_Param_Value (m_CurrentParamIndex));
  372. }
  373. return ;
  374. }
  375. /////////////////////////////////////////////////////////////////////////////
  376. //
  377. // OnSelChangeScriptName
  378. //
  379. /////////////////////////////////////////////////////////////////////////////
  380. void
  381. ScriptEditDialogClass::OnSelChangeScriptName (void)
  382. {
  383. // Get the current selection
  384. int index = SendDlgItemMessage (IDC_SCRIPT_NAME, CB_GETCURSEL);
  385. if (index != CB_ERR) {
  386. EditScriptClass *script = (EditScriptClass *)SendDlgItemMessage (IDC_SCRIPT_NAME, CB_GETITEMDATA, index);
  387. if (script != NULL) {
  388. m_Script = (*script);
  389. // Fill the parameter's combobox with param names
  390. Fill_Param_Combo ();
  391. Switch_Value_Control_Type (0);
  392. m_CurrentParamIndex = 0;
  393. }
  394. }
  395. return ;
  396. }
  397. /////////////////////////////////////////////////////////////////////////////
  398. //
  399. // OnDeltaposParamValueNumberSpin
  400. //
  401. /////////////////////////////////////////////////////////////////////////////
  402. void
  403. ScriptEditDialogClass::OnDeltaposParamValueNumberSpin
  404. (
  405. NMHDR *pNMHDR,
  406. LRESULT *pResult
  407. )
  408. {
  409. NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
  410. int value = GetDlgItemInt (IDC_PARAM_VALUE_NUMBER_EDIT);
  411. value += pNMUpDown->iDelta;
  412. SetDlgItemInt (IDC_PARAM_VALUE_NUMBER_EDIT, value);
  413. (*pResult) = 0;
  414. return ;
  415. }
  416. /////////////////////////////////////////////////////////////////////////////
  417. //
  418. // OnOK
  419. //
  420. /////////////////////////////////////////////////////////////////////////////
  421. void
  422. ScriptEditDialogClass::OnOK (void)
  423. {
  424. Save_Current_Value ();
  425. CDialog::OnOK ();
  426. return ;
  427. }