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.11 by root, Sat Jul 21 18:01:26 2007 UTC vs.
Revision 1.20 by root, Thu Dec 27 18:35:48 2007 UTC

1#! perl # mandatory depends=login 1#! perl # mandatory depends=login
2 2
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_n id # widgetset new 5# ws_n ws # widgetset new
5# ws_d id # widgetset destroy 6# ws_d ws # widgetset destroy
6# 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
7# w_c id [rid] name args # widget method call 9# w_c id rid name @args # widget method call
8# w_s id @attr # widget member set 10# w_s id @attr # widget member set
9# w_g id rid @attr # widget member get 11# w_g id rid @attr # widget member get
10# 12#
11# and expects the following exti message types 13# and expects the following exti message types
12# w_r rid res # widget call return
13# w_e id name args # widget_event 14# w_e id @args # widget_call
15
16our $DEBUG = 1;
14 17
15cf::client->attach ( 18cf::client->attach (
16 on_connect => sub { 19 on_connect => sub {
17 my ($ns) = @_; 20 my ($ns) = @_;
18 21
19 Scalar::Util::weaken (my $weakns = $ns); 22 Scalar::Util::weaken (my $weakns = $ns);
20 23
21 $ns->{id} = "a"; 24 $ns->{id} = "a";
22 $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { 25 $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub {
23 # cannot deserialise ATM 26 $weakns->{widget}{$_[0]}
24 undef
25 }); 27 });
26 }, 28 },
27); 29);
28 30
29sub csc_update_stats { 31sub csc_update_stats {
30 my ($ns) = @_; 32 my ($ns) = @_;
31 33
32 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { 34 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) {
33 $v->set_text ($ns->pl->ob->stats->$k); 35 $v->set_text ($ns->pl->ob->stats->$k);
34 } 36 }
37}
38
39sub demo_start {
40 my ($ns) = @_;
41
42 my $ws = $ns->{csc} = $ns->new_widgetset;
43
44 my ($tl, $entry) = $ws->template (undef,
45 toplevel => {},
46 entry => {
47 text => "xyz",
48 on_changed => sub {
49 warn "changed<@_>\n";#d#
50 },
51 },
52 );
53
54 $tl->show;
55
56 $ns->{xxxw} = [$tl, $entry];#d#
57
58# $ws->find ("setup_notebook")->add ($ws->{tab});
59# $ws->find ("setup_dialog")->toggle_visibility;
35} 60}
36 61
37sub csc_start { 62sub csc_start {
38 my ($ns) = @_; 63 my ($ns) = @_;
39 64
51 }, 76 },
52 ); 77 );
53 78
54 $w->add (my $ntb = $ws->new (Notebook => expand => 1)); 79 $w->add (my $ntb = $ws->new (Notebook => expand => 1));
55 80
56 $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); 81 $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character");
57 82
58 $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); 83 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
59 84
60 for ( 85 for (
61 [0, "Str"], 86 [0, "Str"],
62 [1, "Dex"], 87 [1, "Dex"],
63 [2, "Con"], 88 [2, "Con"],
78 )); 103 ));
79 } 104 }
80 105
81 csc_update_stats $ns; 106 csc_update_stats $ns;
82 107
108 $ws->{tl} = $w;
83 $w->show; 109 $w->show;
84} 110}
85 111
86cf::player->attach ( 112cf::player->attach (
87 on_login => sub { 113 on_login => sub {
91 117
92 my $ns = $pl->ns; 118 my $ns = $pl->ns;
93 119
94 return unless $ns->{can_widget}; 120 return unless $ns->{can_widget};
95 #csc_start $ns; 121 #csc_start $ns;
122 demo_start $ns;
96 }, 123 },
97); 124);
98 125
99cf::register_exticmd w_e => sub { 126cf::register_exticmd w_e => sub {
100 my ($ns, $pkt) = @_; 127 my ($ns, $id, @args) = @_;
101 128
102 if (my $w = $ns->{widget}{$pkt->{id}}) { 129 if (my $cb = $ns->{widget_cb}{$id}) {
103 if (my $cb = $w->{ev}{$pkt->{name}}) { 130 $cb->(@args);
104 $_->($w, @{ $pkt->{args} || [] })
105 for @$cb;
106 }
107 }
108
109 ()
110};
111
112cf::register_exticmd w_r => sub {
113 my ($ns, $pkt) = @_;
114
115 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) {
116 $cb->(@{$pkt->{res} || [] });
117 } 131 }
118 132
119 () 133 ()
120}; 134};
121 135
125 my $id = ++$self->{id}; 139 my $id = ++$self->{id};
126 140
127 my $ws = bless { 141 my $ws = bless {
128 id => $id, 142 id => $id,
129 ns => $self, 143 ns => $self,
130 w => {}, 144 _w => {},
131 }, "ext::widget::set"; 145 }, "ext::widget::set";
132 146
133 $ws->msg (ws_n => id => $id); 147 $ws->msg (ws_n => $id);
134 148
135 $ws 149 $ws
150}
151
152sub cf::client::alloc_wid {
153 pop @{ $_[0]{ids} }
154 or ++$_[0]{id}
155}
156
157sub cf::client::free_wid {
158 push @{ $_[0]{ids} }, $_[1];
136} 159}
137 160
138############################################################################# 161#############################################################################
139 162
140package ext::widget::set; 163package ext::widget::set;
144} 167}
145 168
146sub destroy { 169sub destroy {
147 my ($self) = @_; 170 my ($self) = @_;
148 171
149 $self->msg (ws_d => id => $self->{id}); 172 $self->msg (ws_d => $self->{id});
150 delete $self->{ns}; 173 delete $self->{ns};
151 $_->destroy 174 $_->destroy
152 for values %{ $self->{w} }; 175 for values %{ $self->{w} };
153} 176}
154 177
155sub msg { 178sub msg {
156 my ($self, $type, %msg) = @_; 179 my ($self, @msg) = @_;
157 180
158 if (my $ns = shift->{ns}) { 181 if (my $ns = shift->{ns}) {
159 $msg{msgtype} = $type; 182 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
160 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); 183 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
161 } 184 }
185}
186
187sub alloc {
188 my ($self) = @_;
189
190 my $id = $self->{ns}->alloc_wid;
191
192 my $proxy = bless {
193 id => $id,
194 }, "ext::widget::proxy";
195
196 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
197 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
198
199 $proxy
162} 200}
163 201
164sub new { 202sub new {
165 my ($self, $class, %args) = @_; 203 my ($self, $class, %args) = @_;
166 204
167 my $id = ++$self->{ns}{id}; 205 my $proxy = $self->alloc;
168 206
169 my $proxy = $self->{w}{$id} = bless { 207 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
170 id => $id,
171 }, "ext::widget::proxy";
172
173 Scalar::Util::weaken ($proxy->{ws} = $self); 208 Scalar::Util::weaken ($proxy->{ws} = $self);
174 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
175 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
176 209
177 for my $ev (grep /^on_/, keys %args) { 210 for my $ev (grep /^on_/, keys %args) {
178 push @{$proxy->{ev}{$ev}}, $args{$ev}; 211 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
179 $args{$ev} = 0;
180 } 212 }
181 213
182 $self->msg (ws_c => 214 $self->msg (ws_c =>
183 ws => $self->{w}{id}, 215 $self->{id},
184 id => $id, 216 $proxy->{id},
185 class => $class, 217 $class,
186 args => \%args, 218 \%args,
187 ); 219 );
188 220
189 $proxy 221 $proxy
222}
223
224sub template {
225 my ($self, $template, @args) = @_;
226
227 my %cfg;
228 my @res;
229
230 while (@args) {
231 my ($name, $args) = splice @args, 0, 2, ();
232
233 my $proxy = $self->alloc;
234
235 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
236 Scalar::Util::weaken ($proxy->{ws} = $self);
237
238 for my $ev (grep /^on_/, keys %$args) {
239 $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev});
240 }
241
242 $cfg{$name} = {
243 %$args,
244 id => $proxy->{id},
245 };
246
247 push @res, $proxy;
248 }
249
250 $self->msg (ws_ct =>
251 $self->{id},
252 $template,
253 \%cfg,
254 );
255
256 @res
257}
258
259sub find {
260 my ($self, @names) = @_;
261
262 my @res;
263 my @alloc;
264
265 for my $name (@names) {
266 push @res, $self->{ns}{widget_wkw}{$name} ||= do {
267 my $proxy = $self->alloc;
268
269 push @alloc, $proxy->{id} => $name;
270
271 $proxy
272 };
273 }
274
275 $self->msg (ws_a => @alloc)
276 if @alloc;
277
278 wantarray ? @res : $res[0]
190} 279}
191 280
192############################################################################# 281#############################################################################
193 282
194package ext::widget::proxy; 283package ext::widget::proxy;
197 my ($self) = @_; 286 my ($self) = @_;
198 287
199 delete $self->{ns}{widget}{$self->{id}}; 288 delete $self->{ns}{widget}{$self->{id}};
200 289
201 if (my $ws = $self->{ws}) { 290 if (my $ws = $self->{ws}) {
291 $self->msg (w_c => 0, "destroy");
202 delete $ws->{w}{$self->{id}}; 292 delete $ws->{_w}{$self->{id}};
203 $self->msg (w_c => name => "destroy");
204 } 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
205} 323}
206 324
207sub msg { 325sub msg {
208 my ($self, $type, %msg) = @_; 326 my ($self, $type, @arg) = @_;
327
328 if (my $ns = $self->{ns}) {
329 my @msg = ($type, $self->{id}, @arg);
330 warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
331 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
332 }
333}
334
335sub msg_cb {
336 my ($self, $cb, $type, @arg) = @_;
209 337
210 if (my $ws = $self->{ws}) { 338 if (my $ws = $self->{ws}) {
211 $ws->msg ($type,
212 %msg,
213 id => $self->{id},
214 );
215 }
216}
217
218sub msg_cb {
219 my ($self, $cb, $type, %msg) = @_;
220
221 if (my $ws = $self->{ws}) {
222
223 my $rid = ++$ws->{ns}{id}; 339 my $rid = $ws->{ns}->alloc_wid;
224
225 $self->msg ($type, %msg, rid => $rid);
226 340
227 if ($cb) { 341 if ($cb) {
228 $ws->{ns}{widget_return}{$rid} = $cb; 342 $ws->{ns}{widget_cb}{$rid} = sub {
343 delete $ws->{ns}{widget_cb}{$rid};
344 $ws->{ns}->free_wid ($rid);
345 &$cb
346 };
347
348 $self->msg ($type, $rid, @arg);
229 } else { 349 } else {
230 # synchronous case 350 # synchronous case
231 my $wait = new Coro::Signal; 351 my $wait = new Coro::Signal;
232 my @res; 352 my @res;
233 353
234 $ws->{ns}{widget_return}{$rid} = sub { 354 $ws->{ns}{widget_cb}{$rid} = sub {
355 delete $ws->{ns}{widget_cb}{$rid};
356 $ws->{ns}->free_wid ($rid);
357
235 @res = @_; 358 @res = @_;
236 $wait->send; 359 $wait->send;
237 }; 360 };
361 $self->msg ($type, $rid, @arg);
238 $wait->wait; 362 $wait->wait;
239 363
240 return @res; 364 return @res;
241 } 365 }
242 } 366 }
243 367
244 () 368 ()
245} 369}
246 370
247sub set { 371sub set {
248 my ($self, $member, $value) = @_; 372 my ($self, @kv) = @_;
249 373
250 $self->msg (w_s => attr => [ [$member, $value] ]); 374 $self->msg (w_s => \@kv);
251} 375}
252 376
253sub get { 377sub get {
254 my ($self, $member, $cb) = @_; 378 my ($self, $member, $cb) = @_;
255 379
256 $self->msg_cb ($cb, w_g => attr => [$member]); 380 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
257} 381}
258 382
259sub TO_JSON { 383sub TO_JSON {
260 { __widget_ref__ => $_[0]{id} } 384 { "\fw" => $_[0]{id} }
261} 385}
262 386
263our $AUTOLOAD; 387our $AUTOLOAD;
264 388
265sub AUTOLOAD { 389sub AUTOLOAD {
266 $AUTOLOAD =~ s/^.*:// 390 $AUTOLOAD =~ s/^.*://
267 or return; 391 or return;
268 392
269 my $self = shift; 393 my $self = shift;
270 394
395 #TODO: handle non-void context
271 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 396 $self->msg (w_c => 0, $AUTOLOAD, @_);
272 397
273 () 398 ()
274} 399}
275 400
401package ext::widget::callback;
402
403sub DESTROY {
404 my ($self) = @_;
405
406 if (my $ns = $self->{ns}) {
407 delete $ns->{widget_cb}{$self->{id}};
408 $ns->free_wid ($self->{id});
409 }
410}
411
412sub TO_JSON {
413 { "\fc" => $_[0]{id} }
414}
415

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines