public void draw(Rectangle targetRect, Color color, float angle) { SplitedRect drawRect = splitRect(targetRect); Rectangle[] arrayRect = drawRect.toArray(); Texture2D[] arrayImage = _image.toArray(); Vector2 imageCenter = Utils.pointToVector2(drawRect.centerR.Center); for (int i = 0; i < arrayImage.Length; ++i) { Vector2 rectCenter = Utils.pointToVector2(arrayRect[i].Center); float length = Vector2.Distance(rectCenter, imageCenter); Vector2 origin = new Vector2((float)arrayImage[i].Width / 2f, (float)arrayImage[i].Height / 2f); Vector2 scale = new Vector2((float)arrayRect[i].Width / (float)arrayImage[i].Width, (float)arrayRect[i].Height / (float)arrayImage[i].Height); Vector2 newCenter; if (rectCenter == imageCenter) { newCenter = rectCenter; } else { float positionAngle = Utils.directionToAngle(rectCenter - imageCenter); positionAngle += angle; newCenter = new Vector2((float)Math.Cos(positionAngle), (float)Math.Sin(positionAngle)); newCenter *= (length - 1f); newCenter += imageCenter; } MyGame.SpriteBatch.Draw(arrayImage[i], newCenter, null, Color.White, angle, origin, scale, SpriteEffects.None, 0f); } }
/// <summary> /// /// </summary> /// <param name="image"></param> /// <param name="stretchArea">rectangle defining the center area in percentage (0-100)</param> public StretchableImage(Texture2D image, Rectangle stretchArea) { _stretchableArea = stretchArea; Rectangle imgBounds = new Rectangle(0, 0, image.Width, image.Height); _rect = initalSplitRect(imgBounds, stretchArea); _image = splitImage(_rect, image); }
private SplitedImage splitImage(SplitedRect rect, Texture2D image) { SplitedImage splitedImg = new SplitedImage(); splitedImg._topLeft = Utils.CreatePartImage(rect.topLeftR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._topRight = Utils.CreatePartImage(rect.topRightR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._topCenter = Utils.CreatePartImage(rect.topCenterR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._left = Utils.CreatePartImage(rect.leftR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._right = Utils.CreatePartImage(rect.rightR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._bottomLeft = Utils.CreatePartImage(rect.bottomLeftR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._bottomRight = Utils.CreatePartImage(rect.bottomRightR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._bottomCenter = Utils.CreatePartImage(rect.bottomCenterR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._center = Utils.CreatePartImage(rect.centerR, image, MyGame.SpriteBatch.GraphicsDevice); return(splitedImg); }
private SplitedRect initalSplitRect(Rectangle boundingRect, Rectangle stretchArea) { SplitedRect splited = new SplitedRect(); splited.centerR = new Rectangle( boundingRect.Left + (stretchArea.X * boundingRect.Width)/100, boundingRect.Top + (stretchArea.Y * boundingRect.Height)/100, (stretchArea.Width * boundingRect.Width)/100, (stretchArea.Height * boundingRect.Height) / 100 ); splited.topLeftR = new Rectangle(boundingRect.Left, boundingRect.Top , splited.centerR.Left - boundingRect.Left, splited.centerR.Top - boundingRect.Top); splited.topRightR = new Rectangle(splited.centerR.Right, boundingRect.Top , boundingRect.Right - splited.centerR.Right, splited.centerR.Top - boundingRect.Top); splited.topCenterR = new Rectangle(splited.centerR.Left, boundingRect.Top , splited.centerR.Width, splited.centerR.Top - boundingRect.Top); splited.leftR = new Rectangle(boundingRect.Left, splited.centerR.Top , splited.centerR.Left - boundingRect.Left, splited.centerR.Height); splited.rightR = new Rectangle(splited.centerR.Right, splited.centerR.Top , boundingRect.Right - splited.centerR.Right, splited.centerR.Height); splited.bottomLeftR = new Rectangle(boundingRect.Left, splited.centerR.Bottom , splited.centerR.Left - boundingRect.Left, boundingRect.Bottom - splited.centerR.Bottom); splited.bottomRightR = new Rectangle(splited.centerR.Right, splited.centerR.Bottom , boundingRect.Right - splited.centerR.Right, boundingRect.Bottom - splited.centerR.Bottom); splited.bottomCenterR = new Rectangle(splited.centerR.Left, splited.centerR.Bottom , splited.centerR.Width, boundingRect.Bottom - splited.centerR.Bottom); return splited; }
private SplitedRect initalSplitRect(Rectangle boundingRect, Rectangle stretchArea) { SplitedRect splited = new SplitedRect(); splited.centerR = new Rectangle( boundingRect.Left + (stretchArea.X * boundingRect.Width) / 100, boundingRect.Top + (stretchArea.Y * boundingRect.Height) / 100, (stretchArea.Width * boundingRect.Width) / 100, (stretchArea.Height * boundingRect.Height) / 100 ); splited.topLeftR = new Rectangle(boundingRect.Left, boundingRect.Top , splited.centerR.Left - boundingRect.Left, splited.centerR.Top - boundingRect.Top); splited.topRightR = new Rectangle(splited.centerR.Right, boundingRect.Top , boundingRect.Right - splited.centerR.Right, splited.centerR.Top - boundingRect.Top); splited.topCenterR = new Rectangle(splited.centerR.Left, boundingRect.Top , splited.centerR.Width, splited.centerR.Top - boundingRect.Top); splited.leftR = new Rectangle(boundingRect.Left, splited.centerR.Top , splited.centerR.Left - boundingRect.Left, splited.centerR.Height); splited.rightR = new Rectangle(splited.centerR.Right, splited.centerR.Top , boundingRect.Right - splited.centerR.Right, splited.centerR.Height); splited.bottomLeftR = new Rectangle(boundingRect.Left, splited.centerR.Bottom , splited.centerR.Left - boundingRect.Left, boundingRect.Bottom - splited.centerR.Bottom); splited.bottomRightR = new Rectangle(splited.centerR.Right, splited.centerR.Bottom , boundingRect.Right - splited.centerR.Right, boundingRect.Bottom - splited.centerR.Bottom); splited.bottomCenterR = new Rectangle(splited.centerR.Left, splited.centerR.Bottom , splited.centerR.Width, boundingRect.Bottom - splited.centerR.Bottom); return(splited); }
/// <summary> /// Create a splited rect stretched to fit the boudongRect /// </summary> /// <param name="boundingRect"></param> /// <returns></returns> private SplitedRect splitRect(Rectangle boundingRect) { SplitedRect splited = new SplitedRect(); splited.centerR = new Rectangle( boundingRect.Left + _rect.topLeftR.Width, boundingRect.Top + _rect.topLeftR.Height, boundingRect.Width - _rect.topLeftR.Width * 2, boundingRect.Height - _rect.topLeftR.Height * 2 ); splited.topLeftR = new Rectangle(boundingRect.Left, boundingRect.Top , splited.centerR.Left - boundingRect.Left, splited.centerR.Top - boundingRect.Top); splited.topRightR = new Rectangle(splited.centerR.Right, boundingRect.Top , boundingRect.Right - splited.centerR.Right, splited.centerR.Top - boundingRect.Top); splited.topCenterR = new Rectangle(splited.centerR.Left, boundingRect.Top , splited.centerR.Width, splited.centerR.Top - boundingRect.Top); splited.leftR = new Rectangle(boundingRect.Left, splited.centerR.Top , splited.centerR.Left - boundingRect.Left, splited.centerR.Height); splited.rightR = new Rectangle(splited.centerR.Right, splited.centerR.Top , boundingRect.Right - splited.centerR.Right, splited.centerR.Height); splited.bottomLeftR = new Rectangle(boundingRect.Left, splited.centerR.Bottom , splited.centerR.Left - boundingRect.Left, boundingRect.Bottom - splited.centerR.Bottom); splited.bottomRightR = new Rectangle(splited.centerR.Right, splited.centerR.Bottom , boundingRect.Right - splited.centerR.Right, boundingRect.Bottom - splited.centerR.Bottom); splited.bottomCenterR = new Rectangle(splited.centerR.Left, splited.centerR.Bottom , splited.centerR.Width, boundingRect.Bottom - splited.centerR.Bottom); return(splited); }
/// <summary> /// Create a splited rect stretched to fit the boudongRect /// </summary> /// <param name="boundingRect"></param> /// <returns></returns> private SplitedRect splitRect(Rectangle boundingRect) { SplitedRect splited = new SplitedRect(); splited.centerR = new Rectangle( boundingRect.Left + _rect.topLeftR.Width, boundingRect.Top + _rect.topLeftR.Height, boundingRect.Width - _rect.topLeftR.Width * 2, boundingRect.Height - _rect.topLeftR.Height * 2 ); splited.topLeftR = new Rectangle(boundingRect.Left, boundingRect.Top , splited.centerR.Left - boundingRect.Left, splited.centerR.Top - boundingRect.Top); splited.topRightR = new Rectangle(splited.centerR.Right, boundingRect.Top , boundingRect.Right - splited.centerR.Right, splited.centerR.Top - boundingRect.Top); splited.topCenterR = new Rectangle(splited.centerR.Left, boundingRect.Top , splited.centerR.Width, splited.centerR.Top - boundingRect.Top); splited.leftR = new Rectangle(boundingRect.Left, splited.centerR.Top , splited.centerR.Left - boundingRect.Left, splited.centerR.Height); splited.rightR = new Rectangle(splited.centerR.Right, splited.centerR.Top , boundingRect.Right - splited.centerR.Right, splited.centerR.Height); splited.bottomLeftR = new Rectangle(boundingRect.Left, splited.centerR.Bottom , splited.centerR.Left - boundingRect.Left, boundingRect.Bottom - splited.centerR.Bottom); splited.bottomRightR = new Rectangle(splited.centerR.Right, splited.centerR.Bottom , boundingRect.Right - splited.centerR.Right, boundingRect.Bottom - splited.centerR.Bottom); splited.bottomCenterR = new Rectangle(splited.centerR.Left, splited.centerR.Bottom , splited.centerR.Width, boundingRect.Bottom - splited.centerR.Bottom); return splited; }
private SplitedImage splitImage(SplitedRect rect, Texture2D image) { SplitedImage splitedImg = new SplitedImage(); splitedImg._topLeft = Utils.CreatePartImage(rect.topLeftR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._topRight = Utils.CreatePartImage(rect.topRightR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._topCenter = Utils.CreatePartImage(rect.topCenterR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._left = Utils.CreatePartImage(rect.leftR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._right = Utils.CreatePartImage(rect.rightR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._bottomLeft = Utils.CreatePartImage(rect.bottomLeftR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._bottomRight = Utils.CreatePartImage(rect.bottomRightR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._bottomCenter = Utils.CreatePartImage(rect.bottomCenterR, image, MyGame.SpriteBatch.GraphicsDevice); splitedImg._center = Utils.CreatePartImage(rect.centerR, image, MyGame.SpriteBatch.GraphicsDevice); return splitedImg; }