ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
(Generate patch)

Comparing kgsueme/kgsueme/game.pl (file contents):
Revision 1.15 by pcg, Sun Jun 1 02:30:00 2003 UTC vs.
Revision 1.82 by pcg, Thu May 20 15:05:49 2004 UTC

1use utf8;
2
3use Scalar::Util ();
4
5package game::goclock;
6
7# Lo and Behold! I admit it! The rounding stuff etc.. in goclock
8# is completely borked.
9
10use Time::HiRes ();
11
12use KGS::Constants;
13
14use Glib::Object::Subclass
15 Gtk2::Label;
16
17sub INIT_INSTANCE {
18 my $self = shift;
19
20 $self->{set} = sub { };
21 $self->{format} = sub { "ERROR" };
22}
23
24sub FINALIZE_INSTANCE {
25 my ($self) = @_;
26 print "finalize(clock)\n";#d#
27 $self->stop;
28}
29
30sub configure {
31 my ($self, $timesys, $main, $interval, $count) = @_;
32
33 if ($timesys == TIMESYS_ABSOLUTE) {
34 $self->{set} = sub { $self->{time} = $_[0] };
35 $self->{format} = sub { util::format_time $_[0] };
36
37 } elsif ($timesys == TIMESYS_BYO_YOMI) {
38 my $low = $interval * $count;
39
40 $self->{set} = sub { $self->{time} = $_[0] };
41
42 $self->{format} = sub {
43 if ($_[0] > $low) {
44 util::format_time $_[0] - $low;
45 } else {
46 sprintf "%s (%d)",
47 util::format_time int (($_[0] - 1) % $interval + 1),
48 ($_[0] - 1) / $interval;
49 }
50 };
51
52 } elsif ($timesys == TIMESYS_CANADIAN) {
53 $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] };
54
55 $self->{format} = sub {
56 if (!$self->{moves}) {
57 util::format_time $_[0] - $low;
58 } else {
59 my $time = int (($_[0] - 1) % $interval + 1);
60
61 sprintf "%s/%d =%d",
62 util::format_time $time,
63 $self->{moves},
64 $self->{moves} > 1
65 ? $time / $self->{moves}
66 : $interval;
67 }
68 };
69
70 } else {
71 # none, or unknown
72 $self->{set} = sub { };
73 $self->{format} = sub { "---" }
74 }
75}
76
77sub refresh {
78 my ($self, $timestamp) = @_;
79 my $timer = $self->{time} + $self->{start} - $timestamp;
80
81 # we round the timer value slightly... the protocol isn't exact anyways,
82 # and this gives smoother timers ;)
83 my @format = $self->{format}->(int ($timer + 0.4));
84 $self->set_text ($self->{format}->(int ($timer + 0.4)));
85
86 $timer - int $timer;
87}
88
89sub set_time {
90 my ($self, $time) = @_;
91
92 # we ignore requests to re-set the time of a running clock.
93 # this is the easiest way to ensure that commentary etc.
94 # doesn't re-set the clock. yes, this is frickle design,
95 # but I think the protocol is to blame here, which gives
96 # very little time information. (cgoban2 also has had quite
97 # a lot of small time update problems...)
98 unless ($self->{timeout}) {
99 $self->{set}->($time->[0], $time->[1]);
100 $self->refresh ($self->{start});
101 }
102}
103
104sub start {
105 my ($self, $when) = @_;
106
107 $self->stop;
108
109 $self->{start} = $when;
110
111 my $timeout; $timeout = sub {
112 my $next = $self->refresh (Time::HiRes::time) * 1000;
113 $next += 1000 if $next < 0;
114 $self->{timeout} = add Glib::Timeout $next, $timeout;
115 0;
116 };
117
118 $timeout->();
119}
120
121sub stop {
122 my ($self) = @_;
123
124 remove Glib::Source delete $self->{timeout} if $self->{timeout};
125}
126
127package game::userpanel;
128
129use Glib::Object::Subclass
130 Gtk2::HBox,
131 properties => [
132 Glib::ParamSpec->IV ("colour", "colour", "User Colour", 0, 1, 0, [qw(construct-only writable)]),
133 ];
134
135sub INIT_INSTANCE {
136 my ($self) = @_;
137
138 $self->add (my $vbox = new Gtk2::VBox);
139
140 $vbox->add ($self->{name} = new Gtk2::Label $self->{name});
141 $vbox->add ($self->{info} = new Gtk2::Label "");
142 $vbox->add ($self->{clock} = new game::goclock); Scalar::Util::weaken $self->{clock};
143
144 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
145
146 $self;
147}
148
149sub configure {
150 my ($self, $app, $user, $rules) = @_;
151
152 if ($self->{name}->get_text ne $user->as_string) {
153 $self->{name}->set_text ($user->as_string);
154
155 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
156 $self->{imagebox}->add (gtk::image_from_data undef);
157 $self->{imagebox}->show_all;
158
159 if ($user->has_pic) {
160 # the big picture...
161 $app->userpic ($user->{name}, sub {
162 return unless $self->{imagebox};
163
164 if ($_[0]) {
165 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
166 $self->{imagebox}->add (gtk::image_from_data $_[0]);
167 $self->{imagebox}->show_all;
168 }
169 });
170 }
171 }
172
173 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)});
174}
175
176sub set_state {
177 my ($self, $captures, $timer, $when) = @_;
178
179 $self->{clock}->stop unless $when;
180 $self->{clock}->set_time ($timer);
181 $self->{clock}->start ($when) if $when;
182
183 $self->{info}->set_text ("$captures pris.");
184}
185
1package game; 186package game;
2 187
3use KGS::Constants; 188use KGS::Constants;
4use KGS::Game::Board; 189use KGS::Game::Board;
190
191use Gtk2::GoBoard;
5 192
6use base KGS::Listener::Game; 193use base KGS::Listener::Game;
7use base KGS::Game; 194use base KGS::Game;
8 195
9use base gtk::widget; 196use base gtk::widget;
15 $self = $self->SUPER::new(@_); 202 $self = $self->SUPER::new(@_);
16 203
17 $self->listen($self->{conn}); 204 $self->listen($self->{conn});
18 205
19 $self->{window} = new Gtk2::Window 'toplevel'; 206 $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]; 207 gtk::state $self->{window}, "game::window", undef, window_size => [600, 500];
23 208
24 $self->{window}->signal_connect(delete_event => sub { 209 $self->{window}->signal_connect(delete_event => sub {
25 if ($self->{joined}) {
26 $self->part; 210 $self->part;
27 } else { 211 $self->destroy;
28 $self->event_part;
29 }
30 1; 212 1;
31 }); 213 });
32 214
33 $self->{window}->add($self->{hpane} = new Gtk2::HPaned); 215 $self->{window}->add($self->{hpane} = new Gtk2::HPaned);
34 gtk::state $self->{hpane}, "game::hpane", undef, position => 500; 216 gtk::state $self->{hpane}, "game::hpane", undef, position => 500;
35 217
218 # LEFT PANE
219
220 $self->{hpane}->pack1(($self->{left} = new Gtk2::VBox), 1, 0);
221
222 $self->{boardbox} = new Gtk2::VBox;
223
36 $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1); 224 $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1);
37 225
226 # challenge
227
228 $self->{challenge} = new challenge channel => $self->{channel};
229
230 # board box (aspect/canvas)
231
38 $vbox->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0); 232 $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
39 233
40 { 234 {
41 # grrr...
42 $frame->add(my $vbox = new Gtk2::VBox); 235 $frame->add (my $vbox = new Gtk2::VBox);
43 $vbox->add($self->{title} = new Gtk2::Label $title); 236 $vbox->add ($self->{title} = new Gtk2::Label $title);
44 237
238 $vbox->add (my $hbox = new Gtk2::HBox);
239
240 $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 1, 0);
241
45 $self->{moveadj} = new Gtk2::Adjustment 1, 0, 1, 0.001, 0.05, 0; 242 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0;
46 243
47 $vbox->add(my $scale = new Gtk2::HScale $self->{moveadj}); 244 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0);
48 $scale->set_draw_value (0); 245 $scale->set_draw_value (0);
246 $scale->set_digits (0);
49 247
50 $self->{moveadj}->signal_connect (value_changed => sub { 248 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board });
51 return unless $self->{path};
52
53 my $move = int (@{$self->{path}} * $_[0]->get_value);
54
55 $self->{board} = new KGS::Game::Board $self->{size};
56 $self->{board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
57
58 $self->redraw ($self->repaint_board);
59
60 $self->{text}->set_text(KGS::Listener::Debug::dumpval([$self->{board}{time},$self->{board}{captures}]). $self->{board}{comment});
61 });
62 }
63 249 }
64 $vbox->pack_start(($self->{canvas} = new Gtk2::DrawingArea), 1, 1, 0);
65 250
66 $self->{canvas}->signal_connect(configure_event => \&configure_event, $self); 251 $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
67 $self->{canvas}->signal_connect(expose_event => \&expose_event, $self);
68 252
253 # RIGHT PANE
254
69 $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 0, 0); 255 $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 1, 1);
70 $self->{hpane}->set(position_set => 1); 256 $self->{hpane}->set(position_set => 1);
71 gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80; 257 gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
72 258
73 $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow); 259 $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow);
74 $sw->set_policy("automatic", "always"); 260 $sw->set_policy("automatic", "always");
75 261
76 $sw->add(($self->{userlist} = new gtk::userlist)->widget); 262 $sw->add(($self->{userlist} = new userlist)->widget);
77 263
78 $self->{vpane}->add(my $vbox = new Gtk2::VBox); 264 $self->{vpane}->add(my $vbox = new Gtk2::VBox);
265
266 $vbox->pack_start((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
267
268 for (COLOUR_WHITE, COLOUR_BLACK) {
269 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_);
270 Scalar::Util::weaken $self->{userpanel}[$_];
79 271 }
80 $vbox->pack_start((my $sw = new Gtk2::ScrolledWindow), 1, 1, 0); 272
81 $sw->set_policy("never", "always");
82
83 $sw->add(($self->{text} = new gtk::text)->widget);
84
85 $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0); 273 $vbox->pack_start(($self->{chat} = new chat), 1, 1, 0);
274
86 $self->{entry}->signal_connect(activate => sub { 275 $self->{chat}->signal_connect(command => sub {
87 my $text = $self->{entry}->get_text; 276 my ($chat, $cmd, $arg) = @_;
88 $self->say($text) if $text =~ /\S/; 277 if ($cmd eq "rsave") {
89 $self->{entry}->set_text(""); 278 Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d#
279 } else {
280 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self);
281 }
90 }); 282 });
91 283
284 $self->event_update_game;
92 $self; 285 $self;
93} 286}
94 287
95sub event_update_users { 288sub event_update_users {
96 my ($self) = @_; 289 my ($self, $add, $update, $remove) = @_;
97 290
98 $self->{userlist}->update($self->{users}); 291 return unless $self->{userlist};
292
293 $self->{userlist}->update ($add, $update, $remove);
294
295 my %important;
296 $important{$self->{user1}{name}}++;
297 $important{$self->{user2}{name}}++;
298 $important{$self->{user3}{name}}++;
299
300 if (my @users = grep $important{$_->{name}}, @$add) {
301 $self->{chat}->append_text ("\n<header>Joins:</header>");
302 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
303 }
304 if (my @users = grep $important{$_->{name}}, @$remove) {
305 $self->{chat}->append_text ("\n<header>Parts:</header>");
306 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
307 }
308
99} 309}
100 310
101sub join { 311sub join {
102 my ($self) = @_; 312 my ($self) = @_;
313 return if $self->{joined};
314
103 $self->SUPER::join; 315 $self->SUPER::join;
104 316
105 $self->{window}->show_all; 317 $self->{window}->show_all;
106} 318}
107 319
108sub part { 320sub part {
109 my ($self) = @_; 321 my ($self) = @_;
322
110 $self->SUPER::part; 323 $self->SUPER::part;
111 324 $self->destroy;
112 $self->{window}->hide;
113} 325}
114 326
115sub configure_event { 327sub update_board {
116 my ($widget, $event, $self) = @_; 328 my ($self) = @_;
117 delete $self->{stack}; 329 return unless $self->{path};
118 delete $self->{pixbuf};
119 delete $self->{board_shown};
120 delete $self->{background};
121 $self->repaint_board;
122 0;
123}
124 330
125sub expose_event { 331 my $move = int $self->{moveadj}->get_value;
126 my ($widget, $event, $self) = @_;
127 332
128 $self->{pixbuf} or return; 333 my $running = $move == @{$self->{path}};
129 334
130 my $area = $event->area; 335 $self->{board_label}->set_text ("Move " . ($move - 1));
131 my ($ox, $oy, $s) = @{$self->{offsets}};
132 336
133 $self->redraw ( 337 $self->{cur_board} = new KGS::Game::Board $self->{size};
134 (new Gtk2::Gdk::Rectangle $area->x - $ox, $area->y - $oy, $area->width, $area->height) 338 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
135 ->intersect(new Gtk2::Gdk::Rectangle 0, 0, $s, $s) 339
340 for my $colour (COLOUR_WHITE, COLOUR_BLACK) {
341 $self->{userpanel}[$colour]->set_state (
342 $self->{cur_board}{captures}[$colour],
343 $self->{cur_board}{timer}[$colour],
344 ($running && $self->{lastmove_colour} == !$colour)
345 ? $self->{lastmove_time} : 0
136 ); 346 );
347 }
137 348
138 0; 349 $self->{board}->set_board ($self->{cur_board});
139} 350}
140 351
141# something Gtk2 fixed 352sub event_update_tree {
142sub INTERP_NEAREST (){ 'nearest' } 353 my ($self) = @_;
143sub INTERP_TILES (){ 'tiles' }
144sub INTERP_BILINEAR (){ 'bilinear' }
145sub INTERP_HYPER (){ 'hyper' }
146 354
147sub new_pixbuf { 355 $self->{path} = $self->get_path;
148 my ($w, $h, $alpha, $fill) = @_;
149 356
150 my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h; 357 if ($self->{moveadj}) {
151 $pixbuf->fill ($fill) if defined $fill; 358 my $upper = $self->{moveadj}->upper;
359 my $pos = $self->{moveadj}->get_value;
360 my $move = scalar @{$self->{path}};
152 361
153 $pixbuf; 362 $self->{moveadj}->upper ($move);
154}
155
156sub scale_pixbuf {
157 my ($src, $w, $h, $mode) = @_;
158
159 my $dst = new_pixbuf $w, $h, 1;
160
161 $src->scale(
162 $dst, 0, 0, $w, $h, 0, 0,
163 $w / $src->get_width, $h / $src->get_height,
164 $mode,
165 );
166
167 $dst;
168}
169
170# create a stack of stones
171sub create_stack {
172 my ($self, $mark, $size, $rand) = @_;
173
174 my $shadow = $size * 0.05;
175
176 my $c = \$self->{stack}{$mark};
177 unless ($$c) {
178 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
179 my $base = new_pixbuf $size + $shadow, $size + $shadow, 1, 0x00000000;
180
181 # zeroeth the shadow
182 if ($mark & (MARK_B | MARK_W)) {
183 $::shadow_img->composite (
184 $base, $shadow, $shadow, $size, $size, $shadow - 0.5, $shadow - 0.5,
185 $size / $stone->get_width, $size / $stone->get_height,
186 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
187 );
188 } 363
189 364 $self->{moveadj}->changed;
190 # first the big stones (handicap stones different for effect) 365 if ($pos == $upper) {
191 for ([MARK_B, $mark & MARK_MOVE ? 255 : 192], 366 $self->{moveadj}->value ($move);
192 [MARK_W, $mark & MARK_MOVE ? 255 : 192], 367 $self->{moveadj}->value_changed;
193 [MARK_GRAY_B, 128],
194 [MARK_GRAY_W, 128]) {
195 my ($mask, $alpha) = @$_;
196 if ($mark & $mask) {
197 $stone->composite (
198 $base, 0, 0, $size, $size, 0, 0,
199 $size / $stone->get_width, $size / $stone->get_height,
200 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, $alpha
201 );
202 }
203 }
204
205 # then the small stones
206 for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]],
207 [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) {
208 my ($mask, $img) = @$_;
209 if ($mark & $mask) {
210 $img->composite (
211 $base, (ceil ($size / 4)) x2, (ceil ($size / 2)) x2, (ceil ($size / 4)) x2,
212 $size / $img->get_width / 2, $size / $img->get_height / 2,
213 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 224
214 );
215 }
216 }
217
218 # and lastly any markers
219 my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B));
220
221 for ([MARK_CIRCLE, $::circle_img[$dark_bg]],
222 [MARK_TRIANGLE, $::triangle_img[$dark_bg]],
223 [MARK_SQUARE, $::square_img[$dark_bg]]) {
224 my ($mask, $img) = @$_;
225 if ($mark & $mask) {
226 $img->composite (
227 $base, 0, 0, $size, $size, -0.5, -0.5,
228 $size / $img->get_width, $size / $img->get_height,
229 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255
230 );
231 }
232 }
233
234 push @$$c, $base;
235 } 368 }
236 } 369 }
237
238 $$c->[$rand % @$$c];
239} 370}
240 371
241sub pixbuf_text { 372sub event_update_comments {
242 my ($pixbuf, $colour, $x, $y, $height, $text) = @_; 373 my ($self, $node, $comment, $newnode) = @_;
374 $self->SUPER::event_update_comments($node, $comment, $newnode);
243 375
244 my @c = grep $_, 376 my $text;
245 map $::font[$colour][$::fontmap{$_}],
246 split //, $text;
247 377
248 if (@c) { 378 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>"
249 my $spacing = $height * 0.1; 379 if $newnode;
250 my $s = $height / List::Util::max map $_->get_height, @c;
251 my $W = List::Util::sum map $_->get_width, @c;
252 380
253 $x -= ($W * $s + $spacing * (@c - 1)) * 0.5; 381 for (split /\n/, $comment) {
254 $y -= $height * 0.5; 382 $text .= "\n";
255 383 if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) {
256 for (@c) { 384 $text .= "<user>" . (util::toxml $1) . "</user>:";
257 my $w = $_->get_width * $s;
258 $_->composite ($pixbuf,
259 $x, $y, $w+1, $height+1, $x, $y, $s, $s,
260 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
261
262 $x += $w + $spacing;
263 } 385 }
264 }
265}
266
267sub pixbuf_rect {
268 my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_;
269 # we fake lines by... a horrible method :/
270 my $colour_pb = new_pixbuf 1, 1, 0, $colour;
271 $colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1,
272 INTERP_NEAREST, $alpha);
273}
274
275sub repaint_board {
276 my ($self) = @_;
277 my $canvas = $self->{canvas};
278 my $expose_area = undef;
279
280 return $expose_area unless $self->{board};
281
282 my ($w, $h) = ($canvas->allocation->values)[2,3];
283
284 my $s = $w > $h ? $h : $w;
285
286 return unless $s > 128;
287
288 $self->{offsets} = [int (($w - $s) / 2), int (($h - $s) / 2), $s];
289
290 my $size = $self->{size};
291
292 my $border = int ($s / ($size + 3) * 0.5);
293 my $s2 = $s - $border * 2;
294 my $edge = int ($s2 / ($size + 1) * 0.96) - ($::config->{randomize} ? 3 : 0);
295 my $ofs = int ($edge / 2);
296
297 my @k = map int ($s2 * $_ / ($size+1) + $border + 0.5), 0 .. $size;
298
299 my $pixbuf;
300
301 my $oldboard;
302
303 if ($self->{background}) {
304 if ($oldboard = $self->{board_shown}) {
305 $pixbuf = $self->{pixbuf};
306 } else {
307 $pixbuf = $self->{background}->copy;
308 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
309 } 386
310 } else { 387 # coords only for 19x19 so far
311 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s; 388 $_ = util::toxml $_;
312
313 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
314
315 if ($s < $bw && $s < $bh) {
316 $pixbuf = new_pixbuf $s, $s, 0;
317 $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0);
318 } else {
319 $pixbuf = scale_pixbuf $::board_img, $s, $s, $::config->{speed} ? INTERP_NEAREST : INTERP_TILES;
320 } 389 s{
321
322 my $linew = int ($s / 25 / $size);
323
324 # ornamental border... we have time to waste :/
325 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $s-1, $linew, 255;
326 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $s-1, 255;
327 pixbuf_rect $pixbuf,0xffcc7700, $s-$linew-1, 0, $s-1, $s-1, 255;
328 pixbuf_rect $pixbuf,0xffcc7700, 0, $s-$linew-1, $s-1, $s-1, 255;
329
330 for my $i (1 .. $size) {
331 pixbuf_rect $pixbuf, 0x44111100, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew, 192;
332 pixbuf_rect $pixbuf, 0x44111100, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew, 192;
333
334 # 38 max, but we allow a bit more
335 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
336 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];
337
338 pixbuf_text $pixbuf, 0, $k[$i], $border, $ofs, $label;
339 pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border, $ofs, $label;
340 pixbuf_text $pixbuf, 0, $border, $k[$i], $ofs, $size - $i + 1;
341 pixbuf_text $pixbuf, 0, $s2 + $border, $k[$i], $ofs, $size - $i + 1;
342
343 $a++;
344 $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK...
345 }
346
347 unless ($::config->{conserve_memory}) {
348 $self->{background} = $pixbuf;
349 $pixbuf = $pixbuf->copy;
350 }
351 }
352
353 $self->{pixbuf} = $pixbuf;
354
355 # hoshi-points(!)#d#
356 # caching of empty board gfx(!)#d#
357
358 for my $x (1 .. $size) {
359 for my $y (1 .. $size) {
360 my $rand = ($x ^ $y ^ 0x5555);
361
362 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
363
364 if ($::config->{randomize}) {
365 $dx += ($rand % 7) - 3;
366 $dy += ($rand / 3 % 7) - 3;
367 }
368
369 my $shadow = $edge * 0.05;
370 my $area = [$dx, $dy, $edge + $shadow, $edge + $shadow];
371 390 (
372 my $mark = $self->{board}{board}[$x-1][$y-1];
373 my $old = $oldboard ? $oldboard->{board}[$x-1][$y-1] : 0;
374
375 if ($oldboard) {
376 next if $old == $mark; # no change
377
378 $self->{background}->copy_area (@$area, $pixbuf, $dx, $dy);
379 $expose_area = $expose_area
380 ? $expose_area->union (new Gtk2::Gdk::Rectangle @$area)
381 : new Gtk2::Gdk::Rectangle @$area;
382 }
383
384 if ($mark) {
385 my $pb = $self->create_stack($mark, $edge, $rand);
386
387 $pb->composite ($pixbuf, @$area,
388 $dx, $dy, 1, 1, $::config->{speed} ? INTERP_NEAREST : INTERP_NEAREST, 255);
389
390 # labels are handled here because they are quite rare
391 if ($mark & MARK_LABEL) {
392 my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 1;
393
394 if ($white) {
395 pixbuf_text $pixbuf, 0,
396 $k[$x] + $ofs * 0.1, $k[$y] + $ofs * 0.1, $ofs * 0.7,
397 $self->{board}{label}[$x-1][$y-1];
398 }
399 pixbuf_text $pixbuf, $white,
400 $k[$x], $k[$y], $ofs * 0.7,
401 $self->{board}{label}[$x-1][$y-1];
402 }
403 391 \b
404 # old pixmap&mask-way. that was fast ;( 392 (?:[bw])?
405 #my ($pm, $bm) = $self->create_stack($gc, $mark, $edge, $x * 17 + $y * 11 ); 393 [, ]{0,2}
406 394 [a-hj-t] # valid for upto 19x19
407 #$gc->set_clip_mask ($bm); 395 \s?
408 #$gc->set_clip_origin ($dx, $dy); 396 [1-9]?[0-9]
409 #$pixmap->draw_pixmap ($gc, $pm, 0, 0, $dx, $dy, $edge, $edge); 397 \b
410 } 398 )
411 } 399 }{
412 } 400 "<coord>$1</coord>";
401 }sgexi;
413 402
414 $self->{board_shown} = Storable::dclone $self->{board}; 403 $text .= $_;
415 #d# save 404 }
416 #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable";
417 405
418 $expose_area; 406 $self->{chat}->append_text ($text);
419} 407}
420 408
421sub redraw { 409sub event_join {
422 my ($self, $area) = @_; 410 my ($self) = @_;
423 411 $self->SUPER::event_join;
424 if ($area && $self->{pixbuf}) {
425 my ($x, $y, $w, $h) = $area->values;
426 my ($ox, $oy, $s) = @{$self->{offsets}};
427
428 $self->{canvas}->window->draw_pixbuf ($self->{canvas}->style->white_gc, $self->{pixbuf},
429 $x, $y, $x + $ox, $y + $oy, $w, $h,
430 "normal", 0, 0);
431 $self->{canvas}->window->draw_rectangle ($self->{canvas}->style->black_gc, 0,
432 $x + $ox - 1, $y + $oy - 1, $w + 2, $h + 2) if $::DEBUG_EXPOSE;
433 }
434}
435
436sub event_update_tree {
437 my ($self) = @_;
438
439 $self->{path} = $self->get_path;
440 $self->{moveadj}->value_changed if $self->{moveadj};
441} 412}
442 413
443sub event_part { 414sub event_part {
444 my ($self) = @_; 415 my ($self) = @_;
445 $self->SUPER::event_part; 416 $self->SUPER::event_part;
446 delete $appwin::gamelist->{game}{$self->{channel}};
447 $self->destroy; 417 $self->destroy;
448} 418}
449 419
450sub event_move { 420sub event_move {
451 my ($self, $pass) = @_; 421 my ($self, $pass) = @_;
452 sound::play 1, $pass ? "pass" : "move"; 422 sound::play 1, $pass ? "pass" : "move";
453} 423}
454 424
425sub event_update_game {
426 my ($self) = @_;
427 $self->SUPER::event_update_game;
428
429 return unless $self->{window};
430
431 my $title = defined $self->{channel}
432 ? $self->owner->as_string . " " . $self->opponent_string
433 : "Game Window";
434 $self->{window}->set_title("KGS Game $title");
435 $self->{title}->set_text ($title);
436
437 $self->{user}[COLOUR_BLACK] = $self->{user1};
438 $self->{user}[COLOUR_WHITE] = $self->{user2};
439
440 # show board
441
442 if ($self->is_inprogress) {
443 $self->{left}->remove ($self->{challenge}->widget) if $self->{challenge} && $self->{boardbox}->parent;
444 $self->{left}->add ($self->{boardbox}) unless $self->{boardbox}->parent;
445 } else {
446 $self->{left}->remove ($self->{boardbox}) if $self->{boardbox}->parent;
447 $self->{left}->add ($self->{challenge}->widget) unless $self->{challenge}->widget->parent;
448 }
449 $self->{left}->show_all;
450
451 # view text
452
453 eval { #d#
454 my @ga;
455 $ga[0] = "\nType: " . (util::toxml $gametype{$self->type})
456 . " (" . (util::toxml $gameopt{$self->option}) . ")";
457 $ga[1] = "\nFlags:";
458 $ga[1] .= " started" if $self->is_inprogress;
459 $ga[1] .= " adjourned" if $self->is_adjourned;
460 $ga[1] .= " scored" if $self->is_scored;
461 $ga[1] .= " saved" if $self->is_saved;
462
463 $ga[2] = "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>"
464 if $self->{user3}->is_inprogress;
465
466 $ga[3] = "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>"
467 . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>"
468 if $self->is_inprogress;
469
470 if ($self->is_inprogress) {
471 $ga[4] = "\nHandicap: " . $self->{handicap};
472 $ga[5] = "\nKomi: " . $self->{komi};
473 $ga[6] = "\nSize: " . $self->size_string;
474 }
475
476 if ($self->is_scored) {
477 $ga[7] = "\nResult: " . $self->score_string;
478 }
479
480 $text = "\n<infoblock><header>Game Update</header>";
481 for (0..7) {
482 if ($self->{gatext}[$_] ne $ga[$_]) {
483 $text .= $ga[$_];
484 }
485 }
486 $text .= "</infoblock>";
487
488 $self->{gatext} = \@ga;
489 };
490
491 $self->{chat}->append_text ($text);
492}
493
494sub event_update_rules {
495 my ($self, $rules) = @_;
496
497 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
498 for COLOUR_BLACK, COLOUR_WHITE;
499
500 sound::play 3, "gamestart";
501
502 my $text = "\n<header>Game Rules</header>";
503
504 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
505
506 $text .= "\nTime: ";
507
508 if ($rules->{timesys} == TIMESYS_NONE) {
509 $text .= "UNLIMITED";
510 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
511 $text .= util::format_time $rules->{time};
512 $text .= " ABS";
513 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
514 $text .= util::format_time $rules->{time};
515 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
516 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
517 $text .= util::format_time $rules->{time};
518 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
519 }
520
521 $self->{chat}->append_text ("<infoblock>$text</infoblock>");
522}
523
524sub inject_resign_game {
525 my ($self, $msg) = @_;
526
527 sound::play 3, "resign";
528
529 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
530 . "\n<user>"
531 . (util::toxml $self->{user}[$msg->{player}]->as_string)
532 . "</user> resigned.</infoblock>");
533}
534
535sub inject_final_result {
536 my ($self, $msg) = @_;
537
538 $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>"
539 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
540 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
541 . "</infoblock>"
542 );
543}
544
545sub event_challenge {
546 my ($self, $challenge) = @_;
547
548 use KGS::Listener::Debug;
549 $self->{chat}->append_text ("\n".KGS::Listener::Debug::dumpval($challenge));
550}
551
552sub destroy {
553 my ($self) = @_;
554
555 delete $self->{app}{gamelist}{game}{$self->{channel}};
556 $self->SUPER::destroy;
557}
558
4551; 5591;
456 560

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines