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.12 by root, Sun Jul 22 20:08:38 2007 UTC vs.
Revision 1.19 by root, Sat Sep 1 07:22:20 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
7# w_c id [rid] name args # widget method call 8# w_c id rid name @args # widget method call
8# w_s id @attr # widget member set 9# w_s id @attr # widget member set
9# w_g id rid @attr # widget member get 10# w_g id rid @attr # widget member get
10# 11#
11# and expects the following exti message types 12# and expects the following exti message types
12# w_r rid res # widget call return
13# w_e id name args # widget_event 13# w_e id @args # widget_call
14
15our $DEBUG = 1;
14 16
15cf::client->attach ( 17cf::client->attach (
16 on_connect => sub { 18 on_connect => sub {
17 my ($ns) = @_; 19 my ($ns) = @_;
18 20
19 Scalar::Util::weaken (my $weakns = $ns); 21 Scalar::Util::weaken (my $weakns = $ns);
20 22
21 $ns->{id} = "a"; 23 $ns->{id} = "a";
22 $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { 24 $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub {
23 # cannot deserialise ATM 25 $weakns->{widget}{$_[0]}
24 undef
25 }); 26 });
26 }, 27 },
27); 28);
28 29
29sub csc_update_stats { 30sub csc_update_stats {
30 my ($ns) = @_; 31 my ($ns) = @_;
31 32
32 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { 33 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) {
33 $v->set_text ($ns->pl->ob->stats->$k); 34 $v->set_text ($ns->pl->ob->stats->$k);
34 } 35 }
36}
37
38sub demo_start {
39 my ($ns) = @_;
40
41 my $ws = $ns->{csc} = $ns->new_widgetset;
42
43 $ws->{tab} = $ws->new (Label => text => "dumb tst", c_tab => ["hull"]);
44
45 $ws->find ("setup_notebook")->add ($ws->{tab});
46 $ws->find ("setup_dialog")->toggle_visibility;
35} 47}
36 48
37sub csc_start { 49sub csc_start {
38 my ($ns) = @_; 50 my ($ns) = @_;
39 51
51 }, 63 },
52 ); 64 );
53 65
54 $w->add (my $ntb = $ws->new (Notebook => expand => 1)); 66 $w->add (my $ntb = $ws->new (Notebook => expand => 1));
55 67
56 $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); 68 $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character");
57 69
58 $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); 70 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
59 71
60 for ( 72 for (
61 [0, "Str"], 73 [0, "Str"],
62 [1, "Dex"], 74 [1, "Dex"],
63 [2, "Con"], 75 [2, "Con"],
78 )); 90 ));
79 } 91 }
80 92
81 csc_update_stats $ns; 93 csc_update_stats $ns;
82 94
95 $ws->{tl} = $w;
83 $w->show; 96 $w->show;
84} 97}
85 98
86cf::player->attach ( 99cf::player->attach (
87 on_login => sub { 100 on_login => sub {
91 104
92 my $ns = $pl->ns; 105 my $ns = $pl->ns;
93 106
94 return unless $ns->{can_widget}; 107 return unless $ns->{can_widget};
95 #csc_start $ns; 108 #csc_start $ns;
109 #demo_start $ns;
96 }, 110 },
97); 111);
98 112
99cf::register_exticmd w_e => sub { 113cf::register_exticmd w_e => sub {
100 my ($ns, $pkt) = @_; 114 my ($ns, $id, @args) = @_;
101 115
102 if (my $w = $ns->{widget}{$pkt->{id}}) { 116 if (my $cb = $ns->{widget_cb}{$id}) {
103 if (my $cb = $w->{ev}{$pkt->{name}}) { 117 $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 } 118 }
118 119
119 () 120 ()
120}; 121};
121 122
128 id => $id, 129 id => $id,
129 ns => $self, 130 ns => $self,
130 _w => {}, 131 _w => {},
131 }, "ext::widget::set"; 132 }, "ext::widget::set";
132 133
133 $ws->msg (ws_n => id => $id); 134 $ws->msg (ws_n => $id);
134 135
135 $ws 136 $ws
137}
138
139sub cf::client::alloc_wid {
140 pop @{ $_[0]{ids} }
141 or ++$_[0]{id}
142}
143
144sub cf::client::free_wid {
145 push @{ $_[0]{ids} }, $_[1];
136} 146}
137 147
138############################################################################# 148#############################################################################
139 149
140package ext::widget::set; 150package ext::widget::set;
144} 154}
145 155
146sub destroy { 156sub destroy {
147 my ($self) = @_; 157 my ($self) = @_;
148 158
149 $self->msg (ws_d => id => $self->{id}); 159 $self->msg (ws_d => $self->{id});
150 delete $self->{ns}; 160 delete $self->{ns};
151 $_->destroy 161 $_->destroy
152 for values %{ $self->{w} }; 162 for values %{ $self->{w} };
153} 163}
154 164
155sub msg { 165sub msg {
156 my ($self, $type, %msg) = @_; 166 my ($self, @msg) = @_;
157 167
158 if (my $ns = shift->{ns}) { 168 if (my $ns = shift->{ns}) {
159 $msg{msgtype} = $type; 169 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
160 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); 170 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
161 } 171 }
172}
173
174sub alloc {
175 my ($self) = @_;
176
177 my $id = $self->{ns}->alloc_wid;
178
179 my $proxy = bless {
180 id => $id,
181 }, "ext::widget::proxy";
182
183 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
184 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
185
186 $proxy
162} 187}
163 188
164sub new { 189sub new {
165 my ($self, $class, %args) = @_; 190 my ($self, $class, %args) = @_;
166 191
167 my $id = ++$self->{ns}{id}; 192 my $proxy = $self->alloc;
168 193
169 my $proxy = $self->{_w}{$id} = bless { 194 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
170 id => $id,
171 }, "ext::widget::proxy";
172
173 Scalar::Util::weaken ($proxy->{ws} = $self); 195 Scalar::Util::weaken ($proxy->{ws} = $self);
174 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
175 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
176 196
177 for my $ev (grep /^on_/, keys %args) { 197 for my $ev (grep /^on_/, keys %args) {
178 push @{$proxy->{ev}{$ev}}, $args{$ev}; 198 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
179 $args{$ev} = 0;
180 } 199 }
181 200
182 $self->msg (ws_c => 201 $self->msg (ws_c =>
202 $self->{id},
183 ws => $proxy->{id}, 203 $proxy->{id},
184 id => $id, 204 $class,
185 class => $class, 205 \%args,
186 args => \%args,
187 ); 206 );
188 207
189 $proxy 208 $proxy
190} 209}
191 210
211sub find {
212 my ($self, @names) = @_;
213
214 my @res;
215 my @alloc;
216
217 for my $name (@names) {
218 push @res, $self->{ns}{widget_wkw}{$name} ||= do {
219 my $proxy = $self->alloc;
220
221 push @alloc, $proxy->{id} => $name;
222
223 $proxy
224 };
225 }
226
227 $self->msg (ws_a => @alloc)
228 if @alloc;
229
230 wantarray ? @res : $res[0]
231}
232
192############################################################################# 233#############################################################################
193 234
194package ext::widget::proxy; 235package ext::widget::proxy;
195 236
196sub DESTROY { 237sub DESTROY {
197 my ($self) = @_; 238 my ($self) = @_;
198 239
199 delete $self->{ns}{widget}{$self->{id}}; 240 delete $self->{ns}{widget}{$self->{id}};
200 241
201 #warn "DES<$self> $self->{ws}\n";#d#
202 if (my $ws = $self->{ws}) { 242 if (my $ws = $self->{ws}) {
203 $self->msg (w_c => name => "destroy"); 243 $self->msg (w_c => 0, "destroy");
204 delete $ws->{_w}{$self->{id}}; 244 delete $ws->{_w}{$self->{id}};
205 } 245 }
206} 246}
207 247
248sub cb {
249 my ($self, $cb) = @_;
250
251 my $proxy = bless {
252 ns => $self->{ns},
253 id => $self->{ns}->alloc_wid,
254 }, "ext::widget::callback";
255
256 Scalar::Util::weaken $proxy->{ns};
257
258 $self->{ns}{widget_cb}{$proxy->{id}} = $cb;
259
260 $proxy
261}
262
263sub oneshot_cb {
264 my ($self, $cb) = @_;
265
266 if ("CODE" eq ref $cb) {
267 my $ocb = $cb;
268 $cb = $self->cb (sub {
269 undef $cb;
270 &$ocb
271 });
272 }
273
274 $cb
275}
276
208sub msg { 277sub msg {
209 my ($self, $type, %msg) = @_; 278 my ($self, $type, @arg) = @_;
279
280 if (my $ns = $self->{ns}) {
281 my @msg = ($type, $self->{id}, @arg);
282 warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
283 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
284 }
285}
286
287sub msg_cb {
288 my ($self, $cb, $type, @arg) = @_;
210 289
211 if (my $ws = $self->{ws}) { 290 if (my $ws = $self->{ws}) {
212 $ws->msg ($type,
213 %msg,
214 id => $self->{id},
215 );
216 }
217}
218
219sub msg_cb {
220 my ($self, $cb, $type, %msg) = @_;
221
222 if (my $ws = $self->{ws}) {
223
224 my $rid = ++$ws->{ns}{id}; 291 my $rid = $ws->{ns}->alloc_wid;
225
226 $self->msg ($type, %msg, rid => $rid);
227 292
228 if ($cb) { 293 if ($cb) {
229 $ws->{ns}{widget_return}{$rid} = $cb; 294 $ws->{ns}{widget_cb}{$rid} = sub {
295 delete $ws->{ns}{widget_cb}{$rid};
296 $ws->{ns}->free_wid ($rid);
297 &$cb
298 };
299
300 $self->msg ($type, $rid, @arg);
230 } else { 301 } else {
231 # synchronous case 302 # synchronous case
232 my $wait = new Coro::Signal; 303 my $wait = new Coro::Signal;
233 my @res; 304 my @res;
234 305
235 $ws->{ns}{widget_return}{$rid} = sub { 306 $ws->{ns}{widget_cb}{$rid} = sub {
307 delete $ws->{ns}{widget_cb}{$rid};
308 $ws->{ns}->free_wid ($rid);
309
236 @res = @_; 310 @res = @_;
237 $wait->send; 311 $wait->send;
238 }; 312 };
313 $self->msg ($type, $rid, @arg);
239 $wait->wait; 314 $wait->wait;
240 315
241 return @res; 316 return @res;
242 } 317 }
243 } 318 }
246} 321}
247 322
248sub set { 323sub set {
249 my ($self, @kv) = @_; 324 my ($self, @kv) = @_;
250 325
251 $self->msg (w_s => attr => \@kv); 326 $self->msg (w_s => \@kv);
252} 327}
253 328
254sub get { 329sub get {
255 my ($self, $member, $cb) = @_; 330 my ($self, $member, $cb) = @_;
256 331
257 $self->msg_cb ($cb, w_g => attr => [$member]); 332 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
258} 333}
259 334
260sub TO_JSON { 335sub TO_JSON {
261 { __widget_ref__ => $_[0]{id} } 336 { "\fw" => $_[0]{id} }
262} 337}
263 338
264our $AUTOLOAD; 339our $AUTOLOAD;
265 340
266sub AUTOLOAD { 341sub AUTOLOAD {
267 $AUTOLOAD =~ s/^.*:// 342 $AUTOLOAD =~ s/^.*://
268 or return; 343 or return;
269 344
270 my $self = shift; 345 my $self = shift;
271 346
347 #TODO: handle non-void context
272 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 348 $self->msg (w_c => 0, $AUTOLOAD, @_);
273 349
274 () 350 ()
275} 351}
276 352
353package ext::widget::callback;
354
355sub DESTROY {
356 my ($self) = @_;
357
358 if (my $ns = $self->{ns}) {
359 delete $ns->{widget_cb}{$self->{id}};
360 $ns->free_wid ($self->{id});
361 }
362}
363
364sub TO_JSON {
365 { "\fc" => $_[0]{id} }
366}
367

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines