Esempio n. 1
0
 public void OnMouseOver()
 {
     deltaAxisX               = deltaAxisY = 0;
     dragging                 = true;
     draggingHandle           = this;
     mouseLastPos             = Input.mousePosition;
     axisXProjectedToScreen   = Camera.main.worldToCameraMatrix.MultiplyVector(transform.right);
     axisYProjectedToScreen   = Camera.main.worldToCameraMatrix.MultiplyVector(transform.up);
     axisXProjectedToScreen.z = 0;
     axisYProjectedToScreen.z = 0;
     axisXProjectedToScreen.Scale(Vector3.one / Mathf.Max(0.01f, axisXProjectedToScreen.magnitude));
     axisYProjectedToScreen.Scale(Vector3.one / Mathf.Max(0.01f, axisYProjectedToScreen.magnitude));
     cameraDistanceGain = MathD.Clamp(Mathf.Abs(Camera.main.worldToCameraMatrix.MultiplyPoint3x4(transform.position).z) * .02f, 0.0125f, 2f);
     axisLockState      = 0;
 }
Esempio n. 2
0
        public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime)
        {
            smoothTime = MathD.Max(0.0001d, smoothTime);
            double num1 = 2d / smoothTime;
            double num2 = num1 * deltaTime;
            double num3 = (1.0d / (1.0d + num2 + 0.479999989271164d * num2 * num2 + 0.234999999403954d * num2 * num2 * num2));
            double num4 = current - target;
            double num5 = target;
            double max  = maxSpeed * smoothTime;
            double num6 = MathD.Clamp(num4, -max, max);

            target = current - num6;
            double num7 = (currentVelocity + num1 * num6) * deltaTime;

            currentVelocity = (currentVelocity - num1 * num7) * num3;
            double num8 = target + (num6 + num7) * num3;

            if (num5 - current > 0.0 == num8 > num5)
            {
                num8            = num5;
                currentVelocity = (num8 - num5) / deltaTime;
            }
            return(num8);
        }
Esempio n. 3
0
        void Update()
        {
            if (!HighLogic.LoadedSceneIsEditor)
            {
                return;
            }

            // A temporary fix for a weird bug, can't figure out where the layer is changed in some situations
            if (gameObject.layer == 0)
            {
                gameObject.layer = 2;
            }

            if (!dragging)
            {
                return;
            }
            if (Input.GetMouseButtonUp(0))
            {
                LineDrawer.Instance.enabled = dragging = false;
                axisX = axisY = 0;
                return;
            }

            bool x = Input.GetKeyDown(KeyCode.X);
            bool y = Input.GetKeyDown(KeyCode.Y);

            if (x || y)
            {
                switch (axisLockState)
                {
                case 0:
                    if (x)
                    {
                        axisLockState = 1;
                    }
                    else if (y)
                    {
                        axisLockState = 2;
                    }
                    break;

                case 1:
                    if (x)
                    {
                        axisLockState = 0;
                    }
                    else if (y)
                    {
                        axisLockState = 2;
                    }
                    break;

                case 2:
                    if (x)
                    {
                        axisLockState = 1;
                    }
                    else if (y)
                    {
                        axisLockState = 0;
                    }
                    break;

                default:
                    break;
                }
            }

            var offset = Input.mousePosition - mouseLastPos;

            var yProjected = (offset.y - axisXProjectedToScreen.y * offset.x / axisXProjectedToScreen.x) / ((axisYProjectedToScreen.y - axisXProjectedToScreen.y * axisYProjectedToScreen.x / axisXProjectedToScreen.x));
            var xProjected = (offset.x - yProjected * axisYProjectedToScreen.x) / axisXProjectedToScreen.x;

            var speedMult = MathD.Clamp(offset.sqrMagnitude * .025f, 0.4f, 1.25f);

            axisX       = Gain * speedMult * cameraDistanceGain * xProjected;
            axisY       = Gain * speedMult * cameraDistanceGain * yProjected;
            deltaAxisX += axisX;
            deltaAxisY += axisY;

            if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand))
            {
                if (axisLockState == 0)
                {
                    axisLockState = Math.Abs(deltaAxisY) < Math.Abs(deltaAxisX) ? 2 : 1;
                }
                else if (axisLockState == 1 && Math.Abs(deltaAxisX) > .5f)
                {
                    axisLockState = Math.Abs(deltaAxisY) < Math.Abs(deltaAxisX) * .75f ? 2 : 1;
                }
                else if (axisLockState == 2 && Math.Abs(deltaAxisY) > .5f)
                {
                    axisLockState = Math.Abs(deltaAxisY) * .75f < Math.Abs(deltaAxisX) ? 2 : 1;
                }
            }
            else if (Input.GetKeyUp(KeyCode.LeftControl) || Input.GetKeyUp(KeyCode.LeftCommand))
            {
                axisLockState = 0;
            }

            mouseLastPos = Input.mousePosition;
        }