Jamba C++ API  6.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 
41 enum [[deprecated("Since 5.0.0 - Use StringDrawContext::Style enum instead")]] DeprecatedCControlEnum
42 {
43  kShadowText = 1 << 2,
44  kNoTextStyle = 1 << 11,
45 };
46 
50 {
51  enum class Style : int32_t
52  {
53  kShadowText = 1 << 2,
54  kNoTextStyle = 1 << 11
55  };
56 
57  CHoriTxtAlign fHorizTxtAlign{kCenterText};
59  int32_t fStyle{0};
60  FontPtr fFont{nullptr};
61  CColor fFontColor{kWhiteCColor};
62  CColor fShadowColor{kBlackCColor};
63  CPoint fTextInset{0, 0};
64  CPoint fShadowTextOffset{1., 1.};
65  bool fAntialias{true};
66 
67  constexpr bool hasStyle(Style iStyle) const { return fStyle & static_cast<int32_t>(iStyle); }
68  inline void setStyle(Style iStyle) { fStyle = static_cast<int32_t>(iStyle); }
69  inline void addStyle(Style iStyle) { fStyle |= static_cast<int32_t>(iStyle); }
70 };
71 
72 using RelativeCoord = CCoord;
73 using AbsoluteCoord = CCoord;
74 using RelativePoint = CPoint;
75 using AbsolutePoint = CPoint;
76 using RelativeRect = CRect;
77 using AbsoluteRect = CRect;
78 
80 {
81 public:
82  explicit RelativeView(CView const *iView) : fRect{iView->getViewSize()}
83  {
84  }
85 
86  explicit RelativeView(AbsoluteRect const &iRect) : fRect{iRect}
87  {
88  }
89 
90  inline RelativeRect getViewSize() const
91  {
92  return RelativeRect(0, 0, fRect.getWidth(), fRect.getHeight());
93  }
94 
95  inline CCoord getWidth() const
96  {
97  return fRect.getWidth();
98  }
99 
100  inline Range getHorizontalRange() const { return Range{0, getWidth()}; }
101 
102  inline Range getVerticalRange() const { return Range{0, getHeight()}; }
103 
104  inline CCoord getHeight() const
105  {
106  return fRect.getHeight();
107  }
108 
110  {
111  return x + fRect.left;
112  }
113 
115  {
116  return y + fRect.top;
117  }
118 
120  {
121  return x - fRect.left;
122  }
123 
125  {
126  return y - fRect.top;
127  }
128 
129  inline AbsolutePoint toAbsolutePoint(RelativePoint const &iPoint) const
130  {
131  return AbsolutePoint{toAbsoluteX(iPoint.x), toAbsoluteY(iPoint.y)};
132  }
133 
134  inline RelativePoint fromAbsolutePoint(AbsolutePoint const &iPoint) const
135  {
136  return RelativePoint{fromAbsoluteX(iPoint.x), fromAbsoluteY(iPoint.y)};
137  }
138 
143  inline RelativePoint clampAbsolutePoint(AbsolutePoint const &iPoint) const
144  {
145  return RelativePoint{
146  Utils::clamp(fromAbsoluteX(iPoint.x), 0.0, fRect.getWidth()),
147  Utils::clamp(fromAbsoluteY(iPoint.y), 0.0, fRect.getHeight())
148  };
149  }
150 
152  {
153  return CPoint{x + fRect.left, y + fRect.top};
154  }
155 
156  inline AbsoluteRect toAbsoluteRect(RelativeRect const &iRect) const
157  {
158  return CRect(toAbsolutePoint(iRect.getTopLeft()), iRect.getSize());
159  }
160 
161 protected:
163 };
164 
169 {
170 public:
171  RelativeDrawContext(CView *iView, CDrawContext *iDrawContext) : RelativeView{iView}, fDrawContext{iDrawContext}
172  {
173  }
174 
175  void drawLine(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &color)
176  {
177  fDrawContext->setFrameColor(color);
178  fDrawContext->drawLine(toAbsolutePoint(x1, y1), toAbsolutePoint(x2, y2));
179  }
180 
181  void drawRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
182  {
183  drawRect(RelativeRect{x1, y1, x2, y2}, iStrokeColor);
184  }
185 
186  void drawRect(RelativeRect const &iRect, CColor const &iStrokeColor)
187  {
188  fDrawContext->setFrameColor(iStrokeColor);
189  fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawStroked);
190  }
191 
192  void fillRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
193  {
194  fillRect(RelativeRect{x1, y1, x2, y2}, iColor);
195  }
196 
197  void fillRect(RelativeRect const &iRect, CColor const &iColor)
198  {
199  fDrawContext->setFillColor(iColor);
200  fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawFilled);
201  }
202 
203  void fillAndStrokeRect(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
204  {
205  fDrawContext->setFillColor(iFillColor);
206  fDrawContext->setFrameColor(iStrokeColor);
207  fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawFilledAndStroked);
208  }
209 
210  void drawEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
211  {
212  drawEllipse(RelativeRect{x1, y1, x2, y2}, iStrokeColor);
213  }
214 
215  void drawEllipse(RelativeRect const &iRect, CColor const &iStrokeColor)
216  {
217  fDrawContext->setFrameColor(iStrokeColor);
218  fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawStroked);
219  }
220 
221  void fillEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
222  {
223  fillEllipse(RelativeRect{x1, y1, x2, y2}, iColor);
224  }
225 
226  void fillEllipse(RelativeRect const &iRect, CColor const &iColor)
227  {
228  fDrawContext->setFillColor(iColor);
229  fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawFilled);
230  }
231 
232  void fillAndStrokeEllipse(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
233  {
234  fDrawContext->setFillColor(iFillColor);
235  fDrawContext->setFrameColor(iStrokeColor);
236  fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawFilledAndStroked);
237  }
238 
239  void drawString(UTF8String const &iText, RelativeCoord x, RelativeCoord y, RelativeCoord iHeight, StringDrawContext &iSdc)
240  {
241  CRect size{x, y, fDrawContext->getStringWidth(iText.getPlatformString()), iHeight};
242  drawString(iText, size, iSdc);
243  }
244 
245  void drawString(UTF8String const &iText, RelativeRect const &fSize, StringDrawContext &iSdc);
246 
247  void drawString(UTF8String const &iText, StringDrawContext &iSdc)
248  {
249  drawString(iText, getViewSize(), iSdc);
250  }
251 
252 #if EDITOR_MODE
253 
259  template<typename... Args>
260  inline void debugXY(RelativeCoord x, RelativeCoord y, char const *iFormat, Args&& ...iArgs)
261  {
262  // handles strings up to 4k characters so should be fine for debug...
263  auto s = Steinberg::String().printf(iFormat, std::forward<Args>(iArgs)...);
264  debugText(x, y, s.text8());
265  }
266 
267  // debugTopLeft
268  template<typename... Args>
269  inline void debugTopLeft(char const *iFormat, Args&& ...iArgs) { debugXY(0, 0, iFormat, std::forward<Args>(iArgs)...); }
270 
271  // debugTopRight
272  template<typename... Args>
273  inline void debugTopRight(char const *iFormat, Args&& ...iArgs) { debugXY(-1, 0, iFormat, std::forward<Args>(iArgs)...); }
274 
275  // debugBottomLeft
276  template<typename... Args>
277  inline void debugBottomLeft(char const *iFormat, Args&& ...iArgs) { debugXY(0, -1, iFormat, std::forward<Args>(iArgs)...); }
278 
279  // debugBottomRight
280  template<typename... Args>
281  inline void debugBottomRight(char const *iFormat, Args&& ...iArgs) { debugXY(-1, -1, iFormat, std::forward<Args>(iArgs)...); }
282 
286  template<typename... Args>
287  inline void debug(char const *iFormat, Args&& ...iArgs) { debugTopLeft(iFormat, std::forward<Args>(iArgs)...); }
288 
292  inline void debugText(char const *iText) { debugText(0, 0, iText); }
293 
297  void debugText(RelativeCoord x, RelativeCoord y, char const *iText);
298 
299 #endif
300 
301 protected:
302  CDrawContext *fDrawContext;
303 
304 #if EDITOR_MODE
305 public:
306  CColor fDebugStringColor{kGreenCColor};
307  FontPtr fDebugStringFont{kNormalFontVerySmall};
308 #endif
309 };
310 
311 }
312 }
313 }
void addStyle(Style iStyle)
Definition: DrawContext.h:69
void fillRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
Definition: DrawContext.h:192
Definition: DrawContext.h:43
CDrawContext * fDrawContext
Definition: DrawContext.h:302
RelativeView(CView const *iView)
Definition: DrawContext.h:82
AbsoluteCoord toAbsoluteX(RelativeCoord x) const
Definition: DrawContext.h:109
AbsolutePoint toAbsolutePoint(RelativeCoord x, RelativeCoord y) const
Definition: DrawContext.h:151
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:86
RelativePoint fromAbsolutePoint(AbsolutePoint const &iPoint) const
Definition: DrawContext.h:134
void fillEllipse(RelativeRect const &iRect, CColor const &iColor)
Definition: DrawContext.h:226
CPoint RelativePoint
Definition: DrawContext.h:74
Encapsulates the draw context provided by VSTGUI to reason in relative coordinates (0,...
Definition: DrawContext.h:168
void fillAndStrokeEllipse(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
Definition: DrawContext.h:232
CCoord RelativeCoord
Definition: DrawContext.h:72
void fillAndStrokeRect(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
Definition: DrawContext.h:203
Definition: Clock.h:22
Range getVerticalRange() const
Definition: DrawContext.h:102
RelativeCoord fromAbsoluteX(AbsoluteCoord x) const
Definition: DrawContext.h:119
Definition: DrawContext.h:44
AbsoluteRect const & fRect
Definition: DrawContext.h:162
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:143
AbsolutePoint toAbsolutePoint(RelativePoint const &iPoint) const
Definition: DrawContext.h:129
void drawEllipse(RelativeRect const &iRect, CColor const &iStrokeColor)
Definition: DrawContext.h:215
CFontDesc * FontPtr
Definition: Types.h:53
void drawRect(RelativeRect const &iRect, CColor const &iStrokeColor)
Definition: DrawContext.h:186
DeprecatedCControlEnum
Implementation note: The CControlEnum enumeration has been removed from VSTGUI.
Definition: DrawContext.h:41
CRect RelativeRect
Definition: DrawContext.h:76
Range getHorizontalRange() const
Definition: DrawContext.h:100
void drawEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
Definition: DrawContext.h:210
CRect AbsoluteRect
Definition: DrawContext.h:77
Definition: Types.h:29
constexpr bool hasStyle(Style iStyle) const
Definition: DrawContext.h:67
void drawRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
Definition: DrawContext.h:181
CPoint AbsolutePoint
Definition: DrawContext.h:75
void fillEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
Definition: DrawContext.h:221
CCoord AbsoluteCoord
Definition: DrawContext.h:73
void drawLine(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &color)
Definition: DrawContext.h:175
void setStyle(Style iStyle)
Definition: DrawContext.h:68
AbsoluteCoord toAbsoluteY(RelativeCoord y) const
Definition: DrawContext.h:114
CCoord getHeight() const
Definition: DrawContext.h:104
The context which contains the details on how the string should be drawn.
Definition: DrawContext.h:49
Definition: DrawContext.h:79
CCoord getWidth() const
Definition: DrawContext.h:95
Style
Definition: DrawContext.h:51
void drawString(UTF8String const &iText, RelativeCoord x, RelativeCoord y, RelativeCoord iHeight, StringDrawContext &iSdc)
Definition: DrawContext.h:239
AbsoluteRect toAbsoluteRect(RelativeRect const &iRect) const
Definition: DrawContext.h:156
void fillRect(RelativeRect const &iRect, CColor const &iColor)
Definition: DrawContext.h:197
RelativeRect getViewSize() const
Definition: DrawContext.h:90
void drawString(UTF8String const &iText, StringDrawContext &iSdc)
Definition: DrawContext.h:247
RelativeDrawContext(CView *iView, CDrawContext *iDrawContext)
Definition: DrawContext.h:171
RelativeCoord fromAbsoluteY(AbsoluteCoord y) const
Definition: DrawContext.h:124