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