ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Item.pm
Revision: 1.17
Committed: Sat Apr 3 02:58:24 2010 UTC (14 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-2_11
Changes since 1.16: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

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