1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-19 01:29:49 +00:00
Pixelorama/Scripts/CameraMovement.gd

37 lines
1.2 KiB
GDScript3
Raw Normal View History

2019-08-18 09:28:38 +00:00
extends Camera2D
var zoom_min := Vector2(0.005, 0.005)
var zoom_max := Vector2.ONE
var viewport_container : ViewportContainer
2019-08-18 09:28:38 +00:00
var drag := false
func _ready() -> void:
viewport_container = get_parent().get_parent()
func _input(event : InputEvent) -> void:
var mouse_pos := viewport_container.get_local_mouse_position()
var viewport_size := viewport_container.rect_size
2020-01-09 18:49:27 +00:00
if event.is_action_pressed("camera_drag") || event.is_action_pressed("space"):
drag = true
2020-01-09 18:49:27 +00:00
elif event.is_action_released("camera_drag") || event.is_action_released("space"):
drag = false
if Global.can_draw && Rect2(Vector2.ZERO, viewport_size).has_point(mouse_pos):
if event.is_action_pressed("zoom_in"): # Wheel Up Event
2019-08-18 09:28:38 +00:00
zoom_camera(-1)
elif event.is_action_pressed("zoom_out"): # Wheel Down Event
zoom_camera(1)
elif event is InputEventMouseMotion && drag:
offset = offset - event.relative * zoom
# Zoom Camera
func zoom_camera(dir : int) -> void:
var zoom_margin = zoom * dir / 10
if zoom + zoom_margin > zoom_min:
zoom += zoom_margin
if zoom > zoom_max:
zoom = zoom_max
if name == "Camera2D":
Global.zoom_level_label.text = str(round(100 / Global.camera.zoom.x)) + " %"