Ejemplo n.º 1
0
        private void OnUpdate()
        {
            bool isOrtographic = camera.ProjectionType == ProjectionType.Orthographic;

            if (inputEnabled)
            {
                bool goingForward = VirtualInput.IsButtonHeld(moveForwardBtn);
                bool goingBack    = VirtualInput.IsButtonHeld(moveBackwardBtn);
                bool goingLeft    = VirtualInput.IsButtonHeld(moveLeftBtn);
                bool goingRight   = VirtualInput.IsButtonHeld(moveRightBtn);
                bool goingUp      = VirtualInput.IsButtonHeld(moveUpBtn);
                bool goingDown    = VirtualInput.IsButtonHeld(moveDownBtn);
                bool fastMove     = VirtualInput.IsButtonHeld(fastMoveBtn);
                bool camActive    = VirtualInput.IsButtonHeld(activeBtn);
                bool isPanning    = VirtualInput.IsButtonHeld(panBtn);

                bool hideCursor = camActive || isPanning;
                if (hideCursor != lastButtonState)
                {
                    if (hideCursor)
                    {
                        Cursor.Hide();

                        Rect2I clipRect;
                        clipRect.x      = Input.PointerPosition.x - 2;
                        clipRect.y      = Input.PointerPosition.y - 2;
                        clipRect.width  = 4;
                        clipRect.height = 4;

                        Cursor.ClipToRect(clipRect);
                    }
                    else
                    {
                        Cursor.Show();
                        Cursor.ClipDisable();
                    }

                    lastButtonState = hideCursor;
                }

                float frameDelta = Time.FrameDelta;
                if (camActive)
                {
                    float horzValue = VirtualInput.GetAxisValue(horizontalAxis);
                    float vertValue = VirtualInput.GetAxisValue(verticalAxis);

                    float rotationAmount = RotationalSpeed * EditorSettings.MouseSensitivity * frameDelta;

                    yaw   += new Degree(horzValue * rotationAmount);
                    pitch += new Degree(vertValue * rotationAmount);

                    yaw   = MathEx.WrapAngle(yaw);
                    pitch = MathEx.WrapAngle(pitch);

                    Quaternion yRot = Quaternion.FromAxisAngle(Vector3.YAxis, yaw);
                    Quaternion xRot = Quaternion.FromAxisAngle(Vector3.XAxis, pitch);

                    Quaternion camRot = yRot * xRot;
                    camRot.Normalize();

                    SceneObject.Rotation = camRot;

                    // Handle movement using movement keys
                    Vector3 direction = Vector3.Zero;

                    if (goingForward)
                    {
                        direction += SceneObject.Forward;
                    }
                    if (goingBack)
                    {
                        direction -= SceneObject.Forward;
                    }
                    if (goingRight)
                    {
                        direction += SceneObject.Right;
                    }
                    if (goingLeft)
                    {
                        direction -= SceneObject.Right;
                    }
                    if (goingUp)
                    {
                        direction += SceneObject.Up;
                    }
                    if (goingDown)
                    {
                        direction -= SceneObject.Up;
                    }

                    if (direction.SqrdLength != 0)
                    {
                        direction.Normalize();

                        float multiplier = 1.0f;
                        if (fastMove)
                        {
                            multiplier = FastModeMultiplier;
                        }

                        currentSpeed  = MathEx.Clamp(currentSpeed + Acceleration * frameDelta, StartSpeed, TopSpeed);
                        currentSpeed *= multiplier;
                    }
                    else
                    {
                        currentSpeed = 0.0f;
                    }

                    const float tooSmall = 0.0001f;
                    if (currentSpeed > tooSmall)
                    {
                        Vector3 velocity = direction * currentSpeed;
                        SceneObject.Move(velocity * frameDelta);
                    }
                }

                // Pan
                if (isPanning)
                {
                    float horzValue = VirtualInput.GetAxisValue(horizontalAxis);
                    float vertValue = VirtualInput.GetAxisValue(verticalAxis);

                    Vector3 direction = new Vector3(horzValue, -vertValue, 0.0f);
                    direction = camera.SceneObject.Rotation.Rotate(direction);

                    SceneObject.Move(direction * PanSpeed * frameDelta);
                }
            }
            else
            {
                Cursor.Show();
                Cursor.ClipDisable();
            }

            SceneWindow sceneWindow = EditorWindow.GetWindow <SceneWindow>();

            if (sceneWindow.Active)
            {
                Rect2I bounds = sceneWindow.Bounds;

                // Move using scroll wheel
                if (bounds.Contains(Input.PointerPosition))
                {
                    float scrollAmount = VirtualInput.GetAxisValue(scrollAxis);
                    if (!isOrtographic)
                    {
                        SceneObject.Move(SceneObject.Forward * scrollAmount * ScrollSpeed);
                    }
                    else
                    {
                        float orthoHeight = MathEx.Max(1.0f, camera.OrthoHeight - scrollAmount);
                        camera.OrthoHeight = orthoHeight;
                    }
                }
            }

            UpdateAnim();
        }