ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/board.pl
Revision: 1.12
Committed: Sat Jun 28 16:44:56 2003 UTC (20 years, 11 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.11: +9 -6 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.12 my $gridcolour = 0x88444400; # black is traditional, but only with overlapping stones
172     my $labelcolour = 0x88444400;
173    
174 pcg 1.1 # we leave enough space for the shadows.. I like smaller stones, and we
175 pcg 1.11 # 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 pcg 1.1
183 pcg 1.11 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 pcg 1.1
186     my $pixbuf;
187    
188     my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
189    
190 pcg 1.11 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 pcg 1.1 } else {
194 pcg 1.11 $pixbuf = scale_pixbuf $::board_img, $w, $h, $::config->{speed} ? 'nearest' : 'bilinear', 0;
195 pcg 1.1 }
196    
197 pcg 1.11 my $linew = int ($w / 40 / $size);
198 pcg 1.1
199     # ornamental border... we have time to waste :/
200 pcg 1.11 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 pcg 1.1
205     for my $i (1 .. $size) {
206 pcg 1.12 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 pcg 1.1 }
209    
210     # hoshi points
211     my $hoshi = sub {
212     my ($x, $y) = @_;
213 pcg 1.11 my $hs = 1 | int $edge / 4;
214     $hs = 5 if $hs < 5;
215     $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2;
216 pcg 1.1
217     # we use the shadow mask... not perfect, but I want to finish this
218     $::shadow_img->composite ($pixbuf,
219 pcg 1.11 $x, $y, ($hs + 1) x2, $x, $y,
220 pcg 1.1 $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height,
221 pcg 1.11 'bilinear', 255);
222 pcg 1.1 };
223    
224 pcg 1.11 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 pcg 1.1 }
242     }
243    
244 pcg 1.3 # now we have a board sans text
245     $pixmap->draw_pixbuf ($self->style->white_gc,
246     $pixbuf,
247 pcg 1.11 0, 0, 0, 0, $w, $h,
248 pcg 1.3 "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 pcg 1.12 $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 pcg 1.3 }
261    
262 pcg 1.11 $self->{window}->set_back_pixmap ($pixmap, 0);
263 pcg 1.3
264     $self->{backgroundpm} = $pixmap;
265     $self->{backgroundpb} = $pixbuf;
266 pcg 1.1 }
267    
268 pcg 1.11 # create a stack of stones
269     sub get_stack {
270     my ($self, $mark, $size) = @_;
271 pcg 1.4
272 pcg 1.11 $self->{stack}{$mark} ||= do {
273     my @stack;
274     my $csize = ceil $size;
275     my $shadow = $size * 0.05;
276 pcg 1.1
277 pcg 1.11 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
278     my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000;
279 pcg 1.2
280 pcg 1.11 # zeroeth the shadow
281     if ($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 pcg 1.1
289 pcg 1.11 # first the normal stones (handicap stones could be different)
290     my $ws = $self->{traditional} ? TRAD_SIZE_W / TRAD_SIZE_B : 1;
291     for ([MARK_B, 255, 1],
292     [MARK_W, 255, $ws],
293     [MARK_GRAY_B, 96, 1],
294     [MARK_GRAY_W, 160, $ws]) {
295     my ($mask, $alpha, $scale) = @$_;
296     if ($mark & $mask) {
297     $stone->composite (
298     $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2,
299     $size * $scale / $stone->get_width, $size * $scale / $stone->get_height,
300     'bilinear', $alpha
301     );
302     }
303     }
304 pcg 1.1
305 pcg 1.11 # then the small stones
306     for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]],
307     [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) {
308     my ($mask, $img) = @$_;
309     if ($mark & $mask) {
310     $img->composite (
311     $base, (int $size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2,
312     $size / $img->get_width / 2, $size / $img->get_height / 2,
313     'bilinear', 255
314     );
315 pcg 1.1 }
316 pcg 1.11 }
317    
318     # and lastly any markers
319     my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B));
320    
321     for ([MARK_CIRCLE, $::circle_img[$dark_bg]],
322     [MARK_TRIANGLE, $::triangle_img[$dark_bg]],
323     [MARK_SQUARE, $::square_img[$dark_bg]]) {
324     my ($mask, $img) = @$_;
325     if ($mark & $mask) {
326     $img->composite (
327     $base, 0, 0, $size, $size, 0, 0,
328     $size / $img->get_width, $size / $img->get_height,
329     'bilinear', 176
330     );
331     }
332     }
333    
334     push @stack, $base;
335     }
336    
337     \@stack;
338     }
339     }
340    
341     sub draw_stones {
342     my ($self, $board, $dopaint) = @_;
343 pcg 1.1
344 pcg 1.11 if ($self->{backgroundpb}) {
345     my @areas;
346    
347     my $old = delete $self->{board};
348     my $size = $self->{size};
349     my $edge = int ($self->{edge}) | 1;
350     my $ofs = $edge * 0.5;
351     my $kx = $self->{kx};
352     my $ky = $self->{ky};
353    
354     for my $x (1 .. $size) {
355     for my $y (1 .. $size) {
356     my $mark = $board->{board}[$x-1][$y-1];
357     my $mold = $old->{board}[$x-1][$y-1];
358    
359     if ($mold != $mark) {
360     my $rand = ($x ^ $y ^ 0x5555);
361    
362     my $shadow = $edge * 0.05;
363     my @area = ($kx->[$x] - $ofs, $ky->[$y] - $ofs,
364     $edge + $shadow, $edge + $shadow);
365     my @areai = ((ceil $area[0]), (ceil $area[1]),
366     (int $area[2]), (int $area[3])); # area, integer
367    
368     my $pb = new_pixbuf @areai[2,3];
369     $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0);
370    
371     if ($mark & ~MARK_LABEL) {
372     my $stack = $self->get_stack ($mark, $edge);
373    
374     $stack->[$rand % @$stack]
375     ->composite ($pb, 0, 0, @areai[2,3], 0, 0,
376     1, 1, 'nearest', 255);
377     }
378    
379     # speed none, normal, max
380     $self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb,
381     0, 0, @areai, 'max', 0, 0);
382    
383     # labels are handled here because they are quite rare
384     if ($mark & MARK_LABEL) {
385     my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 0xffffff00;
386    
387     if ($white) {
388     $self->center_text ($self->{backgroundpm}, 0,
389     $area[0] + $ofs * 1.1, $area[1] + $ofs * 1.1,
390     $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]);
391     }
392     $self->center_text ($self->{backgroundpm}, $white,
393     $area[0] + $ofs, $area[1] + $ofs,
394 pcg 1.3 $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]);
395     }
396 pcg 1.11
397     # a single full clear_area is way faster than many single calls here
398     push @areas, \@areai if $dopaint;
399    
400     #my $redraw; $redraw = sub {
401     # my ($x, $y) = @_;
402     # return if $old->{board}[$x][$y] & MARK_REDRAW;
403     # $old->{board}[$x][$y] |= MARK_REDRAW;
404     # $redraw->($x+1, $y) if $old->{board}[$x+1][$y] & (MARK_W | MARK_B | MARK_GREY_W | MARK_GREY_B);
405     # $redraw->($x, $y+1) if $old->{board}[$x][$y+1] & (MARK_W | MARK_B | MARK_GREY_W | MARK_GREY_B);
406     #};
407     #
408     #$redraw->($x-1, $y-1);
409 pcg 1.3 }
410 pcg 1.1 }
411 pcg 1.2 }
412    
413 pcg 1.11 if (@areas) {
414     # the "cut-off" point is arbitrary
415     if (@areas > 16) {
416     # update a single rectangle only
417     my $rect = new Gtk2::Gdk::Rectangle @{pop @areas};
418     $rect = $rect->union (new Gtk2::Gdk::Rectangle @$_) for @areas;
419     $self->{window}->clear_area ($rect->values);
420     } else {
421     # update all the affected rectangles
422     $self->{window}->clear_area (@$_) for @areas;
423     }
424 pcg 1.1 }
425     }
426    
427     $self->{board} = $board;
428     #d# save
429     #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable";
430     }
431    
432     sub FINALIZE {
433     warn "FINALIZE(@_)\n";#d#
434     my ($self) = @_;
435     $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy
436     for BLACK, WHITE;
437     $self->SUPER::destroy;
438     delete $appwin::gamelist->{game}{$self->{channel}};
439     }
440    
441     1;
442