Created
October 19, 2024 00:27
-
-
Save Pizzaandy/770ab043e1477acbb7d35ecd88078e75 to your computer and use it in GitHub Desktop.
Simple Zoom + Pan Camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extends Camera2D | |
| @export_range(1, 20, 0.01) var maxZoom : float = 10.0 | |
| @export_range(0.01, 1, 0.01) var minZoom : float = 0.1 | |
| @export_range(0.01, 0.2, 0.01) var zoomStepRatio : float = 0.1 | |
| @onready var zoom_scale: float = clamp(zoom.x, minZoom, maxZoom) | |
| func _input(event: InputEvent) -> void: | |
| if Input.is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE) or Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT): | |
| var motion = event as InputEventMouseMotion | |
| if motion != null: | |
| global_position -= motion.relative / zoom_scale | |
| var prev_zoom = zoom_scale | |
| var scroll = event as InputEventMouseButton | |
| if scroll != null: | |
| if scroll.button_index == MOUSE_BUTTON_WHEEL_UP: | |
| zoom_scale += zoom_scale * zoomStepRatio | |
| elif scroll.button_index == MOUSE_BUTTON_WHEEL_DOWN: | |
| zoom_scale -= zoom_scale * zoomStepRatio | |
| else: | |
| return | |
| else: | |
| return | |
| zoom_scale = clamp(zoom_scale, minZoom, maxZoom) | |
| var zoom_ratio = prev_zoom / zoom_scale | |
| global_position -= (get_global_mouse_position() - global_position) * (zoom_ratio - 1) | |
| zoom = Vector2.ONE * zoom_scale |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment