-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameBrd.cpp
1731 lines (1467 loc) · 42.1 KB
/
GameBrd.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// GameBrd.cpp : implementation file
//
#include "stdafx.h"
#include "GameImg.h"
#include "GamePce.h"
#include "GameSlot.h"
#include "GameArea.h"
#include "GameBrd.h"
#include "GameApp.h"
#include "GameMIm.h"
#include "GameBtn.h"
#include "GameText.h"
#include "VctComm.h" // GetState needs some of the defines there
#include "Picture.h"
//#include <Winsock.h>
#include <mmsystem.h>
#include "resource.h"
#include "canim.h"
#include "bmp.h"
// ScrollBar stuff
#ifdef WIN32
SCROLLINFO HSi;
SCROLLINFO VSi;
#endif // WIN32
BOOL IsWin95()
{
#ifdef WIN32
return TRUE;
#else
return (HIBYTE(LOWORD(GetVersion())) == 95);
#endif // WIN32
}
#ifdef WIN32
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#endif //WIN32
/////////////////////////////////////////////////////////////////////////////
// CUbqGameBoard
BEGIN_MESSAGE_MAP(CUbqGameBoard, CWnd)
//{{AFX_MSG_MAP(CUbqGameBoard)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_WM_SIZE()
ON_WM_LBUTTONDBLCLK()
ON_WM_RBUTTONUP()
ON_MESSAGE(MM_MCINOTIFY,OnMMNotify)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CUbqGameBoard::CUbqGameBoard(class CUbqGameApplet* pGameApp, CWnd* pParent, CRect rGame,
UINT BoardBmpName, CRect rBoard, BOOL bEmphSlts,
BOOL bShowScrollBar, BOOL bShowPopupMenu)
{
m_pApplet = pGameApp;
m_rGame = rGame;
m_rBoard = rBoard;
m_bSuspend = FALSE; //xxx
m_bIsLocked = FALSE;
m_pDraggedPiece = NULL;
m_pRejectPiece = NULL;
m_pDragLastArea = NULL;
m_pDragLastSlot = NULL;
m_pCurrFocusedButton = NULL;
m_pCurrClickedButton = NULL;
m_bCaptureSet = FALSE;
m_bEmphasizeSlots = bEmphSlts;
m_bShowScrollBar = bShowScrollBar;
m_bShowPopupMenu = bShowPopupMenu;
#ifndef WIN32
#define WS_EX_TOOLWINDOW 0
#endif
CString strClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS, LoadCursor(NULL,IDC_ARROW),
(HBRUSH)GetStockObject(LTGRAY_BRUSH));
if (!strClassName.IsEmpty())
{
m_pParent = pParent;
CreateEx(WS_EX_TOOLWINDOW, strClassName, "GameBoard", WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, m_rGame.left, m_rGame.top, m_rGame.Width(), m_rGame.Height(),
m_pParent->m_hWnd, NULL);
}
m_bViewFlipV = m_bViewFlipH = FALSE;
// ScrollBar stuff
m_RectScroll.SetRectEmpty();
for (int i=0;i<MAX_CURSORS;i++)
m_hCursors[i]=NULL;
m_bDisableCursor=FALSE;
CBmp::Init();
m_nBoardBmpName=BoardBmpName;
m_nBpp=CBmp::ReadBmp(&m_cBoardBmp,m_nBoardBmpName,this);
}
CUbqGameBoard::~CUbqGameBoard()
{
RemoveAllPieces();
RemoveAllAreas();
RemoveAllMultiImages();
RemoveAllButtons();
RemoveRemoveAllTexts();
m_cBoardBmp.DeleteObject();
CBmp::UnInit();
m_bSuspend = TRUE;
}
void CUbqGameBoard::Init()
{
PostPaintEvent(m_rGame);
}
void CUbqGameBoard::Lock(BOOL isLocked)
{
m_bIsLocked = isLocked;
}
/**
* Move a piece on the board
* @param piece The game piece that should be moved.
* @param newX The new board X position
* @param newY The new board Y position
* @param Indicate if you want a line connecting the old position and
* the new position flashing briefly before the piece is moved. Select one
* of the following values:
* <pre>
* SHOW_ANIMATE To show the animated move of the piece
* SHOW_NONE Not to show the connecting line
* </pre>
* @param toTop Indicates if you want the piece Z order to be changed.
* Select one of the following values:
* <pre>
* MOVE_TO_TOP To move the piece to the top of the Z order
* MOVE_NO_Z_ORDER_CHANGE No change in the Z order
* </pre>
*/
void CUbqGameBoard::MovePiece(CUbqGamePiece* piece, CPoint NewPoint, BOOL bAnimate, BOOL bToTop)
{
int iMaxSteps;
if (!bAnimate)
iMaxSteps = 1;
else
iMaxSteps = IsWin95() ? 50 : 5;
DoMoveAnimation(piece, NewPoint, TRUE, bToTop, iMaxSteps);
}
void CUbqGameBoard::RejectMovePiece(CUbqGamePiece* piece, CPoint DestPoint)
{
DWORD ver = GetVersion();
int iMaxSteps = IsWin95() ? 50 : 5;
DoMoveAnimation(piece, DestPoint, FALSE, FALSE, iMaxSteps);
}
void CUbqGameBoard::DoMoveAnimation(CUbqGamePiece* piece, CPoint Point, BOOL bAccept, BOOL bToTop, int iMaxSteps)
{
CObject* o;
CUbqGamePiece* p;
CPoint ap;
if (iMaxSteps <= 0)
iMaxSteps = 10;
int size = m_vecPieces.GetSize();
for (int i=0; i<size; i++)
{
o = m_vecPieces.GetAt(i);
p = (CUbqGamePiece*) o;
if (p == piece)
{
if (p == m_pDraggedPiece) // being currently dragged, so release drag
{
Point = CPoint(m_pDraggedPiece->m_rArea.TopLeft());
CSize Size = m_pDraggedPiece->m_rArea.Size();
CRect Rect = m_pDraggedPiece->m_rArea;
AdjustPoint(CRect(m_ptDragPieceDest, Size), ap);
//m_pDraggedPiece = NULL; // release my piece (My operation is ignored)
PostPaintEvent(CRect(ap, Size));
AdjustPoint(Rect, ap);
PostPaintEvent(CRect(ap, Size));
}
CPoint CurrPoint, NextPoint, StartPoint, EndPoint;
float dx, dy;
if (bAccept) // accept move from piece current to (x,y)
{
StartPoint = p->m_rArea.TopLeft();
EndPoint = Point;
}
else // Reject
{
StartPoint = Point;
EndPoint = p->m_rArea.TopLeft();
m_pRejectPiece = p;
}
int iSteps;
// make steps proportional to distance
CPoint DiffPoint = CPoint(abs(EndPoint.x-StartPoint.x), abs(EndPoint.y-StartPoint.y));
int d = DiffPoint.x+DiffPoint.y;
iSteps = d / 5;
if (iSteps > iMaxSteps) iSteps = iMaxSteps;
if (iSteps < 1) iSteps = 1;
dx = (float)(EndPoint.x-StartPoint.x) / (float)iSteps;
dy = (float)(EndPoint.y-StartPoint.y) / (float)iSteps;
CurrPoint = StartPoint;
for (int s = (bAccept ? 1 : 0); s <= iSteps; s++) // if reject - start from actual end point
{
if (s == iSteps) // make sure last move is to exact point, no truncation
NextPoint = EndPoint;
else
NextPoint = CPoint((int)((float)StartPoint.x+(dx*s)), (int)((float)StartPoint.y+(dy*s)));
if (bAccept)
{
p->m_rArea.OffsetRect(NextPoint-p->m_rArea.TopLeft());
if (bToTop && s==1) // do to-top only on first section
{
m_vecPieces.RemoveAt(i);
m_vecPieces.Add(p); // last element is on the top of the Z order
}
}
else // Reject
m_ptRejectPiece = NextPoint;
// destination point
AdjustPoint(CRect(NextPoint, p->m_rArea.Size()), ap);
CRect r1(ap + CSize(-1,-1), p->m_rArea.Size()+CSize(1,1));
// source point
AdjustPoint(CRect(CurrPoint, p->m_rArea.Size()), ap);
CRect r2(ap + CSize(-1,-1), p->m_rArea.Size()+CSize(1,1));
r1.UnionRect(r1,r2);
PostPaintEvent(r1);
CurrPoint = NextPoint;
}
if (!bAccept) // remove last frame of rejection picture
{
m_pRejectPiece = NULL;
AdjustPoint(CRect(CurrPoint, p->m_rArea.Size()), ap);
CRect r(ap + CSize(-1,-1), p->m_rArea.Size()+CSize(1,1));
PostPaintEvent(r);
}
break;
}
}
}
/**
* Find a game piece
* @param ID The piece ID to find.
* The method finds the first piece that has the specified ID.
* @return The requested piece (null if not found)
*/
CUbqGamePiece* CUbqGameBoard::GetPieceByID(int ID)
{
CObject* o = NULL;
CUbqGamePiece* p = NULL;
int size = m_vecPieces.GetSize();
for (int i=0; i<size; i++)
{
o = m_vecPieces.GetAt(i);
p = (CUbqGamePiece*) o;
if (ID == p->id)
return p;
}
return NULL;
}
void CUbqGameBoard::RemovePiece(int id)
{
CUbqGamePiece* piece = GetPieceByID(id);
CObject* o;
CUbqGamePiece* p;
int size = m_vecPieces.GetSize();
for (int i=0; i<size; i++)
{
o = m_vecPieces.GetAt(i);
p = (CUbqGamePiece*) o;
if (p == piece)
{
m_vecPieces.RemoveAt(i);
CPoint pa;
AdjustPoint(p->m_rArea, pa);
PostPaintEvent(CRect(pa, p->m_rArea.Size()));
delete p;
return;
}
}
}
//
// adjust a point from absolute position to the view-point position,
// and the other way around
//
void CUbqGameBoard::AdjustPoint(CPoint Point, CPoint& pt)
{
AdjustPoint(CRect(Point, CSize(0,0)), pt);
}
void CUbqGameBoard::AdjustPoint(CRect Rect, CPoint& pt)
{
int x = Rect.left;
int y = Rect.top;
if (m_bViewFlipH)
x = m_rBoard.right - (x - m_rBoard.left) - Rect.Width();
if (m_bViewFlipV)
y = m_rBoard.bottom - (y - m_rBoard.top) - Rect.Height();
pt = CPoint(x, y);
}
void CUbqGameBoard::SetViewPoint(BOOL bFlipV, BOOL bFlipH)
{
m_bViewFlipV = bFlipV;
m_bViewFlipH = bFlipH;
PostPaintEvent(m_rBoard);// Invalidate();
}
void CUbqGameBoard::PostPaintEvent(CRect& rc)
{
// rect in the board coordinates
CRect Rect(rc);
// rect in the client area coordinates
Rect.OffsetRect(m_RectScroll.TopLeft());
Rect.OffsetRect(m_cOffsetPoint);
InvalidateRect(Rect, FALSE);
//UpdateWindow();
}
// We get an absolute coordinates.
void CUbqGameBoard::DrawBoardAndPieces(CDC& dc, CRect& rcPaint)
{
CDC* pDC = (CDC*) &dc;
CDC dcBuf;
dcBuf.CreateCompatibleDC(pDC);
CBitmap bmBuf;
bmBuf.CreateCompatibleBitmap(pDC, rcPaint.Width(), rcPaint.Height());
CBitmap* bmOld = (CBitmap*) dcBuf.SelectObject(&bmBuf);
rcPaint.OffsetRect(-m_cOffsetPoint);
m_pApplet->DrawImage(&m_cBoardBmp,-rcPaint.TopLeft(),/*rcPaint.Size()*/CSize(G_BOARD_WIDTH,G_BOARD_HEIGHT), dcBuf);
CUbqGamePiece* p;
CObject* o;
// Draw all pieces
int size = m_vecPieces.GetSize();
for (int i = 0; i < size; i++) // bottom to top to preserve Z order
{
o = m_vecPieces.GetAt(i);
p = (CUbqGamePiece*) o;
if ((p->GetAttribute() & PA_INVISIBLE) == 0 && ( p != m_pDraggedPiece ))
{
CPoint ap;
AdjustPoint(p->m_rArea, ap);
CRect r1(ap, p->m_rArea.Size());
if (r1.IntersectRect(r1, rcPaint)) // intersection is not empty
m_pApplet->DrawImage(p->m_pImage, CPoint(CPoint(ap.x, ap.y) - rcPaint.TopLeft()), dcBuf);
}
}
// draw all buttons and multiple-images
size = m_vecMultipleImages.GetSize();
for (i = 0; i < size; i++) // list of buttons to be redrawn
{
CUbqMultImage* m = (CUbqMultImage*)(m_vecMultipleImages.GetAt(i));
CRect r1(m->m_rArea);
if (r1.IntersectRect(r1, rcPaint)) // intersection is not empty
m_pApplet->DrawImage(m->GetCurrImage(), CPoint(m->m_rArea.TopLeft()- rcPaint.TopLeft()), dcBuf);
}
// Circle around dragged piece - destination
if (m_pDraggedPiece != NULL)
{
CPoint ap;
AdjustPoint(CRect(m_ptDragPieceDest, m_pDraggedPiece->m_rArea.Size()), ap);
CRect r1(ap, m_pDraggedPiece->m_rArea.Size());
CRect rtemp;
if (rtemp.IntersectRect(r1,rcPaint)) // intersection is not empty
{
if (m_bEmphasizeSlots)
{
r1.OffsetRect(-rcPaint.left, -rcPaint.top);
#ifndef WIN32
// Draw a 3-dimentional rectangle.
CPen white_pen(PS_SOLID, 1, GetSysColor(COLOR_BTNHIGHLIGHT)); //sari
CPen* pOldPen = dcBuf.SelectObject(&white_pen);
dcBuf.MoveTo(r1.right-1, r1.top);
dcBuf.LineTo(r1.left, r1.top);
dcBuf.LineTo(r1.left, r1.bottom-1);
dcBuf.SelectStockObject(BLACK_PEN);
dcBuf.MoveTo(r1.right-1, r1.top);
dcBuf.LineTo(r1.right-1, r1.bottom-1);
dcBuf.LineTo(r1.left , r1.bottom-1);
CPen grey_pen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW));
dcBuf.SelectObject(&grey_pen);
dcBuf.MoveTo(r1.right - 2, r1.top + 1);
dcBuf.LineTo(r1.right - 2, r1.bottom - 2);
dcBuf.LineTo(r1.left + 1, r1.bottom - 2);
dcBuf.SelectObject(pOldPen);
#else
dcBuf.Draw3dRect(r1, RGB(0,0,0), RGB(255,255,255)); //sari
#endif
}
else
{
r1.OffsetRect(-rcPaint.left, -rcPaint.top);
dcBuf.SelectStockObject(WHITE_PEN);
dcBuf.SelectStockObject(NULL_BRUSH);
switch (m_pDraggedPiece->m_nShape)
{
case SHAPE_RECT:
dcBuf.Rectangle(r1);
break;
case SHAPE_OVAL:
dcBuf.Ellipse(r1);
break;
case SHAPE_PIECE:
m_pApplet->DrawImage(m_pDraggedPiece->m_pImage,r1.TopLeft(), dcBuf);
break;
}
}
}
// Mark the piece that is being dragged - source
AdjustPoint(m_pDraggedPiece->m_rArea, ap);
CRect r2 (ap, m_pDraggedPiece->m_rArea.Size());
if (rtemp.IntersectRect(r2,rcPaint)) // intersection is not empty
{
if (m_bEmphasizeSlots)
{
r2.OffsetRect(-rcPaint.left, -rcPaint.top);
#ifndef WIN32
// Draw a 3-dimentional rectangle.
CPen grey_pen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW)); //sari
CPen* pOldPen = dcBuf.SelectObject(&grey_pen);
dcBuf.MoveTo(r2.right-1, r2.top);
dcBuf.LineTo(r2.left, r2.top);
dcBuf.LineTo(r2.left, r2.bottom-1);
dcBuf.SelectObject(pOldPen);
dcBuf.SelectStockObject(WHITE_PEN);
dcBuf.MoveTo(r2.left, r2.bottom-1);
dcBuf.LineTo(r2.right-1, r2.bottom-1);
dcBuf.LineTo(r2.right-1, r2.top);
dcBuf.SelectStockObject(BLACK_PEN);
dcBuf.MoveTo(r2.right - 2, r2.top + 1);
dcBuf.LineTo(r2.left + 1, r2.top + 1);
dcBuf.LineTo(r2.left + 1, r2.bottom - 2);
dcBuf.SelectObject(pOldPen);
#else
dcBuf.Draw3dRect(r2, RGB(255,255,255), RGB(0,0,0)); //sari
#endif
}
else
{
dcBuf.SelectStockObject(WHITE_PEN);
dcBuf.SelectStockObject(WHITE_BRUSH);
int d1 = -(m_pDraggedPiece->m_rArea.Width() / 4);
int d2 = -(m_pDraggedPiece->m_rArea.Height() / 4);
r2.InflateRect(d1,d2);
r2.OffsetRect(-rcPaint.left, -rcPaint.top);
switch (m_pDraggedPiece->m_nShape)
{
case SHAPE_RECT:
dcBuf.Rectangle(r2);
break;
case SHAPE_OVAL:
dcBuf.Ellipse(r2);
break;
case SHAPE_PIECE:
break;
}
}
}
}
// Rejected move drawing
if (m_pRejectPiece != NULL)
{
CPoint ap;
AdjustPoint(CRect(m_ptRejectPiece, m_pRejectPiece->m_rArea.Size()), ap);
CRect r1(ap, m_pRejectPiece->m_rArea.Size());
CRect r2;
if (r2.IntersectRect(r1,rcPaint)) // intersection is not empty
{
CPen pen (PS_SOLID, 3, RGB(255,0,0));
CPen* pOldPen = dcBuf.SelectObject(&pen);
dcBuf.SelectStockObject(WHITE_BRUSH);
r1.OffsetRect(-rcPaint.left, -rcPaint.top);
r1.InflateRect(-2, -2);
switch (m_pDraggedPiece->m_nShape)
{
case SHAPE_RECT:
dcBuf.Rectangle(r1);
break;
case SHAPE_OVAL:
dcBuf.Ellipse(r1);
break;
case SHAPE_PIECE:
m_pApplet->DrawImage(m_pDraggedPiece->m_pImage,r1.TopLeft(), dcBuf);
break;
}
CPoint pc;
#ifndef WIN32
pc.x = (r1.left + r1.right) /2;
pc.y = (r1.top + r1.bottom) /2;
#else // WIN32
pc = r1.CenterPoint();
#endif // WIN32
dcBuf.MoveTo(pc); dcBuf.LineTo(pc.x-5, pc.y-5);
dcBuf.MoveTo(pc); dcBuf.LineTo(pc.x-5, pc.y+5);
dcBuf.MoveTo(pc); dcBuf.LineTo(pc.x+5, pc.y-5);
dcBuf.MoveTo(pc); dcBuf.LineTo(pc.x+5, pc.y+5);
dcBuf.SelectObject(pOldPen);
}
}
//Game Text areas
CCmgTextArea* GameText;
size = m_vecTexts.GetSize();
for (i=0; i<size; i++) // list of buttons to be redrawn
{
o = m_vecTexts.GetAt(i);
GameText = (CCmgTextArea*) o;
CRect r1(GameText->m_rArea);
if (r1.IntersectRect(r1,rcPaint)) // intersection is not empty
GameText->DrawText(&dcBuf,rcPaint.TopLeft());
}
rcPaint.OffsetRect(m_cOffsetPoint);
rcPaint.OffsetRect(m_RectScroll.TopLeft());
dc.BitBlt(rcPaint.left, rcPaint.top, rcPaint.Width(), rcPaint.Height(), &dcBuf, 0, 0, SRCCOPY);
dcBuf.SelectObject(bmOld);
dcBuf.DeleteDC();
}
/////////////////////////////////////////////////////////////////////////////////////
// Remove all pieces from the board.
/////////////////////////////////////////////////////////////////////////////////////
void CUbqGameBoard::RemoveAllPieces()
{
while (m_vecPieces.GetSize())
{
CObject* p = m_vecPieces.GetAt(0);
m_vecPieces.RemoveAt(0);
CUbqGamePiece* pgp = (CUbqGamePiece*) p;
delete pgp;
}
m_pDraggedPiece = NULL;
PostPaintEvent(m_rBoard);
}
/////////////////////////////////////////////////////////////////////////////////////
// Add a piece to the game board
// @param piece The game piece that should be added. If there is some overlapping,
// new piece will be placed on top of older pieces.
/////////////////////////////////////////////////////////////////////////////////////
void CUbqGameBoard::AddPiece(CUbqGamePiece* pPiece)
{
m_vecPieces.Add(pPiece);
PostPaintEvent(pPiece->m_rArea);
}
BOOL CUbqGameBoard::MouseDown(CPoint MPoint)
{
if ((m_bIsLocked) && m_rBoard.PtInRect(MPoint))
return TRUE;
// butts
int size = m_vecButtons.GetSize();
for (int i = 0;i < size; i++)
{
CUbqGameButton* btn = (CUbqGameButton*)(m_vecButtons.GetAt(i));
if (btn->IsInside(MPoint) && !btn->IsDisabled())
{
m_pCurrClickedButton = btn;
m_pCurrClickedButton->MouseDown();
break;
}
}
// pieces
CPoint ap;
AdjustPoint(MPoint, ap);
for (i = m_vecPieces.GetSize() - 1; i >= 0; i--) // from top to bottom on Z order
{
CUbqGamePiece* p = (CUbqGamePiece*)(m_vecPieces.GetAt(i));
if (p->IsInside(ap) && (p->GetAttribute() & (PA_CLICK|PA_DRAG|PA_DBLCLK)) != 0)
{
if (m_pApplet->OnPieceMouseDown(p, ap))
{
m_pDraggedPiece = p;
m_ptDragPieceDest = m_pDraggedPiece->m_rArea.TopLeft();
CPoint ap;
AdjustPoint(CRect(m_ptDragPieceDest, m_pDraggedPiece->m_rArea.Size()), ap);
PostPaintEvent(CRect(ap, m_pDraggedPiece->m_rArea.Size()));
}
break;
}
}
return TRUE;
}
BOOL CUbqGameBoard::MouseDrag(CPoint MPoint)
{
if (m_pDraggedPiece == NULL) return TRUE; // no piece to drag
if ((m_pDraggedPiece->GetAttribute() & PA_DRAG) == 0) return TRUE; // piece may not drag
CPoint ap;
AdjustPoint(MPoint, ap);
ap.x -= (m_pDraggedPiece->m_rArea.Width()/2);
ap.y -= (m_pDraggedPiece->m_rArea.Height()/2);
CUbqGameBoardArea* pArea = GetArea(CRect(ap, m_pDraggedPiece->m_rArea.Size()));
if (pArea == NULL) return TRUE; // not in any area
CUbqGameBoardSlot* slot = pArea->GetBestSlot(ap);
if (slot != NULL)
ap = slot->m_ptCoord;
if (ap == m_pDraggedPiece->m_rArea.TopLeft())
return TRUE; // not moved
m_pDragLastArea = pArea;
m_pDragLastSlot = slot;
if (m_pApplet->OnPieceDuringDrag(m_pDraggedPiece, pArea, slot, ap))
{
CPoint tmp;
AdjustPoint(CRect(m_ptDragPieceDest, m_pDraggedPiece->m_rArea.Size()), tmp);
CRect rf(tmp, m_pDraggedPiece->m_rArea.Size());
m_ptDragPieceDest = ap;
AdjustPoint(CRect(m_ptDragPieceDest, m_pDraggedPiece->m_rArea.Size()), tmp);
CRect rt(tmp, m_pDraggedPiece->m_rArea.Size());
rf.UnionRect(rf,rt);
rf.InflateRect(1,1);
PostPaintEvent(rf);
}
return TRUE;
}
BOOL CUbqGameBoard::MouseUp(CPoint MPoint)
{
if (m_pDraggedPiece != NULL)
{
CPoint ap1;
AdjustPoint(CRect(m_ptDragPieceDest, m_pDraggedPiece->m_rArea.Size()), ap1);
CPoint ap2;
AdjustPoint(m_pDraggedPiece->m_rArea, ap2);
if (m_pApplet->OnPieceEndDrag(m_pDraggedPiece, m_pDragLastArea, m_pDragLastSlot, m_ptDragPieceDest))
{
if (m_pDraggedPiece != NULL) // not moved from applet
{
CSize Size = m_pDraggedPiece->m_rArea.Size();
m_pDraggedPiece = NULL;
PostPaintEvent(CRect(ap1, Size));
PostPaintEvent(CRect(ap2, Size));
}
}
else
{
CSize Size = m_pDraggedPiece->m_rArea.Size();
m_pDraggedPiece = NULL;
PostPaintEvent(CRect(ap1, Size));
PostPaintEvent(CRect(ap2, Size));
}
return TRUE;
}
if (m_pCurrClickedButton != NULL)// no piece to drag
{
if ((m_pCurrFocusedButton != NULL) &&
(m_pCurrFocusedButton->GetID() == m_pCurrClickedButton->GetID()))
{
m_pCurrClickedButton = NULL;
m_pCurrFocusedButton->MouseUp();
if (!m_pCurrFocusedButton->IsDisabled())
m_pApplet->OnButtonClicked(m_pCurrFocusedButton->GetID());
}
else
m_pCurrClickedButton = NULL;
return TRUE;
}
return TRUE;
}
/******************************************************************
* Areas management *
******************************************************************/
/**
* Remove all board areas.
*/
void CUbqGameBoard::RemoveAllAreas()
{
while (m_vecAreas.GetSize() > 0)
{
CObject* po = m_vecAreas.GetAt(0);
CUbqGameBoardArea* pa = (CUbqGameBoardArea*) po;
delete pa;
m_vecAreas.RemoveAt(0);
}
}
/**
* Add an area to the board. As Area is a rectangle where pieces can be moved into.
* Board sections that are not in any area can not contain board pieces.
*/
void CUbqGameBoard::AddArea(CUbqGameBoardArea* ar)
{
m_vecAreas.Add(ar);
}
/**
* Remove an area from the board
*/
BOOL CUbqGameBoard::RemoveArea(int id)
{
CObject* o = NULL;
CUbqGameBoardArea* ar = NULL;
int size = m_vecAreas.GetSize();
for (int i = 0; i < size; i++)
{
o = m_vecAreas.GetAt(i);
ar = (CUbqGameBoardArea*)o;
if (ar->GetID() == id)
{
delete ar;
m_vecAreas.RemoveAt(i);
return TRUE;
}
}
return FALSE;
}
CUbqGameBoardArea* CUbqGameBoard::GetArea(CRect rArea)
{
CUbqGameBoardArea* ar = NULL;
CObject* o = NULL;
int size = m_vecAreas.GetSize();
for (int i = 0; i < size; i++)
{
o = m_vecAreas.GetAt(i);
ar = (CUbqGameBoardArea*) o;
if (ar->IsInside(rArea))
return ar;
}
return NULL;
}
/******************************************************************
* Multiple Images management
******************************************************************/
void CUbqGameBoard::AddMultiImage(class CUbqMultImage* pMultiImage)
{
m_vecMultipleImages.Add(pMultiImage);
}
void CUbqGameBoard::RemoveMultiImage(class CUbqMultImage* pMultiImage)
{
int size = m_vecMultipleImages.GetSize();
for (int i = 0; i < size; i++)
{
CUbqMultImage* mi = (CUbqMultImage*)(m_vecMultipleImages.GetAt(i));
if (mi == pMultiImage)
{
m_vecMultipleImages.RemoveAt(i);
PostPaintEvent(mi->m_rArea);
delete mi;
return;
}
}
}
void CUbqGameBoard::RemoveAllMultiImages()
{
while (m_vecMultipleImages.GetSize() > 0)
{
delete (CUbqMultImage*)(m_vecMultipleImages.GetAt(0));
m_vecMultipleImages.RemoveAt(0);
}
}
/******************************************************************
* Buttons management *
******************************************************************/
/**
* Remove all buttons areas.
*/
void CUbqGameBoard::RemoveAllButtons()
{
// buttons are really removed when removing the multi-images
m_pCurrFocusedButton = NULL;
m_pCurrClickedButton = NULL;
m_vecButtons.RemoveAll();
}
/**
* Add a button to the board. As button is a rectangle where user can click on.
*/
void CUbqGameBoard::AddButton(CUbqGameButton* btn)
{
m_vecButtons.Add(btn);
AddMultiImage(btn);
}
/**
* Remove a button from the board
*/
BOOL CUbqGameBoard::RemoveButton(int id)
{
CObject* o = NULL;
CUbqGameButton* btn = NULL;
int size = m_vecButtons.GetSize();
for (int i=0; i<size; i++)
{
o = m_vecButtons.GetAt(i);
btn = (CUbqGameButton*) o;
if (btn->GetID() == id)
{
delete btn;
m_vecButtons.RemoveAt(i);
return TRUE;
}
}
return FALSE;
}
CUbqGameButton* CUbqGameBoard::GetButton(CPoint Point)
{
CUbqGameButton* btn = NULL;
CObject* o = NULL;
int size = m_vecButtons.GetSize();
for (int i = 0; i < size; i++)
{
o = m_vecButtons.GetAt(i);
btn = (CUbqGameButton*)o;
if (btn->IsInside(Point))
return btn;
}
return NULL;
}
CUbqGameButton* CUbqGameBoard::GetButtonByID(int id)
{
CUbqGameButton* btn = NULL;
CObject* o = NULL;
int size = m_vecButtons.GetSize();
for (int i = 0; i < size; i++)
{
o = m_vecButtons.GetAt(i);
btn = (CUbqGameButton*) o;
if (btn->GetID() == id)
return btn;
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
// CUbqGameBoard message handlers
void CUbqGameBoard::OnPaint()
{
AFX_MANAGE_STATE(_afxModuleAddrThis)
if (IsRemovalSuspended()) return; //xxx
CPaintDC dc(this); // device context for painting
CRect rcPaint = dc.m_ps.rcPaint;
if (m_nBpp != dc.GetDeviceCaps(BITSPIXEL))
{
m_cBoardBmp.DeleteObject();
m_nBpp=CBmp::ReadBmp(&m_cBoardBmp,m_nBoardBmpName,this);
CAnim::RereadAllBitmaps();
}
CPalette* OldPal= dc.SelectPalette(&(CBmp::m_cPalette),FALSE);
dc.RealizePalette();
// serg
// we redraw board&pieces only if the board area was clipped
CRect Checkintersect=m_rGame;
Checkintersect.OffsetRect(m_cOffsetPoint);
if (!rcPaint.IsRectEmpty() && rcPaint.IntersectRect(rcPaint,Checkintersect))
{
rcPaint.OffsetRect(-m_RectScroll.TopLeft());
ASSERT(rcPaint.top >= 0 && rcPaint.left >= 0);
DrawBoardAndPieces(dc, rcPaint);
}
dc.SelectPalette(OldPal,TRUE);
dc.RealizePalette();
}
void CUbqGameBoard::OnLButtonDown(UINT nFlags, CPoint point)
{
AFX_MANAGE_STATE(_afxModuleAddrThis)
if (IsRemovalSuspended()) return;//xxx
point.x -= m_RectScroll.TopLeft().x + m_cOffsetPoint.x;
point.y -= m_RectScroll.TopLeft().y + m_cOffsetPoint.y;
MouseDown(point);
m_bCaptureSet = TRUE;
SetCapture();
m_pApplet->OnLButtonDown (point); // call applet callback.
if (m_bSuspend) return;
CWnd::OnLButtonDown(nFlags, point);
}
void CUbqGameBoard::OnRButtonDown(UINT nFlags, CPoint point)
{
AFX_MANAGE_STATE(_afxModuleAddrThis)
if (IsRemovalSuspended()) return;//xxx
if (m_bShowPopupMenu)
m_pApplet->LoadMenu(point);
if (m_bSuspend) return;//xxx