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.459 by root, Sat Dec 29 13:44:30 2007 UTC vs.
Revision 1.477 by root, Sun Jan 11 23:50:27 2009 UTC

2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7
8use Guard ();
7 9
8use DC; 10use DC;
9use DC::Pod; 11use DC::Pod;
10use DC::Texture; 12use DC::Texture;
11 13
189# call when resolution changes etc. 191# call when resolution changes etc.
190sub rescale_widgets { 192sub rescale_widgets {
191 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
192 194
193 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
194 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 197 $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.]+$/; 198 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
197 199
198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 200 $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}; 201 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
286sub set_visible { 288sub set_visible {
287 my ($self) = @_; 289 my ($self) = @_;
288 290
289 return if $self->{visible}; 291 return if $self->{visible};
290 292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
295
291 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
292 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
293 298
294 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
295 300
296 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
297 302
298 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
299} 304}
300 305
301sub set_invisible { 306sub set_invisible {
302 my ($self) = @_; 307 my ($self) = @_;
303 308
527sub connect { 532sub connect {
528 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
529 534
530 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
531 536
532 defined wantarray and DC::guard { 537 defined wantarray and Guard::guard {
533 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
534 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
535 } 540 }
536} 541}
537 542
649 $class->SUPER::new ( 654 $class->SUPER::new (
650 #bg => [0, 0, 0, 0.2], 655 #bg => [0, 0, 0, 0.2],
651 #active_bg => [1, 1, 1, 0.5], 656 #active_bg => [1, 1, 1, 0.5],
652 @_ 657 @_
653 ) 658 )
659}
660
661sub set_bg {
662 my ($self, $bg) = @_;
663
664 $self->{bg} = $bg;
665 $self->update;
654} 666}
655 667
656sub _draw { 668sub _draw {
657 my ($self) = @_; 669 my ($self) = @_;
658 670
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;
1344our @ISA = DC::UI::Bin::; 1356our @ISA = DC::UI::Bin::;
1345 1357
1346use DC::OpenGL; 1358use DC::OpenGL;
1347 1359
1348my $bg = 1360my $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
1352my @border = 1364my @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
1356my @icon = 1368my @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
1360sub new { 1372sub 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;
2290package DC::UI::Entry; 2304package DC::UI::Entry;
2291 2305
2292our @ISA = DC::UI::EntryBase::; 2306our @ISA = DC::UI::EntryBase::;
2293 2307
2294use DC::OpenGL; 2308use DC::OpenGL;
2309
2310sub new {
2311 my $class = shift;
2312
2313 $class->SUPER::new (
2314 history_pointer => -1,
2315 @_
2316 )
2317}
2318
2295 2319
2296sub invoke_key_down { 2320sub invoke_key_down {
2297 my ($self, $ev) = @_; 2321 my ($self, $ev) = @_;
2298 2322
2299 my $sym = $ev->{sym}; 2323 my $sym = $ev->{sym};
2324 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2348 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2325 2349
2326 if ($self->{history_pointer} >= 0) { 2350 if ($self->{history_pointer} >= 0) {
2327 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2351 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2328 } else { 2352 } else {
2353 if (defined $self->{history_saveback}) {
2329 $self->set_text ($self->{history_saveback}); 2354 $self->set_text ($self->{history_saveback});
2355 $self->{history_saveback} = undef;
2356 }
2330 } 2357 }
2331 2358
2332 } else { 2359 } else {
2333 return $self->SUPER::invoke_key_down ($ev) 2360 return $self->SUPER::invoke_key_down ($ev)
2334 } 2361 }
2392our @ISA = DC::UI::Bin::; 2419our @ISA = DC::UI::Bin::;
2393 2420
2394use DC::OpenGL; 2421use DC::OpenGL;
2395 2422
2396my @tex = 2423my @tex =
2397 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2424 map { new_from_resource DC::Texture $_, mipmap => 1 }
2398 qw(b1_button_inactive.png b1_button_active.png); 2425 qw(b1_button_inactive.png b1_button_active.png);
2399 2426
2400sub new { 2427sub new {
2401 my $class = shift; 2428 my $class = shift;
2402 2429
2441our @ISA = DC::UI::Label::; 2468our @ISA = DC::UI::Label::;
2442 2469
2443use DC::OpenGL; 2470use DC::OpenGL;
2444 2471
2445my @tex = 2472my @tex =
2446 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2473 map { new_from_resource DC::Texture $_, mipmap => 1 }
2447 qw(b1_button_inactive.png b1_button_active.png); 2474 qw(b1_button_inactive.png b1_button_active.png);
2448 2475
2449sub new { 2476sub new {
2450 my $class = shift; 2477 my $class = shift;
2451 2478
2494package DC::UI::CheckBox; 2521package DC::UI::CheckBox;
2495 2522
2496our @ISA = DC::UI::DrawBG::; 2523our @ISA = DC::UI::DrawBG::;
2497 2524
2498my @tex = 2525my @tex =
2499 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2526 map { new_from_resource DC::Texture $_, mipmap => 1 }
2500 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2527 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2501 2528
2502use DC::OpenGL; 2529use DC::OpenGL;
2503 2530
2504sub new { 2531sub new {
2505 my $class = shift; 2532 my $class = shift;
2506 2533
2507 $class->SUPER::new ( 2534 $class->SUPER::new (
2535 fontsize => 1,
2508 padding_x => 2, 2536 padding_x => 2,
2509 padding_y => 2, 2537 padding_y => 2,
2510 fg => [1, 1, 1], 2538 fg => [1, 1, 1],
2511 active_fg => [1, 1, 0], 2539 active_fg => [1, 1, 0],
2512 bg => [0, 0, 0, 0.2], 2540 bg => [0, 0, 0, 0.2],
2518} 2546}
2519 2547
2520sub size_request { 2548sub size_request {
2521 my ($self) = @_; 2549 my ($self) = @_;
2522 2550
2523 (6) x 2 2551 ($self->{fontsize} * $::FONTSIZE) x 2
2524} 2552}
2525 2553
2526sub toggle { 2554sub toggle {
2527 my ($self) = @_; 2555 my ($self) = @_;
2528 2556
2585 2613
2586 $self->{path} || $self->{tex} 2614 $self->{path} || $self->{tex}
2587 or Carp::croak "'path' or 'tex' attributes required"; 2615 or Carp::croak "'path' or 'tex' attributes required";
2588 2616
2589 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2617 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2590 new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1; 2618 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2591 2619
2592 DC::weaken $texture_cache{$self->{path}}; 2620 DC::weaken $texture_cache{$self->{path}};
2593 2621
2594 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2622 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2595 2623
2691 2719
2692use DC::OpenGL; 2720use DC::OpenGL;
2693 2721
2694my %tex = ( 2722my %tex = (
2695 food => [ 2723 food => [
2696 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2724 map { new_from_resource DC::Texture $_, mipmap => 1 }
2697 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2725 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2698 ], 2726 ],
2699 grace => [ 2727 grace => [
2700 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2728 map { new_from_resource DC::Texture $_, mipmap => 1 }
2701 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2729 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2702 ], 2730 ],
2703 hp => [ 2731 hp => [
2704 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2732 map { new_from_resource DC::Texture $_, mipmap => 1 }
2705 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2733 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2706 ], 2734 ],
2707 mana => [ 2735 mana => [
2708 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2736 map { new_from_resource DC::Texture $_, mipmap => 1 }
2709 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2737 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2710 ], 2738 ],
2711); 2739);
2712 2740
2713# eg. VGauge->new (gauge => 'food'), default gauge: food 2741# eg. VGauge->new (gauge => 'food'), default gauge: food
2826 2854
2827sub new { 2855sub new {
2828 my ($class, %arg) = @_; 2856 my ($class, %arg) = @_;
2829 2857
2830 my $self = $class->SUPER::new ( 2858 my $self = $class->SUPER::new (
2859 padding_x => 2,
2860 padding_y => 2,
2831 fg => [1, 1, 1], 2861 fg => [1, 1, 1],
2832 bg => [0, 0, 1, 0.2], 2862 bg => [0, 0, 1, 0.2],
2833 bar => [0.7, 0.5, 0.1, 0.8], 2863 bar => [0.7, 0.5, 0.1, 0.8],
2834 outline => [0.4, 0.3, 0], 2864 outline => [0.4, 0.3, 0],
2835 fontsize => 0.9, 2865 fontsize => 0.9,
2875 my ($self) = @_; 2905 my ($self) = @_;
2876 2906
2877 glEnable GL_BLEND; 2907 glEnable GL_BLEND;
2878 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2908 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2879 2909
2910 my $px = $self->{padding_x};
2911 my $py = $self->{padding_y};
2912
2880 if ($self->{value} >= 0) { 2913 if ($self->{value} >= 0) {
2881 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2914 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2882 2915
2883 glColor_premultiply @{$self->{bar}}; 2916 glColor_premultiply @{$self->{bar}};
2884 glRect 2, 2, $s, $self->{h} - 2; 2917 glRect $px, $py, $s, $self->{h} - $py;
2885 glColor_premultiply @{$self->{bg}}; 2918 glColor_premultiply @{$self->{bg}};
2886 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2919 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2887 } 2920 }
2888 2921
2889 glColor_premultiply @{$self->{outline}}; 2922 glColor_premultiply @{$self->{outline}};
2923
2924 $px -= .5;
2925 $py -= .5;
2926
2890 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2927 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2891 2928
2892 glDisable GL_BLEND; 2929 glDisable GL_BLEND;
2893 2930
2894 { 2931 {
2895 local $self->{bg}; # do not draw background 2932 local $self->{bg}; # do not draw background
2904our @ISA = DC::UI::Progress::; 2941our @ISA = DC::UI::Progress::;
2905 2942
2906sub new { 2943sub new {
2907 my ($class, %arg) = @_; 2944 my ($class, %arg) = @_;
2908 2945
2946 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2947
2909 my $self = $class->SUPER::new ( 2948 my $self = $class->SUPER::new (
2949 %arg,
2910 tooltip => sub { 2950 tooltip => sub {
2911 my ($self) = @_; 2951 my ($self) = @_;
2912 2952
2913 sprintf "level %d\n%s points\n%s next level\n%s to go", 2953 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2954 $tt,
2914 $self->{lvl}, 2955 $self->{lvl},
2915 ::formsep ($self->{exp}), 2956 ::formsep ($self->{exp}),
2916 ::formsep ($self->{nxt}), 2957 ::formsep ($self->{nxt}),
2917 ::formsep ($self->{nxt} - $self->{exp}), 2958 ::formsep ($self->{nxt} - $self->{exp}),
2959 $self->_percent * 100,
2918 }, 2960 },
2919 %arg
2920 ); 2961 );
2921 2962
2922 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2963 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2923 if $::CONN; 2964 if $::CONN;
2924 2965
2932 if $::CONN; 2973 if $::CONN;
2933 2974
2934 $self->SUPER::DESTROY; 2975 $self->SUPER::DESTROY;
2935} 2976}
2936 2977
2978sub _percent {
2979 my ($self) = @_;
2980
2981 my $table = $::CONN && $::CONN->{exp_table}
2982 or return -1;
2983
2984 my $l0 = $table->[$self->{lvl} - 1];
2985 my $l1 = $table->[$self->{lvl}];
2986
2987 $self->{nxt} = $l1;
2988
2989 ($self->{exp} - $l0) / ($l1 - $l0)
2990}
2991
2937sub set_value { 2992sub set_value {
2938 my ($self, $lvl, $exp) = @_; 2993 my ($self, $lvl, $exp) = @_;
2939 2994
2940 $self->{lvl} = $lvl; 2995 $self->{lvl} = $lvl;
2941 $self->{exp} = $exp; 2996 $self->{exp} = $exp;
2942 2997
2943 my $v = -1;
2944
2945 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2946 my $l0 = $table->[$lvl - 1];
2947 my $l1 = $table->[$lvl];
2948
2949 $self->{nxt} = $l1;
2950
2951 $v = ($exp - $l0) / ($l1 - $l0);
2952 }
2953
2954 $self->SUPER::set_value ($v); 2998 $self->SUPER::set_value ($self->_percent);
2955} 2999}
2956 3000
2957############################################################################# 3001#############################################################################
2958 3002
2959package DC::UI::Gauge; 3003package DC::UI::Gauge;
3010use DC::OpenGL; 3054use DC::OpenGL;
3011 3055
3012our @ISA = DC::UI::DrawBG::; 3056our @ISA = DC::UI::DrawBG::;
3013 3057
3014my @tex = 3058my @tex =
3015 map { new_from_file DC::Texture DC::find_rcfile $_ } 3059 map { new_from_resource DC::Texture $_ }
3016 qw(s1_slider.png s1_slider_bg.png); 3060 qw(s1_slider.png s1_slider_bg.png);
3017 3061
3018sub new { 3062sub new {
3019 my $class = shift; 3063 my $class = shift;
3020 3064
3057sub set_value { 3101sub set_value {
3058 my ($self, $value) = @_; 3102 my ($self, $value) = @_;
3059 3103
3060 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3104 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3061 3105
3062 $hi = $lo + 1 if $hi <= $lo; 3106 $hi = $lo if $hi < $lo;
3063 3107
3064 $page = $hi - $lo if $page > $hi - $lo; 3108 $value = $hi - $page if $value > $hi - $page;
3065
3066 $value = $lo if $value < $lo; 3109 $value = $lo if $value < $lo;
3067 $value = $hi - $page if $value > $hi - $page;
3068 3110
3069 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3111 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3070 if $unit; 3112 if $unit;
3071 3113
3072 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3114 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3136 my ($self) = @_; 3178 my ($self) = @_;
3137 3179
3138 unless ($self->{knob_w}) { 3180 unless ($self->{knob_w}) {
3139 $self->set_value ($self->{range}[0]); 3181 $self->set_value ($self->{range}[0]);
3140 3182
3141 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3183 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3142 my $range = ($hi - $page - $lo) || 1e-100; 3184 my $range = ($hi - $page - $lo) || 1e-10;
3143 3185
3144 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3186 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3145 3187
3146 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3188 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3147 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3189 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3148 3190
3149 $value = ($value - $lo) / $range; 3191 $value = ($value - $lo) / $range;
3609 3651
3610 $tip =~ s/^\n+//; 3652 $tip =~ s/^\n+//;
3611 $tip =~ s/\n+$//; 3653 $tip =~ s/\n+$//;
3612 3654
3613 $self->add (new DC::UI::Label 3655 $self->add (new DC::UI::Label
3656 fg => $DC::THEME{tooltip_fg},
3614 markup => $tip, 3657 markup => $tip,
3615 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3658 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3616 align => 0, 3659 align => 0,
3617 fontsize => 0.8, 3660 fontsize => 0.8,
3618 style => 1, # FLAG_INVERSE 3661 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3619 ellipsise => 0, 3662 ellipsise => 0,
3620 font => ($widget->{tooltip_font} || $::FONT_PROP), 3663 font => ($widget->{tooltip_font} || $::FONT_PROP),
3621 ); 3664 );
3622} 3665}
3623 3666
3660sub _draw { 3703sub _draw {
3661 my ($self) = @_; 3704 my ($self) = @_;
3662 3705
3663 my ($w, $h) = @$self{qw(w h)}; 3706 my ($w, $h) = @$self{qw(w h)};
3664 3707
3665 glColor 1, 0.8, 0.4; 3708 glColor @{ $DC::THEME{tooltip_bg} };
3666 glRect 0, 0, $w, $h; 3709 glRect 0, 0, $w, $h;
3667 3710
3668 glColor 0, 0, 0; 3711 glColor @{ $DC::THEME{tooltip_border} };
3669 glRect_lineloop .5, .5, $w + .5, $h + .5; 3712 glRect_lineloop .5, .5, $w + .5, $h + .5;
3670 3713
3671 glTranslate 2, 2; 3714 glTranslate 2, 2;
3672 3715
3673 $self->SUPER::_draw; 3716 $self->SUPER::_draw;
3690 aspect => 1, 3733 aspect => 1,
3691 can_events => 0, 3734 can_events => 0,
3692 @_, 3735 @_,
3693 ); 3736 );
3694 3737
3695 if ($self->{anim} && $self->{animspeed}) {
3696 DC::weaken (my $widget = $self);
3697
3698 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3699 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3700 return unless $::CONN;
3701
3702 my $w = $widget
3703 or return;
3704
3705 ++$w->{frame};
3706 $w->update_face;
3707
3708 # somehow, $widget can go away
3709 $w->update;
3710 $w->update_timer;
3711 };
3712
3713 $self->update_face;
3714 $self->update_timer; 3738 $self->update_anim;
3715 }
3716 3739
3717 $self 3740 $self
3718} 3741}
3719 3742
3720sub update_timer { 3743sub update_timer {
3743 $tex->upload (sub { $self->reconfigure }); 3766 $tex->upload (sub { $self->reconfigure });
3744 } 3767 }
3745 } 3768 }
3746 } 3769 }
3747 } 3770 }
3771 }
3772}
3773
3774sub update_anim {
3775 my ($self) = @_;
3776
3777 if ($self->{anim} && $self->{animspeed}) {
3778 DC::weaken (my $widget = $self);
3779
3780 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3781 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3782 return unless $::CONN;
3783
3784 my $w = $widget
3785 or return;
3786
3787 ++$w->{frame};
3788 $w->update_face;
3789
3790 # somehow, $widget can go away
3791 $w->update;
3792 $w->update_timer;
3793 };
3794
3795 $self->update_face;
3796 $self->update_timer;
3797 } else {
3798 delete $self->{timer};
3748 } 3799 }
3749} 3800}
3750 3801
3751sub size_request { 3802sub size_request {
3752 my ($self) = @_; 3803 my ($self) = @_;
3774 return unless $self->{visible}; 3825 return unless $self->{visible};
3775 3826
3776 $self->SUPER::update; 3827 $self->SUPER::update;
3777} 3828}
3778 3829
3830sub set_face {
3831 my ($self, $face) = @_;
3832
3833 $self->{face} = $face;
3834 $self->reconfigure;
3835}
3836
3837sub set_anim {
3838 my ($self, $anim) = @_;
3839
3840 $self->{anim} = $anim;
3841 $self->update_anim;
3842}
3843
3844sub set_animspeed {
3845 my ($self, $animspeed) = @_;
3846
3847 $self->{animspeed} = $animspeed;
3848 $self->update_anim;
3849}
3850
3779sub invoke_visibility_change { 3851sub invoke_visibility_change {
3780 my ($self) = @_; 3852 my ($self) = @_;
3781 3853
3782 $self->update_timer; 3854 $self->update_timer;
3783 3855
3845 $widget = new DC::UI::HBox 3917 $widget = new DC::UI::HBox
3846 can_hover => 1, 3918 can_hover => 1,
3847 can_events => 1, 3919 can_events => 1,
3848 tooltip => $tooltip, 3920 tooltip => $tooltip,
3849 children => [ 3921 children => [
3850 (new DC::UI::Label markup => $left, expand => 1), 3922 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3851 (new DC::UI::Label markup => $right, align => 1), 3923 (new DC::UI::Label markup => $right, align => 1),
3852 ], 3924 ],
3853 ; 3925 ;
3854 3926
3855 } else { 3927 } else {
3879 # maybe save $GRAB? must be careful about events... 3951 # maybe save $GRAB? must be careful about events...
3880 $GRAB = $self; 3952 $GRAB = $self;
3881 $self->{button} = $ev->{button}; 3953 $self->{button} = $ev->{button};
3882 3954
3883 $self->show; 3955 $self->show;
3884 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3956
3957 my $x = $ev->{x};
3958 my $y = $ev->{y};
3959
3960 $self->{root}->on_post_alloc ($self => sub {
3961 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3962 });
3963
3964 1 # so it can be used inside event handlers
3885} 3965}
3886 3966
3887sub invoke_mouse_motion { 3967sub invoke_mouse_motion {
3888 my ($self, $ev, $x, $y) = @_; 3968 my ($self, $ev, $x, $y) = @_;
3889 3969
3921 4001
3922 my $self = $class->SUPER::new ( 4002 my $self = $class->SUPER::new (
3923 @_, 4003 @_,
3924 ); 4004 );
3925 4005
3926 $self->{current} = $self->{children}[0] 4006 $self->set_current_page (0);
3927 if @{ $self->{children} };
3928 4007
3929 $self 4008 $self
3930} 4009}
3931 4010
3932sub add { 4011sub add {
3933 my ($self, @widgets) = @_; 4012 my ($self, @widgets) = @_;
3934 4013
3935 $self->SUPER::add (@widgets); 4014 $self->SUPER::add (@widgets);
3936 4015
3937 $self->{current} = $self->{children}[0] 4016 $self->set_current_page (0)
3938 if @{ $self->{children} }; 4017 if @widgets == @{ $self->{children} };
3939} 4018}
3940 4019
3941sub get_current_page { 4020sub get_current_page {
3942 my ($self) = @_; 4021 my ($self) = @_;
3943 4022
3949 4028
3950 my $widget = ref $page_or_widget 4029 my $widget = ref $page_or_widget
3951 ? $page_or_widget 4030 ? $page_or_widget
3952 : $self->{children}[$page_or_widget]; 4031 : $self->{children}[$page_or_widget];
3953 4032
4033 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4034
3954 $self->{current} = $widget; 4035 if (($self->{current} = $widget)) {
4036 $self->{current}->set_visible if $self->{current} && $self->{visible};
3955 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4037 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3956 4038
3957 $self->emit (page_changed => $self->{current}); 4039 $self->emit (page_changed => $self->{current});
4040 }
3958 4041
3959 $self->realloc; 4042 $self->realloc;
3960} 4043}
3961 4044
3962sub visible_children { 4045sub visible_children {
3963 $_[0]{current} 4046 $_[0]{current} || ()
3964} 4047}
3965 4048
3966sub size_request { 4049sub size_request {
3967 my ($self) = @_; 4050 my ($self) = @_;
3968 4051
4052 $self->{current}
3969 $self->{current}->size_request 4053 ? $self->{current}->size_request
4054 : (0, 0)
3970} 4055}
3971 4056
3972sub invoke_size_allocate { 4057sub invoke_size_allocate {
3973 my ($self, $w, $h) = @_; 4058 my ($self, $w, $h) = @_;
3974 4059
3975 $self->{current}->configure (0, 0, $w, $h); 4060 $self->{current}->configure (0, 0, $w, $h)
4061 if $self->{current};
3976 4062
3977 1 4063 1
3978} 4064}
3979 4065
3980sub _draw { 4066sub _draw {
3981 my ($self) = @_; 4067 my ($self) = @_;
3982 4068
3983 $self->{current}->draw; 4069 $self->{current}->draw
4070 if $self->{current};
3984} 4071}
3985 4072
3986############################################################################# 4073#############################################################################
3987 4074
3988package DC::UI::Notebook; 4075package DC::UI::Notebook;
4055} 4142}
4056 4143
4057sub pages { 4144sub pages {
4058 my ($self) = @_; 4145 my ($self) = @_;
4059 $self->{multiplexer}->children 4146 $self->{multiplexer}->children
4147}
4148
4149sub page_index {
4150 my ($self, $widget) = @_;
4151
4152 my $i = 0;
4153 for ($self->pages) {
4154 if ($_ eq $widget) { return $i };
4155 $i++;
4156 }
4157
4158 undef
4060} 4159}
4061 4160
4062sub add_tab { 4161sub add_tab {
4063 my ($self, $title, $widget, $tooltip) = @_; 4162 my ($self, $title, $widget, $tooltip) = @_;
4064 4163
4186 $self 4285 $self
4187} 4286}
4188 4287
4189sub reorder { 4288sub reorder {
4190 my ($self) = @_; 4289 my ($self) = @_;
4191 my $NOW = Time::HiRes::time; 4290 my $NOW = EV::time;
4192 4291
4193 # freeze display when hovering over any label 4292 # freeze display when hovering over any label
4194 return if $DC::UI::TOOLTIP->{owner} 4293 return if $DC::UI::TOOLTIP->{owner}
4195 && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, 4294 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4196 values %{ $self->{item} }; 4295 values %{ $self->{item} };
4246 $label->{fg}[3] = $item->{fg}[3] || 1; 4345 $label->{fg}[3] = $item->{fg}[3] || 1;
4247 } 4346 }
4248 4347
4249 push @widgets, $label; 4348 push @widgets, $label;
4250 } 4349 }
4350
4351 my $hash = join ",", @widgets;
4352 return if $hash eq $self->{last_widget_hash};
4353 $self->{last_widget_hash} = $hash;
4251 4354
4252 $self->clear; 4355 $self->clear;
4253 $self->SUPER::add (reverse @widgets); 4356 $self->SUPER::add (reverse @widgets);
4254} 4357}
4255 4358
4556 4659
4557############################################################################# 4660#############################################################################
4558 4661
4559package DC::UI; 4662package DC::UI;
4560 4663
4561$ROOT = new DC::UI::Root; 4664$ROOT = new DC::UI::Root;
4562$TOOLTIP = new DC::UI::Tooltip z => 900; 4665$TOOLTIP = new DC::UI::Tooltip z => 900;
4563 4666
45641 46671
4565

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines