- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/GraphicalElements/EnergyIndicator.cs

    00001 ﻿using System;
    00002 using System.Collections.Generic;
    00003 using System.Text;
    00004
    00005 using Microsoft.Xna.Framework;
    00006 using Microsoft.Xna.Framework.Graphics;
    00007
    00008 using NewGamePhysics.StateManager;
    00009 using NewGamePhysics.Utilities;
    00010
    00011 namespace NewGamePhysics.GraphicalElements
    00012 {
    00018     public class EnergyIndicator : GraphicalElementBase
    00019     {
    00023         public const float Width = 14.0f;
    00024
    00028         public const float Height = 102.0f;
    00029
    00033         private double value;
    00034
    00038         private Vector2 position;
    00039
    00043         private string label;
    00044
    00048         private Color drawColor;
    00049
    00053         private Texture2D blackTexture;
    00054
    00058         private Texture2D barTexture;
    00059
    00063         private SpriteFont textFont;
    00064
    00070         public EnergyIndicator(
    00071             ScreenManager screenManager,
    00072             string label) : base(screenManager)
    00073         {
    00074             // Store settings
    00075             this.label = label;
    00076
    00077             // Default position and size
    00078             this.position = new Vector2();
    00079
    00080             // Black hide texture
    00081             this.blackTexture = TextureHelpers.Create(screenManager.GraphicsDevice, Color.Black);
    00082
    00083             // Colored bar texture
    00084             this.barTexture = screenManager.Game.Content.Load<Texture2D>(@"Sprites\energybar");
    00085
    00086             // Color for box and font
    00087             this.drawColor = new Color(255, 255, 255);
    00088
    00089             // The font to use
    00090             this.textFont = ScreenManager.Fonts["small"];
    00091
    00092             // Now set a default value, set bar color.
    00093             this.SetValue(0.0);
    00094         }
    00095
    00100         public void SetValue(double value)
    00101         {
    00102             // Set value but limit range
    00103             if (value < 0.0)
    00104             {
    00105                 this.value = 0.0;
    00106             }
    00107             else if (value > 100.0)
    00108             {
    00109                 this.value = 100.0;
    00110             }
    00111             else
    00112             {
    00113                 this.value = value;
    00114             }
    00115         }
    00116
    00121         public void SetPosition(Vector2 position)
    00122         {
    00123             this.position.X = position.X;
    00124             this.position.Y = position.Y;
    00125         }
    00126
    00131         public void Draw(GameTime gameTime)
    00132         {
    00133             Rectangle destination;
    00134
    00135             // Draw bar
    00136             destination = new Rectangle(
    00137                 (int)this.position.X,
    00138                 (int)this.position.Y,
    00139                 (int)Width,
    00140                 (int)Height);
    00141             SpriteBatch.Begin();
    00142             SpriteBatch.Draw(this.barTexture, destination, Color.White);
    00143             SpriteBatch.End();
    00144
    00145             // Hide invisible part of bar
    00146             int hiddenHeight = (int)((100.0 - this.value) * 0.01 * (double)Height);
    00147             destination = new Rectangle(
    00148                 (int)this.position.X,
    00149                 (int)this.position.Y,
    00150                 (int)Width,
    00151                 (int)hiddenHeight);
    00152             SpriteBatch.Begin();
    00153             SpriteBatch.Draw(this.blackTexture, destination, Color.DarkSlateGray);
    00154             SpriteBatch.End();
    00155
    00156             // Lines
    00157             PrimitiveBatch.Begin(PrimitiveType.LineList);
    00158
    00159             // Box
    00160             PrimitiveBatch.AddVertex(this.position, this.drawColor);
    00161             PrimitiveBatch.AddVertex(this.position + new Vector2(Width, 0.0f), this.drawColor);
    00162
    00163             PrimitiveBatch.AddVertex(this.position + new Vector2(Width, 0.0f), this.drawColor);
    00164             PrimitiveBatch.AddVertex(this.position + new Vector2(Width, Height), this.drawColor);
    00165
    00166             PrimitiveBatch.AddVertex(this.position + new Vector2(Width, Height), this.drawColor);
    00167             PrimitiveBatch.AddVertex(this.position + new Vector2(0.0f, Height), this.drawColor);
    00168
    00169             PrimitiveBatch.AddVertex(this.position + new Vector2(0.0f, Height), this.drawColor);
    00170             PrimitiveBatch.AddVertex(this.position, this.drawColor);
    00171
    00172             PrimitiveBatch.End();
    00173
    00174             // Label (sits on top of visible area)
    00175             if (!String.IsNullOrEmpty(this.label))
    00176             {
    00177                 float ySpacing = 2.0f;
    00178                 Vector2 textSize = this.textFont.MeasureString(this.label);
    00179                 int visibleHeight = (int)(this.value * 0.01 * (double)Height);
    00180                 Vector2 textPosition = new Vector2();
    00181                 textPosition.X = this.position.X + (Width - textSize.X) / 2;
    00182                 float delta = ySpacing + visibleHeight + textSize.Y;
    00183                 if (delta > (Height - ySpacing))
    00184                 {
    00185                     textPosition.Y = this.position.Y + ySpacing;
    00186                 }
    00187                 else
    00188                 {
    00189                     textPosition.Y = this.position.Y + Height - delta;
    00190                 }
    00191
    00192                 SpriteBatch.Begin();
    00193                 SpriteBatch.DrawString(
    00194                     this.textFont,
    00195                     this.label,
    00196                     textPosition,
    00197                     this.drawColor, 0, new Vector2(), 1.0f,
    00198                     SpriteEffects.None, 0);
    00199                 SpriteBatch.End();
    00200             }
    00201         }
    00202     }
    00203 }