controls.gd 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. extends VBoxContainer
  2. const ROT_SPEED = 0.003
  3. const ZOOM_SPEED = 0.5
  4. const MAIN_BUTTONS = MOUSE_BUTTON_MASK_LEFT | MOUSE_BUTTON_MASK_MIDDLE | MOUSE_BUTTON_MASK_RIGHT
  5. @export var camera: Camera3D
  6. @export var camera_holder: Node3D
  7. @export var rotation_x: Node3D
  8. @export var node_to_move: Node3D
  9. @export var rigid_body: RigidBody3D
  10. @onready var zoom := camera.position.z
  11. var base_height: int = ProjectSettings.get_setting("display/window/size/viewport_height")
  12. @onready var rot_x := rotation_x.rotation.x
  13. @onready var rot_y := camera_holder.rotation.y
  14. func _ready() -> void:
  15. if OS.has_feature("double"):
  16. %HelpLabel.text = "Double precision is enabled in this engine build.\nNo shaking should occur at high coordinate levels\n(±65,536 or more on any axis)."
  17. %HelpLabel.add_theme_color_override("font_color", Color(0.667, 1, 0.667))
  18. func _process(delta: float) -> void:
  19. %Coordinates.text = "X: [color=#fb9]%f[/color]\nY: [color=#bfa]%f[/color]\nZ: [color=#9cf]%f[/color]" % [node_to_move.position.x, node_to_move.position.y, node_to_move.position.z]
  20. if %IncrementX.button_pressed:
  21. node_to_move.position.x += 10_000 * delta
  22. if %IncrementY.button_pressed:
  23. node_to_move.position.y += 100_000 * delta
  24. if %IncrementZ.button_pressed:
  25. node_to_move.position.z += 1_000_000 * delta
  26. func _input(event: InputEvent) -> void:
  27. if event is InputEventMouseButton:
  28. if event.button_index == MOUSE_BUTTON_WHEEL_UP:
  29. zoom -= ZOOM_SPEED
  30. if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
  31. zoom += ZOOM_SPEED
  32. zoom = clampf(zoom, 4, 15)
  33. camera.position.z = zoom
  34. if event is InputEventMouseMotion and event.button_mask & MAIN_BUTTONS:
  35. # Compensate motion speed to be resolution-independent (based on the window height).
  36. var relative_motion: Vector2 = event.relative * DisplayServer.window_get_size().y / base_height
  37. rot_y -= relative_motion.x * ROT_SPEED
  38. rot_x -= relative_motion.y * ROT_SPEED
  39. rot_x = clampf(rot_x, -1.4, 0.16)
  40. camera_holder.transform.basis = Basis.from_euler(Vector3(0, rot_y, 0))
  41. rotation_x.transform.basis = Basis.from_euler(Vector3(rot_x, 0, 0))
  42. func _on_go_to_button_pressed(x_position: int) -> void:
  43. if x_position == 0:
  44. # Reset all coordinates, not just X.
  45. node_to_move.position = Vector3.ZERO
  46. else:
  47. node_to_move.position.x = x_position
  48. func _on_open_documentation_pressed() -> void:
  49. OS.shell_open("https://docs.godotengine.org/en/latest/tutorials/physics/large_world_coordinates.html")