ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Gtk2-GoBoard/GoBoard.pm
(Generate patch)

Comparing Gtk2-GoBoard/GoBoard.pm (file contents):
Revision 1.1 by root, Sun Jun 22 16:07:01 2008 UTC vs.
Revision 1.7 by root, Wed Jun 25 07:09:35 2008 UTC

66no warnings; 66no warnings;
67use strict; 67use strict;
68 68
69use Scalar::Util; 69use Scalar::Util;
70use POSIX qw(ceil); 70use POSIX qw(ceil);
71use Carp ();
71use Gtk2; 72use Gtk2;
73
72use Games::Go::SimpleBoard; 74use Games::Go::SimpleBoard;
73use Carp ();
74 75
75use Glib::Object::Subclass 76use Glib::Object::Subclass
76 Gtk2::AspectFrame::, 77 Gtk2::AspectFrame::,
77 properties => [ 78 properties => [
78 Glib::ParamSpec->IV ( 79 Glib::ParamSpec->IV (
137 138
138 new_from_file Gtk2::Gdk::Pixbuf $path 139 new_from_file Gtk2::Gdk::Pixbuf $path
139 or die "$path: $!"; 140 or die "$path: $!";
140} 141}
141 142
143our ($board_img, @black_img, @white_img, $shadow_img,
144 @triangle_img, @square_img, @circle_img, @cross_img);
145
142sub load_images { 146sub load_images {
147 $board_img = load_image "woodgrain-01.jpg";
143 @::black_img = load_image "b-01.png"; 148 @black_img = load_image "b-01.png";
144 @::white_img = map +(load_image "w-0$_.png"), 1,2,3,4,5; 149 @white_img = map +(load_image "w-0$_.png"), 1,2,3,4,5;
145 $::shadow_img = load_image "shadow.png"; 150 $shadow_img = load_image "shadow.png";
146 @::triangle_img = map +(load_image "triangle-$_.png"), qw(b w); 151 @triangle_img = map +(load_image "triangle-$_.png"), qw(b w);
147 @::square_img = map +(load_image "square-$_.png"), qw(b w); 152 @square_img = map +(load_image "square-$_.png" ), qw(b w);
148 @::circle_img = map +(load_image "circle-$_.png"), qw(b w); 153 @circle_img = map +(load_image "circle-$_.png" ), qw(b w);
149 $::board_img = load_image "woodgrain-01.jpg"; 154 @cross_img = map +(load_image "cross-$_.png" ), qw(b w);
150} 155}
151 156
152sub INIT_INSTANCE { 157sub INIT_INSTANCE {
153 my $self = shift; 158 my $self = shift;
154 159
155 @::black_img 160 @black_img
156 or load_images; 161 or load_images;
157 162
158 $self->double_buffered (0); 163 $self->double_buffered (0);
159 $self->set (border_width => 0, shadow_type => 'none', 164 $self->set (border_width => 0, shadow_type => 'none',
160 obey_child => 0, ratio => TRAD_RATIO); 165 obey_child => 0, ratio => TRAD_RATIO);
211 $self->{idle} ||= add Glib::Idle sub { 216 $self->{idle} ||= add Glib::Idle sub {
212 $self->{width} = $self->{canvas}->allocation->width; 217 $self->{width} = $self->{canvas}->allocation->width;
213 $self->{height} = $self->{canvas}->allocation->height; 218 $self->{height} = $self->{canvas}->allocation->height;
214 $self->draw_background; 219 $self->draw_background;
215 220
216 $self->draw_board (delete $self->{board}, 0) if $self->{board}; 221 $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}); 222 $self->{window}->clear_area (0, 0, $self->{width}, $self->{height});
218 223
219 delete $self->{idle}; 224 delete $self->{idle};
220 225
221 0; 226 0;
222 }; 227 };
223 228
224 1; 229 1;
225} 230}
231
232=item $board->set_board ($games_go_simpleboard)
233
234Sets the new board position to display from the current position stored in
235the L<Games::Go::SimpleBoard> object.
236
237=cut
226 238
227sub set_board { 239sub set_board {
228 my ($self, $board) = @_; 240 my ($self, $board) = @_;
229 241
230 $self->cursor (0); 242 $self->cursor (0);
323 my @kx = map int ($w2 * $_ / ($size+1) + $borderw + 0.5), 0 .. $size; $self->{kx} = \@kx; 335 my @kx = map int ($w2 * $_ / ($size+1) + $borderw + 0.5), 0 .. $size; $self->{kx} = \@kx;
324 my @ky = map int ($h2 * $_ / ($size+1) + $borderh + 0.5), 0 .. $size; $self->{ky} = \@ky; 336 my @ky = map int ($h2 * $_ / ($size+1) + $borderh + 0.5), 0 .. $size; $self->{ky} = \@ky;
325 337
326 my $pixbuf; 338 my $pixbuf;
327 339
328 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); 340 my ($bw, $bh) = ($board_img->get_width, $board_img->get_height);
329 341
330 if ($w < $bw && $h < $bh) { 342 if ($w < $bw && $h < $bh) {
331 $pixbuf = new_pixbuf $w, $h, 0; 343 $pixbuf = new_pixbuf $w, $h, 0;
332 $::board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0); 344 $board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0);
333 } else { 345 } else {
334 $pixbuf = scale_pixbuf $::board_img, $w, $h, $::config->{speed} ? 'nearest' : 'bilinear', 0; 346 $pixbuf = scale_pixbuf $board_img, $w, $h, 'bilinear', 0; # nearest for extra speed
335 } 347 }
336 348
337 my $linew = int ($w / 40 / $size); 349 my $linew = int ($w / 40 / $size);
338 350
339 # ornamental border... we have time to waste :/ 351 # ornamental border... we have time to waste :/
353 my $hs = 1 | int $edge / 4; 365 my $hs = 1 | int $edge / 4;
354 $hs = 5 if $hs < 5; 366 $hs = 5 if $hs < 5;
355 $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2; 367 $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2;
356 368
357 # we use the shadow mask... not perfect, but I want to finish this 369 # we use the shadow mask... not perfect, but I want to finish this
358 $::shadow_img->composite ($pixbuf, 370 $shadow_img->composite ($pixbuf,
359 $x, $y, ($hs + 1) x2, $x, $y, 371 $x, $y, ($hs + 1) x2, $x, $y,
360 $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height, 372 $hs / $shadow_img->get_width, $hs / $shadow_img->get_height,
361 'bilinear', 255); 373 'bilinear', 255);
362 }; 374 };
363 375
364 if ($size > 6) { 376 if ($size > 6) {
365 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset 377 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
416 my %stack; 428 my %stack;
417 429
418 my $put_stack = sub { 430 my $put_stack = sub {
419 my ($x, $y, $dx, $dy, $ox, $oy) = @_; 431 my ($x, $y, $dx, $dy, $ox, $oy) = @_;
420 432
421 my $mark = $self->{board}{board}[$x-1][$y-1]; 433 my $mark = $self->{board}[$x-1][$y-1];
422 434
423 if ($mark & ~MARK_LABEL) { 435 if ($mark & ~MARK_LABEL) {
424 my $stack = $stack{$mark} ||= $self->draw_stack ($mark, $edge); 436 my $stack = $stack{$mark} ||= $self->draw_stack ($mark, $edge);
425 437
426 $stack->[($x ^ $y) % @$stack] 438 $stack->[($x ^ $y) % @$stack]
453 $self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb, 465 $self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb,
454 0, 0, @areai, 'max', 0, 0); 466 0, 0, @areai, 'max', 0, 0);
455 467
456 # labels are handled here because they are quite rare 468 # labels are handled here because they are quite rare
457 # (and we can't draw text into pixbufs easily) 469 # (and we can't draw text into pixbufs easily)
458 my $mark = $self->{board}{board}[$x-1][$y-1]; 470 my $mark = $self->{board}[$x-1][$y-1];
459 471
460 if ($mark & MARK_LABEL) { 472 if ($mark & MARK_LABEL) {
461 my $white = $mark & MARK_W ? 0 : 0xffffff00; 473 my $white = $mark & MARK_W ? 0 : 0xffffff00;
462 474
463 if ($white) {
464 $self->center_text ($self->{backgroundpm}, 0, 475 $self->center_text ($self->{backgroundpm}, 0,
465 $areai[0] + $ofs * 1.1, $areai[1] + $ofs * 1.1, 476 $areai[0] + $ofs * 1.1, $areai[1] + $ofs * 1.1,
466 $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); 477 $ofs * 0.7, $self->{label}[$x-1][$y-1])
467 } 478 if $white;
479
468 $self->center_text ($self->{backgroundpm}, $white, 480 $self->center_text ($self->{backgroundpm}, $white,
469 $areai[0] + $ofs, $areai[1] + $ofs, 481 $areai[0] + $ofs, $areai[1] + $ofs,
470 $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); 482 $ofs * 0.7, $self->{label}[$x-1][$y-1]);
471 } 483 }
472 484
473 undef $pb; 485 undef $pb;
474 486
475 [@areai]; 487 [@areai];
483 495
484 my @stack; 496 my @stack;
485 my $csize = ceil $size; 497 my $csize = ceil $size;
486 my $shadow = $size * SHADOW; 498 my $shadow = $size * SHADOW;
487 499
488 for my $stone ($mark & MARK_W ? @::white_img : @::black_img) { 500 for my $stone ($mark & MARK_W ? @white_img : @black_img) {
489 my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000; 501 my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000;
490 502
491 # zeroeth the shadow 503 # zeroeth the shadow
492 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) { 504 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) {
493 $::shadow_img->composite ( 505 $shadow_img->composite (
494 $base, $shadow, $shadow, $csize, $csize, $shadow, $shadow, 506 $base, $shadow, $shadow, $csize, $csize, $shadow, $shadow,
495 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height, 507 $size / $shadow_img->get_width, $size / $shadow_img->get_height,
496 'bilinear', 128 508 'bilinear', 128
497 ); 509 );
498 } 510 }
499 511
500 for ([MARK_B, $mark & MARK_GRAYED ? 96 : 255, 1], 512 for ([MARK_B, $mark & MARK_GRAYED ? 106 : 255, 1],
501 [MARK_W, $mark & MARK_GRAYED ? 160 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) { 513 [MARK_W, $mark & MARK_GRAYED ? 190 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) {
502 my ($mask, $alpha, $scale) = @$_; 514 my ($mask, $alpha, $scale) = @$_;
503 if ($mark & $mask) { 515 if ($mark & $mask) {
504 $stone->composite ( 516 $stone->composite (
505 $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2, 517 $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2,
506 $size * $scale / $stone->get_width, $size * $scale / $stone->get_height, 518 $size * $scale / $stone->get_width, $size * $scale / $stone->get_height,
508 ); 520 );
509 } 521 }
510 } 522 }
511 523
512 # then the small stones (always using the first image) 524 # then the small stones (always using the first image)
513 for ([MARK_SMALL_B, $::black_img[0]], 525 for ([MARK_SMALL_B, $black_img[0]],
514 [MARK_SMALL_W, $::white_img[0]]) { 526 [MARK_SMALL_W, $white_img[0]]) {
515 my ($mask, $img) = @$_; 527 my ($mask, $img) = @$_;
516 if ($mark & $mask) { 528 if ($mark & $mask) {
517 $img->composite ( 529 $img->composite (
518 $base, (int $size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2, 530 $base, (int $size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2,
519 $size / $img->get_width / 2, $size / $img->get_height / 2, 531 $size / $img->get_width / 2, $size / $img->get_height / 2,
523 } 535 }
524 536
525 # and lastly any markers 537 # and lastly any markers
526 my $dark_bg = ! ! ($mark & MARK_B); 538 my $dark_bg = ! ! ($mark & MARK_B);
527 539
528 for ([MARK_CIRCLE, $::circle_img[$dark_bg]], 540 for ([MARK_CIRCLE, $circle_img[$dark_bg]],
529 [MARK_TRIANGLE, $::triangle_img[$dark_bg]], 541 [MARK_TRIANGLE, $triangle_img[$dark_bg]],
530 [MARK_SQUARE, $::square_img[$dark_bg]], 542 [MARK_SQUARE, $square_img[$dark_bg]],
543 [MARK_CROSS, $cross_img[$dark_bg]],
531 [MARK_KO, $::square_img[$dark_bg]]) { 544 [MARK_KO, $square_img[$dark_bg]]) {
532 my ($mask, $img) = @$_; 545 my ($mask, $img) = @$_;
533 if ($mark & $mask) { 546 if ($mark & $mask) {
534 $img->composite ( 547 $img->composite (
535 $base, 0, 0, $size, $size, 0, 0, 548 $base, 0, 0, $size, $size, 0, 0,
536 $size / $img->get_width, $size / $img->get_height, 549 $size / $img->get_width, $size / $img->get_height,
537 'bilinear', 176 550 'bilinear', $dark_bg ? 176 : 190
538 ); 551 );
539 } 552 }
540 } 553 }
541 554
542 push @stack, $base; 555 push @stack, $base;
543 } 556 }
544 557
545 \@stack; 558 \@stack
546} 559}
547 560
548sub draw_board { 561sub draw_board {
549 my ($self, $new, $dopaint) = @_; 562 my ($self, $new, $dopaint) = @_;
550 563
551 ($self->{board}, my $old) = ($new, $self->{board}); 564 my $newboard = $new->{board};
552 565 my $newlabel = $new->{label};
553 my $draw_stone = $self->{draw_stone};
554 566
555 if ($self->{backgroundpb}) { 567 if ($self->{backgroundpb}) {
568 my $draw_stone = $self->{draw_stone};
569
570 my $oldboard = $self->{board} ||= [];
571 my $oldlabel = $self->{label} ||= [];
572
556 my @areas; 573 my @areas;
557 574
558 my $size1 = $self->{size} - 1; 575 my $size1 = $self->{size} - 1;
559 576
560 for my $x (0 .. $size1) { 577 for my $x (0 .. $size1) {
561 my $old = $old->{board}[$x]; 578 my $old = $oldboard->[$x] ||= [];
562 my $new = $new->{board}[$x]; 579 my $new = $newboard->[$x];
563 580
564 for my $y (0 .. $size1) { 581 for my $y (0 .. $size1) {
582 next if $old->[$y] == $new->[$y];
583
584 $old -> [$y] = $new -> [$y];
585 $oldlabel->[$x][$y] = $newlabel->[$x][$y];
586
565 push @areas, $draw_stone->($x+1, $y+1) 587 push @areas, $draw_stone->($x+1, $y+1);
566 if $old->[$y] != $new->[$y];
567 } 588 }
568 } 589 }
569 590
570 if ($dopaint && @areas) { 591 if ($dopaint && @areas) {
571 # a single full clear_area is way faster than many single calls here 592 # a single full clear_area is way faster than many single calls here
578 } else { 599 } else {
579 # update all the affected rectangles 600 # update all the affected rectangles
580 $self->{window}->clear_area (@$_) for @areas; 601 $self->{window}->clear_area (@$_) for @areas;
581 } 602 }
582 } 603 }
604 } else {
605 no strict 'refs';
606
607 # straight copy
608 $self->{board} = [map [@$_], @$newboard];
609 $self->{label} = [map [@$_], @$newlabel];
583 } 610 }
584} 611}
585 612
586sub cursor { 613sub cursor {
587 my ($self, $show) = @_; 614 my ($self, $show) = @_;
590 && $self->{cursor} 617 && $self->{cursor}
591 && $self->{backgroundpb}; 618 && $self->{backgroundpb};
592 619
593 my ($x, $y) = @{$self->{cursorpos}}; 620 my ($x, $y) = @{$self->{cursorpos}};
594 621
595 my $mark = $self->{board}{board}[$x][$y]; 622 my $mark = $self->{board}[$x][$y];
596 623
597 $mark = $self->{cursor}->($mark, $x, $y) if $show; 624 $mark = $self->{cursor}->($mark, $x, $y) if $show;
598 625
599 local $self->{board}{board}[$x][$y] = $mark; 626 local $self->{board}[$x][$y] = $mark;
600 $self->{window}->clear_area (@{ $self->{draw_stone}->($x + 1, $y + 1) }); 627 $self->{window}->clear_area (@{ $self->{draw_stone}->($x + 1, $y + 1) });
601} 628}
602 629
603sub motion { 630sub motion {
604 my ($self) = @_; 631 my ($self) = @_;
673 socketpair $SOUND_SERVER, my $fh, &Socket::AF_UNIX, &Socket::SOCK_STREAM, 0 700 socketpair $SOUND_SERVER, my $fh, &Socket::AF_UNIX, &Socket::SOCK_STREAM, 0
674 or return; 701 or return;
675 702
676 my $pid = fork; 703 my $pid = fork;
677 704
678 if ($pid || !defined $pid) { 705 if ($pid) {
679 # parent 706 # parent
680 close $fh; 707 close $fh;
681 708
682 } else { 709 } elsif (defined $pid) {
683 # child 710 # child
684 close $SOUND_SERVER; 711 close $SOUND_SERVER;
685 712
713 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
686 my %sound; 720 my %sound;
687 721
688 while (<$SOUND_SERVER>) { 722 while (<$fh>) {
689 chomp; 723 chomp;
690 724
691 eval { 725 eval {
692 my $sound = $sound{$_} ||= do { 726 my $sound = $sound{$_} ||= do {
693 my $path = findfile "$_.au" 727 my $path = findfile "$_.au"
702 $data->Load ($fh); 736 $data->Load ($fh);
703 737
704 $data 738 $data
705 }; 739 };
706 740
707 my $server = new Audio::Play (1); 741 my $server = new Audio::Play;
708
709 $server->play ($sound); 742 $server->play ($sound);
710 $server->flush; 743 $server->flush;
711 }; 744 };
712 } 745 }
713 746
714 kill 9, $$; # required for windows, as a mere exit 747 # required for windows, as a mere _exit kills your parent process...
748 kill 9, $$;
749 } else {
750 undef $SOUND_SERVER;
751 return;
715 } 752 }
716 } 753 }
754
755 syswrite $SOUND_SERVER, "$_[0]\n";
717} 756}
718 757
7191; 7581;
720 759
721=back 760=back
748 $game->{board}[18][1] = MARK_W; 787 $game->{board}[18][1] = MARK_W;
749 788
750 my $board = new Gtk2::GoBoard; 789 my $board = new Gtk2::GoBoard;
751 $board->set_board ($game); 790 $board->set_board ($game);
752 791
792 Gtk2::GoBoard::play_sound "gamestart"; # ping
793
753 # enable cursor for black, till click 794 # enable cursor for black, till click
754 $board->set (cursor => sub { 795 $board->set (cursor => sub {
755 my ($mark, $x, $y) = @_; 796 my ($mark, $x, $y) = @_;
756 797
757 $mark |= MARK_GRAYED | MARK_B 798 $mark |= MARK_GRAYED | MARK_B
764 $board->signal_connect (button_release => sub { 805 $board->signal_connect (button_release => sub {
765 my ($board, $button, $x, $y) = @_; 806 my ($board, $button, $x, $y) = @_;
766 807
767 $game->{board}[$x][$y] |= MARK_CIRCLE; 808 $game->{board}[$x][$y] |= MARK_CIRCLE;
768 $board->set_board ($game); # force update 809 $board->set_board ($game); # force update
810
811 Gtk2::GoBoard::play_sound "move"; # play click sound
769 812
770 $board->set (cursor => undef); # disable cursor 813 $board->set (cursor => undef); # disable cursor
771 }); 814 });
772 815
773 my $w = new Gtk2::Window "toplevel"; 816 my $w = new Gtk2::Window "toplevel";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines