… | |
… | |
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_a id name... # associate well-known widget with given id |
5 | # ws_n ws # widgetset new |
5 | # ws_n ws # widgetset new |
6 | # ws_d ws # widgetset destroy |
6 | # ws_d ws # widgetset destroy |
7 | # ws_c ws id class @args # widgetset create |
7 | # ws_c ws id class @args # widgetset create |
|
|
8 | # ws_ct ws ttype ttext done_cb \%cfg # widgetset create from template |
8 | # w_c id rid name @args # widget method call |
9 | # w_c id rid name @args # widget method call |
9 | # w_s id @attr # widget member set |
10 | # w_s id @attr # widget member set |
10 | # w_g id rid @attr # widget member get |
11 | # w_g id rid @attr # widget member get |
11 | # |
12 | # |
12 | # and expects the following exti message types |
13 | # and expects the following exti message types |
13 | # w_r rid res # widget call return |
|
|
14 | # w_e id rid @args # widget_event |
14 | # w_e id @args # widget_call |
15 | |
15 | |
16 | our $DEBUG = 1; |
16 | our $DEBUG = 1; |
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); |
22 | Scalar::Util::weaken (my $weakns = $ns); |
23 | |
23 | |
24 | $ns->{id} = "a"; |
24 | $ns->{id} = "a"; |
25 | $ns->{json_coder}->filter_json_single_key_object (__w_ => sub { |
25 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
26 | # cannot deserialise ATM |
26 | $weakns->{widget}{$_[0]} |
27 | undef |
|
|
28 | }); |
27 | }); |
29 | }, |
28 | }, |
30 | ); |
29 | ); |
31 | |
30 | |
32 | sub csc_update_stats { |
31 | sub csc_update_stats { |
… | |
… | |
35 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
34 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
36 | $v->set_text ($ns->pl->ob->stats->$k); |
35 | $v->set_text ($ns->pl->ob->stats->$k); |
37 | } |
36 | } |
38 | } |
37 | } |
39 | |
38 | |
40 | sub demo_start { |
39 | my $cg_template = eval <<EOF; |
41 | my ($ns) = @_; |
40 | [ |
42 | |
41 | Toplevel => { |
43 | my $ws = $ns->{csc} = $ns->new_widgetset; |
42 | s_id => "toplevel", |
44 | |
43 | title => "Character Creation", |
45 | $ws->{tab} = $ws->new (Label => text => "dumb tst", c_tab => ["hull"]); |
44 | x => "center", |
|
|
45 | y => "center", |
|
|
46 | z => 5, |
|
|
47 | force_w => 760, |
|
|
48 | force_h => 440, |
|
|
49 | s_cl => [VBox => { s_cl => [ |
|
|
50 | Label => { |
|
|
51 | text => "Character Creation", |
|
|
52 | fontsize => 1, |
|
|
53 | align => 0, |
|
|
54 | }, |
|
|
55 | Label => { |
|
|
56 | markup => "View or Edit your character attributes below, then press <b>Finish</b> to create your character", |
|
|
57 | fontsize => 0.8, |
|
|
58 | align => 0, |
|
|
59 | }, |
|
|
60 | HBox => { s_cl => [ |
|
|
61 | Face => { |
|
|
62 | s_id => "face", |
|
|
63 | face => 0, |
|
|
64 | bg => [.2, .2, .2, 1], |
|
|
65 | min_w => 64, |
|
|
66 | min_h => 64, |
|
|
67 | }, |
|
|
68 | Label => { |
|
|
69 | s_id => "desc", |
|
|
70 | fontsize => 0.8, |
|
|
71 | ellipsize => 0, |
|
|
72 | }, |
|
|
73 | ]}, |
|
|
74 | Notebook => { |
|
|
75 | expand => 1, |
|
|
76 | s_cl => [ |
|
|
77 | Table => { |
|
|
78 | c_tab => ["Basics", "Title, background and other information of your character."], |
|
|
79 | }, |
|
|
80 | Table => { |
|
|
81 | c_tab => ["Stats", "Your character's primary stats such as strength, dexterity and so on."], |
|
|
82 | }, |
|
|
83 | Table => { |
|
|
84 | c_tab => ["Race", "Your character's race."], |
|
|
85 | }, |
|
|
86 | Table => { |
|
|
87 | c_tab => ["Class", "Your character's initial class."], |
|
|
88 | }, |
|
|
89 | ], |
|
|
90 | }, |
|
|
91 | Button => { |
|
|
92 | s_id => "finish", |
|
|
93 | text => "Finish", |
|
|
94 | }, |
|
|
95 | ]}], |
46 | |
96 | }, |
47 | $ws->find ("setup_notebook")->add ($ws->{tab}); |
97 | ] |
48 | $ws->find ("setup_dialog")->toggle_visibility; |
98 | EOF |
49 | } |
99 | die if $@; |
50 | |
100 | |
51 | sub csc_start { |
101 | sub csc_start { |
52 | my ($ns) = @_; |
102 | my ($ns) = @_; |
53 | |
103 | |
54 | my $ws = $ns->{csc} = $ns->new_widgetset; |
104 | my $ws = $ns->{csc} = $ns->new_widgetset; |
… | |
… | |
94 | |
144 | |
95 | csc_update_stats $ns; |
145 | csc_update_stats $ns; |
96 | |
146 | |
97 | $ws->{tl} = $w; |
147 | $ws->{tl} = $w; |
98 | $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; |
99 | } |
168 | } |
100 | |
169 | |
101 | cf::player->attach ( |
170 | cf::player->attach ( |
102 | on_login => sub { |
171 | on_login => sub { |
103 | my ($pl) = @_; |
172 | my ($pl) = @_; |
104 | |
173 | |
|
|
174 | my $ns = $pl->ns; |
|
|
175 | return unless $ns->{can_widget}; |
|
|
176 | |
105 | return unless $cf::CFG{devel}; |
177 | return unless $cf::CFG{devel}; |
106 | |
178 | |
107 | my $ns = $pl->ns; |
|
|
108 | |
|
|
109 | return unless $ns->{can_widget}; |
|
|
110 | #csc_start $ns; |
179 | #csc_start $ns; |
111 | demo_start $ns; |
|
|
112 | }, |
180 | }, |
113 | ); |
181 | ); |
114 | |
182 | |
115 | cf::register_exticmd w_e => sub { |
183 | cf::register_exticmd w_e => sub { |
116 | my ($ns, $id, $rid, @args) = @_; |
184 | my ($ns, $id, @args) = @_; |
117 | |
185 | |
118 | if (my $w = $ns->{widget}{$id}) { |
186 | if (my $cb = $ns->{widget_cb}{$id}) { |
119 | if (my $cb = $w->{ev}{$rid}) { |
|
|
120 | $cb->($w, @args); |
|
|
121 | } |
|
|
122 | } |
|
|
123 | |
|
|
124 | () |
|
|
125 | }; |
|
|
126 | |
|
|
127 | cf::register_exticmd w_r => sub { |
|
|
128 | my ($ns, $rid, $res) = @_; |
|
|
129 | |
|
|
130 | if (my $cb = delete $ns->{widget_return}{$rid}) { |
|
|
131 | $cb->(@$res); |
187 | $cb->(@args); |
132 | } |
188 | } |
133 | |
189 | |
134 | () |
190 | () |
135 | }; |
191 | }; |
136 | |
192 | |
… | |
… | |
148 | $ws->msg (ws_n => $id); |
204 | $ws->msg (ws_n => $id); |
149 | |
205 | |
150 | $ws |
206 | $ws |
151 | } |
207 | } |
152 | |
208 | |
|
|
209 | sub cf::client::alloc_wid { |
|
|
210 | pop @{ $_[0]{ids} } |
|
|
211 | or ++$_[0]{id} |
|
|
212 | } |
|
|
213 | |
|
|
214 | sub cf::client::free_wid { |
|
|
215 | push @{ $_[0]{ids} }, $_[1]; |
|
|
216 | } |
|
|
217 | |
153 | ############################################################################# |
218 | ############################################################################# |
154 | |
219 | |
155 | package ext::widget::set; |
220 | package ext::widget::set; |
156 | |
221 | |
157 | sub DESTROY { |
222 | sub DESTROY { |
… | |
… | |
177 | } |
242 | } |
178 | |
243 | |
179 | sub alloc { |
244 | sub alloc { |
180 | my ($self) = @_; |
245 | my ($self) = @_; |
181 | |
246 | |
182 | my $id = ++$self->{ns}{id}; |
247 | my $id = $self->{ns}->alloc_wid; |
183 | |
248 | |
184 | my $proxy = bless { |
249 | my $proxy = bless { |
185 | id => $id, |
250 | id => $id, |
186 | }, "ext::widget::proxy"; |
251 | }, "ext::widget::proxy"; |
187 | |
252 | |
… | |
… | |
198 | |
263 | |
199 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
264 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
200 | Scalar::Util::weaken ($proxy->{ws} = $self); |
265 | Scalar::Util::weaken ($proxy->{ws} = $self); |
201 | |
266 | |
202 | for my $ev (grep /^on_/, keys %args) { |
267 | for my $ev (grep /^on_/, keys %args) { |
203 | my $rid = ++$self->{ns}{id}; |
268 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
204 | $proxy->{ev}{$rid} = $args{$ev}; |
|
|
205 | $args{$ev} = $rid; |
|
|
206 | } |
269 | } |
207 | |
270 | |
208 | $self->msg (ws_c => |
271 | $self->msg (ws_c => |
209 | $self->{id}, |
272 | $self->{id}, |
210 | $proxy->{id}, |
273 | $proxy->{id}, |
… | |
… | |
213 | ); |
276 | ); |
214 | |
277 | |
215 | $proxy |
278 | $proxy |
216 | } |
279 | } |
217 | |
280 | |
|
|
281 | sub template { |
|
|
282 | my ($self, $type, $template, $args, $done_cb) = @_; |
|
|
283 | |
|
|
284 | my %cfg; |
|
|
285 | |
|
|
286 | while (@$args) { |
|
|
287 | my ($name, $args) = splice @$args, 0, 2, (); |
|
|
288 | |
|
|
289 | my $proxy = $self->alloc; |
|
|
290 | |
|
|
291 | $self->{delete $args->{alias} or $name} = $proxy; |
|
|
292 | |
|
|
293 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
|
|
294 | Scalar::Util::weaken ($proxy->{ws} = $self); |
|
|
295 | |
|
|
296 | for my $ev (grep /^on_/, keys %$args) { |
|
|
297 | $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); |
|
|
298 | } |
|
|
299 | |
|
|
300 | $cfg{$name} = { |
|
|
301 | %$args, |
|
|
302 | id => $proxy->{id}, |
|
|
303 | }; |
|
|
304 | } |
|
|
305 | |
|
|
306 | if ($done_cb) { |
|
|
307 | my $proxy = $self->alloc; |
|
|
308 | my $ocb = $done_cb; |
|
|
309 | $done_cb = $proxy->cb (sub { |
|
|
310 | undef $proxy; |
|
|
311 | undef $done_cb; |
|
|
312 | &$ocb |
|
|
313 | }); |
|
|
314 | } |
|
|
315 | |
|
|
316 | if ($type eq "face") { |
|
|
317 | $self->{ns}->send_face ($template, 100); |
|
|
318 | $self->{ns}->flush_fx; |
|
|
319 | } |
|
|
320 | |
|
|
321 | $self->msg (ws_ct => |
|
|
322 | $self->{id}, |
|
|
323 | $type => $template, |
|
|
324 | $done_cb, |
|
|
325 | \%cfg, |
|
|
326 | ); |
|
|
327 | } |
|
|
328 | |
218 | sub find { |
329 | sub find { |
219 | my ($self, @names) = @_; |
330 | my ($self, @names) = @_; |
220 | |
331 | |
221 | my @res; |
332 | my @res; |
222 | my @alloc; |
333 | my @alloc; |
… | |
… | |
248 | |
359 | |
249 | if (my $ws = $self->{ws}) { |
360 | if (my $ws = $self->{ws}) { |
250 | $self->msg (w_c => 0, "destroy"); |
361 | $self->msg (w_c => 0, "destroy"); |
251 | delete $ws->{_w}{$self->{id}}; |
362 | delete $ws->{_w}{$self->{id}}; |
252 | } |
363 | } |
|
|
364 | } |
|
|
365 | |
|
|
366 | sub cb { |
|
|
367 | my ($self, $cb) = @_; |
|
|
368 | |
|
|
369 | my $proxy = bless { |
|
|
370 | ns => $self->{ns}, |
|
|
371 | id => $self->{ns}->alloc_wid, |
|
|
372 | }, "ext::widget::callback"; |
|
|
373 | |
|
|
374 | Scalar::Util::weaken $proxy->{ns}; |
|
|
375 | |
|
|
376 | $self->{ns}{widget_cb}{$proxy->{id}} = $cb; |
|
|
377 | |
|
|
378 | $proxy |
|
|
379 | } |
|
|
380 | |
|
|
381 | sub oneshot_cb { |
|
|
382 | my ($self, $cb) = @_; |
|
|
383 | |
|
|
384 | if ("CODE" eq ref $cb) { |
|
|
385 | my $ocb = $cb; |
|
|
386 | $cb = $self->cb (sub { |
|
|
387 | undef $cb; |
|
|
388 | &$ocb |
|
|
389 | }); |
|
|
390 | } |
|
|
391 | |
|
|
392 | $cb |
253 | } |
393 | } |
254 | |
394 | |
255 | sub msg { |
395 | sub msg { |
256 | my ($self, $type, @arg) = @_; |
396 | my ($self, $type, @arg) = @_; |
257 | |
397 | |
… | |
… | |
264 | |
404 | |
265 | sub msg_cb { |
405 | sub msg_cb { |
266 | my ($self, $cb, $type, @arg) = @_; |
406 | my ($self, $cb, $type, @arg) = @_; |
267 | |
407 | |
268 | if (my $ws = $self->{ws}) { |
408 | if (my $ws = $self->{ws}) { |
269 | my $rid = ++$ws->{ns}{id}; |
409 | my $rid = $ws->{ns}->alloc_wid; |
270 | |
|
|
271 | $self->msg ($type, $rid, @arg); |
|
|
272 | |
410 | |
273 | if ($cb) { |
411 | if ($cb) { |
274 | $ws->{ns}{widget_return}{$rid} = $cb; |
412 | $ws->{ns}{widget_cb}{$rid} = sub { |
|
|
413 | delete $ws->{ns}{widget_cb}{$rid}; |
|
|
414 | $ws->{ns}->free_wid ($rid); |
|
|
415 | &$cb |
|
|
416 | }; |
|
|
417 | |
|
|
418 | $self->msg ($type, $rid, @arg); |
275 | } else { |
419 | } else { |
276 | # synchronous case |
420 | # synchronous case |
277 | my $wait = new Coro::Signal; |
421 | my $wait = new Coro::Signal; |
278 | my @res; |
422 | my @res; |
279 | |
423 | |
280 | $ws->{ns}{widget_return}{$rid} = sub { |
424 | $ws->{ns}{widget_cb}{$rid} = sub { |
|
|
425 | delete $ws->{ns}{widget_cb}{$rid}; |
|
|
426 | $ws->{ns}->free_wid ($rid); |
|
|
427 | |
281 | @res = @_; |
428 | @res = @_; |
282 | $wait->send; |
429 | $wait->send; |
283 | }; |
430 | }; |
|
|
431 | $self->msg ($type, $rid, @arg); |
284 | $wait->wait; |
432 | $wait->wait; |
285 | |
433 | |
286 | return @res; |
434 | return @res; |
287 | } |
435 | } |
288 | } |
436 | } |
… | |
… | |
297 | } |
445 | } |
298 | |
446 | |
299 | sub get { |
447 | sub get { |
300 | my ($self, $member, $cb) = @_; |
448 | my ($self, $member, $cb) = @_; |
301 | |
449 | |
302 | $self->msg_cb ($cb, w_g => [$member]); |
450 | $self->msg_cb ($cb, w_g => ref $member ? @$member : $member); |
303 | } |
451 | } |
304 | |
452 | |
305 | sub TO_JSON { |
453 | sub TO_JSON { |
306 | { __w_ => $_[0]{id} } |
454 | { "\fw" => $_[0]{id} } |
307 | } |
455 | } |
308 | |
456 | |
309 | our $AUTOLOAD; |
457 | our $AUTOLOAD; |
310 | |
458 | |
311 | sub AUTOLOAD { |
459 | sub AUTOLOAD { |
… | |
… | |
318 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
466 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
319 | |
467 | |
320 | () |
468 | () |
321 | } |
469 | } |
322 | |
470 | |
|
|
471 | package ext::widget::callback; |
|
|
472 | |
|
|
473 | sub DESTROY { |
|
|
474 | my ($self) = @_; |
|
|
475 | |
|
|
476 | if (my $ns = $self->{ns}) { |
|
|
477 | delete $ns->{widget_cb}{$self->{id}}; |
|
|
478 | $ns->free_wid ($self->{id}); |
|
|
479 | } |
|
|
480 | } |
|
|
481 | |
|
|
482 | sub TO_JSON { |
|
|
483 | { "\fc" => $_[0]{id} } |
|
|
484 | } |
|
|
485 | |