//---------------------------------------------------------------------------------------------------------------------------------------------------
//
// Copyright (C)2007 DarkWynter Studios. All rights reserved.
//
//---------------------------------------------------------------------------------------------------------------------------------------------------
// {License Information: Creative Commons}
//---------------------------------------------------------------------------------------------------------------------------------------------------
namespace ElementalGame
{
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using RobLoach;
#endregion
// Class which allows us to draw our Heads-up-display
public class HeadsUpDisplay
{
// Attributes:
// Screen dead zone percentage
public static float SIDES_DEAD_ZONE_PERCENT = 0.05f;
public static float TOP_BOTTOM_DEAD_ZONE_PERCENT = 0.03f;
// List of Text Displays
private List textDisplays = new List();
// List of Value Displays
private List valueDisplays = new List();
// List of Image Displays
private List imageDisplays = new List();
// Used to display Text
private class TextDisplay
{
public string text;
public Vector2 position = new Vector2();
public int width;
public int height;
public Color color;
}
// Used to display Values
private class ValueDisplay
{
public string text;
public float value;
public Vector2 position = new Vector2();
public int width;
public int height;
public Color color;
}
// Used to display Images
private class ImageDisplay
{
public Texture2D image;
public Vector2 position = new Vector2();
public int width;
public int height;
public Color color;
public bool draw;
}
// Constructor
public HeadsUpDisplay()
{
}
// Methods:
// Check for edges of the screen if playing on Xbox360
public Vector2 AdjustForDeadZone(Vector2 desiredPosition, int width, int height)
{
#if XBOX360
float minX = ElementalGame.GAME_WINDOW_WIDTH * SIDES_DEAD_ZONE_PERCENT;
float maxX = ElementalGame.GAME_WINDOW_WIDTH - (ElementalGame.GAME_WINDOW_WIDTH * SIDES_DEAD_ZONE_PERCENT);
float minY = ElementalGame.GAME_WINDOW_HEIGHT * TOP_BOTTOM_DEAD_ZONE_PERCENT;
float maxY = ElementalGame.GAME_WINDOW_HEIGHT - (ElementalGame.GAME_WINDOW_HEIGHT * TOP_BOTTOM_DEAD_ZONE_PERCENT);
Vector2 newPosition = Vector2.Zero + desiredPosition;
// First check for the left side dead zone
if (desiredPosition.X < minY)
{
newPosition.X += minY;
}
// else.. check for right side
else if (desiredPosition.X + width > maxX)
{
newPosition.X -= width - (maxX - desiredPosition.X);
}
// Then check upper side
if (desiredPosition.Y < minY)
{
newPosition.Y += minY;
}
else if (desiredPosition.Y + height > maxY)
{
newPosition.Y -= height - (maxY - desiredPosition.Y);
}
return newPosition;
#else
return desiredPosition;
#endif
}
// Add a Text display to our TextDisplay list
public int AddTextDisplay(string text, Vector2 position, Color color)
{
TextDisplay temp = new TextDisplay();
temp.text = text;
temp.width = TextWriter.MeadBold16.TextWidth(text);
temp.height = TextWriter.MeadBold16.LineHeight;
temp.position = position;//AdjustForDeadZone(position, temp.width, temp.height);
temp.color = color;
textDisplays.Add(temp);
return textDisplays.Count - 1;
}
// Add a Value display to our ValueDisplay list
public int AddValueDisplay(string text, float value, Vector2 position, int width, int height, Color color)
{
ValueDisplay temp = new ValueDisplay();
temp.text = text;
temp.value = value;
temp.width = width;
temp.height = height;
temp.position = position;//AdjustForDeadZone(position, temp.width, temp.height);
temp.color = color;
valueDisplays.Add(temp);
return valueDisplays.Count - 1;
}
// Add an Image display to our ImageDisplay list
public int AddImageDisplay(Texture2D image, Vector2 position, int width, int height, Color color, bool draw)
{
ImageDisplay temp = new ImageDisplay();
temp.image = image;
temp.width = width;
temp.height = height;
temp.position = position;//AdjustForDeadZone(position, temp.width, temp.height);
temp.color = color;
temp.draw = draw;
imageDisplays.Add(temp);
return imageDisplays.Count - 1;
}
// Method which allows us to change the text in a TextDisplay
public void UpdateText(int index, string newText)
{
if (index < 0 || index >= textDisplays.Count)
{
// Invalid index
return;
}
else
{
// Change the text
textDisplays[index].text = newText;
}
}
// Method which allows us to change the Value in a ValueDisplay
public void UpdateValue(int index, float newValue)
{
if (index < 0 || index >= valueDisplays.Count)
{
// Invalid index
return;
}
else
{
// Change the value
valueDisplays[index].value = newValue;
}
}
public void UpdateImageHeight(int index, int amount)
{
if (index < 0 || index >= this.imageDisplays.Count)
{
// Invalid index
return;
}
else
{
this.imageDisplays[index].height = amount;
}
}
public void UpdateImageDraw(int index, bool value)
{
if (index < 0 || index >= this.imageDisplays.Count)
{
// Invalid index
return;
}
else
{
this.imageDisplays[index].draw = value;
}
}
// Method which allows us to change the color of a TextDisplay
public void UpdateTextColor(int index, Color color)
{
if (index < 0 || index >= textDisplays.Count)
{
// Invalid index
return;
}
else
{
// Change the value
textDisplays[index].color = color;
}
}
// Default Draw function
public void Draw(SpriteBatch spriteBatch)
{
Draw(spriteBatch, 0, 0);
}
// Draw function for drawing our displays at a certain offset position
public void Draw(SpriteBatch spriteBatch, int xoffset, int yoffset)
{
foreach (TextDisplay element in textDisplays)
{
TextWriter.MeadBold16.Draw(element.position.X + xoffset,
element.position.Y + yoffset,
element.text,
Alignment.Left,
element.color);
}
foreach (ValueDisplay element in valueDisplays)
{
TextWriter.Ogilvie24.Draw(element.position.X + xoffset,
element.position.Y + yoffset,
element.text,
Alignment.Left,
element.color);
int titleWidth = TextWriter.Ogilvie24.TextWidth(element.text);
TextWriter.MeadBold16.Draw(element.position.X + titleWidth + xoffset,
element.position.Y + yoffset,
"" + element.value,
Alignment.Left,
element.color);
}
spriteBatch.Begin();
foreach (ImageDisplay element in imageDisplays)
{
if (element.draw == true)
{
spriteBatch.Draw(element.image,
new Rectangle((int)element.position.X + xoffset,
(int)element.position.Y + yoffset,
element.width,
element.height),
element.color);
}
}
spriteBatch.End();
}
}
}