TermOx
system.hpp
1 #ifndef TERMOX_SYSTEM_SYSTEM_HPP
2 #define TERMOX_SYSTEM_SYSTEM_HPP
3 #include <atomic>
4 #include <functional>
5 #include <utility>
6 
7 #include <signals_light/signal.hpp>
8 
9 #include <termox/system/animation_engine.hpp>
10 #include <termox/system/detail/user_input_event_loop.hpp>
11 #include <termox/system/event_fwd.hpp>
12 #include <termox/terminal/key_mode.hpp>
13 #include <termox/terminal/mouse_mode.hpp>
14 #include <termox/terminal/signals.hpp>
15 #include <termox/widget/cursor.hpp>
16 
17 namespace ox {
18 class Widget;
19 class Event_queue;
20 } // namespace ox
21 
22 namespace ox {
23 
25 
27 class System {
28  public:
29  static sl::Slot<void()> quit;
30 
31  public:
33 
57  System(Mouse_mode mouse_mode = Mouse_mode::Basic,
58  Key_mode key_mode = Key_mode::Normal,
59  Signals signals = Signals::On);
60 
61  System(System const&) = delete;
62  System& operator=(System const&) = delete;
63  System(System&&) = default;
64  System& operator=(System&&) = default;
65 
66  [[noreturn]] ~System();
67 
68  public:
70  [[nodiscard]] static auto focus_widget() -> Widget*;
71 
73 
74  static void set_focus(Widget& w);
75 
77  static void clear_focus();
78 
80  static void enable_tab_focus();
81 
83  static void disable_tab_focus();
84 
86 
88  static void set_head(Widget* new_head);
89 
91 
93  [[nodiscard]] static auto head() -> Widget*;
94 
96 
99  template <typename Widget_t, typename... Args>
100  auto run(Args&&... args) -> int
101  {
102  auto head = Widget_t(std::forward<Args>(args)...);
104  return this->run();
105  }
106 
108 
109  auto run(Widget& head) -> int;
110 
112 
115  static auto run() -> int;
116 
118 
119  static auto send_event(Event e) -> bool;
120 
121  // Minor optimization.
123  static auto send_event(Paint_event e) -> bool;
124 
125  // Minor optimization.
127  static auto send_event(Delete_event e) -> bool;
128 
130 
133  static void post_event(Event e);
134 
136 
140  [[noreturn]] static void exit();
141 
143 
144  static void enable_animation(Widget& w,
145  Animation_engine::Duration_t interval);
146 
148 
149  static void enable_animation(Widget& w, FPS fps);
150 
152 
153  static void disable_animation(Widget& w);
154 
156  static void set_cursor(Cursor cursor, Point offset);
157 
159 
160  static void set_current_queue(Event_queue& queue);
161 
162  private:
163  inline static std::atomic<Widget*> head_ = nullptr;
164  static detail::User_input_event_loop user_input_loop_;
165  static Animation_engine animation_engine_;
166  static std::reference_wrapper<Event_queue> current_queue_;
167 };
168 
169 } // namespace ox
170 #endif // TERMOX_SYSTEM_SYSTEM_HPP
Registers Widgets with intervals to send timer events.
Definition: animation_engine.hpp:15
Holds and provides access to all data relevant to a Widget's cursor.
Definition: cursor.hpp:10
Definition: event_queue.hpp:63
Organizes the highest level of the TUI framework.
Definition: system.hpp:27
static void enable_tab_focus()
Enable Tab/Back_tab keys to change the focus Widget.
Definition: system.cpp:43
static void exit()
Sets the exit flag for the user input event loop.
Definition: system.cpp:114
static void clear_focus()
Removes focus from the currently in focus Widget.
Definition: system.cpp:41
static void set_focus(Widget &w)
Give program focus to w.
Definition: system.cpp:39
static auto focus_widget() -> Widget *
Return a pointer to the currently focused Widget.
Definition: system.cpp:37
auto run(Args &&... args) -> int
Create a Widget_t object, set it as head widget and call System::run().
Definition: system.hpp:100
static void enable_animation(Widget &w, Animation_engine::Duration_t interval)
Enable animation for the given Widget w at interval.
Definition: system.cpp:121
static void post_event(Event e)
Append the event to the Event_queue for the thread it was called on.
Definition: system.cpp:112
System(Mouse_mode mouse_mode=Mouse_mode::Basic, Key_mode key_mode=Key_mode::Normal, Signals signals=Signals::On)
Initializes the terminal screen into curses mode.
Definition: system.cpp:30
static auto send_event(Event e) -> bool
Immediately send the event filters and then to the intended receiver.
Definition: system.cpp:79
static void disable_tab_focus()
Disable Tab/Back_tab keys from changing focus Widget.
Definition: system.cpp:45
static void set_current_queue(Event_queue &queue)
Set the Event_queue that will be used by post_event.
Definition: system.cpp:151
static auto run() -> int
Launch the main Event_loop and start processing Events.
Definition: system.cpp:67
static auto head() -> Widget *
Return a pointer to the head Widget.
Definition: system.cpp:59
static void disable_animation(Widget &w)
Disable animation for the given Widget w.
Definition: system.cpp:135
static void set_cursor(Cursor cursor, Point offset)
Set the terminal cursor via cursor parameters and offset applied.
Definition: system.cpp:140
static void set_head(Widget *new_head)
Set a new head Widget for the entire system.
Definition: system.cpp:47
Definition: widget.hpp:31
Event loop that blocks for user input on each iteration.
Definition: user_input_event_loop.hpp:9
Definition: event.hpp:72
Frames Per Second.
Definition: fps.hpp:8
Definition: event.hpp:23