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