ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI/MessageWindow.pm
Revision: 1.17
Committed: Tue Aug 21 11:36:51 2007 UTC (16 years, 11 months ago) by elmex
Branch: MAIN
Changes since 1.16: +70 -34 lines
Log Message:
added: tooltips tell the user the key to use to switch to a message window tab.
the message window tabs have -N added to their name to indicate the position.
the code became a bit simpler and I even fixed a bug.

File Contents

# Content
1 package CFPlus::UI::MessageWindow;
2
3 use strict;
4 use utf8;
5
6 use Scalar::Util qw/weaken/;
7 use CFPlus::UI::ChatView;
8 use Crossfire::Protocol::Constants;
9
10 our @ISA = CFPlus::UI::Toplevel::;
11
12 our %channel_info;
13
14 sub clr_def($) { "<span foreground=\"#777777\">$_[0]</span>" }
15 sub clr_act($) { "<span foreground=\"#ffffff\">$_[0]</span>" }
16 sub clr_hlt($) { "<span foreground=\"#aaaaff\">$_[0]</span>" }
17 sub clr_hlt2($) { "<span foreground=\"#ff0000\">$_[0]</span>" }
18
19 sub new {
20 my $class = shift;
21
22 my $self = $class->SUPER::new (
23 name => "message_window",
24 title => "Messages",
25 border_bg => [1, 1, 1, 1],
26 x => "max",
27 y => 0,
28 force_w => $::WIDTH * 0.4,
29 force_h => $::HEIGHT * 0.5,
30 child => (my $nb = CFPlus::UI::Notebook->new (expand => 1)),
31 has_close_button => 1
32 );
33
34 $self->{nb} = $nb;
35 $self->{chatviews} = {};
36
37 $nb->connect (page_changed => sub {
38 my ($nb, $page) = @_;
39 $self->set_current_tab ($page);
40 });
41 $nb->connect (c_add => sub {
42 $self->update_tabs;
43 });
44
45 my $l = $self->{main_log} =
46 CFPlus::UI::ChatView->new (expand => 1, say_command => '');
47 $l->{_tab_label} =
48 $l->{c_tab} =
49 CFPlus::UI::Button->new (
50 markup => "Log", tooltip => "This is the main log of the server."
51 );
52
53 $nb->add ($l);
54 $nb->set_current_page ($l);
55
56 $self
57 }
58
59 sub add_chat {
60 my ($self, $id) = @_;
61
62 my $chatviews = $self->{chatviews};
63 my $chaninfo = $self->{channel_info}->{$id};
64 my $nb = $self->{nb};
65
66 my $cv = $chatviews->{$id} =
67 CFPlus::UI::ChatView->new (
68 expand => 1,
69 say_command => $chaninfo->{reply},
70 entry_tooltip => $chaninfo->{tooltip},
71 text_tooltip => "Conversation with $chaninfo->{title}"
72 );
73
74 my $bb = CFPlus::UI::ButtonBin->new (tooltip => $chaninfo->{tooltip});
75 $cv->{c_tab} = $bb;
76
77 $bb->add (my $vb = CFPlus::UI::Box->new);
78 $vb->add (
79 my $b = CFPlus::UI::Label->new (
80 expand => 1, markup => clr_def ($chaninfo->{title}), valign => 0, align => 0
81 )
82 );
83
84 $cv->{_chat_id} = $id;
85 $cv->{_tab_label} = $b;
86 weaken $cv->{_tab_label};
87
88 $vb->add (
89 my $b = CFPlus::UI::ImageButton->new (
90 path => 'x1_close.png',
91 scale => 0.3
92 )
93 );
94 $b->connect (activate => sub {
95 my $b = shift;
96
97 my @chld = $nb->pages;
98 my $cur = pop @chld;
99 while (@chld && $cur != $cv) {
100 $cur = pop @chld;
101 }
102 $cur = pop @chld;
103 $nb->remove ($cv);
104 $nb->set_current_page ($cur);
105
106 delete $chatviews->{$id};
107 0
108 });
109
110 my $preadd = $nb->get_current_page;
111 $nb->add ($cv);
112 $nb->set_current_page ($preadd);
113 }
114
115 sub touch_channel {
116 my ($self, $id) = @_;
117
118 if (not exists $self->{chatviews}->{$id}) {
119 $self->add_chat ($id);
120 }
121 }
122
123 sub highlight_channel {
124 my ($self, $id, $hlt_func) = @_;
125
126 $hlt_func ||= \&clr_hlt;
127
128 my $cv = $self->{chatviews}->{$id};
129
130 # the clr_hlt2 has a "higher priority"
131 unless ($cv->{_channel_highlighted} eq \&clr_hlt2) {
132 $cv->{_channel_highlighted} = $hlt_func;
133 }
134
135 $self->update_tabs;
136 }
137
138 sub set_current_tab {
139 my ($self, $page) = @_;
140
141 for ($self->{nb}->pages) {
142 next if $_ eq $page;
143 $_->{_active} = 0;
144 }
145 $page->{_active} = 1;
146
147 # $label = "Log" unless defined $label;
148
149 # $tab->set_markup (clr_act ($label));
150
151 $self->update_tabs;
152 # for ($self->{nb}->pages) {
153 # next if $_ eq $page;
154 # next if $_->{_channel_highlighted};
155
156 # my $tab = $_->{_tab_label};
157 # next unless $tab;
158
159 # my $label;
160
161 # if (defined $_->{_chat_id}) {
162 # $label = $self->{channel_info}->{$_->{_chat_id}}->{title};
163 # }
164
165 # $label = "Log" unless defined $label;
166
167 # $tab->set_markup (clr_def ($label));
168 # }
169 }
170
171 sub update_tabs {
172 my ($self) = @_;
173
174 my $i = 1;
175 for ($self->{nb}->pages) {
176 if ($i <= 10) {
177 $_->{_tab_pos} = $i++;
178 } else {
179 $_->{_tab_pos} = undef;
180 }
181
182 my $tab = $_->{_tab_label};
183 next unless $tab;
184
185 my ($label, $tooltip) =
186 ("Log", "This is the main log of the server.");
187
188 if (defined $_->{_chat_id}) {
189 my $chinfo = $self->{channel_info}->{$_->{_chat_id}};
190 $label = $chinfo->{title};
191 $tooltip = $chinfo->{tooltip};
192
193 if ($_->{_active}) {
194 $_->{_channel_highlighted} = 0;
195 }
196 }
197
198 $_->{c_tab}->set_tooltip (
199 $tooltip
200 . (defined $_->{_tab_pos}
201 ? "\n<small>To goto this tab press alt+"
202 . ($_->{_tab_pos} == 10 ? '0' : $_->{_tab_pos})
203 . "</small>"
204 : "")
205 );
206
207 my $hltfunc = $_->{_channel_highlighted}
208 || ($_->{_active} ? \&clr_act : \&clr_def);
209
210 $tab->set_markup ($hltfunc->("$label\-$_->{_tab_pos}"));
211 }
212 }
213
214 sub add_channel {
215 my ($self, $info) = @_;
216
217 $self->{channel_info}->{$info->{id}} = $info;
218 $self->touch_channel ($info->{id});
219 }
220
221 sub message {
222 my ($self, $para) = @_;
223
224 #d# require Data::Dumper;
225 #d# print "FOO[".Data::Dumper->Dump ([$para])."]\n";
226
227 my $id = $para->{type};
228
229 if (exists $self->{channel_info}->{$id}) {
230 $self->touch_channel ($id);
231
232 if (my $cv = $self->{chatviews}->{$id}) {
233
234 if ($cv != $self->{nb}->get_current_page) {
235
236 if (($para->{color_flags} & NDI_COLOR_MASK) == NDI_RED) {
237 $self->highlight_channel ($id, \&clr_hlt2);
238 } else {
239 $self->highlight_channel ($id);
240 }
241 }
242
243 if ($para->{color_flags} & NDI_REPLY) {
244 $self->{nb}->set_current_page ($cv);
245 }
246
247 }
248
249 $self->{chatviews}->{$id}->message ($para);
250
251 } else {
252 $self->{main_log}->message ($para);
253 }
254 }
255
256 sub activate_console {
257 my ($self, $preset) = @_;
258
259 $self->{main_log}->activate_console ($preset);
260 }
261
262 sub activate_current {
263 my ($self) = @_;
264
265 $self->{nb}->get_current_page->activate_console;
266 }
267
268 sub set_fontsize {
269 my ($self, $size) = @_;
270
271 for (values %{$self->{chatviews}}, $self->{main_log}) {
272 $_->set_fontsize ($size);
273 }
274 }
275
276 sub set_max_para {
277 my ($self, $max_par) = @_;
278
279 for (values %{$self->{chatviews}}, $self->{main_log}) {
280 $_->set_max_para ($max_par);
281 }
282 }
283
284 sub user_switch_to_page {
285 my ($self, $page) = @_;
286
287 $page = $page eq '0' ? 10 : $page;
288
289 my @tabs = $self->{nb}->pages;
290
291 for (my $i = 0; $i < ($page - 1); $i++) {
292 shift @tabs;
293 }
294
295 my $page = shift @tabs;
296 return unless $page;
297
298 $self->{nb}->set_current_page ($page);
299 }
300
301 1;
302