public void Begin(Camera2d camera) { //Clear hot items before we begin. hotItem = -1; //priteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState); spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, camera.get_transformation(graphics.GraphicsDevice)); }
public void drawGridSystem(MapInfoObject mapInfo, Camera2d camera, GraphicsDeviceManager graphics) { barriersList.Clear(); spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); for (int i = 0; i < mapInfo.gridSizeX; ++i) { for (int j = 0; j < mapInfo.gridSizeY; ++j) { Vector2 transformedV = Vector2.Transform(new Vector2(j * mapInfo.tileSize, i * mapInfo.tileSize), Matrix.Invert(camera.get_transformation(graphics.GraphicsDevice))); var tile = new Rectangle((int)transformedV.X, (int)transformedV.Y, mapInfo.tileSize, mapInfo.tileSize); TileObject tile2 = new TileObject(); tile2.Rectangle = tile; tile2.isGreen = false; barriersList.Add(tile2); DrawBorder(tile, 2, Color.Red); var positionsLabel = "(" + tile2.Rectangle.X + ":" + tile2.Rectangle.Y + ")"; spriteBatch.DrawString(verdana36, positionsLabel, new Vector2(tile2.Rectangle.X, tile2.Rectangle.Y), Color.White); } } spriteBatch.End(); }
public bool DoButton(int id, Rectangle rectangle, Texture2D texture, Camera2d camera) { //Determine if we're hot, and maybe even active if (MouseHit(rectangle, camera)) { hotItem = id; if (activeItem == -1 && mouse.LeftButton == ButtonState.Pressed) { activeItem = id; } } //Draw the button Color drawColor; if (hotItem == id) { if (activeItem == id) { drawColor = Color.White; } else { drawColor = Color.LightGray; } } else { drawColor = Color.Gray; } spriteBatch.Draw(texture, rectangle, drawColor); //If we are hot and active but the mouse is no longer down, we where clicked //Line updated after comment by anonymous user, I feel so silly now! // return (mouse.LeftButton == ButtonState.Released && hotItem == id && activeItem == id ); return(true); }
//http://clintbellanger.net/articles/isometric_math/ //spriteBatch.Draw(texture, new Rectangle(400, 50, 100, 100), null, Color.Red, 0, Vector2.Zero, SpriteEffects.None, 0); public void drawIsometricGridSystem(MapInfoObject mapInfo, Camera2d camera, GraphicsDeviceManager graphics) { barriersList.Clear(); spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); for (int i = 0; i < mapInfo.gridSizeX; ++i) { for (int j = 0; j < mapInfo.gridSizeY; ++j) { // tempPt.x = pt.x - pt.y; //tempPt.y = (pt.x + pt.y) / 2; //var x=j*mapInfo.tileSize/2; //var y=i*mapInfo.tileSize/2; var x = j * mapInfo.tileSize; var y = i * mapInfo.tileSize; //var isotileX = x - y; //var isotileY = (x+y) / 2; //var isotileX = (x - y) * mapInfo.tileSize / 2; //var isotileY = (x + y) * mapInfo.tileSize / 2; Vector2 isoPt = TwoDToIso(new Vector2(x, y)); Vector2 transformedV = Vector2.Transform(new Vector2(isoPt.X, isoPt.Y), Matrix.Invert(camera.get_transformation(graphics.GraphicsDevice))); var tile = new Rectangle((int)transformedV.X, (int)transformedV.Y, mapInfo.tileSize, mapInfo.tileSize); TileObject tile2 = new TileObject(); tile2.Rectangle = tile; tile2.isGreen = false; barriersList.Add(tile2); DrawBorder(tile, 2, Color.Red); var positionsLabel = "(" + tile2.Rectangle.X + ":" + tile2.Rectangle.Y + ")"; spriteBatch.DrawString(verdana36, positionsLabel, new Vector2(tile2.Rectangle.X, tile2.Rectangle.Y), Color.White, 0, Vector2.Zero, new Vector2(1, 1), SpriteEffects.None, 0); } } spriteBatch.End(); }
/// <summary> /// Draws a vertical or horizontal scrollbar at the specified position. If the grip was moved, returns the new value, else returns the inserted value /// </summary> public float DoScrollbar(int id, Rectangle rectangle, Texture2D scrollbarTexture, Texture2D gripTexture, float max, float value, bool horizontal, Camera2d camera) { //No matter the input, value should be at least 0 and at most max value = Math.Min(Math.Max(value, 0), max); //Determine if we're hot, and maybe even active if (MouseHit(rectangle, camera)) //See previous part's code for this method { hotItem = id; if (activeItem == -1 && mouse.LeftButton == ButtonState.Pressed) { activeItem = id; } } //Draw the scrollbar spriteBatch.Draw(scrollbarTexture, rectangle, Color.Red); //Position the grip relative on the scrollbar and make sure the grip stays inside the scrollbar //Note that the grip's width if vertical/height if horizontal is the scrollbar's smallest dimension. int gripPosition; Rectangle grip; if (horizontal) { gripPosition = rectangle.Left + (int)((rectangle.Width - rectangle.Height) * (value / max)); grip = new Rectangle(gripPosition, rectangle.Top, rectangle.Height, rectangle.Height); } else { gripPosition = rectangle.Bottom - rectangle.Width - (int)((rectangle.Height - rectangle.Width) * (value / max)); grip = new Rectangle(rectangle.Left, gripPosition, rectangle.Width, rectangle.Width); } //Draw the grip in the correct color if (activeItem == id || hotItem == id) { spriteBatch.Draw(gripTexture, grip, Color.Red); } else { spriteBatch.Draw(gripTexture, grip, Color.Blue); } //If we're active, calculate the new value and do some bookkeeping to make sure the mouse and grip are in sync if (activeItem == id) { if (horizontal) { //Because the grip's position is defined in the top left corner //we need to shrink the movable domain slightly so our grip doesnt //draw outside the scrollbar, while still getting a full range. float mouseRelative = mouse.X - (rectangle.X + grip.Width / 2); mouseRelative = Math.Min(mouseRelative, rectangle.Width - grip.Width); mouseRelative = Math.Max(0, mouseRelative); //We then calculate the relative mouse offset 0 if the mouse is at //the left end or more to the left. 1 if the mouse is at the right end //or more to the right. We then multiply this by max to get our new value. value = (mouseRelative / (rectangle.Width - grip.Width)) * max; } else { //same as horizontal bit in the end we inverse value, //because we want the bottom to be 0 instead of the top //while in y coordinates the top is 0. float mouseRelative = mouse.Y - (rectangle.Y + grip.Height / 2); mouseRelative = Math.Min(mouseRelative, rectangle.Height - grip.Height); mouseRelative = Math.Max(0, mouseRelative); value = max - (mouseRelative / (rectangle.Height - grip.Height)) * max; } } return(value); }
//Small helper function that checks if the mouse is contained in the rectangle //Might even be unneeded but I like not having to manually write ‘new Point…’ every time. private bool MouseHit(Rectangle rectangle, Camera2d camera) { Vector2 worldPosition = Vector2.Transform(new Vector2(mouse.X, mouse.Y), Matrix.Invert(camera._transform)); return(rectangle.Contains(new Point((int)worldPosition.X, (int)worldPosition.Y))); }
public void updateGridSystem(Texture2D selectedTexture, Camera2d camera) { //spriteBatch.Begin(); foreach (TileObject rect in barriersList) { if (rect.isGreen) { if (rect.texture != null) { if (gridMapType == GridMapType.Default) { spriteBatch.Draw(rect.texture, rect.Rectangle, Color.White); } if (gridMapType == GridMapType.Isometric) { if (rect.isRotated) { Vector2 origin = new Vector2(rect.texture.Width / 2, rect.texture.Height / 2); Rectangle rectangle = new Rectangle(); rectangle = rect.Rectangle; rectangle.X += rect.Rectangle.Width / 2; rectangle.Y += rect.Rectangle.Height / 2; spriteBatch.Draw(rect.texture, rectangle, null, Color.White, rect.rotationAngle, origin, SpriteEffects.None, rect.layerDepth); } else { spriteBatch.Draw(rect.texture, rect.Rectangle, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, rect.layerDepth); } } } else { Texture2D texture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color); texture.SetData <Color>(new Color[] { Color.White }); if (gridMapType == GridMapType.Default) { spriteBatch.Draw(texture, rect.Rectangle, Color.Green); } if (gridMapType == GridMapType.Isometric) { spriteBatch.Draw(texture, rect.Rectangle, null, Color.Green, 0, Vector2.Zero, SpriteEffects.None, rect.layerDepth); } Vector2 worldPos = Vector2.Transform(new Vector2(rect.Rectangle.X, rect.Rectangle.Y), Matrix.Invert(camera._transform)); var positionsLabel = "(" + rect.Rectangle.X + ":" + rect.Rectangle.Y + ")\n(" + worldPos.X + ":" + worldPos.Y + ")"; if (gridMapType == GridMapType.Default) { spriteBatch.DrawString(verdana36, positionsLabel, new Vector2(rect.Rectangle.X, rect.Rectangle.Y), Color.White); } if (gridMapType == GridMapType.Isometric) { spriteBatch.DrawString(verdana36, positionsLabel, new Vector2(rect.Rectangle.X, rect.Rectangle.Y), Color.White, 0, Vector2.Zero, new Vector2(1, 1), SpriteEffects.None, 0); } } } else { DrawBorder(rect.Rectangle, 2, Color.Red); Vector2 worldPos = Vector2.Transform(new Vector2(rect.Rectangle.X, rect.Rectangle.Y), Matrix.Invert(camera._transform)); var positionsLabel = "(" + rect.Rectangle.X + ":" + rect.Rectangle.Y + ")\n(" + worldPos.X + ":" + worldPos.Y + ")"; if (gridMapType == GridMapType.Default) { spriteBatch.DrawString(verdana36, positionsLabel, new Vector2(rect.Rectangle.X, rect.Rectangle.Y), Color.White); } if (gridMapType == GridMapType.Isometric) { spriteBatch.DrawString(verdana36, positionsLabel, new Vector2(rect.Rectangle.X, rect.Rectangle.Y), Color.White, 0, Vector2.Zero, Vector2.Zero, SpriteEffects.None, 0); } } } // spriteBatch.End(); }