use utf8; package board; # my try at a real widget, needs the realobjects branch from Glib-CVS use Glib; use KGS::Constants; use KGS::Game::Board; use POSIX qw(ceil); register Glib::Type Gtk2::DrawingArea, __PACKAGE__, signals => {}, properties => {}; sub new { my ($self, %arg) = @_; $self = Glib::Object::new $self; while (my ($k, $v) = each %arg) { $self->{$k} = $v } $self; } sub INSTANCE_INIT { warn "instance_init @_\n"; my $self = shift; $self->double_buffered (0); $self->signal_connect(configure_event => \&configure_event); } sub size_allocate { my ($self, $alloc) = @_; my ($w, $h) = ($alloc->width, $alloc->height); my $s = $self->{width} = $w; warn "ALLOC($w,$h,$s)\n";#d# 1; } sub configure_event { my ($self, $event) = @_; warn "configure @_\n";#d# delete $self->{stack}; my $s = $self->{width} = $self->allocation->width; $self->{backgroundpb} = $self->draw_board ($s); $self->{backgroundpm} = new Gtk2::Gdk::Pixmap $self->window, $self->{width}, $self->{width}, -1; $self->{backgroundpm}->draw_pixbuf ($self->style->white_gc, $self->{backgroundpb}, 0, 0, 0, 0, $self->{width}, $self->{width}, "normal", 0, 0); $self->window->set_back_pixmap ($self->{backgroundpm}, 0); $self->repaint_board (delete $self->{board}, 0); $self->window->clear_area (0, 0, $self->{width}, $self->{width}); 1; } sub new_pixbuf { my ($w, $h, $alpha, $fill) = @_; my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h; $pixbuf->fill ($fill) if defined $fill; $pixbuf; } sub scale_pixbuf { my ($src, $w, $h, $mode, $alpha) = @_; my $dst = new_pixbuf $w, $h, $alpha; $src->scale( $dst, 0, 0, $w, $h, 0, 0, $w / $src->get_width, $h / $src->get_height, $mode, ); $dst; } # create a stack of stones sub create_stack { my ($self, $mark, $size, $rand) = @_; my $shadow = $size * 0.05; my $c = \$self->{stack}{$mark}; unless ($$c) { for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) { my $base = new_pixbuf $size + $shadow, $size + $shadow, 1, 0x00000000; # zeroeth the shadow if ($mark & (MARK_B | MARK_W)) { # the -0.5's are a mystery to me $::shadow_img->composite ( $base, $shadow, $shadow, $size, $size, $shadow - 0.5, $shadow - 0.5, $size / $::shadow_img->get_width, $size / $::shadow_img->get_height, $::config->{speed} ? 'tiles' : 'bilinear', 192 ); } # first the big stones (handicap stones could be different) for ([MARK_B, $mark & MARK_MOVE ? 255 : 255], [MARK_W, $mark & MARK_MOVE ? 255 : 255], [MARK_GRAY_B, 128], [MARK_GRAY_W, 128]) { my ($mask, $alpha) = @$_; if ($mark & $mask) { $stone->composite ( $base, 0, 0, $size, $size, 0, 0, $size / $stone->get_width, $size / $stone->get_height, $::config->{speed} ? 'tiles' : 'bilinear', $alpha ); } } # then the small stones for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]], [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) { my ($mask, $img) = @$_; if ($mark & $mask) { $img->composite ( $base, (int ($size / 4)) x2, (ceil ($size / 2 + 1)) x2, ($size / 4) x2, $size / $img->get_width / 2, $size / $img->get_height / 2, $::config->{speed} ? 'tiles' : 'bilinear', 224 ); } } # and lastly any markers my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B)); for ([MARK_CIRCLE, $::circle_img[$dark_bg]], [MARK_TRIANGLE, $::triangle_img[$dark_bg]], [MARK_SQUARE, $::square_img[$dark_bg]]) { my ($mask, $img) = @$_; if ($mark & $mask) { $img->composite ( $base, 0, 0, $size, $size, 0, 0, $size / $img->get_width, $size / $img->get_height, $::config->{speed} ? 'tiles' : 'bilinear', 192 ); } } push @$$c, $base; } } $$c->[$rand % @$$c]; } sub pixbuf_text { my ($pixbuf, $colour, $x, $y, $height, $text) = @_; my @c = grep $_, map $::font[$colour][$::fontmap{$_}], split //, $text; if (@c) { my $spacing = $height * 0.1; my $s = $height / List::Util::max map $_->get_height, @c; my $W = List::Util::sum map $_->get_width, @c; $x -= ($W * $s + $spacing * (@c - 1)) * 0.5; $y -= $height * 0.5; for (@c) { my $w = $_->get_width * $s; # +2 == don't fight the rounding $_->composite ($pixbuf, $x, $y, $w+2, $height+2, $x, $y, $s, $s, $::config->{speed} ? 'tiles' : 'bilinear', 255); $x += $w + $spacing; } } } sub pixbuf_rect { my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_; # we fake lines by... a horrible method :/ my $colour_pb = new_pixbuf 1, 1, 0, $colour; $colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1, 'nearest', $alpha); } sub set_board { my ($self, $board) = @_; $self->repaint_board ($board, 1); } # draw an empty board sub draw_board { my ($self, $s) = @_; my $canvas = $self->{canvas}; my $size = $self->{size}; # we leave enough space for the shadows.. I like smaller stones, and we # do no need to do the nifty recursive screen updates that goban2 does my $border = int ($s / ($size + 3) * 0.5); my $s2 = $s - $border * 2; my $edge = $self->{edge} = int ($s2 / ($size + 1) * 0.96) - ($::config->{randomize} ? 3 : 0); my $ofs = int ($edge / 2); my @k = map int ($s2 * $_ / ($size+1) + $border + 0.5), 0 .. $size; $self->{k} = \@k; my $pixbuf; my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); if ($s < $bw && $s < $bh) { $pixbuf = new_pixbuf $s, $s, 0; $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0); } else { $pixbuf = scale_pixbuf $::board_img, $s, $s, $::config->{speed} ? 'nearest' : 'bilinear', 0; } my $linew = int ($s / 40 / $size); # ornamental border... we have time to waste :/ pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $s-1, $linew, 255; pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $s-1, 255; pixbuf_rect $pixbuf, 0xffcc7700, $s-$linew-1, 0, $s-1, $s-1, 255; pixbuf_rect $pixbuf, 0xffcc7700, 0, $s-$linew-1, $s-1, $s-1, 255; for my $i (1 .. $size) { pixbuf_rect $pixbuf, 0x44111100, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew, 192; pixbuf_rect $pixbuf, 0x44111100, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew, 192; # 38 max, but we allow a bit more my $label = (qw(- A B C D E F G H J K L M N O P Q R S T U V W X Y Z AA BB CC DD EE FF GG HH JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX YY ZZ))[$i]; pixbuf_text $pixbuf, 0, $k[$i], $border, $ofs, $label; pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border, $ofs, $label; pixbuf_text $pixbuf, 0, $border, $k[$i], $ofs, $size - $i + 1; pixbuf_text $pixbuf, 0, $s2 + $border, $k[$i], $ofs, $size - $i + 1; $a++; $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK... } # hoshi points my $hoshi = sub { my ($x, $y) = @_; my $hs = int ($edge / 4) | 1; $x = $k[$x] - $hs / 2; $y = $k[$y] - $hs / 2; # we use the shadow mask... not perfect, but I want to finish this $::shadow_img->composite ($pixbuf, $x, $y, $hs + 1, $hs + 1, $x, $y, $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height, $::config->{speed} ? 'tiles' : 'bilinear', 255); }; my $h1 = $size < 10 ? 3 : 4; # corner / edge offset $hoshi->($h1, $h1); $hoshi->($size - $h1 + 1, $h1); $hoshi->($h1, $size - $h1 + 1); $hoshi->($size - $h1 + 1, $size - $h1 + 1); if ($size % 2) { # on odd boards, also the remaining 5 my $h2 = ($size + 1) / 2; if ($size > 10) { $hoshi->($h1, $h2); $hoshi->($size - $h1 + 1, $h2); $hoshi->($h2, $size - $h1 + 1); $hoshi->($h2, $h1); } # the tengen $hoshi->($h2, $h2); } $pixbuf; } sub repaint_board { my ($self, $board, $dopaint) = @_; my $old = $self->{board}; my $size = $self->{size}; my $edge = $self->{edge}; my $ofs = int ($edge * 0.5); my $k = $self->{k}; # repaint for my $x (1 .. $size) { for my $y (1 .. $size) { my $mark = $board->{board}[$x-1][$y-1]; my $old = $old->{board}[$x-1][$y-1]; if ($old != $mark) { my $rand = ($x ^ $y ^ 0x5555); #if ($::config->{randomize}) { # $dx += ($rand % 7) - 3; # $dy += ($rand / 3 % 7) - 3; #} my $shadow = $edge * 0.05; my @area = ($k->[$x] - $ofs, $k->[$y] - $ofs, $edge + $shadow, $edge + $shadow); my $pb = new_pixbuf @area[2,3]; $self->{backgroundpb}->copy_area (@area, $pb, 0, 0); if ($mark) { my $stack = $self->create_stack($mark, $edge, $rand); $stack->composite ($pb, 0, 0, @area[2,3], 0, 0, 1, 1, 'nearest', 255); # labels are handled here because they are quite rare if ($mark & MARK_LABEL) { my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 1; if ($white) { pixbuf_text $pb, 0, $ofs * 0.1, $ofs * 0.1, $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]; } pixbuf_text $pb, $white, 0, 0, $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]; } } # speed none, normal, max $pb->render_to_drawable ($self->{backgroundpm}, $self->style->black_gc, 0, 0, @area, 'max', 0, 0); # a single full clear_area is way faster than many single calls here $self->window->clear_area (@area) if $dopaint; } } } $self->{board} = $board; #d# save #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable"; } sub redraw { my ($self, $area) = @_; if ($area && $self->{pixbuf}) { my ($x, $y, $w, $h) = $area->values; $self->{canvas}->window->draw_pixbuf ($self->{canvas}->style->white_gc, $self->{pixbuf}, $x, $y, $x, $y, $w, $h, "normal", 0, 0); $self->{canvas}->window->draw_rectangle ($self->{canvas}->style->black_gc, 0, $x - 1, $y - 1, $w + 2, $h + 2) if $::DEBUG_EXPOSE; } } sub FINALIZE { warn "FINALIZE(@_)\n";#d# my ($self) = @_; $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy for BLACK, WHITE; $self->SUPER::destroy; delete $appwin::gamelist->{game}{$self->{channel}}; } 1;