Skip to content

Commit

Permalink
Merge pull request #599 from wxwisiasdf/wjerjwenfjosdnof
Browse files Browse the repository at this point in the history
highlight desired factory types for the player
  • Loading branch information
schombert authored Oct 28, 2023
2 parents f9860ec + 0854406 commit 1fa1ae0
Showing 1 changed file with 79 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "gui_element_types.hpp"
#include "gui_production_enum.hpp"
#include "ai.hpp"

namespace ui {

Expand Down Expand Up @@ -88,12 +89,8 @@ class factory_build_cost_text : public simple_text_element_base {
}

void on_update(sys::state& state) noexcept override {
if(parent) {
Cyto::Any payload = dcon::factory_type_id{};
parent->impl_get(state, payload);
auto content = any_cast<dcon::factory_type_id>(payload);
set_text(state, get_text(state, content));
}
auto content = retrieve<dcon::factory_type_id>(state, parent);
set_text(state, get_text(state, content));
}
};

Expand All @@ -106,46 +103,63 @@ class factory_build_time_text : public simple_text_element_base {
}

void on_update(sys::state& state) noexcept override {
if(parent) {
Cyto::Any payload = dcon::factory_type_id{};
parent->impl_get(state, payload);
auto content = any_cast<dcon::factory_type_id>(payload);
set_text(state, get_text(state, content));
}
auto content = retrieve<dcon::factory_type_id>(state, parent);
set_text(state, get_text(state, content));
}
};

class factory_build_item_button : public button_element_base {
bool visible = false;
public:
void on_update(sys::state& state) noexcept override {
if(parent) {
Cyto::Any sidload = dcon::state_instance_id{};
parent->impl_get(state, sidload);
auto sid = any_cast<dcon::state_instance_id>(sidload);
Cyto::Any payload = dcon::factory_type_id{};
parent->impl_get(state, payload);
auto content = any_cast<dcon::factory_type_id>(payload);

disabled = !command::can_begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
}
auto sid = retrieve<dcon::state_instance_id>(state, parent);
auto content = retrieve<dcon::factory_type_id>(state, parent);
disabled = !command::can_begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
visible = !retrieve<bool>(state, parent);
}

void button_action(sys::state& state) noexcept override {
if(parent) {
Cyto::Any payload = dcon::factory_type_id{};
parent->impl_get(state, payload);
auto content = any_cast<dcon::factory_type_id>(payload);
auto content = retrieve<dcon::factory_type_id>(state, parent);
send(state, parent, element_selection_wrapper<dcon::factory_type_id>{content});
}

Cyto::Any payload2 = element_selection_wrapper<dcon::factory_type_id>{content};
parent->impl_get(state, payload2);
}
void render(sys::state& state, int32_t x, int32_t y) noexcept override {
if(visible)
button_element_base::render(state, x, y);
}
};

class factory_build_item_highlight_image : public tinted_image_element_base {
bool visible = false;
public:
void on_create(sys::state& state) noexcept override {
tinted_image_element_base::on_create(state);
color = sys::pack_color(196, 255, 196);
}

void on_update(sys::state& state) noexcept override {
auto sid = retrieve<dcon::state_instance_id>(state, parent);
auto content = retrieve<dcon::factory_type_id>(state, parent);
visible = retrieve<bool>(state, parent);
}

void render(sys::state& state, int32_t x, int32_t y) noexcept override {
if(visible)
tinted_image_element_base::render(state, x, y);
}
};

class factory_build_item : public listbox_row_element_base<dcon::factory_type_id> {
std::vector<dcon::factory_type_id> desired_types;
public:
std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
if(name == "bg") {
auto hl_elm = make_element_by_type<factory_build_item_highlight_image>(state, id);
hl_elm->base_data.size.x *= 2; // Nudge
hl_elm->base_data.size.x += 42; // Nudge
hl_elm->base_data.size.y += 5; // Nudge
add_child_to_back(std::move(hl_elm));

auto ptr = make_element_by_type<factory_build_item_button>(state, id);
ptr->base_data.size.x *= 2; // Nudge
ptr->base_data.size.x += 42; // Nudge
Expand All @@ -169,41 +183,60 @@ class factory_build_item : public listbox_row_element_base<dcon::factory_type_id
}
}

void on_update(sys::state& state) noexcept override {
desired_types.clear();
ai::get_desired_factory_types(state, state.local_player_nation, desired_types);
}

message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
if(payload.holds_type<dcon::commodity_id>()) {
payload.emplace<dcon::commodity_id>(dcon::fatten(state.world, content).get_output().id);
return message_result::consumed;
} else if(payload.holds_type<bool>()) {
auto sid = retrieve<dcon::state_instance_id>(state, parent);
bool is_hl = std::find(desired_types.begin(), desired_types.end(), content) != desired_types.end();
is_hl = is_hl && command::can_begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
payload.emplace<bool>(is_hl);
return message_result::consumed;
}
return listbox_row_element_base<dcon::factory_type_id>::get(state, payload);
}
};

class factory_build_list : public listbox_element_base<factory_build_item, dcon::factory_type_id> {
std::vector<dcon::factory_type_id> desired_types;
bool is_highlighted(sys::state& state, dcon::state_instance_id sid, dcon::factory_type_id ftid) {
bool is_hl = std::find(desired_types.begin(), desired_types.end(), ftid) != desired_types.end();
return is_hl && command::can_begin_factory_building_construction(state, state.local_player_nation, sid, ftid, false);
}
protected:
std::string_view get_row_element_name() override {
return "new_factory_option";
}

public:
void on_update(sys::state& state) noexcept override {
if(parent) {
Cyto::Any s_payload = dcon::state_instance_id{};
parent->impl_get(state, s_payload);
auto sid = any_cast<dcon::state_instance_id>(s_payload);

row_contents.clear();
// First the buildable factories
state.world.for_each_factory_type([&](dcon::factory_type_id ftid) {
if(command::can_begin_factory_building_construction(state, state.local_player_nation, sid, ftid, false))
row_contents.push_back(ftid);
});
// Then the ones that can't be built
state.world.for_each_factory_type([&](dcon::factory_type_id ftid) {
if(!command::can_begin_factory_building_construction(state, state.local_player_nation, sid, ftid, false))
row_contents.push_back(ftid);
});
update(state);
auto sid = retrieve<dcon::state_instance_id>(state, parent);
row_contents.clear();
desired_types.clear();
ai::get_desired_factory_types(state, state.local_player_nation, desired_types);
// First the desired factory types
for(const auto ftid : desired_types)
if(is_highlighted(state, sid, ftid))
row_contents.push_back(ftid);
// Then the buildable factories
for(const auto ftid : state.world.in_factory_type) {
if(command::can_begin_factory_building_construction(state, state.local_player_nation, sid, ftid, false) && std::find(desired_types.begin(), desired_types.end(), ftid) == desired_types.end()) {
row_contents.push_back(ftid);
}
}
// Then the ones that can't be built
for(const auto ftid : state.world.in_factory_type) {
if(!command::can_begin_factory_building_construction(state, state.local_player_nation, sid, ftid, false) && std::find(desired_types.begin(), desired_types.end(), ftid) == desired_types.end()) {
row_contents.push_back(ftid);
}
}
update(state);
}
};

Expand Down

0 comments on commit 1fa1ae0

Please sign in to comment.