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