ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Item.pm
Revision: 1.1
Committed: Sat Dec 9 02:21:25 2006 UTC (17 years, 5 months ago) by root
Branch: MAIN
Log Message:
- slightly cleaned up and "outsourced" components
- completely rewrote keybind bindings
  - not functional yet
  - buggy a shell
  - hits perlbug

File Contents

# User Rev Content
1 root 1.1 package CFPlus::Item;
2    
3     use strict;
4     use Crossfire::Protocol::Constants;
5    
6     my $last_enter_count = 1;
7    
8     sub desc_string {
9     my ($self) = @_;
10    
11     my $desc =
12     $self->{nrof} < 2
13     ? $self->{name}
14     : "$self->{nrof} × $self->{name_pl}";
15    
16     $self->{flags} & F_OPEN
17     and $desc .= " (open)";
18     $self->{flags} & F_APPLIED
19     and $desc .= " (applied)";
20     $self->{flags} & F_UNPAID
21     and $desc .= " (unpaid)";
22     $self->{flags} & F_MAGIC
23     and $desc .= " (magic)";
24     $self->{flags} & F_CURSED
25     and $desc .= " (cursed)";
26     $self->{flags} & F_DAMNED
27     and $desc .= " (damned)";
28     $self->{flags} & F_LOCKED
29     and $desc .= " *";
30    
31     $desc
32     }
33    
34     sub weight_string {
35     my ($self) = @_;
36    
37     my $weight = ($self->{nrof} || 1) * $self->{weight};
38    
39     $weight < 0 ? "?" : $weight * 0.001
40     }
41    
42     sub do_n_dialog {
43     my ($cb) = @_;
44    
45     my $w = new CFPlus::UI::Toplevel
46     on_delete => sub { $_[0]->destroy; 1 },
47     has_close_button => 1,
48     ;
49    
50     $w->add (my $vb = new CFPlus::UI::VBox x => "center", y => "center");
51     $vb->add (new CFPlus::UI::Label text => "Enter item count:");
52     $vb->add (my $entry = new CFPlus::UI::Entry
53     text => $last_enter_count,
54     on_activate => sub {
55     my ($entry) = @_;
56     $last_enter_count = $entry->get_text;
57     $cb->($last_enter_count);
58     $w->hide;
59     $w->destroy;
60    
61     0
62     },
63     on_escape => sub { $w->destroy; 1 },
64     );
65     $entry->grab_focus;
66     $w->show;
67     }
68    
69     sub update_widgets {
70     my ($self) = @_;
71    
72     # necessary to avoid cyclic references
73     Scalar::Util::weaken $self;
74    
75     my $button_cb = sub {
76     my (undef, $ev, $x, $y) = @_;
77    
78     my $targ = $::CONN->{player}{tag};
79    
80     if ($self->{container} == $::CONN->{player}{tag}) {
81     $targ = $::CONN->{open_container};
82     }
83    
84     if (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 1) {
85     $::CONN->send ("move $targ $self->{tag} 0")
86     if $targ || !($self->{flags} & F_LOCKED);
87     } elsif (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 2) {
88     $self->{flags} & F_LOCKED
89     ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag})
90     : $::CONN->send ("lock " . pack "CN", 1, $self->{tag})
91     } elsif ($ev->{button} == 1) {
92     $::CONN->send ("examine $self->{tag}");
93     } elsif ($ev->{button} == 2) {
94     $::CONN->send ("apply $self->{tag}");
95     } elsif ($ev->{button} == 3) {
96     my $move_prefix = $::CONN->{open_container} ? 'put' : 'drop';
97     if ($self->{container} == $::CONN->{open_container}) {
98     $move_prefix = "take";
99     }
100    
101     my $shortname = CFPlus::shorten $self->{name}, 14;
102    
103     my @menu_items = (
104     ["examine", sub { $::CONN->send ("examine $self->{tag}") }],
105     ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }],
106     ["ignite/thaw", # first try of an easier use of flint&steel
107     sub {
108     $::CONN->send ("mark ". pack "N", $self->{tag});
109     $::CONN->send ("command apply flint and steel");
110     }
111     ],
112     ["inscribe", # first try of an easier use of flint&steel
113     sub {
114     &::open_string_query ("Text to inscribe", sub {
115     my ($entry, $txt) = @_;
116     $::CONN->send ("mark ". pack "N", $self->{tag});
117     $::CONN->send ("command use_skill inscription $txt");
118     });
119     }
120     ],
121     ["rename", # first try of an easier use of flint&steel
122     sub {
123     &::open_string_query ("Rename item to:", sub {
124     my ($entry, $txt) = @_;
125     $::CONN->send ("mark ". pack "N", $self->{tag});
126     $::CONN->send ("command rename to <$txt>");
127     }, $self->{name},
128     "If you input no name or erase the current custom name, the custom name will be unset");
129     }
130     ],
131     ["apply", sub { $::CONN->send ("apply $self->{tag}") }],
132     (
133     $self->{flags} & F_LOCKED
134     ? (
135     ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }],
136     )
137     : (
138     ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }],
139     ["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }],
140     ["$move_prefix &lt;n&gt;",
141     sub {
142     do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") })
143     }
144     ]
145     )
146     ),
147     ["bind <i>apply $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["apply $self->{name}"] }],
148     );
149    
150     CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev);
151     }
152    
153     1
154     };
155    
156     my $tooltip_std = "<small>"
157     . "Left click - examine item\n"
158     . "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n"
159     . "Middle click - apply\n"
160     . "Shift-Middle click - lock/unlock\n"
161     . "Right click - further options"
162     . "</small>\n";
163    
164     my $bg = $self->{flags} & F_CURSED ? [1 , 0 , 0, 0.5]
165     : $self->{flags} & F_MAGIC ? [0.2, 0.2, 1, 0.5]
166     : undef;
167    
168     $self->{face_widget} ||= new CFPlus::UI::Face
169     can_events => 1,
170     can_hover => 1,
171     anim => $self->{anim},
172     animspeed => $self->{animspeed}, # TODO# must be set at creation time
173     on_button_down => $button_cb,
174     ;
175     $self->{face_widget}{bg} = $bg;
176     $self->{face_widget}{face} = $self->{face};
177     $self->{face_widget}{anim} = $self->{anim};
178     $self->{face_widget}{animspeed} = $self->{animspeed};
179     $self->{face_widget}->set_tooltip (
180     "<b>Face/Animation.</b>\n"
181     . "Item uses face #$self->{face}. "
182     . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ")
183     . "\n\n$tooltip_std"
184     );
185    
186     $self->{desc_widget} ||= new CFPlus::UI::Label
187     can_events => 1,
188     can_hover => 1,
189     ellipsise => 2,
190     align => -1,
191     on_button_down => $button_cb,
192     ;
193     my $desc = CFPlus::Item::desc_string $self;
194     $self->{desc_widget}{bg} = $bg;
195     $self->{desc_widget}->set_text ($desc);
196     $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std");
197    
198     $self->{weight_widget} ||= new CFPlus::UI::Label
199     can_events => 1,
200     can_hover => 1,
201     ellipsise => 0,
202     align => 0,
203     on_button_down => $button_cb,
204     ;
205     $self->{weight_widget}{bg} = $bg;
206     $self->{weight_widget}->set_text (CFPlus::Item::weight_string $self);
207     $self->{weight_widget}->set_tooltip (
208     "<b>Weight</b>.\n"
209     . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ")
210     . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
211     . "\n\n$tooltip_std"
212     );
213     }