diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2023-11-08 12:29:17 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2023-11-08 12:29:17 +0100 |
commit | 1cb10e65d14c8e7690c797975cbc7945670ca6a9 (patch) | |
tree | fd5834a5ae167d7ae4950f6aa6eda27a8dd8254e /demo | |
parent | c2a17228c19c4f959367b139e39c5e24dd7edd36 (diff) | |
download | godot-hexgrid-1cb10e65d14c8e7690c797975cbc7945670ca6a9.zip godot-hexgrid-1cb10e65d14c8e7690c797975cbc7945670ca6a9.tar.gz |
Demo : fix zoom
Diffstat (limited to 'demo')
-rw-r--r-- | demo/Camera.gd | 13 | ||||
-rw-r--r-- | demo/Main.gd | 2 |
2 files changed, 9 insertions, 6 deletions
diff --git a/demo/Camera.gd b/demo/Camera.gd index 0dc6074..93ae41c 100644 --- a/demo/Camera.gd +++ b/demo/Camera.gd @@ -1,5 +1,7 @@ extends Camera2D +const MAX_ZOOM = 1 + var margin :Vector2 var window : Vector2 var map_center : Vector2 @@ -7,15 +9,16 @@ var texture_size : Vector2 var zoom_boundaries : Vector2 func _ready(): - margin = Vector2(0, 0) + margin = Vector2(50, 50) func configure(w : Vector2, c : Vector2, ts : Vector2) -> void: window = w map_center = c texture_size = ts - var zout : float = max((texture_size.x + margin.x) / window.x, (texture_size.y + margin.y) / window.y) - zoom_boundaries = Vector2(zout - 0.5, zout) - update_camera(0, 0, zoom_boundaries.y) + var zout : float = min(window.x / (texture_size.x + margin.x), window.y / (texture_size.y + margin.y)) + zoom_boundaries = Vector2(zout, zout + MAX_ZOOM) + zoom = Vector2(zout, zout) + position = map_center func update_camera(x : float, y : float, z : float) -> void: if z != 0: @@ -23,7 +26,7 @@ func update_camera(x : float, y : float, z : float) -> void: zoom.y = zoom.x position.x += x position.y += y - var delta = texture_size + margin - (window * zoom.x) + var delta = texture_size + margin - (window / zoom.x) if (delta.x <= 0): position.x = map_center.x else: diff --git a/demo/Main.gd b/demo/Main.gd index 8aad56f..50c0ab6 100644 --- a/demo/Main.gd +++ b/demo/Main.gd @@ -29,7 +29,7 @@ func on_rotate() -> void: on_viewport_resized() func on_zoom(b : bool) -> void: - Camera.update_camera(0, 0, -0.05 if b else 0.05) + Camera.update_camera(0, 0, 0.05 if b else -0.05) func on_toggle() -> void: Map.set_mode(UI.get_node("LOS").is_pressed(), UI.get_node("Move").is_pressed(), UI.get_node("Influence").is_pressed()) |