… | |
… | |
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 templateface \%cfg # widgetset create from template |
8 | # ws_ct ws ttype ttext done_cb \%cfg # widgetset create from template |
9 | # w_c id rid name @args # widget method call |
9 | # w_c id rid name @args # widget method call |
10 | # w_s id @attr # widget member set |
10 | # w_s id @attr # widget member set |
11 | # w_g id rid @attr # widget member get |
11 | # w_g id rid @attr # widget member get |
12 | # |
12 | # |
13 | # and expects the following exti message types |
13 | # and expects the following exti message types |
14 | # w_e id @args # widget_call |
14 | # w_e id @args # widget_call |
15 | |
15 | |
16 | our $DEBUG = 1; |
|
|
17 | |
|
|
18 | cf::client->attach ( |
16 | cf::client->attach ( |
19 | on_connect => sub { |
17 | on_connect => sub { |
20 | my ($ns) = @_; |
18 | my ($ns) = @_; |
21 | |
19 | |
22 | Scalar::Util::weaken (my $weakns = $ns); |
|
|
23 | |
|
|
24 | $ns->{id} = "a"; |
20 | $ns->{id} = "a"; |
25 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
21 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
26 | $weakns->{widget}{$_[0]} |
22 | $ns->{widget}{$_[0]} |
27 | }); |
23 | }); |
|
|
24 | |
|
|
25 | cf::weaken $ns; |
28 | }, |
26 | }, |
29 | ); |
27 | ); |
30 | |
28 | |
31 | sub csc_update_stats { |
29 | sub csc_update_stats { |
32 | my ($ns) = @_; |
30 | my ($ns) = @_; |
… | |
… | |
34 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
32 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
35 | $v->set_text ($ns->pl->ob->stats->$k); |
33 | $v->set_text ($ns->pl->ob->stats->$k); |
36 | } |
34 | } |
37 | } |
35 | } |
38 | |
36 | |
39 | sub demo_start { |
37 | my $cg_template = eval <<EOF; |
40 | my ($ns) = @_; |
38 | [ |
41 | |
|
|
42 | my $ws = $ns->{csc} = $ns->new_widgetset; |
|
|
43 | |
|
|
44 | my ($tl, $entry) = $ws->template (undef, |
|
|
45 | toplevel => {}, |
39 | Toplevel => { |
46 | entry => { |
40 | s_id => "toplevel", |
47 | text => "xyz", |
41 | title => "Character Creation", |
48 | on_changed => sub { |
42 | x => "center", |
49 | warn "changed<@_>\n";#d# |
43 | y => "center", |
|
|
44 | z => 5, |
|
|
45 | force_w => 760, |
|
|
46 | force_h => 440, |
|
|
47 | s_cl => [VBox => { s_cl => [ |
|
|
48 | Label => { |
|
|
49 | text => "Character Creation", |
|
|
50 | fontsize => 1, |
|
|
51 | align => 0, |
50 | }, |
52 | }, |
|
|
53 | Label => { |
|
|
54 | markup => "View or Edit your character attributes below, then press <b>Finish</b> to create your character", |
|
|
55 | fontsize => 0.8, |
|
|
56 | align => 0, |
|
|
57 | }, |
|
|
58 | HBox => { s_cl => [ |
|
|
59 | Face => { |
|
|
60 | s_id => "face", |
|
|
61 | face => 0, |
|
|
62 | bg => [.2, .2, .2, 1], |
|
|
63 | min_w => 64, |
|
|
64 | min_h => 64, |
|
|
65 | }, |
|
|
66 | Label => { |
|
|
67 | s_id => "desc", |
|
|
68 | fontsize => 0.8, |
|
|
69 | ellipsize => 0, |
|
|
70 | }, |
|
|
71 | ]}, |
|
|
72 | Notebook => { |
|
|
73 | expand => 1, |
|
|
74 | s_cl => [ |
|
|
75 | Table => { |
|
|
76 | c_tab => ["Basics", "Title, background and other information of your character."], |
|
|
77 | }, |
|
|
78 | Table => { |
|
|
79 | c_tab => ["Stats", "Your character's primary stats such as strength, dexterity and so on."], |
|
|
80 | }, |
|
|
81 | Table => { |
|
|
82 | c_tab => ["Race", "Your character's race."], |
|
|
83 | }, |
|
|
84 | Table => { |
|
|
85 | c_tab => ["Class", "Your character's initial class."], |
|
|
86 | }, |
|
|
87 | ], |
|
|
88 | }, |
|
|
89 | Button => { |
|
|
90 | s_id => "finish", |
|
|
91 | text => "Finish", |
|
|
92 | }, |
51 | }, |
93 | ]}], |
52 | ); |
|
|
53 | |
|
|
54 | $tl->show; |
|
|
55 | |
|
|
56 | $ns->{xxxw} = [$tl, $entry];#d# |
|
|
57 | |
94 | }, |
58 | # $ws->find ("setup_notebook")->add ($ws->{tab}); |
95 | ] |
59 | # $ws->find ("setup_dialog")->toggle_visibility; |
96 | EOF |
60 | } |
97 | die if $@; |
61 | |
98 | |
62 | sub csc_start { |
99 | sub csc_start { |
63 | my ($ns) = @_; |
100 | my ($ns) = @_; |
64 | |
101 | |
65 | my $ws = $ns->{csc} = $ns->new_widgetset; |
102 | my $ws = $ns->{csc} = $ns->new_widgetset; |
… | |
… | |
105 | |
142 | |
106 | csc_update_stats $ns; |
143 | csc_update_stats $ns; |
107 | |
144 | |
108 | $ws->{tl} = $w; |
145 | $ws->{tl} = $w; |
109 | $w->show; |
146 | $w->show; |
|
|
147 | |
|
|
148 | # my ($tl, $entry) = $ws->template (inline => $cg_template, |
|
|
149 | # [ |
|
|
150 | # toplevel => {}, |
|
|
151 | # entry => { |
|
|
152 | # text => "xyz", |
|
|
153 | # on_changed => sub { |
|
|
154 | # warn "changed<@_>\n";#d# |
|
|
155 | # }, |
|
|
156 | # }, |
|
|
157 | # ], |
|
|
158 | # ); |
|
|
159 | # |
|
|
160 | # $tl->show; |
|
|
161 | # |
|
|
162 | # $ns->{xxxw} = [$tl, $entry];#d# |
|
|
163 | # |
|
|
164 | # $ws->find ("setup_notebook")->add ($ws->{tab}); |
|
|
165 | # $ws->find ("setup_dialog")->toggle_visibility; |
110 | } |
166 | } |
111 | |
167 | |
112 | cf::player->attach ( |
168 | cf::player->attach ( |
113 | on_login => sub { |
169 | on_login => sub { |
114 | my ($pl) = @_; |
170 | my ($pl) = @_; |
115 | |
171 | |
|
|
172 | my $ns = $pl->ns; |
|
|
173 | return unless $ns->{can_widget}; |
|
|
174 | |
116 | return unless $cf::CFG{devel}; |
175 | return unless $cf::CFG{devel}; |
117 | |
176 | |
118 | my $ns = $pl->ns; |
|
|
119 | |
|
|
120 | return unless $ns->{can_widget}; |
|
|
121 | #csc_start $ns; |
177 | #csc_start $ns; |
122 | demo_start $ns; |
|
|
123 | }, |
178 | }, |
124 | ); |
179 | ); |
125 | |
180 | |
126 | cf::register_exticmd w_e => sub { |
181 | cf::register_exticmd w_e => sub { |
127 | my ($ns, $id, @args) = @_; |
182 | my ($ns, $id, @args) = @_; |
… | |
… | |
142 | id => $id, |
197 | id => $id, |
143 | ns => $self, |
198 | ns => $self, |
144 | _w => {}, |
199 | _w => {}, |
145 | }, "ext::widget::set"; |
200 | }, "ext::widget::set"; |
146 | |
201 | |
|
|
202 | cf::weaken $ws->{ns}; |
|
|
203 | |
147 | $ws->msg (ws_n => $id); |
204 | $ws->msg (ws_n => $id); |
148 | |
205 | |
149 | $ws |
206 | $ws |
150 | } |
207 | } |
151 | |
208 | |
… | |
… | |
177 | |
234 | |
178 | sub msg { |
235 | sub msg { |
179 | my ($self, @msg) = @_; |
236 | my ($self, @msg) = @_; |
180 | |
237 | |
181 | if (my $ns = shift->{ns}) { |
238 | if (my $ns = shift->{ns}) { |
182 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
239 | return unless $ns->{json_coder};#d# might be gone at destroy time(??) |
183 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
240 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
184 | } |
241 | } |
185 | } |
242 | } |
186 | |
243 | |
187 | sub alloc { |
244 | sub alloc { |
… | |
… | |
191 | |
248 | |
192 | my $proxy = bless { |
249 | my $proxy = bless { |
193 | id => $id, |
250 | id => $id, |
194 | }, "ext::widget::proxy"; |
251 | }, "ext::widget::proxy"; |
195 | |
252 | |
196 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
253 | cf::weaken ($proxy->{ns} = $self->{ns}); |
197 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
254 | cf::weaken ($self->{ns}{widget}{$id} = $proxy); |
198 | |
255 | |
199 | $proxy |
256 | $proxy |
200 | } |
257 | } |
201 | |
258 | |
202 | sub new { |
259 | sub new { |
203 | my ($self, $class, %args) = @_; |
260 | my ($self, $class, %args) = @_; |
204 | |
261 | |
205 | my $proxy = $self->alloc; |
262 | my $proxy = $self->alloc; |
206 | |
263 | |
207 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
264 | cf::weaken |
208 | Scalar::Util::weaken ($proxy->{ws} = $self); |
265 | +($self->{_w}{$proxy->{id}} = $proxy), |
|
|
266 | +($proxy->{ws} = $self); |
209 | |
267 | |
210 | for my $ev (grep /^on_/, keys %args) { |
268 | for my $ev (grep /^on_/, keys %args) { |
211 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
269 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
212 | } |
270 | } |
213 | |
271 | |
… | |
… | |
220 | |
278 | |
221 | $proxy |
279 | $proxy |
222 | } |
280 | } |
223 | |
281 | |
224 | sub template { |
282 | sub template { |
225 | my ($self, $template, @args) = @_; |
283 | my ($self, $type, $template, $args, $done_cb) = @_; |
226 | |
284 | |
227 | my %cfg; |
285 | my %cfg; |
228 | my @res; |
|
|
229 | |
286 | |
230 | while (@args) { |
287 | while (@$args) { |
231 | my ($name, $args) = splice @args, 0, 2, (); |
288 | my ($name, $args) = splice @$args, 0, 2, (); |
232 | |
289 | |
233 | my $proxy = $self->alloc; |
290 | my $proxy = $self->alloc; |
234 | |
291 | |
|
|
292 | $self->{delete $args->{alias} or $name} = $proxy; |
|
|
293 | |
|
|
294 | cf::weaken |
235 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
295 | +($self->{_w}{$proxy->{id}} = $proxy), |
236 | Scalar::Util::weaken ($proxy->{ws} = $self); |
296 | +($proxy->{ws} = $self); |
237 | |
297 | |
238 | for my $ev (grep /^on_/, keys %$args) { |
298 | for my $ev (grep /^on_/, keys %$args) { |
239 | $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); |
299 | $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); |
240 | } |
300 | } |
241 | |
301 | |
242 | $cfg{$name} = { |
302 | $cfg{$name} = { |
243 | %$args, |
303 | %$args, |
244 | id => $proxy->{id}, |
304 | id => $proxy->{id}, |
245 | }; |
305 | }; |
|
|
306 | } |
246 | |
307 | |
247 | push @res, $proxy; |
308 | if ($done_cb) { |
|
|
309 | my $proxy = $self->alloc; |
|
|
310 | my $ocb = $done_cb; |
|
|
311 | $done_cb = $proxy->cb (sub { |
|
|
312 | undef $proxy; |
|
|
313 | undef $done_cb; |
|
|
314 | &$ocb |
|
|
315 | }); |
|
|
316 | } |
|
|
317 | |
|
|
318 | if ($type eq "face") { |
|
|
319 | $self->{ns}->send_face ($template, 100); |
|
|
320 | $self->{ns}->flush_fx; |
248 | } |
321 | } |
249 | |
322 | |
250 | $self->msg (ws_ct => |
323 | $self->msg (ws_ct => |
251 | $self->{id}, |
324 | $self->{id}, |
252 | $template, |
325 | $type => $template, |
|
|
326 | $done_cb, |
253 | \%cfg, |
327 | \%cfg, |
254 | ); |
328 | ); |
255 | |
|
|
256 | @res |
|
|
257 | } |
329 | } |
258 | |
330 | |
259 | sub find { |
331 | sub find { |
260 | my ($self, @names) = @_; |
332 | my ($self, @names) = @_; |
261 | |
333 | |
… | |
… | |
280 | |
352 | |
281 | ############################################################################# |
353 | ############################################################################# |
282 | |
354 | |
283 | package ext::widget::proxy; |
355 | package ext::widget::proxy; |
284 | |
356 | |
285 | sub DESTROY { |
|
|
286 | my ($self) = @_; |
|
|
287 | |
|
|
288 | delete $self->{ns}{widget}{$self->{id}}; |
|
|
289 | |
|
|
290 | if (my $ws = $self->{ws}) { |
|
|
291 | $self->msg (w_c => 0, "destroy"); |
|
|
292 | delete $ws->{_w}{$self->{id}}; |
|
|
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 |
|
|
323 | } |
|
|
324 | |
|
|
325 | sub msg { |
357 | sub msg { |
326 | my ($self, $type, @arg) = @_; |
358 | my ($self, $type, @arg) = @_; |
327 | |
359 | |
328 | if (my $ns = $self->{ns}) { |
360 | if (my $ns = $self->{ns}) { |
329 | my @msg = ($type, $self->{id}, @arg); |
361 | 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)); |
362 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
332 | } |
363 | } |
333 | } |
364 | } |
334 | |
365 | |
335 | sub msg_cb { |
366 | sub msg_cb { |
… | |
… | |
366 | } |
397 | } |
367 | |
398 | |
368 | () |
399 | () |
369 | } |
400 | } |
370 | |
401 | |
|
|
402 | sub DESTROY { |
|
|
403 | my ($self) = @_; |
|
|
404 | |
|
|
405 | delete $self->{ns}{widget}{$self->{id}}; |
|
|
406 | |
|
|
407 | if (my $ws = $self->{ws}) { |
|
|
408 | $self->msg (w_c => 0, "destroy"); |
|
|
409 | delete $ws->{_w}{$self->{id}}; |
|
|
410 | } |
|
|
411 | } |
|
|
412 | |
|
|
413 | sub cb { |
|
|
414 | my ($self, $cb) = @_; |
|
|
415 | |
|
|
416 | my $proxy = bless { |
|
|
417 | ns => $self->{ns}, |
|
|
418 | id => $self->{ns}->alloc_wid, |
|
|
419 | }, "ext::widget::callback"; |
|
|
420 | |
|
|
421 | cf::weaken $proxy->{ns}; |
|
|
422 | |
|
|
423 | $self->{ns}{widget_cb}{$proxy->{id}} = $cb; |
|
|
424 | |
|
|
425 | $proxy |
|
|
426 | } |
|
|
427 | |
|
|
428 | sub oneshot_cb { |
|
|
429 | my ($self, $cb) = @_; |
|
|
430 | |
|
|
431 | if ("CODE" eq ref $cb) { |
|
|
432 | my $ocb = $cb; |
|
|
433 | $cb = cb $self, sub { |
|
|
434 | undef $cb; |
|
|
435 | &$ocb |
|
|
436 | }; |
|
|
437 | } |
|
|
438 | |
|
|
439 | $cb |
|
|
440 | } |
|
|
441 | |
371 | sub set { |
442 | sub set { |
372 | my ($self, @kv) = @_; |
443 | my ($self, @kv) = @_; |
373 | |
444 | |
374 | $self->msg (w_s => \@kv); |
445 | $self->msg (w_s => \@kv); |
375 | } |
446 | } |