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.21 by root, Thu Dec 27 19:41:26 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); 22 Scalar::Util::weaken (my $weakns = $ns);
23 23
24 $ns->{id} = "a"; 24 $ns->{id} = "a";
25 $ns->{json_coder}->filter_json_single_key_object (__w_ => sub { 25 $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub {
26 # cannot deserialise ATM 26 $weakns->{widget}{$_[0]}
27 undef
28 }); 27 });
29 }, 28 },
30); 29);
31 30
32sub csc_update_stats { 31sub csc_update_stats {
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
39my $cg_template = eval <<EOF;
40[
41 Toplevel => {
42 s_id => "toplevel",
43 title => "Character Creation",
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 ]}],
96 },
97]
98EOF
99die if $@;
100
40sub demo_start { 101sub demo_start {
41 my ($ns) = @_; 102 my ($ns) = @_;
42 103
43 my $ws = $ns->{csc} = $ns->new_widgetset; 104 my $ws = $ns->{csc} = $ns->new_widgetset;
44 105
45 $ws->{tab} = $ws->new (Label => text => "dumb tst", c_tab => ["hull"]); 106 my ($tl, $entry) = $ws->template (inline => $cg_template,
107 [
108 toplevel => {},
109 entry => {
110 text => "xyz",
111 on_changed => sub {
112 warn "changed<@_>\n";#d#
113 },
114 },
115 ],
116 );
117
118 $tl->show;
119
120 $ns->{xxxw} = [$tl, $entry];#d#
46 121
47 $ws->find ("setup_notebook")->add ($ws->{tab}); 122# $ws->find ("setup_notebook")->add ($ws->{tab});
48 $ws->find ("setup_dialog")->toggle_visibility; 123# $ws->find ("setup_dialog")->toggle_visibility;
49} 124}
50 125
51sub csc_start { 126sub csc_start {
52 my ($ns) = @_; 127 my ($ns) = @_;
53 128
106 181
107 my $ns = $pl->ns; 182 my $ns = $pl->ns;
108 183
109 return unless $ns->{can_widget}; 184 return unless $ns->{can_widget};
110 #csc_start $ns; 185 #csc_start $ns;
111 #demo_start $ns; 186 demo_start $ns;
112 }, 187 },
113); 188);
114 189
115cf::register_exticmd w_e => sub { 190cf::register_exticmd w_e => sub {
116 my ($ns, $id, $rid, @args) = @_; 191 my ($ns, $id, @args) = @_;
117 192
118 if (my $w = $ns->{widget}{$id}) { 193 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); 194 $cb->(@args);
132 } 195 }
133 196
134 () 197 ()
135}; 198};
136 199
148 $ws->msg (ws_n => $id); 211 $ws->msg (ws_n => $id);
149 212
150 $ws 213 $ws
151} 214}
152 215
216sub cf::client::alloc_wid {
217 pop @{ $_[0]{ids} }
218 or ++$_[0]{id}
219}
220
221sub cf::client::free_wid {
222 push @{ $_[0]{ids} }, $_[1];
223}
224
153############################################################################# 225#############################################################################
154 226
155package ext::widget::set; 227package ext::widget::set;
156 228
157sub DESTROY { 229sub DESTROY {
177} 249}
178 250
179sub alloc { 251sub alloc {
180 my ($self) = @_; 252 my ($self) = @_;
181 253
182 my $id = ++$self->{ns}{id}; 254 my $id = $self->{ns}->alloc_wid;
183 255
184 my $proxy = bless { 256 my $proxy = bless {
185 id => $id, 257 id => $id,
186 }, "ext::widget::proxy"; 258 }, "ext::widget::proxy";
187 259
198 270
199 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); 271 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
200 Scalar::Util::weaken ($proxy->{ws} = $self); 272 Scalar::Util::weaken ($proxy->{ws} = $self);
201 273
202 for my $ev (grep /^on_/, keys %args) { 274 for my $ev (grep /^on_/, keys %args) {
203 my $rid = ++$self->{ns}{id}; 275 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
204 $proxy->{ev}{$rid} = $args{$ev};
205 $args{$ev} = $rid;
206 } 276 }
207 277
208 $self->msg (ws_c => 278 $self->msg (ws_c =>
209 $self->{id}, 279 $self->{id},
210 $proxy->{id}, 280 $proxy->{id},
213 ); 283 );
214 284
215 $proxy 285 $proxy
216} 286}
217 287
288sub template {
289 my ($self, $type, $template, $args, $done_cb) = @_;
290
291 my %cfg;
292 my @res;
293
294 while (@$args) {
295 my ($name, $args) = splice @$args, 0, 2, ();
296
297 my $proxy = $self->alloc;
298
299 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
300 Scalar::Util::weaken ($proxy->{ws} = $self);
301
302 for my $ev (grep /^on_/, keys %$args) {
303 $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev});
304 }
305
306 $cfg{$name} = {
307 %$args,
308 id => $proxy->{id},
309 };
310
311 push @res, $proxy;
312 }
313
314 if ($done_cb) {
315 my $proxy = $self->alloc;
316 my $ocb = $done_cb;
317 $done_cb = $proxy->cb (sub {
318 undef $proxy;
319 undef $done_cb;
320 &$ocb
321 });
322 }
323
324 $self->msg (ws_ct =>
325 $self->{id},
326 $type => $template,
327 $done_cb,
328 \%cfg,
329 );
330
331 @res
332}
333
218sub find { 334sub find {
219 my ($self, @names) = @_; 335 my ($self, @names) = @_;
220 336
221 my @res; 337 my @res;
222 my @alloc; 338 my @alloc;
248 364
249 if (my $ws = $self->{ws}) { 365 if (my $ws = $self->{ws}) {
250 $self->msg (w_c => 0, "destroy"); 366 $self->msg (w_c => 0, "destroy");
251 delete $ws->{_w}{$self->{id}}; 367 delete $ws->{_w}{$self->{id}};
252 } 368 }
369}
370
371sub cb {
372 my ($self, $cb) = @_;
373
374 my $proxy = bless {
375 ns => $self->{ns},
376 id => $self->{ns}->alloc_wid,
377 }, "ext::widget::callback";
378
379 Scalar::Util::weaken $proxy->{ns};
380
381 $self->{ns}{widget_cb}{$proxy->{id}} = $cb;
382
383 $proxy
384}
385
386sub oneshot_cb {
387 my ($self, $cb) = @_;
388
389 if ("CODE" eq ref $cb) {
390 my $ocb = $cb;
391 $cb = $self->cb (sub {
392 undef $cb;
393 &$ocb
394 });
395 }
396
397 $cb
253} 398}
254 399
255sub msg { 400sub msg {
256 my ($self, $type, @arg) = @_; 401 my ($self, $type, @arg) = @_;
257 402
264 409
265sub msg_cb { 410sub msg_cb {
266 my ($self, $cb, $type, @arg) = @_; 411 my ($self, $cb, $type, @arg) = @_;
267 412
268 if (my $ws = $self->{ws}) { 413 if (my $ws = $self->{ws}) {
269 my $rid = ++$ws->{ns}{id}; 414 my $rid = $ws->{ns}->alloc_wid;
270
271 $self->msg ($type, $rid, @arg);
272 415
273 if ($cb) { 416 if ($cb) {
274 $ws->{ns}{widget_return}{$rid} = $cb; 417 $ws->{ns}{widget_cb}{$rid} = sub {
418 delete $ws->{ns}{widget_cb}{$rid};
419 $ws->{ns}->free_wid ($rid);
420 &$cb
421 };
422
423 $self->msg ($type, $rid, @arg);
275 } else { 424 } else {
276 # synchronous case 425 # synchronous case
277 my $wait = new Coro::Signal; 426 my $wait = new Coro::Signal;
278 my @res; 427 my @res;
279 428
280 $ws->{ns}{widget_return}{$rid} = sub { 429 $ws->{ns}{widget_cb}{$rid} = sub {
430 delete $ws->{ns}{widget_cb}{$rid};
431 $ws->{ns}->free_wid ($rid);
432
281 @res = @_; 433 @res = @_;
282 $wait->send; 434 $wait->send;
283 }; 435 };
436 $self->msg ($type, $rid, @arg);
284 $wait->wait; 437 $wait->wait;
285 438
286 return @res; 439 return @res;
287 } 440 }
288 } 441 }
297} 450}
298 451
299sub get { 452sub get {
300 my ($self, $member, $cb) = @_; 453 my ($self, $member, $cb) = @_;
301 454
302 $self->msg_cb ($cb, w_g => [$member]); 455 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
303} 456}
304 457
305sub TO_JSON { 458sub TO_JSON {
306 { __w_ => $_[0]{id} } 459 { "\fw" => $_[0]{id} }
307} 460}
308 461
309our $AUTOLOAD; 462our $AUTOLOAD;
310 463
311sub AUTOLOAD { 464sub AUTOLOAD {
318 $self->msg (w_c => 0, $AUTOLOAD, @_); 471 $self->msg (w_c => 0, $AUTOLOAD, @_);
319 472
320 () 473 ()
321} 474}
322 475
476package ext::widget::callback;
477
478sub DESTROY {
479 my ($self) = @_;
480
481 if (my $ns = $self->{ns}) {
482 delete $ns->{widget_cb}{$self->{id}};
483 $ns->free_wid ($self->{id});
484 }
485}
486
487sub TO_JSON {
488 { "\fc" => $_[0]{id} }
489}
490

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines