1 |
package Gtk2::GoBoard; |
2 |
|
3 |
use Scalar::Util; |
4 |
|
5 |
use KGS::Constants; |
6 |
use KGS::Game::Board; |
7 |
|
8 |
use POSIX qw(ceil); |
9 |
|
10 |
use Gtk2; |
11 |
|
12 |
use Glib::Object::Subclass |
13 |
Gtk2::AspectFrame, |
14 |
properties => [ |
15 |
Glib::ParamSpec->IV ( |
16 |
"size", |
17 |
"Board Size", |
18 |
"The Go Board size, 2..38", |
19 |
2, 38, 19, |
20 |
[qw(construct-only writable readable)], |
21 |
), |
22 |
Glib::ParamSpec->IV ( |
23 |
"cursor-mask", |
24 |
"cursor mask", |
25 |
"The mask used to show a cursor", |
26 |
0, 1<<30, 0, |
27 |
[qw(writable readable)], |
28 |
), |
29 |
Glib::ParamSpec->IV ( |
30 |
"cursor-value", |
31 |
"cursor value", |
32 |
"The value used to show a cursor", |
33 |
0, 1<<30, 0, |
34 |
[qw(writable readable)], |
35 |
), |
36 |
]; |
37 |
|
38 |
sub TRAD_WIDTH (){ 42.42 } # traditional board width |
39 |
sub TRAD_HEIGHT (){ 45.45 } # traditional board height |
40 |
sub TRAD_SIZE_B (){ 2.18 } # traditional black stone size |
41 |
sub TRAD_SIZE_W (){ 2.12 } # traditional white stone size |
42 |
|
43 |
sub SHADOW (){ 0.06 } |
44 |
|
45 |
sub INIT_INSTANCE { |
46 |
my $self = shift; |
47 |
|
48 |
$self->double_buffered (0); |
49 |
$self->set (border_width => 0, shadow_type => 'none', |
50 |
obey_child => 0, ratio => TRAD_WIDTH / TRAD_HEIGHT); |
51 |
$self->set(cursor_mask => MARK_B | MARK_W, cursor_value => MARK_B | MARK_GRAYED); |
52 |
|
53 |
$self->add ($self->{canvas} = new Gtk2::DrawingArea); |
54 |
|
55 |
$self->{canvas}->signal_connect (configure_event => sub { $self->configure_event ($_[1]) }); |
56 |
$self->{canvas}->signal_connect (motion_notify_event => sub { $self->motion }); |
57 |
$self->{canvas}->signal_connect (leave_notify_event => sub { $self->cursor (0); delete $self->{cursorpos} }); |
58 |
$self->{canvas}->signal_connect (button_press_event => sub { $self->buttonpress ($_[1]) }); |
59 |
|
60 |
$self->{canvas}->set_events ([ |
61 |
@{ $self->{canvas}->get_events }, |
62 |
'leave-notify-mask', |
63 |
'button-press-mask', |
64 |
'pointer-motion-mask', |
65 |
'pointer-motion-hint-mask' |
66 |
]); |
67 |
} |
68 |
|
69 |
sub FINALIZE { |
70 |
warn "FINALIZE(@_)\n";#d# |
71 |
my ($self) = @_; |
72 |
$self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy |
73 |
for BLACK, WHITE; |
74 |
$self->SUPER::destroy; |
75 |
delete $self->{app}{gamelist}{game}{$self->{channel}}; |
76 |
die; |
77 |
} |
78 |
|
79 |
sub SET_PROPERTY { |
80 |
my ($self, $pspec, $newval) = @_; |
81 |
|
82 |
$pspec = $pspec->get_name; |
83 |
|
84 |
$self->cursor (0) if $pspec =~ /^cursor/; |
85 |
$self->{$pspec} = $newval; |
86 |
$self->cursor (1) if $pspec =~ /^cursor/; |
87 |
} |
88 |
|
89 |
sub configure_event { |
90 |
my ($self, $event) = @_; |
91 |
|
92 |
$self->{window} = $self->{canvas}->window; |
93 |
|
94 |
my $drawable = $self->{window}; |
95 |
|
96 |
$drawable->set_back_pixmap (undef, 0); |
97 |
|
98 |
#my $gc = new Gtk2::Gdk::GC $drawable; |
99 |
#$gc->set_rgb_fg_color (new Gtk2::Gdk::Color 0xe0e0, 0xb2b2, 0x5e5e); |
100 |
#$drawable->draw_rectangle ($gc, 1, 0, 0, $self->allocation->width, $self->allocation->height); |
101 |
|
102 |
# remove Glib::Source $self->{idle}; |
103 |
$self->{idle} ||= add Glib::Idle sub { |
104 |
delete $self->{stack}; |
105 |
|
106 |
$self->{width} = $self->{canvas}->allocation->width; |
107 |
$self->{height} = $self->{canvas}->allocation->height; |
108 |
$self->draw_background; |
109 |
|
110 |
$self->draw_board (delete $self->{board}, 0) if $self->{board}; |
111 |
$self->{window}->clear_area (0, 0, $self->{width}, $self->{height}); |
112 |
|
113 |
delete $self->{idle}; |
114 |
|
115 |
0; |
116 |
}; |
117 |
|
118 |
1; |
119 |
} |
120 |
|
121 |
sub set_board { |
122 |
my ($self, $board) = @_; |
123 |
|
124 |
$self->cursor (0); |
125 |
$self->draw_board ($board, 1); |
126 |
$self->cursor (1); |
127 |
} |
128 |
|
129 |
sub new_pixbuf { |
130 |
my ($w, $h, $alpha, $fill) = @_; |
131 |
|
132 |
my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h; |
133 |
$pixbuf->fill ($fill) if defined $fill; |
134 |
|
135 |
$pixbuf; |
136 |
} |
137 |
|
138 |
sub scale_pixbuf { |
139 |
my ($src, $w, $h, $mode, $alpha) = @_; |
140 |
|
141 |
my $dst = new_pixbuf $w, $h, $alpha; |
142 |
|
143 |
$src->scale( |
144 |
$dst, 0, 0, $w, $h, 0, 0, |
145 |
$w / $src->get_width, $h / $src->get_height, |
146 |
$mode, |
147 |
); |
148 |
|
149 |
$dst; |
150 |
} |
151 |
|
152 |
sub pixbuf_rect { |
153 |
my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_; |
154 |
# we fake lines by... a horrible method :/ |
155 |
my $colour_pb = new_pixbuf 1, 1, 0, $colour; |
156 |
$colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1, |
157 |
'nearest', $alpha); |
158 |
} |
159 |
|
160 |
sub center_text { |
161 |
my ($self, $drawable, $colour, $x, $y, $size, $text) = @_; |
162 |
|
163 |
# could be optimized by caching quite a bit |
164 |
|
165 |
my $context = $self->get_pango_context; |
166 |
my $font = $context->get_font_description; |
167 |
$font->set_size ($size * Gtk2::Pango->scale); |
168 |
|
169 |
my $layout = new Gtk2::Pango::Layout $context; |
170 |
$layout->set_text ($text); |
171 |
my ($w, $h) = $layout->get_pixel_size; |
172 |
|
173 |
my $gc = new Gtk2::Gdk::GC $drawable; |
174 |
my $r = (($colour >> 24) & 255) * 65535 / 255; |
175 |
my $g = (($colour >> 16) & 255) * 65535 / 255; |
176 |
my $b = (($colour >> 8) & 255) * 65535 / 255; |
177 |
$gc->set_rgb_fg_color (new Gtk2::Gdk::Color $r, $g, $b); |
178 |
|
179 |
$drawable->draw_layout ($gc, $x - $w*0.5, $y - $h*0.5, $layout); |
180 |
} |
181 |
|
182 |
# draw an empty board and attach the bg pixmap |
183 |
sub draw_background { |
184 |
my ($self) = @_; |
185 |
my $canvas = $self->{canvas}; |
186 |
|
187 |
my $size = $self->{size}; |
188 |
|
189 |
my $w = $self->{width}; |
190 |
my $h = $self->{height}; |
191 |
|
192 |
my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $w, $h, -1; |
193 |
|
194 |
#my $gridcolour = 0x88444400; # black is traditional, but only with overlapping stones |
195 |
my $gridcolour = 0x44444400; # black is traditional, but only with overlapping stones |
196 |
my $labelcolour = 0x88444400; |
197 |
|
198 |
# we leave enough space for the shadows.. I like smaller stones, and we |
199 |
# do no need to do the nifty recursive screen updates that cgoban2 does |
200 |
# TODO: smaller == buggy, want visual perfectness |
201 |
my $borderw = int ($w / ($size + 3) * 0.5); |
202 |
my $borderh = $borderw; |
203 |
my $w2 = $w - $borderw * 2; |
204 |
my $h2 = $h - $borderh * 2; |
205 |
my $edge = ceil ($w2 / ($size + 1)); |
206 |
my $ofs = $edge * 0.5; |
207 |
|
208 |
my @kx = map int ($w2 * $_ / ($size+1) + $borderw + 0.5), 0 .. $size; $self->{kx} = \@kx; |
209 |
my @ky = map int ($h2 * $_ / ($size+1) + $borderh + 0.5), 0 .. $size; $self->{ky} = \@ky; |
210 |
|
211 |
my $pixbuf; |
212 |
|
213 |
my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); |
214 |
|
215 |
if ($w < $bw && $h < $bh) { |
216 |
$pixbuf = new_pixbuf $w, $h, 0; |
217 |
$::board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0); |
218 |
} else { |
219 |
$pixbuf = scale_pixbuf $::board_img, $w, $h, $::config->{speed} ? 'nearest' : 'bilinear', 0; |
220 |
} |
221 |
|
222 |
my $linew = int ($w / 40 / $size); |
223 |
|
224 |
# ornamental border... we have time to waste :/ |
225 |
pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $w-1, $linew, 255; |
226 |
pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $h-1, 255; |
227 |
pixbuf_rect $pixbuf, 0xffcc7700, $w-$linew-1, 0, $w-1, $h-1, 255; |
228 |
pixbuf_rect $pixbuf, 0xffcc7700, 0, $h-$linew-1, $w-1, $h-1, 255; |
229 |
|
230 |
for my $i (1 .. $size) { |
231 |
pixbuf_rect $pixbuf, $gridcolour, $kx[$i] - $linew, $ky[1] - $linew, $kx[$i] + $linew, $ky[$size] + $linew, 255; |
232 |
pixbuf_rect $pixbuf, $gridcolour, $kx[1] - $linew, $ky[$i] - $linew, $kx[$size] + $linew, $ky[$i] + $linew, 255; |
233 |
} |
234 |
|
235 |
# hoshi points |
236 |
my $hoshi = sub { |
237 |
my ($x, $y) = @_; |
238 |
my $hs = 1 | int $edge / 4; |
239 |
$hs = 5 if $hs < 5; |
240 |
$x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2; |
241 |
|
242 |
# we use the shadow mask... not perfect, but I want to finish this |
243 |
$::shadow_img->composite ($pixbuf, |
244 |
$x, $y, ($hs + 1) x2, $x, $y, |
245 |
$hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height, |
246 |
'bilinear', 255); |
247 |
}; |
248 |
|
249 |
if ($size > 6) { |
250 |
my $h1 = $size < 10 ? 3 : 4; # corner / edge offset |
251 |
$hoshi->($h1, $h1); |
252 |
$hoshi->($size - $h1 + 1, $h1); |
253 |
$hoshi->($h1, $size - $h1 + 1); |
254 |
$hoshi->($size - $h1 + 1, $size - $h1 + 1); |
255 |
|
256 |
if ($size % 2) { # on odd boards, also the remaining 5 |
257 |
my $h2 = ($size + 1) / 2; |
258 |
if ($size > 10) { |
259 |
$hoshi->($h1, $h2); |
260 |
$hoshi->($size - $h1 + 1, $h2); |
261 |
$hoshi->($h2, $size - $h1 + 1); |
262 |
$hoshi->($h2, $h1); |
263 |
} |
264 |
# the tengen |
265 |
$hoshi->($h2, $h2); |
266 |
} |
267 |
} |
268 |
|
269 |
# now we have a board sans text |
270 |
$pixmap->draw_pixbuf ($self->style->white_gc, |
271 |
$pixbuf, |
272 |
0, 0, 0, 0, $w, $h, |
273 |
"normal", 0, 0); |
274 |
|
275 |
# now draw the labels |
276 |
for my $i (1 .. $size) { |
277 |
# 38 max, but we allow a bit more |
278 |
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 |
279 |
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]; |
280 |
|
281 |
$self->center_text ($pixmap, $labelcolour, $kx[$i], $borderh, $ofs * 0.7, $label); |
282 |
$self->center_text ($pixmap, $labelcolour, $kx[$i], $h2 + $borderh, $ofs * 0.7, $label); |
283 |
$self->center_text ($pixmap, $labelcolour, $borderw, $ky[$i], $ofs * 0.7, $size - $i + 1); |
284 |
$self->center_text ($pixmap, $labelcolour, $w2 + $borderw, $ky[$i], $ofs * 0.7, $size - $i + 1); |
285 |
} |
286 |
|
287 |
$self->{window}->set_back_pixmap ($pixmap, 0); |
288 |
|
289 |
$self->{backgroundpm} = $pixmap; |
290 |
$self->{backgroundpb} = $pixbuf; |
291 |
|
292 |
$edge = 1 | ceil $edge; |
293 |
$ofs = $edge * 0.5; |
294 |
|
295 |
my $shadow = $edge * SHADOW; |
296 |
|
297 |
$self->{draw_stone} = sub { |
298 |
my ($x, $y) = @_; |
299 |
|
300 |
my @area = ($kx[$x] - $ofs, $ky[$y] - $ofs, |
301 |
$edge + $shadow, $edge + $shadow); |
302 |
my @areai = ((ceil $area[0]), (ceil $area[1]), |
303 |
(int $area[2]), (int $area[3])); # area, integer |
304 |
|
305 |
my $pb = new_pixbuf @areai[2,3]; |
306 |
$self->{backgroundpb}->copy_area (@areai, $pb, 0, 0); |
307 |
|
308 |
my $put_stack = sub { |
309 |
my ($x, $y, $dx, $dy, $ox, $oy) = @_; |
310 |
|
311 |
my $mark = $self->{board}{board}[$x-1][$y-1]; |
312 |
|
313 |
if ($mark & ~MARK_LABEL) { |
314 |
my $stack = $self->get_stack ($mark, $edge); |
315 |
|
316 |
$stack->[($x ^ $y) % @$stack] |
317 |
->composite ($pb, |
318 |
$ox, $oy, |
319 |
$areai[2] + $dx - $ox, $areai[3] + $dy - $oy, |
320 |
$dx + $ox, $dy + $oy, |
321 |
1, 1, 'nearest', 255); |
322 |
} |
323 |
}; |
324 |
|
325 |
$put_stack->($x-1, $y, $kx[$x-1] - $kx[$x], 0, 0, 0) if $x > 1; |
326 |
$put_stack->($x, $y-1, 0, $ky[$y-1] - $ky[$y], 0, 0) if $y > 1; |
327 |
$put_stack->($x , $y , 0, 0); |
328 |
$put_stack->($x+1, $y, 0, 0, $kx[$x+1] - $kx[$x], 0) if $x < $size; |
329 |
$put_stack->($x, $y+1, 0, 0, 0, $ky[$y+1] - $ky[$y]) if $y < $size; |
330 |
|
331 |
# labels are handled here because they are quite rare |
332 |
# FIXME TODO RARE and BROKEN |
333 |
if ($mark & MARK_LABEL) { |
334 |
my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 0xffffff00; |
335 |
|
336 |
if ($white) { |
337 |
$self->center_text ($pb, 0, |
338 |
$ofs * 1.1, $ofs * 1.1, |
339 |
$ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); |
340 |
} |
341 |
$self->center_text ($pb, $white, |
342 |
$ofs, $ofs, |
343 |
$ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); |
344 |
} |
345 |
|
346 |
# speed none, normal, max |
347 |
$self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb, |
348 |
0, 0, @areai, 'max', 0, 0); |
349 |
|
350 |
\@areai; |
351 |
}; |
352 |
} |
353 |
|
354 |
# create a stack of stones |
355 |
sub get_stack { |
356 |
my ($self, $mark, $size) = @_; |
357 |
|
358 |
$self->{stack}{$mark} ||= do { |
359 |
my @stack; |
360 |
my $csize = ceil $size; |
361 |
my $shadow = $size * SHADOW; |
362 |
|
363 |
for my $stone ($mark & MARK_W ? @::white_img : @::black_img) { |
364 |
my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000; |
365 |
|
366 |
# zeroeth the shadow |
367 |
if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) { |
368 |
$::shadow_img->composite ( |
369 |
$base, $shadow, $shadow, $csize, $csize, $shadow, $shadow, |
370 |
$size / $::shadow_img->get_width, $size / $::shadow_img->get_height, |
371 |
'bilinear', 128 |
372 |
); |
373 |
} |
374 |
|
375 |
for ([MARK_B, $mark & MARK_GRAYED ? 96 : 255, 1], |
376 |
[MARK_W, $mark & MARK_GRAYED ? 160 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) { |
377 |
my ($mask, $alpha, $scale) = @$_; |
378 |
if ($mark & $mask) { |
379 |
$stone->composite ( |
380 |
$base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2, |
381 |
$size * $scale / $stone->get_width, $size * $scale / $stone->get_height, |
382 |
'bilinear', $alpha |
383 |
); |
384 |
} |
385 |
} |
386 |
|
387 |
# then the small stones |
388 |
for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]], |
389 |
[MARK_SMALL_W, $::white_img[$rand % @::white_img]]) { |
390 |
my ($mask, $img) = @$_; |
391 |
if ($mark & $mask) { |
392 |
$img->composite ( |
393 |
$base, (int $size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2, |
394 |
$size / $img->get_width / 2, $size / $img->get_height / 2, |
395 |
'bilinear', 255 |
396 |
); |
397 |
} |
398 |
} |
399 |
|
400 |
# and lastly any markers |
401 |
my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B)); |
402 |
|
403 |
for ([MARK_CIRCLE, $::circle_img[$dark_bg]], |
404 |
[MARK_TRIANGLE, $::triangle_img[$dark_bg]], |
405 |
[MARK_SQUARE, $::square_img[$dark_bg]]) { |
406 |
my ($mask, $img) = @$_; |
407 |
if ($mark & $mask) { |
408 |
$img->composite ( |
409 |
$base, 0, 0, $size, $size, 0, 0, |
410 |
$size / $img->get_width, $size / $img->get_height, |
411 |
'bilinear', 176 |
412 |
); |
413 |
} |
414 |
} |
415 |
|
416 |
push @stack, $base; |
417 |
} |
418 |
|
419 |
\@stack; |
420 |
} |
421 |
} |
422 |
|
423 |
sub draw_board { |
424 |
my ($self, $new, $dopaint) = @_; |
425 |
|
426 |
($self->{board}, my $old) = ($new, $self->{board}); |
427 |
|
428 |
my $draw_stone = $self->{draw_stone}; |
429 |
|
430 |
if ($self->{backgroundpb}) { |
431 |
my @areas; |
432 |
|
433 |
my $size1 = $self->{size} - 1; |
434 |
|
435 |
for my $x (0 .. $size1) { |
436 |
my $old = $old->{board}[$x]; |
437 |
my $new = $new->{board}[$x]; |
438 |
|
439 |
for my $y (0 .. $size1) { |
440 |
push @areas, $draw_stone->($x+1, $y+1) |
441 |
if $old->[$y] != $new->[$y]; |
442 |
} |
443 |
} |
444 |
|
445 |
if ($dopaint && @areas) { |
446 |
# a single full clear_area is way faster than many single calls here |
447 |
# the "cut-off" point is arbitrary |
448 |
if (@areas > 32) { |
449 |
# update a single rectangle only |
450 |
my $rect = new Gtk2::Gdk::Rectangle @{pop @areas}; |
451 |
$rect = $rect->union (new Gtk2::Gdk::Rectangle @$_) for @areas; |
452 |
$self->{window}->clear_area ($rect->values); |
453 |
} else { |
454 |
# update all the affected rectangles |
455 |
$self->{window}->clear_area (@$_) for @areas; |
456 |
} |
457 |
} |
458 |
} |
459 |
} |
460 |
|
461 |
sub cursor { |
462 |
my ($self, $show) = @_; |
463 |
|
464 |
return unless exists $self->{cursorpos} |
465 |
&& $self->{cursor_mask} |
466 |
&& $self->{backgroundpb}; |
467 |
|
468 |
my ($x, $y) = @{$self->{cursorpos}}; |
469 |
|
470 |
my $new = $self->{board}{board}[$x-1][$y-1]; |
471 |
|
472 |
if ($show) { |
473 |
if ($new & $self->{cursor_mask}) { |
474 |
delete $self->{cursorpos}; |
475 |
} else { |
476 |
$new = $new & ~$self->{cursor_mask} | $self->{cursor_value}; |
477 |
} |
478 |
} |
479 |
|
480 |
local $self->{board}{board}[$x-1][$y-1] = $new; |
481 |
$self->{window}->clear_area (@{ $self->{draw_stone}->($x, $y) }); |
482 |
} |
483 |
|
484 |
sub motion { |
485 |
my ($self) = @_; |
486 |
|
487 |
return unless $self->{cursor_mask} && $self->{backgroundpb}; |
488 |
|
489 |
my $window = $self->{canvas}->window; |
490 |
my (undef, $x, $y, undef) = $window->get_pointer; |
491 |
|
492 |
my $size = $self->{size}; |
493 |
|
494 |
my $x = int (($x - $self->{kx}[0]) * $size / ($self->{kx}[$size] - $self->{kx}[0]) + 0.5); |
495 |
my $y = int (($y - $self->{ky}[0]) * $size / ($self->{ky}[$size] - $self->{ky}[0]) + 0.5); |
496 |
|
497 |
my $pos = $self->{cursorpos}; |
498 |
if ($x != $pos->[0] |
499 |
|| $y != $pos->[1]) { |
500 |
|
501 |
$self->cursor (0); |
502 |
|
503 |
if ($x > 0 && $x <= $size |
504 |
&& $y > 0 && $y <= $size) { |
505 |
$self->{cursorpos} = [$x, $y]; |
506 |
$self->cursor (1); |
507 |
} |
508 |
} |
509 |
} |
510 |
|
511 |
sub buttonpress { |
512 |
my ($self, $event) = @_; |
513 |
|
514 |
$self->motion; |
515 |
|
516 |
if ($self->{cursorpos}) { |
517 |
my ($button, $x, $y) = ($event->button, @{ $self->{cursorpos} }); |
518 |
warn "buttonpress $button \@$x,$y\n"; |
519 |
} |
520 |
} |
521 |
|
522 |
1; |
523 |
|