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