public override bool MouseDown(MouseInputEventArgs e)
        {
            if (!ClientArea.Contains(new Point((int)e.Position.X, (int)e.Position.Y)))
            {
                return(false);
            }

            var spritePosition = new Point((int)e.Position.X - Position.X + (int)_elementSprite.ImageOffset.X,
                                           (int)e.Position.Y - Position.Y + (int)_elementSprite.ImageOffset.Y);

            Image.ImageLockBox imgData = _elementSprite.Image.GetImageData();
            imgData.Lock(false);

            Color pixColour = Color.FromArgb((int)(imgData[spritePosition.X, spritePosition.Y]));

            imgData.Dispose();
            imgData.Unlock();

            if (pixColour.A != 0)
            {
                if (Clicked != null)
                {
                    Clicked(this);
                }
                _clickPoint = new Point((int)e.Position.X - Position.X, (int)e.Position.Y - Position.Y);
                _selected   = true;
                return(true);
            }

            _selected = false;
            return(false);
        }
예제 #2
0
        public static bool SpritePixelHit(Sprite toCheck, Vector2D clickPos)
        {
            var clickPoint = new PointF(clickPos.X, clickPos.Y);

            if (!toCheck.AABB.Contains(clickPoint))
            {
                return(false);
            }

            var spritePosition = new Point((int)clickPos.X - (int)toCheck.Position.X + (int)toCheck.ImageOffset.X,
                                           (int)clickPos.Y - (int)toCheck.Position.Y + (int)toCheck.ImageOffset.Y);

            Image.ImageLockBox imgData = toCheck.Image.GetImageData();

            imgData.Lock(false);
            Color pixColour = Color.FromArgb((int)(imgData[spritePosition.X, spritePosition.Y]));

            imgData.Dispose();
            imgData.Unlock();

            return(pixColour.A != 0);
        }
예제 #3
0
        protected virtual bool WasClicked(PointF worldPos)
        {
            if (currentBaseSprite == null || !visible)
            {
                return(false);
            }

            Sprite spriteToCheck = GetActiveDirectionalSprite();

            var AABB =
                new RectangleF(
                    Owner.GetComponent <TransformComponent>(ComponentFamily.Transform).Position.X -
                    (spriteToCheck.Width / 2),
                    Owner.GetComponent <TransformComponent>(ComponentFamily.Transform).Position.Y -
                    (spriteToCheck.Height / 2), spriteToCheck.Width, spriteToCheck.Height);

            if (!AABB.Contains(worldPos))
            {
                return(false);
            }

            var spritePosition = new Point((int)(worldPos.X - AABB.X + spriteToCheck.ImageOffset.X),
                                           (int)(worldPos.Y - AABB.Y + spriteToCheck.ImageOffset.Y));

            Image.ImageLockBox imgData = spriteToCheck.Image.GetImageData();
            imgData.Lock(false);
            Color pixColour = Color.FromArgb((int)(imgData[spritePosition.X, spritePosition.Y]));

            imgData.Dispose();
            imgData.Unlock();

            if (pixColour.A == 0)
            {
                return(false);
            }

            return(true);
        }