Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • structure
layer.h
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17#ifndef LAYER_H
18#define LAYER_H
19
20#include <map>
21#include <functional>
22#include <QObject>
23#include <QString>
24#include <QDomElement>
25#include "pencilerror.h"
26
27class KeyFrame;
28class Status;
29
30typedef std::function<void()> ProgressCallback;
31
32class Layer
33{
34 Q_DECLARE_TR_FUNCTIONS(Layer)
35public:
36 enum LAYER_TYPE
37 {
38 UNDEFINED = 0,
39 BITMAP = 1,
40 VECTOR = 2,
41 MOVIE = 3, // not supported yet
42 SOUND = 4,
43 CAMERA = 5,
44 };
45
46 explicit Layer(int id, LAYER_TYPE eType);
47 virtual ~Layer();
48
49 int id() const { return mId; }
50 void setId(int layerId) { mId = layerId; }
51 LAYER_TYPE type() const { return meType; }
52
53 void setName(QString name) { mName = name; }
54 QString name() const { return mName; }
55
56 void switchVisibility() { mVisible = !mVisible; }
57
58 bool visible() const { return mVisible; }
59 void setVisible(bool b) { mVisible = b; }
60
62 QList<int> selectedKeyFramesPositions() const { return mSelectedFrames_byPosition; }
63
65 QList<int> selectedKeyFramesByLast() const { return mSelectedFrames_byLast; }
66
67 virtual Status saveKeyFrameFile(KeyFrame*, QString dataPath) = 0;
68 virtual void loadDomElement(const QDomElement& element, QString dataDirPath, ProgressCallback progressForward) = 0;
69 virtual QDomElement createDomElement(QDomDocument& doc) const = 0;
70 QDomElement createBaseDomElement(QDomDocument& doc) const;
71 void loadBaseDomElement(const QDomElement& elem);
72
73 // KeyFrame interface
74 int getMaxKeyFramePosition() const;
75 int firstKeyFramePosition() const;
76
77 bool keyExists(int position) const;
78 int getPreviousKeyFramePosition(int position) const;
79 int getNextKeyFramePosition(int position) const;
80 int getPreviousFrameNumber(int position, bool isAbsolute) const;
81 int getNextFrameNumber(int position, bool isAbsolute) const;
82
83 int keyFrameCount() const { return static_cast<int>(mKeyFrames.size()); }
84 int selectedKeyFrameCount() const { return mSelectedFrames_byPosition.count(); }
85 bool hasAnySelectedFrames() const { return !mSelectedFrames_byLast.empty() && !mSelectedFrames_byPosition.empty(); }
86
90 bool insertExposureAt(int position);
91
97 bool addNewKeyFrameAt(int position);
98 void addOrReplaceKeyFrame(int position, KeyFrame* pKeyFrame);
105 virtual bool addKeyFrame(int position, KeyFrame* pKeyFrame);
106 virtual bool removeKeyFrame(int position);
107 virtual void replaceKeyFrame(const KeyFrame* pKeyFrame) = 0;
108
109 bool swapKeyFrames(int position1, int position2);
110 bool moveKeyFrame(int position, int offset);
111 KeyFrame* getKeyFrameAt(int position) const;
112 KeyFrame* getLastKeyFrameAtPosition(int position) const;
113 bool keyExistsWhichCovers(int frameNumber);
114 KeyFrame *getKeyFrameWhichCovers(int frameNumber) const;
115
116 void foreachKeyFrame(std::function<void(KeyFrame*)>) const;
117
118 void setModified(int position, bool isModified) const;
119
120 // Handle selection
121 bool isFrameSelected(int position) const;
122 void setFrameSelected(int position, bool isSelected);
123 void toggleFrameSelected(int position, bool allowMultiple = false);
124 void extendSelectionTo(int position);
125 void selectAllFramesAfter(int position);
126
136 bool newSelectionOfConnectedFrames(int position);
137
141 void setExposureForSelectedFrames(int offset);
142
146 bool reverseOrderOfSelection();
147
148 void deselectAll();
149
150 bool moveSelectedFrames(int offset);
151 QList<int> getSelectedFramesByPos() const { return mSelectedFrames_byPosition; }
152
158 bool canMoveSelectedFramesToOffset(int offset) const;
159
160 Status save(const QString& sDataFolder, QStringList& attachedFiles, ProgressCallback progressStep);
161 virtual Status presave(const QString& sDataFolder) { Q_UNUSED(sDataFolder); return Status::SAFE; }
162
163 bool isPaintable() const;
164
166 QList<int> dirtyFrames() const { return mDirtyFrames; }
167
170 void markFrameAsDirty(const int frameNumber) { mDirtyFrames << frameNumber; }
171
173 void clearDirtyFrames() { mDirtyFrames.clear(); }
174
175protected:
176 virtual KeyFrame* createKeyFrame(int position) = 0;
177 bool loadKey(KeyFrame*);
178
179private:
180 void removeFromSelectionList(int position);
181
182 LAYER_TYPE meType = UNDEFINED;
183 int mId = 0;
184 bool mVisible = true;
185 QString mName;
186
187 std::map<int, KeyFrame*, std::greater<int>> mKeyFrames;
188
189 // We need to keep track of selected frames ordered by last selected
190 // and by position.
191 // Both should be pre-sorted on each selection for optimization purpose when moving frames.
192 //
193 QList<int> mSelectedFrames_byLast; // Used to handle selection range (based on last selected
194 QList<int> mSelectedFrames_byPosition; // Used to handle frames movements on the timeline
195
196 // Used for clearing cache for modified frames.
197 QList<int> mDirtyFrames;
198};
199
200#endif
KeyFrame
Definition: keyframe.h:30
Layer
Definition: layer.h:33
Layer::markFrameAsDirty
void markFrameAsDirty(const int frameNumber)
Mark the frame position as dirty.
Definition: layer.h:170
Layer::newSelectionOfConnectedFrames
bool newSelectionOfConnectedFrames(int position)
Make a selection from specified position until a blank spot appears The search is only looking forwar...
Definition: layer.cpp:458
Layer::dirtyFrames
QList< int > dirtyFrames() const
Returns a list of dirty frame positions.
Definition: layer.h:166
Layer::addNewKeyFrameAt
bool addNewKeyFrameAt(int position)
Creates a new keyframe at the given position, unless one already exists.
Definition: layer.cpp:170
Layer::selectedKeyFramesPositions
QList< int > selectedKeyFramesPositions() const
Get selected keyframe positions sorted by position.
Definition: layer.h:62
Layer::reverseOrderOfSelection
bool reverseOrderOfSelection()
Reverse order of selected frames.
Definition: layer.cpp:612
Layer::addKeyFrame
virtual bool addKeyFrame(int position, KeyFrame *pKeyFrame)
Adds a keyframe at the given position, unless one already exists.
Definition: layer.cpp:191
Layer::selectedKeyFramesByLast
QList< int > selectedKeyFramesByLast() const
Get selected keyframe positions based on the order they were selected.
Definition: layer.h:65
Layer::setExposureForSelectedFrames
void setExposureForSelectedFrames(int offset)
Add or subtract exposure from selected frames.
Definition: layer.cpp:522
Layer::clearDirtyFrames
void clearDirtyFrames()
Clear the list of dirty keyframes.
Definition: layer.h:173
Layer::canMoveSelectedFramesToOffset
bool canMoveSelectedFramesToOffset(int offset) const
Predetermines whether the frames can be moved to a new position depending on the offset.
Definition: layer.cpp:507
Layer::insertExposureAt
bool insertExposureAt(int position)
Will insert an empty frame (exposure) after the given position.
Definition: layer.cpp:204
Status
Definition: pencilerror.h:40
QDomDocument
QDomElement
QList
QList::clear
void clear()
QList::count
int count(const T &value) const const
QList::empty
bool empty() const const
QString
QStringList
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39