ASPiK SDK
d2ddrawcontext.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 __d2ddrawcontext__
6 #define __d2ddrawcontext__
7 
8 #include "../../../coffscreencontext.h"
9 
10 #if WINDOWS && VSTGUI_DIRECT2D_SUPPORT
11 
12 #include "d2dbitmap.h"
13 #include <windows.h>
14 #include <d2d1.h>
15 #include <stack>
16 
17 namespace VSTGUI {
18 class CGradient;
19 
20 //-----------------------------------------------------------------------------
21 class D2DDrawContext : public COffscreenContext
22 {
23 public:
24  D2DDrawContext (HWND window, const CRect& drawSurface);
25  D2DDrawContext (D2DBitmap* bitmap);
26  ~D2DDrawContext ();
27 
28  ID2D1RenderTarget* getRenderTarget () const { return renderTarget; }
29  ID2D1SolidColorBrush* getFillBrush () const { return fillBrush; }
30  ID2D1SolidColorBrush* getStrokeBrush () const { return strokeBrush; }
31  ID2D1SolidColorBrush* getFontBrush () const { return fontBrush; }
32  ID2D1StrokeStyle* getStrokeStyle () const { return strokeStyle; }
33 
34  // CDrawContext
35  void drawLine (const LinePair& line) override;
36  void drawLines (const LineList& lines) override;
37  void drawPolygon (const PointList& polygonPointList, const CDrawStyle drawStyle = kDrawStroked) override;
38  void drawRect (const CRect &rect, const CDrawStyle drawStyle = kDrawStroked) override;
39  void drawArc (const CRect &rect, const float startAngle1, const float endAngle2, const CDrawStyle drawStyle = kDrawStroked) override;
40  void drawEllipse (const CRect &rect, const CDrawStyle drawStyle = kDrawStroked) override;
41  void drawPoint (const CPoint &point, const CColor& color) override;
42  void drawBitmap (CBitmap* bitmap, const CRect& dest, const CPoint& offset = CPoint (0, 0), float alpha = 1.f) override;
43  void clearRect (const CRect& rect) override;
44  void setLineStyle (const CLineStyle& style) override;
45  void setLineWidth (CCoord width) override;
46  void setDrawMode (CDrawMode mode) override;
47  void setClipRect (const CRect &clip) override;
48  void resetClipRect () override;
49  void setFillColor (const CColor& color) override;
50  void setFrameColor (const CColor& color) override;
51  void setFontColor (const CColor& color) override;
52  void setGlobalAlpha (float newAlpha) override;
53  void saveGlobalState () override;
54  void restoreGlobalState () override;
55  CGraphicsPath* createGraphicsPath () override;
56  CGraphicsPath* createTextPath (const CFontRef font, UTF8StringPtr text) override;
57  void drawGraphicsPath (CGraphicsPath* path, PathDrawMode mode = kPathFilled, CGraphicsTransform* transformation = 0) override;
58  void fillLinearGradient (CGraphicsPath* path, const CGradient& gradient, const CPoint& startPoint, const CPoint& endPoint, bool evenOdd = false, CGraphicsTransform* transformation = 0) override;
59  void fillRadialGradient (CGraphicsPath* path, const CGradient& gradient, const CPoint& center, CCoord radius, const CPoint& originOffset = CPoint (0, 0), bool evenOdd = false, CGraphicsTransform* transformation = 0) override;
60 
61  void beginDraw () override;
62  void endDraw () override;
63 
64  double getScaleFactor () const override { return scaleFactor; }
65 
66  //-----------------------------------------------------------------------------
67  class D2DApplyClip
68  {
69  public:
70  D2DApplyClip (D2DDrawContext* drawContext, bool halfPointOffset = false);
71  ~D2DApplyClip ();
72  bool isEmpty () const { return applyClip.isEmpty (); }
73  protected:
74  D2DDrawContext* drawContext;
75  CRect applyClip;
76  bool layerIsUsed {false};
77  };
78 
79  template<typename T> void pixelAllign (T& rect) const;
80 
81 //-----------------------------------------------------------------------------
82 protected:
83  void init () override;
84  void createRenderTarget ();
85  void releaseRenderTarget ();
86  ID2D1GradientStopCollection* createGradientStopCollection (const CGradient& gradient) const;
87 
88  void setFillColorInternal (const CColor& color);
89  void setFrameColorInternal (const CColor& color);
90  void setFontColorInternal (const CColor& color);
91  void setLineStyleInternal (const CLineStyle& style);
92  void setDrawModeInternal (CDrawMode mode);
93  void drawLineInternal (CPoint start, CPoint end);
94 
95  bool needsHalfPointOffset () const;
96 
97  HWND window;
98  ID2D1RenderTarget* renderTarget;
99  ID2D1SolidColorBrush* fillBrush;
100  ID2D1SolidColorBrush* strokeBrush;
101  ID2D1SolidColorBrush* fontBrush;
102  ID2D1StrokeStyle* strokeStyle;
103  CRect currentClip;
104  double scaleFactor {1.};
105 };
106 
107 //-----------------------------------------------------------------------------
108 template<typename T> void D2DDrawContext::pixelAllign (T& obj) const
109 {
110  const CGraphicsTransform& t = getCurrentTransform ();
111  CGraphicsTransform tInv = t.inverse ();
112  t.transform (obj);
113  obj.makeIntegral ();
114  tInv.transform (obj);
115 }
116 
117 //-----------------------------------------------------------------------------
118 static inline D2D1_RECT_F makeD2DRect (const CRect& r)
119 {
120  D2D1_RECT_F dr = {(FLOAT)r.left, (FLOAT)r.top, (FLOAT)r.right, (FLOAT)r.bottom};
121  return dr;
122 }
123 
124 //-----------------------------------------------------------------------------
125 static inline D2D1_POINT_2F makeD2DPoint (const CPoint& p)
126 {
127  D2D1_POINT_2F dp = {(FLOAT)p.x, (FLOAT)p.y};
128  return dp;
129 }
130 
131 static inline D2D1_SIZE_F makeD2DSize (CCoord width, CCoord height)
132 {
133  D2D1_SIZE_F ds = {(FLOAT)width, (FLOAT)height};
134  return ds;
135 }
136 
137 //-----------------------------------------------------------------------------
138 static inline D2D1_MATRIX_3X2_F convert (const CGraphicsTransform& t)
139 {
140  D2D1_MATRIX_3X2_F matrix;
141  matrix._11 = static_cast<FLOAT> (t.m11);
142  matrix._12 = static_cast<FLOAT> (t.m21);
143  matrix._21 = static_cast<FLOAT> (t.m12);
144  matrix._22 = static_cast<FLOAT> (t.m22);
145  matrix._31 = static_cast<FLOAT> (t.dx);
146  matrix._32 = static_cast<FLOAT> (t.dy);
147  return matrix;
148 }
149 
150 } // namespace
151 
152 #endif // WINDOWS
153 
154 #endif // __d2ddrawcontext__
Definition: customcontrols.cpp:8