ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/widget.ext
Revision: 1.4
Committed: Mon Jul 2 03:15:30 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
Changes since 1.3: +7 -1 lines
Log Message:
convert to JSON::XS 1.4 api and implement a per-client json transcoder

File Contents

# User Rev Content
1 root 1.1 #! perl # mandatory depends=login
2    
3     # sends the following ext message types
4     # ws_n id # widgetset new
5     # ws_d id # widgetset destroy
6     # ws_c ws id class args # widgetset create
7     # w_c id [rid] name args # widget method call
8     # w_s id name value # widget member set
9     # w_g id rid name # widget member get
10     #
11     # and expects the following exti message types
12     # w_r rid res # widget call return
13     # w_e id name args # widget_event
14    
15     cf::client->attach (
16     on_connect => sub {
17     my ($ns) = @_;
18    
19 root 1.4 Scalar::Util::weaken (my $weakns = $ns);
20    
21 root 1.1 $ns->{id} = "a";
22 root 1.4 $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub {
23     # cannot deserialise ATM
24     undef
25     });
26 root 1.1 },
27     );
28    
29 root 1.2 sub csc_update_stats {
30     my ($ns) = @_;
31    
32     while (my ($k, $v) = each %{ $ns->{csc}{stat} }) {
33     $v->set_text ($ns->pl->ob->stats->$k);
34     }
35     }
36    
37     sub csc_start {
38     my ($ns) = @_;
39    
40     my $ws = $ns->{csc} = $ns->new_widgetset;
41    
42     my $w = $ws->new (Toplevel =>
43     min_w => 600,
44     min_h => 400,
45     x => "center",
46     y => "center",
47     title => "Character Creation",
48 root 1.3 has_close_button => 1,
49     on_delete => sub {
50     $ws->destroy;
51     },
52 root 1.2 );
53    
54     $w->add (my $ntb = $ws->new (Notebook => expand => 1));
55    
56     $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character");
57    
58     $stats->add (0, 0, (my $statstable = $ws->new ("Table")));
59    
60     for (
61 root 1.3 [0, "Str"],
62     [1, "Dex"],
63     [2, "Con"],
64     [3, "Int"],
65     [4, "Wis"],
66     [5, "Pow"],
67     [6, "Cha"],
68 root 1.2 ) {
69 root 1.3 my ($x, $label) = @$_;
70 root 1.2
71     $statstable->add ($x, 0, $ws->new (Label =>
72     can_hover => 1, can_events => 1,
73     align => +1, text => $label, tooltip => "#stat_$label",
74     ));
75     $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
76     can_hover => 1, can_events => 1,
77     align => +1, template => "88", tooltip => "#stat_$label",
78     ));
79     }
80    
81     csc_update_stats $ns;
82    
83     $w->show;
84     }
85    
86 root 1.1 cf::player->attach (
87     on_login => sub {
88     my ($pl) = @_;
89    
90 root 1.2 return unless $cf::CFG{devel};
91 root 1.1
92     my $ns = $pl->ns;
93    
94 root 1.2 return unless $ns->{can_widget};
95 root 1.1
96 root 1.2 csc_start $ns;
97 root 1.1 },
98     );
99    
100     cf::register_exticmd w_e => sub {
101     my ($ns, $pkt) = @_;
102    
103     if (my $w = $ns->{widget}{$pkt->{id}}) {
104     if (my $cb = $w->{ev}{$pkt->{name}}) {
105     $_->($w, @{ $pkt->{args} || [] })
106     for @$cb;
107     }
108     }
109    
110     ()
111     };
112    
113     cf::register_exticmd w_r => sub {
114     my ($ns, $pkt) = @_;
115    
116     if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) {
117     $cb->(@{$pkt->{res} || [] });
118     }
119    
120     ()
121     };
122    
123     sub cf::client::new_widgetset {
124     my ($self) = @_;
125    
126     my $id = ++$self->{id};
127    
128     my $ws = bless {
129     id => $id,
130     ns => $self,
131     w => {},
132     }, "ext::widget::set";
133    
134     $ws->msg (ws_n => id => $id);
135    
136     $ws
137     }
138    
139     #############################################################################
140    
141     package ext::widget::set;
142    
143     sub DESTROY {
144     $_[0]->destroy;
145     }
146    
147     sub destroy {
148     my ($self) = @_;
149    
150     $self->msg (ws_d => id => $self->{id});
151     delete $self->{ns};
152     $_->destroy
153     for values %{ $self->{w} };
154     }
155    
156     sub msg {
157     my ($self, $type, %msg) = @_;
158    
159     if (my $ns = shift->{ns}) {
160     $msg{msgtype} = $type;
161 root 1.4 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg));
162 root 1.1 }
163     }
164    
165     sub new {
166     my ($self, $class, %args) = @_;
167    
168     my $id = ++$self->{ns}{id};
169    
170     my $proxy = $self->{w}{$id} = bless {
171     id => $id,
172     }, "ext::widget::proxy";
173    
174     Scalar::Util::weaken ($proxy->{ws} = $self);
175     Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
176     Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
177    
178     for my $ev (grep /^on_/, keys %args) {
179     push @{$proxy->{ev}{$ev}}, $args{$ev};
180     $args{$ev} = 0;
181     }
182    
183     $self->msg (ws_c =>
184     ws => $self->{w}{id},
185     id => $id,
186     class => $class,
187     args => \%args,
188     );
189    
190     $proxy
191     }
192    
193     #############################################################################
194    
195     package ext::widget::proxy;
196    
197     sub DESTROY {
198     my ($self) = @_;
199    
200     delete $self->{ns}{widget}{$self->{id}};
201    
202     if (my $ws = $self->{ws}) {
203     delete $ws->{w}{$self->{id}};
204     $self->msg (w_c => name => "destroy");
205     }
206     }
207    
208     sub msg {
209     my ($self, $type, %msg) = @_;
210    
211     if (my $ws = $self->{ws}) {
212     $ws->msg ($type,
213     %msg,
214     id => $self->{id},
215     );
216     }
217     }
218    
219     sub msg_cb {
220     my ($self, $cb, $type, %msg) = @_;
221    
222     if (my $ws = $self->{ws}) {
223    
224     my $rid = ++$ws->{ns}{id};
225    
226     $self->msg ($type, %msg, rid => $rid);
227    
228     if ($cb) {
229     $ws->{ns}{widget_return}{$rid} = $cb;
230     } else {
231     # synchronous case
232     my $wait = new Coro::Signal;
233     my @res;
234    
235     $ws->{ns}{widget_return}{$rid} = sub {
236     @res = @_;
237     $wait->send;
238     };
239     $wait->wait;
240    
241     return @res;
242     }
243     }
244    
245     ()
246     }
247    
248     sub set {
249     my ($self, $member, $value) = @_;
250    
251     $self->msg (w_s => name => $member, value => $value);
252     }
253    
254     sub get {
255     my ($self, $member, $cb) = @_;
256    
257     $self->msg_cb ($cb, w_g => name => $member);
258     }
259    
260     sub TO_JSON {
261     { __widget_ref__ => $_[0]{id} }
262     }
263    
264     our $AUTOLOAD;
265    
266     sub AUTOLOAD {
267     $AUTOLOAD =~ s/^.*://
268     or return;
269    
270     my $self = shift;
271    
272     $self->msg (w_c => name => $AUTOLOAD, args => \@_);
273    
274     ()
275     }
276