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.8 by root, Mon Jul 16 19:43:32 2007 UTC vs.
Revision 1.21 by root, Thu Dec 27 19:41:26 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); 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
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 $@;
100
37sub demo_map { 101sub demo_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;
41 105
42 my $w = $ws->new (Toplevel => 106 my ($tl, $entry) = $ws->template (inline => $cg_template,
43 w => 200, 107 [
44 h => 200, 108 toplevel => {},
45 x => "center", 109 entry => {
46 y => "center", 110 text => "xyz",
47 title => "Worldmap", 111 on_changed => sub {
48 has_close_button => 1, 112 warn "changed<@_>\n";#d#
49 on_delete => sub { 113 },
50 $ws->destroy; 114 },
51 }, 115 ],
52 ); 116 );
53 117
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 $fixed = $ws->new (Fixed => expand => 1));
59 $fixed->add ($ws->new (Face => expand => 1, size_w => undef, size_h => undef, face => $face), abs => 0, 0, rel => 1, 1);
60 $fixed->add ($ws->new (Label => text => "lb1"), abs => 10, 10, rel => 1, 1);
61
62 $w->show; 118 $tl->show;
119
120 $ns->{xxxw} = [$tl, $entry];#d#
121
122# $ws->find ("setup_notebook")->add ($ws->{tab});
123# $ws->find ("setup_dialog")->toggle_visibility;
63} 124}
64 125
65sub csc_start { 126sub csc_start {
66 my ($ns) = @_; 127 my ($ns) = @_;
67 128
79 }, 140 },
80 ); 141 );
81 142
82 $w->add (my $ntb = $ws->new (Notebook => expand => 1)); 143 $w->add (my $ntb = $ws->new (Notebook => expand => 1));
83 144
84 $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); 145 $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character");
85 146
86 $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); 147 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
87 148
88 for ( 149 for (
89 [0, "Str"], 150 [0, "Str"],
90 [1, "Dex"], 151 [1, "Dex"],
91 [2, "Con"], 152 [2, "Con"],
94 [5, "Pow"], 155 [5, "Pow"],
95 [6, "Cha"], 156 [6, "Cha"],
96 ) { 157 ) {
97 my ($x, $label) = @$_; 158 my ($x, $label) = @$_;
98 159
99 $statstable->add ($x, 0, $ws->new (Label => 160 $statstable->add_at ($x, 0, $ws->new (Label =>
100 can_hover => 1, can_events => 1, 161 can_hover => 1, can_events => 1,
101 align => +1, text => $label, tooltip => "#stat_$label", 162 align => +1, text => $label, tooltip => "#stat_$label",
102 )); 163 ));
103 $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => 164 $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
104 can_hover => 1, can_events => 1, 165 can_hover => 1, can_events => 1,
105 align => +1, template => "88", tooltip => "#stat_$label", 166 align => +1, template => "88", tooltip => "#stat_$label",
106 )); 167 ));
107 } 168 }
108 169
109 csc_update_stats $ns; 170 csc_update_stats $ns;
110 171
172 $ws->{tl} = $w;
111 $w->show; 173 $w->show;
112} 174}
113 175
114cf::player->attach ( 176cf::player->attach (
115 on_login => sub { 177 on_login => sub {
118 return unless $cf::CFG{devel}; 180 return unless $cf::CFG{devel};
119 181
120 my $ns = $pl->ns; 182 my $ns = $pl->ns;
121 183
122 return unless $ns->{can_widget}; 184 return unless $ns->{can_widget};
123
124 demo_map $ns;
125 #csc_start $ns; 185 #csc_start $ns;
186 demo_start $ns;
126 }, 187 },
127); 188);
128 189
129cf::register_exticmd w_e => sub { 190cf::register_exticmd w_e => sub {
130 my ($ns, $pkt) = @_; 191 my ($ns, $id, @args) = @_;
131 192
132 if (my $w = $ns->{widget}{$pkt->{id}}) { 193 if (my $cb = $ns->{widget_cb}{$id}) {
133 if (my $cb = $w->{ev}{$pkt->{name}}) { 194 $cb->(@args);
134 $_->($w, @{ $pkt->{args} || [] })
135 for @$cb;
136 }
137 }
138
139 ()
140};
141
142cf::register_exticmd w_r => sub {
143 my ($ns, $pkt) = @_;
144
145 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) {
146 $cb->(@{$pkt->{res} || [] });
147 } 195 }
148 196
149 () 197 ()
150}; 198};
151 199
155 my $id = ++$self->{id}; 203 my $id = ++$self->{id};
156 204
157 my $ws = bless { 205 my $ws = bless {
158 id => $id, 206 id => $id,
159 ns => $self, 207 ns => $self,
160 w => {}, 208 _w => {},
161 }, "ext::widget::set"; 209 }, "ext::widget::set";
162 210
163 $ws->msg (ws_n => id => $id); 211 $ws->msg (ws_n => $id);
164 212
165 $ws 213 $ws
214}
215
216sub cf::client::alloc_wid {
217 pop @{ $_[0]{ids} }
218 or ++$_[0]{id}
219}
220
221sub cf::client::free_wid {
222 push @{ $_[0]{ids} }, $_[1];
166} 223}
167 224
168############################################################################# 225#############################################################################
169 226
170package ext::widget::set; 227package ext::widget::set;
174} 231}
175 232
176sub destroy { 233sub destroy {
177 my ($self) = @_; 234 my ($self) = @_;
178 235
179 $self->msg (ws_d => id => $self->{id}); 236 $self->msg (ws_d => $self->{id});
180 delete $self->{ns}; 237 delete $self->{ns};
181 $_->destroy 238 $_->destroy
182 for values %{ $self->{w} }; 239 for values %{ $self->{w} };
183} 240}
184 241
185sub msg { 242sub msg {
186 my ($self, $type, %msg) = @_; 243 my ($self, @msg) = @_;
187 244
188 if (my $ns = shift->{ns}) { 245 if (my $ns = shift->{ns}) {
189 $msg{msgtype} = $type; 246 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
190 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); 247 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
191 } 248 }
249}
250
251sub alloc {
252 my ($self) = @_;
253
254 my $id = $self->{ns}->alloc_wid;
255
256 my $proxy = bless {
257 id => $id,
258 }, "ext::widget::proxy";
259
260 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
261 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
262
263 $proxy
192} 264}
193 265
194sub new { 266sub new {
195 my ($self, $class, %args) = @_; 267 my ($self, $class, %args) = @_;
196 268
197 my $id = ++$self->{ns}{id}; 269 my $proxy = $self->alloc;
198 270
199 my $proxy = $self->{w}{$id} = bless { 271 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
200 id => $id,
201 }, "ext::widget::proxy";
202
203 Scalar::Util::weaken ($proxy->{ws} = $self); 272 Scalar::Util::weaken ($proxy->{ws} = $self);
204 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
205 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
206 273
207 for my $ev (grep /^on_/, keys %args) { 274 for my $ev (grep /^on_/, keys %args) {
208 push @{$proxy->{ev}{$ev}}, $args{$ev}; 275 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
209 $args{$ev} = 0;
210 } 276 }
211 277
212 $self->msg (ws_c => 278 $self->msg (ws_c =>
213 ws => $self->{w}{id}, 279 $self->{id},
214 id => $id, 280 $proxy->{id},
215 class => $class, 281 $class,
216 args => \%args, 282 \%args,
217 ); 283 );
218 284
219 $proxy 285 $proxy
286}
287
288sub template {
289 my ($self, $type, $template, $args, $done_cb) = @_;
290
291 my %cfg;
292 my @res;
293
294 while (@$args) {
295 my ($name, $args) = splice @$args, 0, 2, ();
296
297 my $proxy = $self->alloc;
298
299 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
300 Scalar::Util::weaken ($proxy->{ws} = $self);
301
302 for my $ev (grep /^on_/, keys %$args) {
303 $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev});
304 }
305
306 $cfg{$name} = {
307 %$args,
308 id => $proxy->{id},
309 };
310
311 push @res, $proxy;
312 }
313
314 if ($done_cb) {
315 my $proxy = $self->alloc;
316 my $ocb = $done_cb;
317 $done_cb = $proxy->cb (sub {
318 undef $proxy;
319 undef $done_cb;
320 &$ocb
321 });
322 }
323
324 $self->msg (ws_ct =>
325 $self->{id},
326 $type => $template,
327 $done_cb,
328 \%cfg,
329 );
330
331 @res
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]
220} 354}
221 355
222############################################################################# 356#############################################################################
223 357
224package ext::widget::proxy; 358package ext::widget::proxy;
227 my ($self) = @_; 361 my ($self) = @_;
228 362
229 delete $self->{ns}{widget}{$self->{id}}; 363 delete $self->{ns}{widget}{$self->{id}};
230 364
231 if (my $ws = $self->{ws}) { 365 if (my $ws = $self->{ws}) {
366 $self->msg (w_c => 0, "destroy");
232 delete $ws->{w}{$self->{id}}; 367 delete $ws->{_w}{$self->{id}};
233 $self->msg (w_c => name => "destroy");
234 } 368 }
369}
370
371sub cb {
372 my ($self, $cb) = @_;
373
374 my $proxy = bless {
375 ns => $self->{ns},
376 id => $self->{ns}->alloc_wid,
377 }, "ext::widget::callback";
378
379 Scalar::Util::weaken $proxy->{ns};
380
381 $self->{ns}{widget_cb}{$proxy->{id}} = $cb;
382
383 $proxy
384}
385
386sub oneshot_cb {
387 my ($self, $cb) = @_;
388
389 if ("CODE" eq ref $cb) {
390 my $ocb = $cb;
391 $cb = $self->cb (sub {
392 undef $cb;
393 &$ocb
394 });
395 }
396
397 $cb
235} 398}
236 399
237sub msg { 400sub msg {
238 my ($self, $type, %msg) = @_; 401 my ($self, $type, @arg) = @_;
402
403 if (my $ns = $self->{ns}) {
404 my @msg = ($type, $self->{id}, @arg);
405 warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
406 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
407 }
408}
409
410sub msg_cb {
411 my ($self, $cb, $type, @arg) = @_;
239 412
240 if (my $ws = $self->{ws}) { 413 if (my $ws = $self->{ws}) {
241 $ws->msg ($type,
242 %msg,
243 id => $self->{id},
244 );
245 }
246}
247
248sub msg_cb {
249 my ($self, $cb, $type, %msg) = @_;
250
251 if (my $ws = $self->{ws}) {
252
253 my $rid = ++$ws->{ns}{id}; 414 my $rid = $ws->{ns}->alloc_wid;
254
255 $self->msg ($type, %msg, rid => $rid);
256 415
257 if ($cb) { 416 if ($cb) {
258 $ws->{ns}{widget_return}{$rid} = $cb; 417 $ws->{ns}{widget_cb}{$rid} = sub {
418 delete $ws->{ns}{widget_cb}{$rid};
419 $ws->{ns}->free_wid ($rid);
420 &$cb
421 };
422
423 $self->msg ($type, $rid, @arg);
259 } else { 424 } else {
260 # synchronous case 425 # synchronous case
261 my $wait = new Coro::Signal; 426 my $wait = new Coro::Signal;
262 my @res; 427 my @res;
263 428
264 $ws->{ns}{widget_return}{$rid} = sub { 429 $ws->{ns}{widget_cb}{$rid} = sub {
430 delete $ws->{ns}{widget_cb}{$rid};
431 $ws->{ns}->free_wid ($rid);
432
265 @res = @_; 433 @res = @_;
266 $wait->send; 434 $wait->send;
267 }; 435 };
436 $self->msg ($type, $rid, @arg);
268 $wait->wait; 437 $wait->wait;
269 438
270 return @res; 439 return @res;
271 } 440 }
272 } 441 }
273 442
274 () 443 ()
275} 444}
276 445
277sub set { 446sub set {
278 my ($self, $member, $value) = @_; 447 my ($self, @kv) = @_;
279 448
280 $self->msg (w_s => name => $member, value => $value); 449 $self->msg (w_s => \@kv);
281} 450}
282 451
283sub get { 452sub get {
284 my ($self, $member, $cb) = @_; 453 my ($self, $member, $cb) = @_;
285 454
286 $self->msg_cb ($cb, w_g => name => $member); 455 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
287} 456}
288 457
289sub TO_JSON { 458sub TO_JSON {
290 { __widget_ref__ => $_[0]{id} } 459 { "\fw" => $_[0]{id} }
291} 460}
292 461
293our $AUTOLOAD; 462our $AUTOLOAD;
294 463
295sub AUTOLOAD { 464sub AUTOLOAD {
296 $AUTOLOAD =~ s/^.*:// 465 $AUTOLOAD =~ s/^.*://
297 or return; 466 or return;
298 467
299 my $self = shift; 468 my $self = shift;
300 469
470 #TODO: handle non-void context
301 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 471 $self->msg (w_c => 0, $AUTOLOAD, @_);
302 472
303 () 473 ()
304} 474}
305 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