-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcards.pas
284 lines (257 loc) · 8.48 KB
/
cards.pas
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
unit cards;
//***********************************************************************
//** Cards Component **
//***********************************************************************
//** This component allows you to produce a variety of card games. **
//** The code contained in this document is entirely free to use in **
//** both commercial and non-commercial applications. If this **
//** component is utilized in any way, shape, or form, I would **
//** appreciate a notification via email indicating this, as well as **
//** any comments or suggestions you may have. **
//** I can be reached at: **
//** elvis@sway.com **
//** Sincerely, **
//** T. J. Sobotka **
//***********************************************************************
//** Properties: **
//** Card: Used to select card for individual component. Custom **
//** property editor was required to ensure correct ordering **
//** in the object inspector. **
//** SelectedCard: Used to determine the appearance of a card if **
//** an alternative is desired to indicate a selected card. **
//** ShowCard: Used to determine whether or not the card has its **
//** face or card back showing. **
//** Suit: Used to select card suit for individual component. **
//***********************************************************************
//** modified for use with Lazarus by TheBlackSheep 2012-04-06 **
//***********************************************************************
interface
{$R 'cardres.res'}
uses
Windows,
Messages,
SysUtils,
Classes,
Controls,
PropEdits,
Graphics,
LResources;
const
DEFAULT_CARD_WIDTH = 71;
DEFAULT_CARD_HEIGHT = 96;
type
TCardValue =
(Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King);
TCardSuit = (Clubs, Diamonds, Hearts, Spades);
TCardValueEditor = class(TEnumProperty)
protected
function GetAttributes: TPropertyAttributes; override;
function GetValue: string; override;
procedure SetValue(const Value: string); override;
procedure GetValues(Proc: TGetStrProc); override;
end;
ECard = class(Exception);
TCard = class(TGraphicControl)
private
{ Private declarations }
protected
{ Protected declarations }
FCardValue: TCardValue;
FCardSuit: TCardSuit;
FShowCard: Boolean;
FSelectedCard: Boolean;
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
procedure Paint; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetCardValue(CardValue: TCardValue);
procedure SetShowCard(CardShowValue: Boolean);
procedure SetCardSuit(CardSuitValue: TCardSuit);
procedure SetSelectedCard(CardSelectedValue: Boolean);
published
{ Published declarations }
property Card: TCardValue read FCardValue write SetCardValue;
property SelectedCard: Boolean read FSelectedCard write SetSelectedCard;
property ShowCard: Boolean read FShowCard write SetShowCard;
property Suit: TCardSuit read FCardSuit write SetCardSuit;
property DragMode;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseMove;
property OnMouseDown;
property OnMouseUp;
end;
procedure Register;
var
CardInstanceCount: LongInt = 0;
CardSet: TBitmap;
CardBack: TBitmap;
CardMask: TBitmap;
CardWork: TBitmap;
implementation
constructor TCard.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCardValue := Ace;
if (CardInstanceCount < 1) then
begin
CardSet := TBitmap.Create;
CardMask := TBitmap.Create;
CardMask.Monochrome := True;
CardBack := TBitmap.Create;
CardWork := TBitmap.Create;
CardWork.Width := DEFAULT_CARD_WIDTH;
CardWork.Height := DEFAULT_CARD_HEIGHT;
CardSet.LoadFromResourceName(HInstance, 'CARDSET');
CardMask.LoadFromResourceName(HInstance, 'CARDMASK');
CardBack.LoadFromResourceName(HInstance, 'CARDBACK');
end;
Inc(CardInstanceCount);
end;
destructor TCard.Destroy;
begin
Dec(CardInstanceCount);
if (CardInstanceCount < 1) then
begin
CardSet.Free;
CardMask.Free;
CardBack.Free;
CardWork.Free;
end;
inherited Destroy;
end;
procedure TCard.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited SetBounds(ALeft, ATop, DEFAULT_CARD_WIDTH, DEFAULT_CARD_HEIGHT);
end;
procedure TCard.Paint;
var
CardPaintMode: Integer;
begin
BitBlt(CardWork.Canvas.Handle, 0, 0, DEFAULT_CARD_WIDTH, DEFAULT_CARD_HEIGHT,
Canvas.Handle, 0, 0, SRCCOPY);
if (FSelectedCard = False) then
begin
CardPaintMode := SRCPAINT;
BitBlt(CardWork.Canvas.Handle, 0, 0, DEFAULT_CARD_WIDTH, DEFAULT_CARD_HEIGHT,
CardMask.Canvas.Handle, 0, 0, SRCAND);
end
else
begin
CardPaintMode := NOTSRCERASE;
BitBlt(CardWork.Canvas.Handle, 0, 0, DEFAULT_CARD_WIDTH, DEFAULT_CARD_HEIGHT,
CardMask.Canvas.Handle, 0, 0, SRCERASE);
end;
case FShowCard of
True:
BitBlt(CardWork.Canvas.Handle, 0, 0, DEFAULT_CARD_WIDTH, DEFAULT_CARD_HEIGHT,
CardSet.Canvas.Handle, (LongInt(FCardValue) * DEFAULT_CARD_WIDTH),
(LongInt(FCardSuit) * DEFAULT_CARD_HEIGHT), CardPaintMode);
False:
BitBlt(CardWork.Canvas.Handle, 0, 0, DEFAULT_CARD_WIDTH, DEFAULT_CARD_HEIGHT,
CardBack.Canvas.Handle, 0, 0, CardPaintMode);
end;
BitBlt(Canvas.Handle, 0, 0, DEFAULT_CARD_WIDTH, DEFAULT_CARD_HEIGHT,
CardWork.Canvas.Handle, 0, 0, SRCCOPY);
end;
procedure TCard.SetCardValue(CardValue: TCardValue);
begin
FCardValue := CardValue;
case FShowCard of
True:
Repaint;
False:;
end;
end;
procedure TCard.SetSelectedCard(CardSelectedValue: Boolean);
begin
if (FSelectedCard <> CardSelectedValue) then
begin
FSelectedCard := CardSelectedValue;
Repaint;
end;
end;
procedure TCard.SetShowCard(CardShowValue: Boolean);
begin
if (FShowCard <> CardShowValue) then
begin
FShowCard := CardShowValue;
Repaint;
end;
end;
procedure TCard.SetCardSuit(CardSuitValue: TCardSuit);
begin
FCardSuit := CardSuitValue;
case FShowCard of
True:
Repaint;
False:;
end;
end;
function TCardValueEditor.GetAttributes: TPropertyAttributes;
begin
Result := [paValueList];
end;
procedure TCardValueEditor.GetValues(Proc: TGetStrProc);
begin
Proc('Ace');
Proc('Two');
Proc('Three');
Proc('Four');
Proc('Five');
Proc('Six');
Proc('Seven');
Proc('Eight');
Proc('Nine');
Proc('Ten');
Proc('Jack');
Proc('Queen');
Proc('King');
end;
function TCardValueEditor.GetValue: string;
begin
case GetOrdValue of
LongInt(Ace): Result := 'Ace';
LongInt(Two): Result := 'Two';
LongInt(Three): Result := 'Three';
LongInt(Four): Result := 'Four';
LongInt(Five): Result := 'Five';
LongInt(Six): Result := 'Six';
LongInt(Seven): Result := 'Seven';
LongInt(Eight): Result := 'Eight';
LongInt(Nine): Result := 'Nine';
LongInt(Ten): Result := 'Ten';
LongInt(Jack): Result := 'Jack';
LongInt(Queen): Result := 'Queen';
LongInt(King): Result := 'King';
end;
end;
procedure TCardValueEditor.SetValue(const Value: string);
begin
if (Value = 'Ace') then SetOrdValue(LongInt(Ace));
if (Value = 'Two') then SetOrdValue(LongInt(Two)) ;
if (Value = 'Three') then SetOrdValue(LongInt(Three)) ;
if (Value = 'Four') then SetOrdValue(LongInt(Four)) ;
if (Value = 'Five') then SetOrdValue(LongInt(Five)) ;
if (Value = 'Six') then SetOrdValue(LongInt(Six)) ;
if (Value = 'Seven') then SetOrdValue(LongInt(Seven)) ;
if (Value = 'Eight') then SetOrdValue(LongInt(Eight)) ;
if (Value = 'Nine') then SetOrdValue(LongInt(Nine)) ;
if (Value = 'Ten') then SetOrdValue(LongInt(Ten)) ;
if (Value = 'Jack') then SetOrdValue(LongInt(Jack)) ;
if (Value = 'Queen') then SetOrdValue(LongInt(Queen)) ;
if (Value = 'King') then SetOrdValue(LongInt(King)) ;
end;
procedure Register;
begin
RegisterPropertyEditor(TypeInfo(TCardValue), TCard, '', TCardValueEditor);
RegisterComponents('Cards', [TCard]);
end;
initialization
{$I tcard.lrs}
end.