Пример #1
0
 protected virtual void MouseUp(UIMouseButtonEventArgs args)
 {
     m_State = UIComponentState.Hover;
 }
Пример #2
0
        internal static void HandleInput(UI ui, ref InputHandlingFlags handlingFlags)           // It has to be done here because at this point we have access to private members but. We don't want to attach every UI component to game engine's events, right?
        {
            Component   tmp;
            UIEventArgs args;

            bool invalid = UIInvalid;

            UIInvalid = false;             // reset invalid flag so that it could be set again by events if needed

            if ((handlingFlags & InputHandlingFlags.Mouse) != InputHandlingFlags.None)
            {
                IMouseDevice mouse = IL.FirstMouseDevice;

                /*
                 * if( m_PrevMouseX < 0 ) { // first initialization of mouse coords
                 *      m_PrevMouseX = mouse.X;
                 *      m_PrevMouseY = mouse.Y;
                 * }
                 * int mouseDeltaX = mouse.X - m_PrevMouseX;
                 * int mouseDeltaY = mouse.Y - m_PrevMouseY;
                 * MouseButton mouseButtonDelta = mouse.Buttons ^ mouse.PrevButtons;
                 */

                bool mouseMoved    = mouse.DeltaX != 0 || mouse.DeltaY != 0;
                bool wheelMoved    = mouse.DeltaWheel != 0;
                bool buttonChanged = mouse.ChangedButtons != MouseButton.None;

                //m_PrevMouseX = mouse.X;
                //m_PrevMouseY = mouse.Y;

                if (mouseMoved && m_ClickButton != MouseButton.None)
                {
                    if (mouse.X < m_ClickMouseX - ClickDistance ||
                        mouse.X > m_ClickMouseX + ClickDistance ||
                        mouse.Y < m_ClickMouseY - ClickDistance ||
                        mouse.Y > m_ClickMouseY + ClickDistance)
                    {
                        // moved outside the max click distance
                        m_ClickButton = MouseButton.None;
                        m_ClickMouseX = -1;
                        m_ClickMouseY = -1;
                    }
                }

                if (mouseMoved || wheelMoved || buttonChanged || invalid)
                {
                    HitTestResult hit = m_LastHit = ui.HitTest(mouse.X, mouse.Y);
                    // GameDebugger.Log("Hit: {0} ({1}, {2})", hit.Target, hit.X, hit.Y);

                    if (m_MouseEventReceiver != null)
                    {
                        // There is a function that has to capture absolutely all mouse events!
                        // Captured events are not propagated to parents!

                        if (mouseMoved && m_MouseEventReceiver != null)
                        {
                            args = new UIMouseMoveEventArgs(UIEventType.MouseMove, hit.Target, new Point2(hit.X, hit.Y), new Point2(mouse.DeltaX, mouse.DeltaY));
                            m_MouseEventReceiver((UIMouseMoveEventArgs)args);
                        }

                        if (wheelMoved && m_MouseEventReceiver != null)
                        {
                            args = new UIMouseWheelEventArgs(UIEventType.MouseWheel, hit.Target, new Point2(hit.X, hit.Y), mouse.DeltaWheel);
                            m_MouseEventReceiver((UIMouseWheelEventArgs)args);
                            // mousewheel will not affect scrollbars
                        }

                        if (buttonChanged && m_MouseEventReceiver != null)
                        {
                            MouseButtonCheckDelegate checkMouseButton = btn => {
                                if ((mouse.ChangedButtons & btn) != MouseButton.None && m_MouseEventReceiver != null)
                                {
                                    if ((mouse.Buttons & btn) == MouseButton.None)
                                    {
                                        // button went up
                                        args = new UIMouseButtonEventArgs(UIEventType.MouseUp, hit.Target, new Point2(hit.X, hit.Y), btn);
                                        m_MouseEventReceiver((UIMouseButtonEventArgs)args);

                                        if (!args.PreventDefault && m_ClickButton == btn && m_MouseEventReceiver != null)
                                        {
                                            m_ClickButton = MouseButton.None;
                                            m_ClickMouseX = -1;
                                            m_ClickMouseY = -1;
                                            args          = new UIMouseButtonEventArgs(UIEventType.MouseClick, hit.Target, new Point2(hit.X, hit.Y), btn);
                                            m_MouseEventReceiver((UIMouseButtonEventArgs)args);
                                            if (!args.PreventDefault && m_MouseEventReceiver != null)
                                            {
                                                if (m_LastClickTime + DoubleClickTimeout >= Application.InternalTimer.Time)
                                                {
                                                    args = new UIMouseButtonEventArgs(UIEventType.MouseDblClick, hit.Target, new Point2(hit.X, hit.Y), btn);
                                                    m_MouseEventReceiver((UIMouseButtonEventArgs)args);
                                                    m_LastClickTime = 0.0;
                                                }
                                                else
                                                {
                                                    m_LastClickTime = Application.InternalTimer.Time;
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // mouse went down
                                        args = new UIMouseButtonEventArgs(UIEventType.MouseDown, hit.Target, new Point2(hit.X, hit.Y), btn);
                                        m_MouseEventReceiver((UIMouseButtonEventArgs)args);
                                        if (!args.PreventDefault)
                                        {
                                            m_ClickButton = btn;
                                            m_ClickMouseX = mouse.X;
                                            m_ClickMouseY = mouse.Y;
                                        }
                                    }
                                }
                            };

                            checkMouseButton(MouseButton.Left);
                            checkMouseButton(MouseButton.Right);
                            checkMouseButton(MouseButton.Middle);
                        }

                        handlingFlags = handlingFlags & ~InputHandlingFlags.Mouse;
                    }
                    else
                    {
                        SetMouseOver(hit.Target, hit.X, hit.Y);

                        if (hit.Target != null)
                        {
                            handlingFlags = handlingFlags & ~InputHandlingFlags.Mouse;

                            if (mouseMoved)
                            {
                                args = new UIMouseMoveEventArgs(UIEventType.MouseMove, hit.Target, new Point2(hit.X, hit.Y), new Point2(mouse.DeltaX, mouse.DeltaY));
                                for (tmp = hit.Target; tmp != null && !args.StopPropagation; tmp = tmp.m_Parent)
                                {
                                    if (tmp.OnMouseMove != null)
                                    {
                                        tmp.OnMouseMove((UIMouseMoveEventArgs)args);
                                    }
                                }
                            }

                            if (wheelMoved)
                            {
                                args = new UIMouseWheelEventArgs(UIEventType.MouseWheel, hit.Target, new Point2(hit.X, hit.Y), mouse.DeltaWheel);
                                for (tmp = hit.Target; tmp != null && !args.StopPropagation; tmp = tmp.m_Parent)
                                {
                                    if (tmp.OnMouseWheel != null)
                                    {
                                        tmp.OnMouseWheel((UIMouseWheelEventArgs)args);
                                    }
                                }

                                if (!args.PreventDefault)
                                {
                                    // Check if there is a scroll bar as a direct child and invoke mouse wheel event on it if there is one.
                                    Component vscb = null, hscb = null;
                                    foreach (Component child in hit.Target.Children)
                                    {
                                        if (child is VScrollBar)
                                        {
                                            vscb = child;
                                            if (hscb != null)
                                            {
                                                break;
                                            }
                                        }
                                        else if (child is HScrollBar)
                                        {
                                            hscb = child;
                                            if (vscb != null)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                    if (vscb != null)
                                    {
                                        hscb = vscb;
                                    }
                                    if (hscb != null && hscb.OnMouseWheel != null)
                                    {
                                        hscb.OnMouseWheel((UIMouseWheelEventArgs)args);
                                    }
                                }
                            }

                            if (buttonChanged)
                            {
                                MouseButtonCheckDelegate checkMouseButton = btn => {
                                    if ((mouse.ChangedButtons & btn) != MouseButton.None)
                                    {
                                        if ((mouse.Buttons & btn) == MouseButton.None)
                                        {
                                            // button went up
                                            args = new UIMouseButtonEventArgs(UIEventType.MouseUp, hit.Target, new Point2(hit.X, hit.Y), btn);
                                            for (tmp = hit.Target; tmp != null && !args.StopPropagation; tmp = tmp.m_Parent)
                                            {
                                                if (tmp.OnMouseUp != null)
                                                {
                                                    tmp.OnMouseUp((UIMouseButtonEventArgs)args);
                                                }
                                            }

                                            if (!args.PreventDefault && m_ClickButton == btn)
                                            {
                                                m_ClickButton = MouseButton.None;
                                                m_ClickMouseX = -1;
                                                m_ClickMouseY = -1;

                                                args = new UIMouseButtonEventArgs(UIEventType.MouseClick, hit.Target, new Point2(hit.X, hit.Y), btn);
                                                for (tmp = hit.Target; tmp != null && !args.StopPropagation; tmp = tmp.m_Parent)
                                                {
                                                    if (tmp.OnMouseClick != null)
                                                    {
                                                        tmp.OnMouseClick((UIMouseButtonEventArgs)args);
                                                    }
                                                }

                                                if (!args.PreventDefault)
                                                {
                                                    if (m_LastClickTime + DoubleClickTimeout >= Application.InternalTimer.Time)
                                                    {
                                                        args = new UIMouseButtonEventArgs(UIEventType.MouseDblClick, hit.Target, new Point2(hit.X, hit.Y), btn);
                                                        for (tmp = hit.Target; tmp != null && !args.StopPropagation; tmp = tmp.m_Parent)
                                                        {
                                                            if (tmp.OnMouseDblClick != null)
                                                            {
                                                                tmp.OnMouseDblClick((UIMouseButtonEventArgs)args);
                                                            }
                                                        }
                                                        m_LastClickTime = 0.0;
                                                    }
                                                    else
                                                    {
                                                        m_LastClickTime = Application.InternalTimer.Time;
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            // mouse went down
                                            args = new UIMouseButtonEventArgs(UIEventType.MouseDown, hit.Target, new Point2(hit.X, hit.Y), btn);
                                            for (tmp = hit.Target; tmp != null && !args.StopPropagation; tmp = tmp.m_Parent)
                                            {
                                                if (tmp.OnMouseDown != null)
                                                {
                                                    tmp.OnMouseDown((UIMouseButtonEventArgs)args);
                                                }
                                            }
                                            if (!args.PreventDefault)
                                            {
                                                m_ClickButton = btn;
                                                m_ClickMouseX = mouse.X;
                                                m_ClickMouseY = mouse.Y;
                                            }
                                        }
                                    }
                                };

                                checkMouseButton(MouseButton.Left);
                                checkMouseButton(MouseButton.Right);
                                checkMouseButton(MouseButton.Middle);
                            }
                        }
                    }
                }
                else
                {
                    // mouse state is same as in previous frame
                    if (m_MouseEventReceiver != null || m_LastHit.Target != null)
                    {
                        handlingFlags = handlingFlags & ~InputHandlingFlags.Mouse;
                    }
                }
            }
        }
Пример #3
0
 protected virtual void MouseDown(UIMouseButtonEventArgs args)
 {
     m_State = UIComponentState.MouseDown;
 }