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.3 by root, Sun Jun 22 16:31:17 2008 UTC vs.
Revision 1.9 by root, Wed Jun 25 21:25:03 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 .. 5;
145 $::shadow_img = load_image "shadow.png"; 150 $shadow_img = load_image "shadow.png"; # also used to fake hoshi points
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]
435 $self->{draw_stone} = sub { 447 $self->{draw_stone} = sub {
436 my ($x, $y) = @_; 448 my ($x, $y) = @_;
437 449
438 @area = ($kx[$x] - $ofs, $ky[$y] - $ofs, 450 @area = ($kx[$x] - $ofs, $ky[$y] - $ofs,
439 $edge + $shadow, $edge + $shadow); 451 $edge + $shadow, $edge + $shadow);
440 @areai = ((ceil $area[0]), (ceil $area[1]), 452 @areai = map +(ceil $_), @area; # area, integer
441 (int $area[2]), (int $area[3])); # area, integer
442 453
443 $pb = new_pixbuf @areai[2,3]; 454 $pb = new_pixbuf @areai[2,3];
444 $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0); 455 $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0);
445 456
446 $put_stack->($x-1, $y, $kx[$x-1] - $kx[$x], 0, 0, 0) if $x > 1; 457 $put_stack->($x-1, $y, $kx[$x-1] - $kx[$x], 0, 0, 0) if $x > 1;
453 $self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb, 464 $self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb,
454 0, 0, @areai, 'max', 0, 0); 465 0, 0, @areai, 'max', 0, 0);
455 466
456 # labels are handled here because they are quite rare 467 # labels are handled here because they are quite rare
457 # (and we can't draw text into pixbufs easily) 468 # (and we can't draw text into pixbufs easily)
458 my $mark = $self->{board}{board}[$x-1][$y-1]; 469 my $mark = $self->{board}[$x-1][$y-1];
459 470
460 if ($mark & MARK_LABEL) { 471 if ($mark & MARK_LABEL) {
461 my $white = $mark & MARK_W ? 0 : 0xffffff00; 472 my $white = $mark & MARK_W ? 0 : 0xffffff00;
462 473
463 if ($white) {
464 $self->center_text ($self->{backgroundpm}, 0, 474 $self->center_text ($self->{backgroundpm}, 0,
465 $areai[0] + $ofs * 1.1, $areai[1] + $ofs * 1.1, 475 $areai[0] + $ofs * 1.1, $areai[1] + $ofs * 1.1,
466 $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); 476 $ofs * 0.7, $self->{label}[$x-1][$y-1])
467 } 477 if $white;
478
468 $self->center_text ($self->{backgroundpm}, $white, 479 $self->center_text ($self->{backgroundpm}, $white,
469 $areai[0] + $ofs, $areai[1] + $ofs, 480 $areai[0] + $ofs, $areai[1] + $ofs,
470 $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); 481 $ofs * 0.7, $self->{label}[$x-1][$y-1]);
471 } 482 }
472 483
473 undef $pb; 484 undef $pb;
474 485
475 [@areai]; 486 [@areai];
483 494
484 my @stack; 495 my @stack;
485 my $csize = ceil $size; 496 my $csize = ceil $size;
486 my $shadow = $size * SHADOW; 497 my $shadow = $size * SHADOW;
487 498
488 for my $stone ($mark & MARK_W ? @::white_img : @::black_img) { 499 for my $stone ($mark & MARK_W ? @white_img : @black_img) {
489 my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000; 500 my $base = new_pixbuf +(ceil $csize + $shadow) x2, 1, 0x00000000;
490 501
491 # zeroeth the shadow 502 # zeroeth the shadow
492 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) { 503 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) {
493 $::shadow_img->composite ( 504 $shadow_img->composite (
494 $base, $shadow, $shadow, $csize, $csize, $shadow, $shadow, 505 $base, ($shadow) x2, $csize, $csize, ($shadow) x2,
495 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height, 506 $size / $shadow_img->get_width, $size / $shadow_img->get_height,
496 'bilinear', 128 507 'bilinear', 128
497 ); 508 );
498 } 509 }
499 510
500 for ([MARK_B, $mark & MARK_GRAYED ? 96 : 255, 1], 511 for ([MARK_B, $mark & MARK_GRAYED ? 106 : 255, 1],
501 [MARK_W, $mark & MARK_GRAYED ? 160 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) { 512 [MARK_W, $mark & MARK_GRAYED ? 190 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) {
502 my ($mask, $alpha, $scale) = @$_; 513 my ($mask, $alpha, $scale) = @$_;
503 if ($mark & $mask) { 514 if ($mark & $mask) {
504 $stone->composite ( 515 $stone->composite (
505 $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2, 516 $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5) x2,
506 $size * $scale / $stone->get_width, $size * $scale / $stone->get_height, 517 $size * $scale / $stone->get_width, $size * $scale / $stone->get_height,
507 'bilinear', $alpha 518 'bilinear', $alpha
508 ); 519 );
509 } 520 }
510 } 521 }
511 522
512 # then the small stones (always using the first image) 523 # then the small stones (always using the first image)
513 for ([MARK_SMALL_B, $::black_img[0]], 524 for ([MARK_SMALL_B, $black_img[0]],
514 [MARK_SMALL_W, $::white_img[0]]) { 525 [MARK_SMALL_W, $white_img[0]]) {
515 my ($mask, $img) = @$_; 526 my ($mask, $img) = @$_;
516 if ($mark & $mask) { 527 if ($mark & $mask) {
517 $img->composite ( 528 $img->composite (
518 $base, (int $size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2, 529 $base, ($size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2,
519 $size / $img->get_width / 2, $size / $img->get_height / 2, 530 $size / $img->get_width / 2, $size / $img->get_height / 2,
520 'bilinear', 255 531 'bilinear', 255
521 ); 532 );
522 } 533 }
523 } 534 }
524 535
525 # and lastly any markers 536 # and finally any markers
526 my $dark_bg = ! ! ($mark & MARK_B); 537 my $dark_bg = ! ! ($mark & MARK_B);
527 538
528 for ([MARK_CIRCLE, $::circle_img[$dark_bg]], 539 for ([MARK_CIRCLE, $circle_img [$dark_bg]],
529 [MARK_TRIANGLE, $::triangle_img[$dark_bg]], 540 [MARK_TRIANGLE, $triangle_img[$dark_bg]],
541 [MARK_CROSS, $cross_img [$dark_bg]],
530 [MARK_SQUARE, $::square_img[$dark_bg]], 542 [MARK_SQUARE, $square_img [$dark_bg]],
531 [MARK_KO, $::square_img[$dark_bg]]) { 543 [MARK_KO, $square_img [$dark_bg]]) {
532 my ($mask, $img) = @$_; 544 my ($mask, $img) = @$_;
533 if ($mark & $mask) { 545 if ($mark & $mask) {
534 $img->composite ( 546 $img->composite (
535 $base, 0, 0, $size, $size, 0, 0, 547 $base, 0, 0, $csize, $csize, 0, 0,
536 $size / $img->get_width, $size / $img->get_height, 548 $size / $img->get_width, $size / $img->get_height,
537 'bilinear', 176 549 'bilinear', $dark_bg ? 176 : 190
538 ); 550 );
539 } 551 }
540 } 552 }
541 553
542 push @stack, $base; 554 push @stack, $base;
543 } 555 }
544 556
545 \@stack; 557 \@stack
546} 558}
547 559
548sub draw_board { 560sub draw_board {
549 my ($self, $new, $dopaint) = @_; 561 my ($self, $new, $dopaint) = @_;
550 562
551 ($self->{board}, my $old) = ($new, $self->{board}); 563 my $newboard = $new->{board};
552 564 my $newlabel = $new->{label};
553 my $draw_stone = $self->{draw_stone};
554 565
555 if ($self->{backgroundpb}) { 566 if ($self->{backgroundpb}) {
567 my $draw_stone = $self->{draw_stone};
568
569 my $oldboard = $self->{board} ||= [];
570 my $oldlabel = $self->{label} ||= [];
571
556 my @areas; 572 my @areas;
557 573
558 my $size1 = $self->{size} - 1; 574 my $size1 = $self->{size} - 1;
559 575
560 for my $x (0 .. $size1) { 576 for my $x (0 .. $size1) {
561 my $old = $old->{board}[$x]; 577 my $old = $oldboard->[$x] ||= [];
562 my $new = $new->{board}[$x]; 578 my $new = $newboard->[$x];
563 579
564 for my $y (0 .. $size1) { 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
565 push @areas, $draw_stone->($x+1, $y+1) 586 push @areas, $draw_stone->($x+1, $y+1);
566 if $old->[$y] != $new->[$y];
567 } 587 }
568 } 588 }
569 589
570 if ($dopaint && @areas) { 590 if ($dopaint && @areas) {
571 # a single full clear_area is way faster than many single calls here 591 # a single full clear_area is way faster than many single calls here
578 } else { 598 } else {
579 # update all the affected rectangles 599 # update all the affected rectangles
580 $self->{window}->clear_area (@$_) for @areas; 600 $self->{window}->clear_area (@$_) for @areas;
581 } 601 }
582 } 602 }
603 } else {
604 no strict 'refs';
605
606 # straight copy
607 $self->{board} = [map [@$_], @$newboard];
608 $self->{label} = [map [@$_], @$newlabel];
583 } 609 }
584} 610}
585 611
586sub cursor { 612sub cursor {
587 my ($self, $show) = @_; 613 my ($self, $show) = @_;
590 && $self->{cursor} 616 && $self->{cursor}
591 && $self->{backgroundpb}; 617 && $self->{backgroundpb};
592 618
593 my ($x, $y) = @{$self->{cursorpos}}; 619 my ($x, $y) = @{$self->{cursorpos}};
594 620
595 my $mark = $self->{board}{board}[$x][$y]; 621 my $mark = $self->{board}[$x][$y];
596 622
597 $mark = $self->{cursor}->($mark, $x, $y) if $show; 623 $mark = $self->{cursor}->($mark, $x, $y) if $show;
598 624
599 local $self->{board}{board}[$x][$y] = $mark; 625 local $self->{board}[$x][$y] = $mark;
600 $self->{window}->clear_area (@{ $self->{draw_stone}->($x + 1, $y + 1) }); 626 $self->{window}->clear_area (@{ $self->{draw_stone}->($x + 1, $y + 1) });
601} 627}
602 628
603sub motion { 629sub motion {
604 my ($self) = @_; 630 my ($self) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines