| 1 |
root |
1.1 |
package Gtk2::GoBoard; |
| 2 |
|
|
|
| 3 |
|
|
=head1 NAME |
| 4 |
|
|
|
| 5 |
|
|
Gtk2::GoBoard - high quality goban widget with sound |
| 6 |
|
|
|
| 7 |
|
|
=head1 SYNOPSIS |
| 8 |
|
|
|
| 9 |
|
|
use Games::Go::SimpleBoard; |
| 10 |
|
|
|
| 11 |
|
|
my $goboard = newe Games::Go::SimpleBoard; |
| 12 |
|
|
|
| 13 |
|
|
use Gtk2::GoBoard; |
| 14 |
|
|
|
| 15 |
|
|
my $gtkboard = new Gtk2::GoBoard size => 19; |
| 16 |
|
|
|
| 17 |
|
|
# update board, makes a copy |
| 18 |
|
|
$gtkboard->set_board ($goboard); |
| 19 |
|
|
|
| 20 |
|
|
# advanced: enable stone curser for black player, showing |
| 21 |
|
|
# only valid moves |
| 22 |
|
|
$gtkboard->set (cursor => sub { |
| 23 |
|
|
my ($mark, $x, $y) = @_; |
| 24 |
|
|
|
| 25 |
|
|
$mark |= MARK_GRAYED | MARK_B |
| 26 |
|
|
if $goboard->is_valid_move (COLOUR_WHITE, |
| 27 |
|
|
$x, $y, |
| 28 |
|
|
$ruleset == RULESET_NEW_ZEALAND); |
| 29 |
|
|
|
| 30 |
|
|
$mark |
| 31 |
|
|
}); |
| 32 |
|
|
|
| 33 |
|
|
# button-release and -press events pass board coordinates |
| 34 |
|
|
$gtkboard->signal_connect (button_release => sub { |
| 35 |
|
|
my ($button, $x, $y) = @_; |
| 36 |
|
|
|
| 37 |
|
|
... |
| 38 |
|
|
}); |
| 39 |
|
|
|
| 40 |
|
|
=head1 DESCRIPTION |
| 41 |
|
|
|
| 42 |
|
|
This is the very first "true" Gtk2 widget written in Perl. |
| 43 |
|
|
|
| 44 |
|
|
The C<Gtk2::GoBoard> class works like any other widget, see the SYNOPSIS |
| 45 |
|
|
for short examples of the available methods, and the L<App::IGS> and |
| 46 |
|
|
L<KGS> modules for usage examples. |
| 47 |
|
|
|
| 48 |
|
|
Please supply a more descriptive description :) |
| 49 |
|
|
|
| 50 |
|
|
=head2 SOUND SUPPORT |
| 51 |
|
|
|
| 52 |
|
|
In addition to a graphical board widget, this module has some rudimentary |
| 53 |
|
|
support for sounds. |
| 54 |
|
|
|
| 55 |
|
|
Playing sounds required the L<Audio::Play> module. If it isn't installed, |
| 56 |
|
|
sounds will silently not being played. The module intentionally doesn't |
| 57 |
|
|
depend on L<Audio::Play> as it isn't actively maintained anymore and fails |
| 58 |
|
|
to install cleanly. |
| 59 |
|
|
|
| 60 |
|
|
=over 4 |
| 61 |
|
|
|
| 62 |
|
|
=cut |
| 63 |
|
|
|
| 64 |
|
|
our $VERSION = '1.0'; |
| 65 |
|
|
|
| 66 |
|
|
no warnings; |
| 67 |
|
|
use strict; |
| 68 |
|
|
|
| 69 |
|
|
use Scalar::Util; |
| 70 |
|
|
use POSIX qw(ceil); |
| 71 |
|
|
use Gtk2; |
| 72 |
|
|
use Games::Go::SimpleBoard; |
| 73 |
|
|
use Carp (); |
| 74 |
|
|
|
| 75 |
|
|
use Glib::Object::Subclass |
| 76 |
|
|
Gtk2::AspectFrame::, |
| 77 |
|
|
properties => [ |
| 78 |
|
|
Glib::ParamSpec->IV ( |
| 79 |
|
|
"size", |
| 80 |
|
|
"Board Size", |
| 81 |
|
|
"The Go Board size, 2..38", |
| 82 |
|
|
2, 38, 19, |
| 83 |
|
|
[qw(construct-only writable readable)], |
| 84 |
|
|
), |
| 85 |
|
|
Glib::ParamSpec->scalar ( |
| 86 |
|
|
"cursor", |
| 87 |
|
|
"cursor callback", |
| 88 |
|
|
"The callback that modifies the cursor mask", |
| 89 |
|
|
[qw(writable readable)], |
| 90 |
|
|
), |
| 91 |
|
|
], |
| 92 |
|
|
signals => { |
| 93 |
|
|
"button-press" => { |
| 94 |
|
|
flags => [qw/run-first/], |
| 95 |
|
|
return_type => undef, # void return |
| 96 |
|
|
param_types => [Glib::Int::, Glib::Int::, Glib::Int::], # instance and data are automatic |
| 97 |
|
|
}, |
| 98 |
|
|
"button-release" => { |
| 99 |
|
|
flags => [qw/run-first/], |
| 100 |
|
|
return_type => undef, # void return |
| 101 |
|
|
param_types => [Glib::Int::, Glib::Int::, Glib::Int::], # instance and data are automatic |
| 102 |
|
|
}, |
| 103 |
|
|
destroy => sub { |
| 104 |
|
|
$_[0]->signal_chain_from_overridden; |
| 105 |
|
|
%{$_[0]} = (); |
| 106 |
|
|
}, |
| 107 |
|
|
}; |
| 108 |
|
|
|
| 109 |
|
|
# some internal constants |
| 110 |
|
|
|
| 111 |
|
|
sub TRAD_WIDTH (){ 42.42 } # traditional board width |
| 112 |
|
|
sub TRAD_HEIGHT (){ 45.45 } # traditional board height |
| 113 |
|
|
sub TRAD_RATIO (){ TRAD_WIDTH / TRAD_HEIGHT } # traditional (nihon-kiin) horizontal spacing |
| 114 |
|
|
sub TRAD_SIZE_B (){ 2.18 } # traditional black stone size |
| 115 |
|
|
sub TRAD_SIZE_W (){ 2.12 } # traditional white stone size |
| 116 |
|
|
|
| 117 |
|
|
sub SHADOW (){ 0.06 } # 0.09 probably max. |
| 118 |
|
|
|
| 119 |
|
|
# find a data file using @INC |
| 120 |
|
|
sub findfile { |
| 121 |
|
|
my @files = @_; |
| 122 |
|
|
file: |
| 123 |
|
|
for (@files) { |
| 124 |
|
|
for my $prefix (@INC) { |
| 125 |
|
|
if (-f "$prefix/Gtk2/GoBoard/data/$_") { |
| 126 |
|
|
$_ = "$prefix/Gtk2/GoBoard/data/$_"; |
| 127 |
|
|
next file; |
| 128 |
|
|
} |
| 129 |
|
|
} |
| 130 |
|
|
die "$_: file not found in \@INC\n"; |
| 131 |
|
|
} |
| 132 |
|
|
wantarray ? @files : $files[0]; |
| 133 |
|
|
} |
| 134 |
|
|
|
| 135 |
|
|
sub load_image { |
| 136 |
|
|
my $path = findfile $_[0]; |
| 137 |
|
|
|
| 138 |
|
|
new_from_file Gtk2::Gdk::Pixbuf $path |
| 139 |
|
|
or die "$path: $!"; |
| 140 |
|
|
} |
| 141 |
|
|
|
| 142 |
|
|
sub load_images { |
| 143 |
|
|
@::black_img = load_image "b-01.png"; |
| 144 |
|
|
@::white_img = map +(load_image "w-0$_.png"), 1,2,3,4,5; |
| 145 |
|
|
$::shadow_img = load_image "shadow.png"; |
| 146 |
|
|
@::triangle_img = map +(load_image "triangle-$_.png"), qw(b w); |
| 147 |
|
|
@::square_img = map +(load_image "square-$_.png"), qw(b w); |
| 148 |
|
|
@::circle_img = map +(load_image "circle-$_.png"), qw(b w); |
| 149 |
|
|
$::board_img = load_image "woodgrain-01.jpg"; |
| 150 |
|
|
} |
| 151 |
|
|
|
| 152 |
|
|
sub INIT_INSTANCE { |
| 153 |
|
|
my $self = shift; |
| 154 |
|
|
|
| 155 |
|
|
@::black_img |
| 156 |
|
|
or load_images; |
| 157 |
|
|
|
| 158 |
|
|
$self->double_buffered (0); |
| 159 |
|
|
$self->set (border_width => 0, shadow_type => 'none', |
| 160 |
|
|
obey_child => 0, ratio => TRAD_RATIO); |
| 161 |
|
|
|
| 162 |
|
|
$self->add ($self->{canvas} = new Gtk2::DrawingArea); |
| 163 |
|
|
|
| 164 |
|
|
$self->{canvas}->signal_connect (motion_notify_event => sub { $self->motion }); |
| 165 |
|
|
$self->{canvas}->signal_connect (leave_notify_event => sub { $self->cursor (0); delete $self->{cursorpos} }); |
| 166 |
|
|
$self->{canvas}->signal_connect (button_press_event => sub { $self->button ("press", $_[1]) }); |
| 167 |
|
|
$self->{canvas}->signal_connect (button_release_event => sub { $self->button ("release", $_[1]) }); |
| 168 |
|
|
|
| 169 |
|
|
$self->{canvas}->signal_connect_after (configure_event => sub { $self->configure_event ($_[1]) }); |
| 170 |
|
|
$self->{canvas}->signal_connect_after (realize => sub { |
| 171 |
|
|
my $window = $_[0]->window; |
| 172 |
|
|
my $color = new Gtk2::Gdk::Color 0xdfdf, 0xb2b2, 0x5d5d; |
| 173 |
|
|
$window->get_colormap->alloc_color ($color, 0, 1); |
| 174 |
|
|
$window->set_background ($color); |
| 175 |
|
|
}); |
| 176 |
|
|
|
| 177 |
|
|
$self->{canvas}->set_events ([ |
| 178 |
|
|
@{ $self->{canvas}->get_events }, |
| 179 |
|
|
'leave-notify-mask', |
| 180 |
|
|
'button-press-mask', |
| 181 |
|
|
'button-release-mask', |
| 182 |
|
|
'pointer-motion-mask', |
| 183 |
|
|
'pointer-motion-hint-mask' |
| 184 |
|
|
]); |
| 185 |
|
|
} |
| 186 |
|
|
|
| 187 |
|
|
sub SET_PROPERTY { |
| 188 |
|
|
my ($self, $pspec, $newval) = @_; |
| 189 |
|
|
|
| 190 |
|
|
$pspec = $pspec->get_name; |
| 191 |
|
|
|
| 192 |
|
|
$self->cursor (0) if $pspec eq "cursor"; |
| 193 |
|
|
$self->{$pspec} = $newval; |
| 194 |
|
|
$self->cursor (1) if $pspec eq "cursor"; |
| 195 |
|
|
} |
| 196 |
|
|
|
| 197 |
|
|
sub configure_event { |
| 198 |
|
|
my ($self, $event) = @_; |
| 199 |
|
|
|
| 200 |
|
|
return if $self->{idle}; |
| 201 |
|
|
|
| 202 |
|
|
return unless $self->{canvas}->allocation->width != $self->{width} |
| 203 |
|
|
|| $self->{canvas}->allocation->height != $self->{height}; |
| 204 |
|
|
|
| 205 |
|
|
my $drawable = $self->{window} = $self->{canvas}->window; |
| 206 |
|
|
$drawable->set_back_pixmap (undef, 0); |
| 207 |
|
|
|
| 208 |
|
|
delete $self->{stack}; |
| 209 |
|
|
|
| 210 |
|
|
# remove Glib::Source $self->{idle}; |
| 211 |
|
|
$self->{idle} ||= add Glib::Idle sub { |
| 212 |
|
|
$self->{width} = $self->{canvas}->allocation->width; |
| 213 |
|
|
$self->{height} = $self->{canvas}->allocation->height; |
| 214 |
|
|
$self->draw_background; |
| 215 |
|
|
|
| 216 |
|
|
$self->draw_board (delete $self->{board}, 0) if $self->{board}; |
| 217 |
|
|
$self->{window}->clear_area (0, 0, $self->{width}, $self->{height}); |
| 218 |
|
|
|
| 219 |
|
|
delete $self->{idle}; |
| 220 |
|
|
|
| 221 |
|
|
0; |
| 222 |
|
|
}; |
| 223 |
|
|
|
| 224 |
|
|
1; |
| 225 |
|
|
} |
| 226 |
|
|
|
| 227 |
|
|
sub set_board { |
| 228 |
|
|
my ($self, $board) = @_; |
| 229 |
|
|
|
| 230 |
|
|
$self->cursor (0); |
| 231 |
|
|
$self->draw_board ($board, 1); |
| 232 |
|
|
$self->cursor (1); |
| 233 |
|
|
} |
| 234 |
|
|
|
| 235 |
|
|
sub new_pixbuf { |
| 236 |
|
|
my ($w, $h, $alpha, $fill) = @_; |
| 237 |
|
|
|
| 238 |
|
|
my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h; |
| 239 |
|
|
$pixbuf->fill ($fill) if defined $fill; |
| 240 |
|
|
|
| 241 |
|
|
$pixbuf; |
| 242 |
|
|
} |
| 243 |
|
|
|
| 244 |
|
|
sub scale_pixbuf { |
| 245 |
|
|
my ($src, $w, $h, $mode, $alpha) = @_; |
| 246 |
|
|
|
| 247 |
|
|
my $dst = new_pixbuf $w, $h, $alpha; |
| 248 |
|
|
|
| 249 |
|
|
$src->scale( |
| 250 |
|
|
$dst, 0, 0, $w, $h, 0, 0, |
| 251 |
|
|
$w / $src->get_width, $h / $src->get_height, |
| 252 |
|
|
$mode, |
| 253 |
|
|
); |
| 254 |
|
|
|
| 255 |
|
|
$dst; |
| 256 |
|
|
} |
| 257 |
|
|
|
| 258 |
|
|
sub pixbuf_rect { |
| 259 |
|
|
my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_; |
| 260 |
|
|
|
| 261 |
|
|
# we fake lines by... a horrible method :/ |
| 262 |
|
|
my $colour_pb = new_pixbuf 1, 1, 0, $colour; |
| 263 |
|
|
$colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1, |
| 264 |
|
|
'nearest', $alpha); |
| 265 |
|
|
} |
| 266 |
|
|
|
| 267 |
|
|
sub center_text { |
| 268 |
|
|
my ($self, $drawable, $colour, $x, $y, $size, $text) = @_; |
| 269 |
|
|
|
| 270 |
|
|
# could be optimized by caching quite a bit |
| 271 |
|
|
|
| 272 |
|
|
my $context = $self->get_pango_context; |
| 273 |
|
|
my $font = $context->get_font_description; |
| 274 |
|
|
$font->set_size ($size * Gtk2::Pango->scale); |
| 275 |
|
|
|
| 276 |
|
|
my $layout = new Gtk2::Pango::Layout $context; |
| 277 |
|
|
$layout->set_text ($text); |
| 278 |
|
|
my ($w, $h) = $layout->get_pixel_size; |
| 279 |
|
|
|
| 280 |
|
|
my $gc = new Gtk2::Gdk::GC $drawable; |
| 281 |
|
|
|
| 282 |
|
|
my $r = (($colour >> 24) & 255) * (65535 / 255); |
| 283 |
|
|
my $g = (($colour >> 16) & 255) * (65535 / 255); |
| 284 |
|
|
my $b = (($colour >> 8) & 255) * (65535 / 255); |
| 285 |
|
|
|
| 286 |
|
|
$gc->set_rgb_fg_color (new Gtk2::Gdk::Color $r, $g, $b); |
| 287 |
|
|
|
| 288 |
|
|
$drawable->draw_layout ($gc, $x - $w*0.5, $y - $h*0.5, $layout); |
| 289 |
|
|
} |
| 290 |
|
|
|
| 291 |
|
|
# draw an empty board and attach the bg pixmap |
| 292 |
|
|
sub draw_background { |
| 293 |
|
|
my ($self) = @_; |
| 294 |
|
|
my $canvas = $self->{canvas}; |
| 295 |
|
|
|
| 296 |
|
|
my $size = $self->{size}; |
| 297 |
|
|
|
| 298 |
|
|
my $w = $self->{width}; |
| 299 |
|
|
my $h = $self->{height}; |
| 300 |
|
|
|
| 301 |
|
|
delete $self->{backgroundpm}; |
| 302 |
|
|
delete $self->{backgroundpb}; |
| 303 |
|
|
|
| 304 |
|
|
my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $w, $h, -1; |
| 305 |
|
|
|
| 306 |
|
|
#my $gridcolour = 0x88444400; # black is traditional, but only with overlapping stones |
| 307 |
|
|
my $gridcolour = 0x44444400; # black is traditional, but only with overlapping stones |
| 308 |
|
|
my $labelcolour = 0x88444400; |
| 309 |
|
|
|
| 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); |
| 313 |
|
|
my $borderh = $borderw; |
| 314 |
|
|
my $w2 = $w - $borderw * 2; |
| 315 |
|
|
my $h2 = $h - $borderh * 2; |
| 316 |
|
|
my $edge = ceil ($w2 / ($size + 1)); |
| 317 |
|
|
my $ofs = $edge * 0.5; |
| 318 |
|
|
|
| 319 |
|
|
# we need a certain minimum size, and just fudge some formula here |
| 320 |
|
|
return if $w < $size * 5 + 2 + $borderw |
| 321 |
|
|
|| $h < $size * 6 + 2 + $borderh; |
| 322 |
|
|
|
| 323 |
|
|
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; |
| 325 |
|
|
|
| 326 |
|
|
my $pixbuf; |
| 327 |
|
|
|
| 328 |
|
|
my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); |
| 329 |
|
|
|
| 330 |
|
|
if ($w < $bw && $h < $bh) { |
| 331 |
|
|
$pixbuf = new_pixbuf $w, $h, 0; |
| 332 |
|
|
$::board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0); |
| 333 |
|
|
} else { |
| 334 |
|
|
$pixbuf = scale_pixbuf $::board_img, $w, $h, $::config->{speed} ? 'nearest' : 'bilinear', 0; |
| 335 |
|
|
} |
| 336 |
|
|
|
| 337 |
|
|
my $linew = int ($w / 40 / $size); |
| 338 |
|
|
|
| 339 |
|
|
# ornamental border... we have time to waste :/ |
| 340 |
|
|
pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $w-1, $linew, 255; |
| 341 |
|
|
pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $h-1, 255; |
| 342 |
|
|
pixbuf_rect $pixbuf, 0xffcc7700, $w-$linew-1, 0, $w-1, $h-1, 255; |
| 343 |
|
|
pixbuf_rect $pixbuf, 0xffcc7700, 0, $h-$linew-1, $w-1, $h-1, 255; |
| 344 |
|
|
|
| 345 |
|
|
for my $i (1 .. $size) { |
| 346 |
|
|
pixbuf_rect $pixbuf, $gridcolour, $kx[$i] - $linew, $ky[1] - $linew, $kx[$i] + $linew, $ky[$size] + $linew, 255; |
| 347 |
|
|
pixbuf_rect $pixbuf, $gridcolour, $kx[1] - $linew, $ky[$i] - $linew, $kx[$size] + $linew, $ky[$i] + $linew, 255; |
| 348 |
|
|
} |
| 349 |
|
|
|
| 350 |
|
|
# hoshi points |
| 351 |
|
|
my $hoshi = sub { |
| 352 |
|
|
my ($x, $y) = @_; |
| 353 |
|
|
my $hs = 1 | int $edge / 4; |
| 354 |
|
|
$hs = 5 if $hs < 5; |
| 355 |
|
|
$x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2; |
| 356 |
|
|
|
| 357 |
|
|
# we use the shadow mask... not perfect, but I want to finish this |
| 358 |
|
|
$::shadow_img->composite ($pixbuf, |
| 359 |
|
|
$x, $y, ($hs + 1) x2, $x, $y, |
| 360 |
|
|
$hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height, |
| 361 |
|
|
'bilinear', 255); |
| 362 |
|
|
}; |
| 363 |
|
|
|
| 364 |
|
|
if ($size > 6) { |
| 365 |
|
|
my $h1 = $size < 10 ? 3 : 4; # corner / edge offset |
| 366 |
|
|
$hoshi->($h1, $h1); |
| 367 |
|
|
$hoshi->($size - $h1 + 1, $h1); |
| 368 |
|
|
$hoshi->($h1, $size - $h1 + 1); |
| 369 |
|
|
$hoshi->($size - $h1 + 1, $size - $h1 + 1); |
| 370 |
|
|
|
| 371 |
|
|
if ($size % 2) { # on odd boards, also the remaining 5 |
| 372 |
|
|
my $h2 = ($size + 1) / 2; |
| 373 |
|
|
if ($size > 10) { |
| 374 |
|
|
$hoshi->($h1, $h2); |
| 375 |
|
|
$hoshi->($size - $h1 + 1, $h2); |
| 376 |
|
|
$hoshi->($h2, $size - $h1 + 1); |
| 377 |
|
|
$hoshi->($h2, $h1); |
| 378 |
|
|
} |
| 379 |
|
|
# the tengen |
| 380 |
|
|
$hoshi->($h2, $h2); |
| 381 |
|
|
} |
| 382 |
|
|
} |
| 383 |
|
|
|
| 384 |
|
|
# now we have a board sans text |
| 385 |
|
|
$pixmap->draw_pixbuf ($self->style->white_gc, |
| 386 |
|
|
$pixbuf, |
| 387 |
|
|
0, 0, 0, 0, $w, $h, |
| 388 |
|
|
"normal", 0, 0); |
| 389 |
|
|
|
| 390 |
|
|
# now draw the labels |
| 391 |
|
|
for my $i (1 .. $size) { |
| 392 |
|
|
# 38 max, but we allow a bit more |
| 393 |
|
|
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 |
| 394 |
|
|
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]; |
| 395 |
|
|
|
| 396 |
|
|
$self->center_text ($pixmap, $labelcolour, $kx[$i], $borderh, $ofs * 0.7, $label); |
| 397 |
|
|
$self->center_text ($pixmap, $labelcolour, $kx[$i], $h2 + $borderh, $ofs * 0.7, $label); |
| 398 |
|
|
$self->center_text ($pixmap, $labelcolour, $borderw, $ky[$i], $ofs * 0.7, $size - $i + 1); |
| 399 |
|
|
$self->center_text ($pixmap, $labelcolour, $w2 + $borderw, $ky[$i], $ofs * 0.7, $size - $i + 1); |
| 400 |
|
|
} |
| 401 |
|
|
|
| 402 |
|
|
$self->{window}->set_back_pixmap ($pixmap, 0); |
| 403 |
|
|
|
| 404 |
|
|
$self->{backgroundpm} = $pixmap; |
| 405 |
|
|
$self->{backgroundpb} = $pixbuf; |
| 406 |
|
|
|
| 407 |
|
|
$edge = int ($edge * TRAD_SIZE_B / TRAD_SIZE_W); |
| 408 |
|
|
$ofs = int ($edge * 0.5); |
| 409 |
|
|
|
| 410 |
|
|
{ |
| 411 |
|
|
# shared vars for the stone drawing function |
| 412 |
|
|
my $shadow = $edge * SHADOW; |
| 413 |
|
|
my $pb; |
| 414 |
|
|
my @area; |
| 415 |
|
|
my @areai; |
| 416 |
|
|
my %stack; |
| 417 |
|
|
|
| 418 |
|
|
my $put_stack = sub { |
| 419 |
|
|
my ($x, $y, $dx, $dy, $ox, $oy) = @_; |
| 420 |
|
|
|
| 421 |
|
|
my $mark = $self->{board}{board}[$x-1][$y-1]; |
| 422 |
|
|
|
| 423 |
|
|
if ($mark & ~MARK_LABEL) { |
| 424 |
|
|
my $stack = $stack{$mark} ||= $self->draw_stack ($mark, $edge); |
| 425 |
|
|
|
| 426 |
|
|
$stack->[($x ^ $y) % @$stack] |
| 427 |
|
|
->composite ($pb, |
| 428 |
|
|
$ox, $oy, |
| 429 |
|
|
$areai[2] + $dx - $ox, $areai[3] + $dy - $oy, |
| 430 |
|
|
$dx + $ox, $dy + $oy, |
| 431 |
|
|
1, 1, 'nearest', 255); |
| 432 |
|
|
} |
| 433 |
|
|
}; |
| 434 |
|
|
|
| 435 |
|
|
$self->{draw_stone} = sub { |
| 436 |
|
|
my ($x, $y) = @_; |
| 437 |
|
|
|
| 438 |
|
|
@area = ($kx[$x] - $ofs, $ky[$y] - $ofs, |
| 439 |
|
|
$edge + $shadow, $edge + $shadow); |
| 440 |
|
|
@areai = ((ceil $area[0]), (ceil $area[1]), |
| 441 |
|
|
(int $area[2]), (int $area[3])); # area, integer |
| 442 |
|
|
|
| 443 |
|
|
$pb = new_pixbuf @areai[2,3]; |
| 444 |
|
|
$self->{backgroundpb}->copy_area (@areai, $pb, 0, 0); |
| 445 |
|
|
|
| 446 |
|
|
$put_stack->($x-1, $y, $kx[$x-1] - $kx[$x], 0, 0, 0) if $x > 1; |
| 447 |
|
|
$put_stack->($x, $y-1, 0, $ky[$y-1] - $ky[$y], 0, 0) if $y > 1; |
| 448 |
|
|
$put_stack->($x , $y , 0, 0); |
| 449 |
|
|
$put_stack->($x+1, $y, 0, 0, $kx[$x+1] - $kx[$x], 0) if $x < $size; |
| 450 |
|
|
$put_stack->($x, $y+1, 0, 0, 0, $ky[$y+1] - $ky[$y]) if $y < $size; |
| 451 |
|
|
|
| 452 |
|
|
# speed none, normal, max |
| 453 |
|
|
$self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb, |
| 454 |
|
|
0, 0, @areai, 'max', 0, 0); |
| 455 |
|
|
|
| 456 |
|
|
# labels are handled here because they are quite rare |
| 457 |
|
|
# (and we can't draw text into pixbufs easily) |
| 458 |
|
|
my $mark = $self->{board}{board}[$x-1][$y-1]; |
| 459 |
|
|
|
| 460 |
|
|
if ($mark & MARK_LABEL) { |
| 461 |
|
|
my $white = $mark & MARK_W ? 0 : 0xffffff00; |
| 462 |
|
|
|
| 463 |
|
|
if ($white) { |
| 464 |
|
|
$self->center_text ($self->{backgroundpm}, 0, |
| 465 |
|
|
$areai[0] + $ofs * 1.1, $areai[1] + $ofs * 1.1, |
| 466 |
|
|
$ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); |
| 467 |
|
|
} |
| 468 |
|
|
$self->center_text ($self->{backgroundpm}, $white, |
| 469 |
|
|
$areai[0] + $ofs, $areai[1] + $ofs, |
| 470 |
|
|
$ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); |
| 471 |
|
|
} |
| 472 |
|
|
|
| 473 |
|
|
undef $pb; |
| 474 |
|
|
|
| 475 |
|
|
[@areai]; |
| 476 |
|
|
}; |
| 477 |
|
|
} |
| 478 |
|
|
} |
| 479 |
|
|
|
| 480 |
|
|
# create a stack of stones, possibly in various versions |
| 481 |
|
|
sub draw_stack { |
| 482 |
|
|
my ($self, $mark, $size) = @_; |
| 483 |
|
|
|
| 484 |
|
|
my @stack; |
| 485 |
|
|
my $csize = ceil $size; |
| 486 |
|
|
my $shadow = $size * SHADOW; |
| 487 |
|
|
|
| 488 |
|
|
for my $stone ($mark & MARK_W ? @::white_img : @::black_img) { |
| 489 |
|
|
my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000; |
| 490 |
|
|
|
| 491 |
|
|
# zeroeth the shadow |
| 492 |
|
|
if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) { |
| 493 |
|
|
$::shadow_img->composite ( |
| 494 |
|
|
$base, $shadow, $shadow, $csize, $csize, $shadow, $shadow, |
| 495 |
|
|
$size / $::shadow_img->get_width, $size / $::shadow_img->get_height, |
| 496 |
|
|
'bilinear', 128 |
| 497 |
|
|
); |
| 498 |
|
|
} |
| 499 |
|
|
|
| 500 |
|
|
for ([MARK_B, $mark & MARK_GRAYED ? 96 : 255, 1], |
| 501 |
|
|
[MARK_W, $mark & MARK_GRAYED ? 160 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) { |
| 502 |
|
|
my ($mask, $alpha, $scale) = @$_; |
| 503 |
|
|
if ($mark & $mask) { |
| 504 |
|
|
$stone->composite ( |
| 505 |
|
|
$base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2, |
| 506 |
|
|
$size * $scale / $stone->get_width, $size * $scale / $stone->get_height, |
| 507 |
|
|
'bilinear', $alpha |
| 508 |
|
|
); |
| 509 |
|
|
} |
| 510 |
|
|
} |
| 511 |
|
|
|
| 512 |
|
|
# then the small stones (always using the first image) |
| 513 |
|
|
for ([MARK_SMALL_B, $::black_img[0]], |
| 514 |
|
|
[MARK_SMALL_W, $::white_img[0]]) { |
| 515 |
|
|
my ($mask, $img) = @$_; |
| 516 |
|
|
if ($mark & $mask) { |
| 517 |
|
|
$img->composite ( |
| 518 |
|
|
$base, (int $size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2, |
| 519 |
|
|
$size / $img->get_width / 2, $size / $img->get_height / 2, |
| 520 |
|
|
'bilinear', 255 |
| 521 |
|
|
); |
| 522 |
|
|
} |
| 523 |
|
|
} |
| 524 |
|
|
|
| 525 |
|
|
# and lastly any markers |
| 526 |
|
|
my $dark_bg = ! ! ($mark & MARK_B); |
| 527 |
|
|
|
| 528 |
|
|
for ([MARK_CIRCLE, $::circle_img[$dark_bg]], |
| 529 |
|
|
[MARK_TRIANGLE, $::triangle_img[$dark_bg]], |
| 530 |
|
|
[MARK_SQUARE, $::square_img[$dark_bg]], |
| 531 |
|
|
[MARK_KO, $::square_img[$dark_bg]]) { |
| 532 |
|
|
my ($mask, $img) = @$_; |
| 533 |
|
|
if ($mark & $mask) { |
| 534 |
|
|
$img->composite ( |
| 535 |
|
|
$base, 0, 0, $size, $size, 0, 0, |
| 536 |
|
|
$size / $img->get_width, $size / $img->get_height, |
| 537 |
|
|
'bilinear', 176 |
| 538 |
|
|
); |
| 539 |
|
|
} |
| 540 |
|
|
} |
| 541 |
|
|
|
| 542 |
|
|
push @stack, $base; |
| 543 |
|
|
} |
| 544 |
|
|
|
| 545 |
|
|
\@stack; |
| 546 |
|
|
} |
| 547 |
|
|
|
| 548 |
|
|
sub draw_board { |
| 549 |
|
|
my ($self, $new, $dopaint) = @_; |
| 550 |
|
|
|
| 551 |
|
|
($self->{board}, my $old) = ($new, $self->{board}); |
| 552 |
|
|
|
| 553 |
|
|
my $draw_stone = $self->{draw_stone}; |
| 554 |
|
|
|
| 555 |
|
|
if ($self->{backgroundpb}) { |
| 556 |
|
|
my @areas; |
| 557 |
|
|
|
| 558 |
|
|
my $size1 = $self->{size} - 1; |
| 559 |
|
|
|
| 560 |
|
|
for my $x (0 .. $size1) { |
| 561 |
|
|
my $old = $old->{board}[$x]; |
| 562 |
|
|
my $new = $new->{board}[$x]; |
| 563 |
|
|
|
| 564 |
|
|
for my $y (0 .. $size1) { |
| 565 |
|
|
push @areas, $draw_stone->($x+1, $y+1) |
| 566 |
|
|
if $old->[$y] != $new->[$y]; |
| 567 |
|
|
} |
| 568 |
|
|
} |
| 569 |
|
|
|
| 570 |
|
|
if ($dopaint && @areas) { |
| 571 |
|
|
# a single full clear_area is way faster than many single calls here |
| 572 |
|
|
# the "cut-off" point is quite arbitrary |
| 573 |
|
|
if (@areas > 64) { |
| 574 |
|
|
# update a single rectangle only |
| 575 |
|
|
my $rect = new Gtk2::Gdk::Rectangle @{pop @areas}; |
| 576 |
|
|
$rect = $rect->union (new Gtk2::Gdk::Rectangle @$_) for @areas; |
| 577 |
|
|
$self->{window}->clear_area ($rect->values); |
| 578 |
|
|
} else { |
| 579 |
|
|
# update all the affected rectangles |
| 580 |
|
|
$self->{window}->clear_area (@$_) for @areas; |
| 581 |
|
|
} |
| 582 |
|
|
} |
| 583 |
|
|
} |
| 584 |
|
|
} |
| 585 |
|
|
|
| 586 |
|
|
sub cursor { |
| 587 |
|
|
my ($self, $show) = @_; |
| 588 |
|
|
|
| 589 |
|
|
return unless exists $self->{cursorpos} |
| 590 |
|
|
&& $self->{cursor} |
| 591 |
|
|
&& $self->{backgroundpb}; |
| 592 |
|
|
|
| 593 |
|
|
my ($x, $y) = @{$self->{cursorpos}}; |
| 594 |
|
|
|
| 595 |
|
|
my $mark = $self->{board}{board}[$x][$y]; |
| 596 |
|
|
|
| 597 |
|
|
$mark = $self->{cursor}->($mark, $x, $y) if $show; |
| 598 |
|
|
|
| 599 |
|
|
local $self->{board}{board}[$x][$y] = $mark; |
| 600 |
|
|
$self->{window}->clear_area (@{ $self->{draw_stone}->($x + 1, $y + 1) }); |
| 601 |
|
|
} |
| 602 |
|
|
|
| 603 |
|
|
sub motion { |
| 604 |
|
|
my ($self) = @_; |
| 605 |
|
|
|
| 606 |
|
|
return unless $self->{backgroundpb}; |
| 607 |
|
|
|
| 608 |
|
|
my $window = $self->{canvas}->window; |
| 609 |
|
|
my (undef, $x, $y, undef) = $window->get_pointer; |
| 610 |
|
|
|
| 611 |
|
|
my $size = $self->{size}; |
| 612 |
|
|
|
| 613 |
|
|
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; |
| 615 |
|
|
|
| 616 |
|
|
my $pos = $self->{cursorpos}; |
| 617 |
|
|
if ($x != $pos->[0] || $y != $pos->[1]) { |
| 618 |
|
|
|
| 619 |
|
|
$self->cursor (0); |
| 620 |
|
|
|
| 621 |
|
|
if ($x >= 0 && $x < $size |
| 622 |
|
|
&& $y >= 0 && $y < $size) { |
| 623 |
|
|
$self->{cursorpos} = [$x, $y]; |
| 624 |
|
|
$self->cursor (1); |
| 625 |
|
|
} else { |
| 626 |
|
|
delete $self->{cursorpos}; |
| 627 |
|
|
} |
| 628 |
|
|
} |
| 629 |
|
|
} |
| 630 |
|
|
|
| 631 |
|
|
sub do_button_press { |
| 632 |
|
|
my ($self, $button, $x, $y) = @_; |
| 633 |
|
|
} |
| 634 |
|
|
|
| 635 |
|
|
sub do_button_release { |
| 636 |
|
|
my ($self, $button, $x, $y) = @_; |
| 637 |
|
|
} |
| 638 |
|
|
|
| 639 |
|
|
sub button { |
| 640 |
|
|
my ($self, $type, $event) = @_; |
| 641 |
|
|
|
| 642 |
|
|
$self->motion; |
| 643 |
|
|
|
| 644 |
|
|
if ($self->{cursorpos}) { |
| 645 |
|
|
$self->signal_emit ("button-$type", $event->button, @{ $self->{cursorpos} }); |
| 646 |
|
|
} |
| 647 |
|
|
} |
| 648 |
|
|
|
| 649 |
|
|
=item Gtk2::GoBoard::play_sound "name" |
| 650 |
|
|
|
| 651 |
|
|
Play the sound with the give name. Currently supported names are: |
| 652 |
|
|
|
| 653 |
|
|
alarm connect gamestart info move outoftime pass resign ring warning |
| 654 |
|
|
|
| 655 |
|
|
If the L<Audio::Play> module cannot be loaded, the function will silently |
| 656 |
|
|
fail. If an unsupported sound name is used, the function might C<croak> or |
| 657 |
|
|
might silently fail. |
| 658 |
|
|
|
| 659 |
|
|
This function forks a sound-server to play the sound(s) on first use. |
| 660 |
|
|
|
| 661 |
|
|
=cut |
| 662 |
|
|
|
| 663 |
|
|
our $SOUND_SERVER; |
| 664 |
|
|
|
| 665 |
|
|
sub play_sound { |
| 666 |
|
|
eval { require Audio::Data; require Audio::Play } |
| 667 |
|
|
or return; |
| 668 |
|
|
|
| 669 |
|
|
unless ($SOUND_SERVER) { |
| 670 |
|
|
require Socket; |
| 671 |
|
|
|
| 672 |
|
|
# use this contortion to also work on the broken windows platform |
| 673 |
|
|
socketpair $SOUND_SERVER, my $fh, &Socket::AF_UNIX, &Socket::SOCK_STREAM, 0 |
| 674 |
|
|
or return; |
| 675 |
|
|
|
| 676 |
|
|
my $pid = fork; |
| 677 |
|
|
|
| 678 |
root |
1.2 |
if ($pid) { |
| 679 |
root |
1.1 |
# parent |
| 680 |
|
|
close $fh; |
| 681 |
|
|
|
| 682 |
root |
1.2 |
} elsif (defined $pid) { |
| 683 |
root |
1.1 |
# child |
| 684 |
|
|
close $SOUND_SERVER; |
| 685 |
|
|
|
| 686 |
root |
1.2 |
close STDIN; |
| 687 |
|
|
close STDOUT; |
| 688 |
|
|
close STDERR; |
| 689 |
|
|
|
| 690 |
|
|
# ok, this is a bit pathetic |
| 691 |
|
|
POSIX::close $_ for grep $_ != fileno $fh, 3 .. 1000; |
| 692 |
|
|
|
| 693 |
root |
1.1 |
my %sound; |
| 694 |
|
|
|
| 695 |
root |
1.2 |
while (<$fh>) { |
| 696 |
root |
1.1 |
chomp; |
| 697 |
|
|
|
| 698 |
|
|
eval { |
| 699 |
|
|
my $sound = $sound{$_} ||= do { |
| 700 |
|
|
my $path = findfile "$_.au" |
| 701 |
|
|
or Carp::croak "$_: unable to find sound\n"; |
| 702 |
|
|
|
| 703 |
|
|
open my $fh, "<", $path |
| 704 |
|
|
or Carp::croak "$_: unable to load sound\n"; |
| 705 |
|
|
|
| 706 |
|
|
binmode $fh; |
| 707 |
|
|
|
| 708 |
|
|
my $data = new Audio::Data; |
| 709 |
|
|
$data->Load ($fh); |
| 710 |
|
|
|
| 711 |
|
|
$data |
| 712 |
|
|
}; |
| 713 |
|
|
|
| 714 |
root |
1.2 |
my $server = new Audio::Play; |
| 715 |
root |
1.1 |
$server->play ($sound); |
| 716 |
|
|
$server->flush; |
| 717 |
|
|
}; |
| 718 |
|
|
} |
| 719 |
|
|
|
| 720 |
root |
1.3 |
# required for windows, as a mere _exit kills your parent process... |
| 721 |
|
|
kill 9, $$; |
| 722 |
root |
1.2 |
} else { |
| 723 |
|
|
undef $SOUND_SERVER; |
| 724 |
|
|
return; |
| 725 |
root |
1.1 |
} |
| 726 |
|
|
} |
| 727 |
root |
1.2 |
|
| 728 |
|
|
syswrite $SOUND_SERVER, "$_[0]\n"; |
| 729 |
root |
1.1 |
} |
| 730 |
|
|
|
| 731 |
|
|
1; |
| 732 |
|
|
|
| 733 |
|
|
=back |
| 734 |
|
|
|
| 735 |
|
|
=head2 EXAMPLE PROGRAM |
| 736 |
|
|
|
| 737 |
|
|
This program should get you started. It creates a board with some |
| 738 |
|
|
markings, enables a cursor callback that shows a transparent black stone, |
| 739 |
|
|
and after a click, marks the position with a circle and disables the |
| 740 |
|
|
cursor. |
| 741 |
|
|
|
| 742 |
|
|
use Gtk2 -init; |
| 743 |
|
|
use Games::Go::SimpleBoard; |
| 744 |
|
|
use Gtk2::GoBoard; |
| 745 |
|
|
|
| 746 |
|
|
my $game = new Games::Go::SimpleBoard 9; |
| 747 |
|
|
|
| 748 |
|
|
# show off some markings |
| 749 |
|
|
$game->{board}[0][0] = MARK_B; |
| 750 |
|
|
$game->{board}[1][1] = MARK_GRAY_B | MARK_SMALL_W; |
| 751 |
|
|
$game->{board}[2][2] = MARK_W | MARK_TRIANGLE; |
| 752 |
|
|
$game->{board}[1][2] = MARK_B | MARK_LABEL; |
| 753 |
|
|
$game->{label}[1][2] = "198"; |
| 754 |
|
|
$game->{board}[0][2] = MARK_W | MARK_LABEL; |
| 755 |
|
|
$game->{label}[0][2] = "AWA"; |
| 756 |
|
|
|
| 757 |
|
|
# create a spot where black cannot put a stone |
| 758 |
|
|
$game->{board}[17][0] = MARK_W; |
| 759 |
|
|
$game->{board}[17][1] = MARK_W; |
| 760 |
|
|
$game->{board}[18][1] = MARK_W; |
| 761 |
|
|
|
| 762 |
|
|
my $board = new Gtk2::GoBoard; |
| 763 |
|
|
$board->set_board ($game); |
| 764 |
|
|
|
| 765 |
root |
1.2 |
Gtk2::GoBoard::play_sound "gamestart"; # ping |
| 766 |
|
|
|
| 767 |
root |
1.1 |
# enable cursor for black, till click |
| 768 |
|
|
$board->set (cursor => sub { |
| 769 |
|
|
my ($mark, $x, $y) = @_; |
| 770 |
|
|
|
| 771 |
|
|
$mark |= MARK_GRAYED | MARK_B |
| 772 |
|
|
if $game->is_valid_move (COLOUR_BLACK, $x, $y); |
| 773 |
|
|
|
| 774 |
|
|
$mark |
| 775 |
|
|
}); |
| 776 |
|
|
|
| 777 |
|
|
# on press, set a mark and disable cursor |
| 778 |
|
|
$board->signal_connect (button_release => sub { |
| 779 |
|
|
my ($board, $button, $x, $y) = @_; |
| 780 |
|
|
|
| 781 |
|
|
$game->{board}[$x][$y] |= MARK_CIRCLE; |
| 782 |
|
|
$board->set_board ($game); # force update |
| 783 |
|
|
|
| 784 |
root |
1.2 |
Gtk2::GoBoard::play_sound "move"; # play click sound |
| 785 |
|
|
|
| 786 |
root |
1.1 |
$board->set (cursor => undef); # disable cursor |
| 787 |
|
|
}); |
| 788 |
|
|
|
| 789 |
|
|
my $w = new Gtk2::Window "toplevel"; |
| 790 |
|
|
$w->set_default_size (450, 450); |
| 791 |
|
|
$w->add ($board); |
| 792 |
|
|
$w->show_all; |
| 793 |
|
|
|
| 794 |
|
|
main Gtk2; |
| 795 |
|
|
|
| 796 |
|
|
=head2 AUTHOR |
| 797 |
|
|
|
| 798 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
| 799 |
|
|
|
| 800 |
|
|
=head2 SEE ALSO |
| 801 |
|
|
|
| 802 |
|
|
L<App::IGS>, L<Games::Go::SimpleBoard>, L<AnyEvent::IGS>, L<KGS>. |
| 803 |
|
|
|
| 804 |
|
|
=cut |
| 805 |
|
|
|