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 "../l10n.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 tt {
22
24public:
25 using super = widget;
27 using callback_ptr_type = typename delegate_type::callback_ptr_type;
28
32
36
40
44
47 template<typename Label>
48 void set_label(Label const &rhs) noexcept
49 {
50 tt_axiom(is_gui_thread());
51 on_label = rhs;
52 off_label = rhs;
53 other_label = rhs;
54 }
55
59 [[nodiscard]] button_state state() const noexcept
60 {
61 tt_axiom(is_gui_thread());
62 if (auto delegate = _delegate.lock()) {
63 return delegate->state(*this);
64 } else {
65 return button_state::off;
66 }
67 }
68
71 template<typename Callback>
72 [[nodiscard]] callback_ptr_type subscribe(Callback &&callback) noexcept
73 {
74 tt_axiom(is_gui_thread());
75 return _notifier.subscribe(std::forward<Callback>(callback));
76 }
77
80 void unsubscribe(callback_ptr_type &callback_ptr) noexcept;
81
83 void init() noexcept override;
84 void deinit() noexcept override;
85 [[nodiscard]] bool constrain(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override;
86 [[nodiscard]] void layout(hires_utc_clock::time_point displayTimePoint, bool need_layout) noexcept override;
87 [[nodiscard]] color background_color() const noexcept override;
88 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept final;
89 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
90 void activate() noexcept;
91 [[nodiscard]] bool handle_event(command command) noexcept override;
92 [[nodiscard]] bool handle_event(mouse_event const &event) noexcept final;
94protected:
95 aarectangle _label_rectangle;
96 label_widget *_on_label_widget = nullptr;
97 label_widget *_off_label_widget = nullptr;
98 label_widget *_other_label_widget = nullptr;
99
100 bool _pressed = false;
101 notifier<void()> _notifier;
103
105};
106
107} // namespace tt
button_state
The state of a button.
Definition button_state.hpp:13
This is a RGBA floating point color.
Definition color.hpp:36
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
Definition gui_window.hpp:36
Definition hitbox.hpp:14
Definition mouse_event.hpp:15
Timestamp.
Definition hires_utc_clock.hpp:19
A localizable message.
Definition l10n.hpp:149
Definition notifier.hpp:18
An observable value.
Definition observable.hpp:280
Class that hold either a weak_ptr or a unique_ptr This class is to hold a weak_ptr,...
Definition weak_or_unique_ptr.hpp:25
Definition abstract_button_widget.hpp:23
void unsubscribe(callback_ptr_type &callback_ptr) noexcept
Unsubscribe a callback.
button_state state() const noexcept
Get the current state of the button.
Definition abstract_button_widget.hpp:59
callback_ptr_type subscribe(Callback &&callback) noexcept
Subscribe a callback to call when the button is activated.
Definition abstract_button_widget.hpp:72
observable< label > off_label
The label to show when the button is in the 'off' state.
Definition abstract_button_widget.hpp:35
observable< label > other_label
The label to show when the button is in the 'other' state.
Definition abstract_button_widget.hpp:39
void set_label(Label const &rhs) noexcept
Set on/off/other labels of the button to the same value.
Definition abstract_button_widget.hpp:48
observable< label > on_label
The label to show when the button is in the 'on' state.
Definition abstract_button_widget.hpp:31
observable< alignment > label_alignment
The alignment of the on/off/other label.
Definition abstract_button_widget.hpp:43
Definition button_delegate.hpp:14
The GUI widget displays and lays out text together with an icon.
Definition label_widget.hpp:32
An interactive graphical object as part of the user-interface.
Definition widget.hpp:39
widget *const parent
Pointer to the parent widget.
Definition widget.hpp:48
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:43
widget(gui_window &window, widget *parent) noexcept