Exemplo n.º 1
0
 public dfMouseEventArgs(dfControl Source, dfMouseButtons button, int clicks, UnityEngine.Ray ray, Vector2 location, float wheel) : base(Source)
 {
     this.Buttons    = button;
     this.Clicks     = clicks;
     this.Position   = location;
     this.WheelDelta = wheel;
     this.Ray        = ray;
 }
Exemplo n.º 2
0
 public dfMouseEventArgs(dfControl Source, dfMouseButtons button, int clicks, UnityEngine.Ray ray, Vector2 location, float wheel)
     : base(Source)
 {
     this.Buttons = button;
     this.Clicks = clicks;
     this.Position = location;
     this.WheelDelta = wheel;
     this.Ray = ray;
 }
Exemplo n.º 3
0
 private static void getMouseButtonInfo(IInputAdapter adapter, ref dfMouseButtons buttonsDown, ref dfMouseButtons buttonsReleased, ref dfMouseButtons buttonsPressed)
 {
     for (int i = 0; i < 3; i++)
     {
         if (adapter.GetMouseButton(i))
         {
             buttonsDown = (dfMouseButtons)((int)buttonsDown | 1 << (i & 31));
         }
         if (adapter.GetMouseButtonUp(i))
         {
             buttonsReleased = (dfMouseButtons)((int)buttonsReleased | 1 << (i & 31));
         }
         if (adapter.GetMouseButtonDown(i))
         {
             buttonsPressed = (dfMouseButtons)((int)buttonsPressed | 1 << (i & 31));
         }
     }
 }
        private static void getMouseButtonInfo( IInputAdapter adapter, ref dfMouseButtons buttonsDown, ref dfMouseButtons buttonsReleased, ref dfMouseButtons buttonsPressed )
        {
            for( int i = 0; i < 3; i++ )
            {

                if( adapter.GetMouseButton( i ) )
                {
                    buttonsDown |= (dfMouseButtons)( 1 << i );
                }
                if( adapter.GetMouseButtonUp( i ) )
                {
                    buttonsReleased |= (dfMouseButtons)( 1 << i );
                }
                if( adapter.GetMouseButtonDown( i ) )
                {
                    buttonsPressed |= (dfMouseButtons)( 1 << i );
                }

            }
        }
            public void ProcessInput( dfInputManager manager, IInputAdapter adapter, Ray ray, dfControl control, bool retainFocusSetting )
            {

            var position = adapter.GetMousePosition();

            buttonsDown = dfMouseButtons.None;
            buttonsReleased = dfMouseButtons.None;
            buttonsPressed = dfMouseButtons.None;

            getMouseButtonInfo( adapter, ref buttonsDown, ref buttonsReleased, ref buttonsPressed );

            float scroll = adapter.GetAxis( scrollAxisName );
            if( !Mathf.Approximately( scroll, 0f ) )
            {
                // By default the mouse wheel is reported in increments of 0.1f,
                // which is just a useless number for UI, but this can be changed
                // by the user in the Unity Input Manager. We'll assume that if the
                // number reported is less than 1 then it is probably safe to
                // assume that we can massage it for UI purposes.
                scroll = Mathf.Sign( scroll ) * Mathf.Max( 1, Mathf.Abs( scroll ) );
            }

            mouseMoveDelta = position - lastPosition;
            lastPosition = position;

            #region Drag and drop

            if( dragState == dfDragDropState.Dragging )
            {

                if( buttonsReleased == dfMouseButtons.None )
                {

                    // Do nothing if the drag operation is over the source control
                    // and no buttons have been released.
                    if( control == activeControl )
                        return;

                    if( control != lastDragControl )
                    {

                        if( lastDragControl != null )
                        {
                            var dragArgs = new dfDragEventArgs( lastDragControl, dragState, dragData, ray, position );
                            lastDragControl.OnDragLeave( dragArgs );
                        }

                        if( control != null )
                        {
                            var dragArgs = new dfDragEventArgs( control, dragState, dragData, ray, position );
                            control.OnDragEnter( dragArgs );
                        }

                        lastDragControl = control;

                        return;

                    }

                    if( control != null )
                    {

                        if( mouseMoveDelta.magnitude > 1.0f )
                        {
                            var dragArgs = new dfDragEventArgs( control, dragState, dragData, ray, position );
                            control.OnDragOver( dragArgs );
                        }

                    }

                    return;

                }

                if( control != null && control != activeControl )
                {

                    var dragArgs = new dfDragEventArgs( control, dfDragDropState.Dragging, dragData, ray, position );
                    control.OnDragDrop( dragArgs );

                    // If there was no event consumer, or if the event consumer did not
                    // change the state from Dragging (which is not a valid state for
                    // a drop event) then just cancel the operation
                    if( !dragArgs.Used || dragArgs.State == dfDragDropState.Dragging )
                        dragArgs.State = dfDragDropState.Cancelled;

                    dragArgs = new dfDragEventArgs( activeControl, dragArgs.State, dragArgs.Data, ray, position );
                    dragArgs.Target = control;
                    activeControl.OnDragEnd( dragArgs );

                }
                else
                {
                    var cancelState = ( control == null ) ? dfDragDropState.CancelledNoTarget : dfDragDropState.Cancelled;
                    var dragArgs = new dfDragEventArgs( activeControl, cancelState, dragData, ray, position );
                    activeControl.OnDragEnd( dragArgs );
                }

                dragState = dfDragDropState.None;
                lastDragControl = null;
                activeControl = null;
                lastClickTime = 0f;
                lastHoverTime = 0f;
                lastPosition = position;

                return;

            }

            #endregion

            #region Mouse button pressed

            if( buttonsPressed != dfMouseButtons.None )
            {

                lastHoverTime = Time.realtimeSinceStartup + manager.hoverStartDelay;

                if( activeControl != null )
                {
                    // If a control has capture, forward all events to it
                    if( activeControl.transform.IsChildOf( manager.transform ) )
                    {
                        activeControl.OnMouseDown( new dfMouseEventArgs( activeControl, buttonsPressed, 0, ray, position, scroll ) );
                    }
                }
                else if( control == null || control.transform.IsChildOf( manager.transform ) )
                {

                    setActive( manager, control, position, ray );
                    if( control != null )
                    {
                        dfGUIManager.SetFocus( control );
                        control.OnMouseDown( new dfMouseEventArgs( control, buttonsPressed, 0, ray, position, scroll ) );
                    }
                    else if( !retainFocusSetting )
                    {
                        var focusControl = dfGUIManager.ActiveControl;
                        if( focusControl != null && focusControl.transform.IsChildOf( manager.transform ) )
                        {
                            focusControl.Unfocus();
                        }
                    }

                }

                if( buttonsReleased == dfMouseButtons.None )
                    return;

            }

            #endregion

            #region Mouse button released

            if( buttonsReleased != dfMouseButtons.None )
            {

                lastHoverTime = Time.realtimeSinceStartup + manager.hoverStartDelay;

                // Mouse up without a control having capture is ignored
                if( activeControl == null )
                {
                    setActive( manager, control, position, ray );
                    return;
                }

                // If the mouse button is released over the same control it was pressed on,
                // the Click event gets generated (in addition to MouseUp)
                if( activeControl == control && buttonsDown == dfMouseButtons.None )
                {

                    var p2u = activeControl.PixelsToUnits();
                    var startPosition = activeControlPosition / p2u;
                    var currentPosition = activeControl.transform.position / p2u;

                    // Don't fire click events if the control has been moved since the mouse was down
                    if( Vector3.Distance( startPosition, currentPosition ) <= 1 )
                    {

                        if( Time.realtimeSinceStartup - lastClickTime < DOUBLECLICK_TIME )
                        {
                            lastClickTime = 0f;
                            activeControl.OnDoubleClick( new dfMouseEventArgs( activeControl, buttonsReleased, 1, ray, position, scroll ) );
                        }
                        else
                        {
                            lastClickTime = Time.realtimeSinceStartup;
                            activeControl.OnClick( new dfMouseEventArgs( activeControl, buttonsReleased, 1, ray, position, scroll ) );
                        }

                    }

                }

                // Let the last control know that the button was released whether it was
                // released over the control or not
                activeControl.OnMouseUp( new dfMouseEventArgs( activeControl, buttonsReleased, 0, ray, position, scroll ) );

                // If all buttons are up, then we need to reset the mouse state
                if( buttonsDown == dfMouseButtons.None && activeControl != control )
                {
                    setActive( manager, null, position, ray );
                }

                return;

            }

            #endregion

            #region Doesn't matter if buttons are down or not

            if( activeControl != null && activeControl == control )
            {

                if( mouseMoveDelta.magnitude == 0 && Time.realtimeSinceStartup - lastHoverTime > manager.hoverNotifactionFrequency )
                {
                    activeControl.OnMouseHover( new dfMouseEventArgs( activeControl, buttonsDown, 0, ray, position, scroll ) );
                    lastHoverTime = Time.realtimeSinceStartup;
                }

            }

            #endregion

            #region No buttons down

            if( buttonsDown == dfMouseButtons.None )
            {

                if( scroll != 0 && control != null )
                {
                    setActive( manager, control, position, ray );
                    control.OnMouseWheel( new dfMouseEventArgs( control, buttonsDown, 0, ray, position, scroll ) );
                    return;
                }

                setActive( manager, control, position, ray );

            }

            #endregion

            #region Some buttons down

            else if( buttonsDown != dfMouseButtons.None ) // Some buttons are down
            {

                if( activeControl != null )
                {

                    // Special case: Another control with a higher RenderOrder is now under the mouse.
                    // This can happen when a control moves, such as when you click on a slider and the
                    // thumb position is updated to be under the mouse (when it wasn't previously)
                    if( control != null && control.RenderOrder > activeControl.RenderOrder )
                    {
                        // TODO: What to do about this when a control has capture?
                    }

                    // If the mouse was moved notify the control, otherwise nothing to do
                    // NOTE: This is similar to "mouse capture" on Windows Forms
                    if( mouseMoveDelta.magnitude >= DRAG_START_DELTA )
                    {

                        if( ( buttonsDown & ( dfMouseButtons.Left | dfMouseButtons.Right ) ) != 0 && dragState != dfDragDropState.Denied )
                        {
                            var dragArgs = new dfDragEventArgs( activeControl ) { Position = position };
                            activeControl.OnDragStart( dragArgs );
                            if( dragArgs.State == dfDragDropState.Dragging )
                            {
                                dragState = dfDragDropState.Dragging;
                                dragData = dragArgs.Data;
                                return;
                            }
                            else
                            {
                                dragState = dfDragDropState.Denied;
                            }
                        }

                    }

                }

            }

            #endregion

            if( activeControl != null && mouseMoveDelta.magnitude >= 1 )
            {
                var moveArgs = new dfMouseEventArgs( activeControl, buttonsDown, 0, ray, position, scroll ) { MoveDelta = mouseMoveDelta };
                activeControl.OnMouseMove( moveArgs );
            }
        }
Exemplo n.º 6
0
 public static bool IsSet(this dfMouseButtons value, dfMouseButtons flag)
 {
     return(flag == (value & flag));
 }
Exemplo n.º 7
0
 public static bool IsSet( this dfMouseButtons value, dfMouseButtons flag )
 {
     return flag == ( value & flag );
 }
Exemplo n.º 8
0
        public void ProcessInput(IInputAdapter adapter, Ray ray, dfControl control, bool retainFocusSetting)
        {
            Vector2 mousePosition = adapter.GetMousePosition();

            this.buttonsDown     = dfMouseButtons.None;
            this.buttonsReleased = dfMouseButtons.None;
            this.buttonsPressed  = dfMouseButtons.None;
            dfInputManager.MouseInputManager.getMouseButtonInfo(adapter, ref this.buttonsDown, ref this.buttonsReleased, ref this.buttonsPressed);
            float axis = adapter.GetAxis("Mouse ScrollWheel");

            if (!Mathf.Approximately(axis, 0f))
            {
                axis = Mathf.Sign(axis) * Mathf.Max(1f, Mathf.Abs(axis));
            }
            this.mouseMoveDelta = mousePosition - this.lastPosition;
            this.lastPosition   = mousePosition;
            if (this.dragState == dfDragDropState.Dragging)
            {
                if (this.buttonsReleased == dfMouseButtons.None)
                {
                    if (control == this.activeControl)
                    {
                        return;
                    }
                    if (control == this.lastDragControl)
                    {
                        if (control != null && Vector2.Distance(mousePosition, this.lastPosition) > 1f)
                        {
                            dfDragEventArgs dfDragEventArg = new dfDragEventArgs(control, this.dragState, this.dragData, ray, mousePosition);
                            control.OnDragOver(dfDragEventArg);
                        }
                        return;
                    }
                    if (this.lastDragControl != null)
                    {
                        dfDragEventArgs dfDragEventArg1 = new dfDragEventArgs(this.lastDragControl, this.dragState, this.dragData, ray, mousePosition);
                        this.lastDragControl.OnDragLeave(dfDragEventArg1);
                    }
                    if (control != null)
                    {
                        dfDragEventArgs dfDragEventArg2 = new dfDragEventArgs(control, this.dragState, this.dragData, ray, mousePosition);
                        control.OnDragEnter(dfDragEventArg2);
                    }
                    this.lastDragControl = control;
                    return;
                }
                if (!(control != null) || !(control != this.activeControl))
                {
                    dfDragDropState _dfDragDropState = (control != null ? dfDragDropState.Cancelled : dfDragDropState.CancelledNoTarget);
                    dfDragEventArgs dfDragEventArg3  = new dfDragEventArgs(this.activeControl, _dfDragDropState, this.dragData, ray, mousePosition);
                    this.activeControl.OnDragEnd(dfDragEventArg3);
                }
                else
                {
                    dfDragEventArgs dfDragEventArg4 = new dfDragEventArgs(control, dfDragDropState.Dragging, this.dragData, ray, mousePosition);
                    control.OnDragDrop(dfDragEventArg4);
                    if (!dfDragEventArg4.Used || dfDragEventArg4.State == dfDragDropState.Dragging)
                    {
                        dfDragEventArg4.State = dfDragDropState.Cancelled;
                    }
                    dfDragEventArg4 = new dfDragEventArgs(this.activeControl, dfDragEventArg4.State, dfDragEventArg4.Data, ray, mousePosition)
                    {
                        Target = control
                    };
                    this.activeControl.OnDragEnd(dfDragEventArg4);
                }
                this.dragState       = dfDragDropState.None;
                this.lastDragControl = null;
                this.activeControl   = null;
                this.lastClickTime   = 0f;
                this.lastHoverTime   = 0f;
                this.lastPosition    = mousePosition;
                return;
            }
            if (this.buttonsReleased != dfMouseButtons.None)
            {
                this.lastHoverTime = Time.realtimeSinceStartup + 0.25f;
                if (this.activeControl == null)
                {
                    this.setActive(control, mousePosition, ray);
                    return;
                }
                if (this.activeControl == control && this.buttonsDown == dfMouseButtons.None)
                {
                    if (Time.realtimeSinceStartup - this.lastClickTime >= 0.25f)
                    {
                        this.lastClickTime = Time.realtimeSinceStartup;
                        this.activeControl.OnClick(new dfMouseEventArgs(this.activeControl, this.buttonsReleased, 1, ray, mousePosition, axis));
                    }
                    else
                    {
                        this.lastClickTime = 0f;
                        this.activeControl.OnDoubleClick(new dfMouseEventArgs(this.activeControl, this.buttonsReleased, 1, ray, mousePosition, axis));
                    }
                }
                this.activeControl.OnMouseUp(new dfMouseEventArgs(this.activeControl, this.buttonsReleased, 0, ray, mousePosition, axis));
                if (this.buttonsDown == dfMouseButtons.None && this.activeControl != control)
                {
                    this.setActive(null, mousePosition, ray);
                }
                return;
            }
            if (this.buttonsPressed != dfMouseButtons.None)
            {
                this.lastHoverTime = Time.realtimeSinceStartup + 0.25f;
                if (this.activeControl == null)
                {
                    this.setActive(control, mousePosition, ray);
                    if (control != null)
                    {
                        control.OnMouseDown(new dfMouseEventArgs(control, this.buttonsPressed, 0, ray, mousePosition, axis));
                    }
                    else if (!retainFocusSetting)
                    {
                        dfControl activeControl = dfGUIManager.ActiveControl;
                        if (activeControl != null)
                        {
                            activeControl.Unfocus();
                        }
                    }
                }
                else
                {
                    this.activeControl.OnMouseDown(new dfMouseEventArgs(this.activeControl, this.buttonsPressed, 0, ray, mousePosition, axis));
                }
                return;
            }
            if (this.activeControl != null && this.activeControl == control && this.mouseMoveDelta.magnitude == 0f && Time.realtimeSinceStartup - this.lastHoverTime > 0.1f)
            {
                this.activeControl.OnMouseHover(new dfMouseEventArgs(this.activeControl, this.buttonsDown, 0, ray, mousePosition, axis));
                this.lastHoverTime = Time.realtimeSinceStartup;
            }
            if (this.buttonsDown == dfMouseButtons.None)
            {
                if (axis != 0f && control != null)
                {
                    this.setActive(control, mousePosition, ray);
                    control.OnMouseWheel(new dfMouseEventArgs(control, this.buttonsDown, 0, ray, mousePosition, axis));
                    return;
                }
                this.setActive(control, mousePosition, ray);
            }
            else if (this.activeControl != null)
            {
                if (control != null)
                {
                    control.RenderOrder <= this.activeControl.RenderOrder;
                }
                if (this.mouseMoveDelta.magnitude >= 2f && (this.buttonsDown & (dfMouseButtons.Left | dfMouseButtons.Right)) != dfMouseButtons.None && this.dragState != dfDragDropState.Denied)
                {
                    dfDragEventArgs dfDragEventArg5 = new dfDragEventArgs(this.activeControl)
                    {
                        Position = mousePosition
                    };
                    this.activeControl.OnDragStart(dfDragEventArg5);
                    if (dfDragEventArg5.State == dfDragDropState.Dragging)
                    {
                        this.dragState = dfDragDropState.Dragging;
                        this.dragData  = dfDragEventArg5.Data;
                        return;
                    }
                    this.dragState = dfDragDropState.Denied;
                }
            }
            if (this.activeControl != null && this.mouseMoveDelta.magnitude >= 1f)
            {
                dfMouseEventArgs dfMouseEventArg = new dfMouseEventArgs(this.activeControl, this.buttonsDown, 0, ray, mousePosition, axis)
                {
                    MoveDelta = this.mouseMoveDelta
                };
                this.activeControl.OnMouseMove(dfMouseEventArg);
            }
        }