rj-action-library/Runtime/VirtualCameras/MouseEditorCamera/MouseEditorCamera.cs

147 lines
3.0 KiB
C#
Raw Normal View History

2024-05-19 15:59:41 +00:00
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System;
using Godot;
namespace Rokojori
{
[Tool]
[GlobalClass]
public partial class MouseEditorCamera:VirtualCamera3D
{
[Export]
public Vector3 target;
2024-07-25 05:40:31 +00:00
2024-05-19 15:59:41 +00:00
[Export]
public float yaw = 0;
[Export]
public float pitch = 0;
[Export]
public float distance = 10;
2025-03-13 16:19:28 +00:00
float _smoothDistance = 10;
public float smoothDistance => _smoothDistance;
[Export]
public float minPitch = -89;
[Export]
public float maxPitch = 89;
[Export]
public float minDistance = 0.001f;
[Export]
public float maxDistance = 200f;
2025-04-25 11:47:31 +00:00
[Export( PropertyHint.Range, "0,600")]
2025-04-25 12:02:53 +00:00
public Smoothing zoomSmoothing;
2025-04-25 11:47:31 +00:00
2025-03-13 16:19:28 +00:00
public Vector3 moveDirection = Vector3.Zero;
[Export]
public MouseEditorCameraInputSettings keyboardMouseInputs;
[Export]
public MouseEditorCameraInputSettings controllerInputs;
float _cachedDistance = -1;
public void CacheDistance()
{
if ( _cachedDistance < 0 )
{
return;
}
_cachedDistance = distance;
GlobalPosition = target;
distance = minDistance;
this.LogInfo( "Cached Distance", _cachedDistance );
}
public void RestoreDistance()
{
if ( _cachedDistance < 0 )
{
return;
}
this.LogInfo( "Restored Distance", _cachedDistance );
target = GlobalPosition + GlobalBasis.Z * _cachedDistance;
distance = _cachedDistance;
_cachedDistance = -1;
}
2024-05-19 15:59:41 +00:00
2025-03-13 16:19:28 +00:00
[Export]
public float running = 0;
2024-05-19 15:59:41 +00:00
public override void _Process( double delta )
{
2025-03-13 16:19:28 +00:00
keyboardMouseInputs?.Orbit( this );
controllerInputs?.Orbit( this );
keyboardMouseInputs?.Pan( this );
controllerInputs?.Pan( this );
keyboardMouseInputs?.Zoom( this );
controllerInputs?.Zoom( this );
moveDirection = Vector3.Zero;
keyboardMouseInputs?.Move( this );
controllerInputs?.Move( this );
2024-05-19 15:59:41 +00:00
Apply( (float) delta );
if ( ! hasMotionDelta )
{
2025-03-13 16:19:28 +00:00
_motionDelta.X = 0;
_motionDelta.Y = 0;
2024-05-19 15:59:41 +00:00
}
hasMotionDelta = false;
}
bool hasMotionDelta = false;
2025-03-13 16:19:28 +00:00
Vector2 _motionDelta = Vector2.Zero;
public Vector2 motionDelta => _motionDelta;
2024-05-19 15:59:41 +00:00
public override void _Input( InputEvent inputEvent )
{
2024-12-01 17:07:41 +00:00
var mouseMotionEvent = inputEvent as InputEventMouseMotion;
if ( mouseMotionEvent == null )
2024-05-19 15:59:41 +00:00
{
2024-12-01 17:07:41 +00:00
return;
2024-05-19 15:59:41 +00:00
}
2024-12-01 17:07:41 +00:00
2025-03-13 16:19:28 +00:00
_motionDelta = mouseMotionEvent.ScreenRelative;
2024-12-01 17:07:41 +00:00
hasMotionDelta = true;
2024-05-19 15:59:41 +00:00
}
2025-03-13 16:19:28 +00:00
void Apply( float delta )
{
2025-04-25 11:47:31 +00:00
_smoothDistance = Smoothing.Apply( zoomSmoothing, distance, delta );
2025-03-13 16:19:28 +00:00
GlobalRotation = new Vector3( Mathf.DegToRad( pitch ), Mathf.DegToRad( yaw ), 0 );
var forward = Math3D.GetGlobalForward( this ) * _smoothDistance;
target -= moveDirection * delta;
GlobalPosition = target + forward;
}
2024-05-19 15:59:41 +00:00
}
}