2019-08-18 09:28:38 +00:00
|
|
|
extends Camera2D
|
|
|
|
|
|
|
|
var zoom_min := Vector2(0.005, 0.005)
|
2019-09-09 22:57:46 +00:00
|
|
|
var zoom_max := Vector2.ONE
|
2019-09-25 19:59:48 +00:00
|
|
|
var viewport_container : ViewportContainer
|
2019-08-18 09:28:38 +00:00
|
|
|
var drag := false
|
|
|
|
|
2019-09-25 19:59:48 +00:00
|
|
|
func _ready() -> void:
|
|
|
|
viewport_container = get_parent().get_parent()
|
|
|
|
|
2019-12-05 23:48:29 +00:00
|
|
|
func _input(event : InputEvent) -> void:
|
2019-09-25 19:59:48 +00:00
|
|
|
var mouse_pos := viewport_container.get_local_mouse_position()
|
|
|
|
var viewport_size := viewport_container.rect_size
|
2020-01-27 22:19:55 +00:00
|
|
|
if event.is_action_pressed("middle_mouse") || event.is_action_pressed("space"):
|
2019-09-25 19:59:48 +00:00
|
|
|
drag = true
|
2020-01-27 22:19:55 +00:00
|
|
|
elif event.is_action_released("middle_mouse") || event.is_action_released("space"):
|
2019-09-25 19:59:48 +00:00
|
|
|
drag = false
|
2019-10-25 14:38:38 +00:00
|
|
|
|
2019-12-06 23:08:23 +00:00
|
|
|
if Global.can_draw && Rect2(Vector2.ZERO, viewport_size).has_point(mouse_pos):
|
2019-09-25 19:59:48 +00:00
|
|
|
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
|
2019-09-09 22:57:46 +00:00
|
|
|
if zoom + zoom_margin > zoom_min:
|
|
|
|
zoom += zoom_margin
|
2019-10-25 14:38:38 +00:00
|
|
|
|
2019-09-09 22:57:46 +00:00
|
|
|
if zoom > zoom_max:
|
|
|
|
zoom = zoom_max
|
2019-09-25 19:59:48 +00:00
|
|
|
if name == "Camera2D":
|
2019-11-21 17:10:03 +00:00
|
|
|
Global.zoom_level_label.text = str(round(100 / Global.camera.zoom.x)) + " %"
|