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.9 by root, Fri Jul 20 22:34:56 2007 UTC vs.
Revision 1.16 by root, Tue Jul 24 19:33:57 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_n id # widgetset new 4# ws_n ws # widgetset new
5# ws_d id # widgetset destroy 5# ws_d ws # widgetset destroy
6# ws_c ws id class args # widgetset create 6# ws_c ws id class args # widgetset create
7# w_c id [rid] name args # widget method call 7# w_c id rid name args # widget method call
8# w_s id name value # widget member set 8# w_s id @attr # widget member set
9# w_g id rid name # widget member get 9# w_g id rid @attr # widget member get
10# 10#
11# and expects the following exti message types 11# and expects the following exti message types
12# w_r rid res # widget call return 12# w_r rid res # widget call return
13# w_e id name args # widget_event 13# w_e id name args # widget_event
14 14
15our $DEBUG = 1;
16
15cf::client->attach ( 17cf::client->attach (
16 on_connect => sub { 18 on_connect => sub {
17 my ($ns) = @_; 19 my ($ns) = @_;
18 20
19 Scalar::Util::weaken (my $weakns = $ns); 21 Scalar::Util::weaken (my $weakns = $ns);
20 22
21 $ns->{id} = "a"; 23 $ns->{id} = "a";
22 $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { 24 $ns->{json_coder}->filter_json_single_key_object (__w_ => sub {
23 # cannot deserialise ATM 25 # cannot deserialise ATM
24 undef 26 undef
25 }); 27 });
26 }, 28 },
27); 29);
30 my ($ns) = @_; 32 my ($ns) = @_;
31 33
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}
36
37sub demo_map {
38 my ($ns) = @_;
39
40 my $ws = $ns->{csc} = $ns->new_widgetset;
41
42 my $w = $ws->new (Toplevel =>
43 w => 200,
44 h => 200,
45 x => "center",
46 y => "center",
47 title => "Worldmap",
48 has_close_button => 1,
49 on_delete => sub {
50 $ws->destroy;
51 },
52 );
53
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;
63} 37}
64 38
65sub csc_start { 39sub csc_start {
66 my ($ns) = @_; 40 my ($ns) = @_;
67 41
118 return unless $cf::CFG{devel}; 92 return unless $cf::CFG{devel};
119 93
120 my $ns = $pl->ns; 94 my $ns = $pl->ns;
121 95
122 return unless $ns->{can_widget}; 96 return unless $ns->{can_widget};
123
124 demo_map $ns;
125 #csc_start $ns; 97 #csc_start $ns;
126 }, 98 },
127); 99);
128 100
129cf::register_exticmd w_e => sub { 101cf::register_exticmd w_e => sub {
130 my ($ns, $pkt) = @_; 102 my ($ns, $id, $name, $args) = @_;
131 103
132 if (my $w = $ns->{widget}{$pkt->{id}}) { 104 if (my $w = $ns->{widget}{$id}) {
133 if (my $cb = $w->{ev}{$pkt->{name}}) { 105 if (my $cb = $w->{ev}{$name}) {
134 $_->($w, @{ $pkt->{args} || [] }) 106 $_->($w, @$args)
135 for @$cb; 107 for @$cb;
136 } 108 }
137 } 109 }
138 110
139 () 111 ()
140}; 112};
141 113
142cf::register_exticmd w_r => sub { 114cf::register_exticmd w_r => sub {
143 my ($ns, $pkt) = @_; 115 my ($ns, $rid, $res) = @_;
144 116
145 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { 117 if (my $cb = delete $ns->{widget_return}{$rid}) {
146 $cb->(@{$pkt->{res} || [] }); 118 $cb->(@$res);
147 } 119 }
148 120
149 () 121 ()
150}; 122};
151 123
155 my $id = ++$self->{id}; 127 my $id = ++$self->{id};
156 128
157 my $ws = bless { 129 my $ws = bless {
158 id => $id, 130 id => $id,
159 ns => $self, 131 ns => $self,
160 w => {}, 132 _w => {},
161 }, "ext::widget::set"; 133 }, "ext::widget::set";
162 134
163 $ws->msg (ws_n => id => $id); 135 $ws->msg (ws_n => $id);
164 136
165 $ws 137 $ws
166} 138}
167 139
168############################################################################# 140#############################################################################
174} 146}
175 147
176sub destroy { 148sub destroy {
177 my ($self) = @_; 149 my ($self) = @_;
178 150
179 $self->msg (ws_d => id => $self->{id}); 151 $self->msg (ws_d => $self->{id});
180 delete $self->{ns}; 152 delete $self->{ns};
181 $_->destroy 153 $_->destroy
182 for values %{ $self->{w} }; 154 for values %{ $self->{w} };
183} 155}
184 156
185sub msg { 157sub msg {
186 my ($self, $type, %msg) = @_; 158 my ($self, @msg) = @_;
187 159
188 if (my $ns = shift->{ns}) { 160 if (my $ns = shift->{ns}) {
189 $msg{msgtype} = $type; 161 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
190 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); 162 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
191 } 163 }
192} 164}
193 165
194sub new { 166sub new {
195 my ($self, $class, %args) = @_; 167 my ($self, $class, %args) = @_;
196 168
197 my $id = ++$self->{ns}{id}; 169 my $id = ++$self->{ns}{id};
198 170
199 my $proxy = $self->{w}{$id} = bless { 171 my $proxy = bless {
200 id => $id, 172 id => $id,
201 }, "ext::widget::proxy"; 173 }, "ext::widget::proxy";
202 174
175 Scalar::Util::weaken ($self->{_w}{$id} = $proxy);
203 Scalar::Util::weaken ($proxy->{ws} = $self); 176 Scalar::Util::weaken ($proxy->{ws} = $self);
204 Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); 177 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
205 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); 178 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
206 179
207 for my $ev (grep /^on_/, keys %args) { 180 for my $ev (grep /^on_/, keys %args) {
208 push @{$proxy->{ev}{$ev}}, $args{$ev}; 181 push @{$proxy->{ev}{$ev}}, $args{$ev};
209 $args{$ev} = 0; 182 $args{$ev} = 0;
210 } 183 }
211 184
212 $self->msg (ws_c => 185 $self->msg (ws_c =>
213 ws => $self->{w}{id}, 186 $self->{id},
214 id => $id, 187 $id,
215 class => $class, 188 $class,
216 args => \%args, 189 \%args,
217 ); 190 );
218 191
219 $proxy 192 $proxy
220} 193}
221 194
227 my ($self) = @_; 200 my ($self) = @_;
228 201
229 delete $self->{ns}{widget}{$self->{id}}; 202 delete $self->{ns}{widget}{$self->{id}};
230 203
231 if (my $ws = $self->{ws}) { 204 if (my $ws = $self->{ws}) {
205 $self->msg (w_c => 0, "destroy");
232 delete $ws->{w}{$self->{id}}; 206 delete $ws->{_w}{$self->{id}};
233 $self->msg (w_c => name => "destroy");
234 } 207 }
235} 208}
236 209
237sub msg { 210sub msg {
238 my ($self, $type, %msg) = @_; 211 my ($self, $type, @arg) = @_;
239 212
240 if (my $ws = $self->{ws}) { 213 if (my $ws = $self->{ws}) {
241 $ws->msg ($type, 214 $ws->msg ($type, $self->{id}, @arg);
242 %msg,
243 id => $self->{id},
244 );
245 } 215 }
246} 216}
247 217
248sub msg_cb { 218sub msg_cb {
249 my ($self, $cb, $type, %msg) = @_; 219 my ($self, $cb, $type, @arg) = @_;
250 220
251 if (my $ws = $self->{ws}) { 221 if (my $ws = $self->{ws}) {
252
253 my $rid = ++$ws->{ns}{id}; 222 my $rid = ++$ws->{ns}{id};
254 223
255 $self->msg ($type, %msg, rid => $rid); 224 $self->msg ($type, $rid, @arg);
256 225
257 if ($cb) { 226 if ($cb) {
258 $ws->{ns}{widget_return}{$rid} = $cb; 227 $ws->{ns}{widget_return}{$rid} = $cb;
259 } else { 228 } else {
260 # synchronous case 229 # synchronous case
273 242
274 () 243 ()
275} 244}
276 245
277sub set { 246sub set {
278 my ($self, $member, $value) = @_; 247 my ($self, @kv) = @_;
279 248
280 $self->msg (w_s => name => $member, value => $value); 249 $self->msg (w_s => \@kv);
281} 250}
282 251
283sub get { 252sub get {
284 my ($self, $member, $cb) = @_; 253 my ($self, $member, $cb) = @_;
285 254
286 $self->msg_cb ($cb, w_g => name => $member); 255 $self->msg_cb ($cb, w_g => [$member]);
287} 256}
288 257
289sub TO_JSON { 258sub TO_JSON {
290 { __widget_ref__ => $_[0]{id} } 259 { __w_ => $_[0]{id} }
291} 260}
292 261
293our $AUTOLOAD; 262our $AUTOLOAD;
294 263
295sub AUTOLOAD { 264sub AUTOLOAD {
296 $AUTOLOAD =~ s/^.*:// 265 $AUTOLOAD =~ s/^.*://
297 or return; 266 or return;
298 267
299 my $self = shift; 268 my $self = shift;
300 269
301 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 270 $self->msg (w_c => 0, $AUTOLOAD, \@_);
302 271
303 () 272 ()
304} 273}
305 274

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines