From bc0b3c61ae0b742da304cada451fba1df72bb0ad Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 8 Nov 2023 22:24:21 +0000 Subject: GUI elements -> Godot UI nodes generator --- .../src/openvic-extension/utility/Utilities.hpp | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 extension/src/openvic-extension/utility/Utilities.hpp (limited to 'extension/src/openvic-extension/utility/Utilities.hpp') diff --git a/extension/src/openvic-extension/utility/Utilities.hpp b/extension/src/openvic-extension/utility/Utilities.hpp new file mode 100644 index 0000000..fc106db --- /dev/null +++ b/extension/src/openvic-extension/utility/Utilities.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include +#include + +#include +#include + +#define ERR(x) ((x) ? OK : FAILED) + +namespace OpenVic::Utilities { + + inline std::string godot_to_std_string(godot::String const& str) { + return str.ascii().get_data(); + } + + inline godot::String std_to_godot_string(std::string const& str) { + return str.c_str(); + } + + inline godot::String std_view_to_godot_string(std::string_view str) { + return std_to_godot_string(static_cast(str)); + } + + inline godot::StringName std_to_godot_string_name(std::string const& str) { + return str.c_str(); + } + + inline godot::StringName std_view_to_godot_string_name(std::string_view str) { + return std_to_godot_string_name(static_cast(str)); + } + + inline godot::Color to_godot_color(colour_t colour) { + return { + colour_byte_to_float((colour >> 16) & 0xFF), + colour_byte_to_float((colour >> 8) & 0xFF), + colour_byte_to_float(colour & 0xFF) + }; + } + + inline godot::Vector2i to_godot_ivec2(ivec2_t vec) { + return { vec.x, vec.y }; + } + + godot::Ref load_godot_image(godot::String const& path); + + godot::Ref load_godot_font(godot::String const& fnt_path, godot::Ref const& image); + + void draw_pie_chart( + godot::Ref image, godot::Array const& stopAngles, godot::Array const& colours, float radius, + godot::Vector2 shadow_displacement, float shadow_tightness, float shadow_radius, float shadow_thickness, + godot::Color trim_colour, float trim_size, float gradient_falloff, float gradient_base, bool donut, + bool donut_inner_trim, float donut_inner_radius + ); +} -- cgit v1.2.3-56-ga3b1 From 3490e740a7235dce236fc2ce973e5cbf64d6e727 Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 15 Nov 2023 23:54:57 +0000 Subject: Striped mapmodes --- extension/deps/openvic-simulation | 2 +- extension/src/openvic-extension/UIAdapter.cpp | 8 ++-- .../openvic-extension/singletons/GameSingleton.cpp | 49 ++++++++++++---------- .../openvic-extension/singletons/GameSingleton.hpp | 8 ++-- .../src/openvic-extension/utility/Utilities.hpp | 4 ++ game/localisation/en_GB/mapmodes.csv | 1 + .../Game/GameSession/MapControlPanel/Minimap.gd | 2 +- game/src/Game/GameSession/MapView.gd | 5 ++- .../GameSession/ProvinceIndexSampler.gdshaderinc | 6 +++ game/src/Game/GameSession/TerrainMap.gdshader | 39 +++++++++++------ game/src/Game/GlobalClass/ShaderManager.gd | 9 ++++ 11 files changed, 87 insertions(+), 46 deletions(-) (limited to 'extension/src/openvic-extension/utility/Utilities.hpp') diff --git a/extension/deps/openvic-simulation b/extension/deps/openvic-simulation index f572664..2607fd6 160000 --- a/extension/deps/openvic-simulation +++ b/extension/deps/openvic-simulation @@ -1 +1 @@ -Subproject commit f572664cbe1aa5ec2cb6907de3083f058c20af7e +Subproject commit 2607fd63b849788e92174161a9cea72141c88f7a diff --git a/extension/src/openvic-extension/UIAdapter.cpp b/extension/src/openvic-extension/UIAdapter.cpp index 5b889d6..d688754 100644 --- a/extension/src/openvic-extension/UIAdapter.cpp +++ b/extension/src/openvic-extension/UIAdapter.cpp @@ -65,7 +65,7 @@ static T* new_control(GUI::Element const& element) { UtilityFunctions::push_error("Invalid orientation for GUI element ", std_view_to_godot_string(element.get_name())); } - node->set_position(Utilities::to_godot_ivec2(element.get_position())); + node->set_position(Utilities::to_godot_fvec2(element.get_position())); node->set_focus_mode(Control::FOCUS_NONE); return node; @@ -343,7 +343,7 @@ bool GodotGUIBuilder::generate_overlapping_elements( return false; } - godot_rect->set_size(Utilities::to_godot_ivec2(overlapping_elements.get_size())); + godot_rect->set_size(Utilities::to_godot_fvec2(overlapping_elements.get_size())); godot_rect->set_color({ 0.0f, 0.5f, 1.0f, 0.2f }); result = godot_rect; @@ -362,7 +362,7 @@ bool GodotGUIBuilder::generate_listbox(GUI::Element const& element, AssetManager return false; } - godot_rect->set_size(Utilities::to_godot_ivec2(listbox.get_size())); + godot_rect->set_size(Utilities::to_godot_fvec2(listbox.get_size())); godot_rect->set_color({ 1.0f, 0.5f, 0.0f, 0.2f }); result = godot_rect; @@ -382,7 +382,7 @@ bool GodotGUIBuilder::generate_window(GUI::Element const& element, AssetManager& return false; } - godot_panel->set_size(Utilities::to_godot_ivec2(window.get_size())); + godot_panel->set_size(Utilities::to_godot_fvec2(window.get_size())); godot_panel->set_self_modulate({ 1.0f, 1.0f, 1.0f, 0.0f }); bool ret = true; diff --git a/extension/src/openvic-extension/singletons/GameSingleton.cpp b/extension/src/openvic-extension/singletons/GameSingleton.cpp index bb1732e..d101a86 100644 --- a/extension/src/openvic-extension/singletons/GameSingleton.cpp +++ b/extension/src/openvic-extension/singletons/GameSingleton.cpp @@ -23,15 +23,14 @@ void GameSingleton::_bind_methods() { OV_BIND_METHOD(GameSingleton::load_defines_compatibility_mode, { "file_paths" }); OV_BIND_SMETHOD(search_for_game_path, { "hint_path" }, DEFVAL(String {})); - OV_BIND_METHOD(GameSingleton::lookup_file, { "path" }); OV_BIND_METHOD(GameSingleton::setup_game); OV_BIND_METHOD(GameSingleton::get_province_index_from_uv_coords, { "coords" }); OV_BIND_METHOD(GameSingleton::get_province_info_from_index, { "index" }); - OV_BIND_METHOD(GameSingleton::get_width); - OV_BIND_METHOD(GameSingleton::get_height); - OV_BIND_METHOD(GameSingleton::get_aspect_ratio); + OV_BIND_METHOD(GameSingleton::get_map_width); + OV_BIND_METHOD(GameSingleton::get_map_height); + OV_BIND_METHOD(GameSingleton::get_map_aspect_ratio); OV_BIND_METHOD(GameSingleton::get_terrain_texture); OV_BIND_METHOD(GameSingleton::get_province_shape_image_subdivisions); OV_BIND_METHOD(GameSingleton::get_province_shape_texture); @@ -149,19 +148,25 @@ Dataloader const& GameSingleton::get_dataloader() const { } Error GameSingleton::setup_game() { - bool ret = game_manager.setup(); + BookmarkManager const& bookmark_manager = game_manager.get_history_manager().get_bookmark_manager(); + if (bookmark_manager.bookmarks_empty()) { + UtilityFunctions::push_error("No bookmark to load!"); + return FAILED; + } + bool ret = game_manager.load_bookmark(&bookmark_manager.get_bookmarks().front()); + // TODO - load pop history with the new history system ret &= dataloader.load_pop_history(game_manager, "history/pops/" + game_manager.get_today().to_string()); return ERR(ret); } int32_t GameSingleton::get_province_index_from_uv_coords(Vector2 const& coords) const { - const size_t x_mod_w = UtilityFunctions::fposmod(coords.x, 1.0f) * get_width(); - const size_t y_mod_h = UtilityFunctions::fposmod(coords.y, 1.0f) * get_height(); + const size_t x_mod_w = UtilityFunctions::fposmod(coords.x, 1.0f) * get_map_width(); + const size_t y_mod_h = UtilityFunctions::fposmod(coords.y, 1.0f) * get_map_height(); return game_manager.get_map().get_province_index_at(x_mod_w, y_mod_h); } template T> -static Dictionary _distribution_to_dictionary(decimal_map_t const& dist) { +static Dictionary _distribution_to_dictionary(fixed_point_map_t const& dist) { static const StringName piechart_info_size_key = "size"; static const StringName piechart_info_colour_key = "colour"; Dictionary dict; @@ -216,15 +221,15 @@ Dictionary GameSingleton::get_province_info_from_index(int32_t index) const { } ret[province_info_total_population_key] = province->get_total_population(); - decimal_map_t const& pop_types = province->get_pop_type_distribution(); + fixed_point_map_t const& pop_types = province->get_pop_type_distribution(); if (!pop_types.empty()) { ret[province_info_pop_types_key] = _distribution_to_dictionary(pop_types); } - decimal_map_t const& ideologies = province->get_ideology_distribution(); + fixed_point_map_t const& ideologies = province->get_ideology_distribution(); if (!ideologies.empty()) { ret[province_info_pop_ideologies_key] = _distribution_to_dictionary(ideologies); } - decimal_map_t const& cultures = province->get_culture_distribution(); + fixed_point_map_t const& cultures = province->get_culture_distribution(); if (!cultures.empty()) { ret[province_info_pop_cultures_key] = _distribution_to_dictionary(cultures); } @@ -245,7 +250,7 @@ Dictionary GameSingleton::get_province_info_from_index(int32_t index) const { Dictionary building_dict; building_dict[building_info_building_key] = std_view_to_godot_string(building.get_identifier()); - building_dict[building_info_level_key] = static_cast(building.get_current_level()); + building_dict[building_info_level_key] = static_cast(building.get_level()); building_dict[building_info_expansion_state_key] = static_cast(building.get_expansion_state()); building_dict[building_info_start_date_key] = std_to_godot_string(building.get_start_date().to_string()); building_dict[building_info_end_date_key] = std_to_godot_string(building.get_end_date().to_string()); @@ -258,16 +263,16 @@ Dictionary GameSingleton::get_province_info_from_index(int32_t index) const { return ret; } -int32_t GameSingleton::get_width() const { +int32_t GameSingleton::get_map_width() const { return game_manager.get_map().get_width(); } -int32_t GameSingleton::get_height() const { +int32_t GameSingleton::get_map_height() const { return game_manager.get_map().get_height(); } -float GameSingleton::get_aspect_ratio() const { - return static_cast(get_width()) / static_cast(get_height()); +float GameSingleton::get_map_aspect_ratio() const { + return static_cast(get_map_width()) / static_cast(get_map_height()); } Ref GameSingleton::get_terrain_texture() const { @@ -289,7 +294,7 @@ Ref GameSingleton::get_province_colour_texture() const { Error GameSingleton::_update_colour_image() { static PackedByteArray colour_data_array; static constexpr int64_t colour_data_array_size = - (static_cast(Province::MAX_INDEX) + 1) * Map::MAPMODE_COLOUR_SIZE; + (static_cast(Province::MAX_INDEX) + 1) * sizeof(Mapmode::base_stripe_t); colour_data_array.resize(colour_data_array_size); Error err = OK; @@ -297,12 +302,16 @@ Error GameSingleton::_update_colour_image() { err = FAILED; } + /* We reshape the list of colours into a square, as each texture dimensions cannot exceed 16384. */ static constexpr int32_t PROVINCE_INDEX_SQRT = 1 << (sizeof(Province::index_t) * 4); if (province_colour_image.is_null()) { province_colour_image.instantiate(); ERR_FAIL_NULL_V_EDMSG(province_colour_image, FAILED, "Failed to create province colour image"); } - province_colour_image->set_data(PROVINCE_INDEX_SQRT, PROVINCE_INDEX_SQRT, false, Image::FORMAT_RGBA8, colour_data_array); + /* Width is doubled as each province has a (base, stripe) colour pair. */ + province_colour_image->set_data( + PROVINCE_INDEX_SQRT * 2, PROVINCE_INDEX_SQRT, false, Image::FORMAT_RGBA8, colour_data_array + ); if (province_colour_texture.is_null()) { province_colour_texture = ImageTexture::create_from_image(province_colour_image); ERR_FAIL_NULL_V_EDMSG(province_colour_texture, FAILED, "Failed to create province colour texture"); @@ -543,7 +552,3 @@ Error GameSingleton::load_defines_compatibility_mode(PackedStringArray const& fi String GameSingleton::search_for_game_path(String hint_path) { return std_to_godot_string(Dataloader::search_for_game_path(godot_to_std_string(hint_path)).string()); } - -String GameSingleton::lookup_file(String const& path) const { - return std_to_godot_string(dataloader.lookup_file(godot_to_std_string(path)).string()); -} diff --git a/extension/src/openvic-extension/singletons/GameSingleton.hpp b/extension/src/openvic-extension/singletons/GameSingleton.hpp index 403fbb6..1346a5f 100644 --- a/extension/src/openvic-extension/singletons/GameSingleton.hpp +++ b/extension/src/openvic-extension/singletons/GameSingleton.hpp @@ -69,8 +69,6 @@ namespace OpenVic { static godot::String search_for_game_path(godot::String hint_path = {}); - godot::String lookup_file(godot::String const& path) const; - /* Post-load/restart game setup - reset the game to post-load state * and (re)generate starting data, e.g. buildings. */ @@ -83,9 +81,9 @@ namespace OpenVic { */ godot::Dictionary get_province_info_from_index(int32_t index) const; - int32_t get_width() const; - int32_t get_height() const; - float get_aspect_ratio() const; + int32_t get_map_width() const; + int32_t get_map_height() const; + float get_map_aspect_ratio() const; /* The cosmetic terrain textures stored in a Texture2DArray. */ diff --git a/extension/src/openvic-extension/utility/Utilities.hpp b/extension/src/openvic-extension/utility/Utilities.hpp index fc106db..752f495 100644 --- a/extension/src/openvic-extension/utility/Utilities.hpp +++ b/extension/src/openvic-extension/utility/Utilities.hpp @@ -42,6 +42,10 @@ namespace OpenVic::Utilities { return { vec.x, vec.y }; } + inline godot::Vector2 to_godot_fvec2(fvec2_t vec) { + return { vec.x, vec.y }; + } + godot::Ref load_godot_image(godot::String const& path); godot::Ref load_godot_font(godot::String const& fnt_path, godot::Ref const& image); diff --git a/game/localisation/en_GB/mapmodes.csv b/game/localisation/en_GB/mapmodes.csv index 2aa4cd2..0372192 100644 --- a/game/localisation/en_GB/mapmodes.csv +++ b/game/localisation/en_GB/mapmodes.csv @@ -3,6 +3,7 @@ mapmode_province;Province mapmode_region;Region mapmode_terrain;Terrain +mapmode_political;Political mapmode_index;Index mapmode_terrain_type;Terrain Type mapmode_rgo;RGO diff --git a/game/src/Game/GameSession/MapControlPanel/Minimap.gd b/game/src/Game/GameSession/MapControlPanel/Minimap.gd index be65db5..5564821 100644 --- a/game/src/Game/GameSession/MapControlPanel/Minimap.gd +++ b/game/src/Game/GameSession/MapControlPanel/Minimap.gd @@ -10,7 +10,7 @@ var _minimap_shader : ShaderMaterial var _viewport_points : PackedVector2Array func _ready(): - _minimap_texture.custom_minimum_size = Vector2(GameSingleton.get_aspect_ratio(), 1.0) * 150 + _minimap_texture.custom_minimum_size = Vector2(GameSingleton.get_map_aspect_ratio(), 1.0) * 150 var minimap_material := _minimap_texture.get_material() if GameLoader.ShaderManager.set_up_shader(minimap_material, false) != OK: push_error("Failed to set up minimap shader") diff --git a/game/src/Game/GameSession/MapView.gd b/game/src/Game/GameSession/MapView.gd index 345ab9e..7093c69 100644 --- a/game/src/Game/GameSession/MapView.gd +++ b/game/src/Game/GameSession/MapView.gd @@ -74,7 +74,10 @@ func _ready(): # Set map mesh size and get bounds const pixels_per_terrain_tile : float = 32.0 _map_shader_material.set_shader_parameter(GameLoader.ShaderManager.param_terrain_tile_factor, - float(GameSingleton.get_height()) / pixels_per_terrain_tile) + float(GameSingleton.get_map_height()) / pixels_per_terrain_tile) + const pixels_per_stripe_tile : float = 16.0 + _map_shader_material.set_shader_parameter(GameLoader.ShaderManager.param_stripe_tile_factor, + float(GameSingleton.get_map_height()) / pixels_per_stripe_tile) var map_mesh_aabb := _map_mesh.get_core_aabb() * _map_mesh_instance.transform _map_mesh_corner = Vector2( min(map_mesh_aabb.position.x, map_mesh_aabb.end.x), diff --git a/game/src/Game/GameSession/ProvinceIndexSampler.gdshaderinc b/game/src/Game/GameSession/ProvinceIndexSampler.gdshaderinc index 65f73d8..1adcd95 100644 --- a/game/src/Game/GameSession/ProvinceIndexSampler.gdshaderinc +++ b/game/src/Game/GameSession/ProvinceIndexSampler.gdshaderinc @@ -4,15 +4,21 @@ uniform sampler2DArray province_shape_tex : repeat_enable, filter_nearest; // Province shape subdivisions uniform vec2 province_shape_subdivisions; +// Convert a vector of 3 normalised floats to a vector of 3 unsigned bytes uvec3 vec3_to_uvec3(vec3 v) { return uvec3(v * 255.0); } + +// Create a uint triplet describing the province and terrain data at a map-space UV coordinate: +// (u, v) -> (province index bottom byte, province index top byte, terrain index byte) uvec3 read_uvec3(vec2 uv) { uv *= province_shape_subdivisions; vec2 subdivision_coords = mod(floor(uv), province_shape_subdivisions); float idx = subdivision_coords.x + subdivision_coords.y * province_shape_subdivisions.x; return vec3_to_uvec3(texture(province_shape_tex, vec3(uv, idx)).rgb); } + +// Combine a (lower byte, upper byte) uint pair into a single 2-byte uint uint uvec2_to_uint(uvec2 v) { return (v.y << 8u) | v.x; } diff --git a/game/src/Game/GameSession/TerrainMap.gdshader b/game/src/Game/GameSession/TerrainMap.gdshader index 852ccc3..ff8708a 100644 --- a/game/src/Game/GameSession/TerrainMap.gdshader +++ b/game/src/Game/GameSession/TerrainMap.gdshader @@ -14,16 +14,29 @@ uniform uint selected_index; uniform sampler2DArray terrain_tex: source_color, repeat_enable, filter_linear; // The number of times the terrain textures should tile vertically uniform float terrain_tile_factor; +// Map stripe mask texture +uniform sampler2D stripe_tex: source_color, repeat_enable, filter_nearest; +// The number of times the stripe texture should tile vertically +uniform float stripe_tile_factor; const vec3 highlight_colour = vec3(1.0); -vec3 get_terrain_colour(vec2 uv, vec2 corner, vec2 half_pixel_size, vec2 terrain_uv) { +vec3 get_terrain_colour(vec2 uv, vec2 corner, vec2 half_pixel_size, vec2 terrain_uv, vec2 stripe_uv) { uvec3 province_data = read_uvec3(fma(corner, half_pixel_size, uv)); - vec4 province_colour = texelFetch(province_colour_tex, ivec2(province_data.rg), 0); - vec3 terrain_colour = texture(terrain_tex, vec3(terrain_uv, float(province_data.b))).rgb; + uint province_index = uvec2_to_uint(province_data.rg); + uint terrain_index = province_data.b; + + province_data.r *= 2u; // Double "x coordinate" as colours come in (base, stripe) pairs + vec4 province_base_colour = texelFetch(province_colour_tex, ivec2(province_data.rg), 0); + province_data.r += 1u; // Add 1 to "x coordinate" to move from base to strip colour + vec4 province_stripe_colour = texelFetch(province_colour_tex, ivec2(province_data.rg), 0); + float stripe = texture(stripe_tex, stripe_uv).b; // Stripe mask value - 0 for base, 1 for stripe + vec4 province_colour = mix(province_base_colour, province_stripe_colour, stripe); + + vec3 terrain_colour = texture(terrain_tex, vec3(terrain_uv, float(terrain_index))).rgb; vec3 mixed_colour = mix(terrain_colour, province_colour.rgb, province_colour.a); - uint index = uvec2_to_uint(province_data.rg); - float mix_val = 0.1 * (float(index == hover_index) + float(index == selected_index)); + + float mix_val = 0.1 * (float(province_index == hover_index) + float(province_index == selected_index)); return mix(mixed_colour, highlight_colour, mix_val); } @@ -32,15 +45,17 @@ vec3 mix_terrain_colour(vec2 uv) { vec2 pixel_offset = fract(fma(uv, map_size, vec2(0.5))); vec2 half_pixel_size = 0.49 / map_size; - vec2 terrain_uv = uv; - terrain_uv.x *= map_size.x / map_size.y; - terrain_uv *= terrain_tile_factor; + // UV coords adjusted to remove squashing caused by normalisation relative to map dimensions. + vec2 unscaled_uv = vec2(uv.x * map_size.x / map_size.y, uv.y); + + vec2 terrain_uv = unscaled_uv * terrain_tile_factor; + vec2 stripe_uv = unscaled_uv * stripe_tile_factor; return mix( - mix(get_terrain_colour(uv, vec2(-1, -1), half_pixel_size, terrain_uv), - get_terrain_colour(uv, vec2(+1, -1), half_pixel_size, terrain_uv), pixel_offset.x), - mix(get_terrain_colour(uv, vec2(-1, +1), half_pixel_size, terrain_uv), - get_terrain_colour(uv, vec2(+1, +1), half_pixel_size, terrain_uv), pixel_offset.x), + mix(get_terrain_colour(uv, vec2(-1, -1), half_pixel_size, terrain_uv, stripe_uv), + get_terrain_colour(uv, vec2(+1, -1), half_pixel_size, terrain_uv, stripe_uv), pixel_offset.x), + mix(get_terrain_colour(uv, vec2(-1, +1), half_pixel_size, terrain_uv, stripe_uv), + get_terrain_colour(uv, vec2(+1, +1), half_pixel_size, terrain_uv, stripe_uv), pixel_offset.x), pixel_offset.y); } diff --git a/game/src/Game/GlobalClass/ShaderManager.gd b/game/src/Game/GlobalClass/ShaderManager.gd index fd91e31..1af606c 100644 --- a/game/src/Game/GlobalClass/ShaderManager.gd +++ b/game/src/Game/GlobalClass/ShaderManager.gd @@ -8,6 +8,8 @@ const param_hover_index : StringName = &"hover_index" const param_selected_index : StringName = &"selected_index" const param_terrain_tex : StringName = &"terrain_tex" const param_terrain_tile_factor : StringName = &"terrain_tile_factor" +const param_stripe_tex : StringName = &"stripe_tex" +const param_stripe_tile_factor : StringName = &"stripe_tile_factor" func set_up_shader(material : Material, add_cosmetic_textures : bool) -> Error: # Shader Material @@ -46,4 +48,11 @@ func set_up_shader(material : Material, add_cosmetic_textures : bool) -> Error: return FAILED shader_material.set_shader_parameter(param_terrain_tex, terrain_texture) + # Stripe texture + var stripe_texture := AssetManager.get_texture(&"map/terrain/stripes.dds") + if stripe_texture == null: + push_error("Failed to get stripe texture!") + return FAILED + shader_material.set_shader_parameter(param_stripe_tex, stripe_texture) + return OK -- cgit v1.2.3-56-ga3b1