forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_undo.cpp
More file actions
163 lines (136 loc) · 3.24 KB
/
document_undo.cpp
File metadata and controls
163 lines (136 loc) · 3.24 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
// Aseprite
// Copyright (C) 2001-2016 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/document_undo.h"
#include "app/app.h"
#include "app/cmd.h"
#include "app/cmd_transaction.h"
#include "app/document_undo_observer.h"
#include "app/pref/preferences.h"
#include "doc/context.h"
#include "undo/undo_history.h"
#include "undo/undo_state.h"
#include <cassert>
#include <stdexcept>
namespace app {
DocumentUndo::DocumentUndo()
: m_ctx(NULL)
, m_savedCounter(0)
, m_savedStateIsLost(false)
{
}
void DocumentUndo::setContext(doc::Context* ctx)
{
m_ctx = ctx;
}
void DocumentUndo::add(CmdTransaction* cmd)
{
ASSERT(cmd);
// A linear undo history is the default behavior
if (!App::instance() ||
!App::instance()->preferences().undo.allowNonlinearHistory()) {
clearRedo();
}
m_undoHistory.add(cmd);
notify_observers(&DocumentUndoObserver::onAddUndoState, this);
}
bool DocumentUndo::canUndo() const
{
return m_undoHistory.canUndo();
}
bool DocumentUndo::canRedo() const
{
return m_undoHistory.canRedo();
}
void DocumentUndo::undo()
{
m_undoHistory.undo();
notify_observers(&DocumentUndoObserver::onAfterUndo, this);
}
void DocumentUndo::redo()
{
m_undoHistory.redo();
notify_observers(&DocumentUndoObserver::onAfterRedo, this);
}
void DocumentUndo::clearRedo()
{
m_undoHistory.clearRedo();
notify_observers(&DocumentUndoObserver::onClearRedo, this);
}
bool DocumentUndo::isSavedState() const
{
return (!m_savedStateIsLost && m_savedCounter == 0);
}
void DocumentUndo::markSavedState()
{
m_savedCounter = 0;
m_savedStateIsLost = false;
}
void DocumentUndo::impossibleToBackToSavedState()
{
m_savedStateIsLost = true;
}
std::string DocumentUndo::nextUndoLabel() const
{
const undo::UndoState* state = nextUndo();
if (state)
return static_cast<Cmd*>(state->cmd())->label();
else
return "";
}
std::string DocumentUndo::nextRedoLabel() const
{
const undo::UndoState* state = nextRedo();
if (state)
return static_cast<const Cmd*>(state->cmd())->label();
else
return "";
}
SpritePosition DocumentUndo::nextUndoSpritePosition() const
{
const undo::UndoState* state = nextUndo();
if (state)
return static_cast<const CmdTransaction*>(state->cmd())
->spritePositionBeforeExecute();
else
return SpritePosition();
}
SpritePosition DocumentUndo::nextRedoSpritePosition() const
{
const undo::UndoState* state = nextRedo();
if (state)
return static_cast<const CmdTransaction*>(state->cmd())
->spritePositionAfterExecute();
else
return SpritePosition();
}
Cmd* DocumentUndo::lastExecutedCmd() const
{
const undo::UndoState* state = m_undoHistory.currentState();
if (state)
return static_cast<Cmd*>(state->cmd());
else
return NULL;
}
void DocumentUndo::moveToState(const undo::UndoState* state)
{
m_undoHistory.moveTo(state);
}
const undo::UndoState* DocumentUndo::nextUndo() const
{
return m_undoHistory.currentState();
}
const undo::UndoState* DocumentUndo::nextRedo() const
{
const undo::UndoState* state = m_undoHistory.currentState();
if (state)
return state->next();
else
return m_undoHistory.firstState();
}
} // namespace app