SelectionRectangle forms in all directions

This commit is contained in:
Niklas Ulf Anders Cullberg
2012-10-03 11:01:41 +02:00
parent 8a75c98f48
commit 4658decf32
+190 -169
View File
@@ -1,169 +1,190 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Media;
namespace DepthsBelow namespace DepthsBelow
{ {
public class MouseInput public class MouseInput
{ {
Core core; Core core;
MouseState lastMouseState; MouseState lastMouseState;
Rectangle selectionRectangle; Rectangle selectionRectangle;
Texture2D selectionTexture; Texture2D selectionTexture;
Rectangle gridRectangle; Rectangle gridRectangle;
Texture2D gridTexture; Texture2D gridTexture;
Vector2 directionStart; Vector2 directionStart;
Vector2 directionNow; Vector2 directionNow;
bool checkingDirection = false; bool checkingDirection = false;
bool readjust = false;
public MouseInput(Core core)
{ public MouseInput(Core core)
this.core = core; {
this.core = core;
selectionRectangle = Rectangle.Empty;
gridRectangle = Rectangle.Empty; selectionRectangle = Rectangle.Empty;
} gridRectangle = Rectangle.Empty;
}
public void LoadContent()
{ public void LoadContent()
selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1); {
selectionTexture.SetData(new Color[] { Color.White }); selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1);
selectionTexture.SetData(new Color[] { Color.White });
gridTexture = new Texture2D(core.GraphicsDevice, 1, 1);
gridTexture.SetData(new Color[] { Color.White }); gridTexture = new Texture2D(core.GraphicsDevice, 1, 1);
} gridTexture.SetData(new Color[] { Color.White });
}
public void Update(GameTime gameTime)
{ public void Update(GameTime gameTime)
MouseState ms = Mouse.GetState(); {
Vector2 mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y)); MouseState ms = Mouse.GetState();
KeyboardState ks = Keyboard.GetState(); Vector2 mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y));
KeyboardState ks = Keyboard.GetState();
// Selection rectangle
if (ms.LeftButton == ButtonState.Pressed) // Selection rectangle
{ if (ms.LeftButton == ButtonState.Pressed)
if (selectionRectangle == Rectangle.Empty) {
{
selectionRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 0, 0); if (selectionRectangle == Rectangle.Empty)
} {
else directionStart = mouseWorldPos;
{ selectionRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 0, 0);
selectionRectangle.Width = (int)mouseWorldPos.X - selectionRectangle.X; readjust = false;
selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y; }
} else
} {
// When the mouse is released if (mouseWorldPos.X > directionStart.X && mouseWorldPos.Y < directionStart.Y || mouseWorldPos.X < directionStart.X && mouseWorldPos.Y > directionStart.Y)
if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty) {
{ int rectangleX = (int)mouseWorldPos.X - ((int)mouseWorldPos.X - (int)directionStart.X);
// Deselect all units int rectangleY = (int)mouseWorldPos.Y;
if (!ks.IsKeyDown(Keys.LeftControl)) int rectangleWidth = (int)mouseWorldPos.X - (int)directionStart.X;
{ int rectangleHeight = (int)directionStart.Y - (int)mouseWorldPos.Y;
foreach (var unit in core.Squad) selectionRectangle = new Rectangle(rectangleX, rectangleY, rectangleWidth, rectangleHeight);
unit.Selected = false; readjust = true;
} }
else
// Select all units in the rectangle {
foreach (var unit in core.Squad) if (readjust == true)
{ {
if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle)) selectionRectangle = new Rectangle((int)directionStart.X, (int)directionStart.Y, 0, 0);
unit.Selected = true; }
} selectionRectangle.Width = (int)mouseWorldPos.X - selectionRectangle.X;
selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y;
// Hide the selection rectangle readjust = false;
selectionRectangle = Rectangle.Empty; }
} }
}
// Send orders with right click // When the mouse is released
if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed) if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty)
{ {
foreach (var unit in core.Squad) // Deselect all units
if (unit.Selected) if (!ks.IsKeyDown(Keys.LeftControl))
{ {
unit.GetComponent<Component.PathFinder>().Goal = Grid.WorldToGrid(mouseWorldPos); foreach (var unit in core.Squad)
} unit.Selected = false;
}
}
if (ms.RightButton == ButtonState.Pressed) // Select all units in the rectangle
{ foreach (var unit in core.Squad)
foreach (var unit in core.Squad) {
{ if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
if (unit.Selected && gridRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle)) unit.Selected = true;
{ }
checkingDirection = true;
directionStart = unit.Transform.World + unit.Transform.World.Origin; // Hide the selection rectangle
} selectionRectangle = Rectangle.Empty;
} }
}
if (checkingDirection == true && ms.RightButton == ButtonState.Pressed) // Send orders with right click
{ if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
foreach (var unit in core.Squad) {
if (unit.Selected) foreach (var unit in core.Squad)
{ if (unit.Selected)
directionNow.X = mouseWorldPos.X; {
directionNow.Y = mouseWorldPos.Y; unit.GetComponent<Component.PathFinder>().Goal = Grid.WorldToGrid(mouseWorldPos);
float directionX = mouseWorldPos.X - unit.Transform.World.X; }
float directionY = mouseWorldPos.Y - unit.Transform.World.Y;
var mouseDirection = mouseWorldPos; }
mouseDirection.Normalize(); if (ms.RightButton == ButtonState.Pressed)
//float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi; {
/*if (directionX < 0) foreach (var unit in core.Squad)
{ {
directionX *= -1; if (unit.Selected && gridRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan(directionY / directionX); {
} checkingDirection = true;
else directionStart = unit.Transform.World + unit.Transform.World.Origin;
{ }
*/unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan2(mouseWorldPos.Y - unit.Transform.World.Y, mouseWorldPos.X - unit.Transform.World.X);/* }
}*/ }
//Console.WriteLine(angle); if (checkingDirection == true && ms.RightButton == ButtonState.Pressed)
//unit.GetComponent<Component.Transform>().World.Rotation = angle; {
} foreach (var unit in core.Squad)
} if (unit.Selected)
else {
{ directionNow.X = mouseWorldPos.X;
checkingDirection = false; directionNow.Y = mouseWorldPos.Y;
} float directionX = mouseWorldPos.X - unit.Transform.World.X;
float directionY = mouseWorldPos.Y - unit.Transform.World.Y;
// Grid highlighting var mouseDirection = mouseWorldPos;
Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos); mouseDirection.Normalize();
Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos); //float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi;
gridRectangle = new Rectangle((int)rectWorldPos.X, (int)rectWorldPos.Y, Grid.TileSize, Grid.TileSize); /*if (directionX < 0)
{
lastMouseState = ms; directionX *= -1;
} unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan(directionY / directionX);
}
public void Draw(SpriteBatch spriteBatch) else
{ {
spriteBatch.Draw(selectionTexture, selectionRectangle, Color.Red * 0.5f); */unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan2(mouseWorldPos.Y - unit.Transform.World.Y, mouseWorldPos.X - unit.Transform.World.X);/*
spriteBatch.Draw(gridTexture, gridRectangle, Color.Yellow * 0.3f); }*/
//Console.WriteLine(angle);
if (checkingDirection == true) //unit.GetComponent<Component.Transform>().World.Rotation = angle;
{ }
Texture2D blank = new Texture2D(core.GraphicsDevice, 1, 1, false, SurfaceFormat.Color); }
blank.SetData(new[] { Color.White }); else
DrawLine(spriteBatch, blank, 2, Color.White, directionStart, directionNow); {
} checkingDirection = false;
}
}
// Grid highlighting
void DrawLine(SpriteBatch batch, Texture2D blank, float width, Color color, Vector2 point1, Vector2 point2) Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos);
{ Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos);
float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X); gridRectangle = new Rectangle((int)rectWorldPos.X, (int)rectWorldPos.Y, Grid.TileSize, Grid.TileSize);
float length = Vector2.Distance(point1, point2);
lastMouseState = ms;
batch.Draw(blank, point1, null, color, }
angle, Vector2.Zero, new Vector2(length, width),
SpriteEffects.None, 0); public void Draw(SpriteBatch spriteBatch)
} {
} spriteBatch.Draw(selectionTexture, selectionRectangle, Color.Red * 0.5f);
} spriteBatch.Draw(gridTexture, gridRectangle, Color.Yellow * 0.3f);
if (checkingDirection == true)
{
Texture2D blank = new Texture2D(core.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
blank.SetData(new[] { Color.White });
DrawLine(spriteBatch, blank, 2, Color.White, directionStart, directionNow);
}
}
void DrawLine(SpriteBatch batch, Texture2D blank, float width, Color color, Vector2 point1, Vector2 point2)
{
float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
float length = Vector2.Distance(point1, point2);
batch.Draw(blank, point1, null, color,
angle, Vector2.Zero, new Vector2(length, width),
SpriteEffects.None, 0);
}
}
}