1// Aseprite
2// Copyright (C) 2019 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifndef APP_DOC_API_H_INCLUDED
9#define APP_DOC_API_H_INCLUDED
10#pragma once
11
12#include "app/drop_frame_place.h"
13#include "app/tags_handling.h"
14#include "doc/algorithm/flip_type.h"
15#include "doc/color.h"
16#include "doc/frame.h"
17#include "doc/image_ref.h"
18#include "gfx/rect.h"
19
20#include <map>
21
22namespace doc {
23 class Cel;
24 class CelData;
25 class Image;
26 class Layer;
27 class LayerGroup;
28 class LayerImage;
29 class Mask;
30 class Palette;
31 class Sprite;
32}
33
34namespace app {
35 class Doc;
36 class Transaction;
37
38 using namespace doc;
39
40 // Old high-level API. The new way is to create Cmds and add them
41 // directly to the transaction.
42 //
43 // TODO refactor this class in several Cmd, don't make it bigger
44 class DocApi {
45 public:
46 DocApi(Doc* document, Transaction& transaction);
47
48 // Sprite API
49 void setSpriteSize(Sprite* sprite, int w, int h);
50 void setSpriteTransparentColor(Sprite* sprite, color_t maskColor);
51 void cropSprite(Sprite* sprite,
52 const gfx::Rect& bounds,
53 const bool trimOutside = false);
54 void trimSprite(Sprite* sprite, const bool byGrid);
55
56 // Frames API
57 void addFrame(Sprite* sprite, frame_t newFrame);
58 void addEmptyFrame(Sprite* sprite, frame_t newFrame);
59 void addEmptyFramesTo(Sprite* sprite, frame_t newFrame);
60 void copyFrame(Sprite* sprite,
61 frame_t fromFrame,
62 const frame_t newFrame,
63 const DropFramePlace dropFramePlace,
64 const TagsHandling tagsHandling);
65 void removeFrame(Sprite* sprite, frame_t frame);
66 void setTotalFrames(Sprite* sprite, frame_t frames);
67 void setFrameDuration(Sprite* sprite, frame_t frame, int msecs);
68 void setFrameRangeDuration(Sprite* sprite, frame_t from, frame_t to, int msecs);
69 void moveFrame(Sprite* sprite,
70 const frame_t frame,
71 frame_t targetFrame,
72 const DropFramePlace dropFramePlace,
73 const TagsHandling tagsHandling);
74
75 // Cels API
76 void addCel(LayerImage* layer, Cel* cel);
77 Cel* addCel(LayerImage* layer, frame_t frameNumber, const ImageRef& image);
78 void clearCel(Layer* layer, frame_t frame);
79 void clearCel(Cel* cel);
80 void clearCelAndAllLinks(Cel* cel);
81 void setCelPosition(Sprite* sprite, Cel* cel, int x, int y);
82 void setCelOpacity(Sprite* sprite, Cel* cel, int newOpacity);
83 void moveCel(
84 LayerImage* srcLayer, frame_t srcFrame,
85 LayerImage* dstLayer, frame_t dstFrame);
86 void copyCel(
87 LayerImage* srcLayer, frame_t srcFrame,
88 LayerImage* dstLayer, frame_t dstFrame,
89 const bool* forceContinuous = nullptr);
90 void swapCel(
91 LayerImage* layer, frame_t frame1, frame_t frame2);
92
93 // Layers API
94 LayerImage* newLayer(LayerGroup* parent, const std::string& name);
95 LayerGroup* newGroup(LayerGroup* parent, const std::string& name);
96 void addLayer(LayerGroup* parent, Layer* newLayer, Layer* afterThis);
97 void removeLayer(Layer* layer);
98 void restackLayerAfter(Layer* layer, LayerGroup* parent, Layer* afterThis);
99 void restackLayerBefore(Layer* layer, LayerGroup* parent, Layer* beforeThis);
100 Layer* duplicateLayerAfter(Layer* sourceLayer, LayerGroup* parent, Layer* afterLayer);
101 Layer* duplicateLayerBefore(Layer* sourceLayer, LayerGroup* parent, Layer* beforeLayer);
102
103 // Images API
104 void replaceImage(Sprite* sprite, const ImageRef& oldImage, const ImageRef& newImage);
105
106 // Image API
107 void flipImage(Image* image, const gfx::Rect& bounds, doc::algorithm::FlipType flipType);
108 void flipImageWithMask(Layer* layer, Image* image, doc::algorithm::FlipType flipType);
109
110 // Mask API
111 void copyToCurrentMask(Mask* mask);
112 void setMaskPosition(int x, int y);
113
114 // Palette API
115 void setPalette(Sprite* sprite, frame_t frame, const Palette* newPalette);
116
117 private:
118 void cropImageLayer(LayerImage* layer,
119 const gfx::Rect& bounds,
120 const bool trimOutside);
121 bool cropCel(LayerImage* layer,
122 Cel* cel,
123 const gfx::Rect& bounds,
124 const bool trimOutside);
125 void setCelFramePosition(Cel* cel, frame_t frame);
126 void moveFrameLayer(Layer* layer, frame_t frame, frame_t beforeFrame);
127 void adjustTags(Sprite* sprite,
128 const frame_t frame,
129 const frame_t delta,
130 const DropFramePlace dropFramePlace,
131 const TagsHandling tagsHandling);
132
133 class HandleLinkedCels {
134 public:
135 HandleLinkedCels(
136 DocApi& api,
137 doc::LayerImage* srcLayer, const doc::frame_t srcFrame,
138 doc::LayerImage* dstLayer, const doc::frame_t dstFrame);
139 ~HandleLinkedCels();
140 bool linkWasCreated() { return m_created; }
141 private:
142 DocApi& m_api;
143 doc::ObjectId m_srcDataId;
144 doc::Layer* m_dstLayer;
145 doc::frame_t m_dstFrame;
146 bool m_created;
147 };
148
149 bool copyFromLinkedCels(Cel** srcCel,
150 doc::ObjectId& srcDataId);
151
152 Doc* m_document;
153 Transaction& m_transaction;
154
155 // Map used in copyCel() to re-create the original set of linked
156 // cels from the src layers when we copy a block of cels.
157 // map: ObjectId of CelData -> Cel*
158 std::map<doc::ObjectId, doc::Cel*> m_linkedCels;
159 };
160
161} // namespace app
162
163#endif
164