ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/board.pl
Revision: 1.13
Committed: Sun Jul 20 01:29:20 2003 UTC (20 years, 10 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.12: +4 -8 lines
Log Message:
*** empty log message ***

File Contents

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