… | |
… | |
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 |
… | |
… | |
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 ("\fw" => sub { |
23 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
25 | $weakns->{widget}{$_[0]} |
24 | $ns->{widget}{$_[0]} |
26 | }); |
25 | }); |
|
|
26 | |
|
|
27 | cf::weaken $ns; |
27 | }, |
28 | }, |
28 | ); |
29 | ); |
29 | |
30 | |
30 | sub csc_update_stats { |
31 | sub csc_update_stats { |
31 | my ($ns) = @_; |
32 | my ($ns) = @_; |
… | |
… | |
33 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
34 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
34 | $v->set_text ($ns->pl->ob->stats->$k); |
35 | $v->set_text ($ns->pl->ob->stats->$k); |
35 | } |
36 | } |
36 | } |
37 | } |
37 | |
38 | |
38 | sub demo_start { |
39 | my $cg_template = eval <<EOF; |
39 | my ($ns) = @_; |
40 | [ |
40 | |
41 | Toplevel => { |
41 | my $ws = $ns->{csc} = $ns->new_widgetset; |
42 | s_id => "toplevel", |
42 | |
43 | title => "Character Creation", |
43 | $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 | ]}], |
44 | |
96 | }, |
45 | $ws->find ("setup_notebook")->add ($ws->{tab}); |
97 | ] |
46 | $ws->find ("setup_dialog")->toggle_visibility; |
98 | EOF |
47 | } |
99 | die if $@; |
48 | |
100 | |
49 | sub csc_start { |
101 | sub csc_start { |
50 | my ($ns) = @_; |
102 | my ($ns) = @_; |
51 | |
103 | |
52 | my $ws = $ns->{csc} = $ns->new_widgetset; |
104 | my $ws = $ns->{csc} = $ns->new_widgetset; |
… | |
… | |
92 | |
144 | |
93 | csc_update_stats $ns; |
145 | csc_update_stats $ns; |
94 | |
146 | |
95 | $ws->{tl} = $w; |
147 | $ws->{tl} = $w; |
96 | $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; |
97 | } |
168 | } |
98 | |
169 | |
99 | cf::player->attach ( |
170 | cf::player->attach ( |
100 | on_login => sub { |
171 | on_login => sub { |
101 | my ($pl) = @_; |
172 | my ($pl) = @_; |
102 | |
173 | |
|
|
174 | my $ns = $pl->ns; |
|
|
175 | return unless $ns->{can_widget}; |
|
|
176 | |
103 | return unless $cf::CFG{devel}; |
177 | return unless $cf::CFG{devel}; |
104 | |
178 | |
105 | my $ns = $pl->ns; |
|
|
106 | |
|
|
107 | return unless $ns->{can_widget}; |
|
|
108 | #csc_start $ns; |
179 | #csc_start $ns; |
109 | #demo_start $ns; |
|
|
110 | }, |
180 | }, |
111 | ); |
181 | ); |
112 | |
182 | |
113 | cf::register_exticmd w_e => sub { |
183 | cf::register_exticmd w_e => sub { |
114 | my ($ns, $id, @args) = @_; |
184 | my ($ns, $id, @args) = @_; |
… | |
… | |
129 | id => $id, |
199 | id => $id, |
130 | ns => $self, |
200 | ns => $self, |
131 | _w => {}, |
201 | _w => {}, |
132 | }, "ext::widget::set"; |
202 | }, "ext::widget::set"; |
133 | |
203 | |
|
|
204 | cf::weaken $ws->{ns}; |
|
|
205 | |
134 | $ws->msg (ws_n => $id); |
206 | $ws->msg (ws_n => $id); |
135 | |
207 | |
136 | $ws |
208 | $ws |
137 | } |
209 | } |
138 | |
210 | |
… | |
… | |
164 | |
236 | |
165 | sub msg { |
237 | sub msg { |
166 | my ($self, @msg) = @_; |
238 | my ($self, @msg) = @_; |
167 | |
239 | |
168 | if (my $ns = shift->{ns}) { |
240 | if (my $ns = shift->{ns}) { |
|
|
241 | return unless $ns->{json_coder};#d# |
169 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
242 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
170 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
243 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
171 | } |
244 | } |
172 | } |
245 | } |
173 | |
246 | |
… | |
… | |
189 | sub new { |
262 | sub new { |
190 | my ($self, $class, %args) = @_; |
263 | my ($self, $class, %args) = @_; |
191 | |
264 | |
192 | my $proxy = $self->alloc; |
265 | my $proxy = $self->alloc; |
193 | |
266 | |
194 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
267 | cf::weaken |
195 | Scalar::Util::weaken ($proxy->{ws} = $self); |
268 | +($self->{_w}{$proxy->{id}} = $proxy), |
|
|
269 | +($proxy->{ws} = $self); |
196 | |
270 | |
197 | for my $ev (grep /^on_/, keys %args) { |
271 | for my $ev (grep /^on_/, keys %args) { |
198 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
272 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
199 | } |
273 | } |
200 | |
274 | |
… | |
… | |
206 | ); |
280 | ); |
207 | |
281 | |
208 | $proxy |
282 | $proxy |
209 | } |
283 | } |
210 | |
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 | |
211 | sub find { |
334 | sub find { |
212 | my ($self, @names) = @_; |
335 | my ($self, @names) = @_; |
213 | |
336 | |
214 | my @res; |
337 | my @res; |
215 | my @alloc; |
338 | my @alloc; |
… | |
… | |
231 | } |
354 | } |
232 | |
355 | |
233 | ############################################################################# |
356 | ############################################################################# |
234 | |
357 | |
235 | package ext::widget::proxy; |
358 | package ext::widget::proxy; |
236 | |
|
|
237 | sub DESTROY { |
|
|
238 | my ($self) = @_; |
|
|
239 | |
|
|
240 | delete $self->{ns}{widget}{$self->{id}}; |
|
|
241 | |
|
|
242 | if (my $ws = $self->{ws}) { |
|
|
243 | $self->msg (w_c => 0, "destroy"); |
|
|
244 | delete $ws->{_w}{$self->{id}}; |
|
|
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 |
|
|
275 | } |
|
|
276 | |
359 | |
277 | sub msg { |
360 | sub msg { |
278 | my ($self, $type, @arg) = @_; |
361 | my ($self, $type, @arg) = @_; |
279 | |
362 | |
280 | if (my $ns = $self->{ns}) { |
363 | if (my $ns = $self->{ns}) { |
… | |
… | |
318 | } |
401 | } |
319 | |
402 | |
320 | () |
403 | () |
321 | } |
404 | } |
322 | |
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 | |
323 | sub set { |
446 | sub set { |
324 | my ($self, @kv) = @_; |
447 | my ($self, @kv) = @_; |
325 | |
448 | |
326 | $self->msg (w_s => \@kv); |
449 | $self->msg (w_s => \@kv); |
327 | } |
450 | } |