1 |
package Gtk2::GoBoard; |
2 |
|
3 |
# my try at a real widget, needs the realobjects branch from Glib-CVS |
4 |
|
5 |
use Glib; |
6 |
|
7 |
use KGS::Constants; |
8 |
use KGS::Game::Board; |
9 |
|
10 |
use POSIX qw(ceil); |
11 |
|
12 |
register Glib::Type Gtk2::DrawingArea, __PACKAGE__, |
13 |
signals => {}, |
14 |
properties => {}; |
15 |
|
16 |
sub new { |
17 |
my ($self, %arg) = @_; |
18 |
$self = Glib::Object::new $self; |
19 |
while (my ($k, $v) = each %arg) { |
20 |
$self->{$k} = $v |
21 |
} |
22 |
$self; |
23 |
} |
24 |
|
25 |
sub INIT_INSTANCE { |
26 |
my $self = shift; |
27 |
|
28 |
$self->double_buffered (0); |
29 |
|
30 |
$self->signal_connect(configure_event => \&configure_event); |
31 |
} |
32 |
|
33 |
sub size_allocate { |
34 |
my ($self, $alloc) = @_; |
35 |
|
36 |
my ($w, $h) = ($alloc->width, $alloc->height); |
37 |
|
38 |
my $s = $self->{width} = $w; |
39 |
warn "ALLOC($w,$h,$s)\n";#d# |
40 |
|
41 |
1; |
42 |
} |
43 |
|
44 |
sub configure_event { |
45 |
my ($self, $event) = @_; |
46 |
|
47 |
delete $self->{stack}; |
48 |
|
49 |
my $s = $self->{width} = $self->allocation->width; |
50 |
$self->draw_board ($s); |
51 |
|
52 |
$self->repaint_board (delete $self->{board}, 0); |
53 |
$self->window->clear_area (0, 0, $self->{width}, $self->{width}); |
54 |
|
55 |
1; |
56 |
} |
57 |
|
58 |
sub new_pixbuf { |
59 |
my ($w, $h, $alpha, $fill) = @_; |
60 |
|
61 |
my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h; |
62 |
$pixbuf->fill ($fill) if defined $fill; |
63 |
|
64 |
$pixbuf; |
65 |
} |
66 |
|
67 |
sub scale_pixbuf { |
68 |
my ($src, $w, $h, $mode, $alpha) = @_; |
69 |
|
70 |
my $dst = new_pixbuf $w, $h, $alpha; |
71 |
|
72 |
$src->scale( |
73 |
$dst, 0, 0, $w, $h, 0, 0, |
74 |
$w / $src->get_width, $h / $src->get_height, |
75 |
$mode, |
76 |
); |
77 |
|
78 |
$dst; |
79 |
} |
80 |
|
81 |
# create a stack of stones |
82 |
sub create_stack { |
83 |
my ($self, $mark, $size, $rand) = @_; |
84 |
|
85 |
my $shadow = $size * 0.05; |
86 |
|
87 |
my $c = \$self->{stack}{$mark}; |
88 |
unless ($$c) { |
89 |
for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) { |
90 |
my $base = new_pixbuf $size + $shadow, $size + $shadow, 1, 0x00000000; |
91 |
|
92 |
# zeroeth the shadow |
93 |
if ($mark & (MARK_B | MARK_W)) { |
94 |
# the -0.5's are a mystery to me |
95 |
$::shadow_img->composite ( |
96 |
$base, $shadow, $shadow, $size, $size, $shadow - 0.5, $shadow - 0.5, |
97 |
$size / $::shadow_img->get_width, $size / $::shadow_img->get_height, |
98 |
$::config->{speed} ? 'tiles' : 'bilinear', 192 |
99 |
); |
100 |
} |
101 |
|
102 |
# first the big stones (handicap stones could be different) |
103 |
for ([MARK_B, $mark & MARK_MOVE ? 255 : 255], |
104 |
[MARK_W, $mark & MARK_MOVE ? 255 : 255], |
105 |
[MARK_GRAY_B, 128], |
106 |
[MARK_GRAY_W, 128]) { |
107 |
my ($mask, $alpha) = @$_; |
108 |
if ($mark & $mask) { |
109 |
$stone->composite ( |
110 |
$base, 0, 0, $size, $size, 0, 0, |
111 |
$size / $stone->get_width, $size / $stone->get_height, |
112 |
$::config->{speed} ? 'tiles' : 'bilinear', $alpha |
113 |
); |
114 |
} |
115 |
} |
116 |
|
117 |
# then the small stones |
118 |
for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]], |
119 |
[MARK_SMALL_W, $::white_img[$rand % @::white_img]]) { |
120 |
my ($mask, $img) = @$_; |
121 |
if ($mark & $mask) { |
122 |
$img->composite ( |
123 |
$base, (int ($size / 4)) x2, (ceil ($size / 2 + 1)) x2, ($size / 4) x2, |
124 |
$size / $img->get_width / 2, $size / $img->get_height / 2, |
125 |
$::config->{speed} ? 'tiles' : 'bilinear', 224 |
126 |
); |
127 |
} |
128 |
} |
129 |
|
130 |
# and lastly any markers |
131 |
my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B)); |
132 |
|
133 |
for ([MARK_CIRCLE, $::circle_img[$dark_bg]], |
134 |
[MARK_TRIANGLE, $::triangle_img[$dark_bg]], |
135 |
[MARK_SQUARE, $::square_img[$dark_bg]]) { |
136 |
my ($mask, $img) = @$_; |
137 |
if ($mark & $mask) { |
138 |
$img->composite ( |
139 |
$base, 0, 0, $size, $size, 0, 0, |
140 |
$size / $img->get_width, $size / $img->get_height, |
141 |
$::config->{speed} ? 'tiles' : 'bilinear', 192 |
142 |
); |
143 |
} |
144 |
} |
145 |
|
146 |
push @$$c, $base; |
147 |
} |
148 |
} |
149 |
|
150 |
$$c->[$rand % @$$c]; |
151 |
} |
152 |
|
153 |
sub pixbuf_rect { |
154 |
my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_; |
155 |
# we fake lines by... a horrible method :/ |
156 |
my $colour_pb = new_pixbuf 1, 1, 0, $colour; |
157 |
$colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1, |
158 |
'nearest', $alpha); |
159 |
} |
160 |
|
161 |
sub set_board { |
162 |
my ($self, $board) = @_; |
163 |
|
164 |
$self->repaint_board ($board, 1); |
165 |
} |
166 |
|
167 |
sub center_text { |
168 |
my ($self, $drawable, $colour, $x, $y, $size, $text) = @_; |
169 |
|
170 |
my $context = $self->get_pango_context; |
171 |
my $font = $context->get_font_description; |
172 |
$font->set_size ($size * Gtk2::Pango->scale); |
173 |
|
174 |
my $layout = new Gtk2::Pango::Layout $context; |
175 |
$layout->set_text ($text); |
176 |
my ($w, $h) = $layout->get_pixel_size; |
177 |
|
178 |
#d# does not work |
179 |
my $gc = $self->style->black_gc; |
180 |
$gc->set_foreground (new Gtk2::Gdk::Color +($colour > 24) & 255, +($colour > 16) & 255, +($colour > 8) & 255); |
181 |
|
182 |
$drawable->draw_layout ($gc, |
183 |
$x - $w*0.5, $y - $h*0.5, $layout); |
184 |
} |
185 |
|
186 |
# draw an empty board and attach the bg pixmap |
187 |
sub draw_board { |
188 |
my ($self, $s) = @_; |
189 |
my $canvas = $self->{canvas}; |
190 |
|
191 |
my $size = $self->{size}; |
192 |
|
193 |
my $pixmap = new Gtk2::Gdk::Pixmap $self->window, $self->{width}, $self->{width}, -1; |
194 |
|
195 |
# we leave enough space for the shadows.. I like smaller stones, and we |
196 |
# do no need to do the nifty recursive screen updates that goban2 does |
197 |
my $border = int ($s / ($size + 3) * 0.5); |
198 |
my $s2 = $s - $border * 2; |
199 |
my $edge = $self->{edge} = int ($s2 / ($size + 1) * 0.975); |
200 |
my $ofs = int ($edge / 2); |
201 |
|
202 |
my @k = map int ($s2 * $_ / ($size+1) + $border + 0.5), 0 .. $size; |
203 |
|
204 |
$self->{k} = \@k; |
205 |
|
206 |
my $pixbuf; |
207 |
|
208 |
my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); |
209 |
|
210 |
if ($s < $bw && $s < $bh) { |
211 |
$pixbuf = new_pixbuf $s, $s, 0; |
212 |
$::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0); |
213 |
} else { |
214 |
$pixbuf = scale_pixbuf $::board_img, $s, $s, $::config->{speed} ? 'nearest' : 'bilinear', 0; |
215 |
} |
216 |
|
217 |
my $linew = int ($s / 40 / $size); |
218 |
|
219 |
# ornamental border... we have time to waste :/ |
220 |
pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $s-1, $linew, 255; |
221 |
pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $s-1, 255; |
222 |
pixbuf_rect $pixbuf, 0xffcc7700, $s-$linew-1, 0, $s-1, $s-1, 255; |
223 |
pixbuf_rect $pixbuf, 0xffcc7700, 0, $s-$linew-1, $s-1, $s-1, 255; |
224 |
|
225 |
for my $i (1 .. $size) { |
226 |
pixbuf_rect $pixbuf, 0x44111100, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew, 192; |
227 |
pixbuf_rect $pixbuf, 0x44111100, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew, 192; |
228 |
} |
229 |
|
230 |
# hoshi points |
231 |
my $hoshi = sub { |
232 |
my ($x, $y) = @_; |
233 |
my $hs = int ($edge / 4) | 1; |
234 |
$x = $k[$x] - $hs / 2; $y = $k[$y] - $hs / 2; |
235 |
|
236 |
# we use the shadow mask... not perfect, but I want to finish this |
237 |
$::shadow_img->composite ($pixbuf, |
238 |
$x, $y, $hs + 1, $hs + 1, $x, $y, |
239 |
$hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height, |
240 |
$::config->{speed} ? 'tiles' : 'bilinear', 255); |
241 |
}; |
242 |
|
243 |
my $h1 = $size < 10 ? 3 : 4; # corner / edge offset |
244 |
$hoshi->($h1, $h1); |
245 |
$hoshi->($size - $h1 + 1, $h1); |
246 |
$hoshi->($h1, $size - $h1 + 1); |
247 |
$hoshi->($size - $h1 + 1, $size - $h1 + 1); |
248 |
|
249 |
if ($size % 2) { # on odd boards, also the remaining 5 |
250 |
my $h2 = ($size + 1) / 2; |
251 |
if ($size > 10) { |
252 |
$hoshi->($h1, $h2); |
253 |
$hoshi->($size - $h1 + 1, $h2); |
254 |
$hoshi->($h2, $size - $h1 + 1); |
255 |
$hoshi->($h2, $h1); |
256 |
} |
257 |
# the tengen |
258 |
$hoshi->($h2, $h2); |
259 |
} |
260 |
|
261 |
# now we have a board sans text |
262 |
$pixmap->draw_pixbuf ($self->style->white_gc, |
263 |
$pixbuf, |
264 |
0, 0, 0, 0, $self->{width}, $self->{width}, |
265 |
"normal", 0, 0); |
266 |
|
267 |
# now draw the labels |
268 |
for my $i (1 .. $size) { |
269 |
# 38 max, but we allow a bit more |
270 |
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 |
271 |
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]; |
272 |
|
273 |
$self->center_text ($pixmap, 0, $k[$i], $border, $ofs, $label); |
274 |
$self->center_text ($pixmap, 0, $k[$i], $s2 + $border, $ofs, $label); |
275 |
$self->center_text ($pixmap, 0, $border, $k[$i], $ofs, $size - $i + 1); |
276 |
$self->center_text ($pixmap, 0, $s2 + $border, $k[$i], $ofs, $size - $i + 1); |
277 |
|
278 |
$a++; |
279 |
$a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK... |
280 |
} |
281 |
|
282 |
$self->window->set_back_pixmap ($pixmap, 0); |
283 |
|
284 |
$self->{backgroundpm} = $pixmap; |
285 |
$self->{backgroundpb} = $pixbuf; |
286 |
} |
287 |
|
288 |
sub repaint_board { |
289 |
my ($self, $board, $dopaint) = @_; |
290 |
|
291 |
return unless $self->{backgroundpb}; |
292 |
|
293 |
my @areas; |
294 |
|
295 |
my $old = $self->{board}; |
296 |
my $size = $self->{size}; |
297 |
my $edge = $self->{edge}; |
298 |
my $ofs = int ($edge * 0.5); |
299 |
my $k = $self->{k}; |
300 |
|
301 |
# repaint |
302 |
for my $x (1 .. $size) { |
303 |
for my $y (1 .. $size) { |
304 |
my $mark = $board->{board}[$x-1][$y-1]; |
305 |
my $old = $old->{board}[$x-1][$y-1]; |
306 |
|
307 |
if ($old != $mark) { |
308 |
my $rand = ($x ^ $y ^ 0x5555); |
309 |
|
310 |
my $shadow = $edge * 0.05; |
311 |
my @area = ($k->[$x] - $ofs, $k->[$y] - $ofs, |
312 |
$edge + $shadow, $edge + $shadow); |
313 |
|
314 |
my $pb = new_pixbuf @area[2,3]; |
315 |
$self->{backgroundpb}->copy_area (@area, $pb, 0, 0); |
316 |
|
317 |
if ($mark & ~MARK_LABEL) { |
318 |
my $stack = $self->create_stack($mark, $edge, $rand); |
319 |
|
320 |
$stack->composite ($pb, 0, 0, @area[2,3], 0, 0, 1, 1, |
321 |
'nearest', 255); |
322 |
} |
323 |
|
324 |
# speed none, normal, max |
325 |
$self->{backgroundpm}->draw_pixbuf ($self->style->black_gc, $pb, |
326 |
0, 0, @area, 'max', 0, 0); |
327 |
|
328 |
# labels are handled here because they are quite rare |
329 |
if ($mark & MARK_LABEL) { |
330 |
my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 0xffffff00; |
331 |
|
332 |
if ($white) { |
333 |
$self->center_text ($self->{backgroundpm}, 0, |
334 |
$area[0] + $ofs * 1.1, $area[1] + $ofs * 1.1, |
335 |
$ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); |
336 |
} |
337 |
$self->center_text ($self->{backgroundpm}, $white, |
338 |
$area[0] + $ofs, $area[1] + $ofs, |
339 |
$ofs * 0.7, $self->{board}{label}[$x-1][$y-1]); |
340 |
} |
341 |
|
342 |
# a single full clear_area is way faster than many single calls here |
343 |
push @areas, \@area if $dopaint; |
344 |
} |
345 |
} |
346 |
} |
347 |
|
348 |
if (@areas) { |
349 |
# the "cut-off" point is arbitrary |
350 |
if (@areas > 16) { |
351 |
# update a single rectangle only |
352 |
my $rect = new Gtk2::Gdk::Rectangle @{pop @areas}; |
353 |
$rect = $rect->union (new Gtk2::Gdk::Rectangle @$_) for @areas; |
354 |
$self->window->clear_area ($rect->values); |
355 |
} else { |
356 |
# update all the affected rectangles |
357 |
$self->window->clear_area (@$_) for @areas; |
358 |
} |
359 |
} |
360 |
|
361 |
$self->{board} = $board; |
362 |
#d# save |
363 |
#Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable"; |
364 |
} |
365 |
|
366 |
sub FINALIZE { |
367 |
warn "FINALIZE(@_)\n";#d# |
368 |
my ($self) = @_; |
369 |
$self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy |
370 |
for BLACK, WHITE; |
371 |
$self->SUPER::destroy; |
372 |
delete $appwin::gamelist->{game}{$self->{channel}}; |
373 |
} |
374 |
|
375 |
1; |
376 |
|