FlatUI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Enumerations Groups Pages
flatui.h
Go to the documentation of this file.
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FPL_FLATUI_H
16 #define FPL_FLATUI_H
17 
18 #include <functional>
19 #include <string>
20 
21 #if defined(_MSC_VER)
22 #include <BaseTsd.h>
23 typedef SSIZE_T ssize_t;
24 #endif
25 
26 #include "flatui/font_util.h"
27 #include "flatui/version.h"
28 #include "font_manager.h"
29 #include "fplbase/asset_manager.h"
30 #include "fplbase/input.h"
31 #include "mathfu/constants.h"
32 
33 // Predeclarations.
34 namespace motive {
35 class MotiveEngine;
36 }
37 
38 namespace flatui {
39 
40 /// @enum AnimType
41 ///
42 /// @brief Anim type describes the algorithm used to animate a UI element.
43 ///
44 /// All algorithms maintain the current value and velocity, so any animation
45 /// can be smoothly interrupted by another animation, even if they are of
46 /// different types.
47 ///
48 /// **Enumerations**:
49 ///
50 /// * `kAnimEaseInEaseOut` (`0`) - Smoothly curve towards the target value
51 /// (i.e. ease-in) and smoothly stop at the
52 /// target value (i.e. ease-out).
53 /// The smoothness of the in and out is
54 /// determined by the bias in
55 /// AnimCurveDescription.
56 /// |
57 /// target + *********
58 /// | *******
59 /// | ****
60 /// | ****
61 /// | ***
62 /// | ***
63 /// | **
64 /// | ***
65 /// | **
66 /// | **
67 /// | **
68 /// | bias 0.15 **
69 /// | **
70 /// | *
71 /// | **
72 /// | **
73 /// | *
74 /// | **
75 /// | ***
76 /// start +***
77 ///
78 /// * `kAnimSpring` (`1`) - Oscillate about the target value, with each
79 /// peak having amplitude of the previous peak * bias.
80 /// So, if bias < 1, the amplitude dampens down and
81 /// eventually the curve reaches the target.
82 /// If bias > 1, the amplitude grows with each
83 /// oscillation. If bias = 1, then the amplitude
84 /// remains the same for every oscillation.
85 /// Note that true spring motion follows a sine curve,
86 /// but a sine curve does not move aggressively enough
87 /// for convincing motion, so internally we follow a
88 /// quadratic curve instead.
89 /*
90 /// |
91 /// start +--___
92 /// | --_
93 /// | \_ bias 0.5
94 /// | \
95 /// | \
96 /// | \ _--_
97 /// target +-------------+---------+----+__+-----> x
98 /// | \_ _/
99 /// | -___-
100 */
101 enum AnimType {
102  kAnimEaseInEaseOut,
103  kAnimSpring,
104  kAnimTypeCount,
105 };
106 
107 // Maximum dimension of mathfu::Vector.
108 static const int kMaxDimensions = 4;
109 // Multiplier to convert a second to MotiveTime.
110 static const int kSecondsToMotiveTime = 10000;
111 
112 /// @brief Describes a curve's typical shape.
115  : type(kAnimTypeCount),
117  typical_total_time(0.0f),
118  bias(0.0f) {}
120  float typical_total_time, float bias)
121  : type(type),
122  typical_delta_distance(typical_delta_distance),
123  typical_total_time(typical_total_time),
124  bias(bias) {}
125 
126  /// The overall shape of the motion curve.
127  /// This value determines the meaning of the variables below.
129 
130  /// The amount the animated value would change in a common or worst-case
131  /// situation. Used together with typical_total_time below to describe the
132  /// "typical" curve.
133  ///
134  /// The "typical" curve allows you to parameterize the motion in an intuitive
135  /// way. You provide the distance and time required to travel a common or
136  /// worst-case situation, and the curve's mathematical parameters are
137  /// calculated from that situation. This is much easier, for example, than
138  /// specifying the second derivatives explicitly.
140 
141  /// The time required for the value to travel typical_delta_distance, assuming
142  /// it started at a velocity of zero. See typical_delta_distance for further
143  /// details on the "typical" curve.
145 
146  /// When type is kAnimEaseInEaseOut:
147  /// Determines how much the curve should ease-in and how much it should
148  /// ease-out. Should be a value from 0.0 to 1.0.
149  /// Examples of potential bias values and what they would represent:
150  /// 0.0: ease-in but no ease out (a.k.a. "fly-out").
151  /// 0.3: ease-in more slowly and ease-out more quickly (i.e. less
152  /// responsive).
153  /// 0.5: symmetrical curve: equal ease-in and ease-out.
154  /// 0.7: ease-out more slowly and ease-in more quickly (i.e. more
155  /// reponsive).
156  /// 1.0: ease-out but no ease in (a.k.a. "fly-in").
157  ///
158  /// When type is kAnimSpring:
159  /// Determines how much the amplitude is dampened every oscillation.
160  /// Some examples,
161  /// 0.5: each peak is half the amplitude of the previous peak.
162  /// 1.0: each peak has the same amplitude. Oscillates forever.
163  /// 1.2: each peak is 20% larger than the previous peak. Grows forever.
164  float bias;
165 };
166 
167 /// @file
168 /// @addtogroup flatui_core
169 //
170 /// @{
171 
172 /// @brief The core function that drives the GUI.
173 ///
174 /// While FlatUI i sbeing initialized, it will implicitly load the shaders used
175 /// in the API below via AssetManager (`shaders/color.glslv`,
176 /// `shaders/color.glslf`, `shaders/font.glslv`, `shaders/font.glslf`,
177 /// `shaders/textured.glslv`, and `shaders/textured.glslf`).
178 ///
179 /// @param[in,out] assetman The AssetManager you want to use textures from.
180 /// @param[in] fontman The FontManager to be used by the GUI.
181 /// @param[in] input The InputSystem to be used by the GUI.
182 /// @param[in] motive_engine A pointer to the MotiveEngine to be used by the GUI
183 /// for animation purpose.
184 /// @param[in] gui_definition A function that defines all GUI elements using the
185 /// GUI element construction functions. (It will be run twice, once for the
186 /// layout, and once for rendering & events.)
187 void Run(fplbase::AssetManager &assetman, FontManager &fontman,
188  fplbase::InputSystem &input, motive::MotiveEngine *motive_engine,
189  const std::function<void()> &gui_definition);
190 
191 /// @brief A version of the function above that doesn't use a MotiveEngine.
192 /// With this version of Run(), user will not be able to use the animation
193 /// features of FlatUI.
194 /// If a user tries to use FlatUI's animation features with this version of
195 /// Run(), the program will terminate with an error.
196 void Run(fplbase::AssetManager &assetman, FontManager &fontman,
197  fplbase::InputSystem &input,
198  const std::function<void()> &gui_definition);
199 
200 /// @enum Event
201 ///
202 /// @brief Event types are returned by most interactive elements. These are
203 /// flags, because multiple events may occur during one frame, and thus should
204 /// be tested using a Bitwise operators (`&`, `|`, etc.).
205 ///
206 /// For example, it is not uncommon for the value to be
207 /// `kEventWentDown | kEventWentUp` (or `3`), if the click/touch was quicker
208 /// than the current frametime.
209 ///
210 /// You can then check if a specific event occured using the Bitwise AND (`&`)
211 /// operator. For instance, given an `Event my_event`, you could check if
212 /// the `kEventWentDown` Event happened in that frame with
213 /// `if((my_event & kEventWentDown) != 0)`.
214 ///
215 /// **Enumerations**:
216 ///
217 /// * `kEventNone` (`0`) - No Event occured in the frame. (This is also returned
218 /// by all elements during the layout pass.
219 /// * `kEventWentUp` (`1`) - Pointing device (or button) was released this
220 /// frame, while over this element. (This only triggers
221 /// if the element was also the one to receive the
222 /// corresponding `kEventWentDown`.
223 /// * `kEventWentDown` (`2`) - Pointing device is currently being held down on
224 /// top of this element. You're not guaranteed to
225 /// also receive a `kEventWentUp`, as the pointing
226 /// device may have moved to another element (or
227 /// no element) before the frame ends.
228 /// * `kEventIsDown` (`4`) - Pointing device is currently being held down on
229 /// top of this element. You're not guaranteed to
230 /// receive this event between `kEventWentDown` and
231 /// a `kEventWentUp`. That occurs only if the event
232 /// spans multiple frames. This only triggers for
233 /// the element that the corresponding `kEventWentDown`
234 /// fired on.
235 /// * `kEventStartDrag` (`8`) - Pointing device started dragging this frame
236 /// while over this element. The element is expected
237 /// to call `CapturePointer()` to receive the drag
238 /// event continuously, even if the pointer goes off
239 /// of the element.
240 /// * `kEventEndDrag` (`16`) - Pointing device finished dragging in this frame.
241 /// * `kEventIsDragging` (`32`) - Pointing device is currently in dragging mode.
242 /// * `kEventHover` (`64`) - Pointing device is currently over the element, but
243 /// not pressed. This event does NOT occur on touch
244 /// screen devices. It only occurs for devices that use
245 /// a mouse (or a gamepad that emulates a mouse when
246 /// selecting). As such, it is good to show a subtle
247 /// form of highlighting upon this event. However, the
248 /// UI should not rely on it to function.
249 ///
250 /// For example, a typical drag operation would receive the following events
251 /// in this sequence: `kEventWentDown` --> `kEventIsDown` (until the pointer
252 /// motion exceeds a threshold) --> `kEventStartDrag` --> `kEventIsDragging`
253 /// --> `kEventEndDrag`.
254 ///
255 enum Event {
256  kEventNone = 0,
257  kEventWentUp = 1,
258  kEventWentDown = 2,
259  kEventIsDown = 4,
260  kEventStartDrag = 8,
261  kEventEndDrag = 16,
262  kEventIsDragging = 32,
263  kEventHover = 64,
264 };
265 
266 // clang-format off
267 /// @enum Alignment
268 ///
269 /// @brief Alignment of the groups.
270 ///
271 /// @note: `kAlignTop` and `kAlignLeft`(as well as `kAlignBottom` and
272 /// `kAlignRight`) are intended to be aliases of one another, as they both
273 /// express the same thing on their respective axis.
274 ///
275 /// **Enumerations**:
276 ///
277 /// * `kAlignTop` or `kAlignLeft` (`1`) - Align along the top (or left,
278 /// depending on the axis).
279 /// * `kAlignCenter` (`2`) - Align along the center of the axis.
280 /// * `kAlignBottom` or `kAlignRight` (`3`) - Align along the bottom (or right,
281 /// depending on the axis).
282 enum Alignment {
283  kAlignTop = 1,
284  kAlignLeft = 1,
285  kAlignCenter = 2,
286  kAlignBottom = 3,
287  kAlignRight = 3
288 };
289 
290 /// @enum Direction
291 ///
292 /// @brief Direction of the groups.
293 ///
294 /// **Enumerations**:
295 ///
296 /// * `kDirHorizontal` (`4`) - The direction of the group is horizontal
297 /// (x-axis).
298 /// * `kDirVertical` (`8`) - The direction of the group is vertical (y-axis).
299 /// * `kDirOverlay` (`12`) - The group of elements are placed on top of one
300 /// another (along the z-axis).
301 enum Direction {
302  kDirHorizontal = 4,
303  kDirVertical = 8,
304  kDirOverlay = 12
305 };
306 
307 /// @enum EditStatus
308 ///
309 /// @brief Status of Edit widget.
310 ///
311 /// **Enumerations**:
312 ///
313 /// * `kEditStatusNone` - The widget is not editing.
314 /// * `kEditStatusInEdit` - The widget is in edit.
315 /// * `kEditStatusUpdated` - The widget is in edit and contents has been
316 /// updated in the current update cycle.
317 /// * `kEditStatusFinished` - The widget finished editing with an updated
318 /// contents.
319 /// * `kEditStatusCanceled` - The edit is canceled by the user pressing esc.
321  kEditStatusNone = 0,
322  kEditStatusInEdit = 1,
323  kEditStatusUpdated = 2,
324  kEditStatusFinished = 3,
325  kEditStatusCanceled = 4,
326 };
327 
328 /// @enum Layout
329 ///
330 /// @brief Specify how to layout a group.
331 ///
332 /// Elements can be positioned either horizontally or vertically. The elements
333 /// can be aligned on either side, or centered.
334 ///
335 /// For example, `kLayoutHorizontalTop` indicates that the elements are layed
336 /// out from left-to-right, with items of uneven height being aligned from the
337 /// top.
338 ///
339 /// In this example, we have three elements: `A` with a height of 3, `B` with
340 /// a height of 1, and `C` with a height of 2. So we lay the elements out from
341 /// left to right in the order `A`->`B`->`C`, and we align them along the top.
342 ///
343 /// That layout would look like this:
344 ///
345 /// A @htmlonly &nbsp @endhtmlonly B @htmlonly &nbsp @endhtmlonly C
346 ///
347 /// A @htmlonly &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp @endhtmlonly C
348 ///
349 /// A
350 ///
351 /// **Enumerations**:
352 ///
353 /// * `kLayoutHorizontalTop` (`5`) - Lay out the elements horizontally, aligning
354 /// elements of uneven height along the top.
355 /// * `kLayoutHorizontalCenter` (`6`) - Lay out the elements horizontally,
356 /// aligning elements of uneven height along
357 /// the center.
358 /// * `kLayoutHotizontalBottom` (`7`) - Lay out the elements horizontally,
359 /// aligning elements of uneven height along
360 /// the bottom.
361 /// * `kLayoutVerticalLeft` (`9`) - Lay out the elements vertically, aligning
362 /// elements of uneven width along the left.
363 /// * `kLayoutVerticalCenter` (`10`) - Lay out the elements vertically, aligning
364 /// elements of uneven width along the center.
365 /// * `kLayoutVerticalRight` (`11`) - Lay out the elements vertically, aligning
366 /// the elements of uneven width along the
367 /// right.
368 /// * `kLayoutOverlay` (`14`) - Lay out the elements on top of one another, from
369 /// the center.
370 enum Layout {
371  kLayoutHorizontalTop = kDirHorizontal| kAlignTop,
372  kLayoutHorizontalCenter = kDirHorizontal| kAlignCenter,
373  kLayoutHorizontalBottom = kDirHorizontal| kAlignBottom,
374  kLayoutVerticalLeft = kDirVertical | kAlignLeft,
375  kLayoutVerticalCenter = kDirVertical | kAlignCenter,
376  kLayoutVerticalRight = kDirVertical | kAlignRight,
377  kLayoutOverlay = kDirOverlay | kAlignCenter,
378 };
379 // clang-format on
380 
381 /// @var kDefaultGroupID
382 ///
383 /// @brief A sentinel value for group IDs.
384 const char *const kDefaultGroupID = "__group_id__";
385 
386 /// @var kDefaultImageID
387 ///
388 /// @brief A sentinel value for image IDs.
389 const char *const kDefaultImageID = "__image_id__";
390 
391 /// @struct Margin
392 ///
393 /// @brief Specifies the margins for a group, in units of virtual
394 /// resolution.
395 struct Margin {
396  /// @brief Create a Margin with all four sides of equal size.
397  ///
398  /// @param[in] m A float size to be used as the margin on all sides.
399  Margin(float m) : borders(mathfu::vec4(m)) {}
400 
401  /// @brief Create a Margin with the left and right sizes of `x`, and top
402  /// and bottom sizes of `y`.
403  ///
404  /// @param[in] x A float size to be used as the margin for the left and
405  /// right sides.
406  /// @param[in] y A float size to be used as the margin for the right and
407  /// left sides.
408  Margin(float x, float y) : borders(mathfu::vec4(x, y, x, y)) {}
409 
410  // Create a margin specifying all 4 sides individually.
411  /// @brief Creates a margin specifying all four sides individually.
412  ///
413  /// @param[in] left A float size to be used as the margin for the left side.
414  /// @param[in] top A float size to be used as the margin for the top side.
415  /// @param[in] right A float size to be used as the margin for the right side.
416  /// @param[in] bottom A float size to be used as the margin for the bottom
417  /// side.
418  Margin(float left, float top, float right, float bottom)
419  : borders(mathfu::vec4(left, top, right, bottom)) {}
420 
421  /// @var borders
422  ///
423  /// @brief A vector of four floats containing the values for the
424  /// four sides of the margin.
425  ///
426  /// The internal layout of the margin is: `left`, `top`, `right`, `bottom`.
427  mathfu::vec4_packed borders;
428 };
429 
430 /// @brief Converts a virtual screen coordinate to a physical value.
431 ///
432 /// @param[in] v A mathfu::vec2 vector representing a virtual screen coordinate.
433 ///
434 /// @return Returns a mathfu::vec2i containing the physical value.
435 mathfu::vec2i VirtualToPhysical(const mathfu::vec2 &v);
436 
437 /// @brief Converts a physical screen coordinate to a virtual value.
438 ///
439 /// @param[in] v A mathfu::vec2i vector representing a phsyical screen
440 /// coordinate.
441 ///
442 /// @return Returns a mathfu::vec2 containing the virtual value.
443 mathfu::vec2 PhysicalToVirtual(const mathfu::vec2i &v);
444 
445 /// @brief Get the scaling factor for the virtual resolution.
446 ///
447 /// @return Returns a float representing the scaling factor.
448 float GetScale();
449 
450 /// @brief Render an image as a GUI element.
451 ///
452 /// @param[in] texture A Texture corresponding to the image that should be
453 /// rendered.
454 /// @param[in] ysize A float containing the vertical size in virtual resolution.
455 /// @param[in] id An id to uniquely identify the image.
456 ///
457 /// @note The x-size will be derived automatically based on the image
458 /// dimensions.
459 void Image(const fplbase::Texture &texture, float ysize,
460  const char *id = kDefaultImageID);
461 
462 /// @brief Set the Images's tint.
463 ///
464 /// @param[in] color The RGBA values that get multiplied into the image RGBAs.
465 /// a value of mathfu::kOnes4f draws the image without change.
466 ///
467 /// @note By animating the alpha component, images can be faded out. Or, you can
468 /// apply a particular hue to an image using the RGB components.
469 void SetImageColor(const mathfu::vec4 &color);
470 
471 /// @brief Render a label as a GUI element.
472 ///
473 /// @param[in] text A C-string in UTF-8 format to be displayed as the label.
474 /// @param[in] ysize A float containing the vertical size in virtual resolution.
475 /// @param[in] label_id A HashedId that defaults to null. If it is null, the
476 /// text will be hashed and used as label_id.
477 ///
478 /// @note The x-size will be derived automatically based on the text length.
479 void Label(const char *text, float ysize, HashedId label_id = kNullHash);
480 
481 /// @brief Render a multi-line version of a label as a GUI element.
482 ///
483 /// @param[in] text A C-string in UTF-8 format to be displayed as the label.
484 /// @param[in] ysize A float containing the vertical size in virtual resolution.
485 /// @param[in] size The max size of the label in virtual resolution. A `0` for
486 /// `size.y` indicates no height restriction. The API renders the whole text in
487 /// the label in this case.
488 /// @param[in] label_id A HashedId that defaults to null. If it is null, the
489 /// text will be hashed and used as label_id.
490 void Label(const char *text, float ysize, const mathfu::vec2 &size,
491  HashedId label_id = kNullHash);
492 
493 /// @brief Render a multi-line label with a text alignment.
494 ///
495 /// @param[in] text A C-string in UTF-8 format to be displayed as the label.
496 /// @param[in] ysize A float containing the vertical size in virtual resolution.
497 /// @param[in] alignment A text alignment in the label.
498 /// @param[in] size The max size of the label in virtual resolution. A `0` for
499 /// `size.y` indicates no height restriction. The API renders the whole text in
500 /// the label in this case.
501 /// @param[in] label_id A HashedId that defaults to null. If it is null, the
502 /// text will be hashed and used as label_id.
503 void Label(const char *text, float ysize, const mathfu::vec2 &label_size,
504  TextAlignment alignment, HashedId label_id = kNullHash);
505 
506 /// @brief Render simple HTML text.
507 ///
508 /// @param[in] html A C-string in UTF-8 format to be parsed as HTML and then
509 /// displayed. Note that we support only a simple subset of HTML at the moment,
510 /// including anchor tags, paragraphs, headers, and breaks.
511 /// @param[in] ysize A float containing the vertical size in virtual resolution.
512 /// @param[in] label_size The max size of the label in virtual resolution.
513 /// A `0` for `size.y` indicates no height restriction. The API renders the
514 /// whole text in the label in this case.
515 /// @param[in] alignment A text alignment in the label.
516 /// @param[in] id An id of the label.
517 void HtmlLabel(const char *html, float ysize, const mathfu::vec2 &label_size,
518  TextAlignment alignment, const char *id);
519 
520 /// @brief Set the Label's text color.
521 ///
522 /// @param[in] color A vec4 representing the RGBA values that the text color
523 /// should be set to.
524 void SetTextColor(const mathfu::vec4 &color);
525 
526 /// @brief Set the Label's outer color (e.g. drop shadow color).
527 /// To use the feature, outer SDF generation needs to be enabled by
528 /// EnableTextSDF() API. With SDF, each glyph image includes a distance to the
529 /// nearest edge. The API utilizes the feature to render an outer region of a
530 /// glyph.
531 /// For more details of SDF, refer a paper from Valve:
532 /// http://www.valvesoftware.com/publications/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf
533 ///
534 /// @param[in] color A vec4 representing the RGBA values that the outer color
535 /// should be set to.
536 /// @param[in] size A float value that changes the size of outer color region in
537 /// pixels.
538 /// Typical value range is around 64.0f/255.0f but varies by font
539 /// face. As the value get bigger, the shadow region gets spread out.
540 /// @param[in] offset A vec2 value that controls a position of the outer color
541 /// region in pixels.
542 void SetTextOuterColor(const mathfu::vec4 &color, float size,
543  const mathfu::vec2 &offset);
544 
545 /// @brief Enable/Disable a signed distance field generation with glyphs.
546 /// A SDF generation of an inner region and an outer region is done separately
547 /// and it costs some cycles. So if an application doesn't utilize inner SDF,
548 /// just enable outer SDF.
549 ///
550 /// @param[in] inner_sdf set true to enable a distance field generation for an
551 /// inner region of a glyph. false to disable it.
552 /// @param[in] outer_sdf set true to enable a distance field generation for an
553 /// outer region of a glyph. false to disable it.
554 /// @param[in] threshold Threshold value used in the SDF glyph rendering.
555 /// The value controls a threshold if a pixel nearby a glyph edge is considered
556 /// inside a glyph or not.
557 /// Typical value range is around 8.0f/255.0f ~ 24.0f/255f and it varies by font
558 /// face. As the speficied value get bigger, rendered glyph images become bold.
559 /// Default value is 16.0f/255.0f.
560 void EnableTextSDF(bool inner_sdf, bool outer_sdf, float threshold);
561 
562 /// @brief Enable/Disable the hyphenation in the text label.
563 /// @param[in] enable A flag indicate hyphenation state.
564 void EnableTextHyphenation(bool enable);
565 
566 /// @brief Set the Label's font.
567 ///
568 /// @param[in] font_name A C-string corresponding to the name of the font
569 /// that should be set.
570 ///
571 /// @return Returns `true` if the font file is succeffully opened.
572 bool SetTextFont(const char *font_name);
573 
574 /// @brief Set the Label's fonts with a fallback priority.
575 /// When rendering a text, if a glyph is not found in the first font in the
576 /// array,
577 /// the renderer will look up the glyph in the second font and so on.
578 /// If the glyph is not found in all font files, the glyph won't be rendered on
579 /// the screen.
580 ///
581 /// @param[in] font_names An array of C-string corresponding to the name of the
582 /// font. Font names in the array are stored in a priority order.
583 /// @param[in] count A count of font names in the array.
584 ///
585 /// @return Returns `true` if the font files are succeffully opened.
586 bool SetTextFont(const char *font_names[], int32_t count);
587 
588 /// @brief Set a locale used for the text rendering.
589 ///
590 /// @param[in] locale A C-string corresponding to the of the
591 /// language defined in ISO 639 and country code defined in ISO 3166 connected
592 /// by '-'. (e.g. 'en-US').
593 /// The API sets language, script and layout direction used for following text
594 /// renderings.
595 void SetTextLocale(const char *locale);
596 
597 /// @brief Override a text layout direction set by SetTextLocale() API.
598 ///
599 /// @param[in] direction TextLayoutDirection specifying text layout direction.
600 void SetTextDirection(const TextLayoutDirection direction);
601 
602 /// @brief Set a line height scale used in the text rendering.
603 ///
604 /// @param[in] scale A line height value. The value is multiplied to the font
605 /// height and determines a space between lines.
606 /// The default value is kLineHeightDefault(1.2f).
607 void SetTextLineHeightScale(float scale);
608 
609 /// @brief Set a kerning scale used in the text rendering.
610 ///
611 /// @param[in] scale A kerning scale value applied kerning values.
612 /// The default value is kKerningScale(1.0f).
613 void SetTextKerningScale(float scale);
614 
615 /// @brief Set an ellipsis string used in label/edit widgets.
616 ///
617 /// @param[in] ellipsis A C-string specifying characters used as an ellipsis.
618 /// Can be multiple characters, typically '...'. When a string in a widget
619 /// doesn't fit to the given size, the string is truncated to fit the ellipsis
620 /// string appended at the end.
621 void SetTextEllipsis(const char *ellipsis);
622 
623 /// @brief Renders an edit text box as a GUI element.
624 ///
625 /// @param[in] ysize A float containing the vertical size in virtual resolution.
626 /// @param[in] size A mathfu::vec2 reference to the size of the edit box in
627 /// virtual resolution. A `0` for `size.x` indicates an auto expanding text box.
628 /// A `0` for `size.y` indicates a single line label.
629 /// @param[in] id A C-string in UTF-8 format to uniquely idenitfy this edit box.
630 /// @param[in/out] status A pointer to a EditStatus that indicates the status of
631 /// Edit widget. Can be nullptr if the caller doesn't require the information.
632 /// @param[in/out] string A pointer to a C-string in UTF-8 format that should
633 /// be used as the Label for the edit box.
634 ///
635 /// @return Returns the Event type for the Edit widget.
636 Event Edit(float ysize, const mathfu::vec2 &size, const char *id,
637  EditStatus *status, std::string *string);
638 
639 /// @brief Render an edit text box with a text alignment.
640 ///
641 /// @param[in] ysize A float containing the vertical size in virtual resolution.
642 /// @param[in] size A mathfu::vec2 reference to the size of the edit box in
643 /// virtual resolution. A `0` for `size.x` indicates an auto expanding text box.
644 /// A `0` for `size.y` indicates a single line label.
645 /// @param[in] alignment An alignment of the text in the edit box.
646 /// @param[in] id A C-string in UTF-8 format to uniquely idenitfy this edit box.
647 /// @param[in/out] status A pointer to a EditStatus that indicates the status of
648 /// Edit widget. Can be nullptr if the caller doesn't require the information.
649 /// @param[in/out] string A pointer to a C-string in UTF-8 format that should
650 /// be used as the Label for the edit box.
651 ///
652 /// @return Returns the Event type for the Edit widget.
653 Event Edit(float ysize, const mathfu::vec2 &size, TextAlignment alignment,
654  const char *id, EditStatus *status, std::string *string);
655 
656 /// @brief Create a group of elements with a given layout and intra-element
657 /// spacing.
658 ///
659 /// @note `StartGroup()` and `EndGroup()` calls must be matched. They may,
660 /// however, be nested to create more complex layouts.
661 ///
662 /// @param[in] layout The Layout to be used by the group.
663 /// @param[in] spacing A float corresponding to the intra-element spacing for
664 /// the group.
665 /// @param[in] id A C-string in UTF-8 format to uniquely identify this group.
666 void StartGroup(Layout layout, float spacing = 0,
667  const char *id = kDefaultGroupID);
668 
669 /// @brief Clean up the Group element start by `StartGroup()`.
670 ///
671 /// @note `StartGroup()` and `EndGroup()` calls must be matched. They may,
672 /// however, be nested to create more complex layouts.
673 void EndGroup();
674 
675 /// @brief Sets the margin for the current group.
676 ///
677 /// @note This function is specific to a group, and should be called after
678 /// `StartGroup()` and before any elements.
679 ///
680 /// @param[in] margin The Margin to set for the group.
681 void SetMargin(const Margin &margin);
682 
683 /// @brief Check for events from the current group.
684 ///
685 /// Calling `CheckEvent()` marks the current element as an "interactive"
686 /// element. Each interactive elements needs to have a unique ID in order to
687 /// properly to receive a keyboard/gamepad focus.
688 ///
689 /// IDs for Labels and Images are derived from hashing its contents. The user
690 /// can also specify IDs for CustomElement and Edit elements as an argument.
691 ///
692 /// If multiple interactive elements have the same ID, a keyboard/gamepad focus
693 /// navigation will not work as expected (e.g. a focus jump to other elements
694 /// that share the same ID while naviating with a gamepad).
695 ///
696 /// @note This function is specific to a group, and should be called after
697 /// `StartGroup()` and before any elements.
698 ///
699 /// @return Returns the Event type for the group.
700 Event CheckEvent();
701 
702 /// @brief Check for events from the current group.
703 ///
704 /// @param[in] check_dragevent_only A bool to check if only a drag event
705 /// occurred (ignore button events). If an element is not interested in
706 /// button events, then this flag should be set by the caller because elements
707 /// can only receive the WENT_UP event if it is the same element that received
708 /// the corresponding WENT_DOWN event.
709 ///
710 /// @note This function is specific to a group, and should be called after
711 /// `StartGroup()` and before any elements.
712 ///
713 /// @return Returns the Event type for the group.
714 Event CheckEvent(bool check_dragevent_only);
715 
716 /// @brief Set the default keyboard/gamepad focus to the current element.
717 ///
718 void SetDefaultFocus();
719 
720 // Call inside of a group that is meant to be like a popup inside of a
721 // kLayoutOverlay. It will cause all interactive elements in all groups that
722 // precede it to not respond to input.
723 /// @brief Called inside of a group that that is mean to act like a popup inside
724 /// of a `kLayoutOverlay`.
725 ///
726 /// It will cause all interactive elements in the groups that precede it to not
727 /// respond to input.
728 ///
729 /// @note This function is specific to a group, and should be called after
730 /// `StartGroup()` and before any elements.
731 void ModalGroup();
732 
733 /// @brief Caputre a pointer event.
734 ///
735 /// After an API call, the element with `element_id` will exclusively receive
736 /// pointer events until `ReleasePointer()` is called. This API is used mainly
737 /// for a drag operation, when an element wants to receive events continuously.
738 ///
739 /// @note This function is specific to a group, and should be called after
740 /// `StartGroup()` and before any elements.
741 ///
742 /// @param[in] element_id A C-string in UTF-8 format that contains the ID of the
743 /// element that should capture all pointer events.
744 void CapturePointer(const char *element_id);
745 
746 /// @brief Release a pointer capture.
747 ///
748 /// @note This function is specific to a group, and should be called after
749 /// `StartGroup()` and before any elements.
750 void ReleasePointer();
751 
752 /// @brief Get the index of th ecaptured pointer.
753 ///
754 /// This should be used in conjunction with `CheckEvent()` to determine whether
755 /// a drag operation is in progress.
756 ///
757 /// @note This function is specific to a group, and should be called after
758 /// `StartGroup()` and before any elements.
759 ///
760 /// @return Returns the index of the pointer as a `ssize_t`. Otherwise it
761 /// returns `-1` if no pointer was captured.
762 ssize_t GetCapturedPointerIndex();
763 
764 /// @brief Set the scroll speed of the drag, mouse wheel, and gamepad
765 /// operations.
766 ///
767 /// The defaults are `kScrollSpeedDragDefault`, `kScrollSpeedWheelDefault`,
768 /// and `kScrollSpeedGamepadDefault`.
769 ///
770 /// @param[in] scroll_speed_drag A float determining the new scroll speed
771 /// for dragging.
772 /// @param[in] scroll_speed_wheel A float determining the new scroll speed
773 /// for the mouse scroll wheel.
774 /// @param[in] scroll_speed_gamepad A float determining the new scroll speed
775 /// for a gamepad.
776 ///
777 /// @note This function is specific to a group, and should be called after
778 /// `StartGroup()` and before any elements.
779 void SetScrollSpeed(float scroll_speed_drag, float scroll_speed_wheel,
780  float scroll_speed_gamepad);
781 
782 /// @brief Set a threshold value for the start of a drag operation.
783 ///
784 /// The default value is `kDragStartThresholdDefault`.
785 ///
786 /// @param[in] drag_start_threshold An int determining the new threshold
787 /// value for the start of a drag operation.
788 ///
789 /// @note This function is specific to a group, and should be called after
790 /// `StartGroup()` and before any elements.
791 void SetDragStartThreshold(int drag_start_threshold);
792 
793 /// @brief Set the background color for the group.
794 ///
795 /// @param[in] color A vec4 representing the background color that should be
796 /// set in RGBA.
797 ///
798 /// @note This function is specific to a group, and should be called after
799 /// `StartGroup()` and before any elements.
800 void ColorBackground(const mathfu::vec4 &color);
801 
802 /// @brief Set the background texture for the group.
803 ///
804 /// @note This function is specific to a group, and should be called after
805 /// `StartGroup()` and before any elements.
806 ///
807 /// @param[in] tex The Texture to be set as the background for the group.
808 void ImageBackground(const fplbase::Texture &tex);
809 
810 /// @brief Set the background texture for the group with nine patch settings.
811 ///
812 /// In the `patch_info`, the user can define nine patch settings as
813 /// `vec4(x0, y0, x1, y1)`, where `(x0, y0)` corresponds to the top-left
814 /// corner of the stretchable area in UV coordinates and `(x1, y1)` corresponds
815 /// to the bottom-right corner of stretchable area in UV coordinates.
816 ///
817 /// The coordinates are in UV value in the texture (0.0 ~ 1.0).
818 ///
819 /// For more information for nine patch, refer
820 /// http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
821 ///
822 /// @note This function is specific to a group, and should be called after
823 /// `StartGroup()` and before any elements.
824 ///
825 /// @param[in] tex The Texture of the background image that should be rendered.
826 /// @param[in] patch_info The nine-patch settings for the corners of the
827 /// stretchable area in UV coordinates.
828 void ImageBackgroundNinePatch(const fplbase::Texture &tex,
829  const mathfu::vec4 &patch_info);
830 
831 /// @brief Make the current group into a scrolling group that can display
832 /// arbitrary sized elements inside a window of "size", scrolled to the current
833 /// "offset" (which the caller should store somewhere that survives the current
834 /// frame).
835 ///
836 /// @param[in] size A vec2 corresponding to the size of the window that the
837 /// elements should be displayed in.
838 /// @param[out] offset A vec2 that captures the value of the current scroll
839 /// location.
840 ///
841 /// @note Call `StartScroll()` right after `StartGroup()`.
842 void StartScroll(const mathfu::vec2 &size, mathfu::vec2 *offset);
843 
844 /// @brief Ends the current scrolling group.
845 ///
846 /// @note Call `EndScroll()` right before `EndGroup()`.
847 void EndScroll();
848 
849 /// @brief Make the current group into a slider group that can handle basic
850 /// slider behavior. The group will capture/release the pointer as necessary.
851 ///
852 /// @param[in] direction Sets the horizontal or vertical scroll direction.
853 /// @param[in] scroll_margin Sets the margin around the scroll bar.
854 /// @param[out] value Captures the float output of the slider value.
855 ///
856 /// @note Call `StartSlider()` right after `StartGroup()`.
857 void StartSlider(Direction direction, float scroll_margin, float *value);
858 
859 /// @brief Ends the current slider group.
860 ///
861 /// @note Call `EndSlider()` right before `EndGroup()`.
862 void EndSlider();
863 
864 /// @brief Create a custom element with a given size.
865 ///
866 /// @param[in] virtual_size The size of the element in virtual screen
867 /// coordinates.
868 /// @param[in] id A C-string in UTF-8 format corresponding to the unique
869 /// ID for the CustomElement.
870 /// @param[in] renderer The function that is invoked during the render pass
871 /// to render the element.
872 void CustomElement(const mathfu::vec2 &virtual_size, const char *id,
873  const std::function<void(const mathfu::vec2i &pos,
874  const mathfu::vec2i &size)>
875  renderer);
876 
877 /// @brief Render a Texture to a specific position with a given size.
878 ///
879 /// @note This is usually called in `CustomElement()`'s callback function.
880 ///
881 /// @param[in] tex The Texture to render.
882 /// @param[in] pos The position that `tex` should be rendered at, in physical
883 /// screen coordinates.
884 /// @param[in] size The size that `tex` should be rendered at, in physical
885 /// screen coordinates.
886 void RenderTexture(const fplbase::Texture &tex, const mathfu::vec2i &pos,
887  const mathfu::vec2i &size);
888 
889 /// @brief Render a Texture to a specific position with a given size and color.
890 ///
891 /// @note This is usually called in `CustomElement()`'s callback function.
892 ///
893 /// @param[in] tex The Texture to render.
894 /// @param[in] pos The position that `tex` should be rendered at, in physical
895 /// screen coordinates.
896 /// @param[in] size The size that `tex` should be rendered at, in physical
897 /// screen coordinates.
898 /// @param[in] color A vec4 representing the color of the texture that should
899 /// be rendered in RGBA.
900 void RenderTexture(const fplbase::Texture &tex, const mathfu::vec2i &pos,
901  const mathfu::vec2i &size, const mathfu::vec4 &color);
902 
903 /// @brief Render a nine-patch texture at a specific position with a given
904 /// size.
905 ///
906 /// @note This is usually used in `CustomElement()`'s callback function.
907 ///
908 /// In the `patch_info`, the user can define nine patch settings as
909 /// `vec4(x0, y0, x1, y1)`, where `(x0, y0)` corresponds to the top-left
910 /// corner of the stretchable area in UV coordinates and `(x1, y1)` corresponds
911 /// to the bottom-right corner of stretchable area in UV coordinates.
912 ///
913 /// The coordinates are in UV value in the texture (0.0 ~ 1.0).
914 ///
915 /// For more information for nine patch, refer
916 /// http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
917 ///
918 /// @param[in] tex The Texture to render.
919 /// @param[in] patch_info The nine-patch settings for the corners of the
920 /// stretchable area in UV coordinates.
921 /// @param[in] pos The position that `tex` should be rendered at, in physical
922 /// screen coordinates.
923 /// @param[in] size The size that `tex` should be rendered at, in physical
924 /// screen coordinates.
925 void RenderTextureNinePatch(const fplbase::Texture &tex,
926  const mathfu::vec4 &patch_info,
927  const mathfu::vec2i &pos,
928  const mathfu::vec2i &size);
929 
930 /// @var FLATUI_DEFAULT_VIRTUAL_RESOLUTION
931 ///
932 /// @brief The default virtual resolution, if none is set.
933 const float FLATUI_DEFAULT_VIRTUAL_RESOLUTION = 1000.0f;
934 
935 /// @brief Set the virtual resolution of the smallest dimension of the screen
936 /// (the Y size in landscape mode, or X in portrait).
937 ///
938 /// All dimensions specified elsewhere (in floats) are relative to this value.
939 ///
940 /// The default value, if this function is not called, is
941 /// `FLATUI_DEFAULT_VIRTUAL_RESOLUTION`.
942 ///
943 /// If you wish to use native pixels, call this with `min(screen_x, screen_y)`.
944 ///
945 /// @note This should be called as the first thing in your GUI definition.
946 ///
947 /// @param[in] virtual_resolution A float representing the virtual resolution
948 /// of the smallest dimension of the screen.
949 void SetVirtualResolution(float virtual_resolution);
950 
951 /// @return Returns the virtual resolution of the screen.
952 mathfu::vec2 GetVirtualResolution();
953 
954 /// @brief Position a group within the screen as a whole.
955 ///
956 /// @note This should be called as the first thing in any top level groups
957 /// (either your root group, or the children of your root, if the root is
958 /// `kLayoutOverlay`.
959 ///
960 /// @param[in] horizontal The alignment for the x-axis of the group (defaults
961 /// to `left`).
962 /// @param[in] vertical The alignment for the y-axis of the group (defaults to
963 /// `top`).
964 /// @param[in] offset A vec2 that allows you to displace elements from the
965 /// given alignment.
966 void PositionGroup(Alignment horizontal, Alignment vertical,
967  const mathfu::vec2 &offset);
968 
969 /// @brief By default, FlatUI sets up a projection matrix for all the rendering
970 /// that covers the entire screen (as given by `Renderer::window_size()`. You
971 /// can call to this function instead, to use whatever projection is in place
972 /// before `Run()` is called (which may be a 2D or 3D projection).
973 ///
974 /// @param[in] canvas_size Specifies the new canvas size for the UI to live
975 /// inside of.
976 void UseExistingProjection(const mathfu::vec2i &canvas_size);
977 
978 /// @brief If you're rendering the UI at a location that does not correspond
979 /// to the display's pixels (e.g. in 3D), this call allows you to set your
980 /// a custom transform that corresponds to the inverse of your model-view-
981 /// projection matrix. FlatUI will then transform all incoming (screen-space)
982 /// pointer events with this, such that they are mapped to coordinates that
983 /// match what was passed to UseExistingProjection.
984 /// Important that the UI was rendered with object space coordinates ranging
985 /// from (0,0) to canvas_size as well.
986 /// Call this at the start of your UI.
987 /// For an example of how to use this, see flatuisample_3d.cpp
988 ///
989 /// @param[in] imvp The inverse model-view-projection matrix.
990 void ApplyCustomTransform(const mathfu::mat4 &imvp);
991 
992 /// @return Returns the position of the current group in virtual coordinates.
993 ///
994 /// This is the top/left location of the group. When used in conjunction with
995 /// `GroupSize()`, this can be used t ocalculate the extents of the group.
996 mathfu::vec2 GroupPosition();
997 
998 /// @return Returns the current group's size in virtual coordinates. This
999 /// function is useful to implement UI that requires other element's sizes, such
1000 /// as a scroll bar.
1001 mathfu::vec2 GroupSize();
1002 
1003 /// @return Returns `true` if the last click event was a touch screen or mouse
1004 /// event. Otherwise, it returns `false` (e.g. from a gamepad or keyboard).
1005 bool IsLastEventPointerType();
1006 /// @}
1007 
1008 /// @brief Set a global listener callback that receives all events to all
1009 /// interactive elements (useful for logging/debugging/analytics etc, NOT
1010 /// intended for normal event handling).
1011 /// Does not affect events in the rest of the API.
1012 /// Gets called for all events except None, you must do your own filtering.
1013 /// Call this function as the first thing inside of Run().
1014 /// Callback never fires outside of Run().
1015 /// Use HashId() to compare against ids of elements you may be interested in.
1016 void SetGlobalListener(
1017  const std::function<void(HashedId id, Event event)> &callback);
1018 
1019 // Returns the version of the FlatUI Library.
1020 const FlatUIVersion *GetFlatUIVersion();
1021 
1022 /// @brief Enables depth testing, when needed for rendering a UI in 3D.
1023 ///
1024 /// For example, a simple `FlatUI::Label()` could be rendered, with appropriate
1025 /// depth, on entities in the world to display the game's score as an overlay.
1026 ///
1027 /// @warning This approach only works for 'simple' UIs, because more complex UIs
1028 /// require overlapping UI elements. Depending on the precision of the z-buffer,
1029 /// elements will be susceptible to z-fighting. That is, when the rectangles
1030 /// around UI elements overlap, flickering will occur.
1031 void SetDepthTest(bool enable);
1032 
1033 namespace details {
1034 
1035 /// @class FloatConverter
1036 ///
1037 /// @brief converts from a mathfu::vector to a const float pointer and vice
1038 /// versa.
1039 template <typename T>
1041  public:
1042  static const float *ToFloatArray(const T &data);
1043  static T FromFloatArray(const float *floats);
1044  static int Dimension();
1045 };
1046 
1047 template <>
1048 class FloatConverter<float> {
1049  public:
1050  static const float *ToFloatArray(const float &data) { return &data; }
1051  static float FromFloatArray(const float *floats) { return *floats; }
1052  static int Dimension() { return 1; }
1053 };
1054 
1055 template <int d>
1056 class FloatConverter<mathfu::Vector<float, d>> {
1057  public:
1058  typedef mathfu::Vector<float, d> Vec;
1059  static const float *ToFloatArray(const Vec &data) { return &data[0]; }
1060  static Vec FromFloatArray(const float *floats) { return Vec(floats); }
1061  static int Dimension() { return d; }
1062 };
1063 
1064 // Called by T Animatable() with its templated
1065 // variables represented by float pointers instead. The User will call
1066 // the templated version of Animatable().
1067 const float *Animatable(HashedId id, const float *starting_values,
1068  int dimensions);
1069 
1070 // Called by Animation() with its templated variable
1071 // represented by a float pointer instead. The user will call the templated
1072 // version of StartAnimation().
1073 void StartAnimation(HashedId id, const float *target_values,
1074  const float *target_velocities, int dimensions,
1075  const AnimCurveDescription &description);
1076 
1077 } // namespace details
1078 
1079 /// @brief Returns the time remaining for an animation.
1080 ///
1081 /// @param[in] A HashedId that uniquely identifies an animation.
1082 /// @return Returns a double representing the time remaining.
1083 double AnimationTimeRemaining(HashedId id);
1084 
1085 /// @brief Returns the time remaining for an animation.
1086 ///
1087 /// @param[in] id A C-string in UTF-8 format that uniquely identifies
1088 /// an animation.
1089 ///
1090 /// @return Returns a double representing the time remaining.
1091 inline double AnimationTimeRemaining(const char *id) {
1092  return AnimationTimeRemaining(HashId(id));
1093 }
1094 
1095 /// @brief This function returns the current number of sprites animating on
1096 /// screen.
1097 ///
1098 /// @param[in] id A HashedId that identifies an animation type.
1099 ///
1100 /// @returns Returns the size_t associated with the size of the sprites vector.
1101 int NumActiveSprites(HashedId id);
1102 
1103 /// @brief This function returns the current number of sprites animating on
1104 /// screen.
1105 ///
1106 /// @param[in] id A C-string in UTF-8 format that identifies an animation type.
1107 ///
1108 /// @returns Returns the size_t associated with the size of the sprites vector.
1109 inline int NumActiveSprites(const char *id) {
1110  return NumActiveSprites(HashId(id));
1111 }
1112 
1113 /// @brief This function adds a sprite, which will be drawn and then forgotten
1114 /// after it is finished firing.
1115 ///
1116 /// A sprite would be a UI element that gets drawn onto the screen with a
1117 /// limited lifespan. For this usage, the sprite could be, for example,
1118 /// an image or text.
1119 /// This makes it useful for situations where the user would want a temporary
1120 /// animation. For example, in a game, a sprite could be points that appear
1121 /// and drift off screen when a user earns points for a turn.
1122 /// The returned SequenceId is so that the application can recalculate the hash
1123 /// assigned to a specific sprite using its id and sequence number.
1124 /// An example of how to pass in a lambda for the draw parameter can be found in
1125 /// the FlatUI animation demo.
1126 /// @param[in] group_id A C-string in UTF-8 format that uniquely identifies an
1127 /// animation type.
1128 /// @param[in] draw A function that tells the program how to draw the sprite
1129 /// associated with id.
1130 ///
1131 /// @return Returns the SequenceId assigned to the sprite associated with id.
1132 SequenceId AddSprite(const char *group_id,
1133  const std::function<bool(SequenceId seq)> &draw);
1134 
1135 /// @brief Draws all the sprites created with 'group_id' in AddSprite().
1136 ///
1137 /// @param[in] group_id A C-string in UTF-8 format that uniquely identifies an
1138 /// animation type.
1139 void DrawSprites(const char *group_id);
1140 
1141 /// @brief This function creates a new Motivator if it doesn't already exist
1142 /// and returns the current value of it.
1143 ///
1144 /// @warning This function only works if you have passed in a MotiveEngine
1145 /// to Run().
1146 ///
1147 /// @param[in] id A HashedId that uniquely identifies an animation.
1148 /// @param[in] starting_value An array of length dimensions that contains
1149 /// the value we want our curve to begin at.
1150 ///
1151 /// @return Returns a value of type T.
1152 template <typename T>
1153 T Animatable(HashedId id, const T &starting_value) {
1154  const float *motion = details::Animatable(
1155  id, details::FloatConverter<T>::ToFloatArray(starting_value),
1158 }
1159 
1160 /// @brief This function creates a new animation if it doesn't already exist
1161 /// and returns the current value of it.
1162 ///
1163 /// @warning This function only works if you have passed in a MotiveEngine
1164 /// to Run().
1165 ///
1166 /// @param[in] id A C-string in UTF-8 format that uniquely identifies an
1167 /// animation.
1168 /// @param[in] starting_value An array of length dimensions that contains
1169 /// the value we want our curve to begin at.
1170 ///
1171 /// @return Returns a value of type T.
1172 template <typename T>
1173 T Animatable(const char *id, const T &starting_value) {
1174  return Animatable<T>(HashId(id), starting_value);
1175 }
1176 
1177 /// @brief This function sets the target value and velocity to which an
1178 /// animation, that is identified by id, animates.
1179 ///
1180 /// It also creates a new animation if it doesn't already exist.
1181 ///
1182 /// @param[in] id A HashedId that uniquely identifies an animation.
1183 /// @param[in] target_value An array of length dimensions that contains
1184 /// the value we want our curve to end at.
1185 /// @param[in] target_velocity An array of length dimensions that
1186 /// contains the velocity we want our curve to end at. A velocity of 0.0f would
1187 /// mean our curve would end with a flatter ease-out curve. A large velocity
1188 /// would give our curve a steeper ease-out. If the curve is overdetermined, the
1189 /// desired end velocities might not be achieved.
1190 /// @param[in] description A description of the curve's typical shape.
1191 template <typename T>
1192 void StartAnimation(HashedId id, const T& target_value,
1193  const T& target_velocity,
1194  const AnimCurveDescription &description) {
1196  id, details::FloatConverter<T>::ToFloatArray(target_value),
1198  details::FloatConverter<T>::Dimension(), description);
1199 }
1200 
1201 /// @brief This function sets the target value and velocity to which an
1202 /// animation, that is identified by id, animates.
1203 ///
1204 /// It also creates a new animation if it doesn't already exist.
1205 ///
1206 /// @param[in] id A C-string in UTF-8 format that uniquely identifies an
1207 /// animation.
1208 /// @param[in] target_value An array of length dimensions that contains
1209 /// the value we want our curve to end at.
1210 /// @param[in] target_velocity An array of length dimensions that
1211 /// contains the velocity we want our curve to end at. A velocity of 0.0f would
1212 /// mean our curve would end with a flatter ease-out curve. A large velocity
1213 /// would give our curve a steeper ease-out. If the curve is overdetermined, the
1214 /// desired end velocities might not be achieved.
1215 /// @param[in] description A description of the curve's typical shape.
1216 template <typename T>
1217 void StartAnimation(const char *id, const T& target_value,
1218  const T& target_velocity,
1219  const AnimCurveDescription &description) {
1220  StartAnimation<T>(HashId(id), target_value, target_velocity, description);
1221 }
1222 
1223 } // namespace flatui
1224 
1225 #endif // FPL_FLATUI_H
void StartSlider(Direction direction, float scroll_margin, float *value)
Make the current group into a slider group that can handle basic slider behavior. The group will capt...
void SetDragStartThreshold(int drag_start_threshold)
Set a threshold value for the start of a drag operation.
void EndGroup()
Clean up the Group element start by StartGroup().
Specifies the margins for a group, in units of virtual resolution.
Definition: flatui.h:395
double AnimationTimeRemaining(HashedId id)
Returns the time remaining for an animation.
ssize_t GetCapturedPointerIndex()
Get the index of th ecaptured pointer.
void DrawSprites(const char *group_id)
Draws all the sprites created with 'group_id' in AddSprite().
float GetScale()
Get the scaling factor for the virtual resolution.
void RenderTexture(const fplbase::Texture &tex, const mathfu::vec2i &pos, const mathfu::vec2i &size)
Render a Texture to a specific position with a given size.
mathfu::vec2 PhysicalToVirtual(const mathfu::vec2i &v)
Converts a physical screen coordinate to a virtual value.
void StartAnimation(HashedId id, const T &target_value, const T &target_velocity, const AnimCurveDescription &description)
This function sets the target value and velocity to which an animation, that is identified by id...
Definition: flatui.h:1192
void RenderTextureNinePatch(const fplbase::Texture &tex, const mathfu::vec4 &patch_info, const mathfu::vec2i &pos, const mathfu::vec2i &size)
Render a nine-patch texture at a specific position with a given size.
AnimType
Anim type describes the algorithm used to animate a UI element.
Definition: flatui.h:101
Event Edit(float ysize, const mathfu::vec2 &size, const char *id, EditStatus *status, std::string *string)
Renders an edit text box as a GUI element.
Event
Event types are returned by most interactive elements. These are flags, because multiple events may o...
Definition: flatui.h:255
FontManager manages font rendering with OpenGL utilizing freetype and harfbuzz as a glyph rendering a...
Definition: font_manager.h:109
T Animatable(HashedId id, const T &starting_value)
This function creates a new Motivator if it doesn't already exist and returns the current value of it...
Definition: flatui.h:1153
float typical_total_time
Definition: flatui.h:144
const char *const kDefaultImageID
A sentinel value for image IDs.
Definition: flatui.h:389
EditStatus
Status of Edit widget.
Definition: flatui.h:320
void Run(fplbase::AssetManager &assetman, FontManager &fontman, fplbase::InputSystem &input, motive::MotiveEngine *motive_engine, const std::function< void()> &gui_definition)
The core function that drives the GUI.
void SetTextDirection(const TextLayoutDirection direction)
Override a text layout direction set by SetTextLocale() API.
bool IsLastEventPointerType()
void SetDepthTest(bool enable)
Enables depth testing, when needed for rendering a UI in 3D.
void SetScrollSpeed(float scroll_speed_drag, float scroll_speed_wheel, float scroll_speed_gamepad)
Set the scroll speed of the drag, mouse wheel, and gamepad operations.
void SetGlobalListener(const std::function< void(HashedId id, Event event)> &callback)
Set a global listener callback that receives all events to all interactive elements (useful for loggi...
void ColorBackground(const mathfu::vec4 &color)
Set the background color for the group.
void SetTextColor(const mathfu::vec4 &color)
Set the Label's text color.
void CustomElement(const mathfu::vec2 &virtual_size, const char *id, const std::function< void(const mathfu::vec2i &pos, const mathfu::vec2i &size)> renderer)
Create a custom element with a given size.
int NumActiveSprites(HashedId id)
This function returns the current number of sprites animating on screen.
void PositionGroup(Alignment horizontal, Alignment vertical, const mathfu::vec2 &offset)
Position a group within the screen as a whole.
void StartAnimation(const char *id, const T &target_value, const T &target_velocity, const AnimCurveDescription &description)
This function sets the target value and velocity to which an animation, that is identified by id...
Definition: flatui.h:1217
Margin(float left, float top, float right, float bottom)
Creates a margin specifying all four sides individually.
Definition: flatui.h:418
void EnableTextSDF(bool inner_sdf, bool outer_sdf, float threshold)
Enable/Disable a signed distance field generation with glyphs. A SDF generation of an inner region an...
void SetTextKerningScale(float scale)
Set a kerning scale used in the text rendering.
void Image(const fplbase::Texture &texture, float ysize, const char *id=kDefaultImageID)
Render an image as a GUI element.
void Label(const char *text, float ysize, HashedId label_id=kNullHash)
Render a label as a GUI element.
void SetDefaultFocus()
Set the default keyboard/gamepad focus to the current element.
const char *const kDefaultGroupID
A sentinel value for group IDs.
Definition: flatui.h:384
void SetImageColor(const mathfu::vec4 &color)
Set the Images's tint.
void SetTextEllipsis(const char *ellipsis)
Set an ellipsis string used in label/edit widgets.
void StartGroup(Layout layout, float spacing=0, const char *id=kDefaultGroupID)
Create a group of elements with a given layout and intra-element spacing.
void EndSlider()
Ends the current slider group.
Margin(float x, float y)
Create a Margin with the left and right sizes of x, and top and bottom sizes of y.
Definition: flatui.h:408
AnimType type
Definition: flatui.h:128
TextAlignment
Alignment of the text.
Definition: font_buffer.h:110
void SetMargin(const Margin &margin)
Sets the margin for the current group.
converts from a mathfu::vector to a const float pointer and vice versa.
Definition: flatui.h:1040
void ImageBackgroundNinePatch(const fplbase::Texture &tex, const mathfu::vec4 &patch_info)
Set the background texture for the group with nine patch settings.
const float FLATUI_DEFAULT_VIRTUAL_RESOLUTION
The default virtual resolution, if none is set.
Definition: flatui.h:933
mathfu::vec2i VirtualToPhysical(const mathfu::vec2 &v)
Converts a virtual screen coordinate to a physical value.
void StartScroll(const mathfu::vec2 &size, mathfu::vec2 *offset)
Make the current group into a scrolling group that can display arbitrary sized elements inside a wind...
Margin(float m)
Create a Margin with all four sides of equal size.
Definition: flatui.h:399
void ImageBackground(const fplbase::Texture &tex)
Set the background texture for the group.
void SetTextOuterColor(const mathfu::vec4 &color, float size, const mathfu::vec2 &offset)
Set the Label's outer color (e.g. drop shadow color). To use the feature, outer SDF generation needs ...
void EndScroll()
Ends the current scrolling group.
bool SetTextFont(const char *font_name)
Set the Label's font.
void SetTextLocale(const char *locale)
Set a locale used for the text rendering.
void ModalGroup()
Called inside of a group that that is mean to act like a popup inside of a kLayoutOverlay.
SequenceId AddSprite(const char *group_id, const std::function< bool(SequenceId seq)> &draw)
This function adds a sprite, which will be drawn and then forgotten after it is finished firing...
void HtmlLabel(const char *html, float ysize, const mathfu::vec2 &label_size, TextAlignment alignment, const char *id)
Render simple HTML text.
float typical_delta_distance
Definition: flatui.h:139
void UseExistingProjection(const mathfu::vec2i &canvas_size)
By default, FlatUI sets up a projection matrix for all the rendering that covers the entire screen (a...
Direction
Direction of the groups.
Definition: flatui.h:301
T Animatable(const char *id, const T &starting_value)
This function creates a new animation if it doesn't already exist and returns the current value of it...
Definition: flatui.h:1173
void EnableTextHyphenation(bool enable)
Enable/Disable the hyphenation in the text label.
TextLayoutDirection
Specify how to layout texts. Default value is TextLayoutDirectionLTR.
Definition: font_buffer.h:75
mathfu::vec2 GetVirtualResolution()
mathfu::vec2 GroupSize()
Alignment
Alignment of the groups.
Definition: flatui.h:282
void CapturePointer(const char *element_id)
Caputre a pointer event.
Layout
Specify how to layout a group.
Definition: flatui.h:370
mathfu::vec4_packed borders
A vector of four floats containing the values for the four sides of the margin.
Definition: flatui.h:427
float bias
Definition: flatui.h:164
Describes a curve's typical shape.
Definition: flatui.h:113
void SetTextLineHeightScale(float scale)
Set a line height scale used in the text rendering.
void SetVirtualResolution(float virtual_resolution)
Set the virtual resolution of the smallest dimension of the screen (the Y size in landscape mode...
void ApplyCustomTransform(const mathfu::mat4 &imvp)
If you're rendering the UI at a location that does not correspond to the display's pixels (e...
Event CheckEvent()
Check for events from the current group.
mathfu::vec2 GroupPosition()
void ReleasePointer()
Release a pointer capture.