Jamba C++ API  4.0.0
DrawContext.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
20 #include <vstgui4/vstgui/lib/cdrawcontext.h>
21 #include <vstgui4/vstgui/lib/cview.h>
22 #include <pongasoft/Utils/Misc.h>
23 #include <base/source/fstring.h>
24 
25 #include "Types.h"
26 
27 namespace pongasoft {
28 namespace VST {
29 namespace GUI {
30 
31 using namespace VSTGUI;
32 
34 {
35  CHoriTxtAlign fHorizTxtAlign{kCenterText};
36  int32_t fStyle{0};
37  FontPtr fFont{nullptr};
38  CColor fFontColor{kWhiteCColor};
39  CColor fShadowColor{kBlackCColor};
40  CPoint fTextInset{0, 0};
41  CPoint fShadowTextOffset{1., 1.};
42  bool fAntialias{true};
43 };
44 
45 using RelativeCoord = CCoord;
46 using AbsoluteCoord = CCoord;
47 using RelativePoint = CPoint;
48 using AbsolutePoint = CPoint;
49 using RelativeRect = CRect;
50 using AbsoluteRect = CRect;
51 
53 {
54 public:
55  explicit RelativeView(CView const *iView) : fRect{iView->getViewSize()}
56  {
57  }
58 
59  explicit RelativeView(AbsoluteRect const &iRect) : fRect{iRect}
60  {
61  }
62 
63  inline RelativeRect getViewSize() const
64  {
65  return RelativeRect(0, 0, fRect.getWidth(), fRect.getHeight());
66  }
67 
68  inline CCoord getWidth() const
69  {
70  return fRect.getWidth();
71  }
72 
73  inline Range getHorizontalRange() const { return Range{0, getWidth()}; }
74 
75  inline Range getVerticalRange() const { return Range{0, getHeight()}; }
76 
77  inline CCoord getHeight() const
78  {
79  return fRect.getHeight();
80  }
81 
83  {
84  return x + fRect.left;
85  }
86 
88  {
89  return y + fRect.top;
90  }
91 
93  {
94  return x - fRect.left;
95  }
96 
98  {
99  return y - fRect.top;
100  }
101 
102  inline AbsolutePoint toAbsolutePoint(RelativePoint const &iPoint) const
103  {
104  return AbsolutePoint{toAbsoluteX(iPoint.x), toAbsoluteY(iPoint.y)};
105  }
106 
107  inline RelativePoint fromAbsolutePoint(AbsolutePoint const &iPoint) const
108  {
109  return RelativePoint{fromAbsoluteX(iPoint.x), fromAbsoluteY(iPoint.y)};
110  }
111 
116  inline RelativePoint clampAbsolutePoint(AbsolutePoint const &iPoint) const
117  {
118  return RelativePoint{
119  Utils::clamp(fromAbsoluteX(iPoint.x), 0.0, fRect.getWidth()),
120  Utils::clamp(fromAbsoluteY(iPoint.y), 0.0, fRect.getHeight())
121  };
122  }
123 
125  {
126  return CPoint{x + fRect.left, y + fRect.top};
127  }
128 
129  inline AbsoluteRect toAbsoluteRect(RelativeRect const &iRect) const
130  {
131  return CRect(toAbsolutePoint(iRect.getTopLeft()), iRect.getSize());
132  }
133 
134 protected:
136 };
137 
142 {
143 public:
144  RelativeDrawContext(CView *iView, CDrawContext *iDrawContext) : RelativeView{iView}, fDrawContext{iDrawContext}
145  {
146  }
147 
148  void drawLine(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &color)
149  {
150  fDrawContext->setFrameColor(color);
151  fDrawContext->drawLine(toAbsolutePoint(x1, y1), toAbsolutePoint(x2, y2));
152  }
153 
154  void drawRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
155  {
156  drawRect(RelativeRect{x1, y1, x2, y2}, iStrokeColor);
157  }
158 
159  void drawRect(RelativeRect const &iRect, CColor const &iStrokeColor)
160  {
161  fDrawContext->setFrameColor(iStrokeColor);
162  fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawStroked);
163  }
164 
165  void fillRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
166  {
167  fillRect(RelativeRect{x1, y1, x2, y2}, iColor);
168  }
169 
170  void fillRect(RelativeRect const &iRect, CColor const &iColor)
171  {
172  fDrawContext->setFillColor(iColor);
173  fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawFilled);
174  }
175 
176  void fillAndStrokeRect(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
177  {
178  fDrawContext->setFillColor(iFillColor);
179  fDrawContext->setFrameColor(iStrokeColor);
180  fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawFilledAndStroked);
181  }
182 
183  void drawEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
184  {
185  drawEllipse(RelativeRect{x1, y1, x2, y2}, iStrokeColor);
186  }
187 
188  void drawEllipse(RelativeRect const &iRect, CColor const &iStrokeColor)
189  {
190  fDrawContext->setFrameColor(iStrokeColor);
191  fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawStroked);
192  }
193 
194  void fillEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
195  {
196  fillEllipse(RelativeRect{x1, y1, x2, y2}, iColor);
197  }
198 
199  void fillEllipse(RelativeRect const &iRect, CColor const &iColor)
200  {
201  fDrawContext->setFillColor(iColor);
202  fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawFilled);
203  }
204 
205  void fillAndStrokeEllipse(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
206  {
207  fDrawContext->setFillColor(iFillColor);
208  fDrawContext->setFrameColor(iStrokeColor);
209  fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawFilledAndStroked);
210  }
211 
212  void drawString(UTF8String const &iText, RelativeCoord x, RelativeCoord y, RelativeCoord iHeight, StringDrawContext &iSdc)
213  {
214  CRect size{x, y, fDrawContext->getStringWidth(iText.getPlatformString()), iHeight};
215  drawString(iText, size, iSdc);
216  }
217 
218  void drawString(UTF8String const &iText, RelativeRect const &fSize, StringDrawContext &iSdc);
219 
220  void drawString(UTF8String const &iText, StringDrawContext &iSdc)
221  {
222  drawString(iText, getViewSize(), iSdc);
223  }
224 
225 #if EDITOR_MODE
226 
232  template<typename... Args>
233  inline void debugXY(RelativeCoord x, RelativeCoord y, char const *iFormat, Args&& ...iArgs)
234  {
235  // handles strings up to 4k characters so should be fine for debug...
236  auto s = Steinberg::String().printf(iFormat, std::forward<Args>(iArgs)...);
237  debugText(x, y, s.text8());
238  }
239 
240  // debugTopLeft
241  template<typename... Args>
242  inline void debugTopLeft(char const *iFormat, Args&& ...iArgs) { debugXY(0, 0, iFormat, std::forward<Args>(iArgs)...); }
243 
244  // debugTopRight
245  template<typename... Args>
246  inline void debugTopRight(char const *iFormat, Args&& ...iArgs) { debugXY(-1, 0, iFormat, std::forward<Args>(iArgs)...); }
247 
248  // debugBottomLeft
249  template<typename... Args>
250  inline void debugBottomLeft(char const *iFormat, Args&& ...iArgs) { debugXY(0, -1, iFormat, std::forward<Args>(iArgs)...); }
251 
252  // debugBottomRight
253  template<typename... Args>
254  inline void debugBottomRight(char const *iFormat, Args&& ...iArgs) { debugXY(-1, -1, iFormat, std::forward<Args>(iArgs)...); }
255 
259  template<typename... Args>
260  inline void debug(char const *iFormat, Args&& ...iArgs) { debugTopLeft(iFormat, std::forward<Args>(iArgs)...); }
261 
265  inline void debugText(char const *iText) { debugText(0, 0, iText); }
266 
270  void debugText(RelativeCoord x, RelativeCoord y, char const *iText);
271 
272 #endif
273 
274 protected:
275  CDrawContext *fDrawContext;
276 
277 #if EDITOR_MODE
278 public:
279  CColor fDebugStringColor{kGreenCColor};
280  FontPtr fDebugStringFont{kNormalFontVerySmall};
281 #endif
282 };
283 
284 }
285 }
286 }
void fillRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
Definition: DrawContext.h:165
CDrawContext * fDrawContext
Definition: DrawContext.h:275
RelativeView(CView const *iView)
Definition: DrawContext.h:55
AbsoluteCoord toAbsoluteX(RelativeCoord x) const
Definition: DrawContext.h:82
AbsolutePoint toAbsolutePoint(RelativeCoord x, RelativeCoord y) const
Definition: DrawContext.h:124
static T clamp(const U &iValue, const T &iLower, const T &iUpper)
Make sure that the value remains within its bounds.
Definition: Misc.h:33
RelativeView(AbsoluteRect const &iRect)
Definition: DrawContext.h:59
RelativePoint fromAbsolutePoint(AbsolutePoint const &iPoint) const
Definition: DrawContext.h:107
void fillEllipse(RelativeRect const &iRect, CColor const &iColor)
Definition: DrawContext.h:199
CPoint RelativePoint
Definition: DrawContext.h:47
Encapsulates the draw context provided by VSTGUI to reason in relative coordinates (0,...
Definition: DrawContext.h:141
void fillAndStrokeEllipse(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
Definition: DrawContext.h:205
CCoord RelativeCoord
Definition: DrawContext.h:45
void fillAndStrokeRect(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
Definition: DrawContext.h:176
Definition: Clock.h:22
Range getVerticalRange() const
Definition: DrawContext.h:75
RelativeCoord fromAbsoluteX(AbsoluteCoord x) const
Definition: DrawContext.h:92
AbsoluteRect const & fRect
Definition: DrawContext.h:135
RelativePoint clampAbsolutePoint(AbsolutePoint const &iPoint) const
Convert the absolute point to a relative point while making sure it is clamped (constrained) within t...
Definition: DrawContext.h:116
AbsolutePoint toAbsolutePoint(RelativePoint const &iPoint) const
Definition: DrawContext.h:102
void drawEllipse(RelativeRect const &iRect, CColor const &iStrokeColor)
Definition: DrawContext.h:188
CFontDesc * FontPtr
Definition: Types.h:53
void drawRect(RelativeRect const &iRect, CColor const &iStrokeColor)
Definition: DrawContext.h:159
CRect RelativeRect
Definition: DrawContext.h:49
Range getHorizontalRange() const
Definition: DrawContext.h:73
void drawEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
Definition: DrawContext.h:183
CRect AbsoluteRect
Definition: DrawContext.h:50
Definition: Types.h:29
void drawRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
Definition: DrawContext.h:154
CPoint AbsolutePoint
Definition: DrawContext.h:48
void fillEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
Definition: DrawContext.h:194
CCoord AbsoluteCoord
Definition: DrawContext.h:46
void drawLine(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &color)
Definition: DrawContext.h:148
AbsoluteCoord toAbsoluteY(RelativeCoord y) const
Definition: DrawContext.h:87
CCoord getHeight() const
Definition: DrawContext.h:77
Definition: DrawContext.h:33
Definition: DrawContext.h:52
CCoord getWidth() const
Definition: DrawContext.h:68
void drawString(UTF8String const &iText, RelativeCoord x, RelativeCoord y, RelativeCoord iHeight, StringDrawContext &iSdc)
Definition: DrawContext.h:212
AbsoluteRect toAbsoluteRect(RelativeRect const &iRect) const
Definition: DrawContext.h:129
void fillRect(RelativeRect const &iRect, CColor const &iColor)
Definition: DrawContext.h:170
RelativeRect getViewSize() const
Definition: DrawContext.h:63
void drawString(UTF8String const &iText, StringDrawContext &iSdc)
Definition: DrawContext.h:220
RelativeDrawContext(CView *iView, CDrawContext *iDrawContext)
Definition: DrawContext.h:144
RelativeCoord fromAbsoluteY(AbsoluteCoord y) const
Definition: DrawContext.h:97