ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/board.pl
Revision: 1.19
Committed: Sat Jul 26 02:45:01 2003 UTC (20 years, 10 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.18: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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