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