ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Gtk2-GoBoard/GoBoard.pm
Revision: 1.6
Committed: Wed Jun 25 07:07:34 2008 UTC (15 years, 10 months ago) by root
Branch: MAIN
Changes since 1.5: +1 -1 lines
Log Message:
make black makrers darker

File Contents

# User Rev Content
1 root 1.1 package Gtk2::GoBoard;
2    
3     =head1 NAME
4    
5     Gtk2::GoBoard - high quality goban widget with sound
6    
7     =head1 SYNOPSIS
8    
9     use Games::Go::SimpleBoard;
10    
11     my $goboard = newe Games::Go::SimpleBoard;
12    
13     use Gtk2::GoBoard;
14    
15     my $gtkboard = new Gtk2::GoBoard size => 19;
16    
17     # update board, makes a copy
18     $gtkboard->set_board ($goboard);
19    
20     # advanced: enable stone curser for black player, showing
21     # only valid moves
22     $gtkboard->set (cursor => sub {
23     my ($mark, $x, $y) = @_;
24    
25     $mark |= MARK_GRAYED | MARK_B
26     if $goboard->is_valid_move (COLOUR_WHITE,
27     $x, $y,
28     $ruleset == RULESET_NEW_ZEALAND);
29    
30     $mark
31     });
32    
33     # button-release and -press events pass board coordinates
34     $gtkboard->signal_connect (button_release => sub {
35     my ($button, $x, $y) = @_;
36    
37     ...
38     });
39    
40     =head1 DESCRIPTION
41    
42     This is the very first "true" Gtk2 widget written in Perl.
43    
44     The C<Gtk2::GoBoard> class works like any other widget, see the SYNOPSIS
45     for short examples of the available methods, and the L<App::IGS> and
46     L<KGS> modules for usage examples.
47    
48     Please supply a more descriptive description :)
49    
50     =head2 SOUND SUPPORT
51    
52     In addition to a graphical board widget, this module has some rudimentary
53     support for sounds.
54    
55     Playing sounds required the L<Audio::Play> module. If it isn't installed,
56     sounds will silently not being played. The module intentionally doesn't
57     depend on L<Audio::Play> as it isn't actively maintained anymore and fails
58     to install cleanly.
59    
60     =over 4
61    
62     =cut
63    
64     our $VERSION = '1.0';
65    
66     no warnings;
67     use strict;
68    
69     use Scalar::Util;
70     use POSIX qw(ceil);
71 root 1.5 use Carp ();
72 root 1.1 use Gtk2;
73 root 1.5
74 root 1.1 use Games::Go::SimpleBoard;
75    
76     use Glib::Object::Subclass
77     Gtk2::AspectFrame::,
78     properties => [
79     Glib::ParamSpec->IV (
80     "size",
81     "Board Size",
82     "The Go Board size, 2..38",
83     2, 38, 19,
84     [qw(construct-only writable readable)],
85     ),
86     Glib::ParamSpec->scalar (
87     "cursor",
88     "cursor callback",
89     "The callback that modifies the cursor mask",
90     [qw(writable readable)],
91     ),
92     ],
93     signals => {
94     "button-press" => {
95     flags => [qw/run-first/],
96     return_type => undef, # void return
97     param_types => [Glib::Int::, Glib::Int::, Glib::Int::], # instance and data are automatic
98     },
99     "button-release" => {
100     flags => [qw/run-first/],
101     return_type => undef, # void return
102     param_types => [Glib::Int::, Glib::Int::, Glib::Int::], # instance and data are automatic
103     },
104     destroy => sub {
105     $_[0]->signal_chain_from_overridden;
106     %{$_[0]} = ();
107     },
108     };
109    
110     # some internal constants
111    
112     sub TRAD_WIDTH (){ 42.42 } # traditional board width
113     sub TRAD_HEIGHT (){ 45.45 } # traditional board height
114     sub TRAD_RATIO (){ TRAD_WIDTH / TRAD_HEIGHT } # traditional (nihon-kiin) horizontal spacing
115     sub TRAD_SIZE_B (){ 2.18 } # traditional black stone size
116     sub TRAD_SIZE_W (){ 2.12 } # traditional white stone size
117    
118     sub SHADOW (){ 0.06 } # 0.09 probably max.
119    
120     # find a data file using @INC
121     sub findfile {
122     my @files = @_;
123     file:
124     for (@files) {
125     for my $prefix (@INC) {
126     if (-f "$prefix/Gtk2/GoBoard/data/$_") {
127     $_ = "$prefix/Gtk2/GoBoard/data/$_";
128     next file;
129     }
130     }
131     die "$_: file not found in \@INC\n";
132     }
133     wantarray ? @files : $files[0];
134     }
135    
136     sub load_image {
137     my $path = findfile $_[0];
138    
139     new_from_file Gtk2::Gdk::Pixbuf $path
140     or die "$path: $!";
141     }
142    
143 root 1.5 our ($board_img, @black_img, @white_img, $shadow_img,
144     @triangle_img, @square_img, @circle_img, @cross_img);
145    
146 root 1.1 sub load_images {
147 root 1.5 $board_img = load_image "woodgrain-01.jpg";
148     @black_img = load_image "b-01.png";
149     @white_img = map +(load_image "w-0$_.png"), 1,2,3,4,5;
150     $shadow_img = load_image "shadow.png";
151     @triangle_img = map +(load_image "triangle-$_.png"), qw(b w);
152     @square_img = map +(load_image "square-$_.png" ), qw(b w);
153     @circle_img = map +(load_image "circle-$_.png" ), qw(b w);
154     @cross_img = map +(load_image "cross-$_.png" ), qw(b w);
155 root 1.1 }
156    
157     sub INIT_INSTANCE {
158     my $self = shift;
159    
160 root 1.5 @black_img
161 root 1.1 or load_images;
162    
163     $self->double_buffered (0);
164     $self->set (border_width => 0, shadow_type => 'none',
165     obey_child => 0, ratio => TRAD_RATIO);
166    
167     $self->add ($self->{canvas} = new Gtk2::DrawingArea);
168    
169     $self->{canvas}->signal_connect (motion_notify_event => sub { $self->motion });
170     $self->{canvas}->signal_connect (leave_notify_event => sub { $self->cursor (0); delete $self->{cursorpos} });
171     $self->{canvas}->signal_connect (button_press_event => sub { $self->button ("press", $_[1]) });
172     $self->{canvas}->signal_connect (button_release_event => sub { $self->button ("release", $_[1]) });
173    
174     $self->{canvas}->signal_connect_after (configure_event => sub { $self->configure_event ($_[1]) });
175     $self->{canvas}->signal_connect_after (realize => sub {
176     my $window = $_[0]->window;
177     my $color = new Gtk2::Gdk::Color 0xdfdf, 0xb2b2, 0x5d5d;
178     $window->get_colormap->alloc_color ($color, 0, 1);
179     $window->set_background ($color);
180     });
181    
182     $self->{canvas}->set_events ([
183     @{ $self->{canvas}->get_events },
184     'leave-notify-mask',
185     'button-press-mask',
186     'button-release-mask',
187     'pointer-motion-mask',
188     'pointer-motion-hint-mask'
189     ]);
190     }
191    
192     sub SET_PROPERTY {
193     my ($self, $pspec, $newval) = @_;
194    
195     $pspec = $pspec->get_name;
196    
197     $self->cursor (0) if $pspec eq "cursor";
198     $self->{$pspec} = $newval;
199     $self->cursor (1) if $pspec eq "cursor";
200     }
201    
202     sub configure_event {
203     my ($self, $event) = @_;
204    
205     return if $self->{idle};
206    
207     return unless $self->{canvas}->allocation->width != $self->{width}
208     || $self->{canvas}->allocation->height != $self->{height};
209    
210     my $drawable = $self->{window} = $self->{canvas}->window;
211     $drawable->set_back_pixmap (undef, 0);
212    
213     delete $self->{stack};
214    
215     # remove Glib::Source $self->{idle};
216     $self->{idle} ||= add Glib::Idle sub {
217     $self->{width} = $self->{canvas}->allocation->width;
218     $self->{height} = $self->{canvas}->allocation->height;
219     $self->draw_background;
220    
221 root 1.4 $self->draw_board ({ board => delete $self->{board}, label => delete $self->{label} }, 0) if $self->{board};
222 root 1.1 $self->{window}->clear_area (0, 0, $self->{width}, $self->{height});
223    
224     delete $self->{idle};
225    
226     0;
227     };
228    
229     1;
230     }
231    
232 root 1.4 =item $board->set_board ($games_go_simpleboard)
233    
234     Sets the new board position to display from the current position stored in
235     the L<Games::Go::SimpleBoard> object.
236    
237     =cut
238    
239 root 1.1 sub set_board {
240     my ($self, $board) = @_;
241    
242     $self->cursor (0);
243     $self->draw_board ($board, 1);
244     $self->cursor (1);
245     }
246    
247     sub new_pixbuf {
248     my ($w, $h, $alpha, $fill) = @_;
249    
250     my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
251     $pixbuf->fill ($fill) if defined $fill;
252    
253     $pixbuf;
254     }
255    
256     sub scale_pixbuf {
257     my ($src, $w, $h, $mode, $alpha) = @_;
258    
259     my $dst = new_pixbuf $w, $h, $alpha;
260    
261     $src->scale(
262     $dst, 0, 0, $w, $h, 0, 0,
263     $w / $src->get_width, $h / $src->get_height,
264     $mode,
265     );
266    
267     $dst;
268     }
269    
270     sub pixbuf_rect {
271     my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_;
272    
273     # we fake lines by... a horrible method :/
274     my $colour_pb = new_pixbuf 1, 1, 0, $colour;
275     $colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1,
276     'nearest', $alpha);
277     }
278    
279     sub center_text {
280     my ($self, $drawable, $colour, $x, $y, $size, $text) = @_;
281    
282     # could be optimized by caching quite a bit
283    
284     my $context = $self->get_pango_context;
285     my $font = $context->get_font_description;
286     $font->set_size ($size * Gtk2::Pango->scale);
287    
288     my $layout = new Gtk2::Pango::Layout $context;
289     $layout->set_text ($text);
290     my ($w, $h) = $layout->get_pixel_size;
291    
292     my $gc = new Gtk2::Gdk::GC $drawable;
293    
294     my $r = (($colour >> 24) & 255) * (65535 / 255);
295     my $g = (($colour >> 16) & 255) * (65535 / 255);
296     my $b = (($colour >> 8) & 255) * (65535 / 255);
297    
298     $gc->set_rgb_fg_color (new Gtk2::Gdk::Color $r, $g, $b);
299    
300     $drawable->draw_layout ($gc, $x - $w*0.5, $y - $h*0.5, $layout);
301     }
302    
303     # draw an empty board and attach the bg pixmap
304     sub draw_background {
305     my ($self) = @_;
306     my $canvas = $self->{canvas};
307    
308     my $size = $self->{size};
309    
310     my $w = $self->{width};
311     my $h = $self->{height};
312    
313     delete $self->{backgroundpm};
314     delete $self->{backgroundpb};
315    
316     my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $w, $h, -1;
317    
318     #my $gridcolour = 0x88444400; # black is traditional, but only with overlapping stones
319     my $gridcolour = 0x44444400; # black is traditional, but only with overlapping stones
320     my $labelcolour = 0x88444400;
321    
322     # we leave enough space for the shadows.. I like smaller stones, and we
323     # do no need to do the nifty recursive screen updates that cgoban2 does
324     my $borderw = int ($w / ($size + 1) * 0.5);
325     my $borderh = $borderw;
326     my $w2 = $w - $borderw * 2;
327     my $h2 = $h - $borderh * 2;
328     my $edge = ceil ($w2 / ($size + 1));
329     my $ofs = $edge * 0.5;
330    
331     # we need a certain minimum size, and just fudge some formula here
332     return if $w < $size * 5 + 2 + $borderw
333     || $h < $size * 6 + 2 + $borderh;
334    
335     my @kx = map int ($w2 * $_ / ($size+1) + $borderw + 0.5), 0 .. $size; $self->{kx} = \@kx;
336     my @ky = map int ($h2 * $_ / ($size+1) + $borderh + 0.5), 0 .. $size; $self->{ky} = \@ky;
337    
338     my $pixbuf;
339    
340 root 1.5 my ($bw, $bh) = ($board_img->get_width, $board_img->get_height);
341 root 1.1
342     if ($w < $bw && $h < $bh) {
343     $pixbuf = new_pixbuf $w, $h, 0;
344 root 1.5 $board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0);
345 root 1.1 } else {
346 root 1.5 $pixbuf = scale_pixbuf $board_img, $w, $h, 'bilinear', 0; # nearest for extra speed
347 root 1.1 }
348    
349     my $linew = int ($w / 40 / $size);
350    
351     # ornamental border... we have time to waste :/
352     pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $w-1, $linew, 255;
353     pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $h-1, 255;
354     pixbuf_rect $pixbuf, 0xffcc7700, $w-$linew-1, 0, $w-1, $h-1, 255;
355     pixbuf_rect $pixbuf, 0xffcc7700, 0, $h-$linew-1, $w-1, $h-1, 255;
356    
357     for my $i (1 .. $size) {
358     pixbuf_rect $pixbuf, $gridcolour, $kx[$i] - $linew, $ky[1] - $linew, $kx[$i] + $linew, $ky[$size] + $linew, 255;
359     pixbuf_rect $pixbuf, $gridcolour, $kx[1] - $linew, $ky[$i] - $linew, $kx[$size] + $linew, $ky[$i] + $linew, 255;
360     }
361    
362     # hoshi points
363     my $hoshi = sub {
364     my ($x, $y) = @_;
365     my $hs = 1 | int $edge / 4;
366     $hs = 5 if $hs < 5;
367     $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2;
368    
369     # we use the shadow mask... not perfect, but I want to finish this
370 root 1.5 $shadow_img->composite ($pixbuf,
371 root 1.1 $x, $y, ($hs + 1) x2, $x, $y,
372 root 1.5 $hs / $shadow_img->get_width, $hs / $shadow_img->get_height,
373 root 1.1 'bilinear', 255);
374     };
375    
376     if ($size > 6) {
377     my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
378     $hoshi->($h1, $h1);
379     $hoshi->($size - $h1 + 1, $h1);
380     $hoshi->($h1, $size - $h1 + 1);
381     $hoshi->($size - $h1 + 1, $size - $h1 + 1);
382    
383     if ($size % 2) { # on odd boards, also the remaining 5
384     my $h2 = ($size + 1) / 2;
385     if ($size > 10) {
386     $hoshi->($h1, $h2);
387     $hoshi->($size - $h1 + 1, $h2);
388     $hoshi->($h2, $size - $h1 + 1);
389     $hoshi->($h2, $h1);
390     }
391     # the tengen
392     $hoshi->($h2, $h2);
393     }
394     }
395    
396     # now we have a board sans text
397     $pixmap->draw_pixbuf ($self->style->white_gc,
398     $pixbuf,
399     0, 0, 0, 0, $w, $h,
400     "normal", 0, 0);
401    
402     # now draw the labels
403     for my $i (1 .. $size) {
404     # 38 max, but we allow a bit more
405     my $label = (qw(- A B C D E F G H J K L M N O P Q R S T U V W X Y Z
406     AA BB CC DD EE FF GG HH JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX YY ZZ))[$i];
407    
408     $self->center_text ($pixmap, $labelcolour, $kx[$i], $borderh, $ofs * 0.7, $label);
409     $self->center_text ($pixmap, $labelcolour, $kx[$i], $h2 + $borderh, $ofs * 0.7, $label);
410     $self->center_text ($pixmap, $labelcolour, $borderw, $ky[$i], $ofs * 0.7, $size - $i + 1);
411     $self->center_text ($pixmap, $labelcolour, $w2 + $borderw, $ky[$i], $ofs * 0.7, $size - $i + 1);
412     }
413    
414     $self->{window}->set_back_pixmap ($pixmap, 0);
415    
416     $self->{backgroundpm} = $pixmap;
417     $self->{backgroundpb} = $pixbuf;
418    
419     $edge = int ($edge * TRAD_SIZE_B / TRAD_SIZE_W);
420     $ofs = int ($edge * 0.5);
421    
422     {
423     # shared vars for the stone drawing function
424     my $shadow = $edge * SHADOW;
425     my $pb;
426     my @area;
427     my @areai;
428     my %stack;
429    
430     my $put_stack = sub {
431     my ($x, $y, $dx, $dy, $ox, $oy) = @_;
432    
433 root 1.4 my $mark = $self->{board}[$x-1][$y-1];
434 root 1.1
435     if ($mark & ~MARK_LABEL) {
436     my $stack = $stack{$mark} ||= $self->draw_stack ($mark, $edge);
437    
438     $stack->[($x ^ $y) % @$stack]
439     ->composite ($pb,
440     $ox, $oy,
441     $areai[2] + $dx - $ox, $areai[3] + $dy - $oy,
442     $dx + $ox, $dy + $oy,
443     1, 1, 'nearest', 255);
444     }
445     };
446    
447     $self->{draw_stone} = sub {
448     my ($x, $y) = @_;
449    
450     @area = ($kx[$x] - $ofs, $ky[$y] - $ofs,
451     $edge + $shadow, $edge + $shadow);
452     @areai = ((ceil $area[0]), (ceil $area[1]),
453     (int $area[2]), (int $area[3])); # area, integer
454    
455     $pb = new_pixbuf @areai[2,3];
456     $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0);
457    
458     $put_stack->($x-1, $y, $kx[$x-1] - $kx[$x], 0, 0, 0) if $x > 1;
459     $put_stack->($x, $y-1, 0, $ky[$y-1] - $ky[$y], 0, 0) if $y > 1;
460     $put_stack->($x , $y , 0, 0);
461     $put_stack->($x+1, $y, 0, 0, $kx[$x+1] - $kx[$x], 0) if $x < $size;
462     $put_stack->($x, $y+1, 0, 0, 0, $ky[$y+1] - $ky[$y]) if $y < $size;
463    
464     # speed none, normal, max
465     $self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb,
466     0, 0, @areai, 'max', 0, 0);
467    
468     # labels are handled here because they are quite rare
469     # (and we can't draw text into pixbufs easily)
470 root 1.4 my $mark = $self->{board}[$x-1][$y-1];
471 root 1.1
472     if ($mark & MARK_LABEL) {
473     my $white = $mark & MARK_W ? 0 : 0xffffff00;
474    
475 root 1.4 $self->center_text ($self->{backgroundpm}, 0,
476     $areai[0] + $ofs * 1.1, $areai[1] + $ofs * 1.1,
477     $ofs * 0.7, $self->{label}[$x-1][$y-1])
478     if $white;
479    
480 root 1.1 $self->center_text ($self->{backgroundpm}, $white,
481     $areai[0] + $ofs, $areai[1] + $ofs,
482 root 1.4 $ofs * 0.7, $self->{label}[$x-1][$y-1]);
483 root 1.1 }
484    
485     undef $pb;
486    
487     [@areai];
488     };
489     }
490     }
491    
492     # create a stack of stones, possibly in various versions
493     sub draw_stack {
494     my ($self, $mark, $size) = @_;
495    
496     my @stack;
497     my $csize = ceil $size;
498     my $shadow = $size * SHADOW;
499    
500 root 1.5 for my $stone ($mark & MARK_W ? @white_img : @black_img) {
501 root 1.1 my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000;
502    
503     # zeroeth the shadow
504     if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) {
505 root 1.5 $shadow_img->composite (
506 root 1.1 $base, $shadow, $shadow, $csize, $csize, $shadow, $shadow,
507 root 1.5 $size / $shadow_img->get_width, $size / $shadow_img->get_height,
508 root 1.1 'bilinear', 128
509     );
510     }
511    
512     for ([MARK_B, $mark & MARK_GRAYED ? 96 : 255, 1],
513     [MARK_W, $mark & MARK_GRAYED ? 160 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) {
514     my ($mask, $alpha, $scale) = @$_;
515     if ($mark & $mask) {
516     $stone->composite (
517     $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2,
518     $size * $scale / $stone->get_width, $size * $scale / $stone->get_height,
519     'bilinear', $alpha
520     );
521     }
522     }
523    
524     # then the small stones (always using the first image)
525 root 1.5 for ([MARK_SMALL_B, $black_img[0]],
526     [MARK_SMALL_W, $white_img[0]]) {
527 root 1.1 my ($mask, $img) = @$_;
528     if ($mark & $mask) {
529     $img->composite (
530     $base, (int $size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2,
531     $size / $img->get_width / 2, $size / $img->get_height / 2,
532     'bilinear', 255
533     );
534     }
535     }
536    
537     # and lastly any markers
538     my $dark_bg = ! ! ($mark & MARK_B);
539    
540 root 1.5 for ([MARK_CIRCLE, $circle_img[$dark_bg]],
541     [MARK_TRIANGLE, $triangle_img[$dark_bg]],
542     [MARK_SQUARE, $square_img[$dark_bg]],
543     [MARK_CROSS, $cross_img[$dark_bg]],
544     [MARK_KO, $square_img[$dark_bg]]) {
545 root 1.1 my ($mask, $img) = @$_;
546     if ($mark & $mask) {
547     $img->composite (
548     $base, 0, 0, $size, $size, 0, 0,
549     $size / $img->get_width, $size / $img->get_height,
550 root 1.6 'bilinear', $dark_bg ? 176 : 190
551 root 1.1 );
552     }
553     }
554    
555     push @stack, $base;
556     }
557    
558 root 1.4 \@stack
559 root 1.1 }
560    
561     sub draw_board {
562     my ($self, $new, $dopaint) = @_;
563    
564 root 1.4 my $newboard = $new->{board};
565     my $newlabel = $new->{label};
566    
567     if ($self->{backgroundpb}) {
568     my $draw_stone = $self->{draw_stone};
569 root 1.1
570 root 1.4 my $oldboard = $self->{board} ||= [];
571     my $oldlabel = $self->{label} ||= [];
572 root 1.1
573     my @areas;
574    
575     my $size1 = $self->{size} - 1;
576    
577     for my $x (0 .. $size1) {
578 root 1.4 my $old = $oldboard->[$x] ||= [];
579     my $new = $newboard->[$x];
580 root 1.1
581     for my $y (0 .. $size1) {
582 root 1.4 next if $old->[$y] == $new->[$y];
583    
584     $old -> [$y] = $new -> [$y];
585     $oldlabel->[$x][$y] = $newlabel->[$x][$y];
586    
587     push @areas, $draw_stone->($x+1, $y+1);
588 root 1.1 }
589     }
590    
591     if ($dopaint && @areas) {
592     # a single full clear_area is way faster than many single calls here
593     # the "cut-off" point is quite arbitrary
594     if (@areas > 64) {
595     # update a single rectangle only
596     my $rect = new Gtk2::Gdk::Rectangle @{pop @areas};
597     $rect = $rect->union (new Gtk2::Gdk::Rectangle @$_) for @areas;
598     $self->{window}->clear_area ($rect->values);
599     } else {
600     # update all the affected rectangles
601     $self->{window}->clear_area (@$_) for @areas;
602     }
603     }
604 root 1.4 } else {
605     no strict 'refs';
606    
607     # straight copy
608     $self->{board} = [map [@$_], @$newboard];
609     $self->{label} = [map [@$_], @$newlabel];
610 root 1.1 }
611     }
612    
613     sub cursor {
614     my ($self, $show) = @_;
615    
616     return unless exists $self->{cursorpos}
617     && $self->{cursor}
618     && $self->{backgroundpb};
619    
620     my ($x, $y) = @{$self->{cursorpos}};
621    
622 root 1.4 my $mark = $self->{board}[$x][$y];
623 root 1.1
624     $mark = $self->{cursor}->($mark, $x, $y) if $show;
625    
626 root 1.4 local $self->{board}[$x][$y] = $mark;
627 root 1.1 $self->{window}->clear_area (@{ $self->{draw_stone}->($x + 1, $y + 1) });
628     }
629    
630     sub motion {
631     my ($self) = @_;
632    
633     return unless $self->{backgroundpb};
634    
635     my $window = $self->{canvas}->window;
636     my (undef, $x, $y, undef) = $window->get_pointer;
637    
638     my $size = $self->{size};
639    
640     my $x = int (($x - $self->{kx}[0]) * $size / ($self->{kx}[$size] - $self->{kx}[0]) + 0.5) - 1;
641     my $y = int (($y - $self->{ky}[0]) * $size / ($self->{ky}[$size] - $self->{ky}[0]) + 0.5) - 1;
642    
643     my $pos = $self->{cursorpos};
644     if ($x != $pos->[0] || $y != $pos->[1]) {
645    
646     $self->cursor (0);
647    
648     if ($x >= 0 && $x < $size
649     && $y >= 0 && $y < $size) {
650     $self->{cursorpos} = [$x, $y];
651     $self->cursor (1);
652     } else {
653     delete $self->{cursorpos};
654     }
655     }
656     }
657    
658     sub do_button_press {
659     my ($self, $button, $x, $y) = @_;
660     }
661    
662     sub do_button_release {
663     my ($self, $button, $x, $y) = @_;
664     }
665    
666     sub button {
667     my ($self, $type, $event) = @_;
668    
669     $self->motion;
670    
671     if ($self->{cursorpos}) {
672     $self->signal_emit ("button-$type", $event->button, @{ $self->{cursorpos} });
673     }
674     }
675    
676     =item Gtk2::GoBoard::play_sound "name"
677    
678     Play the sound with the give name. Currently supported names are:
679    
680     alarm connect gamestart info move outoftime pass resign ring warning
681    
682     If the L<Audio::Play> module cannot be loaded, the function will silently
683     fail. If an unsupported sound name is used, the function might C<croak> or
684     might silently fail.
685    
686     This function forks a sound-server to play the sound(s) on first use.
687    
688     =cut
689    
690     our $SOUND_SERVER;
691    
692     sub play_sound {
693     eval { require Audio::Data; require Audio::Play }
694     or return;
695    
696     unless ($SOUND_SERVER) {
697     require Socket;
698    
699     # use this contortion to also work on the broken windows platform
700     socketpair $SOUND_SERVER, my $fh, &Socket::AF_UNIX, &Socket::SOCK_STREAM, 0
701     or return;
702    
703     my $pid = fork;
704    
705 root 1.2 if ($pid) {
706 root 1.1 # parent
707     close $fh;
708    
709 root 1.2 } elsif (defined $pid) {
710 root 1.1 # child
711     close $SOUND_SERVER;
712    
713 root 1.2 close STDIN;
714     close STDOUT;
715     close STDERR;
716    
717     # ok, this is a bit pathetic
718     POSIX::close $_ for grep $_ != fileno $fh, 3 .. 1000;
719    
720 root 1.1 my %sound;
721    
722 root 1.2 while (<$fh>) {
723 root 1.1 chomp;
724    
725     eval {
726     my $sound = $sound{$_} ||= do {
727     my $path = findfile "$_.au"
728     or Carp::croak "$_: unable to find sound\n";
729    
730     open my $fh, "<", $path
731     or Carp::croak "$_: unable to load sound\n";
732    
733     binmode $fh;
734    
735     my $data = new Audio::Data;
736     $data->Load ($fh);
737    
738     $data
739     };
740    
741 root 1.2 my $server = new Audio::Play;
742 root 1.1 $server->play ($sound);
743     $server->flush;
744     };
745     }
746    
747 root 1.3 # required for windows, as a mere _exit kills your parent process...
748     kill 9, $$;
749 root 1.2 } else {
750     undef $SOUND_SERVER;
751     return;
752 root 1.1 }
753     }
754 root 1.2
755     syswrite $SOUND_SERVER, "$_[0]\n";
756 root 1.1 }
757    
758     1;
759    
760     =back
761    
762     =head2 EXAMPLE PROGRAM
763    
764     This program should get you started. It creates a board with some
765     markings, enables a cursor callback that shows a transparent black stone,
766     and after a click, marks the position with a circle and disables the
767     cursor.
768    
769     use Gtk2 -init;
770     use Games::Go::SimpleBoard;
771     use Gtk2::GoBoard;
772    
773     my $game = new Games::Go::SimpleBoard 9;
774    
775     # show off some markings
776     $game->{board}[0][0] = MARK_B;
777     $game->{board}[1][1] = MARK_GRAY_B | MARK_SMALL_W;
778     $game->{board}[2][2] = MARK_W | MARK_TRIANGLE;
779     $game->{board}[1][2] = MARK_B | MARK_LABEL;
780     $game->{label}[1][2] = "198";
781     $game->{board}[0][2] = MARK_W | MARK_LABEL;
782     $game->{label}[0][2] = "AWA";
783    
784     # create a spot where black cannot put a stone
785     $game->{board}[17][0] = MARK_W;
786     $game->{board}[17][1] = MARK_W;
787     $game->{board}[18][1] = MARK_W;
788    
789     my $board = new Gtk2::GoBoard;
790     $board->set_board ($game);
791    
792 root 1.2 Gtk2::GoBoard::play_sound "gamestart"; # ping
793    
794 root 1.1 # enable cursor for black, till click
795     $board->set (cursor => sub {
796     my ($mark, $x, $y) = @_;
797    
798     $mark |= MARK_GRAYED | MARK_B
799     if $game->is_valid_move (COLOUR_BLACK, $x, $y);
800    
801     $mark
802     });
803    
804     # on press, set a mark and disable cursor
805     $board->signal_connect (button_release => sub {
806     my ($board, $button, $x, $y) = @_;
807    
808     $game->{board}[$x][$y] |= MARK_CIRCLE;
809     $board->set_board ($game); # force update
810    
811 root 1.2 Gtk2::GoBoard::play_sound "move"; # play click sound
812    
813 root 1.1 $board->set (cursor => undef); # disable cursor
814     });
815    
816     my $w = new Gtk2::Window "toplevel";
817     $w->set_default_size (450, 450);
818     $w->add ($board);
819     $w->show_all;
820    
821     main Gtk2;
822    
823     =head2 AUTHOR
824    
825     Marc Lehmann <schmorp@schmorp.de>
826    
827     =head2 SEE ALSO
828    
829     L<App::IGS>, L<Games::Go::SimpleBoard>, L<AnyEvent::IGS>, L<KGS>.
830    
831     =cut
832