1 // Aseprite Document Library
2 // Copyright (c) 2001-2017 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_BRUSH_H_INCLUDED
8 #define DOC_BRUSH_H_INCLUDED
9 #pragma once
10 
11 #include "base/unique_ptr.h"
12 #include "doc/brush_pattern.h"
13 #include "doc/brush_type.h"
14 #include "doc/color.h"
15 #include "doc/image_ref.h"
16 #include "gfx/point.h"
17 #include "gfx/rect.h"
18 
19 #include <vector>
20 
21 namespace doc {
22 
23   class Brush {
24   public:
25     static const int kMinBrushSize = 1;
26     static const int kMaxBrushSize = 64;
27 
28     enum class ImageColor { MainColor, BackgroundColor };
29 
30     Brush();
31     Brush(BrushType type, int size, int angle);
32     Brush(const Brush& brush);
33     ~Brush();
34 
type()35     BrushType type() const { return m_type; }
size()36     int size() const { return m_size; }
angle()37     int angle() const { return m_angle; }
image()38     Image* image() const { return m_image.get(); }
maskBitmap()39     Image* maskBitmap() const { return m_maskBitmap.get(); }
gen()40     int gen() const { return m_gen; }
41 
pattern()42     BrushPattern pattern() const { return m_pattern; }
patternOrigin()43     gfx::Point patternOrigin() const { return m_patternOrigin; }
44 
bounds()45     const gfx::Rect& bounds() const { return m_bounds; }
center()46     const gfx::Point& center() const { return m_center; }
47 
48     void setType(BrushType type);
49     void setSize(int size);
50     void setAngle(int angle);
51     void setImage(const Image* image,
52                   const Image* maskBitmap);
53     void setImageColor(ImageColor imageColor, color_t color);
setPattern(BrushPattern pattern)54     void setPattern(BrushPattern pattern) {
55       m_pattern = pattern;
56     }
setPatternOrigin(const gfx::Point & patternOrigin)57     void setPatternOrigin(const gfx::Point& patternOrigin) {
58       m_patternOrigin = patternOrigin;
59     }
60 
61   private:
62     void clean();
63     void regenerate();
64     void resetBounds();
65 
66     BrushType m_type;                     // Type of brush
67     int m_size;                           // Size (diameter)
68     int m_angle;                          // Angle in degrees 0-360
69     ImageRef m_image;                     // Image of the brush
70     ImageRef m_maskBitmap;
71     gfx::Rect m_bounds;
72     gfx::Point m_center;
73     BrushPattern m_pattern;               // How the image should be replicated
74     gfx::Point m_patternOrigin;           // From what position the brush was taken
75     int m_gen;
76 
77     // Extra data used for setImageColor()
78     ImageRef m_backupImage; // Backup image to avoid losing original brush colors/pattern
79     base::UniquePtr<color_t> m_mainColor; // Main image brush color (nullptr if it wasn't specified)
80     base::UniquePtr<color_t> m_bgColor;   // Background color (nullptr if it wasn't specified)
81   };
82 
83   typedef base::SharedPtr<Brush> BrushRef;
84 
85 } // namespace doc
86 
87 #endif
88