-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVGSolitaire.pck.st
1308 lines (1044 loc) · 39.3 KB
/
SVGSolitaire.pck.st
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
'From Cuis 5.0 [latest update: #4895] on 1 October 2021 at 3:00:37 pm'!
'Description FreeCell and Klondike Solitaire using SVG Card Images'!
!provides: 'SVGSolitaire' 1 18!
!requires: 'Cuis-Base' 50 4894 nil!
!requires: 'Morphic-Games-Solitaire' 1 112 nil!
!requires: 'SVG' 1 13 nil!
!requires: 'Morphic-Misc1' 1 186 nil!
SystemOrganization addCategory: 'SVGSolitaire'!
!classDefinition: #SVGFreeCell category: 'SVGSolitaire'!
FreeCell subclass: #SVGFreeCell
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'SVGSolitaire'!
!classDefinition: 'SVGFreeCell class' category: 'SVGSolitaire'!
SVGFreeCell class
instanceVariableNames: ''!
!classDefinition: #PickASVGCardImagePallet category: 'SVGSolitaire'!
ImagePickerPanel subclass: #PickASVGCardImagePallet
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'SVGSolitaire'!
!classDefinition: 'PickASVGCardImagePallet class' category: 'SVGSolitaire'!
PickASVGCardImagePallet class
instanceVariableNames: ''!
!classDefinition: #SVGCardMorph category: 'SVGSolitaire'!
MovableMorph subclass: #SVGCardMorph
instanceVariableNames: 'cardNumber grabPreowner isFaceDown'
classVariableNames: 'Backs CardExtent ColorNames DropShadowMorph FaceNames Fronts Jokers SuitNames UpperLeftOffset'
poolDictionaries: ''
category: 'SVGSolitaire'!
!classDefinition: 'SVGCardMorph class' category: 'SVGSolitaire'!
SVGCardMorph class
instanceVariableNames: ''!
!classDefinition: #SVGCardTests category: 'SVGSolitaire'!
TestCase subclass: #SVGCardTests
instanceVariableNames: 'jackOfClubs aceOfSpades twoOfDiamonds threeOfHearts fiveOfHearts'
classVariableNames: ''
poolDictionaries: ''
category: 'SVGSolitaire'!
!classDefinition: 'SVGCardTests class' category: 'SVGSolitaire'!
SVGCardTests class
instanceVariableNames: ''!
!classDefinition: #SVGCardDeck category: 'SVGSolitaire'!
Object subclass: #SVGCardDeck
instanceVariableNames: 'cards index seed'
classVariableNames: ''
poolDictionaries: ''
category: 'SVGSolitaire'!
!classDefinition: 'SVGCardDeck class' category: 'SVGSolitaire'!
SVGCardDeck class
instanceVariableNames: ''!
!SVGFreeCell commentStamp: '<historical>' prior: 0!
FreeCell Solitare using SVGCardMorphs
@@@REVISIT:
Could parameterize Card Class rather than subclass
Subclass for now to prototype..!
!PickASVGCardImagePallet commentStamp: '<historical>' prior: 0!
I am just a way of getting an initializedInstance from the New Morph.. menu..
See my class side.!
!SVGCardMorph commentStamp: '<historical>' prior: 0!
I have SVG front and back morphs as submorphs.
I hide and show to "flip" between them.
Currently, there is one universal size used for all cards. [@@REVISIT@@]
CardMorph the: #Ace of: #Clubs.
CardMorph the: 3 of: #Hearts.
IVARS:
cardNumber: 1..52
grabPreowner: When grabbed from CardContainer, goes back here on drop rejection
Structure:
SuitNames Array of Symbol -- #(Clubs Diamonds Hearts Spades)
ColorNames Array of Symbol -- #(Red Black)
FaceNames Array of Symbol -- #(Ace Two Three ... Ten Jack Queen King)
Fronts -- 52 card SVG images
Backs -- back SVG images [e.g. might use different backs for different decks]
There is a total ordering on cards
A A A A 2 2 2 2 3 3 3 3 4 4
C D H S C D H S C D H S C D ...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 ..
The SVG morph proto for the Three of Hearts is in Fronts: at 11 (its cardNumber + 1).
See faceNumber and suitNumber instance methods for the math.
There are 54 card images (two jokers, a red and a black).
!
!SVGCardTests commentStamp: '<historical>' prior: 0!
"Unit tests for SVGCardMorph and related classes"!
!SVGCardDeck commentStamp: '<historical>' prior: 0!
I am a 52 Card Deck you can shuffle and deal from.
aDeck shuffle.
aDeck dealACard.!
!PickASVGCardImagePallet class methodsFor: 'instance creation' stamp: 'KenD 9/30/2021 11:06:44'!
initializedInstance
"Answer a pallet of SVG image selections"
| deck sortedCollection |
deck := SVGCardDeck new initialize.
sortedCollection := OrderedCollection new: (deck cards size ).
deck cards do: [ :card |
sortedCollection addLast: (card name)->card
].
^ self
privateBuildPanelLabel: 'Pick a SVG card image'
collection: sortedCollection
transducer: [ :cardImage |
cardImage scale: SVGCardMorph defaultScale
]
! !
!SVGCardMorph methodsFor: 'accessing' stamp: 'KenD 9/11/2021 15:34:11'!
name
"e.g. Jack of Hearts"
^((self face) asString), ' of ', ((self suit) asString)! !
!SVGFreeCell methodsFor: 'dropping/grabbing' stamp: 'KenD 9/12/2021 09:13:21'!
column: columnMorph wantsDropOf: aCardOrColumnMorph
"Answer true if Column is empty or if (lowest)
card is opposit color and one higher in face value."
"Ignore other game card instances"
(self isMyCard: aCardOrColumnMorph) ifFalse: [^false].
(aCardOrColumnMorph isKindOf: SVGCardMorph)
ifTrue: [| topCard |
topCard := columnMorph topCard.
topCard isNil
ifTrue: [^true]
ifFalse: [ ^ (aCardOrColumnMorph hasFaceOneLessThan: topCard)
& (topCard hasDifferentColorThan: aCardOrColumnMorph)]]
ifFalse: [
Error signal: '@@FIXME: implement for column drops!!'.
^ false]! !
!SVGFreeCell methodsFor: 'dropping/grabbing' stamp: 'KenD 9/12/2021 09:14:47'!
container: aContainerMorph wantsDropOf: aCardOrColumnMorph
"Answer true if legal drop by FreeCell game rules"
| dropObjectClass |
"Ignore other game card instances"
(self isMyCard: aCardOrColumnMorph) ifFalse: [^false].
dropObjectClass := aCardOrColumnMorph class.
(dropObjectClass = CardColumnMorph) | (aCardOrColumnMorph isKindOf: SVGCardMorph)
ifFalse: [ ^false ]
ifTrue: [ aContainerMorph class = CardSpareMorph
ifTrue: [^ self spare: aContainerMorph wantsDropOf: aCardOrColumnMorph]
ifFalse: [aContainerMorph class = CardPileMorph
ifTrue: [^ self pile: aContainerMorph wantsDropOf: aCardOrColumnMorph]
ifFalse: [aContainerMorph class = CardColumnMorph
ifTrue: [^ self column: aContainerMorph wantsDropOf: aCardOrColumnMorph]
ifFalse: [^ false]]]]! !
!SVGFreeCell methodsFor: 'dropping/grabbing' stamp: 'KenD 9/12/2021 09:15:15'!
pile: pileMorph wantsDropOf: aCardOrColumnMorph
"Answer true if Pile is empty and Card is Ace or if
card is same suit and one higher in face value."
| card |
card := (aCardOrColumnMorph isKindOf: SVGCardMorph)
ifTrue: [aCardOrColumnMorph]
ifFalse: [aCardOrColumnMorph singletonCard].
"Ignore other game card instances"
(self isMyCard: card) ifFalse: [^false].
card isNil
ifTrue: [ ^nil ]
ifFalse: [ pileMorph isEmpty
ifTrue: [ ^ card face == #Ace ]
ifFalse: [ | topCard |
topCard := pileMorph topCard.
^ (topCard hasFaceOneLessThan: card) & (topCard hasSameSuitAs: card)]]! !
!SVGFreeCell methodsFor: 'dropping/grabbing' stamp: 'KenD 9/12/2021 09:15:31'!
spare: spareMorph wantsDropOf: aCardOrColumnMorph
"Answer true if Spare is empty and Card is singleton."
| card |
aCardOrColumnMorph hideDropShadow.
card := (aCardOrColumnMorph isKindOf: SVGCardMorph)
ifTrue: [aCardOrColumnMorph]
ifFalse: [aCardOrColumnMorph singletonCard].
"Ignore other game card instances"
(self isMyCard: card) ifFalse: [^false].
spareMorph isEmpty
ifTrue: [^ (card isNil not) and: [card submorphs size = 0]] "only holds 1 card"
ifFalse: [^ false]! !
!SVGFreeCell methodsFor: 'fileIn/out' stamp: 'KenD 9/12/2021 09:20:14'!
restoreGameFromDataStream: aDataStream
"Given an open DataStream, read and restore game state."
"Game state consists of information to reconsitiute the deck, spares, piles, and columns."
"NYI: undo stack save/restore."
| stateArray |
stateArray := aDataStream next.
(stateArray at: 1) == #SVGFreeCell
ifFalse: [ Error signal: 'Not a saved SVG FreeCell Game!!' ]
ifTrue: [
| columnInfo pileInfo spareInfo undoInfo |
deck := SVGCardDeck new initializeFromArray: (stateArray at: 3).
deck setScale: self scale.
columnInfo := stateArray at: 5.
pileInfo := stateArray at: 7.
spareInfo := stateArray at: 9.
undoInfo := stateArray at: 11.
1 to: NumColumns
do: [ :idx |
(self columns at: idx) restoreFromArray: (columnInfo at: idx) givenDeck: self deck ].
1 to: NumSpares
do: [ :idx |
(self spares at: idx) restoreFromArray: (spareInfo at: idx) givenDeck: self deck ].
1 to: NumPiles
do: [ :idx |
(self piles at: idx) restoreFromArray: (pileInfo at: idx) givenDeck: self deck ].
undoInfo isNil
ifTrue: [ self makeUndoStackEmpty ]
ifFalse: [ Error signal: 'FIXME: Undo save/restore NYI' ].
]! !
!SVGFreeCell methodsFor: 'fileIn/out' stamp: 'KenD 9/12/2021 09:19:27'!
saveGameToDataStream: aDataStream
"Given an open DataStream, save the current game state."
"Game state consists of information to reconsitiute the deck, spares, piles, and columns."
"NYI: undo stack save/restore."
| someData |
someData := { #SVGFreeCell.
#CardDeck.
self deck saveStateToArray.
#Columns.
self columns
collect: [ :container | container saveStateToArray].
#Piles.
self piles
collect: [ :container | container saveStateToArray].
#Spares.
self spares
collect: [ :container | container saveStateToArray].
#Undo.
nil. "FIXME"
}.
aDataStream nextPut: someData.
! !
!SVGFreeCell methodsFor: 'geometry' stamp: 'KenD 9/12/2021 09:21:39'!
reScale
"reScale self based on new font sizes"
self notYetImplemented ! !
!SVGFreeCell methodsFor: 'initialization' stamp: 'KenD 9/12/2021 09:23:34'!
defaultExtent
| cardWidth cardHeight tableExtent |
cardWidth := SVGCardMorph morphWidth.
cardHeight := SVGCardMorph morphHeight.
"buttonArea height 50; labelArea height 40; pilesArea CardHeight+30"
tableExtent := (cardWidth + self columnSeparation * 4)@(cardHeight * 6 + 120).
^ tableExtent.! !
!SVGFreeCell methodsFor: 'initialization' stamp: 'KenD 10/1/2021 12:52:28'!
initializeWithSVGCards
"Setup my layout. Across the top are 4 Spares and 4 Piles,
then a block of 8 Columns, then a Button area."
super initialize.
deck := SVGCardDeck new initialize.
self makeLabelArea: 'FreeCell';
makePilesArea;
makeColumnArea;
makeButtonArea.
self flag: #reVisit. "Card Class into Table ??"
piles do: [ :p | p cardClass: SVGCardMorph ].
spares do: [ :s | s cardClass: SVGCardMorph ].
columns do: [ :c | c cardClass: SVGCardMorph ].
self
dealCards;
"reScale;"
morphExtent: self minimumExtent;
yourself! !
!SVGFreeCell methodsFor: 'initialization' stamp: 'KenD 9/30/2021 14:11:49'!
initializeWithScale: aScale
self flag: #FixMe. "Ignoring scale"
scale := 1.
super initialize.
deck := SVGCardDeck new initializeWithScale: self scale.
self makeLabelArea: 'SVG FreeCell';
makePilesArea;
makeColumnArea;
makeButtonArea.
piles do: [ :p | p cardClass: SVGCardMorph ].
spares cardClass: SVGCardMorph.
columns do: [ :c | c cardClass: SVGCardMorph ].
self
dealCards;
"reScale;"
morphExtent: self minimumExtent;
yourself! !
!SVGFreeCell class methodsFor: 'instance creation' stamp: 'KenD 10/1/2021 12:16:21'!
newSVGGame
"Answer a new instance"
"
SVGFreeCell newSVGGame.
"
^ (self newColumn initializeWithSVGCards) openInWorld! !
!PickASVGCardImagePallet class methodsFor: 'new-morph participation' stamp: 'KenD 9/18/2021 07:56:33'!
includeInNewMorphMenu
"Return true for all classes that can be instantiated from the menu"
^ true! !
!SVGCardMorph methodsFor: 'accessing' stamp: 'KenD 9/11/2021 15:32:52'!
cardColor
"Answer a symbol denoting the color of this card"
"clubs diamonds hearts spades => black red red black"
| suitNum |
suitNum := self suitNumber.
(suitNum = 1)
ifTrue: [^ #Black]
ifFalse: [(suitNum = 4)
ifTrue: [ ^#Black ]
ifFalse: [ ^#Red ]].! !
!SVGCardMorph methodsFor: 'accessing' stamp: 'KenD 9/11/2021 15:33:43'!
face
"Answer a symbol denoting the face value of this card"
^FaceNames at: (self faceNumber).! !
!SVGCardMorph methodsFor: 'accessing' stamp: 'KenD 9/11/2021 15:34:39'!
suit
"Answer a symbol denoting the suit of this card"
^SuitNames at: (self suitNumber).! !
!SVGCardMorph methodsFor: 'accessing' stamp: 'KenD 9/11/2021 15:34:49'!
suitColor
"Answer a symbol denoting the color of this card"
"clubs diamonds hearts spades => black red red black"
| suitNum |
suitNum := self suitNumber.
(suitNum = 1)
ifTrue: [^ #Black]
ifFalse: [(suitNum = 4)
ifTrue: [ ^#Black ]
ifFalse: [ ^#Red ]].! !
!SVGCardMorph methodsFor: 'comparing' stamp: 'KenD 9/11/2021 15:50:31'!
< aCard
"Absolute ordering over all cards in a 52 card deck."
(aCard isKindOf: SVGCardMorph)
ifTrue: [ ^ (self cardNumber) < (aCard cardNumber) ]
ifFalse: [ ^false ].! !
!SVGCardMorph methodsFor: 'comparing' stamp: 'KenD 9/11/2021 15:50:11'!
= aCard
(aCard isKindOf: SVGCardMorph)
ifTrue: [ ^ (self cardNumber) = (aCard cardNumber) ]
ifFalse: [ ^false ].! !
!SVGCardMorph methodsFor: 'comparing' stamp: 'KenD 9/11/2021 15:50:43'!
> aCard
(aCard isKindOf: SVGCardMorph)
ifTrue: [ ^ (self cardNumber) > (aCard cardNumber) ]
ifFalse: [ ^false ].! !
!SVGCardMorph methodsFor: 'comparing' stamp: 'KenD 9/11/2021 15:47:59'!
hasDifferentColorThan: anotherCard
"Answer true if anotherCard has the same color"
(self hasSameColorAs: anotherCard)
ifTrue: [ ^false ]
ifFalse: [ ^true ].! !
!SVGCardMorph methodsFor: 'comparing' stamp: 'KenD 9/11/2021 15:48:27'!
hasFaceOneLessThan: anotherCard
"Answer true if anotherCard has the same face value plus one"
(anotherCard isKindOf: SVGCardMorph)
ifTrue: [ ^(self faceNumber + 1) == (anotherCard faceNumber) ]
ifFalse: [ Error signal: 'Expected a SVGCardMorph, got: ', (anotherCard asString) ].! !
!SVGCardMorph methodsFor: 'comparing' stamp: 'KenD 9/11/2021 15:48:54'!
hasSameColorAs: anotherCard
"Answer true if anotherCard has the same color"
(anotherCard isKindOf: SVGCardMorph)
ifTrue: [ ^(self suitColor) == (anotherCard suitColor) ]
ifFalse: [ Error signal: 'Expected a SVGCardMorph, got: ', (anotherCard asString) ].! !
!SVGCardMorph methodsFor: 'comparing' stamp: 'KenD 9/11/2021 15:49:12'!
hasSameFaceAs: anotherCard
"Answer true if anotherCard has the same face value"
(anotherCard isKindOf: SVGCardMorph)
ifTrue: [ ^(self face) == (anotherCard face) ]
ifFalse: [ Error signal: 'Expected a SVGCardMorph, got: ', (anotherCard asString) ].! !
!SVGCardMorph methodsFor: 'comparing' stamp: 'KenD 9/11/2021 15:49:30'!
hasSameSuitAs: anotherCard
"Answer true if anotherCard has the same suit"
(anotherCard isKindOf: SVGCardMorph)
ifTrue: [ ^(self suit) == (anotherCard suit) ]
ifFalse: [ Error signal: 'Expected a SVGCardMorph, got: ', (anotherCard asString) ].! !
!SVGCardMorph methodsFor: 'comparing' stamp: 'KenD 9/11/2021 15:49:50'!
hash
"because = is implemented"
^ (self cardNumber) hash! !
!SVGCardMorph methodsFor: 'display' stamp: 'KenD 9/11/2021 15:45:06'!
flip
"Turn the card over"
(isFaceDown := isFaceDown not)
ifTrue: [ "make face down"
(submorphs at: 1) hide. "face"
(submorphs at: 2) show. "back"
]
ifFalse: [ "make face up"
(submorphs at: 1) show. "face"
(submorphs at: 2) hide. "back"
]! !
!SVGCardMorph methodsFor: 'display' stamp: 'KenD 9/11/2021 15:43:59'!
makeFaceDown
self isFaceDown ifFalse: [ self flip ]! !
!SVGCardMorph methodsFor: 'display' stamp: 'KenD 9/11/2021 15:38:46'!
makeFaceUp
self isFaceUp ifFalse: [ self flip ]! !
!SVGCardMorph methodsFor: 'drawing' stamp: 'KenD 9/11/2021 07:11:24'!
drawOn: aCanvas
"VectorCanvas knows how to draw me without being told.."
! !
!SVGCardMorph methodsFor: 'dropShadow' stamp: 'KenD 9/12/2021 10:25:03'!
dropShadowShown
self flag: #FixMe.
^false! !
!SVGCardMorph methodsFor: 'dropShadow' stamp: 'KenD 9/12/2021 10:24:06'!
hideDropShadow
self flag: #FixMe! !
!SVGCardMorph methodsFor: 'dropShadow' stamp: 'KenD 9/12/2021 10:24:14'!
setDropShadowMorph
self flag: #FixMe! !
!SVGCardMorph methodsFor: 'dropShadow' stamp: 'KenD 9/12/2021 10:24:22'!
showDropShadow
self flag: #FixMe! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/30/2021 12:31:47'!
aboutToBeGrabbedBy: aHand
"I'm about to be grabbed by the hand. If other cards are above me in a
Card Container, then move them from the Container to being submorphs of me"
self rememberPreowner.
(self owner isKindOf: CardDeckMorph) | (self owner isNil)
ifFalse: [
"
Transcript newLine; show: ( self name , ' about to be grabbed by ' , aHand name ).
"
super aboutToBeGrabbedBy: aHand.
self collectCardsAboveMe;
showDropShadow;
yourself
]
ifTrue: [^nil]
! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/30/2021 12:32:41'!
acceptDroppingMorph: aMorph event: evt
"Delegate to my owning Card Container"
(self owner isKindOf: CardContainerMorph)
ifTrue: [ ^(self owner) acceptDroppingMorph: aMorph event: evt ]
ifFalse: [ ^false ]! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/30/2021 12:32:52'!
allowsMorphDrop
"Answer whether we accept dropping morphs. By default answer false."
^ true! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/30/2021 12:32:58'!
allowsSubmorphDrag
"Answer whether our morphs can just be grabbed with the hand, instead of requiring the use of the halo. By default answer false."
^ true! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 10/1/2021 13:29:59'!
collectCardsAboveMe
"I'm about to be grabbed by the hand or otherwise moved.
If other cards are above me in a Card Container, then move
them from the Container to being submorphs of me"
| idx cards |
cards _ owner isNil ifTrue: [ nil ] ifFalse: [ owner submorphs ].
cards isNil ifTrue: [ ^self ].
idx _ cards indexOf: self ifAbsent: [^ self].
idx = 1 ifTrue: [^ self].
(cards copyFrom: 1 to: idx - 1)
do: [:m | m class = self class
ifTrue: [self addMorphBack: m]].
"set my extent to include submorphs"
" (self owner isKindOf: CardColumnMorph)
ifTrue: [
self morphExtent:
(self width
@
(self height + (self submorphCount * (
self owner pixelsPerOverlap))))].
(self owner isKindOf: CardDiscardMorph)
ifTrue: [
self morphExtent:
(self width + (self submorphCount * (
CardDiscardMorph pixelsPerOverlap)))
@
(self height)]."
self redrawNeeded.! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/30/2021 12:28:51'!
forgetPreowner
grabPreowner := nil! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/30/2021 12:29:01'!
grabPreowner
^grabPreowner ! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 10/1/2021 13:12:50'!
justDroppedInto: aMorph event: anEvent
"Someone just dropped me.."
self hideDropShadow.
"Tell my container"
(self owner isKindOf: CardContainerMorph)
ifTrue: [ ^ (self owner) dropComplete ]
ifFalse: [^ false ]! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/30/2021 12:33:21'!
rejectDropMorphEvent: evt
"Drop rejected. If have submorphs, give them back to the container."
"
Transcript newLine;
show: ('>>rejectDropMorphEvent>> :' ,self name ,
', grabPreowner ' , self grabPreowner name ).
"
self hideDropShadow.
(self grabPreowner isKindOf: CardContainerMorph)
ifTrue: [self grabPreowner addCard: self.
self submorphs reverseDo: [ :m | self grabPreowner addCard: m].
(evt isNil) ifFalse: [evt wasHandled: true].
self redrawNeeded ]
ifFalse: [ super rejectDropMorphEvent: evt.].! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/11/2021 15:35:26'!
rememberPreowner
(self owner isKindOf: CardContainerMorph )
ifTrue: [grabPreowner := self owner]
ifFalse: [grabPreowner := nil] "no funny stuff.."! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/11/2021 15:36:19'!
theGrabPreowner
"Accessor"
^grabPreowner ! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/30/2021 12:33:32'!
wantsDroppedMorph: aMorph event: evt
"Ask my container."
(self owner isKindOf: CardContainerMorph)
ifTrue: [ ^(self owner) wantsDroppedMorph: aMorph event: evt ]
ifFalse: [^ false]! !
!SVGCardMorph methodsFor: 'dropping/grabbing' stamp: 'KenD 9/30/2021 12:33:44'!
wantsToBeDroppedInto: aMorph
"I want to be dropped onto a Card or a Card Container"
^ (aMorph isKindOf: CardContainerMorph)
| (aMorph isKindOf: SVGCardMorph)! !
!SVGCardMorph methodsFor: 'event handling' stamp: 'KenD 9/30/2021 12:35:35'!
click: evt localPosition: localEventPosition
"ignore this and wait until #firstClickTimedOut: arrives"
"
Transcript newLine; show: (self name , ' got #click:localPosition:').
"
(self owner isKindOf: CardDeckMorph)
ifTrue: [ self owner click: evt localPosition: localEventPosition ]
ifFalse: [ ^false ]! !
!SVGCardMorph methodsFor: 'event handling' stamp: 'KenD 9/30/2021 12:35:43'!
handlesMouseDown: evt
"
Transcript newLine; show: (self name , ' got #handlesMouseDown:').
"
^ true! !
!SVGCardMorph methodsFor: 'event handling' stamp: 'KenD 9/30/2021 12:35:54'!
processMouseDown: evt localPosition: localEventPosition
"Do nothing upon mouse-down except inform the hand to watch for a
double-click; wait until an ensuing click:, doubleClick:, or drag:
message gets dispatched"
| container containerOKsPickup |
"
Transcript newLine; show: (self name , ' got #processMouseDown:localPosition:').
"
container := self owner.
(container isKindOf: CardContainerMorph)
ifFalse: [^ self].
containerOKsPickup := (container okToPickUp: self).
containerOKsPickup | (container isKindOf: CardDeckMorph)
ifFalse: [
evt wasHandled: true. "@@ Don't let World open a menu"
^ self]
ifTrue: [
evt hand waitForClicksOrDrag: self
event: evt
dragSel: #click:localPosition:
clkSel: #click:localPosition: .
containerOKsPickup
ifTrue: [ |hand|
hand := self runningWorld activeHand.
(localEventPosition < self morphExtent )
ifTrue: [hand grabMorph: self] "Close to cursor"
ifFalse: [self aboutToBeGrabbedBy: hand. "Far from cursor"
hand attachMorph: self].
evt wasHandled: true
]
]
! !
!SVGCardMorph methodsFor: 'geometry' stamp: 'KenD 9/11/2021 07:17:09'!
cardExtent
^self class cardExtent! !
!SVGCardMorph methodsFor: 'geometry' stamp: 'KenD 9/30/2021 12:37:09'!
height
^self class cardExtent y! !
!SVGCardMorph methodsFor: 'geometry' stamp: 'KenD 9/11/2021 15:46:04'!
minimumExtent
"Same as maximum extent"
^ self cardExtent! !
!SVGCardMorph methodsFor: 'geometry' stamp: 'KenD 9/18/2021 10:01:23'!
morphExtent
^self class cardExtent! !
!SVGCardMorph methodsFor: 'geometry' stamp: 'KenD 9/22/2021 13:07:47'!
morphHeight
^self class cardExtent y! !
!SVGCardMorph methodsFor: 'geometry' stamp: 'KenD 9/22/2021 13:07:57'!
morphWidth
^self class cardExtent x! !
!SVGCardMorph methodsFor: 'geometry' stamp: 'KenD 9/29/2021 14:46:48'!
rotationCenter
^ self class rotationCenter ! !
!SVGCardMorph methodsFor: 'geometry' stamp: 'KenD 9/11/2021 07:26:36'!
updateExtent
| newExtent |
newExtent := self class cardExtent.
self allSubmorphsDo: [ :s | s fitInto: newExtent ]! !
!SVGCardMorph methodsFor: 'geometry' stamp: 'KenD 9/30/2021 12:37:17'!
width
^self class cardExtent x! !
!SVGCardMorph methodsFor: 'geometry testing' stamp: 'KenD 9/30/2021 12:38:21'!
submorphsMightProtrude
^true! !
!SVGCardMorph methodsFor: 'initialization' stamp: 'KenD 9/30/2021 11:02:38'!
withCardNumber: cardNum
"See my class comment on card numbering"
| face back |
"NB: Must set cardNumber before #initialize"
cardNumber := cardNum.
super initialize.
"self morphExtent: SVGCardMorph cardExtent."
grabPreowner := nil.
face := self class faceAt: cardNumber.
back := self class defaultBack.
self addMorphFront: face.
self addMorphBack: back.
back hide.
face show.
isFaceDown := false.
self setProperty: #balloonText toValue: self name.
^ self
! !
!SVGCardMorph methodsFor: 'testing' stamp: 'KenD 9/11/2021 15:39:17'!
isBlack
^(self suitColor) == #Black! !
!SVGCardMorph methodsFor: 'testing' stamp: 'KenD 9/11/2021 15:39:26'!
isCardMorph
^true! !
!SVGCardMorph methodsFor: 'testing' stamp: 'KenD 9/11/2021 15:39:33'!
isFaceDown
^isFaceDown! !
!SVGCardMorph methodsFor: 'testing' stamp: 'KenD 9/11/2021 15:39:41'!
isFaceUp
^ self isFaceDown not! !
!SVGCardMorph methodsFor: 'testing' stamp: 'KenD 9/11/2021 15:39:49'!
isRed
^(self suitColor) == #Red! !
!SVGCardMorph methodsFor: 'private' stamp: 'KenD 9/11/2021 15:33:28'!
cardNumber
"private"
^cardNumber! !
!SVGCardMorph methodsFor: 'private' stamp: 'KenD 9/11/2021 15:38:01'!
faceNumber
"Private; Answer 1..13"
^((cardNumber // 4) + 1)! !
!SVGCardMorph methodsFor: 'private' stamp: 'KenD 9/30/2021 12:53:21'!
saveImageToFile: aFileName
"NB: overrited file without question"
(self imageForm: 32) writeJPEGfileNamed:aFileName! !
!SVGCardMorph methodsFor: 'private' stamp: 'KenD 9/11/2021 15:37:07'!
suitNumber
"Private: Answer 1..4"
^((cardNumber rem: 4) + 1)! !
!SVGCardMorph class methodsFor: 'accessing' stamp: 'KenD 9/11/2021 15:23:49'!
backAt: index
"Must copy prototype else try to be submorph of multple supers"
^(Backs at: index) duplicate! !
!SVGCardMorph class methodsFor: 'accessing' stamp: 'KenD 9/18/2021 08:00:00'!
backs
^Backs! !
!SVGCardMorph class methodsFor: 'accessing' stamp: 'KenD 9/29/2021 14:36:50'!
cardExtent
"Set in class initializer"
^ CardExtent ! !
!SVGCardMorph class methodsFor: 'accessing' stamp: 'KenD 9/11/2021 15:27:56'!
defaultBack
"Blue back is current default"
^ self backAt: 1! !
!SVGCardMorph class methodsFor: 'accessing' stamp: 'KenD 9/12/2021 10:28:08'!
faceAt: index
"Convert zero based to 1 based index"
"Must copy prototype else try to be submorph of multple supers"
^(Fronts at: index + 1) duplicate! !
!SVGCardMorph class methodsFor: 'accessing' stamp: 'KenD 9/18/2021 07:59:52'!
fronts
^Fronts! !
!SVGCardMorph class methodsFor: 'accessing' stamp: 'KenD 9/18/2021 08:00:09'!
jokers
^Jokers! !
!SVGCardMorph class methodsFor: 'accessing' stamp: 'KenD 9/22/2021 13:10:29'!
morphHeight
^self cardExtent y! !
!SVGCardMorph class methodsFor: 'accessing' stamp: 'KenD 9/22/2021 13:10:11'!
morphWidth
^self cardExtent x! !
!SVGCardMorph class methodsFor: 'class initialization' stamp: 'KenD 9/29/2021 15:03:54'!
initialize
"
SVGCardMorph initialize.
"
| pathPrefix baseIndex |
pathPrefix := ((CodePackage installedPackages at: #'SVG')
fullFileName pathAndLocalName at: 1) ,
'/../Games/Solitaire/SVG-CardImages/'.
ColorNames := #(Red Black).
FaceNames := #(Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King).
SuitNames := #(Clubs Diamonds Hearts Spades).
Backs := {
self SVGMorphFromFile: pathPrefix, 'backs/blue.svg'.
self SVGMorphFromFile: pathPrefix, 'backs/red.svg'.
}.
CardExtent := (Backs at: 1) fullBoundsInOwner extent.
UpperLeftOffset := (Backs at: 1) fullBoundsInOwner center negated.
baseIndex := 0.
Fronts := Array new: 52.
#( #ace 2 3 4 5 6 7 8 9 10 #jack #queen #king ) do: [ :faceId |
SuitNames do: [ :suitName | | suitId |
suitId := suitName asLowercase.
baseIndex := baseIndex + 1.
Fronts at: baseIndex
put: (self SVGMorphFromFile:
pathPrefix,
'fronts/',
suitId asString , '_',
faceId asString, '.svg')
]
].
Jokers := {
self SVGMorphFromFile: pathPrefix, 'fronts/joker_black.svg'.
self SVGMorphFromFile: pathPrefix, 'fronts/joker_red.svg'.
}.
self scaleCards.! !
!SVGCardMorph class methodsFor: 'class initialization' stamp: 'KenD 9/30/2021 09:51:32'!
scaleCards
"
SVGCardMorph scaleCards.
"
| cardScale |
cardScale := self defaultScale.
Fronts do: [ :f | f scale: cardScale ].
Backs do: [ :b | b scale: cardScale ].
Jokers do: [ :j | j scale: cardScale ]. ! !
!SVGCardMorph class methodsFor: 'fileIn/Out' stamp: 'KenD 9/11/2021 12:50:14'!
SVGMorphFromFile: aPath
^self SVGMorphFromFileEntry: aPath asFileEntry! !
!SVGCardMorph class methodsFor: 'fileIn/Out' stamp: 'Install-SVG-Solitaire 9/11/2021 15:12:26'!
SVGMorphFromFileEntry: aFileEntry
| m |
m := [ (SVGMainMorph fromFile: aFileEntry) ]
on: SVGWarning
do: [ :warning | "warning print. "warning resume ].
"m fitInto: self cardExtent."
^m! !
!SVGCardMorph class methodsFor: 'geometry' stamp: 'KenD 9/11/2021 07:19:16'!
baseUnit
"Answer a measure that scales with current font selection"
^ Preferences windowTitleFont lineSpacing! !
!SVGCardMorph class methodsFor: 'geometry' stamp: 'KenD 9/29/2021 15:06:08'!
defaultScale
"Answer a scale which makes my extent about the same as a CardMorph."
^ 0.36! !
!SVGCardMorph class methodsFor: 'geometry' stamp: 'KenD 9/11/2021 07:24:34'!
resetCardExtent
"Font size changed"
| newExtent |
CardExtent := nil.
newExtent := self cardExtent.
self allInstancesDo: [ :aCard | aCard updateExtent ].
^newExtent! !
!SVGCardMorph class methodsFor: 'geometry' stamp: 'KenD 9/29/2021 14:46:18'!
rotationCenter
"Answer the Upper Left Corner (set at class initialization)"
^ UpperLeftOffset! !
!SVGCardMorph class methodsFor: 'instance creation' stamp: 'KenD 9/11/2021 16:14:34'!
the: face of: suit
"Return an Card instance"
"CardMorph the: #Jack of: #Hearts."
"CardMorph the: 11 of: #spades"
"CardMorph the: 11 of: 3."
| faceNum suitNum newInst |
(suit isSymbol)
ifTrue: [ suitNum := SuitNames indexOf: (suit capitalized) ]
ifFalse: [ (suit isInteger) & (1 <= suit) & (suit <= 4)
ifTrue: [suitNum := suit]
ifFalse: [suitNum := 0]].
(suitNum = 0)
ifTrue: [Error signal: ('bad suit: ', suit asString)].
(face isSymbol)
ifTrue: [ faceNum := FaceNames indexOf: (face capitalized) ]
ifFalse: [ (face isInteger) & (1 <= face) & (face <= 13)
ifTrue: [faceNum := face]
ifFalse: [faceNum := 0]].
(faceNum = 0)
ifTrue: [Error signal: ('bad face: ', face asString)].
"Translate between zero based cardNumber and 1 based arrays"
newInst := (super basicNew) withCardNumber: (suitNum - 1 + (4 * (faceNum - 1))).
^newInst! !
!SVGCardMorph class methodsFor: 'new-morph participation' stamp: 'KenD 9/11/2021 12:11:23'!
includeInNewMorphMenu
"Private"
^false! !
!SVGCardTests methodsFor: 'setUp/tearDown' stamp: 'KenD 9/11/2021 16:11:05'!
setUp
jackOfClubs := SVGCardMorph the: #Jack of: #Clubs.
aceOfSpades := SVGCardMorph the: #Ace of: #spades.
twoOfDiamonds := SVGCardMorph the: 2 of: #diamonds.
threeOfHearts := SVGCardMorph the: 3 of: #hearts.
fiveOfHearts := SVGCardMorph the: #five of: #hearts! !
!SVGCardTests methodsFor: 'testing' stamp: 'KenD 9/11/2021 15:55:41'!
testColor
self assert: (jackOfClubs cardColor == #Black).
self assert: (twoOfDiamonds cardColor == #Red).
self assert: (threeOfHearts cardColor == #Red).
self assert: (aceOfSpades cardColor == #Black).
self assert: (fiveOfHearts cardColor == #Red).
self assert: (jackOfClubs hasSameColorAs: aceOfSpades).
self assert: (fiveOfHearts hasSameColorAs: twoOfDiamonds).
self deny: (fiveOfHearts hasSameColorAs: aceOfSpades).
self deny: (jackOfClubs hasSameColorAs: threeOfHearts).