3 #include "../../generic/identifiable.h"
4 #include "../../generic/managed.h"
5 #include "../../generic/optional.h"
6 #include "../../generic/range_value.h"
7 #include "../stage_node.h"
17 ParamValue ret = any_cast<int>(in);
22 auto px = any_cast<Px>(in);
23 result = (int)px.value;
26 float f = any_cast<float>(in);
40 #define S_DEFINE_CORE_WIDGET_PROPERTIES(klass) \
41 _S_DEFINE_STAGE_NODE_PARAM(10000, klass, "width", int, -1, \
42 "The width of the widget"); \
43 _S_DEFINE_STAGE_NODE_PARAM(10001, klass, "height", int, -1, \
44 "The height of the widget"); \
45 _S_DEFINE_STAGE_NODE_PARAM(10002, klass, "theme", smlt::ui::UIConfig, \
46 smlt::ui::UIConfig(), \
47 "The theme to use for this widget"); \
48 _S_DEFINE_STAGE_NODE_PARAM( \
49 10003, klass, "shared_style", smlt::ui::WidgetStylePtr, \
50 smlt::ui::WidgetStylePtr(), "A shared style to use for this widget")
55 enum WidgetLayerIndex {
56 WIDGET_LAYER_INDEX_BORDER,
57 WIDGET_LAYER_INDEX_BACKGROUND,
58 WIDGET_LAYER_INDEX_FOREGROUND,
59 WIDGET_LAYER_INDEX_TEXT,
64 typedef sig::signal<void()> WidgetPressedSignal;
65 typedef sig::signal<void()>
67 typedef sig::signal<void()> WidgetClickedSignal;
68 typedef sig::signal<void()> WidgetFocusedSignal;
69 typedef sig::signal<void()> WidgetBlurredSignal;
81 uint8_t active_layers_ = ~0;
84 Px border_radius_ =
Px(0);
85 Px border_width_ =
Px(1);
87 TextAlignment text_alignment_ = TEXT_ALIGNMENT_CENTER;
89 OverflowType overflow_ = OVERFLOW_TYPE_AUTO;
91 TexturePtr background_image_;
94 TexturePtr foreground_image_;
101 float opacity_ = 1.0f;
103 MaterialPtr materials_[3] = {
nullptr,
nullptr,
nullptr};
106 typedef std::shared_ptr<WidgetStyle> WidgetStylePtr;
139 typedef std::shared_ptr<Widget> ptr;
144 virtual bool on_init()
override;
145 virtual void on_clean_up()
override;
147 bool on_create(
Params params)
override;
149 void resize(
Rem width,
Px height);
150 void resize(
Px width,
Rem height);
151 void resize(
Rem width,
Rem height);
152 void resize(
Px width,
Px height);
154 void set_font(
const std::string& family = DEFAULT_FONT_FAMILY,
155 Rem size =
Rem(1.0f), FontWeight weight = FONT_WEIGHT_NORMAL,
156 FontStyle style = FONT_STYLE_NORMAL);
157 void set_font(
const std::string& family = DEFAULT_FONT_FAMILY,
158 Px size = DEFAULT_FONT_SIZE,
159 FontWeight weight = FONT_WEIGHT_NORMAL,
160 FontStyle style = FONT_STYLE_NORMAL);
163 virtual void set_font(FontPtr font);
169 bool is_focused()
const;
170 void set_focus_previous(
WidgetPtr previous_widget);
171 void set_focus_next(
WidgetPtr next_widget);
176 WidgetPtr previous_in_focus_chain()
const;
177 void focus_next_in_chain(
178 ChangeFocusBehaviour behaviour = FOCUS_THIS_IF_NONE_FOCUSED);
179 void focus_previous_in_chain(
180 ChangeFocusBehaviour behaviour = FOCUS_THIS_IF_NONE_FOCUSED);
188 void set_text(
const unicode& text);
190 void set_text_alignment(TextAlignment alignment);
191 TextAlignment text_alignment()
const;
193 void set_border_width(
Px x);
194 Px border_width()
const;
196 void set_border_radius(
Px x);
197 Px border_radius()
const;
199 void set_border_color(
const Color& color);
200 void set_overflow(OverflowType type);
202 void set_padding(
Px x);
203 void set_padding(
Px left,
Px right,
Px bottom,
Px top);
204 UInt4 padding()
const;
206 virtual bool set_resize_mode(ResizeMode resize_mode);
208 ResizeMode resize_mode()
const;
209 WrapMode wrap_mode()
const {
212 void set_wrap_mode(WrapMode mode) {
216 bool has_background_image()
const;
218 bool has_foreground_image()
const;
229 void set_background_color(
const Color& color);
230 void set_foreground_color(
const Color& color);
241 void set_text_color(
const Color& color);
243 Px requested_width()
const;
244 Px requested_height()
const;
246 Px content_width()
const;
247 Px content_height()
const;
253 Px outer_height()
const;
263 const AABB& aabb()
const override;
270 void fingerdown(uint8_t finger_id);
271 void fingerup(uint8_t finger_id);
272 void fingerenter(uint8_t finger_id);
273 void fingermove(uint8_t finger_id);
274 void fingerleave(uint8_t finger_id);
275 bool is_pressed_by_finger(uint8_t finger_id);
276 bool is_pressed()
const;
279 void force_release();
286 Px line_height()
const;
288 void set_precedence(int16_t precedence);
290 int16_t precedence()
const;
293 MaterialPtr border_material()
const {
294 return style_->materials_[0];
296 MaterialPtr background_material()
const {
297 return style_->materials_[1];
299 MaterialPtr foreground_material()
const {
300 return style_->materials_[2];
304 virtual const unicode& calc_text()
const {
308 void on_render_priority_changed(RenderPriority old_priority,
309 RenderPriority new_priority)
override;
311 bool initialized_ =
false;
316 MeshPtr mesh_ =
nullptr;
319 void set_style(std::shared_ptr<WidgetStyle> style);
324 FontPtr font_ =
nullptr;
326 std::shared_ptr<WidgetStyle> style_;
328 ResizeMode resize_mode_ = RESIZE_MODE_FIT_CONTENT;
329 WrapMode wrap_mode_ = WRAP_MODE_WORD;
331 Px text_width_ =
Px(0);
332 Px text_height_ =
Px(0);
334 Px requested_width_ =
Px(-1);
335 Px requested_height_ =
Px(-1);
337 Px content_width_ =
Px(0);
338 Px content_height_ =
Px(0);
342 bool is_focused_ =
false;
349 bool anchor_point_dirty_ =
true;
351 uint16_t fingers_down_ = 0;
353 virtual void on_size_changed();
359 void _recalc_active_layers();
361 bool border_active()
const;
362 bool background_active()
const;
363 bool foreground_active()
const;
370 return max.x - min.x;
373 return max.y - min.y;
376 bool has_non_zero_area()
const {
379 return std::abs(w.value) > 0 && std::abs(h.value) > 0;
384 calculate_background_size(
const UIDim& content_dimensions)
const;
386 calculate_foreground_size(
const UIDim& content_dimensions)
const;
388 virtual UIDim calculate_content_dimensions(
Px text_width,
Px text_height);
390 void apply_image_rect(
SubMeshPtr submesh, TexturePtr image,
395 const Vec2* uvs =
nullptr,
float z_offset = 0.0f);
398 bool is_initialized()
const {
406 virtual void render_text();
407 virtual void render_border(
const WidgetBounds& border_bounds);
408 virtual void render_background(
const WidgetBounds& background_bounds);
409 virtual void render_foreground(
const WidgetBounds& foreground_bounds);
411 WidgetPtr focused_in_chain_or_this();
413 void on_transformation_change_attempted()
override;
417 virtual void prepare_build() {}
418 virtual void finalize_render() {}
419 virtual void finalize_build() {}
420 virtual bool pre_set_text(
const unicode&) {
424 void build_text_submeshes();
426 FontPtr load_or_get_font(
const std::string& family,
const Px& size,
427 const FontWeight& weight,
const FontStyle& style);
430 MaterialPtr find_or_create_material(
const char* name);
432 FontPtr _load_or_get_font(AssetManager* assets, AssetManager* shared_assets,
433 const std::string& familyc,
const Px& sizec,
434 const FontWeight& weight,
const FontStyle& style);