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.3 by root, Tue Jun 26 04:50:05 2007 UTC vs.
Revision 1.22 by root, Fri Dec 28 12:44:45 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
22 Scalar::Util::weaken (my $weakns = $ns);
23
19 $ns->{id} = "a"; 24 $ns->{id} = "a";
25 $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub {
26 $weakns->{widget}{$_[0]}
27 });
20 }, 28 },
21); 29);
22 30
23sub csc_update_stats { 31sub csc_update_stats {
24 my ($ns) = @_; 32 my ($ns) = @_;
25 33
26 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { 34 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) {
27 $v->set_text ($ns->pl->ob->stats->$k); 35 $v->set_text ($ns->pl->ob->stats->$k);
28 } 36 }
29} 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 $@;
30 100
31sub csc_start { 101sub csc_start {
32 my ($ns) = @_; 102 my ($ns) = @_;
33 103
34 my $ws = $ns->{csc} = $ns->new_widgetset; 104 my $ws = $ns->{csc} = $ns->new_widgetset;
45 }, 115 },
46 ); 116 );
47 117
48 $w->add (my $ntb = $ws->new (Notebook => expand => 1)); 118 $w->add (my $ntb = $ws->new (Notebook => expand => 1));
49 119
50 $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");
51 121
52 $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); 122 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
53 123
54 for ( 124 for (
55 [0, "Str"], 125 [0, "Str"],
56 [1, "Dex"], 126 [1, "Dex"],
57 [2, "Con"], 127 [2, "Con"],
60 [5, "Pow"], 130 [5, "Pow"],
61 [6, "Cha"], 131 [6, "Cha"],
62 ) { 132 ) {
63 my ($x, $label) = @$_; 133 my ($x, $label) = @$_;
64 134
65 $statstable->add ($x, 0, $ws->new (Label => 135 $statstable->add_at ($x, 0, $ws->new (Label =>
66 can_hover => 1, can_events => 1, 136 can_hover => 1, can_events => 1,
67 align => +1, text => $label, tooltip => "#stat_$label", 137 align => +1, text => $label, tooltip => "#stat_$label",
68 )); 138 ));
69 $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => 139 $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
70 can_hover => 1, can_events => 1, 140 can_hover => 1, can_events => 1,
71 align => +1, template => "88", tooltip => "#stat_$label", 141 align => +1, template => "88", tooltip => "#stat_$label",
72 )); 142 ));
73 } 143 }
74 144
75 csc_update_stats $ns; 145 csc_update_stats $ns;
76 146
147 $ws->{tl} = $w;
77 $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;
78} 168}
79 169
80cf::player->attach ( 170cf::player->attach (
81 on_login => sub { 171 on_login => sub {
82 my ($pl) = @_; 172 my ($pl) = @_;
83 173
174 my $ns = $pl->ns;
175 return unless $ns->{can_widget};
176
84 return unless $cf::CFG{devel}; 177 return unless $cf::CFG{devel};
85 178
86 my $ns = $pl->ns;
87
88 return unless $ns->{can_widget};
89
90 csc_start $ns; 179 #csc_start $ns;
91 }, 180 },
92); 181);
93 182
94cf::register_exticmd w_e => sub { 183cf::register_exticmd w_e => sub {
95 my ($ns, $pkt) = @_; 184 my ($ns, $id, @args) = @_;
96 185
97 if (my $w = $ns->{widget}{$pkt->{id}}) { 186 if (my $cb = $ns->{widget_cb}{$id}) {
98 if (my $cb = $w->{ev}{$pkt->{name}}) { 187 $cb->(@args);
99 $_->($w, @{ $pkt->{args} || [] })
100 for @$cb;
101 }
102 }
103
104 ()
105};
106
107cf::register_exticmd w_r => sub {
108 my ($ns, $pkt) = @_;
109
110 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) {
111 $cb->(@{$pkt->{res} || [] });
112 } 188 }
113 189
114 () 190 ()
115}; 191};
116 192
120 my $id = ++$self->{id}; 196 my $id = ++$self->{id};
121 197
122 my $ws = bless { 198 my $ws = bless {
123 id => $id, 199 id => $id,
124 ns => $self, 200 ns => $self,
125 w => {}, 201 _w => {},
126 }, "ext::widget::set"; 202 }, "ext::widget::set";
127 203
128 $ws->msg (ws_n => id => $id); 204 $ws->msg (ws_n => $id);
129 205
130 $ws 206 $ws
207}
208
209sub cf::client::alloc_wid {
210 pop @{ $_[0]{ids} }
211 or ++$_[0]{id}
212}
213
214sub cf::client::free_wid {
215 push @{ $_[0]{ids} }, $_[1];
131} 216}
132 217
133############################################################################# 218#############################################################################
134 219
135package ext::widget::set; 220package ext::widget::set;
139} 224}
140 225
141sub destroy { 226sub destroy {
142 my ($self) = @_; 227 my ($self) = @_;
143 228
144 $self->msg (ws_d => id => $self->{id}); 229 $self->msg (ws_d => $self->{id});
145 delete $self->{ns}; 230 delete $self->{ns};
146 $_->destroy 231 $_->destroy
147 for values %{ $self->{w} }; 232 for values %{ $self->{w} };
148} 233}
149 234
150sub msg { 235sub msg {
151 my ($self, $type, %msg) = @_; 236 my ($self, @msg) = @_;
152 237
153 if (my $ns = shift->{ns}) { 238 if (my $ns = shift->{ns}) {
154 $msg{msgtype} = $type; 239 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
155 $ns->send_packet ("ext " . cf::to_json \%msg); 240 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
156 } 241 }
242}
243
244sub alloc {
245 my ($self) = @_;
246
247 my $id = $self->{ns}->alloc_wid;
248
249 my $proxy = bless {
250 id => $id,
251 }, "ext::widget::proxy";
252
253 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
254 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
255
256 $proxy
157} 257}
158 258
159sub new { 259sub new {
160 my ($self, $class, %args) = @_; 260 my ($self, $class, %args) = @_;
161 261
162 my $id = ++$self->{ns}{id}; 262 my $proxy = $self->alloc;
163 263
164 my $proxy = $self->{w}{$id} = bless { 264 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
165 id => $id,
166 }, "ext::widget::proxy";
167
168 Scalar::Util::weaken ($proxy->{ws} = $self); 265 Scalar::Util::weaken ($proxy->{ws} = $self);
169 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
170 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
171 266
172 for my $ev (grep /^on_/, keys %args) { 267 for my $ev (grep /^on_/, keys %args) {
173 push @{$proxy->{ev}{$ev}}, $args{$ev}; 268 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
174 $args{$ev} = 0;
175 } 269 }
176 270
177 $self->msg (ws_c => 271 $self->msg (ws_c =>
178 ws => $self->{w}{id}, 272 $self->{id},
179 id => $id, 273 $proxy->{id},
180 class => $class, 274 $class,
181 args => \%args, 275 \%args,
182 ); 276 );
183 277
184 $proxy 278 $proxy
279}
280
281sub template {
282 my ($self, $type, $template, $args, $done_cb) = @_;
283
284 my %cfg;
285
286 while (@$args) {
287 my ($name, $args) = splice @$args, 0, 2, ();
288
289 my $proxy = $self->alloc;
290
291 $self->{delete $args->{alias} or $name} = $proxy;
292
293 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
294 Scalar::Util::weaken ($proxy->{ws} = $self);
295
296 for my $ev (grep /^on_/, keys %$args) {
297 $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev});
298 }
299
300 $cfg{$name} = {
301 %$args,
302 id => $proxy->{id},
303 };
304 }
305
306 if ($done_cb) {
307 my $proxy = $self->alloc;
308 my $ocb = $done_cb;
309 $done_cb = $proxy->cb (sub {
310 undef $proxy;
311 undef $done_cb;
312 &$ocb
313 });
314 }
315
316 if ($type eq "face") {
317 $self->{ns}->send_face ($template, 100);
318 $self->{ns}->flush_fx;
319 }
320
321 $self->msg (ws_ct =>
322 $self->{id},
323 $type => $template,
324 $done_cb,
325 \%cfg,
326 );
327}
328
329sub find {
330 my ($self, @names) = @_;
331
332 my @res;
333 my @alloc;
334
335 for my $name (@names) {
336 push @res, $self->{ns}{widget_wkw}{$name} ||= do {
337 my $proxy = $self->alloc;
338
339 push @alloc, $proxy->{id} => $name;
340
341 $proxy
342 };
343 }
344
345 $self->msg (ws_a => @alloc)
346 if @alloc;
347
348 wantarray ? @res : $res[0]
185} 349}
186 350
187############################################################################# 351#############################################################################
188 352
189package ext::widget::proxy; 353package ext::widget::proxy;
192 my ($self) = @_; 356 my ($self) = @_;
193 357
194 delete $self->{ns}{widget}{$self->{id}}; 358 delete $self->{ns}{widget}{$self->{id}};
195 359
196 if (my $ws = $self->{ws}) { 360 if (my $ws = $self->{ws}) {
361 $self->msg (w_c => 0, "destroy");
197 delete $ws->{w}{$self->{id}}; 362 delete $ws->{_w}{$self->{id}};
198 $self->msg (w_c => name => "destroy");
199 } 363 }
364}
365
366sub cb {
367 my ($self, $cb) = @_;
368
369 my $proxy = bless {
370 ns => $self->{ns},
371 id => $self->{ns}->alloc_wid,
372 }, "ext::widget::callback";
373
374 Scalar::Util::weaken $proxy->{ns};
375
376 $self->{ns}{widget_cb}{$proxy->{id}} = $cb;
377
378 $proxy
379}
380
381sub oneshot_cb {
382 my ($self, $cb) = @_;
383
384 if ("CODE" eq ref $cb) {
385 my $ocb = $cb;
386 $cb = $self->cb (sub {
387 undef $cb;
388 &$ocb
389 });
390 }
391
392 $cb
200} 393}
201 394
202sub msg { 395sub msg {
203 my ($self, $type, %msg) = @_; 396 my ($self, $type, @arg) = @_;
397
398 if (my $ns = $self->{ns}) {
399 my @msg = ($type, $self->{id}, @arg);
400 warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
401 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
402 }
403}
404
405sub msg_cb {
406 my ($self, $cb, $type, @arg) = @_;
204 407
205 if (my $ws = $self->{ws}) { 408 if (my $ws = $self->{ws}) {
206 $ws->msg ($type,
207 %msg,
208 id => $self->{id},
209 );
210 }
211}
212
213sub msg_cb {
214 my ($self, $cb, $type, %msg) = @_;
215
216 if (my $ws = $self->{ws}) {
217
218 my $rid = ++$ws->{ns}{id}; 409 my $rid = $ws->{ns}->alloc_wid;
219
220 $self->msg ($type, %msg, rid => $rid);
221 410
222 if ($cb) { 411 if ($cb) {
223 $ws->{ns}{widget_return}{$rid} = $cb; 412 $ws->{ns}{widget_cb}{$rid} = sub {
413 delete $ws->{ns}{widget_cb}{$rid};
414 $ws->{ns}->free_wid ($rid);
415 &$cb
416 };
417
418 $self->msg ($type, $rid, @arg);
224 } else { 419 } else {
225 # synchronous case 420 # synchronous case
226 my $wait = new Coro::Signal; 421 my $wait = new Coro::Signal;
227 my @res; 422 my @res;
228 423
229 $ws->{ns}{widget_return}{$rid} = sub { 424 $ws->{ns}{widget_cb}{$rid} = sub {
425 delete $ws->{ns}{widget_cb}{$rid};
426 $ws->{ns}->free_wid ($rid);
427
230 @res = @_; 428 @res = @_;
231 $wait->send; 429 $wait->send;
232 }; 430 };
431 $self->msg ($type, $rid, @arg);
233 $wait->wait; 432 $wait->wait;
234 433
235 return @res; 434 return @res;
236 } 435 }
237 } 436 }
238 437
239 () 438 ()
240} 439}
241 440
242sub set { 441sub set {
243 my ($self, $member, $value) = @_; 442 my ($self, @kv) = @_;
244 443
245 $self->msg (w_s => name => $member, value => $value); 444 $self->msg (w_s => \@kv);
246} 445}
247 446
248sub get { 447sub get {
249 my ($self, $member, $cb) = @_; 448 my ($self, $member, $cb) = @_;
250 449
251 $self->msg_cb ($cb, w_g => name => $member); 450 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
252} 451}
253 452
254sub TO_JSON { 453sub TO_JSON {
255 { __widget_ref__ => $_[0]{id} } 454 { "\fw" => $_[0]{id} }
256} 455}
257 456
258our $AUTOLOAD; 457our $AUTOLOAD;
259 458
260sub AUTOLOAD { 459sub AUTOLOAD {
261 $AUTOLOAD =~ s/^.*:// 460 $AUTOLOAD =~ s/^.*://
262 or return; 461 or return;
263 462
264 my $self = shift; 463 my $self = shift;
265 464
465 #TODO: handle non-void context
266 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 466 $self->msg (w_c => 0, $AUTOLOAD, @_);
267 467
268 () 468 ()
269} 469}
270 470
471package ext::widget::callback;
472
473sub DESTROY {
474 my ($self) = @_;
475
476 if (my $ns = $self->{ns}) {
477 delete $ns->{widget_cb}{$self->{id}};
478 $ns->free_wid ($self->{id});
479 }
480}
481
482sub TO_JSON {
483 { "\fc" => $_[0]{id} }
484}
485

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines