Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
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 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
21#include <vstgui4/vstgui/lib/cdrawcontext.h>
22#include <vstgui4/vstgui/lib/cview.h>
24#include <base/source/fstring.h>
25
26#include "Types.h"
27
28namespace pongasoft {
29namespace VST {
30namespace GUI {
31
32using namespace VSTGUI;
33
42enum [[deprecated("Since 5.0.0 - Use StringDrawContext::Style enum instead")]] DeprecatedCControlEnum
43{
44 kShadowText = 1 << 2,
45 kNoTextStyle = 1 << 11,
46};
47
51{
52 enum class Style : int32_t
53 {
54 kShadowText = 1 << 2,
55 kNoTextStyle = 1 << 11
56 };
57
58 CHoriTxtAlign fHorizTxtAlign{kCenterText};
60 int32_t fStyle{0};
61 FontPtr fFont{nullptr};
62 CColor fFontColor{kWhiteCColor};
63 CColor fShadowColor{kBlackCColor};
64 CPoint fTextInset{0, 0};
65 CPoint fShadowTextOffset{1., 1.};
66 bool fAntialias{true};
67
68 constexpr bool hasStyle(Style iStyle) const { return fStyle & static_cast<int32_t>(iStyle); }
69 inline void setStyle(Style iStyle) { fStyle = static_cast<int32_t>(iStyle); }
70 inline void addStyle(Style iStyle) { fStyle |= static_cast<int32_t>(iStyle); }
71};
72
73using RelativeCoord = CCoord;
74using AbsoluteCoord = CCoord;
75using RelativePoint = CPoint;
76using AbsolutePoint = CPoint;
77using RelativeRect = CRect;
78using AbsoluteRect = CRect;
79
81{
82public:
83 explicit RelativeView(CView const *iView) : fRect{iView->getViewSize()}
84 {
85 }
86
87 explicit RelativeView(AbsoluteRect const &iRect) : fRect{iRect}
88 {
89 }
90
92 {
93 return RelativeRect(0, 0, fRect.getWidth(), fRect.getHeight());
94 }
95
96 inline CCoord getWidth() const
97 {
98 return fRect.getWidth();
99 }
100
101 inline Range getHorizontalRange() const { return Range{0, getWidth()}; }
102
103 inline Range getVerticalRange() const { return Range{0, getHeight()}; }
104
105 inline CCoord getHeight() const
106 {
107 return fRect.getHeight();
108 }
109
111 {
112 return x + fRect.left;
113 }
114
116 {
117 return y + fRect.top;
118 }
119
121 {
122 return x - fRect.left;
123 }
124
126 {
127 return y - fRect.top;
128 }
129
130 inline AbsolutePoint toAbsolutePoint(RelativePoint const &iPoint) const
131 {
132 return AbsolutePoint{toAbsoluteX(iPoint.x), toAbsoluteY(iPoint.y)};
133 }
134
136 {
137 return RelativePoint{fromAbsoluteX(iPoint.x), fromAbsoluteY(iPoint.y)};
138 }
139
145 {
146 return RelativePoint{
147 Utils::clamp(fromAbsoluteX(iPoint.x), 0.0, fRect.getWidth()),
148 Utils::clamp(fromAbsoluteY(iPoint.y), 0.0, fRect.getHeight())
149 };
150 }
151
153 {
154 return CPoint{x + fRect.left, y + fRect.top};
155 }
156
157 inline AbsoluteRect toAbsoluteRect(RelativeRect const &iRect) const
158 {
159 return CRect(toAbsolutePoint(iRect.getTopLeft()), iRect.getSize());
160 }
161
162protected:
164};
165
170{
171public:
172 RelativeDrawContext(CView *iView, CDrawContext *iDrawContext) : RelativeView{iView}, fDrawContext{iDrawContext}
173 {
174 }
175
176 void drawLine(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &color)
177 {
178 fDrawContext->setFrameColor(color);
179 fDrawContext->drawLine(toAbsolutePoint(x1, y1), toAbsolutePoint(x2, y2));
180 }
181
182 void drawRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
183 {
184 drawRect(RelativeRect{x1, y1, x2, y2}, iStrokeColor);
185 }
186
187 void drawRect(RelativeRect const &iRect, CColor const &iStrokeColor)
188 {
189 fDrawContext->setFrameColor(iStrokeColor);
190 fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawStroked);
191 }
192
193 void fillRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
194 {
195 fillRect(RelativeRect{x1, y1, x2, y2}, iColor);
196 }
197
198 void fillRect(RelativeRect const &iRect, CColor const &iColor)
199 {
200 fDrawContext->setFillColor(iColor);
201 fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawFilled);
202 }
203
204 void fillAndStrokeRect(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
205 {
206 fDrawContext->setFillColor(iFillColor);
207 fDrawContext->setFrameColor(iStrokeColor);
208 fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawFilledAndStroked);
209 }
210
211 void drawEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
212 {
213 drawEllipse(RelativeRect{x1, y1, x2, y2}, iStrokeColor);
214 }
215
216 void drawEllipse(RelativeRect const &iRect, CColor const &iStrokeColor)
217 {
218 fDrawContext->setFrameColor(iStrokeColor);
219 fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawStroked);
220 }
221
222 void fillEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
223 {
224 fillEllipse(RelativeRect{x1, y1, x2, y2}, iColor);
225 }
226
227 void fillEllipse(RelativeRect const &iRect, CColor const &iColor)
228 {
229 fDrawContext->setFillColor(iColor);
230 fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawFilled);
231 }
232
233 void fillAndStrokeEllipse(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
234 {
235 fDrawContext->setFillColor(iFillColor);
236 fDrawContext->setFrameColor(iStrokeColor);
237 fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawFilledAndStroked);
238 }
239
240 void drawString(UTF8String const &iText, RelativeCoord x, RelativeCoord y, RelativeCoord iHeight, StringDrawContext &iSdc)
241 {
242 CRect size{x, y, fDrawContext->getStringWidth(iText.getPlatformString()), iHeight};
243 drawString(iText, size, iSdc);
244 }
245
246 void drawString(UTF8String const &iText, RelativeRect const &fSize, StringDrawContext &iSdc);
247
248 void drawString(UTF8String const &iText, StringDrawContext &iSdc)
249 {
250 drawString(iText, getViewSize(), iSdc);
251 }
252
253#if EDITOR_MODE
260 template<typename... Args>
261 inline void debugXY(RelativeCoord x, RelativeCoord y, char const *iFormat, Args&& ...iArgs)
262 {
263 // handles strings up to 4k characters so should be fine for debug...
264 auto s = Steinberg::String().printf(iFormat, std::forward<Args>(iArgs)...);
265 debugText(x, y, s.text8());
266 }
267
268 // debugTopLeft
269 template<typename... Args>
270 inline void debugTopLeft(char const *iFormat, Args&& ...iArgs) { debugXY(0, 0, iFormat, std::forward<Args>(iArgs)...); }
271
272 // debugTopRight
273 template<typename... Args>
274 inline void debugTopRight(char const *iFormat, Args&& ...iArgs) { debugXY(-1, 0, iFormat, std::forward<Args>(iArgs)...); }
275
276 // debugBottomLeft
277 template<typename... Args>
278 inline void debugBottomLeft(char const *iFormat, Args&& ...iArgs) { debugXY(0, -1, iFormat, std::forward<Args>(iArgs)...); }
279
280 // debugBottomRight
281 template<typename... Args>
282 inline void debugBottomRight(char const *iFormat, Args&& ...iArgs) { debugXY(-1, -1, iFormat, std::forward<Args>(iArgs)...); }
283
287 template<typename... Args>
288 inline void debug(char const *iFormat, Args&& ...iArgs) { debugTopLeft(iFormat, std::forward<Args>(iArgs)...); }
289
293 inline void debugText(char const *iText) { debugText(0, 0, iText); }
294
298 void debugText(RelativeCoord x, RelativeCoord y, char const *iText);
299
300#endif
301
302protected:
303 CDrawContext *fDrawContext;
304
305#if EDITOR_MODE
306public:
307 CColor fDebugStringColor{kGreenCColor};
308 FontPtr fDebugStringFont{kNormalFontVerySmall};
309#endif
310};
311
312}
313}
314}
void drawLine(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &color)
Definition DrawContext.h:176
void fillRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
Definition DrawContext.h:193
void fillEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
Definition DrawContext.h:222
void drawEllipse(RelativeRect const &iRect, CColor const &iStrokeColor)
Definition DrawContext.h:216
void drawRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
Definition DrawContext.h:182
void drawEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
Definition DrawContext.h:211
void fillAndStrokeEllipse(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
Definition DrawContext.h:233
void drawString(UTF8String const &iText, StringDrawContext &iSdc)
Definition DrawContext.h:248
void drawRect(RelativeRect const &iRect, CColor const &iStrokeColor)
Definition DrawContext.h:187
RelativeDrawContext(CView *iView, CDrawContext *iDrawContext)
Definition DrawContext.h:172
void drawString(UTF8String const &iText, RelativeCoord x, RelativeCoord y, RelativeCoord iHeight, StringDrawContext &iSdc)
Definition DrawContext.h:240
CDrawContext * fDrawContext
Definition DrawContext.h:303
void fillEllipse(RelativeRect const &iRect, CColor const &iColor)
Definition DrawContext.h:227
void fillRect(RelativeRect const &iRect, CColor const &iColor)
Definition DrawContext.h:198
void fillAndStrokeRect(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
Definition DrawContext.h:204
Range getHorizontalRange() const
Definition DrawContext.h:101
RelativeView(AbsoluteRect const &iRect)
Definition DrawContext.h:87
RelativeCoord fromAbsoluteX(AbsoluteCoord x) const
Definition DrawContext.h:120
Range getVerticalRange() const
Definition DrawContext.h:103
RelativeRect getViewSize() const
Definition DrawContext.h:91
CCoord getWidth() const
Definition DrawContext.h:96
AbsolutePoint toAbsolutePoint(RelativePoint const &iPoint) const
Definition DrawContext.h:130
AbsoluteRect const & fRect
Definition DrawContext.h:163
RelativePoint fromAbsolutePoint(AbsolutePoint const &iPoint) const
Definition DrawContext.h:135
AbsolutePoint toAbsolutePoint(RelativeCoord x, RelativeCoord y) const
Definition DrawContext.h:152
CCoord getHeight() const
Definition DrawContext.h:105
AbsoluteRect toAbsoluteRect(RelativeRect const &iRect) const
Definition DrawContext.h:157
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:144
RelativeView(CView const *iView)
Definition DrawContext.h:83
AbsoluteCoord toAbsoluteY(RelativeCoord y) const
Definition DrawContext.h:115
RelativeCoord fromAbsoluteY(AbsoluteCoord y) const
Definition DrawContext.h:125
AbsoluteCoord toAbsoluteX(RelativeCoord x) const
Definition DrawContext.h:110
static T clamp(const U &iValue, const T &iLower, const T &iUpper)
Make sure that the value remains within its bounds.
Definition Misc.h:34
Definition DrawContext.cpp:25
CRect AbsoluteRect
Definition DrawContext.h:78
CRect RelativeRect
Definition DrawContext.h:77
CPoint AbsolutePoint
Definition DrawContext.h:76
CCoord RelativeCoord
Definition DrawContext.h:73
CPoint RelativePoint
Definition DrawContext.h:75
CCoord AbsoluteCoord
Definition DrawContext.h:74
CFontDesc * FontPtr
Definition Types.h:54
Utils::Range< CCoord > Range
Defines a Range.
Definition Types.h:64
DeprecatedCControlEnum
Implementation note: The CControlEnum enumeration has been removed from VSTGUI.
Definition DrawContext.h:43
@ kShadowText
Definition DrawContext.h:44
@ kNoTextStyle
Definition DrawContext.h:45
Definition Clock.h:24
Definition Clock.h:23
The context which contains the details on how the string should be drawn.
Definition DrawContext.h:51
void setStyle(Style iStyle)
Definition DrawContext.h:69
CHoriTxtAlign fHorizTxtAlign
Definition DrawContext.h:58
CPoint fShadowTextOffset
Definition DrawContext.h:65
Style
Definition DrawContext.h:53
bool fAntialias
Definition DrawContext.h:66
void addStyle(Style iStyle)
Definition DrawContext.h:70
constexpr bool hasStyle(Style iStyle) const
Definition DrawContext.h:68
FontPtr fFont
Definition DrawContext.h:61
CColor fFontColor
Definition DrawContext.h:62
int32_t fStyle
Should be a value provided by StringDrawContext::Style.
Definition DrawContext.h:60
CPoint fTextInset
Definition DrawContext.h:64
CColor fShadowColor
Definition DrawContext.h:63