package Gtk2::GoBoard; # 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 INIT_INSTANCE { 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) = @_; delete $self->{stack}; my $s = $self->{width} = $self->allocation->width; $self->draw_board ($s); $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', 176 ); } } push @$$c, $base; } } $$c->[$rand % @$$c]; } 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); } sub center_text { my ($self, $drawable, $colour, $x, $y, $size, $text) = @_; my $context = $self->get_pango_context; my $font = $context->get_font_description; $font->set_size ($size * Gtk2::Pango->scale); my $layout = new Gtk2::Pango::Layout $context; $layout->set_text ($text); my ($w, $h) = $layout->get_pixel_size; #d# does not work my $gc = $self->style->black_gc; $gc->set_foreground (new Gtk2::Gdk::Color +($colour > 24) & 255, +($colour > 16) & 255, +($colour > 8) & 255); $drawable->draw_layout ($gc, $x - $w*0.5, $y - $h*0.5, $layout); } # draw an empty board and attach the bg pixmap sub draw_board { my ($self, $s) = @_; my $canvas = $self->{canvas}; my $size = $self->{size}; my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $self->{width}, $self->{width}, -1; # 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.975 - 1); 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; } # 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); } # now we have a board sans text $pixmap->draw_pixbuf ($self->style->white_gc, $pixbuf, 0, 0, 0, 0, $self->{width}, $self->{width}, "normal", 0, 0); # now draw the labels for my $i (1 .. $size) { # 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]; $self->center_text ($pixmap, 0, $k[$i], $border, $ofs * 0.7, $label); $self->center_text ($pixmap, 0, $k[$i], $s2 + $border, $ofs * 0.7, $label); $self->center_text ($pixmap, 0, $border, $k[$i], $ofs * 0.7, $size - $i + 1); $self->center_text ($pixmap, 0, $s2 + $border, $k[$i], $ofs * 0.7, $size - $i + 1); $a++; $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK... } $self->window->set_back_pixmap ($pixmap, 0); $self->{backgroundpm} = $pixmap; $self->{backgroundpb} = $pixbuf; } sub repaint_board { my ($self, $board, $dopaint) = @_; return unless $self->{backgroundpb}; my @areas; 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); 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 & ~MARK_LABEL) { my $stack = $self->create_stack($mark, $edge, $rand); $stack->composite ($pb, 0, 0, @area[2,3], 0, 0, 1, 1, 'nearest', 255); } # speed none, normal, max $self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb, 0, 0, @area, 'max', 0, 0); # labels are handled here because they are quite rare if ($mark & MARK_LABEL) { my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 0xffffff00; if ($white) { $self->center_text ($self->{backgroundpm}, 0, $area[0] + $ofs * 1.1, $area[1] + $ofs * 1.1, $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); } $self->center_text ($self->{backgroundpm}, $white, $area[0] + $ofs, $area[1] + $ofs, $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); } # a single full clear_area is way faster than many single calls here push @areas, \@area if $dopaint; } } } if (@areas) { # the "cut-off" point is arbitrary if (@areas > 16) { # update a single rectangle only my $rect = new Gtk2::Gdk::Rectangle @{pop @areas}; $rect = $rect->union (new Gtk2::Gdk::Rectangle @$_) for @areas; $self->window->clear_area ($rect->values); } else { # update all the affected rectangles $self->window->clear_area (@$_) for @areas; } } $self->{board} = $board; #d# save #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable"; } 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;