1 // Aseprite
2 // Copyright (C) 2001-2018  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "app/cmd/clear_rect.h"
12 
13 #include "app/doc.h"
14 #include "doc/cel.h"
15 #include "doc/image.h"
16 #include "doc/layer.h"
17 #include "doc/primitives.h"
18 
19 namespace app {
20 namespace cmd {
21 
22 using namespace doc;
23 
ClearRect(Cel * cel,const gfx::Rect & bounds)24 ClearRect::ClearRect(Cel* cel, const gfx::Rect& bounds)
25 {
26   ASSERT(cel);
27 
28   Image* image = cel->image();
29   if (!image)
30     return;
31 
32   m_offsetX = bounds.x - cel->x();
33   m_offsetY = bounds.y - cel->y();
34 
35   gfx::Rect bounds2 =
36     image->bounds().createIntersection(
37       gfx::Rect(
38         m_offsetX, m_offsetY,
39         bounds.w, bounds.h));
40   if (bounds.isEmpty())
41     return;
42 
43   m_dstImage.reset(new WithImage(image));
44 
45   Doc* doc = static_cast<Doc*>(cel->document());
46   m_bgcolor = doc->bgColor(cel->layer());
47 
48   m_copy.reset(crop_image(image,
49       bounds2.x, bounds2.y, bounds2.w, bounds2.h, m_bgcolor));
50 }
51 
onExecute()52 void ClearRect::onExecute()
53 {
54   m_seq.execute(context());
55   if (m_dstImage)
56     clear();
57 }
58 
onUndo()59 void ClearRect::onUndo()
60 {
61   if (m_dstImage)
62     restore();
63   m_seq.undo();
64 }
65 
onRedo()66 void ClearRect::onRedo()
67 {
68   m_seq.redo();
69   if (m_dstImage)
70     clear();
71 }
72 
clear()73 void ClearRect::clear()
74 {
75   fill_rect(m_dstImage->image(),
76             m_offsetX, m_offsetY,
77             m_offsetX + m_copy->width() - 1,
78             m_offsetY + m_copy->height() - 1,
79             m_bgcolor);
80 }
81 
restore()82 void ClearRect::restore()
83 {
84   copy_image(m_dstImage->image(), m_copy.get(), m_offsetX, m_offsetY);
85 }
86 
87 } // namespace cmd
88 } // namespace app
89