history.cpp 3.4 KB
Newer Older
1
/* Edddy
魔大农's avatar
魔大农 committed
2
// Copyright (C) 2019 LucKey Productions (luckeyproductions.nl)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "blockinstance.h"
#include "gridblock.h"
#include "freeblock.h"
魔大农's avatar
魔大农 committed
22
#include "view3d.h"
魔大农's avatar
魔大农 committed
23
#include "mainwindow.h"
24
#include "projectpanel.h"
魔大农's avatar
魔大农 committed
25 26

#include "history.h"
27 28 29

History::History(Context* context) : Object(context),
    steps_{},
魔大农's avatar
魔大农 committed
30 31 32
    stepIndex_{ 0 },
    lastSavedIndex_{ LOSTSTEP },
    previousModifiedState_{ true },
33 34 35 36 37 38
    currentStep_{}
{
}

void History::EndStep()
{
魔大农's avatar
魔大农 committed
39
    //Don't write history if nothing happened
40 41
    if (!currentStep_.AnyChange()) {

魔大农's avatar
魔大农 committed
42 43 44 45 46
        currentStep_.Clear();
        return;
    }

    //Clear future
魔大农's avatar
魔大农 committed
47 48 49
    if (lastSavedIndex_ > stepIndex_)
        lastSavedIndex_ = LOSTSTEP;

50 51 52 53
    while (steps_.Size() > stepIndex_) {

        steps_.Pop();
    }
54 55


56 57 58 59
    steps_.Push(currentStep_);
    currentStep_.Clear();

    ++stepIndex_;
魔大农's avatar
魔大农 committed
60
    CheckModifiedStateChange();
61 62
}

魔大农's avatar
魔大农 committed
63
bool History::Undo()
64 65 66 67
{
    if (stepIndex_ > 0) {

        --stepIndex_;
68

69 70 71 72
        for (Change c : steps_.At(stepIndex_)) {

            GridBlock* gridBlock{ static_cast<GridBlock*>(c.instance_) };

73 74 75 76 77
            if (gridBlock) {

                c.instance_->SetBlock(c.block_.first_, c.rotation_.first_);

            } else {
78 79 80 81 82 83

                if (c.block_.first_ == nullptr) {

                    c.instance_->GetNode()->Remove();

                } else if (c.block_.second_ == nullptr) {
84
                    ///Restore free block and update block instance pointer
85 86 87 88

                }
            }
        }
魔大农's avatar
魔大农 committed
89

魔大农's avatar
魔大农 committed
90
        CheckModifiedStateChange();
魔大农's avatar
魔大农 committed
91 92 93 94 95
        return true;

    } else {

        return false;
96 97
    }
}
魔大农's avatar
魔大农 committed
98
bool History::Redo()
99
{
100
    if (steps_.Size() - stepIndex_ != 0) {
101 102 103 104 105

        for (Change c : steps_.At(stepIndex_)) {

            GridBlock* gridBlock{ static_cast<GridBlock*>(c.instance_) };

106 107 108 109 110
            if (gridBlock) {

                c.instance_->SetBlock(c.block_.second_, c.rotation_.second_);

            } else {
111 112

                if (c.block_.first_ == nullptr) {
113
                    ///Readd free block and update block instance pointer
114 115 116 117 118 119 120

                } else if (c.block_.second_ == nullptr) {

                    c.instance_->GetNode()->Remove();
                }
            }
        }
121

122
        ++stepIndex_;
魔大农's avatar
魔大农 committed
123
        CheckModifiedStateChange();
魔大农's avatar
魔大农 committed
124 125 126 127 128 129

        return true;

    } else {

        return false;
130 131
    }
}
魔大农's avatar
魔大农 committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

void History::HandleLoad()
{
    lastSavedIndex_ = stepIndex_ = 0;
    steps_.Clear();
    currentStep_.Clear();
    CheckModifiedStateChange();
}
void History::HandleSave()
{
    lastSavedIndex_ = stepIndex_;
    CheckModifiedStateChange();
}
void History::CheckModifiedStateChange()
{
    if (previousModifiedState_ != Modified()) {

        previousModifiedState_ = Modified();
150
        MainWindow::mainWindow_->handleFileModifiedStateChange();
魔大农's avatar
魔大农 committed
151 152
    }
}