ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/widget.ext
Revision: 1.21
Committed: Thu Dec 27 19:41:26 2007 UTC (16 years, 5 months ago) by root
Branch: MAIN
Changes since 1.20: +87 -12 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.17 sub demo_start {
102     my ($ns) = @_;
103    
104     my $ws = $ns->{csc} = $ns->new_widgetset;
105    
106 root 1.21 my ($tl, $entry) = $ws->template (inline => $cg_template,
107     [
108     toplevel => {},
109     entry => {
110     text => "xyz",
111     on_changed => sub {
112     warn "changed<@_>\n";#d#
113     },
114 root 1.20 },
115 root 1.21 ],
116 root 1.20 );
117    
118     $tl->show;
119    
120     $ns->{xxxw} = [$tl, $entry];#d#
121 root 1.17
122 root 1.20 # $ws->find ("setup_notebook")->add ($ws->{tab});
123     # $ws->find ("setup_dialog")->toggle_visibility;
124 root 1.17 }
125    
126 root 1.2 sub csc_start {
127     my ($ns) = @_;
128    
129     my $ws = $ns->{csc} = $ns->new_widgetset;
130    
131     my $w = $ws->new (Toplevel =>
132     min_w => 600,
133     min_h => 400,
134     x => "center",
135     y => "center",
136     title => "Character Creation",
137 root 1.3 has_close_button => 1,
138     on_delete => sub {
139     $ws->destroy;
140     },
141 root 1.2 );
142    
143     $w->add (my $ntb = $ws->new (Notebook => expand => 1));
144    
145 root 1.17 $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character");
146 root 1.2
147 root 1.17 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
148 root 1.2
149     for (
150 root 1.3 [0, "Str"],
151     [1, "Dex"],
152     [2, "Con"],
153     [3, "Int"],
154     [4, "Wis"],
155     [5, "Pow"],
156     [6, "Cha"],
157 root 1.2 ) {
158 root 1.3 my ($x, $label) = @$_;
159 root 1.2
160 root 1.9 $statstable->add_at ($x, 0, $ws->new (Label =>
161 root 1.2 can_hover => 1, can_events => 1,
162     align => +1, text => $label, tooltip => "#stat_$label",
163     ));
164 root 1.9 $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
165 root 1.2 can_hover => 1, can_events => 1,
166     align => +1, template => "88", tooltip => "#stat_$label",
167     ));
168     }
169    
170     csc_update_stats $ns;
171    
172 root 1.17 $ws->{tl} = $w;
173 root 1.2 $w->show;
174     }
175    
176 root 1.1 cf::player->attach (
177     on_login => sub {
178     my ($pl) = @_;
179    
180 root 1.8 return unless $cf::CFG{devel};
181 root 1.1
182     my $ns = $pl->ns;
183    
184 root 1.2 return unless $ns->{can_widget};
185 root 1.5 #csc_start $ns;
186 root 1.20 demo_start $ns;
187 root 1.1 },
188     );
189    
190     cf::register_exticmd w_e => sub {
191 root 1.19 my ($ns, $id, @args) = @_;
192 root 1.1
193 root 1.19 if (my $cb = $ns->{widget_cb}{$id}) {
194     $cb->(@args);
195 root 1.1 }
196    
197     ()
198     };
199    
200     sub cf::client::new_widgetset {
201     my ($self) = @_;
202    
203     my $id = ++$self->{id};
204    
205     my $ws = bless {
206     id => $id,
207     ns => $self,
208 root 1.12 _w => {},
209 root 1.1 }, "ext::widget::set";
210    
211 root 1.14 $ws->msg (ws_n => $id);
212 root 1.1
213     $ws
214     }
215    
216 root 1.19 sub cf::client::alloc_wid {
217     pop @{ $_[0]{ids} }
218     or ++$_[0]{id}
219     }
220    
221     sub cf::client::free_wid {
222     push @{ $_[0]{ids} }, $_[1];
223     }
224    
225 root 1.1 #############################################################################
226    
227     package ext::widget::set;
228    
229     sub DESTROY {
230     $_[0]->destroy;
231     }
232    
233     sub destroy {
234     my ($self) = @_;
235    
236 root 1.14 $self->msg (ws_d => $self->{id});
237 root 1.1 delete $self->{ns};
238     $_->destroy
239     for values %{ $self->{w} };
240     }
241    
242     sub msg {
243 root 1.14 my ($self, @msg) = @_;
244 root 1.1
245     if (my $ns = shift->{ns}) {
246 root 1.14 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
247     $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
248 root 1.1 }
249     }
250    
251 root 1.17 sub alloc {
252     my ($self) = @_;
253 root 1.1
254 root 1.19 my $id = $self->{ns}->alloc_wid;
255 root 1.1
256 root 1.13 my $proxy = bless {
257 root 1.1 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 root 1.17 $proxy
264     }
265    
266     sub new {
267     my ($self, $class, %args) = @_;
268    
269     my $proxy = $self->alloc;
270    
271     Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
272     Scalar::Util::weaken ($proxy->{ws} = $self);
273    
274 root 1.1 for my $ev (grep /^on_/, keys %args) {
275 root 1.19 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
276 root 1.1 }
277    
278     $self->msg (ws_c =>
279 root 1.16 $self->{id},
280 root 1.17 $proxy->{id},
281 root 1.14 $class,
282     \%args,
283 root 1.1 );
284    
285     $proxy
286     }
287    
288 root 1.20 sub template {
289 root 1.21 my ($self, $type, $template, $args, $done_cb) = @_;
290 root 1.20
291     my %cfg;
292     my @res;
293    
294 root 1.21 while (@$args) {
295     my ($name, $args) = splice @$args, 0, 2, ();
296 root 1.20
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 root 1.21 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 root 1.20 $self->msg (ws_ct =>
325     $self->{id},
326 root 1.21 $type => $template,
327     $done_cb,
328 root 1.20 \%cfg,
329     );
330    
331     @res
332     }
333    
334 root 1.17 sub 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    
356 root 1.1 #############################################################################
357    
358     package ext::widget::proxy;
359    
360     sub DESTROY {
361     my ($self) = @_;
362    
363     delete $self->{ns}{widget}{$self->{id}};
364    
365     if (my $ws = $self->{ws}) {
366 root 1.14 $self->msg (w_c => 0, "destroy");
367 root 1.12 delete $ws->{_w}{$self->{id}};
368 root 1.1 }
369     }
370    
371 root 1.19 sub 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    
386     sub 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
398     }
399    
400 root 1.1 sub msg {
401 root 1.14 my ($self, $type, @arg) = @_;
402 root 1.1
403 root 1.17 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 root 1.1 }
408     }
409    
410     sub msg_cb {
411 root 1.14 my ($self, $cb, $type, @arg) = @_;
412 root 1.1
413     if (my $ws = $self->{ws}) {
414 root 1.19 my $rid = $ws->{ns}->alloc_wid;
415 root 1.1
416 root 1.19 if ($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 root 1.1
423 root 1.19 $self->msg ($type, $rid, @arg);
424 root 1.1 } else {
425     # synchronous case
426     my $wait = new Coro::Signal;
427     my @res;
428    
429 root 1.19 $ws->{ns}{widget_cb}{$rid} = sub {
430     delete $ws->{ns}{widget_cb}{$rid};
431     $ws->{ns}->free_wid ($rid);
432    
433 root 1.1 @res = @_;
434     $wait->send;
435     };
436 root 1.19 $self->msg ($type, $rid, @arg);
437 root 1.1 $wait->wait;
438    
439     return @res;
440     }
441     }
442    
443     ()
444     }
445    
446     sub set {
447 root 1.12 my ($self, @kv) = @_;
448 root 1.1
449 root 1.14 $self->msg (w_s => \@kv);
450 root 1.1 }
451    
452     sub get {
453     my ($self, $member, $cb) = @_;
454    
455 root 1.19 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
456 root 1.1 }
457    
458     sub TO_JSON {
459 root 1.19 { "\fw" => $_[0]{id} }
460 root 1.1 }
461    
462     our $AUTOLOAD;
463    
464     sub AUTOLOAD {
465     $AUTOLOAD =~ s/^.*://
466     or return;
467    
468     my $self = shift;
469    
470 root 1.17 #TODO: handle non-void context
471     $self->msg (w_c => 0, $AUTOLOAD, @_);
472 root 1.1
473     ()
474     }
475    
476 root 1.19 package ext::widget::callback;
477    
478     sub 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    
487     sub TO_JSON {
488     { "\fc" => $_[0]{id} }
489     }
490