1 |
package DC::UI::ChatView; |
2 |
|
3 |
use strict; |
4 |
use utf8; |
5 |
|
6 |
use Deliantra::Protocol::Constants; |
7 |
|
8 |
our @ISA = DC::UI::Dockable::; |
9 |
|
10 |
sub new { |
11 |
my $class = shift; |
12 |
|
13 |
my $self = $class->SUPER::new ( |
14 |
can_close => 1, |
15 |
child => (my $vbox = new DC::UI::VBox), |
16 |
@_, |
17 |
); |
18 |
|
19 |
$self->update_info ($self->{info}); |
20 |
|
21 |
$vbox->add ($self->{txt} = new DC::UI::TextScroller ( |
22 |
expand => 1, |
23 |
font => $::FONT_FIXED, |
24 |
fontsize => $::CFG->{log_fontsize}, |
25 |
indent => -4, |
26 |
can_hover => 1, |
27 |
can_events => 1, |
28 |
max_par => $::CFG->{logview_max_par}, |
29 |
tooltip => $self->{text_tooltip}, |
30 |
)); |
31 |
|
32 |
$vbox->add (my $hb = DC::UI::HBox->new); |
33 |
|
34 |
$hb->add ( |
35 |
$self->{say_command_label} = |
36 |
DC::UI::Label->new (markup => $self->{say_command})); |
37 |
|
38 |
$hb->add ($self->{input} = DC::UI::Entry->new ( |
39 |
expand => 1, |
40 |
tooltip => $self->{entry_tooltip}, |
41 |
on_focus_in => sub { |
42 |
my ($input, $prev_focus) = @_; |
43 |
|
44 |
delete $input->{refocus_map}; |
45 |
|
46 |
if ($prev_focus == $::MAPWIDGET && $input->{auto_activated}) { |
47 |
$input->{refocus_map} = 1; |
48 |
} |
49 |
delete $input->{auto_activated}; |
50 |
|
51 |
0 |
52 |
}, |
53 |
on_activate => sub { |
54 |
my ($input, $text) = @_; |
55 |
$input->set_text (''); |
56 |
|
57 |
return unless $::CONN; |
58 |
|
59 |
if ($text =~ /^\/(.*)/) { |
60 |
$::CONN->user_send ($1); |
61 |
} elsif (length $text) { |
62 |
my $say_cmd = $self->{say_command}; |
63 |
$::CONN->user_send ($say_cmd . $text); |
64 |
} else { |
65 |
$input->{refocus_map} = 1; |
66 |
} |
67 |
if (delete $input->{refocus_map}) { |
68 |
$::MAPWIDGET->grab_focus; |
69 |
} |
70 |
|
71 |
0 |
72 |
}, |
73 |
on_key_down => sub { |
74 |
my ($input, $ev) = @_; |
75 |
my $uni = $ev->{unicode}; |
76 |
my $mod = $ev->{mod}; |
77 |
|
78 |
if ($uni >= ord "0" && $uni <= ord "9" && $mod & DC::KMOD_ALT) { |
79 |
$::MAPWIDGET->emit (key_down => $ev); |
80 |
return 1; |
81 |
} |
82 |
|
83 |
0 |
84 |
}, |
85 |
on_escape => sub { |
86 |
$::MAPWIDGET->grab_focus; |
87 |
|
88 |
0 |
89 |
}, |
90 |
)); |
91 |
|
92 |
$self->{initiated} = 1; # for update_info |
93 |
|
94 |
$self |
95 |
} |
96 |
|
97 |
# (private) This method updates the channel info associated with this chat view. |
98 |
sub update_info { |
99 |
my ($self, $info) = @_; |
100 |
$self->{title} = $info->{title}; |
101 |
$self->{text_tooltip} = $info->{tooltip}; |
102 |
$self->{say_command} = $info->{reply}; |
103 |
$self->{entry_tooltip} = |
104 |
$info->{entry_tooltip} |
105 |
|| "Enter a message and press enter to send it to the channel '$info->{title}'."; |
106 |
|
107 |
# TODO: needs some testing maybe, if known that this works: remove comment! |
108 |
if ($self->{initiated}) { |
109 |
$self->{say_command_label}->set_markup ($self->{say_command}); |
110 |
$self->{txt}->{tooltip} = $self->{text_tooltip}; |
111 |
$self->{input}->{tooltip} = $self->{entry_tooltip}; |
112 |
$self->set_title ($self->{title}); |
113 |
} |
114 |
} |
115 |
|
116 |
# (private) This method overloads the set_dockbar_tab_active method of |
117 |
# the Dockbar to capture the activation event of the tab. Mainly used |
118 |
# to remove highlightin. |
119 |
sub set_dockbar_tab_active { |
120 |
my ($self, $active) = @_; |
121 |
if ($active) { |
122 |
$self->set_inactive_fg (undef); # reset inactive color |
123 |
} |
124 |
$self->SUPER::set_dockbar_tab_active ($active); |
125 |
} |
126 |
|
127 |
# This method renders a message to the text field and sets highlighting |
128 |
# and does other stuff that a message can cause. |
129 |
sub message { |
130 |
my ($self, $para) = @_; |
131 |
|
132 |
if ($self->is_docked && !$self->is_docked_active) { |
133 |
if (($para->{color_flags} & NDI_COLOR_MASK) == NDI_RED) { |
134 |
$self->set_inactive_fg ([0, 0, 1]); |
135 |
} else { |
136 |
$self->set_inactive_fg ([0.6, 0.6, 1]); |
137 |
} |
138 |
} |
139 |
|
140 |
if ($para->{color_flags} & NDI_REPLY) { |
141 |
$self->select_my_tab; |
142 |
} |
143 |
|
144 |
if ($para->{color_flags} & NDI_CLEAR) { |
145 |
$self->clear_log; |
146 |
} |
147 |
|
148 |
my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; |
149 |
|
150 |
$para->{markup} = "<span foreground='#ffffff'>$time</span> $para->{markup}"; |
151 |
|
152 |
my $txt = $self->{txt}; |
153 |
$txt->add_paragraph ($para); |
154 |
$txt->scroll_to_bottom; |
155 |
} |
156 |
|
157 |
# This method is called when |
158 |
sub activate { |
159 |
my ($self, $preset) = @_; |
160 |
|
161 |
$self->SUPER::activate (); |
162 |
|
163 |
$self->{input}->{auto_activated} = 1; |
164 |
$self->{input}->grab_focus; |
165 |
|
166 |
if ($preset && $self->{input}->get_text eq '') { |
167 |
$self->{input}->set_text ($preset); |
168 |
} |
169 |
} |
170 |
|
171 |
# sets the fontsize of the chats textview |
172 |
sub set_fontsize { |
173 |
my ($self, $size) = @_; |
174 |
$self->{txt}->set_fontsize ($size); |
175 |
} |
176 |
|
177 |
# sets the maximum of paragraphs shown |
178 |
sub set_max_par { |
179 |
my ($self, $max_par) = @_; |
180 |
$self->{txt}{max_par} = $max_par; |
181 |
} |
182 |
|
183 |
# clears the text log |
184 |
sub clear_log { |
185 |
my ($self) = @_; |
186 |
|
187 |
$self->{txt}->clear; |
188 |
} |
189 |
|
190 |
1 |