blib/lib/Games/Cards/Tk.pm untested

filter: Statementstmt 0.0% 0 / 173 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 262 CC 16 SCAR 19.6
CC
16
Cov
0%
card_image
20.0
get_card_by_tag
12.0
get_cardset_by_tag
12.0
CRAP 7.1
1 package Games::Cards::Tk;
2 # Pieces of this came from the freecell.tk that Michael Houghton sent me
3  
4 =head1 NAME
5  
6 Games::Cards::Tk - Package to write Tk ports for Games::Cards card games
7  
8 =head1 SYNOPSIS
9  
10 See L<Games::Cards> for all the non-GUI aspects of writing card games.
11  
12 use Games::Cards;
13 use Games::Cards::Tk;
14  
15 # Create a canvas and print background etc.
16 $My_Game->set_canvas($c); # my game will use canvas $c
17  
18 # ... do lots of things you do in Games::Cards anyway
19 # Cards' Tk images will be moved automatically!
20 $Stock->give_cards($Waste, 3);
21  
22 # Mark clicked card
23 $card = $My_Game->get_card_by_tag("current");
24 $card->mark;
25  
26 =head1 DESCRIPTION
27  
28 =head2 WARNING!!!
29  
30 This module is doubleplus alpha. It's entirely possible that large parts
31 of it will be changing as I learn more Tk, and if you try to write a game
32 that's much different from the included games, it may break. There's
33 still some stuff that needs to be better modularized, abstracted, and
34 otherwise made into good code. However, the current games seem to be pretty
35 good for a first try, and I'd like to get comments in case I'm doing
36 anything really stupid.
37  
38 =head2 Overview
39  
40 Each class in Games::Cards had a corresponding Games::Cards::Tk class.
41 The classes are meant to be exactly the same, except that the Tk ones
42 also take care of moving actual card images around the screen.
43  
44 The card images used were created by Oliver Xymoron (oxymoron@waste.org).
45  
46 =cut
47  
48 use strict;
49  
50 {
51 package Games::Cards::Tk::Game;
52 @Games::Cards::Tk::Game::ISA = qw (Games::Cards::Game);
53  
54 =head2 Class Games::Cards::Tk::Game
55  
56 This class ends up holding information - such as the canvas that the game is
57 played on, card images - and methods like finding a card given its tag.
58  
59 =over 4
60  
61 =item card_width
62  
63 =item card_height
64  
65 The size of card images
66  
67 =cut
68  
69 sub card_width { shift->{"card_width"} }
70 sub card_height { shift->{"card_height"} }
71  
72 =item load_card_images
73  
74 Loads the card images and stores them to draw later.
75  
76 =cut
77  
78 sub load_card_images {
79  
80 my $image_dir = Tk::findINC("Games/Cards/images/");
81  
82 my $self = shift;
83 my $canvas = $self->canvas;
84  
85 # Oxymoron's images are stored as two-char names.
86 # First letter is [1-9tjqka], second is [cdhs]
87 my %name_hash = (
88 "Ace" => "a",
89 10 => "t",
90 "Jack" => "j",
91 "Queen" => "q",
92 "King" => "k",
93 );
94  
95 # Load each card image
96 my $im;
97 foreach my $suit (@{$self->{"suits"}}) {
98 my $s = substr($suit,0,1);
99 foreach my $name (keys %{$self->{"cards_in_suit"}}) {
100 my $n = exists $name_hash{$name} ? $name_hash{$name} : $name;
101 my $f = $n . lc($s);
102 $im = $canvas->Photo(-file => "$image_dir/$f.gif");
103 my $key = $name.$suit;
104 $self->{"card_images"}->{$key} = $im;
105 }
106 }
107  
108 $im = $canvas->Photo(-file => "$image_dir/b.gif");
109 $self->{"card_images"}->{"back"} = $im;
110 $self->{"card_width"} = $im->width;
111 $self->{"card_height"} = $im->height;
112 }
113  
114 =item card_image
115  
116 Returns the card image associated with this card.
117  
118 =cut
119  
120 sub card_image {
121 my ($self, $card) = @_;
122 if (ref($card)) {
123 my $key = $card->name("long") . $card->suit("long");
124 if (exists ($self->{"card_images"}->{$key})) {
125 return $self->{"card_images"}->{$key};
126 } else {
127 return undef;
128 }
129 } else {
130 return undef unless $card eq "back";
131 return $self->{"card_images"}->{"back"};
132 }
133 }
134  
135 =item get_card_by_tag
136  
137 Given a tag, return the Card (on this Games' canvas) that has that tag, if any.
138  
139 =cut
140  
141 sub get_card_by_tag {
142 my ($self, $tag) = @_;
143 my $canvas = $self->canvas;
144 my @ids = $canvas->find(withtag => $tag);
145 # Find cardfront: or cardback: tag for each Id
146 my @cards = grep /^card(back|front):/, map {$canvas->gettags($_)} @ids;
147  
148 if (@cards) {
149 # TODO in fact, maybe we should allow multiple cards
150 # Actually, this will probably break if front & back have the tag!
151 warn "too many cards!" if @cards > 1;
152 my $tag = $cards[0];
153 $tag =~ s/^card(front|back)://;
154 my $card = $self->get_card_by_truename($tag);
155 return $card;
156 } else {
157 return undef;
158 }
159 }
160  
161 =item get_card_by_tag
162  
163 Given a tag, return the CardSet (on this Games' canvas) that has that tag, if
164 any.
165  
166 =cut
167  
168 sub get_cardset_by_tag {
169 my ($self, $tag) = @_;
170 my $canvas = $self->canvas;
171 my @ids = $canvas->find(withtag => $tag);
172 my @sets = grep /^set:/, map {$canvas->gettags($_)} @ids;
173  
174 if (@sets) {
175 warn "too many sets!" if @sets > 1;
176 my $tag = $sets[0];
177 $tag =~ s/^set://;
178 my $card = $self->get_cardset_by_name($tag);
179 return $card;
180 } else {
181 print "help!\n";
182 return undef;
183 }
184 }
185  
186 =item get_marked_card
187  
188 Is a card marked? If so, return it.
189  
190 =cut
191  
192 sub get_marked_card {
193 my $self = shift;
194 my $tag = "marked";
195 return $self->get_card_by_tag($tag);
196 }
197  
198 =item get_clicked_cardset
199  
200 Return the set which was clicked on. Do so by looking for the "current" tag,
201 but note that that tag may apply either to a CardSet or to a Card in that set.
202  
203 =cut
204  
205 sub get_clicked_cardset {
206 my $self = shift;
207 my $tag = "current";
208 if (defined (my $card = $self->get_card_by_tag($tag))) {
209 return $card->owning_cardset;
210 } else {
211 return $self->get_cardset_by_tag($tag);
212 }
213 }
214  
215 =item canvas
216  
217 =item set_canvas(Canvas)
218  
219 Return/set the Tk::Canvas associated with this Game
220  
221 =back
222  
223 =cut
224  
225 sub canvas { return shift->{"canvas"}; }
226 sub set_canvas {
227 my ($game, $canvas) = @_;
228 $game->{"canvas"} = $canvas;
229 }
230  
231  
232 } # end package Games::Cards::Tk::Game
233  
234 ###############################################################################
235  
236 {
237 package Games::Cards::Tk::Card;
238 @Games::Cards::Tk::Card::ISA = qw(Games::Cards::Card);
239  
240 =head2 Class Games::Cards::Tk::Card
241  
242 A Card is represented in GC::Tk as two rectangles, the front and back,
243 which are always moved around together. The card is "turned over" by
244 raising the front or back rectangle (but the face_up/face_down methods
245 do that automatically for you).
246  
247 Lots of methods are basically the same as Games::Cards::Card methods. We
248 just have to add some GUI changes. But there are also some Tk-specific
249 methods.
250  
251 =over 4
252  
253 =cut
254  
255 sub face_up {
256 my $self = shift;
257 $self->SUPER::face_up; # do GC::Card::face_up stuff
258 $self->redraw;
259 }
260  
261 sub face_down {
262 my $self = shift;
263 $self->SUPER::face_down; # do GC::Card::face_up stuff
264 $self->redraw;
265 }
266  
267 =item Tk_truename
268  
269 This returns a Tk tag that's guaranteed to belong to just one Card. (However,
270 note this tag will include the card's front and back rectangles.)
271  
272 Tk_truename_front and Tk_truename_back return tags that will access just
273 the front or back image.
274  
275 =cut
276  
277 # A tag that's guaranteed to return just one card (and its back!)
278 sub Tk_truename {
279 my $self = shift;
280 return "card:" . $self->truename;
281 }
282 # A tag that's guaranteed to return just one card front
283 sub Tk_truename_front {
284 my $self = shift;
285 return "cardfront:" . $self->truename;
286 }
287 # A tag that's guaranteed to return just one card back
288 sub Tk_truename_back {
289 my $self = shift;
290 return "cardback:" . $self->truename;
291 }
292
293
294 =item draw
295  
296 Draw a card for the first time. Note that this draws the front and back
297 rectangle. The card is placed at 0,0.
298  
299 =cut
300  
301 sub draw {
302 my $card = shift;
303 my @tags;
304 my $cname = $card->Tk_truename;
305 push @tags, "card", $cname;
306  
307 my $game = &Games::Cards::Game::current_game;
308 my $canvas = $game->canvas;
309 my $id = $canvas->createImage(
310 0,0,
311 -anchor => 'nw',
312 -image => $game->card_image($card),
313 -tags => [@tags, "cardfront", $card->Tk_truename_front],
314 );
315  
316 # now create back of card
317 $id = $canvas->createImage(
318 0,0,
319 -anchor => 'nw',
320 -image => $game->card_image("back"),
321 -tags => [@tags, 'cardback', $card->Tk_truename_back],
322 );
323 } # end sub Games::Cards::Tk::Card::draw
324
325 =item mark
326  
327 Mark a card. This is currently done by placing a black rectangle around
328 it.
329  
330 =cut
331  
332 sub mark {
333 my $self = shift;
334 my $game = &Games::Cards::Game::current_game;
335 my $canvas = $game->canvas;
336 # Mark front or back of card, whichever's showing. (The front & back
337 # are guaranteed to be in the same place. This just makes it easier
338 # for clicking & stuff.)
339 my $cname = $self->is_face_up ?
340 $self->Tk_truename_front :
341 $self->Tk_truename_back;
342 $canvas->addtag("marked", withtag => $cname);
343  
344 # Put a rectangle around the marked card
345 $canvas->createRectangle($canvas->bbox($cname),
346 -outline => "black",
347 -width => 3,
348 -tags => ["outline"],
349 );
350 #$canvas->itemconfigure($cname, -fill => '#dddddd');
351 }
352  
353 =item unmark
354  
355 Unmark a card that was marked with the "mark" method.
356  
357 =cut
358  
359 sub unmark {
360 my $self = shift;
361 my $game = &Games::Cards::Game::current_game;
362 my $canvas = $game->canvas;
363 my $cname = $self->is_face_up ?
364 $self->Tk_truename_front :
365 $self->Tk_truename_back;
366 $canvas->dtag($cname, "marked");
367 # TODO if we can select > 1 card, this will be wrong
368 $canvas->delete("outline");
369 }
370  
371 =item place(X, Y)
372  
373 Put a Card's images at X, Y.
374  
375 =cut
376  
377 sub place {
378 my ($self, $x, $y) = @_;
379 my $game = &Games::Cards::Game::current_game;
380 my $canvas = $game->canvas;
381 my $cardid = $self->Tk_truename;
382 my @fromloc = $canvas->bbox($cardid);
383 $canvas->move($cardid, $x-$fromloc[0], $y-$fromloc[1]);
384 $canvas->Subwidget("canvas")->raise($cardid);
385 }
386  
387 =item redraw
388  
389 Redraw (i.e. raise) the card & make sure you're showing front/back correctly.
390  
391 =back
392  
393 =cut
394  
395 sub redraw {
396 my $self = shift;
397 my $game = &Games::Cards::Game::current_game;
398 my $canvas = $game->canvas;
399 # We might call this method before even creating a canvas. E.g., it
400 # gets called by face_up, which might be called during game init.
401 return unless defined $canvas;
402 # Should card front or back be on top?
403 my ($front, $back) = ($self->Tk_truename_front, $self->Tk_truename_back);
404 my @order = $self->is_face_up ? ($front, $back) : ($back, $front);
405 $canvas->Subwidget("canvas")->raise(@order);
406 }
407  
408 } # end package Games::Cards::Tk::Card
409  
410 ###############################################################################
411  
412 {
413 package Games::Cards::Tk::Deck;
414 @Games::Cards::Tk::Deck::ISA =
415 qw (Games::Cards::Tk::Queue Games::Cards::Deck);
416  
417 =head2 Class Games::Cards::Tk::Deck
418  
419 This class exists but isn't terribly interesting. The main point is that
420 by calling this class' new instead of Games::Cards::Deck::new, you
421 automatically get a deck filled with Games::Cards::Tk::Cards instead of
422 regular cards.
423  
424 =cut
425  
426 # This is terrible coding! However, I need to make ISA have Tk methods first,
427 # so that we try using Tk methods before others. Yet, we *don't* want to
428 # use GC::Tk::Queue::new. Nonetheless, there's probably a better way to do it.
429 sub new {
430 Games::Cards::Deck::new(@_);
431 }
432 } # end package Games::Cards::Tk::Deck
433  
434 {
435 package Games::Cards::Tk::CardSet;
436 @Games::Cards::Tk::CardSet::ISA = qw(Games::Cards::CardSet);
437  
438 =head2 Class Games::Cards::Tk::CardSet
439  
440 This class has extra methods to do Tk stuff to CardSets, i.e. drawing
441 columns, rows, piles, hands of cards.
442  
443 There are a few extra fields in the Tk version of the class:
444  
445 =over 4
446  
447 =item delta_x
448  
449 x distance between right side of one card and the next in the Set. 0 if you
450 want the cards to totally overlap, some number of pixels smaller than a card
451 if you want them to overlap some, larger than cardsize if you want them
452 to not overlap at all.
453  
454 =item border_x
455  
456 A column may be slightly wider/higher than the cards in it, for example.
457  
458 =back
459  
460 Also delta_y and border_y. Fields are changed by the "attributes" method.
461  
462 =over 4
463  
464 =cut
465  
466 # Extra fields for Tk CardSets
467 #
468 sub new {
469 my $a = shift;
470 my $class = ref($a) || $a;
471 (my $non_Tk = $class) =~ s/Tk::// or die "weird class $class!\n";
472 my $self = $non_Tk->new(@_); # Call the non-Tk new sub
473  
474 # Now add some Tk attributes
475 $self->{"delta_x"} = 0;
476 $self->{"delta_y"} = 0;
477 $self->{"border_x"} = 0;
478 $self->{"border_y"} = 0;
479  
480 # Now bless it to the Tk class
481 bless $self, $class;
482 }
483  
484 =item attributes(HASHREF)
485  
486 This is a copout way of setting a bunch of CardSet attributes in one shot.
487 Settable attributes include: delta_x/y and border_x/y. Hashref's keys
488 are attributes and values are things to set them to.
489  
490 =cut
491  
492 sub attributes {
493 my $self = shift;
494 # Attributes that may be changed by this sub
495 my @_changeable = qw (delta_y delta_x border_x border_y);
496 my $aref = shift;
497 foreach my $att (keys %$aref) {
498 if (grep {$att eq $_} @_changeable) {
499 $self->{$att} = $aref->{$att};
500 } else {
501 warn "not allowed to change attribute $att";
502 }
503 }
504 }
505  
506 =item redraw
507  
508 Redraw the Cards in this CardSet. This is the reason you have to set
509 things like delta_y and border_x.
510  
511 =cut
512  
513 # TODO alternatively, just draw *cards* that need to be redrawn?
514 sub redraw {
515 my $self = shift;
516  
517 my $game = &Games::Cards::Game::current_game;
518 my $canvas = $game->canvas;
519 # redraw gets called by give_cards, which may be called during initial
520 # setup before you've created the canvas. In that case, obviously
521 # you can't redraw, and in fact, it will cause errors to try.
522 return unless defined $canvas;
523  
524 my $name = $self->name;
525 my $delta_y = $self->{"delta_y"};
526 my $delta_x = $self->{"delta_x"};
527 my $border_y = $self->{"border_y"};
528 my $border_x = $self->{"border_x"};
529 my ($x, $y) = $canvas->coords("set:$name");
530 $x += $border_x;
531 $y += $border_y;
532 foreach my $card (@{$self->cards}) {
533 $card->place($x, $y);
534 $card->redraw;
535 #$card->change_set($canvas, $name); # in case it has moved
536 $y += $delta_y;
537 $x += $delta_x;
538 }
539 }
540  
541 # Act just like Games::Cards::CardSet::splice but add Tk stuff
542 sub splice {
543 my ($set, $offset, $length, $in_cards) = @_;
544 shift; # shift out $set for SUPER call
545 my $out_cards = $set->SUPER::splice(@_);
546  
547 # Splice is called twice: for splicing out & in, so we'll end up
548 # redrawing the giving & receiving set.
549 $set->redraw;
550  
551 return $out_cards;
552 } # end sub Cards::Games::splice
553  
554 } # end package Games::Cards::Tk::CardSet
555  
556 ###############################################################################
557 # Declare Tk subclass for each non-Tk Games::Cards class
558 {
559 # Note that non-Tk SUPER comes first, so that SUPER methods will use Tk
560 # parent classes if they exist
561 package Games::Cards::Tk::Queue;
562 @Games::Cards::Tk::Queue::ISA =
563 qw (Games::Cards::Tk::Pile Games::Cards::Queue);
564  
565 package Games::Cards::Tk::Stack;
566 @Games::Cards::Tk::Stack::ISA =
567 qw (Games::Cards::Tk::Pile Games::Cards::Stack);
568  
569 package Games::Cards::Tk::Pile;
570 @Games::Cards::Tk::Pile::ISA =
571 qw (Games::Cards::Tk::CardSet Games::Cards::Pile);
572  
573 package Games::Cards::Tk::Hand;
574 @Games::Cards::Tk::Hand::ISA =
575 qw (Games::Cards::Tk::CardSet Games::Cards::Hand);
576 }
577  
578 # return true to caller
579 1;