Vuo 2.4.4
Loading...
Searching...
No Matches
VuoUiThemeButtonRounded.cc
Go to the documentation of this file.
1
10#include "VuoUiThemeBase.hh"
11
12#include "VuoSceneText.h"
13
15#ifdef VUO_COMPILER
17 "title": "UI Theme: Button, Rounded",
18 "dependencies": [
19 "VuoBoolean",
20 "VuoColor",
21 "VuoFont",
22 "VuoImage",
23 "VuoImageText",
24 "VuoLayer",
25 "VuoPoint2d",
26 "VuoReal",
27 "VuoAnchor",
28 "VuoHorizontalAlignment",
29 "VuoVerticalAlignment",
30 "VuoSceneText"
31 ],
32 });
33#endif
35
40{
41private:
42 VuoReal minimumWidth;
43 VuoReal minimumHeight;
44 VuoFont labelFont;
45 VuoAnchor labelPosition;
46 VuoPoint2d labelPadding;
47 VuoColor labelColor;
48 VuoColor labelColorHovered;
49 VuoColor labelColorPressed;
50 VuoColor backgroundColor;
51 VuoColor backgroundColorHovered;
52 VuoColor backgroundColorPressed;
53 VuoColor borderColor;
54 VuoColor borderColorHovered;
55 VuoColor borderColorPressed;
56 VuoReal borderThickness;
57 VuoReal cornerRoundness;
58
59public:
60 static std::string type;
61
65 static VuoSerializable *makeFromJson(json_object *js)
66 {
67 return new VuoUiThemeButtonRounded(VuoJson_getObjectValue(VuoReal, js, "minimumWidth", 0.25),
68 VuoJson_getObjectValue(VuoReal, js, "minimumHeight", 0.08),
69 VuoJson_getObjectValue(VuoFont, js, "labelFont", (VuoFont){VuoText_make("Avenir-Medium"), 24, false, (VuoColor){1,1,1,1}, VuoHorizontalAlignment_Center, 1, 1}),
70 VuoJson_getObjectValue(VuoAnchor, js, "labelPosition", VuoAnchor_make(VuoHorizontalAlignment_Center, VuoVerticalAlignment_Center)),
71 VuoJson_getObjectValue(VuoPoint2d, js, "labelPadding", (VuoPoint2d){.03, 0}),
72 VuoJson_getObjectValue(VuoColor, js, "labelColor", (VuoColor){1,1,1,.7}),
73 VuoJson_getObjectValue(VuoColor, js, "labelColorHovered", (VuoColor){1,1,1,1}),
74 VuoJson_getObjectValue(VuoColor, js, "labelColorPressed", (VuoColor){1,1,1,1}),
75 VuoJson_getObjectValue(VuoColor, js, "backgroundColor", (VuoColor){.4,.4,.4,1}),
76 VuoJson_getObjectValue(VuoColor, js, "backgroundColorHovered", (VuoColor){.4,.42,.44,1}),
77 VuoJson_getObjectValue(VuoColor, js, "backgroundColorPressed", (VuoColor){.4,.5,.6,1}),
78 VuoJson_getObjectValue(VuoColor, js, "borderColor", (VuoColor){.46,.46,.46,1}),
79 VuoJson_getObjectValue(VuoColor, js, "borderColorHovered", (VuoColor){.46,.48,.49,1}),
80 VuoJson_getObjectValue(VuoColor, js, "borderColorPressed", (VuoColor){.46,.55,.64,1}),
81 VuoJson_getObjectValue(VuoReal, js, "borderThickness", 0.005),
82 VuoJson_getObjectValue(VuoReal, js, "cornerRoundness", 0.5));
83 }
84
89 VuoReal _minimumHeight,
90 VuoFont _labelFont,
91 VuoAnchor _labelPosition,
92 VuoPoint2d _labelPadding,
93 VuoColor _labelColor,
94 VuoColor _labelColorHovered,
95 VuoColor _labelColorPressed,
96 VuoColor _backgroundColor,
97 VuoColor _backgroundColorHovered,
98 VuoColor _backgroundColorPressed,
99 VuoColor _borderColor,
100 VuoColor _borderColorHovered,
101 VuoColor _borderColorPressed,
102 VuoReal _borderThickness,
103 VuoReal _cornerRoundness)
104 {
105 minimumWidth = _minimumWidth;
106 minimumHeight = _minimumHeight;
107 labelFont = _labelFont;
108 VuoFont_retain(labelFont);
109 labelPosition = _labelPosition;
110 labelPadding = _labelPadding;
111 labelColor = _labelColor;
112 labelColorHovered = _labelColorHovered;
113 labelColorPressed = _labelColorPressed;
114 backgroundColor = _backgroundColor;
115 backgroundColorHovered = _backgroundColorHovered;
116 backgroundColorPressed = _backgroundColorPressed;
117 borderColor = _borderColor;
118 borderColorHovered = _borderColorHovered;
119 borderColorPressed = _borderColorPressed;
120 borderThickness = _borderThickness;
121 cornerRoundness = _cornerRoundness;
122 }
124 {
125 VuoFont_release(labelFont);
126 }
127
131 json_object *getJson()
132 {
133 json_object *json = VuoSerializable::getJson();
134 json_object_object_add(json, "minimumWidth", VuoReal_getJson(minimumWidth));
135 json_object_object_add(json, "minimumHeight", VuoReal_getJson(minimumHeight));
136 json_object_object_add(json, "labelFont", VuoFont_getJson(labelFont));
137 json_object_object_add(json, "labelPosition", VuoAnchor_getJson(labelPosition));
138 json_object_object_add(json, "labelPadding", VuoPoint2d_getJson(labelPadding));
139 json_object_object_add(json, "labelColor", VuoColor_getJson(labelColor));
140 json_object_object_add(json, "labelColorHovered", VuoColor_getJson(labelColorHovered));
141 json_object_object_add(json, "labelColorPressed", VuoColor_getJson(labelColorPressed));
142 json_object_object_add(json, "backgroundColor", VuoColor_getJson(backgroundColor));
143 json_object_object_add(json, "backgroundColorHovered", VuoColor_getJson(backgroundColorHovered));
144 json_object_object_add(json, "backgroundColorPressed", VuoColor_getJson(backgroundColorPressed));
145 json_object_object_add(json, "borderColor", VuoColor_getJson(borderColor));
146 json_object_object_add(json, "borderColorHovered", VuoColor_getJson(borderColorHovered));
147 json_object_object_add(json, "borderColorPressed", VuoColor_getJson(borderColorPressed));
148 json_object_object_add(json, "borderThickness", VuoReal_getJson(borderThickness));
149 json_object_object_add(json, "cornerRoundness", VuoReal_getJson(cornerRoundness));
150 return json;
151 }
152
157 {
158 return strdup("Action Button Theme (Rounded)");
159 }
160
164 bool operator==(const VuoSerializable &that)
165 {
167 return VuoReal_areEqual(minimumWidth, thatSpecialized->minimumWidth)
168 && VuoReal_areEqual(minimumHeight, thatSpecialized->minimumHeight)
169 && VuoFont_areEqual(labelFont, thatSpecialized->labelFont)
170 && VuoAnchor_areEqual(labelPosition, thatSpecialized->labelPosition)
171 && VuoPoint2d_areEqual(labelPadding, thatSpecialized->labelPadding)
172 && VuoColor_areEqual(labelColor, thatSpecialized->labelColor)
173 && VuoColor_areEqual(labelColorHovered, thatSpecialized->labelColorHovered)
174 && VuoColor_areEqual(labelColorPressed, thatSpecialized->labelColorPressed)
175 && VuoColor_areEqual(backgroundColor, thatSpecialized->backgroundColor)
176 && VuoColor_areEqual(backgroundColorHovered, thatSpecialized->backgroundColorHovered)
177 && VuoColor_areEqual(backgroundColorPressed, thatSpecialized->backgroundColorPressed)
178 && VuoColor_areEqual(borderColor, thatSpecialized->borderColor)
179 && VuoColor_areEqual(borderColorHovered, thatSpecialized->borderColorHovered)
180 && VuoColor_areEqual(borderColorPressed, thatSpecialized->borderColorPressed)
181 && VuoReal_areEqual(borderThickness, thatSpecialized->borderThickness)
182 && VuoReal_areEqual(cornerRoundness, thatSpecialized->cornerRoundness);
183 }
184
188 bool operator<(const VuoSerializable &that)
189 {
191 VuoType_returnInequality(VuoReal, minimumWidth, thatSpecialized->minimumWidth);
192 VuoType_returnInequality(VuoReal, minimumHeight, thatSpecialized->minimumHeight);
193 VuoType_returnInequality(VuoFont, labelFont, thatSpecialized->labelFont);
194 VuoType_returnInequality(VuoAnchor, labelPosition, thatSpecialized->labelPosition);
195 VuoType_returnInequality(VuoPoint2d, labelPadding, thatSpecialized->labelPadding);
196 VuoType_returnInequality(VuoColor, labelColor, thatSpecialized->labelColor);
197 VuoType_returnInequality(VuoColor, labelColorHovered, thatSpecialized->labelColorHovered);
198 VuoType_returnInequality(VuoColor, labelColorPressed, thatSpecialized->labelColorPressed);
199 VuoType_returnInequality(VuoColor, backgroundColor, thatSpecialized->backgroundColor);
200 VuoType_returnInequality(VuoColor, backgroundColorHovered, thatSpecialized->backgroundColorHovered);
201 VuoType_returnInequality(VuoColor, backgroundColorPressed, thatSpecialized->backgroundColorPressed);
202 VuoType_returnInequality(VuoColor, borderColor, thatSpecialized->borderColor);
203 VuoType_returnInequality(VuoColor, borderColorHovered, thatSpecialized->borderColorHovered);
204 VuoType_returnInequality(VuoColor, borderColorPressed, thatSpecialized->borderColorPressed);
205 VuoType_returnInequality(VuoReal, borderThickness, thatSpecialized->borderThickness);
206 VuoType_returnInequality(VuoReal, cornerRoundness, thatSpecialized->cornerRoundness);
207 return false;
208 }
209
217 VuoLayer render(VuoRenderedLayers renderedLayers, VuoText label, VuoPoint2d position, VuoAnchor anchor, bool isHovered, bool isPressed)
218 {
219 VuoLayer textLayer = nullptr;
220 bool hasText = VuoText_length(label) > 0;
221
222 float actualWidth = minimumWidth;
223 float actualHeight = minimumHeight;
224
225 VuoFont f = labelFont;
226 if (isPressed)
227 f.color = (VuoColor){f.color.r * labelColorPressed.r,
228 f.color.g * labelColorPressed.g,
229 f.color.b * labelColorPressed.b,
230 f.color.a * labelColorPressed.a};
231 else if (isHovered)
232 f.color = (VuoColor){f.color.r * labelColorHovered.r,
233 f.color.g * labelColorHovered.g,
234 f.color.b * labelColorHovered.b,
235 f.color.a * labelColorHovered.a};
236 else
237 f.color = (VuoColor){f.color.r * labelColor.r,
238 f.color.g * labelColor.g,
239 f.color.b * labelColor.b,
240 f.color.a * labelColor.a};
241
242 VuoPoint2d textSize = (VuoPoint2d){0,0};
243 if(hasText)
244 {
245 textSize = VuoRenderedLayers_getTextSize(renderedLayers, label, labelFont, true, 1, 0, INFINITY, true);
246
247 actualWidth = fmax(actualWidth, textSize.x);
248 actualHeight = fmax(actualHeight, textSize.y);
249
250 textLayer = (VuoLayer)VuoSceneText_make(label, f, true, INFINITY, labelPosition);
251 }
252
253 actualWidth = fmax(actualWidth, textSize.x + labelPadding.x * 2);
254 actualHeight = fmax(actualHeight, textSize.y + labelPadding.y * 2);
255
256 actualWidth += borderThickness * 2;
257 actualHeight += borderThickness * 2;
258
259 if (VuoReal_areEqual(actualWidth, 0) || VuoReal_areEqual(actualHeight, 0))
260 {
261 VuoLayer_retain(textLayer);
262 VuoLayer_release(textLayer);
263 return nullptr;
264 }
265
266 float innerCornerRoundness = (actualHeight - borderThickness * 2 - (actualHeight * (1-cornerRoundness))) / (actualHeight - borderThickness * 2);
267
268 VuoLayer backgroundLayer = VuoLayer_makeRoundedRectangle( VuoText_make("Button Background"),
269 isPressed ? backgroundColorPressed : (isHovered ? backgroundColorHovered : backgroundColor),
270 VuoPoint2d_make(0,0),
271 0,
272 actualWidth - borderThickness * 2,
273 actualHeight - borderThickness * 2,
274 1,
275 innerCornerRoundness);
276
277 VuoLayer borderLayer = VuoLayer_makeRoundedRectangle( VuoText_make("Button Border"),
278 isPressed ? borderColorPressed : (isHovered ? borderColorHovered : borderColor),
279 VuoPoint2d_make(0,0),
280 0,
281 actualWidth,
282 actualHeight,
283 1,
284 cornerRoundness);
285
286 VuoPoint3d offset = VuoPoint3d_make(0,0,0);
287
288 if (VuoAnchor_getHorizontal(anchor) == VuoHorizontalAlignment_Left)
289 offset.x = actualWidth * .5f;
290 else if (VuoAnchor_getHorizontal(anchor) == VuoHorizontalAlignment_Right)
291 offset.x = -actualWidth * .5f;
292
293 if (VuoAnchor_getVertical(anchor) == VuoVerticalAlignment_Top)
294 offset.y = -actualHeight * .5f;
295 else if (VuoAnchor_getVertical(anchor) == VuoVerticalAlignment_Bottom)
296 offset.y = actualHeight * .5f;
297
298 VuoSceneObject_translate((VuoSceneObject)backgroundLayer, offset);
299 VuoSceneObject_translate((VuoSceneObject)borderLayer, offset);
300
301 if(hasText)
302 {
303 VuoSceneObject_translate((VuoSceneObject)textLayer, offset);
304
305 if (VuoAnchor_getHorizontal(labelPosition) != VuoHorizontalAlignment_Center)
306 VuoSceneObject_translate((VuoSceneObject)textLayer, (VuoPoint3d){
307 (VuoAnchor_getHorizontal(labelPosition) == VuoHorizontalAlignment_Left ? (-(actualWidth-labelPadding.x)) : (actualWidth-labelPadding.x)) * .5f,
308 0, 0});
309
310 if (VuoAnchor_getVertical(labelPosition) != VuoVerticalAlignment_Center)
311 VuoSceneObject_translate((VuoSceneObject)textLayer, (VuoPoint3d){0,
312 (VuoAnchor_getVertical(labelPosition) == VuoVerticalAlignment_Top ? (actualHeight-labelPadding.y) : (-(actualHeight-labelPadding.y))) * .5f,
313 0});
314 }
315
317 VuoLocal(layers);
318
319 VuoListAppendValue_VuoLayer(layers, borderLayer);
320 VuoListAppendValue_VuoLayer(layers, backgroundLayer);
321 if(hasText) VuoListAppendValue_VuoLayer(layers, textLayer);
322
323 return VuoLayer_makeGroup(layers, VuoTransform2d_make(position, 0, VuoPoint2d_make(1,1)));
324 }
325};
326
328
333 VuoReal minimumHeight,
334 VuoFont labelFont,
335 VuoAnchor labelAnchor,
336 VuoPoint2d labelPadding,
337 VuoColor labelColor,
338 VuoColor labelColorHovered,
339 VuoColor labelColorPressed,
340 VuoColor backgroundColor,
341 VuoColor backgroundColorHovered,
342 VuoColor backgroundColorPressed,
343 VuoColor borderColor,
344 VuoColor borderColorHovered,
345 VuoColor borderColorPressed,
346 VuoReal borderThickness,
347 VuoReal cornerRoundness)
348{
349 return reinterpret_cast<VuoUiTheme>(new VuoUiThemeButtonRounded(minimumWidth,
350 minimumHeight,
351 labelFont,
352 labelAnchor,
353 labelPadding,
354 labelColor,
355 labelColorHovered,
356 labelColorPressed,
357 backgroundColor,
358 backgroundColorHovered,
359 backgroundColorPressed,
360 borderColor,
361 borderColorHovered,
362 borderColorPressed,
363 borderThickness,
364 cornerRoundness));
365}