1 | package DC::UI; |
1 | package DC::UI; |
2 | |
2 | |
3 | use utf8; |
3 | use common::sense; |
4 | use strict; |
|
|
5 | |
4 | |
6 | use List::Util (); |
5 | use List::Util (); |
|
|
6 | |
|
|
7 | use Guard (); |
7 | |
8 | |
8 | use DC; |
9 | use DC; |
9 | use DC::Pod; |
10 | use DC::Pod; |
10 | use DC::Texture; |
11 | use DC::Texture; |
11 | |
12 | |
… | |
… | |
189 | # call when resolution changes etc. |
190 | # call when resolution changes etc. |
190 | sub rescale_widgets { |
191 | sub rescale_widgets { |
191 | my ($sx, $sy) = @_; |
192 | my ($sx, $sy) = @_; |
192 | |
193 | |
193 | for my $widget (values %WIDGET) { |
194 | for my $widget (values %WIDGET) { |
194 | if ($widget->{is_toplevel}) { |
195 | if ($widget->{is_toplevel} || $widget->{c_rescale}) { |
195 | $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
196 | $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
196 | $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
197 | $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
197 | |
198 | |
198 | $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; |
199 | $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; |
199 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
200 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
… | |
… | |
221 | |
222 | |
222 | ############################################################################# |
223 | ############################################################################# |
223 | |
224 | |
224 | package DC::UI::Base; |
225 | package DC::UI::Base; |
225 | |
226 | |
226 | use strict; |
227 | use common::sense; |
227 | |
228 | |
228 | use DC::OpenGL; |
229 | use DC::OpenGL; |
229 | |
230 | |
230 | sub new { |
231 | sub new { |
231 | my $class = shift; |
232 | my $class = shift; |
… | |
… | |
286 | sub set_visible { |
287 | sub set_visible { |
287 | my ($self) = @_; |
288 | my ($self) = @_; |
288 | |
289 | |
289 | return if $self->{visible}; |
290 | return if $self->{visible}; |
290 | |
291 | |
|
|
292 | $self->{parent} && $self->{parent}{root}#d# |
|
|
293 | or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1); |
|
|
294 | |
291 | $self->{root} = $self->{parent}{root}; |
295 | $self->{root} = $self->{parent}{root}; |
292 | $self->{visible} = $self->{parent}{visible} + 1; |
296 | $self->{visible} = $self->{parent}{visible} + 1; |
293 | |
297 | |
294 | $self->emit (visibility_change => 1); |
298 | $self->emit (visibility_change => 1); |
295 | |
299 | |
296 | $self->realloc if !exists $self->{req_w}; |
300 | $self->realloc if !exists $self->{req_w}; |
297 | |
301 | |
298 | $_->set_visible for $self->children; |
302 | $_->set_visible for $self->visible_children; |
299 | } |
303 | } |
300 | |
304 | |
301 | sub set_invisible { |
305 | sub set_invisible { |
302 | my ($self) = @_; |
306 | my ($self) = @_; |
303 | |
307 | |
… | |
… | |
527 | sub connect { |
531 | sub connect { |
528 | my ($self, $signal, $cb) = @_; |
532 | my ($self, $signal, $cb) = @_; |
529 | |
533 | |
530 | push @{ $self->{signal_cb}{$signal} }, $cb; |
534 | push @{ $self->{signal_cb}{$signal} }, $cb; |
531 | |
535 | |
532 | defined wantarray and DC::guard { |
536 | defined wantarray and Guard::guard { |
533 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
537 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
534 | @{ $self->{signal_cb}{$signal} }; |
538 | @{ $self->{signal_cb}{$signal} }; |
535 | } |
539 | } |
536 | } |
540 | } |
537 | |
541 | |
… | |
… | |
638 | |
642 | |
639 | package DC::UI::DrawBG; |
643 | package DC::UI::DrawBG; |
640 | |
644 | |
641 | our @ISA = DC::UI::Base::; |
645 | our @ISA = DC::UI::Base::; |
642 | |
646 | |
643 | use strict; |
647 | use common::sense; |
|
|
648 | |
644 | use DC::OpenGL; |
649 | use DC::OpenGL; |
645 | |
650 | |
646 | sub new { |
651 | sub new { |
647 | my $class = shift; |
652 | my $class = shift; |
648 | |
653 | |
… | |
… | |
651 | #active_bg => [1, 1, 1, 0.5], |
656 | #active_bg => [1, 1, 1, 0.5], |
652 | @_ |
657 | @_ |
653 | ) |
658 | ) |
654 | } |
659 | } |
655 | |
660 | |
|
|
661 | sub set_bg { |
|
|
662 | my ($self, $bg) = @_; |
|
|
663 | |
|
|
664 | $self->{bg} = $bg; |
|
|
665 | $self->update; |
|
|
666 | } |
|
|
667 | |
656 | sub _draw { |
668 | sub _draw { |
657 | my ($self) = @_; |
669 | my ($self) = @_; |
658 | |
670 | |
659 | my $color = $FOCUS == $self && $self->{active_bg} |
671 | my $color = $FOCUS == $self |
660 | ? $self->{active_bg} |
672 | ? $self->{active_bg} || $self->{bg} |
661 | : $self->{bg}; |
673 | : $self->{bg}; |
662 | |
674 | |
663 | if ($color && (@$color < 4 || $color->[3])) { |
675 | if ($color && (@$color < 4 || $color->[3])) { |
664 | my ($w, $h) = @$self{qw(w h)}; |
676 | my ($w, $h) = @$self{qw(w h)}; |
665 | |
677 | |
… | |
… | |
1256 | fontsize => ($arg{border} || 0.8) * 0.75; |
1268 | fontsize => ($arg{border} || 0.8) * 0.75; |
1257 | } |
1269 | } |
1258 | |
1270 | |
1259 | my $self = $class->SUPER::new ( |
1271 | my $self = $class->SUPER::new ( |
1260 | # label => "", |
1272 | # label => "", |
1261 | fg => [0.6, 0.3, 0.1], |
1273 | fg => undef, |
1262 | border => 0.8, |
1274 | border => 0.8, |
1263 | style => 'single', |
1275 | style => 'single', |
1264 | %arg, |
1276 | %arg, |
1265 | ); |
1277 | ); |
1266 | |
1278 | |
… | |
… | |
1319 | my $border = $self->border; |
1331 | my $border = $self->border; |
1320 | my ($w, $h) = ($self->{w}, $self->{h}); |
1332 | my ($w, $h) = ($self->{w}, $self->{h}); |
1321 | |
1333 | |
1322 | $child->draw; |
1334 | $child->draw; |
1323 | |
1335 | |
1324 | glColor @{$self->{fg}}; |
1336 | glColor @{$self->{fg} || $DC::THEME{fancyframe}}; |
1325 | glBegin GL_LINE_STRIP; |
1337 | glBegin GL_LINE_STRIP; |
1326 | glVertex $border * 1.5 , $border * 0.5 + 0.5; |
1338 | glVertex $border * 1.5 , $border * 0.5 + 0.5; |
1327 | glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; |
1339 | glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; |
1328 | glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
1340 | glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
1329 | glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
1341 | glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
… | |
… | |
1344 | our @ISA = DC::UI::Bin::; |
1356 | our @ISA = DC::UI::Bin::; |
1345 | |
1357 | |
1346 | use DC::OpenGL; |
1358 | use DC::OpenGL; |
1347 | |
1359 | |
1348 | my $bg = |
1360 | my $bg = |
1349 | new_from_file DC::Texture DC::find_rcfile "d1_bg.png", |
1361 | new_from_resource DC::Texture "d1_bg.png", |
1350 | mipmap => 1, wrap => 1; |
1362 | mipmap => 1, wrap => 1; |
1351 | |
1363 | |
1352 | my @border = |
1364 | my @border = |
1353 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
1365 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
1354 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1366 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1355 | |
1367 | |
1356 | my @icon = |
1368 | my @icon = |
1357 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
1369 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
1358 | qw(x1_move.png x1_resize.png); |
1370 | qw(x1_move.png x1_resize.png); |
1359 | |
1371 | |
1360 | sub new { |
1372 | sub new { |
1361 | my ($class, %arg) = @_; |
1373 | my ($class, %arg) = @_; |
1362 | |
1374 | |
1363 | my $self = $class->SUPER::new ( |
1375 | my $self = $class->SUPER::new ( |
1364 | bg => [1, 1, 1, 1], |
1376 | bg => [1, 1, 1, 1], |
1365 | border_bg => [1, 1, 1, 1], |
1377 | border_bg => [1, 1, 1, 1], |
1366 | border => 0.6, |
1378 | border => 0.8, |
1367 | can_events => 1, |
1379 | can_events => 1, |
1368 | min_w => 64, |
1380 | min_w => 64, |
1369 | min_h => 32, |
1381 | min_h => 32, |
1370 | %arg, |
1382 | %arg, |
1371 | ); |
1383 | ); |
… | |
… | |
1540 | glEnable GL_TEXTURE_2D; |
1552 | glEnable GL_TEXTURE_2D; |
1541 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1553 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1542 | |
1554 | |
1543 | my $border = $self->border; |
1555 | my $border = $self->border; |
1544 | |
1556 | |
|
|
1557 | if ($border) { |
1545 | glColor @{ $self->{border_bg} }; |
1558 | glColor @{ $self->{border_bg} }; |
1546 | $border[0]->draw_quad_alpha ( 0, 0, $w, $border); |
1559 | $border[0]->draw_quad_alpha ( 0, 0, $w, $border); |
1547 | $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); |
1560 | $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); |
1548 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1561 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1549 | $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); |
1562 | $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); |
1550 | |
1563 | |
1551 | # move |
1564 | # move |
1552 | my $w2 = ($w - $border) * .5; |
1565 | my $w2 = ($w - $border) * .5; |
1553 | my $h2 = ($h - $border) * .5; |
1566 | my $h2 = ($h - $border) * .5; |
1554 | $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); |
1567 | $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); |
1555 | $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); |
1568 | $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); |
1556 | $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); |
1569 | $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); |
1557 | |
1570 | |
1558 | # resize |
1571 | # resize |
1559 | $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); |
1572 | $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); |
1560 | $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) |
1573 | $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) |
1561 | unless $self->{has_close_button}; |
1574 | unless $self->{has_close_button}; |
1562 | $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); |
1575 | $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); |
1563 | $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); |
1576 | $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); |
|
|
1577 | } |
1564 | |
1578 | |
1565 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1579 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1566 | glColor @{ $self->{bg} }; |
1580 | glColor @{ $self->{bg} }; |
1567 | |
1581 | |
1568 | # TODO: repeat texture not scale |
1582 | # TODO: repeat texture not scale |
… | |
… | |
2090 | my $class = shift; |
2104 | my $class = shift; |
2091 | |
2105 | |
2092 | $class->SUPER::new ( |
2106 | $class->SUPER::new ( |
2093 | fg => [1, 1, 1], |
2107 | fg => [1, 1, 1], |
2094 | bg => [0, 0, 0, 0.2], |
2108 | bg => [0, 0, 0, 0.2], |
2095 | outline => [0.6, 0.3, 0.1], |
2109 | outline => undef, |
2096 | active_bg => [0, 0, 1, .2], |
2110 | active_bg => [0, 0, 1, .2], |
2097 | active_fg => [1, 1, 1], |
2111 | active_fg => [1, 1, 1], |
2098 | active_outline => [1, 1, 0], |
2112 | active_outline => [1, 1, 0], |
2099 | can_hover => 1, |
2113 | can_hover => 1, |
2100 | can_focus => 1, |
2114 | can_focus => 1, |
… | |
… | |
2158 | |
2172 | |
2159 | my $text = $self->get_text; |
2173 | my $text = $self->get_text; |
2160 | |
2174 | |
2161 | $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; |
2175 | $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; |
2162 | |
2176 | |
2163 | if ($uni == 8) { |
2177 | if ($sym == DC::SDLK_BACKSPACE) { |
2164 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2178 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2165 | } elsif ($uni == 127) { |
2179 | } elsif ($sym == DC::SDLK_DELETE) { |
2166 | substr $text, $self->{cursor}, 1, ""; |
2180 | substr $text, $self->{cursor}, 1, ""; |
2167 | } elsif ($sym == DC::SDLK_LEFT) { |
2181 | } elsif ($sym == DC::SDLK_LEFT) { |
2168 | --$self->{cursor} if $self->{cursor}; |
2182 | --$self->{cursor} if $self->{cursor}; |
2169 | } elsif ($sym == DC::SDLK_RIGHT) { |
2183 | } elsif ($sym == DC::SDLK_RIGHT) { |
2170 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2184 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
… | |
… | |
2273 | glColor @{$self->{active_outline}}; |
2287 | glColor @{$self->{active_outline}}; |
2274 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
2288 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
2275 | glLineWidth 1; |
2289 | glLineWidth 1; |
2276 | |
2290 | |
2277 | } else { |
2291 | } else { |
2278 | glColor @{$self->{outline}}; |
2292 | glColor @{$self->{outline} || $DC::THEME{entry_outline}}; |
2279 | glBegin GL_LINE_STRIP; |
2293 | glBegin GL_LINE_STRIP; |
2280 | glVertex .5, $self->{h} * .5; |
2294 | glVertex .5, $self->{h} * .5; |
2281 | glVertex .5, $self->{h} - 2.5; |
2295 | glVertex .5, $self->{h} - 2.5; |
2282 | glVertex $self->{w} - .5, $self->{h} - 2.5; |
2296 | glVertex $self->{w} - .5, $self->{h} - 2.5; |
2283 | glVertex $self->{w} - .5, $self->{h} * .5; |
2297 | glVertex $self->{w} - .5, $self->{h} * .5; |
… | |
… | |
2405 | our @ISA = DC::UI::Bin::; |
2419 | our @ISA = DC::UI::Bin::; |
2406 | |
2420 | |
2407 | use DC::OpenGL; |
2421 | use DC::OpenGL; |
2408 | |
2422 | |
2409 | my @tex = |
2423 | my @tex = |
2410 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2424 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2411 | qw(b1_button_inactive.png b1_button_active.png); |
2425 | qw(b1_button_inactive.png b1_button_active.png); |
2412 | |
2426 | |
2413 | sub new { |
2427 | sub new { |
2414 | my $class = shift; |
2428 | my $class = shift; |
2415 | |
2429 | |
… | |
… | |
2454 | our @ISA = DC::UI::Label::; |
2468 | our @ISA = DC::UI::Label::; |
2455 | |
2469 | |
2456 | use DC::OpenGL; |
2470 | use DC::OpenGL; |
2457 | |
2471 | |
2458 | my @tex = |
2472 | my @tex = |
2459 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2473 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2460 | qw(b1_button_inactive.png b1_button_active.png); |
2474 | qw(b1_button_inactive.png b1_button_active.png); |
2461 | |
2475 | |
2462 | sub new { |
2476 | sub new { |
2463 | my $class = shift; |
2477 | my $class = shift; |
2464 | |
2478 | |
… | |
… | |
2507 | package DC::UI::CheckBox; |
2521 | package DC::UI::CheckBox; |
2508 | |
2522 | |
2509 | our @ISA = DC::UI::DrawBG::; |
2523 | our @ISA = DC::UI::DrawBG::; |
2510 | |
2524 | |
2511 | my @tex = |
2525 | my @tex = |
2512 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2526 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2513 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2527 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2514 | |
2528 | |
2515 | use DC::OpenGL; |
2529 | use DC::OpenGL; |
2516 | |
2530 | |
2517 | sub new { |
2531 | sub new { |
2518 | my $class = shift; |
2532 | my $class = shift; |
2519 | |
2533 | |
2520 | $class->SUPER::new ( |
2534 | $class->SUPER::new ( |
|
|
2535 | fontsize => 1, |
2521 | padding_x => 2, |
2536 | padding_x => 2, |
2522 | padding_y => 2, |
2537 | padding_y => 2, |
2523 | fg => [1, 1, 1], |
2538 | fg => [1, 1, 1], |
2524 | active_fg => [1, 1, 0], |
2539 | active_fg => [1, 1, 0], |
2525 | bg => [0, 0, 0, 0.2], |
2540 | bg => [0, 0, 0, 0.2], |
… | |
… | |
2531 | } |
2546 | } |
2532 | |
2547 | |
2533 | sub size_request { |
2548 | sub size_request { |
2534 | my ($self) = @_; |
2549 | my ($self) = @_; |
2535 | |
2550 | |
2536 | (6) x 2 |
2551 | ($self->{fontsize} * $::FONTSIZE) x 2 |
2537 | } |
2552 | } |
2538 | |
2553 | |
2539 | sub toggle { |
2554 | sub toggle { |
2540 | my ($self) = @_; |
2555 | my ($self) = @_; |
2541 | |
2556 | |
… | |
… | |
2579 | |
2594 | |
2580 | ############################################################################# |
2595 | ############################################################################# |
2581 | |
2596 | |
2582 | package DC::UI::Image; |
2597 | package DC::UI::Image; |
2583 | |
2598 | |
2584 | our @ISA = DC::UI::Base::; |
2599 | our @ISA = DC::UI::DrawBG::; |
2585 | |
2600 | |
2586 | use DC::OpenGL; |
2601 | use DC::OpenGL; |
2587 | |
2602 | |
2588 | our %texture_cache; |
2603 | our %texture_cache; |
2589 | |
2604 | |
… | |
… | |
2598 | |
2613 | |
2599 | $self->{path} || $self->{tex} |
2614 | $self->{path} || $self->{tex} |
2600 | or Carp::croak "'path' or 'tex' attributes required"; |
2615 | or Carp::croak "'path' or 'tex' attributes required"; |
2601 | |
2616 | |
2602 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2617 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2603 | new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1; |
2618 | new_from_resource DC::Texture $self->{path}, mipmap => 1; |
2604 | |
2619 | |
2605 | DC::weaken $texture_cache{$self->{path}}; |
2620 | DC::weaken $texture_cache{$self->{path}}; |
2606 | |
2621 | |
2607 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2622 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2608 | |
2623 | |
… | |
… | |
2622 | my ($self, $cloning, $path) = @_; |
2637 | my ($self, $cloning, $path) = @_; |
2623 | |
2638 | |
2624 | $self->new (path => $path) |
2639 | $self->new (path => $path) |
2625 | } |
2640 | } |
2626 | |
2641 | |
|
|
2642 | sub set_texture { |
|
|
2643 | my ($self, $tex) = @_; |
|
|
2644 | |
|
|
2645 | $self->{tex} = $tex; |
|
|
2646 | $self->update; |
|
|
2647 | } |
|
|
2648 | |
2627 | sub size_request { |
2649 | sub size_request { |
2628 | my ($self) = @_; |
2650 | my ($self) = @_; |
2629 | |
2651 | |
2630 | (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) |
2652 | (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) |
2631 | } |
2653 | } |
2632 | |
2654 | |
2633 | sub _draw { |
2655 | sub _draw { |
2634 | my ($self) = @_; |
2656 | my ($self) = @_; |
|
|
2657 | |
|
|
2658 | $self->SUPER::_draw; |
2635 | |
2659 | |
2636 | my $tex = $self->{tex}; |
2660 | my $tex = $self->{tex}; |
2637 | |
2661 | |
2638 | my ($w, $h) = ($self->{w}, $self->{h}); |
2662 | my ($w, $h) = ($self->{w}, $self->{h}); |
2639 | |
2663 | |
… | |
… | |
2657 | package DC::UI::ImageButton; |
2681 | package DC::UI::ImageButton; |
2658 | |
2682 | |
2659 | our @ISA = DC::UI::Image::; |
2683 | our @ISA = DC::UI::Image::; |
2660 | |
2684 | |
2661 | use DC::OpenGL; |
2685 | use DC::OpenGL; |
2662 | |
|
|
2663 | my %textures; |
|
|
2664 | |
2686 | |
2665 | sub new { |
2687 | sub new { |
2666 | my $class = shift; |
2688 | my $class = shift; |
2667 | |
2689 | |
2668 | my $self = $class->SUPER::new ( |
2690 | my $self = $class->SUPER::new ( |
… | |
… | |
2704 | |
2726 | |
2705 | use DC::OpenGL; |
2727 | use DC::OpenGL; |
2706 | |
2728 | |
2707 | my %tex = ( |
2729 | my %tex = ( |
2708 | food => [ |
2730 | food => [ |
2709 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2731 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2710 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2732 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2711 | ], |
2733 | ], |
2712 | grace => [ |
2734 | grace => [ |
2713 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2735 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2714 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2736 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2715 | ], |
2737 | ], |
2716 | hp => [ |
2738 | hp => [ |
2717 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2739 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2718 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2740 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2719 | ], |
2741 | ], |
2720 | mana => [ |
2742 | mana => [ |
2721 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2743 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2722 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2744 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2723 | ], |
2745 | ], |
2724 | ); |
2746 | ); |
2725 | |
2747 | |
2726 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
2748 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
… | |
… | |
2839 | |
2861 | |
2840 | sub new { |
2862 | sub new { |
2841 | my ($class, %arg) = @_; |
2863 | my ($class, %arg) = @_; |
2842 | |
2864 | |
2843 | my $self = $class->SUPER::new ( |
2865 | my $self = $class->SUPER::new ( |
|
|
2866 | padding_x => 2, |
|
|
2867 | padding_y => 2, |
2844 | fg => [1, 1, 1], |
2868 | fg => [1, 1, 1], |
2845 | bg => [0, 0, 1, 0.2], |
2869 | bg => [0, 0, 1, 0.2], |
2846 | bar => [0.7, 0.5, 0.1, 0.8], |
2870 | bar => [0.7, 0.5, 0.1, 0.8], |
2847 | outline => [0.4, 0.3, 0], |
2871 | outline => [0.4, 0.3, 0], |
2848 | fontsize => 0.9, |
2872 | fontsize => 0.9, |
… | |
… | |
2888 | my ($self) = @_; |
2912 | my ($self) = @_; |
2889 | |
2913 | |
2890 | glEnable GL_BLEND; |
2914 | glEnable GL_BLEND; |
2891 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2915 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2892 | |
2916 | |
|
|
2917 | my $px = $self->{padding_x}; |
|
|
2918 | my $py = $self->{padding_y}; |
|
|
2919 | |
2893 | if ($self->{value} >= 0) { |
2920 | if ($self->{value} >= 0) { |
2894 | my $s = int 2 + ($self->{w} - 4) * $self->{value}; |
2921 | my $s = int $px + ($self->{w} - $px * 2) * $self->{value}; |
2895 | |
2922 | |
2896 | glColor_premultiply @{$self->{bar}}; |
2923 | glColor_premultiply @{$self->{bar}}; |
2897 | glRect 2, 2, $s, $self->{h} - 2; |
2924 | glRect $px, $py, $s, $self->{h} - $py; |
2898 | glColor_premultiply @{$self->{bg}}; |
2925 | glColor_premultiply @{$self->{bg}}; |
2899 | glRect $s, 2, $self->{w} - 2, $self->{h} - 2; |
2926 | glRect $s , $py, $self->{w} - $px, $self->{h} - $py; |
2900 | } |
2927 | } |
2901 | |
2928 | |
2902 | glColor_premultiply @{$self->{outline}}; |
2929 | glColor_premultiply @{$self->{outline}}; |
|
|
2930 | |
|
|
2931 | $px -= .5; |
|
|
2932 | $py -= .5; |
|
|
2933 | |
2903 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
2934 | glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py; |
2904 | |
2935 | |
2905 | glDisable GL_BLEND; |
2936 | glDisable GL_BLEND; |
2906 | |
2937 | |
2907 | { |
2938 | { |
2908 | local $self->{bg}; # do not draw background |
2939 | local $self->{bg}; # do not draw background |
… | |
… | |
2917 | our @ISA = DC::UI::Progress::; |
2948 | our @ISA = DC::UI::Progress::; |
2918 | |
2949 | |
2919 | sub new { |
2950 | sub new { |
2920 | my ($class, %arg) = @_; |
2951 | my ($class, %arg) = @_; |
2921 | |
2952 | |
|
|
2953 | my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : ""; |
|
|
2954 | |
2922 | my $self = $class->SUPER::new ( |
2955 | my $self = $class->SUPER::new ( |
|
|
2956 | %arg, |
2923 | tooltip => sub { |
2957 | tooltip => sub { |
2924 | my ($self) = @_; |
2958 | my ($self) = @_; |
2925 | |
2959 | |
2926 | sprintf "level %d\n%s points\n%s next level\n%s to go", |
2960 | sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done", |
|
|
2961 | $tt, |
2927 | $self->{lvl}, |
2962 | $self->{lvl}, |
2928 | ::formsep ($self->{exp}), |
2963 | ::formsep ($self->{exp}), |
2929 | ::formsep ($self->{nxt}), |
2964 | ::formsep ($self->{nxt}), |
2930 | ::formsep ($self->{nxt} - $self->{exp}), |
2965 | ::formsep ($self->{nxt} - $self->{exp}), |
|
|
2966 | $self->_percent * 100, |
2931 | }, |
2967 | }, |
2932 | %arg |
|
|
2933 | ); |
2968 | ); |
2934 | |
2969 | |
2935 | $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } |
2970 | $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } |
2936 | if $::CONN; |
2971 | if $::CONN; |
2937 | |
2972 | |
… | |
… | |
2945 | if $::CONN; |
2980 | if $::CONN; |
2946 | |
2981 | |
2947 | $self->SUPER::DESTROY; |
2982 | $self->SUPER::DESTROY; |
2948 | } |
2983 | } |
2949 | |
2984 | |
|
|
2985 | sub _percent { |
|
|
2986 | my ($self) = @_; |
|
|
2987 | |
|
|
2988 | my $table = $::CONN && $::CONN->{exp_table} |
|
|
2989 | or return -1; |
|
|
2990 | |
|
|
2991 | my $l0 = $table->[$self->{lvl} - 1]; |
|
|
2992 | my $l1 = $table->[$self->{lvl}]; |
|
|
2993 | |
|
|
2994 | $self->{nxt} = $l1; |
|
|
2995 | |
|
|
2996 | ($self->{exp} - $l0) / ($l1 - $l0) |
|
|
2997 | } |
|
|
2998 | |
2950 | sub set_value { |
2999 | sub set_value { |
2951 | my ($self, $lvl, $exp) = @_; |
3000 | my ($self, $lvl, $exp) = @_; |
2952 | |
3001 | |
2953 | $self->{lvl} = $lvl; |
3002 | $self->{lvl} = $lvl; |
2954 | $self->{exp} = $exp; |
3003 | $self->{exp} = $exp; |
2955 | |
3004 | |
2956 | my $v = -1; |
|
|
2957 | |
|
|
2958 | if ($::CONN && (my $table = $::CONN->{exp_table})) { |
|
|
2959 | my $l0 = $table->[$lvl - 1]; |
|
|
2960 | my $l1 = $table->[$lvl]; |
|
|
2961 | |
|
|
2962 | $self->{nxt} = $l1; |
|
|
2963 | |
|
|
2964 | $v = ($exp - $l0) / ($l1 - $l0); |
|
|
2965 | } |
|
|
2966 | |
|
|
2967 | $self->SUPER::set_value ($v); |
3005 | $self->SUPER::set_value ($self->_percent); |
2968 | } |
3006 | } |
2969 | |
3007 | |
2970 | ############################################################################# |
3008 | ############################################################################# |
2971 | |
3009 | |
2972 | package DC::UI::Gauge; |
3010 | package DC::UI::Gauge; |
… | |
… | |
3016 | |
3054 | |
3017 | ############################################################################# |
3055 | ############################################################################# |
3018 | |
3056 | |
3019 | package DC::UI::Slider; |
3057 | package DC::UI::Slider; |
3020 | |
3058 | |
3021 | use strict; |
3059 | use common::sense; |
3022 | |
3060 | |
3023 | use DC::OpenGL; |
3061 | use DC::OpenGL; |
3024 | |
3062 | |
3025 | our @ISA = DC::UI::DrawBG::; |
3063 | our @ISA = DC::UI::DrawBG::; |
3026 | |
3064 | |
3027 | my @tex = |
3065 | my @tex = |
3028 | map { new_from_file DC::Texture DC::find_rcfile $_ } |
3066 | map { new_from_resource DC::Texture $_ } |
3029 | qw(s1_slider.png s1_slider_bg.png); |
3067 | qw(s1_slider.png s1_slider_bg.png); |
3030 | |
3068 | |
3031 | sub new { |
3069 | sub new { |
3032 | my $class = shift; |
3070 | my $class = shift; |
3033 | |
3071 | |
… | |
… | |
3070 | sub set_value { |
3108 | sub set_value { |
3071 | my ($self, $value) = @_; |
3109 | my ($self, $value) = @_; |
3072 | |
3110 | |
3073 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
3111 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
3074 | |
3112 | |
3075 | $hi = $lo + 1 if $hi <= $lo; |
3113 | $hi = $lo if $hi < $lo; |
3076 | |
3114 | |
3077 | $page = $hi - $lo if $page > $hi - $lo; |
3115 | $value = $hi - $page if $value > $hi - $page; |
3078 | |
|
|
3079 | $value = $lo if $value < $lo; |
3116 | $value = $lo if $value < $lo; |
3080 | $value = $hi - $page if $value > $hi - $page; |
|
|
3081 | |
3117 | |
3082 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
3118 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
3083 | if $unit; |
3119 | if $unit; |
3084 | |
3120 | |
3085 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
3121 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
… | |
… | |
3114 | if ($GRAB == $self) { |
3150 | if ($GRAB == $self) { |
3115 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
3151 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
3116 | |
3152 | |
3117 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
3153 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
3118 | |
3154 | |
3119 | $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); |
3155 | $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999); |
3120 | |
3156 | |
3121 | $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); |
3157 | $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); |
3122 | } else { |
3158 | } else { |
3123 | return 0; |
3159 | return 0; |
3124 | } |
3160 | } |
… | |
… | |
3149 | my ($self) = @_; |
3185 | my ($self) = @_; |
3150 | |
3186 | |
3151 | unless ($self->{knob_w}) { |
3187 | unless ($self->{knob_w}) { |
3152 | $self->set_value ($self->{range}[0]); |
3188 | $self->set_value ($self->{range}[0]); |
3153 | |
3189 | |
3154 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
3190 | my ($value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
3155 | my $range = ($hi - $page - $lo) || 1e-100; |
3191 | my $range = ($hi - $page - $lo) || 1e-10; |
3156 | |
3192 | |
3157 | my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; |
3193 | my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w}; |
3158 | |
3194 | |
3159 | $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; |
3195 | $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; |
3160 | $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; |
3196 | $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; |
3161 | |
3197 | |
3162 | $value = ($value - $lo) / $range; |
3198 | $value = ($value - $lo) / $range; |
… | |
… | |
3622 | |
3658 | |
3623 | $tip =~ s/^\n+//; |
3659 | $tip =~ s/^\n+//; |
3624 | $tip =~ s/\n+$//; |
3660 | $tip =~ s/\n+$//; |
3625 | |
3661 | |
3626 | $self->add (new DC::UI::Label |
3662 | $self->add (new DC::UI::Label |
|
|
3663 | fg => $DC::THEME{tooltip_fg}, |
3627 | markup => $tip, |
3664 | markup => $tip, |
3628 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3665 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3629 | align => 0, |
3666 | align => 0, |
3630 | fontsize => 0.8, |
3667 | fontsize => 0.8, |
3631 | style => 1, # FLAG_INVERSE |
3668 | style => $DC::THEME{tooltip_style}, # FLAG_INVERSE |
3632 | ellipsise => 0, |
3669 | ellipsise => 0, |
3633 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3670 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3634 | ); |
3671 | ); |
3635 | } |
3672 | } |
3636 | |
3673 | |
… | |
… | |
3673 | sub _draw { |
3710 | sub _draw { |
3674 | my ($self) = @_; |
3711 | my ($self) = @_; |
3675 | |
3712 | |
3676 | my ($w, $h) = @$self{qw(w h)}; |
3713 | my ($w, $h) = @$self{qw(w h)}; |
3677 | |
3714 | |
3678 | glColor 1, 0.8, 0.4; |
3715 | glColor @{ $DC::THEME{tooltip_bg} }; |
3679 | glRect 0, 0, $w, $h; |
3716 | glRect 0, 0, $w, $h; |
3680 | |
3717 | |
3681 | glColor 0, 0, 0; |
3718 | glColor @{ $DC::THEME{tooltip_border} }; |
3682 | glRect_lineloop .5, .5, $w + .5, $h + .5; |
3719 | glRect_lineloop .5, .5, $w + .5, $h + .5; |
3683 | |
3720 | |
3684 | glTranslate 2, 2; |
3721 | glTranslate 2, 2; |
3685 | |
3722 | |
3686 | $self->SUPER::_draw; |
3723 | $self->SUPER::_draw; |
… | |
… | |
3703 | aspect => 1, |
3740 | aspect => 1, |
3704 | can_events => 0, |
3741 | can_events => 0, |
3705 | @_, |
3742 | @_, |
3706 | ); |
3743 | ); |
3707 | |
3744 | |
3708 | if ($self->{anim} && $self->{animspeed}) { |
|
|
3709 | DC::weaken (my $widget = $self); |
|
|
3710 | |
|
|
3711 | $self->{animspeed} = List::Util::max 0.05, $self->{animspeed}; |
|
|
3712 | $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub { |
|
|
3713 | return unless $::CONN; |
|
|
3714 | |
|
|
3715 | my $w = $widget |
|
|
3716 | or return; |
|
|
3717 | |
|
|
3718 | ++$w->{frame}; |
|
|
3719 | $w->update_face; |
|
|
3720 | |
|
|
3721 | # somehow, $widget can go away |
|
|
3722 | $w->update; |
|
|
3723 | $w->update_timer; |
|
|
3724 | }; |
|
|
3725 | |
|
|
3726 | $self->update_face; |
|
|
3727 | $self->update_timer; |
3745 | $self->update_anim; |
3728 | } |
|
|
3729 | |
3746 | |
3730 | $self |
3747 | $self |
3731 | } |
3748 | } |
3732 | |
3749 | |
3733 | sub update_timer { |
3750 | sub update_timer { |
… | |
… | |
3756 | $tex->upload (sub { $self->reconfigure }); |
3773 | $tex->upload (sub { $self->reconfigure }); |
3757 | } |
3774 | } |
3758 | } |
3775 | } |
3759 | } |
3776 | } |
3760 | } |
3777 | } |
|
|
3778 | } |
|
|
3779 | } |
|
|
3780 | |
|
|
3781 | sub update_anim { |
|
|
3782 | my ($self) = @_; |
|
|
3783 | |
|
|
3784 | if ($self->{anim} && $self->{animspeed}) { |
|
|
3785 | DC::weaken (my $widget = $self); |
|
|
3786 | |
|
|
3787 | $self->{animspeed} = List::Util::max 0.05, $self->{animspeed}; |
|
|
3788 | $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub { |
|
|
3789 | return unless $::CONN; |
|
|
3790 | |
|
|
3791 | my $w = $widget |
|
|
3792 | or return; |
|
|
3793 | |
|
|
3794 | ++$w->{frame}; |
|
|
3795 | $w->update_face; |
|
|
3796 | |
|
|
3797 | # somehow, $widget can go away |
|
|
3798 | $w->update; |
|
|
3799 | $w->update_timer; |
|
|
3800 | }; |
|
|
3801 | |
|
|
3802 | $self->update_face; |
|
|
3803 | $self->update_timer; |
|
|
3804 | } else { |
|
|
3805 | delete $self->{timer}; |
3761 | } |
3806 | } |
3762 | } |
3807 | } |
3763 | |
3808 | |
3764 | sub size_request { |
3809 | sub size_request { |
3765 | my ($self) = @_; |
3810 | my ($self) = @_; |
… | |
… | |
3787 | return unless $self->{visible}; |
3832 | return unless $self->{visible}; |
3788 | |
3833 | |
3789 | $self->SUPER::update; |
3834 | $self->SUPER::update; |
3790 | } |
3835 | } |
3791 | |
3836 | |
|
|
3837 | sub set_face { |
|
|
3838 | my ($self, $face) = @_; |
|
|
3839 | |
|
|
3840 | $self->{face} = $face; |
|
|
3841 | $self->reconfigure; |
|
|
3842 | } |
|
|
3843 | |
|
|
3844 | sub set_anim { |
|
|
3845 | my ($self, $anim) = @_; |
|
|
3846 | |
|
|
3847 | $self->{anim} = $anim; |
|
|
3848 | $self->update_anim; |
|
|
3849 | } |
|
|
3850 | |
|
|
3851 | sub set_animspeed { |
|
|
3852 | my ($self, $animspeed) = @_; |
|
|
3853 | |
|
|
3854 | $self->{animspeed} = $animspeed; |
|
|
3855 | $self->update_anim; |
|
|
3856 | } |
|
|
3857 | |
3792 | sub invoke_visibility_change { |
3858 | sub invoke_visibility_change { |
3793 | my ($self) = @_; |
3859 | my ($self) = @_; |
3794 | |
3860 | |
3795 | $self->update_timer; |
3861 | $self->update_timer; |
3796 | |
3862 | |
… | |
… | |
3858 | $widget = new DC::UI::HBox |
3924 | $widget = new DC::UI::HBox |
3859 | can_hover => 1, |
3925 | can_hover => 1, |
3860 | can_events => 1, |
3926 | can_events => 1, |
3861 | tooltip => $tooltip, |
3927 | tooltip => $tooltip, |
3862 | children => [ |
3928 | children => [ |
3863 | (new DC::UI::Label markup => $left, expand => 1), |
3929 | (new DC::UI::Label markup => $left , align => 0, expand => 1), |
3864 | (new DC::UI::Label markup => $right, align => 1), |
3930 | (new DC::UI::Label markup => $right, align => 1), |
3865 | ], |
3931 | ], |
3866 | ; |
3932 | ; |
3867 | |
3933 | |
3868 | } else { |
3934 | } else { |
… | |
… | |
3892 | # maybe save $GRAB? must be careful about events... |
3958 | # maybe save $GRAB? must be careful about events... |
3893 | $GRAB = $self; |
3959 | $GRAB = $self; |
3894 | $self->{button} = $ev->{button}; |
3960 | $self->{button} = $ev->{button}; |
3895 | |
3961 | |
3896 | $self->show; |
3962 | $self->show; |
3897 | $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
3963 | |
|
|
3964 | my $x = $ev->{x}; |
|
|
3965 | my $y = $ev->{y}; |
|
|
3966 | |
|
|
3967 | $self->{root}->on_post_alloc ($self => sub { |
|
|
3968 | $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5); |
|
|
3969 | }); |
|
|
3970 | |
|
|
3971 | 1 # so it can be used inside event handlers |
3898 | } |
3972 | } |
3899 | |
3973 | |
3900 | sub invoke_mouse_motion { |
3974 | sub invoke_mouse_motion { |
3901 | my ($self, $ev, $x, $y) = @_; |
3975 | my ($self, $ev, $x, $y) = @_; |
3902 | |
3976 | |
… | |
… | |
3934 | |
4008 | |
3935 | my $self = $class->SUPER::new ( |
4009 | my $self = $class->SUPER::new ( |
3936 | @_, |
4010 | @_, |
3937 | ); |
4011 | ); |
3938 | |
4012 | |
3939 | $self->{current} = $self->{children}[0] |
4013 | $self->set_current_page (0); |
3940 | if @{ $self->{children} }; |
|
|
3941 | |
4014 | |
3942 | $self |
4015 | $self |
3943 | } |
4016 | } |
3944 | |
4017 | |
3945 | sub add { |
4018 | sub add { |
3946 | my ($self, @widgets) = @_; |
4019 | my ($self, @widgets) = @_; |
3947 | |
4020 | |
3948 | $self->SUPER::add (@widgets); |
4021 | $self->SUPER::add (@widgets); |
3949 | |
4022 | |
3950 | $self->{current} = $self->{children}[0] |
4023 | $self->set_current_page (0) |
3951 | if @{ $self->{children} }; |
4024 | if @widgets == @{ $self->{children} }; |
3952 | } |
4025 | } |
3953 | |
4026 | |
3954 | sub get_current_page { |
4027 | sub get_current_page { |
3955 | my ($self) = @_; |
4028 | my ($self) = @_; |
3956 | |
4029 | |
… | |
… | |
3962 | |
4035 | |
3963 | my $widget = ref $page_or_widget |
4036 | my $widget = ref $page_or_widget |
3964 | ? $page_or_widget |
4037 | ? $page_or_widget |
3965 | : $self->{children}[$page_or_widget]; |
4038 | : $self->{children}[$page_or_widget]; |
3966 | |
4039 | |
|
|
4040 | $self->{current}->set_invisible if $self->{current} && $self->{visible}; |
|
|
4041 | |
3967 | $self->{current} = $widget; |
4042 | if (($self->{current} = $widget)) { |
|
|
4043 | $self->{current}->set_visible if $self->{current} && $self->{visible}; |
3968 | $self->{current}->configure (0, 0, $self->{w}, $self->{h}); |
4044 | $self->{current}->configure (0, 0, $self->{w}, $self->{h}); |
3969 | |
4045 | |
3970 | $self->emit (page_changed => $self->{current}); |
4046 | $self->emit (page_changed => $self->{current}); |
|
|
4047 | } |
3971 | |
4048 | |
3972 | $self->realloc; |
4049 | $self->realloc; |
3973 | } |
4050 | } |
3974 | |
4051 | |
3975 | sub visible_children { |
4052 | sub visible_children { |
3976 | $_[0]{current} |
4053 | $_[0]{current} || () |
3977 | } |
4054 | } |
3978 | |
4055 | |
3979 | sub size_request { |
4056 | sub size_request { |
3980 | my ($self) = @_; |
4057 | my ($self) = @_; |
3981 | |
4058 | |
|
|
4059 | $self->{current} |
3982 | $self->{current}->size_request |
4060 | ? $self->{current}->size_request |
|
|
4061 | : (0, 0) |
3983 | } |
4062 | } |
3984 | |
4063 | |
3985 | sub invoke_size_allocate { |
4064 | sub invoke_size_allocate { |
3986 | my ($self, $w, $h) = @_; |
4065 | my ($self, $w, $h) = @_; |
3987 | |
4066 | |
3988 | $self->{current}->configure (0, 0, $w, $h); |
4067 | $self->{current}->configure (0, 0, $w, $h) |
|
|
4068 | if $self->{current}; |
3989 | |
4069 | |
3990 | 1 |
4070 | 1 |
3991 | } |
4071 | } |
3992 | |
4072 | |
3993 | sub _draw { |
4073 | sub _draw { |
3994 | my ($self) = @_; |
4074 | my ($self) = @_; |
3995 | |
4075 | |
3996 | $self->{current}->draw; |
4076 | $self->{current}->draw |
|
|
4077 | if $self->{current}; |
3997 | } |
4078 | } |
3998 | |
4079 | |
3999 | ############################################################################# |
4080 | ############################################################################# |
4000 | |
4081 | |
4001 | package DC::UI::Notebook; |
4082 | package DC::UI::Notebook; |
… | |
… | |
4211 | $self |
4292 | $self |
4212 | } |
4293 | } |
4213 | |
4294 | |
4214 | sub reorder { |
4295 | sub reorder { |
4215 | my ($self) = @_; |
4296 | my ($self) = @_; |
4216 | my $NOW = Time::HiRes::time; |
4297 | my $NOW = EV::time; |
4217 | |
4298 | |
4218 | # freeze display when hovering over any label |
4299 | # freeze display when hovering over any label |
4219 | return if $DC::UI::TOOLTIP->{owner} |
4300 | return if $DC::UI::TOOLTIP->{owner} |
4220 | && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, |
4301 | && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, |
4221 | values %{ $self->{item} }; |
4302 | values %{ $self->{item} }; |
… | |
… | |
4271 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4352 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4272 | } |
4353 | } |
4273 | |
4354 | |
4274 | push @widgets, $label; |
4355 | push @widgets, $label; |
4275 | } |
4356 | } |
|
|
4357 | |
|
|
4358 | my $hash = join ",", @widgets; |
|
|
4359 | return if $hash eq $self->{last_widget_hash}; |
|
|
4360 | $self->{last_widget_hash} = $hash; |
4276 | |
4361 | |
4277 | $self->clear; |
4362 | $self->clear; |
4278 | $self->SUPER::add (reverse @widgets); |
4363 | $self->SUPER::add (reverse @widgets); |
4279 | } |
4364 | } |
4280 | |
4365 | |
… | |
… | |
4585 | |
4670 | |
4586 | $ROOT = new DC::UI::Root; |
4671 | $ROOT = new DC::UI::Root; |
4587 | $TOOLTIP = new DC::UI::Tooltip z => 900; |
4672 | $TOOLTIP = new DC::UI::Tooltip z => 900; |
4588 | |
4673 | |
4589 | 1 |
4674 | 1 |
4590 | |
|
|