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