-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCAniTextInput.cpp
464 lines (341 loc) · 11.6 KB
/
CAniTextInput.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// CAniTextInput.cpp
//
// CAniTextInput 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_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_FOCUS_BORDER_COLOR = 7;
#define PROP_FOCUS_BORDER_COLOR CONSTLIT("focusBorderColor")
const int INDEX_ENABLED = 8;
#define PROP_ENABLED CONSTLIT("enabled")
const int INDEX_FONT = 9;
#define PROP_FONT CONSTLIT("font")
const int INDEX_LABEL_FONT = 10;
#define PROP_LABEL_FONT CONSTLIT("labelFont")
const int INDEX_LABEL_TEXT = 11;
#define PROP_LABEL_TEXT CONSTLIT("labelText")
const int INDEX_LABEL_COLOR = 12;
#define PROP_LABEL_COLOR CONSTLIT("labelColor")
#define STYLE_FRAME CONSTLIT("frame")
#define STYLE_FRAME_FOCUS CONSTLIT("frameFocus")
#define STYLE_FRAME_DISABLED CONSTLIT("frameDisabled")
const int PADDING_BOTTOM = 2;
const int PADDING_LEFT = 4;
const int PADDING_TOP = 2;
const int CURSOR_BLINK_RATE = 30;
const int CURSOR_WIDTH = 2;
const int FOCUS_BRACKET_THICKNESS = 2;
const int FOCUS_BRACKET_WIDTH = 12;
const int LABEL_PADDING = 2;
CAniTextInput::CAniTextInput (void) :
m_bFocus(false),
m_bPassword(false)
// CAniTextInput 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.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.SetColor(PROP_FOCUS_BORDER_COLOR, 0xffff);
m_Properties.SetBool(PROP_ENABLED, true);
m_Properties.SetFont(PROP_FONT, NULL);
m_Properties.SetFont(PROP_LABEL_FONT, NULL);
m_Properties.SetString(PROP_LABEL_TEXT, NULL_STR);
m_Properties.SetColor(PROP_LABEL_COLOR, 0xffff);
IAnimatron *pStyle = new CAniRect;
pStyle->SetPropertyColor(PROP_COLOR, CG32bitPixel(64, 64, 64));
pStyle->SetPropertyOpacity(PROP_OPACITY, 255);
SetStyle(styleFrame, pStyle);
pStyle = new CAniRect;
pStyle->SetPropertyColor(PROP_COLOR, CG32bitPixel(128, 128, 128));
pStyle->SetPropertyOpacity(PROP_OPACITY, 255);
SetStyle(styleFrameFocus, pStyle);
pStyle = new CAniRect;
pStyle->SetPropertyColor(PROP_COLOR, CG32bitPixel(64, 64, 64));
pStyle->SetPropertyOpacity(PROP_OPACITY, 128);
SetStyle(styleFrameDisabled, pStyle);
}
CAniTextInput::~CAniTextInput (void)
// CAniButton destructor
{
}
void CAniTextInput::Create (const RECT &rcRect,
const CG16bitFont *pFont,
DWORD dwOptions,
IAnimatron **retpAni)
// Create
//
// Creates text with basic attributes
{
// Create
CAniTextInput *pText = new CAniTextInput;
pText->SetPropertyVector(PROP_POSITION, CVector(rcRect.left, rcRect.top));
pText->SetPropertyFont(PROP_FONT, pFont);
// Make sure size is big enough to fit font
if (pFont)
{
int cyMin = pFont->GetHeight() + PADDING_TOP + PADDING_BOTTOM;
if (cyMin > RectHeight(rcRect))
pText->SetPropertyVector(PROP_SCALE, CVector(RectWidth(rcRect), cyMin));
else
pText->SetPropertyVector(PROP_SCALE, CVector(RectWidth(rcRect), RectHeight(rcRect)));
}
// Options
if (dwOptions & OPTION_PASSWORD)
pText->m_bPassword = true;
*retpAni = pText;
}
int CAniTextInput::GetDefaultHeight (const CG16bitFont &Font)
// GetDefaultHeight
//
// Returns the default height of the input field with the given font
{
return Font.GetHeight() + PADDING_TOP + PADDING_BOTTOM;
}
void CAniTextInput::GetFocusElements (TArray<IAnimatron *> *retList)
// GetFocusElements
//
// Adds elements capable of receiving the focus
{
if (!m_Properties[INDEX_ENABLED].GetBool())
return;
retList->Insert(this);
}
void CAniTextInput::GetSpacingRect (RECT *retrcRect)
// GetSpacingRect
//
// Returns the spacing rect
{
CVector vScale = m_Properties[INDEX_SCALE].GetVector();
const CG16bitFont *pFont = m_Properties[INDEX_FONT].GetFont();
const CG16bitFont *pLabelFont = m_Properties[INDEX_LABEL_FONT].GetFont();
retrcRect->left = 0;
retrcRect->top = 0;
retrcRect->right = (int)vScale.GetX();
retrcRect->bottom = (pLabelFont ? pLabelFont->GetHeight() + LABEL_PADDING : 0)
+ PADDING_TOP
+ (pFont ? pFont->GetHeight() : 0)
+ PADDING_BOTTOM;
}
bool CAniTextInput::HandleChar (char chChar, DWORD dwKeyData)
// HandleChar
//
// Handle the character
{
if (!m_Properties[INDEX_ENABLED].GetBool())
return false;
// If this is a backspace, delete a character
else if (chChar == VK_BACK)
{
CString sText = m_Properties[INDEX_TEXT].GetString();
sText = strSubString(sText, 0, sText.GetLength() - 1);
m_Properties.SetString(PROP_TEXT, sText);
return true;
}
// If this is a text character, then add it to the input
else if (strIsAlphaNumeric(&chChar) || chChar == ' ' || strIsASCIISymbol(&chChar))
{
CString sText = m_Properties[INDEX_TEXT].GetString();
sText.Append(CString(&chChar, 1));
m_Properties.SetString(PROP_TEXT, sText);
return true;
}
else
return false;
}
void CAniTextInput::HandleLButtonDblClick (int x, int y, DWORD dwFlags, bool *retbCapture, bool *retbFocus)
// HandleLButtonDblClick
//
// LButton double click
{
*retbCapture = false;
*retbFocus = m_Properties[INDEX_ENABLED].GetBool();
}
void CAniTextInput::HandleLButtonDown (int x, int y, DWORD dwFlags, bool *retbCapture, bool *retbFocus)
// HandleLButtonDown
//
// LButton down
{
*retbCapture = false;
*retbFocus = m_Properties[INDEX_ENABLED].GetBool();
}
bool CAniTextInput::HitTest (const CXForm &ToLocal, SAniHitTestResult &Result)
// HitTest
//
// Returns the element if x,y is in our bounds
{
const CG16bitFont *pFont = m_Properties[INDEX_FONT].GetFont();
const CG16bitFont *pLabelFont = m_Properties[INDEX_LABEL_FONT].GetFont();
// 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();
// Compute the position and size of the field
CVector vField = vPos + CVector(0.0, LABEL_PADDING + (pLabelFont ? pLabelFont->GetHeight() : 0.0));
CVector vField2 = CVector(vPos2.GetX(), vField.GetY() + (pFont ? pFont->GetHeight() : 0) + PADDING_TOP + PADDING_BOTTOM);
// Are we in bounds? If not, we fail
if (!vHit.InBox(vField2, vField))
{
// 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 CAniTextInput::MapStyleName (const CString &sComponent) const
// MapStyleName
//
// Maps from a style name to an index
{
if (strEquals(sComponent, STYLE_FRAME))
return styleFrame;
else if (strEquals(sComponent, STYLE_FRAME_FOCUS))
return styleFrameFocus;
else if (strEquals(sComponent, STYLE_FRAME_DISABLED))
return styleFrameDisabled;
else
return -1;
}
void CAniTextInput::Paint (SAniPaintCtx &Ctx)
// Paint
//
// Paints
{
// Get the font for the input
const CG16bitFont *pFont = m_Properties[INDEX_FONT].GetFont();
if (pFont == NULL)
return;
// Get the font for the label
const CG16bitFont *pLabelFont = m_Properties[INDEX_LABEL_FONT].GetFont();
// Enabled?
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();
CVector vPosTrans = Ctx.ToDest.Transform(vPos);
CVector vPosTrans2 = Ctx.ToDest.Transform(vPos + vSize);
CVector vSizeTrans = vPosTrans2 - vPosTrans;
// The field (including the label starts here)
int x = (int)vPosTrans.GetX();
int y = (int)vPosTrans.GetY();
// Figure out the position of the label
int xLabel = x;
int yLabel = y;
// Figure out the position of the input field frame
int xField = x;
int yField = yLabel + LABEL_PADDING + (pLabelFont ? pLabelFont->GetHeight() : 0);
// Figure out the position of the text within the input field
int xText = xField + PADDING_LEFT;
int yText = yField + PADDING_TOP;
int cxText = 0;
int cyText = pFont->GetHeight();
// Now compute the size of the input field frame
int cxField = (int)vSizeTrans.GetX();
int cyField = cyText + PADDING_TOP + PADDING_BOTTOM;
// Get color & opacity
CG32bitPixel rgbColor = m_Properties[INDEX_COLOR].GetColor();
DWORD dwOpacity = m_Properties[INDEX_OPACITY].GetOpacity() * Ctx.dwOpacityToDest / 255;
if (!bEnabled)
dwOpacity = dwOpacity / 2;
// Paint the label
CString sLabel = m_Properties[INDEX_LABEL_TEXT].GetString();
if (pLabelFont && !sLabel.IsBlank())
{
pLabelFont->DrawText(Ctx.Dest,
xLabel,
yLabel,
CG32bitPixel(m_Properties[INDEX_LABEL_COLOR].GetColor(), (BYTE)dwOpacity),
sLabel,
0);
}
// Paint the background
EStyleParts iStyle;
if (!bEnabled)
iStyle = styleFrameDisabled;
else if (m_bFocus)
iStyle = styleFrameFocus;
else
iStyle = styleFrame;
// Paint the button shape
IAnimatron *pFrameStyle = GetStyle(iStyle);
if (pFrameStyle)
{
pFrameStyle->SetPropertyVector(PROP_POSITION, CVector(vPos.GetX(), vPos.GetY() + yField - y));
pFrameStyle->SetPropertyVector(PROP_SCALE, CVector(cxField, cyField));
pFrameStyle->Paint(Ctx);
}
// Get the text
CString sText;
if (m_bPassword)
sText = strRepeat(CONSTLIT(""), m_Properties[INDEX_TEXT].GetString().GetLength());
else
sText = m_Properties[INDEX_TEXT].GetString();
// Draw
int xPos;
pFont->DrawText(Ctx.Dest,
xText,
yText,
CG32bitPixel(rgbColor, (BYTE)dwOpacity),
sText,
0,
&xPos);
// Draw state if we have the focus
if (m_bFocus && bEnabled)
{
CG32bitPixel rgbFocusColor = m_Properties[INDEX_FOCUS_BORDER_COLOR].GetColor();
Ctx.Dest.Fill(xField, yField, FOCUS_BRACKET_WIDTH, FOCUS_BRACKET_THICKNESS, rgbFocusColor);
Ctx.Dest.Fill(xField, yField, FOCUS_BRACKET_THICKNESS, cyField, rgbFocusColor);
Ctx.Dest.Fill(xField, yField + cyField - FOCUS_BRACKET_THICKNESS, FOCUS_BRACKET_WIDTH, FOCUS_BRACKET_THICKNESS, rgbFocusColor);
Ctx.Dest.Fill(xField + cxField - FOCUS_BRACKET_WIDTH, yField, FOCUS_BRACKET_WIDTH, FOCUS_BRACKET_THICKNESS, rgbFocusColor);
Ctx.Dest.Fill(xField + cxField - FOCUS_BRACKET_THICKNESS, yField, FOCUS_BRACKET_THICKNESS, cyField, rgbFocusColor);
Ctx.Dest.Fill(xField + cxField - FOCUS_BRACKET_WIDTH, yField + cyField - FOCUS_BRACKET_THICKNESS, FOCUS_BRACKET_WIDTH, FOCUS_BRACKET_THICKNESS, rgbFocusColor);
#if 0
DrawRectDotted(Ctx.Dest,
xField,
yField,
cxField,
cyField,
m_Properties[INDEX_FOCUS_BORDER_COLOR].GetColor());
#endif
// Paint cursor
if ((Ctx.iFrame / CURSOR_BLINK_RATE) % 2)
Ctx.Dest.Fill(xPos, yText, CURSOR_WIDTH, cyText, rgbColor);
}
}