ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Item.pm
Revision: 1.11
Committed: Wed Dec 26 21:03:21 2007 UTC (16 years, 5 months ago) by root
Branch: MAIN
Changes since 1.10: +16 -16 lines
Log Message:
initial module hiding

File Contents

# User Rev Content
1 root 1.11 package DC::Item;
2 root 1.1
3     use strict;
4 root 1.3 use utf8;
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     : "$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 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     sub update_widgets {
73     my ($self) = @_;
74    
75     # necessary to avoid cyclic references
76 root 1.11 DC::weaken $self;
77 root 1.1
78     my $button_cb = sub {
79     my (undef, $ev, $x, $y) = @_;
80    
81     my $targ = $::CONN->{player}{tag};
82    
83     if ($self->{container} == $::CONN->{player}{tag}) {
84     $targ = $::CONN->{open_container};
85     }
86    
87 root 1.11 if (($ev->{mod} & DC::KMOD_SHIFT) && $ev->{button} == 1) {
88 root 1.1 $::CONN->send ("move $targ $self->{tag} 0")
89     if $targ || !($self->{flags} & F_LOCKED);
90 root 1.11 } elsif (($ev->{mod} & DC::KMOD_SHIFT) && $ev->{button} == 2) {
91 root 1.1 $self->{flags} & F_LOCKED
92     ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag})
93     : $::CONN->send ("lock " . pack "CN", 1, $self->{tag})
94     } elsif ($ev->{button} == 1) {
95     $::CONN->send ("examine $self->{tag}");
96     } elsif ($ev->{button} == 2) {
97     $::CONN->send ("apply $self->{tag}");
98     } elsif ($ev->{button} == 3) {
99     my $move_prefix = $::CONN->{open_container} ? 'put' : 'drop';
100     if ($self->{container} == $::CONN->{open_container}) {
101     $move_prefix = "take";
102     }
103    
104 root 1.11 my $shortname = DC::shorten $self->{name}, 14;
105 root 1.1
106     my @menu_items = (
107     ["examine", sub { $::CONN->send ("examine $self->{tag}") }],
108     ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }],
109     ["ignite/thaw", # first try of an easier use of flint&steel
110     sub {
111     $::CONN->send ("mark ". pack "N", $self->{tag});
112     $::CONN->send ("command apply flint and steel");
113     }
114     ],
115     ["inscribe", # first try of an easier use of flint&steel
116     sub {
117     &::open_string_query ("Text to inscribe", sub {
118     my ($entry, $txt) = @_;
119     $::CONN->send ("mark ". pack "N", $self->{tag});
120 elmex 1.7 $::CONN->send_utf8 ("command use_skill inscription $txt");
121 root 1.1 });
122     }
123     ],
124     ["rename", # first try of an easier use of flint&steel
125     sub {
126     &::open_string_query ("Rename item to:", sub {
127     my ($entry, $txt) = @_;
128     $::CONN->send ("mark ". pack "N", $self->{tag});
129 elmex 1.7 $::CONN->send_utf8 ("command rename to <$txt>");
130 root 1.1 }, $self->{name},
131     "If you input no name or erase the current custom name, the custom name will be unset");
132     }
133     ],
134     ["apply", sub { $::CONN->send ("apply $self->{tag}") }],
135     (
136     $self->{flags} & F_LOCKED
137     ? (
138     ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }],
139     )
140     : (
141     ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }],
142     ["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }],
143     ["$move_prefix &lt;n&gt;",
144     sub {
145     do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") })
146     }
147     ]
148     )
149     ),
150 root 1.11 ["bind <i>apply $shortname</i> to a key" => sub { DC::Macro::quick_macro ["apply $self->{name}"] }],
151 root 1.1 );
152    
153 root 1.11 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
154 root 1.1 }
155    
156     1
157     };
158    
159 root 1.4 my $tooltip_std =
160     "<small>"
161     . "Left click - examine item\n"
162     . "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n"
163     . "Middle click - apply\n"
164     . "Shift-Middle click - lock/unlock\n"
165     . "Right click - further options"
166     . "</small>\n";
167 root 1.1
168     my $bg = $self->{flags} & F_CURSED ? [1 , 0 , 0, 0.5]
169     : $self->{flags} & F_MAGIC ? [0.2, 0.2, 1, 0.5]
170     : undef;
171    
172 root 1.11 my $desc = DC::Item::desc_string $self;
173 root 1.8
174 root 1.11 $self->{face_widget} ||= new DC::UI::Face
175 root 1.1 can_events => 1,
176     can_hover => 1,
177     anim => $self->{anim},
178     animspeed => $self->{animspeed}, # TODO# must be set at creation time
179     on_button_down => $button_cb,
180     ;
181     $self->{face_widget}{bg} = $bg;
182     $self->{face_widget}{face} = $self->{face};
183     $self->{face_widget}{anim} = $self->{anim};
184     $self->{face_widget}{animspeed} = $self->{animspeed};
185 root 1.8 # $self->{face_widget}->set_tooltip (
186     # "<b>Face/Animation.</b>\n"
187     # . "Item uses face #$self->{face}. "
188     # . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ")
189     # . "\n\n$tooltip_std"
190     # );
191     $self->{face_widget}->set_tooltip ("<b>$desc</b>\n\n$tooltip_std");
192 root 1.1
193 root 1.11 $self->{desc_widget} ||= new DC::UI::Label
194 root 1.1 can_events => 1,
195     can_hover => 1,
196     ellipsise => 2,
197     align => -1,
198 root 1.4
199     on_button_down => $button_cb,
200     on_tooltip_show => sub {
201     my ($widget) = @_;
202    
203 root 1.6 $::CONN && $::CONN->ex ($self->{tag}, sub {
204 root 1.4 my ($long_desc) = @_;
205    
206     $long_desc =~ s/\s+$//;
207    
208     $self->{long_desc} = $long_desc;
209     $widget->set_tooltip ("<b>$long_desc</b>\n\n$tooltip_std");
210     });
211     },
212 root 1.1 ;
213 root 1.4
214 root 1.1 $self->{desc_widget}{bg} = $bg;
215     $self->{desc_widget}->set_text ($desc);
216 root 1.4
217 root 1.5 my $long_desc = $self->{long_desc} || $desc;
218     $self->{desc_widget}->set_tooltip ("<b>$long_desc</b>\n\n$tooltip_std");
219 root 1.1
220 root 1.11 $self->{weight_widget} ||= new DC::UI::Label
221 root 1.1 can_events => 1,
222     can_hover => 1,
223     ellipsise => 0,
224     align => 0,
225     on_button_down => $button_cb,
226     ;
227     $self->{weight_widget}{bg} = $bg;
228 root 1.11 $self->{weight_widget}->set_text (DC::Item::weight_string $self);
229 root 1.1 $self->{weight_widget}->set_tooltip (
230     "<b>Weight</b>.\n"
231     . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ")
232     . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
233     . "\n\n$tooltip_std"
234     );
235     }
236 root 1.6