00001 using System;
00002 using System.Threading;
00003 using System.Collections.Generic;
00004
00005 using Microsoft.Xna.Framework;
00006 using Microsoft.Xna.Framework.Content;
00007 using Microsoft.Xna.Framework.Graphics;
00008
00009 using NewGamePhysics.StateManager;
00010 using NewGamePhysics.Utilities;
00011
00012 namespace NewGamePhysics.GraphicalElements
00013 {
00017 public enum InfoMessageAlignment
00018 {
00023 Left,
00024
00029 Center,
00030
00035 Right
00036 }
00037
00041 public class InfoMessages : GraphicalElementBase
00042 {
00046 private List<string> messages;
00047
00051 private int numRows;
00052
00056 private int currentRow;
00057
00061 private float fadeSpeed = 0.1f;
00062
00066 private float[] textAlpha;
00067
00071 private Vector2 messagesOrigin;
00072
00076 private SpriteFont font;
00077
00081 private Color color;
00082
00086 private InfoMessageAlignment alignment = InfoMessageAlignment.Left;
00087
00091 private float scale = 1.0f;
00092
00100 public InfoMessages(
00101 ScreenManager manager,
00102 List<string> messages,
00103 Vector2 origin) : base(manager)
00104 {
00105 if (null == messages)
00106 {
00107 throw new ArgumentNullException(
00108 "messages",
00109 "List of messages cannot be null");
00110 }
00111
00112 if (null == origin)
00113 {
00114 throw new ArgumentNullException(
00115 "origin",
00116 "Text origin cannot be null");
00117 }
00118
00119 if (0 == messages.Count)
00120 {
00121 throw new ArgumentOutOfRangeException(
00122 "messages",
00123 "Message List cannot be empty");
00124 }
00125
00126
00127 this.messages = messages;
00128 this.messagesOrigin = origin;
00129
00130
00131 this.numRows = messages.Count;
00132 this.currentRow = 0;
00133 this.textAlpha = new float[this.numRows];
00134 for (int i = 0; i < this.numRows; i++)
00135 {
00136 this.textAlpha[i] = 0.0f;
00137 }
00138
00139
00140 this.font = this.ScreenManager.Fonts["game"];
00141
00142
00143 Color color = Color.White;
00144 }
00145
00149 public SpriteFont Font
00150 {
00151 get { return this.font; }
00152 set { this.font = value; }
00153 }
00154
00158 public Color Color
00159 {
00160 get { return this.color; }
00161 set { this.color = value; }
00162 }
00163
00167 public Vector2 MessagesOrigin
00168 {
00169 get { return this.messagesOrigin; }
00170 set { this.messagesOrigin = value; }
00171 }
00172
00176 public InfoMessageAlignment Alignment
00177 {
00178 get { return this.alignment; }
00179 set { this.alignment = value; }
00180 }
00181
00185 public float Scale
00186 {
00187 get { return this.scale; }
00188 set { this.scale = value; }
00189 }
00190
00195 public void Update(GameTime gameTime)
00196 {
00197
00198 if (this.currentRow < this.numRows)
00199 {
00200 this.textAlpha[this.currentRow] += this.fadeSpeed;
00201 if (string.IsNullOrEmpty(this.messages[this.currentRow]) ||
00202 (this.textAlpha[this.currentRow] >= 1.0f))
00203 {
00204 this.textAlpha[this.currentRow] = 1.0f;
00205 this.currentRow++;
00206 }
00207 }
00208 }
00209
00214 public void Draw(GameTime gameTime)
00215 {
00216 Vector2 lineOrigin = new Vector2(0, 0);
00217 Vector2 linePosition = this.messagesOrigin;
00218 Vector2 textPosition;
00219 this.SpriteBatch.Begin();
00220 for (int i = 0; i < numRows; i++)
00221 {
00222
00223 string message = messages[i];
00224
00225
00226 textPosition = linePosition;
00227 Vector2 textSize = font.MeasureString(message) * this.scale;
00228 switch (this.alignment)
00229 {
00230 case InfoMessageAlignment.Left:
00231
00232 break;
00233 case InfoMessageAlignment.Center:
00234 textPosition.Y -= (font.LineSpacing / 2);
00235 textPosition.X -= (textSize.X / 2);
00236 break;
00237 case InfoMessageAlignment.Right:
00238 textPosition.X -= textSize.X;
00239 break;
00240 }
00241
00242 if (textAlpha[i] > 0.0f)
00243 {
00244 if (string.IsNullOrEmpty(message))
00245 {
00246
00247 linePosition.Y += ((float)font.LineSpacing * 0.5f * this.scale);
00248 }
00249 else
00250 {
00251
00252 color = new Color(
00253 Color.White,
00254 MathHelper.SmoothStep(0.0f, 1.0f, textAlpha[i]));
00255 this.SpriteBatch.DrawString(
00256 font,
00257 message,
00258 textPosition,
00259 color,
00260 0,
00261 lineOrigin,
00262 this.scale,
00263 SpriteEffects.None,
00264 0);
00265 linePosition.Y += ((float)font.LineSpacing * 1.2f * this.scale);
00266 }
00267 }
00268 }
00269 this.SpriteBatch.End();
00270 }
00271 }
00272 }