ASPiK SDK
cdrawcontext.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __cdrawcontext__
6 #define __cdrawcontext__
7 
8 #include "vstguifwd.h"
9 
10 #include "cpoint.h"
11 #include "crect.h"
12 #include "cfont.h"
13 #include "ccolor.h"
14 #include "cgraphicstransform.h"
15 #include "clinestyle.h"
16 #include "cdrawdefs.h"
17 #include <cmath>
18 #include <stack>
19 #include <vector>
20 
21 namespace VSTGUI {
22 
23 struct CNinePartTiledDescription;
24 
25 //-----------------------------------------------------------------------------
26 // CDrawContext Declaration
28 //-----------------------------------------------------------------------------
30 {
31 public:
32  //-----------------------------------------------------------------------------
34  //-----------------------------------------------------------------------------
35  struct Transform
36  {
37  Transform (CDrawContext& context, const CGraphicsTransform& transformation);
38  ~Transform () noexcept;
39 
40  private:
41  CDrawContext& context;
42  const CGraphicsTransform transformation;
43  };
44 
45  //-----------------------------------------------------------------------------
47  //-----------------------------------------------------------------------------
49  using LinePair = std::pair<CPoint, CPoint>;
50  using LineList = std::vector<LinePair>;
51  using PointList = std::vector<CPoint>;
52 
53  inline void drawLine (const CPoint& start, const CPoint& end) { drawLine (std::make_pair (start, end)); }
54  virtual void drawLine (const LinePair& line) = 0;
55  virtual void drawLines (const LineList& lines) = 0;
56  virtual void drawPolygon (const PointList& polygonPointList, const CDrawStyle drawStyle = kDrawStroked) = 0;
57  virtual void drawRect (const CRect &rect, const CDrawStyle drawStyle = kDrawStroked) = 0;
58  virtual void drawArc (const CRect &rect, const float startAngle1, const float endAngle2, const CDrawStyle drawStyle = kDrawStroked) = 0;
59  virtual void drawEllipse (const CRect &rect, const CDrawStyle drawStyle = kDrawStroked) = 0;
60  virtual void drawPoint (const CPoint &point, const CColor& color) = 0;
61  virtual void drawBitmap (CBitmap* bitmap, const CRect& dest, const CPoint& offset = CPoint (0, 0), float alpha = 1.f) = 0;
62 
63  virtual void drawBitmapNinePartTiled (CBitmap* bitmap, const CRect& dest, const CNinePartTiledDescription& desc, float alpha = 1.f);
64  virtual void fillRectWithBitmap (CBitmap* bitmap, const CRect& srcRect, const CRect& dstRect, float alpha);
65 
66  virtual void clearRect (const CRect& rect) = 0;
67 
68 
69  //-----------------------------------------------------------------------------
71  //-----------------------------------------------------------------------------
73  virtual void setLineStyle (const CLineStyle& style);
74  const CLineStyle& getLineStyle () const { return currentState.lineStyle; }
75 
76  virtual void setLineWidth (CCoord width);
77  CCoord getLineWidth () const { return currentState.frameWidth; }
78 
79 
80  //-----------------------------------------------------------------------------
82  //-----------------------------------------------------------------------------
84  virtual void setDrawMode (CDrawMode mode);
85  CDrawMode getDrawMode () const { return currentState.drawMode; }
86 
87 
88  //-----------------------------------------------------------------------------
90  //-----------------------------------------------------------------------------
92  virtual void setClipRect (const CRect &clip);
93  CRect& getClipRect (CRect &clip) const;
94  virtual void resetClipRect ();
95 
96 
97  //-----------------------------------------------------------------------------
99  //-----------------------------------------------------------------------------
101  virtual void setFillColor (const CColor& color);
102  CColor getFillColor () const { return currentState.fillColor; }
103  virtual void setFrameColor (const CColor& color);
104  CColor getFrameColor () const { return currentState.frameColor; }
105 
106 
107  //-----------------------------------------------------------------------------
109  //-----------------------------------------------------------------------------
111  virtual void setFontColor (const CColor& color);
112  CColor getFontColor () const { return currentState.fontColor; }
113  virtual void setFont (const CFontRef font, const CCoord& size = 0, const int32_t& style = -1);
114  const CFontRef getFont () const { return currentState.font; }
115 
116 
117  //-----------------------------------------------------------------------------
119  //-----------------------------------------------------------------------------
121  CCoord getStringWidth (UTF8StringPtr pStr);
122  void drawString (UTF8StringPtr string, const CRect& _rect, const CHoriTxtAlign hAlign = kCenterText, bool antialias = true);
123  void drawString (UTF8StringPtr string, const CPoint& _point, bool antialias = true);
124 
125  CCoord getStringWidth (IPlatformString* pStr);
126  void drawString (IPlatformString* string, const CRect& _rect, const CHoriTxtAlign hAlign = kCenterText, bool antialias = true);
127  void drawString (IPlatformString* string, const CPoint& _point, bool antialias = true);
128 
129 
130  //-----------------------------------------------------------------------------
132  //-----------------------------------------------------------------------------
134  virtual void setGlobalAlpha (float newAlpha);
135  float getGlobalAlpha () const { return currentState.globalAlpha; }
136 
137 
138  //-----------------------------------------------------------------------------
140  //-----------------------------------------------------------------------------
142  virtual void saveGlobalState ();
143  virtual void restoreGlobalState ();
145 
146  //-----------------------------------------------------------------------------
148  //-----------------------------------------------------------------------------
150  const CGraphicsTransform& getCurrentTransform () const;
151  const CRect& getAbsoluteClipRect () const { return currentState.clipRect; }
152 
154  virtual double getScaleFactor () const { return 1.; }
155 
160  CCoord getHairlineSize () const;
162 
163  //-----------------------------------------------------------------------------
165  //-----------------------------------------------------------------------------
167  virtual CGraphicsPath* createGraphicsPath () = 0;
168  virtual CGraphicsPath* createTextPath (const CFontRef font, UTF8StringPtr text) = 0;
169 
170  CGraphicsPath* createRoundRectGraphicsPath (const CRect& size, CCoord radius);
171 
172  enum PathDrawMode
173  {
174  kPathFilled,
175  kPathFilledEvenOdd,
176  kPathStroked
177  };
178 
179  virtual void drawGraphicsPath (CGraphicsPath* path, PathDrawMode mode = kPathFilled, CGraphicsTransform* transformation = nullptr) = 0;
180  virtual void fillLinearGradient (CGraphicsPath* path, const CGradient& gradient, const CPoint& startPoint, const CPoint& endPoint, bool evenOdd = false, CGraphicsTransform* transformation = nullptr) = 0;
181  virtual void fillRadialGradient (CGraphicsPath* path, const CGradient& gradient, const CPoint& center, CCoord radius, const CPoint& originOffset = CPoint (0,0), bool evenOdd = false, CGraphicsTransform* transformation = nullptr) = 0;
183 
184  virtual void beginDraw () {}
185  virtual void endDraw () {}
186 
187 protected:
188  explicit CDrawContext (const CRect& surfaceRect);
189  ~CDrawContext () noexcept override;
190 
191  virtual void init ();
192 
193  void pushTransform (const CGraphicsTransform& transformation);
194  void popTransform ();
195 
196  const UTF8String& getDrawString (UTF8StringPtr string);
197  void clearDrawString ();
198 
200  struct CDrawContextState
201  {
202  SharedPointer<CFontDesc> font;
203  CColor frameColor {kTransparentCColor};
204  CColor fillColor {kTransparentCColor};
205  CColor fontColor {kTransparentCColor};
206  CCoord frameWidth {0.};
207  CPoint penLoc {};
208  CRect clipRect {};
209  CLineStyle lineStyle {kLineOnOffDash};
210  CDrawMode drawMode {kAntiAliasing};
211  float globalAlpha {1.f};
212 
213  CDrawContextState () = default;
214  CDrawContextState (const CDrawContextState& state);
215  CDrawContextState& operator= (const CDrawContextState& state) = default;
216  CDrawContextState (CDrawContextState&& state) noexcept;
217  CDrawContextState& operator= (CDrawContextState&& state) noexcept;
218  };
220 
221  UTF8String* drawStringHelper;
222  CRect surfaceRect;
223 
224  CDrawContextState currentState;
225 
226 private:
227  std::stack<CDrawContextState> globalStatesStack;
228  std::stack<CGraphicsTransform> transformStack;
229 };
230 
231 //-----------------------------------------------------------------------------
233 {
234  ConcatClip (CDrawContext& context, CRect rect)
235  : context (context)
236  {
237  context.getClipRect (origClip);
238  rect.normalize ();
239  rect.bound (origClip);
240  context.setClipRect (rect);
241  }
242  ~ConcatClip () noexcept
243  {
244  context.setClipRect (origClip);
245  }
246 private:
247  CDrawContext& context;
248  CRect origClip;
249 };
250 
251 } // namespace
252 
253 #endif
virtual void drawArc(const CRect &rect, const float startAngle1, const float endAngle2, const CDrawStyle drawStyle=kDrawStroked)=0
draw an arc, angles are in degree
CCoord getStringWidth(UTF8StringPtr pStr)
get the width of an UTF-8 encoded string
Definition: cdrawcontext.cpp:270
CCoord getLineWidth() const
get the current line width
Definition: cdrawcontext.h:77
virtual double getScaleFactor() const
Definition: cdrawcontext.h:154
font class
Definition: cfont.h:31
Rect structure.
Definition: crect.h:17
CColor getFontColor() const
get current font color
Definition: cdrawcontext.h:112
virtual void drawEllipse(const CRect &rect, const CDrawStyle drawStyle=kDrawStroked)=0
draw an ellipse
Definition: cbitmap.h:63
virtual void drawBitmap(CBitmap *bitmap, const CRect &dest, const CPoint &offset=CPoint(0, 0), float alpha=1.f)=0
don&#39;t call directly, please use CBitmap::draw instead
virtual void setFillColor(const CColor &color)
set current fill color
Definition: cdrawcontext.cpp:155
CColor getFillColor() const
get current fill color
Definition: cdrawcontext.h:102
float getGlobalAlpha() const
get current global alpha value
Definition: cdrawcontext.h:135
virtual void drawRect(const CRect &rect, const CDrawStyle drawStyle=kDrawStroked)=0
draw a rect
CCoord getHairlineSize() const
Definition: cdrawcontext.cpp:371
A drawing context encapsulates the drawing context of the underlying OS.
Definition: cdrawcontext.h:29
CColor getFrameColor() const
get current stroke color
Definition: cdrawcontext.h:104
const CLineStyle & getLineStyle() const
get the current line style
Definition: cdrawcontext.h:74
RGBA Color structure.
Definition: ccolor.h:15
const CFontRef getFont() const
get current font
Definition: cdrawcontext.h:114
virtual void drawPoint(const CPoint &point, const CColor &color)=0
draw a point
CDrawMode getDrawMode() const
get the current draw mode, see CDrawMode
Definition: cdrawcontext.h:85
Graphics Path Object.
Definition: cgraphicspath.h:19
virtual CGraphicsPath * createTextPath(const CFontRef font, UTF8StringPtr text)=0
create a graphics path from a text
virtual void setClipRect(const CRect &clip)
set the current clip
Definition: cdrawcontext.cpp:141
Encapsulates various platform depended kinds of bitmaps.
Definition: cbitmap.h:21
void drawString(UTF8StringPtr string, const CRect &_rect, const CHoriTxtAlign hAlign=kCenterText, bool antialias=true)
draw an UTF-8 encoded string
Definition: cdrawcontext.cpp:283
Definition: clinestyle.h:16
Definition: customcontrols.cpp:8
virtual void setGlobalAlpha(float newAlpha)
sets the global alpha value[0..1]
Definition: cdrawcontext.cpp:192
virtual void resetClipRect()
reset the clip to the default state
Definition: cdrawcontext.cpp:149
Definition: cdrawdefs.h:25
virtual void setLineWidth(CCoord width)
set the current line width
Definition: cdrawcontext.cpp:120
virtual void setFontColor(const CColor &color)
set current font color
Definition: cdrawcontext.cpp:167
Definition: vstguibase.h:247
virtual void setLineStyle(const CLineStyle &style)
set the current line style
Definition: cdrawcontext.cpp:114
Graphics Transform Matrix.
Definition: cgraphicstransform.h:23
Definition: cdrawcontext.h:35
CRect & getClipRect(CRect &clip) const
get the current clip
Definition: cdrawcontext.cpp:132
virtual void setFrameColor(const CColor &color)
set current stroke color
Definition: cdrawcontext.cpp:161
Definition: cdrawcontext.h:232
virtual CGraphicsPath * createGraphicsPath()=0
create a graphics path object, you need to forget it after usage
virtual void drawLines(const LineList &lines)=0
draw multiple lines at once
virtual void setFont(const CFontRef font, const CCoord &size=0, const int32_t &style=-1)
set current font
Definition: cdrawcontext.cpp:173
Point structure.
Definition: cpoint.h:17
virtual void clearRect(const CRect &rect)=0
clears the rect (makes r = 0, g = 0, b = 0, a = 0)
virtual void drawPolygon(const PointList &polygonPointList, const CDrawStyle drawStyle=kDrawStroked)=0
draw a polygon
virtual void setDrawMode(CDrawMode mode)
set the current draw mode, see CDrawMode
Definition: cdrawcontext.cpp:126
CGraphicsPath * createRoundRectGraphicsPath(const CRect &size, CCoord radius)
create a rect with round corners as graphics path, you need to forget it after usage ...
Definition: cdrawcontext.cpp:338