1 // Aseprite Document Library
2 // Copyright (c) 2001-2018 David Capello
3 //
4 // This file is released under the terms of the MIT license.
5 // Read LICENSE.txt for more information.
6 
7 #ifndef DOC_LAYER_H_INCLUDED
8 #define DOC_LAYER_H_INCLUDED
9 #pragma once
10 
11 #include "doc/blend_mode.h"
12 #include "doc/cel_list.h"
13 #include "doc/frame.h"
14 #include "doc/layer_list.h"
15 #include "doc/object.h"
16 #include "doc/with_user_data.h"
17 
18 #include <string>
19 
20 namespace doc {
21 
22   class Cel;
23   class Image;
24   class Sprite;
25   class Layer;
26   class LayerImage;
27   class LayerGroup;
28 
29   //////////////////////////////////////////////////////////////////////
30   // Layer class
31 
32   enum class LayerFlags {
33     None       = 0,
34     Visible    = 1,             // Can be read
35     Editable   = 2,             // Can be written
36     LockMove   = 4,             // Cannot be moved
37     Background = 8,             // Stack order cannot be changed
38     Continuous = 16,            // Prefer to link cels when the user copy them
39     Collapsed  = 32,            // Prefer to show this group layer collapsed
40     Reference  = 64,            // Is a reference layer
41 
42     PersistentFlagsMask = 0xffff,
43 
44     Internal_WasVisible = 0x10000, // Was visible in the alternative state (Alt+click)
45 
46     BackgroundLayerFlags = LockMove | Background,
47   };
48 
49   class Layer : public WithUserData {
50   protected:
51     Layer(ObjectType type, Sprite* sprite);
52 
53   public:
54     virtual ~Layer();
55 
56     virtual int getMemSize() const override;
57 
name()58     const std::string& name() const { return m_name; }
setName(const std::string & name)59     void setName(const std::string& name) { m_name = name; }
60 
sprite()61     Sprite* sprite() const { return m_sprite; }
parent()62     LayerGroup* parent() const { return m_parent; }
setParent(LayerGroup * group)63     void setParent(LayerGroup* group) { m_parent = group; }
64 
65     // Gets the previous sibling of this layer.
66     Layer* getPrevious() const;
67     Layer* getNext() const;
68 
69     Layer* getPreviousBrowsable() const;
70     Layer* getNextBrowsable() const;
71 
72     Layer* getPreviousInWholeHierarchy() const;
73     Layer* getNextInWholeHierarchy() const;
74 
isImage()75     bool isImage() const { return type() == ObjectType::LayerImage; }
isGroup()76     bool isGroup() const { return type() == ObjectType::LayerGroup; }
isBrowsable()77     virtual bool isBrowsable() const { return false; }
78 
isBackground()79     bool isBackground() const  { return hasFlags(LayerFlags::Background); }
isTransparent()80     bool isTransparent() const { return !hasFlags(LayerFlags::Background); }
isVisible()81     bool isVisible() const     { return hasFlags(LayerFlags::Visible); }
isEditable()82     bool isEditable() const    { return hasFlags(LayerFlags::Editable); }
isMovable()83     bool isMovable() const     { return !hasFlags(LayerFlags::LockMove); }
isContinuous()84     bool isContinuous() const  { return hasFlags(LayerFlags::Continuous); }
isCollapsed()85     bool isCollapsed() const   { return hasFlags(LayerFlags::Collapsed); }
isExpanded()86     bool isExpanded() const    { return !hasFlags(LayerFlags::Collapsed); }
isReference()87     bool isReference() const   { return hasFlags(LayerFlags::Reference); }
88 
89     bool isVisibleHierarchy() const;
90     bool isEditableHierarchy() const;
91 
setBackground(bool state)92     void setBackground(bool state) { switchFlags(LayerFlags::Background, state); }
setVisible(bool state)93     void setVisible   (bool state) { switchFlags(LayerFlags::Visible, state); }
setEditable(bool state)94     void setEditable  (bool state) { switchFlags(LayerFlags::Editable, state); }
setMovable(bool state)95     void setMovable   (bool state) { switchFlags(LayerFlags::LockMove, !state); }
setContinuous(bool state)96     void setContinuous(bool state) { switchFlags(LayerFlags::Continuous, state); }
setCollapsed(bool state)97     void setCollapsed (bool state) { switchFlags(LayerFlags::Collapsed, state); }
setReference(bool state)98     void setReference (bool state) { switchFlags(LayerFlags::Reference, state); }
99 
flags()100     LayerFlags flags() const {
101       return m_flags;
102     }
103 
hasFlags(LayerFlags flags)104     bool hasFlags(LayerFlags flags) const {
105       return (int(m_flags) & int(flags)) == int(flags);
106     }
107 
setFlags(LayerFlags flags)108     void setFlags(LayerFlags flags) {
109       m_flags = flags;
110     }
111 
switchFlags(LayerFlags flags,bool state)112     void switchFlags(LayerFlags flags, bool state) {
113       if (state)
114         m_flags = LayerFlags(int(m_flags) | int(flags));
115       else
116         m_flags = LayerFlags(int(m_flags) & ~int(flags));
117     }
118 
119     virtual Cel* cel(frame_t frame) const;
120     virtual void getCels(CelList& cels) const = 0;
121     virtual void displaceFrames(frame_t fromThis, frame_t delta) = 0;
122 
123   private:
124     std::string m_name;           // layer name
125     Sprite* m_sprite;             // owner of the layer
126     LayerGroup* m_parent;        // parent layer
127     LayerFlags m_flags;           // stack order cannot be changed
128 
129     // Disable assigment
130     Layer& operator=(const Layer& other);
131   };
132 
133   //////////////////////////////////////////////////////////////////////
134   // LayerImage class
135 
136   class LayerImage : public Layer {
137   public:
138     explicit LayerImage(Sprite* sprite);
139     virtual ~LayerImage();
140 
141     virtual int getMemSize() const override;
142 
blendMode()143     BlendMode blendMode() const { return m_blendmode; }
setBlendMode(BlendMode blendmode)144     void setBlendMode(BlendMode blendmode) { m_blendmode = blendmode; }
145 
opacity()146     int opacity() const { return m_opacity; }
setOpacity(int opacity)147     void setOpacity(int opacity) { m_opacity = opacity; }
148 
149     void addCel(Cel *cel);
150     void removeCel(Cel *cel);
151     void moveCel(Cel *cel, frame_t frame);
152 
153     Cel* cel(frame_t frame) const override;
154     void getCels(CelList& cels) const override;
155     void displaceFrames(frame_t fromThis, frame_t delta) override;
156 
157     Cel* getLastCel() const;
158     CelConstIterator findCelIterator(frame_t frame) const;
159     CelIterator findCelIterator(frame_t frame);
160     CelIterator findFirstCelIteratorAfter(frame_t firstAfterFrame);
161 
162     void configureAsBackground();
163 
getCelBegin()164     CelIterator getCelBegin() { return m_cels.begin(); }
getCelEnd()165     CelIterator getCelEnd() { return m_cels.end(); }
getCelBegin()166     CelConstIterator getCelBegin() const { return m_cels.begin(); }
getCelEnd()167     CelConstIterator getCelEnd() const { return m_cels.end(); }
getCelsCount()168     int getCelsCount() const { return (int)m_cels.size(); }
169 
170   private:
171     void destroyAllCels();
172 
173     BlendMode m_blendmode;
174     int m_opacity;
175     CelList m_cels;   // List of all cels inside this layer used by frames.
176   };
177 
178   //////////////////////////////////////////////////////////////////////
179   // LayerGroup class
180 
181   class LayerGroup : public Layer {
182   public:
183     explicit LayerGroup(Sprite* sprite);
184     virtual ~LayerGroup();
185 
186     virtual int getMemSize() const override;
187 
layers()188     const LayerList& layers() const { return m_layers; }
layersCount()189     int layersCount() const { return (int)m_layers.size(); }
190 
191     void addLayer(Layer* layer);
192     void removeLayer(Layer* layer);
193     void insertLayer(Layer* layer, Layer* after);
194     void stackLayer(Layer* layer, Layer* after);
195 
firstLayer()196     Layer* firstLayer() const { return (m_layers.empty() ? nullptr: m_layers.front()); }
197     Layer* firstLayerInWholeHierarchy() const;
lastLayer()198     Layer* lastLayer() const { return (m_layers.empty() ? nullptr: m_layers.back()); }
199 
200     void allLayers(LayerList& list) const;
201     layer_t allLayersCount() const;
202     bool hasVisibleReferenceLayers() const;
203     void allVisibleLayers(LayerList& list) const;
204     void allVisibleReferenceLayers(LayerList& list) const;
205     void allBrowsableLayers(LayerList& list) const;
206 
207     void getCels(CelList& cels) const override;
208     void displaceFrames(frame_t fromThis, frame_t delta) override;
209 
isBrowsable()210     bool isBrowsable() const override {
211       return isGroup() && isExpanded() && !m_layers.empty();
212     }
213 
214   private:
215     void destroyAllLayers();
216 
217     LayerList m_layers;
218   };
219 
220 } // namespace doc
221 
222 #endif
223