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.247 by root, Sun May 28 01:16:04 2006 UTC vs.
Revision 1.255 by root, Mon May 29 02:01:56 2006 UTC

40 if (!$GRAB) { 40 if (!$GRAB) {
41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
42 if (length $widget->{tooltip}) { 42 if (length $widget->{tooltip}) {
43 43
44 if ($TOOLTIP->{owner} != $widget) { 44 if ($TOOLTIP->{owner} != $widget) {
45 $TOOLTIP->hide;
46
45 $TOOLTIP->{owner} = $widget; 47 $TOOLTIP->{owner} = $widget;
46 48
47 my $tip = $widget->{tooltip}; 49 my $tip = $widget->{tooltip};
48 50
49 $tip = $tip->($widget) if CODE:: eq ref $tip; 51 $tip = $tip->($widget) if CODE:: eq ref $tip;
50 52
51 $TOOLTIP->set_tooltip_from ($widget); 53 $TOOLTIP->set_tooltip_from ($widget);
52 $TOOLTIP->show; 54 $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 } 55 }
63 56
64 return; 57 return;
65 } 58 }
66 } 59 }
221 } 214 }
222 215
223 $self 216 $self
224} 217}
225 218
226sub toggle_visibility {
227 my ($self) = @_;
228
229 if ($self->{visible}) {
230 $self->hide;
231 } else {
232 $self->show;
233 }
234}
235
236sub destroy { 219sub destroy {
237 my ($self) = @_; 220 my ($self) = @_;
238 221
239 $self->hide; 222 $self->hide;
240 %$self = (); 223 %$self = ();
241} 224}
242 225
243sub show { 226sub show {
244 my ($self) = @_; 227 my ($self) = @_;
228
245 return if $self->{parent}; 229 return if $self->{parent};
246 230
247 $CFClient::UI::ROOT->add ($self); 231 $CFClient::UI::ROOT->add ($self);
248} 232}
249 233
250sub center { 234sub center {
251 my ($self) = @_; 235 my ($self) = @_;
252 236
253 $CFClient::UI::ROOT->on_post_alloc ( 237 $CFClient::UI::ROOT->on_post_alloc (
254 "center_$self" => sub { 238 "center_$self" => sub {
255 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 239 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
256 }, 240 },
257 ); 241 );
258 242
259 $self->update; 243 $self->update;
260} 244}
265 return if $self->{visible}; 249 return if $self->{visible};
266 250
267 $self->{root} = $self->{parent}{root}; 251 $self->{root} = $self->{parent}{root};
268 $self->{visible} = $self->{parent}{visible} + 1; 252 $self->{visible} = $self->{parent}{visible} + 1;
269 253
270 $self->emit (visibility_change => 1) 254 $self->emit (visibility_change => 1);
255
256 $self->realloc if !exists $self->{req_w};
257
258 $_->set_visible for $self->children;
271} 259}
272 260
273sub set_invisible { 261sub set_invisible {
274 my ($self) = @_; 262 my ($self) = @_;
275 263
276 return unless $self->{visible}; 264 return unless $self->{visible};
277 265
278 # broken show/hide model 266 $_->set_invisible for $self->children;
279 267
280 delete $self->{root}; 268 delete $self->{root};
281 delete $self->{visible}; 269 delete $self->{visible};
282 270
283 undef $GRAB if $GRAB == $self; 271 undef $GRAB if $GRAB == $self;
284 undef $HOVER if $HOVER == $self; 272 undef $HOVER if $HOVER == $self;
285 273
286 CFClient::UI::check_tooltip 274 CFClient::UI::check_tooltip
287 if $CFClient::UI::TOOLTIP->{owner} == $self; 275 if $TOOLTIP->{owner} == $self;
288 276
289 $self->focus_out; 277 $self->focus_out;
290 278
291 $self->emit (visibility_change => 0); 279 $self->emit (visibility_change => 0);
280}
281
282sub set_visibility {
283 my ($self, $visible) = @_;
284
285 return if $self->{visible} == $visible;
286
287 $visible ? $self->hide
288 : $self->show;
289}
290
291sub toggle_visibility {
292 my ($self) = @_;
293
294 $self->{visible}
295 ? $self->hide
296 : $self->show;
292} 297}
293 298
294sub hide { 299sub hide {
295 my ($self) = @_; 300 my ($self) = @_;
296 301
314 my ($self, $w, $h) = @_; 319 my ($self, $w, $h) = @_;
315 320
316 $self->{def_w} = $w; 321 $self->{def_w} = $w;
317 $self->{def_h} = $h; 322 $self->{def_h} = $h;
318 323
319 $self->check_size; 324 $self->realloc;
320} 325}
321 326
322sub size_request { 327sub size_request {
323 require Carp; 328 require Carp;
324 Carp::confess "size_request is abstract"; 329 Carp::confess "size_request is abstract";
344 $self->{y} = $y; 349 $self->{y} = $y;
345 $self->update; 350 $self->update;
346 } 351 }
347 352
348 if ($self->{w} != $w || $self->{h} != $h) { 353 if ($self->{w} != $w || $self->{h} != $h) {
354 return unless $self->{visible};
355
349 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 356 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
350 } 357 }
351} 358}
352 359
353sub size_allocate { 360sub size_allocate {
354 # nothing to be done 361 # nothing to be done
355}
356
357sub reconfigure {
358 my ($self) = @_;
359
360 $self->check_size (1);
361 $self->update;
362} 362}
363 363
364sub children { 364sub children {
365} 365}
366 366
444sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 444sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
445sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 445sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
446sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 446sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
447sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 447sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
448sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 448sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
449
450sub find_widget {
451 my ($self, $x, $y) = @_;
452
453 return () unless $self->{can_events};
454
455 return $self
456 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
457 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
458
459 ()
460}
461
462sub set_parent {
463 my ($self, $parent) = @_;
464
465 Scalar::Util::weaken ($self->{parent} = $parent);
466
467 $self->set_visible if $parent->{visible};
468}
469
470sub connect {
471 my ($self, $signal, $cb) = @_;
472
473 push @{ $self->{signal_cb}{$signal} }, $cb;
474}
475
476sub _emit {
477 my ($self, $signal, @args) = @_;
478
479 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
480}
481
482sub emit {
483 my ($self, $signal, @args) = @_;
484
485 $self->_emit ($signal, @args)
486 || $self->$signal (@args);
487}
488
489sub visibility_change {
490 #my ($self, $visible) = @_;
491}
492
493sub realloc {
494 my ($self) = @_;
495
496 if ($self->{visible}) {
497 return if $self->{root}{realloc}{$self};
498
499 $self->{root}{realloc}{$self} = $self;
500 $self->{root}->update;
501 } else {
502 delete $self->{req_w};
503 }
504}
505
506sub update {
507 my ($self) = @_;
508
509 $self->{parent}->update
510 if $self->{parent};
511}
449 512
450sub draw { 513sub draw {
451 my ($self) = @_; 514 my ($self) = @_;
452 515
453 return unless $self->{h} && $self->{w}; 516 return unless $self->{h} && $self->{w};
491 my ($self) = @_; 554 my ($self) = @_;
492 555
493 warn "no draw defined for $self\n"; 556 warn "no draw defined for $self\n";
494} 557}
495 558
496sub find_widget {
497 my ($self, $x, $y) = @_;
498
499 return () unless $self->{can_events};
500
501 return $self
502 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
503 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
504
505 ()
506}
507
508sub set_parent {
509 my ($self, $parent) = @_;
510
511 Scalar::Util::weaken ($self->{parent} = $parent);
512
513 $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible};
514
515 $self->check_size;
516}
517
518sub check_size {
519 my ($self, $forced) = @_;
520
521 $self->{force_alloc} = 1 if $forced;
522 $CFClient::UI::ROOT->{check_size}{$self} = $self;
523}
524
525sub update {
526 my ($self) = @_;
527
528 $self->{parent}->update
529 if $self->{parent};
530}
531
532sub connect {
533 my ($self, $signal, $cb) = @_;
534
535 push @{ $self->{signal_cb}{$signal} }, $cb;
536}
537
538sub _emit {
539 my ($self, $signal, @args) = @_;
540
541 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
542}
543
544sub emit {
545 my ($self, $signal, @args) = @_;
546
547 $self->_emit ($signal, @args)
548 || $self->$signal (@args);
549}
550
551sub visibility_change {
552 #my ($self, $visible) = @_;
553}
554
555sub DESTROY { 559sub DESTROY {
556 my ($self) = @_; 560 my ($self) = @_;
557 561
558 delete $WIDGET{$self+0}; 562 delete $WIDGET{$self+0};
559 #$self->deactivate; 563 #$self->deactivate;
654 $self->{children} = [ 658 $self->{children} = [
655 sort { $a->{z} <=> $b->{z} } 659 sort { $a->{z} <=> $b->{z} }
656 @{$self->{children}}, @widgets 660 @{$self->{children}}, @widgets
657 ]; 661 ];
658 662
659 $self->check_size (1); 663 $self->realloc;
660 $self->update;
661} 664}
662 665
663sub children { 666sub children {
664 @{ $_[0]{children} } 667 @{ $_[0]{children} }
665} 668}
670 delete $child->{parent}; 673 delete $child->{parent};
671 $child->hide; 674 $child->hide;
672 675
673 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 676 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
674 677
675 $self->check_size (1); 678 $self->realloc;
676 $self->update;
677} 679}
678 680
679sub clear { 681sub clear {
680 my ($self) = @_; 682 my ($self) = @_;
681 683
685 for (@$children) { 687 for (@$children) {
686 delete $_->{parent}; 688 delete $_->{parent};
687 $_->hide; 689 $_->hide;
688 } 690 }
689 691
690 $self->check_size; 692 $self->realloc;
691 $self->update;
692} 693}
693 694
694sub find_widget { 695sub find_widget {
695 my ($self, $x, $y) = @_; 696 my ($self, $x, $y) = @_;
696 697
749sub size_request { 750sub size_request {
750 $_[0]{children}[0]->size_request 751 $_[0]{children}[0]->size_request
751} 752}
752 753
753sub size_allocate { 754sub size_allocate {
754 my ($self, $w, $h) = @_; 755 my ($self, $w, $h, $changed) = @_;
755 756
756 $self->{children}[0]->configure (0, 0, $w, $h); 757 $self->{children}[0]->configure (0, 0, $w, $h);
757} 758}
758 759
759############################################################################# 760#############################################################################
776 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 777 $ROOT->on_post_alloc ($self => sub { $self->render_child });
777 $self->SUPER::update; 778 $self->SUPER::update;
778} 779}
779 780
780sub size_allocate { 781sub size_allocate {
781 my ($self, $w, $h) = @_; 782 my ($self, $w, $h, $changed) = @_;
782 783
783 $self->SUPER::size_allocate ($w, $h); 784 $self->SUPER::size_allocate ($w, $h, $changed);
784 $self->update; 785 $self->update
786 if $changed;
785} 787}
786 788
787sub _render { 789sub _render {
788 $_[0]{children}[0]->draw; 790 $_[0]{children}[0]->draw;
789} 791}
842 844
843 ($w, $h) 845 ($w, $h)
844} 846}
845 847
846sub size_allocate { 848sub size_allocate {
847 my ($self, $w, $h) = @_; 849 my ($self, $w, $h, $changed) = @_;
848 850
849 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; 851 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
850 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; 852 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
851 853
852 $self->child->configure (0, 0, $w, $h); 854 $self->child->configure (0, 0, $w, $h);
939 my $child = $self->{vp}->child; 941 my $child = $self->{vp}->child;
940 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 942 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
941} 943}
942 944
943sub size_allocate { 945sub size_allocate {
944 my ($self, $w, $h) = @_; 946 my ($self, $w, $h, $changed) = @_;
945 947
946 $self->SUPER::size_allocate ($w, $h); 948 $self->SUPER::size_allocate ($w, $h, $changed);
947 949
948 my $child = $self->{vp}->child; 950 my $child = $self->{vp}->child;
949 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 951 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
950} 952}
951 953
998 1000
999our @ISA = CFClient::UI::Bin::; 1001our @ISA = CFClient::UI::Bin::;
1000 1002
1001use CFClient::OpenGL; 1003use CFClient::OpenGL;
1002 1004
1003my @tex = 1005my $bg =
1006 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1007 mipmap => 1, wrap => 1;
1008
1009my @border =
1004 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1010 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1005 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1011 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1006 1012
1007sub new { 1013sub new {
1008 my $class = shift; 1014 my $class = shift;
1009 1015
1010 my $self = $class->SUPER::new ( 1016 my $self = $class->SUPER::new (
1039 $h + $self->border * 2, 1045 $h + $self->border * 2,
1040 ) 1046 )
1041} 1047}
1042 1048
1043sub size_allocate { 1049sub size_allocate {
1044 my ($self, $w, $h) = @_; 1050 my ($self, $w, $h, $changed) = @_;
1051
1052 return unless $changed;
1045 1053
1046 $h -= List::Util::max 0, $self->border * 2; 1054 $h -= List::Util::max 0, $self->border * 2;
1047 $w -= List::Util::max 0, $self->border * 2; 1055 $w -= List::Util::max 0, $self->border * 2;
1048 1056
1049 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1057 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1078 $self->{user_x} = $wx + $dx * $mx; 1086 $self->{user_x} = $wx + $dx * $mx;
1079 $self->{user_y} = $wy + $dy * $my; 1087 $self->{user_y} = $wy + $dy * $my;
1080 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); 1088 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1081 $self->{def_h} = $bh + $dy * ($my ? -1 : 1); 1089 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1082 $self->move ($self->{user_x}, $self->{user_y}); 1090 $self->move ($self->{user_x}, $self->{user_y});
1083 $self->check_size; 1091 $self->realloc;
1084 }; 1092 };
1085 1093
1086 } elsif ($lr ^ $td) { 1094 } elsif ($lr ^ $td) {
1087 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1095 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1088 my ($bx, $by) = ($self->{x}, $self->{y}); 1096 my ($bx, $by) = ($self->{x}, $self->{y});
1094 1102
1095 $self->{user_x} = $bx + $x - $ox; 1103 $self->{user_x} = $bx + $x - $ox;
1096 $self->{user_y} = $by + $y - $oy; 1104 $self->{user_y} = $by + $y - $oy;
1097 1105
1098 $self->move ($self->{user_x}, $self->{user_y}); 1106 $self->move ($self->{user_x}, $self->{user_y});
1099 $self->update; 1107 $self->realloc;
1100 }; 1108 };
1101 } 1109 }
1102} 1110}
1103 1111
1104sub button_up { 1112sub button_up {
1123 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1131 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1124 1132
1125 my $border = $self->border; 1133 my $border = $self->border;
1126 1134
1127 glColor @{ $self->{border_bg} }; 1135 glColor @{ $self->{border_bg} };
1128 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1136 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1129 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1137 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1130 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1138 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1131 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1139 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1132 1140
1133 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1141 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1134 my $bg = $tex[0]; 1142 glColor @{ $self->{bg} };
1135 1143
1136 # TODO: repeat texture not scale 1144 # TODO: repeat texture not scale
1145 # solve this better(?)
1137 my $rep_x = $cw / $bg->{w}; 1146 $bg->{s} = $cw / $bg->{w};
1138 my $rep_y = $ch / $bg->{h}; 1147 $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); 1148 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1146 } 1149 }
1147 1150
1148 glDisable GL_TEXTURE_2D; 1151 glDisable GL_TEXTURE_2D;
1149 1152
1179 my ($self, $x, $y, $child) = @_; 1182 my ($self, $x, $y, $child) = @_;
1180 1183
1181 $child->set_parent ($self); 1184 $child->set_parent ($self);
1182 $self->{children}[$y][$x] = $child; 1185 $self->{children}[$y][$x] = $child;
1183 1186
1184 $self->check_size (1); 1187 $self->realloc;
1185} 1188}
1186 1189
1187# TODO: move to container class maybe? send children a signal on removal? 1190# TODO: move to container class maybe? send children a signal on removal?
1188sub clear { 1191sub clear {
1189 my ($self) = @_; 1192 my ($self) = @_;
1194 for (@children) { 1197 for (@children) {
1195 delete $_->{parent}; 1198 delete $_->{parent};
1196 $_->hide; 1199 $_->hide;
1197 } 1200 }
1198 1201
1199 $self->check_size (1); 1202 $self->realloc;
1200 $self->update;
1201} 1203}
1202 1204
1203sub get_wh { 1205sub get_wh {
1204 my ($self) = @_; 1206 my ($self) = @_;
1205 1207
1232 (sum @$hs), 1234 (sum @$hs),
1233 ) 1235 )
1234} 1236}
1235 1237
1236sub size_allocate { 1238sub size_allocate {
1237 my ($self, $w, $h) = @_; 1239 my ($self, $w, $h, $changed) = @_;
1238 1240
1239 my ($ws, $hs) = $self->get_wh; 1241 my ($ws, $hs) = $self->get_wh;
1240 1242
1241 my $req_w = (sum @$ws) || 1; 1243 my $req_w = (sum @$ws) || 1;
1242 my $req_h = (sum @$hs) || 1; 1244 my $req_h = (sum @$hs) || 1;
1320 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1322 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1321 ) 1323 )
1322} 1324}
1323 1325
1324sub size_allocate { 1326sub size_allocate {
1325 my ($self, $w, $h) = @_; 1327 my ($self, $w, $h, $changed) = @_;
1326 1328
1327 my $space = $self->{vertical} ? $h : $w; 1329 my $space = $self->{vertical} ? $h : $w;
1328 my $children = $self->{children}; 1330 my $children = $self->{children};
1329 1331
1330 my @req; 1332 my @req;
1459 $self->{text} = "T$text"; 1461 $self->{text} = "T$text";
1460 1462
1461 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1463 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1462 $self->{layout}->set_text ($text); 1464 $self->{layout}->set_text ($text);
1463 1465
1466 $self->realloc;
1464 $self->update; 1467 $self->update;
1465 $self->check_size;
1466} 1468}
1467 1469
1468sub set_markup { 1470sub set_markup {
1469 my ($self, $markup) = @_; 1471 my ($self, $markup) = @_;
1470 1472
1474 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1476 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1475 1477
1476 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1478 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1477 $self->{layout}->set_markup ($markup); 1479 $self->{layout}->set_markup ($markup);
1478 1480
1481 $self->realloc;
1479 $self->update; 1482 $self->update;
1480 $self->check_size;
1481} 1483}
1482 1484
1483sub size_request { 1485sub size_request {
1484 my ($self) = @_; 1486 my ($self) = @_;
1485 1487
1506 $h + $self->{padding} * 2, 1508 $h + $self->{padding} * 2,
1507 ) 1509 )
1508} 1510}
1509 1511
1510sub size_allocate { 1512sub size_allocate {
1511 my ($self, $w, $h) = @_; 1513 my ($self, $w, $h, $changed) = @_;
1512 1514
1513 delete $self->{texture}; 1515 delete $self->{texture}
1516 if $changed;
1514} 1517}
1515 1518
1516sub set_fontsize { 1519sub set_fontsize {
1517 my ($self, $fontsize) = @_; 1520 my ($self, $fontsize) = @_;
1518 1521
1519 $self->{fontsize} = $fontsize; 1522 $self->{fontsize} = $fontsize;
1520 delete $self->{texture}; 1523 delete $self->{texture};
1521 1524
1522 $self->update; 1525 $self->realloc;
1523 $self->check_size;
1524} 1526}
1525 1527
1526sub _draw { 1528sub _draw {
1527 my ($self) = @_; 1529 my ($self) = @_;
1528 1530
1608sub set_text { 1610sub set_text {
1609 my ($self, $text) = @_; 1611 my ($self, $text) = @_;
1610 1612
1611 $self->{cursor} = length $text; 1613 $self->{cursor} = length $text;
1612 $self->_set_text ($text); 1614 $self->_set_text ($text);
1613 $self->update; 1615
1614 $self->check_size; 1616 $self->realloc;
1615} 1617}
1616 1618
1617sub get_text { 1619sub get_text {
1618 $_[0]{text} 1620 $_[0]{text}
1619} 1621}
1652 } elsif ($uni) { 1654 } elsif ($uni) {
1653 substr $text, $self->{cursor}++, 0, chr $uni; 1655 substr $text, $self->{cursor}++, 0, chr $uni;
1654 } 1656 }
1655 1657
1656 $self->_set_text ($text); 1658 $self->_set_text ($text);
1657 $self->update; 1659
1658 $self->check_size; 1660 $self->realloc;
1659} 1661}
1660 1662
1661sub focus_in { 1663sub focus_in {
1662 my ($self) = @_; 1664 my ($self) = @_;
1663 1665
2170 $self->update; 2172 $self->update;
2171 2173
2172 $self 2174 $self
2173} 2175}
2174 2176
2177sub changed { }
2178
2175sub set_range { 2179sub set_range {
2176 my ($self, $range) = @_; 2180 my ($self, $range) = @_;
2177 2181
2178 ($range, $self->{range}) = ($self->{range}, $range); 2182 ($range, $self->{range}) = ($self->{range}, $range);
2179 2183
2368 $self->{fontsize} = $fontsize; 2372 $self->{fontsize} = $fontsize;
2369 $self->reflow; 2373 $self->reflow;
2370} 2374}
2371 2375
2372sub size_allocate { 2376sub size_allocate {
2373 my ($self, $w, $h) = @_; 2377 my ($self, $w, $h, $changed) = @_;
2374 2378
2375 $self->SUPER::size_allocate ($w, $h); 2379 $self->SUPER::size_allocate ($w, $h, $changed);
2380
2381 return unless $changed;
2376 2382
2377 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2383 $self->{layout}->set_font ($self->{font}) if $self->{font};
2378 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2384 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2379 $self->{layout}->set_width ($self->{children}[0]{w}); 2385 $self->{layout}->set_width ($self->{children}[0]{w});
2380 2386
2622 2628
2623 ($w + 4, $h + 4) 2629 ($w + 4, $h + 4)
2624} 2630}
2625 2631
2626sub size_allocate { 2632sub size_allocate {
2627 my ($self, $w, $h) = @_; 2633 my ($self, $w, $h, $changed) = @_;
2628 2634
2635 return unless $changed;
2636
2629 $self->SUPER::size_allocate ($w - 4, $h - 4); 2637 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2638}
2639
2640sub visibility_change {
2641 my ($self, $visible) = @_;
2642
2643 return unless $visible;
2644
2645 $self->{root}->on_post_alloc ("move_$self" => sub {
2646 my $widget = $self->{owner}
2647 or return;
2648
2649 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2650
2651 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2652 if $x + $self->{w} > $::WIDTH;
2653
2654 $self->move ($x, $y);
2655 });
2630} 2656}
2631 2657
2632sub _draw { 2658sub _draw {
2633 my ($self) = @_; 2659 my ($self) = @_;
2634 2660
2651 glVertex $w, $h; 2677 glVertex $w, $h;
2652 glVertex $w, 0; 2678 glVertex $w, 0;
2653 glEnd; 2679 glEnd;
2654 2680
2655 glTranslate 2 - 0.375, 2 - 0.375; 2681 glTranslate 2 - 0.375, 2 - 0.375;
2682
2656 $self->SUPER::_draw; 2683 $self->SUPER::_draw;
2657} 2684}
2658 2685
2659############################################################################# 2686#############################################################################
2660 2687
2969use CFClient::OpenGL; 2996use CFClient::OpenGL;
2970 2997
2971sub new { 2998sub new {
2972 my $class = shift; 2999 my $class = shift;
2973 3000
2974 $class->SUPER::new ( 3001 my $self = $class->SUPER::new (
2975 visible => 1, 3002 visible => 1,
2976 @_, 3003 @_,
2977 ) 3004 );
3005
3006 Scalar::Util::weaken ($self->{root} = $self);
3007
3008 $self
2978} 3009}
2979 3010
2980sub configure { 3011sub configure {
2981 my ($self, $x, $y, $w, $h) = @_; 3012 my ($self, $x, $y, $w, $h) = @_;
2982 3013
2983 $self->{w} = $w; 3014 $self->{w} = $w;
2984 $self->{h} = $h; 3015 $self->{h} = $h;
2985} 3016}
2986 3017
2987sub check_size { 3018sub reconfigure {
2988 my ($self) = @_; 3019 my ($self) = @_;
2989 3020
3021 $self->SUPER::reconfigure;
3022
2990 $self->size_allocate ($self->{w}, $self->{h}) 3023 $self->size_allocate ($self->{w}, $self->{h}, 1)
2991 if $self->{w}; 3024 if $self->{w};
2992} 3025}
2993 3026
2994sub size_request { 3027sub size_request {
2995 my ($self) = @_; 3028 my ($self) = @_;
2996 3029
2997 ($self->{w}, $self->{h}) 3030 ($self->{w}, $self->{h})
2998} 3031}
2999 3032
3000sub size_allocate { 3033sub size_allocate {
3001 my ($self, $w, $h) = @_; 3034 my ($self, $w, $h, $changed) = @_;
3002 3035
3003 for my $child ($self->children) { 3036 for my $child ($self->children) {
3004 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3037 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3005 3038
3006 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3039 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3007 if exists $child->{req_x}; 3040 if exists $child->{req_x};
3008 3041
3009 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3042 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3010 if exists $child->{req_y}; 3043 if exists $child->{req_y};
3011 3044
3012 delete @$child{qw(req_x req_y)}; 3045 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3013 3046
3014 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3047 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3015 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3048 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3016 3049
3017 $child->configure ($X, $Y, $W, $H); 3050 $child->configure ($X, $Y, $W, $H);
3031} 3064}
3032 3065
3033sub update { 3066sub update {
3034 my ($self) = @_; 3067 my ($self) = @_;
3035 3068
3036 $self->check_size;
3037 $::WANT_REFRESH++; 3069 $::WANT_REFRESH++;
3038} 3070}
3039 3071
3040sub add { 3072sub add {
3041 my ($self, @children) = @_; 3073 my ($self, @children) = @_;
3047 $child->{x} = int $child->{x}; 3079 $child->{x} = int $child->{x};
3048 $child->{y} = int $child->{y}; 3080 $child->{y} = int $child->{y};
3049 } 3081 }
3050 3082
3051 $self->SUPER::add (@children); 3083 $self->SUPER::add (@children);
3052
3053 for (my @widgets = @children; my $w = pop @widgets; ) {
3054 push @widgets, $w->children;
3055 $w->set_visible;
3056 }
3057
3058} 3084}
3059 3085
3060sub remove { 3086sub remove {
3061 my ($self, @children) = @_; 3087 my ($self, @children) = @_;
3062 3088
3087 while ($self->{refresh_hook}) { 3113 while ($self->{refresh_hook}) {
3088 $_->() 3114 $_->()
3089 for values %{delete $self->{refresh_hook}}; 3115 for values %{delete $self->{refresh_hook}};
3090 } 3116 }
3091 3117
3092 if ($self->{check_size}) { 3118 if ($self->{realloc}) {
3093 my @queue; 3119 my @queue;
3094 3120
3095 for (;;) { 3121 while () {
3096 if ($self->{check_size}) { 3122 if ($self->{realloc}) {
3097 #TODO use array-of-depth approach 3123 #TODO use array-of-depth approach
3098 3124
3099 @queue = sort { $a->{visible} <=> $b->{visible} } 3125 @queue = sort { $a->{visible} <=> $b->{visible} }
3100 @queue, values %{delete $self->{check_size}}; 3126 @queue, values %{delete $self->{realloc}};
3101 } 3127 }
3102 3128
3103 my $widget = pop @queue || last; 3129 my $widget = pop @queue || last;
3104 3130
3105 defined $widget->{visible} or last; # do not resize invisible widgets 3131 $widget->{visible} or last; # do not resize invisible widgets
3106 3132
3107 my ($w, $h) = $widget->{def_w} && $widget->{def_h} 3133 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3108 ? @$widget{qw(def_w def_h)} 3134 ? @$widget{qw(def_w def_h)}
3109 : $widget->size_request; 3135 : $widget->size_request;
3110 3136
3111 if (delete $widget->{force_alloc}
3112 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3113 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3137 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3114 3138
3115 $widget->{req_w} = $w; 3139 $widget->{req_w} = $w;
3116 $widget->{req_h} = $h; 3140 $widget->{req_h} = $h;
3117 3141
3118 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3142 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
3119 3143
3120 $widget->{parent}->check_size 3144 push @queue, $widget->{parent}
3121 if $widget->{parent}; 3145 if $widget->{parent};
3122 }
3123 } 3146 }
3124 } 3147 }
3125 3148
3126 while ($self->{size_alloc}) { 3149 while (my $size_alloc = delete $self->{size_alloc}) {
3127 for (values %{delete $self->{size_alloc}}) { 3150 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3128 my ($widget, $w, $h) = @$_; 3151 values %$size_alloc;
3152
3153 while () {
3154 my ($widget, $w, $h) = @{ pop @queue or last };
3129 3155
3130 $w = 0 if $w < 0; 3156 $w = 0 if $w < 0;
3131 $h = 0 if $h < 0; 3157 $h = 0 if $h < 0;
3132 3158
3159 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3160
3133 $widget->{w} = $w; 3161 $widget->{w} = $w;
3134 $widget->{h} = $h; 3162 $widget->{h} = $h;
3163
3135 $widget->emit (size_allocate => $w, $h); 3164 $widget->emit (size_allocate => $w, $h, $changed);
3136 } 3165 }
3137 } 3166 }
3138 3167
3139 while ($self->{post_alloc_hook}) { 3168 while ($self->{post_alloc_hook}) {
3140 $_->() 3169 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines