Skip to content

Commit

Permalink
Merge pull request #179 from OpenVicProject/ui-work
Browse files Browse the repository at this point in the history
GUIOverlappingElementsBox + GUINode improvements
  • Loading branch information
Hop311 authored Dec 25, 2023
2 parents d26c990 + 4e9764e commit d114eca
Show file tree
Hide file tree
Showing 31 changed files with 943 additions and 603 deletions.
2 changes: 1 addition & 1 deletion extension/deps/openvic-simulation
Submodule openvic-simulation updated 43 files
+62 −73 src/openvic-simulation/GameManager.cpp
+1 −1 src/openvic-simulation/GameManager.hpp
+1 −5 src/openvic-simulation/country/Country.cpp
+23 −12 src/openvic-simulation/dataloader/Dataloader.cpp
+13 −9 src/openvic-simulation/dataloader/NodeTools.cpp
+3 −1 src/openvic-simulation/dataloader/NodeTools.hpp
+3 −2 src/openvic-simulation/economy/BuildingInstance.cpp
+29 −6 src/openvic-simulation/economy/BuildingType.cpp
+3 −2 src/openvic-simulation/economy/BuildingType.hpp
+8 −6 src/openvic-simulation/economy/Good.cpp
+5 −0 src/openvic-simulation/economy/ProductionType.cpp
+4 −4 src/openvic-simulation/history/DiplomaticHistory.cpp
+17 −3 src/openvic-simulation/interface/GFX.cpp
+21 −3 src/openvic-simulation/interface/GFX.hpp
+4 −2 src/openvic-simulation/interface/GUI.cpp
+1 −2 src/openvic-simulation/interface/GUI.hpp
+9 −3 src/openvic-simulation/interface/UI.cpp
+1 −1 src/openvic-simulation/interface/UI.hpp
+48 −39 src/openvic-simulation/map/Map.cpp
+12 −4 src/openvic-simulation/map/Map.hpp
+7 −8 src/openvic-simulation/map/Province.cpp
+1 −1 src/openvic-simulation/map/Province.hpp
+4 −2 src/openvic-simulation/map/Region.cpp
+4 −6 src/openvic-simulation/map/TerrainType.cpp
+2 −2 src/openvic-simulation/military/Unit.cpp
+9 −16 src/openvic-simulation/misc/Decision.cpp
+4 −6 src/openvic-simulation/misc/Decision.hpp
+10 −6 src/openvic-simulation/misc/Event.cpp
+5 −4 src/openvic-simulation/misc/Event.hpp
+52 −34 src/openvic-simulation/misc/Modifier.cpp
+4 −7 src/openvic-simulation/politics/Ideology.cpp
+3 −6 src/openvic-simulation/pop/Culture.cpp
+3 −6 src/openvic-simulation/pop/Pop.cpp
+4 −7 src/openvic-simulation/pop/Religion.cpp
+415 −26 src/openvic-simulation/types/Colour.hpp
+2 −2 src/openvic-simulation/types/Date.cpp
+1 −1 src/openvic-simulation/types/Date.hpp
+0 −29 src/openvic-simulation/types/IdentifierRegistry.cpp
+31 −18 src/openvic-simulation/types/IdentifierRegistry.hpp
+4 −4 src/openvic-simulation/utility/BMP.cpp
+7 −3 src/openvic-simulation/utility/BMP.hpp
+4 −1 src/openvic-simulation/utility/Getters.hpp
+14 −0 src/openvic-simulation/utility/Utility.hpp
28 changes: 0 additions & 28 deletions extension/src/openvic-extension/UIAdapter.hpp

This file was deleted.

9 changes: 3 additions & 6 deletions extension/src/openvic-extension/classes/GFXIconTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "openvic-extension/singletons/AssetManager.hpp"
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/UITools.hpp"
#include "openvic-extension/utility/Utilities.hpp"

using namespace godot;
Expand Down Expand Up @@ -71,12 +72,8 @@ Error GFXIconTexture::set_gfx_texture_sprite_name(String const& gfx_texture_spri
if (gfx_texture_sprite_name.is_empty()) {
return set_gfx_texture_sprite(nullptr);
}
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, FAILED);
GFX::Sprite const* sprite = game_singleton->get_game_manager().get_ui_manager().get_sprite_by_identifier(
godot_to_std_string(gfx_texture_sprite_name)
);
ERR_FAIL_NULL_V_MSG(sprite, FAILED, vformat("GFX sprite not found: %s", gfx_texture_sprite_name));
GFX::Sprite const* sprite = UITools::get_gfx_sprite(gfx_texture_sprite_name);
ERR_FAIL_NULL_V(sprite, FAILED);
GFX::TextureSprite const* new_texture_sprite = sprite->cast_to<GFX::TextureSprite>();
ERR_FAIL_NULL_V_MSG(
new_texture_sprite, FAILED, vformat(
Expand Down
4 changes: 2 additions & 2 deletions extension/src/openvic-extension/classes/GFXIconTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ namespace OpenVic {
);

/* Search for a GFX::TextureSprite with the specfied name and,
* if successful, call set_gfx_texture_sprite to set it and its icon */
* if successful, call set_gfx_texture_sprite to set it and its icon. */
godot::Error set_gfx_texture_sprite_name(
godot::String const& gfx_texture_sprite_name, GFX::frame_t icon = GFX::NO_FRAMES
);

/* Return the name of the GFX::TextureSprite, or an empty String if it's null */
/* Return the name of the GFX::TextureSprite, or an empty String if it's null. */
godot::String get_gfx_texture_sprite_name() const;

/* Set icon_index to a value between one and icon_count (inclusive), and update the AtlasTexture's region
Expand Down
31 changes: 23 additions & 8 deletions extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "GFXMaskedFlagTexture.hpp"

#include <godot_cpp/variant/utility_functions.hpp>

#include "openvic-extension/singletons/AssetManager.hpp"
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/UITools.hpp"
#include "openvic-extension/utility/Utilities.hpp"

using namespace godot;
Expand Down Expand Up @@ -66,6 +65,7 @@ void GFXMaskedFlagTexture::_bind_methods() {
OV_BIND_METHOD(GFXMaskedFlagTexture::get_gfx_masked_flag_name);

OV_BIND_METHOD(GFXMaskedFlagTexture::set_flag_country_name_and_type, { "new_flag_country_name", "new_flag_type" });
OV_BIND_METHOD(GFXMaskedFlagTexture::set_flag_country_name, { "new_flag_country_name" });
OV_BIND_METHOD(GFXMaskedFlagTexture::get_flag_country_name);
OV_BIND_METHOD(GFXMaskedFlagTexture::get_flag_type);
}
Expand Down Expand Up @@ -123,12 +123,8 @@ Error GFXMaskedFlagTexture::set_gfx_masked_flag_name(String const& gfx_masked_fl
if (gfx_masked_flag_name.is_empty()) {
return set_gfx_masked_flag(nullptr);
}
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, FAILED);
GFX::Sprite const* sprite = game_singleton->get_game_manager().get_ui_manager().get_sprite_by_identifier(
godot_to_std_string(gfx_masked_flag_name)
);
ERR_FAIL_NULL_V_MSG(sprite, FAILED, vformat("GFX sprite not found: %s", gfx_masked_flag_name));
GFX::Sprite const* sprite = UITools::get_gfx_sprite(gfx_masked_flag_name);
ERR_FAIL_NULL_V(sprite, FAILED);
GFX::MaskedFlag const* new_masked_flag = sprite->cast_to<GFX::MaskedFlag>();
ERR_FAIL_NULL_V_MSG(
new_masked_flag, FAILED, vformat(
Expand Down Expand Up @@ -158,6 +154,7 @@ Error GFXMaskedFlagTexture::set_flag_country_and_type(Country const* new_flag_co
flag_type = new_flag_type;
flag_image = new_flag_image;
} else {
// TODO - use REB flag as default/error flag
flag_country = nullptr;
flag_type = String {};
flag_image.unref();
Expand All @@ -178,6 +175,24 @@ Error GFXMaskedFlagTexture::set_flag_country_name_and_type(String const& new_fla
return set_flag_country_and_type(new_flag_country, new_flag_type);
}

Error GFXMaskedFlagTexture::set_flag_country(Country const* new_flag_country) {
// TODO - get country's current flag type from the game state
return set_flag_country_and_type( new_flag_country, {});
}

Error GFXMaskedFlagTexture::set_flag_country_name(String const& new_flag_country_name) {
if (new_flag_country_name.is_empty()) {
return set_flag_country(nullptr);
}
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, FAILED);
Country const* new_flag_country = game_singleton->get_game_manager().get_country_manager().get_country_by_identifier(
godot_to_std_string(new_flag_country_name)
);
ERR_FAIL_NULL_V_MSG(new_flag_country, FAILED, vformat("Country not found: %s", new_flag_country_name));
return set_flag_country(new_flag_country);
}

String GFXMaskedFlagTexture::get_flag_country_name() const {
return flag_country != nullptr ? std_view_to_godot_string(flag_country->get_identifier()) : String {};
}
11 changes: 9 additions & 2 deletions extension/src/openvic-extension/classes/GFXMaskedFlagTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace OpenVic {
/* Search for a GFX::MaskedFlag with the specfied name and, if successful, set it using set_gfx_masked_flag. */
godot::Error set_gfx_masked_flag_name(godot::String const& gfx_masked_flag_name);

/* Return the name of the GFX::MaskedFlag, or an empty String if it's null */
/* Return the name of the GFX::MaskedFlag, or an empty String if it's null. */
godot::String get_gfx_masked_flag_name() const;

/* Set flag_country and flag_type and update the combined image to use that flag, or no flag if it doesn't exist. */
Expand All @@ -49,7 +49,14 @@ namespace OpenVic {
godot::String const& new_flag_country_name, godot::StringName const& new_flag_type
);

/* Return the name of the selected flag's country, or an empty String if it's null */
/* Look up the specified country's current flag type, then call set_flag_country_and_type
* with the country and its flag type as arguments. */
godot::Error set_flag_country(Country const* new_flag_country);

/* Look up the country with the specified identifier, then call set_flag_country with the country its argument. */
godot::Error set_flag_country_name(godot::String const& new_flag_country_name);

/* Return the name of the selected flag's country, or an empty String if it's null. */
godot::String get_flag_country_name() const;
};
}
40 changes: 15 additions & 25 deletions extension/src/openvic-extension/classes/GFXPieChartTexture.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include "GFXPieChartTexture.hpp"

#include <godot_cpp/variant/utility_functions.hpp>

#include "openvic-extension/singletons/AssetManager.hpp"
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/Utilities.hpp"
#include "openvic-extension/utility/UITools.hpp"

using namespace godot;
using namespace OpenVic;
Expand All @@ -14,14 +12,14 @@ using OpenVic::Utilities::godot_to_std_string;
using OpenVic::Utilities::std_view_to_godot_string;
using OpenVic::Utilities::std_view_to_godot_string_name;

#define PI std::numbers::pi_v<float>
static constexpr float PI = std::numbers::pi_v<float>;

Error GFXPieChartTexture::_generate_pie_chart_image() {
ERR_FAIL_NULL_V(gfx_pie_chart, FAILED);
if (gfx_pie_chart->get_size() <= 0) {
UtilityFunctions::push_error("Invalid GFX::PieChart size for GFXPieChartTexture - ", gfx_pie_chart->get_size());
return FAILED;
}
ERR_FAIL_COND_V_MSG(
gfx_pie_chart->get_size() <= 0, FAILED,
vformat("Invalid GFX::PieChart size for GFXPieChartTexture - %d", gfx_pie_chart->get_size())
);
const int32_t pie_chart_size = 2 * gfx_pie_chart->get_size();
bool can_update = true;
if (
Expand Down Expand Up @@ -75,23 +73,19 @@ Error GFXPieChartTexture::_generate_pie_chart_image() {
return OK;
}

Error GFXPieChartTexture::set_slices(Array const& new_slices) {
Error GFXPieChartTexture::set_slices_array(TypedArray<Dictionary> const& new_slices) {
static const StringName colour_key = "colour";
static const StringName weight_key = "weight";

slices.clear();
total_weight = 0.0f;
for (int32_t i = 0; i < new_slices.size(); ++i) {
Dictionary const& slice_dict = new_slices[i];
if (!slice_dict.has(colour_key) || !slice_dict.has(weight_key)) {
UtilityFunctions::push_error("Invalid slice keys at index ", i, " - ", slice_dict);
continue;
}
ERR_CONTINUE_MSG(
!slice_dict.has(colour_key) || !slice_dict.has(weight_key), vformat("Invalid slice keys at index %d", i)
);
const slice_t slice = std::make_pair(slice_dict[colour_key], slice_dict[weight_key]);
if (slice.second <= 0.0f) {
UtilityFunctions::push_error("Invalid slice weight at index ", i, " - ", slice.second);
continue;
}
ERR_CONTINUE_MSG(slice.second <= 0.0f, vformat("Invalid slice values at index %d", i));
total_weight += slice.second;
slices.emplace_back(std::move(slice));
}
Expand All @@ -104,10 +98,10 @@ void GFXPieChartTexture::_bind_methods() {
OV_BIND_METHOD(GFXPieChartTexture::set_gfx_pie_chart_name, { "gfx_pie_chart_name" });
OV_BIND_METHOD(GFXPieChartTexture::get_gfx_pie_chart_name);

OV_BIND_METHOD(GFXPieChartTexture::set_slices, { "new_slices" });
OV_BIND_METHOD(GFXPieChartTexture::set_slices_array, { "new_slices" });
}

GFXPieChartTexture::GFXPieChartTexture() : total_weight { 0.0f } {}
GFXPieChartTexture::GFXPieChartTexture() : gfx_pie_chart { nullptr }, total_weight { 0.0f } {}

Ref<GFXPieChartTexture> GFXPieChartTexture::make_gfx_pie_chart_texture(GFX::PieChart const* gfx_pie_chart) {
Ref<GFXPieChartTexture> pie_chart_texture;
Expand Down Expand Up @@ -146,12 +140,8 @@ Error GFXPieChartTexture::set_gfx_pie_chart_name(String const& gfx_pie_chart_nam
if (gfx_pie_chart_name.is_empty()) {
return set_gfx_pie_chart(nullptr);
}
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, FAILED);
GFX::Sprite const* sprite = game_singleton->get_game_manager().get_ui_manager().get_sprite_by_identifier(
godot_to_std_string(gfx_pie_chart_name)
);
ERR_FAIL_NULL_V_MSG(sprite, FAILED, vformat("GFX sprite not found: %s", gfx_pie_chart_name));
GFX::Sprite const* sprite = UITools::get_gfx_sprite(gfx_pie_chart_name);
ERR_FAIL_NULL_V(sprite, FAILED);
GFX::PieChart const* new_pie_chart = sprite->cast_to<GFX::PieChart>();
ERR_FAIL_NULL_V_MSG(
new_pie_chart, FAILED, vformat(
Expand Down
41 changes: 36 additions & 5 deletions extension/src/openvic-extension/classes/GFXPieChartTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <openvic-simulation/interface/GFX.hpp>

#include "openvic-extension/utility/Utilities.hpp"

namespace OpenVic {
class GFXPieChartTexture : public godot::ImageTexture {
GDCLASS(GFXPieChartTexture, godot::ImageTexture)
Expand All @@ -23,11 +25,40 @@ namespace OpenVic {
public:
GFXPieChartTexture();

/* Set slices given new_slices, an Array of Dictionaries, each with the following keys:
/* Set slices given an Array of Dictionaries, each with the following key-value entries:
* - colour: Color
* - weight: float
*/
godot::Error set_slices(godot::Array const& new_slices);
* - weight: float */
godot::Error set_slices_array(godot::TypedArray<godot::Dictionary> const& new_slices);

/* Generate slice data from a distribution of HasIdentifierAndColour derived objects, sorted by their weight.
* The resulting Array of Dictionaries can be used as an argument for set_slices_array. */
template<std::derived_from<HasIdentifierAndColour> T>
static godot::TypedArray<godot::Dictionary> distribution_to_slices_array(fixed_point_map_t<T const*> const& dist) {
using entry_t = std::pair<T const*, fixed_point_t>;
std::vector<entry_t> sorted_dist;
sorted_dist.reserve(dist.size());
for (entry_t const& entry : dist) {
ERR_CONTINUE_MSG(
entry.first == nullptr, godot::vformat("Null distribution key with value %f", entry.second.to_float())
);
sorted_dist.push_back(entry);
}
std::sort(sorted_dist.begin(), sorted_dist.end(), [](entry_t const& lhs, entry_t const& rhs) -> bool {
return lhs.second < rhs.second;
});
static const godot::StringName identifier_key = "identifier";
static const godot::StringName colour_key = "colour";
static const godot::StringName weight_key = "weight";
godot::TypedArray<godot::Dictionary> array;
for (auto const& [key, val] : sorted_dist) {
godot::Dictionary sub_dict;
sub_dict[identifier_key] = Utilities::std_view_to_godot_string(key->get_identifier());
sub_dict[colour_key] = Utilities::to_godot_color(key->get_colour());
sub_dict[weight_key] = val.to_float();
array.push_back(sub_dict);
}
return array;
}

/* Create a GFXPieChartTexture using the specific GFX::PieChart.
* Returns nullptr if setting gfx_pie_chart fails. */
Expand All @@ -43,7 +74,7 @@ namespace OpenVic {
/* Search for a GFX::PieChart with the specfied name and, if successful, set it using set_gfx_pie_chart. */
godot::Error set_gfx_pie_chart_name(godot::String const& gfx_pie_chart_name);

/* Return the name of the GFX::PieChart, or an empty String if it's null */
/* Return the name of the GFX::PieChart, or an empty String if it's null. */
godot::String get_gfx_pie_chart_name() const;
};
}
Loading

0 comments on commit d114eca

Please sign in to comment.