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 static constexpr uint8_t wants_update_when_hidden_mask = 0x02;
19
21 element_base* parent = nullptr;
23
24 bool is_visible() const {
25 return (flags & is_invisible_mask) == 0;
26 }
27 void set_visible(sys::state& state, bool vis) {
28 auto old_visibility = is_visible();
30 if(vis && !old_visibility) {
34 } else if(!vis && old_visibility) {
36 }
37 }
38
40
41 // impl members: to be overridden only for the very basic container / not a container distinction
42 // - are responsible for propagating messages and responses
43 // - should be called in general when something happens
44 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
45 virtual message_result impl_on_lbutton_down(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
46 virtual message_result impl_on_lbutton_up(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods, bool under_mouse) noexcept;
47 virtual message_result impl_on_rbutton_down(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
49 virtual message_result impl_on_scroll(sys::state& state, int32_t x, int32_t y, float amount, sys::key_modifiers mods) noexcept;
50 virtual message_result impl_on_mouse_move(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
51 virtual void impl_on_update(sys::state& state) noexcept;
53 virtual void* get_by_name(sys::state& state, std::string_view name) noexcept {
54 return nullptr;
55 }
56 virtual message_result impl_set(sys::state& state, Cyto::Any& payload) noexcept;
57 virtual void impl_render(sys::state& state, int32_t x, int32_t y) noexcept;
58 virtual void impl_on_reset_text(sys::state& state) noexcept;
59 virtual void impl_on_drag_finish(sys::state& state) noexcept {
61 }
62
63 virtual tooltip_behavior has_tooltip(sys::state& state) noexcept { // used to test whether a tooltip is possible
65 }
66 virtual void tooltip_position(sys::state& state, int32_t x, int32_t y, int32_t& ident, urect& subrect) noexcept {
67 ident = 0;
68 subrect.top_left = ui::get_absolute_location(state, *this);
69 subrect.size = base_data.size;
70 }
71 virtual void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept { }
72
73 virtual void on_hover(sys::state& state) noexcept { } // when the mouse first moves over the element
74 virtual void on_hover_end(sys::state& state) noexcept { } // when the mouse is no longer over the element
75
76 // these message handlers can be overridden by basically anyone
77 // - generally *should not* be called directly
78protected:
79 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
80 virtual message_result on_lbutton_down(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
81 virtual message_result on_lbutton_up(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods, bool under_mouse) noexcept;
82 virtual message_result on_rbutton_down(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
84 virtual message_result on_scroll(sys::state& state, int32_t x, int32_t y, float amount, sys::key_modifiers mods) noexcept;
85 virtual message_result on_mouse_move(sys::state& state, int32_t x, int32_t y, sys::key_modifiers mods) noexcept;
86 virtual message_result get(sys::state& state, Cyto::Any& payload) noexcept;
87 virtual message_result set(sys::state& state, Cyto::Any& payload) noexcept;
88 virtual void render(sys::state& state, int32_t x, int32_t y) noexcept { }
89 virtual void on_update(sys::state& state) noexcept;
90 virtual void on_create(sys::state& state) noexcept { } // called automatically after the element has been created by the system
91 virtual void on_drag(sys::state& state, int32_t oldx, int32_t oldy, int32_t x, int32_t y,
92 sys::key_modifiers mods) noexcept; // as drag events are generated
93 virtual void on_text(sys::state& state, char32_t ch) noexcept { }
94 virtual void on_visible(sys::state& state) noexcept { }
95 virtual void on_hide(sys::state& state) noexcept { }
96 virtual void on_reset_text(sys::state& state) noexcept { }
97
98 virtual focus_result on_get_focus(sys::state& state) noexcept { // used to both react to getting the focus and to accept or reject it
100 }
101 virtual void on_lose_focus(sys::state& state) noexcept { } // called when the focus is taken away
102 virtual void on_drag_finish(sys::state& state) noexcept { } // when the mouse is released, and drag ends
103private:
104 uint8_t get_pixel_opacity(sys::state& state, int32_t x, int32_t y, dcon::texture_id tid);
105
106public:
107 // these commands are meaningful only if the element has children
108 virtual std::unique_ptr<element_base> remove_child(element_base* child) noexcept {
109 return std::unique_ptr<element_base>{};
110 }
111 virtual void move_child_to_front(element_base* child) noexcept { }
112 virtual void move_child_to_back(element_base* child) noexcept { }
113 virtual void add_child_to_front(std::unique_ptr<element_base> child) noexcept {
114 std::abort();
115 }
116 virtual void add_child_to_back(std::unique_ptr<element_base> child) noexcept {
117 std::abort();
118 }
119 virtual element_base* get_child_by_name(sys::state const& state, std::string_view name) noexcept {
120 return nullptr;
121 }
122 virtual element_base* get_child_by_index(sys::state const& state, int32_t index) noexcept {
123 return nullptr;
124 }
125
126 virtual ~element_base() { }
127
128 friend std::unique_ptr<element_base> make_element(sys::state& state, std::string_view name);
129 friend std::unique_ptr<element_base> make_element_immediate(sys::state& state, dcon::gui_def_id id);
130 friend void sys::state::on_mouse_drag(int32_t x, int32_t y, sys::key_modifiers mod);
131 friend void sys::state::on_text(char32_t c);
132 friend void sys::state::on_drag_finished(int32_t x, int32_t y, key_modifiers mod);
133 template<typename T, typename ...Params>
134 friend std::unique_ptr<T> make_element_by_type(sys::state& state, dcon::gui_def_id id, Params&&... params);
135 template<typename T, typename ...Params>
136 friend std::unique_ptr<element_base> make_element_by_type(sys::state& state, std::string_view name, Params&&... params);
137};
138
139template<typename T>
141 if(parent) {
142 Cyto::Any payload = T{};
143 parent->impl_get(state, payload);
144 return any_cast<T>(payload);
145 } else {
146 return T{};
147 }
148}
149
150template<typename T>
151inline void send(sys::state& state, element_base* parent, T value) {
152 if(parent) {
153 Cyto::Any payload = value;
154 parent->impl_get(state, payload);
155 }
156}
157
158template<typename T>
159inline T send_and_retrieve(sys::state& state, element_base* parent, T value) {
160 if(parent) {
161 Cyto::Any payload = value;
162 parent->impl_get(state, payload);
163 return any_cast<T>(payload);
164 } else {
165 return T{};
166 }
167}
168
169void trigger_description(sys::state& state, text::layout_base& layout, dcon::trigger_key k, int32_t primary_slot = -1,
170 int32_t this_slot = -1, int32_t from_slot = -1);
171void multiplicative_value_modifier_description(sys::state& state, text::layout_base& layout, dcon::value_modifier_key modifier,
172 int32_t primary, int32_t this_slot, int32_t from_slot);
173void additive_value_modifier_description(sys::state& state, text::layout_base& layout, dcon::value_modifier_key modifier,
174 int32_t primary, int32_t this_slot, int32_t from_slot);
175void modifier_description(sys::state& state, text::layout_base& layout, dcon::modifier_id mid, int32_t indentation = 0);
176void active_modifiers_description(sys::state& state, text::layout_base& layout, dcon::nation_id n, int32_t identation,
177 dcon::national_modifier_value nmid, bool header);
178void active_modifiers_description(sys::state& state, text::layout_base& layout, dcon::province_id p, int32_t identation,
179 dcon::provincial_modifier_value nmid, bool have_header);
180void effect_description(sys::state& state, text::layout_base& layout, dcon::effect_key k, int32_t primary_slot, int32_t this_slot,
181 int32_t from_slot, uint32_t r_lo, uint32_t r_hi);
182void invention_description(sys::state& state, text::layout_base& contents, dcon::invention_id inv_id, int32_t indent) noexcept;
183void technology_description(sys::state& state, text::layout_base& contents, dcon::technology_id tech_id) noexcept;
184
185void reform_description(sys::state& state, text::columnar_layout& contents, dcon::issue_option_id ref);
186void reform_description(sys::state& state, text::columnar_layout& contents, dcon::reform_option_id ref);
188
189} // 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_hover_end(sys::state &state) noexcept
virtual void on_lose_focus(sys::state &state) noexcept
virtual focus_result on_get_focus(sys::state &state) noexcept
virtual void tooltip_position(sys::state &state, int32_t x, int32_t y, int32_t &ident, urect &subrect) noexcept
virtual void * get_by_name(sys::state &state, std::string_view name) 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
static constexpr uint8_t wants_update_when_hidden_mask
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 void on_hover(sys::state &state) 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)
xy_pair get_absolute_location(sys::state &state, element_base const &node)
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
@ ident
Holds important data about the game world, state, and other data regarding windowing,...
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)