From 4597189f157834c80f56b12b701fd2b2a15c2798 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 8 Sep 2024 22:35:06 +0200 Subject: next commit --- UI/Camera.gd | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 UI/Camera.gd (limited to 'UI/Camera.gd') diff --git a/UI/Camera.gd b/UI/Camera.gd new file mode 100644 index 0000000..4e02662 --- /dev/null +++ b/UI/Camera.gd @@ -0,0 +1,46 @@ +extends Camera2D + + +var is_in_drag_mode = false +var drag_anchor = Vector2.ZERO + +@export var speed = 5 + +@export_group("Zoom", "zoom") +@export var zoom_min: float = 0.5 +@export var zoom_max: float = 3 +@export var zoom_step: float = 0.01 + + +func _input(event): + if event.is_action("camera_zoom_out"): + var new_zoom = max(zoom.x - zoom_step, zoom_min) + zoom = Vector2(new_zoom, new_zoom) + if event.is_action("camera_zoom_in"): + var new_zoom = min(zoom.x + zoom_step, zoom_max) + zoom = Vector2(new_zoom, new_zoom) + #global_position -= (global_position + (get_viewport_rect().size/2) - get_global_mouse_position()) + + if event.is_action_pressed("camera_drag"): + is_in_drag_mode = true + drag_anchor = get_global_mouse_position() + elif event.is_action_released("camera_drag"): + is_in_drag_mode = false + + +func _process(_delta): + if is_in_drag_mode: + global_position += drag_anchor - get_global_mouse_position() + + var direction_h = Input.get_axis("ui_left", "ui_right") + var direction_v = Input.get_axis("ui_up", "ui_down") + global_position += Vector2(direction_h * speed, direction_v * speed) + + +func get_rect(): + var viewport = get_viewport_rect() + + return Rect2( + global_position, + viewport.size * Vector2(1 / zoom.x, 1 / zoom.y) + ) -- cgit v1.2.3