diff options
author | Hop311 <Hop3114@gmail.com> | 2023-09-14 22:17:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-14 22:17:56 +0200 |
commit | d56941e60016bf73587c63a36ac5ca9da3f6023e (patch) | |
tree | 2085b1f4798811f52025d795b2d61b2959b78122 /game/src | |
parent | 9de41a0ed5f050f205219ab2a9f114800d048c14 (diff) | |
parent | 503cf424a3bd993df66f6a68b09603adfbb91650 (diff) |
Merge pull request #155 from Ben1152000/zoom-to-cursor
Implemented zoom-to-cursor functionality
Diffstat (limited to 'game/src')
-rw-r--r-- | game/src/Game/GameSession/MapView.gd | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/game/src/Game/GameSession/MapView.gd b/game/src/Game/GameSession/MapView.gd index a2fe6b1..345ab9e 100644 --- a/game/src/Game/GameSession/MapView.gd +++ b/game/src/Game/GameSession/MapView.gd @@ -20,15 +20,18 @@ var _drag_anchor : Vector2 var _drag_active : bool = false var _mouse_over_viewport : bool = true +var _window_in_focus : bool = true @export var _zoom_target_min : float = 0.05 @export var _zoom_target_max : float = 5.0 @export var _zoom_target_step : float = 0.1 -@export var _zoom_epsilon : float = _zoom_target_step * 0.1 +@export var _zoom_epsilon : float = _zoom_target_step * 0.005 @export var _zoom_speed : float = 5.0 @export var _zoom_target : float = 1.0: get: return _zoom_target set(v): _zoom_target = clamp(v, _zoom_target_min, _zoom_target_max) +const _zoom_position_multiplier = 3.14159 # Horizontal movement coefficient during zoom +var _zoom_position : Vector2 @export var _map_mesh_instance : MeshInstance3D var _map_mesh : MapMesh @@ -90,6 +93,10 @@ func _notification(what : int): _on_mouse_entered_viewport() NOTIFICATION_WM_MOUSE_EXIT: # Mouse out of window _on_mouse_exited_viewport() + NOTIFICATION_WM_WINDOW_FOCUS_IN: # Window comes into focus + _on_window_entered_focus() + NOTIFICATION_WM_WINDOW_FOCUS_OUT: # Window goes out of focus + _on_window_exited_focus() func _world_to_map_coords(pos : Vector3) -> Vector2: return (Vector2(pos.x, pos.z) - _map_mesh_corner) / _map_mesh_dims @@ -109,9 +116,13 @@ func _viewport_to_map_coords(pos_viewport : Vector2) -> Vector2: func zoom_in() -> void: _zoom_target -= _zoom_target_step + _zoom_position = (Vector2(0.5, 0.5) - _mouse_pos_viewport * GuiScale.get_current_guiscale() / _viewport_dims) * _zoom_position_multiplier func zoom_out() -> void: _zoom_target += _zoom_target_step + # For some reason, zooming out in the original game does not consider the + # cursor location. I'm not sure if we want to preserve this behavior. + _zoom_position = Vector2() func _on_province_selected(index : int) -> void: _map_shader_material.set_shader_parameter(GameLoader.ShaderManager.param_selected_index, index) @@ -170,11 +181,12 @@ func _movement_process(delta : float) -> void: # REQUIREMENTS # * UIFUN-125 func _edge_scrolling_vector() -> Vector2: - if not _mouse_over_viewport: + if not _window_in_focus: return Vector2() var mouse_vector := _mouse_pos_viewport * GuiScale.get_current_guiscale() / _viewport_dims - Vector2(0.5, 0.5) + # Only scroll if outside the move threshold. if abs(mouse_vector.x) < 0.5 - _edge_move_threshold and abs(mouse_vector.y) < 0.5 - _edge_move_threshold: - mouse_vector *= 0 + return Vector2() return mouse_vector * _edge_move_speed # REQUIREMENTS @@ -196,12 +208,11 @@ func _clamp_over_map() -> void: func _zoom_process(delta : float) -> void: var height := _camera.position.y var zoom := _zoom_target - height - height += zoom * _zoom_speed * delta - var new_zoom := _zoom_target - height + var zoom_delta = zoom * _zoom_speed * delta # 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.position.y = height + if abs(zoom - zoom_delta) < _zoom_epsilon or sign(zoom) != sign(zoom - zoom_delta): + zoom_delta = zoom + _camera.position += Vector3(_zoom_position.x * zoom_delta * int(_mouse_over_viewport), zoom_delta, _zoom_position.y * zoom_delta * int(_mouse_over_viewport)) func _update_orientation() -> void: var dir := Vector3(0, -1, -exp(-_camera.position.y - 1)) @@ -227,6 +238,12 @@ func _on_mouse_exited_viewport(): _mouse_over_viewport = false _map_shader_material.set_shader_parameter(GameLoader.ShaderManager.param_hover_index, 0) +func _on_window_entered_focus(): + _window_in_focus = true + +func _on_window_exited_focus(): + _window_in_focus = false + func _on_minimap_clicked(pos_clicked : Vector2): pos_clicked *= _map_mesh_dims _camera.position.x = pos_clicked.x |