ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.230 by root, Wed May 24 21:59:54 2006 UTC vs.
Revision 1.257 by root, Tue May 30 02:22:02 2006 UTC

5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8 8
9use CFClient; 9use CFClient;
10use CFClient::Texture;
10 11
11our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
12 13
14our $LAYOUT;
13our $ROOT; 15our $ROOT;
14our $TOOLTIP; 16our $TOOLTIP;
15our $BUTTON_STATE; 17our $BUTTON_STATE;
16 18
17our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20
21sub get_layout {
22 my $layout;
23
24 for (grep { $_->{name} } values %WIDGET) {
25 my $win = $layout->{$_->{name}} = { };
26
27 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
28 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
29 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
30 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
31 }
32
33 $layout
34}
35
36sub set_layout {
37 my ($layout) = @_;
38 $LAYOUT = $layout;
39}
18 40
19sub check_tooltip { 41sub check_tooltip {
20 if (!$GRAB) { 42 if (!$GRAB) {
21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 43 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
22 if (length $widget->{tooltip}) { 44 if (length $widget->{tooltip}) {
23 45
24 if ($TOOLTIP->{owner} != $widget) { 46 if ($TOOLTIP->{owner} != $widget) {
47 $TOOLTIP->hide;
48
25 $TOOLTIP->{owner} = $widget; 49 $TOOLTIP->{owner} = $widget;
26 50
27 my $tip = $widget->{tooltip}; 51 my $tip = $widget->{tooltip};
28 52
29 $tip = $tip->($widget) if CODE:: eq ref $tip; 53 $tip = $tip->($widget) if CODE:: eq ref $tip;
30 54
31 $TOOLTIP->set_tooltip_from ($widget); 55 $TOOLTIP->set_tooltip_from ($widget);
32 $TOOLTIP->show; 56 $TOOLTIP->show;
33
34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
35
36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
37 if $x + $TOOLTIP->{w} > $::WIDTH;
38
39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
42 } 57 }
43 58
44 return; 59 return;
45 } 60 }
46 } 61 }
50 delete $TOOLTIP->{owner}; 65 delete $TOOLTIP->{owner};
51} 66}
52 67
53# class methods for events 68# class methods for events
54sub feed_sdl_key_down_event { 69sub feed_sdl_key_down_event {
55 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) 70 $FOCUS->emit (key_down => $_[0])
56 if $FOCUS; 71 if $FOCUS;
57} 72}
58 73
59sub feed_sdl_key_up_event { 74sub feed_sdl_key_up_event {
60 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) 75 $FOCUS->emit (key_up => $_[0])
61 if $FOCUS; 76 if $FOCUS;
62} 77}
63 78
64sub feed_sdl_button_down_event { 79sub feed_sdl_button_down_event {
65 my ($ev) = @_; 80 my ($ev) = @_;
74 check_tooltip; 89 check_tooltip;
75 } 90 }
76 91
77 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 92 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
78 93
79 if ($GRAB) { 94 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
80 ($x, $y) = $GRAB->coord2local ($x, $y); 95 if $GRAB;
81 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
82 }
83} 96}
84 97
85sub feed_sdl_button_up_event { 98sub feed_sdl_button_up_event {
86 my ($ev) = @_; 99 my ($ev) = @_;
87 my ($x, $y) = ($ev->{x}, $ev->{y}); 100 my ($x, $y) = ($ev->{x}, $ev->{y});
88 101
89 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 102 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
90 103
91 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 104 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
92 105
93 if ($GRAB) { 106 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y))
94 ($x, $y) = $GRAB->coord2local ($x, $y); 107 if $GRAB;
95 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
96 }
97 108
98 if (!$BUTTON_STATE) { 109 if (!$BUTTON_STATE) {
99 my $grab = $GRAB; undef $GRAB; 110 my $grab = $GRAB; undef $GRAB;
100 $grab->update if $grab; 111 $grab->update if $grab;
101 $GRAB->update if $GRAB; 112 $GRAB->update if $GRAB;
117 $HOVER->update if $HOVER && $HOVER->{can_hover}; 128 $HOVER->update if $HOVER && $HOVER->{can_hover};
118 129
119 check_tooltip; 130 check_tooltip;
120 } 131 }
121 132
122 if ($HOVER) {
123 ($x, $y) = $HOVER->coord2local ($x, $y);
124 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); 133 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
125 } 134 if $HOVER;
126} 135}
127 136
128# convert position array to integers 137# convert position array to integers
129sub harmonize { 138sub harmonize {
130 my ($vals) = @_; 139 my ($vals) = @_;
158sub rescale_widgets { 167sub rescale_widgets {
159 my ($sx, $sy) = @_; 168 my ($sx, $sy) = @_;
160 169
161 for my $widget (values %WIDGET) { 170 for my $widget (values %WIDGET) {
162 if ($widget->{is_toplevel}) { 171 if ($widget->{is_toplevel}) {
172 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
173 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
174
163 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 175 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
164 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 176 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
165 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 177 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
166 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
167 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 178 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
168 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 179 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
169 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 180 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
170 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; 181
182 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
183 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
184
171 } 185 }
172 } 186 }
173 187
174 reconfigure_widgets; 188 reconfigure_widgets;
175} 189}
184 198
185sub new { 199sub new {
186 my $class = shift; 200 my $class = shift;
187 201
188 my $self = bless { 202 my $self = bless {
189 x => 0, 203 x => "center",
190 y => 0, 204 y => "center",
191 z => 0, 205 z => 0,
206 w => undef,
207 h => undef,
192 can_events => 1, 208 can_events => 1,
193 @_ 209 @_
194 }, $class; 210 }, $class;
195 211
212 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
213 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x};
214 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y};
215 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w};
216 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{h} if exists $layout->{h};
217
218 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
219 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
220 }
221
222 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
223
196 for (keys %$self) { 224 for (keys %$self) {
197 if (/^connect_(.*)$/) { 225 if (/^on_(.*)$/) {
198 $self->connect ($1 => delete $self->{$_}); 226 $self->connect ($1 => delete $self->{$_});
199 } 227 }
200 } 228 }
201 229
202 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
203
204 $self 230 $self
205} 231}
206 232
207sub destroy { 233sub destroy {
208 my ($self) = @_; 234 my ($self) = @_;
211 %$self = (); 237 %$self = ();
212} 238}
213 239
214sub show { 240sub show {
215 my ($self) = @_; 241 my ($self) = @_;
242
216 return if $self->{parent}; 243 return if $self->{parent};
217 244
218 $CFClient::UI::ROOT->add ($self); 245 $CFClient::UI::ROOT->add ($self);
219} 246}
220 247
221sub show_centered { 248sub set_visible {
222 my ($self) = @_; 249 my ($self) = @_;
250
223 return if $self->{parent}; 251 return if $self->{visible};
224 252
225 $self->show; 253 $self->{root} = $self->{parent}{root};
254 $self->{visible} = $self->{parent}{visible} + 1;
226 255
227 $CFClient::UI::ROOT->on_post_alloc ( 256 $self->emit (visibility_change => 1);
228 "centered $self" => sub {
229 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
230 },
231 );
232}
233 257
234sub hide { 258 $self->realloc if !exists $self->{req_w};
259
260 $_->set_visible for $self->children;
261}
262
263sub set_invisible {
235 my ($self) = @_; 264 my ($self) = @_;
265
266 return unless $self->{visible};
267
268 $_->set_invisible for $self->children;
269
270 delete $self->{root};
271 delete $self->{visible};
236 272
237 undef $GRAB if $GRAB == $self; 273 undef $GRAB if $GRAB == $self;
238 undef $HOVER if $HOVER == $self; 274 undef $HOVER if $HOVER == $self;
239 275
276 CFClient::UI::check_tooltip
277 if $TOOLTIP->{owner} == $self;
278
279 $self->focus_out;
280
281 $self->emit (visibility_change => 0);
282}
283
284sub set_visibility {
285 my ($self, $visible) = @_;
286
287 return if $self->{visible} == $visible;
288
289 $visible ? $self->hide
290 : $self->show;
291}
292
293sub toggle_visibility {
294 my ($self) = @_;
295
296 $self->{visible}
297 ? $self->hide
298 : $self->show;
299}
300
301sub hide {
302 my ($self) = @_;
303
304 $self->set_invisible;
305
240 $self->{parent}->remove ($self) 306 $self->{parent}->remove ($self)
241 if $self->{parent}; 307 if $self->{parent};
242} 308}
243 309
244sub move { 310sub move_abs {
245 my ($self, $x, $y, $z) = @_; 311 my ($self, $x, $y, $z) = @_;
246 312
247 $self->{x} = int $x; 313 $self->{x} = List::Util::max 0, int $x;
248 $self->{y} = int $y; 314 $self->{y} = List::Util::max 0, int $y;
249 $self->{z} = $z if defined $z; 315 $self->{z} = $z if defined $z;
250 316
251 $self->update; 317 $self->update;
252} 318}
253 319
254sub set_size { 320sub set_size {
255 my ($self, $w, $h) = @_; 321 my ($self, $w, $h) = @_;
256 322
257 $self->{user_w} = $w; 323 $self->{force_w} = $w;
258 $self->{user_h} = $h; 324 $self->{force_h} = $h;
259 325
260 $self->check_size; 326 $self->realloc;
261} 327}
262 328
263sub size_request { 329sub size_request {
264 require Carp; 330 require Carp;
265 Carp::confess "size_request is abstract"; 331 Carp::confess "size_request is abstract";
267 333
268sub configure { 334sub configure {
269 my ($self, $x, $y, $w, $h) = @_; 335 my ($self, $x, $y, $w, $h) = @_;
270 336
271 if ($self->{aspect}) { 337 if ($self->{aspect}) {
338 my ($ow, $oh) = ($w, $h);
339
272 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 340 $w = List::Util::min $w, int $h * $self->{aspect};
273 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 341 $h = List::Util::min $h, int $w / $self->{aspect};
274 342
275 # use alignment to adjust x, y 343 # use alignment to adjust x, y
276 344
277 $x += int +($w - $w2) * 0.5; 345 $x += int 0.5 * ($ow - $w);
278 $y += int +($h - $h2) * 0.5; 346 $y += int 0.5 * ($oh - $h);
279
280 ($w, $h) = ($w2, $h2);
281 } 347 }
282 348
283 if ($self->{x} != $x || $self->{y} != $y) { 349 if ($self->{x} ne $x || $self->{y} ne $y) {
284 $self->{x} = $x; 350 $self->{x} = $x;
285 $self->{y} = $y; 351 $self->{y} = $y;
286 $self->update; 352 $self->update;
287 } 353 }
288 354
289 if ($self->{w} != $w || $self->{h} != $h) { 355 if ($self->{w} != $w || $self->{h} != $h) {
356 return unless $self->{visible};
357
290 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 358 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
291 } 359 }
292} 360}
293 361
294sub size_allocate { 362sub size_allocate {
295 # nothing to be done 363 # nothing to be done
296} 364}
297 365
298sub reconfigure {
299 my ($self) = @_;
300
301 $self->check_size (1);
302 $self->update;
303}
304
305sub children { 366sub children {
306} 367}
307 368
308sub set_max_size { 369sub set_max_size {
309 my ($self, $w, $h) = @_; 370 my ($self, $w, $h) = @_;
312 delete $self->{max_h}; $self->{max_h} = $h if $h; 373 delete $self->{max_h}; $self->{max_h} = $h if $h;
313} 374}
314 375
315sub set_tooltip { 376sub set_tooltip {
316 my ($self, $tooltip) = @_; 377 my ($self, $tooltip) = @_;
378
379 $tooltip =~ s/^\s+//;
380 $tooltip =~ s/\s+$//;
381
382 return if $self->{tooltip} eq $tooltip;
317 383
318 $self->{tooltip} = $tooltip; 384 $self->{tooltip} = $tooltip;
319 385
320 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 386 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
321 delete $CFClient::UI::TOOLTIP->{owner}; 387 delete $CFClient::UI::TOOLTIP->{owner};
343 return if $FOCUS == $self; 409 return if $FOCUS == $self;
344 return unless $self->{can_focus}; 410 return unless $self->{can_focus};
345 411
346 my $focus = $FOCUS; $FOCUS = $self; 412 my $focus = $FOCUS; $FOCUS = $self;
347 413
348 $self->emit (focus_in => $focus); 414 $self->_emit (focus_in => $focus);
349 415
350 $focus->update if $focus; 416 $focus->update if $focus;
351 $FOCUS->update; 417 $FOCUS->update;
352} 418}
353 419
356 422
357 return unless $FOCUS == $self; 423 return unless $FOCUS == $self;
358 424
359 my $focus = $FOCUS; undef $FOCUS; 425 my $focus = $FOCUS; undef $FOCUS;
360 426
361 $self->emit (focus_out => $focus); 427 $self->_emit (focus_out => $focus);
362 428
363 $focus->update if $focus; #? 429 $focus->update if $focus; #?
430
431 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
432 unless $FOCUS;
364} 433}
365 434
366sub mouse_motion { } 435sub mouse_motion { }
367sub button_up { } 436sub button_up { }
368sub key_down { } 437sub key_down { }
369sub key_up { } 438sub key_up { }
370 439
371sub button_down { 440sub button_down {
372 my ($self, $ev, $x, $y) = @_; 441 my ($self, $ev, $x, $y) = @_;
373 442
374 $self->focus_in; 443 $self->focus_in;
377sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 446sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
378sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 447sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
379sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 448sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
380sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 449sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
381sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 450sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
451
452sub find_widget {
453 my ($self, $x, $y) = @_;
454
455 return () unless $self->{can_events};
456
457 return $self
458 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
459 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
460
461 ()
462}
463
464sub set_parent {
465 my ($self, $parent) = @_;
466
467 Scalar::Util::weaken ($self->{parent} = $parent);
468
469 $self->set_visible if $parent->{visible};
470}
471
472sub connect {
473 my ($self, $signal, $cb) = @_;
474
475 push @{ $self->{signal_cb}{$signal} }, $cb;
476}
477
478sub _emit {
479 my ($self, $signal, @args) = @_;
480
481 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
482}
483
484sub emit {
485 my ($self, $signal, @args) = @_;
486
487 $self->_emit ($signal, @args)
488 || $self->$signal (@args);
489}
490
491sub visibility_change {
492 #my ($self, $visible) = @_;
493}
494
495sub realloc {
496 my ($self) = @_;
497
498 if ($self->{visible}) {
499 return if $self->{root}{realloc}{$self};
500
501 $self->{root}{realloc}{$self} = $self;
502 $self->{root}->update;
503 } else {
504 delete $self->{req_w};
505 }
506}
507
508sub update {
509 my ($self) = @_;
510
511 $self->{parent}->update
512 if $self->{parent};
513}
514
515sub reconfigure {
516 my ($self) = @_;
517
518 $self->realloc;
519 $self->update;
520}
382 521
383sub draw { 522sub draw {
384 my ($self) = @_; 523 my ($self) = @_;
385 524
386 return unless $self->{h} && $self->{w}; 525 return unless $self->{h} && $self->{w};
403 glVertex $x , $y + $self->{h}; 542 glVertex $x , $y + $self->{h};
404 glEnd; 543 glEnd;
405 glDisable GL_BLEND; 544 glDisable GL_BLEND;
406 } 545 }
407 546
408 if ($ENV{PCLIENT_DEBUG}) { 547 if ($ENV{CFPLUS_DEBUG}) {
409 glPushMatrix; 548 glPushMatrix;
410 glColor 1, 1, 0, 1; 549 glColor 1, 1, 0, 1;
411 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 550 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
412 glBegin GL_LINE_LOOP; 551 glBegin GL_LINE_LOOP;
413 glVertex 0 , 0; 552 glVertex 0 , 0;
414 glVertex $self->{w}, 0; 553 glVertex $self->{w} - 1, 0;
415 glVertex $self->{w}, $self->{h}; 554 glVertex $self->{w} - 1, $self->{h} - 1;
416 glVertex 0 , $self->{h}; 555 glVertex 0 , $self->{h} - 1;
417 glEnd; 556 glEnd;
418 glPopMatrix; 557 glPopMatrix;
419 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 558 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
420 } 559 }
421} 560}
422 561
423sub _draw { 562sub _draw {
424 my ($self) = @_; 563 my ($self) = @_;
425 564
426 warn "no draw defined for $self\n"; 565 warn "no draw defined for $self\n";
427}
428
429sub find_widget {
430 my ($self, $x, $y) = @_;
431
432 return () unless $self->{can_events};
433
434 return $self
435 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
436 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
437
438 ()
439}
440
441sub set_parent {
442 my ($self, $parent) = @_;
443
444 Scalar::Util::weaken ($self->{parent} = $parent);
445
446 # TODO: req_w _does_change after ->reconfigure
447 $self->check_size
448 unless exists $self->{req_w};
449
450 $self->show;
451}
452
453sub check_size {
454 my ($self, $forced) = @_;
455
456 $self->{force_alloc} = 1 if $forced;
457 $CFClient::UI::ROOT->{check_size}{$self} = $self;
458}
459
460sub update {
461 my ($self) = @_;
462
463 $self->{parent}->update
464 if $self->{parent};
465}
466
467sub connect {
468 my ($self, $signal, $cb) = @_;
469
470 push @{ $self->{signal_cb}{$signal} }, $cb;
471}
472
473sub emit {
474 my ($self, $signal, @args) = @_;
475
476 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
477} 566}
478 567
479sub DESTROY { 568sub DESTROY {
480 my ($self) = @_; 569 my ($self) = @_;
481 570
539 my ($class, %arg) = @_; 628 my ($class, %arg) = @_;
540 $class->SUPER::new (can_events => 0, %arg); 629 $class->SUPER::new (can_events => 0, %arg);
541} 630}
542 631
543sub size_request { 632sub size_request {
544 (0, 0) 633 my ($self) = @_;
634
635 ($self->{w} + 0, $self->{h} + 0)
545} 636}
546 637
547sub draw { } 638sub draw { }
548 639
549############################################################################# 640#############################################################################
578 $self->{children} = [ 669 $self->{children} = [
579 sort { $a->{z} <=> $b->{z} } 670 sort { $a->{z} <=> $b->{z} }
580 @{$self->{children}}, @widgets 671 @{$self->{children}}, @widgets
581 ]; 672 ];
582 673
583 $self->check_size (1); 674 $self->realloc;
584 $self->update;
585} 675}
586 676
587sub children { 677sub children {
588 @{ $_[0]{children} } 678 @{ $_[0]{children} }
589} 679}
594 delete $child->{parent}; 684 delete $child->{parent};
595 $child->hide; 685 $child->hide;
596 686
597 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 687 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
598 688
599 $self->check_size; 689 $self->realloc;
600 $self->update;
601} 690}
602 691
603sub clear { 692sub clear {
604 my ($self) = @_; 693 my ($self) = @_;
605 694
609 for (@$children) { 698 for (@$children) {
610 delete $_->{parent}; 699 delete $_->{parent};
611 $_->hide; 700 $_->hide;
612 } 701 }
613 702
614 $self->check_size; 703 $self->realloc;
615 $self->update;
616} 704}
617 705
618sub find_widget { 706sub find_widget {
619 my ($self, $x, $y) = @_; 707 my ($self, $x, $y) = @_;
620 708
673sub size_request { 761sub size_request {
674 $_[0]{children}[0]->size_request 762 $_[0]{children}[0]->size_request
675} 763}
676 764
677sub size_allocate { 765sub size_allocate {
678 my ($self, $w, $h) = @_; 766 my ($self, $w, $h, $changed) = @_;
679 767
680 $self->{children}[0]->configure (0, 0, $w, $h); 768 $self->{children}[0]->configure (0, 0, $w, $h);
681} 769}
682 770
683############################################################################# 771#############################################################################
700 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 788 $ROOT->on_post_alloc ($self => sub { $self->render_child });
701 $self->SUPER::update; 789 $self->SUPER::update;
702} 790}
703 791
704sub size_allocate { 792sub size_allocate {
705 my ($self, $w, $h) = @_; 793 my ($self, $w, $h, $changed) = @_;
706 794
707 $self->SUPER::size_allocate ($w, $h); 795 $self->SUPER::size_allocate ($w, $h, $changed);
708 $self->update; 796 $self->update
797 if $changed;
709} 798}
710 799
711sub _render { 800sub _render {
712 $_[0]{children}[0]->draw; 801 $_[0]{children}[0]->draw;
713} 802}
744 833
745package CFClient::UI::ViewPort; 834package CFClient::UI::ViewPort;
746 835
747our @ISA = CFClient::UI::Window::; 836our @ISA = CFClient::UI::Window::;
748 837
838sub new {
839 my $class = shift;
840
841 $class->SUPER::new (
842 scroll_x => 0,
843 scroll_y => 1,
844 @_,
845 )
846}
847
749sub size_request { 848sub size_request {
750 my ($self) = @_; 849 my ($self) = @_;
751 850
752 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 851 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
753 852
754 @$self{qw(child_w child_h)} 853 $w = 10 if $self->{scroll_x};
854 $h = 10 if $self->{scroll_y};
855
856 ($w, $h)
755} 857}
756 858
757sub size_allocate { 859sub size_allocate {
758 my ($self, $w, $h) = @_; 860 my ($self, $w, $h, $changed) = @_;
759 861
760 my ($cw, $ch) = @$self{qw(child_w child_h)}; 862 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
761# $w = $self->{w}; 863 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
864
762 $self->child->configure (0, 0, $cw, $ch); 865 $self->child->configure (0, 0, $w, $h);
763 $self->update; 866 $self->update;
764} 867}
765 868
766sub set_offset { 869sub set_offset {
767 my ($self, $x, $y) = @_; 870 my ($self, $x, $y) = @_;
818 my $class = shift; 921 my $class = shift;
819 922
820 my $self; 923 my $self;
821 924
822 my $slider = new CFClient::UI::Slider 925 my $slider = new CFClient::UI::Slider
823 vertical => 1, 926 vertical => 1,
824 range => [0, 0, 1, 0.01], # HACK fix 927 range => [0, 0, 1, 0.01], # HACK fix
825 connect_changed => sub { 928 on_changed => sub {
826 $self->{vp}->set_offset (0, $_[1]); 929 $self->{vp}->set_offset (0, $_[1]);
827 }, 930 },
828 ; 931 ;
829 932
830 $self = $class->SUPER::new ( 933 $self = $class->SUPER::new (
838 $self->add ($self->{slider}); 941 $self->add ($self->{slider});
839 942
840 $self 943 $self
841} 944}
842 945
946sub update {
947 my ($self) = @_;
948
949 $self->SUPER::update;
950
951 # todo: overwrite size_allocate of child
952 my $child = $self->{vp}->child;
953 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
954}
955
843sub size_allocate { 956sub size_allocate {
844 my ($self, $w, $h) = @_; 957 my ($self, $w, $h, $changed) = @_;
845 958
846 $self->SUPER::size_allocate ($w, $h); 959 $self->SUPER::size_allocate ($w, $h, $changed);
847 960
848 my $child = $self->{vp}->child; 961 my $child = $self->{vp}->child;
849 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 962 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
850} 963}
851 964
898 1011
899our @ISA = CFClient::UI::Bin::; 1012our @ISA = CFClient::UI::Bin::;
900 1013
901use CFClient::OpenGL; 1014use CFClient::OpenGL;
902 1015
903my @tex = 1016my $bg =
1017 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1018 mipmap => 1, wrap => 1;
1019
1020my @border =
904 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1021 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
905 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1022 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
906 1023
907sub new { 1024sub new {
908 my $class = shift; 1025 my $class = shift;
909
910 # TODO: user_x, user_y, overwrite moveto?
911 1026
912 my $self = $class->SUPER::new ( 1027 my $self = $class->SUPER::new (
913 bg => [1, 1, 1, 1], 1028 bg => [1, 1, 1, 1],
914 border_bg => [1, 1, 1, 1], 1029 border_bg => [1, 1, 1, 1],
915 border => 0.6, 1030 border => 0.6,
916 is_toplevel => 1,
917 can_events => 1, 1031 can_events => 1,
918 @_ 1032 @_
919 ); 1033 );
920 1034
921 $self->{title} &&= new CFClient::UI::Label 1035 $self->{title} &&= new CFClient::UI::Label
941 $h + $self->border * 2, 1055 $h + $self->border * 2,
942 ) 1056 )
943} 1057}
944 1058
945sub size_allocate { 1059sub size_allocate {
946 my ($self, $w, $h) = @_; 1060 my ($self, $w, $h, $changed) = @_;
1061
1062 return unless $changed;
947 1063
948 $h -= List::Util::max 0, $self->border * 2; 1064 $h -= List::Util::max 0, $self->border * 2;
949 $w -= List::Util::max 0, $self->border * 2; 1065 $w -= List::Util::max 0, $self->border * 2;
950 1066
951 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1067 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
975 my ($ev, $x, $y) = @_; 1091 my ($ev, $x, $y) = @_;
976 1092
977 my $dx = $ev->{x} - $ox; 1093 my $dx = $ev->{x} - $ox;
978 my $dy = $ev->{y} - $oy; 1094 my $dy = $ev->{y} - $oy;
979 1095
980 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1096 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
981 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1097 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1098
1099 $self->realloc;
982 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1100 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
983 $self->check_size;
984 }; 1101 };
985 1102
986 } elsif ($lr ^ $td) { 1103 } elsif ($lr ^ $td) {
987 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1104 my ($ox, $oy) = ($ev->{x}, $ev->{y});
988 my ($bx, $by) = ($self->{x}, $self->{y}); 1105 my ($bx, $by) = ($self->{x}, $self->{y});
990 $self->{motion} = sub { 1107 $self->{motion} = sub {
991 my ($ev, $x, $y) = @_; 1108 my ($ev, $x, $y) = @_;
992 1109
993 ($x, $y) = ($ev->{x}, $ev->{y}); 1110 ($x, $y) = ($ev->{x}, $ev->{y});
994 1111
995 $self->move ($bx + $x - $ox, $by + $y - $oy); 1112 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
996 $self->update;
997 }; 1113 };
998 } 1114 }
999} 1115}
1000 1116
1001sub button_up { 1117sub button_up {
1020 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1136 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1021 1137
1022 my $border = $self->border; 1138 my $border = $self->border;
1023 1139
1024 glColor @{ $self->{border_bg} }; 1140 glColor @{ $self->{border_bg} };
1025 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1141 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1026 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1142 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1027 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1143 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1028 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1144 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1029 1145
1030 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1146 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1031 my $bg = $tex[0]; 1147 glColor @{ $self->{bg} };
1032 1148
1033 # TODO: repeat texture not scale 1149 # TODO: repeat texture not scale
1150 # solve this better(?)
1034 my $rep_x = $cw / $bg->{w}; 1151 $bg->{s} = $cw / $bg->{w};
1035 my $rep_y = $ch / $bg->{h}; 1152 $bg->{t} = $ch / $bg->{h};
1036
1037 glColor @{ $self->{bg} };
1038
1039 $bg->{s} = $rep_x;
1040 $bg->{t} = $rep_y;
1041 $bg->{wrap_mode} = 1;
1042 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1153 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1043 } 1154 }
1044 1155
1045 glDisable GL_TEXTURE_2D; 1156 glDisable GL_TEXTURE_2D;
1046 1157
1062sub new { 1173sub new {
1063 my $class = shift; 1174 my $class = shift;
1064 1175
1065 $class->SUPER::new ( 1176 $class->SUPER::new (
1066 col_expand => [], 1177 col_expand => [],
1067 @_ 1178 @_,
1068 ) 1179 )
1180}
1181
1182sub children {
1183 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1069} 1184}
1070 1185
1071sub add { 1186sub add {
1072 my ($self, $x, $y, $child) = @_; 1187 my ($self, $x, $y, $child) = @_;
1073 1188
1074 $child->set_parent ($self); 1189 $child->set_parent ($self);
1075 $self->{children}[$y][$x] = $child; 1190 $self->{children}[$y][$x] = $child;
1076 1191
1077 $child->check_size; 1192 $self->realloc;
1078} 1193}
1079 1194
1080sub children {
1081 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1082}
1083
1084# TODO: move to container class maybe? send childs a signal on removal? 1195# TODO: move to container class maybe? send children a signal on removal?
1085sub clear { 1196sub clear {
1086 my ($self) = @_; 1197 my ($self) = @_;
1087 1198
1088 my @children = $self->children; 1199 my @children = $self->children;
1089 delete $self->{children}; 1200 delete $self->{children};
1091 for (@children) { 1202 for (@children) {
1092 delete $_->{parent}; 1203 delete $_->{parent};
1093 $_->hide; 1204 $_->hide;
1094 } 1205 }
1095 1206
1096 $self->update; 1207 $self->realloc;
1097} 1208}
1098 1209
1099sub get_wh { 1210sub get_wh {
1100 my ($self) = @_; 1211 my ($self) = @_;
1101 1212
1128 (sum @$hs), 1239 (sum @$hs),
1129 ) 1240 )
1130} 1241}
1131 1242
1132sub size_allocate { 1243sub size_allocate {
1133 my ($self, $w, $h) = @_; 1244 my ($self, $w, $h, $changed) = @_;
1134 1245
1135 my ($ws, $hs) = $self->get_wh; 1246 my ($ws, $hs) = $self->get_wh;
1136 1247
1137 my $req_w = sum @$ws; 1248 my $req_w = (sum @$ws) || 1;
1138 my $req_h = sum @$hs; 1249 my $req_h = (sum @$hs) || 1;
1139 1250
1140 # TODO: nicer code && do row_expand 1251 # TODO: nicer code && do row_expand
1141 my @col_expand = @{$self->{col_expand}}; 1252 my @col_expand = @{$self->{col_expand}};
1142 @col_expand = (1) x @$ws unless @col_expand; 1253 @col_expand = (1) x @$ws unless @col_expand;
1143 my $col_expand = (sum @col_expand) || 1; 1254 my $col_expand = (sum @col_expand) || 1;
1197 } 1308 }
1198} 1309}
1199 1310
1200############################################################################# 1311#############################################################################
1201 1312
1313package CFClient::UI::Box;
1314
1315our @ISA = CFClient::UI::Container::;
1316
1317sub size_request {
1318 my ($self) = @_;
1319
1320 $self->{vertical}
1321 ? (
1322 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1323 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1324 )
1325 : (
1326 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1327 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1328 )
1329}
1330
1331sub size_allocate {
1332 my ($self, $w, $h, $changed) = @_;
1333
1334 my $space = $self->{vertical} ? $h : $w;
1335 my $children = $self->{children};
1336
1337 my @req;
1338
1339 if ($self->{homogeneous}) {
1340 @req = ($space / (@$children || 1)) x @$children;
1341 } else {
1342 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1343 my $req = List::Util::sum @req;
1344
1345 if ($req > $space) {
1346 # ah well, not enough space
1347 $_ *= $space / $req for @req;
1348 } else {
1349 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1350
1351 $space = ($space - $req) / $expand; # remaining space to give away
1352
1353 $req[$_] += $space * $children->[$_]{expand}
1354 for 0 .. $#$children;
1355 }
1356 }
1357
1358 CFClient::UI::harmonize \@req;
1359
1360 my $pos = 0;
1361 for (0 .. $#$children) {
1362 my $alloc = $req[$_];
1363 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1364
1365 $pos += $alloc;
1366 }
1367
1368 1
1369}
1370
1371#############################################################################
1372
1202package CFClient::UI::HBox; 1373package CFClient::UI::HBox;
1203 1374
1204# TODO: wrap into common Box base class
1205
1206our @ISA = CFClient::UI::Container::; 1375our @ISA = CFClient::UI::Box::;
1207 1376
1208sub size_request { 1377sub new {
1209 my ($self) = @_; 1378 my $class = shift;
1210 1379
1211 my @alloc = map [$_->size_request], @{$self->{children}}; 1380 $class->SUPER::new (
1212 1381 vertical => 0,
1213 ( 1382 @_,
1214 (List::Util::sum map $_->[0], @alloc),
1215 (List::Util::max map $_->[1], @alloc),
1216 ) 1383 )
1217} 1384}
1218 1385
1219sub size_allocate {
1220 my ($self, $w, $h) = @_;
1221
1222 ($h, $w) = ($w, $h);
1223
1224 my $children = $self->{children};
1225
1226 my @h = map $_->{req_w}, @$children;
1227
1228 my $req_h = List::Util::sum @h;
1229
1230 if ($req_h > $h) {
1231 # ah well, not enough space
1232 $_ *= $h / $req_h for @h;
1233 } else {
1234 my $exp = List::Util::sum map $_->{expand}, @$children;
1235 $exp ||= 1;
1236
1237 for (0 .. $#$children) {
1238 my $child = $children->[$_];
1239
1240 my $alloc_h = $h[$_];
1241 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1242 $h[$_] = $alloc_h;
1243 }
1244 }
1245
1246 CFClient::UI::harmonize \@h;
1247
1248 my $y = 0;
1249 for (0 .. $#$children) {
1250 my $child = $children->[$_];
1251 my $h = $h[$_];
1252 $child->configure ($y, 0, $h, $w);
1253
1254 $y += $h;
1255 }
1256
1257 1
1258}
1259
1260############################################################################# 1386#############################################################################
1261 1387
1262package CFClient::UI::VBox; 1388package CFClient::UI::VBox;
1263 1389
1264# TODO: wrap into common Box base class
1265
1266our @ISA = CFClient::UI::Container::; 1390our @ISA = CFClient::UI::Box::;
1267 1391
1268sub size_request { 1392sub new {
1269 my ($self) = @_; 1393 my $class = shift;
1270 1394
1271 my @alloc = map [$_->size_request], @{$self->{children}}; 1395 $class->SUPER::new (
1272 1396 vertical => 1,
1273 ( 1397 @_,
1274 (List::Util::max map $_->[0], @alloc),
1275 (List::Util::sum map $_->[1], @alloc),
1276 ) 1398 )
1277}
1278
1279sub size_allocate {
1280 my ($self, $w, $h) = @_;
1281
1282 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1283
1284 my $children = $self->{children};
1285
1286 my @h = map $_->{req_h}, @$children;
1287
1288 my $req_h = List::Util::sum @h;
1289
1290 if ($req_h > $h) {
1291 # ah well, not enough space
1292 $_ *= $h / $req_h for @h;
1293 } else {
1294 my $exp = List::Util::sum map $_->{expand}, @$children;
1295 $exp ||= 1;
1296
1297 for (0 .. $#$children) {
1298 my $child = $children->[$_];
1299
1300 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1301 }
1302 }
1303
1304 CFClient::UI::harmonize \@h;
1305
1306 my $y = 0;
1307 for (0 .. $#$children) {
1308 my $child = $children->[$_];
1309 my $h = $h[$_];
1310 $child->configure (0, $y, $w, $h);
1311
1312 $y += $h;
1313 }
1314
1315 1
1316} 1399}
1317 1400
1318############################################################################# 1401#############################################################################
1319 1402
1320package CFClient::UI::Label; 1403package CFClient::UI::Label;
1383 $self->{text} = "T$text"; 1466 $self->{text} = "T$text";
1384 1467
1385 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1468 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1386 $self->{layout}->set_text ($text); 1469 $self->{layout}->set_text ($text);
1387 1470
1471 $self->realloc;
1388 $self->update; 1472 $self->update;
1389 $self->check_size;
1390} 1473}
1391 1474
1392sub set_markup { 1475sub set_markup {
1393 my ($self, $markup) = @_; 1476 my ($self, $markup) = @_;
1394 1477
1398 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1481 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1399 1482
1400 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1483 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1401 $self->{layout}->set_markup ($markup); 1484 $self->{layout}->set_markup ($markup);
1402 1485
1486 $self->realloc;
1403 $self->update; 1487 $self->update;
1404 $self->check_size;
1405} 1488}
1406 1489
1407sub size_request { 1490sub size_request {
1408 my ($self) = @_; 1491 my ($self) = @_;
1409 1492
1430 $h + $self->{padding} * 2, 1513 $h + $self->{padding} * 2,
1431 ) 1514 )
1432} 1515}
1433 1516
1434sub size_allocate { 1517sub size_allocate {
1435 my ($self, $w, $h) = @_; 1518 my ($self, $w, $h, $changed) = @_;
1436 1519
1437 delete $self->{texture}; 1520 delete $self->{texture}
1521 if $changed;
1438} 1522}
1439 1523
1440sub set_fontsize { 1524sub set_fontsize {
1441 my ($self, $fontsize) = @_; 1525 my ($self, $fontsize) = @_;
1442 1526
1443 $self->{fontsize} = $fontsize; 1527 $self->{fontsize} = $fontsize;
1444 delete $self->{texture}; 1528 delete $self->{texture};
1445 1529
1446 $self->update; 1530 $self->realloc;
1447 $self->check_size;
1448} 1531}
1449 1532
1450sub _draw { 1533sub _draw {
1451 my ($self) = @_; 1534 my ($self) = @_;
1452 1535
1524 $self->{text} = $text; 1607 $self->{text} = $text;
1525 1608
1526 $text =~ s/./*/g if $self->{hidden}; 1609 $text =~ s/./*/g if $self->{hidden};
1527 $self->{layout}->set_text ("$text "); 1610 $self->{layout}->set_text ("$text ");
1528 1611
1529 $self->emit (changed => $self->{text}); 1612 $self->_emit (changed => $self->{text});
1530} 1613}
1531 1614
1532sub set_text { 1615sub set_text {
1533 my ($self, $text) = @_; 1616 my ($self, $text) = @_;
1534 1617
1535 $self->{cursor} = length $text; 1618 $self->{cursor} = length $text;
1536 $self->_set_text ($text); 1619 $self->_set_text ($text);
1537 $self->update; 1620
1538 $self->check_size; 1621 $self->realloc;
1539} 1622}
1540 1623
1541sub get_text { 1624sub get_text {
1542 $_[0]{text} 1625 $_[0]{text}
1543} 1626}
1546 my ($self) = @_; 1629 my ($self) = @_;
1547 1630
1548 my ($w, $h) = $self->SUPER::size_request; 1631 my ($w, $h) = $self->SUPER::size_request;
1549 1632
1550 ($w + 1, $h) # add 1 for cursor 1633 ($w + 1, $h) # add 1 for cursor
1551}
1552
1553sub size_allocate {
1554 my ($self, $w, $h) = @_;
1555
1556 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1557} 1634}
1558 1635
1559sub key_down { 1636sub key_down {
1560 my ($self, $ev) = @_; 1637 my ($self, $ev) = @_;
1561 1638
1576 } elsif ($sym == CFClient::SDLK_HOME) { 1653 } elsif ($sym == CFClient::SDLK_HOME) {
1577 $self->{cursor} = 0; 1654 $self->{cursor} = 0;
1578 } elsif ($sym == CFClient::SDLK_END) { 1655 } elsif ($sym == CFClient::SDLK_END) {
1579 $self->{cursor} = length $text; 1656 $self->{cursor} = length $text;
1580 } elsif ($uni == 27) { 1657 } elsif ($uni == 27) {
1581 $self->emit ('escape'); 1658 $self->_emit ('escape');
1582 } elsif ($uni) { 1659 } elsif ($uni) {
1583 substr $text, $self->{cursor}++, 0, chr $uni; 1660 substr $text, $self->{cursor}++, 0, chr $uni;
1584 } 1661 }
1585 1662
1586 $self->_set_text ($text); 1663 $self->_set_text ($text);
1587 $self->update; 1664
1588 $self->check_size; 1665 $self->realloc;
1589} 1666}
1590 1667
1591sub focus_in { 1668sub focus_in {
1592 my ($self) = @_; 1669 my ($self) = @_;
1593 1670
1673 if ($sym == 13) { 1750 if ($sym == 13) {
1674 unshift @{$self->{history}}, 1751 unshift @{$self->{history}},
1675 my $txt = $self->get_text; 1752 my $txt = $self->get_text;
1676 $self->{history_pointer} = -1; 1753 $self->{history_pointer} = -1;
1677 $self->{history_saveback} = ''; 1754 $self->{history_saveback} = '';
1678 $self->emit (activate => $txt); 1755 $self->_emit (activate => $txt);
1679 $self->update; 1756 $self->update;
1680 1757
1681 } elsif ($sym == CFClient::SDLK_UP) { 1758 } elsif ($sym == CFClient::SDLK_UP) {
1682 if ($self->{history_pointer} < 0) { 1759 if ($self->{history_pointer} < 0) {
1683 $self->{history_saveback} = $self->get_text; 1760 $self->{history_saveback} = $self->get_text;
1731 can_events => 1, 1808 can_events => 1,
1732 @_ 1809 @_
1733 ) 1810 )
1734} 1811}
1735 1812
1813sub activate { }
1814
1736sub button_up { 1815sub button_up {
1737 my ($self, $ev, $x, $y) = @_; 1816 my ($self, $ev, $x, $y) = @_;
1738 1817
1818 $self->emit ("activate")
1739 if ($x >= 0 && $x < $self->{w} 1819 if $x >= 0 && $x < $self->{w}
1740 && $y >= 0 && $y < $self->{h}) { 1820 && $y >= 0 && $y < $self->{h};
1741 $self->emit ("activate");
1742 }
1743} 1821}
1744 1822
1745sub _draw { 1823sub _draw {
1746 my ($self) = @_; 1824 my ($self) = @_;
1747 1825
1799 my ($self, $ev, $x, $y) = @_; 1877 my ($self, $ev, $x, $y) = @_;
1800 1878
1801 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1879 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1802 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1880 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1803 $self->{state} = !$self->{state}; 1881 $self->{state} = !$self->{state};
1804 $self->emit (changed => $self->{state}); 1882 $self->_emit (changed => $self->{state});
1805 } 1883 }
1806} 1884}
1807 1885
1808sub _draw { 1886sub _draw {
1809 my ($self) = @_; 1887 my ($self) = @_;
2085 fg => [1, 1, 1], 2163 fg => [1, 1, 1],
2086 active_fg => [0, 0, 0], 2164 active_fg => [0, 0, 0],
2087 bg => [0, 0, 0, 0.2], 2165 bg => [0, 0, 0, 0.2],
2088 active_bg => [1, 1, 1, 0.5], 2166 active_bg => [1, 1, 1, 0.5],
2089 range => [0, 0, 100, 10, 0], 2167 range => [0, 0, 100, 10, 0],
2090 req_w => $::WIDTH / 80, 2168 min_w => $::WIDTH / 80,
2091 req_h => $::WIDTH / 80, 2169 min_h => $::WIDTH / 80,
2092 vertical => 0, 2170 vertical => 0,
2093 can_hover => 1, 2171 can_hover => 1,
2094 inner_pad => 0.02, 2172 inner_pad => 0.02,
2095 @_ 2173 @_
2096 ); 2174 );
2099 $self->update; 2177 $self->update;
2100 2178
2101 $self 2179 $self
2102} 2180}
2103 2181
2182sub changed { }
2183
2104sub set_range { 2184sub set_range {
2105 my ($self, $range) = @_; 2185 my ($self, $range) = @_;
2106 2186
2107 $self->{range} = $range; 2187 ($range, $self->{range}) = ($self->{range}, $range);
2108 2188
2109 $self->update; 2189 $self->update
2190 if "@$range" ne "@{$self->{range}}";
2110} 2191}
2111 2192
2112sub set_value { 2193sub set_value {
2113 my ($self, $value) = @_; 2194 my ($self, $value) = @_;
2114 2195
2125 if $unit; 2206 if $unit;
2126 2207
2127 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2208 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2128 2209
2129 if ($value != $old_value) { 2210 if ($value != $old_value) {
2130 $self->emit (changed => $value); 2211 $self->_emit (changed => $value);
2131 $self->update; 2212 $self->update;
2132 } 2213 }
2133} 2214}
2134 2215
2135sub size_request { 2216sub size_request {
2136 my ($self) = @_; 2217 my ($self) = @_;
2137 2218
2138 my $w = $self->{req_w}; 2219 ($self->{req_w}, $self->{req_h})
2139 my $h = $self->{req_h};
2140
2141 $self->{vertical} ? ($h, $w) : ($w, $h)
2142} 2220}
2143 2221
2144sub button_down { 2222sub button_down {
2145 my ($self, $ev, $x, $y) = @_; 2223 my ($self, $ev, $x, $y) = @_;
2146 2224
2296 $self->{fontsize} = $fontsize; 2374 $self->{fontsize} = $fontsize;
2297 $self->reflow; 2375 $self->reflow;
2298} 2376}
2299 2377
2300sub size_allocate { 2378sub size_allocate {
2301 my ($self, $w, $h) = @_; 2379 my ($self, $w, $h, $changed) = @_;
2302 2380
2303 $self->SUPER::size_allocate ($w, $h); 2381 $self->SUPER::size_allocate ($w, $h, $changed);
2382
2383 return unless $changed;
2304 2384
2305 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2385 $self->{layout}->set_font ($self->{font}) if $self->{font};
2306 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2386 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2307 $self->{layout}->set_width ($self->{children}[0]{w}); 2387 $self->{layout}->set_width ($self->{children}[0]{w});
2308 2388
2497 2577
2498sub new { 2578sub new {
2499 my $class = shift; 2579 my $class = shift;
2500 2580
2501 my $self = $class->SUPER::new ( 2581 my $self = $class->SUPER::new (
2502 state => 0, 2582 state => 0,
2503 connect_activate => \&toggle_flopper, 2583 on_activate => \&toggle_flopper,
2504 @_ 2584 @_
2505 ); 2585 );
2506 2586
2507 if ($self->{state}) {
2508 $self->{state} = 0;
2509 $self->toggle_flopper;
2510 }
2511
2512 $self 2587 $self
2513} 2588}
2514 2589
2515sub toggle_flopper { 2590sub toggle_flopper {
2516 my ($self) = @_; 2591 my ($self) = @_;
2517 2592
2518 # TODO: use animation 2593 $self->{other}->toggle_visibility;
2519 if ($self->{state} = !$self->{state}) {
2520 $CFClient::UI::ROOT->add ($self->{other});
2521 $self->{other}->move ($self->coord2global (0, $self->{h}));
2522 $self->emit ("open");
2523 } else {
2524 $CFClient::UI::ROOT->remove ($self->{other});
2525 $self->emit ("close");
2526 }
2527
2528 $self->emit (changed => $self->{state});
2529} 2594}
2530 2595
2531############################################################################# 2596#############################################################################
2532 2597
2533package CFClient::UI::Tooltip; 2598package CFClient::UI::Tooltip;
2565 2630
2566 ($w + 4, $h + 4) 2631 ($w + 4, $h + 4)
2567} 2632}
2568 2633
2569sub size_allocate { 2634sub size_allocate {
2570 my ($self, $w, $h) = @_; 2635 my ($self, $w, $h, $changed) = @_;
2571 2636
2637 return unless $changed;
2638
2572 $self->SUPER::size_allocate ($w - 4, $h - 4); 2639 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2640}
2641
2642sub visibility_change {
2643 my ($self, $visible) = @_;
2644
2645 return unless $visible;
2646
2647 $self->{root}->on_post_alloc ("move_$self" => sub {
2648 my $widget = $self->{owner}
2649 or return;
2650
2651 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2652
2653 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2654 if $x + $self->{w} > $::WIDTH;
2655
2656 $self->move_abs ($x, $y);
2657 });
2573} 2658}
2574 2659
2575sub _draw { 2660sub _draw {
2576 my ($self) = @_; 2661 my ($self) = @_;
2577 2662
2594 glVertex $w, $h; 2679 glVertex $w, $h;
2595 glVertex $w, 0; 2680 glVertex $w, 0;
2596 glEnd; 2681 glEnd;
2597 2682
2598 glTranslate 2 - 0.375, 2 - 0.375; 2683 glTranslate 2 - 0.375, 2 - 0.375;
2684
2599 $self->SUPER::_draw; 2685 $self->SUPER::_draw;
2600} 2686}
2601 2687
2602############################################################################# 2688#############################################################################
2603 2689
2609 2695
2610sub new { 2696sub new {
2611 my $class = shift; 2697 my $class = shift;
2612 2698
2613 my $self = $class->SUPER::new ( 2699 my $self = $class->SUPER::new (
2614 aspect => 1, 2700 aspect => 1,
2701 can_events => 0,
2615 @_, 2702 @_,
2616 ); 2703 );
2617 2704
2618 if ($self->{anim} && $self->{animspeed}) { 2705 if ($self->{anim} && $self->{animspeed}) {
2619 Scalar::Util::weaken (my $widget = $self); 2706 Scalar::Util::weaken (my $widget = $self);
2678 $self->SUPER::DESTROY; 2765 $self->SUPER::DESTROY;
2679} 2766}
2680 2767
2681############################################################################# 2768#############################################################################
2682 2769
2683package CFClient::UI::InventoryItem; 2770package CFClient::UI::Inventory;
2684 2771
2685our @ISA = CFClient::UI::HBox::; 2772our @ISA = CFClient::UI::ScrolledWindow::;
2686
2687sub _item_to_desc {
2688 my ($item) = @_;
2689
2690 my $desc =
2691 $item->{nrof} < 2
2692 ? $item->{name}
2693 : "$item->{nrof} × $item->{name_pl}";
2694
2695 $item->{flags} & Crossfire::Protocol::F_OPEN
2696 and $desc .= " (open)";
2697 $item->{flags} & Crossfire::Protocol::F_APPLIED
2698 and $desc .= " (applied)";
2699 $item->{flags} & Crossfire::Protocol::F_UNPAID
2700 and $desc .= " (unpaid)";
2701 $item->{flags} & Crossfire::Protocol::F_MAGIC
2702 and $desc .= " (magic)";
2703 $item->{flags} & Crossfire::Protocol::F_CURSED
2704 and $desc .= " (cursed)";
2705 $item->{flags} & Crossfire::Protocol::F_DAMNED
2706 and $desc .= " (damned)";
2707 $item->{flags} & Crossfire::Protocol::F_LOCKED
2708 and $desc .= " *";
2709
2710 $desc
2711}
2712 2773
2713sub new { 2774sub new {
2714 my $class = shift; 2775 my $class = shift;
2715 2776
2716 my %args = @_;
2717
2718 my $item = delete $args{item};
2719
2720 my $desc = _item_to_desc ($item);
2721
2722 my $self = $class->SUPER::new ( 2777 my $self = $class->SUPER::new (
2723 can_hover => 1,
2724 can_events => 1,
2725 tooltip => ((CFClient::UI::Label::escape $desc)
2726 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2727 connect_button_down => sub {
2728 my ($self, $ev, $x, $y) = @_;
2729
2730 # todo: maybe put examine on 1? but should just be a tooltip :(
2731 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2732 my $targ = $::CONN->{player}{tag};
2733
2734 if ($item->{container} == $::CONN->{player}{tag}) {
2735 $targ = $main::OPENCONT;
2736 }
2737
2738 $::CONN->send ("move $targ $item->{tag} 0");
2739 } elsif ($ev->{button} == 1) {
2740 $::CONN->send ("examine $item->{tag}");
2741 } elsif ($ev->{button} == 2) {
2742 $::CONN->send ("apply $item->{tag}");
2743 } elsif ($ev->{button} == 3) {
2744 my @menu_items = (
2745 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2746 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2747 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2748 (
2749 $item->{flags} & Crossfire::Protocol::F_LOCKED
2750 ? (
2751 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2752 )
2753 : (
2754 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2755 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2756 )
2757 ),
2758 );
2759
2760 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2761 }
2762
2763 1
2764 },
2765 %args
2766 );
2767
2768
2769 $self->add (new CFClient::UI::Face
2770 can_events => 0,
2771 face => $item->{face},
2772 anim => $item->{anim},
2773 animspeed => $item->{animspeed},
2774 );
2775
2776 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2777
2778 $self->{item} = $item;
2779
2780 $self->update_item;
2781
2782 $self
2783}
2784
2785sub update_item {
2786 my ($self) = @_;
2787
2788 my $desc = _item_to_desc ($self->{item});
2789
2790 $self->{name_lbl}->set_text ($desc);
2791}
2792
2793#############################################################################
2794
2795package CFClient::UI::Inventory;
2796
2797our @ISA = CFClient::UI::ScrolledWindow::;
2798
2799sub new {
2800 my $class = shift;
2801
2802 my $self = $class->SUPER::new (
2803 scrolled => (new CFClient::UI::Table), 2778 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2804 @_, 2779 @_,
2805 ); 2780 );
2806 2781
2807 $self 2782 $self
2808} 2783}
2818 or ($a->{name} cmp $b->{name}) 2793 or ($a->{name} cmp $b->{name})
2819 } @$items; 2794 } @$items;
2820 2795
2821 $self->{real_items} = \@items; 2796 $self->{real_items} = \@items;
2822 2797
2798 my $row = 0;
2823 for my $item (@items) { 2799 for my $item (@items) {
2824 $item->{item} = $item; 2800 CFClient::Item::update_widgets $item;
2825 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2826 $item->update_item ();
2827 }
2828 2801
2829 my $i = 0; 2802 $self->{scrolled}->add (0, $row, $item->{face_widget});
2830 for (@items) { 2803 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2831 $self->{scrolled}->add (0, $i, $_); 2804 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2832 my $nrof = $_->{item}->{nrof} || 1;
2833 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2834 }
2835 2805
2836# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2806 $row++;
2837} 2807 }
2838
2839sub size_request {
2840 my ($self) = @_;
2841 ($self->{req_w}, $self->{req_h});
2842} 2808}
2843 2809
2844############################################################################# 2810#############################################################################
2845 2811
2846package CFClient::UI::Menu; 2812package CFClient::UI::Menu;
2881 2847
2882# popup given the event (must be a mouse button down event currently) 2848# popup given the event (must be a mouse button down event currently)
2883sub popup { 2849sub popup {
2884 my ($self, $ev) = @_; 2850 my ($self, $ev) = @_;
2885 2851
2886 $self->emit ("popdown"); 2852 $self->_emit ("popdown");
2887 2853
2888 # maybe save $GRAB? must be careful about events... 2854 # maybe save $GRAB? must be careful about events...
2889 $GRAB = $self; 2855 $GRAB = $self;
2890 $self->{button} = $ev->{button}; 2856 $self->{button} = $ev->{button};
2891 2857
2906 2872
2907 if ($ev->{button} == $self->{button}) { 2873 if ($ev->{button} == $self->{button}) {
2908 undef $GRAB; 2874 undef $GRAB;
2909 $self->hide; 2875 $self->hide;
2910 2876
2911 $self->emit ("popdown"); 2877 $self->_emit ("popdown");
2912 $self->{hover}[1]->() if $self->{hover}; 2878 $self->{hover}[1]->() if $self->{hover};
2913 } 2879 }
2914} 2880}
2915 2881
2916############################################################################# 2882#############################################################################
2978sub add { 2944sub add {
2979 my ($self, $text, %arg) = @_; 2945 my ($self, $text, %arg) = @_;
2980 2946
2981 $text =~ s/^\s+//; 2947 $text =~ s/^\s+//;
2982 $text =~ s/\s+$//; 2948 $text =~ s/\s+$//;
2949
2950 return unless $text;
2983 2951
2984 my $timeout = time + ((delete $arg{timeout}) || 60); 2952 my $timeout = time + ((delete $arg{timeout}) || 60);
2985 2953
2986 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2954 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2987 2955
3030use CFClient::OpenGL; 2998use CFClient::OpenGL;
3031 2999
3032sub new { 3000sub new {
3033 my $class = shift; 3001 my $class = shift;
3034 3002
3035 $class->SUPER::new ( 3003 my $self = $class->SUPER::new (
3004 visible => 1,
3036 @_, 3005 @_,
3037 ) 3006 );
3007
3008 Scalar::Util::weaken ($self->{root} = $self);
3009
3010 $self
3038} 3011}
3039 3012
3040sub configure { 3013sub configure {
3041 my ($self, $x, $y, $w, $h) = @_; 3014 my ($self, $x, $y, $w, $h) = @_;
3042 3015
3043 $self->{w} = $w; 3016 $self->{w} = $w;
3044 $self->{h} = $h; 3017 $self->{h} = $h;
3045} 3018}
3046 3019
3047sub check_size { 3020sub reconfigure {
3048 my ($self) = @_; 3021 my ($self) = @_;
3049 3022
3023 $self->SUPER::reconfigure;
3024
3050 $self->size_allocate ($self->{w}, $self->{h}) 3025 $self->size_allocate ($self->{w}, $self->{h}, 1)
3051 if $self->{w}; 3026 if $self->{w};
3052} 3027}
3053 3028
3054sub size_request { 3029sub size_request {
3055 my ($self) = @_; 3030 my ($self) = @_;
3056 3031
3057 ($self->{w}, $self->{h}) 3032 ($self->{w}, $self->{h})
3058} 3033}
3059 3034
3035sub _to_pixel {
3036 my ($coord, $size, $max) = @_;
3037
3038 $coord =
3039 $coord eq "center" ? ($max - $size) * 0.5
3040 : $coord eq "max" ? $max
3041 : $coord;
3042
3043 $coord = 0 if $coord < 0;
3044 $coord = $max - $size if $coord > $max - $size;
3045
3046 int $coord + 0.5
3047}
3048
3060sub size_allocate { 3049sub size_allocate {
3061 my ($self, $w, $h) = @_; 3050 my ($self, $w, $h, $changed) = @_;
3062 3051
3063 for my $child ($self->children) { 3052 for my $child ($self->children) {
3064 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3053 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3065 3054
3066 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3055 $X = $child->{force_x} if exists $child->{force_x};
3067 if exists $child->{req_x}; 3056 $Y = $child->{force_y} if exists $child->{force_y};
3068 3057
3069 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3058 $X = _to_pixel $X, $W, $self->{w};
3070 if exists $child->{req_y}; 3059 $Y = _to_pixel $Y, $H, $self->{h};
3071
3072 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3073 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3074 3060
3075 $child->configure ($X, $Y, $W, $H); 3061 $child->configure ($X, $Y, $W, $H);
3076 } 3062 }
3077} 3063}
3078 3064
3089} 3075}
3090 3076
3091sub update { 3077sub update {
3092 my ($self) = @_; 3078 my ($self) = @_;
3093 3079
3094 $self->check_size;
3095 $::WANT_REFRESH++; 3080 $::WANT_REFRESH++;
3096} 3081}
3097 3082
3098sub add { 3083sub add {
3099 my ($self, @children) = @_; 3084 my ($self, @children) = @_;
3100 3085
3101 for (my @widgets = @children; my $w = pop @widgets; ) {
3102 push @widgets, $w->children;
3103 $w->{root} = $self;
3104 $w->{visible} = 1;
3105 }
3106
3107 for my $child (@children) {
3108 $child->{is_toplevel} = 1; 3086 $_->{is_toplevel} = 1
3109 3087 for @children;
3110 # integerise window positions
3111 $child->{x} = int $child->{x};
3112 $child->{y} = int $child->{y};
3113 }
3114 3088
3115 $self->SUPER::add (@children); 3089 $self->SUPER::add (@children);
3116} 3090}
3117 3091
3118sub remove { 3092sub remove {
3119 my ($self, @children) = @_; 3093 my ($self, @children) = @_;
3120 3094
3121 $self->SUPER::remove (@children); 3095 $self->SUPER::remove (@children);
3096
3097 delete $self->{is_toplevel}
3098 for @children;
3122 3099
3123 while (@children) { 3100 while (@children) {
3124 my $w = pop @children; 3101 my $w = pop @children;
3125 push @children, $w->children; 3102 push @children, $w->children;
3126 delete $w->{visible}; 3103 $w->set_invisible;
3127 } 3104 }
3128} 3105}
3129 3106
3130sub on_refresh { 3107sub on_refresh {
3131 my ($self, $id, $cb) = @_; 3108 my ($self, $id, $cb) = @_;
3145 while ($self->{refresh_hook}) { 3122 while ($self->{refresh_hook}) {
3146 $_->() 3123 $_->()
3147 for values %{delete $self->{refresh_hook}}; 3124 for values %{delete $self->{refresh_hook}};
3148 } 3125 }
3149 3126
3150 if ($self->{check_size}) { 3127 if ($self->{realloc}) {
3151 my @queue = ([], []); 3128 my @queue;
3152 3129
3153 for (;;) { 3130 while () {
3154 if ($self->{check_size}) { 3131 if ($self->{realloc}) {
3155 # heuristic: check containers last 3132 #TODO use array-of-depth approach
3156 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3133
3157 for values %{delete $self->{check_size}} 3134 @queue = sort { $a->{visible} <=> $b->{visible} }
3135 @queue, values %{delete $self->{realloc}};
3158 } 3136 }
3159 3137
3160 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3138 my $widget = pop @queue || last;
3161 3139
3162 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3140 $widget->{visible} or last; # do not resize invisible widgets
3163 ? @$widget{qw(user_w user_h)} 3141
3164 : $widget->size_request; 3142 my ($w, $h) = $widget->size_request;
3165 3143
3166 if (delete $widget->{force_alloc} 3144 my $min_size = $widget->{is_toplevel} ? 16 : 0;
3167 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3145
3168 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3146 $w = List::Util::max $widget->{min_w} || $widget->{is_toplevel} * 16, $w;
3169 3147 $h = List::Util::max $widget->{min_h} || $widget->{is_toplevel} * 16, $h;
3148
3149 $w = $widget->{force_w} if exists $widget->{force_w};
3150 $h = $widget->{force_h} if exists $widget->{force_h};
3151
3170 $widget->{req_w} = $w; 3152 $widget->{req_w} = $w;
3171 $widget->{req_h} = $h; 3153 $widget->{req_h} = $h;
3172 3154
3173 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3155 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3174 3156
3175 $widget->{parent}->check_size 3157 push @queue, $widget->{parent}
3176 if $widget->{parent}; 3158 if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent};
3177 }
3178 } 3159 }
3179 } 3160 }
3180 3161
3181 while ($self->{size_alloc}) { 3162 while (my $size_alloc = delete $self->{size_alloc}) {
3182 for (values %{delete $self->{size_alloc}}) { 3163 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3183 my ($widget, $w, $h) = @$_; 3164 values %$size_alloc;
3165
3166 while () {
3167 my ($widget, $w, $h) = @{ pop @queue or last };
3168
3169 $w = $widget->{w} || $widget->{req_w} unless defined $w;
3170 $h = $widget->{h} || $widget->{req_h} unless defined $h;
3184 3171
3185 $w = 0 if $w < 0; 3172 $w = 0 if $w < 0;
3186 $h = 0 if $h < 0; 3173 $h = 0 if $h < 0;
3187 3174
3175 $w = int $w + 0.5;
3176 $h = int $h + 0.5;
3177
3178 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3179
3188 $widget->{w} = $w; 3180 $widget->{w} = $w;
3189 $widget->{h} = $h; 3181 $widget->{h} = $h;
3190 $widget->size_allocate ($w, $h); 3182
3191 $widget->emit (size_allocate => $w, $h); 3183 $widget->emit (size_allocate => $w, $h, $changed);
3192 } 3184 }
3193 } 3185 }
3194 3186
3195 while ($self->{post_alloc_hook}) { 3187 while ($self->{post_alloc_hook}) {
3196 $_->() 3188 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines