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