Exemple #1
0
        /// <summary>
        /// End all movement.
        /// </summary>
        /// <param name="mousePosition">The current mose position.</param>
        public void StopMoveInput(Vector2 mousePosition)
        {
            if (activeHandler == this)
            {
                activeHandler = null;
            }

            MoveActive             = false;
            m_CurrentMousePosition = mousePosition;
            m_MouseDelta           = Vector2.zero;
            m_LastMousePosition    = m_CurrentMousePosition;
            m_CurrentInputVector   = Vector3.zero;
            m_ShiftSpeed           = false;
            m_HadMouseButtonEvent  = false;
            m_MouseButtonOneDown   = false;
        }
Exemple #2
0
        /// <summary>
        /// Handle the fps movement input in the given rect.
        /// </summary>
        /// <param name="rect">The rect the the input handling is masked to.</param>
        /// <param name="currentEvent">Current unity event to process.</param>
        /// <param name="type">The event type, cached from the beginning of OnGUI</param>
        /// <param name="eventDelta">Mouse delta from current event, cached at the beginning of OnGUI</param>
        public void HandleGUIInput(Rect rect, Event currentEvent, EventType type, Vector2 eventDelta = default)
        {
            if (Application.isPlaying)
            {
                return;
            }

            m_Dragging    = type == EventType.MouseDrag;
            activeHandler = this;

            var inRect = rect.Contains(Event.current.mousePosition);

            if (currentEvent.button == 1)
            {
                switch (type)
                {
                // Prevents view from getting locking in slow spin without input after last mouse move
                case EventType.MouseUp:
                    StopMoveInput(currentEvent.mousePosition);
                    return;

                case EventType.Layout:
                    // handle case EventType.MouseDown when not inRect since down is getting consumed before handler
                    if (m_NeedsUpdateMouse && !m_HadMouseButtonEvent && !inRect)
                    {
                        m_HadMouseButtonEvent = true;
                        m_NeedsUpdateMouse    = false;
                        StopMoveInput(currentEvent.mousePosition);
                        return;
                    }

                    // handle case EventType.MouseDown when inRect since down is getting consumed before handler
                    if (m_NeedsUpdateMouse && !m_HadMouseButtonEvent)
                    {
                        m_HadMouseButtonEvent  = true;
                        m_NeedsUpdateMouse     = false;
                        m_CurrentMousePosition = currentEvent.mousePosition;
                        m_LastMousePosition    = m_CurrentMousePosition;
                        m_MouseDelta           = Vector2.zero;
                        MoveActive             = true;
                    }
                    else if (m_NeedsUpdateMouse && MoveActive)
                    {
                        m_CurrentMousePosition = currentEvent.mousePosition;
                        m_MouseDelta           = eventDelta;
                        m_LastMousePosition    = m_CurrentMousePosition;
                        m_NeedsUpdateMouse     = false;
                        MoveActive             = true;
                    }

                    break;

                case EventType.Repaint:
#if UNITY_EDITOR_OSX // MouseDrag event feels sluggish on Win
                case EventType.MouseDrag:
#endif
                    m_NeedsUpdateMouse   = true;
                    m_MouseButtonOneDown = true;
                    break;
                }
            }
            else if (currentEvent.isMouse && currentEvent.button != 1)
            {
                StopMoveInput(currentEvent.mousePosition);
                return;
            }

            // handles the case for mouse button one being held but mouse is still
            else if (m_NeedsUpdateMouse && MoveActive && m_MouseButtonOneDown)
            {
                m_CurrentMousePosition = currentEvent.mousePosition;
                m_MouseDelta           = eventDelta;
                m_LastMousePosition    = m_CurrentMousePosition;
                m_NeedsUpdateMouse     = false;
                MoveActive             = true;
            }

            if (MoveActive || inRect)
            {
                m_ShiftSpeed = currentEvent.shift;

                if (currentEvent.type == EventType.KeyDown)
                {
                    var eatInput = false;
                    switch (currentEvent.keyCode)
                    {
                    case KeyCode.W:
                        m_CurrentInputVector.z = m_CurrentInputVector.z < 0f ? 0f : 1f;
                        eatInput = true;
                        break;

                    case KeyCode.S:
                        m_CurrentInputVector.z = m_CurrentInputVector.z > 0f ? 0f : -1f;
                        eatInput = true;
                        break;

                    case KeyCode.A:
                        m_CurrentInputVector.x = m_CurrentInputVector.x > 0f ? 0f : -1f;
                        eatInput = true;
                        break;

                    case KeyCode.D:
                        m_CurrentInputVector.x = m_CurrentInputVector.x < 0f ? 0f : 1f;
                        eatInput = true;
                        break;

                    case KeyCode.E:
                        m_CurrentInputVector.y = m_CurrentInputVector.y < 0f ? 0f : 1f;
                        eatInput = true;
                        break;

                    case KeyCode.Q:
                        m_CurrentInputVector.y = m_CurrentInputVector.y > 0f ? 0f : -1f;
                        eatInput = true;
                        break;
                    }

                    if (eatInput)
                    {
                        currentEvent.Use();
                    }
                }

                if (currentEvent.type == EventType.KeyUp)
                {
                    switch (currentEvent.keyCode)
                    {
                    case KeyCode.W:
                        m_CurrentInputVector.z = m_CurrentInputVector.z > 0f ? 0f : m_CurrentInputVector.z;
                        break;

                    case KeyCode.S:
                        m_CurrentInputVector.z = m_CurrentInputVector.z < 0f ? 0f : m_CurrentInputVector.z;
                        break;

                    case KeyCode.A:
                        m_CurrentInputVector.x = m_CurrentInputVector.x < 0f ? 0f : m_CurrentInputVector.x;
                        break;

                    case KeyCode.D:
                        m_CurrentInputVector.x = m_CurrentInputVector.x > 0f ? 0f : m_CurrentInputVector.x;
                        break;

                    case KeyCode.E:
                        m_CurrentInputVector.y = m_CurrentInputVector.y > 0f ? 0f : m_CurrentInputVector.y;
                        break;

                    case KeyCode.Q:
                        m_CurrentInputVector.y = m_CurrentInputVector.y < 0f ? 0f : m_CurrentInputVector.y;
                        break;
                    }
                }
            }
            else
            {
                m_CurrentInputVector = Vector3.zero;
            }
        }