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.5 by root, Sun Jul 15 22:39:48 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 name value # widget member set 10# w_s id @attr # widget member set
9# w_g id rid name # 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 {
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 }
35} 37}
36 38
37sub demo_map { 39sub demo_start {
38 my ($ns) = @_; 40 my ($ns) = @_;
39 41
40 my $ws = $ns->{csc} = $ns->new_widgetset; 42 my $ws = $ns->{csc} = $ns->new_widgetset;
41 43
42 my $w = $ws->new (Toplevel => 44 my ($tl, $entry) = $ws->template (undef,
43 w => 200, 45 toplevel => {},
44 h => 200, 46 entry => {
45 x => "center", 47 text => "xyz",
46 y => "center", 48 on_changed => sub {
47 title => "Worldmap", 49 warn "changed<@_>\n";#d#
48 has_close_button => 1, 50 },
49 on_delete => sub {
50 $ws->destroy;
51 }, 51 },
52 ); 52 );
53 53
54 my $face = cf::face::find "res/worldmap.jpg";
55 $ns->send_face ($face);
56
57 $w->add (my $sw = $ws->new (ScrolledWindow => scroll_x => 1, scroll_y => 1));
58 $sw->add (my $vb = $ws->new (VBox => expand => 1));
59 #$vb->add ($ws->new (Label => text => "lb1"));
60 $vb->add ($ws->new (Face => expand => 1, size_w => undef, size_h => undef, face => $face));
61 $sw->show;
62 $vb->show;
63
64 $w->show; 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;
65} 60}
66 61
67sub csc_start { 62sub csc_start {
68 my ($ns) = @_; 63 my ($ns) = @_;
69 64
81 }, 76 },
82 ); 77 );
83 78
84 $w->add (my $ntb = $ws->new (Notebook => expand => 1)); 79 $w->add (my $ntb = $ws->new (Notebook => expand => 1));
85 80
86 $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");
87 82
88 $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); 83 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
89 84
90 for ( 85 for (
91 [0, "Str"], 86 [0, "Str"],
92 [1, "Dex"], 87 [1, "Dex"],
93 [2, "Con"], 88 [2, "Con"],
96 [5, "Pow"], 91 [5, "Pow"],
97 [6, "Cha"], 92 [6, "Cha"],
98 ) { 93 ) {
99 my ($x, $label) = @$_; 94 my ($x, $label) = @$_;
100 95
101 $statstable->add ($x, 0, $ws->new (Label => 96 $statstable->add_at ($x, 0, $ws->new (Label =>
102 can_hover => 1, can_events => 1, 97 can_hover => 1, can_events => 1,
103 align => +1, text => $label, tooltip => "#stat_$label", 98 align => +1, text => $label, tooltip => "#stat_$label",
104 )); 99 ));
105 $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => 100 $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
106 can_hover => 1, can_events => 1, 101 can_hover => 1, can_events => 1,
107 align => +1, template => "88", tooltip => "#stat_$label", 102 align => +1, template => "88", tooltip => "#stat_$label",
108 )); 103 ));
109 } 104 }
110 105
111 csc_update_stats $ns; 106 csc_update_stats $ns;
112 107
108 $ws->{tl} = $w;
113 $w->show; 109 $w->show;
114} 110}
115 111
116cf::player->attach ( 112cf::player->attach (
117 on_login => sub { 113 on_login => sub {
120 return unless $cf::CFG{devel}; 116 return unless $cf::CFG{devel};
121 117
122 my $ns = $pl->ns; 118 my $ns = $pl->ns;
123 119
124 return unless $ns->{can_widget}; 120 return unless $ns->{can_widget};
125
126 demo_map $ns;
127 #csc_start $ns; 121 #csc_start $ns;
122 demo_start $ns;
128 }, 123 },
129); 124);
130 125
131cf::register_exticmd w_e => sub { 126cf::register_exticmd w_e => sub {
132 my ($ns, $pkt) = @_; 127 my ($ns, $id, @args) = @_;
133 128
134 if (my $w = $ns->{widget}{$pkt->{id}}) { 129 if (my $cb = $ns->{widget_cb}{$id}) {
135 if (my $cb = $w->{ev}{$pkt->{name}}) { 130 $cb->(@args);
136 $_->($w, @{ $pkt->{args} || [] })
137 for @$cb;
138 }
139 }
140
141 ()
142};
143
144cf::register_exticmd w_r => sub {
145 my ($ns, $pkt) = @_;
146
147 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) {
148 $cb->(@{$pkt->{res} || [] });
149 } 131 }
150 132
151 () 133 ()
152}; 134};
153 135
157 my $id = ++$self->{id}; 139 my $id = ++$self->{id};
158 140
159 my $ws = bless { 141 my $ws = bless {
160 id => $id, 142 id => $id,
161 ns => $self, 143 ns => $self,
162 w => {}, 144 _w => {},
163 }, "ext::widget::set"; 145 }, "ext::widget::set";
164 146
165 $ws->msg (ws_n => id => $id); 147 $ws->msg (ws_n => $id);
166 148
167 $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];
168} 159}
169 160
170############################################################################# 161#############################################################################
171 162
172package ext::widget::set; 163package ext::widget::set;
176} 167}
177 168
178sub destroy { 169sub destroy {
179 my ($self) = @_; 170 my ($self) = @_;
180 171
181 $self->msg (ws_d => id => $self->{id}); 172 $self->msg (ws_d => $self->{id});
182 delete $self->{ns}; 173 delete $self->{ns};
183 $_->destroy 174 $_->destroy
184 for values %{ $self->{w} }; 175 for values %{ $self->{w} };
185} 176}
186 177
187sub msg { 178sub msg {
188 my ($self, $type, %msg) = @_; 179 my ($self, @msg) = @_;
189 180
190 if (my $ns = shift->{ns}) { 181 if (my $ns = shift->{ns}) {
191 $msg{msgtype} = $type; 182 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
192 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); 183 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
193 } 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
194} 200}
195 201
196sub new { 202sub new {
197 my ($self, $class, %args) = @_; 203 my ($self, $class, %args) = @_;
198 204
199 my $id = ++$self->{ns}{id}; 205 my $proxy = $self->alloc;
200 206
201 my $proxy = $self->{w}{$id} = bless { 207 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
202 id => $id,
203 }, "ext::widget::proxy";
204
205 Scalar::Util::weaken ($proxy->{ws} = $self); 208 Scalar::Util::weaken ($proxy->{ws} = $self);
206 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
207 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
208 209
209 for my $ev (grep /^on_/, keys %args) { 210 for my $ev (grep /^on_/, keys %args) {
210 push @{$proxy->{ev}{$ev}}, $args{$ev}; 211 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
211 $args{$ev} = 0;
212 } 212 }
213 213
214 $self->msg (ws_c => 214 $self->msg (ws_c =>
215 ws => $self->{w}{id}, 215 $self->{id},
216 id => $id, 216 $proxy->{id},
217 class => $class, 217 $class,
218 args => \%args, 218 \%args,
219 ); 219 );
220 220
221 $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]
222} 279}
223 280
224############################################################################# 281#############################################################################
225 282
226package ext::widget::proxy; 283package ext::widget::proxy;
229 my ($self) = @_; 286 my ($self) = @_;
230 287
231 delete $self->{ns}{widget}{$self->{id}}; 288 delete $self->{ns}{widget}{$self->{id}};
232 289
233 if (my $ws = $self->{ws}) { 290 if (my $ws = $self->{ws}) {
291 $self->msg (w_c => 0, "destroy");
234 delete $ws->{w}{$self->{id}}; 292 delete $ws->{_w}{$self->{id}};
235 $self->msg (w_c => name => "destroy");
236 } 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
237} 323}
238 324
239sub msg { 325sub msg {
240 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) = @_;
241 337
242 if (my $ws = $self->{ws}) { 338 if (my $ws = $self->{ws}) {
243 $ws->msg ($type,
244 %msg,
245 id => $self->{id},
246 );
247 }
248}
249
250sub msg_cb {
251 my ($self, $cb, $type, %msg) = @_;
252
253 if (my $ws = $self->{ws}) {
254
255 my $rid = ++$ws->{ns}{id}; 339 my $rid = $ws->{ns}->alloc_wid;
256
257 $self->msg ($type, %msg, rid => $rid);
258 340
259 if ($cb) { 341 if ($cb) {
260 $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);
261 } else { 349 } else {
262 # synchronous case 350 # synchronous case
263 my $wait = new Coro::Signal; 351 my $wait = new Coro::Signal;
264 my @res; 352 my @res;
265 353
266 $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
267 @res = @_; 358 @res = @_;
268 $wait->send; 359 $wait->send;
269 }; 360 };
361 $self->msg ($type, $rid, @arg);
270 $wait->wait; 362 $wait->wait;
271 363
272 return @res; 364 return @res;
273 } 365 }
274 } 366 }
275 367
276 () 368 ()
277} 369}
278 370
279sub set { 371sub set {
280 my ($self, $member, $value) = @_; 372 my ($self, @kv) = @_;
281 373
282 $self->msg (w_s => name => $member, value => $value); 374 $self->msg (w_s => \@kv);
283} 375}
284 376
285sub get { 377sub get {
286 my ($self, $member, $cb) = @_; 378 my ($self, $member, $cb) = @_;
287 379
288 $self->msg_cb ($cb, w_g => name => $member); 380 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
289} 381}
290 382
291sub TO_JSON { 383sub TO_JSON {
292 { __widget_ref__ => $_[0]{id} } 384 { "\fw" => $_[0]{id} }
293} 385}
294 386
295our $AUTOLOAD; 387our $AUTOLOAD;
296 388
297sub AUTOLOAD { 389sub AUTOLOAD {
298 $AUTOLOAD =~ s/^.*:// 390 $AUTOLOAD =~ s/^.*://
299 or return; 391 or return;
300 392
301 my $self = shift; 393 my $self = shift;
302 394
395 #TODO: handle non-void context
303 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 396 $self->msg (w_c => 0, $AUTOLOAD, @_);
304 397
305 () 398 ()
306} 399}
307 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