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.231 by root, Thu May 25 00:26:19 2006 UTC vs.
Revision 1.240 by root, Fri May 26 18:28:23 2006 UTC

5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8 8
9use CFClient; 9use CFClient;
10use CFClient::Texture;
10 11
11our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
12 13
13our $ROOT; 14our $ROOT;
14our $TOOLTIP; 15our $TOOLTIP;
233 delete $self->{visible}; 234 delete $self->{visible};
234 235
235 undef $GRAB if $GRAB == $self; 236 undef $GRAB if $GRAB == $self;
236 undef $HOVER if $HOVER == $self; 237 undef $HOVER if $HOVER == $self;
237 238
239 CFClient::UI::check_tooltip
240 if $CFClient::UI::TOOLTIP->{owner} == $self;
241
238 $self->focus_out; 242 $self->focus_out;
239} 243}
240 244
241sub hide { 245sub hide {
242 my ($self) = @_; 246 my ($self) = @_;
318 delete $self->{max_h}; $self->{max_h} = $h if $h; 322 delete $self->{max_h}; $self->{max_h} = $h if $h;
319} 323}
320 324
321sub set_tooltip { 325sub set_tooltip {
322 my ($self, $tooltip) = @_; 326 my ($self, $tooltip) = @_;
327
328 $tooltip =~ s/^\s+//;
329 $tooltip =~ s/\s+$//;
330
331 return if $self->{tooltip} eq $tooltip;
323 332
324 $self->{tooltip} = $tooltip; 333 $self->{tooltip} = $tooltip;
325 334
326 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 335 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
327 delete $CFClient::UI::TOOLTIP->{owner}; 336 delete $CFClient::UI::TOOLTIP->{owner};
417 if ($ENV{PCLIENT_DEBUG}) { 426 if ($ENV{PCLIENT_DEBUG}) {
418 glPushMatrix; 427 glPushMatrix;
419 glColor 1, 1, 0, 1; 428 glColor 1, 1, 0, 1;
420 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 429 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
421 glBegin GL_LINE_LOOP; 430 glBegin GL_LINE_LOOP;
422 glVertex 0 , 0; 431 glVertex 0 , 0;
423 glVertex $self->{w}, 0; 432 glVertex $self->{w} - 1, 0;
424 glVertex $self->{w}, $self->{h}; 433 glVertex $self->{w} - 1, $self->{h} - 1;
425 glVertex 0 , $self->{h}; 434 glVertex 0 , $self->{h} - 1;
426 glEnd; 435 glEnd;
427 glPopMatrix; 436 glPopMatrix;
428 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 437 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
429 } 438 }
430} 439}
449 458
450sub set_parent { 459sub set_parent {
451 my ($self, $parent) = @_; 460 my ($self, $parent) = @_;
452 461
453 Scalar::Util::weaken ($self->{parent} = $parent); 462 Scalar::Util::weaken ($self->{parent} = $parent);
463
464 $self->{root} = $parent->{root};
465 $self->{visible} = $parent->{visible};
454 466
455 # TODO: req_w _does_change after ->reconfigure 467 # TODO: req_w _does_change after ->reconfigure
456 $self->check_size 468 $self->check_size
457 unless exists $self->{req_w}; 469 unless exists $self->{req_w};
458 470
610 delete $child->{parent}; 622 delete $child->{parent};
611 $child->hide; 623 $child->hide;
612 624
613 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 625 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
614 626
615 $self->check_size; 627 $self->check_size (1);
616 $self->update; 628 $self->update;
617} 629}
618 630
619sub clear { 631sub clear {
620 my ($self) = @_; 632 my ($self) = @_;
760 772
761package CFClient::UI::ViewPort; 773package CFClient::UI::ViewPort;
762 774
763our @ISA = CFClient::UI::Window::; 775our @ISA = CFClient::UI::Window::;
764 776
777sub new {
778 my $class = shift;
779
780 $class->SUPER::new (
781 scroll_x => 0,
782 scroll_y => 1,
783 @_,
784 )
785}
786
765sub size_request { 787sub size_request {
766 my ($self) = @_; 788 my ($self) = @_;
767 789
768 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 790 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
769 791
771} 793}
772 794
773sub size_allocate { 795sub size_allocate {
774 my ($self, $w, $h) = @_; 796 my ($self, $w, $h) = @_;
775 797
776 my ($cw, $ch) = @$self{qw(child_w child_h)}; 798 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
777# $w = $self->{w}; 799 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
800
778 $self->child->configure (0, 0, $cw, $ch); 801 $self->child->configure (0, 0, $w, $h);
779 $self->update; 802 $self->update;
780} 803}
781 804
782sub set_offset { 805sub set_offset {
783 my ($self, $x, $y) = @_; 806 my ($self, $x, $y) = @_;
852 $self->{vp}->add ($self->{scrolled}); 875 $self->{vp}->add ($self->{scrolled});
853 $self->add ($self->{vp}); 876 $self->add ($self->{vp});
854 $self->add ($self->{slider}); 877 $self->add ($self->{slider});
855 878
856 $self 879 $self
880}
881
882sub update {
883 my ($self) = @_;
884
885 $self->SUPER::update;
886
887 # todo: overwrite size_allocate of child
888 my $child = $self->{vp}->child;
889 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
857} 890}
858 891
859sub size_allocate { 892sub size_allocate {
860 my ($self, $w, $h) = @_; 893 my ($self, $w, $h) = @_;
861 894
1078sub new { 1111sub new {
1079 my $class = shift; 1112 my $class = shift;
1080 1113
1081 $class->SUPER::new ( 1114 $class->SUPER::new (
1082 col_expand => [], 1115 col_expand => [],
1083 @_ 1116 @_,
1084 ) 1117 )
1118}
1119
1120sub children {
1121 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1085} 1122}
1086 1123
1087sub add { 1124sub add {
1088 my ($self, $x, $y, $child) = @_; 1125 my ($self, $x, $y, $child) = @_;
1089 1126
1090 $child->set_parent ($self); 1127 $child->set_parent ($self);
1091 $self->{children}[$y][$x] = $child; 1128 $self->{children}[$y][$x] = $child;
1092 1129
1093 $child->check_size; 1130 $self->check_size (1);
1094} 1131}
1095 1132
1096sub children {
1097 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1098}
1099
1100# TODO: move to container class maybe? send childs a signal on removal? 1133# TODO: move to container class maybe? send children a signal on removal?
1101sub clear { 1134sub clear {
1102 my ($self) = @_; 1135 my ($self) = @_;
1103 1136
1104 my @children = $self->children; 1137 my @children = $self->children;
1105 delete $self->{children}; 1138 delete $self->{children};
1107 for (@children) { 1140 for (@children) {
1108 delete $_->{parent}; 1141 delete $_->{parent};
1109 $_->hide; 1142 $_->hide;
1110 } 1143 }
1111 1144
1145 $self->check_size (1);
1112 $self->update; 1146 $self->update;
1113} 1147}
1114 1148
1115sub get_wh { 1149sub get_wh {
1116 my ($self) = @_; 1150 my ($self) = @_;
1148sub size_allocate { 1182sub size_allocate {
1149 my ($self, $w, $h) = @_; 1183 my ($self, $w, $h) = @_;
1150 1184
1151 my ($ws, $hs) = $self->get_wh; 1185 my ($ws, $hs) = $self->get_wh;
1152 1186
1153 my $req_w = sum @$ws; 1187 my $req_w = (sum @$ws) || 1;
1154 my $req_h = sum @$hs; 1188 my $req_h = (sum @$hs) || 1;
1155 1189
1156 # TODO: nicer code && do row_expand 1190 # TODO: nicer code && do row_expand
1157 my @col_expand = @{$self->{col_expand}}; 1191 my @col_expand = @{$self->{col_expand}};
1158 @col_expand = (1) x @$ws unless @col_expand; 1192 @col_expand = (1) x @$ws unless @col_expand;
1159 my $col_expand = (sum @col_expand) || 1; 1193 my $col_expand = (sum @col_expand) || 1;
1562 my ($self) = @_; 1596 my ($self) = @_;
1563 1597
1564 my ($w, $h) = $self->SUPER::size_request; 1598 my ($w, $h) = $self->SUPER::size_request;
1565 1599
1566 ($w + 1, $h) # add 1 for cursor 1600 ($w + 1, $h) # add 1 for cursor
1567}
1568
1569sub size_allocate {
1570 my ($self, $w, $h) = @_;
1571
1572 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1573} 1601}
1574 1602
1575sub key_down { 1603sub key_down {
1576 my ($self, $ev) = @_; 1604 my ($self, $ev) = @_;
1577 1605
2119} 2147}
2120 2148
2121sub set_range { 2149sub set_range {
2122 my ($self, $range) = @_; 2150 my ($self, $range) = @_;
2123 2151
2124 $self->{range} = $range; 2152 ($range, $self->{range}) = ($self->{range}, $range);
2125 2153
2126 $self->update; 2154 $self->update
2155 if "@$range" ne "@{$self->{range}}";
2127} 2156}
2128 2157
2129sub set_value { 2158sub set_value {
2130 my ($self, $value) = @_; 2159 my ($self, $value) = @_;
2131 2160
2626 2655
2627sub new { 2656sub new {
2628 my $class = shift; 2657 my $class = shift;
2629 2658
2630 my $self = $class->SUPER::new ( 2659 my $self = $class->SUPER::new (
2631 aspect => 1, 2660 aspect => 1,
2661 can_events => 0,
2632 @_, 2662 @_,
2633 ); 2663 );
2634 2664
2635 if ($self->{anim} && $self->{animspeed}) { 2665 if ($self->{anim} && $self->{animspeed}) {
2636 Scalar::Util::weaken (my $widget = $self); 2666 Scalar::Util::weaken (my $widget = $self);
2695 $self->SUPER::DESTROY; 2725 $self->SUPER::DESTROY;
2696} 2726}
2697 2727
2698############################################################################# 2728#############################################################################
2699 2729
2700package CFClient::UI::InventoryItem; 2730package CFClient::UI::Inventory;
2701 2731
2702our @ISA = CFClient::UI::HBox::; 2732our @ISA = CFClient::UI::ScrolledWindow::;
2703
2704sub _item_to_desc {
2705 my ($item) = @_;
2706
2707 my $desc =
2708 $item->{nrof} < 2
2709 ? $item->{name}
2710 : "$item->{nrof} × $item->{name_pl}";
2711
2712 $item->{flags} & Crossfire::Protocol::F_OPEN
2713 and $desc .= " (open)";
2714 $item->{flags} & Crossfire::Protocol::F_APPLIED
2715 and $desc .= " (applied)";
2716 $item->{flags} & Crossfire::Protocol::F_UNPAID
2717 and $desc .= " (unpaid)";
2718 $item->{flags} & Crossfire::Protocol::F_MAGIC
2719 and $desc .= " (magic)";
2720 $item->{flags} & Crossfire::Protocol::F_CURSED
2721 and $desc .= " (cursed)";
2722 $item->{flags} & Crossfire::Protocol::F_DAMNED
2723 and $desc .= " (damned)";
2724 $item->{flags} & Crossfire::Protocol::F_LOCKED
2725 and $desc .= " *";
2726
2727 $desc
2728}
2729 2733
2730sub new { 2734sub new {
2731 my $class = shift; 2735 my $class = shift;
2732 2736
2733 my %args = @_;
2734
2735 my $item = delete $args{item};
2736
2737 my $desc = _item_to_desc ($item);
2738
2739 my $self = $class->SUPER::new ( 2737 my $self = $class->SUPER::new (
2740 can_hover => 1,
2741 can_events => 1,
2742 tooltip => ((CFClient::UI::Label::escape $desc)
2743 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2744 connect_button_down => sub {
2745 my ($self, $ev, $x, $y) = @_;
2746
2747 # todo: maybe put examine on 1? but should just be a tooltip :(
2748 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2749 my $targ = $::CONN->{player}{tag};
2750
2751 if ($item->{container} == $::CONN->{player}{tag}) {
2752 $targ = $main::OPENCONT;
2753 }
2754
2755 $::CONN->send ("move $targ $item->{tag} 0");
2756 } elsif ($ev->{button} == 1) {
2757 $::CONN->send ("examine $item->{tag}");
2758 } elsif ($ev->{button} == 2) {
2759 $::CONN->send ("apply $item->{tag}");
2760 } elsif ($ev->{button} == 3) {
2761 my @menu_items = (
2762 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2763 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2764 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2765 (
2766 $item->{flags} & Crossfire::Protocol::F_LOCKED
2767 ? (
2768 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2769 )
2770 : (
2771 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2772 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2773 )
2774 ),
2775 );
2776
2777 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2778 }
2779
2780 1
2781 },
2782 %args
2783 );
2784
2785
2786 $self->add (new CFClient::UI::Face
2787 can_events => 0,
2788 face => $item->{face},
2789 anim => $item->{anim},
2790 animspeed => $item->{animspeed},
2791 );
2792
2793 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2794
2795 $self->{item} = $item;
2796
2797 $self->update_item;
2798
2799 $self
2800}
2801
2802sub update_item {
2803 my ($self) = @_;
2804
2805 my $desc = _item_to_desc ($self->{item});
2806
2807 $self->{name_lbl}->set_text ($desc);
2808}
2809
2810#############################################################################
2811
2812package CFClient::UI::Inventory;
2813
2814our @ISA = CFClient::UI::ScrolledWindow::;
2815
2816sub new {
2817 my $class = shift;
2818
2819 my $self = $class->SUPER::new (
2820 scrolled => (new CFClient::UI::Table), 2738 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2821 @_, 2739 @_,
2822 ); 2740 );
2823 2741
2824 $self 2742 $self
2825} 2743}
2835 or ($a->{name} cmp $b->{name}) 2753 or ($a->{name} cmp $b->{name})
2836 } @$items; 2754 } @$items;
2837 2755
2838 $self->{real_items} = \@items; 2756 $self->{real_items} = \@items;
2839 2757
2758 my $row = 0;
2840 for my $item (@items) { 2759 for my $item (@items) {
2841 $item->{item} = $item; 2760 CFClient::Item::update_widgets $item;
2842 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2843 $item->update_item ();
2844 }
2845 2761
2846 my $i = 0; 2762 $self->{scrolled}->add (0, $row, $item->{face_widget});
2847 for (@items) { 2763 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2848 $self->{scrolled}->add (0, $i, $_); 2764 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2849 my $nrof = $_->{item}->{nrof} || 1;
2850 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2851 }
2852 2765
2853# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2766 $row++;
2767 }
2854} 2768}
2855 2769
2856sub size_request { 2770sub size_request {
2857 my ($self) = @_; 2771 my ($self) = @_;
2858 ($self->{req_w}, $self->{req_h}); 2772 ($self->{req_w}, $self->{req_h});
2995sub add { 2909sub add {
2996 my ($self, $text, %arg) = @_; 2910 my ($self, $text, %arg) = @_;
2997 2911
2998 $text =~ s/^\s+//; 2912 $text =~ s/^\s+//;
2999 $text =~ s/\s+$//; 2913 $text =~ s/\s+$//;
2914
2915 return unless $text;
3000 2916
3001 my $timeout = time + ((delete $arg{timeout}) || 60); 2917 my $timeout = time + ((delete $arg{timeout}) || 60);
3002 2918
3003 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2919 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
3004 2920

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines