Ejemplo n.º 1
0
        internal static bool CollideBoundingBoxLine(ICollidible boxCollidible, CollisionTypeLine cTypeLine)
        {
            CCRect box = ((CCNode)boxCollidible).BoundingBoxTransformedToWorld;
            float  sx  = cTypeLine.StartPoint.X;
            float  sy  = cTypeLine.StartPoint.Y;
            float  ex  = cTypeLine.EndPoint.X;
            float  ey  = cTypeLine.EndPoint.Y;

            // for performance first check whether both points of the line are outside and on one side of the box
            if ((sx < box.MinX && ex < box.MinX) ||
                (sx > box.MaxX && ex > box.MaxX) ||
                (sy < box.MinY && ey < box.MinY) ||
                (sy > box.MaxY && ey > box.MaxY))
            {
                return(false);
            }
            // check whether the start or end point is contained in the box
            if (box.ContainsPoint(cTypeLine.StartPoint) || box.ContainsPoint(cTypeLine.EndPoint))
            {
                return(true);
            }
            // check for intersections of the line and the box boundaries
            CCPoint[] boxPoints = Constants.CCRectPoints(box);
            for (int i = 0; i < 3; i++)
            {
                if (CCPoint.SegmentIntersect(cTypeLine.StartPoint, cTypeLine.EndPoint, boxPoints[i], boxPoints[i + 1]))
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        // dirty as it only checks whether one contains a point of the other
        internal static bool CollideBoundingBoxPolygon(ICollidible boxCollidible, ICollidible polyCollidible, CollisionTypePolygon cTypePoly)
        {
            // first check the bounding box of the polygon (for performance)
            CCRect box = ((CCNode)boxCollidible).BoundingBoxTransformedToWorld;

            if (box.IntersectsRect(((CCNode)polyCollidible).BoundingBoxTransformedToWorld))
            {
                CCPoint[] boxPoints = Constants.CCRectPoints(box);
                // transform the polygon to match the positioning, rotation and scale of the node
                Polygon transformedPolygon = ((Polygon)cTypePoly.collisionPolygon.Clone());
                transformedPolygon.TransformAccordingToGameObject(polyCollidible);
                foreach (var point in boxPoints)
                {
                    if (transformedPolygon.ContainsPoint(point))
                    {
                        return(true);
                    }
                }
                foreach (var point in transformedPolygon.Points)
                {
                    if (box.ContainsPoint(point))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 3
0
        private bool ToucheBegan(CCTouch touch, CCEvent e)
        {
            var colorToMove = this.piece.Board.IsWhiteMove ? PieceColor.White : PieceColor.Black;

            if (colorToMove != this.piece.Color)
            {
                return(false);
            }

            this.initPosition = this.Position;
            var scaledWidth  = ContentSize.Width * ScaleX;
            var scaledHeight = ContentSize.Height * ScaleY;

            var rect = new CCRect(
                PositionWorldspace.X - scaledWidth / 2,
                PositionWorldspace.Y - scaledHeight / 2,
                scaledWidth,
                scaledHeight);

            if (!rect.ContainsPoint(touch.Location))
            {
                return(false);
            }

            this.AnchorPoint     = new CCPoint(0.2f, 0.5f);
            this.Scale           = this.ScaleX * 3f;
            this.possibleSquares = this.piece.PossibleMoves().ToList();
            this.DrawPossibleMoves();
            return(true);
        }
Ejemplo n.º 4
0
        internal static bool CollideBoundingBoxCircle(CCRect box, CCPoint circlePos, float radius)
        {
            // for peformance first approximate the box with a circle and check whether these two collide
            // if they don't then the circle can't collide with the box either
            float boxRadius = box.Size.Width > box.Size.Height ? box.Size.Width : box.Size.Height;

            if (!CollideCircleCircle(circlePos, radius, box.Center, boxRadius))
            {
                return(false);
            }
            // check whether the circle center is inside the bounding box
            if (box.ContainsPoint(circlePos))
            {
                return(true);
            }
            // check if the circle collides with the lines of the box
            var boxPoints = Constants.CCRectPoints(box);
            int i, j;

            for (i = 0, j = 3; i < 4; j = i++)
            {
                if (CollideCircleLine(circlePos, radius, boxPoints[i], boxPoints[j]))
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 5
0
        private protected override void OnTouchesMovedUI(List <CCTouch> touches, CCEvent touchEvent)
        {
            switch (touches.Count)
            {
            case 1:
            {
                var touch = touches[0];
                if (Rows == 1)
                {
                    var rect = BoundingBoxTransformedToWorld;
                    rect = new CCRect(rect.MinX, rect.MinY + rect.Size.Height / 4, rect.Size.Width, rect.Size.Height / 2);
                    if (!rect.ContainsPoint(touch.Location))
                    {
                        var boxSize = BoxSize * GetTotalScale();
                        foreach (var node in Collection)
                        {
                            var    ccNode  = (CCNode)node;
                            CCRect boxRect = new CCRect(ccNode.PositionWorldspace.X - boxSize.Width / 2, ccNode.PositionWorldspace.Y - boxSize.Height / 2, boxSize.Width, boxSize.Height);
                            if (boxRect.ContainsPoint(new CCPoint(touch.Location.X, touch.StartLocation.Y)))
                            {
                                RemoveFromCollection(node, touch);
                                Pressed = false;
                                return;
                            }
                        }
                    }
                }
                else if (Columns == 1)
                {
                    var rect = BoundingBoxTransformedToWorld;
                    rect = new CCRect(rect.MinX + rect.Size.Width / 4, rect.MinY, rect.Size.Width / 2, rect.Size.Height);
                    if (!rect.ContainsPoint(touch.Location))
                    {
                        var boxSize = BoxSize * GetTotalScale();
                        foreach (var node in Collection)
                        {
                            var    ccNode  = (CCNode)node;
                            CCRect boxRect = new CCRect(ccNode.PositionWorldspace.X - boxSize.Width / 2, ccNode.PositionWorldspace.Y - boxSize.Height / 2, boxSize.Width, boxSize.Height);
                            if (boxRect.ContainsPoint(new CCPoint(touch.StartLocation.X, touch.Location.Y)))
                            {
                                RemoveFromCollection(node, touch);
                                Pressed = false;
                                return;
                            }
                        }
                    }
                }
                // move the collectionNode via scroller
                Scroller.OnTouchesMoved(touches, touchEvent);
            }
            break;

            default:
                break;
            }
        }
Ejemplo n.º 6
0
        public override void TouchesBegan(List <CCTouch> touches)
        {
            CCTouch touch   = touches[0];
            CCNode  clipper = this.GetChildByTag(kTagClipperNode);
            CCPoint point   = clipper.ConvertToNodeSpace(touch.Location);
            CCRect  rect    = new CCRect(0, 0, clipper.ContentSize.Width, clipper.ContentSize.Height);

            m_bScrolling = rect.ContainsPoint(point);
            m_lastPoint  = point;
        }
Ejemplo n.º 7
0
        bool IsPointInNode(CCPoint pt, CCNode node)
        {
            var locationInNode = node.Layer.ScreenToWorldspace(pt);
            var s    = node.ContentSize;
            var rect = new CCRect(0, 0, s.Width, s.Height);

            if (rect.ContainsPoint(locationInNode))
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 8
0
        public override void TouchesBegan(List <CCTouch> touches)
        {
            CCTouch touch = touches[0];
            CCPoint point =
                m_pOuterClipper.ConvertToNodeSpace(touch.Location);
            CCRect rect = new CCRect(0, 0, m_pOuterClipper.ContentSize.Width,
                                     m_pOuterClipper.ContentSize.Height);

            if (!rect.ContainsPoint(point))
            {
                return;
            }
            this.pokeHoleAtPoint(point);
        }
Ejemplo n.º 9
0
 public void OnTouchEnded(CCTouch touch, CCEvent e)
 {
     if (Enable)
     {
         foreach (var control in ControlList)
         {
             var p = control.PositionWorldspace;
             var rect = new CCRect(p.X, p.Y, control.ContentSize.Width, control.ContentSize.Height);
             if (rect.ContainsPoint(Target.Layer.ScreenToWorldspace(touch.LocationOnScreen)))
             {
                 control.OnTouchEnded(touch, e);
                 return;
             }
         }
     }
 }
Ejemplo n.º 10
0
 public void OnTouchEnded(CCTouch touch, CCEvent e)
 {
     if (Enable)
     {
         foreach (var control in ControlList)
         {
             var p    = control.PositionWorldspace;
             var rect = new CCRect(p.X, p.Y, control.ContentSize.Width, control.ContentSize.Height);
             if (rect.ContainsPoint(Target.Layer.ScreenToWorldspace(touch.LocationOnScreen)))
             {
                 control.OnTouchEnded(touch, e);
                 return;
             }
         }
     }
 }
Ejemplo n.º 11
0
        private bool OnTouchBegan(CCTouch touch, CCEvent e)
        {
            if (!Visible)
            {
                return(false);
            }

            var rect = new CCRect(PositionWorldspace.X, PositionWorldspace.Y, ContentSize.Width, ContentSize.Height);

            if (!rect.ContainsPoint(touch.Location))
            {
                return(false);
            }

            OnKeyDown();
            return(true);
        }
Ejemplo n.º 12
0
        void onTouchesBegan(List <CCTouch> touches, CCEvent touchEvent)
        {
            CCTouch touch = touches[0];

            CCPoint point = m_pOuterClipper.WorldToParentspace(
                Layer.ScreenToWorldspace(touch.LocationOnScreen));


            CCRect rect = m_pOuterClipper.BoundingBox;

            if (!rect.ContainsPoint(point))
            {
                return;
            }

            this.pokeHoleAtPoint(point - rect.Origin);
        }
Ejemplo n.º 13
0
        public override void OnEnter()
        {
            base.OnEnter();
            var listener = new CCEventListenerTouchOneByOne();

            listener.IsSwallowTouches = true;

            listener.OnTouchBegan = (CCTouch touch, CCEvent touchEvent) =>
            {
                var    locationInNode = touch.Location;//     Layer.ScreenToWorldspace(touch.LocationOnScreen);
                var    s    = ContentSize;
                CCRect rect = BoundingBoxTransformedToWorld;

                if (rect.ContainsPoint(locationInNode))
                {
                    Color = CCColor3B.Red;
                    return(true);
                }
                return(false);
            };

            listener.OnTouchEnded = (CCTouch touch, CCEvent rouchEvent) =>
            {
                Color = CCColor3B.White;

                if (IsRemoveListenerOnTouchEnded)
                {
                    RemoveEventListener(Listener);
                }
            };

            if (FixedPriority != 0)
            {
                AddEventListener(listener, FixedPriority);
            }
            else
            {
                AddEventListener(listener);
            }

            Listener = listener;
        }
Ejemplo n.º 14
0
        public void OnTouchEnded(CCTouch touch, CCEvent e)
        {
            if (!Visible)
            {
                return;
            }

            var beginrect = new CCRect(StartPoint.X, StartPoint.Y, ContentSize.Width, ContentSize.Height);

            if (!beginrect.ContainsPoint(touch.Location))
            {
                return;
            }

            var rect = new CCRect(PositionWorldspace.X, PositionWorldspace.Y, ContentSize.Width, ContentSize.Height);

            if (!rect.ContainsPoint(touch.Location))
            {
                return;
            }
            OnKeyUp();
        }
Ejemplo n.º 15
0
		bool IsPointInNode(CCPoint pt, CCNode node)
		{
			var locationInNode = node.Layer.ScreenToWorldspace(pt);
			var s = node.ContentSize;
			var rect = new CCRect(0, 0, s.Width, s.Height);

			if (rect.ContainsPoint(locationInNode))
			{
				return true;
			}
			return false;
		}
Ejemplo n.º 16
0
        private bool ToucheBegan(CCTouch touch, CCEvent e)
        {
            var colorToMove = this.piece.Board.IsWhiteMove ? PieceColor.White : PieceColor.Black;
            if (colorToMove != this.piece.Color)
            {
                return false;
            }

            this.initPosition = this.Position;
            var scaledWidth = ContentSize.Width * ScaleX;
            var scaledHeight = ContentSize.Height * ScaleY;

            var rect = new CCRect(
                PositionWorldspace.X - scaledWidth / 2,
                PositionWorldspace.Y - scaledHeight / 2,
                scaledWidth,
                scaledHeight);

            if (!rect.ContainsPoint(touch.Location))
            {
                return false;
            }

            this.AnchorPoint = new CCPoint(0.2f, 0.5f);
            this.Scale = this.ScaleX * 3f;
            this.possibleSquares = this.piece.PossibleMoves().ToList();
            this.DrawPossibleMoves();
            return true;
        }
Ejemplo n.º 17
0
        private bool OnTouchBegan(CCTouch touch, CCEvent e)
        {
            if (!Visible)
            {
                return false;
            }

            var rect = new CCRect(PositionWorldspace.X, PositionWorldspace.Y, ContentSize.Width, ContentSize.Height);
            if (!rect.ContainsPoint(touch.Location))
            {
                return false;
            }

            OnKeyDown();
            return true;
        }
Ejemplo n.º 18
0
        public void OnTouchEnded(CCTouch touch, CCEvent e)
        {
            if (!Visible)
            {
                return;
            }

            var beginrect = new CCRect(StartPoint.X, StartPoint.Y, ContentSize.Width, ContentSize.Height);
            if(!beginrect.ContainsPoint(touch.Location))
            {
                return;
            }

            var rect = new CCRect(PositionWorldspace.X, PositionWorldspace.Y, ContentSize.Width, ContentSize.Height);
            if (!rect.ContainsPoint(touch.Location))
            {
                return;
            }
            OnKeyUp();
        }
Ejemplo n.º 19
0
        public override void OnEnter()
        {
            base.OnEnter();

            var visibleBounds = VisibleBoundsWorldspace;
            var origin        = VisibleBoundsWorldspace.Origin;
            var size          = VisibleBoundsWorldspace.Size;

            var sprite1 = new CCSprite("Images/CyanSquare.png");

            sprite1.Position = origin + size.Center + new CCPoint(-80, 80);
            AddChild(sprite1, 10);

            var sprite2 = new CCSprite("Images/MagentaSquare.png");

            sprite2.Position = origin + size.Center;
            AddChild(sprite2, 20);

            var sprite3 = new CCSprite("Images/YellowSquare.png");

            sprite3.Position = CCPoint.Zero;
            sprite2.AddChild(sprite3, 1);

            // Make sprite1 touchable
            var listener1 = new CCEventListenerTouchOneByOne();

            listener1.IsSwallowTouches = true;

            listener1.OnTouchBegan = (touch, touchEvent) =>
            {
                var target = (CCSprite)touchEvent.CurrentTarget;

                var    locationInNode = touch.Location;
                var    s    = target.ContentSize;
                CCRect rect = target.BoundingBoxTransformedToWorld;

                if (rect.ContainsPoint(locationInNode))
                {
                    CCLog.Log("sprite began... x = {0}, y = {1}", locationInNode.X, locationInNode.Y);
                    target.Opacity = 180;
                    return(true);
                }
                return(false);
            };

            listener1.OnTouchMoved = (touch, touchEvent) =>
            {
                var     target = (CCSprite)touchEvent.CurrentTarget;
                CCPoint pt     = touch.PreviousLocation + touch.Delta;
                target.Position = target.WorldToParentspace(pt);
            };

            listener1.OnTouchEnded = (touch, touchEvent) =>
            {
                var target = (CCSprite)touchEvent.CurrentTarget;
                CCLog.Log("sprite onTouchesEnded..");
                target.Opacity = 255;
                if (target == sprite2)
                {
                    sprite1.LocalZOrder = 100;
                }
                else if (target == sprite1)
                {
                    sprite1.LocalZOrder = 0;
                }
            };


            sprite1.AddEventListener(listener1);
            sprite2.AddEventListener(listener1.Copy());
            sprite3.AddEventListener(listener1.Copy());


            var removeAllTouchItem = new CCMenuItemFont("Remove All Touch Listeners", (sender) => {
                var senderItem        = (CCMenuItemFont)sender;
                senderItem.Label.Text = "Only Next item could be clicked";

                RemoveEventListeners(CCEventListenerType.TOUCH_ONE_BY_ONE);

                var nextItem = new CCMenuItemFont("Next", (senderNext) => NextCallback(senderNext));


                CCMenuItemFont.FontSize = 16;
                nextItem.Position       = new CCPoint(visibleBounds.Origin.X + visibleBounds.Size.Width - 100, 100);

                var menu2         = new CCMenu(nextItem);
                menu2.Position    = CCPoint.Zero;
                menu2.AnchorPoint = CCPoint.AnchorLowerLeft;
                this.AddChild(menu2);
            });

            CCMenuItemFont.FontSize     = 16;
            removeAllTouchItem.Position = new CCPoint(visibleBounds.Origin.X + visibleBounds.Size.Width - 100, 80);

            var menu = new CCMenu(removeAllTouchItem);

            menu.Position    = CCPoint.Zero;
            menu.AnchorPoint = CCPoint.AnchorLowerLeft;
            AddChild(menu);
        }
Ejemplo n.º 20
0
 public static bool IsClickOnMe(this CCRect rect, CCEventMouse ev)
 {
     return(rect.ContainsPoint(new CCPoint(ev.CursorX, ev.CursorY)));
 }
Ejemplo n.º 21
0
 public static void RouteTouchUp(CCNode root, Touch touch)
 {
     CCPoint touchLocation = touch.CCTouch.Location;
     if (root.ChildrenCount > 0)
     {
         for (int i = root.ChildrenCount - 1; i >= 0; i--)
         {
             RouteTouchUp(root.Children[i], touch);  //递归
             if (touch.Handled == true) return;
             ITouch itouch = root.Children[i] as ITouch;
             if (itouch != null)
             {
                 CCNode node = root.Children[i] as CCNode;
                 if (node.Parent.Visible == true && node.Visible == true)
                 {
                     CCPoint local = node.ConvertToNodeSpace(touchLocation);
                     CCRect r = new CCRect(
                         node.PositionX - node.ContentSize.Width * node.AnchorPoint.X,
                         node.PositionY - node.ContentSize.Height * node.AnchorPoint.Y,
                         node.ContentSize.Width,
                         node.ContentSize.Height
                         );
                     r.Origin = CCPoint.Zero;
                     if (r.ContainsPoint(local))
                         itouch.OnTouchDown(touch);
                 }
             }
         }
     }
 }
Ejemplo n.º 22
0
		public override void OnEnter ()
		{
			base.OnEnter ();
			var listener = new CCEventListenerTouchOneByOne();
			listener.IsSwallowTouches = true;

			listener.OnTouchBegan = (CCTouch touch, CCEvent rouchEvent) => 
			{

                var locationInNode = Layer.ScreenToWorldspace(touch.LocationOnScreen);
				var s = ContentSize;
				var rect = new CCRect(0, 0, s.Width, s.Height);

				if (rect.ContainsPoint(locationInNode))
				{
					Color = CCColor3B.Red;
					return true;
				}
				return false;
			};

			listener.OnTouchEnded = (CCTouch touch, CCEvent rouchEvent) =>
			{
				Color = CCColor3B.White;

				if (IsRemoveListenerOnTouchEnded)
				{
                    RemoveEventListener(Listener);
				}
			};

            if (FixedPriority != 0)
            {
                AddEventListener(listener, FixedPriority);
            }
            else
            {
                AddEventListener(listener);
            }

			Listener = listener;
		}
Ejemplo n.º 23
0
		public override void OnEnter ()
		{

			spriteSaved = false;

			base.OnEnter ();

			var origin = Layer.VisibleBoundsWorldspace.Origin;
			var size = Layer.VisibleBoundsWorldspace.Size;

			//MenuItemFont::setFontSize(20);

			var sprite = new CCSprite("Images/CyanSquare.png");
			sprite.Position = origin + size.Center;
			AddChild(sprite, 10);

			// Make sprite1 touchable
			var listener1 = new CCEventListenerTouchOneByOne ();
			listener1.IsSwallowTouches = true;

			listener1.OnTouchBegan = (touch, touchEvent) => 
			{
				var target = (CCSprite) touchEvent.CurrentTarget;

				var locationInNode = target.Layer.ScreenToWorldspace(touch.LocationOnScreen);
				var s = target.ContentSize;
				var rect = new CCRect(0, 0, s.Width, s.Height);

				if (rect.ContainsPoint(locationInNode))
				{
					CCLog.Log("sprite began... x = {0}, y = {1}", locationInNode.X, locationInNode.Y);
					target.Opacity = 180;
					return true;
				}
				return false;
			};

			listener1.OnTouchMoved = (touch, touchEvent) =>
			{
				var target = (CCSprite) touchEvent.CurrentTarget;
				target.Position += touch.Delta;
			};

			listener1.OnTouchEnded = (touch, touchEvent) =>
			{
				var target = (CCSprite) touchEvent.CurrentTarget;
				CCLog.Log("sprite onTouchesEnded.. ");
				target.Opacity = 255;
			};

            sprite.AddEventListener(listener1);

			RunActions(new CCDelayTime(5.0f),
				new CCCallFunc(() => 
				{
					spriteSaved = true;
					sprite.RemoveFromParent();
				}),
				new CCDelayTime(5.0f),
				new CCCallFunc(() =>
				{
					spriteSaved = false;
					AddChild(sprite);
				})
			);


		}
Ejemplo n.º 24
0
        public override void OnEnter()
        {
            spriteSaved = false;

            base.OnEnter();

            var origin = Layer.VisibleBoundsWorldspace.Origin;
            var size   = Layer.VisibleBoundsWorldspace.Size;

            //MenuItemFont::setFontSize(20);

            var sprite = new CCSprite("Images/CyanSquare.png");

            sprite.Position = origin + size.Center;
            AddChild(sprite, 10);

            // Make sprite1 touchable
            var listener1 = new CCEventListenerTouchOneByOne();

            listener1.IsSwallowTouches = true;

            listener1.OnTouchBegan = (touch, touchEvent) =>
            {
                var target = (CCSprite)touchEvent.CurrentTarget;

                var locationInNode = target.Layer.ScreenToWorldspace(touch.LocationOnScreen);
                var s    = target.ContentSize;
                var rect = new CCRect(0, 0, s.Width, s.Height);

                if (rect.ContainsPoint(locationInNode))
                {
                    CCLog.Log("sprite began... x = {0}, y = {1}", locationInNode.X, locationInNode.Y);
                    target.Opacity = 180;
                    return(true);
                }
                return(false);
            };

            listener1.OnTouchMoved = (touch, touchEvent) =>
            {
                var target = (CCSprite)touchEvent.CurrentTarget;
                target.Position += touch.Delta;
            };

            listener1.OnTouchEnded = (touch, touchEvent) =>
            {
                var target = (CCSprite)touchEvent.CurrentTarget;
                CCLog.Log("sprite onTouchesEnded.. ");
                target.Opacity = 255;
            };

            sprite.AddEventListener(listener1);

            RunActions(new CCDelayTime(5.0f),
                       new CCCallFunc(() =>
            {
                spriteSaved = true;
                sprite.RemoveFromParent();
            }),
                       new CCDelayTime(5.0f),
                       new CCCallFunc(() =>
            {
                spriteSaved = false;
                AddChild(sprite);
            })
                       );
        }