ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/widget.ext
(Generate patch)

Comparing deliantra/server/ext/widget.ext (file contents):
Revision 1.20 by root, Thu Dec 27 18:35:48 2007 UTC vs.
Revision 1.25 by root, Sun Jan 13 12:42:55 2008 UTC

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
17 17
18cf::client->attach ( 18cf::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);
23
24 $ns->{id} = "a"; 22 $ns->{id} = "a";
25 $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { 23 $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub {
26 $weakns->{widget}{$_[0]} 24 $ns->{widget}{$_[0]}
27 }); 25 });
26
27 cf::weaken $ns;
28 }, 28 },
29); 29);
30 30
31sub csc_update_stats { 31sub csc_update_stats {
32 my ($ns) = @_; 32 my ($ns) = @_;
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 38
39sub demo_start { 39my $cg_template = eval <<EOF;
40 my ($ns) = @_; 40[
41
42 my $ws = $ns->{csc} = $ns->new_widgetset;
43
44 my ($tl, $entry) = $ws->template (undef,
45 toplevel => {}, 41 Toplevel => {
46 entry => { 42 s_id => "toplevel",
47 text => "xyz", 43 title => "Character Creation",
48 on_changed => sub { 44 x => "center",
49 warn "changed<@_>\n";#d# 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,
50 }, 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 },
51 }, 95 ]}],
52 );
53
54 $tl->show;
55
56 $ns->{xxxw} = [$tl, $entry];#d#
57 96 },
58# $ws->find ("setup_notebook")->add ($ws->{tab}); 97]
59# $ws->find ("setup_dialog")->toggle_visibility; 98EOF
60} 99die if $@;
61 100
62sub csc_start { 101sub csc_start {
63 my ($ns) = @_; 102 my ($ns) = @_;
64 103
65 my $ws = $ns->{csc} = $ns->new_widgetset; 104 my $ws = $ns->{csc} = $ns->new_widgetset;
105 144
106 csc_update_stats $ns; 145 csc_update_stats $ns;
107 146
108 $ws->{tl} = $w; 147 $ws->{tl} = $w;
109 $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;
110} 168}
111 169
112cf::player->attach ( 170cf::player->attach (
113 on_login => sub { 171 on_login => sub {
114 my ($pl) = @_; 172 my ($pl) = @_;
115 173
174 my $ns = $pl->ns;
175 return unless $ns->{can_widget};
176
116 return unless $cf::CFG{devel}; 177 return unless $cf::CFG{devel};
117 178
118 my $ns = $pl->ns;
119
120 return unless $ns->{can_widget};
121 #csc_start $ns; 179 #csc_start $ns;
122 demo_start $ns;
123 }, 180 },
124); 181);
125 182
126cf::register_exticmd w_e => sub { 183cf::register_exticmd w_e => sub {
127 my ($ns, $id, @args) = @_; 184 my ($ns, $id, @args) = @_;
142 id => $id, 199 id => $id,
143 ns => $self, 200 ns => $self,
144 _w => {}, 201 _w => {},
145 }, "ext::widget::set"; 202 }, "ext::widget::set";
146 203
204 cf::weaken $ws->{ns};
205
147 $ws->msg (ws_n => $id); 206 $ws->msg (ws_n => $id);
148 207
149 $ws 208 $ws
150} 209}
151 210
177 236
178sub msg { 237sub msg {
179 my ($self, @msg) = @_; 238 my ($self, @msg) = @_;
180 239
181 if (my $ns = shift->{ns}) { 240 if (my $ns = shift->{ns}) {
241 return unless $ns->{json_coder};#d# might be gone at destroy time(??)
182 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# 242 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
183 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); 243 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
184 } 244 }
185} 245}
186 246
202sub new { 262sub new {
203 my ($self, $class, %args) = @_; 263 my ($self, $class, %args) = @_;
204 264
205 my $proxy = $self->alloc; 265 my $proxy = $self->alloc;
206 266
207 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); 267 cf::weaken
208 Scalar::Util::weaken ($proxy->{ws} = $self); 268 +($self->{_w}{$proxy->{id}} = $proxy),
269 +($proxy->{ws} = $self);
209 270
210 for my $ev (grep /^on_/, keys %args) { 271 for my $ev (grep /^on_/, keys %args) {
211 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); 272 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
212 } 273 }
213 274
220 281
221 $proxy 282 $proxy
222} 283}
223 284
224sub template { 285sub template {
225 my ($self, $template, @args) = @_; 286 my ($self, $type, $template, $args, $done_cb) = @_;
226 287
227 my %cfg; 288 my %cfg;
228 my @res;
229 289
230 while (@args) { 290 while (@$args) {
231 my ($name, $args) = splice @args, 0, 2, (); 291 my ($name, $args) = splice @$args, 0, 2, ();
232 292
233 my $proxy = $self->alloc; 293 my $proxy = $self->alloc;
234 294
295 $self->{delete $args->{alias} or $name} = $proxy;
296
297 cf::weaken
235 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); 298 +($self->{_w}{$proxy->{id}} = $proxy),
236 Scalar::Util::weaken ($proxy->{ws} = $self); 299 +($proxy->{ws} = $self);
237 300
238 for my $ev (grep /^on_/, keys %$args) { 301 for my $ev (grep /^on_/, keys %$args) {
239 $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); 302 $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev});
240 } 303 }
241 304
242 $cfg{$name} = { 305 $cfg{$name} = {
243 %$args, 306 %$args,
244 id => $proxy->{id}, 307 id => $proxy->{id},
245 }; 308 };
309 }
246 310
247 push @res, $proxy; 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;
248 } 324 }
249 325
250 $self->msg (ws_ct => 326 $self->msg (ws_ct =>
251 $self->{id}, 327 $self->{id},
252 $template, 328 $type => $template,
329 $done_cb,
253 \%cfg, 330 \%cfg,
254 ); 331 );
255
256 @res
257} 332}
258 333
259sub find { 334sub find {
260 my ($self, @names) = @_; 335 my ($self, @names) = @_;
261 336
279} 354}
280 355
281############################################################################# 356#############################################################################
282 357
283package ext::widget::proxy; 358package ext::widget::proxy;
284
285sub 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
296sub 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
311sub 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 359
325sub msg { 360sub msg {
326 my ($self, $type, @arg) = @_; 361 my ($self, $type, @arg) = @_;
327 362
328 if (my $ns = $self->{ns}) { 363 if (my $ns = $self->{ns}) {
366 } 401 }
367 402
368 () 403 ()
369} 404}
370 405
406sub 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
417sub 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
432sub 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
371sub set { 446sub set {
372 my ($self, @kv) = @_; 447 my ($self, @kv) = @_;
373 448
374 $self->msg (w_s => \@kv); 449 $self->msg (w_s => \@kv);
375} 450}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines