1 | #! perl # mandatory depends=login |
1 | #! perl # mandatory depends=login |
2 | |
2 | |
3 | # sends the following ext message types |
3 | # sends the following ext message types |
|
|
4 | # ws_a id name... # associate well-known widget with given id |
4 | # ws_n ws # widgetset new |
5 | # ws_n ws # widgetset new |
5 | # ws_d ws # widgetset destroy |
6 | # ws_d ws # widgetset destroy |
6 | # ws_c ws id class args # widgetset create |
7 | # ws_c ws id class @args # widgetset create |
|
|
8 | # ws_ct ws templateface \%cfg # widgetset create from template |
7 | # w_c id rid name args # widget method call |
9 | # w_c id rid name @args # widget method call |
8 | # w_s id @attr # widget member set |
10 | # w_s id @attr # widget member set |
9 | # w_g id rid @attr # widget member get |
11 | # w_g id rid @attr # widget member get |
10 | # |
12 | # |
11 | # and expects the following exti message types |
13 | # and expects the following exti message types |
12 | # w_r rid res # widget call return |
|
|
13 | # w_e id name args # widget_event |
14 | # w_e id @args # widget_call |
14 | |
15 | |
15 | our $DEBUG = 1; |
16 | our $DEBUG = 1; |
16 | |
17 | |
17 | cf::client->attach ( |
18 | cf::client->attach ( |
18 | on_connect => sub { |
19 | on_connect => sub { |
19 | my ($ns) = @_; |
20 | my ($ns) = @_; |
20 | |
21 | |
21 | Scalar::Util::weaken (my $weakns = $ns); |
22 | Scalar::Util::weaken (my $weakns = $ns); |
22 | |
23 | |
23 | $ns->{id} = "a"; |
24 | $ns->{id} = "a"; |
24 | $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { |
25 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
25 | # cannot deserialise ATM |
26 | $weakns->{widget}{$_[0]} |
26 | undef |
|
|
27 | }); |
27 | }); |
28 | }, |
28 | }, |
29 | ); |
29 | ); |
30 | |
30 | |
31 | sub csc_update_stats { |
31 | sub csc_update_stats { |
32 | my ($ns) = @_; |
32 | my ($ns) = @_; |
33 | |
33 | |
34 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
34 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
35 | $v->set_text ($ns->pl->ob->stats->$k); |
35 | $v->set_text ($ns->pl->ob->stats->$k); |
36 | } |
36 | } |
|
|
37 | } |
|
|
38 | |
|
|
39 | sub demo_start { |
|
|
40 | my ($ns) = @_; |
|
|
41 | |
|
|
42 | my $ws = $ns->{csc} = $ns->new_widgetset; |
|
|
43 | |
|
|
44 | my ($tl, $entry) = $ws->template (undef, |
|
|
45 | toplevel => {}, |
|
|
46 | entry => { |
|
|
47 | text => "xyz", |
|
|
48 | on_changed => sub { |
|
|
49 | warn "changed<@_>\n";#d# |
|
|
50 | }, |
|
|
51 | }, |
|
|
52 | ); |
|
|
53 | |
|
|
54 | $tl->show; |
|
|
55 | |
|
|
56 | $ns->{xxxw} = [$tl, $entry];#d# |
|
|
57 | |
|
|
58 | # $ws->find ("setup_notebook")->add ($ws->{tab}); |
|
|
59 | # $ws->find ("setup_dialog")->toggle_visibility; |
37 | } |
60 | } |
38 | |
61 | |
39 | sub csc_start { |
62 | sub csc_start { |
40 | my ($ns) = @_; |
63 | my ($ns) = @_; |
41 | |
64 | |
… | |
… | |
53 | }, |
76 | }, |
54 | ); |
77 | ); |
55 | |
78 | |
56 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
79 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
57 | |
80 | |
58 | $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); |
81 | $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); |
59 | |
82 | |
60 | $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); |
83 | $stats->add_at (0, 0, (my $statstable = $ws->new ("Table"))); |
61 | |
84 | |
62 | for ( |
85 | for ( |
63 | [0, "Str"], |
86 | [0, "Str"], |
64 | [1, "Dex"], |
87 | [1, "Dex"], |
65 | [2, "Con"], |
88 | [2, "Con"], |
… | |
… | |
80 | )); |
103 | )); |
81 | } |
104 | } |
82 | |
105 | |
83 | csc_update_stats $ns; |
106 | csc_update_stats $ns; |
84 | |
107 | |
|
|
108 | $ws->{tl} = $w; |
85 | $w->show; |
109 | $w->show; |
86 | } |
110 | } |
87 | |
111 | |
88 | cf::player->attach ( |
112 | cf::player->attach ( |
89 | on_login => sub { |
113 | on_login => sub { |
… | |
… | |
93 | |
117 | |
94 | my $ns = $pl->ns; |
118 | my $ns = $pl->ns; |
95 | |
119 | |
96 | return unless $ns->{can_widget}; |
120 | return unless $ns->{can_widget}; |
97 | #csc_start $ns; |
121 | #csc_start $ns; |
|
|
122 | demo_start $ns; |
98 | }, |
123 | }, |
99 | ); |
124 | ); |
100 | |
125 | |
101 | cf::register_exticmd w_e => sub { |
126 | cf::register_exticmd w_e => sub { |
102 | my ($ns, $id, $name, $args) = @_; |
127 | my ($ns, $id, @args) = @_; |
103 | |
128 | |
104 | if (my $w = $ns->{widget}{$id}) { |
129 | if (my $cb = $ns->{widget_cb}{$id}) { |
105 | if (my $cb = $w->{ev}{$name}) { |
|
|
106 | $_->($w, @$args) |
|
|
107 | for @$cb; |
|
|
108 | } |
|
|
109 | } |
|
|
110 | |
|
|
111 | () |
|
|
112 | }; |
|
|
113 | |
|
|
114 | cf::register_exticmd w_r => sub { |
|
|
115 | my ($ns, $rid, $res) = @_; |
|
|
116 | |
|
|
117 | if (my $cb = delete $ns->{widget_return}{$rid}) { |
|
|
118 | $cb->(@$res); |
130 | $cb->(@args); |
119 | } |
131 | } |
120 | |
132 | |
121 | () |
133 | () |
122 | }; |
134 | }; |
123 | |
135 | |
… | |
… | |
135 | $ws->msg (ws_n => $id); |
147 | $ws->msg (ws_n => $id); |
136 | |
148 | |
137 | $ws |
149 | $ws |
138 | } |
150 | } |
139 | |
151 | |
|
|
152 | sub cf::client::alloc_wid { |
|
|
153 | pop @{ $_[0]{ids} } |
|
|
154 | or ++$_[0]{id} |
|
|
155 | } |
|
|
156 | |
|
|
157 | sub cf::client::free_wid { |
|
|
158 | push @{ $_[0]{ids} }, $_[1]; |
|
|
159 | } |
|
|
160 | |
140 | ############################################################################# |
161 | ############################################################################# |
141 | |
162 | |
142 | package ext::widget::set; |
163 | package ext::widget::set; |
143 | |
164 | |
144 | sub DESTROY { |
165 | sub DESTROY { |
… | |
… | |
161 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
182 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
162 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
183 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
163 | } |
184 | } |
164 | } |
185 | } |
165 | |
186 | |
166 | sub new { |
187 | sub alloc { |
167 | my ($self, $class, %args) = @_; |
188 | my ($self) = @_; |
168 | |
189 | |
169 | my $id = ++$self->{ns}{id}; |
190 | my $id = $self->{ns}->alloc_wid; |
170 | |
191 | |
171 | my $proxy = bless { |
192 | my $proxy = bless { |
172 | id => $id, |
193 | id => $id, |
173 | }, "ext::widget::proxy"; |
194 | }, "ext::widget::proxy"; |
174 | |
195 | |
175 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
|
|
176 | Scalar::Util::weaken ($proxy->{ws} = $self); |
|
|
177 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
196 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
178 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
197 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
179 | |
198 | |
|
|
199 | $proxy |
|
|
200 | } |
|
|
201 | |
|
|
202 | sub new { |
|
|
203 | my ($self, $class, %args) = @_; |
|
|
204 | |
|
|
205 | my $proxy = $self->alloc; |
|
|
206 | |
|
|
207 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
|
|
208 | Scalar::Util::weaken ($proxy->{ws} = $self); |
|
|
209 | |
180 | for my $ev (grep /^on_/, keys %args) { |
210 | for my $ev (grep /^on_/, keys %args) { |
181 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
211 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
182 | $args{$ev} = 0; |
|
|
183 | } |
212 | } |
184 | |
213 | |
185 | $self->msg (ws_c => |
214 | $self->msg (ws_c => |
|
|
215 | $self->{id}, |
186 | $proxy->{id}, |
216 | $proxy->{id}, |
187 | $id, |
|
|
188 | $class, |
217 | $class, |
189 | \%args, |
218 | \%args, |
190 | ); |
219 | ); |
191 | |
220 | |
192 | $proxy |
221 | $proxy |
193 | } |
222 | } |
194 | |
223 | |
|
|
224 | sub template { |
|
|
225 | my ($self, $template, @args) = @_; |
|
|
226 | |
|
|
227 | my %cfg; |
|
|
228 | my @res; |
|
|
229 | |
|
|
230 | while (@args) { |
|
|
231 | my ($name, $args) = splice @args, 0, 2, (); |
|
|
232 | |
|
|
233 | my $proxy = $self->alloc; |
|
|
234 | |
|
|
235 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
|
|
236 | Scalar::Util::weaken ($proxy->{ws} = $self); |
|
|
237 | |
|
|
238 | for my $ev (grep /^on_/, keys %$args) { |
|
|
239 | $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); |
|
|
240 | } |
|
|
241 | |
|
|
242 | $cfg{$name} = { |
|
|
243 | %$args, |
|
|
244 | id => $proxy->{id}, |
|
|
245 | }; |
|
|
246 | |
|
|
247 | push @res, $proxy; |
|
|
248 | } |
|
|
249 | |
|
|
250 | $self->msg (ws_ct => |
|
|
251 | $self->{id}, |
|
|
252 | $template, |
|
|
253 | \%cfg, |
|
|
254 | ); |
|
|
255 | |
|
|
256 | @res |
|
|
257 | } |
|
|
258 | |
|
|
259 | sub find { |
|
|
260 | my ($self, @names) = @_; |
|
|
261 | |
|
|
262 | my @res; |
|
|
263 | my @alloc; |
|
|
264 | |
|
|
265 | for my $name (@names) { |
|
|
266 | push @res, $self->{ns}{widget_wkw}{$name} ||= do { |
|
|
267 | my $proxy = $self->alloc; |
|
|
268 | |
|
|
269 | push @alloc, $proxy->{id} => $name; |
|
|
270 | |
|
|
271 | $proxy |
|
|
272 | }; |
|
|
273 | } |
|
|
274 | |
|
|
275 | $self->msg (ws_a => @alloc) |
|
|
276 | if @alloc; |
|
|
277 | |
|
|
278 | wantarray ? @res : $res[0] |
|
|
279 | } |
|
|
280 | |
195 | ############################################################################# |
281 | ############################################################################# |
196 | |
282 | |
197 | package ext::widget::proxy; |
283 | package ext::widget::proxy; |
198 | |
284 | |
199 | sub DESTROY { |
285 | sub DESTROY { |
… | |
… | |
205 | $self->msg (w_c => 0, "destroy"); |
291 | $self->msg (w_c => 0, "destroy"); |
206 | delete $ws->{_w}{$self->{id}}; |
292 | delete $ws->{_w}{$self->{id}}; |
207 | } |
293 | } |
208 | } |
294 | } |
209 | |
295 | |
|
|
296 | sub cb { |
|
|
297 | my ($self, $cb) = @_; |
|
|
298 | |
|
|
299 | my $proxy = bless { |
|
|
300 | ns => $self->{ns}, |
|
|
301 | id => $self->{ns}->alloc_wid, |
|
|
302 | }, "ext::widget::callback"; |
|
|
303 | |
|
|
304 | Scalar::Util::weaken $proxy->{ns}; |
|
|
305 | |
|
|
306 | $self->{ns}{widget_cb}{$proxy->{id}} = $cb; |
|
|
307 | |
|
|
308 | $proxy |
|
|
309 | } |
|
|
310 | |
|
|
311 | sub oneshot_cb { |
|
|
312 | my ($self, $cb) = @_; |
|
|
313 | |
|
|
314 | if ("CODE" eq ref $cb) { |
|
|
315 | my $ocb = $cb; |
|
|
316 | $cb = $self->cb (sub { |
|
|
317 | undef $cb; |
|
|
318 | &$ocb |
|
|
319 | }); |
|
|
320 | } |
|
|
321 | |
|
|
322 | $cb |
|
|
323 | } |
|
|
324 | |
210 | sub msg { |
325 | sub msg { |
211 | my ($self, $type, @arg) = @_; |
326 | my ($self, $type, @arg) = @_; |
212 | |
327 | |
213 | if (my $ws = $self->{ws}) { |
328 | if (my $ns = $self->{ns}) { |
214 | $ws->msg ($type, $self->{id}, @arg); |
329 | my @msg = ($type, $self->{id}, @arg); |
|
|
330 | warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
|
|
331 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
215 | } |
332 | } |
216 | } |
333 | } |
217 | |
334 | |
218 | sub msg_cb { |
335 | sub msg_cb { |
219 | my ($self, $cb, $type, @arg) = @_; |
336 | my ($self, $cb, $type, @arg) = @_; |
220 | |
337 | |
221 | if (my $ws = $self->{ws}) { |
338 | if (my $ws = $self->{ws}) { |
222 | my $rid = ++$ws->{ns}{id}; |
339 | my $rid = $ws->{ns}->alloc_wid; |
223 | |
|
|
224 | $self->msg ($type, $rid, @arg); |
|
|
225 | |
340 | |
226 | if ($cb) { |
341 | if ($cb) { |
227 | $ws->{ns}{widget_return}{$rid} = $cb; |
342 | $ws->{ns}{widget_cb}{$rid} = sub { |
|
|
343 | delete $ws->{ns}{widget_cb}{$rid}; |
|
|
344 | $ws->{ns}->free_wid ($rid); |
|
|
345 | &$cb |
|
|
346 | }; |
|
|
347 | |
|
|
348 | $self->msg ($type, $rid, @arg); |
228 | } else { |
349 | } else { |
229 | # synchronous case |
350 | # synchronous case |
230 | my $wait = new Coro::Signal; |
351 | my $wait = new Coro::Signal; |
231 | my @res; |
352 | my @res; |
232 | |
353 | |
233 | $ws->{ns}{widget_return}{$rid} = sub { |
354 | $ws->{ns}{widget_cb}{$rid} = sub { |
|
|
355 | delete $ws->{ns}{widget_cb}{$rid}; |
|
|
356 | $ws->{ns}->free_wid ($rid); |
|
|
357 | |
234 | @res = @_; |
358 | @res = @_; |
235 | $wait->send; |
359 | $wait->send; |
236 | }; |
360 | }; |
|
|
361 | $self->msg ($type, $rid, @arg); |
237 | $wait->wait; |
362 | $wait->wait; |
238 | |
363 | |
239 | return @res; |
364 | return @res; |
240 | } |
365 | } |
241 | } |
366 | } |
… | |
… | |
250 | } |
375 | } |
251 | |
376 | |
252 | sub get { |
377 | sub get { |
253 | my ($self, $member, $cb) = @_; |
378 | my ($self, $member, $cb) = @_; |
254 | |
379 | |
255 | $self->msg_cb ($cb, w_g => [$member]); |
380 | $self->msg_cb ($cb, w_g => ref $member ? @$member : $member); |
256 | } |
381 | } |
257 | |
382 | |
258 | sub TO_JSON { |
383 | sub TO_JSON { |
259 | { __widget_ref__ => $_[0]{id} } |
384 | { "\fw" => $_[0]{id} } |
260 | } |
385 | } |
261 | |
386 | |
262 | our $AUTOLOAD; |
387 | our $AUTOLOAD; |
263 | |
388 | |
264 | sub AUTOLOAD { |
389 | sub AUTOLOAD { |
265 | $AUTOLOAD =~ s/^.*:// |
390 | $AUTOLOAD =~ s/^.*:// |
266 | or return; |
391 | or return; |
267 | |
392 | |
268 | my $self = shift; |
393 | my $self = shift; |
269 | |
394 | |
|
|
395 | #TODO: handle non-void context |
270 | $self->msg (w_c => 0, $AUTOLOAD, \@_); |
396 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
271 | |
397 | |
272 | () |
398 | () |
273 | } |
399 | } |
274 | |
400 | |
|
|
401 | package ext::widget::callback; |
|
|
402 | |
|
|
403 | sub DESTROY { |
|
|
404 | my ($self) = @_; |
|
|
405 | |
|
|
406 | if (my $ns = $self->{ns}) { |
|
|
407 | delete $ns->{widget_cb}{$self->{id}}; |
|
|
408 | $ns->free_wid ($self->{id}); |
|
|
409 | } |
|
|
410 | } |
|
|
411 | |
|
|
412 | sub TO_JSON { |
|
|
413 | { "\fc" => $_[0]{id} } |
|
|
414 | } |
|
|
415 | |