… | |
… | |
48 | sort { (length $a) <=> (length $b) } |
48 | sort { (length $a) <=> (length $b) } |
49 | @args |
49 | @args |
50 | } sort { $a->{par} <=> $b->{par} } |
50 | } sort { $a->{par} <=> $b->{par} } |
51 | CFPlus::Pod::find command => "*"; |
51 | CFPlus::Pod::find command => "*"; |
52 | |
52 | |
53 | $self->connect_ext (event_music => sub { |
53 | $self->connect_ext (music => sub { |
54 | my ($ev) = @_; |
54 | my (%ev) = @_; |
55 | |
55 | |
56 | return unless $::CFG->{bgm_enable}; |
56 | return unless $::CFG->{bgm_enable}; |
57 | |
57 | |
58 | my $faces = $ev->{faces}; |
58 | my $faces = $ev{faces}; |
59 | my @songs; |
59 | my @songs; |
60 | |
60 | |
61 | # request music from server if appropriate |
61 | # request music from server if appropriate |
62 | if (my $faces = $ev->{faces}) { |
62 | if (my $faces = $ev{faces}) { |
63 | for (@$faces) { |
63 | for (@$faces) { |
64 | my ($face, $pri, $chksum) = @$_; |
64 | my ($face, $pri, $chksum) = @$_; |
65 | utf8::downgrade $chksum; |
65 | utf8::downgrade $chksum; |
66 | |
66 | |
67 | $chksum = unpack "H*", $chksum; |
67 | $chksum = unpack "H*", $chksum; |
… | |
… | |
97 | } |
97 | } |
98 | } |
98 | } |
99 | } |
99 | } |
100 | } |
100 | } |
101 | |
101 | |
102 | if (my $play = $ev->{play}) { |
102 | if (my $play = $ev{play}) { |
103 | &::audio_music_set ($self->{songs} = [map $self->{music_map}{$_}, @$play]); |
103 | &::audio_music_set ($self->{songs} = [map $self->{music_map}{$_}, @$play]); |
104 | } |
104 | } |
105 | }); |
105 | }); |
106 | |
106 | |
107 | $self->connect_ext (event_capabilities => sub { |
107 | $self->connect_ext (capabilities => sub { |
108 | my ($cap) = @_; |
108 | my (%cap) = @_; |
109 | |
109 | |
110 | if (my $ts = $cap->{tileset}) { |
110 | if (my $ts = $cap{tileset}) { |
111 | if (my ($default) = grep $_->[2] & 1, @$ts) { |
111 | if (my ($default) = grep $_->[2] & 1, @$ts) { |
112 | $self->{tileset} = $default; |
112 | $self->{tileset} = $default; |
113 | $self->{tilesize} = $default->[3]; |
113 | $self->{tilesize} = $default->[3]; |
114 | $self->setup_req (tileset => $default->[0]); |
114 | $self->setup_req (tileset => $default->[0]); |
115 | |
115 | |
… | |
… | |
121 | } |
121 | } |
122 | }); |
122 | }); |
123 | |
123 | |
124 | $self->{json_coder} |
124 | $self->{json_coder} |
125 | ->convert_blessed |
125 | ->convert_blessed |
126 | ->filter_json_single_key_object (__widget_ref__ => sub { |
126 | ->filter_json_single_key_object (__w_ => sub { |
127 | $self->{widget}{$_[0]} |
127 | $self->{widget}{$_[0]} |
128 | }); |
128 | }); |
129 | |
129 | |
130 | # widgetset new |
130 | # widgetset new |
131 | $self->connect_ext (ws_n => sub { |
131 | $self->connect_ext (ws_n => sub { |
132 | my ($arg) = @_; |
132 | my ($id) = @_; |
133 | |
133 | |
134 | $self->{widgetset}{$arg{id}} = { |
134 | $self->{widgetset}{$id} = { |
135 | w => {}, |
135 | w => {}, |
136 | }; |
136 | }; |
137 | }); |
137 | }); |
138 | |
138 | |
139 | # widgetset destroy |
139 | # widgetset destroy |
140 | $self->connect_ext (ws_d => sub { |
140 | $self->connect_ext (ws_d => sub { |
141 | my ($arg) = @_; |
141 | my ($id) = @_; |
142 | |
142 | |
143 | my $ws = delete $self->{widgetset}{$arg{id}} |
143 | my $ws = delete $self->{widgetset}{$id} |
144 | or return; |
144 | or return; |
145 | |
145 | |
146 | $_->destroy |
146 | $_->destroy |
147 | for values %{$ws->{w}}; |
147 | for values %{$ws->{w}}; |
148 | }); |
148 | }); |
149 | |
149 | |
150 | # widgetset destroy |
150 | # widgetset create |
151 | $self->connect_ext (ws_c => sub { |
151 | $self->connect_ext (ws_c => sub { |
152 | my ($arg) = @_; |
152 | my ($ws, $id, $class, $args) = @_; |
153 | |
|
|
154 | my $args = $arg->{args} || {}; |
|
|
155 | |
153 | |
156 | for my $ev (grep /^on_/, keys %$args) { |
154 | for my $ev (grep /^on_/, keys %$args) { |
157 | $args->{$ev} = sub { |
155 | $args->{$ev} = sub { |
158 | my $id = shift->{s_id}; |
156 | my $id = shift->{s_id}; |
159 | $self->send_exti_msg (w_e => id => $id, name => $ev, args => \@_); |
157 | $self->send_exti_msg (w_e => $id, $ev, \@_); |
160 | |
158 | |
161 | 1 |
159 | 1 |
162 | }; |
160 | }; |
163 | } |
161 | } |
164 | |
162 | |
165 | if (my $widget = eval { |
163 | if (my $widget = eval { |
166 | local $SIG{__DIE__}; |
164 | local $SIG{__DIE__}; |
167 | "CFPlus::UI::$arg->{class}"->new ( |
165 | "CFPlus::UI::$class"->new ( |
168 | %$args, |
166 | %$args, |
169 | s_ws => $arg->{ws}, |
167 | s_ws => $ws, |
170 | s_id => $arg->{id}, |
168 | s_id => $id, |
171 | ) |
169 | ) |
172 | } |
170 | } |
173 | ) { |
171 | ) { |
174 | $self->{widget}{$arg->{id}} |
172 | $self->{widget}{$id} |
175 | = $self->{widgetset}{$arg->{ws}}{w}{$arg->{id}} |
173 | = $self->{widgetset}{$ws}{w}{$id} |
176 | = $widget; |
174 | = $widget; |
177 | |
175 | |
178 | $widget->connect (on_destroy => sub { |
176 | $widget->connect (on_destroy => sub { |
179 | my ($widget) = @_; |
177 | my ($widget) = @_; |
180 | |
178 | |
181 | delete $self->{widget}{$widget->{s_id}}; |
179 | delete $self->{widget}{$widget->{s_id}}; |
182 | delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}}; |
180 | delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}}; |
183 | }); |
181 | }); |
184 | } else { |
182 | } else { |
185 | warn "server failed creating client-side widget " . (CFPlus::to_json $arg) . ": $@\n"; |
183 | warn "server failed creating client-side widget " . (CFPlus::to_json $class) . ": $@\n"; |
186 | $self->send_exti_msg (w_e => id => $arg->{id}, name => "destroy"); |
184 | $self->send_exti_msg (w_e => $id, "destroy"); |
187 | } |
185 | } |
188 | }); |
186 | }); |
189 | |
187 | |
190 | # widget call |
188 | # widget call |
191 | $self->connect_ext (w_c => sub { |
189 | $self->connect_ext (w_c => sub { |
192 | my ($arg) = @_; |
190 | my ($id, $rid, $method, $args) = @_; |
193 | |
191 | |
194 | my $w = $self->{widget}{$arg->{id}} |
192 | my $w = $self->{widget}{$id} |
195 | or return; |
193 | or return; |
196 | my $m = $arg->{name}; |
|
|
197 | |
194 | |
198 | my $a = $arg->{args} || []; |
195 | $args ||= []; |
199 | |
196 | |
200 | if (exists $arg->{rid}) { |
197 | if ($rid) { |
201 | $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->$m (@$a)]); |
198 | $self->send_exti_msg (w_r => $rid, [$w->$method (@$args)]); |
202 | } else { |
199 | } else { |
203 | $w->$m (@$a); |
200 | $w->$method (@$args); |
204 | } |
201 | } |
205 | }); |
202 | }); |
206 | |
203 | |
207 | # widget set |
204 | # widget set |
208 | $self->connect_ext (w_s => sub { |
205 | $self->connect_ext (w_s => sub { |
209 | my ($arg) = @_; |
206 | my ($id, $attr) = @_; |
210 | |
207 | |
211 | my $w = $self->{widget}{$arg->{id}} |
208 | my $w = $self->{widget}{$id} |
212 | or return; |
209 | or return; |
213 | |
210 | |
214 | $w->{$_->[0]} = $_->[1] |
211 | for (my $i = 0; $i < $#$attr; $i += 2) { |
215 | for @{ $arg->{attr} }; |
212 | my ($member, $value) = @$attr[$i, $i+1]; |
|
|
213 | if (defined $value) { |
|
|
214 | $w->{$member} = $value; |
|
|
215 | } else { |
|
|
216 | delete $w->{$member}; |
|
|
217 | } |
|
|
218 | $w->{parent}->realloc if $member =~ /^c_/ && $w->{visible}; |
|
|
219 | } |
216 | }); |
220 | }); |
217 | |
221 | |
218 | # widget get |
222 | # widget get |
219 | $self->connect_ext (w_g => sub { |
223 | $self->connect_ext (w_g => sub { |
220 | my ($arg) = @_; |
224 | my ($id, $rid, $attr) = @_; |
221 | |
225 | |
222 | my $w = $self->{widget}{$arg->{id}} |
226 | my $w = $self->{widget}{$id} |
223 | or return; |
227 | or return; |
224 | |
228 | |
225 | $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [map $w->{$_}, @{$arg->{attr}}]); |
229 | $self->send_exti_msg (w_r => $rid, [map $w->{$_}, @$attr]); |
226 | }); |
230 | }); |
227 | |
231 | |
228 | # destroy widgets on logout |
232 | # destroy widgets on logout |
229 | $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub { |
233 | $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub { |
230 | for my $ws (values %{delete $self->{widgetset} || {}}) { |
234 | for my $ws (values %{delete $self->{widgetset} || {}}) { |
… | |
… | |
1162 | } |
1166 | } |
1163 | |
1167 | |
1164 | sub logged_in { |
1168 | sub logged_in { |
1165 | my ($self) = @_; |
1169 | my ($self) = @_; |
1166 | |
1170 | |
1167 | $self->send_ext_req (cfplus_support => version => 1, sub { |
1171 | $self->send_ext_req (cfplus_support => version => 2, sub { |
|
|
1172 | my (%msg) = @_; |
|
|
1173 | |
1168 | $self->{cfplus_ext} = $_[0]{version}; |
1174 | $self->{cfplus_ext} = $msg{version}; |
1169 | $self->update_server_info; |
1175 | $self->update_server_info; |
1170 | |
1176 | |
1171 | if ($self->{cfplus_ext} >= 2) { |
1177 | if ($self->{cfplus_ext} >= 2) { |
1172 | $self->send_ext_req ("editor_support", sub { |
1178 | $self->send_ext_req ("editor_support", sub { |
1173 | $self->{editor_support} = $_[0]; |
1179 | $self->{editor_support} = { @_ }; |
1174 | $self->update_server_info; |
1180 | $self->update_server_info; |
1175 | |
1181 | |
1176 | 0 |
1182 | 0 |
1177 | }); |
1183 | }); |
1178 | } |
1184 | } |
… | |
… | |
1198 | |
1204 | |
1199 | sub lookat { |
1205 | sub lookat { |
1200 | my ($self, $x, $y) = @_; |
1206 | my ($self, $x, $y) = @_; |
1201 | |
1207 | |
1202 | if ($self->{cfplus_ext}) { |
1208 | if ($self->{cfplus_ext}) { |
1203 | $self->send_ext_req (lookat => dx => $x, dy => $y, sub { |
1209 | $self->send_ext_req (lookat => $x, $y, sub { |
1204 | my ($msg) = @_; |
1210 | my (%msg) = @_; |
1205 | |
1211 | |
1206 | if (exists $msg->{npc_dialog}) { |
1212 | if (exists $msg{npc_dialog}) { |
1207 | # start npc chat dialog |
1213 | # start npc chat dialog |
1208 | $self->{npc_dialog} = new CFPlus::NPCDialog:: |
1214 | $self->{npc_dialog} = new CFPlus::NPCDialog:: |
1209 | dx => $x, |
1215 | token => $msg{npc_dialog}, |
1210 | dy => $y, |
|
|
1211 | title => "$msg->{npc_dialog} (NPC)", |
1216 | title => "$msg{npc_dialog}[0] (NPC)", |
1212 | conn => $self, |
1217 | conn => $self, |
1213 | ; |
1218 | ; |
1214 | } |
1219 | } |
1215 | }); |
1220 | }); |
1216 | } |
1221 | } |
… | |
… | |
1281 | on_activate => sub { $this->destroy; 1 }, |
1286 | on_activate => sub { $this->destroy; 1 }, |
1282 | ; |
1287 | ; |
1283 | |
1288 | |
1284 | $self->update_options; |
1289 | $self->update_options; |
1285 | |
1290 | |
1286 | $self->{id} = $self->{conn}->send_ext_req ( |
1291 | $self->{id} = "npc-channel-" . $self->{conn}->token; |
1287 | npc_dialog_begin => dx => $self->{dx}, dy => $self->{dy}, |
1292 | $self->{conn}->connect_ext ($self->{id} => sub { |
1288 | sub { $this && $this->feed (@_) } |
1293 | $this->feed (@_) if $this; |
1289 | ); |
1294 | }); |
|
|
1295 | |
|
|
1296 | $self->{conn}->send_ext_msg (npc_dialog_begin => $self->{id}, $self->{token}); |
1290 | |
1297 | |
1291 | $self->{entry}->grab_focus; |
1298 | $self->{entry}->grab_focus; |
1292 | |
1299 | |
1293 | $self->{textview}->add_paragraph ({ |
1300 | $self->{textview}->add_paragraph ({ |
1294 | fg => [1, 1, 0, 1], |
1301 | fg => [1, 1, 0, 1], |
… | |
… | |
1317 | ); |
1324 | ); |
1318 | } |
1325 | } |
1319 | } |
1326 | } |
1320 | |
1327 | |
1321 | sub feed { |
1328 | sub feed { |
1322 | my ($self, $msg) = @_; |
1329 | my ($self, $type, @arg) = @_; |
1323 | |
1330 | |
1324 | CFPlus::weaken $self; |
1331 | CFPlus::weaken $self; |
1325 | |
1332 | |
1326 | if ($msg->{msgtype} eq "reply") { |
1333 | if ($type eq "update") { |
|
|
1334 | my (%info) = @arg; |
|
|
1335 | |
1327 | $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []}; |
1336 | $self->{kw}{$_} = 1 for @{$info{add_topics} || []}; |
1328 | $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []}; |
1337 | $self->{kw}{$_} = 0 for @{$info{del_topics} || []}; |
1329 | |
|
|
1330 | my $text = "\n" . CFPlus::Protocol::sanitise_xml $msg->{msg}; |
|
|
1331 | my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; |
|
|
1332 | my @link; |
|
|
1333 | $text =~ s{ |
|
|
1334 | ($match) |
|
|
1335 | }{ |
|
|
1336 | my $kw = $1; |
|
|
1337 | |
|
|
1338 | push @link, new CFPlus::UI::Label |
|
|
1339 | markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>", |
|
|
1340 | can_hover => 1, |
|
|
1341 | can_events => 1, |
|
|
1342 | padding_x => 0, |
|
|
1343 | padding_y => 0, |
|
|
1344 | on_button_up => sub { |
|
|
1345 | $self->send ($kw); |
|
|
1346 | }; |
|
|
1347 | |
|
|
1348 | "\x{fffc}" |
|
|
1349 | }giex; |
|
|
1350 | |
1338 | |
|
|
1339 | if (exists $info{msg}) { |
|
|
1340 | my $text = "\n" . CFPlus::Protocol::sanitise_xml $info{msg}; |
|
|
1341 | my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; |
|
|
1342 | my @link; |
|
|
1343 | $text =~ s{ |
|
|
1344 | ($match) |
|
|
1345 | }{ |
|
|
1346 | my $kw = $1; |
|
|
1347 | |
|
|
1348 | push @link, new CFPlus::UI::Label |
|
|
1349 | markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>", |
|
|
1350 | can_hover => 1, |
|
|
1351 | can_events => 1, |
|
|
1352 | padding_x => 0, |
|
|
1353 | padding_y => 0, |
|
|
1354 | on_button_up => sub { |
|
|
1355 | $self->send ($kw); |
|
|
1356 | }; |
|
|
1357 | |
|
|
1358 | "\x{fffc}" |
|
|
1359 | }giex; |
|
|
1360 | |
1351 | $self->{textview}->add_paragraph ({ markup => $text, widget => \@link }); |
1361 | $self->{textview}->add_paragraph ({ markup => $text, widget => \@link }); |
1352 | $self->{textview}->scroll_to_bottom; |
1362 | $self->{textview}->scroll_to_bottom; |
|
|
1363 | } |
|
|
1364 | |
1353 | $self->update_options; |
1365 | $self->update_options; |
1354 | } else { |
1366 | } else { |
1355 | $self->destroy; |
1367 | $self->destroy; |
1356 | } |
1368 | } |
1357 | |
1369 | |
… | |
… | |
1362 | my ($self, $msg) = @_; |
1374 | my ($self, $msg) = @_; |
1363 | |
1375 | |
1364 | $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg }); |
1376 | $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg }); |
1365 | $self->{textview}->scroll_to_bottom; |
1377 | $self->{textview}->scroll_to_bottom; |
1366 | |
1378 | |
1367 | $self->{conn}->send_ext_msg (npc_dialog_tell => msgid => $self->{id}, msg => $msg); |
1379 | $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg); |
1368 | } |
1380 | } |
1369 | |
1381 | |
1370 | sub destroy { |
1382 | sub destroy { |
1371 | my ($self) = @_; |
1383 | my ($self) = @_; |
1372 | |
1384 | |
1373 | #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 |
1385 | #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 |
1374 | |
1386 | |
1375 | if ($self->{conn}) { |
1387 | if ($self->{conn}) { |
1376 | $self->{conn}->send_ext_msg (npc_dialog_end => msgid => $self->{id}) if $self->{id}; |
1388 | $self->{conn}->send_ext_msg (npc_dialog_end => $self->{id}) if $self->{id}; |
1377 | delete $self->{conn}{npc_dialog}; |
1389 | delete $self->{conn}{npc_dialog}; |
1378 | $self->{conn}->disconnect_ext ($self->{id}); |
1390 | $self->{conn}->disconnect_ext ($self->{id}); |
1379 | } |
1391 | } |
1380 | |
1392 | |
1381 | $self->SUPER::destroy; |
1393 | $self->SUPER::destroy; |