From d52f1e5ef58ba1e603bdc3881479cb6dcf0bd63c Mon Sep 17 00:00:00 2001 From: Simon Holmberg Date: Mon, 24 Sep 2012 23:52:41 +0200 Subject: [PATCH] Experimental selection code --- DepthsBelow/DepthsBelow/Camera.cs | 2 +- DepthsBelow/DepthsBelow/Core.cs | 9 +++- DepthsBelow/DepthsBelow/DepthsBelow.csproj | 1 + DepthsBelow/DepthsBelow/MouseControl.cs | 63 ++++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 DepthsBelow/DepthsBelow/MouseControl.cs diff --git a/DepthsBelow/DepthsBelow/Camera.cs b/DepthsBelow/DepthsBelow/Camera.cs index 7957269..f2078db 100644 --- a/DepthsBelow/DepthsBelow/Camera.cs +++ b/DepthsBelow/DepthsBelow/Camera.cs @@ -7,7 +7,7 @@ using Microsoft.Xna.Framework.Input; namespace DepthsBelow { - class Camera + public class Camera { public float Zoom; public Matrix Transform; diff --git a/DepthsBelow/DepthsBelow/Core.cs b/DepthsBelow/DepthsBelow/Core.cs index 50d89b6..0b10036 100644 --- a/DepthsBelow/DepthsBelow/Core.cs +++ b/DepthsBelow/DepthsBelow/Core.cs @@ -19,7 +19,8 @@ namespace DepthsBelow GraphicsDeviceManager graphics; SpriteBatch spriteBatch; - Camera camera; + public Camera camera; + MouseControl mouse; Soldier soldier; public Core() @@ -59,6 +60,9 @@ namespace DepthsBelow // TODO: use this.Content to load your game content here Soldier.LoadContent(this); soldier = new Soldier(this); + + mouse = new MouseControl(this); + mouse.LoadContent(); } /// @@ -82,6 +86,7 @@ namespace DepthsBelow this.Exit(); camera.Update(gameTime); + mouse.Update(gameTime); // TODO: Add your update logic here soldier.Update(gameTime); @@ -104,6 +109,8 @@ namespace DepthsBelow if (rc != null) rc.Draw(spriteBatch); + mouse.Draw(spriteBatch); + spriteBatch.End(); base.Draw(gameTime); diff --git a/DepthsBelow/DepthsBelow/DepthsBelow.csproj b/DepthsBelow/DepthsBelow/DepthsBelow.csproj index c4961e0..a540a42 100644 --- a/DepthsBelow/DepthsBelow/DepthsBelow.csproj +++ b/DepthsBelow/DepthsBelow/DepthsBelow.csproj @@ -125,6 +125,7 @@ + diff --git a/DepthsBelow/DepthsBelow/MouseControl.cs b/DepthsBelow/DepthsBelow/MouseControl.cs new file mode 100644 index 0000000..9482663 --- /dev/null +++ b/DepthsBelow/DepthsBelow/MouseControl.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using Microsoft.Xna.Framework.Media; + +namespace DepthsBelow +{ + class MouseControl + { + Core core; + MouseState lastMouseState; + Rectangle selectionRectangle; + Texture2D selectionTexture; + + public MouseControl(Core core) + { + this.core = core; + + selectionRectangle = Rectangle.Empty; + } + + public void LoadContent() + { + selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1); + selectionTexture.SetData(new Color[] { Color.White }); + } + + public void Update(GameTime gameTime) + { + MouseState ms = Mouse.GetState(); + + if (ms.LeftButton == ButtonState.Pressed) + { + if (selectionRectangle == Rectangle.Empty) + { + selectionRectangle = new Rectangle(ms.X + (int)core.camera.Position.X, ms.Y + (int)core.camera.Position.Y, 0, 0); + } + else + { + selectionRectangle.Width = ms.X - selectionRectangle.X + (int)core.camera.Position.X; + selectionRectangle.Height = ms.Y - selectionRectangle.Y + (int)core.camera.Position.Y; + } + } + + if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty) + { + selectionRectangle = Rectangle.Empty; + } + + lastMouseState = ms; + } + + public void Draw(SpriteBatch spriteBatch) + { + spriteBatch.Draw(selectionTexture, selectionRectangle, Color.Red * 0.5f); + } + } +}