ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
Revision: 1.18
Committed: Sun Jun 1 04:52:16 2003 UTC (21 years ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.17: +3 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 package game;
2    
3     use KGS::Constants;
4     use KGS::Game::Board;
5    
6     use base KGS::Listener::Game;
7     use base KGS::Game;
8    
9 pcg 1.4 use base gtk::widget;
10    
11 pcg 1.15 use POSIX qw(ceil);
12    
13 pcg 1.1 sub new {
14     my $self = shift;
15     $self = $self->SUPER::new(@_);
16    
17     $self->listen($self->{conn});
18    
19     $self->{window} = new Gtk2::Window 'toplevel';
20 pcg 1.5 my $title = $self->{channel} ? $self->owner->as_string." ".$self->opponent_string : "Game Window";
21 pcg 1.1 $self->{window}->set_title("KGS Game $title");
22     gtk::state $self->{window}, "game::window", undef, window_size => [600, 500];
23    
24 pcg 1.4 $self->{window}->signal_connect(delete_event => sub {
25     if ($self->{joined}) {
26     $self->part;
27     } else {
28     $self->event_part;
29     }
30     1;
31     });
32 pcg 1.1
33 pcg 1.4 $self->{window}->add($self->{hpane} = new Gtk2::HPaned);
34     gtk::state $self->{hpane}, "game::hpane", undef, position => 500;
35 pcg 1.1
36 pcg 1.4 $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1);
37 pcg 1.1
38     $vbox->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
39    
40     {
41 pcg 1.6 # grrr...
42 pcg 1.1 $frame->add(my $vbox = new Gtk2::VBox);
43     $vbox->add($self->{title} = new Gtk2::Label $title);
44    
45 pcg 1.9 $self->{moveadj} = new Gtk2::Adjustment 1, 0, 1, 0.001, 0.05, 0;
46 pcg 1.2
47 pcg 1.1 $vbox->add(my $scale = new Gtk2::HScale $self->{moveadj});
48 pcg 1.2 $scale->set_draw_value (0);
49 pcg 1.1
50     $self->{moveadj}->signal_connect (value_changed => sub {
51 pcg 1.2 return unless $self->{path};
52    
53     my $move = int (@{$self->{path}} * $_[0]->get_value);
54    
55 pcg 1.1 $self->{board} = new KGS::Game::Board $self->{size};
56 pcg 1.2 $self->{board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
57 pcg 1.1
58 pcg 1.2 $self->redraw ($self->repaint_board);
59 pcg 1.1
60 pcg 1.2 $self->{text}->set_text(KGS::Listener::Debug::dumpval([$self->{board}{time},$self->{board}{captures}]). $self->{board}{comment});
61 pcg 1.1 });
62     }
63    
64     $vbox->pack_start(($self->{canvas} = new Gtk2::DrawingArea), 1, 1, 0);
65    
66     $self->{canvas}->signal_connect(configure_event => \&configure_event, $self);
67     $self->{canvas}->signal_connect(expose_event => \&expose_event, $self);
68    
69 pcg 1.4 $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 0, 0);
70     $self->{hpane}->set(position_set => 1);
71     gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
72 pcg 1.1
73 pcg 1.4 $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow);
74 pcg 1.1 $sw->set_policy("automatic", "always");
75    
76 pcg 1.18 $sw->add(($self->{userlist} = new userlist)->widget);
77 pcg 1.1
78 pcg 1.4 $self->{vpane}->add(my $vbox = new Gtk2::VBox);
79 pcg 1.1
80     $vbox->pack_start((my $sw = new Gtk2::ScrolledWindow), 1, 1, 0);
81 pcg 1.7 $sw->set_policy("never", "always");
82 pcg 1.1
83 pcg 1.2 $sw->add(($self->{text} = new gtk::text)->widget);
84 pcg 1.1
85     $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0);
86     $self->{entry}->signal_connect(activate => sub {
87     my $text = $self->{entry}->get_text;
88 pcg 1.10 $self->say($text) if $text =~ /\S/;
89 pcg 1.1 $self->{entry}->set_text("");
90     });
91    
92     $self;
93     }
94    
95     sub event_update_users {
96 pcg 1.18 my ($self, $add, $update, $remove) = @_;
97 pcg 1.1
98 pcg 1.18 $self->{userlist}->update ($add, $update, $remove);
99 pcg 1.1 }
100    
101     sub join {
102     my ($self) = @_;
103     $self->SUPER::join;
104    
105     $self->{window}->show_all;
106     }
107    
108     sub part {
109     my ($self) = @_;
110     $self->SUPER::part;
111    
112     $self->{window}->hide;
113     }
114    
115     sub configure_event {
116 pcg 1.2 my ($widget, $event, $self) = @_;
117 pcg 1.1 delete $self->{stack};
118     delete $self->{pixbuf};
119     delete $self->{board_shown};
120     delete $self->{background};
121     $self->repaint_board;
122 pcg 1.6 0;
123     }
124    
125     sub expose_event {
126     my ($widget, $event, $self) = @_;
127    
128     $self->{pixbuf} or return;
129    
130     my $area = $event->area;
131     my ($ox, $oy, $s) = @{$self->{offsets}};
132    
133     $self->redraw (
134     (new Gtk2::Gdk::Rectangle $area->x - $ox, $area->y - $oy, $area->width, $area->height)
135     ->intersect(new Gtk2::Gdk::Rectangle 0, 0, $s, $s)
136     );
137    
138     0;
139 pcg 1.1 }
140    
141 pcg 1.2 # something Gtk2 fixed
142     sub INTERP_NEAREST (){ 'nearest' }
143     sub INTERP_TILES (){ 'tiles' }
144     sub INTERP_BILINEAR (){ 'bilinear' }
145     sub INTERP_HYPER (){ 'hyper' }
146 pcg 1.1
147     sub new_pixbuf {
148 pcg 1.2 my ($w, $h, $alpha, $fill) = @_;
149 pcg 1.1
150     my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
151 pcg 1.2 $pixbuf->fill ($fill) if defined $fill;
152 pcg 1.1
153     $pixbuf;
154     }
155    
156     sub scale_pixbuf {
157     my ($src, $w, $h, $mode) = @_;
158    
159     my $dst = new_pixbuf $w, $h, 1;
160    
161     $src->scale(
162     $dst, 0, 0, $w, $h, 0, 0,
163     $w / $src->get_width, $h / $src->get_height,
164     $mode,
165     );
166    
167     $dst;
168     }
169    
170     # create a stack of stones
171     sub create_stack {
172     my ($self, $mark, $size, $rand) = @_;
173    
174 pcg 1.10 my $shadow = $size * 0.05;
175 pcg 1.1
176     my $c = \$self->{stack}{$mark};
177     unless ($$c) {
178     for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
179 pcg 1.2 my $base = new_pixbuf $size + $shadow, $size + $shadow, 1, 0x00000000;
180 pcg 1.1
181     # zeroeth the shadow
182     if ($mark & (MARK_B | MARK_W)) {
183 pcg 1.14 $::shadow_img->composite (
184     $base, $shadow, $shadow, $size, $size, $shadow - 0.5, $shadow - 0.5,
185 pcg 1.1 $size / $stone->get_width, $size / $stone->get_height,
186 pcg 1.12 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
187 pcg 1.1 );
188     }
189    
190     # first the big stones (handicap stones different for effect)
191     for ([MARK_B, $mark & MARK_MOVE ? 255 : 192],
192     [MARK_W, $mark & MARK_MOVE ? 255 : 192],
193     [MARK_GRAY_B, 128],
194     [MARK_GRAY_W, 128]) {
195     my ($mask, $alpha) = @$_;
196     if ($mark & $mask) {
197     $stone->composite (
198 pcg 1.11 $base, 0, 0, $size, $size, 0, 0,
199 pcg 1.1 $size / $stone->get_width, $size / $stone->get_height,
200 pcg 1.2 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, $alpha
201 pcg 1.1 );
202     }
203     }
204    
205 pcg 1.12 # then the small stones
206 pcg 1.1 for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]],
207     [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) {
208     my ($mask, $img) = @$_;
209     if ($mark & $mask) {
210     $img->composite (
211 pcg 1.15 $base, (ceil ($size / 4)) x2, (ceil ($size / 2)) x2, (ceil ($size / 4)) x2,
212 pcg 1.1 $size / $img->get_width / 2, $size / $img->get_height / 2,
213 pcg 1.15 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 224
214 pcg 1.1 );
215     }
216     }
217    
218     # and lastly any markers
219     my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B));
220    
221     for ([MARK_CIRCLE, $::circle_img[$dark_bg]],
222     [MARK_TRIANGLE, $::triangle_img[$dark_bg]],
223     [MARK_SQUARE, $::square_img[$dark_bg]]) {
224     my ($mask, $img) = @$_;
225     if ($mark & $mask) {
226     $img->composite (
227     $base, 0, 0, $size, $size, -0.5, -0.5,
228     $size / $img->get_width, $size / $img->get_height,
229 pcg 1.2 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255
230 pcg 1.1 );
231     }
232     }
233    
234     push @$$c, $base;
235     }
236     }
237    
238     $$c->[$rand % @$$c];
239     }
240    
241     sub pixbuf_text {
242     my ($pixbuf, $colour, $x, $y, $height, $text) = @_;
243    
244     my @c = grep $_,
245     map $::font[$colour][$::fontmap{$_}],
246     split //, $text;
247    
248     if (@c) {
249     my $spacing = $height * 0.1;
250     my $s = $height / List::Util::max map $_->get_height, @c;
251     my $W = List::Util::sum map $_->get_width, @c;
252    
253     $x -= ($W * $s + $spacing * (@c - 1)) * 0.5;
254     $y -= $height * 0.5;
255    
256     for (@c) {
257     my $w = $_->get_width * $s;
258 pcg 1.17 # +2 == don't fight the rounding
259 pcg 1.1 $_->composite ($pixbuf,
260 pcg 1.17 $x, $y, $w+2, $height+2, $x, $y, $s, $s,
261 pcg 1.1 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
262    
263     $x += $w + $spacing;
264     }
265     }
266     }
267    
268     sub pixbuf_rect {
269     my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_;
270 pcg 1.2 # we fake lines by... a horrible method :/
271     my $colour_pb = new_pixbuf 1, 1, 0, $colour;
272     $colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1,
273     INTERP_NEAREST, $alpha);
274 pcg 1.1 }
275    
276     sub repaint_board {
277     my ($self) = @_;
278     my $canvas = $self->{canvas};
279     my $expose_area = undef;
280    
281     return $expose_area unless $self->{board};
282    
283 pcg 1.2 my ($w, $h) = ($canvas->allocation->values)[2,3];
284 pcg 1.1
285     my $s = $w > $h ? $h : $w;
286 pcg 1.6
287     return unless $s > 128;
288 pcg 1.1
289     $self->{offsets} = [int (($w - $s) / 2), int (($h - $s) / 2), $s];
290    
291     my $size = $self->{size};
292    
293 pcg 1.16 # we leave enough space for the shadows.. I like smaller stones, and we
294     # do no need to do the nifty recursive screen updates that goban2 does
295 pcg 1.1 my $border = int ($s / ($size + 3) * 0.5);
296     my $s2 = $s - $border * 2;
297 pcg 1.10 my $edge = int ($s2 / ($size + 1) * 0.96) - ($::config->{randomize} ? 3 : 0);
298 pcg 1.1 my $ofs = int ($edge / 2);
299    
300     my @k = map int ($s2 * $_ / ($size+1) + $border + 0.5), 0 .. $size;
301    
302     my $pixbuf;
303    
304     my $oldboard;
305    
306     if ($self->{background}) {
307     if ($oldboard = $self->{board_shown}) {
308     $pixbuf = $self->{pixbuf};
309     } else {
310     $pixbuf = $self->{background}->copy;
311 pcg 1.5 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
312 pcg 1.1 }
313     } else {
314 pcg 1.5 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
315 pcg 1.1
316     my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
317    
318     if ($s < $bw && $s < $bh) {
319 pcg 1.2 $pixbuf = new_pixbuf $s, $s, 0;
320 pcg 1.1 $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0);
321     } else {
322     $pixbuf = scale_pixbuf $::board_img, $s, $s, $::config->{speed} ? INTERP_NEAREST : INTERP_TILES;
323     }
324    
325     my $linew = int ($s / 25 / $size);
326    
327     # ornamental border... we have time to waste :/
328 pcg 1.2 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $s-1, $linew, 255;
329     pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $s-1, 255;
330     pixbuf_rect $pixbuf,0xffcc7700, $s-$linew-1, 0, $s-1, $s-1, 255;
331     pixbuf_rect $pixbuf,0xffcc7700, 0, $s-$linew-1, $s-1, $s-1, 255;
332 pcg 1.1
333     for my $i (1 .. $size) {
334 pcg 1.2 pixbuf_rect $pixbuf, 0x44111100, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew, 192;
335     pixbuf_rect $pixbuf, 0x44111100, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew, 192;
336 pcg 1.1
337     # 38 max, but we allow a bit more
338     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
339     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];
340    
341     pixbuf_text $pixbuf, 0, $k[$i], $border, $ofs, $label;
342     pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border, $ofs, $label;
343     pixbuf_text $pixbuf, 0, $border, $k[$i], $ofs, $size - $i + 1;
344     pixbuf_text $pixbuf, 0, $s2 + $border, $k[$i], $ofs, $size - $i + 1;
345    
346     $a++;
347     $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK...
348     }
349    
350     unless ($::config->{conserve_memory}) {
351     $self->{background} = $pixbuf;
352     $pixbuf = $pixbuf->copy;
353     }
354     }
355    
356     $self->{pixbuf} = $pixbuf;
357    
358     # hoshi-points(!)#d#
359     # caching of empty board gfx(!)#d#
360    
361     for my $x (1 .. $size) {
362     for my $y (1 .. $size) {
363     my $rand = ($x ^ $y ^ 0x5555);
364    
365     my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
366    
367     if ($::config->{randomize}) {
368     $dx += ($rand % 7) - 3;
369     $dy += ($rand / 3 % 7) - 3;
370     }
371    
372 pcg 1.10 my $shadow = $edge * 0.05;
373 pcg 1.1 my $area = [$dx, $dy, $edge + $shadow, $edge + $shadow];
374    
375     my $mark = $self->{board}{board}[$x-1][$y-1];
376     my $old = $oldboard ? $oldboard->{board}[$x-1][$y-1] : 0;
377    
378     if ($oldboard) {
379     next if $old == $mark; # no change
380    
381     $self->{background}->copy_area (@$area, $pixbuf, $dx, $dy);
382     $expose_area = $expose_area
383 pcg 1.2 ? $expose_area->union (new Gtk2::Gdk::Rectangle @$area)
384     : new Gtk2::Gdk::Rectangle @$area;
385 pcg 1.1 }
386    
387     if ($mark) {
388     my $pb = $self->create_stack($mark, $edge, $rand);
389    
390     $pb->composite ($pixbuf, @$area,
391     $dx, $dy, 1, 1, $::config->{speed} ? INTERP_NEAREST : INTERP_NEAREST, 255);
392    
393     # labels are handled here because they are quite rare
394     if ($mark & MARK_LABEL) {
395     my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 1;
396    
397     if ($white) {
398     pixbuf_text $pixbuf, 0,
399     $k[$x] + $ofs * 0.1, $k[$y] + $ofs * 0.1, $ofs * 0.7,
400     $self->{board}{label}[$x-1][$y-1];
401     }
402     pixbuf_text $pixbuf, $white,
403     $k[$x], $k[$y], $ofs * 0.7,
404     $self->{board}{label}[$x-1][$y-1];
405     }
406    
407     # old pixmap&mask-way. that was fast ;(
408     #my ($pm, $bm) = $self->create_stack($gc, $mark, $edge, $x * 17 + $y * 11 );
409    
410     #$gc->set_clip_mask ($bm);
411     #$gc->set_clip_origin ($dx, $dy);
412     #$pixmap->draw_pixmap ($gc, $pm, 0, 0, $dx, $dy, $edge, $edge);
413     }
414     }
415     }
416    
417     $self->{board_shown} = Storable::dclone $self->{board};
418     #d# save
419     #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable";
420    
421     $expose_area;
422     }
423    
424 pcg 1.2 sub redraw {
425 pcg 1.1 my ($self, $area) = @_;
426    
427     if ($area && $self->{pixbuf}) {
428 pcg 1.2 my ($x, $y, $w, $h) = $area->values;
429 pcg 1.1 my ($ox, $oy, $s) = @{$self->{offsets}};
430    
431 pcg 1.2 $self->{canvas}->window->draw_pixbuf ($self->{canvas}->style->white_gc, $self->{pixbuf},
432     $x, $y, $x + $ox, $y + $oy, $w, $h,
433     "normal", 0, 0);
434 pcg 1.1 $self->{canvas}->window->draw_rectangle ($self->{canvas}->style->black_gc, 0,
435     $x + $ox - 1, $y + $oy - 1, $w + 2, $h + 2) if $::DEBUG_EXPOSE;
436     }
437     }
438    
439     sub event_update_tree {
440     my ($self) = @_;
441    
442     $self->{path} = $self->get_path;
443 pcg 1.6 $self->{moveadj}->value_changed if $self->{moveadj};
444 pcg 1.1 }
445    
446     sub event_part {
447     my ($self) = @_;
448     $self->SUPER::event_part;
449 pcg 1.4 delete $appwin::gamelist->{game}{$self->{channel}};
450     $self->destroy;
451 pcg 1.1 }
452    
453     sub event_move {
454     my ($self, $pass) = @_;
455     sound::play 1, $pass ? "pass" : "move";
456     }
457    
458     1;
459