using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework; namespace OperationDrawDown { public class SelectionRectangle { Rectangle rectangle; Texture2D selectionTexture; Color color = Color.White; public Rectangle Rectangle { get { return rectangle ; } } public int StartX { get { return rectangle.X; } } public int StartY { get { return rectangle.Y; } } public int EndX { get { return rectangle.X + rectangle.Width; } } public int EndY { get { return rectangle.Y + rectangle.Height; } } public int Width { get { return rectangle.Width; } } public int Height { get { return rectangle.Height; } } public SelectionRectangle() { rectangle = new Rectangle(0,0,0,0); selectionTexture = Engine.content.Load("_textures/SelectionRectangle"); } public SelectionRectangle(int x, int y, int width, int height) { rectangle = new Rectangle(x, y, width, height); selectionTexture = Engine.content.Load("_textures/SelectionRectangle"); } public SelectionRectangle(int x, int y, int width, int height, Color backColor) { rectangle = new Rectangle(x, y, width, height); selectionTexture = Engine.content.Load("_textures/SelectionRectangle"); color = backColor; } public SelectionRectangle(int x, int y, int width, int height, Color backColor, Texture2D drawTexture) { rectangle = new Rectangle(x, y, width, height); selectionTexture = drawTexture; color = backColor; } public SelectionRectangle Clone() { return new SelectionRectangle( this.rectangle.X, this.rectangle.Y, this.rectangle.Width, this.rectangle.Height, this.color, this.selectionTexture ); } /// /// Draw the selection Rectangle. /// public void Draw() { Engine.spriteBatch.Draw(selectionTexture, rectangle, color); } /// /// Returns true if rectangle contains x/y point. /// Enforces that startpoint is at top/left, and endpoint is at bottom/right. /// /// /// /// Returns true if rectangle contains x/y point. public bool Contains(int x, int y) { if (rectangle.Width < 0) { rectangle.X += rectangle.Width; rectangle.Width = -rectangle.Width; } if (rectangle.Height < 0) { rectangle.Y += rectangle.Height; rectangle.Height = -rectangle.Height; } // See if point is within startpoint and endpoint if ( (rectangle.X < x) && (x < rectangle.X + rectangle.Width) && (rectangle.Y < y) && (y < rectangle.Y + rectangle.Height) ) return true; return false; } /// /// Auto-snap selection to a discrete selection of block based on block width. /// Includes all partially selected blocks. /// /// Approximate selection of blocks (usually from mouse selection). /// Number of Pixels in each cell or tile. public void DescretizeSelection(int blockWidth) { rectangle.X = (rectangle.X / blockWidth) * blockWidth; rectangle.Y = (rectangle.Y / blockWidth) * blockWidth; rectangle.Width = ((rectangle.Width + blockWidth) / blockWidth) * blockWidth; rectangle.Height = ((rectangle.Height + blockWidth) / blockWidth) * blockWidth; } /// /// Move Start Point by amount X/Y, keeping the same width and height /// /// Distance to move X /// Distance to move Y public void Move(int dx, int dy) { rectangle.X += dx; rectangle.Y += dy; } /// /// Move Start Point to posiition X/Y, keeping the same width and height. /// /// New X Position /// New Y Position public void MoveTo(int x, int y) { rectangle.X = x; rectangle.Y = y; } /// /// Sets Start Point position w/o effecting End Point position /// /// New X position /// New Y position public void SetStartPoint(int x, int y) { rectangle.Width += rectangle.X - x; rectangle.Height += rectangle.Y - y; rectangle.X = x; rectangle.Y = y; } /// /// Set End Point position w/o effecting Start Point position /// /// New End Point X /// New End Point Y public void SetEndPoint(int x, int y) { rectangle.Width = x - rectangle.X; rectangle.Height= y - rectangle.Y; } /// /// Set Color of the Selection Rectangle. /// /// Red (0-1) /// Green (0-1) /// Blue (0-1) /// Alpha (0-1) public void Color_Set(float r, float g, float b, float a) { color = new Color(new Vector4(r, g, b, a)); } /// /// Modify the Color of the Selection Rectangle. /// /// Red (0-1) /// Green (0-1) /// Blue (0-1) /// Alpha (0-1) public void Color_Modify(float r, float g, float b, float a) { color = new Color(new Vector4( r + color.R, g + color.G, b + color.B, a + color.A )); } } }