Project Alice
Loading...
Searching...
No Matches
gui_search_window.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "dcon_generated.hpp"
5#include "gui_graphics.hpp"
7#include "province.hpp"
8#include "text.hpp"
9#include <algorithm>
10
11namespace ui {
12
14public:
15 void edit_box_update(sys::state& state, std::string_view s) noexcept override {
16 send(state, parent, s);
17 }
18};
19
20class province_search_list_item : public listbox_row_button_base<dcon::province_id> {
21public:
22 void button_action(sys::state& state) noexcept override {
23 auto map_prov_id = content;
24 if(map_prov_id && map_prov_id.value < state.province_definitions.first_sea_province.value) {
25 state.map_state.set_selected_province(map_prov_id);
26 static_cast<province_view_window*>(state.ui_state.province_window)->set_active_province(state, map_prov_id);
27 state.map_state.center_map_on_province(state, map_prov_id);
28 }
29 }
30
31 void update(sys::state& state) noexcept override {
32 dcon::province_fat_id fat_id = dcon::fatten(state.world, content);
33 auto name = text::produce_simple_string(state, fat_id.get_name());
35 }
36};
37
38class province_search_list : public listbox_element_base<province_search_list_item, dcon::province_id> {
39protected:
40 std::string_view get_row_element_name() override {
41 return "goto_location_button";
42 }
43};
44
46private:
47 province_search_list* search_listbox = nullptr;
48 province_search_edit* edit_box = nullptr;
49
50 std::vector<dcon::province_id> search_provinces(sys::state& state, std::string_view search_term) noexcept {
51 std::vector<dcon::province_id> results{};
52 std::string search_term_lower = parsers::lowercase_str(search_term);
53
54 if(!search_term.empty()) {
55 state.world.for_each_province([&](dcon::province_id prov_id) {
56 dcon::province_fat_id fat_id = dcon::fatten(state.world, prov_id);
58 if(name.starts_with(search_term_lower)) {
59 results.push_back(prov_id);
60 }
61 });
62 }
63
64 return results;
65 }
66
67public:
68 void on_create(sys::state& state) noexcept override {
70 move_child_to_front(search_listbox);
71 state.ui_state.edit_target = edit_box;
72 }
73
74 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
75 if(name == "cancel") {
76 return make_element_by_type<generic_close_button>(state, id);
77 } else if(name == "goto_edit") {
78 auto ptr = make_element_by_type<province_search_edit>(state, id);
79 edit_box = ptr.get();
80 return ptr;
81 } else if(name == "provinces") {
82 auto ptr = make_element_by_type<province_search_list>(state, id);
83 search_listbox = ptr.get();
84 return ptr;
85 } else {
86 return nullptr;
87 }
88 }
89
90 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
91 if(payload.holds_type<std::string_view>()) {
92 auto search_term = any_cast<std::string_view>(payload);
93 search_listbox->row_contents = search_provinces(state, search_term);
94 search_listbox->update(state);
96 } else {
98 }
99 }
100
101 void on_visible(sys::state& state) noexcept override {
102 state.ui_state.edit_target = edit_box;
103 }
104
105 void on_hide(sys::state& state) noexcept override {
106 state.ui_state.edit_target = nullptr;
107 }
108};
109
110} // namespace ui
void set_button_text(sys::state &state, std::string const &new_text)
void move_child_to_front(element_base *child) noexcept final
element_base * parent
virtual message_result get(sys::state &state, Cyto::Any &payload) noexcept
std::vector< RowConT > row_contents
void update(sys::state &state)
void edit_box_update(sys::state &state, std::string_view s) noexcept override
void update(sys::state &state) noexcept override
void button_action(sys::state &state) noexcept override
std::string_view get_row_element_name() override
void on_visible(sys::state &state) noexcept override
std::unique_ptr< element_base > make_child(sys::state &state, std::string_view name, dcon::gui_def_id id) noexcept override
void on_hide(sys::state &state) noexcept override
void on_create(sys::state &state) noexcept override
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
void on_create(sys::state &state) noexcept override
pop_satisfaction_wrapper_fat fatten(data_container const &c, pop_satisfaction_wrapper_id id) noexcept
std::string lowercase_str(std::string_view sv)
std::string produce_simple_string(sys::state const &state, dcon::text_key id)
Definition: text.cpp:617
void send(sys::state &state, element_base *parent, T value)
message_result
element_base * edit_target
element_base * province_window