… | |
… | |
17 | |
17 | |
18 | cf::client->attach ( |
18 | cf::client->attach ( |
19 | on_connect => sub { |
19 | on_connect => sub { |
20 | my ($ns) = @_; |
20 | my ($ns) = @_; |
21 | |
21 | |
22 | Scalar::Util::weaken (my $weakns = $ns); |
|
|
23 | |
|
|
24 | $ns->{id} = "a"; |
22 | $ns->{id} = "a"; |
25 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
23 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
26 | $weakns->{widget}{$_[0]} |
24 | $ns->{widget}{$_[0]} |
27 | }); |
25 | }); |
|
|
26 | |
|
|
27 | cf::weaken $ns; |
28 | }, |
28 | }, |
29 | ); |
29 | ); |
30 | |
30 | |
31 | sub csc_update_stats { |
31 | sub csc_update_stats { |
32 | my ($ns) = @_; |
32 | my ($ns) = @_; |
… | |
… | |
96 | }, |
96 | }, |
97 | ] |
97 | ] |
98 | EOF |
98 | EOF |
99 | die if $@; |
99 | die if $@; |
100 | |
100 | |
101 | sub demo_start { |
|
|
102 | my ($ns) = @_; |
|
|
103 | |
|
|
104 | my $ws = $ns->{csc} = $ns->new_widgetset; |
|
|
105 | |
|
|
106 | my ($tl, $entry) = $ws->template (inline => $cg_template, |
|
|
107 | [ |
|
|
108 | toplevel => {}, |
|
|
109 | entry => { |
|
|
110 | text => "xyz", |
|
|
111 | on_changed => sub { |
|
|
112 | warn "changed<@_>\n";#d# |
|
|
113 | }, |
|
|
114 | }, |
|
|
115 | ], |
|
|
116 | ); |
|
|
117 | |
|
|
118 | $tl->show; |
|
|
119 | |
|
|
120 | $ns->{xxxw} = [$tl, $entry];#d# |
|
|
121 | |
|
|
122 | # $ws->find ("setup_notebook")->add ($ws->{tab}); |
|
|
123 | # $ws->find ("setup_dialog")->toggle_visibility; |
|
|
124 | } |
|
|
125 | |
|
|
126 | sub csc_start { |
101 | sub csc_start { |
127 | my ($ns) = @_; |
102 | my ($ns) = @_; |
128 | |
103 | |
129 | my $ws = $ns->{csc} = $ns->new_widgetset; |
104 | my $ws = $ns->{csc} = $ns->new_widgetset; |
130 | |
105 | |
… | |
… | |
169 | |
144 | |
170 | csc_update_stats $ns; |
145 | csc_update_stats $ns; |
171 | |
146 | |
172 | $ws->{tl} = $w; |
147 | $ws->{tl} = $w; |
173 | $w->show; |
148 | $w->show; |
|
|
149 | |
|
|
150 | # my ($tl, $entry) = $ws->template (inline => $cg_template, |
|
|
151 | # [ |
|
|
152 | # toplevel => {}, |
|
|
153 | # entry => { |
|
|
154 | # text => "xyz", |
|
|
155 | # on_changed => sub { |
|
|
156 | # warn "changed<@_>\n";#d# |
|
|
157 | # }, |
|
|
158 | # }, |
|
|
159 | # ], |
|
|
160 | # ); |
|
|
161 | # |
|
|
162 | # $tl->show; |
|
|
163 | # |
|
|
164 | # $ns->{xxxw} = [$tl, $entry];#d# |
|
|
165 | # |
|
|
166 | # $ws->find ("setup_notebook")->add ($ws->{tab}); |
|
|
167 | # $ws->find ("setup_dialog")->toggle_visibility; |
174 | } |
168 | } |
175 | |
169 | |
176 | cf::player->attach ( |
170 | cf::player->attach ( |
177 | on_login => sub { |
171 | on_login => sub { |
178 | my ($pl) = @_; |
172 | my ($pl) = @_; |
179 | |
173 | |
|
|
174 | my $ns = $pl->ns; |
|
|
175 | return unless $ns->{can_widget}; |
|
|
176 | |
180 | return unless $cf::CFG{devel}; |
177 | return unless $cf::CFG{devel}; |
181 | |
178 | |
182 | my $ns = $pl->ns; |
|
|
183 | |
|
|
184 | return unless $ns->{can_widget}; |
|
|
185 | #csc_start $ns; |
179 | #csc_start $ns; |
186 | demo_start $ns; |
|
|
187 | }, |
180 | }, |
188 | ); |
181 | ); |
189 | |
182 | |
190 | cf::register_exticmd w_e => sub { |
183 | cf::register_exticmd w_e => sub { |
191 | my ($ns, $id, @args) = @_; |
184 | my ($ns, $id, @args) = @_; |
… | |
… | |
206 | id => $id, |
199 | id => $id, |
207 | ns => $self, |
200 | ns => $self, |
208 | _w => {}, |
201 | _w => {}, |
209 | }, "ext::widget::set"; |
202 | }, "ext::widget::set"; |
210 | |
203 | |
|
|
204 | cf::weaken $ws->{ns}; |
|
|
205 | |
211 | $ws->msg (ws_n => $id); |
206 | $ws->msg (ws_n => $id); |
212 | |
207 | |
213 | $ws |
208 | $ws |
214 | } |
209 | } |
215 | |
210 | |
… | |
… | |
241 | |
236 | |
242 | sub msg { |
237 | sub msg { |
243 | my ($self, @msg) = @_; |
238 | my ($self, @msg) = @_; |
244 | |
239 | |
245 | if (my $ns = shift->{ns}) { |
240 | if (my $ns = shift->{ns}) { |
|
|
241 | return unless $ns->{json_coder};#d# |
246 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
242 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
247 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
243 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
248 | } |
244 | } |
249 | } |
245 | } |
250 | |
246 | |
… | |
… | |
266 | sub new { |
262 | sub new { |
267 | my ($self, $class, %args) = @_; |
263 | my ($self, $class, %args) = @_; |
268 | |
264 | |
269 | my $proxy = $self->alloc; |
265 | my $proxy = $self->alloc; |
270 | |
266 | |
271 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
267 | cf::weaken |
272 | Scalar::Util::weaken ($proxy->{ws} = $self); |
268 | +($self->{_w}{$proxy->{id}} = $proxy), |
|
|
269 | +($proxy->{ws} = $self); |
273 | |
270 | |
274 | for my $ev (grep /^on_/, keys %args) { |
271 | for my $ev (grep /^on_/, keys %args) { |
275 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
272 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
276 | } |
273 | } |
277 | |
274 | |
… | |
… | |
287 | |
284 | |
288 | sub template { |
285 | sub template { |
289 | my ($self, $type, $template, $args, $done_cb) = @_; |
286 | my ($self, $type, $template, $args, $done_cb) = @_; |
290 | |
287 | |
291 | my %cfg; |
288 | my %cfg; |
292 | my @res; |
|
|
293 | |
289 | |
294 | while (@$args) { |
290 | while (@$args) { |
295 | my ($name, $args) = splice @$args, 0, 2, (); |
291 | my ($name, $args) = splice @$args, 0, 2, (); |
296 | |
292 | |
297 | my $proxy = $self->alloc; |
293 | my $proxy = $self->alloc; |
298 | |
294 | |
|
|
295 | $self->{delete $args->{alias} or $name} = $proxy; |
|
|
296 | |
|
|
297 | cf::weaken |
299 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
298 | +($self->{_w}{$proxy->{id}} = $proxy), |
300 | Scalar::Util::weaken ($proxy->{ws} = $self); |
299 | +($proxy->{ws} = $self); |
301 | |
300 | |
302 | for my $ev (grep /^on_/, keys %$args) { |
301 | for my $ev (grep /^on_/, keys %$args) { |
303 | $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); |
302 | $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); |
304 | } |
303 | } |
305 | |
304 | |
306 | $cfg{$name} = { |
305 | $cfg{$name} = { |
307 | %$args, |
306 | %$args, |
308 | id => $proxy->{id}, |
307 | id => $proxy->{id}, |
309 | }; |
308 | }; |
310 | |
|
|
311 | push @res, $proxy; |
|
|
312 | } |
309 | } |
313 | |
310 | |
314 | if ($done_cb) { |
311 | if ($done_cb) { |
315 | my $proxy = $self->alloc; |
312 | my $proxy = $self->alloc; |
316 | my $ocb = $done_cb; |
313 | my $ocb = $done_cb; |
… | |
… | |
319 | undef $done_cb; |
316 | undef $done_cb; |
320 | &$ocb |
317 | &$ocb |
321 | }); |
318 | }); |
322 | } |
319 | } |
323 | |
320 | |
|
|
321 | if ($type eq "face") { |
|
|
322 | $self->{ns}->send_face ($template, 100); |
|
|
323 | $self->{ns}->flush_fx; |
|
|
324 | } |
|
|
325 | |
324 | $self->msg (ws_ct => |
326 | $self->msg (ws_ct => |
325 | $self->{id}, |
327 | $self->{id}, |
326 | $type => $template, |
328 | $type => $template, |
327 | $done_cb, |
329 | $done_cb, |
328 | \%cfg, |
330 | \%cfg, |
329 | ); |
331 | ); |
330 | |
|
|
331 | @res |
|
|
332 | } |
332 | } |
333 | |
333 | |
334 | sub find { |
334 | sub find { |
335 | my ($self, @names) = @_; |
335 | my ($self, @names) = @_; |
336 | |
336 | |
… | |
… | |
354 | } |
354 | } |
355 | |
355 | |
356 | ############################################################################# |
356 | ############################################################################# |
357 | |
357 | |
358 | package ext::widget::proxy; |
358 | package ext::widget::proxy; |
359 | |
|
|
360 | sub DESTROY { |
|
|
361 | my ($self) = @_; |
|
|
362 | |
|
|
363 | delete $self->{ns}{widget}{$self->{id}}; |
|
|
364 | |
|
|
365 | if (my $ws = $self->{ws}) { |
|
|
366 | $self->msg (w_c => 0, "destroy"); |
|
|
367 | delete $ws->{_w}{$self->{id}}; |
|
|
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | sub cb { |
|
|
372 | my ($self, $cb) = @_; |
|
|
373 | |
|
|
374 | my $proxy = bless { |
|
|
375 | ns => $self->{ns}, |
|
|
376 | id => $self->{ns}->alloc_wid, |
|
|
377 | }, "ext::widget::callback"; |
|
|
378 | |
|
|
379 | Scalar::Util::weaken $proxy->{ns}; |
|
|
380 | |
|
|
381 | $self->{ns}{widget_cb}{$proxy->{id}} = $cb; |
|
|
382 | |
|
|
383 | $proxy |
|
|
384 | } |
|
|
385 | |
|
|
386 | sub oneshot_cb { |
|
|
387 | my ($self, $cb) = @_; |
|
|
388 | |
|
|
389 | if ("CODE" eq ref $cb) { |
|
|
390 | my $ocb = $cb; |
|
|
391 | $cb = $self->cb (sub { |
|
|
392 | undef $cb; |
|
|
393 | &$ocb |
|
|
394 | }); |
|
|
395 | } |
|
|
396 | |
|
|
397 | $cb |
|
|
398 | } |
|
|
399 | |
359 | |
400 | sub msg { |
360 | sub msg { |
401 | my ($self, $type, @arg) = @_; |
361 | my ($self, $type, @arg) = @_; |
402 | |
362 | |
403 | if (my $ns = $self->{ns}) { |
363 | if (my $ns = $self->{ns}) { |
… | |
… | |
441 | } |
401 | } |
442 | |
402 | |
443 | () |
403 | () |
444 | } |
404 | } |
445 | |
405 | |
|
|
406 | sub DESTROY { |
|
|
407 | my ($self) = @_; |
|
|
408 | |
|
|
409 | delete $self->{ns}{widget}{$self->{id}}; |
|
|
410 | |
|
|
411 | if (my $ws = $self->{ws}) { |
|
|
412 | $self->msg (w_c => 0, "destroy"); |
|
|
413 | delete $ws->{_w}{$self->{id}}; |
|
|
414 | } |
|
|
415 | } |
|
|
416 | |
|
|
417 | sub cb { |
|
|
418 | my ($self, $cb) = @_; |
|
|
419 | |
|
|
420 | my $proxy = bless { |
|
|
421 | ns => $self->{ns}, |
|
|
422 | id => $self->{ns}->alloc_wid, |
|
|
423 | }, "ext::widget::callback"; |
|
|
424 | |
|
|
425 | cf::weaken $proxy->{ns}; |
|
|
426 | |
|
|
427 | $self->{ns}{widget_cb}{$proxy->{id}} = $cb; |
|
|
428 | |
|
|
429 | $proxy |
|
|
430 | } |
|
|
431 | |
|
|
432 | sub oneshot_cb { |
|
|
433 | my ($self, $cb) = @_; |
|
|
434 | |
|
|
435 | if ("CODE" eq ref $cb) { |
|
|
436 | my $ocb = $cb; |
|
|
437 | $cb = cb $self, sub { |
|
|
438 | undef $cb; |
|
|
439 | &$ocb |
|
|
440 | }; |
|
|
441 | } |
|
|
442 | |
|
|
443 | $cb |
|
|
444 | } |
|
|
445 | |
446 | sub set { |
446 | sub set { |
447 | my ($self, @kv) = @_; |
447 | my ($self, @kv) = @_; |
448 | |
448 | |
449 | $self->msg (w_s => \@kv); |
449 | $self->msg (w_s => \@kv); |
450 | } |
450 | } |