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