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     SIZE GetToolSize() const;
128 
129     BOOL IsBackgroundTransparent() const;
130     void SetBackgroundTransparent(BOOL bTransparent);
131 
132     int GetZoom() const;
133     void SetZoom(int nZoom);
134 
135     void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick);
136     void OnMouseMove(BOOL bLeftButton, LONG x, LONG y);
137     void OnButtonUp(BOOL bLeftButton, LONG x, LONG y);
138     void OnEndDraw(BOOL bCancel);
139     void OnDrawOverlayOnImage(HDC hdc);
140     void OnDrawOverlayOnCanvas(HDC hdc);
141 
142     void resetTool();
143     void selectAll();
144 
145     void NotifyToolChanged();
146     void NotifyToolSettingsChanged();
147     void NotifyZoomChanged();
148 
149     void SpecialTweak(BOOL bMinus);
150 
151     void DrawWithMouseTool(POINT pt, WPARAM wParam);
152 };
153 
154 extern ToolsModel toolsModel;
155 
156 static inline int Zoomed(int xy)
157 {
158     return xy * toolsModel.GetZoom() / 1000;
159 }
160 
161 static inline int UnZoomed(int xy)
162 {
163     return xy * 1000 / toolsModel.GetZoom();
164 }
165 
166 static inline void Zoomed(POINT& pt)
167 {
168     pt.x = Zoomed(pt.x);
169     pt.y = Zoomed(pt.y);
170 }
171 
172 static inline void Zoomed(RECT& rc)
173 {
174     rc.left = Zoomed(rc.left);
175     rc.top = Zoomed(rc.top);
176     rc.right = Zoomed(rc.right);
177     rc.bottom = Zoomed(rc.bottom);
178 }
179 
180 static inline void UnZoomed(POINT& pt)
181 {
182     pt.x = UnZoomed(pt.x);
183     pt.y = UnZoomed(pt.y);
184 }
185 
186 static inline void UnZoomed(RECT& rc)
187 {
188     rc.left = UnZoomed(rc.left);
189     rc.top = UnZoomed(rc.top);
190     rc.right = UnZoomed(rc.right);
191     rc.bottom = UnZoomed(rc.bottom);
192 }
193