ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/widget.ext
Revision: 1.22
Committed: Fri Dec 28 12:44:45 2007 UTC (16 years, 5 months ago) by root
Branch: MAIN
Changes since 1.21: +29 -34 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #! perl # mandatory depends=login
2    
3     # sends the following ext message types
4 root 1.17 # ws_a id name... # associate well-known widget with given id
5 root 1.14 # ws_n ws # widgetset new
6     # ws_d ws # widgetset destroy
7 root 1.17 # ws_c ws id class @args # widgetset create
8 root 1.21 # ws_ct ws ttype ttext done_cb \%cfg # widgetset create from template
9 root 1.17 # w_c id rid name @args # widget method call
10 root 1.10 # w_s id @attr # widget member set
11     # w_g id rid @attr # widget member get
12 root 1.1 #
13     # and expects the following exti message types
14 root 1.19 # w_e id @args # widget_call
15 root 1.1
16 root 1.13 our $DEBUG = 1;
17    
18 root 1.1 cf::client->attach (
19     on_connect => sub {
20     my ($ns) = @_;
21    
22 root 1.4 Scalar::Util::weaken (my $weakns = $ns);
23    
24 root 1.1 $ns->{id} = "a";
25 root 1.19 $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub {
26     $weakns->{widget}{$_[0]}
27 root 1.4 });
28 root 1.1 },
29     );
30    
31 root 1.2 sub csc_update_stats {
32     my ($ns) = @_;
33    
34     while (my ($k, $v) = each %{ $ns->{csc}{stat} }) {
35     $v->set_text ($ns->pl->ob->stats->$k);
36     }
37     }
38    
39 root 1.21 my $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     ]
98     EOF
99     die if $@;
100    
101 root 1.2 sub csc_start {
102     my ($ns) = @_;
103    
104     my $ws = $ns->{csc} = $ns->new_widgetset;
105    
106     my $w = $ws->new (Toplevel =>
107     min_w => 600,
108     min_h => 400,
109     x => "center",
110     y => "center",
111     title => "Character Creation",
112 root 1.3 has_close_button => 1,
113     on_delete => sub {
114     $ws->destroy;
115     },
116 root 1.2 );
117    
118     $w->add (my $ntb = $ws->new (Notebook => expand => 1));
119    
120 root 1.17 $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character");
121 root 1.2
122 root 1.17 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
123 root 1.2
124     for (
125 root 1.3 [0, "Str"],
126     [1, "Dex"],
127     [2, "Con"],
128     [3, "Int"],
129     [4, "Wis"],
130     [5, "Pow"],
131     [6, "Cha"],
132 root 1.2 ) {
133 root 1.3 my ($x, $label) = @$_;
134 root 1.2
135 root 1.9 $statstable->add_at ($x, 0, $ws->new (Label =>
136 root 1.2 can_hover => 1, can_events => 1,
137     align => +1, text => $label, tooltip => "#stat_$label",
138     ));
139 root 1.9 $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
140 root 1.2 can_hover => 1, can_events => 1,
141     align => +1, template => "88", tooltip => "#stat_$label",
142     ));
143     }
144    
145     csc_update_stats $ns;
146    
147 root 1.17 $ws->{tl} = $w;
148 root 1.2 $w->show;
149 root 1.22
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;
168 root 1.2 }
169    
170 root 1.1 cf::player->attach (
171     on_login => sub {
172     my ($pl) = @_;
173    
174 root 1.22 my $ns = $pl->ns;
175     return unless $ns->{can_widget};
176    
177 root 1.8 return unless $cf::CFG{devel};
178 root 1.1
179 root 1.5 #csc_start $ns;
180 root 1.1 },
181     );
182    
183     cf::register_exticmd w_e => sub {
184 root 1.19 my ($ns, $id, @args) = @_;
185 root 1.1
186 root 1.19 if (my $cb = $ns->{widget_cb}{$id}) {
187     $cb->(@args);
188 root 1.1 }
189    
190     ()
191     };
192    
193     sub cf::client::new_widgetset {
194     my ($self) = @_;
195    
196     my $id = ++$self->{id};
197    
198     my $ws = bless {
199     id => $id,
200     ns => $self,
201 root 1.12 _w => {},
202 root 1.1 }, "ext::widget::set";
203    
204 root 1.14 $ws->msg (ws_n => $id);
205 root 1.1
206     $ws
207     }
208    
209 root 1.19 sub cf::client::alloc_wid {
210     pop @{ $_[0]{ids} }
211     or ++$_[0]{id}
212     }
213    
214     sub cf::client::free_wid {
215     push @{ $_[0]{ids} }, $_[1];
216     }
217    
218 root 1.1 #############################################################################
219    
220     package ext::widget::set;
221    
222     sub DESTROY {
223     $_[0]->destroy;
224     }
225    
226     sub destroy {
227     my ($self) = @_;
228    
229 root 1.14 $self->msg (ws_d => $self->{id});
230 root 1.1 delete $self->{ns};
231     $_->destroy
232     for values %{ $self->{w} };
233     }
234    
235     sub msg {
236 root 1.14 my ($self, @msg) = @_;
237 root 1.1
238     if (my $ns = shift->{ns}) {
239 root 1.14 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
240     $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
241 root 1.1 }
242     }
243    
244 root 1.17 sub alloc {
245     my ($self) = @_;
246 root 1.1
247 root 1.19 my $id = $self->{ns}->alloc_wid;
248 root 1.1
249 root 1.13 my $proxy = bless {
250 root 1.1 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 root 1.17 $proxy
257     }
258    
259     sub new {
260     my ($self, $class, %args) = @_;
261    
262     my $proxy = $self->alloc;
263    
264     Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
265     Scalar::Util::weaken ($proxy->{ws} = $self);
266    
267 root 1.1 for my $ev (grep /^on_/, keys %args) {
268 root 1.19 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
269 root 1.1 }
270    
271     $self->msg (ws_c =>
272 root 1.16 $self->{id},
273 root 1.17 $proxy->{id},
274 root 1.14 $class,
275     \%args,
276 root 1.1 );
277    
278     $proxy
279     }
280    
281 root 1.20 sub template {
282 root 1.21 my ($self, $type, $template, $args, $done_cb) = @_;
283 root 1.20
284     my %cfg;
285    
286 root 1.21 while (@$args) {
287     my ($name, $args) = splice @$args, 0, 2, ();
288 root 1.20
289     my $proxy = $self->alloc;
290    
291 root 1.22 $self->{delete $args->{alias} or $name} = $proxy;
292    
293 root 1.20 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 root 1.21 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 root 1.22 if ($type eq "face") {
317     $self->{ns}->send_face ($template, 100);
318     $self->{ns}->flush_fx;
319     }
320    
321 root 1.20 $self->msg (ws_ct =>
322     $self->{id},
323 root 1.21 $type => $template,
324     $done_cb,
325 root 1.20 \%cfg,
326     );
327     }
328    
329 root 1.17 sub 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]
349     }
350    
351 root 1.1 #############################################################################
352    
353     package ext::widget::proxy;
354    
355     sub DESTROY {
356     my ($self) = @_;
357    
358     delete $self->{ns}{widget}{$self->{id}};
359    
360     if (my $ws = $self->{ws}) {
361 root 1.14 $self->msg (w_c => 0, "destroy");
362 root 1.12 delete $ws->{_w}{$self->{id}};
363 root 1.1 }
364     }
365    
366 root 1.19 sub 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    
381     sub 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
393     }
394    
395 root 1.1 sub msg {
396 root 1.14 my ($self, $type, @arg) = @_;
397 root 1.1
398 root 1.17 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 root 1.1 }
403     }
404    
405     sub msg_cb {
406 root 1.14 my ($self, $cb, $type, @arg) = @_;
407 root 1.1
408     if (my $ws = $self->{ws}) {
409 root 1.19 my $rid = $ws->{ns}->alloc_wid;
410 root 1.1
411 root 1.19 if ($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 root 1.1
418 root 1.19 $self->msg ($type, $rid, @arg);
419 root 1.1 } else {
420     # synchronous case
421     my $wait = new Coro::Signal;
422     my @res;
423    
424 root 1.19 $ws->{ns}{widget_cb}{$rid} = sub {
425     delete $ws->{ns}{widget_cb}{$rid};
426     $ws->{ns}->free_wid ($rid);
427    
428 root 1.1 @res = @_;
429     $wait->send;
430     };
431 root 1.19 $self->msg ($type, $rid, @arg);
432 root 1.1 $wait->wait;
433    
434     return @res;
435     }
436     }
437    
438     ()
439     }
440    
441     sub set {
442 root 1.12 my ($self, @kv) = @_;
443 root 1.1
444 root 1.14 $self->msg (w_s => \@kv);
445 root 1.1 }
446    
447     sub get {
448     my ($self, $member, $cb) = @_;
449    
450 root 1.19 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
451 root 1.1 }
452    
453     sub TO_JSON {
454 root 1.19 { "\fw" => $_[0]{id} }
455 root 1.1 }
456    
457     our $AUTOLOAD;
458    
459     sub AUTOLOAD {
460     $AUTOLOAD =~ s/^.*://
461     or return;
462    
463     my $self = shift;
464    
465 root 1.17 #TODO: handle non-void context
466     $self->msg (w_c => 0, $AUTOLOAD, @_);
467 root 1.1
468     ()
469     }
470    
471 root 1.19 package ext::widget::callback;
472    
473     sub 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    
482     sub TO_JSON {
483     { "\fc" => $_[0]{id} }
484     }
485