-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathOperation.cpp
798 lines (750 loc) · 32.3 KB
/
Operation.cpp
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#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
static unsigned int CurrentTilesetOperationId = 0;
static unsigned int CurrentSpritestuffOperationId = 0;
static unsigned int CurrentAnimatedTileGroupOperationId = 0;
/// <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->GetLayer1Width();
}
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->ResetCameraControlDockWidget();
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::LevelDoorVector &tmpDoorVec = singleton->GetCurrentLevel()->GetDoorListRef();
int globalDoorId = tmpDoorVec.GetGlobalIDByLocalID(currentRoom->GetRoomID(), om->objectID);
auto curDoor = tmpDoorVec.GetDoor(globalDoorId);
// Calculating the deltas
int px1 = curDoor.x1;
int py1 = curDoor.y1;
int deltaX = curDoor.x2 - px1;
int deltaY = curDoor.y2 - py1;
// If the door exists and if it is still in the room
if (om->objectID != -1 && curDoor.DoorTypeByte)
{
if (currentRoom->IsNewDoorPositionInsideRoom(om->nextPositionX, om->nextPositionX + deltaX, om->nextPositionY, om->nextPositionY + deltaY))
{
singleton->GetCurrentLevel()->GetDoorListRef().SetDoorPlace(globalDoorId,
om->nextPositionX,
om->nextPositionX + deltaX,
om->nextPositionY,
om->nextPositionY + deltaY);
singleton->RenderScreenElementsLayersUpdate((unsigned int) om->objectID, -1);
singleton->SetUnsavedChanges(true);
}
}
}
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);
}
}
// Update the Tileset using the current ROMUtils::animatedTileGroups instances
ROMUtils::singletonTilesets[tilesetId]->UpdateAllAnimatedTileFromGlobalSingletons();
// UI update if needed
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);
}
if (operation->AnimatedTileGroupChange)
{
// update all animated tile group to global singletons
for (LevelComponents::AnimatedTile8x8Group *&animatedTileGroupIter : operation->newAnimatedTileEditParam->animatedTileGroups)
{
ROMUtils::animatedTileGroups[animatedTileGroupIter->GetGlobalID()] = animatedTileGroupIter;
}
// Update all the Tilesets using the current ROMUtils::animatedTileGroups instances
for(int i = 0; i < (sizeof(ROMUtils::singletonTilesets)) / sizeof(ROMUtils::singletonTilesets[0]); ++i)
{
ROMUtils::singletonTilesets[i]->UpdateAllAnimatedTileFromGlobalSingletons();
}
singleton->GetTile16DockWidgetPtr()->SetTileset(singleton->GetCurrentRoom()->GetTilesetID());
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->GetLayer1Width();
}
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);
// hint to show undo operation
singleton->GetOutputWidgetPtr()->PrintString(QObject::tr("Undo tile changes."));
}
if (operation->roomConfigChange)
{
// new to last
singleton->RoomConfigReset(operation->newRoomConfigParams, operation->lastRoomConfigParams);
singleton->RenderScreenFull();
singleton->SetEditModeDockWidgetLayerEditability();
singleton->SetEditModeWidgetDifficultyRadioBox(1);
singleton->ResetEntitySetDockWidget();
// hint to show undo operation
singleton->GetOutputWidgetPtr()->PrintString(QObject::tr("Undo Room Config changes."));
}
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::LevelDoorVector &tmpDoorVec = singleton->GetCurrentLevel()->GetDoorListRef();
int globalDoorId = tmpDoorVec.GetGlobalIDByLocalID(currentRoom->GetRoomID(), om->objectID);
auto curDoor = tmpDoorVec.GetDoor(globalDoorId);
// Calculating the deltas
int px1 = curDoor.x1;
int py1 = curDoor.y1;
int deltaX = curDoor.x2 - px1;
int deltaY = curDoor.y2 - py1;
// If the door exists and if it is still in the room
if (om->objectID != -1 && curDoor.DoorTypeByte)
{
if (currentRoom->IsNewDoorPositionInsideRoom(om->previousPositionX, om->previousPositionX+deltaX, om->previousPositionY, om->previousPositionY + deltaY))
{
singleton->GetCurrentLevel()->GetDoorListRef().SetDoorPlace(globalDoorId,
om->previousPositionX,
om->previousPositionX + deltaX,
om->previousPositionY,
om->previousPositionY + deltaY);
singleton->RenderScreenElementsLayersUpdate((unsigned int) om->objectID, -1);
singleton->SetUnsavedChanges(true);
}
}
} 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);
singleton->SetUnsavedChanges(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);
}
}
// Update the Tileset using the current ROMUtils::animatedTileGroups instances
ROMUtils::singletonTilesets[tilesetId]->UpdateAllAnimatedTileFromGlobalSingletons();
// UI update if needed
if (singleton->GetCurrentRoom()->GetTilesetID() == tilesetId)
{
singleton->GetTile16DockWidgetPtr()->SetTileset(tilesetId);
singleton->RenderScreenFull();
}
CurrentTilesetOperationId = operationIndexGlobal;
// hint to show undo operation
singleton->GetOutputWidgetPtr()->PrintString(QObject::tr("Undo Tileset changes."));
}
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);
CurrentSpritestuffOperationId = operationIndexGlobal;
// hint to show undo operation
singleton->GetOutputWidgetPtr()->PrintString(QObject::tr("Undo Sprites and Spritesets changes."));
}
if (operation->AnimatedTileGroupChange)
{
// update all animated tile group to global singletons
for (LevelComponents::AnimatedTile8x8Group *&animatedTileGroupIter : operation->lastAnimatedTileEditParam->animatedTileGroups)
{
ROMUtils::animatedTileGroups[animatedTileGroupIter->GetGlobalID()] = animatedTileGroupIter;
}
// Update all the Tilesets using the current ROMUtils::animatedTileGroups instances
for(int i = 0; i < (sizeof(ROMUtils::singletonTilesets)) / sizeof(ROMUtils::singletonTilesets[0]); ++i)
{
ROMUtils::singletonTilesets[i]->UpdateAllAnimatedTileFromGlobalSingletons();
}
singleton->GetTile16DockWidgetPtr()->SetTileset(singleton->GetCurrentRoom()->GetTilesetID());
singleton->RenderScreenFull();
singleton->SetUnsavedChanges(true);
CurrentAnimatedTileGroupOperationId = operationIndexGlobal;
}
}
/// <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);
}
else
{
// hint to show undo operation
singleton->GetOutputWidgetPtr()->PrintString(QObject::tr("No more operation to undo."));
}
}
/// <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);
}
else
{
// hint to show redo operation
singleton->GetOutputWidgetPtr()->PrintString(QObject::tr("No more operation to redo."));
}
}
/// <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();
}
// Re-initialize all the operation indexes to zero
memset(operationIndex, 0, sizeof(operationIndex));
}
/// <summary>
/// Reset the undo deque of a specified Room in the current Level.
/// </summary>
void ResetRoomUndoHistory(int currentRoomId)
{
if (!(operationHistory[currentRoomId].size())) return;
// Deconstruct the dynamically allocated operation structs within the history queue
for (unsigned int j = 0; j < operationHistory[currentRoomId].size(); ++j)
{
// as long as only tileChange and roomConfigChange in the current Room
// we can delete the whole undo deque without dealing with any memory problem
struct OperationParams *curParam = operationHistory[currentRoomId][j];
if (curParam->objectPositionChange ||
curParam->TilesetChange ||
curParam->CreditChange ||
curParam->SpritesSpritesetChange ||
curParam->AnimatedTileGroupChange)
{
// there is something wrong with the internal logic, we just skip the curParam atm
singleton->GetOutputWidgetPtr()->PrintString(QObject::tr("An error occured when reset the Undo history for Room 0x") +
QString::number(currentRoomId, 16));
continue;
}
delete curParam;
}
operationHistory[currentRoomId].clear();
operationIndex[currentRoomId] = 0;
}
/// <summary>
/// Clean up the global undo deque.
/// </summary>
/// <remarks>
/// Only call this when deconstruct the editor, don't use it elsewhere.
/// </remarks>
void DeleteUndoHistoryGlobal()
{
// It is different from deleting OperationParams, so cannot use the deconstructor
// Deconstruct the global history
for (unsigned int j = 0; j < operationHistoryGlobal.size(); ++j) // from old to new
{
if (operationHistoryGlobal[j]->TilesetChange)
{
if (CurrentTilesetOperationId > j)
{
delete operationHistoryGlobal[j]; // call default deconstructor
}
else
{
if (operationHistoryGlobal[j]->newTilesetEditParams)
{
delete operationHistoryGlobal[j]->newTilesetEditParams;
}
if (operationHistoryGlobal[j]->lastTilesetEditParams)
{
delete operationHistoryGlobal[j]->lastTilesetEditParams->newTileset;
operationHistoryGlobal[j]->lastTilesetEditParams->newTileset = nullptr;
delete operationHistoryGlobal[j]->lastTilesetEditParams;
}
}
}
else if (operationHistoryGlobal[j]->SpritesSpritesetChange)
{
if (CurrentSpritestuffOperationId > j)
{
delete operationHistoryGlobal[j]; // call default deconstructor
}
else
{
if (operationHistoryGlobal[j]->newSpritesAndSetParam)
{
delete operationHistoryGlobal[j]->newSpritesAndSetParam;
}
if (operationHistoryGlobal[j]->lastSpritesAndSetParam)
{
qDeleteAll(operationHistoryGlobal[j]->lastSpritesAndSetParam->entities);
qDeleteAll(operationHistoryGlobal[j]->lastSpritesAndSetParam->entitySets);
delete operationHistoryGlobal[j]->lastSpritesAndSetParam;
}
}
}
else if (operationHistoryGlobal[j]->AnimatedTileGroupChange)
{
if (CurrentAnimatedTileGroupOperationId > j)
{
delete operationHistoryGlobal[j]; // call default deconstructor
}
else
{
if (operationHistoryGlobal[j]->newAnimatedTileEditParam)
{
delete operationHistoryGlobal[j]->newAnimatedTileEditParam;
}
if (operationHistoryGlobal[j]->lastAnimatedTileEditParam)
{
qDeleteAll(operationHistoryGlobal[j]->lastAnimatedTileEditParam->animatedTileGroups);
delete operationHistoryGlobal[j]->lastAnimatedTileEditParam;
}
}
}
}
operationHistoryGlobal.clear();
operationIndexGlobal = 0;
CurrentTilesetOperationId = 0;
CurrentSpritestuffOperationId = 0;
CurrentAnimatedTileGroupOperationId = 0;
}
/// <summary>
/// Reset all the changes bools can be found through the whole history
/// </summary>
/// <remarks>
/// Sometimes the history saved the vanilla elements' data and we make some changes to it and save changes
/// now the current changed bool of the element is false and the first corresponding element in the history is false too
/// all the others corresponding elements' changed bools are true
/// usual undo and redo won't cause problems, but on the second or the third save
/// when the current element are marked unchanged, the save process cannot detect it.
/// So we have to always make it that only the current element changed bool to be false, and all the others be true.
/// call this function every time when finishing saving level
/// </remarks>
void ResetChangedBoolsThroughHistory()
{
for (unsigned int j = 0; j < operationHistoryGlobal.size(); ++j) // from old to new
{
// no need to exclude the current elements, they will be set changed bool to false in the SaveLevel function
// so call this function before that part
if (operationHistoryGlobal[j]->TilesetChange)
{
operationHistoryGlobal[j]->newTilesetEditParams->newTileset->SetChanged(true);
operationHistoryGlobal[j]->lastTilesetEditParams->newTileset->SetChanged(true);
}
else if (operationHistoryGlobal[j]->SpritesSpritesetChange)
{
if (operationHistoryGlobal[j]->newSpritesAndSetParam)
{
for (LevelComponents::Entity *entityIter: operationHistoryGlobal[j]->newSpritesAndSetParam->entities)
{
entityIter->SetChanged(true);
}
for (LevelComponents::EntitySet *entitySetIter: operationHistoryGlobal[j]->newSpritesAndSetParam->entitySets)
{
entitySetIter->SetChanged(true);
}
}
if (operationHistoryGlobal[j]->lastSpritesAndSetParam)
{
for (LevelComponents::Entity *entityIter: operationHistoryGlobal[j]->lastSpritesAndSetParam->entities)
{
entityIter->SetChanged(true);
}
for (LevelComponents::EntitySet *entitySetIter: operationHistoryGlobal[j]->lastSpritesAndSetParam->entitySets)
{
entitySetIter->SetChanged(true);
}
}
}
else if (operationHistoryGlobal[j]->AnimatedTileGroupChange)
{
for (LevelComponents::AnimatedTile8x8Group *&animtedTileGroupIter: operationHistoryGlobal[j]->lastAnimatedTileEditParam->animatedTileGroups)
{
animtedTileGroupIter->SetChanged(true);
}
for (LevelComponents::AnimatedTile8x8Group *&animtedTileGroupIter: operationHistoryGlobal[j]->newAnimatedTileEditParam->animatedTileGroups)
{
animtedTileGroupIter->SetChanged(true);
}
}
}
}
/// <summary>
/// Reset all the global elements operation indexes
/// </summary>
/// <remarks>
/// the DeleteUndoHistoryGlobal() function won't set global element indexes back to 0,
/// by using this function, we can reset some global element operation indexes back to 0 when we need to load a new ROM
/// </remarks>
void ResetGlobalElementOperationIndexes()
{
CurrentTilesetOperationId = 0;
CurrentSpritestuffOperationId = 0;
CurrentAnimatedTileGroupOperationId = 0;
}