Project Alice
Loading...
Searching...
No Matches
gui_diplomacy_request_topbar.hpp
Go to the documentation of this file.
1#pragma once
2
7
8namespace ui {
9
11 std::string_view get_type_key(diplomatic_message::type_t type) {
12 switch(type) {
14 return "???";
16 return "askmilitaryaccess_di";
18 return "alliance_di";
20 return "callally_di";
22 return "back_crisis_di";
24 return "back_crisis_di";
26 return "peace_di";
28 return "back_crisis_di";
30 return "crisis_offer_di";
32 return "state_transfer_di";
33 default:
34 return "???";
35 }
36
37 }
38public:
39 void on_update(sys::state& state) noexcept override {
40 auto const m = retrieve<diplomatic_message::message>(state, parent);
41 /*
42 requestmilaccess = 0,
43 offermilaccess = 1,
44 offeralliance = 2,
45 calltoarms = 3,
46 offerpeace = 4,
47 invitecrisis = 5,
48 */
49 switch(m.type) {
52 frame = 0;
53 break;
55 frame = 1;
56 break;
58 frame = 2;
59 break;
61 frame = 3;
62 break;
66 frame = 4;
67 break;
70 frame = 5;
71 break;
72 default:
73 break;
74 }
75 }
76
79 }
80
81 void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override {
82 auto m = retrieve<diplomatic_message::message>(state, parent);
83 auto box = text::open_layout_box(contents);
84 auto tstr = text::produce_simple_string(state, get_type_key(m.type));
88 text::add_to_substitution_map(sub, text::variable_type::type, std::string_view(tstr.c_str()));
89 text::localised_format_box(state, contents, box, std::string_view("diploicon_tip"), sub);
90 text::close_layout_box(contents, box);
91 }
92
93 void button_action(sys::state& state) noexcept override;
94
95 // Right click must remove message
96 void button_right_action(sys::state& state) noexcept override;
97};
98
100public:
101 void button_action(sys::state& state) noexcept override;
102 void button_right_action(sys::state& state) noexcept override;
103};
104
105class diplomatic_message_topbar_entry_window : public listbox_row_element_base<diplomatic_message::message> {
106public:
108 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
109 if(name == "diplomessageicon_button") {
110 auto ptr = make_element_by_type<diplomatic_message_topbar_button>(state, id);
111 btn = ptr.get();
112 return ptr;
113 } else if(name == "flag") {
114 return make_element_by_type<diplomatic_message_topbar_flag_button>(state, id);
115 } else if(name == "messageicon_bg_overlay") {
116 return make_element_by_type<image_element_base>(state, id);
117 } else {
118 return nullptr;
119 }
120 }
121 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
122 if(payload.holds_type<dcon::nation_id>()) {
123 payload.emplace<dcon::nation_id>(content.from);
125 } else if(payload.holds_type<dcon::national_identity_id>()) {
126 payload.emplace<dcon::national_identity_id>(state.world.nation_get_identity_from_identity_holder(content.from));
128 }
130 }
131};
132
133class diplomatic_message_topbar_listbox : public overlapping_listbox_element_base<diplomatic_message_topbar_entry_window, diplomatic_message::message> {
134 std::string_view get_row_element_name() override {
135 return "alice_diplomessageicon_entry_window";
136 }
137public:
138 std::vector< diplomatic_message::message> messages;
139 void on_update(sys::state& state) noexcept override {
140 auto it = std::remove_if(messages.begin(), messages.end(), [&](auto& m) {
141 return m.when + diplomatic_message::expiration_in_days <= state.current_date
142 || !diplomatic_message::can_accept(state, m);
143 });
144 messages.erase(it, messages.end());
145
146 row_contents.clear();
147 for(auto m : messages) {
148 row_contents.push_back(m);
149 }
150 update(state);
151 }
152};
153
155 auto const m = retrieve<diplomatic_message::message>(state, parent);
156 //Invoke window
157 auto dpw = static_cast<ui::diplomacy_request_window*>(state.ui_state.request_window);
158 dpw->messages.push_back(m);
159 dpw->set_visible(state, true);
160 dpw->impl_on_update(state);
161 //Remove from listbox
162 auto dmtl = static_cast<diplomatic_message_topbar_listbox*>(state.ui_state.request_topbar_listbox);
163 auto it = std::remove_if(dmtl->messages.begin(), dmtl->messages.end(),
164 [&](auto& e) { return e.from == m.from && e.to == m.to && e.type == m.type && e.when == m.when; });
165 auto r = std::distance(it, dmtl->messages.end());
166 dmtl->messages.erase(it, dmtl->messages.end());
167 dmtl->impl_on_update(state);
168};
169
171 auto const m = retrieve<diplomatic_message::message>(state, parent);
172 //Remove from listbox
173 auto dmtl = static_cast<diplomatic_message_topbar_listbox*>(state.ui_state.request_topbar_listbox);
174 auto it = std::remove_if(dmtl->messages.begin(), dmtl->messages.end(),
175 [&](auto& e) { return e.from == m.from && e.to == m.to && e.type == m.type && e.when == m.when; });
176 auto r = std::distance(it, dmtl->messages.end());
177 dmtl->messages.erase(it, dmtl->messages.end());
178 dmtl->impl_on_update(state);
179};
180
182 if(parent) {
183 auto win = static_cast<diplomatic_message_topbar_entry_window*>(parent);
184 if(win->btn)
185 win->btn->button_action(state);
186 }
187};
188
190 if(parent) {
191 auto win = static_cast<diplomatic_message_topbar_entry_window*>(parent);
192 if(win->btn)
194 }
195};
196
197}
std::vector< diplomatic_message::message > messages
void update_tooltip(sys::state &state, int32_t x, int32_t y, text::columnar_layout &contents) noexcept override
void button_right_action(sys::state &state) noexcept override
void button_action(sys::state &state) noexcept override
tooltip_behavior has_tooltip(sys::state &state) noexcept override
void on_update(sys::state &state) noexcept override
diplomatic_message_topbar_button * btn
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
std::unique_ptr< element_base > make_child(sys::state &state, std::string_view name, dcon::gui_def_id id) noexcept override
void button_action(sys::state &state) noexcept override
void button_right_action(sys::state &state) noexcept override
std::vector< diplomatic_message::message > messages
void on_update(sys::state &state) noexcept override
element_base * parent
virtual message_result get(sys::state &state, Cyto::Any &payload) noexcept
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
constexpr int32_t expiration_in_days
layout_box open_layout_box(layout_base &dest, int32_t indent)
Definition: text.cpp:1823
void localised_format_box(sys::state &state, layout_base &dest, layout_box &box, std::string_view key, text::substitution_map const &sub)
Definition: text.cpp:1904
void add_to_substitution_map(substitution_map &mp, variable_type key, substitution value)
Definition: text.cpp:1068
ankerl::unordered_dense::map< uint32_t, substitution > substitution_map
Definition: text.hpp:797
std::string produce_simple_string(sys::state const &state, dcon::text_key id)
Definition: text.cpp:617
void close_layout_box(columnar_layout &dest, layout_box &box)
Definition: text.cpp:1831
tooltip_behavior
message_result
Holds important data about the game world, state, and other data regarding windowing,...
element_base * request_window
element_base * request_topbar_listbox