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.4 by root, Mon Jul 2 03:15:30 2007 UTC vs.
Revision 1.23 by root, Sat Dec 29 21:07:23 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 ttype ttext done_cb \%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);
20
21 $ns->{id} = "a"; 22 $ns->{id} = "a";
22 $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { 23 $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub {
23 # cannot deserialise ATM 24 $ns->{widget}{$_[0]}
24 undef
25 }); 25 });
26
27 cf::weaken $ns;
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 }
35} 37}
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 $@;
36 100
37sub csc_start { 101sub csc_start {
38 my ($ns) = @_; 102 my ($ns) = @_;
39 103
40 my $ws = $ns->{csc} = $ns->new_widgetset; 104 my $ws = $ns->{csc} = $ns->new_widgetset;
51 }, 115 },
52 ); 116 );
53 117
54 $w->add (my $ntb = $ws->new (Notebook => expand => 1)); 118 $w->add (my $ntb = $ws->new (Notebook => expand => 1));
55 119
56 $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); 120 $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character");
57 121
58 $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); 122 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
59 123
60 for ( 124 for (
61 [0, "Str"], 125 [0, "Str"],
62 [1, "Dex"], 126 [1, "Dex"],
63 [2, "Con"], 127 [2, "Con"],
66 [5, "Pow"], 130 [5, "Pow"],
67 [6, "Cha"], 131 [6, "Cha"],
68 ) { 132 ) {
69 my ($x, $label) = @$_; 133 my ($x, $label) = @$_;
70 134
71 $statstable->add ($x, 0, $ws->new (Label => 135 $statstable->add_at ($x, 0, $ws->new (Label =>
72 can_hover => 1, can_events => 1, 136 can_hover => 1, can_events => 1,
73 align => +1, text => $label, tooltip => "#stat_$label", 137 align => +1, text => $label, tooltip => "#stat_$label",
74 )); 138 ));
75 $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => 139 $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
76 can_hover => 1, can_events => 1, 140 can_hover => 1, can_events => 1,
77 align => +1, template => "88", tooltip => "#stat_$label", 141 align => +1, template => "88", tooltip => "#stat_$label",
78 )); 142 ));
79 } 143 }
80 144
81 csc_update_stats $ns; 145 csc_update_stats $ns;
82 146
147 $ws->{tl} = $w;
83 $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;
84} 168}
85 169
86cf::player->attach ( 170cf::player->attach (
87 on_login => sub { 171 on_login => sub {
88 my ($pl) = @_; 172 my ($pl) = @_;
89 173
174 my $ns = $pl->ns;
175 return unless $ns->{can_widget};
176
90 return unless $cf::CFG{devel}; 177 return unless $cf::CFG{devel};
91 178
92 my $ns = $pl->ns;
93
94 return unless $ns->{can_widget};
95
96 csc_start $ns; 179 #csc_start $ns;
97 }, 180 },
98); 181);
99 182
100cf::register_exticmd w_e => sub { 183cf::register_exticmd w_e => sub {
101 my ($ns, $pkt) = @_; 184 my ($ns, $id, @args) = @_;
102 185
103 if (my $w = $ns->{widget}{$pkt->{id}}) { 186 if (my $cb = $ns->{widget_cb}{$id}) {
104 if (my $cb = $w->{ev}{$pkt->{name}}) { 187 $cb->(@args);
105 $_->($w, @{ $pkt->{args} || [] })
106 for @$cb;
107 }
108 }
109
110 ()
111};
112
113cf::register_exticmd w_r => sub {
114 my ($ns, $pkt) = @_;
115
116 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) {
117 $cb->(@{$pkt->{res} || [] });
118 } 188 }
119 189
120 () 190 ()
121}; 191};
122 192
126 my $id = ++$self->{id}; 196 my $id = ++$self->{id};
127 197
128 my $ws = bless { 198 my $ws = bless {
129 id => $id, 199 id => $id,
130 ns => $self, 200 ns => $self,
131 w => {}, 201 _w => {},
132 }, "ext::widget::set"; 202 }, "ext::widget::set";
133 203
204 cf::weaken $ws->{ns};
205
134 $ws->msg (ws_n => id => $id); 206 $ws->msg (ws_n => $id);
135 207
136 $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];
137} 218}
138 219
139############################################################################# 220#############################################################################
140 221
141package ext::widget::set; 222package ext::widget::set;
145} 226}
146 227
147sub destroy { 228sub destroy {
148 my ($self) = @_; 229 my ($self) = @_;
149 230
150 $self->msg (ws_d => id => $self->{id}); 231 $self->msg (ws_d => $self->{id});
151 delete $self->{ns}; 232 delete $self->{ns};
152 $_->destroy 233 $_->destroy
153 for values %{ $self->{w} }; 234 for values %{ $self->{w} };
154} 235}
155 236
156sub msg { 237sub msg {
157 my ($self, $type, %msg) = @_; 238 my ($self, @msg) = @_;
158 239
159 if (my $ns = shift->{ns}) { 240 if (my $ns = shift->{ns}) {
160 $msg{msgtype} = $type; 241 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
161 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); 242 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
162 } 243 }
244}
245
246sub alloc {
247 my ($self) = @_;
248
249 my $id = $self->{ns}->alloc_wid;
250
251 my $proxy = bless {
252 id => $id,
253 }, "ext::widget::proxy";
254
255 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
256 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
257
258 $proxy
163} 259}
164 260
165sub new { 261sub new {
166 my ($self, $class, %args) = @_; 262 my ($self, $class, %args) = @_;
167 263
168 my $id = ++$self->{ns}{id}; 264 my $proxy = $self->alloc;
169 265
170 my $proxy = $self->{w}{$id} = bless { 266 cf::weaken
171 id => $id, 267 +($self->{_w}{$proxy->{id}} = $proxy),
172 }, "ext::widget::proxy"; 268 +($proxy->{ws} = $self);
173
174 Scalar::Util::weaken ($proxy->{ws} = $self);
175 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
176 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
177 269
178 for my $ev (grep /^on_/, keys %args) { 270 for my $ev (grep /^on_/, keys %args) {
179 push @{$proxy->{ev}{$ev}}, $args{$ev}; 271 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
180 $args{$ev} = 0;
181 } 272 }
182 273
183 $self->msg (ws_c => 274 $self->msg (ws_c =>
184 ws => $self->{w}{id}, 275 $self->{id},
185 id => $id, 276 $proxy->{id},
186 class => $class, 277 $class,
187 args => \%args, 278 \%args,
188 ); 279 );
189 280
190 $proxy 281 $proxy
191} 282}
192 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
333sub find {
334 my ($self, @names) = @_;
335
336 my @res;
337 my @alloc;
338
339 for my $name (@names) {
340 push @res, $self->{ns}{widget_wkw}{$name} ||= do {
341 my $proxy = $self->alloc;
342
343 push @alloc, $proxy->{id} => $name;
344
345 $proxy
346 };
347 }
348
349 $self->msg (ws_a => @alloc)
350 if @alloc;
351
352 wantarray ? @res : $res[0]
353}
354
193############################################################################# 355#############################################################################
194 356
195package ext::widget::proxy; 357package ext::widget::proxy;
196 358
197sub DESTROY { 359sub msg {
198 my ($self) = @_; 360 my ($self, $type, @arg) = @_;
199 361
200 delete $self->{ns}{widget}{$self->{id}}; 362 if (my $ns = $self->{ns}) {
363 my @msg = ($type, $self->{id}, @arg);
364 warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
365 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
366 }
367}
368
369sub msg_cb {
370 my ($self, $cb, $type, @arg) = @_;
201 371
202 if (my $ws = $self->{ws}) { 372 if (my $ws = $self->{ws}) {
203 delete $ws->{w}{$self->{id}};
204 $self->msg (w_c => name => "destroy");
205 }
206}
207
208sub msg {
209 my ($self, $type, %msg) = @_;
210
211 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}; 373 my $rid = $ws->{ns}->alloc_wid;
225
226 $self->msg ($type, %msg, rid => $rid);
227 374
228 if ($cb) { 375 if ($cb) {
229 $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);
230 } else { 383 } else {
231 # synchronous case 384 # synchronous case
232 my $wait = new Coro::Signal; 385 my $wait = new Coro::Signal;
233 my @res; 386 my @res;
234 387
235 $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
236 @res = @_; 392 @res = @_;
237 $wait->send; 393 $wait->send;
238 }; 394 };
395 $self->msg ($type, $rid, @arg);
239 $wait->wait; 396 $wait->wait;
240 397
241 return @res; 398 return @res;
242 } 399 }
243 } 400 }
244 401
245 () 402 ()
246} 403}
247 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
248sub set { 445sub set {
249 my ($self, $member, $value) = @_; 446 my ($self, @kv) = @_;
250 447
251 $self->msg (w_s => name => $member, value => $value); 448 $self->msg (w_s => \@kv);
252} 449}
253 450
254sub get { 451sub get {
255 my ($self, $member, $cb) = @_; 452 my ($self, $member, $cb) = @_;
256 453
257 $self->msg_cb ($cb, w_g => name => $member); 454 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
258} 455}
259 456
260sub TO_JSON { 457sub TO_JSON {
261 { __widget_ref__ => $_[0]{id} } 458 { "\fw" => $_[0]{id} }
262} 459}
263 460
264our $AUTOLOAD; 461our $AUTOLOAD;
265 462
266sub AUTOLOAD { 463sub AUTOLOAD {
267 $AUTOLOAD =~ s/^.*:// 464 $AUTOLOAD =~ s/^.*://
268 or return; 465 or return;
269 466
270 my $self = shift; 467 my $self = shift;
271 468
469 #TODO: handle non-void context
272 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 470 $self->msg (w_c => 0, $AUTOLOAD, @_);
273 471
274 () 472 ()
275} 473}
276 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