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