FlatUI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Enumerations Groups Pages
flatui_common.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_COMMON_H
16 #define FPL_FLATUI_COMMON_H
17 
18 #include "fplbase/material.h"
19 
20 namespace flatui {
21 
22 /// @file
23 /// @addtogroup flatui_common_widgets
24 /// @{
25 ///
26 /// @brief This file contains some common widgets that will be widely
27 /// useful.
28 ///
29 /// FlatUI is structured to be very compositional, meaning that no one widget
30 /// will always be universally useful for every UI. This means that these
31 /// widgets are intended for the average UI, but may not be sufficient for
32 /// custom use cases that extend beyond the scope of these widgets. Therefore
33 /// these widgets may also serve as building blocks, or as examples, to easily
34 /// build your own specialized widgets.
35 
36 /// @enum ButtonProperty
37 ///
38 /// @brief Specifies the button properties used in common widgets.
39 ///
40 /// `kButtonPropertyDisabled` disables buttons (`CheckEvent()` calls will be
41 /// skipped).
42 /// `kButtonPropertyImageLeft` shows the specified image to the left of the text
43 /// label in a TextButton.
44 /// `kButtonPropertyImageRight` shows the specified image to the right of the
45 /// text label in a TextButton.
47  kButtonPropertyDisabled = 1,
48  kButtonPropertyImageLeft = 2,
49  kButtonPropertyImageRight = 4,
50 };
51 
52 /// @brief The bitwise OR operator for ButtonProperties.
53 ///
54 /// E.g. `c = a | b;`
55 ///
56 /// @param[in] a The first ButtonProperty whose `int` value should be used in
57 /// the bitwise OR.
58 /// @param[in] b The second ButtonProperty whose `int` value should be used in
59 /// the bitwise OR.
60 ///
61 /// @return Returns a new ButtonProperty that is formed from the result of the
62 /// bitwise OR of the two input ButtonProperties' `int` values.
64  return static_cast<ButtonProperty>(static_cast<int>(a) | static_cast<int>(b));
65 }
66 
67 /// @brief The bitwise OR assignment operator for ButtonProperties.
68 ///
69 /// E.g. `a |= b;`
70 ///
71 /// @param[in] a The modifiable ButtonProperty lvalue whose `int` value should
72 /// be used in the bitwise OR. It also captures the return value of the
73 /// function.
74 /// @param[in] b The second ButtonProperty whose `int` value is used in the
75 /// bitwise OR.
76 ///
77 /// @return Returns a new ButtonProperty that is formed from the result of the
78 /// bitwise OR of the two input ButtonProperties' `int` values.
80  a = a | b;
81  return a;
82 }
83 
84 /// @brief Some of the widgets provide user feedback by rendering a transparent
85 /// background color to signal that the user is hovering over or
86 /// clicking/touching/interacting with the widget.
87 ///
88 /// Be default, these are a dark grey and a medium grey, respectively. Both
89 /// are semi-transparent.
90 ///
91 /// @param[in] hover_color A const vec4 reference to the RGBA color values to
92 /// use when hovering over a widget.
93 /// @param[in] click_color A const vec4 reference to the RGBA color values to
94 /// use when clicking on a widget.
95 void SetHoverClickColor(const mathfu::vec4 &hover_color,
96  const mathfu::vec4 &click_color);
97 
98 /// @brief A simple button showing clickable text.
99 ///
100 /// @note Uses the colors that are set via `SetHoverClickColor`.
101 ///
102 /// @param[in] text A C-string of text to display on the button.
103 /// @param[in] size A float indicating the vertical height.
104 /// @param[in] margin A Margin that should be placed around the text.
105 ///
106 /// @return Returns the Event type for the button.
107 Event TextButton(const char *text, float size, const Margin &margin);
108 
109 /// @brief A simple button showing a clickable image.
110 ///
111 /// @note Uses the colors that are set via `SetHoverClickColor`.
112 ///
113 /// @param[in] texture The Texture of the image to display.
114 /// @param[in] size A float indicating the vertical height.
115 /// @param[in] margin A Margin around the `texture`.
116 /// @param[in] id A C-string to uniquely identify the button.
117 ///
118 /// @return Returns the Event type for the button.
119 Event ImageButton(const fplbase::Texture &texture, float size,
120  const Margin &margin, const char *id);
121 
122 /// @brief A button showing a different image when clicked or not.
123 ///
124 /// @param[in] up_texture The Texture of the image to display if not clicked.
125 /// @param[in] down_texture The Texture of the image to display if clicked.
126 /// @param[in] size A float indicating the vertical height.
127 /// @param[in] margin A Margin around the `texture`.
128 /// @param[in] id A C-string to uniquely identify the button.
129 ///
130 /// @return Returns the Event type for the button.
131 Event ToggleImageButton(const fplbase::Texture &up_texture,
132  const fplbase::Texture &down_texture, float size,
133  const Margin &margin, const char *id);
134 
135 /// @brief A simple button showing clickable text with an image shown beside it.
136 ///
137 /// @note Uses the colors that are set via `SetHoverClickColor`.
138 ///
139 /// @param[in] texture The Texture of the image to display beside the button.
140 /// @param[in] texture_margin The Margin around the `texture`.
141 /// @param[in] text A C-string of text to display on the button.
142 /// @param[in] size A float indicating the vertical height.
143 /// @param[in] margin A Margin that should be placed around the text.
144 /// @param[in] property A ButtonProperty enum corresponding to where the
145 /// image should be placed, relative to the button.
146 ///
147 /// @return Returns the Event type for the button.
148 Event TextButton(const fplbase::Texture &texture, const Margin &texture_margin,
149  const char *text, float size, const Margin &margin,
150  const ButtonProperty property);
151 
152 /// @brief A checkbox with a label next to it.
153 ///
154 /// @param[in] texture_checked A const reference to the Texture for when the
155 /// box is checked.
156 /// @param[in] texture_unchecked A const reference to the Texture for when the
157 /// box is unchecked.
158 /// @param[in] label A C-string to be used as the label that appears next to the
159 /// checkbox.
160 /// @param[in] size A float corresponding to the size of the checkbox and label.
161 /// @param[in] margin A Margin around the checkbox.
162 /// @param[in,out] is_checked A pointer to a bool determining if the checkbox is
163 /// checked or not. It will capture the output of the `kEventWentUp` as well.
164 ///
165 /// @return Returns the Event type for the checkbox.
166 Event CheckBox(const fplbase::Texture &texture_checked,
167  const fplbase::Texture &texture_unchecked, const char *label,
168  float size, const Margin &margin, bool *is_checked);
169 
170 /// @brief A clider to change a numeric value.
171 ///
172 /// @param[in] tex_bar The Texture for the slider. Typically this is a bar of
173 /// some kind as a ninepatch texture.
174 /// @param[in] tex_knob The Texture for the knob to move on top of the
175 /// `tex_bar`.
176 /// @param[in] size A const vec2 reference to specify the whole size, including
177 /// the margin, and relative size of the slider.
178 /// @param[in] bar_height A float corresponding the the Y ratio of the bar
179 /// (usually 0.5).
180 /// @param[in] id A C-string to uniquely identify the slider.
181 /// @param[out] slider_value A pointer to a float between 0.0 and 1.0 inclusive,
182 /// which contains the position of the slider.
183 Event Slider(const fplbase::Texture &tex_bar, const fplbase::Texture &tex_knob,
184  const mathfu::vec2 &size, float bar_height, const char *id,
185  float *slider_value);
186 
187 /// @brief A scrollbar to indicate position in a scroll view.
188 ///
189 /// @note The background and foreground Textures must be a ninepatch texture.
190 ///
191 /// @param[in] tex_background A const Texture reference for the background.
192 /// @param[in] tex_foreground A const Texture reference for the foreground.
193 /// @param[in] size A const vec2 reference to specify the whole size, including
194 /// the margin, and the relative size of the scroll bar.
195 /// @param[in] bar_size A float corresponding to the size of the scroll bar.
196 /// @param[in] id A C-string to uniquely identify the scroll bar.
197 /// @param[out] scroll_value A pointer to a float between 0.0 and 1.0 inclusive,
198 /// which contains the position of the slider.
199 ///
200 /// @return Returns the Event type for the scroll bar.
201 Event ScrollBar(const fplbase::Texture &tex_background,
202  const fplbase::Texture &tex_foreground,
203  const mathfu::vec2 &size, float bar_size, const char *id,
204  float *scroll_value);
205 
206 /// @brief Sets a background color of the widget based on the event status.
207 ///
208 /// If the event is `kEventIsDown`, the background color used will be the
209 /// `click_color`. If the event is `kEventHover`, the background color used
210 /// will be `hover_color`.
211 ///
212 /// @param[in] event The Event type used to determine the background color.
213 void EventBackground(Event event);
214 /// @}
215 
216 } // namespace flatui
217 
218 #endif // FPL_FLATUI_COMMON_H
Event ScrollBar(const fplbase::Texture &tex_background, const fplbase::Texture &tex_foreground, const mathfu::vec2 &size, float bar_size, const char *id, float *scroll_value)
A scrollbar to indicate position in a scroll view.
Event
Event types are returned by most interactive elements. These are flags, because multiple events may o...
Definition: flatui.h:255
ButtonProperty operator|=(ButtonProperty &a, const ButtonProperty &b)
The bitwise OR assignment operator for ButtonProperties.
Definition: flatui_common.h:79
ButtonProperty
Specifies the button properties used in common widgets.
Definition: flatui_common.h:46
void SetHoverClickColor(const mathfu::vec4 &hover_color, const mathfu::vec4 &click_color)
Some of the widgets provide user feedback by rendering a transparent background color to signal that ...
void EventBackground(Event event)
Sets a background color of the widget based on the event status.
Event Slider(const fplbase::Texture &tex_bar, const fplbase::Texture &tex_knob, const mathfu::vec2 &size, float bar_height, const char *id, float *slider_value)
A clider to change a numeric value.
Event TextButton(const char *text, float size, const Margin &margin)
A simple button showing clickable text.
Event CheckBox(const fplbase::Texture &texture_checked, const fplbase::Texture &texture_unchecked, const char *label, float size, const Margin &margin, bool *is_checked)
A checkbox with a label next to it.
Event ImageButton(const fplbase::Texture &texture, float size, const Margin &margin, const char *id)
A simple button showing a clickable image.
Event ToggleImageButton(const fplbase::Texture &up_texture, const fplbase::Texture &down_texture, float size, const Margin &margin, const char *id)
A button showing a different image when clicked or not.
ButtonProperty operator|(ButtonProperty a, ButtonProperty b)
The bitwise OR operator for ButtonProperties.
Definition: flatui_common.h:63