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.245 by elmex, Sat May 27 21:15:57 2006 UTC vs.
Revision 1.258 by root, Tue May 30 02:55:45 2006 UTC

17our $BUTTON_STATE; 17our $BUTTON_STATE;
18 18
19our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20 20
21sub get_layout { 21sub get_layout {
22 my $layout;
23
22 for (grep { $_->{name} } values %WIDGET) { 24 for (grep { $_->{name} } values %WIDGET) {
23 $LAYOUT->{$_->{name}} = { 25 my $win = $layout->{$_->{name}} = { };
24 x => $_->{x} / $::WIDTH,
25 y => $_->{y} / $::HEIGHT,
26 w => $_->{w} / $::WIDTH,
27 h => $_->{h} / $::HEIGHT
28 }; 26
29 } 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};
30 31
31 return $LAYOUT; 32 $win->{show} = $_->{visible} && $_->{is_toplevel};
33 }
34
35 $layout
32} 36}
33 37
34sub set_layout { 38sub set_layout {
35 my ($layout) = @_; 39 my ($layout) = @_;
40
36 $LAYOUT = $layout; 41 $LAYOUT = $layout;
37} 42}
38 43
39sub check_tooltip { 44sub check_tooltip {
40 if (!$GRAB) { 45 if (!$GRAB) {
41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 46 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
42 if (length $widget->{tooltip}) { 47 if (length $widget->{tooltip}) {
43 48
44 if ($TOOLTIP->{owner} != $widget) { 49 if ($TOOLTIP->{owner} != $widget) {
50 $TOOLTIP->hide;
51
45 $TOOLTIP->{owner} = $widget; 52 $TOOLTIP->{owner} = $widget;
46 53
47 my $tip = $widget->{tooltip}; 54 my $tip = $widget->{tooltip};
48 55
49 $tip = $tip->($widget) if CODE:: eq ref $tip; 56 $tip = $tip->($widget) if CODE:: eq ref $tip;
50 57
51 $TOOLTIP->set_tooltip_from ($widget); 58 $TOOLTIP->set_tooltip_from ($widget);
52 $TOOLTIP->show; 59 $TOOLTIP->show;
53
54 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
55
56 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
57 if $x + $TOOLTIP->{w} > $::WIDTH;
58
59 $TOOLTIP->move ($x, $y);
60 $TOOLTIP->check_size;
61 $TOOLTIP->update;
62 } 60 }
63 61
64 return; 62 return;
65 } 63 }
66 } 64 }
172sub rescale_widgets { 170sub rescale_widgets {
173 my ($sx, $sy) = @_; 171 my ($sx, $sy) = @_;
174 172
175 for my $widget (values %WIDGET) { 173 for my $widget (values %WIDGET) {
176 if ($widget->{is_toplevel}) { 174 if ($widget->{is_toplevel}) {
175 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
176 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
177
177 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 178 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
178 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 179 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
179 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 180 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
180 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
181 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 181 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
182 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 182 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
183 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 183 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
184 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; 184
185 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
186 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
187
185 } 188 }
186 } 189 }
187 190
188 reconfigure_widgets; 191 reconfigure_widgets;
189} 192}
198 201
199sub new { 202sub new {
200 my $class = shift; 203 my $class = shift;
201 204
202 my $self = bless { 205 my $self = bless {
203 x => 0, 206 x => "center",
204 y => 0, 207 y => "center",
205 z => 0, 208 z => 0,
209 w => undef,
210 h => undef,
206 can_events => 1, 211 can_events => 1,
207 @_ 212 @_
208 }, $class; 213 }, $class;
214
215 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
209 216
210 for (keys %$self) { 217 for (keys %$self) {
211 if (/^on_(.*)$/) { 218 if (/^on_(.*)$/) {
212 $self->connect ($1 => delete $self->{$_}); 219 $self->connect ($1 => delete $self->{$_});
213 } 220 }
214 } 221 }
215 222
216 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
217
218 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { 223 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
219 $self->{user_x} = $layout->{x} * $::WIDTH; 224 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x};
220 $self->{user_y} = $layout->{y} * $::HEIGHT; 225 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y};
221 $self->{user_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; 226 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w};
222 $self->{user_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; 227 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{h} if exists $layout->{h};
228
229 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
230 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
231
232 $self->show if $layout->{show};
223 } 233 }
224 234
225 $self 235 $self
226}
227
228sub toggle_visibility {
229 my ($self) = @_;
230
231 if ($self->{visible}) {
232 $self->hide;
233 } else {
234 $self->show;
235 }
236} 236}
237 237
238sub destroy { 238sub destroy {
239 my ($self) = @_; 239 my ($self) = @_;
240 240
242 %$self = (); 242 %$self = ();
243} 243}
244 244
245sub show { 245sub show {
246 my ($self) = @_; 246 my ($self) = @_;
247
247 return if $self->{parent}; 248 return if $self->{parent};
248 249
249 $CFClient::UI::ROOT->add ($self); 250 $CFClient::UI::ROOT->add ($self);
250} 251}
251 252
252sub show_centered { 253sub set_visible {
253 my ($self) = @_; 254 my ($self) = @_;
255
254 return if $self->{parent}; 256 return if $self->{visible};
255 257
256 $self->show; 258 $self->{root} = $self->{parent}{root};
259 $self->{visible} = $self->{parent}{visible} + 1;
257 260
258 $CFClient::UI::ROOT->on_post_alloc ( 261 $self->emit (visibility_change => 1);
259 "centered $self" => sub { 262
260 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 263 $self->realloc if !exists $self->{req_w};
261 }, 264
262 ); 265 $_->set_visible for $self->children;
263} 266}
264 267
265sub set_invisible { 268sub set_invisible {
266 my ($self) = @_; 269 my ($self) = @_;
267 270
268 return unless $self->{visible}; 271 return unless $self->{visible};
269 272
270 # broken show/hide model 273 $_->set_invisible for $self->children;
271 274
272 delete $self->{root}; 275 delete $self->{root};
273 delete $self->{visible}; 276 delete $self->{visible};
274 277
275 undef $GRAB if $GRAB == $self; 278 undef $GRAB if $GRAB == $self;
276 undef $HOVER if $HOVER == $self; 279 undef $HOVER if $HOVER == $self;
277 280
278 CFClient::UI::check_tooltip 281 CFClient::UI::check_tooltip
279 if $CFClient::UI::TOOLTIP->{owner} == $self; 282 if $TOOLTIP->{owner} == $self;
280 283
281 $self->focus_out; 284 $self->focus_out;
282 285
283 $self->emit (visibility_change => 0); 286 $self->emit (visibility_change => 0);
287}
288
289sub set_visibility {
290 my ($self, $visible) = @_;
291
292 return if $self->{visible} == $visible;
293
294 $visible ? $self->hide
295 : $self->show;
296}
297
298sub toggle_visibility {
299 my ($self) = @_;
300
301 $self->{visible}
302 ? $self->hide
303 : $self->show;
284} 304}
285 305
286sub hide { 306sub hide {
287 my ($self) = @_; 307 my ($self) = @_;
288 308
290 310
291 $self->{parent}->remove ($self) 311 $self->{parent}->remove ($self)
292 if $self->{parent}; 312 if $self->{parent};
293} 313}
294 314
295sub move { 315sub move_abs {
296 my ($self, $x, $y, $z) = @_; 316 my ($self, $x, $y, $z) = @_;
297 317
298 $self->{x} = int $x; 318 $self->{x} = List::Util::max 0, int $x;
299 $self->{y} = int $y; 319 $self->{y} = List::Util::max 0, int $y;
300 $self->{z} = $z if defined $z; 320 $self->{z} = $z if defined $z;
301 321
302 $self->update; 322 $self->update;
303} 323}
304 324
305sub set_size { 325sub set_size {
306 my ($self, $w, $h) = @_; 326 my ($self, $w, $h) = @_;
307 327
308 $self->{user_w} = $w; 328 $self->{force_w} = $w;
309 $self->{user_h} = $h; 329 $self->{force_h} = $h;
310 330
311 $self->check_size; 331 $self->realloc;
312} 332}
313 333
314sub size_request { 334sub size_request {
315 require Carp; 335 require Carp;
316 Carp::confess "size_request is abstract"; 336 Carp::confess "size_request is abstract";
318 338
319sub configure { 339sub configure {
320 my ($self, $x, $y, $w, $h) = @_; 340 my ($self, $x, $y, $w, $h) = @_;
321 341
322 if ($self->{aspect}) { 342 if ($self->{aspect}) {
343 my ($ow, $oh) = ($w, $h);
344
323 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 345 $w = List::Util::min $w, int $h * $self->{aspect};
324 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 346 $h = List::Util::min $h, int $w / $self->{aspect};
325 347
326 # use alignment to adjust x, y 348 # use alignment to adjust x, y
327 349
328 $x += int +($w - $w2) * 0.5; 350 $x += int 0.5 * ($ow - $w);
329 $y += int +($h - $h2) * 0.5; 351 $y += int 0.5 * ($oh - $h);
330
331 ($w, $h) = ($w2, $h2);
332 } 352 }
333 353
334 if ($self->{x} != $x || $self->{y} != $y) { 354 if ($self->{x} ne $x || $self->{y} ne $y) {
335 $self->{x} = $x; 355 $self->{x} = $x;
336 $self->{y} = $y; 356 $self->{y} = $y;
337 $self->update; 357 $self->update;
338 } 358 }
339 359
340 if ($self->{w} != $w || $self->{h} != $h) { 360 if ($self->{w} != $w || $self->{h} != $h) {
361 return unless $self->{visible};
362
341 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 363 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
342 } 364 }
343} 365}
344 366
345sub size_allocate { 367sub size_allocate {
346 # nothing to be done 368 # nothing to be done
347}
348
349sub reconfigure {
350 my ($self) = @_;
351
352 $self->check_size (1);
353 $self->update;
354} 369}
355 370
356sub children { 371sub children {
357} 372}
358 373
436sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 451sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
437sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 452sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
438sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 453sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
439sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 454sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
440sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 455sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
456
457sub find_widget {
458 my ($self, $x, $y) = @_;
459
460 return () unless $self->{can_events};
461
462 return $self
463 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
464 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
465
466 ()
467}
468
469sub set_parent {
470 my ($self, $parent) = @_;
471
472 Scalar::Util::weaken ($self->{parent} = $parent);
473 $self->set_visible if $parent->{visible};
474}
475
476sub connect {
477 my ($self, $signal, $cb) = @_;
478
479 push @{ $self->{signal_cb}{$signal} }, $cb;
480}
481
482sub _emit {
483 my ($self, $signal, @args) = @_;
484
485 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
486}
487
488sub emit {
489 my ($self, $signal, @args) = @_;
490
491 $self->_emit ($signal, @args)
492 || $self->$signal (@args);
493}
494
495sub visibility_change {
496 #my ($self, $visible) = @_;
497}
498
499sub realloc {
500 my ($self) = @_;
501
502 if ($self->{visible}) {
503 return if $self->{root}{realloc}{$self};
504
505 $self->{root}{realloc}{$self} = $self;
506 $self->{root}->update;
507 } else {
508 delete $self->{req_w};
509 }
510}
511
512sub update {
513 my ($self) = @_;
514
515 $self->{parent}->update
516 if $self->{parent};
517}
518
519sub reconfigure {
520 my ($self) = @_;
521
522 $self->realloc;
523 $self->update;
524}
441 525
442sub draw { 526sub draw {
443 my ($self) = @_; 527 my ($self) = @_;
444 528
445 return unless $self->{h} && $self->{w}; 529 return unless $self->{h} && $self->{w};
462 glVertex $x , $y + $self->{h}; 546 glVertex $x , $y + $self->{h};
463 glEnd; 547 glEnd;
464 glDisable GL_BLEND; 548 glDisable GL_BLEND;
465 } 549 }
466 550
467 if ($ENV{PCLIENT_DEBUG}) { 551 if ($ENV{CFPLUS_DEBUG}) {
468 glPushMatrix; 552 glPushMatrix;
469 glColor 1, 1, 0, 1; 553 glColor 1, 1, 0, 1;
470 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 554 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
471 glBegin GL_LINE_LOOP; 555 glBegin GL_LINE_LOOP;
472 glVertex 0 , 0; 556 glVertex 0 , 0;
483 my ($self) = @_; 567 my ($self) = @_;
484 568
485 warn "no draw defined for $self\n"; 569 warn "no draw defined for $self\n";
486} 570}
487 571
488sub find_widget {
489 my ($self, $x, $y) = @_;
490
491 return () unless $self->{can_events};
492
493 return $self
494 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
495 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
496
497 ()
498}
499
500sub set_parent {
501 my ($self, $parent) = @_;
502
503 Scalar::Util::weaken ($self->{parent} = $parent);
504
505 if ($parent->{visible} || 1) {
506 $self->{root} = $parent->{root};
507 $self->{visible} = $parent->{visible} + 1;
508
509 $self->emit (visibility_change => 1)
510 unless $self->{parent}{visible};
511 }
512
513 # TODO: req_w _does_change after ->reconfigure
514 $self->check_size
515 unless exists $self->{req_w};
516
517 $self->show;
518}
519
520sub check_size {
521 my ($self, $forced) = @_;
522
523 $self->{force_alloc} = 1 if $forced;
524 $CFClient::UI::ROOT->{check_size}{$self} = $self;
525}
526
527sub update {
528 my ($self) = @_;
529
530 $self->{parent}->update
531 if $self->{parent};
532}
533
534sub connect {
535 my ($self, $signal, $cb) = @_;
536
537 push @{ $self->{signal_cb}{$signal} }, $cb;
538}
539
540sub _emit {
541 my ($self, $signal, @args) = @_;
542
543 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
544}
545
546sub emit {
547 my ($self, $signal, @args) = @_;
548
549 $self->_emit ($signal, @args)
550 || $self->$signal (@args);
551}
552
553sub visibility_change {
554 #my ($self, $visible) = @_;
555}
556
557sub DESTROY { 572sub DESTROY {
558 my ($self) = @_; 573 my ($self) = @_;
559 574
560 delete $WIDGET{$self+0}; 575 delete $WIDGET{$self+0};
561 #$self->deactivate; 576 #$self->deactivate;
617 my ($class, %arg) = @_; 632 my ($class, %arg) = @_;
618 $class->SUPER::new (can_events => 0, %arg); 633 $class->SUPER::new (can_events => 0, %arg);
619} 634}
620 635
621sub size_request { 636sub size_request {
622 (0, 0) 637 my ($self) = @_;
638
639 ($self->{w} + 0, $self->{h} + 0)
623} 640}
624 641
625sub draw { } 642sub draw { }
626 643
627############################################################################# 644#############################################################################
656 $self->{children} = [ 673 $self->{children} = [
657 sort { $a->{z} <=> $b->{z} } 674 sort { $a->{z} <=> $b->{z} }
658 @{$self->{children}}, @widgets 675 @{$self->{children}}, @widgets
659 ]; 676 ];
660 677
661 $self->check_size (1); 678 $self->realloc;
662 $self->update;
663} 679}
664 680
665sub children { 681sub children {
666 @{ $_[0]{children} } 682 @{ $_[0]{children} }
667} 683}
672 delete $child->{parent}; 688 delete $child->{parent};
673 $child->hide; 689 $child->hide;
674 690
675 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 691 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
676 692
677 $self->check_size (1); 693 $self->realloc;
678 $self->update;
679} 694}
680 695
681sub clear { 696sub clear {
682 my ($self) = @_; 697 my ($self) = @_;
683 698
687 for (@$children) { 702 for (@$children) {
688 delete $_->{parent}; 703 delete $_->{parent};
689 $_->hide; 704 $_->hide;
690 } 705 }
691 706
692 $self->check_size; 707 $self->realloc;
693 $self->update;
694} 708}
695 709
696sub find_widget { 710sub find_widget {
697 my ($self, $x, $y) = @_; 711 my ($self, $x, $y) = @_;
698 712
751sub size_request { 765sub size_request {
752 $_[0]{children}[0]->size_request 766 $_[0]{children}[0]->size_request
753} 767}
754 768
755sub size_allocate { 769sub size_allocate {
756 my ($self, $w, $h) = @_; 770 my ($self, $w, $h, $changed) = @_;
757 771
758 $self->{children}[0]->configure (0, 0, $w, $h); 772 $self->{children}[0]->configure (0, 0, $w, $h);
759} 773}
760 774
761############################################################################# 775#############################################################################
778 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 792 $ROOT->on_post_alloc ($self => sub { $self->render_child });
779 $self->SUPER::update; 793 $self->SUPER::update;
780} 794}
781 795
782sub size_allocate { 796sub size_allocate {
783 my ($self, $w, $h) = @_; 797 my ($self, $w, $h, $changed) = @_;
784 798
785 $self->SUPER::size_allocate ($w, $h); 799 $self->SUPER::size_allocate ($w, $h, $changed);
786 $self->update; 800 $self->update
801 if $changed;
787} 802}
788 803
789sub _render { 804sub _render {
790 $_[0]{children}[0]->draw; 805 $_[0]{children}[0]->draw;
791} 806}
835} 850}
836 851
837sub size_request { 852sub size_request {
838 my ($self) = @_; 853 my ($self) = @_;
839 854
840 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 855 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
841 856
842 @$self{qw(child_w child_h)} 857 $w = 10 if $self->{scroll_x};
858 $h = 10 if $self->{scroll_y};
859
860 ($w, $h)
843} 861}
844 862
845sub size_allocate { 863sub size_allocate {
846 my ($self, $w, $h) = @_; 864 my ($self, $w, $h, $changed) = @_;
847 865
848 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; 866 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
849 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; 867 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
850 868
851 $self->child->configure (0, 0, $w, $h); 869 $self->child->configure (0, 0, $w, $h);
938 my $child = $self->{vp}->child; 956 my $child = $self->{vp}->child;
939 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 957 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
940} 958}
941 959
942sub size_allocate { 960sub size_allocate {
943 my ($self, $w, $h) = @_; 961 my ($self, $w, $h, $changed) = @_;
944 962
945 $self->SUPER::size_allocate ($w, $h); 963 $self->SUPER::size_allocate ($w, $h, $changed);
946 964
947 my $child = $self->{vp}->child; 965 my $child = $self->{vp}->child;
948 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 966 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
949} 967}
950 968
997 1015
998our @ISA = CFClient::UI::Bin::; 1016our @ISA = CFClient::UI::Bin::;
999 1017
1000use CFClient::OpenGL; 1018use CFClient::OpenGL;
1001 1019
1002my @tex = 1020my $bg =
1021 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1022 mipmap => 1, wrap => 1;
1023
1024my @border =
1003 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1025 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1004 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1026 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1005 1027
1006sub new { 1028sub new {
1007 my $class = shift; 1029 my $class = shift;
1008
1009 # TODO: user_x, user_y, overwrite moveto?
1010 1030
1011 my $self = $class->SUPER::new ( 1031 my $self = $class->SUPER::new (
1012 bg => [1, 1, 1, 1], 1032 bg => [1, 1, 1, 1],
1013 border_bg => [1, 1, 1, 1], 1033 border_bg => [1, 1, 1, 1],
1014 border => 0.6, 1034 border => 0.6,
1015 is_toplevel => 1,
1016 can_events => 1, 1035 can_events => 1,
1036 min_w => 16,
1037 min_h => 16,
1017 @_ 1038 @_
1018 ); 1039 );
1019 1040
1020 $self->{title} &&= new CFClient::UI::Label 1041 $self->{title} &&= new CFClient::UI::Label
1021 align => 0, 1042 align => 0,
1040 $h + $self->border * 2, 1061 $h + $self->border * 2,
1041 ) 1062 )
1042} 1063}
1043 1064
1044sub size_allocate { 1065sub size_allocate {
1045 my ($self, $w, $h) = @_; 1066 my ($self, $w, $h, $changed) = @_;
1067
1068 return unless $changed;
1046 1069
1047 $h -= List::Util::max 0, $self->border * 2; 1070 $h -= List::Util::max 0, $self->border * 2;
1048 $w -= List::Util::max 0, $self->border * 2; 1071 $w -= List::Util::max 0, $self->border * 2;
1049 1072
1050 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1073 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1074 my ($ev, $x, $y) = @_; 1097 my ($ev, $x, $y) = @_;
1075 1098
1076 my $dx = $ev->{x} - $ox; 1099 my $dx = $ev->{x} - $ox;
1077 my $dy = $ev->{y} - $oy; 1100 my $dy = $ev->{y} - $oy;
1078 1101
1079 $self->{user_x} = $wx + $dx * $mx;
1080 $self->{user_y} = $wy + $dy * $my;
1081 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1102 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1082 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1103 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1083 $self->move ($self->{user_x}, $self->{user_y}); 1104
1084 $self->check_size; 1105 $self->realloc;
1106 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1085 }; 1107 };
1086 1108
1087 } elsif ($lr ^ $td) { 1109 } elsif ($lr ^ $td) {
1088 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1110 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1089 my ($bx, $by) = ($self->{x}, $self->{y}); 1111 my ($bx, $by) = ($self->{x}, $self->{y});
1091 $self->{motion} = sub { 1113 $self->{motion} = sub {
1092 my ($ev, $x, $y) = @_; 1114 my ($ev, $x, $y) = @_;
1093 1115
1094 ($x, $y) = ($ev->{x}, $ev->{y}); 1116 ($x, $y) = ($ev->{x}, $ev->{y});
1095 1117
1096 $self->{user_x} = $bx + $x - $ox; 1118 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1097 $self->{user_y} = $by + $y - $oy;
1098 $self->move ($self->{user_x}, $self->{user_y});
1099 $self->update;
1100 }; 1119 };
1101 } 1120 }
1102} 1121}
1103 1122
1104sub button_up { 1123sub button_up {
1123 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1142 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1124 1143
1125 my $border = $self->border; 1144 my $border = $self->border;
1126 1145
1127 glColor @{ $self->{border_bg} }; 1146 glColor @{ $self->{border_bg} };
1128 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1147 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1129 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1148 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1130 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1149 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1131 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1150 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1132 1151
1133 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1152 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1134 my $bg = $tex[0]; 1153 glColor @{ $self->{bg} };
1135 1154
1136 # TODO: repeat texture not scale 1155 # TODO: repeat texture not scale
1156 # solve this better(?)
1137 my $rep_x = $cw / $bg->{w}; 1157 $bg->{s} = $cw / $bg->{w};
1138 my $rep_y = $ch / $bg->{h}; 1158 $bg->{t} = $ch / $bg->{h};
1139
1140 glColor @{ $self->{bg} };
1141
1142 $bg->{s} = $rep_x;
1143 $bg->{t} = $rep_y;
1144 $bg->{wrap_mode} = 1;
1145 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1159 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1146 } 1160 }
1147 1161
1148 glDisable GL_TEXTURE_2D; 1162 glDisable GL_TEXTURE_2D;
1149 1163
1179 my ($self, $x, $y, $child) = @_; 1193 my ($self, $x, $y, $child) = @_;
1180 1194
1181 $child->set_parent ($self); 1195 $child->set_parent ($self);
1182 $self->{children}[$y][$x] = $child; 1196 $self->{children}[$y][$x] = $child;
1183 1197
1184 $self->check_size (1); 1198 $self->realloc;
1185} 1199}
1186 1200
1187# TODO: move to container class maybe? send children a signal on removal? 1201# TODO: move to container class maybe? send children a signal on removal?
1188sub clear { 1202sub clear {
1189 my ($self) = @_; 1203 my ($self) = @_;
1194 for (@children) { 1208 for (@children) {
1195 delete $_->{parent}; 1209 delete $_->{parent};
1196 $_->hide; 1210 $_->hide;
1197 } 1211 }
1198 1212
1199 $self->check_size (1); 1213 $self->realloc;
1200 $self->update;
1201} 1214}
1202 1215
1203sub get_wh { 1216sub get_wh {
1204 my ($self) = @_; 1217 my ($self) = @_;
1205 1218
1232 (sum @$hs), 1245 (sum @$hs),
1233 ) 1246 )
1234} 1247}
1235 1248
1236sub size_allocate { 1249sub size_allocate {
1237 my ($self, $w, $h) = @_; 1250 my ($self, $w, $h, $changed) = @_;
1238 1251
1239 my ($ws, $hs) = $self->get_wh; 1252 my ($ws, $hs) = $self->get_wh;
1240 1253
1241 my $req_w = (sum @$ws) || 1; 1254 my $req_w = (sum @$ws) || 1;
1242 my $req_h = (sum @$hs) || 1; 1255 my $req_h = (sum @$hs) || 1;
1301 } 1314 }
1302} 1315}
1303 1316
1304############################################################################# 1317#############################################################################
1305 1318
1319package CFClient::UI::Box;
1320
1321our @ISA = CFClient::UI::Container::;
1322
1323sub size_request {
1324 my ($self) = @_;
1325
1326 $self->{vertical}
1327 ? (
1328 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1329 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1330 )
1331 : (
1332 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1333 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1334 )
1335}
1336
1337sub size_allocate {
1338 my ($self, $w, $h, $changed) = @_;
1339
1340 my $space = $self->{vertical} ? $h : $w;
1341 my $children = $self->{children};
1342
1343 my @req;
1344
1345 if ($self->{homogeneous}) {
1346 @req = ($space / (@$children || 1)) x @$children;
1347 } else {
1348 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1349 my $req = List::Util::sum @req;
1350
1351 if ($req > $space) {
1352 # ah well, not enough space
1353 $_ *= $space / $req for @req;
1354 } else {
1355 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1356
1357 $space = ($space - $req) / $expand; # remaining space to give away
1358
1359 $req[$_] += $space * $children->[$_]{expand}
1360 for 0 .. $#$children;
1361 }
1362 }
1363
1364 CFClient::UI::harmonize \@req;
1365
1366 my $pos = 0;
1367 for (0 .. $#$children) {
1368 my $alloc = $req[$_];
1369 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1370
1371 $pos += $alloc;
1372 }
1373
1374 1
1375}
1376
1377#############################################################################
1378
1306package CFClient::UI::HBox; 1379package CFClient::UI::HBox;
1307 1380
1308# TODO: wrap into common Box base class
1309
1310our @ISA = CFClient::UI::Container::; 1381our @ISA = CFClient::UI::Box::;
1311 1382
1312sub size_request { 1383sub new {
1313 my ($self) = @_; 1384 my $class = shift;
1314 1385
1315 my @alloc = map [$_->size_request], @{$self->{children}}; 1386 $class->SUPER::new (
1316 1387 vertical => 0,
1317 ( 1388 @_,
1318 (List::Util::sum map $_->[0], @alloc),
1319 (List::Util::max map $_->[1], @alloc),
1320 ) 1389 )
1321} 1390}
1322 1391
1323sub size_allocate {
1324 my ($self, $w, $h) = @_;
1325
1326 ($h, $w) = ($w, $h);
1327
1328 my $children = $self->{children};
1329
1330 my @h = map $_->{req_w}, @$children;
1331
1332 my $req_h = List::Util::sum @h;
1333
1334 if ($req_h > $h) {
1335 # ah well, not enough space
1336 $_ *= $h / $req_h for @h;
1337 } else {
1338 my $exp = List::Util::sum map $_->{expand}, @$children;
1339 $exp ||= 1;
1340
1341 for (0 .. $#$children) {
1342 my $child = $children->[$_];
1343
1344 my $alloc_h = $h[$_];
1345 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1346 $h[$_] = $alloc_h;
1347 }
1348 }
1349
1350 CFClient::UI::harmonize \@h;
1351
1352 my $y = 0;
1353 for (0 .. $#$children) {
1354 my $child = $children->[$_];
1355 my $h = $h[$_];
1356 $child->configure ($y, 0, $h, $w);
1357
1358 $y += $h;
1359 }
1360
1361 1
1362}
1363
1364############################################################################# 1392#############################################################################
1365 1393
1366package CFClient::UI::VBox; 1394package CFClient::UI::VBox;
1367 1395
1368# TODO: wrap into common Box base class
1369
1370our @ISA = CFClient::UI::Container::; 1396our @ISA = CFClient::UI::Box::;
1371 1397
1372sub size_request { 1398sub new {
1373 my ($self) = @_; 1399 my $class = shift;
1374 1400
1375 my @alloc = map [$_->size_request], @{$self->{children}}; 1401 $class->SUPER::new (
1376 1402 vertical => 1,
1377 ( 1403 @_,
1378 (List::Util::max map $_->[0], @alloc),
1379 (List::Util::sum map $_->[1], @alloc),
1380 ) 1404 )
1381}
1382
1383sub size_allocate {
1384 my ($self, $w, $h) = @_;
1385
1386 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1387
1388 my $children = $self->{children};
1389
1390 my @h = map $_->{req_h}, @$children;
1391
1392 my $req_h = List::Util::sum @h;
1393
1394 if ($req_h > $h) {
1395 # ah well, not enough space
1396 $_ *= $h / $req_h for @h;
1397 } else {
1398 my $exp = List::Util::sum map $_->{expand}, @$children;
1399 $exp ||= 1;
1400
1401 for (0 .. $#$children) {
1402 my $child = $children->[$_];
1403
1404 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1405 }
1406 }
1407
1408 CFClient::UI::harmonize \@h;
1409
1410 my $y = 0;
1411 for (0 .. $#$children) {
1412 my $child = $children->[$_];
1413 my $h = $h[$_];
1414 $child->configure (0, $y, $w, $h);
1415
1416 $y += $h;
1417 }
1418
1419 1
1420} 1405}
1421 1406
1422############################################################################# 1407#############################################################################
1423 1408
1424package CFClient::UI::Label; 1409package CFClient::UI::Label;
1441 ellipsise => 3, # end 1426 ellipsise => 3, # end
1442 layout => (new CFClient::Layout), 1427 layout => (new CFClient::Layout),
1443 fontsize => 1, 1428 fontsize => 1,
1444 align => -1, 1429 align => -1,
1445 valign => -1, 1430 valign => -1,
1446 padding => 2, 1431 padding_x => 2,
1432 padding_y => 2,
1447 can_events => 0, 1433 can_events => 0,
1448 %arg 1434 %arg
1449 ); 1435 );
1450 1436
1451 if (exists $self->{template}) { 1437 if (exists $self->{template}) {
1487 $self->{text} = "T$text"; 1473 $self->{text} = "T$text";
1488 1474
1489 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1475 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1490 $self->{layout}->set_text ($text); 1476 $self->{layout}->set_text ($text);
1491 1477
1478 $self->realloc;
1492 $self->update; 1479 $self->update;
1493 $self->check_size;
1494} 1480}
1495 1481
1496sub set_markup { 1482sub set_markup {
1497 my ($self, $markup) = @_; 1483 my ($self, $markup) = @_;
1498 1484
1502 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1488 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1503 1489
1504 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1490 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1505 $self->{layout}->set_markup ($markup); 1491 $self->{layout}->set_markup ($markup);
1506 1492
1493 $self->realloc;
1507 $self->update; 1494 $self->update;
1508 $self->check_size;
1509} 1495}
1510 1496
1511sub size_request { 1497sub size_request {
1512 my ($self) = @_; 1498 my ($self) = @_;
1513 1499
1527 1513
1528 $w = List::Util::max $w, $w2; 1514 $w = List::Util::max $w, $w2;
1529 $h = List::Util::max $h, $h2; 1515 $h = List::Util::max $h, $h2;
1530 } 1516 }
1531 1517
1532 ( 1518 ($w, $h)
1533 $w + $self->{padding} * 2,
1534 $h + $self->{padding} * 2,
1535 )
1536} 1519}
1537 1520
1538sub size_allocate { 1521sub size_allocate {
1539 my ($self, $w, $h) = @_; 1522 my ($self, $w, $h, $changed) = @_;
1540 1523
1541 delete $self->{texture}; 1524 delete $self->{texture}
1525 if $changed;
1542} 1526}
1543 1527
1544sub set_fontsize { 1528sub set_fontsize {
1545 my ($self, $fontsize) = @_; 1529 my ($self, $fontsize) = @_;
1546 1530
1547 $self->{fontsize} = $fontsize; 1531 $self->{fontsize} = $fontsize;
1548 delete $self->{texture}; 1532 delete $self->{texture};
1549 1533
1550 $self->update; 1534 $self->realloc;
1551 $self->check_size;
1552} 1535}
1553 1536
1554sub _draw { 1537sub _draw {
1555 my ($self) = @_; 1538 my ($self) = @_;
1556 1539
1564 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1547 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1565 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1548 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1566 1549
1567 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1550 my $tex = new_from_layout CFClient::Texture $self->{layout};
1568 1551
1569 $self->{ox} = int ($self->{align} < 0 ? $self->{padding} 1552 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1570 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1553 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x}
1571 : ($self->{w} - $tex->{w}) * 0.5); 1554 : ($self->{w} - $tex->{w}) * 0.5);
1572 1555
1573 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} 1556 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1574 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1557 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1575 : ($self->{h} - $tex->{h}) * 0.5); 1558 : ($self->{h} - $tex->{h}) * 0.5);
1576 1559
1577 $tex 1560 $tex
1578 }; 1561 };
1579 1562
1636sub set_text { 1619sub set_text {
1637 my ($self, $text) = @_; 1620 my ($self, $text) = @_;
1638 1621
1639 $self->{cursor} = length $text; 1622 $self->{cursor} = length $text;
1640 $self->_set_text ($text); 1623 $self->_set_text ($text);
1641 $self->update; 1624
1642 $self->check_size; 1625 $self->realloc;
1643} 1626}
1644 1627
1645sub get_text { 1628sub get_text {
1646 $_[0]{text} 1629 $_[0]{text}
1647} 1630}
1680 } elsif ($uni) { 1663 } elsif ($uni) {
1681 substr $text, $self->{cursor}++, 0, chr $uni; 1664 substr $text, $self->{cursor}++, 0, chr $uni;
1682 } 1665 }
1683 1666
1684 $self->_set_text ($text); 1667 $self->_set_text ($text);
1685 $self->update; 1668
1686 $self->check_size; 1669 $self->realloc;
1687} 1670}
1688 1671
1689sub focus_in { 1672sub focus_in {
1690 my ($self) = @_; 1673 my ($self) = @_;
1691 1674
1818 1801
1819sub new { 1802sub new {
1820 my $class = shift; 1803 my $class = shift;
1821 1804
1822 $class->SUPER::new ( 1805 $class->SUPER::new (
1823 padding => 4, 1806 padding_x => 4,
1807 padding_y => 4,
1824 fg => [1, 1, 1], 1808 fg => [1, 1, 1],
1825 active_fg => [0, 0, 1], 1809 active_fg => [0, 0, 1],
1826 can_hover => 1, 1810 can_hover => 1,
1827 align => 0, 1811 align => 0,
1828 valign => 0, 1812 valign => 0,
1875 1859
1876sub new { 1860sub new {
1877 my $class = shift; 1861 my $class = shift;
1878 1862
1879 $class->SUPER::new ( 1863 $class->SUPER::new (
1880 padding => 2, 1864 padding_x => 2,
1865 padding_y => 2,
1881 fg => [1, 1, 1], 1866 fg => [1, 1, 1],
1882 active_fg => [1, 1, 0], 1867 active_fg => [1, 1, 0],
1883 bg => [0, 0, 0, 0.2], 1868 bg => [0, 0, 0, 0.2],
1884 active_bg => [1, 1, 1, 0.5], 1869 active_bg => [1, 1, 1, 0.5],
1885 state => 0, 1870 state => 0,
1889} 1874}
1890 1875
1891sub size_request { 1876sub size_request {
1892 my ($self) = @_; 1877 my ($self) = @_;
1893 1878
1894 ($self->{padding} * 2 + 6) x 2 1879 (6) x 2
1895} 1880}
1896 1881
1897sub button_down { 1882sub button_down {
1898 my ($self, $ev, $x, $y) = @_; 1883 my ($self, $ev, $x, $y) = @_;
1899 1884
1900 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1885 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1901 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1886 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1902 $self->{state} = !$self->{state}; 1887 $self->{state} = !$self->{state};
1903 $self->_emit (changed => $self->{state}); 1888 $self->_emit (changed => $self->{state});
1904 } 1889 }
1905} 1890}
1906 1891
1907sub _draw { 1892sub _draw {
1908 my ($self) = @_; 1893 my ($self) = @_;
1909 1894
1910 $self->SUPER::_draw; 1895 $self->SUPER::_draw;
1911 1896
1912 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 1897 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
1913 1898
1914 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1899 my ($w, $h) = @$self{qw(w h)};
1900
1901 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
1915 1902
1916 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1903 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1917 1904
1918 my $tex = $self->{state} ? $tex[1] : $tex[0]; 1905 my $tex = $self->{state} ? $tex[1] : $tex[0];
1919 1906
2184 fg => [1, 1, 1], 2171 fg => [1, 1, 1],
2185 active_fg => [0, 0, 0], 2172 active_fg => [0, 0, 0],
2186 bg => [0, 0, 0, 0.2], 2173 bg => [0, 0, 0, 0.2],
2187 active_bg => [1, 1, 1, 0.5], 2174 active_bg => [1, 1, 1, 0.5],
2188 range => [0, 0, 100, 10, 0], 2175 range => [0, 0, 100, 10, 0],
2189 req_w => $::WIDTH / 80, 2176 min_w => $::WIDTH / 80,
2190 req_h => $::WIDTH / 80, 2177 min_h => $::WIDTH / 80,
2191 vertical => 0, 2178 vertical => 0,
2192 can_hover => 1, 2179 can_hover => 1,
2193 inner_pad => 0.02, 2180 inner_pad => 0.02,
2194 @_ 2181 @_
2195 ); 2182 );
2198 $self->update; 2185 $self->update;
2199 2186
2200 $self 2187 $self
2201} 2188}
2202 2189
2190sub changed { }
2191
2203sub set_range { 2192sub set_range {
2204 my ($self, $range) = @_; 2193 my ($self, $range) = @_;
2205 2194
2206 ($range, $self->{range}) = ($self->{range}, $range); 2195 ($range, $self->{range}) = ($self->{range}, $range);
2207 2196
2233} 2222}
2234 2223
2235sub size_request { 2224sub size_request {
2236 my ($self) = @_; 2225 my ($self) = @_;
2237 2226
2238 my $w = $self->{req_w}; 2227 ($self->{req_w}, $self->{req_h})
2239 my $h = $self->{req_h};
2240
2241 $self->{vertical} ? ($h, $w) : ($w, $h)
2242} 2228}
2243 2229
2244sub button_down { 2230sub button_down {
2245 my ($self, $ev, $x, $y) = @_; 2231 my ($self, $ev, $x, $y) = @_;
2246 2232
2396 $self->{fontsize} = $fontsize; 2382 $self->{fontsize} = $fontsize;
2397 $self->reflow; 2383 $self->reflow;
2398} 2384}
2399 2385
2400sub size_allocate { 2386sub size_allocate {
2401 my ($self, $w, $h) = @_; 2387 my ($self, $w, $h, $changed) = @_;
2402 2388
2403 $self->SUPER::size_allocate ($w, $h); 2389 $self->SUPER::size_allocate ($w, $h, $changed);
2390
2391 return unless $changed;
2404 2392
2405 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2393 $self->{layout}->set_font ($self->{font}) if $self->{font};
2406 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2394 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2407 $self->{layout}->set_width ($self->{children}[0]{w}); 2395 $self->{layout}->set_width ($self->{children}[0]{w});
2408 2396
2650 2638
2651 ($w + 4, $h + 4) 2639 ($w + 4, $h + 4)
2652} 2640}
2653 2641
2654sub size_allocate { 2642sub size_allocate {
2655 my ($self, $w, $h) = @_; 2643 my ($self, $w, $h, $changed) = @_;
2656 2644
2645 return unless $changed;
2646
2657 $self->SUPER::size_allocate ($w - 4, $h - 4); 2647 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2648}
2649
2650sub visibility_change {
2651 my ($self, $visible) = @_;
2652
2653 return unless $visible;
2654
2655 $self->{root}->on_post_alloc ("move_$self" => sub {
2656 my $widget = $self->{owner}
2657 or return;
2658
2659 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2660
2661 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2662 if $x + $self->{w} > $::WIDTH;
2663
2664 $self->move_abs ($x, $y);
2665 });
2658} 2666}
2659 2667
2660sub _draw { 2668sub _draw {
2661 my ($self) = @_; 2669 my ($self) = @_;
2662 2670
2679 glVertex $w, $h; 2687 glVertex $w, $h;
2680 glVertex $w, 0; 2688 glVertex $w, 0;
2681 glEnd; 2689 glEnd;
2682 2690
2683 glTranslate 2 - 0.375, 2 - 0.375; 2691 glTranslate 2 - 0.375, 2 - 0.375;
2692
2684 $self->SUPER::_draw; 2693 $self->SUPER::_draw;
2685} 2694}
2686 2695
2687############################################################################# 2696#############################################################################
2688 2697
2804 2813
2805 $row++; 2814 $row++;
2806 } 2815 }
2807} 2816}
2808 2817
2809sub size_request {
2810 my ($self) = @_;
2811 ($self->{req_w}, $self->{req_h});
2812}
2813
2814############################################################################# 2818#############################################################################
2815 2819
2816package CFClient::UI::Menu; 2820package CFClient::UI::Menu;
2817 2821
2818our @ISA = CFClient::UI::FancyFrame::; 2822our @ISA = CFClient::UI::FancyFrame::;
2858 # maybe save $GRAB? must be careful about events... 2862 # maybe save $GRAB? must be careful about events...
2859 $GRAB = $self; 2863 $GRAB = $self;
2860 $self->{button} = $ev->{button}; 2864 $self->{button} = $ev->{button};
2861 2865
2862 $self->show; 2866 $self->show;
2863 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 2867 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2864} 2868}
2865 2869
2866sub mouse_motion { 2870sub mouse_motion {
2867 my ($self, $ev, $x, $y) = @_; 2871 my ($self, $ev, $x, $y) = @_;
2868 2872
3002use CFClient::OpenGL; 3006use CFClient::OpenGL;
3003 3007
3004sub new { 3008sub new {
3005 my $class = shift; 3009 my $class = shift;
3006 3010
3007 $class->SUPER::new ( 3011 my $self = $class->SUPER::new (
3008 visible => 1, 3012 visible => 1,
3009 @_, 3013 @_,
3010 ) 3014 );
3015
3016 Scalar::Util::weaken ($self->{root} = $self);
3017
3018 $self
3011} 3019}
3012 3020
3013sub configure { 3021sub configure {
3014 my ($self, $x, $y, $w, $h) = @_; 3022 my ($self, $x, $y, $w, $h) = @_;
3015 3023
3016 $self->{w} = $w; 3024 $self->{w} = $w;
3017 $self->{h} = $h; 3025 $self->{h} = $h;
3018} 3026}
3019 3027
3020sub check_size { 3028sub reconfigure {
3021 my ($self) = @_; 3029 my ($self) = @_;
3022 3030
3031 $self->SUPER::reconfigure;
3032
3023 $self->size_allocate ($self->{w}, $self->{h}) 3033 $self->size_allocate ($self->{w}, $self->{h}, 1)
3024 if $self->{w}; 3034 if $self->{w};
3025} 3035}
3026 3036
3027sub size_request { 3037sub size_request {
3028 my ($self) = @_; 3038 my ($self) = @_;
3029 3039
3030 ($self->{w}, $self->{h}) 3040 ($self->{w}, $self->{h})
3031} 3041}
3032 3042
3043sub _to_pixel {
3044 my ($coord, $size, $max) = @_;
3045
3046 $coord =
3047 $coord eq "center" ? ($max - $size) * 0.5
3048 : $coord eq "max" ? $max
3049 : $coord;
3050
3051 $coord = 0 if $coord < 0;
3052 $coord = $max - $size if $coord > $max - $size;
3053
3054 int $coord + 0.5
3055}
3056
3033sub size_allocate { 3057sub size_allocate {
3034 my ($self, $w, $h) = @_; 3058 my ($self, $w, $h, $changed) = @_;
3035 3059
3036 for my $child ($self->children) { 3060 for my $child ($self->children) {
3037 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3061 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3038 3062
3039 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3063 $X = $child->{force_x} if exists $child->{force_x};
3040 if exists $child->{req_x}; 3064 $Y = $child->{force_y} if exists $child->{force_y};
3041 3065
3042 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3066 $X = _to_pixel $X, $W, $self->{w};
3043 if exists $child->{req_y}; 3067 $Y = _to_pixel $Y, $H, $self->{h};
3044
3045 $X = $self->{user_x} if exists $self->{user_x};
3046 $Y = $self->{user_y} if exists $self->{user_y};
3047
3048 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3049 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3050 3068
3051 $child->configure ($X, $Y, $W, $H); 3069 $child->configure ($X, $Y, $W, $H);
3052 } 3070 }
3053} 3071}
3054 3072
3065} 3083}
3066 3084
3067sub update { 3085sub update {
3068 my ($self) = @_; 3086 my ($self) = @_;
3069 3087
3070 $self->check_size;
3071 $::WANT_REFRESH++; 3088 $::WANT_REFRESH++;
3072} 3089}
3073 3090
3074sub add { 3091sub add {
3075 my ($self, @children) = @_; 3092 my ($self, @children) = @_;
3076 3093
3077 for (my @widgets = @children; my $w = pop @widgets; ) {
3078 push @widgets, $w->children;
3079 $w->{root} = $self;
3080 $w->{visible} = $self->{visible} + 1;
3081 }
3082
3083 for my $child (@children) {
3084 $child->{is_toplevel} = 1; 3094 $_->{is_toplevel} = 1
3085 3095 for @children;
3086 # integerise window positions
3087 $child->{x} = int $child->{x};
3088 $child->{y} = int $child->{y};
3089 }
3090 3096
3091 $self->SUPER::add (@children); 3097 $self->SUPER::add (@children);
3092} 3098}
3093 3099
3094sub remove { 3100sub remove {
3095 my ($self, @children) = @_; 3101 my ($self, @children) = @_;
3096 3102
3097 $self->SUPER::remove (@children); 3103 $self->SUPER::remove (@children);
3104
3105 delete $self->{is_toplevel}
3106 for @children;
3098 3107
3099 while (@children) { 3108 while (@children) {
3100 my $w = pop @children; 3109 my $w = pop @children;
3101 push @children, $w->children; 3110 push @children, $w->children;
3102 $w->set_invisible; 3111 $w->set_invisible;
3121 while ($self->{refresh_hook}) { 3130 while ($self->{refresh_hook}) {
3122 $_->() 3131 $_->()
3123 for values %{delete $self->{refresh_hook}}; 3132 for values %{delete $self->{refresh_hook}};
3124 } 3133 }
3125 3134
3126 if ($self->{check_size}) { 3135 if ($self->{realloc}) {
3127 my @queue; 3136 my @queue;
3128 3137
3129 for (;;) { 3138 while () {
3130 if ($self->{check_size}) { 3139 if ($self->{realloc}) {
3131 #TODO use array-of-depth approach 3140 #TODO use array-of-depth approach
3132 3141
3133 @queue = sort { $a->{visible} <=> $b->{visible} } 3142 @queue = sort { $a->{visible} <=> $b->{visible} }
3134 @queue, values %{delete $self->{check_size}}; 3143 @queue, values %{delete $self->{realloc}};
3135 } 3144 }
3136 3145
3137 my $widget = pop @queue || last; 3146 my $widget = pop @queue || last;
3138 3147
3139 defined $widget->{visible} or last; # do not resize invisible widgets 3148 $widget->{visible} or last; # do not resize invisible widgets
3140 3149
3141 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
3142 ? @$widget{qw(user_w user_h)}
3143 : $widget->size_request; 3150 my ($w, $h) = $widget->size_request;
3144 3151
3145 if (delete $widget->{force_alloc} 3152 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3146 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3153 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3147 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3154
3148 3155 $w = $widget->{force_w} if exists $widget->{force_w};
3156 $h = $widget->{force_h} if exists $widget->{force_h};
3157
3149 $widget->{req_w} = $w; 3158 $widget->{req_w} = $w;
3150 $widget->{req_h} = $h; 3159 $widget->{req_h} = $h;
3151 3160
3152 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3161 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3153 3162
3154 $widget->{parent}->check_size 3163 push @queue, $widget->{parent}
3155 if $widget->{parent}; 3164 if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent};
3156 }
3157 } 3165 }
3158 } 3166 }
3159 3167
3160 while ($self->{size_alloc}) { 3168 while (my $size_alloc = delete $self->{size_alloc}) {
3161 for (values %{delete $self->{size_alloc}}) { 3169 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3162 my ($widget, $w, $h) = @$_; 3170 values %$size_alloc;
3171
3172 while () {
3173 my ($widget, $w, $h) = @{ pop @queue or last };
3174
3175 $w = $widget->{w} || $widget->{req_w} unless defined $w;
3176 $h = $widget->{h} || $widget->{req_h} unless defined $h;
3163 3177
3164 $w = 0 if $w < 0; 3178 $w = 0 if $w < 0;
3165 $h = 0 if $h < 0; 3179 $h = 0 if $h < 0;
3166 3180
3181 $w = int $w + 0.5;
3182 $h = int $h + 0.5;
3183
3184 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3185
3167 $widget->{w} = $w; 3186 $widget->{w} = $w;
3168 $widget->{h} = $h; 3187 $widget->{h} = $h;
3188
3169 $widget->emit (size_allocate => $w, $h); 3189 $widget->emit (size_allocate => $w, $h, $changed);
3170 } 3190 }
3171 } 3191 }
3172 3192
3173 while ($self->{post_alloc_hook}) { 3193 while ($self->{post_alloc_hook}) {
3174 $_->() 3194 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines