ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/board.pl
Revision: 1.11
Committed: Sat Jun 28 04:26:15 2003 UTC (20 years, 11 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.10: +270 -203 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.2 package Gtk2::GoBoard;
2 pcg 1.1
3 pcg 1.11 use Scalar::Util;
4    
5 pcg 1.10 use Gtk2;
6     use Glib::Object::Subclass
7 pcg 1.11 Gtk2::AspectFrame,
8 pcg 1.10 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 pcg 1.11 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 pcg 1.10 ];
31 pcg 1.1
32     use KGS::Constants;
33     use KGS::Game::Board;
34    
35     use POSIX qw(ceil);
36    
37 pcg 1.11 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 pcg 1.4 sub INIT_INSTANCE {
43 pcg 1.1 my $self = shift;
44    
45     $self->double_buffered (0);
46 pcg 1.11 $self->set (border_width => 0, shadow_type => 'none', obey_child => 0);
47 pcg 1.1
48 pcg 1.11 $self->add ($self->{canvas} = new Gtk2::DrawingArea);
49     $self->{canvas}->signal_connect (configure_event => sub { $self->configure_event ($_[1]) });
50 pcg 1.1 }
51    
52 pcg 1.11 sub FINALIZE_INSTANCE {
53     my $self = shift;
54 pcg 1.1
55 pcg 1.11 die "FINALIZE board\n";#d#
56     }
57 pcg 1.1
58 pcg 1.11 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 pcg 1.1 }
70    
71     sub configure_event {
72     my ($self, $event) = @_;
73    
74 pcg 1.11 $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 pcg 1.1
85 pcg 1.11 $self->{width} = $self->{canvas}->allocation->width;
86     $self->{height} = $self->{canvas}->allocation->height;
87     $self->draw_board ();
88 pcg 1.1
89 pcg 1.11 $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 pcg 1.1
97     1;
98     }
99    
100 pcg 1.11 sub set_board {
101     my ($self, $board) = @_;
102    
103     $self->draw_stones ($board, 1);
104     }
105    
106 pcg 1.1 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 pcg 1.3 sub center_text {
138     my ($self, $drawable, $colour, $x, $y, $size, $text) = @_;
139    
140 pcg 1.11 # could be optimized by caching quite a bit
141    
142 pcg 1.3 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 pcg 1.11 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 pcg 1.3
156 pcg 1.11 $drawable->draw_layout ($gc, $x - $w*0.5, $y - $h*0.5, $layout);
157 pcg 1.3 }
158    
159     # draw an empty board and attach the bg pixmap
160 pcg 1.1 sub draw_board {
161 pcg 1.11 my ($self) = @_;
162 pcg 1.1 my $canvas = $self->{canvas};
163    
164     my $size = $self->{size};
165    
166 pcg 1.11 my $w = $self->{width};
167     my $h = $self->{height};
168    
169     my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $w, $h, -1;
170 pcg 1.3
171 pcg 1.1 # we leave enough space for the shadows.. I like smaller stones, and we
172 pcg 1.11 # do no need to do the nifty recursive screen updates that cgoban2 does
173     my $borderw = int ($w / ($size + 3) * 0.5);
174     my $borderh = int ($h / ($size + 3) * 0.5);
175     my $w2 = $w - $borderw * 2;
176     my $h2 = $h - $borderh * 2;
177     my $edge = $self->{edge} = $w2 / ($size + 1) * 0.975 - 1;
178     my $ofs = $edge * 0.5;
179 pcg 1.1
180 pcg 1.11 my @kx = map int ($w2 * $_ / ($size+1) + $borderw + 0.5), 0 .. $size; $self->{kx} = \@kx;
181     my @ky = map int ($h2 * $_ / ($size+1) + $borderh + 0.5), 0 .. $size; $self->{ky} = \@ky;
182 pcg 1.1
183     my $pixbuf;
184    
185     my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
186    
187 pcg 1.11 if ($w < $bw && $h < $bh) {
188     $pixbuf = new_pixbuf $w, $h, 0;
189     $::board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0);
190 pcg 1.1 } else {
191 pcg 1.11 $pixbuf = scale_pixbuf $::board_img, $w, $h, $::config->{speed} ? 'nearest' : 'bilinear', 0;
192 pcg 1.1 }
193    
194 pcg 1.11 my $linew = int ($w / 40 / $size);
195 pcg 1.1
196     # ornamental border... we have time to waste :/
197 pcg 1.11 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $w-1, $linew, 255;
198     pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $h-1, 255;
199     pixbuf_rect $pixbuf, 0xffcc7700, $w-$linew-1, 0, $w-1, $h-1, 255;
200     pixbuf_rect $pixbuf, 0xffcc7700, 0, $h-$linew-1, $w-1, $h-1, 255;
201 pcg 1.1
202     for my $i (1 .. $size) {
203 pcg 1.11 pixbuf_rect $pixbuf, 0x88444400, $kx[$i] - $linew, $ky[1] - $linew, $kx[$i] + $linew, $ky[$size] + $linew, 255;
204     pixbuf_rect $pixbuf, 0x88444400, $kx[1] - $linew, $ky[$i] - $linew, $kx[$size] + $linew, $ky[$i] + $linew, 255;
205 pcg 1.1 }
206    
207     # hoshi points
208     my $hoshi = sub {
209     my ($x, $y) = @_;
210 pcg 1.11 my $hs = 1 | int $edge / 4;
211     $hs = 5 if $hs < 5;
212     $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2;
213 pcg 1.1
214     # we use the shadow mask... not perfect, but I want to finish this
215     $::shadow_img->composite ($pixbuf,
216 pcg 1.11 $x, $y, ($hs + 1) x2, $x, $y,
217 pcg 1.1 $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height,
218 pcg 1.11 'bilinear', 255);
219 pcg 1.1 };
220    
221 pcg 1.11 if ($size > 6) {
222     my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
223     $hoshi->($h1, $h1);
224     $hoshi->($size - $h1 + 1, $h1);
225     $hoshi->($h1, $size - $h1 + 1);
226     $hoshi->($size - $h1 + 1, $size - $h1 + 1);
227    
228     if ($size % 2) { # on odd boards, also the remaining 5
229     my $h2 = ($size + 1) / 2;
230     if ($size > 10) {
231     $hoshi->($h1, $h2);
232     $hoshi->($size - $h1 + 1, $h2);
233     $hoshi->($h2, $size - $h1 + 1);
234     $hoshi->($h2, $h1);
235     }
236     # the tengen
237     $hoshi->($h2, $h2);
238 pcg 1.1 }
239     }
240    
241 pcg 1.3 # now we have a board sans text
242     $pixmap->draw_pixbuf ($self->style->white_gc,
243     $pixbuf,
244 pcg 1.11 0, 0, 0, 0, $w, $h,
245 pcg 1.3 "normal", 0, 0);
246    
247     # now draw the labels
248     for my $i (1 .. $size) {
249     # 38 max, but we allow a bit more
250     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
251     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];
252    
253 pcg 1.11 $self->center_text ($pixmap, 0x88444400, $kx[$i], $borderh, $ofs * 0.7, $label);
254     $self->center_text ($pixmap, 0x88444400, $kx[$i], $h2 + $borderh, $ofs * 0.7, $label);
255     $self->center_text ($pixmap, 0x88444400, $borderw, $ky[$i], $ofs * 0.7, $size - $i + 1);
256     $self->center_text ($pixmap, 0x88444400, $w2 + $borderw, $ky[$i], $ofs * 0.7, $size - $i + 1);
257 pcg 1.3 }
258    
259 pcg 1.11 $self->{window}->set_back_pixmap ($pixmap, 0);
260 pcg 1.3
261     $self->{backgroundpm} = $pixmap;
262     $self->{backgroundpb} = $pixbuf;
263 pcg 1.1 }
264    
265 pcg 1.11 # create a stack of stones
266     sub get_stack {
267     my ($self, $mark, $size) = @_;
268 pcg 1.4
269 pcg 1.11 $self->{stack}{$mark} ||= do {
270     my @stack;
271     my $csize = ceil $size;
272     my $shadow = $size * 0.05;
273 pcg 1.1
274 pcg 1.11 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
275     my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000;
276 pcg 1.2
277 pcg 1.11 # zeroeth the shadow
278     if ($mark & (MARK_B | MARK_W)) {
279     $::shadow_img->composite (
280     $base, $shadow, $shadow, $csize, $csize, $shadow, $shadow,
281     $size / $::shadow_img->get_width, $size / $::shadow_img->get_height,
282     'bilinear', 128
283     );
284     }
285 pcg 1.1
286 pcg 1.11 # first the normal stones (handicap stones could be different)
287     my $ws = $self->{traditional} ? TRAD_SIZE_W / TRAD_SIZE_B : 1;
288     for ([MARK_B, 255, 1],
289     [MARK_W, 255, $ws],
290     [MARK_GRAY_B, 96, 1],
291     [MARK_GRAY_W, 160, $ws]) {
292     my ($mask, $alpha, $scale) = @$_;
293     if ($mark & $mask) {
294     $stone->composite (
295     $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2,
296     $size * $scale / $stone->get_width, $size * $scale / $stone->get_height,
297     'bilinear', $alpha
298     );
299     }
300     }
301 pcg 1.1
302 pcg 1.11 # then the small stones
303     for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]],
304     [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) {
305     my ($mask, $img) = @$_;
306     if ($mark & $mask) {
307     $img->composite (
308     $base, (int $size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2,
309     $size / $img->get_width / 2, $size / $img->get_height / 2,
310     'bilinear', 255
311     );
312 pcg 1.1 }
313 pcg 1.11 }
314    
315     # and lastly any markers
316     my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B));
317    
318     for ([MARK_CIRCLE, $::circle_img[$dark_bg]],
319     [MARK_TRIANGLE, $::triangle_img[$dark_bg]],
320     [MARK_SQUARE, $::square_img[$dark_bg]]) {
321     my ($mask, $img) = @$_;
322     if ($mark & $mask) {
323     $img->composite (
324     $base, 0, 0, $size, $size, 0, 0,
325     $size / $img->get_width, $size / $img->get_height,
326     'bilinear', 176
327     );
328     }
329     }
330    
331     push @stack, $base;
332     }
333    
334     \@stack;
335     }
336     }
337    
338     sub draw_stones {
339     my ($self, $board, $dopaint) = @_;
340 pcg 1.1
341 pcg 1.11 if ($self->{backgroundpb}) {
342     my @areas;
343    
344     my $old = delete $self->{board};
345     my $size = $self->{size};
346     my $edge = int ($self->{edge}) | 1;
347     my $ofs = $edge * 0.5;
348     my $kx = $self->{kx};
349     my $ky = $self->{ky};
350    
351     for my $x (1 .. $size) {
352     for my $y (1 .. $size) {
353     my $mark = $board->{board}[$x-1][$y-1];
354     my $mold = $old->{board}[$x-1][$y-1];
355    
356     if ($mold != $mark) {
357     my $rand = ($x ^ $y ^ 0x5555);
358    
359     my $shadow = $edge * 0.05;
360     my @area = ($kx->[$x] - $ofs, $ky->[$y] - $ofs,
361     $edge + $shadow, $edge + $shadow);
362     my @areai = ((ceil $area[0]), (ceil $area[1]),
363     (int $area[2]), (int $area[3])); # area, integer
364    
365     my $pb = new_pixbuf @areai[2,3];
366     $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0);
367    
368     if ($mark & ~MARK_LABEL) {
369     my $stack = $self->get_stack ($mark, $edge);
370    
371     $stack->[$rand % @$stack]
372     ->composite ($pb, 0, 0, @areai[2,3], 0, 0,
373     1, 1, 'nearest', 255);
374     }
375    
376     # speed none, normal, max
377     $self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb,
378     0, 0, @areai, 'max', 0, 0);
379    
380     # labels are handled here because they are quite rare
381     if ($mark & MARK_LABEL) {
382     my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 0xffffff00;
383    
384     if ($white) {
385     $self->center_text ($self->{backgroundpm}, 0,
386     $area[0] + $ofs * 1.1, $area[1] + $ofs * 1.1,
387     $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]);
388     }
389     $self->center_text ($self->{backgroundpm}, $white,
390     $area[0] + $ofs, $area[1] + $ofs,
391 pcg 1.3 $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]);
392     }
393 pcg 1.11
394     # a single full clear_area is way faster than many single calls here
395     push @areas, \@areai if $dopaint;
396    
397     #my $redraw; $redraw = sub {
398     # my ($x, $y) = @_;
399     # return if $old->{board}[$x][$y] & MARK_REDRAW;
400     # $old->{board}[$x][$y] |= MARK_REDRAW;
401     # $redraw->($x+1, $y) if $old->{board}[$x+1][$y] & (MARK_W | MARK_B | MARK_GREY_W | MARK_GREY_B);
402     # $redraw->($x, $y+1) if $old->{board}[$x][$y+1] & (MARK_W | MARK_B | MARK_GREY_W | MARK_GREY_B);
403     #};
404     #
405     #$redraw->($x-1, $y-1);
406 pcg 1.3 }
407 pcg 1.1 }
408 pcg 1.2 }
409    
410 pcg 1.11 if (@areas) {
411     # the "cut-off" point is arbitrary
412     if (@areas > 16) {
413     # update a single rectangle only
414     my $rect = new Gtk2::Gdk::Rectangle @{pop @areas};
415     $rect = $rect->union (new Gtk2::Gdk::Rectangle @$_) for @areas;
416     $self->{window}->clear_area ($rect->values);
417     } else {
418     # update all the affected rectangles
419     $self->{window}->clear_area (@$_) for @areas;
420     }
421 pcg 1.1 }
422     }
423    
424     $self->{board} = $board;
425     #d# save
426     #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable";
427     }
428    
429     sub FINALIZE {
430     warn "FINALIZE(@_)\n";#d#
431     my ($self) = @_;
432     $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy
433     for BLACK, WHITE;
434     $self->SUPER::destroy;
435     delete $appwin::gamelist->{game}{$self->{channel}};
436     }
437    
438     1;
439