… | |
… | |
17 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
18 | |
18 | |
19 | our %WIDGET; # all widgets, weak-referenced |
19 | our %WIDGET; # all widgets, weak-referenced |
20 | |
20 | |
21 | sub get_layout { |
21 | sub 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 | |
34 | sub set_layout { |
38 | sub set_layout { |
35 | my ($layout) = @_; |
39 | my ($layout) = @_; |
|
|
40 | |
36 | $LAYOUT = $layout; |
41 | $LAYOUT = $layout; |
37 | } |
42 | } |
38 | |
43 | |
39 | sub check_tooltip { |
44 | sub check_tooltip { |
40 | if (!$GRAB) { |
45 | if (!$GRAB) { |
… | |
… | |
165 | sub rescale_widgets { |
170 | sub rescale_widgets { |
166 | my ($sx, $sy) = @_; |
171 | my ($sx, $sy) = @_; |
167 | |
172 | |
168 | for my $widget (values %WIDGET) { |
173 | for my $widget (values %WIDGET) { |
169 | 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 | |
170 | $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.]+$/; |
171 | $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}; |
172 | $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}; |
173 | $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.]+$/; |
174 | $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}; |
175 | $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 | |
176 | } |
188 | } |
177 | } |
189 | } |
178 | |
190 | |
179 | reconfigure_widgets; |
191 | reconfigure_widgets; |
180 | } |
192 | } |
… | |
… | |
189 | |
201 | |
190 | sub new { |
202 | sub new { |
191 | my $class = shift; |
203 | my $class = shift; |
192 | |
204 | |
193 | my $self = bless { |
205 | my $self = bless { |
194 | x => 0, |
206 | x => "center", |
195 | y => 0, |
207 | y => "center", |
196 | z => 0, |
208 | z => 0, |
|
|
209 | w => undef, |
|
|
210 | h => undef, |
197 | can_events => 1, |
211 | can_events => 1, |
198 | @_ |
212 | @_ |
199 | }, $class; |
213 | }, $class; |
|
|
214 | |
|
|
215 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
200 | |
216 | |
201 | for (keys %$self) { |
217 | for (keys %$self) { |
202 | if (/^on_(.*)$/) { |
218 | if (/^on_(.*)$/) { |
203 | $self->connect ($1 => delete $self->{$_}); |
219 | $self->connect ($1 => delete $self->{$_}); |
204 | } |
220 | } |
205 | } |
221 | } |
206 | |
222 | |
207 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
208 | |
|
|
209 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
223 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
210 | $self->{req_x} = $layout->{x} * $::WIDTH; |
224 | $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x}; |
211 | $self->{req_y} = $layout->{y} * $::HEIGHT; |
225 | $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y}; |
212 | $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; |
226 | $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w}; |
213 | $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}; |
214 | } |
233 | } |
215 | |
234 | |
216 | $self |
235 | $self |
217 | } |
236 | } |
218 | |
237 | |
… | |
… | |
227 | my ($self) = @_; |
246 | my ($self) = @_; |
228 | |
247 | |
229 | return if $self->{parent}; |
248 | return if $self->{parent}; |
230 | |
249 | |
231 | $CFClient::UI::ROOT->add ($self); |
250 | $CFClient::UI::ROOT->add ($self); |
232 | } |
|
|
233 | |
|
|
234 | sub center { |
|
|
235 | my ($self) = @_; |
|
|
236 | |
|
|
237 | $CFClient::UI::ROOT->on_post_alloc ( |
|
|
238 | "center_$self" => sub { |
|
|
239 | $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5); |
|
|
240 | }, |
|
|
241 | ); |
|
|
242 | |
|
|
243 | $self->update; |
|
|
244 | } |
251 | } |
245 | |
252 | |
246 | sub set_visible { |
253 | sub set_visible { |
247 | my ($self) = @_; |
254 | my ($self) = @_; |
248 | |
255 | |
… | |
… | |
303 | |
310 | |
304 | $self->{parent}->remove ($self) |
311 | $self->{parent}->remove ($self) |
305 | if $self->{parent}; |
312 | if $self->{parent}; |
306 | } |
313 | } |
307 | |
314 | |
308 | sub move { |
315 | sub move_abs { |
309 | my ($self, $x, $y, $z) = @_; |
316 | my ($self, $x, $y, $z) = @_; |
310 | |
317 | |
311 | $self->{x} = int $x; |
318 | $self->{x} = List::Util::max 0, int $x; |
312 | $self->{y} = int $y; |
319 | $self->{y} = List::Util::max 0, int $y; |
313 | $self->{z} = $z if defined $z; |
320 | $self->{z} = $z if defined $z; |
314 | |
321 | |
315 | $self->update; |
322 | $self->update; |
316 | } |
323 | } |
317 | |
324 | |
318 | sub set_size { |
325 | sub set_size { |
319 | my ($self, $w, $h) = @_; |
326 | my ($self, $w, $h) = @_; |
320 | |
327 | |
321 | $self->{def_w} = $w; |
328 | $self->{force_w} = $w; |
322 | $self->{def_h} = $h; |
329 | $self->{force_h} = $h; |
323 | |
330 | |
324 | $self->realloc; |
331 | $self->realloc; |
325 | } |
332 | } |
326 | |
333 | |
327 | sub size_request { |
334 | sub size_request { |
… | |
… | |
331 | |
338 | |
332 | sub configure { |
339 | sub configure { |
333 | my ($self, $x, $y, $w, $h) = @_; |
340 | my ($self, $x, $y, $w, $h) = @_; |
334 | |
341 | |
335 | if ($self->{aspect}) { |
342 | if ($self->{aspect}) { |
|
|
343 | my ($ow, $oh) = ($w, $h); |
|
|
344 | |
336 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
345 | $w = List::Util::min $w, int $h * $self->{aspect}; |
337 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
346 | $h = List::Util::min $h, int $w / $self->{aspect}; |
338 | |
347 | |
339 | # use alignment to adjust x, y |
348 | # use alignment to adjust x, y |
340 | |
349 | |
341 | $x += int +($w - $w2) * 0.5; |
350 | $x += int 0.5 * ($ow - $w); |
342 | $y += int +($h - $h2) * 0.5; |
351 | $y += int 0.5 * ($oh - $h); |
343 | |
|
|
344 | ($w, $h) = ($w2, $h2); |
|
|
345 | } |
352 | } |
346 | |
353 | |
347 | if ($self->{x} != $x || $self->{y} != $y) { |
354 | if ($self->{x} ne $x || $self->{y} ne $y) { |
348 | $self->{x} = $x; |
355 | $self->{x} = $x; |
349 | $self->{y} = $y; |
356 | $self->{y} = $y; |
350 | $self->update; |
357 | $self->update; |
351 | } |
358 | } |
352 | |
359 | |
… | |
… | |
461 | |
468 | |
462 | sub set_parent { |
469 | sub set_parent { |
463 | my ($self, $parent) = @_; |
470 | my ($self, $parent) = @_; |
464 | |
471 | |
465 | Scalar::Util::weaken ($self->{parent} = $parent); |
472 | Scalar::Util::weaken ($self->{parent} = $parent); |
466 | |
|
|
467 | $self->set_visible if $parent->{visible}; |
473 | $self->set_visible if $parent->{visible}; |
468 | } |
474 | } |
469 | |
475 | |
470 | sub connect { |
476 | sub connect { |
471 | my ($self, $signal, $cb) = @_; |
477 | my ($self, $signal, $cb) = @_; |
… | |
… | |
506 | sub update { |
512 | sub update { |
507 | my ($self) = @_; |
513 | my ($self) = @_; |
508 | |
514 | |
509 | $self->{parent}->update |
515 | $self->{parent}->update |
510 | if $self->{parent}; |
516 | if $self->{parent}; |
|
|
517 | } |
|
|
518 | |
|
|
519 | sub reconfigure { |
|
|
520 | my ($self) = @_; |
|
|
521 | |
|
|
522 | $self->realloc; |
|
|
523 | $self->update; |
511 | } |
524 | } |
512 | |
525 | |
513 | sub draw { |
526 | sub draw { |
514 | my ($self) = @_; |
527 | my ($self) = @_; |
515 | |
528 | |
… | |
… | |
533 | glVertex $x , $y + $self->{h}; |
546 | glVertex $x , $y + $self->{h}; |
534 | glEnd; |
547 | glEnd; |
535 | glDisable GL_BLEND; |
548 | glDisable GL_BLEND; |
536 | } |
549 | } |
537 | |
550 | |
538 | if ($ENV{PCLIENT_DEBUG}) { |
551 | if ($ENV{CFPLUS_DEBUG}) { |
539 | glPushMatrix; |
552 | glPushMatrix; |
540 | glColor 1, 1, 0, 1; |
553 | glColor 1, 1, 0, 1; |
541 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
554 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
542 | glBegin GL_LINE_LOOP; |
555 | glBegin GL_LINE_LOOP; |
543 | glVertex 0 , 0; |
556 | glVertex 0 , 0; |
… | |
… | |
619 | my ($class, %arg) = @_; |
632 | my ($class, %arg) = @_; |
620 | $class->SUPER::new (can_events => 0, %arg); |
633 | $class->SUPER::new (can_events => 0, %arg); |
621 | } |
634 | } |
622 | |
635 | |
623 | sub size_request { |
636 | sub size_request { |
624 | (0, 0) |
637 | my ($self) = @_; |
|
|
638 | |
|
|
639 | ($self->{w} + 0, $self->{h} + 0) |
625 | } |
640 | } |
626 | |
641 | |
627 | sub draw { } |
642 | sub draw { } |
628 | |
643 | |
629 | ############################################################################# |
644 | ############################################################################# |
… | |
… | |
1000 | |
1015 | |
1001 | our @ISA = CFClient::UI::Bin::; |
1016 | our @ISA = CFClient::UI::Bin::; |
1002 | |
1017 | |
1003 | use CFClient::OpenGL; |
1018 | use CFClient::OpenGL; |
1004 | |
1019 | |
1005 | my @tex = |
1020 | my $bg = |
|
|
1021 | new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png", |
|
|
1022 | mipmap => 1, wrap => 1; |
|
|
1023 | |
|
|
1024 | my @border = |
1006 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1025 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1007 | 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); |
1008 | |
1027 | |
1009 | sub new { |
1028 | sub new { |
1010 | my $class = shift; |
1029 | my $class = shift; |
1011 | |
1030 | |
1012 | my $self = $class->SUPER::new ( |
1031 | my $self = $class->SUPER::new ( |
1013 | bg => [1, 1, 1, 1], |
1032 | bg => [1, 1, 1, 1], |
1014 | border_bg => [1, 1, 1, 1], |
1033 | border_bg => [1, 1, 1, 1], |
1015 | border => 0.6, |
1034 | border => 0.6, |
1016 | is_toplevel => 1, |
|
|
1017 | can_events => 1, |
1035 | can_events => 1, |
|
|
1036 | min_w => 16, |
|
|
1037 | min_h => 16, |
1018 | @_ |
1038 | @_ |
1019 | ); |
1039 | ); |
1020 | |
1040 | |
1021 | $self->{title} &&= new CFClient::UI::Label |
1041 | $self->{title} &&= new CFClient::UI::Label |
1022 | align => 0, |
1042 | align => 0, |
… | |
… | |
1077 | my ($ev, $x, $y) = @_; |
1097 | my ($ev, $x, $y) = @_; |
1078 | |
1098 | |
1079 | my $dx = $ev->{x} - $ox; |
1099 | my $dx = $ev->{x} - $ox; |
1080 | my $dy = $ev->{y} - $oy; |
1100 | my $dy = $ev->{y} - $oy; |
1081 | |
1101 | |
1082 | $self->{user_x} = $wx + $dx * $mx; |
|
|
1083 | $self->{user_y} = $wy + $dy * $my; |
|
|
1084 | $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); |
1102 | $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); |
1085 | $self->{def_h} = $bh + $dy * ($my ? -1 : 1); |
1103 | $self->{force_h} = $bh + $dy * ($my ? -1 : 1); |
1086 | $self->move ($self->{user_x}, $self->{user_y}); |
1104 | |
1087 | $self->realloc; |
1105 | $self->realloc; |
|
|
1106 | $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); |
1088 | }; |
1107 | }; |
1089 | |
1108 | |
1090 | } elsif ($lr ^ $td) { |
1109 | } elsif ($lr ^ $td) { |
1091 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1110 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1092 | my ($bx, $by) = ($self->{x}, $self->{y}); |
1111 | my ($bx, $by) = ($self->{x}, $self->{y}); |
… | |
… | |
1094 | $self->{motion} = sub { |
1113 | $self->{motion} = sub { |
1095 | my ($ev, $x, $y) = @_; |
1114 | my ($ev, $x, $y) = @_; |
1096 | |
1115 | |
1097 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1116 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1098 | |
1117 | |
1099 | $self->{user_x} = $bx + $x - $ox; |
1118 | $self->move_abs ($bx + $x - $ox, $by + $y - $oy); |
1100 | $self->{user_y} = $by + $y - $oy; |
|
|
1101 | |
|
|
1102 | $self->move ($self->{user_x}, $self->{user_y}); |
|
|
1103 | $self->realloc; |
|
|
1104 | }; |
1119 | }; |
1105 | } |
1120 | } |
1106 | } |
1121 | } |
1107 | |
1122 | |
1108 | sub button_up { |
1123 | sub button_up { |
… | |
… | |
1127 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1142 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1128 | |
1143 | |
1129 | my $border = $self->border; |
1144 | my $border = $self->border; |
1130 | |
1145 | |
1131 | glColor @{ $self->{border_bg} }; |
1146 | glColor @{ $self->{border_bg} }; |
1132 | $tex[1]->draw_quad_alpha (0, 0, $w, $border); |
1147 | $border[0]->draw_quad_alpha (0, 0, $w, $border); |
1133 | $tex[3]->draw_quad_alpha (0, $border, $border, $ch); |
1148 | $border[1]->draw_quad_alpha (0, $border, $border, $ch); |
1134 | $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1149 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1135 | $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); |
1150 | $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); |
1136 | |
1151 | |
1137 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1152 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1138 | my $bg = $tex[0]; |
1153 | glColor @{ $self->{bg} }; |
1139 | |
1154 | |
1140 | # TODO: repeat texture not scale |
1155 | # TODO: repeat texture not scale |
|
|
1156 | # solve this better(?) |
1141 | my $rep_x = $cw / $bg->{w}; |
1157 | $bg->{s} = $cw / $bg->{w}; |
1142 | my $rep_y = $ch / $bg->{h}; |
1158 | $bg->{t} = $ch / $bg->{h}; |
1143 | |
|
|
1144 | glColor @{ $self->{bg} }; |
|
|
1145 | |
|
|
1146 | $bg->{s} = $rep_x; |
|
|
1147 | $bg->{t} = $rep_y; |
|
|
1148 | $bg->{wrap_mode} = 1; |
|
|
1149 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
1159 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
1150 | } |
1160 | } |
1151 | |
1161 | |
1152 | glDisable GL_TEXTURE_2D; |
1162 | glDisable GL_TEXTURE_2D; |
1153 | |
1163 | |
… | |
… | |
1416 | ellipsise => 3, # end |
1426 | ellipsise => 3, # end |
1417 | layout => (new CFClient::Layout), |
1427 | layout => (new CFClient::Layout), |
1418 | fontsize => 1, |
1428 | fontsize => 1, |
1419 | align => -1, |
1429 | align => -1, |
1420 | valign => -1, |
1430 | valign => -1, |
1421 | padding => 2, |
1431 | padding_x => 2, |
|
|
1432 | padding_y => 2, |
1422 | can_events => 0, |
1433 | can_events => 0, |
1423 | %arg |
1434 | %arg |
1424 | ); |
1435 | ); |
1425 | |
1436 | |
1426 | if (exists $self->{template}) { |
1437 | if (exists $self->{template}) { |
… | |
… | |
1502 | |
1513 | |
1503 | $w = List::Util::max $w, $w2; |
1514 | $w = List::Util::max $w, $w2; |
1504 | $h = List::Util::max $h, $h2; |
1515 | $h = List::Util::max $h, $h2; |
1505 | } |
1516 | } |
1506 | |
1517 | |
1507 | ( |
1518 | ($w, $h) |
1508 | $w + $self->{padding} * 2, |
|
|
1509 | $h + $self->{padding} * 2, |
|
|
1510 | ) |
|
|
1511 | } |
1519 | } |
1512 | |
1520 | |
1513 | sub size_allocate { |
1521 | sub size_allocate { |
1514 | my ($self, $w, $h, $changed) = @_; |
1522 | my ($self, $w, $h, $changed) = @_; |
1515 | |
1523 | |
… | |
… | |
1539 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1547 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1540 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1548 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1541 | |
1549 | |
1542 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1550 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1543 | |
1551 | |
1544 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding} |
1552 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} |
1545 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1553 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} |
1546 | : ($self->{w} - $tex->{w}) * 0.5); |
1554 | : ($self->{w} - $tex->{w}) * 0.5); |
1547 | |
1555 | |
1548 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} |
1556 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
1549 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
1557 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} |
1550 | : ($self->{h} - $tex->{h}) * 0.5); |
1558 | : ($self->{h} - $tex->{h}) * 0.5); |
1551 | |
1559 | |
1552 | $tex |
1560 | $tex |
1553 | }; |
1561 | }; |
1554 | |
1562 | |
… | |
… | |
1793 | |
1801 | |
1794 | sub new { |
1802 | sub new { |
1795 | my $class = shift; |
1803 | my $class = shift; |
1796 | |
1804 | |
1797 | $class->SUPER::new ( |
1805 | $class->SUPER::new ( |
1798 | padding => 4, |
1806 | padding_x => 4, |
|
|
1807 | padding_y => 4, |
1799 | fg => [1, 1, 1], |
1808 | fg => [1, 1, 1], |
1800 | active_fg => [0, 0, 1], |
1809 | active_fg => [0, 0, 1], |
1801 | can_hover => 1, |
1810 | can_hover => 1, |
1802 | align => 0, |
1811 | align => 0, |
1803 | valign => 0, |
1812 | valign => 0, |
… | |
… | |
1850 | |
1859 | |
1851 | sub new { |
1860 | sub new { |
1852 | my $class = shift; |
1861 | my $class = shift; |
1853 | |
1862 | |
1854 | $class->SUPER::new ( |
1863 | $class->SUPER::new ( |
1855 | padding => 2, |
1864 | padding_x => 2, |
|
|
1865 | padding_y => 2, |
1856 | fg => [1, 1, 1], |
1866 | fg => [1, 1, 1], |
1857 | active_fg => [1, 1, 0], |
1867 | active_fg => [1, 1, 0], |
1858 | bg => [0, 0, 0, 0.2], |
1868 | bg => [0, 0, 0, 0.2], |
1859 | active_bg => [1, 1, 1, 0.5], |
1869 | active_bg => [1, 1, 1, 0.5], |
1860 | state => 0, |
1870 | state => 0, |
… | |
… | |
1864 | } |
1874 | } |
1865 | |
1875 | |
1866 | sub size_request { |
1876 | sub size_request { |
1867 | my ($self) = @_; |
1877 | my ($self) = @_; |
1868 | |
1878 | |
1869 | ($self->{padding} * 2 + 6) x 2 |
1879 | (6) x 2 |
1870 | } |
1880 | } |
1871 | |
1881 | |
1872 | sub button_down { |
1882 | sub button_down { |
1873 | my ($self, $ev, $x, $y) = @_; |
1883 | my ($self, $ev, $x, $y) = @_; |
1874 | |
1884 | |
1875 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1885 | if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} |
1876 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1886 | && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { |
1877 | $self->{state} = !$self->{state}; |
1887 | $self->{state} = !$self->{state}; |
1878 | $self->_emit (changed => $self->{state}); |
1888 | $self->_emit (changed => $self->{state}); |
1879 | } |
1889 | } |
1880 | } |
1890 | } |
1881 | |
1891 | |
1882 | sub _draw { |
1892 | sub _draw { |
1883 | my ($self) = @_; |
1893 | my ($self) = @_; |
1884 | |
1894 | |
1885 | $self->SUPER::_draw; |
1895 | $self->SUPER::_draw; |
1886 | |
1896 | |
1887 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1897 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
1888 | |
1898 | |
1889 | 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; |
1890 | |
1902 | |
1891 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1903 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1892 | |
1904 | |
1893 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1905 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1894 | |
1906 | |
… | |
… | |
2159 | fg => [1, 1, 1], |
2171 | fg => [1, 1, 1], |
2160 | active_fg => [0, 0, 0], |
2172 | active_fg => [0, 0, 0], |
2161 | bg => [0, 0, 0, 0.2], |
2173 | bg => [0, 0, 0, 0.2], |
2162 | active_bg => [1, 1, 1, 0.5], |
2174 | active_bg => [1, 1, 1, 0.5], |
2163 | range => [0, 0, 100, 10, 0], |
2175 | range => [0, 0, 100, 10, 0], |
2164 | req_w => $::WIDTH / 80, |
2176 | min_w => $::WIDTH / 80, |
2165 | req_h => $::WIDTH / 80, |
2177 | min_h => $::WIDTH / 80, |
2166 | vertical => 0, |
2178 | vertical => 0, |
2167 | can_hover => 1, |
2179 | can_hover => 1, |
2168 | inner_pad => 0.02, |
2180 | inner_pad => 0.02, |
2169 | @_ |
2181 | @_ |
2170 | ); |
2182 | ); |
… | |
… | |
2210 | } |
2222 | } |
2211 | |
2223 | |
2212 | sub size_request { |
2224 | sub size_request { |
2213 | my ($self) = @_; |
2225 | my ($self) = @_; |
2214 | |
2226 | |
2215 | my $w = $self->{req_w}; |
2227 | ($self->{req_w}, $self->{req_h}) |
2216 | my $h = $self->{req_h}; |
|
|
2217 | |
|
|
2218 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
2219 | } |
2228 | } |
2220 | |
2229 | |
2221 | sub button_down { |
2230 | sub button_down { |
2222 | my ($self, $ev, $x, $y) = @_; |
2231 | my ($self, $ev, $x, $y) = @_; |
2223 | |
2232 | |
… | |
… | |
2650 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
2659 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
2651 | |
2660 | |
2652 | ($x, $y) = $widget->coord2global (-$self->{w}, 0) |
2661 | ($x, $y) = $widget->coord2global (-$self->{w}, 0) |
2653 | if $x + $self->{w} > $::WIDTH; |
2662 | if $x + $self->{w} > $::WIDTH; |
2654 | |
2663 | |
2655 | $self->move ($x, $y); |
2664 | $self->move_abs ($x, $y); |
2656 | }); |
2665 | }); |
2657 | } |
2666 | } |
2658 | |
2667 | |
2659 | sub _draw { |
2668 | sub _draw { |
2660 | my ($self) = @_; |
2669 | my ($self) = @_; |
… | |
… | |
2853 | # maybe save $GRAB? must be careful about events... |
2862 | # maybe save $GRAB? must be careful about events... |
2854 | $GRAB = $self; |
2863 | $GRAB = $self; |
2855 | $self->{button} = $ev->{button}; |
2864 | $self->{button} = $ev->{button}; |
2856 | |
2865 | |
2857 | $self->show; |
2866 | $self->show; |
2858 | $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); |
2859 | } |
2868 | } |
2860 | |
2869 | |
2861 | sub mouse_motion { |
2870 | sub mouse_motion { |
2862 | my ($self, $ev, $x, $y) = @_; |
2871 | my ($self, $ev, $x, $y) = @_; |
2863 | |
2872 | |
… | |
… | |
3029 | my ($self) = @_; |
3038 | my ($self) = @_; |
3030 | |
3039 | |
3031 | ($self->{w}, $self->{h}) |
3040 | ($self->{w}, $self->{h}) |
3032 | } |
3041 | } |
3033 | |
3042 | |
|
|
3043 | sub _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 | |
3034 | sub size_allocate { |
3057 | sub size_allocate { |
3035 | my ($self, $w, $h, $changed) = @_; |
3058 | my ($self, $w, $h, $changed) = @_; |
3036 | |
3059 | |
3037 | for my $child ($self->children) { |
3060 | for my $child ($self->children) { |
3038 | 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)}; |
3039 | |
3062 | |
3040 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
3063 | $X = $child->{force_x} if exists $child->{force_x}; |
3041 | if exists $child->{req_x}; |
3064 | $Y = $child->{force_y} if exists $child->{force_y}; |
3042 | |
3065 | |
3043 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3066 | $X = _to_pixel $X, $W, $self->{w}; |
3044 | if exists $child->{req_y}; |
3067 | $Y = _to_pixel $Y, $H, $self->{h}; |
3045 | |
|
|
3046 | #delete @$child{qw(req_x req_y)};#d# def_x, def_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 | |
… | |
… | |
3071 | } |
3089 | } |
3072 | |
3090 | |
3073 | sub add { |
3091 | sub add { |
3074 | my ($self, @children) = @_; |
3092 | my ($self, @children) = @_; |
3075 | |
3093 | |
3076 | for my $child (@children) { |
|
|
3077 | $child->{is_toplevel} = 1; |
3094 | $_->{is_toplevel} = 1 |
3078 | |
3095 | for @children; |
3079 | # integerise window positions |
|
|
3080 | $child->{x} = int $child->{x}; |
|
|
3081 | $child->{y} = int $child->{y}; |
|
|
3082 | } |
|
|
3083 | |
3096 | |
3084 | $self->SUPER::add (@children); |
3097 | $self->SUPER::add (@children); |
3085 | } |
3098 | } |
3086 | |
3099 | |
3087 | sub remove { |
3100 | sub remove { |
3088 | my ($self, @children) = @_; |
3101 | my ($self, @children) = @_; |
3089 | |
3102 | |
3090 | $self->SUPER::remove (@children); |
3103 | $self->SUPER::remove (@children); |
|
|
3104 | |
|
|
3105 | delete $self->{is_toplevel} |
|
|
3106 | for @children; |
3091 | |
3107 | |
3092 | while (@children) { |
3108 | while (@children) { |
3093 | my $w = pop @children; |
3109 | my $w = pop @children; |
3094 | push @children, $w->children; |
3110 | push @children, $w->children; |
3095 | $w->set_invisible; |
3111 | $w->set_invisible; |
… | |
… | |
3129 | |
3145 | |
3130 | my $widget = pop @queue || last; |
3146 | my $widget = pop @queue || last; |
3131 | |
3147 | |
3132 | $widget->{visible} or last; # do not resize invisible widgets |
3148 | $widget->{visible} or last; # do not resize invisible widgets |
3133 | |
3149 | |
3134 | my ($w, $h) = $widget->{def_w} && $widget->{def_h} |
|
|
3135 | ? @$widget{qw(def_w def_h)} |
|
|
3136 | : $widget->size_request; |
3150 | my ($w, $h) = $widget->size_request; |
3137 | |
3151 | |
3138 | 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; |
3139 | |
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 | |
3140 | $widget->{req_w} = $w; |
3158 | $widget->{req_w} = $w; |
3141 | $widget->{req_h} = $h; |
3159 | $widget->{req_h} = $h; |
3142 | |
3160 | |
3143 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
3161 | $self->{size_alloc}{$widget} = [$widget, undef, undef]; |
3144 | |
3162 | |
3145 | push @queue, $widget->{parent} |
3163 | push @queue, $widget->{parent} |
3146 | if $widget->{parent}; |
3164 | if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent}; |
3147 | } |
3165 | } |
3148 | } |
3166 | } |
3149 | |
3167 | |
3150 | while (my $size_alloc = delete $self->{size_alloc}) { |
3168 | while (my $size_alloc = delete $self->{size_alloc}) { |
3151 | my @queue = sort $b->[0]{visible} <=> $a->[0]{visible}, |
3169 | my @queue = sort $b->[0]{visible} <=> $a->[0]{visible}, |
3152 | values %$size_alloc; |
3170 | values %$size_alloc; |
3153 | |
3171 | |
3154 | while () { |
3172 | while () { |
3155 | my ($widget, $w, $h) = @{ pop @queue or last }; |
3173 | my ($widget, $w, $h) = @{ pop @queue or last }; |
3156 | |
3174 | |
|
|
3175 | $w = $widget->{w} || $widget->{req_w} unless defined $w; |
|
|
3176 | $h = $widget->{h} || $widget->{req_h} unless defined $h; |
|
|
3177 | |
3157 | $w = 0 if $w < 0; |
3178 | $w = 0 if $w < 0; |
3158 | $h = 0 if $h < 0; |
3179 | $h = 0 if $h < 0; |
|
|
3180 | |
|
|
3181 | $w = int $w + 0.5; |
|
|
3182 | $h = int $h + 0.5; |
3159 | |
3183 | |
3160 | my $changed = $widget->{w} != $w || $widget->{h} != $h; |
3184 | my $changed = $widget->{w} != $w || $widget->{h} != $h; |
3161 | |
3185 | |
3162 | $widget->{w} = $w; |
3186 | $widget->{w} = $w; |
3163 | $widget->{h} = $h; |
3187 | $widget->{h} = $h; |