Skip to content

Commit

Permalink
Implement Population Menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Hop311 committed Apr 15, 2024
1 parent 0cd9763 commit 4f83397
Show file tree
Hide file tree
Showing 7 changed files with 1,464 additions and 5 deletions.
7 changes: 7 additions & 0 deletions extension/src/openvic-extension/singletons/GameSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "openvic-extension/singletons/AssetManager.hpp"
#include "openvic-extension/singletons/LoadLocalisation.hpp"
#include "openvic-extension/singletons/MenuSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/Utilities.hpp"

Expand Down Expand Up @@ -115,13 +116,19 @@ Error GameSingleton::setup_game(int32_t bookmark_index) {
Bookmark const* bookmark = game_manager.get_history_manager().get_bookmark_manager().get_bookmark_by_index(bookmark_index);
ERR_FAIL_NULL_V_MSG(bookmark, FAILED, vformat("Failed to get bookmark with index: %d", bookmark_index));
bool ret = game_manager.load_bookmark(bookmark);

for (Province& province : game_manager.get_map().get_provinces()) {
province.set_crime(
game_manager.get_crime_manager().get_crime_modifier_by_index(
(province.get_index() - 1) % game_manager.get_crime_manager().get_crime_modifier_count()
)
);
}

MenuSingleton* menu_singleton = MenuSingleton::get_singleton();
ERR_FAIL_NULL_V(menu_singleton, FAILED);
menu_singleton->_population_menu_update_provinces();

return ERR(ret);
}

Expand Down
69 changes: 67 additions & 2 deletions extension/src/openvic-extension/singletons/MenuSingleton.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "MenuSingleton.hpp"

#include <godot_cpp/variant/utility_functions.hpp>

#include <openvic-simulation/GameManager.hpp>

#include "openvic-extension/classes/GFXPieChartTexture.hpp"
Expand All @@ -15,6 +13,19 @@ using namespace OpenVic;
using OpenVic::Utilities::std_to_godot_string;
using OpenVic::Utilities::std_view_to_godot_string;

StringName const& MenuSingleton::_signal_population_menu_province_list_changed() {
static const StringName signal_population_menu_province_list_changed = "population_menu_province_list_changed";
return signal_population_menu_province_list_changed;
}
StringName const& MenuSingleton::_signal_population_menu_province_list_selected_changed() {
static const StringName signal_population_menu_province_list_selected_changed = "population_menu_province_list_selected_changed";
return signal_population_menu_province_list_selected_changed;
}
StringName const& MenuSingleton::_signal_population_menu_pops_changed() {
static const StringName signal_population_menu_pops_changed = "population_menu_pops_changed";
return signal_population_menu_pops_changed;
}

void MenuSingleton::_bind_methods() {
/* PROVINCE OVERVIEW PANEL */
OV_BIND_METHOD(MenuSingleton::get_province_info_from_index, { "index" });
Expand All @@ -35,6 +46,60 @@ void MenuSingleton::_bind_methods() {
OV_BIND_METHOD(MenuSingleton::can_increase_speed);
OV_BIND_METHOD(MenuSingleton::can_decrease_speed);
OV_BIND_METHOD(MenuSingleton::get_longform_date);

/* POPULATION MENU */
OV_BIND_METHOD(MenuSingleton::get_population_menu_province_list_row_count);
OV_BIND_METHOD(MenuSingleton::get_population_menu_province_list_rows, { "start", "count" });
OV_BIND_METHOD(
MenuSingleton::population_menu_select_province_list_entry, { "select_index", "set_scroll_index" }, DEFVAL(false)
);
OV_BIND_METHOD(MenuSingleton::population_menu_select_province, { "province_index" });
OV_BIND_METHOD(MenuSingleton::population_menu_toggle_expanded, { "toggle_index", "emit_selected_changed" }, DEFVAL(true));

OV_BIND_METHOD(MenuSingleton::population_menu_select_sort_key, { "sort_key" });
OV_BIND_METHOD(MenuSingleton::get_population_menu_pop_rows, { "start", "count" });
OV_BIND_METHOD(MenuSingleton::get_population_menu_pop_row_count);

OV_BIND_METHOD(MenuSingleton::get_population_menu_pop_filter_setup_info);
OV_BIND_METHOD(MenuSingleton::get_population_menu_pop_filter_info);
OV_BIND_METHOD(MenuSingleton::population_menu_toggle_pop_filter, { "filter_index" });
OV_BIND_METHOD(MenuSingleton::population_menu_select_all_pop_filters);
OV_BIND_METHOD(MenuSingleton::population_menu_deselect_all_pop_filters);

OV_BIND_METHOD(MenuSingleton::get_population_menu_distribution_setup_info);
OV_BIND_METHOD(MenuSingleton::get_population_menu_distribution_info);

ADD_SIGNAL(MethodInfo(_signal_population_menu_province_list_changed()));
ADD_SIGNAL(
MethodInfo(_signal_population_menu_province_list_selected_changed(), PropertyInfo(Variant::INT, "scroll_index"))
);
ADD_SIGNAL(MethodInfo(_signal_population_menu_pops_changed()));

using enum population_menu_t::ProvinceListEntry;
BIND_ENUM_CONSTANT(LIST_ENTRY_NONE);
BIND_ENUM_CONSTANT(LIST_ENTRY_COUNTRY);
BIND_ENUM_CONSTANT(LIST_ENTRY_STATE);
BIND_ENUM_CONSTANT(LIST_ENTRY_PROVINCE);

using enum population_menu_t::PopSortKey;
BIND_ENUM_CONSTANT(NONE);
BIND_ENUM_CONSTANT(SORT_SIZE);
BIND_ENUM_CONSTANT(SORT_TYPE);
BIND_ENUM_CONSTANT(SORT_CULTURE);
BIND_ENUM_CONSTANT(SORT_RELIGION);
BIND_ENUM_CONSTANT(SORT_LOCATION);
BIND_ENUM_CONSTANT(SORT_MILITANCY);
BIND_ENUM_CONSTANT(SORT_CONSCIOUSNESS);
BIND_ENUM_CONSTANT(SORT_IDEOLOGY);
BIND_ENUM_CONSTANT(SORT_ISSUES);
BIND_ENUM_CONSTANT(SORT_UNEMPLOYMENT);
BIND_ENUM_CONSTANT(SORT_CASH);
BIND_ENUM_CONSTANT(SORT_LIFE_NEEDS);
BIND_ENUM_CONSTANT(SORT_EVERYDAY_NEEDS);
BIND_ENUM_CONSTANT(SORT_LUXURY_NEEDS);
BIND_ENUM_CONSTANT(SORT_REBEL_FACTION);
BIND_ENUM_CONSTANT(SORT_SIZE_CHANGE);
BIND_ENUM_CONSTANT(SORT_LITERACY);
}

MenuSingleton* MenuSingleton::get_singleton() {
Expand Down
96 changes: 96 additions & 0 deletions extension/src/openvic-extension/singletons/MenuSingleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#include <godot_cpp/classes/image.hpp>

#include <openvic-simulation/pop/Pop.hpp>
#include <openvic-simulation/types/OrderedContainers.hpp>

namespace OpenVic {
struct GameManager;
struct Region;

class MenuSingleton : public godot::Object {
GDCLASS(MenuSingleton, godot::Object)
Expand All @@ -14,6 +16,70 @@ namespace OpenVic {

GameManager* game_manager;

public:
struct population_menu_t {
enum ProvinceListEntry {
LIST_ENTRY_NONE, LIST_ENTRY_COUNTRY, LIST_ENTRY_STATE, LIST_ENTRY_PROVINCE
};

struct country_entry_t {
Country const& country;
bool selected = true;
};

struct state_entry_t {
// TODO - change to State
Region const& state;
bool selected = true, expanded = false;
};

struct province_entry_t {
Province const& province;
bool selected = true;
};

using province_list_entry_t = std::variant<country_entry_t, state_entry_t, province_entry_t>;

std::vector<province_list_entry_t> province_list_entries;
int32_t visible_province_list_entries = 0;

struct pop_filter_t {
Pop::pop_size_t count, promotion_demotion_change;
bool selected;
};
ordered_map<PopType const*, pop_filter_t> pop_filters;

static constexpr int32_t DISTRIBUTION_COUNT = 6;
/* Distributions:
* - Workforce (PopType)
* - Religion
* - Ideology
* - Nationality (Culture)
* - Issues
* - Vote */
std::array<fixed_point_map_t<HasIdentifierAndColour const*>, DISTRIBUTION_COUNT> distributions;

enum PopSortKey {
NONE, SORT_SIZE, SORT_TYPE, SORT_CULTURE, SORT_RELIGION, SORT_LOCATION, SORT_MILITANCY, SORT_CONSCIOUSNESS,
SORT_IDEOLOGY, SORT_ISSUES, SORT_UNEMPLOYMENT, SORT_CASH, SORT_LIFE_NEEDS, SORT_EVERYDAY_NEEDS,
SORT_LUXURY_NEEDS, SORT_REBEL_FACTION, SORT_SIZE_CHANGE, SORT_LITERACY, MAX_SORT_KEY
} sort_key = NONE;
bool sort_descending = true;

std::vector<Pop const*> pops, filtered_pops;
};

private:
population_menu_t population_menu;

/* Emitted when the number of visible province list rows changes (list generated or state entry expanded).*/
static godot::StringName const& _signal_population_menu_province_list_changed();
/* Emitted when the state of visible province list rows changes (selection changes). Provides an integer argument
* which, if not negative, the province list scroll index should be updated to. */
static godot::StringName const& _signal_population_menu_province_list_selected_changed();
/* Emitted when the selected/filtered collection of pops changes. */
static godot::StringName const& _signal_population_menu_pops_changed();

protected:
static void _bind_methods();

Expand Down Expand Up @@ -44,5 +110,35 @@ namespace OpenVic {
bool can_increase_speed() const;
bool can_decrease_speed() const;
godot::String get_longform_date() const;

/* POPULATION MENU */
void _population_menu_update_provinces();
int32_t get_population_menu_province_list_row_count() const;
godot::TypedArray<godot::Dictionary> get_population_menu_province_list_rows(int32_t start, int32_t count) const;
godot::Error population_menu_select_province_list_entry(int32_t select_index, bool set_scroll_index = false);
godot::Error population_menu_select_province(int32_t province_index);
godot::Error population_menu_toggle_expanded(int32_t toggle_index, bool emit_selected_changed = true);

void _population_menu_update_pops();
void _population_menu_update_filtered_pops();
using sort_func_t = std::function<bool(Pop const*, Pop const*)>;
sort_func_t _get_population_menu_sort_func(population_menu_t::PopSortKey sort_key) const;
void _population_menu_sort_pops();
godot::Error population_menu_select_sort_key(population_menu_t::PopSortKey sort_key);
godot::TypedArray<godot::Dictionary> get_population_menu_pop_rows(int32_t start, int32_t count) const;
int32_t get_population_menu_pop_row_count() const;

godot::PackedInt32Array get_population_menu_pop_filter_setup_info();
godot::TypedArray<godot::Dictionary> get_population_menu_pop_filter_info() const;
godot::Error population_menu_toggle_pop_filter(int32_t filter_index);
void population_menu_select_all_pop_filters();
void population_menu_deselect_all_pop_filters();

godot::PackedStringArray get_population_menu_distribution_setup_info() const;
/* Array of GFXPieChartTexture::godot_pie_chart_data_t. */
godot::TypedArray<godot::Array> get_population_menu_distribution_info() const;
};
}

VARIANT_ENUM_CAST(OpenVic::MenuSingleton::population_menu_t::ProvinceListEntry);
VARIANT_ENUM_CAST(OpenVic::MenuSingleton::population_menu_t::PopSortKey);
Loading

0 comments on commit 4f83397

Please sign in to comment.