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.252 by root, Sun May 28 23:21:28 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
53 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
54
55 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
56 if $x + $TOOLTIP->{w} > $::WIDTH;
57
58 $TOOLTIP->move ($x, $y);
59 $TOOLTIP->show; 59 $TOOLTIP->show;
60 } 60 }
61 61
62 return; 62 return;
63 } 63 }
170sub rescale_widgets { 170sub rescale_widgets {
171 my ($sx, $sy) = @_; 171 my ($sx, $sy) = @_;
172 172
173 for my $widget (values %WIDGET) { 173 for my $widget (values %WIDGET) {
174 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
175 $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.]+$/;
176 $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};
177 $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};
178 $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.]+$/;
179 $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};
180 $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
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
181 } 188 }
182 } 189 }
183 190
184 reconfigure_widgets; 191 reconfigure_widgets;
185} 192}
194 201
195sub new { 202sub new {
196 my $class = shift; 203 my $class = shift;
197 204
198 my $self = bless { 205 my $self = bless {
199 x => 0, 206 x => "center",
200 y => 0, 207 y => "center",
201 z => 0, 208 z => 0,
209 w => undef,
210 h => undef,
202 can_events => 1, 211 can_events => 1,
203 @_ 212 @_
204 }, $class; 213 }, $class;
214
215 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
205 216
206 for (keys %$self) { 217 for (keys %$self) {
207 if (/^on_(.*)$/) { 218 if (/^on_(.*)$/) {
208 $self->connect ($1 => delete $self->{$_}); 219 $self->connect ($1 => delete $self->{$_});
209 } 220 }
210 } 221 }
211 222
212 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
213
214 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { 223 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
215 $self->{req_x} = $layout->{x} * $::WIDTH; 224 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x};
216 $self->{req_y} = $layout->{y} * $::HEIGHT; 225 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y};
217 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; 226 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w};
218 $self->{def_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};
219 } 233 }
220 234
221 $self 235 $self
222} 236}
223 237
232 my ($self) = @_; 246 my ($self) = @_;
233 247
234 return if $self->{parent}; 248 return if $self->{parent};
235 249
236 $CFClient::UI::ROOT->add ($self); 250 $CFClient::UI::ROOT->add ($self);
237}
238
239sub center {
240 my ($self) = @_;
241
242 $CFClient::UI::ROOT->on_post_alloc (
243 "center_$self" => sub {
244 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
245 },
246 );
247
248 $self->update;
249} 251}
250 252
251sub set_visible { 253sub set_visible {
252 my ($self) = @_; 254 my ($self) = @_;
253 255
308 310
309 $self->{parent}->remove ($self) 311 $self->{parent}->remove ($self)
310 if $self->{parent}; 312 if $self->{parent};
311} 313}
312 314
313sub move { 315sub move_abs {
314 my ($self, $x, $y, $z) = @_; 316 my ($self, $x, $y, $z) = @_;
315 317
316 $self->{x} = int $x; 318 $self->{x} = List::Util::max 0, int $x;
317 $self->{y} = int $y; 319 $self->{y} = List::Util::max 0, int $y;
318 $self->{z} = $z if defined $z; 320 $self->{z} = $z if defined $z;
319 321
320 $self->update; 322 $self->update;
321} 323}
322 324
323sub set_size { 325sub set_size {
324 my ($self, $w, $h) = @_; 326 my ($self, $w, $h) = @_;
325 327
326 $self->{def_w} = $w; 328 $self->{force_w} = $w;
327 $self->{def_h} = $h; 329 $self->{force_h} = $h;
328 330
329 $self->realloc; 331 $self->realloc;
330} 332}
331 333
332sub size_request { 334sub size_request {
336 338
337sub configure { 339sub configure {
338 my ($self, $x, $y, $w, $h) = @_; 340 my ($self, $x, $y, $w, $h) = @_;
339 341
340 if ($self->{aspect}) { 342 if ($self->{aspect}) {
343 my ($ow, $oh) = ($w, $h);
344
341 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 345 $w = List::Util::min $w, int $h * $self->{aspect};
342 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 346 $h = List::Util::min $h, int $w / $self->{aspect};
343 347
344 # use alignment to adjust x, y 348 # use alignment to adjust x, y
345 349
346 $x += int +($w - $w2) * 0.5; 350 $x += int 0.5 * ($ow - $w);
347 $y += int +($h - $h2) * 0.5; 351 $y += int 0.5 * ($oh - $h);
348
349 ($w, $h) = ($w2, $h2);
350 } 352 }
351 353
352 if ($self->{x} != $x || $self->{y} != $y) { 354 if ($self->{x} ne $x || $self->{y} ne $y) {
353 $self->{x} = $x; 355 $self->{x} = $x;
354 $self->{y} = $y; 356 $self->{y} = $y;
355 $self->update; 357 $self->update;
356 } 358 }
357 359
466 468
467sub set_parent { 469sub set_parent {
468 my ($self, $parent) = @_; 470 my ($self, $parent) = @_;
469 471
470 Scalar::Util::weaken ($self->{parent} = $parent); 472 Scalar::Util::weaken ($self->{parent} = $parent);
471
472 $self->set_visible if $parent->{visible}; 473 $self->set_visible if $parent->{visible};
473} 474}
474 475
475sub connect { 476sub connect {
476 my ($self, $signal, $cb) = @_; 477 my ($self, $signal, $cb) = @_;
511sub update { 512sub update {
512 my ($self) = @_; 513 my ($self) = @_;
513 514
514 $self->{parent}->update 515 $self->{parent}->update
515 if $self->{parent}; 516 if $self->{parent};
517}
518
519sub reconfigure {
520 my ($self) = @_;
521
522 $self->realloc;
523 $self->update;
516} 524}
517 525
518sub draw { 526sub draw {
519 my ($self) = @_; 527 my ($self) = @_;
520 528
538 glVertex $x , $y + $self->{h}; 546 glVertex $x , $y + $self->{h};
539 glEnd; 547 glEnd;
540 glDisable GL_BLEND; 548 glDisable GL_BLEND;
541 } 549 }
542 550
543 if ($ENV{PCLIENT_DEBUG}) { 551 if ($ENV{CFPLUS_DEBUG}) {
544 glPushMatrix; 552 glPushMatrix;
545 glColor 1, 1, 0, 1; 553 glColor 1, 1, 0, 1;
546 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 554 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
547 glBegin GL_LINE_LOOP; 555 glBegin GL_LINE_LOOP;
548 glVertex 0 , 0; 556 glVertex 0 , 0;
624 my ($class, %arg) = @_; 632 my ($class, %arg) = @_;
625 $class->SUPER::new (can_events => 0, %arg); 633 $class->SUPER::new (can_events => 0, %arg);
626} 634}
627 635
628sub size_request { 636sub size_request {
629 (0, 0) 637 my ($self) = @_;
638
639 ($self->{w} + 0, $self->{h} + 0)
630} 640}
631 641
632sub draw { } 642sub draw { }
633 643
634############################################################################# 644#############################################################################
1005 1015
1006our @ISA = CFClient::UI::Bin::; 1016our @ISA = CFClient::UI::Bin::;
1007 1017
1008use CFClient::OpenGL; 1018use CFClient::OpenGL;
1009 1019
1010my @tex = 1020my $bg =
1021 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1022 mipmap => 1, wrap => 1;
1023
1024my @border =
1011 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1025 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1012 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);
1013 1027
1014sub new { 1028sub new {
1015 my $class = shift; 1029 my $class = shift;
1016 1030
1017 my $self = $class->SUPER::new ( 1031 my $self = $class->SUPER::new (
1018 bg => [1, 1, 1, 1], 1032 bg => [1, 1, 1, 1],
1019 border_bg => [1, 1, 1, 1], 1033 border_bg => [1, 1, 1, 1],
1020 border => 0.6, 1034 border => 0.6,
1021 is_toplevel => 1,
1022 can_events => 1, 1035 can_events => 1,
1036 min_w => 16,
1037 min_h => 16,
1023 @_ 1038 @_
1024 ); 1039 );
1025 1040
1026 $self->{title} &&= new CFClient::UI::Label 1041 $self->{title} &&= new CFClient::UI::Label
1027 align => 0, 1042 align => 0,
1082 my ($ev, $x, $y) = @_; 1097 my ($ev, $x, $y) = @_;
1083 1098
1084 my $dx = $ev->{x} - $ox; 1099 my $dx = $ev->{x} - $ox;
1085 my $dy = $ev->{y} - $oy; 1100 my $dy = $ev->{y} - $oy;
1086 1101
1087 $self->{user_x} = $wx + $dx * $mx;
1088 $self->{user_y} = $wy + $dy * $my;
1089 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); 1102 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1090 $self->{def_h} = $bh + $dy * ($my ? -1 : 1); 1103 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1091 $self->move ($self->{user_x}, $self->{user_y}); 1104
1092 $self->realloc; 1105 $self->realloc;
1106 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1093 }; 1107 };
1094 1108
1095 } elsif ($lr ^ $td) { 1109 } elsif ($lr ^ $td) {
1096 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1110 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1097 my ($bx, $by) = ($self->{x}, $self->{y}); 1111 my ($bx, $by) = ($self->{x}, $self->{y});
1099 $self->{motion} = sub { 1113 $self->{motion} = sub {
1100 my ($ev, $x, $y) = @_; 1114 my ($ev, $x, $y) = @_;
1101 1115
1102 ($x, $y) = ($ev->{x}, $ev->{y}); 1116 ($x, $y) = ($ev->{x}, $ev->{y});
1103 1117
1104 $self->{user_x} = $bx + $x - $ox; 1118 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1105 $self->{user_y} = $by + $y - $oy;
1106
1107 $self->move ($self->{user_x}, $self->{user_y});
1108 $self->realloc;
1109 }; 1119 };
1110 } 1120 }
1111} 1121}
1112 1122
1113sub button_up { 1123sub button_up {
1132 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1142 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1133 1143
1134 my $border = $self->border; 1144 my $border = $self->border;
1135 1145
1136 glColor @{ $self->{border_bg} }; 1146 glColor @{ $self->{border_bg} };
1137 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1147 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1138 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1148 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1139 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1149 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1140 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1150 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1141 1151
1142 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1152 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1143 my $bg = $tex[0]; 1153 glColor @{ $self->{bg} };
1144 1154
1145 # TODO: repeat texture not scale 1155 # TODO: repeat texture not scale
1156 # solve this better(?)
1146 my $rep_x = $cw / $bg->{w}; 1157 $bg->{s} = $cw / $bg->{w};
1147 my $rep_y = $ch / $bg->{h}; 1158 $bg->{t} = $ch / $bg->{h};
1148
1149 glColor @{ $self->{bg} };
1150
1151 $bg->{s} = $rep_x;
1152 $bg->{t} = $rep_y;
1153 $bg->{wrap_mode} = 1;
1154 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1159 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1155 } 1160 }
1156 1161
1157 glDisable GL_TEXTURE_2D; 1162 glDisable GL_TEXTURE_2D;
1158 1163
1421 ellipsise => 3, # end 1426 ellipsise => 3, # end
1422 layout => (new CFClient::Layout), 1427 layout => (new CFClient::Layout),
1423 fontsize => 1, 1428 fontsize => 1,
1424 align => -1, 1429 align => -1,
1425 valign => -1, 1430 valign => -1,
1426 padding => 2, 1431 padding_x => 2,
1432 padding_y => 2,
1427 can_events => 0, 1433 can_events => 0,
1428 %arg 1434 %arg
1429 ); 1435 );
1430 1436
1431 if (exists $self->{template}) { 1437 if (exists $self->{template}) {
1507 1513
1508 $w = List::Util::max $w, $w2; 1514 $w = List::Util::max $w, $w2;
1509 $h = List::Util::max $h, $h2; 1515 $h = List::Util::max $h, $h2;
1510 } 1516 }
1511 1517
1512 ( 1518 ($w, $h)
1513 $w + $self->{padding} * 2,
1514 $h + $self->{padding} * 2,
1515 )
1516} 1519}
1517 1520
1518sub size_allocate { 1521sub size_allocate {
1519 my ($self, $w, $h, $changed) = @_; 1522 my ($self, $w, $h, $changed) = @_;
1520 1523
1544 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1547 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1545 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1548 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1546 1549
1547 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1550 my $tex = new_from_layout CFClient::Texture $self->{layout};
1548 1551
1549 $self->{ox} = int ($self->{align} < 0 ? $self->{padding} 1552 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1550 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1553 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x}
1551 : ($self->{w} - $tex->{w}) * 0.5); 1554 : ($self->{w} - $tex->{w}) * 0.5);
1552 1555
1553 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} 1556 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1554 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1557 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1555 : ($self->{h} - $tex->{h}) * 0.5); 1558 : ($self->{h} - $tex->{h}) * 0.5);
1556 1559
1557 $tex 1560 $tex
1558 }; 1561 };
1559 1562
1798 1801
1799sub new { 1802sub new {
1800 my $class = shift; 1803 my $class = shift;
1801 1804
1802 $class->SUPER::new ( 1805 $class->SUPER::new (
1803 padding => 4, 1806 padding_x => 4,
1807 padding_y => 4,
1804 fg => [1, 1, 1], 1808 fg => [1, 1, 1],
1805 active_fg => [0, 0, 1], 1809 active_fg => [0, 0, 1],
1806 can_hover => 1, 1810 can_hover => 1,
1807 align => 0, 1811 align => 0,
1808 valign => 0, 1812 valign => 0,
1855 1859
1856sub new { 1860sub new {
1857 my $class = shift; 1861 my $class = shift;
1858 1862
1859 $class->SUPER::new ( 1863 $class->SUPER::new (
1860 padding => 2, 1864 padding_x => 2,
1865 padding_y => 2,
1861 fg => [1, 1, 1], 1866 fg => [1, 1, 1],
1862 active_fg => [1, 1, 0], 1867 active_fg => [1, 1, 0],
1863 bg => [0, 0, 0, 0.2], 1868 bg => [0, 0, 0, 0.2],
1864 active_bg => [1, 1, 1, 0.5], 1869 active_bg => [1, 1, 1, 0.5],
1865 state => 0, 1870 state => 0,
1869} 1874}
1870 1875
1871sub size_request { 1876sub size_request {
1872 my ($self) = @_; 1877 my ($self) = @_;
1873 1878
1874 ($self->{padding} * 2 + 6) x 2 1879 (6) x 2
1875} 1880}
1876 1881
1877sub button_down { 1882sub button_down {
1878 my ($self, $ev, $x, $y) = @_; 1883 my ($self, $ev, $x, $y) = @_;
1879 1884
1880 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1885 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1881 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1886 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1882 $self->{state} = !$self->{state}; 1887 $self->{state} = !$self->{state};
1883 $self->_emit (changed => $self->{state}); 1888 $self->_emit (changed => $self->{state});
1884 } 1889 }
1885} 1890}
1886 1891
1887sub _draw { 1892sub _draw {
1888 my ($self) = @_; 1893 my ($self) = @_;
1889 1894
1890 $self->SUPER::_draw; 1895 $self->SUPER::_draw;
1891 1896
1892 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 1897 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
1893 1898
1894 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;
1895 1902
1896 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1903 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1897 1904
1898 my $tex = $self->{state} ? $tex[1] : $tex[0]; 1905 my $tex = $self->{state} ? $tex[1] : $tex[0];
1899 1906
2164 fg => [1, 1, 1], 2171 fg => [1, 1, 1],
2165 active_fg => [0, 0, 0], 2172 active_fg => [0, 0, 0],
2166 bg => [0, 0, 0, 0.2], 2173 bg => [0, 0, 0, 0.2],
2167 active_bg => [1, 1, 1, 0.5], 2174 active_bg => [1, 1, 1, 0.5],
2168 range => [0, 0, 100, 10, 0], 2175 range => [0, 0, 100, 10, 0],
2169 req_w => $::WIDTH / 80, 2176 min_w => $::WIDTH / 80,
2170 req_h => $::WIDTH / 80, 2177 min_h => $::WIDTH / 80,
2171 vertical => 0, 2178 vertical => 0,
2172 can_hover => 1, 2179 can_hover => 1,
2173 inner_pad => 0.02, 2180 inner_pad => 0.02,
2174 @_ 2181 @_
2175 ); 2182 );
2215} 2222}
2216 2223
2217sub size_request { 2224sub size_request {
2218 my ($self) = @_; 2225 my ($self) = @_;
2219 2226
2220 my $w = $self->{req_w}; 2227 ($self->{req_w}, $self->{req_h})
2221 my $h = $self->{req_h};
2222
2223 $self->{vertical} ? ($h, $w) : ($w, $h)
2224} 2228}
2225 2229
2226sub button_down { 2230sub button_down {
2227 my ($self, $ev, $x, $y) = @_; 2231 my ($self, $ev, $x, $y) = @_;
2228 2232
2639 my ($self, $w, $h, $changed) = @_; 2643 my ($self, $w, $h, $changed) = @_;
2640 2644
2641 return unless $changed; 2645 return unless $changed;
2642 2646
2643 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed); 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 });
2644} 2666}
2645 2667
2646sub _draw { 2668sub _draw {
2647 my ($self) = @_; 2669 my ($self) = @_;
2648 2670
2840 # maybe save $GRAB? must be careful about events... 2862 # maybe save $GRAB? must be careful about events...
2841 $GRAB = $self; 2863 $GRAB = $self;
2842 $self->{button} = $ev->{button}; 2864 $self->{button} = $ev->{button};
2843 2865
2844 $self->show; 2866 $self->show;
2845 $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);
2846} 2868}
2847 2869
2848sub mouse_motion { 2870sub mouse_motion {
2849 my ($self, $ev, $x, $y) = @_; 2871 my ($self, $ev, $x, $y) = @_;
2850 2872
3016 my ($self) = @_; 3038 my ($self) = @_;
3017 3039
3018 ($self->{w}, $self->{h}) 3040 ($self->{w}, $self->{h})
3019} 3041}
3020 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
3021sub size_allocate { 3057sub size_allocate {
3022 my ($self, $w, $h, $changed) = @_; 3058 my ($self, $w, $h, $changed) = @_;
3023 3059
3024 for my $child ($self->children) { 3060 for my $child ($self->children) {
3025 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)};
3026 3062
3027 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3063 $X = $child->{force_x} if exists $child->{force_x};
3028 if exists $child->{req_x}; 3064 $Y = $child->{force_y} if exists $child->{force_y};
3029 3065
3030 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3066 $X = _to_pixel $X, $W, $self->{w};
3031 if exists $child->{req_y}; 3067 $Y = _to_pixel $Y, $H, $self->{h};
3032
3033 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3034
3035 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3036 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3037 3068
3038 $child->configure ($X, $Y, $W, $H); 3069 $child->configure ($X, $Y, $W, $H);
3039 } 3070 }
3040} 3071}
3041 3072
3058} 3089}
3059 3090
3060sub add { 3091sub add {
3061 my ($self, @children) = @_; 3092 my ($self, @children) = @_;
3062 3093
3063 for my $child (@children) {
3064 $child->{is_toplevel} = 1; 3094 $_->{is_toplevel} = 1
3065 3095 for @children;
3066 # integerise window positions
3067 $child->{x} = int $child->{x};
3068 $child->{y} = int $child->{y};
3069 }
3070 3096
3071 $self->SUPER::add (@children); 3097 $self->SUPER::add (@children);
3072} 3098}
3073 3099
3074sub remove { 3100sub remove {
3075 my ($self, @children) = @_; 3101 my ($self, @children) = @_;
3076 3102
3077 $self->SUPER::remove (@children); 3103 $self->SUPER::remove (@children);
3104
3105 delete $self->{is_toplevel}
3106 for @children;
3078 3107
3079 while (@children) { 3108 while (@children) {
3080 my $w = pop @children; 3109 my $w = pop @children;
3081 push @children, $w->children; 3110 push @children, $w->children;
3082 $w->set_invisible; 3111 $w->set_invisible;
3116 3145
3117 my $widget = pop @queue || last; 3146 my $widget = pop @queue || last;
3118 3147
3119 $widget->{visible} or last; # do not resize invisible widgets 3148 $widget->{visible} or last; # do not resize invisible widgets
3120 3149
3121 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3122 ? @$widget{qw(def_w def_h)}
3123 : $widget->size_request; 3150 my ($w, $h) = $widget->size_request;
3124 3151
3125 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3152 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3126 3153 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3154
3155 $w = $widget->{force_w} if exists $widget->{force_w};
3156 $h = $widget->{force_h} if exists $widget->{force_h};
3157
3127 $widget->{req_w} = $w; 3158 $widget->{req_w} = $w;
3128 $widget->{req_h} = $h; 3159 $widget->{req_h} = $h;
3129 3160
3130 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3161 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3131 3162
3132 push @queue, $widget->{parent} 3163 push @queue, $widget->{parent}
3133 if $widget->{parent}; 3164 if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent};
3134 } 3165 }
3135 } 3166 }
3136 3167
3137 while (my $size_alloc = delete $self->{size_alloc}) { 3168 while (my $size_alloc = delete $self->{size_alloc}) {
3138 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible}, 3169 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3139 values %$size_alloc; 3170 values %$size_alloc;
3140 3171
3141 while () { 3172 while () {
3142 my ($widget, $w, $h) = @{ pop @queue or last }; 3173 my ($widget, $w, $h) = @{ pop @queue or last };
3143 3174
3175 $w = $widget->{w} || $widget->{req_w} unless defined $w;
3176 $h = $widget->{h} || $widget->{req_h} unless defined $h;
3177
3144 $w = 0 if $w < 0; 3178 $w = 0 if $w < 0;
3145 $h = 0 if $h < 0; 3179 $h = 0 if $h < 0;
3180
3181 $w = int $w + 0.5;
3182 $h = int $h + 0.5;
3146 3183
3147 my $changed = $widget->{w} != $w || $widget->{h} != $h; 3184 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3148 3185
3149 $widget->{w} = $w; 3186 $widget->{w} = $w;
3150 $widget->{h} = $h; 3187 $widget->{h} = $h;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines