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;
|
|
|
|
float smoothDistance = 10;
|
|
|
|
|
2025-01-03 12:09:23 +00:00
|
|
|
[ExportGroup("Orbit")]
|
2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
[Export]
|
|
|
|
public float yawSpeed = 1;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public float pitchSpeed = 1;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public float minPitch = -89;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public float maxPitch = 89;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public RJSensor orbitButton;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public RJSensor[] orbitModifierButtons;
|
|
|
|
|
2025-01-03 12:09:23 +00:00
|
|
|
[ExportGroup("Pan")]
|
2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
[Export]
|
2025-01-03 12:09:23 +00:00
|
|
|
public float panSpeedX = 0.01f;
|
2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
[Export]
|
2025-01-03 12:09:23 +00:00
|
|
|
public float panSpeedY = 0.01f;
|
2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
[Export]
|
|
|
|
public RJSensor panButton;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public RJSensor[] panModifierButtons;
|
|
|
|
|
2025-01-03 12:09:23 +00:00
|
|
|
[ExportGroup("Zoom")]
|
2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
[Export]
|
|
|
|
public float zoomStepInPercentage = 10;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public float minDistance = 0.001f;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public float maxDistance = 200f;
|
|
|
|
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public RJSensor zoomInButton;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public RJSensor[] zoomInModifierButtons;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public RJSensor zoomOutButton;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public RJSensor[] zoomOutModifierButtons;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public float zoomSmoothingCoefficient = 0.1f;
|
|
|
|
Smoother smoother = new Smoother();
|
2025-01-03 12:09:23 +00:00
|
|
|
|
|
|
|
[ExportGroup("Move")]
|
|
|
|
[Export]
|
|
|
|
public RJSensor forwardButton;
|
|
|
|
[Export]
|
|
|
|
public RJSensor backwardsButton;
|
|
|
|
[Export]
|
|
|
|
public RJSensor leftButton;
|
|
|
|
[Export]
|
|
|
|
public RJSensor rightButton;
|
|
|
|
[Export]
|
|
|
|
public RJSensor upButton;
|
|
|
|
[Export]
|
|
|
|
public RJSensor downButton;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public float moveSpeed = 1;
|
|
|
|
|
2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
public override void _Process( double delta )
|
|
|
|
{
|
2024-12-01 17:07:41 +00:00
|
|
|
|
2024-05-19 15:59:41 +00:00
|
|
|
Orbit();
|
|
|
|
Pan();
|
|
|
|
Zoom();
|
2025-01-03 12:09:23 +00:00
|
|
|
Move();
|
2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
Apply( (float) delta );
|
|
|
|
|
|
|
|
if ( ! hasMotionDelta )
|
|
|
|
{
|
|
|
|
motionDelta.X = 0;
|
|
|
|
motionDelta.Y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasMotionDelta = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasMotionDelta = false;
|
|
|
|
Vector2 motionDelta = Vector2.Zero;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
motionDelta = mouseMotionEvent.ScreenRelative;
|
|
|
|
hasMotionDelta = true;
|
2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Orbit()
|
|
|
|
{
|
2024-12-01 17:07:41 +00:00
|
|
|
if ( ! Sensors.IsActive( orbitButton ) )
|
2024-05-19 15:59:41 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
yaw += motionDelta.X * yawSpeed;
|
|
|
|
pitch += motionDelta.Y * pitchSpeed;
|
|
|
|
|
|
|
|
pitch = Mathf.Clamp( pitch, minPitch, maxPitch );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pan()
|
|
|
|
{
|
2024-12-01 17:07:41 +00:00
|
|
|
if ( ! Sensors.IsActive( panButton ) )
|
2024-05-19 15:59:41 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var xAmount = motionDelta.X * smoothDistance * GlobalBasis.X * panSpeedX;
|
|
|
|
var yAmount = motionDelta.Y * smoothDistance * GlobalBasis.Y * panSpeedY;
|
|
|
|
|
|
|
|
target += xAmount + yAmount;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Zoom()
|
|
|
|
{
|
2024-12-01 17:07:41 +00:00
|
|
|
if ( Sensors.IsActive( zoomInButton ) )
|
2024-05-19 15:59:41 +00:00
|
|
|
{
|
|
|
|
distance *= Mathf.Pow( 1 + zoomStepInPercentage / 100f, 1 );
|
|
|
|
}
|
|
|
|
|
2024-12-01 17:07:41 +00:00
|
|
|
if ( Sensors.IsActive( zoomOutButton ) )
|
2024-05-19 15:59:41 +00:00
|
|
|
{
|
|
|
|
distance *= Mathf.Pow( 1 + zoomStepInPercentage / 100f, -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
distance = Mathf.Clamp( distance, minDistance, maxDistance );
|
|
|
|
}
|
|
|
|
|
2025-01-03 12:09:23 +00:00
|
|
|
Vector3 moveDirection = Vector3.Zero;
|
|
|
|
|
|
|
|
void Move()
|
|
|
|
{
|
|
|
|
moveDirection = Vector3.Zero;
|
|
|
|
|
|
|
|
if ( Sensors.IsActive( forwardButton ) || Sensors.IsActive( backwardsButton ) )
|
|
|
|
{
|
|
|
|
moveDirection = ( Sensors.GetValue( forwardButton ) - Sensors.GetValue( backwardsButton ) ) * this.GlobalForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Sensors.IsActive( rightButton ) || Sensors.IsActive( leftButton ) )
|
|
|
|
{
|
|
|
|
moveDirection = ( Sensors.GetValue( rightButton ) - Sensors.GetValue( leftButton ) ) * this.GlobalRight();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Sensors.IsActive( upButton ) || Sensors.IsActive( downButton ) )
|
|
|
|
{
|
|
|
|
moveDirection = ( Sensors.GetValue( upButton ) - Sensors.GetValue( downButton ) ) * this.GlobalUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
moveDirection = moveDirection.Normalized() * moveSpeed;
|
|
|
|
}
|
|
|
|
|
2024-05-19 15:59:41 +00:00
|
|
|
|
|
|
|
void Apply( float delta )
|
|
|
|
{
|
|
|
|
smoothDistance = smoother.SmoothWithCoefficient( smoothDistance, distance, zoomSmoothingCoefficient, delta );
|
|
|
|
GlobalRotation = new Vector3( Mathf.DegToRad( pitch ), Mathf.DegToRad( yaw ), 0 );
|
|
|
|
|
|
|
|
var forward = Math3D.GetGlobalForward( this ) * smoothDistance;
|
2025-01-03 12:09:23 +00:00
|
|
|
target -= moveDirection * delta;
|
2024-08-04 09:08:12 +00:00
|
|
|
GlobalPosition = target + forward;
|
2024-05-19 15:59:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|