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.481 by root, Tue Oct 12 05:11:38 2010 UTC

1package DC::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use List::Util (); 5use List::Util ();
6
7use Guard ();
7 8
8use DC; 9use DC;
9use DC::Pod; 10use DC::Pod;
10use DC::Texture; 11use DC::Texture;
11 12
189# call when resolution changes etc. 190# call when resolution changes etc.
190sub rescale_widgets { 191sub 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
224package DC::UI::Base; 225package DC::UI::Base;
225 226
226use strict; 227use common::sense;
227 228
228use DC::OpenGL; 229use DC::OpenGL;
229 230
230sub new { 231sub new {
231 my $class = shift; 232 my $class = shift;
286sub set_visible { 287sub 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
301sub set_invisible { 305sub set_invisible {
302 my ($self) = @_; 306 my ($self) = @_;
303 307
527sub connect { 531sub 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
639package DC::UI::DrawBG; 643package DC::UI::DrawBG;
640 644
641our @ISA = DC::UI::Base::; 645our @ISA = DC::UI::Base::;
642 646
643use strict; 647use common::sense;
648
644use DC::OpenGL; 649use DC::OpenGL;
645 650
646sub new { 651sub 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
661sub set_bg {
662 my ($self, $bg) = @_;
663
664 $self->{bg} = $bg;
665 $self->update;
666}
667
656sub _draw { 668sub _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;
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
2566 2594
2567############################################################################# 2595#############################################################################
2568 2596
2569package DC::UI::Image; 2597package DC::UI::Image;
2570 2598
2571our @ISA = DC::UI::Base::; 2599our @ISA = DC::UI::DrawBG::;
2572 2600
2573use DC::OpenGL; 2601use DC::OpenGL;
2574 2602
2575our %texture_cache; 2603our %texture_cache;
2576 2604
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
2609 my ($self, $cloning, $path) = @_; 2637 my ($self, $cloning, $path) = @_;
2610 2638
2611 $self->new (path => $path) 2639 $self->new (path => $path)
2612} 2640}
2613 2641
2642sub set_texture {
2643 my ($self, $tex) = @_;
2644
2645 $self->{tex} = $tex;
2646 $self->update;
2647}
2648
2614sub size_request { 2649sub size_request {
2615 my ($self) = @_; 2650 my ($self) = @_;
2616 2651
2617 (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})
2618} 2653}
2619 2654
2620sub _draw { 2655sub _draw {
2621 my ($self) = @_; 2656 my ($self) = @_;
2657
2658 $self->SUPER::_draw;
2622 2659
2623 my $tex = $self->{tex}; 2660 my $tex = $self->{tex};
2624 2661
2625 my ($w, $h) = ($self->{w}, $self->{h}); 2662 my ($w, $h) = ($self->{w}, $self->{h});
2626 2663
2644package DC::UI::ImageButton; 2681package DC::UI::ImageButton;
2645 2682
2646our @ISA = DC::UI::Image::; 2683our @ISA = DC::UI::Image::;
2647 2684
2648use DC::OpenGL; 2685use DC::OpenGL;
2649
2650my %textures;
2651 2686
2652sub new { 2687sub new {
2653 my $class = shift; 2688 my $class = shift;
2654 2689
2655 my $self = $class->SUPER::new ( 2690 my $self = $class->SUPER::new (
2691 2726
2692use DC::OpenGL; 2727use DC::OpenGL;
2693 2728
2694my %tex = ( 2729my %tex = (
2695 food => [ 2730 food => [
2696 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2731 map { new_from_resource DC::Texture $_, mipmap => 1 }
2697 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2732 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2698 ], 2733 ],
2699 grace => [ 2734 grace => [
2700 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2735 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/ 2736 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2702 ], 2737 ],
2703 hp => [ 2738 hp => [
2704 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2739 map { new_from_resource DC::Texture $_, mipmap => 1 }
2705 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2740 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2706 ], 2741 ],
2707 mana => [ 2742 mana => [
2708 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } 2743 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/ 2744 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2710 ], 2745 ],
2711); 2746);
2712 2747
2713# eg. VGauge->new (gauge => 'food'), default gauge: food 2748# eg. VGauge->new (gauge => 'food'), default gauge: food
2826 2861
2827sub new { 2862sub new {
2828 my ($class, %arg) = @_; 2863 my ($class, %arg) = @_;
2829 2864
2830 my $self = $class->SUPER::new ( 2865 my $self = $class->SUPER::new (
2866 padding_x => 2,
2867 padding_y => 2,
2831 fg => [1, 1, 1], 2868 fg => [1, 1, 1],
2832 bg => [0, 0, 1, 0.2], 2869 bg => [0, 0, 1, 0.2],
2833 bar => [0.7, 0.5, 0.1, 0.8], 2870 bar => [0.7, 0.5, 0.1, 0.8],
2834 outline => [0.4, 0.3, 0], 2871 outline => [0.4, 0.3, 0],
2835 fontsize => 0.9, 2872 fontsize => 0.9,
2875 my ($self) = @_; 2912 my ($self) = @_;
2876 2913
2877 glEnable GL_BLEND; 2914 glEnable GL_BLEND;
2878 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2915 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2879 2916
2917 my $px = $self->{padding_x};
2918 my $py = $self->{padding_y};
2919
2880 if ($self->{value} >= 0) { 2920 if ($self->{value} >= 0) {
2881 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2921 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2882 2922
2883 glColor_premultiply @{$self->{bar}}; 2923 glColor_premultiply @{$self->{bar}};
2884 glRect 2, 2, $s, $self->{h} - 2; 2924 glRect $px, $py, $s, $self->{h} - $py;
2885 glColor_premultiply @{$self->{bg}}; 2925 glColor_premultiply @{$self->{bg}};
2886 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2926 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2887 } 2927 }
2888 2928
2889 glColor_premultiply @{$self->{outline}}; 2929 glColor_premultiply @{$self->{outline}};
2930
2931 $px -= .5;
2932 $py -= .5;
2933
2890 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;
2891 2935
2892 glDisable GL_BLEND; 2936 glDisable GL_BLEND;
2893 2937
2894 { 2938 {
2895 local $self->{bg}; # do not draw background 2939 local $self->{bg}; # do not draw background
2904our @ISA = DC::UI::Progress::; 2948our @ISA = DC::UI::Progress::;
2905 2949
2906sub new { 2950sub new {
2907 my ($class, %arg) = @_; 2951 my ($class, %arg) = @_;
2908 2952
2953 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2954
2909 my $self = $class->SUPER::new ( 2955 my $self = $class->SUPER::new (
2956 %arg,
2910 tooltip => sub { 2957 tooltip => sub {
2911 my ($self) = @_; 2958 my ($self) = @_;
2912 2959
2913 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,
2914 $self->{lvl}, 2962 $self->{lvl},
2915 ::formsep ($self->{exp}), 2963 ::formsep ($self->{exp}),
2916 ::formsep ($self->{nxt}), 2964 ::formsep ($self->{nxt}),
2917 ::formsep ($self->{nxt} - $self->{exp}), 2965 ::formsep ($self->{nxt} - $self->{exp}),
2966 $self->_percent * 100,
2918 }, 2967 },
2919 %arg
2920 ); 2968 );
2921 2969
2922 $::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}) }
2923 if $::CONN; 2971 if $::CONN;
2924 2972
2932 if $::CONN; 2980 if $::CONN;
2933 2981
2934 $self->SUPER::DESTROY; 2982 $self->SUPER::DESTROY;
2935} 2983}
2936 2984
2985sub _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
2937sub set_value { 2999sub set_value {
2938 my ($self, $lvl, $exp) = @_; 3000 my ($self, $lvl, $exp) = @_;
2939 3001
2940 $self->{lvl} = $lvl; 3002 $self->{lvl} = $lvl;
2941 $self->{exp} = $exp; 3003 $self->{exp} = $exp;
2942 3004
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); 3005 $self->SUPER::set_value ($self->_percent);
2955} 3006}
2956 3007
2957############################################################################# 3008#############################################################################
2958 3009
2959package DC::UI::Gauge; 3010package DC::UI::Gauge;
3003 3054
3004############################################################################# 3055#############################################################################
3005 3056
3006package DC::UI::Slider; 3057package DC::UI::Slider;
3007 3058
3008use strict; 3059use common::sense;
3009 3060
3010use DC::OpenGL; 3061use DC::OpenGL;
3011 3062
3012our @ISA = DC::UI::DrawBG::; 3063our @ISA = DC::UI::DrawBG::;
3013 3064
3014my @tex = 3065my @tex =
3015 map { new_from_file DC::Texture DC::find_rcfile $_ } 3066 map { new_from_resource DC::Texture $_ }
3016 qw(s1_slider.png s1_slider_bg.png); 3067 qw(s1_slider.png s1_slider_bg.png);
3017 3068
3018sub new { 3069sub new {
3019 my $class = shift; 3070 my $class = shift;
3020 3071
3057sub set_value { 3108sub set_value {
3058 my ($self, $value) = @_; 3109 my ($self, $value) = @_;
3059 3110
3060 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3111 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3061 3112
3062 $hi = $lo + 1 if $hi <= $lo; 3113 $hi = $lo if $hi < $lo;
3063 3114
3064 $page = $hi - $lo if $page > $hi - $lo; 3115 $value = $hi - $page if $value > $hi - $page;
3065
3066 $value = $lo if $value < $lo; 3116 $value = $lo if $value < $lo;
3067 $value = $hi - $page if $value > $hi - $page;
3068 3117
3069 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3118 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3070 if $unit; 3119 if $unit;
3071 3120
3072 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3121 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3101 if ($GRAB == $self) { 3150 if ($GRAB == $self) {
3102 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3151 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3103 3152
3104 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3153 my (undef, $lo, $hi, $page) = @{$self->{range}};
3105 3154
3106 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3155 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3107 3156
3108 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3157 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3109 } else { 3158 } else {
3110 return 0; 3159 return 0;
3111 } 3160 }
3136 my ($self) = @_; 3185 my ($self) = @_;
3137 3186
3138 unless ($self->{knob_w}) { 3187 unless ($self->{knob_w}) {
3139 $self->set_value ($self->{range}[0]); 3188 $self->set_value ($self->{range}[0]);
3140 3189
3141 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3190 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3142 my $range = ($hi - $page - $lo) || 1e-100; 3191 my $range = ($hi - $page - $lo) || 1e-10;
3143 3192
3144 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};
3145 3194
3146 $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;
3147 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3196 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3148 3197
3149 $value = ($value - $lo) / $range; 3198 $value = ($value - $lo) / $range;
3609 3658
3610 $tip =~ s/^\n+//; 3659 $tip =~ s/^\n+//;
3611 $tip =~ s/\n+$//; 3660 $tip =~ s/\n+$//;
3612 3661
3613 $self->add (new DC::UI::Label 3662 $self->add (new DC::UI::Label
3663 fg => $DC::THEME{tooltip_fg},
3614 markup => $tip, 3664 markup => $tip,
3615 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3665 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3616 align => 0, 3666 align => 0,
3617 fontsize => 0.8, 3667 fontsize => 0.8,
3618 style => 1, # FLAG_INVERSE 3668 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3619 ellipsise => 0, 3669 ellipsise => 0,
3620 font => ($widget->{tooltip_font} || $::FONT_PROP), 3670 font => ($widget->{tooltip_font} || $::FONT_PROP),
3621 ); 3671 );
3622} 3672}
3623 3673
3660sub _draw { 3710sub _draw {
3661 my ($self) = @_; 3711 my ($self) = @_;
3662 3712
3663 my ($w, $h) = @$self{qw(w h)}; 3713 my ($w, $h) = @$self{qw(w h)};
3664 3714
3665 glColor 1, 0.8, 0.4; 3715 glColor @{ $DC::THEME{tooltip_bg} };
3666 glRect 0, 0, $w, $h; 3716 glRect 0, 0, $w, $h;
3667 3717
3668 glColor 0, 0, 0; 3718 glColor @{ $DC::THEME{tooltip_border} };
3669 glRect_lineloop .5, .5, $w + .5, $h + .5; 3719 glRect_lineloop .5, .5, $w + .5, $h + .5;
3670 3720
3671 glTranslate 2, 2; 3721 glTranslate 2, 2;
3672 3722
3673 $self->SUPER::_draw; 3723 $self->SUPER::_draw;
3690 aspect => 1, 3740 aspect => 1,
3691 can_events => 0, 3741 can_events => 0,
3692 @_, 3742 @_,
3693 ); 3743 );
3694 3744
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; 3745 $self->update_anim;
3715 }
3716 3746
3717 $self 3747 $self
3718} 3748}
3719 3749
3720sub update_timer { 3750sub update_timer {
3743 $tex->upload (sub { $self->reconfigure }); 3773 $tex->upload (sub { $self->reconfigure });
3744 } 3774 }
3745 } 3775 }
3746 } 3776 }
3747 } 3777 }
3778 }
3779}
3780
3781sub 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};
3748 } 3806 }
3749} 3807}
3750 3808
3751sub size_request { 3809sub size_request {
3752 my ($self) = @_; 3810 my ($self) = @_;
3774 return unless $self->{visible}; 3832 return unless $self->{visible};
3775 3833
3776 $self->SUPER::update; 3834 $self->SUPER::update;
3777} 3835}
3778 3836
3837sub set_face {
3838 my ($self, $face) = @_;
3839
3840 $self->{face} = $face;
3841 $self->reconfigure;
3842}
3843
3844sub set_anim {
3845 my ($self, $anim) = @_;
3846
3847 $self->{anim} = $anim;
3848 $self->update_anim;
3849}
3850
3851sub set_animspeed {
3852 my ($self, $animspeed) = @_;
3853
3854 $self->{animspeed} = $animspeed;
3855 $self->update_anim;
3856}
3857
3779sub invoke_visibility_change { 3858sub invoke_visibility_change {
3780 my ($self) = @_; 3859 my ($self) = @_;
3781 3860
3782 $self->update_timer; 3861 $self->update_timer;
3783 3862
3845 $widget = new DC::UI::HBox 3924 $widget = new DC::UI::HBox
3846 can_hover => 1, 3925 can_hover => 1,
3847 can_events => 1, 3926 can_events => 1,
3848 tooltip => $tooltip, 3927 tooltip => $tooltip,
3849 children => [ 3928 children => [
3850 (new DC::UI::Label markup => $left, expand => 1), 3929 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3851 (new DC::UI::Label markup => $right, align => 1), 3930 (new DC::UI::Label markup => $right, align => 1),
3852 ], 3931 ],
3853 ; 3932 ;
3854 3933
3855 } else { 3934 } else {
3879 # maybe save $GRAB? must be careful about events... 3958 # maybe save $GRAB? must be careful about events...
3880 $GRAB = $self; 3959 $GRAB = $self;
3881 $self->{button} = $ev->{button}; 3960 $self->{button} = $ev->{button};
3882 3961
3883 $self->show; 3962 $self->show;
3884 $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
3885} 3972}
3886 3973
3887sub invoke_mouse_motion { 3974sub invoke_mouse_motion {
3888 my ($self, $ev, $x, $y) = @_; 3975 my ($self, $ev, $x, $y) = @_;
3889 3976
3921 4008
3922 my $self = $class->SUPER::new ( 4009 my $self = $class->SUPER::new (
3923 @_, 4010 @_,
3924 ); 4011 );
3925 4012
3926 $self->{current} = $self->{children}[0] 4013 $self->set_current_page (0);
3927 if @{ $self->{children} };
3928 4014
3929 $self 4015 $self
3930} 4016}
3931 4017
3932sub add { 4018sub add {
3933 my ($self, @widgets) = @_; 4019 my ($self, @widgets) = @_;
3934 4020
3935 $self->SUPER::add (@widgets); 4021 $self->SUPER::add (@widgets);
3936 4022
3937 $self->{current} = $self->{children}[0] 4023 $self->set_current_page (0)
3938 if @{ $self->{children} }; 4024 if @widgets == @{ $self->{children} };
3939} 4025}
3940 4026
3941sub get_current_page { 4027sub get_current_page {
3942 my ($self) = @_; 4028 my ($self) = @_;
3943 4029
3949 4035
3950 my $widget = ref $page_or_widget 4036 my $widget = ref $page_or_widget
3951 ? $page_or_widget 4037 ? $page_or_widget
3952 : $self->{children}[$page_or_widget]; 4038 : $self->{children}[$page_or_widget];
3953 4039
4040 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4041
3954 $self->{current} = $widget; 4042 if (($self->{current} = $widget)) {
4043 $self->{current}->set_visible if $self->{current} && $self->{visible};
3955 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4044 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3956 4045
3957 $self->emit (page_changed => $self->{current}); 4046 $self->emit (page_changed => $self->{current});
4047 }
3958 4048
3959 $self->realloc; 4049 $self->realloc;
3960} 4050}
3961 4051
3962sub visible_children { 4052sub visible_children {
3963 $_[0]{current} 4053 $_[0]{current} || ()
3964} 4054}
3965 4055
3966sub size_request { 4056sub size_request {
3967 my ($self) = @_; 4057 my ($self) = @_;
3968 4058
4059 $self->{current}
3969 $self->{current}->size_request 4060 ? $self->{current}->size_request
4061 : (0, 0)
3970} 4062}
3971 4063
3972sub invoke_size_allocate { 4064sub invoke_size_allocate {
3973 my ($self, $w, $h) = @_; 4065 my ($self, $w, $h) = @_;
3974 4066
3975 $self->{current}->configure (0, 0, $w, $h); 4067 $self->{current}->configure (0, 0, $w, $h)
4068 if $self->{current};
3976 4069
3977 1 4070 1
3978} 4071}
3979 4072
3980sub _draw { 4073sub _draw {
3981 my ($self) = @_; 4074 my ($self) = @_;
3982 4075
3983 $self->{current}->draw; 4076 $self->{current}->draw
4077 if $self->{current};
3984} 4078}
3985 4079
3986############################################################################# 4080#############################################################################
3987 4081
3988package DC::UI::Notebook; 4082package DC::UI::Notebook;
4055} 4149}
4056 4150
4057sub pages { 4151sub pages {
4058 my ($self) = @_; 4152 my ($self) = @_;
4059 $self->{multiplexer}->children 4153 $self->{multiplexer}->children
4154}
4155
4156sub page_index {
4157 my ($self, $widget) = @_;
4158
4159 my $i = 0;
4160 for ($self->pages) {
4161 if ($_ eq $widget) { return $i };
4162 $i++;
4163 }
4164
4165 undef
4060} 4166}
4061 4167
4062sub add_tab { 4168sub add_tab {
4063 my ($self, $title, $widget, $tooltip) = @_; 4169 my ($self, $title, $widget, $tooltip) = @_;
4064 4170
4186 $self 4292 $self
4187} 4293}
4188 4294
4189sub reorder { 4295sub reorder {
4190 my ($self) = @_; 4296 my ($self) = @_;
4191 my $NOW = Time::HiRes::time; 4297 my $NOW = EV::time;
4192 4298
4193 # freeze display when hovering over any label 4299 # freeze display when hovering over any label
4194 return if $DC::UI::TOOLTIP->{owner} 4300 return if $DC::UI::TOOLTIP->{owner}
4195 && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, 4301 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4196 values %{ $self->{item} }; 4302 values %{ $self->{item} };
4246 $label->{fg}[3] = $item->{fg}[3] || 1; 4352 $label->{fg}[3] = $item->{fg}[3] || 1;
4247 } 4353 }
4248 4354
4249 push @widgets, $label; 4355 push @widgets, $label;
4250 } 4356 }
4357
4358 my $hash = join ",", @widgets;
4359 return if $hash eq $self->{last_widget_hash};
4360 $self->{last_widget_hash} = $hash;
4251 4361
4252 $self->clear; 4362 $self->clear;
4253 $self->SUPER::add (reverse @widgets); 4363 $self->SUPER::add (reverse @widgets);
4254} 4364}
4255 4365
4556 4666
4557############################################################################# 4667#############################################################################
4558 4668
4559package DC::UI; 4669package DC::UI;
4560 4670
4561$ROOT = new DC::UI::Root; 4671$ROOT = new DC::UI::Root;
4562$TOOLTIP = new DC::UI::Tooltip z => 900; 4672$TOOLTIP = new DC::UI::Tooltip z => 900;
4563 4673
45641 46741
4565

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines