Exemplo n.º 1
0
        /// <summary>
        /// 渲染当前添加的所有元素,在OnGUI进行调用,默认自动进行调用
        /// </summary>
        public void OnGUI()
        {
            Event evt = Event.current;


            if (evt.type == EventType.KeyDown && evt.control && evt.keyCode == KeyCode.R)
            {
                AssetDatabase.Refresh();
                evt.Use();
            }

            //先进行渲染
            if (evt.type == EventType.Repaint)
            {
                foreach (var VARIABLE in _rootElements)
                {
                    VARIABLE.Draw();
                }
            }

            //实现局部事件
            {
                Vector2 mousePos = evt.mousePosition;

                //Drag
                if (_pointerDragIndex != -1 && mousePos != _lastDragMousePosition)
                {
                    IPointerDragHandler drag = _uiElements[_pointerDragIndex] as IPointerDragHandler;
                    if (drag != null)
                    {
                        drag.OnDrag(mousePos - _lastDragMousePosition, mousePos);
                        _lastDragMousePosition = mousePos;
                        _refreshWindow         = true;
                    }
                }


                //PointerExit
                if (_pointerEnterIndex != -1)
                {
                    if (_uiElements.Count <= _pointerEnterIndex)
                    {
                        _pointerEnterIndex = -1;
                    }
                    else
                    {
                        IOverlapPoint ip = _uiElements[_pointerEnterIndex] as IOverlapPoint;
                        if (ip != null && !ip.OverlapPoint(mousePos, evt))
                        {
                            IPointerExitHandler ie = ip as IPointerExitHandler;
                            ie?.OnPointerExit();
                            _pointerEnterIndex = -1;
                        }
                    }
                }

                //PointerClickExit
                if (_pointerClickEnterIndex != -1)
                {
                    if (_uiElements.Count <= _pointerClickEnterIndex)
                    {
                        _pointerClickEnterIndex = -1;
                    }
                    else
                    {
                        IOverlapPoint ip = _uiElements[_pointerClickEnterIndex] as IOverlapPoint;
                        if (ip != null && !ip.OverlapPoint(mousePos, evt))
                        {
                            _pointerClickEnterIndex = -1;
                        }
                    }
                }

                //PointerEnter
                for (int i = _uiElements.Count - 1; i >= 0; i--)
                {
                    IPointerEnterHandler id = _uiElements[i] as IPointerEnterHandler;
                    if (id != null && id.OverlapPoint(mousePos, evt))
                    {
                        if (_pointerEnterIndex == -1 || _pointerEnterIndex < i)
                        {
                            if (_pointerEnterIndex != -1)
                            {
                                IPointerExitHandler ie = _uiElements[_pointerEnterIndex] as IPointerExitHandler;
                                ie?.OnPointerExit();
                            }

                            _pointerEnterIndex = i;
                            id.OnPointerEnter();
                            break;
                        }
                    }
                }

                //PointerClickEnter
                for (int i = _uiElements.Count - 1; i >= 0; i--)
                {
                    IPointerClickHandler id = _uiElements[i] as IPointerClickHandler;
                    if (id != null && id.OverlapPoint(mousePos, evt))
                    {
                        if (_pointerClickEnterIndex == -1 || _pointerClickEnterIndex < i)
                        {
                            _pointerClickEnterIndex = i;
                            break;
                        }
                    }
                }
            }

            //处理鼠标按下
            if (evt.type == EventType.MouseDown)
            {
                Vector2 mousePos = evt.mousePosition;

                bool hasFocus = false;
                //PointerDown
                for (int i = _uiElements.Count - 1; i >= 0; i--)
                {
                    UIElement     ui = _uiElements[i];
                    IOverlapPoint ip = ui as IOverlapPoint;

                    if (ip.OverlapPoint(mousePos, evt))
                    {
                        hasFocus = true;
                        IFocus focus = ui as IFocus;

                        IPointerDownHandler down = ui as IPointerDownHandler;

                        IPointerDragHandler drag = ui as IPointerDragHandler;

                        //Focus
                        if (focus != null)
                        {
                            //向上查找所有父亲
                            UIElement parent = ui;
                            while (parent != null)
                            {
                                focus = parent as IFocus;
                                if (focus != null)
                                {
                                    _focusElementsCur.Add(focus);
                                }

                                parent = parent.Parent;
                            }

                            //取消之前的
                            for (int i2 = _focusElements.Count - 1; i2 >= 0; i2--)
                            {
                                if (!_focusElementsCur.Contains(_focusElements[i2]))
                                {
                                    _focusElements[i2].Focus = false;
                                }
                            }


                            //聚焦现在的,从父亲到孩子
                            for (int i2 = _focusElementsCur.Count - 1; i2 >= 0; i2--)
                            {
                                _focusElementsCur[i2].Focus = true;
                            }

                            //交换之前与现在的列表,并清除现在
                            List <IFocus> temp = _focusElements;
                            _focusElements    = _focusElementsCur;
                            _focusElementsCur = temp;
                            _focusElementsCur.Clear();

                            if (down == null && drag == null)
                            {
                                break;
                            }
                        }

                        if (drag != null && _pointerDragIndex == -1 && evt.button == 0)
                        {
                            _pointerDragIndex = i;
                            drag.OnStartDrag(mousePos);

                            _lastDragMousePosition = mousePos;

                            if (down == null)
                            {
                                break;
                            }
                        }

                        if (down != null)
                        {
                            //PointerDown
                            down.OnPointerDown(mousePos, evt.shift, evt.control, evt.alt);
                            break;
                        }
                    }
                }


                //PointerClickDown
                for (int i = _uiElements.Count - 1; i >= 0; i--)
                {
                    IPointerClickHandler id = _uiElements[i] as IPointerClickHandler;
                    if (id != null && id.OverlapPoint(mousePos, evt) && _pointerClickPressIndex == -1)
                    {
                        _pointerClickPressButton = evt.button;
                        _pointerClickPressIndex  = i;
                        break;
                    }
                }

                //处理空白区域点击取消Focus
                if (!hasFocus)
                {
                    //取消焦点
                    for (int i2 = _focusElements.Count - 1; i2 >= 0; i2--)
                    {
                        _focusElements[i2].Focus = false;
                    }

                    _focusElements.Clear();
                }

                _refreshWindow = true;
            }

            //处理鼠标抬起
            if (evt.type == EventType.MouseUp || evt.type == EventType.MouseLeaveWindow)
            {
                Vector2 mousePos = evt.mousePosition;

                if (evt.button == 0)
                {
                    if (_pointerDragIndex != -1)
                    {
                        if (_uiElements[_pointerDragIndex] is IPointerDragHandler drag)
                        {
                            drag.OnDragEnd(mousePos);
                        }

                        _pointerDragIndex = -1;
                    }
                }

                //PointerUp
                for (int i = _uiElements.Count - 1; i >= 0; i--)
                {
                    IPointerUpHandler id = _uiElements[i] as IPointerUpHandler;
                    if (id != null && id.OverlapPoint(mousePos, evt))
                    {
                        id.OnPointerUp(mousePos);
                        break;
                    }
                }

                //PointerClickUp
                if (_pointerClickPressIndex != -1)
                {
                    if (_pointerClickEnterIndex != -1)
                    {
                        IPointerClickHandler ic = _uiElements[_pointerClickPressIndex] as IPointerClickHandler;
                        if (_pointerClickPressButton == evt.button)
                        {
                            if (_pointerClickPressIndex == _pointerClickEnterIndex)
                            {
                                ic?.OnClick(mousePos);
                            }
                            else
                            {
                                ic?.OnCancelClick(mousePos);
                            }
                        }
                        else
                        {
                            ic?.OnCancelClick(mousePos);
                        }

                        _pointerClickEnterIndex = -1;
                        _pointerClickPressIndex = -1;
                    }
                    else
                    {
                        IPointerClickHandler ic = _uiElements[_pointerClickPressIndex] as IPointerClickHandler;
                        ic?.OnCancelClick(mousePos);
                        _pointerClickEnterIndex = -1;
                        _pointerClickPressIndex = -1;
                    }
                }

                _refreshWindow = true;
            }

            //处理鼠标滚轮滑动
            if (evt.type == EventType.ScrollWheel)
            {
                for (int i = _uiElements.Count - 1; i >= 0; i--)
                {
                    IScrollHandler ic = _uiElements[i] as IScrollHandler;
                    ic?.OnScroll(evt.delta.y);
                }
            }

            //处理键盘按下
            if (evt.type == EventType.KeyDown)
            {
                if (evt.character != 0)
                {
                    for (int i = _uiElements.Count - 1; i >= 0; i--)
                    {
                        ICharacterInputHandler ic = _uiElements[i] as ICharacterInputHandler;
                        ic?.OnCharacterInput(evt.character);
                    }
                }

                for (int i = _uiElements.Count - 1; i >= 0; i--)
                {
                    IKeyDownHandler ui = _uiElements[i] as IKeyDownHandler;
                    ui?.OnKeyDown(evt.keyCode, evt.shift, evt.control, evt.alt);
                }

                evt.Use();

                _refreshWindow = true;
            }

            if (evt.type == EventType.KeyUp)
            {
                for (int i = _uiElements.Count - 1; i >= 0; i--)
                {
                    IKeyUpHandler ui = _uiElements[i] as IKeyUpHandler;
                    ui?.OnKeyUp(evt.keyCode);
                }

                evt.Use();

                _refreshWindow = true;
            }


            //RefreshAllElement
            if (_hasChangedElement)
            {
                RefreshAllElementInline();
                _hasChangedElement = false;
                _refreshWindow     = true;
            }

            //鼠标移动重绘
            if (_lastMoveMousePosition != evt.mousePosition)
            {
                _refreshWindow         = true;
                _lastDragMousePosition = evt.mousePosition;
            }

            //自动重绘界面
            if (_refreshWindow)
            {
                _refreshWindow = false;
                CurEditorWindow.Repaint();
            }
        }
Exemplo n.º 2
0
 static void Execute(IKeyUpHandler handler, BaseEventData eventData)
 {
     handler.OnKeyUp(ExecuteEvents.ValidateEventData <KeyEventData>(eventData));
 }