HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
abstract_button_widget.hpp
1// Copyright Take Vos 2019-2020.
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
5#pragma once
6
7#include "widget.hpp"
8#include "button_delegate.hpp"
9#include "label_widget.hpp"
10#include "button_type.hpp"
11#include "../animator.hpp"
12#include "../i18n/translate.hpp"
13#include "../notifier.hpp"
14#include "../weak_or_unique_ptr.hpp"
15#include <memory>
16#include <string>
17#include <array>
18#include <optional>
19#include <future>
20
21namespace hi::inline v1 {
22
24public:
25 using super = widget;
27
30 observable<label> on_label = tr("on");
31
34 observable<label> off_label = tr("off");
35
38 observable<label> other_label = tr("other");
39
43
44 notifier<void()> pressed;
45
48 template<typename Label>
49 void set_label(Label const &rhs) noexcept
50 {
51 hi_axiom(is_gui_thread());
52 on_label = rhs;
53 off_label = rhs;
54 other_label = rhs;
55 }
56
60 [[nodiscard]] button_state state() const noexcept
61 {
62 hi_axiom(is_gui_thread());
63 if (auto delegate = _delegate.lock()) {
64 return delegate->state(*this);
65 } else {
66 return button_state::off;
67 }
68 }
69
71 [[nodiscard]] generator<widget *> children() const noexcept override
72 {
73 co_yield _on_label_widget.get();
74 co_yield _off_label_widget.get();
75 co_yield _other_label_widget.get();
76 }
77
78 [[nodiscard]] color background_color() const noexcept override;
79 [[nodiscard]] hitbox hitbox_test(point3 position) const noexcept final;
80 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
81 void activate() noexcept;
82 [[nodiscard]] bool handle_event(command command) noexcept override;
83 [[nodiscard]] bool handle_event(mouse_event const &event) noexcept final;
85protected:
86 aarectangle _label_rectangle;
87 std::unique_ptr<label_widget> _on_label_widget;
88 std::unique_ptr<label_widget> _off_label_widget;
89 std::unique_ptr<label_widget> _other_label_widget;
90
91 bool _pressed = false;
92 weak_or_unique_ptr<delegate_type> _delegate;
93 notifier<>::token_type _delegate_cbt;
94
95 ~abstract_button_widget();
96 abstract_button_widget(gui_window &window, widget *parent, weak_or_unique_ptr<delegate_type> delegate) noexcept;
97
98 widget_constraints set_constraints_button() const noexcept;
99 void set_layout_button(widget_layout const &context) noexcept;
100 void draw_button(draw_context const &context) noexcept;
101};
102
103} // namespace hi::inline v1
button_state
The state of a button.
Definition button_state.hpp:13
STL namespace.
A return value for a generator-function.
Definition generator.hpp:27
A localizable message.
Definition translate.hpp:150
A notifier which can be used to call a set of registered callbacks.
Definition notifier.hpp:23
An observable value.
Definition observable.hpp:356
Definition abstract_button_widget.hpp:23
void set_label(Label const &rhs) noexcept
Set on/off/other labels of the button to the same value.
Definition abstract_button_widget.hpp:49
button_state state() const noexcept
Get the current state of the button.
Definition abstract_button_widget.hpp:60
observable< alignment > label_alignment
The alignment of the on/off/other label.
Definition abstract_button_widget.hpp:42
Definition button_delegate.hpp:14
An interactive graphical object as part of the user-interface.
Definition widget.hpp:40