Debian Codegraph @

editor/ObjectListDialog.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 // ObjectListDialog.cpp : implementation file
20 //
21
22 #include "stdafx.h"
23 #include "editor.h"
24 #include "ObjectListDialog.h"
25 #include "HObject.h"
26 #include "MainFrm.h"
27
28 #include "object.h"
29 #include "objinfo.h"
30 #include "pserror.h"
31
32 #include <string.h>
33 #include <stdio.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 // CObjectListDialog dialog
43
44 CObjectListDialog::CObjectListDialog(CWnd *pParent /*=NULL*/) : CDialog(CObjectListDialog::IDD, pParent) {
45 //{{AFX_DATA_INIT(CObjectListDialog)
46 // NOTE: the ClassWizard will add member initialization here
47 //}}AFX_DATA_INIT
48
49 m_Leafs[CLIPBOARD_LEAF] = NULL;
50 m_Leafs[POWERUP_LEAF] = NULL;
51 m_Leafs[ROBOT_LEAF] = NULL;
52 m_Leafs[BUILDING_LEAF] = NULL;
53 m_Leafs[DOOR_LEAF] = NULL;
54 m_Leafs[CLUTTER_LEAF] = NULL;
55 m_Leafs[PLAYER_LEAF] = NULL;
56 m_Leafs[CAMERA_LEAF] = NULL;
57 m_Leafs[SOUNDSOURCE_LEAF] = NULL;
58 m_Leafs[MISC_LEAF] = NULL;
59
60 nclipboard = 0;
61 npowerup = 0;
62 nrobot = 0;
63 nbuilding = 0;
64 nclutter = 0;
65 ndoor = 0;
66 nplayer = 0;
67 ncamera = 0;
68 nsndsrc = 0;
69 nmisc = 0;
70 }
71
72 void CObjectListDialog::MakeInfoStr(object *obj, char *str) {
73 char tempstr[128];
74
75 if (obj->type == OBJ_POWERUP || obj->type == OBJ_ROBOT || obj->type == OBJ_BUILDING || obj->type == OBJ_CLUTTER)
76 sprintf(tempstr, "%d-(0x%x)[%s](%s) ", static_cast<int>(OBJNUM(obj)), obj->handle, Object_info[obj->id].name,
77 (obj->name) ? obj->name : "No Name Given");
78 else {
79 char *obj_type_name =
80 (obj->type >= 0 && obj->type < MAX_OBJECT_TYPES) ? Object_type_names[obj->type] : "Unnamed type";
81 sprintf(tempstr, "%d-(0x%x)[%s](%s) ", static_cast<int>(OBJNUM(obj)), obj->handle, obj_type_name,
82 (obj->name) ? obj->name : "No Name Given");
83 }
84
85 strcpy(str, tempstr);
86
87 if (OBJECT_OUTSIDE(obj))
88 sprintf(tempstr, "cell %d ", CELLNUM(obj->roomnum));
89 else
90 sprintf(tempstr, "room %d ", obj->roomnum);
91
92 strcat(str, tempstr);
93 }
94
95 void CObjectListDialog::Refresh() {
96 int i;
97
98 // Generate object tree
99 // categorize them here.
100 CTreeCtrl *tree = (CTreeCtrl *)GetDlgItem(IDC_OBJTREE);
101
102 nclipboard = 0;
103 npowerup = 0;
104 nrobot = 0;
105 nbuilding = 0;
106 nclutter = 0;
107 ndoor = 0;
108 nplayer = 0;
109 ncamera = 0;
110 nsndsrc = 0;
111 nmisc = 0;
112
113 for (i = 0; i <= Highest_object_index; i++) {
114 if (!(Objects[i].flags & OF_DEAD)) {
115 if (Objects[i].type == OBJ_POWERUP)
116 npowerup++;
117 else if (Objects[i].type == OBJ_ROBOT)
118 nrobot++;
119 else if (Objects[i].type == OBJ_BUILDING)
120 nbuilding++;
121 else if (Objects[i].type == OBJ_CLUTTER)
122 nclutter++;
123 else if (Objects[i].type == OBJ_DOOR)
124 ndoor++;
125 else if (Objects[i].type == OBJ_PLAYER)
126 nplayer++;
127 else if (Objects[i].type == OBJ_CAMERA)
128 ncamera++;
129 else if (Objects[i].type == OBJ_SOUNDSOURCE)
130 nsndsrc++;
131 else if (Objects[i].type != OBJ_VIEWER)
132 nmisc++;
133 }
134 }
135
136 ClearTree();
137
138 if (nclipboard)
139 m_Leafs[CLIPBOARD_LEAF] = new CObjectListDialog::tObjNode[nclipboard];
140 if (npowerup)
141 m_Leafs[POWERUP_LEAF] = new CObjectListDialog::tObjNode[npowerup];
142 if (nrobot)
143 m_Leafs[ROBOT_LEAF] = new CObjectListDialog::tObjNode[nrobot];
144 if (nbuilding)
145 m_Leafs[BUILDING_LEAF] = new CObjectListDialog::tObjNode[nbuilding];
146 if (nclutter)
147 m_Leafs[CLUTTER_LEAF] = new CObjectListDialog::tObjNode[nclutter];
148 if (nplayer)
149 m_Leafs[PLAYER_LEAF] = new CObjectListDialog::tObjNode[nplayer];
150 if (ndoor)
151 m_Leafs[DOOR_LEAF] = new CObjectListDialog::tObjNode[ndoor];
152 if (ncamera)
153 m_Leafs[CAMERA_LEAF] = new CObjectListDialog::tObjNode[ncamera];
154 if (nsndsrc)
155 m_Leafs[SOUNDSOURCE_LEAF] = new CObjectListDialog::tObjNode[nsndsrc];
156 if (nmisc)
157 m_Leafs[MISC_LEAF] = new CObjectListDialog::tObjNode[nmisc];
158
159 // add sections to tree
160 tree->DeleteAllItems();
161
162 hClipboardTree = tree->InsertItem("Clipboard");
163 hPowerupTree = tree->InsertItem("Powerups");
164 hRobotTree = tree->InsertItem("Robots");
165 hBuildingTree = tree->InsertItem("Buildings");
166 hDoorTree = tree->InsertItem("Door");
167 hClutterTree = tree->InsertItem("Clutter");
168 hPlayerTree = tree->InsertItem("Players");
169 hCameraTree = tree->InsertItem("Cameras");
170 hSoundSourceTree = tree->InsertItem("Sound sources");
171 hMiscTree = tree->InsertItem("Miscellaneous");
172
173 nclipboard = 0;
174 npowerup = 0;
175 nrobot = 0;
176 nbuilding = 0;
177 nclutter = 0;
178 ndoor = 0;
179 nplayer = 0;
180 ncamera = 0;
181 nsndsrc = 0;
182 nmisc = 0;
183
184 // generate list
185 for (i = 0; i <= Highest_object_index; i++) {
186 if (!(Objects[i].flags & OF_DEAD)) {
187 char str[256];
188
189 MakeInfoStr(&Objects[i], str);
190
191 if (Objects[i].type == OBJ_POWERUP) {
192 m_Leafs[POWERUP_LEAF][npowerup].hItem = tree->InsertItem(str, hPowerupTree);
193 m_Leafs[POWERUP_LEAF][npowerup++].handle = Objects[i].handle;
194 } else if (Objects[i].type == OBJ_ROBOT) {
195 m_Leafs[ROBOT_LEAF][nrobot].hItem = tree->InsertItem(str, hRobotTree);
196 m_Leafs[ROBOT_LEAF][nrobot++].handle = Objects[i].handle;
197 } else if (Objects[i].type == OBJ_BUILDING) {
198 m_Leafs[BUILDING_LEAF][nbuilding].hItem = tree->InsertItem(str, hBuildingTree);
199 m_Leafs[BUILDING_LEAF][nbuilding++].handle = Objects[i].handle;
200 } else if (Objects[i].type == OBJ_CLUTTER) {
201 m_Leafs[CLUTTER_LEAF][nclutter].hItem = tree->InsertItem(str, hClutterTree);
202 m_Leafs[CLUTTER_LEAF][nclutter++].handle = Objects[i].handle;
203 } else if (Objects[i].type == OBJ_PLAYER) {
204 m_Leafs[PLAYER_LEAF][nplayer].hItem = tree->InsertItem(str, hPlayerTree);
205 m_Leafs[PLAYER_LEAF][nplayer++].handle = Objects[i].handle;
206 } else if (Objects[i].type == OBJ_DOOR) {
207 m_Leafs[DOOR_LEAF][ndoor].hItem = tree->InsertItem(str, hDoorTree);
208 m_Leafs[DOOR_LEAF][ndoor++].handle = Objects[i].handle;
209 } else if (Objects[i].type == OBJ_CAMERA) {
210 m_Leafs[CAMERA_LEAF][ncamera].hItem = tree->InsertItem(str, hCameraTree);
211 m_Leafs[CAMERA_LEAF][ncamera++].handle = Objects[i].handle;
212 } else if (Objects[i].type == OBJ_SOUNDSOURCE) {
213 m_Leafs[SOUNDSOURCE_LEAF][nsndsrc].hItem = tree->InsertItem(str, hSoundSourceTree);
214 m_Leafs[SOUNDSOURCE_LEAF][nsndsrc++].handle = Objects[i].handle;
215 } else if (Objects[i].type != OBJ_VIEWER) {
216 m_Leafs[MISC_LEAF][nmisc].hItem = tree->InsertItem(str, hMiscTree);
217 m_Leafs[MISC_LEAF][nmisc++].handle = Objects[i].handle;
218 }
219 }
220 }
221 }
222
223 void CObjectListDialog::ClearTree() {
224 if (m_Leafs[CLIPBOARD_LEAF])
225 delete[] m_Leafs[CLIPBOARD_LEAF];
226 if (m_Leafs[POWERUP_LEAF])
227 delete[] m_Leafs[POWERUP_LEAF];
228 if (m_Leafs[ROBOT_LEAF])
229 delete[] m_Leafs[ROBOT_LEAF];
230 if (m_Leafs[BUILDING_LEAF])
231 delete[] m_Leafs[BUILDING_LEAF];
232 if (m_Leafs[CLUTTER_LEAF])
233 delete[] m_Leafs[CLUTTER_LEAF];
234 if (m_Leafs[DOOR_LEAF])
235 delete[] m_Leafs[DOOR_LEAF];
236 if (m_Leafs[PLAYER_LEAF])
237 delete[] m_Leafs[PLAYER_LEAF];
238 if (m_Leafs[CAMERA_LEAF])
239 delete[] m_Leafs[CAMERA_LEAF];
240 if (m_Leafs[SOUNDSOURCE_LEAF])
241 delete[] m_Leafs[SOUNDSOURCE_LEAF];
242 if (m_Leafs[MISC_LEAF])
243 delete[] m_Leafs[MISC_LEAF];
244
245 m_Leafs[CLIPBOARD_LEAF] = NULL;
246 m_Leafs[POWERUP_LEAF] = NULL;
247 m_Leafs[ROBOT_LEAF] = NULL;
248 m_Leafs[BUILDING_LEAF] = NULL;
249 m_Leafs[DOOR_LEAF] = NULL;
250 m_Leafs[CLUTTER_LEAF] = NULL;
251 m_Leafs[PLAYER_LEAF] = NULL;
252 m_Leafs[CAMERA_LEAF] = NULL;
253 m_Leafs[SOUNDSOURCE_LEAF] = NULL;
254 m_Leafs[MISC_LEAF] = NULL;
255
256 m_ObjectHandle = -1;
257 }
258
259 void CObjectListDialog::DoDataExchange(CDataExchange *pDX) {
260 CDialog::DoDataExchange(pDX);
261 //{{AFX_DATA_MAP(CObjectListDialog)
262 // NOTE: the ClassWizard will add DDX and DDV calls here
263 //}}AFX_DATA_MAP
264 }
265
266 int CObjectListDialog::GetObjectSelected() const { return m_ObjectHandle; }
267
268 // takes a tree item and returns the object handle by looking through all the ObjNodes.
269 int CObjectListDialog::GetObjHandleFromItem(HTREEITEM hItem) {
270 CTreeCtrl *tree = (CTreeCtrl *)GetDlgItem(IDC_OBJTREE);
271 HTREEITEM hParent;
272 tObjNode *leaf;
273 int count, i;
274
275 if (!(hParent = tree->GetParentItem(hItem)))
276 return -1;
277
278 // check each leaf
279 if (hParent == hClipboardTree) {
280 leaf = m_Leafs[CLIPBOARD_LEAF];
281 count = nclipboard;
282 } else if (hParent == hRobotTree) {
283 leaf = m_Leafs[ROBOT_LEAF];
284 count = nrobot;
285 } else if (hParent == hPowerupTree) {
286 leaf = m_Leafs[POWERUP_LEAF];
287 count = npowerup;
288 } else if (hParent == hBuildingTree) {
289 leaf = m_Leafs[BUILDING_LEAF];
290 count = nbuilding;
291 } else if (hParent == hDoorTree) {
292 leaf = m_Leafs[DOOR_LEAF];
293 count = ndoor;
294 } else if (hParent == hClutterTree) {
295 leaf = m_Leafs[CLUTTER_LEAF];
296 count = nclutter;
297 } else if (hParent == hPlayerTree) {
298 leaf = m_Leafs[PLAYER_LEAF];
299 count = nplayer;
300 } else if (hParent == hCameraTree) {
301 leaf = m_Leafs[CAMERA_LEAF];
302 count = ncamera;
303 } else if (hParent == hSoundSourceTree) {
304 leaf = m_Leafs[SOUNDSOURCE_LEAF];
305 count = nsndsrc;
306 } else if (hParent == hMiscTree) {
307 leaf = m_Leafs[MISC_LEAF];
308 count = nmisc;
309 } else {
310 count = 0;
311 }
312
313 for (i = 0; i < count; i++, leaf++)
314 if (leaf->hItem == hItem)
315 return leaf->handle;
316
317 // mprintf(0, "ObjectListDialog: Unable to find handle in object tree!!\n");
318
319 return -1;
320 }
321
322 BEGIN_MESSAGE_MAP(CObjectListDialog, CDialog)
323 //{{AFX_MSG_MAP(CObjectListDialog)
324 ON_WM_DESTROY()
325 ON_NOTIFY(TVN_SELCHANGED, IDC_OBJTREE, OnSelchangedObjtree)
326 ON_BN_CLICKED(IDC_GOTO, OnGoto)
327 ON_BN_CLICKED(IDC_DELETE, OnDelete)
328 ON_BN_CLICKED(IDC_CLEAR, OnClear)
329 //}}AFX_MSG_MAP
330 END_MESSAGE_MAP()
331
332 /////////////////////////////////////////////////////////////////////////////
333 // CObjectListDialog message handlers
334
335 BOOL CObjectListDialog::OnInitDialog() {
336 CDialog::OnInitDialog();
337
338 // Generate object tree
339 // categorize them here.
340 Refresh();
341
342 CTreeCtrl *tree = (CTreeCtrl *)GetDlgItem(IDC_OBJTREE);
343 CWnd *btn1 = GetDlgItem(IDC_GOTO);
344 CWnd *btn2 = GetDlgItem(IDC_DELETE);
345
346 if (!tree->GetSelectedItem()) {
347 btn1->EnableWindow(FALSE);
348 btn2->EnableWindow(FALSE);
349 }
350
351 return TRUE; // return TRUE unless you set the focus to a control
352 // EXCEPTION: OCX Property Pages should return FALSE
353 }
354
355 void CObjectListDialog::OnDestroy() {
356 CDialog::OnDestroy();
357
358 ClearTree();
359 }
360
361 void CObjectListDialog::OnSelchangedObjtree(NMHDR *pNMHDR, LRESULT *pResult) {
362 CWnd *btn1 = GetDlgItem(IDC_GOTO);
363 CWnd *btn2 = GetDlgItem(IDC_DELETE);
364
365 NM_TREEVIEW *pNMTreeView = (NM_TREEVIEW *)pNMHDR;
366
367 m_ObjectHandle = GetObjHandleFromItem(pNMTreeView->itemNew.hItem);
368
369 if (m_ObjectHandle < 0) {
370 btn1->EnableWindow(FALSE);
371 btn2->EnableWindow(FALSE);
372 } else {
373 btn1->EnableWindow(TRUE);
374 btn2->EnableWindow(TRUE);
375 }
376
377 *pResult = 0;
378 }
379
380 void CObjectListDialog::OnGoto() {
381 int objhandle = GetObjectSelected();
382 object *obj = NULL;
383
384 if (objhandle > -1) {
385 obj = ObjGet(objhandle);
386 }
387
388 if (obj) {
389 if (OBJECT_OUTSIDE(obj) && Editor_view_mode != VM_TERRAIN) {
390 SetViewMode(VM_TERRAIN);
391 } else if (!OBJECT_OUTSIDE(obj) && Editor_view_mode != VM_MINE) {
392 SetViewMode(VM_MINE);
393 }
394 ObjSetPos(Viewer_object, &obj->pos, obj->roomnum, &obj->orient, false);
395 EditorStatus("Viewer moved to object %d", OBJNUM(obj));
396 Viewer_moved = 1;
397 }
398 }
399
400 void CObjectListDialog::OnDelete() {
401 int objhandle = GetObjectSelected();
402 object *obj = NULL;
403
404 if (objhandle > -1) {
405 obj = ObjGet(objhandle);
406 }
407
408 if (obj) {
409 CTreeCtrl *tree = (CTreeCtrl *)GetDlgItem(IDC_OBJTREE);
410 Cur_object_index = OBJNUM(obj);
411 HObjectDelete();
412 tree->DeleteItem(tree->GetSelectedItem());
413 }
414 }
415
416 void CObjectListDialog::OnClear() {
417 // delete all items.
418 int i;
419
420 if (OutrageMessageBox(MBOX_YESNO, "Will delete ALL objects except doors. Are you sure?") != IDYES)
421 return;
422
423 for (i = 0; i <= Highest_object_index; i++) {
424 if (&Objects[i] == Player_object) {
425 continue;
426 }
427 if (&Objects[i] == Viewer_object) {
428 continue;
429 }
430 if (Objects[i].type == OBJ_DOOR) {
431 continue;
432 }
433 if (Objects[i].type != OBJ_NONE)
434 ObjDelete(i);
435 }
436 World_changed = 1;
437 Cur_object_index = -1;
438
439 Refresh();
440 }
441
442 /////////////////////////////////////////////////////////////////////////////
443 // CObjectListDialog dialog
444
445 CObjectListModeless::CObjectListModeless(CWnd *pParent /*=NULL*/) : CObjectListDialog(pParent) {
446 //{{AFX_DATA_INIT(CObjectListModeless)
447 // NOTE: the ClassWizard will add member initialization here
448 //}}AFX_DATA_INIT
449
450 m_wndParent = pParent;
451 }
452
453 void CObjectListModeless::DoDataExchange(CDataExchange *pDX) {
454 CObjectListDialog::DoDataExchange(pDX);
455 //{{AFX_DATA_MAP(CObjectListModeless)
456 // NOTE: the ClassWizard will add DDX and DDV calls here
457 //}}AFX_DATA_MAP
458 }
459
460 BOOL CObjectListModeless::Create() { return CDialog::Create(IDD, m_wndParent); }
461
462 BEGIN_MESSAGE_MAP(CObjectListModeless, CObjectListDialog)
463 //{{AFX_MSG_MAP(CObjectListModeless)
464 ON_WM_MOVE()
465 //}}AFX_MSG_MAP
466 END_MESSAGE_MAP()
467
468 /////////////////////////////////////////////////////////////////////////////
469 // CObjectListModeless message handlers
470
471 void CObjectListModeless::PostNcDestroy() {
472 ClearTree();
473 delete this;
474 }
475
476 void CObjectListModeless::OnMove(int x, int y) {
477 CObjectListDialog::OnMove(x, y);
478
479 // save location of modeless.
480 RECT rect;
481 GetWindowRect(&rect);
482 GetParent()->ScreenToClient(&rect);
483 D3EditState.objmodeless_x = rect.left;
484 D3EditState.objmodeless_y = rect.top;
485 }
486
487 void CObjectListModeless::OnCancel() {
488 // TODO: Add extra cleanup here
489
490 DestroyWindow();
491 D3EditState.objmodeless_on = false;
492 }
493
494 BOOL CObjectListModeless::OnInitDialog() {
495 CObjectListDialog::OnInitDialog();
496
497 CWnd *okbtn = GetDlgItem(IDOK);
498 CWnd *canbtn = GetDlgItem(IDCANCEL);
499
500 // remove ok and cancel buttons.
501 okbtn->DestroyWindow();
502 canbtn->DestroyWindow();
503
504 return TRUE; // return TRUE unless you set the focus to a control
505 // EXCEPTION: OCX Property Pages should return FALSE
506 }