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

# User Rev Content
1 root 1.11 package DC::Item;
2 root 1.1
3 root 1.17 use common::sense;
4    
5 elmex 1.7 use Encode;
6 root 1.3
7 root 1.9 use Deliantra::Protocol::Constants;
8 root 1.1
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 root 1.16 : "$self->{nrof} $self->{name_pl}";
18 root 1.1
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 root 1.11 my $w = new DC::UI::Toplevel
49 root 1.1 on_delete => sub { $_[0]->destroy; 1 },
50     has_close_button => 1,
51     ;
52    
53 root 1.11 $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 root 1.1 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 root 1.14 my $bg_cursed = [1 , 0 , 0, 0.5];
73     my $bg_magic = [0.2, 0.2, 1, 0.5];
74    
75 root 1.1 sub update_widgets {
76     my ($self) = @_;
77    
78     # necessary to avoid cyclic references
79 root 1.11 DC::weaken $self;
80 root 1.1
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 root 1.11 if (($ev->{mod} & DC::KMOD_SHIFT) && $ev->{button} == 1) {
91 root 1.1 $::CONN->send ("move $targ $self->{tag} 0")
92     if $targ || !($self->{flags} & F_LOCKED);
93 root 1.11 } elsif (($ev->{mod} & DC::KMOD_SHIFT) && $ev->{button} == 2) {
94 root 1.1 $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 root 1.11 my $shortname = DC::shorten $self->{name}, 14;
108 root 1.1
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 elmex 1.7 $::CONN->send_utf8 ("command use_skill inscription $txt");
124 root 1.1 });
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 elmex 1.7 $::CONN->send_utf8 ("command rename to <$txt>");
133 root 1.1 }, $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 root 1.11 ["bind <i>apply $shortname</i> to a key" => sub { DC::Macro::quick_macro ["apply $self->{name}"] }],
154 root 1.1 );
155    
156 root 1.11 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
157 root 1.1 }
158    
159     1
160     };
161    
162 root 1.4 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 root 1.1
171 root 1.14 my $bg = $self->{flags} & F_CURSED ? $bg_cursed
172     : $self->{flags} & F_MAGIC ? $bg_magic
173 root 1.1 : undef;
174    
175 root 1.11 my $desc = DC::Item::desc_string $self;
176 root 1.14 my $face_tooltip = "<b>$desc</b>\n\n$tooltip_std";
177    
178     if (my $face = $self->{face_widget}) {
179 root 1.15 # already exists, so update if it changed
180 root 1.14 if ($face->{bg} != $bg) {
181     $face->{bg} = $bg;
182     $face->update;
183     }
184 root 1.8
185 root 1.15 $face->set_bg ($bg) if $face->{bg} != $bg;
186 root 1.14 $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 root 1.15 # new object, create new face
199 root 1.14 $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 root 1.1
211 root 1.11 $self->{desc_widget} ||= new DC::UI::Label
212 root 1.1 can_events => 1,
213     can_hover => 1,
214     ellipsise => 2,
215 root 1.12 align => 0,
216 root 1.4
217     on_button_down => $button_cb,
218     on_tooltip_show => sub {
219     my ($widget) = @_;
220    
221 root 1.6 $::CONN && $::CONN->ex ($self->{tag}, sub {
222 root 1.4 my ($long_desc) = @_;
223    
224 root 1.13 $long_desc = DC::Protocol::sanitise_xml ($long_desc);
225 root 1.4
226     $self->{long_desc} = $long_desc;
227     $widget->set_tooltip ("<b>$long_desc</b>\n\n$tooltip_std");
228     });
229     },
230 root 1.1 ;
231 root 1.4
232 root 1.15 my $long_desc = $self->{long_desc} || $desc;
233    
234     $self->{desc_widget}->set_bg ($bg) if $self->{desc_widget}{bg} != $bg;
235 root 1.1 $self->{desc_widget}->set_text ($desc);
236 root 1.5 $self->{desc_widget}->set_tooltip ("<b>$long_desc</b>\n\n$tooltip_std");
237 root 1.1
238 root 1.11 $self->{weight_widget} ||= new DC::UI::Label
239 root 1.1 can_events => 1,
240     can_hover => 1,
241     ellipsise => 0,
242     on_button_down => $button_cb,
243     ;
244     $self->{weight_widget}{bg} = $bg;
245 root 1.11 $self->{weight_widget}->set_text (DC::Item::weight_string $self);
246 root 1.1 $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 root 1.6