Beispiel #1
0
        public DisplayObject AddChildAt(DisplayObject child, int index)
        {
            int numChildren = _children.Count;

            if (index >= 0 && index <= numChildren)
            {
                if (child.parent == this)
                {
                    SetChildIndex(child, index);
                }
                else
                {
                    child.RemoveFromParent();
                    if (index == numChildren)
                    {
                        if (_isRendering)
                        {
                            CacheAddList(_children.Count, child);
                        }
                        else
                        {
                            _children.Add(child);
                        }
                    }
                    else
                    {
                        if (_isRendering)
                        {
                            CacheAddList(index, child);
                        }
                        else
                        {
                            _children.Insert(index, child);
                        }
                    }
                    child.SetParent(this);

                    if (stage != null)
                    {
                        Container container = child as Container;
                        if (container != null)
                        {
                            container.BroadcastEvent(sHelperEvent1);
                            container.onAddedToStage.BroadcastCall();
                        }
                        else
                        {
                            child.DispatchEventObsolete(sHelperEvent1);
                            child.onAddedToStage.Call();
                        }
                    }
                }
                return(child);
            }
            else
            {
                throw new Exception("Invalid child index");
            }
        }
Beispiel #2
0
        public DisplayObject RemoveChildAt(int index)
        {
            if (index >= 0 && index < _children.Count)
            {
                DisplayObject child = _children[index];

                if (stage != null)
                {
                    Container container = child as Container;
                    if (container != null)
                    {
                        container.BroadcastEvent(sHelperEvent2);
                        container.onRemovedFromStage.BroadcastCall();
                    }
                    else
                    {
                        child.DispatchEventObsolete(sHelperEvent2);
                        child.onRemovedFromStage.Call();
                    }
                }


                if (_isRendering)
                {
                    _cahcheRemoveList.Add(child);
                }
                else
                {
                    _children.Remove(child);
                }

                child.SetParent(null);

                return(child);
            }
            else
            {
                throw new Exception("Invalid child index");
            }
        }
Beispiel #3
0
        public void OnGUI()
        {
            Event evt = Event.current;

            int w, h;

            w = Screen.width;
            h = Screen.height;
            if (w != _stageWidth || h != _stageHeight)
            {
                _stageWidth  = w;
                _stageHeight = h;
                DispatchEventObsolete(eResize);
            }

            //if(evt.type==EventType.Repaint)
            //Debug.Log(""+(Timers.time*1000) + evt.isMouse + "," + evt.type);
            bool hitTested = false;

            if (_mouseX != evt.mousePosition.x || _mouseY != evt.mousePosition.y)
            {
                _objectUnderMouse = HitTest(_lastMousePos, true);
                hitTested         = true;

                _mouseX = evt.mousePosition.x;
                _mouseY = evt.mousePosition.y;

                eMouseMove.InitFrom(evt);
                DispatchEventObsolete(eMouseMove);

                _lastMousePos.x = _mouseX;
                _lastMousePos.y = _mouseY;
                if (_lastRollOver != _objectUnderMouse)
                {
                    if (_lastRollOver != null && _lastRollOver.stage != null)
                    {
                        //rollout
                        eMouseOut.InitFrom(evt);
                        _lastRollOver.DispatchEventObsolete(eMouseOut);
                    }

                    if (_objectUnderMouse != null)
                    {
                        _lastRollOver = _objectUnderMouse;
                        eMouseOver.InitFrom(evt);
                        _objectUnderMouse.DispatchEventObsolete(eMouseOver);
                    }
                }
            }

            if (evt.isMouse)
            {
                if (evt.button == 0)
                {
                    if (evt.type == EventType.MouseDown)
                    {
                        if (!_isMouseDown)
                        {
                            _isMouseDown = true;
                            if (!hitTested)
                            {
                                _objectUnderMouse = HitTest(_lastMousePos, true);
                            }

                            _clickCount         = evt.clickCount;
                            _lastMouseDownPos.x = _mouseX;
                            _lastMouseDownPos.y = _mouseY;
                            if (_objectUnderMouse != null)
                            {
                                _focused = _objectUnderMouse;
                                eMouseDown.InitFrom(evt);
                                _objectUnderMouse.DispatchEventObsolete(eMouseDown);
                            }
                            else
                            {
                                _focused = this;
                            }
                        }
                    }
                    else if (evt.type == EventType.MouseUp)
                    {
                        if (_isMouseDown)
                        {
                            _isMouseDown = false;
                            if (!hitTested)
                            {
                                _objectUnderMouse = HitTest(_lastMousePos, true);
                            }

                            if (_objectUnderMouse != null)
                            {
                                eMouseUp.InitFrom(evt);
                                _objectUnderMouse.DispatchEventObsolete(eMouseUp);

                                if (Math.Abs(_mouseX - _lastMouseDownPos.x) < 10 && Math.Abs(_mouseY - _lastMouseDownPos.y) < 10)
                                {
                                    eMouseClick.InitFrom(evt);
                                    eMouseClick.ClickCount = _clickCount;
                                    _objectUnderMouse.DispatchEventObsolete(eMouseClick);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (evt.button == 1)
                    {
                        if (evt.type == EventType.MouseDown)
                        {
                            //ignore
                        }
                        else if (evt.type == EventType.MouseUp)
                        {
                            if (!hitTested)
                            {
                                _objectUnderMouse = HitTest(_lastMousePos, true);
                            }
                            if (_objectUnderMouse != null)
                            {
                                eMouseRightClick.InitFrom(evt);
                                _objectUnderMouse.DispatchEventObsolete(eMouseRightClick);
                            }
                        }
                    }
                }
            }
            else if (evt.type == EventType.scrollWheel)
            {
                if (!hitTested)
                {
                    _objectUnderMouse = HitTest(_lastMousePos, true);
                }
                if (_objectUnderMouse != null)
                {
                    eMouseWheel.InitFrom(evt);
                    _objectUnderMouse.DispatchEventObsolete(eMouseWheel);
                }
            }
            else if (evt.isKey)
            {
                if (evt.keyCode != KeyCode.None)
                {
                    if (evt.type == EventType.keyDown)
                    {
                        eKeyDown.InitFrom(evt);
                        _focused.HandleKeyEvent(eKeyDown);
                        if (eKeyDown.isDefaultPrevented)
                        {
                            _ignoreNextKeydown = true;
                            evt.Use();
                        }
                        else
                        {
                            _ignoreNextKeydown = false;
                        }
                    }
                    else
                    {
                        eKeyUp.InitFrom(evt);
                        _focused.HandleKeyEvent(eKeyUp);
                        _ignoreNextKeydown = false;
                    }
                }
                else
                {
                    if (_ignoreNextKeydown)
                    {
                        evt.Use();
                    }
                }
            }

            if (evt.type == EventType.Repaint || (_focused is TextField))
            {
                if (_invalidated)
                {
                    _invalidated = false;
                    DispatchEventObsolete(eRender);
                }
                if (_focused != null && _focused != this)
                {
                    GUI.FocusControl(_focused.internalId);
                }
                else
                {
                    GUI.FocusControl(string.Empty);
                }
                support.Reset();
                Render(support, 1);
            }
        }