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 Carp (); |
72 |
use Gtk2; |
73 |
|
74 |
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 |
our ($board_img, @black_img, @white_img, $shadow_img, |
144 |
@triangle_img, @square_img, @circle_img, @cross_img); |
145 |
|
146 |
sub load_images { |
147 |
$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 .. 5; |
150 |
$shadow_img = load_image "shadow.png"; # also used to fake hoshi points |
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 |
} |
156 |
|
157 |
sub INIT_INSTANCE { |
158 |
my $self = shift; |
159 |
|
160 |
@black_img |
161 |
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 |
$self->draw_board ({ board => delete $self->{board}, label => delete $self->{label} }, 0) if $self->{board}; |
222 |
$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 |
=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 |
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 |
my ($bw, $bh) = ($board_img->get_width, $board_img->get_height); |
341 |
|
342 |
if ($w < $bw && $h < $bh) { |
343 |
$pixbuf = new_pixbuf $w, $h, 0; |
344 |
$board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0); |
345 |
} else { |
346 |
$pixbuf = scale_pixbuf $board_img, $w, $h, 'bilinear', 0; # nearest for extra speed |
347 |
} |
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 |
$shadow_img->composite ($pixbuf, |
371 |
$x, $y, ($hs + 1) x2, $x, $y, |
372 |
$hs / $shadow_img->get_width, $hs / $shadow_img->get_height, |
373 |
'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 |
my $mark = $self->{board}[$x-1][$y-1]; |
434 |
|
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 = map +(ceil $_), @area; # area, integer |
453 |
|
454 |
$pb = new_pixbuf @areai[2,3]; |
455 |
$self->{backgroundpb}->copy_area (@areai, $pb, 0, 0); |
456 |
|
457 |
$put_stack->($x-1, $y, $kx[$x-1] - $kx[$x], 0, 0, 0) if $x > 1; |
458 |
$put_stack->($x, $y-1, 0, $ky[$y-1] - $ky[$y], 0, 0) if $y > 1; |
459 |
$put_stack->($x , $y , 0, 0); |
460 |
$put_stack->($x+1, $y, 0, 0, $kx[$x+1] - $kx[$x], 0) if $x < $size; |
461 |
$put_stack->($x, $y+1, 0, 0, 0, $ky[$y+1] - $ky[$y]) if $y < $size; |
462 |
|
463 |
# speed none, normal, max |
464 |
$self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb, |
465 |
0, 0, @areai, 'max', 0, 0); |
466 |
|
467 |
# labels are handled here because they are quite rare |
468 |
# (and we can't draw text into pixbufs easily) |
469 |
my $mark = $self->{board}[$x-1][$y-1]; |
470 |
|
471 |
if ($mark & MARK_LABEL) { |
472 |
my $white = $mark & MARK_W ? 0 : 0xffffff00; |
473 |
|
474 |
$self->center_text ($self->{backgroundpm}, 0, |
475 |
$areai[0] + $ofs * 1.1, $areai[1] + $ofs * 1.1, |
476 |
$ofs * 0.7, $self->{label}[$x-1][$y-1]) |
477 |
if $white; |
478 |
|
479 |
$self->center_text ($self->{backgroundpm}, $white, |
480 |
$areai[0] + $ofs, $areai[1] + $ofs, |
481 |
$ofs * 0.7, $self->{label}[$x-1][$y-1]); |
482 |
} |
483 |
|
484 |
undef $pb; |
485 |
|
486 |
[@areai]; |
487 |
}; |
488 |
} |
489 |
} |
490 |
|
491 |
# create a stack of stones, possibly in various versions |
492 |
sub draw_stack { |
493 |
my ($self, $mark, $size) = @_; |
494 |
|
495 |
my @stack; |
496 |
my $csize = ceil $size; |
497 |
my $shadow = $size * SHADOW; |
498 |
|
499 |
for my $stone ($mark & MARK_W ? @white_img : @black_img) { |
500 |
my $base = new_pixbuf +(ceil $csize + $shadow) x2, 1, 0x00000000; |
501 |
|
502 |
# zeroeth the shadow |
503 |
if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) { |
504 |
$shadow_img->composite ( |
505 |
$base, ($shadow) x2, $csize, $csize, ($shadow) x2, |
506 |
$size / $shadow_img->get_width, $size / $shadow_img->get_height, |
507 |
'bilinear', 128 |
508 |
); |
509 |
} |
510 |
|
511 |
for ([MARK_B, $mark & MARK_GRAYED ? 106 : 255, 1], |
512 |
[MARK_W, $mark & MARK_GRAYED ? 190 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) { |
513 |
my ($mask, $alpha, $scale) = @$_; |
514 |
if ($mark & $mask) { |
515 |
$stone->composite ( |
516 |
$base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5) x2, |
517 |
$size * $scale / $stone->get_width, $size * $scale / $stone->get_height, |
518 |
'bilinear', $alpha |
519 |
); |
520 |
} |
521 |
} |
522 |
|
523 |
# then the small stones (always using the first image) |
524 |
for ([MARK_SMALL_B, $mark & MARK_SMALL_GRAYED ? 106 : 255, $black_img[0]], |
525 |
[MARK_SMALL_W, $mark & MARK_SMALL_GRAYED ? 190 : 255, $white_img[0]]) { |
526 |
my ($mask, $alpha, $img) = @$_; |
527 |
if ($mark & $mask) { |
528 |
$img->composite ( |
529 |
$base, ($size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2, |
530 |
$size / $img->get_width / 2, $size / $img->get_height / 2, |
531 |
'bilinear', $alpha |
532 |
); |
533 |
} |
534 |
} |
535 |
|
536 |
# and finally any markers |
537 |
my $dark_bg = ! ! ($mark & MARK_B); |
538 |
|
539 |
for ([MARK_CIRCLE, $circle_img [$dark_bg]], |
540 |
[MARK_TRIANGLE, $triangle_img[$dark_bg]], |
541 |
[MARK_CROSS, $cross_img [$dark_bg]], |
542 |
[MARK_SQUARE, $square_img [$dark_bg]], |
543 |
[MARK_KO, $square_img [$dark_bg]]) { |
544 |
my ($mask, $img) = @$_; |
545 |
if ($mark & $mask) { |
546 |
$img->composite ( |
547 |
$base, 0, 0, $csize, $csize, 0, 0, |
548 |
$size / $img->get_width, $size / $img->get_height, |
549 |
'bilinear', $dark_bg ? 176 : 190 |
550 |
); |
551 |
} |
552 |
} |
553 |
|
554 |
push @stack, $base; |
555 |
} |
556 |
|
557 |
\@stack |
558 |
} |
559 |
|
560 |
sub draw_board { |
561 |
my ($self, $new, $dopaint) = @_; |
562 |
|
563 |
my $newboard = $new->{board}; |
564 |
my $newlabel = $new->{label}; |
565 |
|
566 |
if ($self->{backgroundpb}) { |
567 |
my $draw_stone = $self->{draw_stone}; |
568 |
|
569 |
my $oldboard = $self->{board} ||= []; |
570 |
my $oldlabel = $self->{label} ||= []; |
571 |
|
572 |
my @areas; |
573 |
|
574 |
my $size1 = $self->{size} - 1; |
575 |
|
576 |
for my $x (0 .. $size1) { |
577 |
my $old = $oldboard->[$x] ||= []; |
578 |
my $new = $newboard->[$x]; |
579 |
|
580 |
for my $y (0 .. $size1) { |
581 |
next if $old->[$y] == $new->[$y]; |
582 |
|
583 |
$old -> [$y] = $new -> [$y]; |
584 |
$oldlabel->[$x][$y] = $newlabel->[$x][$y]; |
585 |
|
586 |
push @areas, $draw_stone->($x+1, $y+1); |
587 |
} |
588 |
} |
589 |
|
590 |
if ($dopaint && @areas) { |
591 |
# a single full clear_area is way faster than many single calls here |
592 |
# the "cut-off" point is quite arbitrary |
593 |
if (@areas > 64) { |
594 |
# update a single rectangle only |
595 |
my $rect = new Gtk2::Gdk::Rectangle @{pop @areas}; |
596 |
$rect = $rect->union (new Gtk2::Gdk::Rectangle @$_) for @areas; |
597 |
$self->{window}->clear_area ($rect->values); |
598 |
} else { |
599 |
# update all the affected rectangles |
600 |
$self->{window}->clear_area (@$_) for @areas; |
601 |
} |
602 |
} |
603 |
} else { |
604 |
no strict 'refs'; |
605 |
|
606 |
# straight copy |
607 |
$self->{board} = [map [@$_], @$newboard]; |
608 |
$self->{label} = [map [@$_], @$newlabel]; |
609 |
} |
610 |
} |
611 |
|
612 |
sub cursor { |
613 |
my ($self, $show) = @_; |
614 |
|
615 |
return unless exists $self->{cursorpos} |
616 |
&& $self->{cursor} |
617 |
&& $self->{backgroundpb}; |
618 |
|
619 |
my ($x, $y) = @{$self->{cursorpos}}; |
620 |
|
621 |
my $mark = $self->{board}[$x][$y]; |
622 |
|
623 |
$mark = $self->{cursor}->($mark, $x, $y) if $show; |
624 |
|
625 |
local $self->{board}[$x][$y] = $mark; |
626 |
$self->{window}->clear_area (@{ $self->{draw_stone}->($x + 1, $y + 1) }); |
627 |
} |
628 |
|
629 |
sub motion { |
630 |
my ($self) = @_; |
631 |
|
632 |
return unless $self->{backgroundpb}; |
633 |
|
634 |
my $window = $self->{canvas}->window; |
635 |
my (undef, $x, $y, undef) = $window->get_pointer; |
636 |
|
637 |
my $size = $self->{size}; |
638 |
|
639 |
my $x = int (($x - $self->{kx}[0]) * $size / ($self->{kx}[$size] - $self->{kx}[0]) + 0.5) - 1; |
640 |
my $y = int (($y - $self->{ky}[0]) * $size / ($self->{ky}[$size] - $self->{ky}[0]) + 0.5) - 1; |
641 |
|
642 |
my $pos = $self->{cursorpos}; |
643 |
if ((not (defined $pos) && $x >= 0 && $x < $size && $y >= 0 && $y < $size) |
644 |
|| $x != $pos->[0] |
645 |
|| $y != $pos->[1]) { |
646 |
|
647 |
$self->cursor (0); |
648 |
|
649 |
if ($x >= 0 && $x < $size |
650 |
&& $y >= 0 && $y < $size) { |
651 |
$self->{cursorpos} = [$x, $y]; |
652 |
$self->cursor (1); |
653 |
} else { |
654 |
delete $self->{cursorpos}; |
655 |
} |
656 |
} |
657 |
} |
658 |
|
659 |
sub do_button_press { |
660 |
my ($self, $button, $x, $y) = @_; |
661 |
} |
662 |
|
663 |
sub do_button_release { |
664 |
my ($self, $button, $x, $y) = @_; |
665 |
} |
666 |
|
667 |
sub button { |
668 |
my ($self, $type, $event) = @_; |
669 |
|
670 |
$self->motion; |
671 |
|
672 |
if ($self->{cursorpos}) { |
673 |
$self->signal_emit ("button-$type", $event->button, @{ $self->{cursorpos} }); |
674 |
} |
675 |
} |
676 |
|
677 |
=item Gtk2::GoBoard::play_sound "name" |
678 |
|
679 |
Play the sound with the give name. Currently supported names are: |
680 |
|
681 |
alarm connect gamestart info move outoftime pass resign ring warning |
682 |
|
683 |
If the L<Audio::Play> module cannot be loaded, the function will silently |
684 |
fail. If an unsupported sound name is used, the function might C<croak> or |
685 |
might silently fail. |
686 |
|
687 |
This function forks a sound-server to play the sound(s) on first use. |
688 |
|
689 |
=cut |
690 |
|
691 |
our $SOUND_SERVER; |
692 |
|
693 |
sub play_sound { |
694 |
eval { require Audio::Data; require Audio::Play } |
695 |
or return; |
696 |
|
697 |
unless ($SOUND_SERVER) { |
698 |
require Socket; |
699 |
|
700 |
# use this contortion to also work on the broken windows platform |
701 |
socketpair $SOUND_SERVER, my $fh, &Socket::AF_UNIX, &Socket::SOCK_STREAM, 0 |
702 |
or return; |
703 |
|
704 |
my $pid = fork; |
705 |
|
706 |
if ($pid) { |
707 |
# parent |
708 |
close $fh; |
709 |
|
710 |
} elsif (defined $pid) { |
711 |
# child |
712 |
close $SOUND_SERVER; |
713 |
|
714 |
close STDIN; |
715 |
close STDOUT; |
716 |
close STDERR; |
717 |
|
718 |
# ok, this is a bit pathetic |
719 |
POSIX::close $_ for grep $_ != fileno $fh, 3 .. 1000; |
720 |
|
721 |
my %sound; |
722 |
|
723 |
while (<$fh>) { |
724 |
chomp; |
725 |
|
726 |
eval { |
727 |
my $sound = $sound{$_} ||= do { |
728 |
my $path = findfile "$_.au" |
729 |
or Carp::croak "$_: unable to find sound\n"; |
730 |
|
731 |
open my $fh, "<", $path |
732 |
or Carp::croak "$_: unable to load sound\n"; |
733 |
|
734 |
binmode $fh; |
735 |
|
736 |
my $data = new Audio::Data; |
737 |
$data->Load ($fh); |
738 |
|
739 |
$data |
740 |
}; |
741 |
|
742 |
my $server = new Audio::Play; |
743 |
$server->play ($sound); |
744 |
$server->flush; |
745 |
}; |
746 |
} |
747 |
|
748 |
# required for windows, as a mere _exit kills your parent process... |
749 |
kill 9, $$; |
750 |
} else { |
751 |
undef $SOUND_SERVER; |
752 |
return; |
753 |
} |
754 |
} |
755 |
|
756 |
syswrite $SOUND_SERVER, "$_[0]\n"; |
757 |
} |
758 |
|
759 |
1; |
760 |
|
761 |
=back |
762 |
|
763 |
=head2 EXAMPLE PROGRAM |
764 |
|
765 |
This program should get you started. It creates a board with some |
766 |
markings, enables a cursor callback that shows a transparent black stone, |
767 |
and after a click, marks the position with a circle and disables the |
768 |
cursor. |
769 |
|
770 |
use Gtk2 -init; |
771 |
use Games::Go::SimpleBoard; |
772 |
use Gtk2::GoBoard; |
773 |
|
774 |
my $game = new Games::Go::SimpleBoard 9; |
775 |
|
776 |
# show off some markings |
777 |
$game->{board}[0][0] = MARK_B; |
778 |
$game->{board}[1][1] = MARK_GRAY_B | MARK_SMALL_W; |
779 |
$game->{board}[2][2] = MARK_W | MARK_TRIANGLE; |
780 |
$game->{board}[1][2] = MARK_B | MARK_LABEL; |
781 |
$game->{label}[1][2] = "198"; |
782 |
$game->{board}[0][2] = MARK_W | MARK_LABEL; |
783 |
$game->{label}[0][2] = "AWA"; |
784 |
|
785 |
# create a spot where black cannot put a stone |
786 |
$game->{board}[17][0] = MARK_W; |
787 |
$game->{board}[17][1] = MARK_W; |
788 |
$game->{board}[18][1] = MARK_W; |
789 |
|
790 |
my $board = new Gtk2::GoBoard; |
791 |
$board->set_board ($game); |
792 |
|
793 |
Gtk2::GoBoard::play_sound "gamestart"; # ping |
794 |
|
795 |
# enable cursor for black, till click |
796 |
$board->set (cursor => sub { |
797 |
my ($mark, $x, $y) = @_; |
798 |
|
799 |
$mark |= MARK_GRAYED | MARK_B |
800 |
if $game->is_valid_move (COLOUR_BLACK, $x, $y); |
801 |
|
802 |
$mark |
803 |
}); |
804 |
|
805 |
# on press, set a mark and disable cursor |
806 |
$board->signal_connect (button_release => sub { |
807 |
my ($board, $button, $x, $y) = @_; |
808 |
|
809 |
$game->{board}[$x][$y] |= MARK_CIRCLE; |
810 |
$board->set_board ($game); # force update |
811 |
|
812 |
Gtk2::GoBoard::play_sound "move"; # play click sound |
813 |
|
814 |
$board->set (cursor => undef); # disable cursor |
815 |
}); |
816 |
|
817 |
my $w = new Gtk2::Window "toplevel"; |
818 |
$w->set_default_size (450, 450); |
819 |
$w->add ($board); |
820 |
$w->show_all; |
821 |
|
822 |
main Gtk2; |
823 |
|
824 |
=head2 AUTHOR |
825 |
|
826 |
Marc Lehmann <schmorp@schmorp.de> |
827 |
|
828 |
=head2 SEE ALSO |
829 |
|
830 |
L<App::IGS>, L<Games::Go::SimpleBoard>, L<AnyEvent::IGS>, L<KGS>. |
831 |
|
832 |
=cut |
833 |
|