-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCAniListBox.cpp
698 lines (505 loc) · 15.8 KB
/
CAniListBox.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// CAniListBox.cpp
//
// CAniListBox class
// Copyright (c) 2012 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_OPACITY = 4;
#define PROP_OPACITY CONSTLIT("opacity")
const int INDEX_VIEWPORT_HEIGHT = 5;
#define PROP_VIEWPORT_HEIGHT CONSTLIT("viewportHeight")
const int INDEX_FADE_EDGE_HEIGHT = 6;
#define PROP_FADE_EDGE_HEIGHT CONSTLIT("fadeEdgeHeight")
const int INDEX_SCROLL_POS = 7;
#define PROP_SCROLL_POS CONSTLIT("scrollPos")
const int INDEX_MAX_SCROLL_POS = 8;
#define PROP_MAX_SCROLL_POS CONSTLIT("maxScrollPos")
const int INDEX_PADDING_BOTTOM = 9;
#define PROP_PADDING_BOTTOM CONSTLIT("paddingBottom")
const int INDEX_ENABLED = 10;
#define PROP_ENABLED CONSTLIT("enabled")
const int INDEX_COUNT = 11;
#define PROP_COUNT CONSTLIT("count")
const int INDEX_SELECTION = 12;
#define PROP_SELECTION CONSTLIT("selection")
#define EVENT_ON_DOUBLE_CLICK CONSTLIT("onDoubleClick")
#define EVENT_ON_SELECTION_CHANGED CONSTLIT("onSelectionChanged")
#define PROP_SELECTION_ID CONSTLIT("selectionID")
#define STYLE_SELECTION CONSTLIT("selection")
#define STYLE_SELECTION_FOCUS CONSTLIT("selectionFocus")
#define ID_SCROLL_ANIMATOR CONSTLIT("idScrollAnimator")
const int MOUSE_SENSITIVITY = 30;
CAniListBox::CAniListBox (void)
// CAniListBox 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.SetOpacity(PROP_OPACITY, 255);
m_Properties.SetMetric(PROP_VIEWPORT_HEIGHT, 100.0);
m_Properties.SetMetric(PROP_FADE_EDGE_HEIGHT, 20.0);
m_Properties.SetMetric(PROP_SCROLL_POS, 0.0);
m_Properties.SetMetric(PROP_MAX_SCROLL_POS, 0.0);
m_Properties.SetMetric(PROP_PADDING_BOTTOM, 0.0);
m_Properties.SetBool(PROP_ENABLED, true);
m_Properties.SetInteger(PROP_COUNT, 0);
m_Properties.SetInteger(PROP_SELECTION, 0);
m_pScroller = new CAniVScroller;
}
CAniListBox::~CAniListBox (void)
// CAniListBox destructor
{
delete m_pScroller;
}
void CAniListBox::AddEntry (const CString &sID, IAnimatron *pEntry)
// AddEntry
//
// Adds an entry to the list
{
SEntry *pEntryDesc = m_Entries.Insert();
pEntryDesc->pAni = pEntry;
pEntry->GetSpacingRect(&pEntryDesc->rcRect);
pEntry->SetID(sID);
m_pScroller->AddLine(pEntry);
}
bool CAniListBox::DeleteElement (const CString &sID)
// DeleteElement
//
// Deletes the element by ID
{
int i, j;
for (i = 0; i < m_Entries.GetCount(); i++)
if (strEquals(m_Entries[i].pAni->GetID(), sID))
{
// Compute the height by taking the difference in y value between
// this entry and the next (we can't just take the height because there
// might be spacing).
IAnimatron *pNext = (i + 1 < m_Entries.GetCount() ? m_Entries[i + 1].pAni : NULL);
int cyHeight = (pNext ? (int)pNext->GetPropertyVector(PROP_POSITION).GetY() - (int)m_Entries[i].pAni->GetPropertyVector(PROP_POSITION).GetY() : 0);
// Move up all the entries below this one
for (j = i + 1; j < m_Entries.GetCount(); j++)
{
CVector vNewPos = m_Entries[j].pAni->GetPropertyVector(PROP_POSITION) + CVector(0, -cyHeight);
m_Entries[j].pAni->SetPropertyVector(PROP_POSITION, vNewPos);
}
// Delete it
m_Entries.Delete(i);
// Update the selection
int iSelection = m_Properties[INDEX_SELECTION].GetInteger();
if (iSelection >= m_Entries.GetCount())
iSelection = m_Entries.GetCount() - 1;
Select(iSelection);
break;
}
return m_pScroller->DeleteElement(sID);
}
bool CAniListBox::FindDynamicPropertyInteger (const CString &sName, int *retiValue) const
// FindDynamicPropertyInteger
//
// Returns a dynamic property
{
if (strEquals(sName, PROP_COUNT))
{
*retiValue = m_Entries.GetCount();
return true;
}
return false;
}
bool CAniListBox::FindDynamicPropertyMetric (const CString &sName, Metric *retrValue) const
// FindDynamicPropertyMetric
//
// Returns a dynamic property
{
if (strEquals(sName, PROP_MAX_SCROLL_POS))
{
*retrValue = m_pScroller->GetPropertyMetric(sName);
return true;
}
return false;
}
bool CAniListBox::FindDynamicPropertyString (const CString &sName, CString *retsValue) const
// FindDynamicPropertyString
//
// Returns a dynamic property
{
if (strEquals(sName, PROP_SELECTION_ID))
{
int iSelection = m_Properties[INDEX_SELECTION].GetInteger();
if (iSelection < m_Entries.GetCount() && iSelection >= 0)
*retsValue = m_Entries[iSelection].pAni->GetID();
else
*retsValue = NULL_STR;
return true;
}
return false;
}
int CAniListBox::FindEntry (const CString &sID) const
// FindEntry
//
// Returns the index of the given entry (or -1).
{
for (int i = 0; i < m_Entries.GetCount(); i++)
{
if (strEquals(m_Entries[i].pAni->GetID(), sID))
return i;
}
// Not found
return -1;
}
void CAniListBox::GetFocusElements (TArray<IAnimatron *> *retList)
// GetFocusElements
//
// Returns the focus element
{
if (!m_Properties[INDEX_ENABLED].GetBool())
return;
retList->Insert(this);
}
int CAniListBox::GetRowAtPos (int x, int y)
// GetRowAtPos
//
// Returns the entry at the given position (or -1 if none).
//
// x,y are absolute coordinates.
{
int i;
// Convert to relative coords
CVector vPos = m_Properties[INDEX_POSITION].GetVector();
x -= (int)vPos.GetX();
y -= (int)vPos.GetY();
// Look for an entry
for (i = 0; i < m_Entries.GetCount(); i++)
{
RECT rcRect;
if (GetRowRect(i, &rcRect)
&& x >= rcRect.left && x < rcRect.right && y >= rcRect.top && y < rcRect.bottom)
return i;
}
return -1;
}
bool CAniListBox::GetRowRect (int iRow, RECT *retrcRect)
// GetRowRect
//
// Returns the rect for the given row. Returns TRUE if the row is inside the
// viewport. The rect is relative to the listbox at the current scroll
// position.
{
CVector vSize = m_Properties[INDEX_SCALE].GetVector();
int cyViewport = (int)m_pScroller->GetPropertyMetric(PROP_VIEWPORT_HEIGHT);
// Figure out where the selection should paint
RECT rcSelection = m_Entries[iRow].rcRect;
int cxSel = RectWidth(rcSelection);
int cySel = RectHeight(rcSelection);
CVector vEntryPos = m_Entries[iRow].pAni->GetPropertyVector(PROP_POSITION);
cxSel = Max(cxSel, (int)vSize.GetX());
int yOffset = (int)m_pScroller->GetPropertyMetric(PROP_SCROLL_POS);
int xPadding = 8;
int yPadding = 8;
rcSelection.left = (int)vEntryPos.GetX() - xPadding;
rcSelection.right = rcSelection.left + cxSel + 2 * xPadding;
rcSelection.top = (int)vEntryPos.GetY() - yOffset - yPadding;
rcSelection.bottom = rcSelection.top + cySel + 2 * yPadding;
if (retrcRect)
*retrcRect = rcSelection;
// Are we in the viewport?
return (rcSelection.bottom > 0 && rcSelection.top < cyViewport);
}
bool CAniListBox::HandleChar (char chChar, DWORD dwKeyData)
// HandleChar
//
// Handle character input
{
if (!m_Properties[INDEX_ENABLED].GetBool())
return false;
int iSelection = m_Properties[INDEX_SELECTION].GetInteger();
int iCount = m_Properties[INDEX_COUNT].GetInteger();
return false;
}
bool CAniListBox::HandleKeyDown (int iVirtKey, DWORD dwKeyData)
// HandleKeyDown
//
// Handle character input
{
if (!m_Properties[INDEX_ENABLED].GetBool())
return false;
int iSelection = m_Properties[INDEX_SELECTION].GetInteger();
// PROP_COUNT is dynamic, so we have to do the full call.
int iCount = GetPropertyInteger(PROP_COUNT);
// Entries per page
int iRowsPerPage = 5;
switch (iVirtKey)
{
case VK_DOWN:
if (iSelection + 1 < iCount)
Select(iSelection + 1);
return true;
case VK_END:
if (iSelection != (iCount - 1))
Select(iCount - 1);
return true;
case VK_HOME:
if (iSelection != 0)
Select(0);
return true;
case VK_NEXT:
{
int iNewSel = Min(iSelection + iRowsPerPage, iCount - 1);
if (iNewSel != iSelection)
Select(iNewSel);
return true;
}
case VK_PRIOR:
{
int iNewSel = Max(iSelection - iRowsPerPage, 0);
if (iNewSel != iSelection)
Select(iNewSel);
return true;
}
case VK_UP:
if (iSelection > 0)
Select(iSelection - 1);
return true;
default:
return false;
}
}
void CAniListBox::HandleLButtonDblClick (int x, int y, DWORD dwFlags, bool *retbCapture, bool *retbFocus)
// HandleLButtonDblClick
//
// LButton double click
{
if (!m_Properties[INDEX_ENABLED].GetBool())
{
*retbCapture = false;
*retbFocus = false;
return;
}
*retbFocus = true;
*retbCapture = true;
int iCurSelection = m_Properties[INDEX_SELECTION].GetInteger();
int iSel = GetRowAtPos(x, y);
if (iSel != -1 && iSel == iCurSelection)
{
// We don't set the focus or capture because raising the event might
// destroy us.
*retbFocus = false;
*retbCapture = false;
// Raise event
RaiseEvent(EVENT_ON_DOUBLE_CLICK);
}
}
void CAniListBox::HandleLButtonDown (int x, int y, DWORD dwFlags, bool *retbCapture, bool *retbFocus)
// HandleLButtonDown
//
// LButton down
{
if (!m_Properties[INDEX_ENABLED].GetBool())
{
*retbCapture = false;
*retbFocus = false;
return;
}
*retbFocus = true;
*retbCapture = true;
// See if we hit one of the rows
int iCurSelection = m_Properties[INDEX_SELECTION].GetInteger();
int iNewSel = GetRowAtPos(x, y);
if (iNewSel != -1 && iNewSel != iCurSelection)
Select(iNewSel);
}
void CAniListBox::HandleLButtonUp (int x, int y, DWORD dwFlags)
// HandleLButtonUp
//
// LButton up
{
}
bool CAniListBox::HandleMouseWheel (int iDelta, int x, int y, DWORD dwFlags)
// HandleMouseWheel
//
// Mouse scrolling
{
if (!m_Properties[INDEX_ENABLED].GetBool())
return false;
int iSelection = m_Properties[INDEX_SELECTION].GetInteger();
// PROP_COUNT is dynamic, so we have to do the full call.
int iCount = GetPropertyInteger(PROP_COUNT);
// Entries per page
int iChange = -Sign(iDelta / MOUSE_SENSITIVITY);
// Scroll
int iNewSel = Max(0, Min(iSelection + iChange, iCount - 1));
if (iNewSel != iSelection)
Select(iNewSel);
return true;
}
bool CAniListBox::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 + CVector(m_Properties[INDEX_SCALE].GetVector().GetX(), m_pScroller->GetPropertyMetric(PROP_VIEWPORT_HEIGHT));
// 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 CAniListBox::MapStyleName (const CString &sComponent) const
// MapStyleName
//
// Maps a style name
{
if (strEquals(sComponent, STYLE_SELECTION))
return styleSelection;
else if (strEquals(sComponent, STYLE_SELECTION_FOCUS))
return styleSelectionFocus;
else
return -1;
}
void CAniListBox::OnPropertyChanged (const CString &sName)
// OnPropertyChanged
//
// Property has changed
{
int iIndex = m_Properties.FindProperty(sName);
if (iIndex == -1)
return;
m_pScroller->SetProperty(sName, m_Properties[iIndex]);
}
void CAniListBox::Paint (SAniPaintCtx &Ctx)
// Paint
//
// Paints the control
{
CVector vPos = m_Properties[INDEX_POSITION].GetVector();
bool bEnabled = m_Properties[INDEX_ENABLED].GetBool();
int cyViewport = (int)m_pScroller->GetPropertyMetric(PROP_VIEWPORT_HEIGHT);
// Paint the contents
int iSelection = m_Properties[INDEX_SELECTION].GetInteger();
if (iSelection >= 0 && iSelection < m_Entries.GetCount())
{
// Figure out where the selection should paint
RECT rcSelection;
GetRowRect(iSelection, &rcSelection);
// Create a transform for the selection
CXForm SelToDest = CXForm(xformTranslate, vPos + CVector((Metric)rcSelection.left, (Metric)rcSelection.top)) * Ctx.ToDest;
// Create a context
DWORD dwOpacity = (bEnabled ? 255 : 128);
SAniPaintCtx SelCtx(Ctx.Dest,
SelToDest,
dwOpacity * Ctx.dwOpacityToDest / 255,
0);
// Paint the selection
IAnimatron *pSelStyle = (m_bFocus ? GetStyle(styleSelectionFocus) : GetStyle(styleSelection));
if (pSelStyle)
{
// Clip to the viewport
RECT rcSavedClip = Ctx.Dest.GetClipRect();
CVector vPosT = Ctx.ToDest.Transform(vPos);
RECT rcClip = rcSavedClip;
rcClip.top = (int)vPosT.GetY();
rcClip.bottom = (int)(vPosT.GetY() + cyViewport);
Ctx.Dest.SetClipRect(rcClip);
// Paint
pSelStyle->SetPropertyVector(PROP_POSITION, CVector());
pSelStyle->SetPropertyVector(PROP_SCALE, CVector(RectWidth(rcSelection), RectHeight(rcSelection)));
pSelStyle->Paint(SelCtx);
// Restore
Ctx.Dest.SetClipRect(rcSavedClip);
}
// Paint all rows
m_pScroller->Paint(Ctx);
}
}
void CAniListBox::Select (int iEntry)
// Select
//
// Selects the given entry
{
SetPropertyInteger(PROP_SELECTION, iEntry);
if (iEntry != -1)
{
// Scroll the viewport so that the selection is centered
int iMaxScrollPos = (int)m_pScroller->GetPropertyMetric(PROP_MAX_SCROLL_POS);
int cyViewport = (int)m_pScroller->GetPropertyMetric(PROP_VIEWPORT_HEIGHT);
int iScrollPos = (int)m_pScroller->GetPropertyMetric(PROP_SCROLL_POS);
// Compute the new scroll position
CVector vEntryPos = m_Entries[iEntry].pAni->GetPropertyVector(PROP_POSITION);
int yEntry = (int)vEntryPos.GetY();
int cyEntry = RectHeight(m_Entries[iEntry].rcRect);
int iNewScrollPos = Max(0, Min(yEntry - ((cyViewport - cyEntry) / 2), iMaxScrollPos));
if (iNewScrollPos != iScrollPos)
{
m_pScroller->RemoveAnimation(ID_SCROLL_ANIMATOR);
m_pScroller->AnimatePropertyLinear(ID_SCROLL_ANIMATOR, PROP_SCROLL_POS, CAniProperty((Metric)iScrollPos), CAniProperty((Metric)iNewScrollPos), 5, true);
}
}
// Raise event
RaiseEvent(EVENT_ON_SELECTION_CHANGED);
}
void CAniListBox::SetSelection (const CString &sID, bool bAnimate)
// SetSelection
//
// Sets the given selection
{
int iEntry = FindEntry(sID);
if (iEntry == -1)
return;
SetPropertyInteger(PROP_SELECTION, iEntry);
// Scroll the viewport so that the selection is centered
int iMaxScrollPos = (int)m_pScroller->GetPropertyMetric(PROP_MAX_SCROLL_POS);
int cyViewport = (int)m_pScroller->GetPropertyMetric(PROP_VIEWPORT_HEIGHT);
int iScrollPos = (int)m_pScroller->GetPropertyMetric(PROP_SCROLL_POS);
// Compute the new scroll position
CVector vEntryPos = m_Entries[iEntry].pAni->GetPropertyVector(PROP_POSITION);
int yEntry = (int)vEntryPos.GetY();
int cyEntry = RectHeight(m_Entries[iEntry].rcRect);
int iNewScrollPos = Max(0, Min(yEntry - ((cyViewport - cyEntry) / 2), iMaxScrollPos));
if (iNewScrollPos != iScrollPos)
{
if (bAnimate)
{
m_pScroller->RemoveAnimation(ID_SCROLL_ANIMATOR);
m_pScroller->AnimatePropertyLinear(ID_SCROLL_ANIMATOR, PROP_SCROLL_POS, CAniProperty((Metric)iScrollPos), CAniProperty((Metric)iNewScrollPos), 5, true);
}
else
{
m_pScroller->RemoveAnimation(ID_SCROLL_ANIMATOR);
m_pScroller->SetPropertyMetric(PROP_SCROLL_POS, (Metric)iNewScrollPos);
}
}
}