From 29b334995b084e5d6944c45b7950c88f370941cc Mon Sep 17 00:00:00 2001 From: Hop311 Date: Wed, 5 Apr 2023 17:14:32 +0100 Subject: Input and province selection cleanup --- extension/src/LoadLocalisation.cpp | 8 +- extension/src/LoadLocalisation.hpp | 8 +- extension/src/MapSingleton.cpp | 18 ++-- extension/src/MapSingleton.hpp | 3 +- extension/src/TestSingleton.cpp | 4 +- extension/src/TestSingleton.hpp | 4 +- extension/src/openvic2/Map.cpp | 43 ++++++-- extension/src/openvic2/Map.hpp | 17 +++- extension/src/register_types.cpp | 9 +- game/localisation/en_GB/menus.csv | 2 +- game/localisation/en_GB/provinces.csv | 11 ++- game/localisation/en_GB/provinces.csv.import | 16 +-- game/src/GameSession/GameSession.tscn | 32 +++--- game/src/GameSession/MapView.gd | 126 +++++++++++------------- game/src/GameSession/MapView.tscn | 4 +- game/src/GameSession/ProvinceOverviewPanel.gd | 23 +++-- game/src/GameSession/ProvinceOverviewPanel.tscn | 25 ++--- game/src/GameSession/TerrainMap.gdshader | 21 ++-- 18 files changed, 196 insertions(+), 178 deletions(-) diff --git a/extension/src/LoadLocalisation.cpp b/extension/src/LoadLocalisation.cpp index c95c08b..8698bb2 100644 --- a/extension/src/LoadLocalisation.cpp +++ b/extension/src/LoadLocalisation.cpp @@ -8,7 +8,7 @@ using namespace godot; using namespace OpenVic2; -LoadLocalisation *LoadLocalisation::singleton = nullptr; +LoadLocalisation* LoadLocalisation::singleton = nullptr; void LoadLocalisation::_bind_methods() { ClassDB::bind_method(D_METHOD("load_file", "file_path", "locale"), &LoadLocalisation::load_file); @@ -16,7 +16,7 @@ void LoadLocalisation::_bind_methods() { ClassDB::bind_method(D_METHOD("load_localisation_dir", "dir_path"), &LoadLocalisation::load_localisation_dir); } -LoadLocalisation *LoadLocalisation::get_singleton() { +LoadLocalisation* LoadLocalisation::get_singleton() { return singleton; } @@ -54,7 +54,7 @@ Error LoadLocalisation::_load_file_into_translation(String const& file_path, Ref } Ref LoadLocalisation::_get_translation(String const& locale) { - TranslationServer *server = TranslationServer::get_singleton(); + TranslationServer* server = TranslationServer::get_singleton(); Ref translation = server->get_translation_object(locale); if (translation.is_null() || translation->get_locale() != locale) { translation.instantiate(); @@ -93,7 +93,7 @@ Error LoadLocalisation::load_locale_dir(String const& dir_path, String const& lo */ Error LoadLocalisation::load_localisation_dir(String const& dir_path) { if (DirAccess::dir_exists_absolute(dir_path)) { - TranslationServer *server = TranslationServer::get_singleton(); + TranslationServer* server = TranslationServer::get_singleton(); Error err = OK; for (String const& locale_name : DirAccess::get_directories_at(dir_path)) { if (locale_name == server->standardize_locale(locale_name)) { diff --git a/extension/src/LoadLocalisation.hpp b/extension/src/LoadLocalisation.hpp index 90f3158..f54a025 100644 --- a/extension/src/LoadLocalisation.hpp +++ b/extension/src/LoadLocalisation.hpp @@ -4,11 +4,11 @@ #include namespace OpenVic2 { - class LoadLocalisation : public godot::Object - { + class LoadLocalisation : public godot::Object { + GDCLASS(LoadLocalisation, godot::Object) - static LoadLocalisation *singleton; + static LoadLocalisation* singleton; godot::Error _load_file_into_translation(godot::String const& file_path, godot::Ref translation); godot::Ref _get_translation(godot::String const& locale); @@ -17,7 +17,7 @@ namespace OpenVic2 { static void _bind_methods(); public: - static LoadLocalisation *get_singleton(); + static LoadLocalisation* get_singleton(); LoadLocalisation(); ~LoadLocalisation(); diff --git a/extension/src/MapSingleton.cpp b/extension/src/MapSingleton.cpp index 9f76508..9d13496 100644 --- a/extension/src/MapSingleton.cpp +++ b/extension/src/MapSingleton.cpp @@ -13,7 +13,7 @@ void MapSingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("load_province_identifier_file", "file_path"), &MapSingleton::load_province_identifier_file); ClassDB::bind_method(D_METHOD("load_province_shape_file", "file_path"), &MapSingleton::load_province_shape_file); ClassDB::bind_method(D_METHOD("get_province_shape_image"), &MapSingleton::get_province_shape_image); - ClassDB::bind_method(D_METHOD("get_province_id"), &MapSingleton::get_province_id); + ClassDB::bind_method(D_METHOD("get_province_identifier_from_colour", "colour"), &MapSingleton::get_province_identifier_from_colour); } MapSingleton* MapSingleton::get_singleton() { @@ -64,9 +64,9 @@ Error MapSingleton::load_province_identifier_file(String const& file_path) { err = FAILED; continue; } - // static const String prov_prefix = "prov_"; - // if (!identifier.begins_with(prov_prefix)) - // UtilityFunctions::push_warning("Province identifier missing prefix: ", identifier); + static const String prov_prefix = "prov_"; + if (!identifier.begins_with(prov_prefix)) + UtilityFunctions::push_warning("Province identifier missing prefix: ", identifier); type = colour_var.get_type(); Province::colour_t colour = Province::NULL_COLOUR; if (type == Variant::ARRAY) { @@ -115,12 +115,10 @@ Error MapSingleton::load_province_identifier_file(String const& file_path) { return err; } -godot::String MapSingleton::get_province_id(godot::String const& hex_str) { - UtilityFunctions::print(hex_str); - int64_t colour_string = hex_str.hex_to_int(); - godot::String province_id = map.get_province(colour_string).identifier.c_str(); - UtilityFunctions::print("Returning: ",map.get_province(colour_string).to_string().c_str()); - return province_id; +String MapSingleton::get_province_identifier_from_colour(Province::colour_t colour) { + const Province* province = map.get_province_by_colour(colour); + if (province) return province->get_identifier().c_str(); + else return String{}; } Error MapSingleton::load_province_shape_file(String const& file_path) { diff --git a/extension/src/MapSingleton.hpp b/extension/src/MapSingleton.hpp index fd549eb..fb62e11 100644 --- a/extension/src/MapSingleton.hpp +++ b/extension/src/MapSingleton.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include "openvic2/Map.hpp" @@ -25,7 +24,7 @@ namespace OpenVic2 { godot::Error load_province_identifier_file(godot::String const& file_path); godot::Error load_province_shape_file(godot::String const& file_path); - godot::String get_province_id(godot::String const& hex_str); + godot::String get_province_identifier_from_colour(Province::colour_t colour); godot::Ref get_province_shape_image() const; }; } diff --git a/extension/src/TestSingleton.cpp b/extension/src/TestSingleton.cpp index 0855a30..d9e2b03 100644 --- a/extension/src/TestSingleton.cpp +++ b/extension/src/TestSingleton.cpp @@ -6,14 +6,14 @@ using namespace godot; using namespace OpenVic2; -TestSingleton *TestSingleton::singleton = nullptr; +TestSingleton* TestSingleton::singleton = nullptr; void TestSingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("hello_singleton"), &TestSingleton::hello_singleton); } -TestSingleton *TestSingleton::get_singleton() +TestSingleton* TestSingleton::get_singleton() { return singleton; } diff --git a/extension/src/TestSingleton.hpp b/extension/src/TestSingleton.hpp index de27589..d140516 100644 --- a/extension/src/TestSingleton.hpp +++ b/extension/src/TestSingleton.hpp @@ -8,13 +8,13 @@ namespace OpenVic2 { { GDCLASS(TestSingleton, godot::Object) - static TestSingleton *singleton; + static TestSingleton* singleton; protected: static void _bind_methods(); public: - static TestSingleton *get_singleton(); + static TestSingleton* get_singleton(); TestSingleton(); ~TestSingleton(); diff --git a/extension/src/openvic2/Map.cpp b/extension/src/openvic2/Map.cpp index 40c421a..4815dff 100644 --- a/extension/src/openvic2/Map.cpp +++ b/extension/src/openvic2/Map.cpp @@ -5,14 +5,33 @@ using namespace OpenVic2; -std::string Province::to_string() const { +Province::Province(std::string const& new_identifier, colour_t new_colour) : + identifier(new_identifier), colour(new_colour) {} + +std::string Province::colour_to_hex_string(colour_t colour) { std::ostringstream stream; - stream << "(" << identifier << ", " << std::hex << std::setfill('0') << std::setw(6) << colour << ")"; + stream << std::hex << std::setfill('0') << std::setw(6) << colour; return stream.str(); } +std::string const& Province::get_identifier() const { + return identifier; +} + +Province::colour_t Province::get_colour() const { + return colour; +} + +std::string Province::to_string() const { + return "(" + std::to_string(index) + ", " + identifier + ", " + colour_to_hex_string(colour) + ")"; +} + bool Map::add_province(std::string const& identifier, Province::colour_t colour, std::string& error_message) { - Province new_province = { identifier, colour }; + if (colour == Province::NULL_COLOUR || colour > Province::MAX_COLOUR) { + error_message = "Invalid province colour: " + Province::colour_to_hex_string(colour); + return false; + } + Province new_province{ identifier, colour }; for (Province const& province : provinces) { if (province.identifier == identifier) { error_message = "Duplicate province identifiers: " + province.to_string() + " and " + new_province.to_string(); @@ -28,10 +47,14 @@ bool Map::add_province(std::string const& identifier, Province::colour_t colour, return true; } -Province Map::get_province(Province::colour_t colour) { - for(Province const& province : provinces) { - if (province.colour == colour) { - return province; - } - } -} \ No newline at end of file +Province* Map::get_province_by_identifier(std::string const& identifier) { + for (Province& province : provinces) + if (province.identifier == identifier) return &province; + return nullptr; +} + +Province* Map::get_province_by_colour(Province::colour_t colour) { + for (Province& province : provinces) + if (province.colour == colour) return &province; + return nullptr; +} diff --git a/extension/src/openvic2/Map.hpp b/extension/src/openvic2/Map.hpp index 6d8482e..f3fd1ef 100644 --- a/extension/src/openvic2/Map.hpp +++ b/extension/src/openvic2/Map.hpp @@ -8,12 +8,18 @@ namespace OpenVic2 { struct Province { using colour_t = uint32_t; - - static const colour_t NULL_COLOUR = 0; - + friend struct Map; + static const colour_t NULL_COLOUR = 0, MAX_COLOUR = 0xFFFFFF; + private: std::string identifier; colour_t colour; - + + Province(std::string const& identifier, colour_t colour); + public: + static std::string colour_to_hex_string(colour_t colour); + + std::string const& get_identifier() const; + colour_t get_colour() const; std::string to_string() const; }; @@ -23,7 +29,8 @@ namespace OpenVic2 { public: bool add_province(std::string const& identifier, Province::colour_t colour, std::string& error_message); - Province get_province(Province::colour_t colour); + Province* get_province_by_identifier(std::string const& identifier); + Province* get_province_by_colour(Province::colour_t colour); }; } diff --git a/extension/src/register_types.cpp b/extension/src/register_types.cpp index ef5428c..3ef0c24 100644 --- a/extension/src/register_types.cpp +++ b/extension/src/register_types.cpp @@ -20,8 +20,7 @@ static Checksum* _checksum; static LoadLocalisation* _load_localisation; static MapSingleton* _map_singleton; -void initialize_openvic2_types(ModuleInitializationLevel p_level) -{ +void initialize_openvic2_types(ModuleInitializationLevel p_level) { if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { return; } @@ -68,13 +67,11 @@ void uninitialize_openvic2_types(ModuleInitializationLevel p_level) { memdelete(_map_singleton); } -extern "C" -{ +extern "C" { // Initialization. - GDExtensionBool GDE_EXPORT openvic2_library_init(const GDExtensionInterface *p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) - { + GDExtensionBool GDE_EXPORT openvic2_library_init(const GDExtensionInterface* p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization* r_initialization) { GDExtensionBinding::InitObject init_obj(p_interface, p_library, r_initialization); init_obj.register_initializer(initialize_openvic2_types); diff --git a/game/localisation/en_GB/menus.csv b/game/localisation/en_GB/menus.csv index 97e6f2c..b483662 100644 --- a/game/localisation/en_GB/menus.csv +++ b/game/localisation/en_GB/menus.csv @@ -1,3 +1,4 @@ + ,, Main Menu MAINMENU_TITLE,OpenVic2 MAINMENU_NEW_GAME,New Game @@ -13,7 +14,6 @@ OPTIONS_RESET,R OPTIONS_BACK,X ,, General Tab -OPTIONS_NYAF,Nyaf OPTIONS_GENERAL_SAVEFORMAT,Savegame Format OPTIONS_GENERAL_BINARY,Binary OPTIONS_GENERAL_TEXT,Text diff --git a/game/localisation/en_GB/provinces.csv b/game/localisation/en_GB/provinces.csv index ad0d0b8..3b47955 100644 --- a/game/localisation/en_GB/provinces.csv +++ b/game/localisation/en_GB/provinces.csv @@ -1,2 +1,9 @@ -,, Provinces -1023_NAME,London \ No newline at end of file + +,, Test Provinces +prov_britain_NAME,Britain +prov_ireland_NAME,Ireland +prov_iceland_NAME,Iceland +prov_cuba_NAME,Cuba +prov_madagascar_NAME,Madagascar +prov_ceylon_NAME,Ceylon +prov_formosa_NAME,Formosa diff --git a/game/localisation/en_GB/provinces.csv.import b/game/localisation/en_GB/provinces.csv.import index 0ae9755..8dd0c09 100644 --- a/game/localisation/en_GB/provinces.csv.import +++ b/game/localisation/en_GB/provinces.csv.import @@ -1,17 +1,3 @@ [remap] -importer="csv_translation" -type="Translation" -uid="uid://ljh3sy6dw0tv" - -[deps] - -files=["res://localisation/en_GB/provinces..translation", "res://localisation/en_GB/provinces. Provinces.translation"] - -source_file="res://localisation/en_GB/provinces.csv" -dest_files=["res://localisation/en_GB/provinces..translation", "res://localisation/en_GB/provinces. Provinces.translation"] - -[params] - -compress=true -delimiter=0 +importer="keep" diff --git a/game/src/GameSession/GameSession.tscn b/game/src/GameSession/GameSession.tscn index 25a9509..390040e 100644 --- a/game/src/GameSession/GameSession.tscn +++ b/game/src/GameSession/GameSession.tscn @@ -4,16 +4,10 @@ [ext_resource type="PackedScene" uid="uid://g524p8lr574w" path="res://src/GameSession/MapControlPanel.tscn" id="3_afh6d"] [ext_resource type="PackedScene" uid="uid://dvdynl6eir40o" path="res://src/GameSession/GameSessionMenu.tscn" id="3_bvmqh"] [ext_resource type="PackedScene" uid="uid://dkehmdnuxih2r" path="res://src/GameSession/MapView.tscn" id="4_xkg5j"] -[ext_resource type="PackedScene" uid="uid://byq323jbel48u" path="res://src/GameSession/ProvinceOverviewPanel.tscn" id="5_5gj48"] +[ext_resource type="PackedScene" uid="uid://byq323jbel48u" path="res://src/GameSession/ProvinceOverviewPanel.tscn" id="5_osjnn"] -[node name="GameSession" type="Control" node_paths=PackedStringArray("_game_session_menu")] +[node name="GameSession" type="Node" node_paths=PackedStringArray("_game_session_menu")] editor_description = "SS-102" -layout_mode = 3 -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 script = ExtResource("1_eklvp") _game_session_menu = NodePath("GameSessionMenu") @@ -21,24 +15,30 @@ _game_session_menu = NodePath("GameSessionMenu") [node name="GameSessionMenu" parent="." instance=ExtResource("3_bvmqh")] visible = false -layout_mode = 1 -offset_right = 232.0 -offset_bottom = 74.0 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +grow_horizontal = 2 +grow_vertical = 2 [node name="MapControlPanel" parent="." instance=ExtResource("3_afh6d")] -layout_mode = 1 anchors_preset = 3 anchor_left = 1.0 anchor_top = 1.0 anchor_right = 1.0 anchor_bottom = 1.0 -offset_left = -133.0 -offset_top = -56.0 grow_horizontal = 0 grow_vertical = 0 -[node name="ProvinceOverviewPanel" parent="." instance=ExtResource("5_5gj48")] -layout_mode = 1 +[node name="ProvinceOverviewPanel" parent="." instance=ExtResource("5_osjnn")] +anchors_preset = -1 +anchor_top = 0.583333 +anchor_right = 0.15625 +offset_top = 0.0 +offset_right = 0.0 +[connection signal="province_selected" from="MapView" to="ProvinceOverviewPanel" method="_on_province_selected"] [connection signal="close_button_pressed" from="GameSessionMenu" to="." method="_on_game_session_menu_close_button_pressed"] [connection signal="game_session_menu_button_pressed" from="MapControlPanel" to="." method="_on_game_session_menu_button_pressed"] diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd index 958fabd..a8b4dcc 100644 --- a/game/src/GameSession/MapView.gd +++ b/game/src/GameSession/MapView.gd @@ -1,9 +1,6 @@ extends Node3D -var ProvScene = preload("res://src/GameSession/ProvinceOverviewPanel.tscn") -var ProvinceShape = MapSingleton.get_province_shape_image() - -@onready var viewport_size = get_viewport().size +signal province_selected(identifier : String) const _action_north : StringName = &"map_north" const _action_east : StringName = &"map_east" @@ -13,10 +10,10 @@ const _action_zoomin : StringName = &"map_zoomin" const _action_zoomout : StringName = &"map_zoomout" const _action_drag : StringName = &"map_drag" const _action_click : StringName = &"map_click" -const _shader_param_provinces : StringName = &"province_tex" -const _shader_param_mouse_pos : StringName = &"mouse_pos" - +const _shader_param_provinces : StringName = &"province_tex" +const _shader_param_hover_pos : StringName = &"hover_pos" +const _shader_param_selected_pos : StringName = &"selected_pos" @export var _camera : Camera3D @@ -24,6 +21,7 @@ const _shader_param_mouse_pos : StringName = &"mouse_pos" @export var _edge_move_threshold: float = 50.0 @export var _edge_move_speed: float = 0.02 @export var _dragSensitivity: float = 0.005 + @export var _zoom_target_min : float = 0.2 @export var _zoom_target_max : float = 5.0 @export var _zoom_target_step : float = 0.1 @@ -35,10 +33,15 @@ const _shader_param_mouse_pos : StringName = &"mouse_pos" @export var _map_mesh : MeshInstance3D var _map_shader_material : ShaderMaterial +var _map_province_shape_image : Image var _map_aspect_ratio : float = 1.0 var _map_mesh_corner : Vector2 var _map_mesh_dims : Vector2 -var _mouse_pos : Vector2 = Vector2(0.5, 0.5) + +var _mouse_pos_window : Vector2 = Vector2(0.5, 0.5) +var _mouse_pos_map : Vector2 = Vector2(0.5, 0.5) +var _viewport_dims : Vector2i = Vector2i(1, 1) + func _ready(): if _camera == null: push_error("MapView's _camera variable hasn't been set!") @@ -46,18 +49,18 @@ func _ready(): if _map_mesh == null: push_error("MapView's _map_mesh variable hasn't been set!") return - var province_shape_image : Image = MapSingleton.get_province_shape_image() - if province_shape_image == null: + _map_province_shape_image = MapSingleton.get_province_shape_image() + if _map_province_shape_image == null: push_error("Failed to get province shape image!") return # Set map mesh size and get bounds - _map_aspect_ratio = float(province_shape_image.get_width()) / float(province_shape_image.get_height()) - if _map_mesh.mesh.get_class() != "PlaneMesh": - push_error("Invalid map mesh class: ", _map_mesh.mesh.get_class()) - else: + _map_aspect_ratio = float(_map_province_shape_image.get_width()) / float(_map_province_shape_image.get_height()) + if _map_mesh.mesh is PlaneMesh: # Width is doubled so that the map appears to loop horizontally (_map_mesh.mesh as PlaneMesh).size = Vector2(_map_aspect_ratio * 2, 1) + else: + push_error("Invalid map mesh class: ", _map_mesh.mesh.get_class(), "(expected PlaneMesh)") var map_mesh_aabb := _map_mesh.get_aabb() * _map_mesh.transform _map_mesh_corner = Vector2( min(map_mesh_aabb.position.x, map_mesh_aabb.end.x), @@ -72,54 +75,39 @@ func _ready(): if map_material == null: push_error("Map mesh is missing material!") return - if map_material.get_class() != "ShaderMaterial": + if not map_material is ShaderMaterial: push_error("Invalid map mesh material class: ", map_material.get_class()) return _map_shader_material = map_material - var texture := ImageTexture.create_from_image(province_shape_image) + var texture := ImageTexture.create_from_image(_map_province_shape_image) _map_shader_material.set_shader_parameter(_shader_param_provinces, texture) - -func _input(event : InputEvent): - if Input.is_action_pressed(_action_click, true) or event is InputEventMouseMotion: +func _unhandled_input(event : InputEvent): + if event.is_action_pressed(_action_click): # Check if the mouse is outside of bounds - var _mouse_inside_flag = false - if _mouse_pos.x > 1.0 or _mouse_pos.x < 0.0 and _mouse_pos.y > 1.0 or _mouse_pos.y < 0.0: - _mouse_inside_flag = false - else: - _mouse_inside_flag = true - - # Convert the relative event position from 3D to 2D - # Could do one-liner but here split for readability - var mouse_pos2D = _mouse_pos - mouse_pos2D.x = mouse_pos2D.x * 2.0 - 0.5 - mouse_pos2D.x = mouse_pos2D.x * ProvinceShape.get_size().x - mouse_pos2D.y = mouse_pos2D.y * ProvinceShape.get_size().y - - if Input.is_action_pressed(_action_click, true) and _mouse_inside_flag == true and not event is InputEventMouseMotion: - var pxColour = ProvinceShape.get_pixel(int(mouse_pos2D.x), int(mouse_pos2D.y)) - get_node('MapMeshInstance').material_override.set_shader_parameter("selection_hover", 1.2) - if get_parent().has_node("ProvinceOverviewPanel"): - get_parent().get_node("ProvinceOverviewPanel").ProvinceID = MapSingleton.get_province_id(pxColour.to_html(false)) - get_parent().get_node("ProvinceOverviewPanel").set_id() - else: - var Province = ProvScene.instantiate() - Province.ProvinceID = MapSingleton.get_province_id(pxColour.to_html(false)) - get_parent().add_child(Province) - else: - get_node('MapMeshInstance').material_override.set_shader_parameter("selection_hover", 0.8) -# - - if event is InputEventMouseMotion and Input.is_action_pressed(_action_drag, true): + var mouse_inside_flag := 0 < _mouse_pos_map.x and _mouse_pos_map.x < 1 and 0 < _mouse_pos_map.y and _mouse_pos_map.y < 1 + if mouse_inside_flag: + var mouse_pos2D := _mouse_pos_map + mouse_pos2D.x = mouse_pos2D.x * 2.0 - 0.5 + mouse_pos2D *= Vector2(_map_province_shape_image.get_size()) + + var province_colour := _map_province_shape_image.get_pixelv(Vector2i(mouse_pos2D)).to_argb32() & 0xFFFFFF + var province_identifier := MapSingleton.get_province_identifier_from_colour(province_colour) + _map_shader_material.set_shader_parameter(_shader_param_selected_pos, _mouse_pos_map) + province_selected.emit(province_identifier) + + elif event is InputEventMouseMotion and Input.is_action_pressed(_action_drag): _camera.position.x -= event.relative.x * _dragSensitivity _camera.position.z -= event.relative.y * _dragSensitivity - if event.is_action_pressed(_action_zoomin, true): + elif event.is_action_pressed(_action_zoomin, true): _zoom_target -= _zoom_target_step elif event.is_action_pressed(_action_zoomout, true): _zoom_target += _zoom_target_step func _physics_process(delta : float): + _mouse_pos_window = get_viewport().get_mouse_position() + _viewport_dims = get_viewport().size # Process movement _move_process(delta) _edge_scrolling() @@ -131,21 +119,18 @@ func _physics_process(delta : float): _update_orientation() # Calculate where the mouse lies on the map _update_mouse_map_position() - - + func _edge_scrolling() -> void: - var local_mouse_pos = get_viewport().get_mouse_position() - - if local_mouse_pos.y < _edge_move_threshold: + if _mouse_pos_window.y < _edge_move_threshold: _camera.position.z -= _edge_move_speed - elif local_mouse_pos.y > get_viewport().size.y - _edge_move_threshold: + elif _mouse_pos_window.y > _viewport_dims.y - _edge_move_threshold: _camera.position.z += _edge_move_speed - - if local_mouse_pos.x < _edge_move_threshold: + + if _mouse_pos_window.x < _edge_move_threshold: _camera.position.x -= _edge_move_speed - elif local_mouse_pos.x > get_viewport().size.x - _edge_move_threshold: + elif _mouse_pos_window.x > _viewport_dims.x - _edge_move_threshold: _camera.position.x += _edge_move_speed - + func _move_process(delta : float) -> void: var move := Vector3( float(Input.is_action_pressed(_action_east)) - float(Input.is_action_pressed(_action_west)), @@ -153,40 +138,39 @@ func _move_process(delta : float) -> void: float(Input.is_action_pressed(_action_south)) - float(Input.is_action_pressed(_action_north)) ) # Scale movement speed with height - move *= _move_speed * _camera.transform.origin.y * delta + move *= _move_speed * _camera.position.y * delta _camera.global_translate(move) func _clamp_over_map() -> void: var left := _map_mesh_corner.x + 0.25 * _map_mesh_dims.x - var longitude := fposmod(_camera.transform.origin.x - left, _map_mesh_dims.x * 0.5) - _camera.transform.origin.x = left + longitude - _camera.transform.origin.z = clamp(_camera.transform.origin.z, _map_mesh_corner.y, _map_mesh_corner.y + _map_mesh_dims.y) + var longitude := fposmod(_camera.position.x - left, _map_mesh_dims.x * 0.5) + _camera.position.x = left + longitude + _camera.position.z = clamp(_camera.position.z, _map_mesh_corner.y, _map_mesh_corner.y + _map_mesh_dims.y) func _zoom_process(delta : float) -> void: - var height := _camera.transform.origin.y + var height := _camera.position.y var zoom := _zoom_target - height height += zoom * _zoom_speed * delta var new_zoom := _zoom_target - height # Set to target if height is within _zoom_epsilon of it or has overshot past it if abs(new_zoom) < _zoom_epsilon or sign(zoom) != sign(new_zoom): height = _zoom_target - _camera.transform.origin.y = height + _camera.position.y = height func _update_orientation() -> void: - var dir := Vector3(0, -1, -exp(-_camera.transform.origin.y * 2.0 + 0.5)) - _camera.look_at(_camera.transform.origin + dir) + var dir := Vector3(0, -1, -exp(-_camera.position.y * 2.0 + 0.5)) + _camera.look_at(_camera.position + dir) func _update_mouse_map_position() -> void: - var mouse_pos_window := get_viewport().get_mouse_position() - var ray_origin := _camera.project_ray_origin(mouse_pos_window) - var ray_normal := _camera.project_ray_normal(mouse_pos_window) + var ray_origin := _camera.project_ray_origin(_mouse_pos_window) + var ray_normal := _camera.project_ray_normal(_mouse_pos_window) # Plane with normal (0,1,0) facing upwards, at a distance 0 from the origin var intersection = Plane(0, 1, 0, 0).intersects_ray(ray_origin, ray_normal) if typeof(intersection) == TYPE_VECTOR3: var intersection_vec := intersection as Vector3 # This loops both horizontally (good) and vertically (bad) - _mouse_pos = (Vector2(intersection_vec.x, intersection_vec.z) - _map_mesh_corner) / _map_mesh_dims - _map_shader_material.set_shader_parameter(_shader_param_mouse_pos, _mouse_pos) + _mouse_pos_map = (Vector2(intersection_vec.x, intersection_vec.z) - _map_mesh_corner) / _map_mesh_dims + _map_shader_material.set_shader_parameter(_shader_param_hover_pos, _mouse_pos_map) else: # Normals parallel to the xz-plane could cause null intersections, # but the camera's orientation should prevent such normals diff --git a/game/src/GameSession/MapView.tscn b/game/src/GameSession/MapView.tscn index 927d162..457bc8e 100644 --- a/game/src/GameSession/MapView.tscn +++ b/game/src/GameSession/MapView.tscn @@ -7,8 +7,8 @@ [sub_resource type="ShaderMaterial" id="ShaderMaterial_tayeg"] render_priority = 0 shader = ExtResource("1_upocn") -shader_parameter/selection_hover = 0.8 -shader_parameter/mouse_pos = Vector2(0.5, 0.5) +shader_parameter/hover_pos = Vector2(0.5, 0.5) +shader_parameter/selected_pos = Vector2(0.5, 0.5) shader_parameter/terrain_tex = ExtResource("3_l8pnf") [sub_resource type="PlaneMesh" id="PlaneMesh_skc48"] diff --git a/game/src/GameSession/ProvinceOverviewPanel.gd b/game/src/GameSession/ProvinceOverviewPanel.gd index f68ac73..434f6b1 100644 --- a/game/src/GameSession/ProvinceOverviewPanel.gd +++ b/game/src/GameSession/ProvinceOverviewPanel.gd @@ -1,13 +1,22 @@ extends Panel -@export var ProvinceID: String = "ID not loaded" -# Called when the node enters the scene tree for the first time. +@export var _province_name_label : Label + +@export var province_identifier: String = "": + get: return province_identifier + set(v): + province_identifier = v + update_info() + func _ready(): - set_id() + update_info() -func set_id(): - $VBoxContainer/ProvinceName.text = str(ProvinceID)+"_NAME" +func update_info() -> void: + _province_name_label.text = province_identifier + "_NAME" + visible = not province_identifier.is_empty() +func _on_province_selected(identifier : String) -> void: + province_identifier = identifier -func _on_button_pressed(): - queue_free() +func _on_button_pressed() -> void: + province_identifier = "" diff --git a/game/src/GameSession/ProvinceOverviewPanel.tscn b/game/src/GameSession/ProvinceOverviewPanel.tscn index c31ff6e..e21b1c3 100644 --- a/game/src/GameSession/ProvinceOverviewPanel.tscn +++ b/game/src/GameSession/ProvinceOverviewPanel.tscn @@ -2,7 +2,7 @@ [ext_resource type="Script" path="res://src/GameSession/ProvinceOverviewPanel.gd" id="1_3n8k5"] -[node name="ProvinceOverviewPanel" type="Panel"] +[node name="ProvinceOverviewPanel" type="Panel" node_paths=PackedStringArray("_province_name_label")] anchors_preset = 2 anchor_top = 1.0 anchor_bottom = 1.0 @@ -10,16 +10,7 @@ offset_top = -300.0 offset_right = 200.0 grow_vertical = 0 script = ExtResource("1_3n8k5") - -[node name="Button" type="Button" parent="."] -custom_minimum_size = Vector2(30, 30) -layout_mode = 1 -anchors_preset = 1 -anchor_left = 1.0 -anchor_right = 1.0 -offset_left = -30.0 -offset_bottom = 30.0 -grow_horizontal = 0 +_province_name_label = NodePath("VBoxContainer/ProvinceName") [node name="VBoxContainer" type="VBoxContainer" parent="."] layout_mode = 1 @@ -33,7 +24,17 @@ offset_bottom = -5.0 [node name="ProvinceName" type="Label" parent="VBoxContainer"] layout_mode = 2 -text = "1_NAME" +text = "PROVINCE_NAME" vertical_alignment = 1 +[node name="Button" type="Button" parent="."] +custom_minimum_size = Vector2(30, 30) +layout_mode = 1 +anchors_preset = -1 +anchor_left = 0.85 +anchor_right = 1.0 +anchor_bottom = 0.103333 +grow_horizontal = 0 +text = "X" + [connection signal="pressed" from="Button" to="." method="_on_button_pressed"] diff --git a/game/src/GameSession/TerrainMap.gdshader b/game/src/GameSession/TerrainMap.gdshader index 1e5e6b9..f78779d 100644 --- a/game/src/GameSession/TerrainMap.gdshader +++ b/game/src/GameSession/TerrainMap.gdshader @@ -6,9 +6,10 @@ render_mode unshaded; uniform sampler2D terrain_tex: source_color, repeat_enable; // Province shape texture uniform sampler2D province_tex: source_color, repeat_enable; -uniform float selection_hover = 0.8; -// Mouse position in UV coords over the map mesh -uniform vec2 mouse_pos; +// Position of the mouse over the map mesh in UV coords +uniform vec2 hover_pos; +// Position in UV coords of a pixel belonging to the currently selected province +uniform vec2 selected_pos; // Transform map mesh UV coordinates to account for the extra // half map on either side. This takes the x-coord from [0 -> 1] @@ -17,11 +18,17 @@ vec2 fix_uv(vec2 uv) { return vec2(uv.x * 2.0 - 0.5, uv.y); } +const vec3 NULL_COLOUR = vec3(0.0); + void fragment() { vec2 fixed_uv = fix_uv(UV); + vec3 terrain_colour = texture(terrain_tex, fixed_uv).rgb; vec3 prov_colour = texture(province_tex, fixed_uv).rgb; - vec3 mouse_colour = texture(province_tex, fix_uv(mouse_pos)).rgb; - // Boost prov_colour's contribution if the mouse is over that colour and it isn't (0,0,0) - float mix_val = prov_colour == mouse_colour && mouse_colour != vec3(0.0) ? selection_hover : 0.4; - ALBEDO = mix(texture(terrain_tex, fixed_uv).rgb, prov_colour, mix_val); + vec3 hover_colour = texture(province_tex, fix_uv(hover_pos)).rgb; + vec3 selected_colour = texture(province_tex, fix_uv(selected_pos)).rgb; + // Boost prov_colour's contribution if it matches hover_colour or selected_colour + float mix_val = float(prov_colour == hover_colour) * 0.3 + float(prov_colour == selected_colour) * 0.5; + // Set to 0 if the province has NULL colour + mix_val *= 1.0 - float(prov_colour == NULL_COLOUR); + ALBEDO = mix(terrain_colour, prov_colour, mix_val); } -- cgit v1.2.3-56-ga3b1