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.2 by root, Sun Jun 22 16:25:50 2008 UTC vs.
Revision 1.14 by root, Tue Jul 29 10:13:05 2008 UTC

59 59
60=over 4 60=over 4
61 61
62=cut 62=cut
63 63
64our $VERSION = '1.0'; 64our $VERSION = '1.01';
65 65
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);
301 delete $self->{backgroundpm}; 313 delete $self->{backgroundpm};
302 delete $self->{backgroundpb}; 314 delete $self->{backgroundpb};
303 315
304 my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $w, $h, -1; 316 my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $w, $h, -1;
305 317
306 #my $gridcolour = 0x88444400; # black is traditional, but only with overlapping stones
307 my $gridcolour = 0x44444400; # black is traditional, but only with overlapping stones 318 my $gridcolour = 0x44444400; # black is traditional, but only with overlapping stones
308 my $labelcolour = 0x88444400; 319 my $labelcolour = 0x88444400;
309 320
310 # we leave enough space for the shadows.. I like smaller stones, and we
311 # do no need to do the nifty recursive screen updates that cgoban2 does
312 my $borderw = int ($w / ($size + 1) * 0.5); 321 my $borderw = int $w / ($size + 1) * 0.5;
313 my $borderh = $borderw; 322 my $borderh = $borderw;
314 my $w2 = $w - $borderw * 2; 323 my $w2 = $w - $borderw * 2;
315 my $h2 = $h - $borderh * 2; 324 my $h2 = $h - $borderh * 2;
316 my $edge = ceil ($w2 / ($size + 1)); 325 my $edge = ceil $w2 / ($size + 1);
317 my $ofs = $edge * 0.5; 326 my $ofs = $edge * 0.5;
318 327
319 # we need a certain minimum size, and just fudge some formula here 328 # we need a certain minimum size, and just fudge some formula here
320 return if $w < $size * 5 + 2 + $borderw 329 return if $w < $size * 5 + 2 + $borderw
321 || $h < $size * 6 + 2 + $borderh; 330 || $h < $size * 6 + 2 + $borderh;
323 my @kx = map int ($w2 * $_ / ($size+1) + $borderw + 0.5), 0 .. $size; $self->{kx} = \@kx; 332 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; 333 my @ky = map int ($h2 * $_ / ($size+1) + $borderh + 0.5), 0 .. $size; $self->{ky} = \@ky;
325 334
326 my $pixbuf; 335 my $pixbuf;
327 336
328 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); 337 my ($bw, $bh) = ($board_img->get_width, $board_img->get_height);
329 338
330 if ($w < $bw && $h < $bh) { 339 if ($w < $bw && $h < $bh) {
331 $pixbuf = new_pixbuf $w, $h, 0; 340 $pixbuf = new_pixbuf $w, $h, 0;
332 $::board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0); 341 $board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0);
333 } else { 342 } else {
334 $pixbuf = scale_pixbuf $::board_img, $w, $h, $::config->{speed} ? 'nearest' : 'bilinear', 0; 343 $pixbuf = scale_pixbuf $board_img, $w, $h, 'bilinear', 0; # nearest for extra speed
335 } 344 }
336 345
337 my $linew = int ($w / 40 / $size); 346 my $linew = int $w / 40 / $size;
338 347
339 # ornamental border... we have time to waste :/ 348 # ornamental border... we have time to waste :/
340 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $w-1, $linew, 255; 349 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $w-1, $linew, 255;
341 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $h-1, 255; 350 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $h-1, 255;
342 pixbuf_rect $pixbuf, 0xffcc7700, $w-$linew-1, 0, $w-1, $h-1, 255; 351 pixbuf_rect $pixbuf, 0xffcc7700, $w-$linew-1, 0, $w-1, $h-1, 255;
353 my $hs = 1 | int $edge / 4; 362 my $hs = 1 | int $edge / 4;
354 $hs = 5 if $hs < 5; 363 $hs = 5 if $hs < 5;
355 $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2; 364 $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2;
356 365
357 # we use the shadow mask... not perfect, but I want to finish this 366 # we use the shadow mask... not perfect, but I want to finish this
358 $::shadow_img->composite ($pixbuf, 367 $shadow_img->composite ($pixbuf,
359 $x, $y, ($hs + 1) x2, $x, $y, 368 $x, $y, ($hs + 1) x2, $x, $y,
360 $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height, 369 $hs / $shadow_img->get_width, $hs / $shadow_img->get_height,
361 'bilinear', 255); 370 'bilinear', 255);
362 }; 371 };
363 372
364 if ($size > 6) { 373 if ($size > 6) {
365 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset 374 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
416 my %stack; 425 my %stack;
417 426
418 my $put_stack = sub { 427 my $put_stack = sub {
419 my ($x, $y, $dx, $dy, $ox, $oy) = @_; 428 my ($x, $y, $dx, $dy, $ox, $oy) = @_;
420 429
421 my $mark = $self->{board}{board}[$x-1][$y-1]; 430 my $mark = $self->{board}[$x-1][$y-1];
422 431
423 if ($mark & ~MARK_LABEL) { 432 if ($mark & ~MARK_LABEL) {
424 my $stack = $stack{$mark} ||= $self->draw_stack ($mark, $edge); 433 my $stack = $stack{$mark} ||= $self->draw_stack ($mark, $edge);
425 434
426 $stack->[($x ^ $y) % @$stack] 435 $stack->[($x ^ $y) % @$stack]
435 $self->{draw_stone} = sub { 444 $self->{draw_stone} = sub {
436 my ($x, $y) = @_; 445 my ($x, $y) = @_;
437 446
438 @area = ($kx[$x] - $ofs, $ky[$y] - $ofs, 447 @area = ($kx[$x] - $ofs, $ky[$y] - $ofs,
439 $edge + $shadow, $edge + $shadow); 448 $edge + $shadow, $edge + $shadow);
440 @areai = ((ceil $area[0]), (ceil $area[1]), 449 @areai = map +(ceil $_), @area; # area, integer
441 (int $area[2]), (int $area[3])); # area, integer
442 450
443 $pb = new_pixbuf @areai[2,3]; 451 $pb = new_pixbuf @areai[2,3];
444 $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0); 452 $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0);
445 453
446 $put_stack->($x-1, $y, $kx[$x-1] - $kx[$x], 0, 0, 0) if $x > 1; 454 $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, 461 $self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb,
454 0, 0, @areai, 'max', 0, 0); 462 0, 0, @areai, 'max', 0, 0);
455 463
456 # labels are handled here because they are quite rare 464 # labels are handled here because they are quite rare
457 # (and we can't draw text into pixbufs easily) 465 # (and we can't draw text into pixbufs easily)
458 my $mark = $self->{board}{board}[$x-1][$y-1]; 466 my $mark = $self->{board}[$x-1][$y-1];
459 467
460 if ($mark & MARK_LABEL) { 468 if ($mark & MARK_LABEL) {
461 my $white = $mark & MARK_W ? 0 : 0xffffff00; 469 my $white = $mark & MARK_W ? 0 : 0xffffff00;
462 470
463 if ($white) {
464 $self->center_text ($self->{backgroundpm}, 0, 471 $self->center_text ($self->{backgroundpm}, 0,
465 $areai[0] + $ofs * 1.1, $areai[1] + $ofs * 1.1, 472 $areai[0] + $ofs * 1.1, $areai[1] + $ofs * 1.1,
466 $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); 473 $ofs * 0.7, $self->{label}[$x-1][$y-1])
467 } 474 if $white;
475
468 $self->center_text ($self->{backgroundpm}, $white, 476 $self->center_text ($self->{backgroundpm}, $white,
469 $areai[0] + $ofs, $areai[1] + $ofs, 477 $areai[0] + $ofs, $areai[1] + $ofs,
470 $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); 478 $ofs * 0.7, $self->{label}[$x-1][$y-1]);
471 } 479 }
472 480
473 undef $pb; 481 undef $pb;
474 482
475 [@areai]; 483 [@areai];
483 491
484 my @stack; 492 my @stack;
485 my $csize = ceil $size; 493 my $csize = ceil $size;
486 my $shadow = $size * SHADOW; 494 my $shadow = $size * SHADOW;
487 495
488 for my $stone ($mark & MARK_W ? @::white_img : @::black_img) { 496 for my $stone ($mark & MARK_W ? @white_img : @black_img) {
489 my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000; 497 my $base = new_pixbuf +(ceil $csize + $shadow) x2, 1, 0x00000000;
490 498
491 # zeroeth the shadow 499 # zeroeth the shadow
492 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) { 500 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) {
493 $::shadow_img->composite ( 501 $shadow_img->composite (
494 $base, $shadow, $shadow, $csize, $csize, $shadow, $shadow, 502 $base, ($shadow) x2, $csize, $csize, ($shadow) x2,
495 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height, 503 $size / $shadow_img->get_width, $size / $shadow_img->get_height,
496 'bilinear', 128 504 'bilinear', 128
497 ); 505 );
498 } 506 }
499 507
500 for ([MARK_B, $mark & MARK_GRAYED ? 96 : 255, 1], 508 for ([MARK_B, $mark & MARK_GRAYED ? 106 : 255, 1],
501 [MARK_W, $mark & MARK_GRAYED ? 160 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) { 509 [MARK_W, $mark & MARK_GRAYED ? 190 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) {
502 my ($mask, $alpha, $scale) = @$_; 510 my ($mask, $alpha, $scale) = @$_;
503 if ($mark & $mask) { 511 if ($mark & $mask) {
504 $stone->composite ( 512 $stone->composite (
505 $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2, 513 $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5) x2,
506 $size * $scale / $stone->get_width, $size * $scale / $stone->get_height, 514 $size * $scale / $stone->get_width, $size * $scale / $stone->get_height,
507 'bilinear', $alpha 515 'bilinear', $alpha
508 ); 516 );
509 } 517 }
510 } 518 }
511 519
512 # then the small stones (always using the first image) 520 # then the small stones (always using the first image)
513 for ([MARK_SMALL_B, $::black_img[0]], 521 for ([MARK_SMALL_B, $mark & MARK_SMALL_GRAYED ? 106 : 255, $black_img[0]],
514 [MARK_SMALL_W, $::white_img[0]]) { 522 [MARK_SMALL_W, $mark & MARK_SMALL_GRAYED ? 190 : 255, $white_img[0]]) {
515 my ($mask, $img) = @$_; 523 my ($mask, $alpha, $img) = @$_;
516 if ($mark & $mask) { 524 if ($mark & $mask) {
517 $img->composite ( 525 $img->composite (
518 $base, (int $size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2, 526 $base, ($size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2,
519 $size / $img->get_width / 2, $size / $img->get_height / 2, 527 $size / $img->get_width / 2, $size / $img->get_height / 2,
520 'bilinear', 255 528 'bilinear', $alpha
521 ); 529 );
522 } 530 }
523 } 531 }
524 532
525 # and lastly any markers 533 # and finally any markers
526 my $dark_bg = ! ! ($mark & MARK_B); 534 my $dark_bg = ! ! ($mark & MARK_B);
527 535
528 for ([MARK_CIRCLE, $::circle_img[$dark_bg]], 536 for ([MARK_CIRCLE, $circle_img [$dark_bg]],
529 [MARK_TRIANGLE, $::triangle_img[$dark_bg]], 537 [MARK_TRIANGLE, $triangle_img[$dark_bg]],
538 [MARK_CROSS, $cross_img [$dark_bg]],
530 [MARK_SQUARE, $::square_img[$dark_bg]], 539 [MARK_SQUARE, $square_img [$dark_bg]],
531 [MARK_KO, $::square_img[$dark_bg]]) { 540 [MARK_KO, $square_img [$dark_bg]]) {
532 my ($mask, $img) = @$_; 541 my ($mask, $img) = @$_;
533 if ($mark & $mask) { 542 if ($mark & $mask) {
534 $img->composite ( 543 $img->composite (
535 $base, 0, 0, $size, $size, 0, 0, 544 $base, 0, 0, $csize, $csize, 0, 0,
536 $size / $img->get_width, $size / $img->get_height, 545 $size / $img->get_width, $size / $img->get_height,
537 'bilinear', 176 546 'bilinear', $dark_bg ? 176 : 190
538 ); 547 );
539 } 548 }
540 } 549 }
541 550
542 push @stack, $base; 551 push @stack, $base;
543 } 552 }
544 553
545 \@stack; 554 \@stack
546} 555}
547 556
548sub draw_board { 557sub draw_board {
549 my ($self, $new, $dopaint) = @_; 558 my ($self, $new, $dopaint) = @_;
550 559
551 ($self->{board}, my $old) = ($new, $self->{board}); 560 my $newboard = $new->{board};
552 561 my $newlabel = $new->{label};
553 my $draw_stone = $self->{draw_stone};
554 562
555 if ($self->{backgroundpb}) { 563 if ($self->{backgroundpb}) {
564 my $draw_stone = $self->{draw_stone};
565
566 my $oldboard = $self->{board} ||= [];
567 my $oldlabel = $self->{label} ||= [];
568
556 my @areas; 569 my @areas;
557 570
558 my $size1 = $self->{size} - 1; 571 my $size1 = $self->{size} - 1;
559 572
560 for my $x (0 .. $size1) { 573 for my $x (0 .. $size1) {
561 my $old = $old->{board}[$x]; 574 my $old = $oldboard->[$x] ||= [];
562 my $new = $new->{board}[$x]; 575 my $new = $newboard->[$x];
563 576
564 for my $y (0 .. $size1) { 577 for my $y (0 .. $size1) {
578 next if $old->[$y] == $new->[$y];
579
580 $old -> [$y] = $new -> [$y];
581 $oldlabel->[$x][$y] = $newlabel->[$x][$y];
582
565 push @areas, $draw_stone->($x+1, $y+1) 583 push @areas, $draw_stone->($x+1, $y+1);
566 if $old->[$y] != $new->[$y];
567 } 584 }
568 } 585 }
569 586
570 if ($dopaint && @areas) { 587 if ($dopaint && @areas) {
571 # a single full clear_area is way faster than many single calls here 588 # a single full clear_area is way faster than many single calls here
578 } else { 595 } else {
579 # update all the affected rectangles 596 # update all the affected rectangles
580 $self->{window}->clear_area (@$_) for @areas; 597 $self->{window}->clear_area (@$_) for @areas;
581 } 598 }
582 } 599 }
600 } else {
601 no strict 'refs';
602
603 # straight copy
604 $self->{board} = [map [@$_], @$newboard];
605 $self->{label} = [map [@$_], @$newlabel];
583 } 606 }
584} 607}
585 608
586sub cursor { 609sub cursor {
587 my ($self, $show) = @_; 610 my ($self, $show) = @_;
590 && $self->{cursor} 613 && $self->{cursor}
591 && $self->{backgroundpb}; 614 && $self->{backgroundpb};
592 615
593 my ($x, $y) = @{$self->{cursorpos}}; 616 my ($x, $y) = @{$self->{cursorpos}};
594 617
595 my $mark = $self->{board}{board}[$x][$y]; 618 my $mark = $self->{board}[$x][$y];
596 619
597 $mark = $self->{cursor}->($mark, $x, $y) if $show; 620 $mark = $self->{cursor}->($mark, $x, $y) if $show;
598 621
599 local $self->{board}{board}[$x][$y] = $mark; 622 local $self->{board}[$x][$y] = $mark;
600 $self->{window}->clear_area (@{ $self->{draw_stone}->($x + 1, $y + 1) }); 623 $self->{window}->clear_area (@{ $self->{draw_stone}->($x + 1, $y + 1) });
601} 624}
602 625
603sub motion { 626sub motion {
604 my ($self) = @_; 627 my ($self) = @_;
612 635
613 my $x = int (($x - $self->{kx}[0]) * $size / ($self->{kx}[$size] - $self->{kx}[0]) + 0.5) - 1; 636 my $x = int (($x - $self->{kx}[0]) * $size / ($self->{kx}[$size] - $self->{kx}[0]) + 0.5) - 1;
614 my $y = int (($y - $self->{ky}[0]) * $size / ($self->{ky}[$size] - $self->{ky}[0]) + 0.5) - 1; 637 my $y = int (($y - $self->{ky}[0]) * $size / ($self->{ky}[$size] - $self->{ky}[0]) + 0.5) - 1;
615 638
616 my $pos = $self->{cursorpos}; 639 my $pos = $self->{cursorpos};
617 if ($x != $pos->[0] || $y != $pos->[1]) { 640 if ((not (defined $pos) && $x >= 0 && $x < $size && $y >= 0 && $y < $size)
641 || $x != $pos->[0]
642 || $y != $pos->[1]) {
618 643
619 $self->cursor (0); 644 $self->cursor (0);
620 645
621 if ($x >= 0 && $x < $size 646 if ($x >= 0 && $x < $size
622 && $y >= 0 && $y < $size) { 647 && $y >= 0 && $y < $size) {
715 $server->play ($sound); 740 $server->play ($sound);
716 $server->flush; 741 $server->flush;
717 }; 742 };
718 } 743 }
719 744
720 kill 9, $$; # required for windows, as a mere exit 745 # required for windows, as a mere _exit kills your parent process...
746 kill 9, $$;
721 } else { 747 } else {
722 undef $SOUND_SERVER; 748 undef $SOUND_SERVER;
723 return; 749 return;
724 } 750 }
725 } 751 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines