1 /*
2  * PROJECT:    PAINT for ReactOS
3  * LICENSE:    LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
4  * PURPOSE:    Keep track of tool parameters, notify listeners
5  * COPYRIGHT:  Copyright 2015 Benedikt Freisen <b.freisen@gmx.net>
6  */
7 
8 #pragma once
9 
10 enum TOOLTYPE
11 {
12     TOOL_FREESEL  =  1,
13     TOOL_RECTSEL  =  2,
14     TOOL_RUBBER   =  3,
15     TOOL_FILL     =  4,
16     TOOL_COLOR    =  5,
17     TOOL_ZOOM     =  6,
18     TOOL_PEN      =  7,
19     TOOL_BRUSH    =  8,
20     TOOL_AIRBRUSH =  9,
21     TOOL_TEXT     = 10,
22     TOOL_LINE     = 11,
23     TOOL_BEZIER   = 12,
24     TOOL_RECT     = 13,
25     TOOL_SHAPE    = 14,
26     TOOL_ELLIPSE  = 15,
27     TOOL_RRECT    = 16,
28     TOOL_MAX = TOOL_RRECT,
29 };
30 
31 enum BrushStyle
32 {
33     BrushStyleRound,
34     BrushStyleSquare,
35     BrushStyleForeSlash,
36     BrushStyleBackSlash,
37 };
38 
39 /* CLASSES **********************************************************/
40 
41 struct ToolBase
42 {
43     HDC m_hdc;
44     COLORREF m_fg, m_bg;
45 
ToolBaseToolBase46     ToolBase() : m_hdc(NULL) { }
~ToolBaseToolBase47     virtual ~ToolBase() { }
48 
OnButtonDownToolBase49     virtual void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) { }
OnMouseMoveToolBase50     virtual BOOL OnMouseMove(BOOL bLeftButton, LONG& x, LONG& y) { return TRUE; }
OnButtonUpToolBase51     virtual BOOL OnButtonUp(BOOL bLeftButton, LONG& x, LONG& y) { return TRUE; }
52 
OnDrawOverlayOnImageToolBase53     virtual void OnDrawOverlayOnImage(HDC hdc) { }
OnDrawOverlayOnCanvasToolBase54     virtual void OnDrawOverlayOnCanvas(HDC hdc) { }
55 
OnSpecialTweakToolBase56     virtual void OnSpecialTweak(BOOL bMinus) { }
57 
58     virtual void OnEndDraw(BOOL bCancel);
59 
60     void beginEvent();
61     void endEvent();
62     void reset();
63 
64     static ToolBase* createToolObject(TOOLTYPE type);
65 };
66 
67 class ToolsModel
68 {
69 private:
70     int m_lineWidth;
71     INT m_penWidth;
72     INT m_brushWidth;
73     int m_shapeStyle;
74     BrushStyle m_brushStyle;
75     TOOLTYPE m_activeTool;
76     TOOLTYPE m_oldActiveTool;
77     INT m_airBrushRadius;
78     int m_rubberRadius;
79     BOOL m_transpBg;
80     int m_zoom;
81     ToolBase *m_pToolObject;
82 
83     ToolBase *GetOrCreateTool(TOOLTYPE nTool);
84 
85 public:
86     ToolsModel();
87     ~ToolsModel();
88 
89     BOOL IsSelection() const;
90 
91     int GetLineWidth() const;
92     void SetLineWidth(int nLineWidth);
93     void MakeLineThickerOrThinner(BOOL bThinner);
94 
95     INT GetPenWidth() const;
96     void SetPenWidth(INT nPenWidth);
97     void MakePenThickerOrThinner(BOOL bThinner);
98 
99     int GetShapeStyle() const;
100     void SetShapeStyle(int nShapeStyle);
101 
102     INT GetBrushWidth() const;
103     void SetBrushWidth(INT nBrushWidth);
104     void MakeBrushThickerOrThinner(BOOL bThinner);
105 
106     BrushStyle GetBrushStyle() const;
107     void SetBrushStyle(BrushStyle nBrushStyle);
108 
109     TOOLTYPE GetActiveTool() const;
110     TOOLTYPE GetOldActiveTool() const;
111     void SetActiveTool(TOOLTYPE nActiveTool);
112 
113     INT GetAirBrushRadius() const;
114     void SetAirBrushRadius(INT nAirBrushRadius);
115     void MakeAirBrushThickerOrThinner(BOOL bThinner);
116 
117     int GetRubberRadius() const;
118     void SetRubberRadius(int nRubberRadius);
119     void MakeRubberThickerOrThinner(BOOL bThinner);
120 
121     SIZE GetToolSize() const;
122 
123     BOOL IsBackgroundTransparent() const;
124     void SetBackgroundTransparent(BOOL bTransparent);
125 
126     int GetZoom() const;
127     void SetZoom(int nZoom);
128 
129     void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick);
130     void OnMouseMove(BOOL bLeftButton, LONG x, LONG y);
131     void OnButtonUp(BOOL bLeftButton, LONG x, LONG y);
132     void OnEndDraw(BOOL bCancel);
133     void OnDrawOverlayOnImage(HDC hdc);
134     void OnDrawOverlayOnCanvas(HDC hdc);
135 
136     void resetTool();
137     void selectAll();
138 
139     void NotifyToolChanged();
140     void NotifyToolSettingsChanged();
141     void NotifyZoomChanged();
142 
143     void SpecialTweak(BOOL bMinus);
144 
145     void DrawWithMouseTool(POINT pt, WPARAM wParam);
146 };
147 
148 extern ToolsModel toolsModel;
149 
Zoomed(int xy)150 static inline int Zoomed(int xy)
151 {
152     return xy * toolsModel.GetZoom() / 1000;
153 }
154 
UnZoomed(int xy)155 static inline int UnZoomed(int xy)
156 {
157     return xy * 1000 / toolsModel.GetZoom();
158 }
159 
Zoomed(POINT & pt)160 static inline void Zoomed(POINT& pt)
161 {
162     pt = { Zoomed(pt.x), Zoomed(pt.y) };
163 }
164 
Zoomed(RECT & rc)165 static inline void Zoomed(RECT& rc)
166 {
167     rc = { Zoomed(rc.left), Zoomed(rc.top), Zoomed(rc.right), Zoomed(rc.bottom) };
168 }
169 
UnZoomed(POINT & pt)170 static inline void UnZoomed(POINT& pt)
171 {
172     pt = { UnZoomed(pt.x), UnZoomed(pt.y) };
173 }
174 
UnZoomed(RECT & rc)175 static inline void UnZoomed(RECT& rc)
176 {
177     rc = { UnZoomed(rc.left), UnZoomed(rc.top), UnZoomed(rc.right), UnZoomed(rc.bottom) };
178 }
179