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