Procházet zdrojové kódy

Add a current() method to History, so that consumers don't need to worry about bounds checking.

Marcus Huderle před 6 roky
rodič
revize
dfc990f0da
2 změnil soubory, kde provedl 14 přidání a 7 odebrání
  1. 2
    2
      map.cpp
  2. 12
    5
      map.h

+ 2
- 2
map.cpp Zobrazit soubor

@@ -618,7 +618,7 @@ void Map::_floodFillCollisionElevation(int x, int y, uint collision, uint elevat
618 618
 
619 619
 void Map::undo() {
620 620
     if (blockdata) {
621
-        Blockdata *commit = history.pop();
621
+        Blockdata *commit = history.back();
622 622
         if (commit != NULL) {
623 623
             blockdata->copyFrom(commit);
624 624
             emit mapChanged(this);
@@ -638,7 +638,7 @@ void Map::redo() {
638 638
 
639 639
 void Map::commit() {
640 640
     if (blockdata) {
641
-        if (!blockdata->equals(history.history.at(history.head))) {
641
+        if (!blockdata->equals(history.current())) {
642 642
             Blockdata* commit = blockdata->copy();
643 643
             history.push(commit);
644 644
             emit mapChanged(this);

+ 12
- 5
map.h Zobrazit soubor

@@ -13,14 +13,10 @@
13 13
 template <typename T>
14 14
 class History {
15 15
 public:
16
-    QList<T> history;
17
-    int head = -1;
18
-    int saved = -1;
19
-
20 16
     History() {
21 17
 
22 18
     }
23
-    T pop() {
19
+    T back() {
24 20
         if (head > 0) {
25 21
             return history.at(--head);
26 22
         }
@@ -42,12 +38,23 @@ public:
42 38
         history.append(commit);
43 39
         head++;
44 40
     }
41
+    T current() {
42
+        if (head < 0 || history.length() == 0) {
43
+            return NULL;
44
+        }
45
+        return history.at(head);
46
+    }
45 47
     void save() {
46 48
         saved = head;
47 49
     }
48 50
     bool isSaved() {
49 51
         return saved == head;
50 52
     }
53
+
54
+private:
55
+    QList<T> history;
56
+    int head = -1;
57
+    int saved = -1;
51 58
 };
52 59
 
53 60
 class Connection {