forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.h
269 lines (204 loc) · 8.22 KB
/
doc.h
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
// Aseprite
// Copyright (C) 2018-2020 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.
#ifndef APP_DOC_H_INCLUDED
#define APP_DOC_H_INCLUDED
#pragma once
#include "app/doc_observer.h"
#include "app/extra_cel.h"
#include "app/file/format_options.h"
#include "app/transformation.h"
#include "base/disable_copying.h"
#include "base/mutex.h"
#include "base/rw_lock.h"
#include "doc/blend_mode.h"
#include "doc/color.h"
#include "doc/document.h"
#include "doc/frame.h"
#include "doc/mask_boundaries.h"
#include "doc/pixel_format.h"
#include "gfx/rect.h"
#include "obs/observable.h"
#include "os/color_space.h"
#include <atomic>
#include <memory>
#include <string>
namespace doc {
class Cel;
class Layer;
class Mask;
class Sprite;
}
namespace gfx {
class Region;
}
namespace app {
class Context;
class DocApi;
class DocUndo;
class Transaction;
using namespace doc;
enum DuplicateType {
DuplicateExactCopy,
DuplicateWithFlattenLayers,
};
// An application document. It is the class used to contain one file
// opened and being edited by the user (a sprite).
class Doc : public doc::Document,
public obs::observable<DocObserver> {
enum Flags {
kAssociatedToFile = 1, // This sprite is associated to a file in the file-system
kMaskVisible = 2, // The mask wasn't hidden by the user
kInhibitBackup = 4, // Inhibit the backup process
kFullyBackedUp = 8, // Full backup was done
};
public:
Doc(Sprite* sprite);
~Doc();
Context* context() const { return m_ctx; }
void setContext(Context* ctx);
// Lock/unlock API (RWLock wrapper)
bool canWriteLockFromRead() const;
bool readLock(int timeout);
bool writeLock(int timeout);
bool upgradeToWrite(int timeout);
void downgradeToRead();
void unlock();
bool weakLock(std::atomic<base::RWLock::WeakLock>* weak_lock_flag);
void weakUnlock();
// Sets active/running transaction.
void setTransaction(Transaction* transaction);
Transaction* transaction() { return m_transaction; }
// Returns a high-level API: observable and undoable methods.
DocApi getApi(Transaction& transaction);
//////////////////////////////////////////////////////////////////////
// Main properties
const DocUndo* undoHistory() const { return m_undo.get(); }
DocUndo* undoHistory() { return m_undo.get(); }
color_t bgColor() const;
color_t bgColor(Layer* layer) const;
os::ColorSpacePtr osColorSpace() const { return m_osColorSpace; }
//////////////////////////////////////////////////////////////////////
// Notifications
void notifyGeneralUpdate();
void notifyColorSpaceChanged();
void notifyPaletteChanged();
void notifySpritePixelsModified(Sprite* sprite, const gfx::Region& region, frame_t frame);
void notifyExposeSpritePixels(Sprite* sprite, const gfx::Region& region);
void notifyLayerMergedDown(Layer* srcLayer, Layer* targetLayer);
void notifyCelMoved(Layer* fromLayer, frame_t fromFrame, Layer* toLayer, frame_t toFrame);
void notifyCelCopied(Layer* fromLayer, frame_t fromFrame, Layer* toLayer, frame_t toFrame);
void notifySelectionChanged();
void notifySelectionBoundariesChanged();
//////////////////////////////////////////////////////////////////////
// File related properties
bool isModified() const;
bool isAssociatedToFile() const;
void markAsSaved();
// You can use this to indicate that we've destroyed (or we cannot
// trust) the file associated with the document (e.g. when we
// cancel a Save operation in the middle). So it's impossible to
// back to the saved state using the UndoHistory.
void impossibleToBackToSavedState();
// Returns true if it does make sense to create a backup in this
// document. For example, it doesn't make sense to create a backup
// for an unmodified document.
bool needsBackup() const;
// Can be used to avoid creating a backup when the file is in a
// unusual temporary state (e.g. when the file is resized to be
// exported with other size)
bool inhibitBackup() const;
void setInhibitBackup(const bool inhibitBackup);
void markAsBackedUp();
bool isFullyBackedUp() const;
//////////////////////////////////////////////////////////////////////
// Loaded options from file
void setFormatOptions(const FormatOptionsPtr& format_options);
FormatOptionsPtr formatOptions() const { return m_format_options; }
//////////////////////////////////////////////////////////////////////
// Boundaries
void destroyMaskBoundaries();
void generateMaskBoundaries(const Mask* mask = nullptr);
const MaskBoundaries& maskBoundaries() const {
return m_maskBoundaries;
}
MaskBoundaries& maskBoundaries() {
return m_maskBoundaries;
}
bool hasMaskBoundaries() const {
return !m_maskBoundaries.isEmpty();
}
//////////////////////////////////////////////////////////////////////
// Extra Cel (it is used to draw pen preview, pixels in movement, etc.)
ExtraCelRef extraCel() const { return m_extraCel; }
void setExtraCel(const ExtraCelRef& extraCel) { m_extraCel = extraCel; }
//////////////////////////////////////////////////////////////////////
// Mask
// Returns the current mask, it can be empty. The mask could be not
// empty but hidden to the user if the setMaskVisible(false) was
// used called before.
Mask* mask() const { return m_mask.get(); }
// Sets the current mask. The new mask will be visible by default,
// so you don't need to call setMaskVisible(true).
void setMask(const Mask* mask);
// Returns true only when the mask is not empty, and was not yet
// hidden using setMaskVisible (e.g. when the user "deselect the
// mask").
bool isMaskVisible() const;
// Changes the visibility state of the mask (it is useful only if
// the getMask() is not empty and the user can see that the mask is
// being hidden and shown to him).
void setMaskVisible(bool visible);
//////////////////////////////////////////////////////////////////////
// Transformation
Transformation getTransformation() const;
void setTransformation(const Transformation& transform);
void resetTransformation();
//////////////////////////////////////////////////////////////////////
// Last point used to draw straight lines using freehand tools + Shift key
// (EditorCustomizationDelegate::isStraightLineFromLastPoint() modifier)
static gfx::Point NoLastDrawingPoint();
gfx::Point lastDrawingPoint() const { return m_lastDrawingPoint; }
void setLastDrawingPoint(const gfx::Point& pos) { m_lastDrawingPoint = pos; }
//////////////////////////////////////////////////////////////////////
// Copying
void copyLayerContent(const Layer* sourceLayer, Doc* destDoc, Layer* destLayer) const;
Doc* duplicate(DuplicateType type) const;
void close();
protected:
void onFileNameChange() override;
virtual void onContextChanged();
private:
void removeFromContext();
void updateOSColorSpace(bool appWideSignal);
// The document is in the collection of documents of this context.
Context* m_ctx;
// Internal states of the document.
int m_flags;
// Read-Write locks.
base::RWLock m_rwLock;
// Undo and redo information about the document.
std::unique_ptr<DocUndo> m_undo;
// Current transaction for this document (when this is commit(), a
// new undo command is added to m_undo).
Transaction* m_transaction;
// Selected mask region boundaries
doc::MaskBoundaries m_maskBoundaries;
// Data to save the file in the same format that it was loaded
FormatOptionsPtr m_format_options;
// Extra cel used to draw extra stuff (e.g. editor's pen preview, pixels in movement, etc.)
ExtraCelRef m_extraCel;
// Current mask.
std::unique_ptr<doc::Mask> m_mask;
// Current transformation.
Transformation m_transformation;
gfx::Point m_lastDrawingPoint;
// Last used color space to render a sprite.
os::ColorSpacePtr m_osColorSpace;
DISABLE_COPYING(Doc);
};
} // namespace app
#endif