Project Alice
Loading...
Searching...
No Matches
gui_element_base.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "system_state.hpp"
4#include "gui_graphics.hpp"
5#include "text.hpp"
6
7namespace sys {
8struct state;
9}
10
11namespace ui {
12
14
16public:
17 static constexpr uint8_t is_invisible_mask = 0x01;
18
20 element_base* parent = nullptr;
22
23 bool is_visible() const {
24 return (flags & is_invisible_mask) == 0;
25 }
26 void set_visible(sys::state& state, bool vis) {
27 auto old_visibility = is_visible();
29 if(vis && !old_visibility) {
32 } else if(!vis && old_visibility) {
34 }
35 }
36
38
39 // impl members: to be overridden only for the very basic container / not a container distinction
40 // - are responsible for propagating messages and responses
41 // - should be called in general when something happens
42 virtual mouse_probe impl_probe_mouse(sys::state& state, int32_t x, int32_t y, mouse_probe_type type) noexcept; // tests which element is under the cursor
43 virtual message_result impl_on_lbutton_down(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
44 virtual message_result impl_on_lbutton_up(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods, bool under_mouse) noexcept;
45 virtual message_result impl_on_rbutton_down(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
47 virtual message_result impl_on_scroll(sys::state& state, int32_t x, int32_t y, float amount, sys::key_modifiers mods) noexcept;
48 virtual message_result impl_on_mouse_move(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
49 virtual void impl_on_update(sys::state& state) noexcept;
51 virtual message_result impl_set(sys::state& state, Cyto::Any& payload) noexcept;
52 virtual void impl_render(sys::state& state, int32_t x, int32_t y) noexcept;
53 virtual void impl_on_reset_text(sys::state& state) noexcept;
54 virtual void impl_on_drag_finish(sys::state& state) noexcept {
56 }
57
58 virtual tooltip_behavior has_tooltip(sys::state& state) noexcept { // used to test whether a tooltip is possible
60 }
61 virtual void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept { }
62
63 // these message handlers can be overridden by basically anyone
64 // - generally *should not* be called directly
65protected:
66 virtual message_result test_mouse(sys::state& state, int32_t x, int32_t y, mouse_probe_type type) noexcept; // asks whether the mouse would be intercepted here, but without taking an action
67 virtual message_result on_lbutton_down(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
68 virtual message_result on_lbutton_up(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods, bool under_mouse) noexcept;
69 virtual message_result on_rbutton_down(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
71 virtual message_result on_scroll(sys::state& state, int32_t x, int32_t y, float amount, sys::key_modifiers mods) noexcept;
72 virtual message_result on_mouse_move(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
73 virtual message_result get(sys::state& state, Cyto::Any& payload) noexcept;
74 virtual message_result set(sys::state& state, Cyto::Any& payload) noexcept;
75 virtual void render(sys::state& state, int32_t x, int32_t y) noexcept { }
76 virtual void on_update(sys::state& state) noexcept;
77 virtual void on_create(sys::state& state) noexcept { } // called automatically after the element has been created by the system
78 virtual void on_drag(sys::state& state, int32_t oldx, int32_t oldy, int32_t x, int32_t y,
79 sys::key_modifiers mods) noexcept; // as drag events are generated
80 virtual void on_text(sys::state& state, char32_t ch) noexcept { }
81 virtual void on_visible(sys::state& state) noexcept { }
82 virtual void on_hide(sys::state& state) noexcept { }
83 virtual void on_reset_text(sys::state& state) noexcept { }
84
85 virtual focus_result on_get_focus(sys::state& state) noexcept { // used to both react to getting the focus and to accept or reject it
87 }
88 virtual void on_lose_focus(sys::state& state) noexcept { } // called when the focus is taken away
89 virtual void on_drag_finish(sys::state& state) noexcept { } // when the mouse is released, and drag ends
90private:
91 uint8_t get_pixel_opacity(sys::state& state, int32_t x, int32_t y, dcon::texture_id tid);
92
93public:
94 // these commands are meaningful only if the element has children
95 virtual std::unique_ptr<element_base> remove_child(element_base* child) noexcept {
96 return std::unique_ptr<element_base>{};
97 }
98 virtual void move_child_to_front(element_base* child) noexcept { }
99 virtual void move_child_to_back(element_base* child) noexcept { }
100 virtual void add_child_to_front(std::unique_ptr<element_base> child) noexcept {
101 std::abort();
102 }
103 virtual void add_child_to_back(std::unique_ptr<element_base> child) noexcept {
104 std::abort();
105 }
106 virtual element_base* get_child_by_name(sys::state const& state, std::string_view name) noexcept {
107 return nullptr;
108 }
109 virtual element_base* get_child_by_index(sys::state const& state, int32_t index) noexcept {
110 return nullptr;
111 }
112
113 virtual ~element_base() { }
114
115 friend std::unique_ptr<element_base> make_element(sys::state& state, std::string_view name);
116 friend std::unique_ptr<element_base> make_element_immediate(sys::state& state, dcon::gui_def_id id);
117 friend void sys::state::on_mouse_drag(int32_t x, int32_t y, sys::key_modifiers mod);
118 friend void sys::state::on_text(char32_t c);
119 friend void sys::state::on_drag_finished(int32_t x, int32_t y, key_modifiers mod);
120 template<typename T, typename ...Params>
121 friend std::unique_ptr<T> make_element_by_type(sys::state& state, dcon::gui_def_id id, Params&&... params);
122 template<typename T, typename ...Params>
123 friend std::unique_ptr<element_base> make_element_by_type(sys::state& state, std::string_view name, Params&&... params);
124};
125
126template<typename T>
128 if(parent) {
129 Cyto::Any payload = T{};
130 parent->impl_get(state, payload);
131 return any_cast<T>(payload);
132 } else {
133 return T{};
134 }
135}
136
137template<typename T>
138inline void send(sys::state& state, element_base* parent, T value) {
139 if(parent) {
140 Cyto::Any payload = value;
141 parent->impl_get(state, payload);
142 }
143}
144
145template<typename T>
146inline T send_and_retrieve(sys::state& state, element_base* parent, T value) {
147 if(parent) {
148 Cyto::Any payload = value;
149 parent->impl_get(state, payload);
150 return any_cast<T>(payload);
151 } else {
152 return T{};
153 }
154}
155
156void trigger_description(sys::state& state, text::layout_base& layout, dcon::trigger_key k, int32_t primary_slot = -1,
157 int32_t this_slot = -1, int32_t from_slot = -1);
158void multiplicative_value_modifier_description(sys::state& state, text::layout_base& layout, dcon::value_modifier_key modifier,
159 int32_t primary, int32_t this_slot, int32_t from_slot);
160void additive_value_modifier_description(sys::state& state, text::layout_base& layout, dcon::value_modifier_key modifier,
161 int32_t primary, int32_t this_slot, int32_t from_slot);
162void modifier_description(sys::state& state, text::layout_base& layout, dcon::modifier_id mid, int32_t indentation = 0);
163void active_modifiers_description(sys::state& state, text::layout_base& layout, dcon::nation_id n, int32_t identation,
164 dcon::national_modifier_value nmid, bool header);
165void active_modifiers_description(sys::state& state, text::layout_base& layout, dcon::province_id p, int32_t identation,
166 dcon::provincial_modifier_value nmid, bool have_header);
167void effect_description(sys::state& state, text::layout_base& layout, dcon::effect_key k, int32_t primary_slot, int32_t this_slot,
168 int32_t from_slot, uint32_t r_lo, uint32_t r_hi);
169void invention_description(sys::state& state, text::layout_base& contents, dcon::invention_id inv_id, int32_t indent) noexcept;
170void technology_description(sys::state& state, text::layout_base& contents, dcon::technology_id tech_id) noexcept;
171
172void reform_description(sys::state& state, text::columnar_layout& contents, dcon::issue_option_id ref);
173void reform_description(sys::state& state, text::columnar_layout& contents, dcon::reform_option_id ref);
175
176} // namespace ui
virtual message_result impl_set(sys::state &state, Cyto::Any &payload) noexcept
virtual tooltip_behavior has_tooltip(sys::state &state) noexcept
virtual void on_text(sys::state &state, char32_t ch) noexcept
element_base * parent
virtual void impl_on_reset_text(sys::state &state) noexcept
virtual message_result test_mouse(sys::state &state, int32_t x, int32_t y, mouse_probe_type type) noexcept
virtual void add_child_to_back(std::unique_ptr< element_base > child) noexcept
static constexpr uint8_t is_invisible_mask
virtual message_result on_lbutton_down(sys::state &state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept
virtual void update_tooltip(sys::state &state, int32_t x, int32_t y, text::columnar_layout &contents) noexcept
virtual void impl_on_drag_finish(sys::state &state) noexcept
friend std::unique_ptr< element_base > make_element_immediate(sys::state &state, dcon::gui_def_id id)
virtual void render(sys::state &state, int32_t x, int32_t y) noexcept
virtual message_result on_lbutton_up(sys::state &state, int32_t x, int32_t y, sys::key_modifiers mods, bool under_mouse) noexcept
virtual message_result impl_on_rbutton_down(sys::state &state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept
virtual message_result get(sys::state &state, Cyto::Any &payload) noexcept
virtual void on_lose_focus(sys::state &state) noexcept
virtual focus_result on_get_focus(sys::state &state) noexcept
virtual message_result impl_on_scroll(sys::state &state, int32_t x, int32_t y, float amount, sys::key_modifiers mods) noexcept
virtual element_base * get_child_by_name(sys::state const &state, std::string_view name) noexcept
virtual message_result set(sys::state &state, Cyto::Any &payload) noexcept
virtual element_base * get_child_by_index(sys::state const &state, int32_t index) noexcept
friend std::unique_ptr< T > make_element_by_type(sys::state &state, dcon::gui_def_id id, Params &&... params)
virtual void on_update(sys::state &state) noexcept
virtual void impl_on_update(sys::state &state) noexcept
virtual void impl_render(sys::state &state, int32_t x, int32_t y) noexcept
virtual void move_child_to_back(element_base *child) noexcept
bool is_visible() const
virtual message_result impl_on_key_down(sys::state &state, sys::virtual_key key, sys::key_modifiers mods) noexcept
virtual message_result impl_on_lbutton_up(sys::state &state, int32_t x, int32_t y, sys::key_modifiers mods, bool under_mouse) noexcept
virtual void add_child_to_front(std::unique_ptr< element_base > child) noexcept
virtual std::unique_ptr< element_base > remove_child(element_base *child) noexcept
virtual message_result on_scroll(sys::state &state, int32_t x, int32_t y, float amount, sys::key_modifiers mods) noexcept
virtual void on_drag_finish(sys::state &state) noexcept
friend std::unique_ptr< element_base > make_element(sys::state &state, std::string_view name)
virtual message_result impl_on_lbutton_down(sys::state &state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept
message_result impl_get(sys::state &state, Cyto::Any &payload) noexcept
virtual void on_visible(sys::state &state) noexcept
virtual message_result on_mouse_move(sys::state &state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept
virtual void on_create(sys::state &state) noexcept
virtual void move_child_to_front(element_base *child) noexcept
element_data base_data
virtual void on_reset_text(sys::state &state) noexcept
virtual void on_drag(sys::state &state, int32_t oldx, int32_t oldy, int32_t x, int32_t y, sys::key_modifiers mods) noexcept
void set_visible(sys::state &state, bool vis)
virtual message_result on_key_down(sys::state &state, sys::virtual_key key, sys::key_modifiers mods) noexcept
virtual message_result impl_on_mouse_move(sys::state &state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept
virtual mouse_probe impl_probe_mouse(sys::state &state, int32_t x, int32_t y, mouse_probe_type type) noexcept
virtual message_result on_rbutton_down(sys::state &state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept
virtual void on_hide(sys::state &state) noexcept
Definition: constants.hpp:4
virtual_key
Definition: constants.hpp:5
key_modifiers
Definition: constants.hpp:156
void modifier_description(sys::state &state, text::layout_base &layout, dcon::modifier_id mid, int32_t indentation=0)
void reform_description(sys::state &state, text::columnar_layout &contents, dcon::issue_option_id ref)
focus_result
T retrieve(sys::state &state, element_base *parent)
void multiplicative_value_modifier_description(sys::state &state, text::layout_base &layout, dcon::value_modifier_key modifier, int32_t primary, int32_t this_slot, int32_t from_slot)
void effect_description(sys::state &state, text::layout_base &layout, dcon::effect_key k, int32_t primary_slot, int32_t this_slot, int32_t from_slot, uint32_t r_lo, uint32_t r_hi)
T send_and_retrieve(sys::state &state, element_base *parent, T value)
void additive_value_modifier_description(sys::state &state, text::layout_base &layout, dcon::value_modifier_key modifier, int32_t primary, int32_t this_slot, int32_t from_slot)
tooltip_behavior
void send(sys::state &state, element_base *parent, T value)
void technology_description(sys::state &state, text::layout_base &contents, dcon::technology_id tech_id) noexcept
void trigger_description(sys::state &state, text::layout_base &layout, dcon::trigger_key k, int32_t primary_slot=-1, int32_t this_slot=-1, int32_t from_slot=-1)
message_result
void reform_rules_description(sys::state &state, text::columnar_layout &contents, uint32_t rules)
void invention_description(sys::state &state, text::layout_base &contents, dcon::invention_id inv_id, int32_t indent) noexcept
void active_modifiers_description(sys::state &state, text::layout_base &layout, dcon::nation_id n, int32_t identation, dcon::national_modifier_value nmid, bool header)
uint uint32_t
uchar uint8_t
void on_text(char32_t c)
void on_drag_finished(int32_t x, int32_t y, key_modifiers mod)
void on_mouse_drag(int32_t x, int32_t y, key_modifiers mod)