forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_transaction.cpp
171 lines (146 loc) · 3.83 KB
/
cmd_transaction.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
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2001-2018 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/cmd_transaction.h"
#include "app/context.h"
#include "app/site.h"
#ifdef ENABLE_UI
#include "app/app.h"
#include "app/ui/timeline/timeline.h"
#endif
namespace app {
CmdTransaction::CmdTransaction(const std::string& label,
bool changeSavedState,
int* savedCounter)
: m_ranges(nullptr)
, m_label(label)
, m_changeSavedState(changeSavedState)
, m_savedCounter(savedCounter)
{
}
CmdTransaction* CmdTransaction::moveToEmptyCopy()
{
CmdTransaction* copy = new CmdTransaction(m_label,
m_changeSavedState,
m_savedCounter);
copy->m_spritePositionBefore = m_spritePositionBefore;
copy->m_spritePositionAfter = m_spritePositionAfter;
if (m_ranges) {
copy->m_ranges.reset(new Ranges);
copy->m_ranges->m_before = std::move(m_ranges->m_before);
copy->m_ranges->m_after = std::move(m_ranges->m_after);
}
return copy;
}
void CmdTransaction::setNewDocRange(const DocRange& range)
{
#ifdef ENABLE_UI
if (m_ranges)
range.write(m_ranges->m_after);
#endif
}
void CmdTransaction::updateSpritePositionAfter()
{
m_spritePositionAfter = calcSpritePosition();
// We cannot capture m_ranges->m_after from the Timeline here
// because the document range in the Timeline is updated after the
// commit/command (on Timeline::onAfterCommandExecution).
//
// So m_ranges->m_after is captured explicitly in
// setNewDocRange().
}
std::istream* CmdTransaction::documentRangeBeforeExecute() const
{
if (m_ranges && m_ranges->m_before.tellp() > 0) {
m_ranges->m_before.seekg(0);
return &m_ranges->m_before;
}
else
return nullptr;
}
std::istream* CmdTransaction::documentRangeAfterExecute() const
{
if (m_ranges && m_ranges->m_after.tellp() > 0) {
m_ranges->m_after.seekg(0);
return &m_ranges->m_after;
}
else
return nullptr;
}
void CmdTransaction::onExecute()
{
CmdSequence::onExecute();
// The execution of CmdTransaction is called by Transaction at the
// very beginning, just to save the current sprite position.
m_spritePositionBefore = calcSpritePosition();
#ifdef ENABLE_UI
if (isDocRangeEnabled()) {
m_ranges.reset(new Ranges);
calcDocRange().write(m_ranges->m_before);
}
#endif
if (m_changeSavedState)
++(*m_savedCounter);
}
void CmdTransaction::onUndo()
{
CmdSequence::onUndo();
if (m_changeSavedState)
--(*m_savedCounter);
}
void CmdTransaction::onRedo()
{
CmdSequence::onRedo();
if (m_changeSavedState)
++(*m_savedCounter);
}
std::string CmdTransaction::onLabel() const
{
return m_label;
}
size_t CmdTransaction::onMemSize() const
{
size_t size = CmdSequence::onMemSize();
if (m_ranges) {
size += (m_ranges->m_before.tellp() +
m_ranges->m_after.tellp());
}
return size;
}
SpritePosition CmdTransaction::calcSpritePosition() const
{
Site site = context()->activeSite();
return SpritePosition(site.layer(), site.frame());
}
bool CmdTransaction::isDocRangeEnabled() const
{
#ifdef ENABLE_UI
if (App::instance()) {
Timeline* timeline = App::instance()->timeline();
if (timeline && timeline->range().enabled())
return true;
}
#endif
return false;
}
DocRange CmdTransaction::calcDocRange() const
{
#ifdef ENABLE_UI
// TODO We cannot use Context::activeSite() because it losts
// important information about the DocRange() (type and
// flags).
if (App::instance()) {
Timeline* timeline = App::instance()->timeline();
if (timeline)
return timeline->range();
}
#endif
return DocRange();
}
} // namespace app