-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCAniButton.cpp
405 lines (315 loc) · 9.83 KB
/
CAniButton.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// CAniButton.cpp
//
// CAniButton class
// Copyright (c) 2011 by Kronosaur Productions, LLC. All Rights Reserved.
#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_TEXT = 3;
#define PROP_TEXT CONSTLIT("text")
const int INDEX_ENABLED = 4;
#define PROP_ENABLED CONSTLIT("enabled")
const int INDEX_CHECKED = 5;
#define PROP_CHECKED CONSTLIT("checked")
#define EVENT_ON_CLICK CONSTLIT("onClick")
#define PROP_COLOR CONSTLIT("color")
#define PROP_OPACITY CONSTLIT("opacity")
#define STYLE_DOWN CONSTLIT("down")
#define STYLE_HOVER CONSTLIT("hover")
#define STYLE_NORMAL CONSTLIT("normal")
#define STYLE_TEXT CONSTLIT("text")
#define STYLE_DISABLED CONSTLIT("disabled")
#define STYLE_CHECK CONSTLIT("check")
#define STYLE_IMAGE CONSTLIT("image")
const int CHECKBOX_SPACING_X = 8;
const int LINK_SPACING_X = 4;
CAniButton::CAniButton (EButtonTypes iType) :
m_iType(iType),
m_bDown(false),
m_bHover(false)
// CAniButton constructor
{
m_Properties.SetInteger(PROP_VISIBLE, 1);
m_Properties.SetVector(PROP_POSITION, CVector());
m_Properties.SetVector(PROP_SCALE, CVector(1.0, 1.0));
m_Properties.SetString(PROP_TEXT, NULL_STR);
m_Properties.SetBool(PROP_ENABLED, true);
m_Properties.SetBool(PROP_CHECKED, false);
IAnimatron *pStyle = new CAniRect;
pStyle->SetPropertyColor(PROP_COLOR, CG32bitPixel(255, 255, 255));
pStyle->SetPropertyOpacity(PROP_OPACITY, 255);
SetStyle(styleDown, pStyle);
pStyle = new CAniRect;
pStyle->SetPropertyColor(PROP_COLOR, CG32bitPixel(255, 255, 255));
pStyle->SetPropertyOpacity(PROP_OPACITY, 255);
SetStyle(styleHover, pStyle);
pStyle = new CAniRect;
pStyle->SetPropertyColor(PROP_COLOR, CG32bitPixel(128, 128, 128));
pStyle->SetPropertyOpacity(PROP_OPACITY, 255);
SetStyle(styleNormal, pStyle);
pStyle = new CAniRect;
pStyle->SetPropertyColor(PROP_COLOR, CG32bitPixel(128, 128, 128));
pStyle->SetPropertyOpacity(PROP_OPACITY, 128);
SetStyle(styleDisabled, pStyle);
pStyle = new CAniText;
pStyle->SetPropertyColor(PROP_COLOR, CG32bitPixel(0, 0, 0));
pStyle->SetPropertyString(CONSTLIT("textAlignHorz"), CONSTLIT("center"));
pStyle->SetPropertyString(CONSTLIT("textAlignVert"), CONSTLIT("center"));
SetStyle(styleText, pStyle);
pStyle = new CAniText;
pStyle->SetPropertyColor(PROP_COLOR, CG32bitPixel(0, 0, 0));
pStyle->SetPropertyString(CONSTLIT("textAlignHorz"), CONSTLIT("center"));
pStyle->SetPropertyString(CONSTLIT("textAlignVert"), CONSTLIT("center"));
SetStyle(styleCheck, pStyle);
}
CAniButton::~CAniButton (void)
// CAniButton destructor
{
}
void CAniButton::GetSpacingRect (RECT *retrcRect)
// GetSpacingRect
//
// Returns the size of the element
{
if (m_iType == typeCheckbox)
{
IAnimatron *pStyle = GetStyle(styleNormal);
IAnimatron *pTextStyle = GetStyle(styleText);
if (pStyle && pTextStyle)
{
RECT rcButton;
pStyle->GetSpacingRect(&rcButton);
CVector vSize = m_Properties[INDEX_SCALE].GetVector();
pTextStyle->SetPropertyVector(PROP_SCALE, CVector(vSize.GetX() - (RectWidth(rcButton) + CHECKBOX_SPACING_X), RectHeight(rcButton)));
pTextStyle->SetPropertyString(PROP_TEXT, m_Properties[INDEX_TEXT].GetString());
RECT rcText;
pTextStyle->GetSpacingRect(&rcText);
retrcRect->left = 0;
retrcRect->top = 0;
retrcRect->right = RectWidth(rcButton) + CHECKBOX_SPACING_X + RectWidth(rcText);
retrcRect->bottom = Max(RectHeight(rcButton), RectHeight(rcText));
}
}
else
{
IAnimatron *pStyle = GetStyle(styleNormal);
if (pStyle)
pStyle->GetSpacingRect(retrcRect);
}
}
void CAniButton::HandleLButtonDblClick (int x, int y, DWORD dwFlags, bool *retbCapture, bool *retbFocus)
// HandleLButtonDblClick
//
// LButton double click
{
*retbFocus = false;
m_bDown = m_Properties[INDEX_ENABLED].GetBool();
*retbCapture = m_bDown;
}
void CAniButton::HandleLButtonDown (int x, int y, DWORD dwFlags, bool *retbCapture, bool *retbFocus)
// HandleLButtonDown
//
// LButton down
{
*retbFocus = false;
m_bDown = m_Properties[INDEX_ENABLED].GetBool();
*retbCapture = m_bDown;
}
void CAniButton::HandleLButtonUp (int x, int y, DWORD dwFlags)
// HandleLButtonUp
//
// LButton up
{
bool bRaiseEvent = (m_bDown
&& m_bHover
&& m_Properties[INDEX_ENABLED].GetBool());
m_bDown = false;
// Fire event at the end because it may end up destroying the session
if (bRaiseEvent)
{
if (m_iType == typeCheckbox)
SetPropertyBool(PROP_CHECKED, !m_Properties[INDEX_CHECKED].GetBool());
RaiseEvent(EVENT_ON_CLICK);
}
}
bool CAniButton::HitTest (const CXForm &ToLocal, SAniHitTestResult &Result)
// HitTest
//
// Returns the element if x,y is in our bounds
{
// If someone else has captured the mouse, then we never hit
if (Result.pCaptured && Result.pCaptured != this)
return false;
// Transform to local coordinates
CVector vHit = ToLocal.Transform(CVector(Result.x, Result.y));
// Position and size
CVector vPos = m_Properties[INDEX_POSITION].GetVector();
CVector vPos2 = vPos + m_Properties[INDEX_SCALE].GetVector();
// Are we in bounds? If not, we fail
if (!vHit.InBox(vPos2, vPos))
{
// If we've captured the mouse, we return TRUE anyway and transform
// the coordinates.
if (Result.pCaptured)
{
Result.pHit = NULL;
Result.xLocal = (int)vHit.GetX();
Result.yLocal = (int)vHit.GetY();
return true;
}
else
return false;
}
// Otherwise, we hit
Result.pHit = this;
Result.xLocal = (int)vHit.GetX();
Result.yLocal = (int)vHit.GetY();
return true;
}
int CAniButton::MapStyleName (const CString &sComponent) const
// MapStyleName
//
// Map from a style name to an index.
{
if (strEquals(sComponent, STYLE_DOWN))
return styleDown;
else if (strEquals(sComponent, STYLE_HOVER))
return styleHover;
else if (strEquals(sComponent, STYLE_NORMAL))
return styleNormal;
else if (strEquals(sComponent, STYLE_TEXT))
return styleText;
else if (strEquals(sComponent, STYLE_DISABLED))
return styleDisabled;
else if (strEquals(sComponent, STYLE_CHECK))
return styleCheck;
else if (strEquals(sComponent, STYLE_IMAGE))
return styleImage;
else
return -1;
}
void CAniButton::Paint (SAniPaintCtx &Ctx)
// Paint
//
// Paint the element
{
bool bEnabled = m_Properties[INDEX_ENABLED].GetBool();
// Get position and size
CVector vPos = m_Properties[INDEX_POSITION].GetVector();
CVector vSize = m_Properties[INDEX_SCALE].GetVector();
// Transform
CXForm LocalToDest = CXForm(xformTranslate, vPos) * Ctx.ToDest;
// Create a context
DWORD dwOpacity = (bEnabled ? 255 : 128);
SAniPaintCtx LocalCtx(Ctx.Dest,
LocalToDest,
dwOpacity * Ctx.dwOpacityToDest / 255,
0);
// Figure out how we're going to paint the button shape
EStyleParts iStyle;
if (!bEnabled)
iStyle = styleDisabled;
else if (m_bHover)
iStyle = styleHover;
else if (m_bHover && m_bDown)
iStyle = styleDown;
else
iStyle = styleNormal;
// Paint the button shape
IAnimatron *pFrameStyle = GetStyle(iStyle);
RECT rcContent;
if (pFrameStyle)
{
pFrameStyle->SetPropertyVector(PROP_POSITION, vPos);
// If this is a checkbox then we allow the style to control the size
if (m_iType == typeCheckbox)
{
CVector vCheckbox = pFrameStyle->GetPropertyVector(PROP_SCALE);
rcContent.left = (int)vPos.GetX();
rcContent.top = (int)vPos.GetY();
rcContent.right = rcContent.left + (int)vCheckbox.GetX();
rcContent.bottom = rcContent.top + (int)vCheckbox.GetY();
}
// Otherwise we set the size based on the control size
else
{
pFrameStyle->SetPropertyVector(PROP_SCALE, vSize);
pFrameStyle->GetContentRect(&rcContent);
}
// Paint
pFrameStyle->Paint(Ctx);
}
else
{
rcContent.left = 0;
rcContent.right = 30;
rcContent.top = 0;
rcContent.bottom = 30;
}
// Set the rect for the text
IAnimatron *pTextStyle = GetStyle(styleText);
if (pTextStyle)
{
// Compute the position of the text
int xText;
int yText;
int cxText;
int cyText;
if (m_iType == typeCheckbox)
{
xText = (int)vPos.GetX() + CHECKBOX_SPACING_X + RectWidth(rcContent);
yText = (int)vPos.GetY();
cxText = (int)vSize.GetX() - (CHECKBOX_SPACING_X + RectWidth(rcContent));
cyText = (int)vSize.GetY();
}
else if (m_iType == typeLink)
{
xText = (int)vPos.GetX() + LINK_SPACING_X;
yText = (int)vPos.GetY();
cxText = (int)vSize.GetX();
cyText = (int)vSize.GetY();
}
else
{
xText = (int)vPos.GetX() + rcContent.left;
yText = (int)vPos.GetY() + rcContent.top;
cxText = RectWidth(rcContent);
cyText = RectHeight(rcContent);
}
// Set up the style
pTextStyle->SetPropertyVector(PROP_POSITION, CVector(xText, yText));
pTextStyle->SetPropertyVector(PROP_SCALE, CVector(cxText, cyText));
pTextStyle->SetPropertyOpacity(PROP_OPACITY, dwOpacity);
// Paint the text
pTextStyle->SetPropertyString(PROP_TEXT, m_Properties[INDEX_TEXT].GetString());
pTextStyle->Paint(Ctx);
}
// Paint the image
IAnimatron *pImageStyle = GetStyle(styleImage);
if (pImageStyle)
pImageStyle->Paint(LocalCtx);
// Draw checkmark
if (m_iType == typeCheckbox)
{
bool bChecked = m_Properties[INDEX_CHECKED].GetBool();
if (bChecked)
{
IAnimatron *pCheckStyle = GetStyle(styleCheck);
if (pCheckStyle)
{
pCheckStyle->SetPropertyVector(PROP_POSITION, CVector(rcContent.left, rcContent.top));
pCheckStyle->SetPropertyVector(PROP_SCALE, CVector(RectWidth(rcContent), RectHeight(rcContent)));
pCheckStyle->Paint(Ctx);
}
}
}
}