Jamba C++ API 8.0.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
37{
38 enum class Style : int32_t
39 {
40 kShadowText = 1 << 2,
41 kNoTextStyle = 1 << 11
42 };
43
44 CHoriTxtAlign fHorizTxtAlign{kCenterText};
46 int32_t fStyle{0};
47 FontPtr fFont{nullptr};
48 CColor fFontColor{kWhiteCColor};
49 CColor fShadowColor{kBlackCColor};
50 CPoint fTextInset{0, 0};
51 CPoint fShadowTextOffset{1., 1.};
52 bool fAntialias{true};
53
54 constexpr bool hasStyle(Style iStyle) const { return fStyle & static_cast<int32_t>(iStyle); }
55 void setStyle(Style iStyle) { fStyle = static_cast<int32_t>(iStyle); }
56 void addStyle(Style iStyle) { fStyle |= static_cast<int32_t>(iStyle); }
57};
58
59using RelativeCoord = CCoord;
60using AbsoluteCoord = CCoord;
61using RelativePoint = CPoint;
62using AbsolutePoint = CPoint;
63using RelativeRect = CRect;
64using AbsoluteRect = CRect;
65
67{
68public:
69 explicit RelativeView(CView const *iView) : fRect{iView->getViewSize()}
70 {
71 }
72
73 explicit RelativeView(AbsoluteRect const &iRect) : fRect{iRect}
74 {
75 }
76
78 {
79 return RelativeRect(0, 0, fRect.getWidth(), fRect.getHeight());
80 }
81
82 CCoord getWidth() const
83 {
84 return fRect.getWidth();
85 }
86
87 Range getHorizontalRange() const { return Range{0, getWidth()}; }
88
89 Range getVerticalRange() const { return Range{0, getHeight()}; }
90
91 CCoord getHeight() const
92 {
93 return fRect.getHeight();
94 }
95
97 {
98 return x + fRect.left;
99 }
100
102 {
103 return y + fRect.top;
104 }
105
107 {
108 return x - fRect.left;
109 }
110
112 {
113 return y - fRect.top;
114 }
115
117 {
118 return AbsolutePoint{toAbsoluteX(iPoint.x), toAbsoluteY(iPoint.y)};
119 }
120
122 {
123 return RelativePoint{fromAbsoluteX(iPoint.x), fromAbsoluteY(iPoint.y)};
124 }
125
131 {
132 return RelativePoint{
133 Utils::clamp(fromAbsoluteX(iPoint.x), 0.0, fRect.getWidth()),
134 Utils::clamp(fromAbsoluteY(iPoint.y), 0.0, fRect.getHeight())
135 };
136 }
137
139 {
140 return CPoint{x + fRect.left, y + fRect.top};
141 }
142
144 {
145 return CRect(toAbsolutePoint(iRect.getTopLeft()), iRect.getSize());
146 }
147
148protected:
150};
151
156{
157public:
158 RelativeDrawContext(CView *iView, CDrawContext *iDrawContext) : RelativeView{iView}, fDrawContext{iDrawContext}
159 {
160 }
161
162 void drawLine(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &color)
163 {
164 fDrawContext->setFrameColor(color);
165 fDrawContext->drawLine(toAbsolutePoint(x1, y1), toAbsolutePoint(x2, y2));
166 }
167
168 void drawRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
169 {
170 drawRect(RelativeRect{x1, y1, x2, y2}, iStrokeColor);
171 }
172
173 void drawRect(RelativeRect const &iRect, CColor const &iStrokeColor)
174 {
175 fDrawContext->setFrameColor(iStrokeColor);
176 fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawStroked);
177 }
178
179 void fillRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
180 {
181 fillRect(RelativeRect{x1, y1, x2, y2}, iColor);
182 }
183
184 void fillRect(RelativeRect const &iRect, CColor const &iColor)
185 {
186 fDrawContext->setFillColor(iColor);
187 fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawFilled);
188 }
189
190 void fillAndStrokeRect(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
191 {
192 fDrawContext->setFillColor(iFillColor);
193 fDrawContext->setFrameColor(iStrokeColor);
194 fDrawContext->drawRect(toAbsoluteRect(iRect), kDrawFilledAndStroked);
195 }
196
197 void drawEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
198 {
199 drawEllipse(RelativeRect{x1, y1, x2, y2}, iStrokeColor);
200 }
201
202 void drawEllipse(RelativeRect const &iRect, CColor const &iStrokeColor)
203 {
204 fDrawContext->setFrameColor(iStrokeColor);
205 fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawStroked);
206 }
207
208 void fillEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
209 {
210 fillEllipse(RelativeRect{x1, y1, x2, y2}, iColor);
211 }
212
213 void fillEllipse(RelativeRect const &iRect, CColor const &iColor)
214 {
215 fDrawContext->setFillColor(iColor);
216 fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawFilled);
217 }
218
219 void fillAndStrokeEllipse(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
220 {
221 fDrawContext->setFillColor(iFillColor);
222 fDrawContext->setFrameColor(iStrokeColor);
223 fDrawContext->drawEllipse(toAbsoluteRect(iRect), kDrawFilledAndStroked);
224 }
225
226 void drawString(UTF8String const &iText, RelativeCoord x, RelativeCoord y, RelativeCoord iHeight, StringDrawContext &iSdc)
227 {
228 CRect size{x, y, fDrawContext->getStringWidth(iText.getPlatformString()), iHeight};
229 drawString(iText, size, iSdc);
230 }
231
232 void drawString(UTF8String const &iText, RelativeRect const &fSize, StringDrawContext &iSdc);
233
234 void drawString(UTF8String const &iText, StringDrawContext &iSdc)
235 {
236 drawString(iText, getViewSize(), iSdc);
237 }
238
239#if EDITOR_MODE
246 template<typename... Args>
247 void debugXY(RelativeCoord x, RelativeCoord y, char const *iFormat, Args&& ...iArgs)
248 {
249 // handles strings up to 4k characters so should be fine for debug...
250 auto s = Steinberg::String().printf(iFormat, std::forward<Args>(iArgs)...);
251 debugText(x, y, s.text8());
252 }
253
254 // debugTopLeft
255 template<typename... Args>
256 void debugTopLeft(char const *iFormat, Args&& ...iArgs) { debugXY(0, 0, iFormat, std::forward<Args>(iArgs)...); }
257
258 // debugTopRight
259 template<typename... Args>
260 void debugTopRight(char const *iFormat, Args&& ...iArgs) { debugXY(-1, 0, iFormat, std::forward<Args>(iArgs)...); }
261
262 // debugBottomLeft
263 template<typename... Args>
264 void debugBottomLeft(char const *iFormat, Args&& ...iArgs) { debugXY(0, -1, iFormat, std::forward<Args>(iArgs)...); }
265
266 // debugBottomRight
267 template<typename... Args>
268 void debugBottomRight(char const *iFormat, Args&& ...iArgs) { debugXY(-1, -1, iFormat, std::forward<Args>(iArgs)...); }
269
273 template<typename... Args>
274 void debug(char const *iFormat, Args&& ...iArgs) { debugTopLeft(iFormat, std::forward<Args>(iArgs)...); }
275
279 void debugText(char const *iText) { debugText(0, 0, iText); }
280
284 void debugText(RelativeCoord x, RelativeCoord y, char const *iText);
285
286#endif
287
288protected:
289 CDrawContext *fDrawContext;
290
291#if EDITOR_MODE
292public:
293 CColor fDebugStringColor{kGreenCColor};
294 FontPtr fDebugStringFont{kNormalFontVerySmall};
295#endif
296};
297
298}
299}
300}
void drawLine(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &color)
Definition DrawContext.h:162
void fillRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
Definition DrawContext.h:179
void fillEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iColor)
Definition DrawContext.h:208
void drawEllipse(RelativeRect const &iRect, CColor const &iStrokeColor)
Definition DrawContext.h:202
void drawRect(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
Definition DrawContext.h:168
void drawEllipse(RelativeCoord x1, RelativeCoord y1, RelativeCoord x2, RelativeCoord y2, CColor const &iStrokeColor)
Definition DrawContext.h:197
void fillAndStrokeEllipse(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
Definition DrawContext.h:219
void drawString(UTF8String const &iText, StringDrawContext &iSdc)
Definition DrawContext.h:234
void drawRect(RelativeRect const &iRect, CColor const &iStrokeColor)
Definition DrawContext.h:173
RelativeDrawContext(CView *iView, CDrawContext *iDrawContext)
Definition DrawContext.h:158
void drawString(UTF8String const &iText, RelativeCoord x, RelativeCoord y, RelativeCoord iHeight, StringDrawContext &iSdc)
Definition DrawContext.h:226
CDrawContext * fDrawContext
Definition DrawContext.h:289
void fillEllipse(RelativeRect const &iRect, CColor const &iColor)
Definition DrawContext.h:213
void fillRect(RelativeRect const &iRect, CColor const &iColor)
Definition DrawContext.h:184
void fillAndStrokeRect(RelativeRect const &iRect, CColor const &iFillColor, CColor const &iStrokeColor)
Definition DrawContext.h:190
Range getHorizontalRange() const
Definition DrawContext.h:87
RelativeView(AbsoluteRect const &iRect)
Definition DrawContext.h:73
RelativeCoord fromAbsoluteX(AbsoluteCoord x) const
Definition DrawContext.h:106
Range getVerticalRange() const
Definition DrawContext.h:89
RelativeRect getViewSize() const
Definition DrawContext.h:77
CCoord getWidth() const
Definition DrawContext.h:82
AbsolutePoint toAbsolutePoint(RelativePoint const &iPoint) const
Definition DrawContext.h:116
AbsoluteRect const & fRect
Definition DrawContext.h:149
RelativePoint fromAbsolutePoint(AbsolutePoint const &iPoint) const
Definition DrawContext.h:121
AbsolutePoint toAbsolutePoint(RelativeCoord x, RelativeCoord y) const
Definition DrawContext.h:138
CCoord getHeight() const
Definition DrawContext.h:91
AbsoluteRect toAbsoluteRect(RelativeRect const &iRect) const
Definition DrawContext.h:143
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:130
RelativeView(CView const *iView)
Definition DrawContext.h:69
AbsoluteCoord toAbsoluteY(RelativeCoord y) const
Definition DrawContext.h:101
RelativeCoord fromAbsoluteY(AbsoluteCoord y) const
Definition DrawContext.h:111
AbsoluteCoord toAbsoluteX(RelativeCoord x) const
Definition DrawContext.h:96
static constexpr 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:64
CRect RelativeRect
Definition DrawContext.h:63
CPoint AbsolutePoint
Definition DrawContext.h:62
CCoord RelativeCoord
Definition DrawContext.h:59
CPoint RelativePoint
Definition DrawContext.h:61
CCoord AbsoluteCoord
Definition DrawContext.h:60
CFontDesc * FontPtr
Definition Types.h:52
Utils::Range< CCoord > Range
Defines a Range.
Definition Types.h:62
Definition Clock.h:24
Definition Clock.h:23
The context which contains the details on how the string should be drawn.
Definition DrawContext.h:37
void setStyle(Style iStyle)
Definition DrawContext.h:55
CHoriTxtAlign fHorizTxtAlign
Definition DrawContext.h:44
CPoint fShadowTextOffset
Definition DrawContext.h:51
Style
Definition DrawContext.h:39
@ kShadowText
Definition DrawContext.h:40
@ kNoTextStyle
Definition DrawContext.h:41
bool fAntialias
Definition DrawContext.h:52
void addStyle(Style iStyle)
Definition DrawContext.h:56
constexpr bool hasStyle(Style iStyle) const
Definition DrawContext.h:54
FontPtr fFont
Definition DrawContext.h:47
CColor fFontColor
Definition DrawContext.h:48
int32_t fStyle
Should be a value provided by StringDrawContext::Style.
Definition DrawContext.h:46
CPoint fTextInset
Definition DrawContext.h:50
CColor fShadowColor
Definition DrawContext.h:49