HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
momentary_button_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
8
9#pragma once
10
12#include "../macros.hpp"
13
14namespace hi { inline namespace v1 {
15
19class momentary_button_widget final : public abstract_button_widget {
20public:
21 using super = abstract_button_widget;
22 using delegate_type = typename super::delegate_type;
23
27 button_widget_attribute auto&&...attributes) noexcept :
29 {
30 alignment = alignment::middle_center();
31 set_attributes<0>(hi_forward(attributes)...);
32 }
33
34 momentary_button_widget(widget *parent, button_widget_attribute auto&&...attributes) noexcept :
36 {
37 }
38
40 [[nodiscard]] box_constraints update_constraints() noexcept override
41 {
42 _label_constraints = super::update_constraints();
43
44 // On left side a check mark, on right side short-cut. Around the label extra margin.
45 hilet extra_size = extent2{theme().margin<float>() * 2.0f, theme().margin<float>() * 2.0f};
46
47 auto constraints = _label_constraints + extra_size;
48 constraints.margins = theme().margin();
49 return constraints;
50 }
51 void set_layout(widget_layout const& context) noexcept override
52 {
53 if (compare_store(_layout, context)) {
54 hilet label_rectangle =
55 aarectangle{theme().margin<float>(), 0.0f, context.width() - theme().margin<float>() * 2.0f, context.height()};
56 _on_label_shape = _off_label_shape = _other_label_shape =
57 box_shape{_label_constraints, label_rectangle, theme().baseline_adjustment()};
58 }
59 super::set_layout(context);
60 }
61 void draw(draw_context const& context) noexcept override
62 {
63 if (*mode > widget_mode::invisible and overlaps(context, layout())) {
64 draw_label_button(context);
65 draw_button(context);
66 }
67 }
69private:
70 box_constraints _label_constraints;
71
72 void draw_label_button(draw_context const& context) noexcept
73 {
74 // Move the border of the button in the middle of a pixel.
75 context.draw_box(
76 layout(),
77 layout().rectangle(),
78 background_color(),
79 focus_color(),
80 theme().border_width(),
82 theme().rounding_radius());
83 }
84};
85
86}} // namespace hi::v1
Defines abstract_button_widget.
@ rectangle
The gui_event has rectangle data.
Definition gui_event_variant.hpp:42
@ invisible
The widget is invisible.
Definition widget_mode.hpp:36
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
The HikoGUI API version 1.
Definition lookahead_iterator.hpp:6
@ inside
The border is drawn inside the edge of a quad.
Definition draw_context.hpp:34
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:56
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:29
A high-level geometric extent.
Definition extent2.hpp:29
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:208
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:28
virtual box_constraints update_constraints() noexcept=0
Update the constraints of the widget.
The layout of a widget.
Definition widget_layout.hpp:38
2D constraints.
Definition box_constraints.hpp:25
Definition box_shape.hpp:18
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition abstract_button_widget.hpp:40
observer< alignment > alignment
The alignment of the button and on/off/other label.
Definition abstract_button_widget.hpp:56
A momentary button widget.
Definition momentary_button_widget.hpp:19
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
widget_layout const & layout() const noexcept override
Get the current layout for this widget.
Definition widget.hpp:169
void set_layout(widget_layout const &context) noexcept override
Update the internal layout of the widget.
Definition widget.hpp:164
void draw(draw_context const &context) noexcept override
Draw the widget.
Definition widget.hpp:174
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:158
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:42
T make_shared(T... args)
T move(T... args)