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.4 by root, Mon Jun 23 00:28:13 2008 UTC vs.
Revision 1.10 by elmex, Fri Jun 27 13:08:06 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);
330 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;
331 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;
332 337
333 my $pixbuf; 338 my $pixbuf;
334 339
335 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); 340 my ($bw, $bh) = ($board_img->get_width, $board_img->get_height);
336 341
337 if ($w < $bw && $h < $bh) { 342 if ($w < $bw && $h < $bh) {
338 $pixbuf = new_pixbuf $w, $h, 0; 343 $pixbuf = new_pixbuf $w, $h, 0;
339 $::board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0); 344 $board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0);
340 } else { 345 } else {
341 $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
342 } 347 }
343 348
344 my $linew = int ($w / 40 / $size); 349 my $linew = int ($w / 40 / $size);
345 350
346 # ornamental border... we have time to waste :/ 351 # ornamental border... we have time to waste :/
360 my $hs = 1 | int $edge / 4; 365 my $hs = 1 | int $edge / 4;
361 $hs = 5 if $hs < 5; 366 $hs = 5 if $hs < 5;
362 $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2; 367 $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2;
363 368
364 # 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
365 $::shadow_img->composite ($pixbuf, 370 $shadow_img->composite ($pixbuf,
366 $x, $y, ($hs + 1) x2, $x, $y, 371 $x, $y, ($hs + 1) x2, $x, $y,
367 $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height, 372 $hs / $shadow_img->get_width, $hs / $shadow_img->get_height,
368 'bilinear', 255); 373 'bilinear', 255);
369 }; 374 };
370 375
371 if ($size > 6) { 376 if ($size > 6) {
372 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset 377 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
442 $self->{draw_stone} = sub { 447 $self->{draw_stone} = sub {
443 my ($x, $y) = @_; 448 my ($x, $y) = @_;
444 449
445 @area = ($kx[$x] - $ofs, $ky[$y] - $ofs, 450 @area = ($kx[$x] - $ofs, $ky[$y] - $ofs,
446 $edge + $shadow, $edge + $shadow); 451 $edge + $shadow, $edge + $shadow);
447 @areai = ((ceil $area[0]), (ceil $area[1]), 452 @areai = map +(ceil $_), @area; # area, integer
448 (int $area[2]), (int $area[3])); # area, integer
449 453
450 $pb = new_pixbuf @areai[2,3]; 454 $pb = new_pixbuf @areai[2,3];
451 $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0); 455 $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0);
452 456
453 $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;
490 494
491 my @stack; 495 my @stack;
492 my $csize = ceil $size; 496 my $csize = ceil $size;
493 my $shadow = $size * SHADOW; 497 my $shadow = $size * SHADOW;
494 498
495 for my $stone ($mark & MARK_W ? @::white_img : @::black_img) { 499 for my $stone ($mark & MARK_W ? @white_img : @black_img) {
496 my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000; 500 my $base = new_pixbuf +(ceil $csize + $shadow) x2, 1, 0x00000000;
497 501
498 # zeroeth the shadow 502 # zeroeth the shadow
499 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) { 503 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) {
500 $::shadow_img->composite ( 504 $shadow_img->composite (
501 $base, $shadow, $shadow, $csize, $csize, $shadow, $shadow, 505 $base, ($shadow) x2, $csize, $csize, ($shadow) x2,
502 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height, 506 $size / $shadow_img->get_width, $size / $shadow_img->get_height,
503 'bilinear', 128 507 'bilinear', 128
504 ); 508 );
505 } 509 }
506 510
507 for ([MARK_B, $mark & MARK_GRAYED ? 96 : 255, 1], 511 for ([MARK_B, $mark & MARK_GRAYED ? 106 : 255, 1],
508 [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]) {
509 my ($mask, $alpha, $scale) = @$_; 513 my ($mask, $alpha, $scale) = @$_;
510 if ($mark & $mask) { 514 if ($mark & $mask) {
511 $stone->composite ( 515 $stone->composite (
512 $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2, 516 $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5) x2,
513 $size * $scale / $stone->get_width, $size * $scale / $stone->get_height, 517 $size * $scale / $stone->get_width, $size * $scale / $stone->get_height,
514 'bilinear', $alpha 518 'bilinear', $alpha
515 ); 519 );
516 } 520 }
517 } 521 }
518 522
519 # then the small stones (always using the first image) 523 # then the small stones (always using the first image)
520 for ([MARK_SMALL_B, $::black_img[0]], 524 for ([MARK_SMALL_B, $black_img[0]],
521 [MARK_SMALL_W, $::white_img[0]]) { 525 [MARK_SMALL_W, $white_img[0]]) {
522 my ($mask, $img) = @$_; 526 my ($mask, $img) = @$_;
523 if ($mark & $mask) { 527 if ($mark & $mask) {
524 $img->composite ( 528 $img->composite (
525 $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,
526 $size / $img->get_width / 2, $size / $img->get_height / 2, 530 $size / $img->get_width / 2, $size / $img->get_height / 2,
527 'bilinear', 255 531 'bilinear', 255
528 ); 532 );
529 } 533 }
530 } 534 }
531 535
532 # and lastly any markers 536 # and finally any markers
533 my $dark_bg = ! ! ($mark & MARK_B); 537 my $dark_bg = ! ! ($mark & MARK_B);
534 538
535 for ([MARK_CIRCLE, $::circle_img[$dark_bg]], 539 for ([MARK_CIRCLE, $circle_img [$dark_bg]],
536 [MARK_TRIANGLE, $::triangle_img[$dark_bg]], 540 [MARK_TRIANGLE, $triangle_img[$dark_bg]],
541 [MARK_CROSS, $cross_img [$dark_bg]],
537 [MARK_SQUARE, $::square_img[$dark_bg]], 542 [MARK_SQUARE, $square_img [$dark_bg]],
538 [MARK_KO, $::square_img[$dark_bg]]) { 543 [MARK_KO, $square_img [$dark_bg]]) {
539 my ($mask, $img) = @$_; 544 my ($mask, $img) = @$_;
540 if ($mark & $mask) { 545 if ($mark & $mask) {
541 $img->composite ( 546 $img->composite (
542 $base, 0, 0, $size, $size, 0, 0, 547 $base, 0, 0, $csize, $csize, 0, 0,
543 $size / $img->get_width, $size / $img->get_height, 548 $size / $img->get_width, $size / $img->get_height,
544 'bilinear', 176 549 'bilinear', $dark_bg ? 176 : 190
545 ); 550 );
546 } 551 }
547 } 552 }
548 553
549 push @stack, $base; 554 push @stack, $base;
633 638
634 my $x = int (($x - $self->{kx}[0]) * $size / ($self->{kx}[$size] - $self->{kx}[0]) + 0.5) - 1; 639 my $x = int (($x - $self->{kx}[0]) * $size / ($self->{kx}[$size] - $self->{kx}[0]) + 0.5) - 1;
635 my $y = int (($y - $self->{ky}[0]) * $size / ($self->{ky}[$size] - $self->{ky}[0]) + 0.5) - 1; 640 my $y = int (($y - $self->{ky}[0]) * $size / ($self->{ky}[$size] - $self->{ky}[0]) + 0.5) - 1;
636 641
637 my $pos = $self->{cursorpos}; 642 my $pos = $self->{cursorpos};
638 if ($x != $pos->[0] || $y != $pos->[1]) { 643 if ((not (defined $pos) && $x >= 0 && $x < $size && $y >= 0 && $y < $size)
644 || $x != $pos->[0]
645 || $y != $pos->[1]) {
639 646
640 $self->cursor (0); 647 $self->cursor (0);
641 648
642 if ($x >= 0 && $x < $size 649 if ($x >= 0 && $x < $size
643 && $y >= 0 && $y < $size) { 650 && $y >= 0 && $y < $size) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines