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