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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines