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