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};
96public:
97 void button_action(sys::state& state) noexcept override;
98};
99class diplomatic_message_topbar_entry_window : public listbox_row_element_base<diplomatic_message::message> {
100public:
102 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
103 if(name == "diplomessageicon_button") {
104 auto ptr = make_element_by_type<diplomatic_message_topbar_button>(state, id);
105 btn = ptr.get();
106 return ptr;
107 } else if(name == "flag") {
108 return make_element_by_type<diplomatic_message_topbar_flag_button>(state, id);
109 } else if(name == "messageicon_bg_overlay") {
110 return make_element_by_type<image_element_base>(state, id);
111 } else {
112 return nullptr;
113 }
114 }
115 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
116 if(payload.holds_type<dcon::nation_id>()) {
117 payload.emplace<dcon::nation_id>(content.from);
119 } else if(payload.holds_type<dcon::national_identity_id>()) {
120 payload.emplace<dcon::national_identity_id>(state.world.nation_get_identity_from_identity_holder(content.from));
122 }
124 }
125};
126
127class diplomatic_message_topbar_listbox : public overlapping_listbox_element_base<diplomatic_message_topbar_entry_window, diplomatic_message::message> {
128 std::string_view get_row_element_name() override {
129 return "alice_diplomessageicon_entry_window";
130 }
131public:
132 std::vector< diplomatic_message::message> messages;
133 void on_update(sys::state& state) noexcept override {
134 auto it = std::remove_if(messages.begin(), messages.end(), [&](auto& m) {
135 return m.when + diplomatic_message::expiration_in_days <= state.current_date
136 || !diplomatic_message::can_accept(state, m);
137 });
138 messages.erase(it, messages.end());
139
140 row_contents.clear();
141 for(auto m : messages) {
142 row_contents.push_back(m);
143 }
144 update(state);
145 }
146};
147
149 auto const m = retrieve<diplomatic_message::message>(state, parent);
150 //Invoke window
151 auto dpw = static_cast<ui::diplomacy_request_window*>(state.ui_state.request_window);
152 dpw->messages.push_back(m);
153 dpw->set_visible(state, true);
154 dpw->impl_on_update(state);
155 //Remove from listbox
156 auto dmtl = static_cast<diplomatic_message_topbar_listbox*>(state.ui_state.request_topbar_listbox);
157 auto it = std::remove_if(dmtl->messages.begin(), dmtl->messages.end(),
158 [&](auto& e) { return e.from == m.from && e.to == m.to && e.type == m.type && e.when == m.when; });
159 auto r = std::distance(it, dmtl->messages.end());
160 dmtl->messages.erase(it, dmtl->messages.end());
161 dmtl->impl_on_update(state);
162}
163
165 if(parent) {
166 auto win = static_cast<diplomatic_message_topbar_entry_window*>(parent);
167 if(win->btn)
168 win->btn->button_action(state);
169 }
170}
171
172}
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_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
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:1799
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:1880
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:794
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:1807
tooltip_behavior
message_result
element_base * request_window
element_base * request_topbar_listbox