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.195 by root, Thu May 11 23:54:31 2006 UTC vs.
Revision 1.200 by root, Sat May 13 00:27:09 2006 UTC

24 24
25 my $tip = $widget->{tooltip}; 25 my $tip = $widget->{tooltip};
26 26
27 $tip = $tip->($widget) if CODE:: eq ref $tip; 27 $tip = $tip->($widget) if CODE:: eq ref $tip;
28 28
29 $TOOLTIP->set_markup ($widget->{tooltip}, $widget->{tooltip_font}); 29 $TOOLTIP->set_tooltip_from ($widget);
30
31 $TOOLTIP->show; 30 $TOOLTIP->show;
32 31
33 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
34 33
35 if ($x + $TOOLTIP->{w} > $::WIDTH) { 34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
37 } 36 }
38 37
39 $TOOLTIP->move ($x, $y); 38 $TOOLTIP->move ($x, $y);
39 $TOOLTIP->check_size;
40 $TOOLTIP->update;
40 } 41 }
41 42
42 return; 43 return;
43 } 44 }
44 } 45 }
251 my ($self) = @_; 252 my ($self) = @_;
252 253
253 $_->reconfigure 254 $_->reconfigure
254 for $self->children; 255 for $self->children;
255 256
256 $self->check_size; 257 $self->check_size (1);
257 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
258 $self->update; 258 $self->update;
259} 259}
260 260
261sub set_max_size { 261sub set_max_size {
262 my ($self, $w, $h) = @_; 262 my ($self, $w, $h) = @_;
383sub set_parent { 383sub set_parent {
384 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
385 385
386 Scalar::Util::weaken ($self->{parent} = $parent); 386 Scalar::Util::weaken ($self->{parent} = $parent);
387 387
388 # TODO: req_w _does_change after ->reconfigure
388 $self->check_size 389 $self->check_size
389 unless exists $self->{req_w}; 390 unless exists $self->{req_w};
390} 391}
391 392
392sub check_size { 393sub check_size {
393 my ($self) = @_; 394 my ($self, $forced) = @_;
394 395
396 $self->{force_alloc} = 1 if $forced;
395 $CFClient::UI::ROOT->{check_size}{$self} = $self; 397 $CFClient::UI::ROOT->{check_size}{$self} = $self;
396} 398}
397 399
398sub update { 400sub update {
399 my ($self) = @_; 401 my ($self) = @_;
509 $self->{children} = [ 511 $self->{children} = [
510 sort { $a->{z} <=> $b->{z} } 512 sort { $a->{z} <=> $b->{z} }
511 @{$self->{children}}, @widgets 513 @{$self->{children}}, @widgets
512 ]; 514 ];
513 515
514 $self->check_size; 516 $self->check_size (1);
515 $self->update; 517 $self->update;
516} 518}
517 519
518sub children { 520sub children {
519 @{ $_[0]{children} } 521 @{ $_[0]{children} }
626} 628}
627 629
628sub update { 630sub update {
629 my ($self) = @_; 631 my ($self) = @_;
630 632
631 $ROOT->on_refresh ($self => sub { $self->render_child }); 633 $ROOT->on_post_alloc ($self => sub { $self->render_child });
632 $self->SUPER::update; 634 $self->SUPER::update;
633} 635}
634 636
635sub size_allocate { 637sub size_allocate {
636 my ($self, $w, $h) = @_; 638 my ($self, $w, $h) = @_;
774 $self->add ($self->{slider}); 776 $self->add ($self->{slider});
775 777
776 $self 778 $self
777} 779}
778 780
779#TODO# update range on size_allocate depeneing on child 781#TODO# update range on size_allocate depending on child
780# update viewport offset on scroll 782# update viewport offset on scroll
781 783
782############################################################################# 784#############################################################################
783 785
784package CFClient::UI::Frame; 786package CFClient::UI::Frame;
788use CFClient::OpenGL; 790use CFClient::OpenGL;
789 791
790sub new { 792sub new {
791 my $class = shift; 793 my $class = shift;
792 794
793 my $self = $class->SUPER::new ( 795 $class->SUPER::new (
794 bg => [1, 1, 1, 1], 796 bg => undef,
795 border_bg => [1, 1, 1, 1],
796 border => 0.8,
797 @_ 797 @_,
798 ); 798 )
799
800 $self
801} 799}
802 800
803sub _draw { 801sub _draw {
804 my ($self) = @_; 802 my ($self) = @_;
805 803
806 my ($w, $h) = ($self->{w}, $self->{h}); 804 if ($self->{bg}) {
805 my ($w, $h) = @$self{qw(w h)};
807 806
808 glEnable GL_BLEND; 807 glEnable GL_BLEND;
809 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 808 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
810 glEnable GL_TEXTURE_2D; 809 glColor @{ $self->{bg} };
811 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
812 810
813# glBegin GL_QUADS; 811 glBegin GL_QUADS;
814# glColor 0, 0, 0, 0;
815# glVertex 0 , 0; 812 glVertex 0 , 0;
816# glVertex 0 , $h; 813 glVertex 0 , $h;
817# glVertex $w, $h; 814 glVertex $w, $h;
818# glVertex $w, 0; 815 glVertex $w, 0;
819# glEnd; 816 glEnd;
820 817
821
822 $self->child->draw;
823 glDisable GL_BLEND; 818 glDisable GL_BLEND;
824 glDisable GL_TEXTURE_2D; 819 }
820
821 $self->SUPER::_draw;
825} 822}
826 823
827############################################################################# 824#############################################################################
828 825
829package CFClient::UI::FancyFrame; 826package CFClient::UI::FancyFrame;
945 my ($self) = @_; 942 my ($self) = @_;
946 943
947 my ($w, $h ) = ($self->{w}, $self->{h}); 944 my ($w, $h ) = ($self->{w}, $self->{h});
948 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 945 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
949 946
950 glEnable GL_BLEND;
951 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
952 glEnable GL_TEXTURE_2D; 947 glEnable GL_TEXTURE_2D;
953 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 948 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
954 949
955 my $border = $self->border; 950 my $border = $self->border;
956 951
957 glColor @{ $self->{border_bg} }; 952 glColor @{ $self->{border_bg} };
958 $tex[1]->draw_quad (0, 0, $w, $border); 953 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
959 $tex[3]->draw_quad (0, $border, $border, $ch); 954 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
960 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 955 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
961 $tex[4]->draw_quad (0, $h - $border, $w, $border); 956 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
962 957
963 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 958 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
964 my $bg = $tex[0]; 959 my $bg = $tex[0];
965 960
966 # TODO: repeat texture not scale 961 # TODO: repeat texture not scale
970 glColor @{ $self->{bg} }; 965 glColor @{ $self->{bg} };
971 966
972 $bg->{s} = $rep_x; 967 $bg->{s} = $rep_x;
973 $bg->{t} = $rep_y; 968 $bg->{t} = $rep_y;
974 $bg->{wrap_mode} = 1; 969 $bg->{wrap_mode} = 1;
975 $bg->draw_quad ($border, $border, $cw, $ch); 970 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
971 }
976 972
977 glDisable GL_TEXTURE_2D; 973 glDisable GL_TEXTURE_2D;
978 glDisable GL_BLEND;
979 }
980 974
981 $self->{title}->draw if $self->{title}; 975 $self->{title}->draw if $self->{title};
982 976
983 $self->child->draw; 977 $self->child->draw;
984} 978}
1486 my $sym = $ev->{sym}; 1480 my $sym = $ev->{sym};
1487 my $uni = $ev->{unicode}; 1481 my $uni = $ev->{unicode};
1488 1482
1489 my $text = $self->get_text; 1483 my $text = $self->get_text;
1490 1484
1491 if ($sym == 8) { 1485 if ($uni == 8) {
1492 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1486 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1493 } elsif ($sym == 127) { 1487 } elsif ($uni == 127) {
1494 substr $text, $self->{cursor}, 1, ""; 1488 substr $text, $self->{cursor}, 1, "";
1495 } elsif ($sym == CFClient::SDLK_LEFT) { 1489 } elsif ($sym == CFClient::SDLK_LEFT) {
1496 --$self->{cursor} if $self->{cursor}; 1490 --$self->{cursor} if $self->{cursor};
1497 } elsif ($sym == CFClient::SDLK_RIGHT) { 1491 } elsif ($sym == CFClient::SDLK_RIGHT) {
1498 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1492 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1499 } elsif ($sym == CFClient::SDLK_HOME) { 1493 } elsif ($sym == CFClient::SDLK_HOME) {
1500 $self->{cursor} = 0; 1494 $self->{cursor} = 0;
1501 } elsif ($sym == CFClient::SDLK_END) { 1495 } elsif ($sym == CFClient::SDLK_END) {
1502 $self->{cursor} = length $text; 1496 $self->{cursor} = length $text;
1503 } elsif ($sym == 27) { 1497 } elsif ($uni == 27) {
1504 $self->emit ('escape'); 1498 $self->emit ('escape');
1505 } elsif ($uni) { 1499 } elsif ($uni) {
1506 substr $text, $self->{cursor}++, 0, chr $uni; 1500 substr $text, $self->{cursor}++, 0, chr $uni;
1507 } 1501 }
1508 1502
1735 1729
1736 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1730 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1737 1731
1738 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1732 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1739 1733
1734 my $tex = $self->{state} ? $tex[1] : $tex[0];
1735
1740 glEnable GL_TEXTURE_2D; 1736 glEnable GL_TEXTURE_2D;
1741
1742 my $tex = $self->{state} ? $tex[1] : $tex[0];
1743
1744 $tex->draw_quad_alpha (0, 0, $s, $s); 1737 $tex->draw_quad_alpha (0, 0, $s, $s);
1745
1746 glDisable GL_TEXTURE_2D; 1738 glDisable GL_TEXTURE_2D;
1747} 1739}
1748 1740
1749############################################################################# 1741#############################################################################
1750 1742
2205 2197
2206 return unless $self->{h} > 0; 2198 return unless $self->{h} > 0;
2207 2199
2208 delete $self->{texture}; 2200 delete $self->{texture};
2209 2201
2210 $ROOT->on_refresh ($self, sub { 2202 $ROOT->on_post_alloc ($self, sub {
2211 if (delete $self->{need_reflow}) { 2203 if (delete $self->{need_reflow}) {
2212 my $height = 0; 2204 my $height = 0;
2213 2205
2214 $height += $_->[0] = $self->text_height ($_->[2]) 2206 $height += $_->[0] = $self->text_height ($_->[2])
2215 for @{$self->{par}}; 2207 for @{$self->{par}};
2367 @_, 2359 @_,
2368 can_events => 0, 2360 can_events => 0,
2369 ) 2361 )
2370} 2362}
2371 2363
2372sub set_markup { 2364sub set_tooltip_from {
2373 my ($self, $text, $font) = @_; 2365 my ($self, $widget) = @_;
2374 2366
2375 $self->{label} = new CFClient::UI::Label 2367 $self->add (new CFClient::UI::Label
2368 markup => $widget->{tooltip},
2369 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2376 fontsize => 0.8, 2370 fontsize => 0.8,
2377 fg => [0, 0, 0], 2371 fg => [0, 0, 0, 1],
2378 font => ($font || $::FONT_PROP); 2372 font => ($widget->{tooltip_font} || $::FONT_PROP),
2379 2373 );
2380 $self->{label}->set_max_size ($::WIDTH * 0.3);
2381 $self->{label}->set_markup ($text);
2382 $self->add ($self->{label});
2383} 2374}
2384 2375
2385sub size_request { 2376sub size_request {
2386 my ($self) = @_; 2377 my ($self) = @_;
2387 2378
2397} 2388}
2398 2389
2399sub _draw { 2390sub _draw {
2400 my ($self) = @_; 2391 my ($self) = @_;
2401 2392
2402 glPushMatrix;
2403 glTranslate 0.375, 0.375; 2393 glTranslate 0.375, 0.375;
2404 2394
2405 my ($w, $h) = @$self{qw(w h)}; 2395 my ($w, $h) = @$self{qw(w h)};
2406 2396
2407 glColor 1, 0.8, 0.4; 2397 glColor 1, 0.8, 0.4;
2418 glVertex 0 , $h; 2408 glVertex 0 , $h;
2419 glVertex $w, $h; 2409 glVertex $w, $h;
2420 glVertex $w, 0; 2410 glVertex $w, 0;
2421 glEnd; 2411 glEnd;
2422 2412
2423 glPopMatrix; 2413 glTranslate 2 - 0.375, 2 - 0.375;
2424
2425 glTranslate 2, 2;
2426 $self->SUPER::_draw; 2414 $self->SUPER::_draw;
2427} 2415}
2428 2416
2429############################################################################# 2417#############################################################################
2430 2418
2657 while (my ($k, $v) = each %{ $self->{item} }) { 2645 while (my ($k, $v) = each %{ $self->{item} }) {
2658 delete $self->{item}{$k} if $v->{timeout} < $NOW; 2646 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2659 } 2647 }
2660 2648
2661 my @widgets; 2649 my @widgets;
2662 my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} }; 2650
2651 my @items = sort {
2652 $a->{pri} <=> $b->{pri}
2653 or $b->{id} <=> $a->{id}
2654 } values %{ $self->{item} };
2655
2663 my $count = 10 + 1; 2656 my $count = 10 + 1;
2664 for my $item (@items) { 2657 for my $item (@items) {
2665 last unless --$count; 2658 last unless --$count;
2666 2659
2667 push @widgets, $item->{label} ||= do { 2660 push @widgets, $item->{label} ||= do {
2668 # TODO: doesn't handle markup well (read as: at all) 2661 # TODO: doesn't handle markup well (read as: at all)
2669 my $short = delete $item->{text}; 2662 my $short = $item->{count} > 1
2663 ? "<b>$item->{count} ×</b> $item->{text}"
2664 : $item->{text};
2665
2670 for ($short) { 2666 for ($short) {
2671 s/^\s+//; 2667 s/^\s+//;
2672 s/\012.*//s; 2668 s/\012.*//s;
2673 my $len = int 30 / $item->{fontsize}; 2669 my $len = int 40 / $item->{fontsize};
2674 substr $_, $len, length, "…" if $len < length; 2670 substr $_, $len, length, "…" if $len < length;
2675 } 2671 }
2676 2672
2677 new CFClient::UI::Label 2673 new CFClient::UI::Label
2678 markup => $short, 2674 markup => $short,
2679 tooltip => delete $item->{tooltip}, 2675 tooltip => $item->{tooltip},
2676 tooltip_font => $::FONT_PROP,
2677 tooltip_width => 0.67,
2680 fontsize => delete $item->{fontsize}, 2678 fontsize => $item->{fontsize},
2681 color => delete $item->{color}, 2679 color => $item->{color},
2682 can_events => 1, 2680 can_events => 1,
2683 can_hover => 1, 2681 can_hover => 1
2684 }; 2682 };
2685 } 2683 }
2686 2684
2687 $self->clear; 2685 $self->clear;
2688 $self->SUPER::add (@widgets); 2686 $self->SUPER::add (reverse @widgets);
2689} 2687}
2690 2688
2691sub add { 2689sub add {
2692 my ($self, $text, %arg) = @_; 2690 my ($self, $text, %arg) = @_;
2693 2691
2694 my $item = { 2692 $text =~ s/^\s+//;
2695 time => time, 2693 $text =~ s/\s+$//;
2694
2695 my $timeout = time + ((delete $arg{timeout}) || 60);
2696
2697 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2698
2699 if (my $item = $self->{item}{$group}) {
2700 if ($item->{text} eq $text) {
2701 $item->{count}++;
2702 } else {
2703 $item->{count} = 1;
2704 $item->{text} = $item->{tooltip} = $text;
2705 }
2706 $item->{id} = ++$self->{id};
2707 $item->{timeout} = $timeout;
2708 delete $item->{label};
2709 } else {
2710 $self->{item}{$group} = {
2711 id => ++$self->{id},
2696 text => $text, 2712 text => $text,
2697 timeout => 60, 2713 timeout => $timeout,
2698 tooltip => $text, 2714 tooltip => $text,
2699 fontsize => 0.8, 2715 fontsize => 0.8,
2700 color => [0.8, 0.8, 0.8, 0.8], 2716 color => [0.8, 0.8, 0.8, 0.8],
2717 pri => 0,
2718 count => 1,
2701 %arg, 2719 %arg,
2720 };
2702 }; 2721 }
2703
2704 $item->{timeout} += time;
2705 $item->{group} ||= $item+0;
2706
2707 $item = $self->{item}{$item->{group}} ||= $item;
2708 2722
2709 $self->reorder; 2723 $self->reorder;
2710} 2724}
2711 2725
2712############################################################################# 2726#############################################################################
2809 my ($self, $id, $cb) = @_; 2823 my ($self, $id, $cb) = @_;
2810 2824
2811 $self->{refresh_hook}{$id} = $cb; 2825 $self->{refresh_hook}{$id} = $cb;
2812} 2826}
2813 2827
2828sub on_post_alloc {
2829 my ($self, $id, $cb) = @_;
2830
2831 $self->{post_alloc_hook}{$id} = $cb;
2832}
2833
2814sub draw { 2834sub draw {
2815 my ($self) = @_; 2835 my ($self) = @_;
2836
2837 while ($self->{refresh_hook}) {
2838 $_->()
2839 for values %{delete $self->{refresh_hook}};
2840 }
2816 2841
2817 if ($self->{check_size}) { 2842 if ($self->{check_size}) {
2818 my @queue = ([], []); 2843 my @queue = ([], []);
2819 2844
2820 for (;;) { 2845 for (;;) {
2828 2853
2829 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 2854 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2830 ? @$widget{qw(user_w user_h)} 2855 ? @$widget{qw(user_w user_h)}
2831 : $widget->size_request; 2856 : $widget->size_request;
2832 2857
2858 if (delete $widget->{force_alloc}
2833 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 2859 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2834 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 2860 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2835 2861
2836 $widget->{req_w} = $w; 2862 $widget->{req_w} = $w;
2837 $widget->{req_h} = $h; 2863 $widget->{req_h} = $h;
2838 2864
2839 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 2865 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2840 2866
2841 $widget->{parent}->check_size 2867 $widget->{parent}->check_size
2842 if $widget->{parent}; 2868 if $widget->{parent};
2843 } 2869 }
2844 } 2870 }
2856 $widget->size_allocate ($w, $h); 2882 $widget->size_allocate ($w, $h);
2857 $widget->emit (size_allocate => $w, $h); 2883 $widget->emit (size_allocate => $w, $h);
2858 } 2884 }
2859 } 2885 }
2860 2886
2861 while ($self->{refresh_hook}) { 2887 while ($self->{post_alloc_hook}) {
2862 $_->() 2888 $_->()
2863 for values %{delete $self->{refresh_hook}}; 2889 for values %{delete $self->{post_alloc_hook}};
2864 } 2890 }
2865 2891
2866 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2892 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2867 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2893 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2868 glClear GL_COLOR_BUFFER_BIT; 2894 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines