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 |
7 | # w_c id rid name args # widget method call |
8 | # w_c id rid name @args # widget method call |
8 | # w_s id @attr # widget member set |
9 | # w_s id @attr # widget member set |
9 | # w_g id rid @attr # widget member get |
10 | # w_g id rid @attr # widget member get |
10 | # |
11 | # |
11 | # and expects the following exti message types |
12 | # and expects the following exti message types |
12 | # w_r rid res # widget call return |
|
|
13 | # w_e id name args # widget_event |
13 | # w_e id @args # widget_call |
14 | |
14 | |
15 | our $DEBUG = 1; |
15 | our $DEBUG = 1; |
16 | |
16 | |
17 | cf::client->attach ( |
17 | cf::client->attach ( |
18 | on_connect => sub { |
18 | on_connect => sub { |
19 | my ($ns) = @_; |
19 | my ($ns) = @_; |
20 | |
20 | |
21 | Scalar::Util::weaken (my $weakns = $ns); |
21 | Scalar::Util::weaken (my $weakns = $ns); |
22 | |
22 | |
23 | $ns->{id} = "a"; |
23 | $ns->{id} = "a"; |
24 | $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { |
24 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
25 | # cannot deserialise ATM |
25 | $weakns->{widget}{$_[0]} |
26 | undef |
|
|
27 | }); |
26 | }); |
28 | }, |
27 | }, |
29 | ); |
28 | ); |
30 | |
29 | |
31 | sub csc_update_stats { |
30 | sub csc_update_stats { |
32 | my ($ns) = @_; |
31 | my ($ns) = @_; |
33 | |
32 | |
34 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
33 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
35 | $v->set_text ($ns->pl->ob->stats->$k); |
34 | $v->set_text ($ns->pl->ob->stats->$k); |
36 | } |
35 | } |
|
|
36 | } |
|
|
37 | |
|
|
38 | sub demo_start { |
|
|
39 | my ($ns) = @_; |
|
|
40 | |
|
|
41 | my $ws = $ns->{csc} = $ns->new_widgetset; |
|
|
42 | |
|
|
43 | $ws->{tab} = $ws->new (Label => text => "dumb tst", c_tab => ["hull"]); |
|
|
44 | |
|
|
45 | $ws->find ("setup_notebook")->add ($ws->{tab}); |
|
|
46 | $ws->find ("setup_dialog")->toggle_visibility; |
37 | } |
47 | } |
38 | |
48 | |
39 | sub csc_start { |
49 | sub csc_start { |
40 | my ($ns) = @_; |
50 | my ($ns) = @_; |
41 | |
51 | |
… | |
… | |
53 | }, |
63 | }, |
54 | ); |
64 | ); |
55 | |
65 | |
56 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
66 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
57 | |
67 | |
58 | $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); |
68 | $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); |
59 | |
69 | |
60 | $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); |
70 | $stats->add_at (0, 0, (my $statstable = $ws->new ("Table"))); |
61 | |
71 | |
62 | for ( |
72 | for ( |
63 | [0, "Str"], |
73 | [0, "Str"], |
64 | [1, "Dex"], |
74 | [1, "Dex"], |
65 | [2, "Con"], |
75 | [2, "Con"], |
… | |
… | |
80 | )); |
90 | )); |
81 | } |
91 | } |
82 | |
92 | |
83 | csc_update_stats $ns; |
93 | csc_update_stats $ns; |
84 | |
94 | |
|
|
95 | $ws->{tl} = $w; |
85 | $w->show; |
96 | $w->show; |
86 | } |
97 | } |
87 | |
98 | |
88 | cf::player->attach ( |
99 | cf::player->attach ( |
89 | on_login => sub { |
100 | on_login => sub { |
… | |
… | |
93 | |
104 | |
94 | my $ns = $pl->ns; |
105 | my $ns = $pl->ns; |
95 | |
106 | |
96 | return unless $ns->{can_widget}; |
107 | return unless $ns->{can_widget}; |
97 | #csc_start $ns; |
108 | #csc_start $ns; |
|
|
109 | #demo_start $ns; |
98 | }, |
110 | }, |
99 | ); |
111 | ); |
100 | |
112 | |
101 | cf::register_exticmd w_e => sub { |
113 | cf::register_exticmd w_e => sub { |
102 | my ($ns, $id, $name, $args) = @_; |
114 | my ($ns, $id, @args) = @_; |
103 | |
115 | |
104 | if (my $w = $ns->{widget}{$id}) { |
116 | 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); |
117 | $cb->(@args); |
119 | } |
118 | } |
120 | |
119 | |
121 | () |
120 | () |
122 | }; |
121 | }; |
123 | |
122 | |
… | |
… | |
135 | $ws->msg (ws_n => $id); |
134 | $ws->msg (ws_n => $id); |
136 | |
135 | |
137 | $ws |
136 | $ws |
138 | } |
137 | } |
139 | |
138 | |
|
|
139 | sub cf::client::alloc_wid { |
|
|
140 | pop @{ $_[0]{ids} } |
|
|
141 | or ++$_[0]{id} |
|
|
142 | } |
|
|
143 | |
|
|
144 | sub cf::client::free_wid { |
|
|
145 | push @{ $_[0]{ids} }, $_[1]; |
|
|
146 | } |
|
|
147 | |
140 | ############################################################################# |
148 | ############################################################################# |
141 | |
149 | |
142 | package ext::widget::set; |
150 | package ext::widget::set; |
143 | |
151 | |
144 | sub DESTROY { |
152 | sub DESTROY { |
… | |
… | |
161 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
169 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
162 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
170 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
163 | } |
171 | } |
164 | } |
172 | } |
165 | |
173 | |
166 | sub new { |
174 | sub alloc { |
167 | my ($self, $class, %args) = @_; |
175 | my ($self) = @_; |
168 | |
176 | |
169 | my $id = ++$self->{ns}{id}; |
177 | my $id = $self->{ns}->alloc_wid; |
170 | |
178 | |
171 | my $proxy = bless { |
179 | my $proxy = bless { |
172 | id => $id, |
180 | id => $id, |
173 | }, "ext::widget::proxy"; |
181 | }, "ext::widget::proxy"; |
174 | |
182 | |
175 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
|
|
176 | Scalar::Util::weaken ($proxy->{ws} = $self); |
|
|
177 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
183 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
178 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
184 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
179 | |
185 | |
|
|
186 | $proxy |
|
|
187 | } |
|
|
188 | |
|
|
189 | sub new { |
|
|
190 | my ($self, $class, %args) = @_; |
|
|
191 | |
|
|
192 | my $proxy = $self->alloc; |
|
|
193 | |
|
|
194 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
|
|
195 | Scalar::Util::weaken ($proxy->{ws} = $self); |
|
|
196 | |
180 | for my $ev (grep /^on_/, keys %args) { |
197 | for my $ev (grep /^on_/, keys %args) { |
181 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
198 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
182 | $args{$ev} = 0; |
|
|
183 | } |
199 | } |
184 | |
200 | |
185 | $self->msg (ws_c => |
201 | $self->msg (ws_c => |
|
|
202 | $self->{id}, |
186 | $proxy->{id}, |
203 | $proxy->{id}, |
187 | $id, |
|
|
188 | $class, |
204 | $class, |
189 | \%args, |
205 | \%args, |
190 | ); |
206 | ); |
191 | |
207 | |
192 | $proxy |
208 | $proxy |
193 | } |
209 | } |
194 | |
210 | |
|
|
211 | sub find { |
|
|
212 | my ($self, @names) = @_; |
|
|
213 | |
|
|
214 | my @res; |
|
|
215 | my @alloc; |
|
|
216 | |
|
|
217 | for my $name (@names) { |
|
|
218 | push @res, $self->{ns}{widget_wkw}{$name} ||= do { |
|
|
219 | my $proxy = $self->alloc; |
|
|
220 | |
|
|
221 | push @alloc, $proxy->{id} => $name; |
|
|
222 | |
|
|
223 | $proxy |
|
|
224 | }; |
|
|
225 | } |
|
|
226 | |
|
|
227 | $self->msg (ws_a => @alloc) |
|
|
228 | if @alloc; |
|
|
229 | |
|
|
230 | wantarray ? @res : $res[0] |
|
|
231 | } |
|
|
232 | |
195 | ############################################################################# |
233 | ############################################################################# |
196 | |
234 | |
197 | package ext::widget::proxy; |
235 | package ext::widget::proxy; |
198 | |
236 | |
199 | sub DESTROY { |
237 | sub DESTROY { |
… | |
… | |
205 | $self->msg (w_c => 0, "destroy"); |
243 | $self->msg (w_c => 0, "destroy"); |
206 | delete $ws->{_w}{$self->{id}}; |
244 | delete $ws->{_w}{$self->{id}}; |
207 | } |
245 | } |
208 | } |
246 | } |
209 | |
247 | |
|
|
248 | sub cb { |
|
|
249 | my ($self, $cb) = @_; |
|
|
250 | |
|
|
251 | my $proxy = bless { |
|
|
252 | ns => $self->{ns}, |
|
|
253 | id => $self->{ns}->alloc_wid, |
|
|
254 | }, "ext::widget::callback"; |
|
|
255 | |
|
|
256 | Scalar::Util::weaken $proxy->{ns}; |
|
|
257 | |
|
|
258 | $self->{ns}{widget_cb}{$proxy->{id}} = $cb; |
|
|
259 | |
|
|
260 | $proxy |
|
|
261 | } |
|
|
262 | |
|
|
263 | sub oneshot_cb { |
|
|
264 | my ($self, $cb) = @_; |
|
|
265 | |
|
|
266 | if ("CODE" eq ref $cb) { |
|
|
267 | my $ocb = $cb; |
|
|
268 | $cb = $self->cb (sub { |
|
|
269 | undef $cb; |
|
|
270 | &$ocb |
|
|
271 | }); |
|
|
272 | } |
|
|
273 | |
|
|
274 | $cb |
|
|
275 | } |
|
|
276 | |
210 | sub msg { |
277 | sub msg { |
211 | my ($self, $type, @arg) = @_; |
278 | my ($self, $type, @arg) = @_; |
212 | |
279 | |
213 | if (my $ws = $self->{ws}) { |
280 | if (my $ns = $self->{ns}) { |
214 | $ws->msg ($type, $self->{id}, @arg); |
281 | my @msg = ($type, $self->{id}, @arg); |
|
|
282 | warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
|
|
283 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
215 | } |
284 | } |
216 | } |
285 | } |
217 | |
286 | |
218 | sub msg_cb { |
287 | sub msg_cb { |
219 | my ($self, $cb, $type, @arg) = @_; |
288 | my ($self, $cb, $type, @arg) = @_; |
220 | |
289 | |
221 | if (my $ws = $self->{ws}) { |
290 | if (my $ws = $self->{ws}) { |
222 | my $rid = ++$ws->{ns}{id}; |
291 | my $rid = $ws->{ns}->alloc_wid; |
223 | |
|
|
224 | $self->msg ($type, $rid, @arg); |
|
|
225 | |
292 | |
226 | if ($cb) { |
293 | if ($cb) { |
227 | $ws->{ns}{widget_return}{$rid} = $cb; |
294 | $ws->{ns}{widget_cb}{$rid} = sub { |
|
|
295 | delete $ws->{ns}{widget_cb}{$rid}; |
|
|
296 | $ws->{ns}->free_wid ($rid); |
|
|
297 | &$cb |
|
|
298 | }; |
|
|
299 | |
|
|
300 | $self->msg ($type, $rid, @arg); |
228 | } else { |
301 | } else { |
229 | # synchronous case |
302 | # synchronous case |
230 | my $wait = new Coro::Signal; |
303 | my $wait = new Coro::Signal; |
231 | my @res; |
304 | my @res; |
232 | |
305 | |
233 | $ws->{ns}{widget_return}{$rid} = sub { |
306 | $ws->{ns}{widget_cb}{$rid} = sub { |
|
|
307 | delete $ws->{ns}{widget_cb}{$rid}; |
|
|
308 | $ws->{ns}->free_wid ($rid); |
|
|
309 | |
234 | @res = @_; |
310 | @res = @_; |
235 | $wait->send; |
311 | $wait->send; |
236 | }; |
312 | }; |
|
|
313 | $self->msg ($type, $rid, @arg); |
237 | $wait->wait; |
314 | $wait->wait; |
238 | |
315 | |
239 | return @res; |
316 | return @res; |
240 | } |
317 | } |
241 | } |
318 | } |
… | |
… | |
250 | } |
327 | } |
251 | |
328 | |
252 | sub get { |
329 | sub get { |
253 | my ($self, $member, $cb) = @_; |
330 | my ($self, $member, $cb) = @_; |
254 | |
331 | |
255 | $self->msg_cb ($cb, w_g => [$member]); |
332 | $self->msg_cb ($cb, w_g => ref $member ? @$member : $member); |
256 | } |
333 | } |
257 | |
334 | |
258 | sub TO_JSON { |
335 | sub TO_JSON { |
259 | { __widget_ref__ => $_[0]{id} } |
336 | { "\fw" => $_[0]{id} } |
260 | } |
337 | } |
261 | |
338 | |
262 | our $AUTOLOAD; |
339 | our $AUTOLOAD; |
263 | |
340 | |
264 | sub AUTOLOAD { |
341 | sub AUTOLOAD { |
265 | $AUTOLOAD =~ s/^.*:// |
342 | $AUTOLOAD =~ s/^.*:// |
266 | or return; |
343 | or return; |
267 | |
344 | |
268 | my $self = shift; |
345 | my $self = shift; |
269 | |
346 | |
|
|
347 | #TODO: handle non-void context |
270 | $self->msg (w_c => 0, $AUTOLOAD, \@_); |
348 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
271 | |
349 | |
272 | () |
350 | () |
273 | } |
351 | } |
274 | |
352 | |
|
|
353 | package ext::widget::callback; |
|
|
354 | |
|
|
355 | sub DESTROY { |
|
|
356 | my ($self) = @_; |
|
|
357 | |
|
|
358 | if (my $ns = $self->{ns}) { |
|
|
359 | delete $ns->{widget_cb}{$self->{id}}; |
|
|
360 | $ns->free_wid ($self->{id}); |
|
|
361 | } |
|
|
362 | } |
|
|
363 | |
|
|
364 | sub TO_JSON { |
|
|
365 | { "\fc" => $_[0]{id} } |
|
|
366 | } |
|
|
367 | |