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.4 by root, Mon Jul 2 03:15:30 2007 UTC vs.
Revision 1.18 by root, Sun Aug 19 09:27:08 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
7# w_c id [rid] name args # widget method call 8# w_c id rid name @args # widget method call
8# w_s id name value # widget member set 9# w_s id @attr # widget member set
9# w_g id rid name # widget member get 10# w_g id rid @attr # widget member get
10# 11#
11# and expects the following exti message types 12# and expects the following exti message types
12# w_r rid res # widget call return 13# w_r rid res # widget call return
13# w_e id name args # widget_event 14# w_e id rid @args # widget_event
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 (__w_ => sub {
23 # cannot deserialise ATM 26 # cannot deserialise ATM
24 undef 27 undef
25 }); 28 });
26 }, 29 },
27); 30);
30 my ($ns) = @_; 33 my ($ns) = @_;
31 34
32 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { 35 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) {
33 $v->set_text ($ns->pl->ob->stats->$k); 36 $v->set_text ($ns->pl->ob->stats->$k);
34 } 37 }
38}
39
40sub demo_start {
41 my ($ns) = @_;
42
43 my $ws = $ns->{csc} = $ns->new_widgetset;
44
45 $ws->{tab} = $ws->new (Label => text => "dumb tst", c_tab => ["hull"]);
46
47 $ws->find ("setup_notebook")->add ($ws->{tab});
48 $ws->find ("setup_dialog")->toggle_visibility;
35} 49}
36 50
37sub csc_start { 51sub csc_start {
38 my ($ns) = @_; 52 my ($ns) = @_;
39 53
51 }, 65 },
52 ); 66 );
53 67
54 $w->add (my $ntb = $ws->new (Notebook => expand => 1)); 68 $w->add (my $ntb = $ws->new (Notebook => expand => 1));
55 69
56 $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); 70 $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character");
57 71
58 $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); 72 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
59 73
60 for ( 74 for (
61 [0, "Str"], 75 [0, "Str"],
62 [1, "Dex"], 76 [1, "Dex"],
63 [2, "Con"], 77 [2, "Con"],
66 [5, "Pow"], 80 [5, "Pow"],
67 [6, "Cha"], 81 [6, "Cha"],
68 ) { 82 ) {
69 my ($x, $label) = @$_; 83 my ($x, $label) = @$_;
70 84
71 $statstable->add ($x, 0, $ws->new (Label => 85 $statstable->add_at ($x, 0, $ws->new (Label =>
72 can_hover => 1, can_events => 1, 86 can_hover => 1, can_events => 1,
73 align => +1, text => $label, tooltip => "#stat_$label", 87 align => +1, text => $label, tooltip => "#stat_$label",
74 )); 88 ));
75 $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => 89 $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
76 can_hover => 1, can_events => 1, 90 can_hover => 1, can_events => 1,
77 align => +1, template => "88", tooltip => "#stat_$label", 91 align => +1, template => "88", tooltip => "#stat_$label",
78 )); 92 ));
79 } 93 }
80 94
81 csc_update_stats $ns; 95 csc_update_stats $ns;
82 96
97 $ws->{tl} = $w;
83 $w->show; 98 $w->show;
84} 99}
85 100
86cf::player->attach ( 101cf::player->attach (
87 on_login => sub { 102 on_login => sub {
90 return unless $cf::CFG{devel}; 105 return unless $cf::CFG{devel};
91 106
92 my $ns = $pl->ns; 107 my $ns = $pl->ns;
93 108
94 return unless $ns->{can_widget}; 109 return unless $ns->{can_widget};
95
96 csc_start $ns; 110 #csc_start $ns;
111 #demo_start $ns;
97 }, 112 },
98); 113);
99 114
100cf::register_exticmd w_e => sub { 115cf::register_exticmd w_e => sub {
101 my ($ns, $pkt) = @_; 116 my ($ns, $id, $rid, @args) = @_;
102 117
103 if (my $w = $ns->{widget}{$pkt->{id}}) { 118 if (my $w = $ns->{widget}{$id}) {
104 if (my $cb = $w->{ev}{$pkt->{name}}) { 119 if (my $cb = $w->{ev}{$rid}) {
105 $_->($w, @{ $pkt->{args} || [] }) 120 $cb->($w, @args);
106 for @$cb;
107 } 121 }
108 } 122 }
109 123
110 () 124 ()
111}; 125};
112 126
113cf::register_exticmd w_r => sub { 127cf::register_exticmd w_r => sub {
114 my ($ns, $pkt) = @_; 128 my ($ns, $rid, $res) = @_;
115 129
116 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { 130 if (my $cb = delete $ns->{widget_return}{$rid}) {
117 $cb->(@{$pkt->{res} || [] }); 131 $cb->(@$res);
118 } 132 }
119 133
120 () 134 ()
121}; 135};
122 136
126 my $id = ++$self->{id}; 140 my $id = ++$self->{id};
127 141
128 my $ws = bless { 142 my $ws = bless {
129 id => $id, 143 id => $id,
130 ns => $self, 144 ns => $self,
131 w => {}, 145 _w => {},
132 }, "ext::widget::set"; 146 }, "ext::widget::set";
133 147
134 $ws->msg (ws_n => id => $id); 148 $ws->msg (ws_n => $id);
135 149
136 $ws 150 $ws
137} 151}
138 152
139############################################################################# 153#############################################################################
145} 159}
146 160
147sub destroy { 161sub destroy {
148 my ($self) = @_; 162 my ($self) = @_;
149 163
150 $self->msg (ws_d => id => $self->{id}); 164 $self->msg (ws_d => $self->{id});
151 delete $self->{ns}; 165 delete $self->{ns};
152 $_->destroy 166 $_->destroy
153 for values %{ $self->{w} }; 167 for values %{ $self->{w} };
154} 168}
155 169
156sub msg { 170sub msg {
157 my ($self, $type, %msg) = @_; 171 my ($self, @msg) = @_;
158 172
159 if (my $ns = shift->{ns}) { 173 if (my $ns = shift->{ns}) {
160 $msg{msgtype} = $type; 174 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
161 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); 175 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
162 } 176 }
177}
178
179sub alloc {
180 my ($self) = @_;
181
182 my $id = ++$self->{ns}{id};
183
184 my $proxy = bless {
185 id => $id,
186 }, "ext::widget::proxy";
187
188 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
189 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
190
191 $proxy
163} 192}
164 193
165sub new { 194sub new {
166 my ($self, $class, %args) = @_; 195 my ($self, $class, %args) = @_;
167 196
168 my $id = ++$self->{ns}{id}; 197 my $proxy = $self->alloc;
169 198
170 my $proxy = $self->{w}{$id} = bless { 199 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
171 id => $id,
172 }, "ext::widget::proxy";
173
174 Scalar::Util::weaken ($proxy->{ws} = $self); 200 Scalar::Util::weaken ($proxy->{ws} = $self);
175 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
176 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
177 201
178 for my $ev (grep /^on_/, keys %args) { 202 for my $ev (grep /^on_/, keys %args) {
203 my $rid = ++$self->{ns}{id};
179 push @{$proxy->{ev}{$ev}}, $args{$ev}; 204 $proxy->{ev}{$rid} = $args{$ev};
180 $args{$ev} = 0; 205 $args{$ev} = $rid;
181 } 206 }
182 207
183 $self->msg (ws_c => 208 $self->msg (ws_c =>
184 ws => $self->{w}{id}, 209 $self->{id},
185 id => $id, 210 $proxy->{id},
186 class => $class, 211 $class,
187 args => \%args, 212 \%args,
188 ); 213 );
189 214
190 $proxy 215 $proxy
216}
217
218sub find {
219 my ($self, @names) = @_;
220
221 my @res;
222 my @alloc;
223
224 for my $name (@names) {
225 push @res, $self->{ns}{widget_wkw}{$name} ||= do {
226 my $proxy = $self->alloc;
227
228 push @alloc, $proxy->{id} => $name;
229
230 $proxy
231 };
232 }
233
234 $self->msg (ws_a => @alloc)
235 if @alloc;
236
237 wantarray ? @res : $res[0]
191} 238}
192 239
193############################################################################# 240#############################################################################
194 241
195package ext::widget::proxy; 242package ext::widget::proxy;
198 my ($self) = @_; 245 my ($self) = @_;
199 246
200 delete $self->{ns}{widget}{$self->{id}}; 247 delete $self->{ns}{widget}{$self->{id}};
201 248
202 if (my $ws = $self->{ws}) { 249 if (my $ws = $self->{ws}) {
250 $self->msg (w_c => 0, "destroy");
203 delete $ws->{w}{$self->{id}}; 251 delete $ws->{_w}{$self->{id}};
204 $self->msg (w_c => name => "destroy");
205 } 252 }
206} 253}
207 254
208sub msg { 255sub msg {
209 my ($self, $type, %msg) = @_; 256 my ($self, $type, @arg) = @_;
257
258 if (my $ns = $self->{ns}) {
259 my @msg = ($type, $self->{id}, @arg);
260 warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
261 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
262 }
263}
264
265sub msg_cb {
266 my ($self, $cb, $type, @arg) = @_;
210 267
211 if (my $ws = $self->{ws}) { 268 if (my $ws = $self->{ws}) {
212 $ws->msg ($type,
213 %msg,
214 id => $self->{id},
215 );
216 }
217}
218
219sub msg_cb {
220 my ($self, $cb, $type, %msg) = @_;
221
222 if (my $ws = $self->{ws}) {
223
224 my $rid = ++$ws->{ns}{id}; 269 my $rid = ++$ws->{ns}{id};
225 270
226 $self->msg ($type, %msg, rid => $rid); 271 $self->msg ($type, $rid, @arg);
227 272
228 if ($cb) { 273 if ($cb) {
229 $ws->{ns}{widget_return}{$rid} = $cb; 274 $ws->{ns}{widget_return}{$rid} = $cb;
230 } else { 275 } else {
231 # synchronous case 276 # synchronous case
244 289
245 () 290 ()
246} 291}
247 292
248sub set { 293sub set {
249 my ($self, $member, $value) = @_; 294 my ($self, @kv) = @_;
250 295
251 $self->msg (w_s => name => $member, value => $value); 296 $self->msg (w_s => \@kv);
252} 297}
253 298
254sub get { 299sub get {
255 my ($self, $member, $cb) = @_; 300 my ($self, $member, $cb) = @_;
256 301
257 $self->msg_cb ($cb, w_g => name => $member); 302 $self->msg_cb ($cb, w_g => [$member]);
258} 303}
259 304
260sub TO_JSON { 305sub TO_JSON {
261 { __widget_ref__ => $_[0]{id} } 306 { __w_ => $_[0]{id} }
262} 307}
263 308
264our $AUTOLOAD; 309our $AUTOLOAD;
265 310
266sub AUTOLOAD { 311sub AUTOLOAD {
267 $AUTOLOAD =~ s/^.*:// 312 $AUTOLOAD =~ s/^.*://
268 or return; 313 or return;
269 314
270 my $self = shift; 315 my $self = shift;
271 316
317 #TODO: handle non-void context
272 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 318 $self->msg (w_c => 0, $AUTOLOAD, @_);
273 319
274 () 320 ()
275} 321}
276 322

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines