1 |
#! perl # mandatory depends=login |
2 |
|
3 |
# sends the following ext message types |
4 |
# ws_a id name... # associate well-known widget with given id |
5 |
# ws_n ws # widgetset new |
6 |
# ws_d ws # widgetset destroy |
7 |
# ws_c ws id class @args # widgetset create |
8 |
# ws_ct ws ttype ttext done_cb \%cfg # widgetset create from template |
9 |
# w_c id rid name @args # widget method call |
10 |
# w_s id @attr # widget member set |
11 |
# w_g id rid @attr # widget member get |
12 |
# |
13 |
# and expects the following exti message types |
14 |
# w_e id @args # widget_call |
15 |
|
16 |
cf::client->attach ( |
17 |
on_connect => sub { |
18 |
my ($ns) = @_; |
19 |
|
20 |
$ns->{id} = "a"; |
21 |
$ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
22 |
$ns->{widget}{$_[0]} |
23 |
}); |
24 |
|
25 |
cf::weaken $ns; |
26 |
}, |
27 |
); |
28 |
|
29 |
sub csc_update_stats { |
30 |
my ($ns) = @_; |
31 |
|
32 |
while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
33 |
$v->set_text ($ns->pl->ob->stats->$k); |
34 |
} |
35 |
} |
36 |
|
37 |
my $cg_template = eval <<EOF; |
38 |
[ |
39 |
Toplevel => { |
40 |
s_id => "toplevel", |
41 |
title => "Character Creation", |
42 |
x => "center", |
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, |
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 |
}, |
93 |
]}], |
94 |
}, |
95 |
] |
96 |
EOF |
97 |
die if $@; |
98 |
|
99 |
sub csc_start { |
100 |
my ($ns) = @_; |
101 |
|
102 |
my $ws = $ns->{csc} = $ns->new_widgetset; |
103 |
|
104 |
my $w = $ws->new (Toplevel => |
105 |
min_w => 600, |
106 |
min_h => 400, |
107 |
x => "center", |
108 |
y => "center", |
109 |
title => "Character Creation", |
110 |
has_close_button => 1, |
111 |
on_delete => sub { |
112 |
$ws->destroy; |
113 |
}, |
114 |
); |
115 |
|
116 |
$w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
117 |
|
118 |
$ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); |
119 |
|
120 |
$stats->add_at (0, 0, (my $statstable = $ws->new ("Table"))); |
121 |
|
122 |
for ( |
123 |
[0, "Str"], |
124 |
[1, "Dex"], |
125 |
[2, "Con"], |
126 |
[3, "Int"], |
127 |
[4, "Wis"], |
128 |
[5, "Pow"], |
129 |
[6, "Cha"], |
130 |
) { |
131 |
my ($x, $label) = @$_; |
132 |
|
133 |
$statstable->add_at ($x, 0, $ws->new (Label => |
134 |
can_hover => 1, can_events => 1, |
135 |
align => +1, text => $label, tooltip => "#stat_$label", |
136 |
)); |
137 |
$statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
138 |
can_hover => 1, can_events => 1, |
139 |
align => +1, template => "88", tooltip => "#stat_$label", |
140 |
)); |
141 |
} |
142 |
|
143 |
csc_update_stats $ns; |
144 |
|
145 |
$ws->{tl} = $w; |
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; |
166 |
} |
167 |
|
168 |
cf::player->attach ( |
169 |
on_login => sub { |
170 |
my ($pl) = @_; |
171 |
|
172 |
my $ns = $pl->ns; |
173 |
return unless $ns->{can_widget}; |
174 |
|
175 |
return unless $cf::CFG{devel}; |
176 |
|
177 |
#csc_start $ns; |
178 |
}, |
179 |
); |
180 |
|
181 |
cf::register_exticmd w_e => sub { |
182 |
my ($ns, $id, @args) = @_; |
183 |
|
184 |
if (my $cb = $ns->{widget_cb}{$id}) { |
185 |
$cb->(@args); |
186 |
} |
187 |
|
188 |
() |
189 |
}; |
190 |
|
191 |
sub cf::client::new_widgetset { |
192 |
my ($self) = @_; |
193 |
|
194 |
my $id = ++$self->{id}; |
195 |
|
196 |
my $ws = bless { |
197 |
id => $id, |
198 |
ns => $self, |
199 |
_w => {}, |
200 |
}, "ext::widget::set"; |
201 |
|
202 |
cf::weaken $ws->{ns}; |
203 |
|
204 |
$ws->msg (ws_n => $id); |
205 |
|
206 |
$ws |
207 |
} |
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 |
|
218 |
############################################################################# |
219 |
|
220 |
package ext::widget::set; |
221 |
|
222 |
sub DESTROY { |
223 |
$_[0]->destroy; |
224 |
} |
225 |
|
226 |
sub destroy { |
227 |
my ($self) = @_; |
228 |
|
229 |
$self->msg (ws_d => $self->{id}); |
230 |
delete $self->{ns}; |
231 |
$_->destroy |
232 |
for values %{ $self->{w} }; |
233 |
} |
234 |
|
235 |
sub msg { |
236 |
my ($self, @msg) = @_; |
237 |
|
238 |
if (my $ns = shift->{ns}) { |
239 |
return unless $ns->{json_coder};#d# might be gone at destroy time(??) |
240 |
$ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
241 |
} |
242 |
} |
243 |
|
244 |
sub alloc { |
245 |
my ($self) = @_; |
246 |
|
247 |
my $id = $self->{ns}->alloc_wid; |
248 |
|
249 |
my $proxy = bless { |
250 |
id => $id, |
251 |
}, "ext::widget::proxy"; |
252 |
|
253 |
cf::weaken ($proxy->{ns} = $self->{ns}); |
254 |
cf::weaken ($self->{ns}{widget}{$id} = $proxy); |
255 |
|
256 |
$proxy |
257 |
} |
258 |
|
259 |
sub new { |
260 |
my ($self, $class, %args) = @_; |
261 |
|
262 |
my $proxy = $self->alloc; |
263 |
|
264 |
cf::weaken |
265 |
+($self->{_w}{$proxy->{id}} = $proxy), |
266 |
+($proxy->{ws} = $self); |
267 |
|
268 |
for my $ev (grep /^on_/, keys %args) { |
269 |
$args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
270 |
} |
271 |
|
272 |
$self->msg (ws_c => |
273 |
$self->{id}, |
274 |
$proxy->{id}, |
275 |
$class, |
276 |
\%args, |
277 |
); |
278 |
|
279 |
$proxy |
280 |
} |
281 |
|
282 |
sub template { |
283 |
my ($self, $type, $template, $args, $done_cb) = @_; |
284 |
|
285 |
my %cfg; |
286 |
|
287 |
while (@$args) { |
288 |
my ($name, $args) = splice @$args, 0, 2, (); |
289 |
|
290 |
my $proxy = $self->alloc; |
291 |
|
292 |
$self->{delete $args->{alias} or $name} = $proxy; |
293 |
|
294 |
cf::weaken |
295 |
+($self->{_w}{$proxy->{id}} = $proxy), |
296 |
+($proxy->{ws} = $self); |
297 |
|
298 |
for my $ev (grep /^on_/, keys %$args) { |
299 |
$args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); |
300 |
} |
301 |
|
302 |
$cfg{$name} = { |
303 |
%$args, |
304 |
id => $proxy->{id}, |
305 |
}; |
306 |
} |
307 |
|
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; |
321 |
} |
322 |
|
323 |
$self->msg (ws_ct => |
324 |
$self->{id}, |
325 |
$type => $template, |
326 |
$done_cb, |
327 |
\%cfg, |
328 |
); |
329 |
} |
330 |
|
331 |
sub find { |
332 |
my ($self, @names) = @_; |
333 |
|
334 |
my @res; |
335 |
my @alloc; |
336 |
|
337 |
for my $name (@names) { |
338 |
push @res, $self->{ns}{widget_wkw}{$name} ||= do { |
339 |
my $proxy = $self->alloc; |
340 |
|
341 |
push @alloc, $proxy->{id} => $name; |
342 |
|
343 |
$proxy |
344 |
}; |
345 |
} |
346 |
|
347 |
$self->msg (ws_a => @alloc) |
348 |
if @alloc; |
349 |
|
350 |
wantarray ? @res : $res[0] |
351 |
} |
352 |
|
353 |
############################################################################# |
354 |
|
355 |
package ext::widget::proxy; |
356 |
|
357 |
sub msg { |
358 |
my ($self, $type, @arg) = @_; |
359 |
|
360 |
if (my $ns = $self->{ns}) { |
361 |
my @msg = ($type, $self->{id}, @arg); |
362 |
$ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
363 |
} |
364 |
} |
365 |
|
366 |
sub msg_cb { |
367 |
my ($self, $cb, $type, @arg) = @_; |
368 |
|
369 |
if (my $ws = $self->{ws}) { |
370 |
my $rid = $ws->{ns}->alloc_wid; |
371 |
|
372 |
if ($cb) { |
373 |
$ws->{ns}{widget_cb}{$rid} = sub { |
374 |
delete $ws->{ns}{widget_cb}{$rid}; |
375 |
$ws->{ns}->free_wid ($rid); |
376 |
&$cb |
377 |
}; |
378 |
|
379 |
$self->msg ($type, $rid, @arg); |
380 |
} else { |
381 |
# synchronous case |
382 |
my $wait = new Coro::Signal; |
383 |
my @res; |
384 |
|
385 |
$ws->{ns}{widget_cb}{$rid} = sub { |
386 |
delete $ws->{ns}{widget_cb}{$rid}; |
387 |
$ws->{ns}->free_wid ($rid); |
388 |
|
389 |
@res = @_; |
390 |
$wait->send; |
391 |
}; |
392 |
$self->msg ($type, $rid, @arg); |
393 |
$wait->wait; |
394 |
|
395 |
return @res; |
396 |
} |
397 |
} |
398 |
|
399 |
() |
400 |
} |
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 |
|
442 |
sub set { |
443 |
my ($self, @kv) = @_; |
444 |
|
445 |
$self->msg (w_s => \@kv); |
446 |
} |
447 |
|
448 |
sub get { |
449 |
my ($self, $member, $cb) = @_; |
450 |
|
451 |
$self->msg_cb ($cb, w_g => ref $member ? @$member : $member); |
452 |
} |
453 |
|
454 |
sub TO_JSON { |
455 |
{ "\fw" => $_[0]{id} } |
456 |
} |
457 |
|
458 |
our $AUTOLOAD; |
459 |
|
460 |
sub AUTOLOAD { |
461 |
$AUTOLOAD =~ s/^.*:// |
462 |
or return; |
463 |
|
464 |
my $self = shift; |
465 |
|
466 |
#TODO: handle non-void context |
467 |
$self->msg (w_c => 0, $AUTOLOAD, @_); |
468 |
|
469 |
() |
470 |
} |
471 |
|
472 |
package ext::widget::callback; |
473 |
|
474 |
sub DESTROY { |
475 |
my ($self) = @_; |
476 |
|
477 |
if (my $ns = $self->{ns}) { |
478 |
delete $ns->{widget_cb}{$self->{id}}; |
479 |
$ns->free_wid ($self->{id}); |
480 |
} |
481 |
} |
482 |
|
483 |
sub TO_JSON { |
484 |
{ "\fc" => $_[0]{id} } |
485 |
} |
486 |
|