ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/board.pl
Revision: 1.24
Committed: Wed Jul 30 00:32:42 2003 UTC (20 years, 10 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.23: +0 -0 lines
State: FILE REMOVED
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.16 use KGS::Constants;
6     use KGS::Game::Board;
7    
8     use POSIX qw(ceil);
9    
10 pcg 1.10 use Gtk2;
11 pcg 1.16
12 pcg 1.10 use Glib::Object::Subclass
13 pcg 1.11 Gtk2::AspectFrame,
14 pcg 1.10 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 pcg 1.20 Glib::ParamSpec->IV (
23     "cursor-mask",
24     "cursor mask",
25     "The mask used to show a cursor",
26     0, 1<<30, 0,
27     [qw(writable readable)],
28     ),
29     Glib::ParamSpec->IV (
30     "cursor-value",
31     "cursor value",
32     "The value used to show a cursor",
33     0, 1<<30, 0,
34     [qw(writable readable)],
35     ),
36 pcg 1.10 ];
37 pcg 1.1
38 pcg 1.11 sub TRAD_WIDTH (){ 42.42 } # traditional board width
39     sub TRAD_HEIGHT (){ 45.45 } # traditional board height
40     sub TRAD_SIZE_B (){ 2.18 } # traditional black stone size
41     sub TRAD_SIZE_W (){ 2.12 } # traditional white stone size
42    
43 pcg 1.19 sub SHADOW (){ 0.06 }
44 pcg 1.17
45 pcg 1.4 sub INIT_INSTANCE {
46 pcg 1.1 my $self = shift;
47    
48     $self->double_buffered (0);
49 pcg 1.14 $self->set (border_width => 0, shadow_type => 'none',
50     obey_child => 0, ratio => TRAD_WIDTH / TRAD_HEIGHT);
51 pcg 1.20 $self->set(cursor_mask => MARK_B | MARK_W, cursor_value => MARK_B | MARK_GRAYED);
52 pcg 1.1
53 pcg 1.11 $self->add ($self->{canvas} = new Gtk2::DrawingArea);
54 pcg 1.20
55     $self->{canvas}->signal_connect (configure_event => sub { $self->configure_event ($_[1]) });
56     $self->{canvas}->signal_connect (motion_notify_event => sub { $self->motion });
57     $self->{canvas}->signal_connect (leave_notify_event => sub { $self->cursor (0); delete $self->{cursorpos} });
58     $self->{canvas}->signal_connect (button_press_event => sub { $self->buttonpress ($_[1]) });
59    
60     $self->{canvas}->set_events ([
61     @{ $self->{canvas}->get_events },
62     'leave-notify-mask',
63     'button-press-mask',
64     'pointer-motion-mask',
65     'pointer-motion-hint-mask'
66     ]);
67 pcg 1.1 }
68    
69 pcg 1.20 sub FINALIZE {
70     warn "FINALIZE(@_)\n";#d#
71     my ($self) = @_;
72     $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy
73     for BLACK, WHITE;
74     $self->SUPER::destroy;
75     delete $self->{app}{gamelist}{game}{$self->{channel}};
76     die;
77     }
78    
79     sub SET_PROPERTY {
80     my ($self, $pspec, $newval) = @_;
81    
82     $pspec = $pspec->get_name;
83 pcg 1.1
84 pcg 1.20 $self->cursor (0) if $pspec =~ /^cursor/;
85     $self->{$pspec} = $newval;
86     $self->cursor (1) if $pspec =~ /^cursor/;
87 pcg 1.11 }
88 pcg 1.1
89     sub configure_event {
90     my ($self, $event) = @_;
91    
92 pcg 1.11 $self->{window} = $self->{canvas}->window;
93    
94 pcg 1.17 my $drawable = $self->{window};
95    
96     $drawable->set_back_pixmap (undef, 0);
97    
98     #my $gc = new Gtk2::Gdk::GC $drawable;
99     #$gc->set_rgb_fg_color (new Gtk2::Gdk::Color 0xe0e0, 0xb2b2, 0x5e5e);
100     #$drawable->draw_rectangle ($gc, 1, 0, 0, $self->allocation->width, $self->allocation->height);
101 pcg 1.11
102 pcg 1.17 # remove Glib::Source $self->{idle};
103 pcg 1.11 $self->{idle} ||= add Glib::Idle sub {
104     delete $self->{stack};
105 pcg 1.1
106 pcg 1.11 $self->{width} = $self->{canvas}->allocation->width;
107     $self->{height} = $self->{canvas}->allocation->height;
108 pcg 1.20 $self->draw_background;
109 pcg 1.1
110 pcg 1.20 $self->draw_board (delete $self->{board}, 0) if $self->{board};
111 pcg 1.11 $self->{window}->clear_area (0, 0, $self->{width}, $self->{height});
112    
113     delete $self->{idle};
114    
115     0;
116     };
117 pcg 1.1
118     1;
119     }
120    
121 pcg 1.11 sub set_board {
122     my ($self, $board) = @_;
123    
124 pcg 1.20 $self->cursor (0);
125     $self->draw_board ($board, 1);
126     $self->cursor (1);
127 pcg 1.11 }
128    
129 pcg 1.1 sub new_pixbuf {
130     my ($w, $h, $alpha, $fill) = @_;
131    
132     my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
133     $pixbuf->fill ($fill) if defined $fill;
134    
135     $pixbuf;
136     }
137    
138     sub scale_pixbuf {
139     my ($src, $w, $h, $mode, $alpha) = @_;
140    
141     my $dst = new_pixbuf $w, $h, $alpha;
142    
143     $src->scale(
144     $dst, 0, 0, $w, $h, 0, 0,
145     $w / $src->get_width, $h / $src->get_height,
146     $mode,
147     );
148    
149     $dst;
150     }
151    
152     sub pixbuf_rect {
153     my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_;
154     # we fake lines by... a horrible method :/
155     my $colour_pb = new_pixbuf 1, 1, 0, $colour;
156     $colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1,
157     'nearest', $alpha);
158     }
159    
160 pcg 1.3 sub center_text {
161     my ($self, $drawable, $colour, $x, $y, $size, $text) = @_;
162    
163 pcg 1.11 # could be optimized by caching quite a bit
164    
165 pcg 1.3 my $context = $self->get_pango_context;
166     my $font = $context->get_font_description;
167     $font->set_size ($size * Gtk2::Pango->scale);
168    
169     my $layout = new Gtk2::Pango::Layout $context;
170     $layout->set_text ($text);
171     my ($w, $h) = $layout->get_pixel_size;
172    
173 pcg 1.11 my $gc = new Gtk2::Gdk::GC $drawable;
174     my $r = (($colour >> 24) & 255) * 65535 / 255;
175     my $g = (($colour >> 16) & 255) * 65535 / 255;
176     my $b = (($colour >> 8) & 255) * 65535 / 255;
177     $gc->set_rgb_fg_color (new Gtk2::Gdk::Color $r, $g, $b);
178 pcg 1.3
179 pcg 1.11 $drawable->draw_layout ($gc, $x - $w*0.5, $y - $h*0.5, $layout);
180 pcg 1.3 }
181    
182     # draw an empty board and attach the bg pixmap
183 pcg 1.20 sub draw_background {
184 pcg 1.11 my ($self) = @_;
185 pcg 1.1 my $canvas = $self->{canvas};
186    
187     my $size = $self->{size};
188    
189 pcg 1.11 my $w = $self->{width};
190     my $h = $self->{height};
191    
192     my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $w, $h, -1;
193 pcg 1.3
194 pcg 1.18 #my $gridcolour = 0x88444400; # black is traditional, but only with overlapping stones
195     my $gridcolour = 0x44444400; # black is traditional, but only with overlapping stones
196 pcg 1.12 my $labelcolour = 0x88444400;
197    
198 pcg 1.1 # we leave enough space for the shadows.. I like smaller stones, and we
199 pcg 1.11 # do no need to do the nifty recursive screen updates that cgoban2 does
200 pcg 1.14 # TODO: smaller == buggy, want visual perfectness
201 pcg 1.11 my $borderw = int ($w / ($size + 3) * 0.5);
202 pcg 1.14 my $borderh = $borderw;
203 pcg 1.11 my $w2 = $w - $borderw * 2;
204     my $h2 = $h - $borderh * 2;
205 pcg 1.21 my $edge = ceil ($w2 / ($size + 1));
206 pcg 1.11 my $ofs = $edge * 0.5;
207 pcg 1.1
208 pcg 1.11 my @kx = map int ($w2 * $_ / ($size+1) + $borderw + 0.5), 0 .. $size; $self->{kx} = \@kx;
209     my @ky = map int ($h2 * $_ / ($size+1) + $borderh + 0.5), 0 .. $size; $self->{ky} = \@ky;
210 pcg 1.1
211     my $pixbuf;
212    
213     my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
214    
215 pcg 1.11 if ($w < $bw && $h < $bh) {
216     $pixbuf = new_pixbuf $w, $h, 0;
217     $::board_img->copy_area (0, 0, $w, $h, $pixbuf, 0, 0);
218 pcg 1.1 } else {
219 pcg 1.11 $pixbuf = scale_pixbuf $::board_img, $w, $h, $::config->{speed} ? 'nearest' : 'bilinear', 0;
220 pcg 1.1 }
221    
222 pcg 1.11 my $linew = int ($w / 40 / $size);
223 pcg 1.1
224     # ornamental border... we have time to waste :/
225 pcg 1.11 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $w-1, $linew, 255;
226     pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $h-1, 255;
227     pixbuf_rect $pixbuf, 0xffcc7700, $w-$linew-1, 0, $w-1, $h-1, 255;
228     pixbuf_rect $pixbuf, 0xffcc7700, 0, $h-$linew-1, $w-1, $h-1, 255;
229 pcg 1.1
230     for my $i (1 .. $size) {
231 pcg 1.12 pixbuf_rect $pixbuf, $gridcolour, $kx[$i] - $linew, $ky[1] - $linew, $kx[$i] + $linew, $ky[$size] + $linew, 255;
232     pixbuf_rect $pixbuf, $gridcolour, $kx[1] - $linew, $ky[$i] - $linew, $kx[$size] + $linew, $ky[$i] + $linew, 255;
233 pcg 1.1 }
234    
235     # hoshi points
236     my $hoshi = sub {
237     my ($x, $y) = @_;
238 pcg 1.11 my $hs = 1 | int $edge / 4;
239     $hs = 5 if $hs < 5;
240     $x = $kx[$x] - $hs / 2; $y = $ky[$y] - $hs / 2;
241 pcg 1.1
242     # we use the shadow mask... not perfect, but I want to finish this
243     $::shadow_img->composite ($pixbuf,
244 pcg 1.11 $x, $y, ($hs + 1) x2, $x, $y,
245 pcg 1.1 $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height,
246 pcg 1.11 'bilinear', 255);
247 pcg 1.1 };
248    
249 pcg 1.11 if ($size > 6) {
250     my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
251     $hoshi->($h1, $h1);
252     $hoshi->($size - $h1 + 1, $h1);
253     $hoshi->($h1, $size - $h1 + 1);
254     $hoshi->($size - $h1 + 1, $size - $h1 + 1);
255    
256     if ($size % 2) { # on odd boards, also the remaining 5
257     my $h2 = ($size + 1) / 2;
258     if ($size > 10) {
259     $hoshi->($h1, $h2);
260     $hoshi->($size - $h1 + 1, $h2);
261     $hoshi->($h2, $size - $h1 + 1);
262     $hoshi->($h2, $h1);
263     }
264     # the tengen
265     $hoshi->($h2, $h2);
266 pcg 1.1 }
267     }
268    
269 pcg 1.3 # now we have a board sans text
270     $pixmap->draw_pixbuf ($self->style->white_gc,
271     $pixbuf,
272 pcg 1.11 0, 0, 0, 0, $w, $h,
273 pcg 1.3 "normal", 0, 0);
274    
275     # now draw the labels
276     for my $i (1 .. $size) {
277     # 38 max, but we allow a bit more
278     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
279     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];
280    
281 pcg 1.12 $self->center_text ($pixmap, $labelcolour, $kx[$i], $borderh, $ofs * 0.7, $label);
282     $self->center_text ($pixmap, $labelcolour, $kx[$i], $h2 + $borderh, $ofs * 0.7, $label);
283     $self->center_text ($pixmap, $labelcolour, $borderw, $ky[$i], $ofs * 0.7, $size - $i + 1);
284     $self->center_text ($pixmap, $labelcolour, $w2 + $borderw, $ky[$i], $ofs * 0.7, $size - $i + 1);
285 pcg 1.3 }
286    
287 pcg 1.11 $self->{window}->set_back_pixmap ($pixmap, 0);
288 pcg 1.3
289     $self->{backgroundpm} = $pixmap;
290     $self->{backgroundpb} = $pixbuf;
291 pcg 1.20
292 pcg 1.21 $edge = 1 | ceil $edge;
293 pcg 1.20 $ofs = $edge * 0.5;
294    
295     my $shadow = $edge * SHADOW;
296    
297     $self->{draw_stone} = sub {
298     my ($x, $y) = @_;
299    
300     my @area = ($kx[$x] - $ofs, $ky[$y] - $ofs,
301     $edge + $shadow, $edge + $shadow);
302     my @areai = ((ceil $area[0]), (ceil $area[1]),
303     (int $area[2]), (int $area[3])); # area, integer
304    
305     my $pb = new_pixbuf @areai[2,3];
306     $self->{backgroundpb}->copy_area (@areai, $pb, 0, 0);
307    
308     my $put_stack = sub {
309     my ($x, $y, $dx, $dy, $ox, $oy) = @_;
310    
311     my $mark = $self->{board}{board}[$x-1][$y-1];
312    
313     if ($mark & ~MARK_LABEL) {
314     my $stack = $self->get_stack ($mark, $edge);
315    
316     $stack->[($x ^ $y) % @$stack]
317     ->composite ($pb,
318     $ox, $oy,
319     $areai[2] + $dx - $ox, $areai[3] + $dy - $oy,
320     $dx + $ox, $dy + $oy,
321     1, 1, 'nearest', 255);
322     }
323     };
324    
325     $put_stack->($x-1, $y, $kx[$x-1] - $kx[$x], 0, 0, 0) if $x > 1;
326     $put_stack->($x, $y-1, 0, $ky[$y-1] - $ky[$y], 0, 0) if $y > 1;
327     $put_stack->($x , $y , 0, 0);
328     $put_stack->($x+1, $y, 0, 0, $kx[$x+1] - $kx[$x], 0) if $x < $size;
329     $put_stack->($x, $y+1, 0, 0, 0, $ky[$y+1] - $ky[$y]) if $y < $size;
330    
331     # labels are handled here because they are quite rare
332     # FIXME TODO RARE and BROKEN
333     if ($mark & MARK_LABEL) {
334     my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 0xffffff00;
335    
336     if ($white) {
337     $self->center_text ($pb, 0,
338     $ofs * 1.1, $ofs * 1.1,
339     $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]);
340     }
341     $self->center_text ($pb, $white,
342     $ofs, $ofs,
343     $ofs * 0.7, $self->{board}{label}[$x-1][$y-1]);
344     }
345    
346     # speed none, normal, max
347     $self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb,
348     0, 0, @areai, 'max', 0, 0);
349    
350     \@areai;
351     };
352 pcg 1.1 }
353    
354 pcg 1.11 # create a stack of stones
355     sub get_stack {
356     my ($self, $mark, $size) = @_;
357 pcg 1.4
358 pcg 1.11 $self->{stack}{$mark} ||= do {
359     my @stack;
360     my $csize = ceil $size;
361 pcg 1.17 my $shadow = $size * SHADOW;
362 pcg 1.1
363 pcg 1.13 for my $stone ($mark & MARK_W ? @::white_img : @::black_img) {
364 pcg 1.11 my $base = new_pixbuf +(ceil $size + $shadow) x2, 1, 0x00000000;
365 pcg 1.2
366 pcg 1.11 # zeroeth the shadow
367 pcg 1.13 if (~$mark & MARK_GRAYED and $mark & (MARK_B | MARK_W)) {
368 pcg 1.11 $::shadow_img->composite (
369     $base, $shadow, $shadow, $csize, $csize, $shadow, $shadow,
370     $size / $::shadow_img->get_width, $size / $::shadow_img->get_height,
371     'bilinear', 128
372     );
373     }
374 pcg 1.1
375 pcg 1.13 for ([MARK_B, $mark & MARK_GRAYED ? 96 : 255, 1],
376     [MARK_W, $mark & MARK_GRAYED ? 160 : 255, TRAD_SIZE_W / TRAD_SIZE_B]) {
377 pcg 1.11 my ($mask, $alpha, $scale) = @$_;
378     if ($mark & $mask) {
379     $stone->composite (
380     $base, 0, 0, $csize, $csize, ($size * (1 - $scale) * 0.5 ) x2,
381     $size * $scale / $stone->get_width, $size * $scale / $stone->get_height,
382     'bilinear', $alpha
383     );
384     }
385     }
386 pcg 1.1
387 pcg 1.11 # then the small stones
388     for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]],
389     [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) {
390     my ($mask, $img) = @$_;
391     if ($mark & $mask) {
392     $img->composite (
393     $base, (int $size / 4) x2, (ceil $size / 2 + 1) x2, ($size / 4) x2,
394     $size / $img->get_width / 2, $size / $img->get_height / 2,
395     'bilinear', 255
396     );
397 pcg 1.1 }
398 pcg 1.11 }
399    
400     # and lastly any markers
401     my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B));
402    
403     for ([MARK_CIRCLE, $::circle_img[$dark_bg]],
404     [MARK_TRIANGLE, $::triangle_img[$dark_bg]],
405     [MARK_SQUARE, $::square_img[$dark_bg]]) {
406     my ($mask, $img) = @$_;
407     if ($mark & $mask) {
408     $img->composite (
409     $base, 0, 0, $size, $size, 0, 0,
410     $size / $img->get_width, $size / $img->get_height,
411     'bilinear', 176
412     );
413     }
414     }
415    
416     push @stack, $base;
417     }
418    
419     \@stack;
420     }
421     }
422    
423 pcg 1.20 sub draw_board {
424     my ($self, $new, $dopaint) = @_;
425    
426     ($self->{board}, my $old) = ($new, $self->{board});
427    
428     my $draw_stone = $self->{draw_stone};
429 pcg 1.1
430 pcg 1.11 if ($self->{backgroundpb}) {
431     my @areas;
432    
433 pcg 1.20 my $size1 = $self->{size} - 1;
434    
435     for my $x (0 .. $size1) {
436     my $old = $old->{board}[$x];
437     my $new = $new->{board}[$x];
438    
439     for my $y (0 .. $size1) {
440     push @areas, $draw_stone->($x+1, $y+1)
441     if $old->[$y] != $new->[$y];
442 pcg 1.1 }
443 pcg 1.2 }
444    
445 pcg 1.20 if ($dopaint && @areas) {
446     # a single full clear_area is way faster than many single calls here
447 pcg 1.11 # the "cut-off" point is arbitrary
448 pcg 1.20 if (@areas > 32) {
449 pcg 1.11 # update a single rectangle only
450     my $rect = new Gtk2::Gdk::Rectangle @{pop @areas};
451     $rect = $rect->union (new Gtk2::Gdk::Rectangle @$_) for @areas;
452     $self->{window}->clear_area ($rect->values);
453     } else {
454     # update all the affected rectangles
455     $self->{window}->clear_area (@$_) for @areas;
456     }
457 pcg 1.1 }
458     }
459 pcg 1.20 }
460    
461     sub cursor {
462     my ($self, $show) = @_;
463    
464     return unless exists $self->{cursorpos}
465     && $self->{cursor_mask}
466     && $self->{backgroundpb};
467    
468     my ($x, $y) = @{$self->{cursorpos}};
469    
470     my $new = $self->{board}{board}[$x-1][$y-1];
471    
472 pcg 1.22 if ($show) {
473     if ($new & $self->{cursor_mask}) {
474     delete $self->{cursorpos};
475     } else {
476 pcg 1.23 $new = $new & ~$self->{cursor_mask} | $self->{cursor_value};
477 pcg 1.22 }
478 pcg 1.20 }
479 pcg 1.1
480 pcg 1.20 local $self->{board}{board}[$x-1][$y-1] = $new;
481     $self->{window}->clear_area (@{ $self->{draw_stone}->($x, $y) });
482 pcg 1.1 }
483    
484 pcg 1.20 sub motion {
485 pcg 1.1 my ($self) = @_;
486 pcg 1.20
487     return unless $self->{cursor_mask} && $self->{backgroundpb};
488    
489     my $window = $self->{canvas}->window;
490     my (undef, $x, $y, undef) = $window->get_pointer;
491    
492     my $size = $self->{size};
493    
494     my $x = int (($x - $self->{kx}[0]) * $size / ($self->{kx}[$size] - $self->{kx}[0]) + 0.5);
495     my $y = int (($y - $self->{ky}[0]) * $size / ($self->{ky}[$size] - $self->{ky}[0]) + 0.5);
496    
497     my $pos = $self->{cursorpos};
498     if ($x != $pos->[0]
499     || $y != $pos->[1]) {
500    
501     $self->cursor (0);
502    
503     if ($x > 0 && $x <= $size
504     && $y > 0 && $y <= $size) {
505     $self->{cursorpos} = [$x, $y];
506     $self->cursor (1);
507     }
508     }
509     }
510    
511     sub buttonpress {
512     my ($self, $event) = @_;
513    
514     $self->motion;
515    
516     if ($self->{cursorpos}) {
517     my ($button, $x, $y) = ($event->button, @{ $self->{cursorpos} });
518     warn "buttonpress $button \@$x,$y\n";
519     }
520 pcg 1.1 }
521    
522     1;
523