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