XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using XFEEDToolBox.Core.Models;

namespace XFEEDToolBox.Core.Galaxy;

/// <summary>Processed original graph values, before the camera's own gains.</summary>
public readonly record struct GalaxyCameraInputValues(double Pitch,double Yaw,double Right,double Forward,double Crane,double Dolly);

/// <summary>
/// Original galaxymapcontrols graph and 3B507D0 input consumer. The caller owns
/// the input-event float and physical mouse units; this class does not equate
/// an original raw unit with a WinUI DIP or impose a frame cadence.
/// </summary>
public static class GalaxyOriginalCameraInput
{
    public const double AngularGain=(double)1.3f;
    public const double ZoomExponentGain=(double)3.6f;
    public const double AxisTranslationScale=(double).7f;
    public const double MouseAxisScale=(double).002f;
    public const double WheelScale=(double).0005f;
    public const double TranslationScale=(double).1f*8;

    public static GalaxyCameraInputValues Keyboard(double pitchUp,double yawRight,double right,double forward,double crane,double dollyOut,double eventFloat)
    {
        Validate(pitchUp,yawRight,right,forward,crane,dollyOut,eventFloat);
        if(eventFloat<0)throw new ArgumentOutOfRangeException(nameof(eventFloat));
        // The pitch TwoInputAxis passes through an explicit InvertValue node.
        // Independent axes are not normalized as a single movement vector.
        return new(-pitchUp*eventFloat,yawRight*eventFloat,right*AxisTranslationScale*eventFloat,
            forward*AxisTranslationScale*eventFloat,crane*AxisTranslationScale*eventFloat,dollyOut*eventFloat);
    }

    public static GalaxyCameraInputValues Mouse(GalaxyMouseButtons buttons,double rawX,double rawY,double rawWheel=0,bool reverseCrane=false)
    {
        Validate(rawX,rawY,rawWheel);
        var x=rawX*MouseAxisScale;var y=rawY*MouseAxisScale;
        var value=GalaxyMouseControls.Gesture(buttons) switch
        {
            GalaxyMouseGesture.Orbit=>new GalaxyCameraInputValues(y,x,0,0,0,0),
            GalaxyMouseGesture.Crane=>new(0,x,0,0,y*(reverseCrane?-1:1),0),
            GalaxyMouseGesture.Translate=>new(0,0,-x,y,0,0),
            _=>default
        };
        return value with{Dolly=-rawWheel*WheelScale};
    }

    /// <summary>Original ScaleValue/CopyValue arbitration, retaining the first on a tie.</summary>
    public static GalaxyCameraInputValues Combine(GalaxyCameraInputValues first,GalaxyCameraInputValues next)
    {
        Validate(first.Pitch,first.Yaw,first.Right,first.Forward,first.Crane,first.Dolly,
            next.Pitch,next.Yaw,next.Right,next.Forward,next.Crane,next.Dolly);
        static double Max(double a,double b)=>Math.Abs(b)>Math.Abs(a)?b:a;
        return new(Max(first.Pitch,next.Pitch),Max(first.Yaw,next.Yaw),Max(first.Right,next.Right),
            Max(first.Forward,next.Forward),Max(first.Crane,next.Crane),Max(first.Dolly,next.Dolly));
    }

    public static double TranslationSpeed(double actualDistance)
    {
        Validate(actualDistance);
        if(actualDistance<=0)throw new ArgumentOutOfRangeException(nameof(actualDistance));
        return (actualDistance+1)*TranslationScale*AxisTranslationScale;
    }

    public static GalaxyCameraPose Apply(GalaxyCameraPose actual,GalaxyCameraPose target,GalaxyCameraInputValues input,bool cameraCraning=false)
    {
        Validate(actual.Yaw,actual.Distance,target.Yaw,target.Pitch,target.Distance,
            input.Pitch,input.Yaw,input.Right,input.Forward,input.Crane,input.Dolly);
        if(actual.Focus is not{IsValid:true}||target.Focus is not{IsValid:true}||actual.Distance<=0||target.Distance<=0)
            throw new ArgumentException("星图相机输入姿态无效。");
        var movement=(actual.Distance+1)*TranslationScale;
        var sin=Math.Sin(actual.Yaw);var cos=Math.Cos(actual.Yaw);
        var result=target with
        {
            Focus=new(target.Focus.X+(cos*input.Right+sin*input.Forward)*movement,
                target.Focus.Y+input.Crane*movement,
                target.Focus.Z+(-sin*input.Right+cos*input.Forward)*movement),
            Pitch=target.Pitch+input.Pitch*AngularGain,
            Yaw=target.Yaw+input.Yaw*AngularGain,
            Distance=target.Distance*Math.Pow(2,input.Dolly*ZoomExponentGain)
        };
        return GalaxyOriginalCameraControls.PrepareTarget(result,cameraCraning,input.Crane);
    }

    /// <summary>
    /// Home's proved target rule (3B55710/3A632C0). This does not implement the
    /// original cinematic transition or identify the caller's centre source.
    /// </summary>
    public static GalaxyCameraPose Home(GalaxyCameraPose target,Coordinates navigationCentre)
    {
        if(navigationCentre is not{IsValid:true}||!double.IsFinite(target.Distance)||target.Distance<=0)
            throw new ArgumentException("星图导航中心无效。");
        return target with{Focus=new(navigationCentre.X,Math.Floor(navigationCentre.Y),navigationCentre.Z),
            Distance=Math.Min(target.Distance,GalaxyOriginalCameraControls.DefaultDistance)};
    }

    private static void Validate(params double[] values)
    {
        if(values.Any(value=>!double.IsFinite(value)))throw new ArgumentException("星图相机输入无效。");
    }
}