/
FormattedLine.cs
186 lines (166 loc) · 5.45 KB
/
FormattedLine.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Happiness
{
public class FormattedLine
{
public class PieceInfo
{
public string m_Str;
public Texture2D m_Icon;
public PieceInfo(string str)
{
m_Str = str;
m_Icon = null;
}
public PieceInfo(Texture2D icon)
{
m_Str = null;
m_Icon = icon;
}
}
List<PieceInfo> m_Pieces;
public FormattedLine()
{
m_Pieces = new List<PieceInfo>();
}
FormattedLine(FormattedLine other)
{
List<PieceInfo> pieces = new List<PieceInfo>();
foreach (PieceInfo p in other.m_Pieces)
{
if (p.m_Str != null)
pieces.Add(new PieceInfo(p.m_Str));
else
pieces.Add(new PieceInfo(p.m_Icon));
}
m_Pieces = pieces;
}
public FormattedLine(string line)
{
List<PieceInfo> pieces = new List<PieceInfo>();
string[] icons = line.Split('{');
foreach (string iconStr in icons)
{
int idx = iconStr.IndexOf('}');
if (idx >= 0)
{
string iconInfo = iconStr.Substring(0, idx++);
string remaining = iconStr.Substring(idx, iconStr.Length - idx);
string[] iconInfoPieces = iconInfo.Split(':');
if (iconInfoPieces[0] == "icon")
{
pieces.Add(new PieceInfo(Assets.IconMap[iconInfoPieces[1]]));
}
if (remaining.Length > 0)
pieces.Add(new PieceInfo(remaining));
}
else
{
pieces.Add(new PieceInfo(iconStr));
}
}
m_Pieces = pieces;
}
public void Draw(SpriteBatch sb, SpriteFont font, Vector2 position, Color color)
{
Vector2 fontSize = font.MeasureString("qQ");
int iconSize = (int)fontSize.Y;
float fX = position.X;
float fY = position.Y;
foreach (PieceInfo piece in m_Pieces)
{
if (piece.m_Str != null)
{
Happiness.ShadowString(sb, font, piece.m_Str, new Vector2(fX, fY), color);
fX += font.MeasureString(piece.m_Str).X;
}
if (piece.m_Icon != null)
{
sb.Draw(piece.m_Icon, new Rectangle((int)fX, (int)fY, iconSize, iconSize), Color.White);
fX += iconSize;
}
}
}
public Vector2 Size(SpriteFont font)
{
Vector2 fontSize = font.MeasureString("qQ");
float width = 0;
foreach (PieceInfo piece in m_Pieces)
{
if (piece.m_Str != null)
{
width += font.MeasureString(piece.m_Str).X;
}
if (piece.m_Icon != null)
{
width += fontSize.Y;
}
}
return new Vector2(width, fontSize.Y);
}
public PieceInfo[] Split()
{
List<PieceInfo> words = new List<PieceInfo>();
foreach (PieceInfo piece in m_Pieces)
{
if (piece.m_Str != null)
{
string[] splitWords = piece.m_Str.Split(' ');
foreach (string word in splitWords)
words.Add(new PieceInfo(word));
}
if (piece.m_Icon != null)
words.Add(piece);
}
return words.ToArray();
}
public void Add(PieceInfo piece)
{
if( m_Pieces.Count > 0 )
m_Pieces.Add(new PieceInfo(" "));
m_Pieces.Add(piece);
}
public void Merge()
{
List<PieceInfo> merged = new List<PieceInfo>();
PieceInfo pending = null;
foreach (PieceInfo piece in m_Pieces)
{
if (piece.m_Str != null)
{
if (pending != null)
{
pending.m_Str += piece.m_Str;
}
else
{
pending = piece;
}
}
else
{
if (pending != null)
{
merged.Add(pending);
pending = null;
}
merged.Add(piece);
}
}
if( pending != null )
merged.Add(pending);
m_Pieces = merged;
}
public static FormattedLine operator +(FormattedLine a, PieceInfo piece)
{
FormattedLine nl = new FormattedLine(a);
nl.Add(piece);
return nl;
}
}
}