Created
November 11, 2013 23:01
-
-
Save nanexcool/7422176 to your computer and use it in GitHub Desktop.
Xna component that draws a window on top of your game, printing an object's properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Graphics; | |
using Microsoft.Xna.Framework.Input; | |
namespace RogueLike.Engine | |
{ | |
class DebugWindow : DrawableGameComponent | |
{ | |
SpriteFont font; | |
SpriteBatch spriteBatch; | |
MouseState oldMouseState; | |
MouseState mouseState; | |
bool moving = false; | |
public string Title { get; set; } | |
public bool ShowTitle { get; set; } | |
public StringBuilder Lines { get; set; } | |
public Dictionary<Object, string> Stuff { get; set; } | |
public List<Object> List { get; set; } | |
public Vector2 Position { get; set; } | |
public Rectangle DrawRectangle | |
{ | |
get { return new Rectangle((int)Position.X, (int)Position.Y, (int)font.MeasureString(Lines).X, (int)font.MeasureString(Lines).Y); } | |
} | |
public Color TextColor { get; set; } | |
public Color WindowColor { get; set; } | |
public float Alpha { get; set; } | |
public bool SnapToCorners { get; set; } | |
public int SnapLimit { get; set; } | |
public DebugWindow(Game game) | |
: base(game) | |
{ | |
game.Components.Add(this); | |
spriteBatch = new SpriteBatch(game.GraphicsDevice); | |
font = game.Content.Load<SpriteFont>("DebugFont"); | |
Title = "Text Window"; | |
ShowTitle = true; | |
Lines = new StringBuilder(); | |
Stuff = new Dictionary<Object, string>(); | |
List = new List<object>(); | |
if (ShowTitle) | |
{ | |
Lines.AppendLine(Title); | |
} | |
Position = new Vector2(50, 50); | |
TextColor = Color.White; | |
WindowColor = Color.Black; | |
Alpha = 0.8f; | |
SnapToCorners = true; | |
SnapLimit = 5; | |
oldMouseState = mouseState = Mouse.GetState(); | |
} | |
public override void Update(GameTime gameTime) | |
{ | |
oldMouseState = mouseState; | |
mouseState = Mouse.GetState(); | |
Lines.Clear(); | |
if (ShowTitle) | |
{ | |
Lines.AppendLine(Title); | |
} | |
foreach (KeyValuePair<Object, string> s in Stuff) | |
{ | |
string data = ""; | |
if (s.Value == null) | |
{ | |
data = s.Key.ToString(); | |
} | |
else | |
{ | |
data = s.Key.GetType().GetProperty(s.Value).GetValue(s.Key, null).ToString(); | |
} | |
Lines.AppendLine(data); | |
} | |
foreach (object item in List) | |
{ | |
Lines.AppendLine(item.ToString()); | |
} | |
Test(); | |
base.Update(gameTime); | |
} | |
public override void Draw(GameTime gameTime) | |
{ | |
spriteBatch.Begin(); | |
spriteBatch.Draw(Util.BlankTexture, DrawRectangle, WindowColor * Alpha); | |
spriteBatch.DrawString(font, Lines, Position, TextColor); | |
spriteBatch.End(); | |
base.Draw(gameTime); | |
} | |
public void Test() | |
{ | |
if (mouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released && DrawRectangle.Contains(new Point(mouseState.X, mouseState.Y))) | |
{ | |
moving = true; | |
WindowColor = Color.Red; | |
} | |
if (moving && mouseState.LeftButton == ButtonState.Released) | |
{ | |
moving = false; | |
WindowColor = Color.Black; | |
} | |
if (moving) | |
{ | |
Position += Vector2.Subtract(new Vector2(mouseState.X, mouseState.Y), new Vector2(oldMouseState.X, oldMouseState.Y)); | |
} | |
if (SnapToCorners) | |
{ | |
if (Position.X > -SnapLimit && Position.X < SnapLimit) | |
{ | |
Position = new Vector2(0, Position.Y); | |
} | |
if (Position.Y > -SnapLimit && Position.Y < SnapLimit) | |
{ | |
Position = new Vector2(Position.X, 0); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment