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.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);
308 delete $self->{backgroundpm}; 313 delete $self->{backgroundpm};
309 delete $self->{backgroundpb}; 314 delete $self->{backgroundpb};
310 315
311 my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $w, $h, -1; 316 my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $w, $h, -1;
312 317
313 #my $gridcolour = 0x88444400; # black is traditional, but only with overlapping stones
314 my $gridcolour = 0x44444400; # black is traditional, but only with overlapping stones 318 my $gridcolour = 0x44444400; # black is traditional, but only with overlapping stones
315 my $labelcolour = 0x88444400; 319 my $labelcolour = 0x88444400;
316 320
317 # we leave enough space for the shadows.. I like smaller stones, and we
318 # do no need to do the nifty recursive screen updates that cgoban2 does
319 my $borderw = int ($w / ($size + 1) * 0.5); 321 my $borderw = int $w / ($size + 1) * 0.5;
320 my $borderh = $borderw; 322 my $borderh = $borderw;
321 my $w2 = $w - $borderw * 2; 323 my $w2 = $w - $borderw * 2;
322 my $h2 = $h - $borderh * 2; 324 my $h2 = $h - $borderh * 2;
323 my $edge = ceil ($w2 / ($size + 1)); 325 my $edge = ceil $w2 / ($size + 1);
324 my $ofs = $edge * 0.5; 326 my $ofs = $edge * 0.5;
325 327
326 # 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
327 return if $w < $size * 5 + 2 + $borderw 329 return if $w < $size * 5 + 2 + $borderw
328 || $h < $size * 6 + 2 + $borderh; 330 || $h < $size * 6 + 2 + $borderh;
330 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;
331 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;
332 334
333 my $pixbuf; 335 my $pixbuf;
334 336
335 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); 337 my ($bw, $bh) = ($board_img->get_width, $board_img->get_height);
336 338
337 if ($w < $bw && $h < $bh) { 339 if ($w < $bw && $h < $bh) {
338 $pixbuf = new_pixbuf $w, $h, 0; 340 $pixbuf = new_pixbuf $w, $h, 0;
339 $::board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0); 341 $board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0);
340 } else { 342 } else {
341 $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
342 } 344 }
343 345
344 my $linew = int ($w / 40 / $size); 346 my $linew = int $w / 40 / $size;
345 347
346 # ornamental border... we have time to waste :/ 348 # ornamental border... we have time to waste :/
347 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $w-1, $linew, 255; 349 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $w-1, $linew, 255;
348 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $h-1, 255; 350 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $h-1, 255;
349 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;
360 my $hs = 1 | int $edge / 4; 362 my $hs = 1 | int $edge / 4;
361 $hs = 5 if $hs < 5; 363 $hs = 5 if $hs < 5;
362 $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2; 364 $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2;
363 365
364 # 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
365 $::shadow_img->composite ($pixbuf, 367 $shadow_img->composite ($pixbuf,
366 $x, $y, ($hs + 1) x2, $x, $y, 368 $x, $y, ($hs + 1) x2, $x, $y,
367 $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height, 369 $hs / $shadow_img->get_width, $hs / $shadow_img->get_height,
368 'bilinear', 255); 370 'bilinear', 255);
369 }; 371 };
370 372
371 if ($size > 6) { 373 if ($size > 6) {
372 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset 374 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
442 $self->{draw_stone} = sub { 444 $self->{draw_stone} = sub {
443 my ($x, $y) = @_; 445 my ($x, $y) = @_;
444 446
445 @area = ($kx[$x] - $ofs, $ky[$y] - $ofs, 447 @area = ($kx[$x] - $ofs, $ky[$y] - $ofs,
446 $edge + $shadow, $edge + $shadow); 448 $edge + $shadow, $edge + $shadow);
447 @areai = ((ceil $area[0]), (ceil $area[1]), 449 @areai = map +(ceil $_), @area; # area, integer
448 (int $area[2]), (int $area[3])); # area, integer
449 450
450 $pb = new_pixbuf @areai[2,3]; 451 $pb = new_pixbuf @areai[2,3];
451 $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0); 452 $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0);
452 453
453 $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;
490 491
491 my @stack; 492 my @stack;
492 my $csize = ceil $size; 493 my $csize = ceil $size;
493 my $shadow = $size * SHADOW; 494 my $shadow = $size * SHADOW;
494 495
495 for my $stone ($mark & MARK_W ? @::white_img : @::black_img) { 496 for my $stone ($mark & MARK_W ? @white_img : @black_img) {
496 my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000; 497 my $base = new_pixbuf +(ceil $csize + $shadow) x2, 1, 0x00000000;
497 498
498 # zeroeth the shadow 499 # zeroeth the shadow
499 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) { 500 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) {
500 $::shadow_img->composite ( 501 $shadow_img->composite (
501 $base, $shadow, $shadow, $csize, $csize, $shadow, $shadow, 502 $base, ($shadow) x2, $csize, $csize, ($shadow) x2,
502 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height, 503 $size / $shadow_img->get_width, $size / $shadow_img->get_height,
503 'bilinear', 128 504 'bilinear', 128
504 ); 505 );
505 } 506 }
506 507
507 for ([MARK_B, $mark & MARK_GRAYED ? 96 : 255, 1], 508 for ([MARK_B, $mark & MARK_GRAYED ? 106 : 255, 1],
508 [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]) {
509 my ($mask, $alpha, $scale) = @$_; 510 my ($mask, $alpha, $scale) = @$_;
510 if ($mark & $mask) { 511 if ($mark & $mask) {
511 $stone->composite ( 512 $stone->composite (
512 $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2, 513 $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5) x2,
513 $size * $scale / $stone->get_width, $size * $scale / $stone->get_height, 514 $size * $scale / $stone->get_width, $size * $scale / $stone->get_height,
514 'bilinear', $alpha 515 'bilinear', $alpha
515 ); 516 );
516 } 517 }
517 } 518 }
518 519
519 # then the small stones (always using the first image) 520 # then the small stones (always using the first image)
520 for ([MARK_SMALL_B, $::black_img[0]], 521 for ([MARK_SMALL_B, $mark & MARK_SMALL_GRAYED ? 106 : 255, $black_img[0]],
521 [MARK_SMALL_W, $::white_img[0]]) { 522 [MARK_SMALL_W, $mark & MARK_SMALL_GRAYED ? 190 : 255, $white_img[0]]) {
522 my ($mask, $img) = @$_; 523 my ($mask, $alpha, $img) = @$_;
523 if ($mark & $mask) { 524 if ($mark & $mask) {
524 $img->composite ( 525 $img->composite (
525 $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,
526 $size / $img->get_width / 2, $size / $img->get_height / 2, 527 $size / $img->get_width / 2, $size / $img->get_height / 2,
527 'bilinear', 255 528 'bilinear', $alpha
528 ); 529 );
529 } 530 }
530 } 531 }
531 532
532 # and lastly any markers 533 # and finally any markers
533 my $dark_bg = ! ! ($mark & MARK_B); 534 my $dark_bg = ! ! ($mark & MARK_B);
534 535
535 for ([MARK_CIRCLE, $::circle_img[$dark_bg]], 536 for ([MARK_CIRCLE, $circle_img [$dark_bg]],
536 [MARK_TRIANGLE, $::triangle_img[$dark_bg]], 537 [MARK_TRIANGLE, $triangle_img[$dark_bg]],
538 [MARK_CROSS, $cross_img [$dark_bg]],
537 [MARK_SQUARE, $::square_img[$dark_bg]], 539 [MARK_SQUARE, $square_img [$dark_bg]],
538 [MARK_KO, $::square_img[$dark_bg]]) { 540 [MARK_KO, $square_img [$dark_bg]]) {
539 my ($mask, $img) = @$_; 541 my ($mask, $img) = @$_;
540 if ($mark & $mask) { 542 if ($mark & $mask) {
541 $img->composite ( 543 $img->composite (
542 $base, 0, 0, $size, $size, 0, 0, 544 $base, 0, 0, $csize, $csize, 0, 0,
543 $size / $img->get_width, $size / $img->get_height, 545 $size / $img->get_width, $size / $img->get_height,
544 'bilinear', 176 546 'bilinear', $dark_bg ? 176 : 190
545 ); 547 );
546 } 548 }
547 } 549 }
548 550
549 push @stack, $base; 551 push @stack, $base;
633 635
634 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;
635 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;
636 638
637 my $pos = $self->{cursorpos}; 639 my $pos = $self->{cursorpos};
638 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]) {
639 643
640 $self->cursor (0); 644 $self->cursor (0);
641 645
642 if ($x >= 0 && $x < $size 646 if ($x >= 0 && $x < $size
643 && $y >= 0 && $y < $size) { 647 && $y >= 0 && $y < $size) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines