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 |
|
|
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 name value # widget member set |
10 | # w_s id @attr # widget member set |
9 | # w_g id rid name # 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 |
|
|
15 | |
|
|
16 | our $DEBUG = 1; |
14 | |
17 | |
15 | cf::client->attach ( |
18 | cf::client->attach ( |
16 | on_connect => sub { |
19 | on_connect => sub { |
17 | my ($ns) = @_; |
20 | my ($ns) = @_; |
18 | |
21 | |
|
|
22 | Scalar::Util::weaken (my $weakns = $ns); |
|
|
23 | |
19 | $ns->{id} = "a"; |
24 | $ns->{id} = "a"; |
|
|
25 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
|
|
26 | $weakns->{widget}{$_[0]} |
|
|
27 | }); |
20 | }, |
28 | }, |
21 | ); |
29 | ); |
22 | |
30 | |
23 | sub csc_update_stats { |
31 | sub csc_update_stats { |
24 | my ($ns) = @_; |
32 | my ($ns) = @_; |
25 | |
33 | |
26 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
34 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
27 | $v->set_text ($ns->pl->ob->stats->$k); |
35 | $v->set_text ($ns->pl->ob->stats->$k); |
28 | } |
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; |
29 | } |
60 | } |
30 | |
61 | |
31 | sub csc_start { |
62 | sub csc_start { |
32 | my ($ns) = @_; |
63 | my ($ns) = @_; |
33 | |
64 | |
… | |
… | |
37 | min_w => 600, |
68 | min_w => 600, |
38 | min_h => 400, |
69 | min_h => 400, |
39 | x => "center", |
70 | x => "center", |
40 | y => "center", |
71 | y => "center", |
41 | title => "Character Creation", |
72 | title => "Character Creation", |
|
|
73 | has_close_button => 1, |
|
|
74 | on_delete => sub { |
|
|
75 | $ws->destroy; |
|
|
76 | }, |
42 | ); |
77 | ); |
43 | |
78 | |
44 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
79 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
45 | |
80 | |
46 | $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"); |
47 | |
82 | |
48 | $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); |
83 | $stats->add_at (0, 0, (my $statstable = $ws->new ("Table"))); |
49 | |
84 | |
50 | for ( |
85 | for ( |
51 | [0, "Str", "Strength"], |
86 | [0, "Str"], |
52 | [1, "Dex", "Dexterity"], |
87 | [1, "Dex"], |
53 | [2, "Con", "Constitution"], |
88 | [2, "Con"], |
54 | [3, "Int", "Intelligence"], |
89 | [3, "Int"], |
55 | [4, "Wis", "Wisdom"], |
90 | [4, "Wis"], |
56 | [5, "Pow", "Power"], |
91 | [5, "Pow"], |
57 | [6, "Cha", "Charisma"], |
92 | [6, "Cha"], |
58 | ) { |
93 | ) { |
59 | my ($x, $label, $description) = @$_; |
94 | my ($x, $label) = @$_; |
60 | |
95 | |
61 | $statstable->add ($x, 0, $ws->new (Label => |
96 | $statstable->add_at ($x, 0, $ws->new (Label => |
62 | can_hover => 1, can_events => 1, |
97 | can_hover => 1, can_events => 1, |
63 | align => +1, text => $label, tooltip => "#stat_$label", |
98 | align => +1, text => $label, tooltip => "#stat_$label", |
64 | )); |
99 | )); |
65 | $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
100 | $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
66 | can_hover => 1, can_events => 1, |
101 | can_hover => 1, can_events => 1, |
67 | align => +1, template => "88", tooltip => "#stat_$label", |
102 | align => +1, template => "88", tooltip => "#stat_$label", |
68 | )); |
103 | )); |
69 | } |
104 | } |
70 | |
105 | |
71 | csc_update_stats $ns; |
106 | csc_update_stats $ns; |
72 | |
107 | |
|
|
108 | $ws->{tl} = $w; |
73 | $w->show; |
109 | $w->show; |
74 | } |
110 | } |
75 | |
111 | |
76 | cf::player->attach ( |
112 | cf::player->attach ( |
77 | on_login => sub { |
113 | on_login => sub { |
… | |
… | |
80 | return unless $cf::CFG{devel}; |
116 | return unless $cf::CFG{devel}; |
81 | |
117 | |
82 | my $ns = $pl->ns; |
118 | my $ns = $pl->ns; |
83 | |
119 | |
84 | return unless $ns->{can_widget}; |
120 | return unless $ns->{can_widget}; |
85 | |
|
|
86 | csc_start $ns; |
121 | #csc_start $ns; |
|
|
122 | demo_start $ns; |
87 | }, |
123 | }, |
88 | ); |
124 | ); |
89 | |
125 | |
90 | cf::register_exticmd w_e => sub { |
126 | cf::register_exticmd w_e => sub { |
91 | my ($ns, $pkt) = @_; |
127 | my ($ns, $id, @args) = @_; |
92 | |
128 | |
93 | if (my $w = $ns->{widget}{$pkt->{id}}) { |
129 | if (my $cb = $ns->{widget_cb}{$id}) { |
94 | if (my $cb = $w->{ev}{$pkt->{name}}) { |
130 | $cb->(@args); |
95 | $_->($w, @{ $pkt->{args} || [] }) |
|
|
96 | for @$cb; |
|
|
97 | } |
|
|
98 | } |
|
|
99 | |
|
|
100 | () |
|
|
101 | }; |
|
|
102 | |
|
|
103 | cf::register_exticmd w_r => sub { |
|
|
104 | my ($ns, $pkt) = @_; |
|
|
105 | |
|
|
106 | if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { |
|
|
107 | $cb->(@{$pkt->{res} || [] }); |
|
|
108 | } |
131 | } |
109 | |
132 | |
110 | () |
133 | () |
111 | }; |
134 | }; |
112 | |
135 | |
… | |
… | |
116 | my $id = ++$self->{id}; |
139 | my $id = ++$self->{id}; |
117 | |
140 | |
118 | my $ws = bless { |
141 | my $ws = bless { |
119 | id => $id, |
142 | id => $id, |
120 | ns => $self, |
143 | ns => $self, |
121 | w => {}, |
144 | _w => {}, |
122 | }, "ext::widget::set"; |
145 | }, "ext::widget::set"; |
123 | |
146 | |
124 | $ws->msg (ws_n => id => $id); |
147 | $ws->msg (ws_n => $id); |
125 | |
148 | |
126 | $ws |
149 | $ws |
|
|
150 | } |
|
|
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]; |
127 | } |
159 | } |
128 | |
160 | |
129 | ############################################################################# |
161 | ############################################################################# |
130 | |
162 | |
131 | package ext::widget::set; |
163 | package ext::widget::set; |
… | |
… | |
135 | } |
167 | } |
136 | |
168 | |
137 | sub destroy { |
169 | sub destroy { |
138 | my ($self) = @_; |
170 | my ($self) = @_; |
139 | |
171 | |
140 | $self->msg (ws_d => id => $self->{id}); |
172 | $self->msg (ws_d => $self->{id}); |
141 | delete $self->{ns}; |
173 | delete $self->{ns}; |
142 | $_->destroy |
174 | $_->destroy |
143 | for values %{ $self->{w} }; |
175 | for values %{ $self->{w} }; |
144 | } |
176 | } |
145 | |
177 | |
146 | sub msg { |
178 | sub msg { |
147 | my ($self, $type, %msg) = @_; |
179 | my ($self, @msg) = @_; |
148 | |
180 | |
149 | if (my $ns = shift->{ns}) { |
181 | if (my $ns = shift->{ns}) { |
150 | $msg{msgtype} = $type; |
182 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
151 | $ns->send_packet ("ext " . cf::to_json \%msg); |
183 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
152 | } |
184 | } |
|
|
185 | } |
|
|
186 | |
|
|
187 | sub alloc { |
|
|
188 | my ($self) = @_; |
|
|
189 | |
|
|
190 | my $id = $self->{ns}->alloc_wid; |
|
|
191 | |
|
|
192 | my $proxy = bless { |
|
|
193 | id => $id, |
|
|
194 | }, "ext::widget::proxy"; |
|
|
195 | |
|
|
196 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
|
|
197 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
|
|
198 | |
|
|
199 | $proxy |
153 | } |
200 | } |
154 | |
201 | |
155 | sub new { |
202 | sub new { |
156 | my ($self, $class, %args) = @_; |
203 | my ($self, $class, %args) = @_; |
157 | |
204 | |
158 | my $id = ++$self->{ns}{id}; |
205 | my $proxy = $self->alloc; |
159 | |
206 | |
160 | my $proxy = $self->{w}{$id} = bless { |
207 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
161 | id => $id, |
|
|
162 | }, "ext::widget::proxy"; |
|
|
163 | |
|
|
164 | Scalar::Util::weaken ($proxy->{ws} = $self); |
208 | Scalar::Util::weaken ($proxy->{ws} = $self); |
165 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
|
|
166 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
|
|
167 | |
209 | |
168 | for my $ev (grep /^on_/, keys %args) { |
210 | for my $ev (grep /^on_/, keys %args) { |
169 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
211 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
170 | $args{$ev} = 0; |
|
|
171 | } |
212 | } |
172 | |
213 | |
173 | $self->msg (ws_c => |
214 | $self->msg (ws_c => |
174 | ws => $self->{w}{id}, |
215 | $self->{id}, |
175 | id => $id, |
216 | $proxy->{id}, |
176 | class => $class, |
217 | $class, |
177 | args => \%args, |
218 | \%args, |
178 | ); |
219 | ); |
179 | |
220 | |
180 | $proxy |
221 | $proxy |
|
|
222 | } |
|
|
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] |
181 | } |
279 | } |
182 | |
280 | |
183 | ############################################################################# |
281 | ############################################################################# |
184 | |
282 | |
185 | package ext::widget::proxy; |
283 | package ext::widget::proxy; |
… | |
… | |
188 | my ($self) = @_; |
286 | my ($self) = @_; |
189 | |
287 | |
190 | delete $self->{ns}{widget}{$self->{id}}; |
288 | delete $self->{ns}{widget}{$self->{id}}; |
191 | |
289 | |
192 | if (my $ws = $self->{ws}) { |
290 | if (my $ws = $self->{ws}) { |
|
|
291 | $self->msg (w_c => 0, "destroy"); |
193 | delete $ws->{w}{$self->{id}}; |
292 | delete $ws->{_w}{$self->{id}}; |
194 | $self->msg (w_c => name => "destroy"); |
|
|
195 | } |
293 | } |
|
|
294 | } |
|
|
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 |
196 | } |
323 | } |
197 | |
324 | |
198 | sub msg { |
325 | sub msg { |
199 | my ($self, $type, %msg) = @_; |
326 | my ($self, $type, @arg) = @_; |
|
|
327 | |
|
|
328 | if (my $ns = $self->{ns}) { |
|
|
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)); |
|
|
332 | } |
|
|
333 | } |
|
|
334 | |
|
|
335 | sub msg_cb { |
|
|
336 | my ($self, $cb, $type, @arg) = @_; |
200 | |
337 | |
201 | if (my $ws = $self->{ws}) { |
338 | if (my $ws = $self->{ws}) { |
202 | $ws->msg ($type, |
|
|
203 | %msg, |
|
|
204 | id => $self->{id}, |
|
|
205 | ); |
|
|
206 | } |
|
|
207 | } |
|
|
208 | |
|
|
209 | sub msg_cb { |
|
|
210 | my ($self, $cb, $type, %msg) = @_; |
|
|
211 | |
|
|
212 | if (my $ws = $self->{ws}) { |
|
|
213 | |
|
|
214 | my $rid = ++$ws->{ns}{id}; |
339 | my $rid = $ws->{ns}->alloc_wid; |
215 | |
|
|
216 | $self->msg ($type, %msg, rid => $rid); |
|
|
217 | |
340 | |
218 | if ($cb) { |
341 | if ($cb) { |
219 | $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); |
220 | } else { |
349 | } else { |
221 | # synchronous case |
350 | # synchronous case |
222 | my $wait = new Coro::Signal; |
351 | my $wait = new Coro::Signal; |
223 | my @res; |
352 | my @res; |
224 | |
353 | |
225 | $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 | |
226 | @res = @_; |
358 | @res = @_; |
227 | $wait->send; |
359 | $wait->send; |
228 | }; |
360 | }; |
|
|
361 | $self->msg ($type, $rid, @arg); |
229 | $wait->wait; |
362 | $wait->wait; |
230 | |
363 | |
231 | return @res; |
364 | return @res; |
232 | } |
365 | } |
233 | } |
366 | } |
234 | |
367 | |
235 | () |
368 | () |
236 | } |
369 | } |
237 | |
370 | |
238 | sub set { |
371 | sub set { |
239 | my ($self, $member, $value) = @_; |
372 | my ($self, @kv) = @_; |
240 | |
373 | |
241 | $self->msg (w_s => name => $member, value => $value); |
374 | $self->msg (w_s => \@kv); |
242 | } |
375 | } |
243 | |
376 | |
244 | sub get { |
377 | sub get { |
245 | my ($self, $member, $cb) = @_; |
378 | my ($self, $member, $cb) = @_; |
246 | |
379 | |
247 | $self->msg_cb ($cb, w_g => name => $member); |
380 | $self->msg_cb ($cb, w_g => ref $member ? @$member : $member); |
248 | } |
381 | } |
249 | |
382 | |
250 | sub TO_JSON { |
383 | sub TO_JSON { |
251 | { __widget_ref__ => $_[0]{id} } |
384 | { "\fw" => $_[0]{id} } |
252 | } |
385 | } |
253 | |
386 | |
254 | our $AUTOLOAD; |
387 | our $AUTOLOAD; |
255 | |
388 | |
256 | sub AUTOLOAD { |
389 | sub AUTOLOAD { |
257 | $AUTOLOAD =~ s/^.*:// |
390 | $AUTOLOAD =~ s/^.*:// |
258 | or return; |
391 | or return; |
259 | |
392 | |
260 | my $self = shift; |
393 | my $self = shift; |
261 | |
394 | |
|
|
395 | #TODO: handle non-void context |
262 | $self->msg (w_c => name => $AUTOLOAD, args => \@_); |
396 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
263 | |
397 | |
264 | () |
398 | () |
265 | } |
399 | } |
266 | |
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 | |