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 ttype ttext done_cb \%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 | |
|
|
23 | $ns->{id} = "a"; |
22 | $ns->{id} = "a"; |
24 | $ns->{json_coder}->filter_json_single_key_object (__w_ => sub { |
23 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
25 | # cannot deserialise ATM |
24 | $ns->{widget}{$_[0]} |
26 | undef |
|
|
27 | }); |
25 | }); |
|
|
26 | |
|
|
27 | cf::weaken $ns; |
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 | } |
37 | } |
|
|
38 | |
|
|
39 | my $cg_template = eval <<EOF; |
|
|
40 | [ |
|
|
41 | Toplevel => { |
|
|
42 | s_id => "toplevel", |
|
|
43 | title => "Character Creation", |
|
|
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 | ]}], |
|
|
96 | }, |
|
|
97 | ] |
|
|
98 | EOF |
|
|
99 | die if $@; |
38 | |
100 | |
39 | sub csc_start { |
101 | sub csc_start { |
40 | my ($ns) = @_; |
102 | my ($ns) = @_; |
41 | |
103 | |
42 | my $ws = $ns->{csc} = $ns->new_widgetset; |
104 | my $ws = $ns->{csc} = $ns->new_widgetset; |
… | |
… | |
53 | }, |
115 | }, |
54 | ); |
116 | ); |
55 | |
117 | |
56 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
118 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
57 | |
119 | |
58 | $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); |
120 | $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); |
59 | |
121 | |
60 | $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); |
122 | $stats->add_at (0, 0, (my $statstable = $ws->new ("Table"))); |
61 | |
123 | |
62 | for ( |
124 | for ( |
63 | [0, "Str"], |
125 | [0, "Str"], |
64 | [1, "Dex"], |
126 | [1, "Dex"], |
65 | [2, "Con"], |
127 | [2, "Con"], |
… | |
… | |
80 | )); |
142 | )); |
81 | } |
143 | } |
82 | |
144 | |
83 | csc_update_stats $ns; |
145 | csc_update_stats $ns; |
84 | |
146 | |
|
|
147 | $ws->{tl} = $w; |
85 | $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; |
86 | } |
168 | } |
87 | |
169 | |
88 | cf::player->attach ( |
170 | cf::player->attach ( |
89 | on_login => sub { |
171 | on_login => sub { |
90 | my ($pl) = @_; |
172 | my ($pl) = @_; |
91 | |
173 | |
|
|
174 | my $ns = $pl->ns; |
|
|
175 | return unless $ns->{can_widget}; |
|
|
176 | |
92 | return unless $cf::CFG{devel}; |
177 | return unless $cf::CFG{devel}; |
93 | |
178 | |
94 | my $ns = $pl->ns; |
|
|
95 | |
|
|
96 | return unless $ns->{can_widget}; |
|
|
97 | #csc_start $ns; |
179 | #csc_start $ns; |
98 | }, |
180 | }, |
99 | ); |
181 | ); |
100 | |
182 | |
101 | cf::register_exticmd w_e => sub { |
183 | cf::register_exticmd w_e => sub { |
102 | my ($ns, $id, $name, $args) = @_; |
184 | my ($ns, $id, @args) = @_; |
103 | |
185 | |
104 | if (my $w = $ns->{widget}{$id}) { |
186 | 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); |
187 | $cb->(@args); |
119 | } |
188 | } |
120 | |
189 | |
121 | () |
190 | () |
122 | }; |
191 | }; |
123 | |
192 | |
… | |
… | |
130 | id => $id, |
199 | id => $id, |
131 | ns => $self, |
200 | ns => $self, |
132 | _w => {}, |
201 | _w => {}, |
133 | }, "ext::widget::set"; |
202 | }, "ext::widget::set"; |
134 | |
203 | |
|
|
204 | cf::weaken $ws->{ns}; |
|
|
205 | |
135 | $ws->msg (ws_n => $id); |
206 | $ws->msg (ws_n => $id); |
136 | |
207 | |
137 | $ws |
208 | $ws |
|
|
209 | } |
|
|
210 | |
|
|
211 | sub cf::client::alloc_wid { |
|
|
212 | pop @{ $_[0]{ids} } |
|
|
213 | or ++$_[0]{id} |
|
|
214 | } |
|
|
215 | |
|
|
216 | sub cf::client::free_wid { |
|
|
217 | push @{ $_[0]{ids} }, $_[1]; |
138 | } |
218 | } |
139 | |
219 | |
140 | ############################################################################# |
220 | ############################################################################# |
141 | |
221 | |
142 | package ext::widget::set; |
222 | package ext::widget::set; |
… | |
… | |
156 | |
236 | |
157 | sub msg { |
237 | sub msg { |
158 | my ($self, @msg) = @_; |
238 | my ($self, @msg) = @_; |
159 | |
239 | |
160 | if (my $ns = shift->{ns}) { |
240 | if (my $ns = shift->{ns}) { |
|
|
241 | return unless $ns->{json_coder};#d# |
161 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
242 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
162 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
243 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
163 | } |
244 | } |
164 | } |
245 | } |
165 | |
246 | |
166 | sub new { |
247 | sub alloc { |
167 | my ($self, $class, %args) = @_; |
248 | my ($self) = @_; |
168 | |
249 | |
169 | my $id = ++$self->{ns}{id}; |
250 | my $id = $self->{ns}->alloc_wid; |
170 | |
251 | |
171 | my $proxy = bless { |
252 | my $proxy = bless { |
172 | id => $id, |
253 | id => $id, |
173 | }, "ext::widget::proxy"; |
254 | }, "ext::widget::proxy"; |
174 | |
255 | |
175 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
|
|
176 | Scalar::Util::weaken ($proxy->{ws} = $self); |
|
|
177 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
256 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
178 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
257 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
179 | |
258 | |
|
|
259 | $proxy |
|
|
260 | } |
|
|
261 | |
|
|
262 | sub new { |
|
|
263 | my ($self, $class, %args) = @_; |
|
|
264 | |
|
|
265 | my $proxy = $self->alloc; |
|
|
266 | |
|
|
267 | cf::weaken |
|
|
268 | +($self->{_w}{$proxy->{id}} = $proxy), |
|
|
269 | +($proxy->{ws} = $self); |
|
|
270 | |
180 | for my $ev (grep /^on_/, keys %args) { |
271 | for my $ev (grep /^on_/, keys %args) { |
181 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
272 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
182 | $args{$ev} = 0; |
|
|
183 | } |
273 | } |
184 | |
274 | |
185 | $self->msg (ws_c => |
275 | $self->msg (ws_c => |
186 | $self->{id}, |
276 | $self->{id}, |
187 | $id, |
277 | $proxy->{id}, |
188 | $class, |
278 | $class, |
189 | \%args, |
279 | \%args, |
190 | ); |
280 | ); |
191 | |
281 | |
192 | $proxy |
282 | $proxy |
193 | } |
283 | } |
194 | |
284 | |
|
|
285 | sub template { |
|
|
286 | my ($self, $type, $template, $args, $done_cb) = @_; |
|
|
287 | |
|
|
288 | my %cfg; |
|
|
289 | |
|
|
290 | while (@$args) { |
|
|
291 | my ($name, $args) = splice @$args, 0, 2, (); |
|
|
292 | |
|
|
293 | my $proxy = $self->alloc; |
|
|
294 | |
|
|
295 | $self->{delete $args->{alias} or $name} = $proxy; |
|
|
296 | |
|
|
297 | cf::weaken |
|
|
298 | +($self->{_w}{$proxy->{id}} = $proxy), |
|
|
299 | +($proxy->{ws} = $self); |
|
|
300 | |
|
|
301 | for my $ev (grep /^on_/, keys %$args) { |
|
|
302 | $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); |
|
|
303 | } |
|
|
304 | |
|
|
305 | $cfg{$name} = { |
|
|
306 | %$args, |
|
|
307 | id => $proxy->{id}, |
|
|
308 | }; |
|
|
309 | } |
|
|
310 | |
|
|
311 | if ($done_cb) { |
|
|
312 | my $proxy = $self->alloc; |
|
|
313 | my $ocb = $done_cb; |
|
|
314 | $done_cb = $proxy->cb (sub { |
|
|
315 | undef $proxy; |
|
|
316 | undef $done_cb; |
|
|
317 | &$ocb |
|
|
318 | }); |
|
|
319 | } |
|
|
320 | |
|
|
321 | if ($type eq "face") { |
|
|
322 | $self->{ns}->send_face ($template, 100); |
|
|
323 | $self->{ns}->flush_fx; |
|
|
324 | } |
|
|
325 | |
|
|
326 | $self->msg (ws_ct => |
|
|
327 | $self->{id}, |
|
|
328 | $type => $template, |
|
|
329 | $done_cb, |
|
|
330 | \%cfg, |
|
|
331 | ); |
|
|
332 | } |
|
|
333 | |
|
|
334 | sub find { |
|
|
335 | my ($self, @names) = @_; |
|
|
336 | |
|
|
337 | my @res; |
|
|
338 | my @alloc; |
|
|
339 | |
|
|
340 | for my $name (@names) { |
|
|
341 | push @res, $self->{ns}{widget_wkw}{$name} ||= do { |
|
|
342 | my $proxy = $self->alloc; |
|
|
343 | |
|
|
344 | push @alloc, $proxy->{id} => $name; |
|
|
345 | |
|
|
346 | $proxy |
|
|
347 | }; |
|
|
348 | } |
|
|
349 | |
|
|
350 | $self->msg (ws_a => @alloc) |
|
|
351 | if @alloc; |
|
|
352 | |
|
|
353 | wantarray ? @res : $res[0] |
|
|
354 | } |
|
|
355 | |
195 | ############################################################################# |
356 | ############################################################################# |
196 | |
357 | |
197 | package ext::widget::proxy; |
358 | package ext::widget::proxy; |
198 | |
|
|
199 | sub DESTROY { |
|
|
200 | my ($self) = @_; |
|
|
201 | |
|
|
202 | delete $self->{ns}{widget}{$self->{id}}; |
|
|
203 | |
|
|
204 | if (my $ws = $self->{ws}) { |
|
|
205 | $self->msg (w_c => 0, "destroy"); |
|
|
206 | delete $ws->{_w}{$self->{id}}; |
|
|
207 | } |
|
|
208 | } |
|
|
209 | |
359 | |
210 | sub msg { |
360 | sub msg { |
211 | my ($self, $type, @arg) = @_; |
361 | my ($self, $type, @arg) = @_; |
212 | |
362 | |
213 | if (my $ws = $self->{ws}) { |
363 | if (my $ns = $self->{ns}) { |
214 | $ws->msg ($type, $self->{id}, @arg); |
364 | my @msg = ($type, $self->{id}, @arg); |
|
|
365 | warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
|
|
366 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
215 | } |
367 | } |
216 | } |
368 | } |
217 | |
369 | |
218 | sub msg_cb { |
370 | sub msg_cb { |
219 | my ($self, $cb, $type, @arg) = @_; |
371 | my ($self, $cb, $type, @arg) = @_; |
220 | |
372 | |
221 | if (my $ws = $self->{ws}) { |
373 | if (my $ws = $self->{ws}) { |
222 | my $rid = ++$ws->{ns}{id}; |
374 | my $rid = $ws->{ns}->alloc_wid; |
223 | |
|
|
224 | $self->msg ($type, $rid, @arg); |
|
|
225 | |
375 | |
226 | if ($cb) { |
376 | if ($cb) { |
227 | $ws->{ns}{widget_return}{$rid} = $cb; |
377 | $ws->{ns}{widget_cb}{$rid} = sub { |
|
|
378 | delete $ws->{ns}{widget_cb}{$rid}; |
|
|
379 | $ws->{ns}->free_wid ($rid); |
|
|
380 | &$cb |
|
|
381 | }; |
|
|
382 | |
|
|
383 | $self->msg ($type, $rid, @arg); |
228 | } else { |
384 | } else { |
229 | # synchronous case |
385 | # synchronous case |
230 | my $wait = new Coro::Signal; |
386 | my $wait = new Coro::Signal; |
231 | my @res; |
387 | my @res; |
232 | |
388 | |
233 | $ws->{ns}{widget_return}{$rid} = sub { |
389 | $ws->{ns}{widget_cb}{$rid} = sub { |
|
|
390 | delete $ws->{ns}{widget_cb}{$rid}; |
|
|
391 | $ws->{ns}->free_wid ($rid); |
|
|
392 | |
234 | @res = @_; |
393 | @res = @_; |
235 | $wait->send; |
394 | $wait->send; |
236 | }; |
395 | }; |
|
|
396 | $self->msg ($type, $rid, @arg); |
237 | $wait->wait; |
397 | $wait->wait; |
238 | |
398 | |
239 | return @res; |
399 | return @res; |
240 | } |
400 | } |
241 | } |
401 | } |
242 | |
402 | |
243 | () |
403 | () |
244 | } |
404 | } |
245 | |
405 | |
|
|
406 | sub DESTROY { |
|
|
407 | my ($self) = @_; |
|
|
408 | |
|
|
409 | delete $self->{ns}{widget}{$self->{id}}; |
|
|
410 | |
|
|
411 | if (my $ws = $self->{ws}) { |
|
|
412 | $self->msg (w_c => 0, "destroy"); |
|
|
413 | delete $ws->{_w}{$self->{id}}; |
|
|
414 | } |
|
|
415 | } |
|
|
416 | |
|
|
417 | sub cb { |
|
|
418 | my ($self, $cb) = @_; |
|
|
419 | |
|
|
420 | my $proxy = bless { |
|
|
421 | ns => $self->{ns}, |
|
|
422 | id => $self->{ns}->alloc_wid, |
|
|
423 | }, "ext::widget::callback"; |
|
|
424 | |
|
|
425 | cf::weaken $proxy->{ns}; |
|
|
426 | |
|
|
427 | $self->{ns}{widget_cb}{$proxy->{id}} = $cb; |
|
|
428 | |
|
|
429 | $proxy |
|
|
430 | } |
|
|
431 | |
|
|
432 | sub oneshot_cb { |
|
|
433 | my ($self, $cb) = @_; |
|
|
434 | |
|
|
435 | if ("CODE" eq ref $cb) { |
|
|
436 | my $ocb = $cb; |
|
|
437 | $cb = cb $self, sub { |
|
|
438 | undef $cb; |
|
|
439 | &$ocb |
|
|
440 | }; |
|
|
441 | } |
|
|
442 | |
|
|
443 | $cb |
|
|
444 | } |
|
|
445 | |
246 | sub set { |
446 | sub set { |
247 | my ($self, @kv) = @_; |
447 | my ($self, @kv) = @_; |
248 | |
448 | |
249 | $self->msg (w_s => \@kv); |
449 | $self->msg (w_s => \@kv); |
250 | } |
450 | } |
251 | |
451 | |
252 | sub get { |
452 | sub get { |
253 | my ($self, $member, $cb) = @_; |
453 | my ($self, $member, $cb) = @_; |
254 | |
454 | |
255 | $self->msg_cb ($cb, w_g => [$member]); |
455 | $self->msg_cb ($cb, w_g => ref $member ? @$member : $member); |
256 | } |
456 | } |
257 | |
457 | |
258 | sub TO_JSON { |
458 | sub TO_JSON { |
259 | { __w_ => $_[0]{id} } |
459 | { "\fw" => $_[0]{id} } |
260 | } |
460 | } |
261 | |
461 | |
262 | our $AUTOLOAD; |
462 | our $AUTOLOAD; |
263 | |
463 | |
264 | sub AUTOLOAD { |
464 | sub AUTOLOAD { |
265 | $AUTOLOAD =~ s/^.*:// |
465 | $AUTOLOAD =~ s/^.*:// |
266 | or return; |
466 | or return; |
267 | |
467 | |
268 | my $self = shift; |
468 | my $self = shift; |
269 | |
469 | |
|
|
470 | #TODO: handle non-void context |
270 | $self->msg (w_c => 0, $AUTOLOAD, \@_); |
471 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
271 | |
472 | |
272 | () |
473 | () |
273 | } |
474 | } |
274 | |
475 | |
|
|
476 | package ext::widget::callback; |
|
|
477 | |
|
|
478 | sub DESTROY { |
|
|
479 | my ($self) = @_; |
|
|
480 | |
|
|
481 | if (my $ns = $self->{ns}) { |
|
|
482 | delete $ns->{widget_cb}{$self->{id}}; |
|
|
483 | $ns->free_wid ($self->{id}); |
|
|
484 | } |
|
|
485 | } |
|
|
486 | |
|
|
487 | sub TO_JSON { |
|
|
488 | { "\fc" => $_[0]{id} } |
|
|
489 | } |
|
|
490 | |