TermOx
button.hpp
1 #ifndef TERMOX_WIDGET_WIDGETS_BUTTON_HPP
2 #define TERMOX_WIDGET_WIDGETS_BUTTON_HPP
3 #include <memory>
4 
5 #include <signals_light/signal.hpp>
6 
7 #include <termox/painter/color.hpp>
8 #include <termox/painter/glyph_string.hpp>
9 #include <termox/system/mouse.hpp>
10 #include <termox/widget/layouts/horizontal.hpp>
11 #include <termox/widget/layouts/vertical.hpp>
12 #include <termox/widget/widget.hpp>
13 
14 namespace ox {
15 class Painter;
16 
18 class Button : public Widget {
19  public:
20  struct Parameters {
21  Glyph_string text = U"";
22  };
23 
24  public:
26  sl::Signal<void()> pressed;
27 
29  sl::Signal<void()> released;
30 
31  public:
33  explicit Button(Glyph_string text = U"");
34 
36  explicit Button(Parameters p);
37 
38  public:
41 
43  [[nodiscard]] auto text() const -> Glyph_string const&;
44 
45  protected:
46  auto mouse_press_event(Mouse const& m) -> bool override;
47 
48  auto mouse_release_event(Mouse const& m) -> bool override;
49 
50  auto paint_event(Painter& p) -> bool override;
51 
52  private:
53  Glyph_string text_;
54 };
55 
57 [[nodiscard]] auto button(Glyph_string text = U"") -> std::unique_ptr<Button>;
58 
60 [[nodiscard]] auto button(Button::Parameters p) -> std::unique_ptr<Button>;
61 
63 class Push_button : public Button {
64  public:
65  struct Parameters {
66  Glyph_string text;
67  Color pressed_color = Color::Foreground;
68  Color released_color = Color::Background;
69  };
70 
71  public:
73  explicit Push_button(Glyph_string text = U"",
74  Color pressed_color = Color::Foreground,
75  Color released_color = Color::Background);
76 
78  explicit Push_button(Parameters p);
79 
80  public:
82  void set_pressed_color(Color c);
83 
85  [[nodiscard]] auto get_pressed_color() const -> Color;
86 
88  void set_released_color(Color c);
89 
91  [[nodiscard]] auto get_released_color() const -> Color;
92 
93  private:
94  Color pressed_color_;
95  Color released_color_;
96 };
97 
99 [[nodiscard]] auto push_button(Glyph_string text = U"",
100  Color pressed_color = Color::Foreground,
101  Color released_color = Color::Background)
102  -> std::unique_ptr<Push_button>;
103 
105 [[nodiscard]] auto push_button(Push_button::Parameters p)
106  -> std::unique_ptr<Push_button>;
107 
109 template <template <typename> typename Layout_t>
110 class Thin_button : public Button {
111  public:
112  using Button::Parameters;
113 
114  public:
115  explicit Thin_button(Glyph_string text = U"");
116 
117  explicit Thin_button(Parameters p);
118 };
119 
121 template <template <typename> typename Layout_t>
122 [[nodiscard]] auto thin_button(Glyph_string text = U"")
123  -> std::unique_ptr<Thin_button<Layout_t>>;
124 
126 template <template <typename> typename Layout_t>
127 [[nodiscard]] auto thin_button(typename Thin_button<Layout_t>::Parameters p)
128  -> std::unique_ptr<Thin_button<Layout_t>>;
129 
131 
133 [[nodiscard]] auto hthin_button(Glyph_string text = U"")
134  -> std::unique_ptr<HThin_button>;
135 
137 [[nodiscard]] auto hthin_button(HThin_button::Parameters p)
138  -> std::unique_ptr<HThin_button>;
139 
141 
143 [[nodiscard]] auto vthin_button(Glyph_string text = U"")
144  -> std::unique_ptr<VThin_button>;
145 
147 [[nodiscard]] auto vthin_button(VThin_button::Parameters p)
148  -> std::unique_ptr<VThin_button>;
149 
150 } // namespace ox
151 #endif // TERMOX_WIDGET_WIDGETS_BUTTON_HPP
Button widget that emits Signal on a left mouse button press.
Definition: button.hpp:18
sl::Signal< void()> pressed
Emitted on a left mouse button press.
Definition: button.hpp:26
sl::Signal< void()> released
Emitted on a left mouse button release.
Definition: button.hpp:29
auto paint_event(Painter &p) -> bool override
Handles Paint_event objects.
Definition: button.cpp:42
Button(Glyph_string text=U"")
Construct a Button with centered text.
Definition: button.cpp:16
void set_text(Glyph_string text)
Set the text and repaint.
Definition: button.cpp:20
auto mouse_press_event(Mouse const &m) -> bool override
Handles Mouse_press_event objects.
Definition: button.cpp:28
auto mouse_release_event(Mouse const &m) -> bool override
Handles Mouse_release_event objects.
Definition: button.cpp:35
auto text() const -> Glyph_string const &
Return the current text.
Definition: button.cpp:26
Color numbers [0 - 180] are valid.
Definition: color.hpp:16
Holds a collection of Glyphs with a similar interface to std::string.
Definition: glyph_string.hpp:19
Contains functions to paint Glyphs to a Widget's screen area.
Definition: painter.hpp:21
Button that changes its background Color on press and release events.
Definition: button.hpp:63
Width or Height 1 Button.
Definition: button.hpp:110
Definition: widget.hpp:31
Definition: button.hpp:20
Definition: button.hpp:65
Definition: widget.hpp:33