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