forked from wario-land/WL4Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperation.cpp
More file actions
529 lines (496 loc) · 20.1 KB
/
Operation.cpp
File metadata and controls
529 lines (496 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#include "Operation.h"
#include "WL4EditorWindow.h"
#include <deque>
extern WL4EditorWindow *singleton;
// Globals used by the undo system
static std::deque<struct OperationParams *> operationHistory[16];
static unsigned int operationIndex[16]; // For room-specific changes
static std::deque<struct OperationParams *> operationHistoryGlobal;
static unsigned int operationIndexGlobal; // For level-wide changes
/// <summary>
/// Perform an operation based on its parameters.
/// </summary>
/// <remarks>
/// This function does not take into consideration the undo deque. It only performs an operation.
/// </remarks>
/// <param name="operation">
/// The operation to perform.
/// </param>
void PerformOperation(struct OperationParams *operation)
{
LevelComponents::Room *room;
if (operation->tileChange)
{
room = singleton->GetCurrentRoom();
int tl1 = -1, tl2 = -1; // there are 2 target layers only when doing cross layer rect-copy
QVector<LevelComponents::Tileinfo> tilechangelist, tilechangelist2;
for (auto iter = operation->tileChangeParams.begin(); iter != operation->tileChangeParams.end(); ++iter)
{
struct TileChangeParams *tcp = *iter;
LevelComponents::Layer *layer = room->GetLayer(tcp->targetLayer);
unsigned int index;
if (!tcp->targetLayer) // i.e. targetLayer is Layer 0
{
index = tcp->tileX + tcp->tileY * room->GetLayer0Width();
}
else
{
index = tcp->tileX + tcp->tileY * room->GetWidth();
}
layer->GetLayerData()[index] = tcp->newTile;
if(tl1 == -1)
{
tl1 = tcp->targetLayer;
}
if(tl1 != -1 && tl2 == -1 && tl1 != tcp->targetLayer)
{
tl2 = tcp->targetLayer;
}
struct LevelComponents::Tileinfo tinfo;
tinfo.tileX = tcp->tileX;
tinfo.tileY = tcp->tileY;
tinfo.tileID = tcp->newTile;
if(tl1 == tcp->targetLayer)
{
tilechangelist.push_back(tinfo);
continue;
}
if(tl2 == tcp->targetLayer)
{
tilechangelist2.push_back(tinfo);
continue;
}
}
// Update graphic changes
singleton->RenderScreenTilesChange(tilechangelist, tl1);
if(tl2 == -1) return;
singleton->RenderScreenTilesChange(tilechangelist2, tl2);
}
if (operation->roomConfigChange)
{
// change the width and height for all layers
singleton->RoomConfigReset(operation->lastRoomConfigParams, operation->newRoomConfigParams);
singleton->RenderScreenFull();
singleton->SetEditModeDockWidgetLayerEditability();
singleton->SetEditModeWidgetDifficultyRadioBox(1);
singleton->ResetEntitySetDockWidget();
singleton->SetUnsavedChanges(true);
}
if (operation->objectPositionChange)
{
struct ObjectMoveParams *om=operation->objectMoveParams;
/*om->previousPositionX = pX;
om->previousPositionY = pY;*/
if (om->type == ObjectMoveParams::DOOR_TYPE)
{
LevelComponents::Room *currentRoom = singleton->GetCurrentRoom();
LevelComponents::Door *selectedDoor = currentRoom->GetDoor(om->objectID);
// Calculating the deltas
int px1 = selectedDoor->GetX1();
int py1 = selectedDoor->GetY1();
int deltaX = selectedDoor->GetX2()-px1;
int deltaY = selectedDoor->GetY2()-py1;
// If the door exists and if it is still in the room
if (om->objectID != -1 && selectedDoor)
{
if (currentRoom->IsNewDoorPositionInsideRoom(om->nextPositionX, om->nextPositionX+deltaX, om->nextPositionY, om->nextPositionY + deltaY))
{
selectedDoor->SetDoorPlace(om->nextPositionX, om->nextPositionX+deltaX, om->nextPositionY, om->nextPositionY + deltaY);
singleton->RenderScreenElementsLayersUpdate((unsigned int) om->objectID, -1);
}
}
}
else if (om->type == ObjectMoveParams::ENTITY_TYPE)
{
LevelComponents::Room *currentRoom = singleton->GetCurrentRoom();
// If the entity exists
if (om->objectID != -1)
{
if (currentRoom->IsNewEntityPositionInsideRoom(om->nextPositionX, om->nextPositionY))
{
currentRoom->SetEntityPosition(om->nextPositionX, om->nextPositionY, om->objectID);
singleton->RenderScreenElementsLayersUpdate(0xFFFFFFFFu, om->objectID);
int difficulty = singleton->GetEditModeWidgetPtr()->GetEditModeParams().selectedDifficulty;
singleton->GetCurrentRoom()->SetEntityListDirty(difficulty, true);
singleton->SetUnsavedChanges(true);
}
}
}
}
if (operation->TilesetChange)
{
// Update Rooms's Tileset in CurrentLevel
int roomnum = singleton->GetCurrentLevel()->GetRooms().size();
int tilesetId = operation->newTilesetEditParams->currentTilesetIndex;
ROMUtils::singletonTilesets[tilesetId] = operation->newTilesetEditParams->newTileset;
for(int i = 0; i < roomnum; ++i)
{
if(singleton->GetCurrentLevel()->GetRooms()[i]->GetTilesetID() == tilesetId)
{
singleton->GetCurrentLevel()->GetRooms()[i]->SetTileset(operation->newTilesetEditParams->newTileset, tilesetId);
}
}
if (singleton->GetCurrentRoom()->GetTilesetID() == tilesetId)
{
singleton->GetTile16DockWidgetPtr()->SetTileset(tilesetId);
singleton->RenderScreenFull();
}
singleton->SetUnsavedChanges(true);
}
if (operation->SpritesSpritesetChange)
{
// Update new Entities and Entitysets to global singeltons
for (LevelComponents::Entity *entityIter: operation->newSpritesAndSetParam->entities)
{
ROMUtils::entities[entityIter->GetEntityGlobalID()] = entityIter;
}
for (LevelComponents::EntitySet *entitySetIter: operation->newSpritesAndSetParam->entitySets)
{
ROMUtils::entitiessets[entitySetIter->GetEntitySetId()] = entitySetIter;
}
// Update Rooms's Entities and Entitysets in CurrentLevel
int roomnum = singleton->GetCurrentLevel()->GetRooms().size();
for(int i = 0; i < roomnum; ++i)
{
LevelComponents::Room *curRoom = singleton->GetCurrentLevel()->GetRooms()[i];
curRoom->SetCurrentEntitySet(curRoom->GetCurrentEntitySetID());
}
singleton->ResetEntitySetDockWidget();
singleton->RenderScreenFull();
singleton->SetUnsavedChanges(true);
}
}
/// <summary>
/// Backtrack an operation based on its parameters.
/// </summary>
/// <remarks>
/// This function does not take into consideration the undo deque. It only resets the effect of an operation.
/// </remarks>
/// <param name="operation">
/// The operation to backtrack.
/// </param>
void BackTrackOperation(struct OperationParams *operation)
{
LevelComponents::Room *room;
if (operation->tileChange)
{
room = singleton->GetCurrentRoom();
int tl1 = -1, tl2 = -1; // there are 2 target layers only when doing cross layer rect-copy
QVector<LevelComponents::Tileinfo> tilechangelist, tilechangelist2;
for (auto iter = operation->tileChangeParams.begin(); iter != operation->tileChangeParams.end(); ++iter)
{
struct TileChangeParams *tcp = *iter;
LevelComponents::Layer *layer = room->GetLayer(tcp->targetLayer);
unsigned int index;
if (!tcp->targetLayer) // i.e. targetLayer is Layer 0
{
index = tcp->tileX + tcp->tileY * room->GetLayer0Width();
}
else
{
index = tcp->tileX + tcp->tileY * room->GetWidth();
}
layer->GetLayerData()[index] = tcp->oldTile;
if(tl1 == -1)
{
tl1 = tcp->targetLayer;
}
if(tl1 != -1 && tl2 == -1 && tl1 != tcp->targetLayer)
{
tl2 = tcp->targetLayer;
}
struct LevelComponents::Tileinfo tinfo;
tinfo.tileX = tcp->tileX;
tinfo.tileY = tcp->tileY;
tinfo.tileID = tcp->oldTile;
if(tl1 == tcp->targetLayer)
{
tilechangelist.push_back(tinfo);
continue;
}
if(tl2 == tcp->targetLayer)
{
tilechangelist2.push_back(tinfo);
continue;
}
}
// Update graphic changes
singleton->RenderScreenTilesChange(tilechangelist, tl1);
if(tl2 == -1) return;
singleton->RenderScreenTilesChange(tilechangelist2, tl2);
}
if (operation->roomConfigChange)
{
// new to last
singleton->RoomConfigReset(operation->newRoomConfigParams, operation->lastRoomConfigParams);
singleton->RenderScreenFull();
singleton->SetEditModeDockWidgetLayerEditability();
singleton->SetEditModeWidgetDifficultyRadioBox(1);
singleton->ResetEntitySetDockWidget();
}
if (operation->objectPositionChange)
{
struct ObjectMoveParams *om=operation->objectMoveParams;
/*om->previousPositionX = pX;
om->previousPositionY = pY;*/
if (om->type == ObjectMoveParams::DOOR_TYPE)
{
LevelComponents::Room *currentRoom = singleton->GetCurrentRoom();
LevelComponents::Door *selectedDoor = currentRoom->GetDoor(om->objectID);
// Calculating the deltas
int px1 = selectedDoor->GetX1();
int py1 = selectedDoor->GetY1();
int deltaX = selectedDoor->GetX2()-px1;
int deltaY = selectedDoor->GetY2()-py1;
// If the door exists and if it is still in the room
if (om->objectID != -1)
{
if (currentRoom->IsNewDoorPositionInsideRoom(om->previousPositionX, om->previousPositionX+deltaX, om->previousPositionY, om->previousPositionY + deltaY))
{
selectedDoor->SetDoorPlace(om->previousPositionX, om->previousPositionX+deltaX, om->previousPositionY, om->previousPositionY + deltaY);
singleton->RenderScreenElementsLayersUpdate((unsigned int) om->objectID, -1);
}
}
} else if (om->type == ObjectMoveParams::ENTITY_TYPE)
{
LevelComponents::Room *currentRoom = singleton->GetCurrentRoom();
// If the entity exists and if it is still in the room
if (om->objectID != -1)
{
if (currentRoom->IsNewEntityPositionInsideRoom(om->previousPositionX, om->previousPositionY))
{
currentRoom->SetEntityPosition(om->previousPositionX, om->previousPositionY, om->objectID);
singleton->RenderScreenElementsLayersUpdate(0xFFFFFFFFu, om->objectID);
int difficulty = singleton->GetEditModeWidgetPtr()->GetEditModeParams().selectedDifficulty;
singleton->GetCurrentRoom()->SetEntityListDirty(difficulty, true);
}
}
}
}
if (operation->TilesetChange)
{
// Update Rooms's Tileset in CurrentLevel
int roomnum = singleton->GetCurrentLevel()->GetRooms().size();
int tilesetId = operation->lastTilesetEditParams->currentTilesetIndex;
ROMUtils::singletonTilesets[tilesetId] = operation->lastTilesetEditParams->newTileset;
for(int i = 0; i < roomnum; ++i)
{
if(singleton->GetCurrentLevel()->GetRooms()[i]->GetTilesetID() == tilesetId)
{
singleton->GetCurrentLevel()->GetRooms()[i]->SetTileset(operation->lastTilesetEditParams->newTileset, tilesetId);
}
}
singleton->GetTile16DockWidgetPtr()->SetTileset(tilesetId);
singleton->RenderScreenFull();
}
if (operation->SpritesSpritesetChange)
{
// Update old Entities and Entitysets to global singeltons
for (LevelComponents::Entity *entityIter: operation->lastSpritesAndSetParam->entities)
{
ROMUtils::entities[entityIter->GetEntityGlobalID()] = entityIter;
}
for (LevelComponents::EntitySet *entitySetIter: operation->lastSpritesAndSetParam->entitySets)
{
ROMUtils::entitiessets[entitySetIter->GetEntitySetId()] = entitySetIter;
}
// Update Rooms's Entities and Entitysets in CurrentLevel
int roomnum = singleton->GetCurrentLevel()->GetRooms().size();
for(int i = 0; i < roomnum; ++i)
{
LevelComponents::Room *curRoom = singleton->GetCurrentLevel()->GetRooms()[i];
curRoom->SetCurrentEntitySet(curRoom->GetCurrentEntitySetID());
}
singleton->GetEntitySetDockWidgetPtr()->ResetEntitySet(singleton->GetCurrentRoom());
singleton->RenderScreenFull();
singleton->SetUnsavedChanges(true);
}
}
/// <summary>
/// Perform an operation based on its parameters, and add it to the undo deque.
/// </summary>
/// <param name="operation">
/// The operation to perform.
/// </param>
/// <param name="operationHist">
/// The the history deque from which to execute an operation.
/// </param>
/// <param name="operationIdx">
/// The operation indexer to modify.
/// </param>
static void ExecuteOperationImpl(struct OperationParams *operation, std::deque<struct OperationParams *> &operationHist, unsigned int *operationIdx)
{
PerformOperation(operation);
// If we perform an action after a series of undo, then delete the "undone" operations from history
while (*operationIdx)
{
// Delete the front operation in the queue while decrementing the operation index until the index reaches 0
--(*operationIdx);
struct OperationParams *frontOP = operationHist[0];
delete frontOP;
operationHist.pop_front();
}
operationHist.push_front(operation);
singleton->SetUnsavedChanges(true);
}
/// <summary>
/// Perform an operation based on its parameters, and add it to the undo deque.
/// This is for performing an operation within a Room.
/// </summary>
/// <param name="operation">
/// The operation to perform.
/// </param>
void ExecuteOperation(struct OperationParams *operation)
{
int currentRoomNumber = singleton->GetCurrentRoom()->GetRoomID();
ExecuteOperationImpl(operation, operationHistory[currentRoomNumber], operationIndex + currentRoomNumber);
}
/// <summary>
/// Perform an operation based on its parameters, and add it to the undo deque.
/// This is for performing a global operation.
/// </summary>
/// <param name="operation">
/// The operation to perform.
/// </param>
void ExecuteOperationGlobal(struct OperationParams *operation)
{
ExecuteOperationImpl(operation, operationHistoryGlobal, &operationIndexGlobal);
}
/// <summary>
/// Undo a previously performed operation in the undo deque.
/// </summary>
/// <remarks>
/// This function does not remove the operation from the deque.
/// Instead, an index is used within the deque to track which operation should be undone next.
/// That way, an operation can be undone and redone multiple times.
/// </remarks>
/// </param>
/// <param name="operationHist">
/// The the history deque from which to undo an operation.
/// </param>
/// <param name="operationIdx">
/// The operation indexer to modify.
/// </param>
static void UndoOperationImpl(std::deque<struct OperationParams *> &operationHist, unsigned int *operationIdx)
{
// We cannot undo past the end of the deque
if (*operationIdx < operationHist.size())
{
BackTrackOperation(operationHist[(*operationIdx)++]);
// If the entire operation history is undone for all rooms, then there are no unsaved changes
for (unsigned int i = 0; i < sizeof(operationIndex) / sizeof(operationIndex[0]); ++i)
{
if (*operationIdx != operationHist.size())
{
return;
}
}
// TODO uncomment this once all operations that change the level go through Operation.cpp
// singleton->SetUnsavedChanges(false);
}
}
/// <summary>
/// Undo a previously performed operation in the undo deque.
/// This is for undoing an operation within a Room.
/// </summary>
/// <remarks>
/// This function does not remove the operation from the deque.
/// Instead, an index is used within the deque to track which operation should be undone next.
/// That way, an operation can be undone and redone multiple times.
/// </remarks>
void UndoOperation()
{
int currentRoomNumber = singleton->GetCurrentRoom()->GetRoomID();
UndoOperationImpl(operationHistory[currentRoomNumber], operationIndex + currentRoomNumber);
}
/// <summary>
/// Undo a previously performed operation in the undo deque.
/// This is for undoing a global operation.
/// </summary>
/// <remarks>
/// This function does not remove the operation from the deque.
/// Instead, an index is used within the deque to track which operation should be undone next.
/// That way, an operation can be undone and redone multiple times.
/// </remarks>
void UndoOperationGlobal()
{
UndoOperationImpl(operationHistoryGlobal, &operationIndexGlobal);
}
/// <summary>
/// Redo a previously undone operation from the undo deque.
/// </summary>
/// <remarks>
/// This function does not add the operation to the deque.
/// Instead, an index is used within the deque to track which operation should be redone next.
/// That way, an operation can be undone and redone multiple times.
/// </remarks>
/// <param name="operationHist">
/// The the history deque from which to undo an operation.
/// </param>
/// <param name="operationIdx">
/// The operation indexer to modify.
/// </param>
static void RedoOperationImpl(std::deque<struct OperationParams *> &operationHist, unsigned int *operationIdx)
{
// We cannot redo past the front of the deque
if (*operationIdx)
{
PerformOperation(operationHist[--(*operationIdx)]);
// Performing a "redo" will make unsaved changes
singleton->SetUnsavedChanges(true);
}
}
/// <summary>
/// Redo a previously undone operation from the undo deque.
/// This is for redoing an operation within a Room.
/// </summary>
/// <remarks>
/// This function does not add the operation to the deque.
/// Instead, an index is used within the deque to track which operation should be redone next.
/// That way, an operation can be undone and redone multiple times.
/// </remarks>
void RedoOperation()
{
int currentRoomNumber = singleton->GetCurrentRoom()->GetRoomID();
RedoOperationImpl(operationHistory[currentRoomNumber], operationIndex + currentRoomNumber);
}
/// <summary>
/// Redo a previously undone operation from the undo deque.
/// This is for redoing a global operation.
/// </summary>
/// <remarks>
/// This function does not add the operation to the deque.
/// Instead, an index is used within the deque to track which operation should be redone next.
/// That way, an operation can be undone and redone multiple times.
/// </remarks>
void RedoOperationGlobal()
{
RedoOperationImpl(operationHistoryGlobal, &operationIndexGlobal);
}
/// <summary>
/// Reset the undo deque.
/// </summary>
/// <remarks>
/// This is necessary to ensure that undo history does not persist between multiple levels.
/// </remarks>
void ResetUndoHistory()
{
for (unsigned int i = 0; i < sizeof(operationHistory) / sizeof(operationHistory[0]); ++i)
{
// Deconstruct the dynamically allocated operation structs within the history queue
for (unsigned int j = 0; j < operationHistory[i].size(); ++j)
{
delete operationHistory[i][j];
}
operationHistory[i].clear();
}
// Deconstruct the global history
for (unsigned int j = 0; j < operationHistoryGlobal.size(); ++j)
{
delete operationHistoryGlobal[j];
}
operationHistoryGlobal.clear();
// Re-initialize all the operation indexes to zero
memset(operationIndex, 0, sizeof(operationIndex));
operationIndexGlobal = 0;
}