Debian Codegraph @

editor/ScriptSelect.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
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 // ScriptSelect.cpp : implementation file
20 //
21
22 #include "stdafx.h"
23 #include "editor.h"
24
25 #include "ScriptSelect.h"
26 #include "AddScriptDialog.h"
27 #include "ScriptStudio.h"
28
29 #include "pserror.h"
30 #include "d3x.h"
31 #include "ObjCScript.h"
32 #include "ScriptWizard.h"
33 #include "ScriptParmDialog.h"
34
35 #ifdef _DEBUG
36 #define new DEBUG_NEW
37 #undef THIS_FILE
38 static char THIS_FILE[] = __FILE__;
39 #endif
40
41 /////////////////////////////////////////////////////////////////////////////
42 // CScriptSelect dialog
43
44 CScriptSelect::CScriptSelect(uint16_t scr_type, int custom_type, CWnd *pParent /*=NULL*/)
45 : CDialog(CScriptSelect::IDD, pParent) {
46 //{{AFX_DATA_INIT(CScriptSelect)
47 m_ScriptName = _T("");
48 //}}AFX_DATA_INIT
49
50 m_ScriptCode = NULL;
51 m_CurScriptSel = -1;
52 m_ScriptType = (uint16_t)scr_type;
53 m_NumParms = 0;
54 m_CustomType = custom_type;
55 }
56
57 void CScriptSelect::DoDataExchange(CDataExchange *pDX) {
58 CDialog::DoDataExchange(pDX);
59 //{{AFX_DATA_MAP(CScriptSelect)
60 DDX_LBString(pDX, IDC_SCRIPTSEL_LISTBOX, m_ScriptName);
61 //}}AFX_DATA_MAP
62 }
63
64 BEGIN_MESSAGE_MAP(CScriptSelect, CDialog)
65 //{{AFX_MSG_MAP(CScriptSelect)
66 ON_LBN_DBLCLK(IDC_SCRIPTSEL_LISTBOX, OnDblclkScriptselListbox)
67 ON_WM_DESTROY()
68 ON_BN_CLICKED(IDC_PARAMETERS, OnParameters)
69 ON_LBN_SELCHANGE(IDC_SCRIPTSEL_LISTBOX, OnSelchangeScriptselListbox)
70 ON_BN_CLICKED(IDC_SCRIPTSEL_ADD, OnScriptselAdd)
71 ON_BN_CLICKED(IDC_EDITSCRIPT, OnEditScript)
72 ON_BN_CLICKED(IDC_SCRTYPE, OnScrtype)
73 //}}AFX_MSG_MAP
74 END_MESSAGE_MAP()
75
76 /////////////////////////////////////////////////////////////////////////////
77 // CScriptSelect message handlers
78
79 BOOL CScriptSelect::OnInitDialog() {
80 CListBox *scrlistbox = (CListBox *)GetDlgItem(IDC_SCRIPTSEL_LISTBOX);
81
82 CDialog::OnInitDialog();
83
84 // update names of scripts into listbox
85 // m_ScriptCode = D3XReallocProgram(NULL, 0, 0, 0); LGT- undefined
86 UpdateScriptListbox();
87 if (m_ScriptName.IsEmpty())
88 scrlistbox->SelectString(-1, "null");
89 else
90 scrlistbox->SelectString(-1, m_ScriptName);
91 OnSelchangeScriptselListbox();
92
93 // update script list selector
94 CButton *btn = (CButton *)GetDlgItem(IDC_SCRTYPE);
95
96 if (m_CustomType & DEFAULT_SCRIPT_MASK) {
97 btn->SetWindowText("Custom List");
98 } else if (m_CustomType & CUSTOM_SCRIPT_MASK) {
99 btn->SetWindowText("Default List");
100 } else {
101 btn->EnableWindow(FALSE);
102 }
103
104 return TRUE; // return TRUE unless you set the focus to a control
105 // EXCEPTION: OCX Property Pages should return FALSE
106 }
107
108 void CScriptSelect::OnDestroy() {
109 CDialog::OnDestroy();
110
111 // if (m_ScriptCode) D3XFreeProgram(m_ScriptCode); LGT- undefined
112 }
113
114 //////////////////////////////////////////////////////////////////////////////
115 // CONTROL HANDLERS
116
117 void CScriptSelect::OnOK() {
118 // DDX takes care of returning the script name selected
119
120 CDialog::OnOK();
121 }
122
123 void CScriptSelect::OnDblclkScriptselListbox() { OnOK(); }
124
125 //@@void CScriptSelect::OnScriptselScriptWiz()
126 //@@{
127 //@@ CListBox *scrlistbox = (CListBox *)GetDlgItem(IDC_SCRIPTSEL_LISTBOX);
128 //@@ CString str;
129 //@@ int sel;
130 //@@
131 //@@ sel = scrlistbox->GetCurSel();
132 //@@ if (sel != LB_ERR) {
133 //@@ scrlistbox->GetText(sel, str);
134 //@@ }
135 //@@
136 //@@ CScriptWizard dlg;
137 //@@ dlg.DoModal();
138 //@@ UpdateScriptListbox();
139 //@@
140 //@@ if (sel != LB_ERR)
141 //@@ scrlistbox->SelectString(-1, str);
142 //@@}
143
144 void CScriptSelect::OnSelchangeScriptselListbox() {
145 CListBox *scrlistbox = (CListBox *)GetDlgItem(IDC_SCRIPTSEL_LISTBOX);
146 CButton *btn = (CButton *)GetDlgItem(IDC_PARAMETERS);
147 CButton *btn2 = (CButton *)GetDlgItem(IDC_EDITSCRIPT);
148 CString str;
149 int sel, id;
150
151 sel = scrlistbox->GetCurSel();
152 if (sel != LB_ERR) {
153 scrlistbox->GetText(sel, str);
154 id = FindScriptIDFromName(str);
155 if (id > -1) {
156 btn2->EnableWindow(TRUE);
157 m_NumParms = Script_names[id].nparms;
158 if (Script_names[id].nparms) {
159 btn->EnableWindow(TRUE);
160 return;
161 }
162 } else {
163 m_NumParms = 0;
164 btn2->EnableWindow(FALSE);
165 }
166 }
167 btn->EnableWindow(FALSE);
168 }
169
170 void CScriptSelect::OnParameters() {
171 // search list of script names for match with listbox. Then use this index
172 // to look in the m_SciptCode program to see if that script has parameters
173 CListBox *scrlistbox = (CListBox *)GetDlgItem(IDC_SCRIPTSEL_LISTBOX);
174 CString str;
175 int sel, id;
176
177 sel = scrlistbox->GetCurSel();
178 if (sel != LB_ERR) {
179 scrlistbox->GetText(sel, str);
180 id = FindScriptIDFromName(str);
181 if (id > -1) {
182 CScriptParmDialog dlg;
183
184 // grab parameter information.
185 SETUP_SCRIPTPARM_DIALOG(&dlg, Script_names[id].parameters, m_Parm);
186
187 if (dlg.DoModal() == IDOK) {
188 RETURN_SCRIPTPARM_DIALOG(m_Parm, &dlg, Script_names[id].paramtypes);
189 }
190 } else {
191 Int3();
192 }
193 }
194 }
195
196 //////////////////////////////////////////////////////////////////////////////
197 // INTERNAL FUNCTIONS
198
199 void CScriptSelect::UpdateScriptListbox() {
200 CListBox *scrlistbox = (CListBox *)GetDlgItem(IDC_SCRIPTSEL_LISTBOX);
201 int i;
202
203 GenerateScriptListFromAllFiles(m_CustomType);
204
205 // Add script names to list box
206 scrlistbox->ResetContent();
207 for (i = 0; i < MAX_SCRIPTS; i++) {
208 if (Script_names[i].used)
209 if (m_ScriptType == 0xffff || Script_names[i].type == m_ScriptType)
210 if ((Script_names[i].iscustom && (m_CustomType & CUSTOM_SCRIPT_MASK)) ||
211 (!Script_names[i].iscustom && (m_CustomType & DEFAULT_SCRIPT_MASK)))
212 scrlistbox->AddString(Script_names[i].name);
213 }
214
215 scrlistbox->AddString("null");
216
217 // make sure we reselect the current string.
218 if (scrlistbox->GetCount() > 0)
219 scrlistbox->SetCurSel(0);
220 }
221
222 void CScriptSelect::OnScriptselAdd() {
223 CAddScriptDialog dlg(this);
224
225 // in this case, load the script source file level.scr and add to it.
226 if (dlg.DoModal() == IDOK) {
227 char *source;
228
229 source = LoadScript(LEVEL_SCRIPT_NAME);
230 if (!source) {
231 OutrageMessageBox("Unable to find script for level. Something is wrong!");
232 return;
233 }
234 source = AddScriptBlockToScript(source, (LPCSTR)dlg.m_Name, (LPCSTR)dlg.m_TypeName);
235 SaveScript(LEVEL_SCRIPT_NAME, source);
236 FreeScript(source);
237 EditScript(LEVEL_SCRIPT_NAME, dlg.m_Name);
238 }
239 }
240
241 void CScriptSelect::OnEditScript() {
242 CListBox *scrlistbox = (CListBox *)GetDlgItem(IDC_SCRIPTSEL_LISTBOX);
243 CString str;
244 int sel, id;
245
246 sel = scrlistbox->GetCurSel();
247 if (sel != LB_ERR) {
248 scrlistbox->GetText(sel, str);
249 id = FindScriptIDFromName(str);
250 if (id > -1)
251 EditScript(Script_names[id].iscustom ? LEVEL_SCRIPT_NAME : DEFAULT_SCRIPT_NAME, str);
252 }
253 }
254
255 void CScriptSelect::EditScript(const char *filename, const char *scrname) {
256 if (InvokeScriptStudio(filename, scrname))
257 UpdateScriptListbox();
258 }
259
260 void CScriptSelect::OnScrtype() {
261 CListBox *scrlistbox = (CListBox *)GetDlgItem(IDC_SCRIPTSEL_LISTBOX);
262 CButton *btn = (CButton *)GetDlgItem(IDC_SCRTYPE);
263
264 if (m_CustomType & DEFAULT_SCRIPT_MASK) {
265 m_CustomType = CUSTOM_SCRIPT_MASK;
266 btn->SetWindowText("Default List");
267 } else if (m_CustomType & CUSTOM_SCRIPT_MASK) {
268 m_CustomType = DEFAULT_SCRIPT_MASK;
269 btn->SetWindowText("Custom List");
270 }
271
272 UpdateScriptListbox();
273
274 if (m_ScriptName.IsEmpty())
275 scrlistbox->SelectString(-1, "null");
276 else
277 scrlistbox->SelectString(-1, m_ScriptName);
278 }