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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines