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     TOOLTYPE m_tool;
44     HDC m_hdc;
45     COLORREF m_fg, m_bg;
46     static INT s_pointSP;
47     static POINT s_pointStack[256];
48 
49     ToolBase(TOOLTYPE tool) : m_tool(tool), m_hdc(NULL) { }
50     virtual ~ToolBase() { }
51 
52     virtual void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) { }
53     virtual BOOL OnMouseMove(BOOL bLeftButton, LONG& x, LONG& y) { return TRUE; }
54     virtual BOOL OnButtonUp(BOOL bLeftButton, LONG& x, LONG& y) { return TRUE; }
55 
56     virtual void OnDrawOverlayOnImage(HDC hdc) { }
57     virtual void OnDrawOverlayOnCanvas(HDC hdc) { }
58 
59     virtual void OnSpecialTweak(BOOL bMinus) { }
60 
61     virtual void OnEndDraw(BOOL bCancel);
62 
63     void beginEvent();
64     void endEvent();
65     void reset();
66 
67     static ToolBase* createToolObject(TOOLTYPE type);
68 
69 protected:
70     void OnDrawSelectionOnCanvas(HDC hdc);
71 };
72 
73 class ToolsModel
74 {
75 private:
76     int m_lineWidth;
77     INT m_penWidth;
78     INT m_brushWidth;
79     int m_shapeStyle;
80     BrushStyle m_brushStyle;
81     TOOLTYPE m_activeTool;
82     TOOLTYPE m_oldActiveTool;
83     INT m_airBrushRadius;
84     int m_rubberRadius;
85     BOOL m_transpBg;
86     int m_zoom;
87     ToolBase *m_pToolObject;
88 
89     ToolBase *GetOrCreateTool(TOOLTYPE nTool);
90 
91 public:
92     ToolsModel();
93     ~ToolsModel();
94 
95     BOOL IsSelection() const;
96 
97     int GetLineWidth() const;
98     void SetLineWidth(int nLineWidth);
99     void MakeLineThickerOrThinner(BOOL bThinner);
100 
101     INT GetPenWidth() const;
102     void SetPenWidth(INT nPenWidth);
103     void MakePenThickerOrThinner(BOOL bThinner);
104 
105     int GetShapeStyle() const;
106     void SetShapeStyle(int nShapeStyle);
107 
108     INT GetBrushWidth() const;
109     void SetBrushWidth(INT nBrushWidth);
110     void MakeBrushThickerOrThinner(BOOL bThinner);
111 
112     BrushStyle GetBrushStyle() const;
113     void SetBrushStyle(BrushStyle nBrushStyle);
114 
115     TOOLTYPE GetActiveTool() const;
116     TOOLTYPE GetOldActiveTool() const;
117     void SetActiveTool(TOOLTYPE nActiveTool);
118 
119     INT GetAirBrushRadius() const;
120     void SetAirBrushRadius(INT nAirBrushRadius);
121     void MakeAirBrushThickerOrThinner(BOOL bThinner);
122 
123     int GetRubberRadius() const;
124     void SetRubberRadius(int nRubberRadius);
125     void MakeRubberThickerOrThinner(BOOL bThinner);
126 
127     BOOL IsBackgroundTransparent() const;
128     void SetBackgroundTransparent(BOOL bTransparent);
129 
130     int GetZoom() const;
131     void SetZoom(int nZoom);
132 
133     void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick);
134     void OnMouseMove(BOOL bLeftButton, LONG x, LONG y);
135     void OnButtonUp(BOOL bLeftButton, LONG x, LONG y);
136     void OnEndDraw(BOOL bCancel);
137     void OnDrawOverlayOnImage(HDC hdc);
138     void OnDrawOverlayOnCanvas(HDC hdc);
139 
140     void resetTool();
141     void selectAll();
142 
143     void NotifyToolChanged();
144     void NotifyToolSettingsChanged();
145     void NotifyZoomChanged();
146 
147     void SpecialTweak(BOOL bMinus);
148 
149     void DrawWithMouseTool(POINT pt, WPARAM wParam);
150 };
151 
152 extern ToolsModel toolsModel;
153 
154 static inline int Zoomed(int xy)
155 {
156     return xy * toolsModel.GetZoom() / 1000;
157 }
158 
159 static inline int UnZoomed(int xy)
160 {
161     return xy * 1000 / toolsModel.GetZoom();
162 }
163 
164 static inline void Zoomed(POINT& pt)
165 {
166     pt.x = Zoomed(pt.x);
167     pt.y = Zoomed(pt.y);
168 }
169 
170 static inline void Zoomed(RECT& rc)
171 {
172     rc.left = Zoomed(rc.left);
173     rc.top = Zoomed(rc.top);
174     rc.right = Zoomed(rc.right);
175     rc.bottom = Zoomed(rc.bottom);
176 }
177 
178 static inline void UnZoomed(POINT& pt)
179 {
180     pt.x = UnZoomed(pt.x);
181     pt.y = UnZoomed(pt.y);
182 }
183 
184 static inline void UnZoomed(RECT& rc)
185 {
186     rc.left = UnZoomed(rc.left);
187     rc.top = UnZoomed(rc.top);
188     rc.right = UnZoomed(rc.right);
189     rc.bottom = UnZoomed(rc.bottom);
190 }
191