ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
Revision: 1.6
Committed: Sat May 31 14:55:55 2003 UTC (20 years, 11 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +21 -24 lines
Log Message:
*** empty log message ***

File Contents

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