-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCAniText.cpp
240 lines (187 loc) · 5.67 KB
/
CAniText.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// CAniText.cpp
//
// CAniText class
#include <windows.h>
#include "Alchemy.h"
#include "DirectXUtil.h"
#include <math.h>
#include <stdio.h>
#include "Reanimator.h"
const int INDEX_VISIBLE = 0;
#define PROP_VISIBLE CONSTLIT("visible")
const int INDEX_POSITION = 1;
#define PROP_POSITION CONSTLIT("position")
const int INDEX_SCALE = 2;
#define PROP_SCALE CONSTLIT("scale")
const int INDEX_ROTATION = 3;
#define PROP_ROTATION CONSTLIT("rotation")
const int INDEX_COLOR = 4;
#define PROP_COLOR CONSTLIT("color")
const int INDEX_OPACITY = 5;
#define PROP_OPACITY CONSTLIT("opacity")
const int INDEX_TEXT = 6;
#define PROP_TEXT CONSTLIT("text")
const int INDEX_TEXT_ALIGN_HORZ = 7;
#define PROP_TEXT_ALIGN_HORZ CONSTLIT("textAlignHorz")
const int INDEX_TEXT_ALIGN_VERT = 8;
#define PROP_TEXT_ALIGN_VERT CONSTLIT("textAlignVert")
const int INDEX_FONT = 9;
#define PROP_FONT CONSTLIT("font")
#define ALIGN_BOTTOM CONSTLIT("bottom")
#define ALIGN_CENTER CONSTLIT("center")
#define ALIGN_LEFT CONSTLIT("left")
#define ALIGN_RIGHT CONSTLIT("right")
#define ALIGN_TOP CONSTLIT("top")
CAniText::CAniText (void) :
m_dwFontFlags(0)
// CAniText constructor
{
m_Properties.SetInteger(PROP_VISIBLE, 1);
m_Properties.SetVector(PROP_POSITION, CVector());
m_Properties.SetVector(PROP_SCALE, CVector(0.0, 0.0));
m_Properties.SetInteger(PROP_ROTATION, 0);
m_Properties.SetColor(PROP_COLOR, 0xffff);
m_Properties.SetOpacity(PROP_OPACITY, 255);
m_Properties.SetString(PROP_TEXT, NULL_STR);
m_Properties.SetString(PROP_TEXT_ALIGN_HORZ, ALIGN_LEFT);
m_Properties.SetString(PROP_TEXT_ALIGN_VERT, ALIGN_TOP);
m_Properties.SetFont(PROP_FONT, NULL);
}
void CAniText::Create (const CString &sText,
const CVector &vPos,
const CG16bitFont *pFont,
DWORD dwFontFlags,
CG32bitPixel rgbColor,
IAnimatron **retpAni)
// Create
//
// Creates text with basic attributes
{
CAniText *pText = new CAniText;
pText->SetPropertyString(PROP_TEXT, sText);
pText->SetPropertyVector(PROP_POSITION, vPos);
pText->SetPropertyFont(PROP_FONT, pFont);
pText->SetFontFlags(dwFontFlags);
pText->SetPropertyColor(PROP_COLOR, rgbColor);
if (dwFontFlags & CG16bitFont::AlignCenter)
pText->SetPropertyString(PROP_TEXT_ALIGN_HORZ, ALIGN_CENTER);
else if (dwFontFlags & CG16bitFont::AlignRight)
pText->SetPropertyString(PROP_TEXT_ALIGN_HORZ, ALIGN_RIGHT);
*retpAni = pText;
}
void CAniText::GetSpacingRect (RECT *retrcRect)
// GetSpacingRect
//
// Returns the spacing rect
{
const CG16bitFont *pFont = m_Properties[INDEX_FONT].GetFont();
if (pFont == NULL)
return IAnimatron::GetSpacingRect(retrcRect);
CVector vSize = m_Properties[INDEX_SCALE].GetVector();
int cxWidth = (int)vSize.GetX();
// Wrap?
if (cxWidth > 0)
{
int iLines = pFont->BreakText(m_Properties[INDEX_TEXT].GetString(), cxWidth, NULL, 0);
retrcRect->left = 0;
retrcRect->top = 0;
if (iLines > 1)
{
retrcRect->right = cxWidth;
retrcRect->bottom = iLines * pFont->GetHeight();
}
else
retrcRect->right = pFont->MeasureText(m_Properties[INDEX_TEXT].GetString(), (int *)&retrcRect->bottom);
}
else
{
retrcRect->left = 0;
retrcRect->top = 0;
retrcRect->right = pFont->MeasureText(m_Properties[INDEX_TEXT].GetString(), (int *)&retrcRect->bottom);
}
}
void CAniText::Paint (SAniPaintCtx &Ctx)
// Paint
//
// Paints
{
int i;
const CG16bitFont *pFont = m_Properties[INDEX_FONT].GetFont();
if (pFont == NULL)
pFont = &CReanimator::GetDefaultFont();
// Get the rect size
CVector vPos = Ctx.ToDest.Transform(m_Properties[INDEX_POSITION].GetVector());
CVector vSize = m_Properties[INDEX_SCALE].GetVector();
RECT rcRect;
rcRect.left = (int)vPos.GetX();
rcRect.top = (int)vPos.GetY();
rcRect.right = rcRect.left + (int)vSize.GetX();
rcRect.bottom = rcRect.top + (int)vSize.GetY();
bool bWrap = RectWidth(rcRect) > 0;
bool bVertAlign = RectHeight(rcRect) > 0;
// Get color & opacity
CG32bitPixel rgbColor = m_Properties[INDEX_COLOR].GetColor();
DWORD dwOpacity = m_Properties[INDEX_OPACITY].GetOpacity() * Ctx.dwOpacityToDest / 255;
// Get the font flags
DWORD dwFlags = m_dwFontFlags;
CString sAlignHorz = m_Properties[INDEX_TEXT_ALIGN_HORZ].GetString();
if (strEquals(sAlignHorz, ALIGN_CENTER))
dwFlags |= CG16bitFont::AlignCenter;
else if (strEquals(sAlignHorz, ALIGN_RIGHT))
dwFlags |= CG16bitFont::AlignRight;
// See if we need to wrap the text
int x;
TArray<CString> Lines;
if (bWrap)
{
pFont->BreakText(m_Properties[INDEX_TEXT].GetString(), RectWidth(rcRect), &Lines, dwFlags);
if (dwFlags & CG16bitFont::AlignCenter)
x = rcRect.left + RectWidth(rcRect) / 2;
else if (dwFlags & CG16bitFont::AlignRight)
x = rcRect.right;
else
x = rcRect.left;
}
else
{
x = rcRect.left;
Lines.Insert(m_Properties[INDEX_TEXT].GetString());
}
// Paint
if (bVertAlign)
{
CString sAlignVert = m_Properties[INDEX_TEXT_ALIGN_VERT].GetString();
int cyHeight = pFont->GetHeight() * Lines.GetCount();
// Compute the x and y
int y = rcRect.top;
if (strEquals(sAlignVert, ALIGN_CENTER))
y += (RectHeight(rcRect) - cyHeight) / 2;
else if (strEquals(sAlignVert, ALIGN_BOTTOM))
y += RectHeight(rcRect) - cyHeight;
// Paint
for (i = 0; i < Lines.GetCount(); i++)
{
pFont->DrawText(Ctx.Dest,
x,
y,
CG32bitPixel(rgbColor, (BYTE)dwOpacity),
Lines[i],
dwFlags);
y += pFont->GetHeight();
}
}
else
{
int y = rcRect.top;
for (i = 0; i < Lines.GetCount(); i++)
{
pFont->DrawText(Ctx.Dest,
x,
y,
CG32bitPixel(rgbColor, (BYTE)dwOpacity),
Lines[i],
dwFlags);
y += pFont->GetHeight();
}
}
}