From 5f09e4720581994ca88446add32d8b6189428b13 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Mon, 10 Apr 2023 23:27:08 +0100 Subject: Minimap viewport loops from x=0 to x=1 --- game/art/terrain/terrain.png.import | 2 +- game/art/ui/minimap.png.import | 8 +-- game/art/ui/minimap_frame.png.import | 8 +-- game/src/GameSession/GameSession.tscn | 8 +-- game/src/GameSession/MapControlPanel.gd | 24 +++------ game/src/GameSession/MapControlPanel.tscn | 49 ++++++++--------- game/src/GameSession/MapView.gd | 62 +++++++++++----------- game/src/GameSession/MapView.tscn | 4 +- game/src/GameSession/Minimap.gd | 88 +++++++++++++++++++++++++------ game/src/GameSession/TerrainMap.gdshader | 11 ++-- 10 files changed, 150 insertions(+), 114 deletions(-) (limited to 'game') diff --git a/game/art/terrain/terrain.png.import b/game/art/terrain/terrain.png.import index 2642176..d7a078f 100644 --- a/game/art/terrain/terrain.png.import +++ b/game/art/terrain/terrain.png.import @@ -25,7 +25,7 @@ mipmaps/generate=true mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" -process/fix_alpha_border=true +process/fix_alpha_border=false process/premult_alpha=false process/normal_map_invert_y=false process/hdr_as_srgb=false diff --git a/game/art/ui/minimap.png.import b/game/art/ui/minimap.png.import index 31da5f0..36f22e7 100644 --- a/game/art/ui/minimap.png.import +++ b/game/art/ui/minimap.png.import @@ -15,20 +15,20 @@ dest_files=["res://.godot/imported/minimap.png-05ee44469856e77fd05107fe9e259e25. [params] -compress/mode=0 +compress/mode=3 compress/high_quality=false compress/lossy_quality=0.7 compress/hdr_compression=1 -compress/normal_map=0 +compress/normal_map=2 compress/channel_pack=0 mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" -process/fix_alpha_border=true +process/fix_alpha_border=false process/premult_alpha=false process/normal_map_invert_y=false process/hdr_as_srgb=false process/hdr_clamp_exposure=false process/size_limit=0 -detect_3d/compress_to=1 +detect_3d/compress_to=0 diff --git a/game/art/ui/minimap_frame.png.import b/game/art/ui/minimap_frame.png.import index 6db81bc..3d2aeee 100644 --- a/game/art/ui/minimap_frame.png.import +++ b/game/art/ui/minimap_frame.png.import @@ -15,20 +15,20 @@ dest_files=["res://.godot/imported/minimap_frame.png-3668393435d2582fb1dc8a95985 [params] -compress/mode=0 +compress/mode=3 compress/high_quality=false compress/lossy_quality=0.7 compress/hdr_compression=1 -compress/normal_map=0 +compress/normal_map=2 compress/channel_pack=0 mipmaps/generate=false mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" -process/fix_alpha_border=true +process/fix_alpha_border=false process/premult_alpha=false process/normal_map_invert_y=false process/hdr_as_srgb=false process/hdr_clamp_exposure=false process/size_limit=0 -detect_3d/compress_to=1 +detect_3d/compress_to=0 diff --git a/game/src/GameSession/GameSession.tscn b/game/src/GameSession/GameSession.tscn index 76a5fcc..bba5671 100644 --- a/game/src/GameSession/GameSession.tscn +++ b/game/src/GameSession/GameSession.tscn @@ -41,11 +41,11 @@ anchor_right = 0.15625 offset_top = 0.0 offset_right = 0.0 -[connection signal="map_view_camera_changed" from="MapView" to="MapControlPanel" method="_on_map_view_map_view_camera_changed"] +[connection signal="map_view_camera_changed" from="MapView" to="MapControlPanel" method="_on_map_view_camera_changed"] [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="camera_change" from="MapControlPanel" to="MapView" method="_on_map_control_panel_camera_change"] [connection signal="game_session_menu_button_pressed" from="MapControlPanel" to="." method="_on_game_session_menu_button_pressed"] [connection signal="mapmode_changed" from="MapControlPanel" to="MapView" method="_update_colour_texture"] -[connection signal="mouse_entered" from="MapControlPanel" to="MapView" method="_on_map_control_panel_mouse_entered"] -[connection signal="mouse_exited" from="MapControlPanel" to="MapView" method="_on_map_control_panel_mouse_exited"] +[connection signal="minimap_clicked" from="MapControlPanel" to="MapView" method="_on_minimap_clicked"] +[connection signal="mouse_entered" from="MapControlPanel" to="MapView" method="_on_mouse_exited_viewport"] +[connection signal="mouse_exited" from="MapControlPanel" to="MapView" method="_on_mouse_entered_viewport"] diff --git a/game/src/GameSession/MapControlPanel.gd b/game/src/GameSession/MapControlPanel.gd index b9aaaeb..fb4f161 100644 --- a/game/src/GameSession/MapControlPanel.gd +++ b/game/src/GameSession/MapControlPanel.gd @@ -2,15 +2,10 @@ extends PanelContainer signal game_session_menu_button_pressed signal mapmode_changed -signal camera_change(_camera_pos_clicked: Vector2) - -const _action_click : StringName = &"map_click" +signal map_view_camera_changed(near_left : Vector2, far_left : Vector2, far_right : Vector2, near_right : Vector2) +signal minimap_clicked(pos_clicked : Vector2) @export var _mapmodes_grid : GridContainer -@export var _minimap : PanelContainer -@onready var _map_camera : Control = _minimap.get_node("RectangularCamera") - -var _mouse_inside: bool = false var _mapmode_button_group : ButtonGroup @@ -39,15 +34,8 @@ func _mapmode_pressed(button : BaseButton) -> void: MapSingleton.set_mapmode(button.tooltip_text) mapmode_changed.emit() -func _on_map_view_map_view_camera_changed(near_left, far_left, far_right, near_right): - _map_camera._on_camera_view_changed(near_left, far_left, far_right, near_right) - -func _process(delta): - if _mouse_inside and Input.is_action_pressed(_action_click): - camera_change.emit($VBoxContainer/Minimap.get_local_mouse_position()) - -func _on_minimap_mouse_entered(): - _mouse_inside = true +func _on_map_view_camera_changed(near_left : Vector2, far_left : Vector2, far_right : Vector2, near_right : Vector2) -> void: + map_view_camera_changed.emit(near_left, far_left, far_right, near_right) -func _on_minimap_mouse_exited(): - _mouse_inside = false +func _on_minimap_clicked(pos_clicked : Vector2) -> void: + minimap_clicked.emit(pos_clicked) diff --git a/game/src/GameSession/MapControlPanel.tscn b/game/src/GameSession/MapControlPanel.tscn index daf0c96..b76af10 100644 --- a/game/src/GameSession/MapControlPanel.tscn +++ b/game/src/GameSession/MapControlPanel.tscn @@ -11,55 +11,50 @@ action = &"ui_cancel" [sub_resource type="Shortcut" id="Shortcut_fc1tk"] events = [SubResource("InputEventAction_5nck3")] -[node name="PanelContainer" type="PanelContainer" node_paths=PackedStringArray("_mapmodes_grid", "_minimap")] +[node name="MapControlPanel" type="PanelContainer" node_paths=PackedStringArray("_mapmodes_grid")] editor_description = "SS-103" mouse_filter = 1 script = ExtResource("1_ign64") -_mapmodes_grid = NodePath("VBoxContainer/HBoxContainer/VBoxContainer/MapmodesGrid") -_minimap = NodePath("VBoxContainer/Minimap") +_mapmodes_grid = NodePath("HBoxContainer/VBoxContainer/MapmodesGrid") -[node name="VBoxContainer" type="VBoxContainer" parent="."] -layout_mode = 2 - -[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"] +[node name="HBoxContainer" type="HBoxContainer" parent="."] layout_mode = 2 alignment = 2 -[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/HBoxContainer"] +[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer"] layout_mode = 2 -[node name="MapmodesGrid" type="GridContainer" parent="VBoxContainer/HBoxContainer/VBoxContainer"] +[node name="MapmodesGrid" type="GridContainer" parent="HBoxContainer/VBoxContainer"] layout_mode = 2 columns = 11 -[node name="AuxiliaryPanel" type="VBoxContainer" parent="VBoxContainer/HBoxContainer"] -editor_description = "UI-761" -layout_mode = 2 - -[node name="GameSessionMenuButton" type="Button" parent="VBoxContainer/HBoxContainer/AuxiliaryPanel"] -editor_description = "UI-9" -layout_mode = 2 -shortcut = SubResource("Shortcut_fc1tk") -text = "ESC" - -[node name="Minimap" type="PanelContainer" parent="VBoxContainer"] +[node name="Minimap" type="PanelContainer" parent="HBoxContainer/VBoxContainer"] layout_mode = 2 mouse_filter = 1 -[node name="TextureRect" type="TextureRect" parent="VBoxContainer/Minimap"] +[node name="TextureRect" type="TextureRect" parent="HBoxContainer/VBoxContainer/Minimap"] layout_mode = 2 texture = ExtResource("2_r613r") -[node name="RectangularCamera" type="Control" parent="VBoxContainer/Minimap" node_paths=PackedStringArray("_background")] +[node name="ViewportQuad" type="Control" parent="HBoxContainer/VBoxContainer/Minimap"] layout_mode = 2 mouse_filter = 2 script = ExtResource("3_s4dml") -_background = NodePath("../TextureRect") -[node name="Frame" type="NinePatchRect" parent="VBoxContainer/Minimap"] +[node name="Frame" type="NinePatchRect" parent="HBoxContainer/VBoxContainer/Minimap"] layout_mode = 2 texture = ExtResource("4_f1exl") -[connection signal="pressed" from="VBoxContainer/HBoxContainer/AuxiliaryPanel/GameSessionMenuButton" to="." method="_on_game_session_menu_button_pressed"] -[connection signal="mouse_entered" from="VBoxContainer/Minimap" to="." method="_on_minimap_mouse_entered"] -[connection signal="mouse_exited" from="VBoxContainer/Minimap" to="." method="_on_minimap_mouse_exited"] +[node name="AuxiliaryPanel" type="VBoxContainer" parent="HBoxContainer"] +editor_description = "UI-761" +layout_mode = 2 + +[node name="GameSessionMenuButton" type="Button" parent="HBoxContainer/AuxiliaryPanel"] +editor_description = "UI-9" +layout_mode = 2 +shortcut = SubResource("Shortcut_fc1tk") +text = "ESC" + +[connection signal="map_view_camera_changed" from="." to="HBoxContainer/VBoxContainer/Minimap/ViewportQuad" method="_on_map_view_camera_changed"] +[connection signal="minimap_clicked" from="HBoxContainer/VBoxContainer/Minimap/ViewportQuad" to="." method="_on_minimap_clicked"] +[connection signal="pressed" from="HBoxContainer/AuxiliaryPanel/GameSessionMenuButton" to="." method="_on_game_session_menu_button_pressed"] diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd index 05da6d5..fc13b07 100644 --- a/game/src/GameSession/MapView.gd +++ b/game/src/GameSession/MapView.gd @@ -14,8 +14,8 @@ const _action_click : StringName = &"map_click" const _shader_param_province_index : StringName = &"province_index_tex" const _shader_param_province_colour : StringName = &"province_colour_tex" -const _shader_param_hover_pos : StringName = &"hover_pos" -const _shader_param_selected_pos : StringName = &"selected_pos" +const _shader_param_hover_index : StringName = &"hover_index" +const _shader_param_selected_index : StringName = &"selected_index" @export var _camera : Camera3D @@ -25,7 +25,7 @@ const _shader_param_selected_pos : StringName = &"selected_pos" var _drag_anchor : Vector2 var _drag_active : bool = false -var _lock_movement : bool = false +var _mouse_over_viewport : bool = false @export var _zoom_target_min : float = 0.2 @export var _zoom_target_max : float = 5.0 @@ -57,17 +57,6 @@ var _viewport_dims : Vector2 = Vector2(1, 1) # to a failed HashMap lookup. I'm not sure if this is a bug in the # editor, GDExtension, my own extension, or a combination of them. # This was an absolute pain to track down. --- hop311 - -func _notification(what): - # Working with MainLoop notifications system. - # https://godotengine.org/qa/101601/how-to-detect-when-my-application-is-not-visible - # https://docs.godotengine.org/en/stable/classes/class_mainloop.html#constants - - if what == 1003: # Mouse out of window - _lock_movement = true - elif what == 1002: # Mouse inside window - _lock_movement = false - func _ready(): if _camera == null: push_error("MapView's _camera variable hasn't been set!") @@ -121,6 +110,13 @@ func _ready(): map_mesh_aabb.position.z - map_mesh_aabb.end.z )) +func _notification(what : int): + match what: + NOTIFICATION_WM_MOUSE_ENTER: # Mouse inside window + _on_mouse_entered_viewport() + NOTIFICATION_WM_MOUSE_EXIT: # Mouse out of window + _on_mouse_exited_viewport() + func _update_colour_texture() -> void: MapSingleton.update_colour_image() _map_province_colour_texture.update(_map_province_colour_image) @@ -145,10 +141,12 @@ func _unhandled_input(event : InputEvent): if event.is_action_pressed(_action_click): # Check if the mouse is outside of bounds if _map_mesh.is_valid_uv_coord(_mouse_pos_map): - _map_shader_material.set_shader_parameter(_shader_param_selected_pos, _mouse_pos_map) - var mouse_pixel_pos := Vector2i(_mouse_pos_map * _map_image_size) - var province_identifier := MapSingleton.get_province_identifier_from_pixel_coords(mouse_pixel_pos) + var selected_index := MapSingleton.get_province_index_from_uv_coords(_mouse_pos_map) + _map_shader_material.set_shader_parameter(_shader_param_selected_index, selected_index) + var province_identifier := MapSingleton.get_province_identifier_from_uv_coords(_mouse_pos_map) province_selected.emit(province_identifier) + else: + print("Clicked outside the map!") elif event.is_action_pressed(_action_drag): if _drag_active: push_warning("Drag being activated while already active!") @@ -190,12 +188,11 @@ func _movement_process(delta : float) -> void: _camera.position += Vector3(direction.x, 0, direction.y) func _edge_scrolling_vector() -> Vector2: + if not _mouse_over_viewport: + return Vector2() var mouse_vector := _mouse_pos_viewport / _viewport_dims - Vector2(0.5, 0.5) - if _lock_movement: - mouse_vector = Vector2(0,0) - else: - if abs(mouse_vector.x) < 0.5 - _edge_move_threshold and abs(mouse_vector.y) < 0.5 - _edge_move_threshold: - mouse_vector *= 0 + if abs(mouse_vector.x) < 0.5 - _edge_move_threshold and abs(mouse_vector.y) < 0.5 - _edge_move_threshold: + mouse_vector *= 0 return mouse_vector * _edge_move_speed func _cardinal_movement_vector() -> Vector2: @@ -232,15 +229,18 @@ func _update_minimap_viewport() -> void: func _update_mouse_map_position() -> void: _mouse_pos_map = _viewport_to_map_coords(_mouse_pos_viewport) - if not _lock_movement: - _map_shader_material.set_shader_parameter(_shader_param_hover_pos, _mouse_pos_map) + var hover_index := MapSingleton.get_province_index_from_uv_coords(_mouse_pos_map) + if not _mouse_over_viewport: + _map_shader_material.set_shader_parameter(_shader_param_hover_index, hover_index) -func _on_map_control_panel_mouse_entered(): - _lock_movement = true +func _on_mouse_entered_viewport(): + _mouse_over_viewport = true -func _on_map_control_panel_mouse_exited(): - _lock_movement = false +func _on_mouse_exited_viewport(): + _mouse_over_viewport = false -func _on_map_control_panel_camera_change(_camera_pos_clicked): - _camera.position.x = (_camera_pos_clicked.x-$"../MapControlPanel"._minimap.size.x/2)/$"../MapControlPanel"._minimap.size.x * _map_mesh_dims.x - _camera.position.z = (_camera_pos_clicked.y-$"../MapControlPanel"._minimap.size.y/2)/$"../MapControlPanel"._minimap.size.y * _map_mesh_dims.y +func _on_minimap_clicked(pos_clicked : Vector2): + pos_clicked *= _map_mesh_dims + _camera.position.x = pos_clicked.x + _camera.position.z = pos_clicked.y + _clamp_over_map() diff --git a/game/src/GameSession/MapView.tscn b/game/src/GameSession/MapView.tscn index f4f3a9e..b48617a 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/hover_pos = Vector2(0.5, 0.5) -shader_parameter/selected_pos = Vector2(0.5, 0.5) +shader_parameter/hover_index = null +shader_parameter/selected_index = null shader_parameter/terrain_tex = ExtResource("3_l8pnf") [sub_resource type="MapMesh" id="MapMesh_3gtsd"] diff --git a/game/src/GameSession/Minimap.gd b/game/src/GameSession/Minimap.gd index a8b0217..5d15ce8 100644 --- a/game/src/GameSession/Minimap.gd +++ b/game/src/GameSession/Minimap.gd @@ -1,27 +1,83 @@ extends Control -@export var _background : TextureRect +signal minimap_clicked(pos_clicked : Vector2) + +const _action_click : StringName = &"map_click" var _viewport_points : PackedVector2Array func _draw() -> void: if _viewport_points.size() > 1: - draw_polyline(_viewport_points, Color.WHITE, -1) + draw_multiline(_viewport_points, Color.WHITE, -1) + +func _unhandled_input(event : InputEvent): + if event.is_action_pressed(_action_click): + var pos_clicked := get_local_mouse_position() / size - Vector2(0.5, 0.5) + if abs(pos_clicked.x) < 0.5 and abs(pos_clicked.y) < 0.5: + minimap_clicked.emit(pos_clicked) + +# Returns the point on the line going through p and q with the specific x coord +func _intersect_x(p : Vector2, q : Vector2, x : float) -> Vector2: + if p.x == q.x: + return Vector2(x, 0.5 * (p.y + q.y)) + var t := (x - q.x) / (p.x - q.x) + return q + t * (p - q) + +# Returns the point on the line going through p and q with the specific y coord +func _intersect_y(p : Vector2, q : Vector2, y : float) -> Vector2: + if p.y == q.y: + return Vector2(0.5 * (p.x + q.x), y) + var t := (y - q.y) / (p.y - q.y) + return q + t * (p - q) + +const _one_x := Vector2(1, 0) + +func _add_line_looped_over_x(left : Vector2, right : Vector2) -> void: + if left.x < 0: + if right.x < 0: + _viewport_points.push_back(left + _one_x) + _viewport_points.push_back(right + _one_x) + else: + var mid_point := _intersect_x(left, right, 0) + _viewport_points.push_back(mid_point) + _viewport_points.push_back(right) + mid_point.x = 1 + _viewport_points.push_back(left + _one_x) + _viewport_points.push_back(mid_point) + elif right.x > 1: + if left.x > 1: + _viewport_points.push_back(left - _one_x) + _viewport_points.push_back(right - _one_x) + else: + var mid_point := _intersect_x(left, right, 1) + _viewport_points.push_back(left) + _viewport_points.push_back(mid_point) + mid_point.x = 0 + _viewport_points.push_back(mid_point) + _viewport_points.push_back(right - _one_x) + else: + _viewport_points.push_back(left) + _viewport_points.push_back(right) + +# This can break if the viewport is rotated too far! +func _on_map_view_camera_changed(near_left : Vector2, far_left : Vector2, far_right : Vector2, near_right : Vector2) -> void: + # Bound far y coords + if far_left.y < 0: + far_left = _intersect_y(near_left, far_left, 0) + if far_right.y < 0: + far_right = _intersect_y(near_right, far_right, 0) + # Bound near y coords + if near_left.y > 1: + near_left = _intersect_y(near_left, far_left, 1) + if near_right.y > 1: + near_right = _intersect_y(near_right, far_right, 1) -func _on_camera_view_changed(near_left : Vector2, far_left : Vector2, far_right : Vector2, near_right : Vector2) -> void: _viewport_points.clear() - _viewport_points.push_back(near_left * _background.size) - _viewport_points.push_back(far_left * _background.size) - _viewport_points.push_back(far_right * _background.size) - _viewport_points.push_back(near_right * _background.size) - _viewport_points.push_back(_viewport_points[0]) - - # Cutting out of Frame part of camera polyline - for i in range(0,_viewport_points.size()): - for j in range(0,2): - if _viewport_points[i][j] > _background.size[j]: - _viewport_points[i][j] = _background.size[j] - elif _viewport_points[i][j] < 0: - _viewport_points[i][j] = 1 + _add_line_looped_over_x(near_left, near_right) + _add_line_looped_over_x(far_left, far_right) + _add_line_looped_over_x(far_left, near_left) + _add_line_looped_over_x(near_right, far_right) + for i in _viewport_points.size(): + _viewport_points[i] *= size queue_redraw() diff --git a/game/src/GameSession/TerrainMap.gdshader b/game/src/GameSession/TerrainMap.gdshader index f3d0c7d..7aca0f9 100644 --- a/game/src/GameSession/TerrainMap.gdshader +++ b/game/src/GameSession/TerrainMap.gdshader @@ -8,10 +8,10 @@ uniform sampler2D terrain_tex: source_color, repeat_enable, filter_linear; uniform sampler2D province_index_tex : repeat_enable, filter_nearest; // Province colour texture uniform sampler2D province_colour_tex: source_color, repeat_enable, filter_nearest; -// 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; +// Index of the mouse over the map mesh +uniform uint hover_index; +// Index of the currently selected province +uniform uint selected_index; uvec2 vec2_to_uvec2(vec2 v) { return uvec2(v * 255.0); @@ -31,10 +31,7 @@ uint read_uint16(sampler2D tex, vec2 uv) { void fragment() { uvec2 prov_idx_split = read_uvec2(province_index_tex, UV); - uint prov_index = uvec2_to_uint(prov_idx_split); - uint hover_index = read_uint16(province_index_tex, hover_pos); - uint selected_index = read_uint16(province_index_tex, selected_pos); // Boost prov_colour's contribution if it matches hover_colour or selected_colour float mix_val = 0.3 + float(prov_index == hover_index) * 0.3 + float(prov_index == selected_index) * 0.3; -- cgit v1.2.3-56-ga3b1