-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCAniRichText.cpp
164 lines (122 loc) · 3.96 KB
/
CAniRichText.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
// CAniRichText.cpp
//
// CAniRichText 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_TEXT = 4;
#define PROP_TEXT CONSTLIT("text")
const int INDEX_TEXT_ALIGN_HORZ = 5;
#define PROP_TEXT_ALIGN_HORZ CONSTLIT("textAlignHorz")
const int INDEX_TEXT_ALIGN_VERT = 6;
#define PROP_TEXT_ALIGN_VERT CONSTLIT("textAlignVert")
const int INDEX_COLOR = 7;
#define PROP_COLOR CONSTLIT("color")
const int INDEX_FONT = 8;
#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")
CAniRichText::CAniRichText (const IFontTable &FontTable) :
m_FontTable(FontTable),
m_bInvalid(true)
// CAniRichText 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.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.SetColor(PROP_COLOR, 0xffff);
m_Properties.SetFont(PROP_FONT, NULL);
}
void CAniRichText::Format (int cxWidth, int cyHeight)
// Format
//
// Format the text
{
if (m_bInvalid)
{
SBlockFormatDesc BlockFormat;
BlockFormat.cxWidth = cxWidth;
BlockFormat.cyHeight = cyHeight;
CString sAlignHorz = m_Properties[INDEX_TEXT_ALIGN_HORZ].GetString();
if (strEquals(sAlignHorz, ALIGN_CENTER))
BlockFormat.iHorzAlign = alignCenter;
else if (strEquals(sAlignHorz, ALIGN_RIGHT))
BlockFormat.iHorzAlign = alignRight;
else
BlockFormat.iHorzAlign = alignLeft;
CString sAlignVert = m_Properties[INDEX_TEXT_ALIGN_VERT].GetString();
if (strEquals(sAlignVert, ALIGN_CENTER))
BlockFormat.iVertAlign = alignMiddle;
else if (strEquals(sAlignVert, ALIGN_BOTTOM))
BlockFormat.iVertAlign = alignBottom;
else
BlockFormat.iVertAlign = alignTop;
BlockFormat.DefaultFormat.rgbColor = m_Properties[INDEX_COLOR].GetColor();
BlockFormat.DefaultFormat.pFont = m_Properties[INDEX_FONT].GetFont();
m_Text.InitFromRTF(m_Properties[INDEX_TEXT].GetString(), m_FontTable, BlockFormat);
m_bInvalid = false;
}
}
void CAniRichText::GetSpacingRect (RECT *retrcRect)
// GetSpacingRect
//
// Returns the spacing rect
{
CVector vSize = m_Properties[INDEX_SCALE].GetVector();
Format((int)vSize.GetX(), (int)vSize.GetY());
m_Text.GetBounds(retrcRect);
}
void CAniRichText::OnPropertyChanged (const CString &sName)
// OnPropertyChanged
//
// Property has changed
{
if (strEquals(sName, PROP_TEXT)
|| strEquals(sName, PROP_SCALE)
|| strEquals(sName, PROP_TEXT_ALIGN_HORZ)
|| strEquals(sName, PROP_TEXT_ALIGN_VERT))
m_bInvalid = true;
}
void CAniRichText::Paint (SAniPaintCtx &Ctx)
// Paint
//
// Paints
{
int i;
// Get some metrics
CVector vPos = Ctx.ToDest.Transform(m_Properties[INDEX_POSITION].GetVector());
int x = (int)vPos.GetX();
int y = (int)vPos.GetY();
CVector vSize = m_Properties[INDEX_SCALE].GetVector();
// Make sure we are formatted
Format((int)vSize.GetX(), (int)vSize.GetY());
// Paint
for (i = 0; i < m_Text.GetFormattedSpanCount(); i++)
{
const SFormattedTextSpan &Span = m_Text.GetFormattedSpan(i);
DWORD dwOpacity = Span.Format.dwOpacity * Ctx.dwOpacityToDest / 255;
Span.Format.pFont->DrawText(Ctx.Dest,
x + Span.x,
y + Span.y,
CG32bitPixel(Span.Format.rgbColor, (BYTE)dwOpacity),
Span.sText);
}
}