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.18 by root, Sun Aug 19 09:27:08 2007 UTC vs.
Revision 1.23 by root, Sat Dec 29 21:07:23 2007 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 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
13# w_r rid res # widget call return
14# w_e id rid @args # widget_event 14# w_e id @args # widget_call
15 15
16our $DEBUG = 1; 16our $DEBUG = 1;
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 (__w_ => sub { 23 $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub {
26 # cannot deserialise ATM 24 $ns->{widget}{$_[0]}
27 undef
28 }); 25 });
26
27 cf::weaken $ns;
29 }, 28 },
30); 29);
31 30
32sub csc_update_stats { 31sub csc_update_stats {
33 my ($ns) = @_; 32 my ($ns) = @_;
35 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { 34 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) {
36 $v->set_text ($ns->pl->ob->stats->$k); 35 $v->set_text ($ns->pl->ob->stats->$k);
37 } 36 }
38} 37}
39 38
40sub demo_start { 39my $cg_template = eval <<EOF;
41 my ($ns) = @_; 40[
42 41 Toplevel => {
43 my $ws = $ns->{csc} = $ns->new_widgetset; 42 s_id => "toplevel",
44 43 title => "Character Creation",
45 $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 ]}],
46 96 },
47 $ws->find ("setup_notebook")->add ($ws->{tab}); 97]
48 $ws->find ("setup_dialog")->toggle_visibility; 98EOF
49} 99die if $@;
50 100
51sub csc_start { 101sub csc_start {
52 my ($ns) = @_; 102 my ($ns) = @_;
53 103
54 my $ws = $ns->{csc} = $ns->new_widgetset; 104 my $ws = $ns->{csc} = $ns->new_widgetset;
94 144
95 csc_update_stats $ns; 145 csc_update_stats $ns;
96 146
97 $ws->{tl} = $w; 147 $ws->{tl} = $w;
98 $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;
99} 168}
100 169
101cf::player->attach ( 170cf::player->attach (
102 on_login => sub { 171 on_login => sub {
103 my ($pl) = @_; 172 my ($pl) = @_;
104 173
174 my $ns = $pl->ns;
175 return unless $ns->{can_widget};
176
105 return unless $cf::CFG{devel}; 177 return unless $cf::CFG{devel};
106 178
107 my $ns = $pl->ns;
108
109 return unless $ns->{can_widget};
110 #csc_start $ns; 179 #csc_start $ns;
111 #demo_start $ns;
112 }, 180 },
113); 181);
114 182
115cf::register_exticmd w_e => sub { 183cf::register_exticmd w_e => sub {
116 my ($ns, $id, $rid, @args) = @_; 184 my ($ns, $id, @args) = @_;
117 185
118 if (my $w = $ns->{widget}{$id}) { 186 if (my $cb = $ns->{widget_cb}{$id}) {
119 if (my $cb = $w->{ev}{$rid}) {
120 $cb->($w, @args);
121 }
122 }
123
124 ()
125};
126
127cf::register_exticmd w_r => sub {
128 my ($ns, $rid, $res) = @_;
129
130 if (my $cb = delete $ns->{widget_return}{$rid}) {
131 $cb->(@$res); 187 $cb->(@args);
132 } 188 }
133 189
134 () 190 ()
135}; 191};
136 192
143 id => $id, 199 id => $id,
144 ns => $self, 200 ns => $self,
145 _w => {}, 201 _w => {},
146 }, "ext::widget::set"; 202 }, "ext::widget::set";
147 203
204 cf::weaken $ws->{ns};
205
148 $ws->msg (ws_n => $id); 206 $ws->msg (ws_n => $id);
149 207
150 $ws 208 $ws
209}
210
211sub cf::client::alloc_wid {
212 pop @{ $_[0]{ids} }
213 or ++$_[0]{id}
214}
215
216sub cf::client::free_wid {
217 push @{ $_[0]{ids} }, $_[1];
151} 218}
152 219
153############################################################################# 220#############################################################################
154 221
155package ext::widget::set; 222package ext::widget::set;
177} 244}
178 245
179sub alloc { 246sub alloc {
180 my ($self) = @_; 247 my ($self) = @_;
181 248
182 my $id = ++$self->{ns}{id}; 249 my $id = $self->{ns}->alloc_wid;
183 250
184 my $proxy = bless { 251 my $proxy = bless {
185 id => $id, 252 id => $id,
186 }, "ext::widget::proxy"; 253 }, "ext::widget::proxy";
187 254
194sub new { 261sub new {
195 my ($self, $class, %args) = @_; 262 my ($self, $class, %args) = @_;
196 263
197 my $proxy = $self->alloc; 264 my $proxy = $self->alloc;
198 265
199 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); 266 cf::weaken
200 Scalar::Util::weaken ($proxy->{ws} = $self); 267 +($self->{_w}{$proxy->{id}} = $proxy),
268 +($proxy->{ws} = $self);
201 269
202 for my $ev (grep /^on_/, keys %args) { 270 for my $ev (grep /^on_/, keys %args) {
203 my $rid = ++$self->{ns}{id}; 271 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
204 $proxy->{ev}{$rid} = $args{$ev};
205 $args{$ev} = $rid;
206 } 272 }
207 273
208 $self->msg (ws_c => 274 $self->msg (ws_c =>
209 $self->{id}, 275 $self->{id},
210 $proxy->{id}, 276 $proxy->{id},
213 ); 279 );
214 280
215 $proxy 281 $proxy
216} 282}
217 283
284sub template {
285 my ($self, $type, $template, $args, $done_cb) = @_;
286
287 my %cfg;
288
289 while (@$args) {
290 my ($name, $args) = splice @$args, 0, 2, ();
291
292 my $proxy = $self->alloc;
293
294 $self->{delete $args->{alias} or $name} = $proxy;
295
296 cf::weaken
297 +($self->{_w}{$proxy->{id}} = $proxy),
298 +($proxy->{ws} = $self);
299
300 for my $ev (grep /^on_/, keys %$args) {
301 $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev});
302 }
303
304 $cfg{$name} = {
305 %$args,
306 id => $proxy->{id},
307 };
308 }
309
310 if ($done_cb) {
311 my $proxy = $self->alloc;
312 my $ocb = $done_cb;
313 $done_cb = $proxy->cb (sub {
314 undef $proxy;
315 undef $done_cb;
316 &$ocb
317 });
318 }
319
320 if ($type eq "face") {
321 $self->{ns}->send_face ($template, 100);
322 $self->{ns}->flush_fx;
323 }
324
325 $self->msg (ws_ct =>
326 $self->{id},
327 $type => $template,
328 $done_cb,
329 \%cfg,
330 );
331}
332
218sub find { 333sub find {
219 my ($self, @names) = @_; 334 my ($self, @names) = @_;
220 335
221 my @res; 336 my @res;
222 my @alloc; 337 my @alloc;
238} 353}
239 354
240############################################################################# 355#############################################################################
241 356
242package ext::widget::proxy; 357package ext::widget::proxy;
243
244sub DESTROY {
245 my ($self) = @_;
246
247 delete $self->{ns}{widget}{$self->{id}};
248
249 if (my $ws = $self->{ws}) {
250 $self->msg (w_c => 0, "destroy");
251 delete $ws->{_w}{$self->{id}};
252 }
253}
254 358
255sub msg { 359sub msg {
256 my ($self, $type, @arg) = @_; 360 my ($self, $type, @arg) = @_;
257 361
258 if (my $ns = $self->{ns}) { 362 if (my $ns = $self->{ns}) {
264 368
265sub msg_cb { 369sub msg_cb {
266 my ($self, $cb, $type, @arg) = @_; 370 my ($self, $cb, $type, @arg) = @_;
267 371
268 if (my $ws = $self->{ws}) { 372 if (my $ws = $self->{ws}) {
269 my $rid = ++$ws->{ns}{id}; 373 my $rid = $ws->{ns}->alloc_wid;
270
271 $self->msg ($type, $rid, @arg);
272 374
273 if ($cb) { 375 if ($cb) {
274 $ws->{ns}{widget_return}{$rid} = $cb; 376 $ws->{ns}{widget_cb}{$rid} = sub {
377 delete $ws->{ns}{widget_cb}{$rid};
378 $ws->{ns}->free_wid ($rid);
379 &$cb
380 };
381
382 $self->msg ($type, $rid, @arg);
275 } else { 383 } else {
276 # synchronous case 384 # synchronous case
277 my $wait = new Coro::Signal; 385 my $wait = new Coro::Signal;
278 my @res; 386 my @res;
279 387
280 $ws->{ns}{widget_return}{$rid} = sub { 388 $ws->{ns}{widget_cb}{$rid} = sub {
389 delete $ws->{ns}{widget_cb}{$rid};
390 $ws->{ns}->free_wid ($rid);
391
281 @res = @_; 392 @res = @_;
282 $wait->send; 393 $wait->send;
283 }; 394 };
395 $self->msg ($type, $rid, @arg);
284 $wait->wait; 396 $wait->wait;
285 397
286 return @res; 398 return @res;
287 } 399 }
288 } 400 }
289 401
290 () 402 ()
291} 403}
292 404
405sub DESTROY {
406 my ($self) = @_;
407
408 delete $self->{ns}{widget}{$self->{id}};
409
410 if (my $ws = $self->{ws}) {
411 $self->msg (w_c => 0, "destroy");
412 delete $ws->{_w}{$self->{id}};
413 }
414}
415
416sub cb {
417 my ($self, $cb) = @_;
418
419 my $proxy = bless {
420 ns => $self->{ns},
421 id => $self->{ns}->alloc_wid,
422 }, "ext::widget::callback";
423
424 cf::weaken $proxy->{ns};
425
426 $self->{ns}{widget_cb}{$proxy->{id}} = $cb;
427
428 $proxy
429}
430
431sub oneshot_cb {
432 my ($self, $cb) = @_;
433
434 if ("CODE" eq ref $cb) {
435 my $ocb = $cb;
436 $cb = cb $self, sub {
437 undef $cb;
438 &$ocb
439 };
440 }
441
442 $cb
443}
444
293sub set { 445sub set {
294 my ($self, @kv) = @_; 446 my ($self, @kv) = @_;
295 447
296 $self->msg (w_s => \@kv); 448 $self->msg (w_s => \@kv);
297} 449}
298 450
299sub get { 451sub get {
300 my ($self, $member, $cb) = @_; 452 my ($self, $member, $cb) = @_;
301 453
302 $self->msg_cb ($cb, w_g => [$member]); 454 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
303} 455}
304 456
305sub TO_JSON { 457sub TO_JSON {
306 { __w_ => $_[0]{id} } 458 { "\fw" => $_[0]{id} }
307} 459}
308 460
309our $AUTOLOAD; 461our $AUTOLOAD;
310 462
311sub AUTOLOAD { 463sub AUTOLOAD {
318 $self->msg (w_c => 0, $AUTOLOAD, @_); 470 $self->msg (w_c => 0, $AUTOLOAD, @_);
319 471
320 () 472 ()
321} 473}
322 474
475package ext::widget::callback;
476
477sub DESTROY {
478 my ($self) = @_;
479
480 if (my $ns = $self->{ns}) {
481 delete $ns->{widget_cb}{$self->{id}};
482 $ns->free_wid ($self->{id});
483 }
484}
485
486sub TO_JSON {
487 { "\fc" => $_[0]{id} }
488}
489

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines