1 |
package CFPlus::UI::ChatView; |
2 |
|
3 |
use strict; |
4 |
use utf8; |
5 |
|
6 |
our @ISA = CFPlus::UI::VBox::; |
7 |
|
8 |
sub new { |
9 |
my $class = shift; |
10 |
my $self = $class->SUPER::new (@_); |
11 |
|
12 |
$self->add ($self->{txt} = new CFPlus::UI::TextScroller ( |
13 |
expand => 1, |
14 |
font => $::FONT_FIXED, |
15 |
fontsize => $::CFG->{log_fontsize}, |
16 |
indent => -4, |
17 |
can_hover => 1, |
18 |
can_events => 1, |
19 |
max_par => $::CFG->{logview_max_par}, |
20 |
tooltip => |
21 |
$self->{text_tooltip} |
22 |
|| "<b>Server Log</b>. This text viewer contains all recent messages " |
23 |
."sent by the server.", |
24 |
)); |
25 |
|
26 |
$self->add (my $hb = CFPlus::UI::HBox->new); |
27 |
|
28 |
if ($self->{say_command}) { |
29 |
$hb->add (CFPlus::UI::Label->new (markup => $self->{say_command})); |
30 |
} |
31 |
$hb->add ($self->{input} = CFPlus::UI::Entry->new ( |
32 |
expand => 1, |
33 |
tooltip => |
34 |
$self->{entry_tooltip} |
35 |
|| "<b>Chat Box</b>. If you enter a text and press return/enter here, the current <i>communication command</i> " |
36 |
. "from the client setup will be prepended (e.g. <b>shout</b>, <b>chat</b>...). " |
37 |
. "If you prepend a slash (/), you will submit a command instead (similar to IRC). " |
38 |
. "A better way to submit commands (and the occasional chat command) is often the map command completer.", |
39 |
on_focus_in => sub { |
40 |
my ($input, $prev_focus) = @_; |
41 |
|
42 |
delete $input->{refocus_map}; |
43 |
|
44 |
if ($prev_focus == $::MAPWIDGET && $input->{auto_activated}) { |
45 |
$input->{refocus_map} = 1; |
46 |
} |
47 |
delete $input->{auto_activated}; |
48 |
|
49 |
0 |
50 |
}, |
51 |
on_activate => sub { |
52 |
my ($input, $text) = @_; |
53 |
$input->set_text (''); |
54 |
|
55 |
return unless $::CONN; |
56 |
|
57 |
if ($text =~ /^\/(.*)/) { |
58 |
$::CONN->user_send ($1); |
59 |
} elsif (length $text) { |
60 |
my $say_cmd = $self->{say_command}; |
61 |
$::CONN->user_send ($say_cmd . $text); |
62 |
} else { |
63 |
$input->{refocus_map} = 1; |
64 |
} |
65 |
if (delete $input->{refocus_map}) { |
66 |
$::MAPWIDGET->grab_focus; |
67 |
} |
68 |
|
69 |
0 |
70 |
}, |
71 |
on_key_down => sub { |
72 |
my ($input, $ev) = @_; |
73 |
my $uni = $ev->{unicode}; |
74 |
my $mod = $ev->{mod}; |
75 |
|
76 |
if ($uni >= ord "0" && $uni <= ord "9" && $mod & CFPlus::KMOD_ALT) { |
77 |
$::MAPWIDGET->emit (key_down => $ev); |
78 |
return 1; |
79 |
} |
80 |
|
81 |
0 |
82 |
}, |
83 |
on_escape => sub { |
84 |
$::MAPWIDGET->grab_focus; |
85 |
|
86 |
0 |
87 |
}, |
88 |
)); |
89 |
|
90 |
$self |
91 |
} |
92 |
|
93 |
sub message { |
94 |
my ($self, $para) = @_; |
95 |
|
96 |
my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; |
97 |
|
98 |
$para->{markup} = "<span foreground='#ffffff'>$time</span> $para->{markup}"; |
99 |
|
100 |
my $txt = $self->{txt}; |
101 |
$txt->add_paragraph ($para); |
102 |
$txt->scroll_to_bottom; |
103 |
} |
104 |
|
105 |
sub activate_console { |
106 |
my ($self, $preset) = @_; |
107 |
|
108 |
$self->{input}->{auto_activated} = 1; |
109 |
$self->{input}->grab_focus; |
110 |
|
111 |
if ($preset && $self->{input}->get_text eq '') { |
112 |
$self->{input}->set_text ($preset); |
113 |
} |
114 |
} |
115 |
|
116 |
sub set_fontsize { |
117 |
my ($self, $size) = @_; |
118 |
$self->{txt}->set_fontsize ($size); |
119 |
} |
120 |
|
121 |
sub set_max_para { |
122 |
my ($self, $max_par) = @_; |
123 |
$self->{txt}{max_par} = $max_par; |
124 |
} |
125 |
|