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 id # widgetset new |
5 | # ws_n ws # widgetset new |
5 | # ws_d id # 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 name value # widget member set |
9 | # w_s id @attr # widget member set |
9 | # w_g id rid name # 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 | |
|
|
15 | our $DEBUG = 1; |
14 | |
16 | |
15 | cf::client->attach ( |
17 | cf::client->attach ( |
16 | on_connect => sub { |
18 | on_connect => sub { |
17 | my ($ns) = @_; |
19 | my ($ns) = @_; |
18 | |
20 | |
|
|
21 | Scalar::Util::weaken (my $weakns = $ns); |
|
|
22 | |
19 | $ns->{id} = "a"; |
23 | $ns->{id} = "a"; |
|
|
24 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
|
|
25 | $weakns->{widget}{$_[0]} |
|
|
26 | }); |
20 | }, |
27 | }, |
21 | ); |
28 | ); |
22 | |
29 | |
23 | sub csc_update_stats { |
30 | sub csc_update_stats { |
24 | my ($ns) = @_; |
31 | my ($ns) = @_; |
25 | |
32 | |
26 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
33 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
27 | $v->set_text ($ns->pl->ob->stats->$k); |
34 | $v->set_text ($ns->pl->ob->stats->$k); |
28 | } |
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; |
29 | } |
47 | } |
30 | |
48 | |
31 | sub csc_start { |
49 | sub csc_start { |
32 | my ($ns) = @_; |
50 | my ($ns) = @_; |
33 | |
51 | |
… | |
… | |
45 | }, |
63 | }, |
46 | ); |
64 | ); |
47 | |
65 | |
48 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
66 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
49 | |
67 | |
50 | $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"); |
51 | |
69 | |
52 | $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); |
70 | $stats->add_at (0, 0, (my $statstable = $ws->new ("Table"))); |
53 | |
71 | |
54 | for ( |
72 | for ( |
55 | [0, "Str"], |
73 | [0, "Str"], |
56 | [1, "Dex"], |
74 | [1, "Dex"], |
57 | [2, "Con"], |
75 | [2, "Con"], |
… | |
… | |
60 | [5, "Pow"], |
78 | [5, "Pow"], |
61 | [6, "Cha"], |
79 | [6, "Cha"], |
62 | ) { |
80 | ) { |
63 | my ($x, $label) = @$_; |
81 | my ($x, $label) = @$_; |
64 | |
82 | |
65 | $statstable->add ($x, 0, $ws->new (Label => |
83 | $statstable->add_at ($x, 0, $ws->new (Label => |
66 | can_hover => 1, can_events => 1, |
84 | can_hover => 1, can_events => 1, |
67 | align => +1, text => $label, tooltip => "#stat_$label", |
85 | align => +1, text => $label, tooltip => "#stat_$label", |
68 | )); |
86 | )); |
69 | $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
87 | $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
70 | can_hover => 1, can_events => 1, |
88 | can_hover => 1, can_events => 1, |
71 | align => +1, template => "88", tooltip => "#stat_$label", |
89 | align => +1, template => "88", tooltip => "#stat_$label", |
72 | )); |
90 | )); |
73 | } |
91 | } |
74 | |
92 | |
75 | csc_update_stats $ns; |
93 | csc_update_stats $ns; |
76 | |
94 | |
|
|
95 | $ws->{tl} = $w; |
77 | $w->show; |
96 | $w->show; |
78 | } |
97 | } |
79 | |
98 | |
80 | cf::player->attach ( |
99 | cf::player->attach ( |
81 | on_login => sub { |
100 | on_login => sub { |
… | |
… | |
84 | return unless $cf::CFG{devel}; |
103 | return unless $cf::CFG{devel}; |
85 | |
104 | |
86 | my $ns = $pl->ns; |
105 | my $ns = $pl->ns; |
87 | |
106 | |
88 | return unless $ns->{can_widget}; |
107 | return unless $ns->{can_widget}; |
89 | |
|
|
90 | csc_start $ns; |
108 | #csc_start $ns; |
|
|
109 | #demo_start $ns; |
91 | }, |
110 | }, |
92 | ); |
111 | ); |
93 | |
112 | |
94 | cf::register_exticmd w_e => sub { |
113 | cf::register_exticmd w_e => sub { |
95 | my ($ns, $pkt) = @_; |
114 | my ($ns, $id, @args) = @_; |
96 | |
115 | |
97 | if (my $w = $ns->{widget}{$pkt->{id}}) { |
116 | if (my $cb = $ns->{widget_cb}{$id}) { |
98 | if (my $cb = $w->{ev}{$pkt->{name}}) { |
117 | $cb->(@args); |
99 | $_->($w, @{ $pkt->{args} || [] }) |
|
|
100 | for @$cb; |
|
|
101 | } |
|
|
102 | } |
|
|
103 | |
|
|
104 | () |
|
|
105 | }; |
|
|
106 | |
|
|
107 | cf::register_exticmd w_r => sub { |
|
|
108 | my ($ns, $pkt) = @_; |
|
|
109 | |
|
|
110 | if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { |
|
|
111 | $cb->(@{$pkt->{res} || [] }); |
|
|
112 | } |
118 | } |
113 | |
119 | |
114 | () |
120 | () |
115 | }; |
121 | }; |
116 | |
122 | |
… | |
… | |
120 | my $id = ++$self->{id}; |
126 | my $id = ++$self->{id}; |
121 | |
127 | |
122 | my $ws = bless { |
128 | my $ws = bless { |
123 | id => $id, |
129 | id => $id, |
124 | ns => $self, |
130 | ns => $self, |
125 | w => {}, |
131 | _w => {}, |
126 | }, "ext::widget::set"; |
132 | }, "ext::widget::set"; |
127 | |
133 | |
128 | $ws->msg (ws_n => id => $id); |
134 | $ws->msg (ws_n => $id); |
129 | |
135 | |
130 | $ws |
136 | $ws |
|
|
137 | } |
|
|
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]; |
131 | } |
146 | } |
132 | |
147 | |
133 | ############################################################################# |
148 | ############################################################################# |
134 | |
149 | |
135 | package ext::widget::set; |
150 | package ext::widget::set; |
… | |
… | |
139 | } |
154 | } |
140 | |
155 | |
141 | sub destroy { |
156 | sub destroy { |
142 | my ($self) = @_; |
157 | my ($self) = @_; |
143 | |
158 | |
144 | $self->msg (ws_d => id => $self->{id}); |
159 | $self->msg (ws_d => $self->{id}); |
145 | delete $self->{ns}; |
160 | delete $self->{ns}; |
146 | $_->destroy |
161 | $_->destroy |
147 | for values %{ $self->{w} }; |
162 | for values %{ $self->{w} }; |
148 | } |
163 | } |
149 | |
164 | |
150 | sub msg { |
165 | sub msg { |
151 | my ($self, $type, %msg) = @_; |
166 | my ($self, @msg) = @_; |
152 | |
167 | |
153 | if (my $ns = shift->{ns}) { |
168 | if (my $ns = shift->{ns}) { |
154 | $msg{msgtype} = $type; |
169 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
155 | $ns->send_packet ("ext " . cf::to_json \%msg); |
170 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
156 | } |
171 | } |
|
|
172 | } |
|
|
173 | |
|
|
174 | sub alloc { |
|
|
175 | my ($self) = @_; |
|
|
176 | |
|
|
177 | my $id = $self->{ns}->alloc_wid; |
|
|
178 | |
|
|
179 | my $proxy = bless { |
|
|
180 | id => $id, |
|
|
181 | }, "ext::widget::proxy"; |
|
|
182 | |
|
|
183 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
|
|
184 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
|
|
185 | |
|
|
186 | $proxy |
157 | } |
187 | } |
158 | |
188 | |
159 | sub new { |
189 | sub new { |
160 | my ($self, $class, %args) = @_; |
190 | my ($self, $class, %args) = @_; |
161 | |
191 | |
162 | my $id = ++$self->{ns}{id}; |
192 | my $proxy = $self->alloc; |
163 | |
193 | |
164 | my $proxy = $self->{w}{$id} = bless { |
194 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
165 | id => $id, |
|
|
166 | }, "ext::widget::proxy"; |
|
|
167 | |
|
|
168 | Scalar::Util::weaken ($proxy->{ws} = $self); |
195 | Scalar::Util::weaken ($proxy->{ws} = $self); |
169 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
|
|
170 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
|
|
171 | |
196 | |
172 | for my $ev (grep /^on_/, keys %args) { |
197 | for my $ev (grep /^on_/, keys %args) { |
173 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
198 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
174 | $args{$ev} = 0; |
|
|
175 | } |
199 | } |
176 | |
200 | |
177 | $self->msg (ws_c => |
201 | $self->msg (ws_c => |
178 | ws => $self->{w}{id}, |
202 | $self->{id}, |
179 | id => $id, |
203 | $proxy->{id}, |
180 | class => $class, |
204 | $class, |
181 | args => \%args, |
205 | \%args, |
182 | ); |
206 | ); |
183 | |
207 | |
184 | $proxy |
208 | $proxy |
185 | } |
209 | } |
186 | |
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 | |
187 | ############################################################################# |
233 | ############################################################################# |
188 | |
234 | |
189 | package ext::widget::proxy; |
235 | package ext::widget::proxy; |
190 | |
236 | |
191 | sub DESTROY { |
237 | sub DESTROY { |
192 | my ($self) = @_; |
238 | my ($self) = @_; |
193 | |
239 | |
194 | delete $self->{ns}{widget}{$self->{id}}; |
240 | delete $self->{ns}{widget}{$self->{id}}; |
195 | |
241 | |
196 | if (my $ws = $self->{ws}) { |
242 | if (my $ws = $self->{ws}) { |
|
|
243 | $self->msg (w_c => 0, "destroy"); |
197 | delete $ws->{w}{$self->{id}}; |
244 | delete $ws->{_w}{$self->{id}}; |
198 | $self->msg (w_c => name => "destroy"); |
|
|
199 | } |
245 | } |
|
|
246 | } |
|
|
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 |
200 | } |
275 | } |
201 | |
276 | |
202 | sub msg { |
277 | sub msg { |
203 | my ($self, $type, %msg) = @_; |
278 | my ($self, $type, @arg) = @_; |
|
|
279 | |
|
|
280 | if (my $ns = $self->{ns}) { |
|
|
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)); |
|
|
284 | } |
|
|
285 | } |
|
|
286 | |
|
|
287 | sub msg_cb { |
|
|
288 | my ($self, $cb, $type, @arg) = @_; |
204 | |
289 | |
205 | if (my $ws = $self->{ws}) { |
290 | if (my $ws = $self->{ws}) { |
206 | $ws->msg ($type, |
|
|
207 | %msg, |
|
|
208 | id => $self->{id}, |
|
|
209 | ); |
|
|
210 | } |
|
|
211 | } |
|
|
212 | |
|
|
213 | sub msg_cb { |
|
|
214 | my ($self, $cb, $type, %msg) = @_; |
|
|
215 | |
|
|
216 | if (my $ws = $self->{ws}) { |
|
|
217 | |
|
|
218 | my $rid = ++$ws->{ns}{id}; |
291 | my $rid = $ws->{ns}->alloc_wid; |
219 | |
|
|
220 | $self->msg ($type, %msg, rid => $rid); |
|
|
221 | |
292 | |
222 | if ($cb) { |
293 | if ($cb) { |
223 | $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); |
224 | } else { |
301 | } else { |
225 | # synchronous case |
302 | # synchronous case |
226 | my $wait = new Coro::Signal; |
303 | my $wait = new Coro::Signal; |
227 | my @res; |
304 | my @res; |
228 | |
305 | |
229 | $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 | |
230 | @res = @_; |
310 | @res = @_; |
231 | $wait->send; |
311 | $wait->send; |
232 | }; |
312 | }; |
|
|
313 | $self->msg ($type, $rid, @arg); |
233 | $wait->wait; |
314 | $wait->wait; |
234 | |
315 | |
235 | return @res; |
316 | return @res; |
236 | } |
317 | } |
237 | } |
318 | } |
238 | |
319 | |
239 | () |
320 | () |
240 | } |
321 | } |
241 | |
322 | |
242 | sub set { |
323 | sub set { |
243 | my ($self, $member, $value) = @_; |
324 | my ($self, @kv) = @_; |
244 | |
325 | |
245 | $self->msg (w_s => name => $member, value => $value); |
326 | $self->msg (w_s => \@kv); |
246 | } |
327 | } |
247 | |
328 | |
248 | sub get { |
329 | sub get { |
249 | my ($self, $member, $cb) = @_; |
330 | my ($self, $member, $cb) = @_; |
250 | |
331 | |
251 | $self->msg_cb ($cb, w_g => name => $member); |
332 | $self->msg_cb ($cb, w_g => ref $member ? @$member : $member); |
252 | } |
333 | } |
253 | |
334 | |
254 | sub TO_JSON { |
335 | sub TO_JSON { |
255 | { __widget_ref__ => $_[0]{id} } |
336 | { "\fw" => $_[0]{id} } |
256 | } |
337 | } |
257 | |
338 | |
258 | our $AUTOLOAD; |
339 | our $AUTOLOAD; |
259 | |
340 | |
260 | sub AUTOLOAD { |
341 | sub AUTOLOAD { |
261 | $AUTOLOAD =~ s/^.*:// |
342 | $AUTOLOAD =~ s/^.*:// |
262 | or return; |
343 | or return; |
263 | |
344 | |
264 | my $self = shift; |
345 | my $self = shift; |
265 | |
346 | |
|
|
347 | #TODO: handle non-void context |
266 | $self->msg (w_c => name => $AUTOLOAD, args => \@_); |
348 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
267 | |
349 | |
268 | () |
350 | () |
269 | } |
351 | } |
270 | |
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 | |