-
Notifications
You must be signed in to change notification settings - Fork 2
/
AnimateDialog.cpp
387 lines (323 loc) · 10.8 KB
/
AnimateDialog.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
// AnimateDialog.cpp : implementation file
//
#include "stdafx.h"
#include "SCIPicEditor.h"
#include "AnimateDialog.h"
#include "ViewResource.h"
// CAnimateDialog dialog
CAnimateDialog::CAnimateDialog(CWnd* pParent /*=NULL*/)
: CExtResizableDialog(CAnimateDialog::IDD, pParent)
{
_pView = NULL;
_fDoubleBuffer = FALSE;
_pbitmapDoubleBuf = NULL;
_nCel = 0;
_rectDialogSize.SetRectEmpty();
_fInitialized = FALSE;
_iZoom = 1;
_ptOrigin = CPoint(0, 0);
_rectPlacements.SetRectEmpty();
_sizeWeDrawIn.SetSize(0, 0);
_fAnimating = TRUE;
}
CAnimateDialog::~CAnimateDialog()
{
if (_pView)
{
delete _pView;
}
if (_pbitmapDoubleBuf)
{
delete _pbitmapDoubleBuf;
}
}
//
// Call this after changing the size of the animate control.
//
CSize CAnimateDialog::_RecalcSizeNeeded()
{
CSize sizeMax(0, 0);
if (_pView)
{
int cCels = _pView->GetCelCount(_nLoop);
CRect rectPlacements(1000, 1000, -1000, -1000);
for (int i = 0; i < cCels; i++)
{
CPoint point = _pView->GetPlacement(MAKE_INDEX(_nLoop, i));
rectPlacements.left = min(rectPlacements.left, point.x);
rectPlacements.right = max(rectPlacements.right, point.x);
rectPlacements.top = min(rectPlacements.top, point.y);
rectPlacements.bottom = max(rectPlacements.bottom, point.y);
CSize size = _pView->GetSize(MAKE_INDEX(_nLoop, i));
sizeMax.cx = max(sizeMax.cx, size.cx);
sizeMax.cy = max(sizeMax.cy, size.cy);
}
_rectPlacements = rectPlacements;
//_size
// Increase our "max rect" by all the placement possibilities.
sizeMax.cx += rectPlacements.Width();
sizeMax.cy += rectPlacements.Height();
// Try to fit the thing intelligently in our space.
_iZoom = min(_sizeAnimate.cx / sizeMax.cx, _sizeAnimate.cy / sizeMax.cy);
_iZoom = max(_iZoom, 1);
}
sizeMax.cx *= _iZoom;
sizeMax.cy *= _iZoom;
return sizeMax;
}
//
// Called once at the beginning (or later, if the view data changes)
//
void CAnimateDialog::_AutoSize()
{
if (_pView)
{
CSize sizeMax = _RecalcSizeNeeded();
CRect rect;
m_wndAnimate.GetClientRect(&rect);
int dx = sizeMax.cx - rect.Width();
int dy = sizeMax.cy - rect.Height();
if ((dx > 0) || (dy > 0))
{
CRect rectDialog;
GetClientRect(&rectDialog);
if (dx > 0)
{
rectDialog.right += dx;
}
if (dy > 0)
{
rectDialog.left += dy;
}
MoveWindow(&rectDialog);
}
}
}
void CAnimateDialog::SetView(const ViewResource *pView)
{
_pView = static_cast<ViewResource*>(pView->Clone());
}
void CAnimateDialog::DoDataExchange(CDataExchange* pDX)
{
__super::DoDataExchange(pDX);
DDX_Control(pDX, IDC_ANIMATE, m_wndAnimate);
DDX_Control(pDX, IDC_SLIDER, m_wndSlider);
DDX_Control(pDX, IDC_BUTTONPLAY, m_wndButton);
CRect rectOrig;
m_wndAnimate.GetClientRect(&rectOrig);
_sizeAnimate.cx = rectOrig.Width();
_sizeAnimate.cy = rectOrig.Height();
_sizeWeDrawIn = _RecalcSizeNeeded();
GetClientRect(&_rectDialogSize);
_AutoSize();
SetWindowText(_strTitle);
SetTimer(ANIMATE_TIMER, 200, NULL);
_fInitialized = TRUE;
m_wndSlider.SetRange(0, _pView->GetCelCount(_nLoop) - 1);
_UpdateButton();
DDX_Control(pDX, IDOK, m_wndOK);
}
void CAnimateDialog::_UpdateButton()
{
// Update command ui doesn't work here, so set the text directly.
m_wndButton.SetWindowText(_fAnimating ? TEXT("Pause") : TEXT("Play"));
}
void CAnimateDialog::OnPlay()
{
_fAnimating = !_fAnimating;
_UpdateButton();
}
BEGIN_MESSAGE_MAP(CAnimateDialog, CExtResizableDialog)
ON_WM_SIZE()
ON_WM_DRAWITEM()
ON_WM_TIMER()
ON_WM_HSCROLL()
ON_COMMAND(IDC_BUTTONPLAY, OnPlay)
END_MESSAGE_MAP()
void CAnimateDialog::OnSize(UINT nType, int cx, int cy)
{
CRect rectDialogSize;
GetClientRect(&rectDialogSize);
if (_fInitialized)
{
int dx = rectDialogSize.Width() - _rectDialogSize.Width();
int dy = rectDialogSize.Height() - _rectDialogSize.Height();
if (dx || dy)
{
CRect rectAnimateScreen;
m_wndAnimate.GetWindowRect(&rectAnimateScreen);
ScreenToClient(&rectAnimateScreen);
rectAnimateScreen.right = rectAnimateScreen.left + rectAnimateScreen.Width() + dx;
rectAnimateScreen.bottom = rectAnimateScreen.top + rectAnimateScreen.Height() + dy;
m_wndAnimate.MoveWindow(&rectAnimateScreen, TRUE);
_sizeAnimate.cx = rectAnimateScreen.Width();
_sizeAnimate.cy = rectAnimateScreen.Height();
_sizeWeDrawIn = _RecalcSizeNeeded();
int rgid[] = { IDOK, IDC_BUTTONPLAY };
for (int i = 0; i < ARRAYSIZE(rgid); i++)
{
CWnd *pOk = GetDlgItem(rgid[i]);
if (pOk)
{
CRect rectOkScreen;
pOk->GetWindowRect(&rectOkScreen);
ScreenToClient(&rectOkScreen);
rectOkScreen.OffsetRect(dx, dy);
pOk->MoveWindow(&rectOkScreen, TRUE);
}
}
CWnd *pSlider = GetDlgItem(IDC_SLIDER);
if (pSlider)
{
CRect rectScreen;
pSlider->GetWindowRect(&rectScreen);
ScreenToClient(&rectScreen);
rectScreen.OffsetRect(0, dy);
pSlider->SetWindowPos(NULL, rectScreen.left, rectScreen.top, rectScreen.Width() + dx, rectScreen.Height(), SWP_NOZORDER);
}
m_wndAnimate.Invalidate(FALSE);
}
}
_rectDialogSize = rectDialogSize;
__super::OnSize(nType, cx, cy);
}
void CAnimateDialog::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == ANIMATE_TIMER)
{
if (_pView && _fAnimating)
{
int cCels = _pView->GetCelCount(_nLoop);
_nCel++;
_nCel %= cCels;
m_wndSlider.SetPos(_nCel);
m_wndAnimate.Invalidate(FALSE);
}
}
else
{
__super::OnTimer(nIDEvent);
}
}
void CAnimateDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *pWnd)
{
switch (nSBCode)
{
case SB_THUMBTRACK:
_nCel = nPos;
_fAnimating = FALSE;
_UpdateButton();
m_wndAnimate.Invalidate(FALSE);
break;
case SB_LEFT:
case SB_ENDSCROLL:
case SB_LINELEFT:
case SB_LINERIGHT:
case SB_PAGELEFT:
case SB_PAGERIGHT:
case SB_RIGHT:
_nCel = m_wndSlider.GetPos();
_fAnimating = FALSE;
_UpdateButton();
m_wndAnimate.Invalidate(FALSE);
break;
case SB_THUMBPOSITION:
// We update constantly, so no need to do anything here.
break;
}
}
//
// REVIEW: we're still off by one here, in some cases (due to placement)
//
void CAnimateDialog::_OnDraw(CDC *pDC, LPRECT prc)
{
// First fill with the background color.
BYTE bColor = _pView->GetTransparentColor(MAKE_INDEX(_nLoop, _nCel));
//COLORREF color = EGA_TO_COLORREF(15 - bColor);
COLORREF color = RGB(255, 192, 128);
CBrush brush;
brush.CreateSolidBrush(color);
pDC->FillRect(prc, &brush);
if (_pView)
{
DWORD dwIndex = MAKE_INDEX(_nLoop, _nCel);
CSize size = _pView->GetSize(dwIndex);
size_t cbViewData = CX_ACTUAL(size.cx) * size.cy;
if (cbViewData > 0)
{
CPoint ptPlacement = _pView->GetPlacement(dwIndex);
scoped_array<BYTE> viewData(new BYTE[cbViewData]);
_pView->CopyBitmapData(dwIndex, viewData.get(), size);
SCIBitmapInfo bmi(size.cx, size.cy);
CSize sizeDest(size.cx * _iZoom, size.cy * _iZoom);
// Put view at the bottom and centered.
CPoint pointAt((RECTWIDTH(*prc) - _sizeWeDrawIn.cx) / 2, RECTHEIGHT(*prc) - _sizeWeDrawIn.cy);
CBrush brush;
brush.CreateSolidBrush(RGB(255, 255, 255));
CRect rectFoo(pointAt.x, pointAt.y, pointAt.x + _sizeWeDrawIn.cx, pointAt.y + _sizeWeDrawIn.cy);
::FillRect((HDC)*pDC, &rectFoo, brush);
// Now look at the placement we got for this item, and consider it against
// the rect of *all* placements. The above x and y is good for placement 0,0
CPoint pointOffset = ptPlacement - _rectPlacements.TopLeft();
pointOffset.x *= _iZoom;
pointOffset.y *= _iZoom;
pointAt.Offset(pointOffset);
// Adjust it a little more, since each frame is of a different height. We need to
// shove it to the bottom of where the highest one would paint.
int cyHighestFrame = _sizeWeDrawIn.cy - _rectPlacements.Height() * _iZoom;
ASSERT(cyHighestFrame >= sizeDest.cy);
int dy = cyHighestFrame - sizeDest.cy;
pointAt.y += dy;
int cxWidestFrame = _sizeWeDrawIn.cx - _rectPlacements.Width() * _iZoom;
ASSERT(cxWidestFrame >= sizeDest.cx);
int dx = (cxWidestFrame - sizeDest.cx) / 2; // since x is centered
pointAt.x += dx;
StretchDIBits((HDC)*pDC, pointAt.x, pointAt.y, sizeDest.cx, sizeDest.cy, 0, 0, size.cx, size.cy, viewData.get(), &bmi, DIB_RGB_COLORS, SRCCOPY);
}
}
}
void CAnimateDialog::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (nIDCtl == IDC_ANIMATE)
{
LPRECT prc = &lpDrawItemStruct->rcItem;
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
_GenerateDoubleBuffer(&dc, prc);
CDC dcMem;
if (dcMem.CreateCompatibleDC(&dc))
{
if (_pbitmapDoubleBuf)
{
HGDIOBJ hOldBmp = dcMem.SelectObject(_pbitmapDoubleBuf);
// Now we're ready to draw.
_OnDraw(&dcMem, prc);
// Copy into the final buffer:
dc.StretchBlt(0, 0, RECTWIDTH(*prc), RECTHEIGHT(*prc), &dcMem, 0, 0, RECTWIDTH(*prc), RECTHEIGHT(*prc), SRCCOPY);
dcMem.SelectObject(hOldBmp);
}
}
dc.Detach();
}
else
{
__super::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
}
void CAnimateDialog::_GenerateDoubleBuffer(CDC *pDC, LPRECT prc)
{
if (!_fDoubleBuffer || (_sizeDoubleBuf != _sizeAnimate))
{
_sizeDoubleBuf = _sizeAnimate;
if (_pbitmapDoubleBuf)
{
delete _pbitmapDoubleBuf;
}
_pbitmapDoubleBuf = new CBitmap();
if (_pbitmapDoubleBuf)
{
_fDoubleBuffer = _pbitmapDoubleBuf->CreateCompatibleBitmap(pDC, _sizeDoubleBuf.cx, _sizeDoubleBuf.cy);
}
}
}
// CAnimateDialog message handlers