-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameMessage.cs
executable file
·124 lines (100 loc) · 3.19 KB
/
GameMessage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace PlanetParade
{
class GameMessage
{
#region Fields
string text;
SpriteFont font;
Vector2 center;
Vector2 position;
Color color;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
/// <param name="text">the text for the message</param>
/// <param name="font">the sprite font for the message</param>
/// <param name="center">the center of the message</param>
/// <param name="color">the color of the message</param>
public GameMessage(string text, SpriteFont font,
Vector2 center, Color color)
{
this.text = text;
this.font = font;
this.center = center;
this.color = color;
// calculate position from text and center
float textWidth = font.MeasureString(text).X;
float textHeight = font.MeasureString(text).Y;
position = new Vector2(center.X - textWidth / 2,
center.Y - textHeight / 2);
}
#endregion
#region Properties
/// <summary>
/// Sets the text for the message
/// </summary>
public string Text
{
get
{
return text;
}
set
{
text = value;
// changing text could change text location
float textWidth = font.MeasureString(text).X;
float textHeight = font.MeasureString(text).Y;
position.X = center.X - textWidth / 2;
position.Y = center.Y - textHeight / 2;
}
}
public Color Color
{
set { color = value; }
}
public Vector2 Position
{
set { position = value; }
}
#endregion
#region Methods
/// <summary>
/// Draws the message
/// </summary>
/// <param name="spriteBatch"></param>
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(font, Text, position, color);
}
public void DrawMultiline(SpriteBatch spriteBatch, int width)
{
spriteBatch.DrawString(font, parseText(Text, width), position, color);
}
private String parseText(String text, int width)
{
String line = String.Empty;
String returnString = String.Empty;
String[] wordArray = text.Split(' ');
foreach (String word in wordArray)
{
if (font.MeasureString(line + word).Length() > width)
{
returnString = returnString + line + '\n';
line = String.Empty;
}
line = line + word + ' ';
}
return returnString + line;
}
#endregion
}
}