1// Aseprite Document Library
2// Copyright (C) 2019-2022 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifndef DOC_BRUSH_H_INCLUDED
9#define DOC_BRUSH_H_INCLUDED
10#pragma once
11
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 <memory>
20#include <vector>
21
22namespace doc {
23
24 class Brush {
25 public:
26 static const int kMinBrushSize = 1;
27 static const int kMaxBrushSize = 64;
28
29 enum class ImageColor { MainColor, BackgroundColor };
30
31 Brush();
32 Brush(BrushType type, int size, int angle);
33 Brush(const Brush& brush);
34 ~Brush();
35
36 BrushType type() const { return m_type; }
37 int size() const { return m_size; }
38 int angle() const { return m_angle; }
39 Image* image() const { return m_image.get(); }
40 Image* maskBitmap() const { return m_maskBitmap.get(); }
41 int gen() const { return m_gen; }
42
43 BrushPattern pattern() const { return m_pattern; }
44 gfx::Point patternOrigin() const { return m_patternOrigin; }
45 Image* patternImage() const { return m_patternImage.get(); }
46
47 const gfx::Rect& bounds() const { return m_bounds; }
48 const gfx::Point& center() const { return m_center; }
49
50 void setType(BrushType type);
51 void setSize(int size);
52 void setAngle(int angle);
53 void setImage(const Image* image,
54 const Image* maskBitmap);
55
56 // Special functions to change the colors of the image or restore
57 // the colors to the original image used to create the brush.
58 void setImageColor(ImageColor imageColor, color_t color);
59 void resetImageColors();
60
61 void setPattern(BrushPattern pattern) {
62 m_pattern = pattern;
63 }
64 void setPatternOrigin(const gfx::Point& patternOrigin) {
65 m_patternOrigin = patternOrigin;
66 }
67 void setPatternImage(ImageRef& patternImage) {
68 m_patternImage = patternImage;
69 }
70 void setCenter(const gfx::Point& center);
71
72 // Returns the original image used to create the brush before
73 // calling any setImageColor()
74 Image* originalImage() const {
75 if (m_backupImage)
76 return m_backupImage.get();
77 else
78 return m_image.get();
79 }
80
81 private:
82 void clean();
83 void regenerate();
84 void resetBounds();
85
86 BrushType m_type; // Type of brush
87 int m_size; // Size (diameter)
88 int m_angle; // Angle in degrees 0-360
89 ImageRef m_image; // Image of the brush
90 ImageRef m_maskBitmap;
91 gfx::Rect m_bounds;
92 gfx::Point m_center;
93 BrushPattern m_pattern; // How the image should be replicated
94 gfx::Point m_patternOrigin; // From what position the brush was taken
95 ImageRef m_patternImage;
96 int m_gen;
97
98 // Extra data used for setImageColor()
99 ImageRef m_backupImage; // Backup image to avoid losing original brush colors/pattern
100 std::unique_ptr<color_t> m_mainColor; // Main image brush color (nullptr if it wasn't specified)
101 std::unique_ptr<color_t> m_bgColor; // Background color (nullptr if it wasn't specified)
102 };
103
104 typedef std::shared_ptr<Brush> BrushRef;
105
106} // namespace doc
107
108#endif
109