60 lines
1.8 KiB
GDScript
60 lines
1.8 KiB
GDScript
extends Node3D
|
|
|
|
var camera: Camera3D
|
|
var focal_point: Node3D
|
|
|
|
var is_dragging_position = false
|
|
var is_dragging_rotation = false
|
|
|
|
var camera_distance: float = 5
|
|
|
|
func _ready():
|
|
camera = self.get_node("Camera")
|
|
focal_point = self.get_node("FocalPoint")
|
|
Input.set_use_accumulated_input(false)
|
|
|
|
func _process(delta):
|
|
var adjusting = Input.is_action_pressed("cam_adjust")
|
|
is_dragging_position = adjusting and Input.is_action_pressed("cam_pan_mod")
|
|
is_dragging_rotation = adjusting and Input.is_action_pressed("cam_rot_mod")
|
|
|
|
if is_dragging_position or is_dragging_rotation:
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED)
|
|
else:
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
|
|
camera.transform = focal_point.transform.translated(Vector3(0, 0, camera_distance))
|
|
|
|
func _input(event):
|
|
if event is InputEventMouseMotion:
|
|
self.mouse_move(event)
|
|
|
|
if event is InputEventMouseButton:
|
|
self.mouse_button(event)
|
|
|
|
func mouse_button(event: InputEventMouseButton):
|
|
if Input.is_action_pressed("cam_adjust"):
|
|
if event.pressed:
|
|
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
|
camera_distance += 1
|
|
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
|
|
camera_distance -= 1
|
|
|
|
if camera_distance < 1:
|
|
camera_distance = 1
|
|
|
|
func mouse_move(event: InputEventMouseMotion):
|
|
var mouse_delta: Vector2 = event.relative
|
|
|
|
if is_dragging_position:
|
|
var relative_movement = camera.quaternion * Vector3(-mouse_delta.x, mouse_delta.y, 0) * 0.005
|
|
focal_point.position += relative_movement
|
|
|
|
if is_dragging_rotation:
|
|
focal_point.rotation.x += deg2rad(-mouse_delta.y * 0.022 * 6)
|
|
focal_point.rotation.y += deg2rad(-mouse_delta.x * 0.022 * 6)
|
|
if focal_point.rotation.x > PI / 2:
|
|
focal_point.rotation.x = PI / 2
|
|
if focal_point.rotation.x < -PI/ 2:
|
|
focal_point.rotation.x = -PI / 2
|