tester.gd 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. extends WorldEnvironment
  2. const ROT_SPEED = 0.003
  3. const ZOOM_SPEED = 0.125
  4. const MAIN_BUTTONS = MOUSE_BUTTON_MASK_LEFT | MOUSE_BUTTON_MASK_RIGHT | MOUSE_BUTTON_MASK_MIDDLE
  5. var tester_index = 0
  6. var rot_x = deg_to_rad(-22.5) # This must be kept in sync with RotationX.
  7. var rot_y = deg_to_rad(90) # This must be kept in sync with CameraHolder.
  8. var zoom = 2.5
  9. var base_height = ProjectSettings.get_setting("display/window/size/viewport_height")
  10. @onready var testers = $Testers
  11. @onready var camera_holder = $CameraHolder # Has a position and rotates on Y.
  12. @onready var rotation_x = $CameraHolder/RotationX
  13. @onready var camera = $CameraHolder/RotationX/Camera3D
  14. func _ready():
  15. camera_holder.transform.basis = Basis.from_euler(Vector3(0, rot_y, 0))
  16. rotation_x.transform.basis = Basis.from_euler(Vector3(rot_x, 0, 0))
  17. update_gui()
  18. func _unhandled_input(event):
  19. if event.is_action_pressed("ui_left"):
  20. _on_previous_pressed()
  21. if event.is_action_pressed("ui_right"):
  22. _on_next_pressed()
  23. if event is InputEventMouseButton:
  24. if event.button_index == MOUSE_BUTTON_WHEEL_UP:
  25. zoom -= ZOOM_SPEED
  26. if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
  27. zoom += ZOOM_SPEED
  28. zoom = clamp(zoom, 1.5, 4)
  29. if event is InputEventMouseMotion and event.button_mask & MAIN_BUTTONS:
  30. # Compensate motion speed to be resolution-independent (based on the window height).
  31. var relative_motion = event.relative * DisplayServer.window_get_size().y / base_height
  32. rot_y -= relative_motion.x * ROT_SPEED
  33. rot_x -= relative_motion.y * ROT_SPEED
  34. rot_x = clamp(rot_x, deg_to_rad(-90), 0)
  35. camera_holder.transform.basis = Basis.from_euler(Vector3(0, rot_y, 0))
  36. rotation_x.transform.basis = Basis.from_euler(Vector3(rot_x, 0, 0))
  37. func _process(delta):
  38. var current_tester = testers.get_child(tester_index)
  39. # This code assumes CameraHolder's X and Y coordinates are already correct.
  40. var current_position = camera_holder.global_transform.origin.z
  41. var target_position = current_tester.global_transform.origin.z
  42. camera_holder.global_transform.origin.z = lerpf(current_position, target_position, 3 * delta)
  43. camera.position.z = lerpf(camera.position.z, zoom, 10 * delta)
  44. func _on_previous_pressed():
  45. tester_index = max(0, tester_index - 1)
  46. update_gui()
  47. func _on_next_pressed():
  48. tester_index = min(tester_index + 1, testers.get_child_count() - 1)
  49. update_gui()
  50. func update_gui():
  51. $TestName.text = str(testers.get_child(tester_index).name).capitalize()
  52. $Previous.disabled = tester_index == 0
  53. $Next.disabled = tester_index == testers.get_child_count() - 1