Пример #1
0
        private DisplayObjectX findCommonParent(DisplayObjectX object1, DisplayObjectX object2)
        {
            DisplayObjectX currentObject = object1;

            while (currentObject != null)
            {
                sAncestors.Add(currentObject);
                currentObject = currentObject.mParent;
            }

            currentObject = object2;
            while (currentObject != null && sAncestors.IndexOf(currentObject) == -1)
            {
                currentObject = currentObject.mParent;
            }

            sAncestors.Clear();

            if (currentObject != null)
            {
                return(currentObject);
            }

            throw new ArgumentException("Object not connected to target");
        }
Пример #2
0
        public override void render(RenderSupport support, float parentAlpha)
        {
            float alpha       = parentAlpha * this.alpha;
            int   numChildren = mChildren.Count;


            for (int i = 0; i < numChildren; ++i)
            {
                DisplayObjectX child = mChildren[i];

                if (child.hasVisibleArea)
                {
                    child.sceneTransform = Transform2X.multiply(mSceneTransform, child.transform);
                    support.blendMode    = child.blendMode;

//                    if (filter)
//                    {
//                        filter.render(child, support, alpha);
//                    }
//                    else
//                    {
                    child.render(support, alpha);
//                    }

                    support.blendMode = blendMode;
                }
            }
        }
Пример #3
0
 private void touchEndHandle()
 {
     if (_draggingTarget == this)
     {
         _draggingTarget = null;
     }
 }
Пример #4
0
        private void resetContents()
        {
            removeEventListener(MouseEventX.MOUSE_UP, onMouseUp);
            removeEventListener(MouseEventX.MOUSE_LEAVE, onMouseLeave);

            mIsDown          = false;
            mBackground      = mUpState;
            mContents.x      = mContents.y = 0f;
            mContents.scaleX = mContents.scaleY = 1.0f;
        }
Пример #5
0
        public DisplayObjectX removeChild(DisplayObjectX child, bool dispose = false)
        {
            int childIndex = getChildIndex(child);

            if (childIndex != -1)
            {
                removeChildAt(childIndex, dispose);
            }
            return(child);
        }
Пример #6
0
 public void upState(DisplayObjectX value)
 {
     if (mUpState != value)
     {
         mUpState = value;
         if (!mIsDown)
         {
             mBackground = value;
         }
     }
 }
Пример #7
0
 public void downState(DisplayObjectX value)
 {
     if (mDownState != value)
     {
         mDownState = value;
         if (mIsDown)
         {
             mBackground = value;
         }
     }
 }
Пример #8
0
        public override RectangleX getBounds(DisplayObjectX targetSpace)
        {
            RectangleX resultRect = new RectangleX();


            int numChildren = mChildren.Count;

            if (numChildren == 0)
            {
                getTransformationMatrix(targetSpace, sHelperTransform);
                //todo;
                resultRect.setTo(0, 0, 0, 0);
            }
            else if (numChildren == 1)
            {
                resultRect = mChildren[0].getBounds(targetSpace);
            }
            else
            {
                float minX = float.MaxValue;
                float maxX = -float.MaxValue;
                float minY = float.MinValue;
                float maxY = -float.MinValue;

                for (int i = 0; i < numChildren; ++i)
                {
                    resultRect = mChildren[i].getBounds(targetSpace);

                    if (minX > resultRect.x)
                    {
                        minX = resultRect.x;
                    }
                    if (maxX < resultRect.width)
                    {
                        maxX = resultRect.width;
                    }
                    if (minY > resultRect.y)
                    {
                        minY = resultRect.y;
                    }
                    if (maxY < resultRect.height)
                    {
                        maxY = resultRect.height;
                    }
                }

                resultRect.setTo(minX, minY, maxX - minX, maxY - minY);
            }

            return(resultRect);
        }
Пример #9
0
        public virtual DisplayObjectX addChildAt(DisplayObjectX child, int index)
        {
            int numChildren = mChildren.Count;

            if (index >= 0 && index <= numChildren)
            {
                if (child.parent == this)
                {
                    setChildIndex(child, index); // avoids dispatching events
                }
                else
                {
                    child.removeFromParent();

                    // 'splice' creates a temporary object, so we avoid it if it's not necessary
                    if (index == numChildren)
                    {
                        mChildren.Add(child);
                    }
                    else
                    {
                        mChildren.Insert(index, child);
                    }

                    child.setParent(this);
                    //child.dispatchEventWith(EventX.ADDED, true);

                    if (stage != null)
                    {
                        DisplayObjectContainerX container = child as DisplayObjectContainerX;
                        if (container != null)
                        {
                            //递归上去;
                            //container.simpleDispatch(EventX.ADDED_TO_STAGE);
                        }
                        else
                        {
                            child.simpleDispatch(EventX.ADDED_TO_STAGE);
                        }
                    }
                }

                return(child);
            }
            else
            {
                throw new RankException();
            }
        }
Пример #10
0
 public bool contains(DisplayObjectX child)
 {
     while (child != null)
     {
         if (child == this)
         {
             return(true);
         }
         else
         {
             child = child.parent;
         }
     }
     return(false);
 }
Пример #11
0
        internal void setParent(DisplayObjectContainerX value)
        {
            DisplayObjectX ancestor = value;

            while (ancestor != this && ancestor != null)
            {
                ancestor = ancestor.mParent;
            }
            if (ancestor == this)
            {
                throw new ArgumentException(
                          "An object cannot be added as a child to itself or one of its children (or children's children, etc.)");
            }
            else
            {
                mParent = value;
            }
        }
Пример #12
0
        public void setChildIndex(DisplayObjectX child, int index)
        {
            int oldIndex = getChildIndex(child);

            if (oldIndex == index)
            {
                return;
            }
            if (oldIndex == -1)
            {
                throw new ArgumentException("Not a child of this container");
            }

            mChildren.RemoveAt(oldIndex);

            index = Math.Min(numChildren, index);
            mChildren.Insert(index, child);
        }
Пример #13
0
        public Button(DisplayObjectX upState, string text = "", DisplayObjectX downState = null, TextFormat format = null)
        {
            //if (upState == null) throw new ErrorEvent("Texture cannot be null");

            mParent     = upState.parent;
            mUpState    = upState;
            mDownState  = downState != null ? downState : upState;
            mBackground = upState;
            mTextFormat = format;

            mScaleWhenDown     = 0.9f;
            mAlphaWhenDisabled = 0.5f;
            mEnabled           = true;
            mIsDown            = false;
            mUseHandCursor     = true;
            mTextBounds        = new RectangleX(0, 0, upState.width, upState.height);

            mContents = new DisplayObjectContainerX();
            mContents.addChild(mBackground);
            addChild(mContents);

            //addEventListener(TouchEvent.TOUCH, onTouch);
            addEventListener(MouseEventX.MOUSE_DOWN, onMouseDown);

            if (text.Length > 0)
            {
                this.text = text;
            }

            this.x    = upState.x;
            this.y    = upState.y;
            upState.x = upState.y = 0;

            if (mParent != null)
            {
                mParent.addChild(this);
            }
        }
Пример #14
0
        public DisplayObjectX removeChildAt(int index, bool dispose = false)
        {
            if (index >= 0 && index < numChildren)
            {
                DisplayObjectX child = mChildren[index];
                child.simpleDispatch(EventX.REMOVED);

                if (stage != null)
                {
                    DisplayObjectContainerX container = child as DisplayObjectContainerX;
                    if (container != null)
                    {
                        container.broadcastEventWith(EventX.REMOVED_FROM_STAGE);
                    }
                    else
                    {
                        child.simpleDispatch(EventX.REMOVED_FROM_STAGE);
                    }
                }

                child.setParent(null);
                index = mChildren.IndexOf(child);
                if (index >= 0)
                {
                    mChildren.RemoveAt(index);
                }
                if (dispose)
                {
                    child.dispose();
                }

                return(child);
            }
            else
            {
                throw new RankException("Invalid child index");
            }
        }
Пример #15
0
        private void onMouseDown(EventX e)
        {
            MouseEventX mouseEvent = e as MouseEventX;
            Touch       touch      = mouseEvent.touch;

            if (!mEnabled)
            {
                return;
            }

            if (touch.phase == TouchPhase.Began && !mIsDown)
            {
                mBackground = mDownState;
                addEventListener(MouseEventX.MOUSE_UP, onMouseUp);
                addEventListener(MouseEventX.MOUSE_LEAVE, onMouseLeave);

                mContents.scaleX = mContents.scaleY = mScaleWhenDown;
                mContents.x      = (1.0f - mScaleWhenDown) / 2.0f * mBackground.width;
                mContents.y      = (1.0f - mScaleWhenDown) / 2.0f * mBackground.height;
                mIsDown          = true;
                //Debug.Log( "mouse down" );
            }
        }
Пример #16
0
        protected void layout(DisplayObjectX _displayItem, int i)
        {
            int oriXY = 0;

            if (_vertical)
            {
                if (_maxDirect == -1)
                {
                    if (isCenter)
                    {
//居中
                        oriXY = (centerWidth - itemLen * _itemBoundHeight) / 2;
                    }
                    _displayItem.y = oriXY + i * _itemBoundHeight + _border.top;
                    _displayItem.x = _border.left;
                    return;
                }
                _displayItem.x = (int)(i / _maxDirect) * _itemBoundWidth + _border.left;
                _displayItem.y = (i % _maxDirect) * _itemBoundHeight + _border.top;
            }
            else
            {
                if (_maxDirect == -1)
                {
                    if (isCenter)
                    {
//居中
                        oriXY = (centerWidth - itemLen * _itemBoundWidth) / 2;
                    }
                    _displayItem.x = oriXY + i * _itemBoundWidth + _border.left;
                    _displayItem.y = _border.top;
                    return;
                }
                _displayItem.y = (int)(i / _maxDirect) * _itemBoundHeight + _border.top;
                _displayItem.x = (i % _maxDirect) * _itemBoundWidth + _border.left;
            }
        }
Пример #17
0
        private void touchMoveHandle(EventX e)
        {
            Vector2X p      = (Vector2X)e.data;
            float    stageX = p.x;
            float    stageY = p.y;

            float dx = stageX - mx0;
            float dy = stageY - my0;

            if (!_isDragging)
            {
                if (Math.Abs(dx) < draggingDistanceX || Math.Abs(dy) < draggingDistanceY || (dx * dx + dy * dy) < MIN_DISTANCE_SQ)
                {
                    return;
                }
                _isDragging     = true;
                _draggingTarget = this;
                CallLater.Remove(touchEndHandle);

                touchStart(dx, dy);//这里面可能会有需求再变为不可拖;

                if (hasEventListener(TouchEventX.TOUCH_START))
                {
                    this.dispatchEvent(new TouchEventX(TouchEventX.TOUCH_START, 0, 0, dx, dy));
                }
            }

            touchMoving(stageX - mx, stageY - my, dx, dy);
            if (hasEventListener(TouchEventX.TOUCH_START))
            {
                this.dispatchEvent(new TouchEventX(TouchEventX.TOUCH_START, stageX - mx, stageY - my, dx, dy));
            }

            mx = stageX;
            my = stageY;
        }
Пример #18
0
 public virtual DisplayObjectX addChild(DisplayObjectX child)
 {
     addChildAt(child, numChildren);
     return(child);
 }
Пример #19
0
 public int getChildIndex(DisplayObjectX child)
 {
     return(mChildren.IndexOf(child));
 }
Пример #20
0
 public virtual RectangleX getBounds(DisplayObjectX targetSpace)
 {
     throw new NotImplementedException();
 }
Пример #21
0
 protected void setAlpha(DisplayObjectX v, float alpha)
 {
     v.alpha = alpha;
 }
Пример #22
0
        private void updateInput()
        {
            if (_stage == null)
            {
                return;
            }
            bool        isNew = false;
            MouseEventX mouseEvent;
            Event       currentEvent = Event.current;

            if (currentEvent != null && currentEvent.button == 0)
            {
                Vector2 t = currentEvent.mousePosition;
                mp.setTo(t.x, t.y);

                _collidingObject = _stage.hitTest(mp);
                isNew            = (_previousCollidingObject != _collidingObject);

                if (currentEvent.type == EventType.MouseDown)
                {
                    mouseEvent = new MouseEventX(MouseEventX.MOUSE_DOWN, mp);

                    if (_previousCollidingObject != null && isNew)
                    {
                        _previousCollidingObject.dispatchEvent(new MouseEventX(MouseEventX.MOUSE_UP, mp));
                    }

                    if (_collidingObject != null && isNew)
                    {
                        _collidingObject.dispatchEvent(mouseEvent);
                    }

                    _stage.dispatchEvent(mouseEvent);
                }
                else if (currentEvent.type == EventType.MouseUp)
                {
                    mouseEvent = new MouseEventX(MouseEventX.MOUSE_UP, mp);
                    if (_collidingObject != null && isNew)
                    {
                        _collidingObject.dispatchEvent(mouseEvent);
                    }
                    _stage.dispatchEvent(mouseEvent);
                }
                else if (currentEvent.type == EventType.mouseMove)
                {
                    mouseEvent = new MouseEventX(MouseEventX.MOUSE_MOVE, mp);

                    if (_collidingObject != null && isNew == false)
                    {
                        _collidingObject.dispatchEvent(mouseEvent);
                    }

                    _stage.dispatchEvent(mouseEvent);
                }



                if (_collidingObject != _previousCollidingObject)
                {
                    if (_previousCollidingObject != null)
                    {
                        _previousCollidingObject.dispatchEvent(new MouseEventX(MouseEventX.MOUSE_OUT, mp));
                    }
                    if (_collidingObject != null)
                    {
                        _collidingObject.dispatchEvent(new MouseEventX(MouseEventX.MOUSE_OVER, mp));
                    }
                }
                else if (_collidingObject != null)
                {
                    _collidingObject.dispatchEvent(new MouseEventX(MouseEventX.MOUSE_MOVE, mp));
                }

                _previousCollidingObject = _collidingObject;



                return;
            }

            //todo;
            int touchCount = Input.touchCount;

            if (touchCount < 1)
            {
                if (_previousCollidingObject != null)
                {
                    _previousCollidingObject.dispatchEvent(new MouseEventX(MouseEventX.MOUSE_UP, null));
                    _previousCollidingObject = null;
                }
                return;
            }
            Touch touch = Input.touches[0];

            mp.setTo(touch.position.x, touch.position.y);
            _collidingObject = stage.hitTest(mp);

            isNew = (_previousCollidingObject != _collidingObject);

            switch (touch.phase)
            {
            case TouchPhase.Began:

                if (_previousCollidingObject != null && isNew)
                {
                    _previousCollidingObject.dispatchEvent(new MouseEventX(MouseEventX.MOUSE_UP, mp));
                }

                if (_collidingObject != null && isNew)
                {
                    _collidingObject.dispatchEvent(new MouseEventX(MouseEventX.MOUSE_DOWN, mp));
                }
                break;


            case TouchPhase.Canceled:
            case TouchPhase.Ended:

                if (_previousCollidingObject != null)
                {
                    _previousCollidingObject.dispatchEvent(new MouseEventX(MouseEventX.MOUSE_UP, mp));
                }
                break;

            case TouchPhase.Moved:

                if (_collidingObject != null && isNew == false)
                {
                    _collidingObject.dispatchEvent(new MouseEventX(MouseEventX.MOUSE_MOVE, mp));
                }

                break;
            }


            _previousCollidingObject = _collidingObject;
        }
Пример #23
0
        public override RectangleX getBounds(DisplayObjectX targetSpace)
        {
            RectangleX resultRect = new RectangleX();

            if (targetSpace == this) // optimization
            {
                resultRect.setTo(0.0f, 0.0f, vertices[2].x, vertices[2].y);
            }
            else if (targetSpace == parent && rotation == 0.0) // optimization
            {
                float    scaleX = this.scaleX;
                float    scaleY = this.scaleY;
                Vector3X v      = vertices[2];

                resultRect.setTo(x - pivotX * scaleX, y - pivotY * scaleY, v.x * scaleX, v.y * scaleY);
                if (scaleX < 0)
                {
                    resultRect.width *= -1;
                    resultRect.x     -= resultRect.width;
                }
                if (scaleY < 0)
                {
                    resultRect.height *= -1;
                    resultRect.y      -= resultRect.height;
                }
            }
            else
            {
                getTransformationMatrix(targetSpace, sHelperTransform);

                float minX = float.MaxValue;
                float maxX = -float.MaxValue;
                float minY = float.MinValue;
                float maxY = -float.MinValue;

                int len = vertices.Length;
                for (int i = 0; i < len; i++)
                {
                    Vector3X v = sHelperTransform.transformVector(vertices[i]);

                    if (minX > v.x)
                    {
                        minX = v.x;
                    }
                    if (maxX < v.x)
                    {
                        maxX = v.x;
                    }
                    if (minY > v.y)
                    {
                        minY = v.y;
                    }
                    if (maxY < v.y)
                    {
                        maxY = v.y;
                    }
                }

                resultRect.setTo(minX, minY, maxX - minX, maxY - minY);
            }

            return(resultRect);
        }
Пример #24
0
        public Transform2X getTransformationMatrix(DisplayObjectX targetSpace, Transform2X resultMatrix = null)
        {
            DisplayObjectX commonParent;
            DisplayObjectX currentObject;

            if (resultMatrix != null)
            {
                resultMatrix.identity();
            }
            else
            {
                resultMatrix = new Transform2X();
            }

            if (targetSpace == this)
            {
                return(resultMatrix);
            }
            else if (targetSpace == mParent || (targetSpace == null && mParent == null))
            {
                resultMatrix.copyFrom(transform);
                return(resultMatrix);
            }
            else if (targetSpace == null || targetSpace == stage)
            {
                // targetCoordinateSpace 'null' represents the target space of the base object.
                // -> move up from this to base

                currentObject = this;
                while (currentObject != targetSpace)
                {
                    resultMatrix  = Transform2X.multiply(resultMatrix, currentObject.transform);
                    currentObject = currentObject.mParent;
                }

                return(resultMatrix);
            }
            else if (targetSpace.mParent == this) // optimization
            {
                targetSpace.getTransformationMatrix(this, resultMatrix);
                resultMatrix.invert();

                return(resultMatrix);
            }

            // 1. find a common parent of this and the target space
            commonParent = findCommonParent(this, targetSpace);
            // 2. move up from this to common parent

            currentObject = this;
            while (currentObject != commonParent)
            {
                resultMatrix  = Transform2X.multiply(resultMatrix, currentObject.transform);
                currentObject = currentObject.mParent;
            }

            if (commonParent == targetSpace)
            {
                return(resultMatrix);
            }
            // 3. now move up from target until we reach the common parent

            sHelperTransform.identity();
            currentObject = targetSpace;
            while (currentObject != commonParent)
            {
                sHelperTransform = Transform2X.multiply(sHelperTransform, currentObject.transform);
                currentObject    = currentObject.mParent;
            }

            // 4. now combine the two matrices

            sHelperTransform.invert();
            resultMatrix = Transform2X.multiply(resultMatrix, sHelperTransform);

            return(resultMatrix);
        }