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