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.2 by root, Mon Jun 25 07:40:53 2007 UTC vs.
Revision 1.19 by root, Sat Sep 1 07:22:20 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_e id name args # widget_event 13# w_e id @args # widget_call
14
15our $DEBUG = 1;
14 16
15cf::client->attach ( 17cf::client->attach (
16 on_connect => sub { 18 on_connect => sub {
17 my ($ns) = @_; 19 my ($ns) = @_;
18 20
21 Scalar::Util::weaken (my $weakns = $ns);
22
19 $ns->{id} = "a"; 23 $ns->{id} = "a";
24 $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub {
25 $weakns->{widget}{$_[0]}
26 });
20 }, 27 },
21); 28);
22 29
23sub csc_update_stats { 30sub csc_update_stats {
24 my ($ns) = @_; 31 my ($ns) = @_;
25 32
26 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { 33 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) {
27 $v->set_text ($ns->pl->ob->stats->$k); 34 $v->set_text ($ns->pl->ob->stats->$k);
28 } 35 }
36}
37
38sub demo_start {
39 my ($ns) = @_;
40
41 my $ws = $ns->{csc} = $ns->new_widgetset;
42
43 $ws->{tab} = $ws->new (Label => text => "dumb tst", c_tab => ["hull"]);
44
45 $ws->find ("setup_notebook")->add ($ws->{tab});
46 $ws->find ("setup_dialog")->toggle_visibility;
29} 47}
30 48
31sub csc_start { 49sub csc_start {
32 my ($ns) = @_; 50 my ($ns) = @_;
33 51
37 min_w => 600, 55 min_w => 600,
38 min_h => 400, 56 min_h => 400,
39 x => "center", 57 x => "center",
40 y => "center", 58 y => "center",
41 title => "Character Creation", 59 title => "Character Creation",
60 has_close_button => 1,
61 on_delete => sub {
62 $ws->destroy;
63 },
42 ); 64 );
43 65
44 $w->add (my $ntb = $ws->new (Notebook => expand => 1)); 66 $w->add (my $ntb = $ws->new (Notebook => expand => 1));
45 67
46 $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); 68 $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character");
47 69
48 $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); 70 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
49 71
50 for ( 72 for (
51 [0, "Str", "Strength"], 73 [0, "Str"],
52 [1, "Dex", "Dexterity"], 74 [1, "Dex"],
53 [2, "Con", "Constitution"], 75 [2, "Con"],
54 [3, "Int", "Intelligence"], 76 [3, "Int"],
55 [4, "Wis", "Wisdom"], 77 [4, "Wis"],
56 [5, "Pow", "Power"], 78 [5, "Pow"],
57 [6, "Cha", "Charisma"], 79 [6, "Cha"],
58 ) { 80 ) {
59 my ($x, $label, $description) = @$_; 81 my ($x, $label) = @$_;
60 82
61 $statstable->add ($x, 0, $ws->new (Label => 83 $statstable->add_at ($x, 0, $ws->new (Label =>
62 can_hover => 1, can_events => 1, 84 can_hover => 1, can_events => 1,
63 align => +1, text => $label, tooltip => "#stat_$label", 85 align => +1, text => $label, tooltip => "#stat_$label",
64 )); 86 ));
65 $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => 87 $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
66 can_hover => 1, can_events => 1, 88 can_hover => 1, can_events => 1,
67 align => +1, template => "88", tooltip => "#stat_$label", 89 align => +1, template => "88", tooltip => "#stat_$label",
68 )); 90 ));
69 } 91 }
70 92
71 csc_update_stats $ns; 93 csc_update_stats $ns;
72 94
95 $ws->{tl} = $w;
73 $w->show; 96 $w->show;
74} 97}
75 98
76cf::player->attach ( 99cf::player->attach (
77 on_login => sub { 100 on_login => sub {
80 return unless $cf::CFG{devel}; 103 return unless $cf::CFG{devel};
81 104
82 my $ns = $pl->ns; 105 my $ns = $pl->ns;
83 106
84 return unless $ns->{can_widget}; 107 return unless $ns->{can_widget};
85
86 csc_start $ns; 108 #csc_start $ns;
109 #demo_start $ns;
87 }, 110 },
88); 111);
89 112
90cf::register_exticmd w_e => sub { 113cf::register_exticmd w_e => sub {
91 my ($ns, $pkt) = @_; 114 my ($ns, $id, @args) = @_;
92 115
93 if (my $w = $ns->{widget}{$pkt->{id}}) { 116 if (my $cb = $ns->{widget_cb}{$id}) {
94 if (my $cb = $w->{ev}{$pkt->{name}}) { 117 $cb->(@args);
95 $_->($w, @{ $pkt->{args} || [] })
96 for @$cb;
97 }
98 }
99
100 ()
101};
102
103cf::register_exticmd w_r => sub {
104 my ($ns, $pkt) = @_;
105
106 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) {
107 $cb->(@{$pkt->{res} || [] });
108 } 118 }
109 119
110 () 120 ()
111}; 121};
112 122
116 my $id = ++$self->{id}; 126 my $id = ++$self->{id};
117 127
118 my $ws = bless { 128 my $ws = bless {
119 id => $id, 129 id => $id,
120 ns => $self, 130 ns => $self,
121 w => {}, 131 _w => {},
122 }, "ext::widget::set"; 132 }, "ext::widget::set";
123 133
124 $ws->msg (ws_n => id => $id); 134 $ws->msg (ws_n => $id);
125 135
126 $ws 136 $ws
137}
138
139sub cf::client::alloc_wid {
140 pop @{ $_[0]{ids} }
141 or ++$_[0]{id}
142}
143
144sub cf::client::free_wid {
145 push @{ $_[0]{ids} }, $_[1];
127} 146}
128 147
129############################################################################# 148#############################################################################
130 149
131package ext::widget::set; 150package ext::widget::set;
135} 154}
136 155
137sub destroy { 156sub destroy {
138 my ($self) = @_; 157 my ($self) = @_;
139 158
140 $self->msg (ws_d => id => $self->{id}); 159 $self->msg (ws_d => $self->{id});
141 delete $self->{ns}; 160 delete $self->{ns};
142 $_->destroy 161 $_->destroy
143 for values %{ $self->{w} }; 162 for values %{ $self->{w} };
144} 163}
145 164
146sub msg { 165sub msg {
147 my ($self, $type, %msg) = @_; 166 my ($self, @msg) = @_;
148 167
149 if (my $ns = shift->{ns}) { 168 if (my $ns = shift->{ns}) {
150 $msg{msgtype} = $type; 169 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
151 $ns->send_packet ("ext " . cf::to_json \%msg); 170 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
152 } 171 }
172}
173
174sub alloc {
175 my ($self) = @_;
176
177 my $id = $self->{ns}->alloc_wid;
178
179 my $proxy = bless {
180 id => $id,
181 }, "ext::widget::proxy";
182
183 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
184 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
185
186 $proxy
153} 187}
154 188
155sub new { 189sub new {
156 my ($self, $class, %args) = @_; 190 my ($self, $class, %args) = @_;
157 191
158 my $id = ++$self->{ns}{id}; 192 my $proxy = $self->alloc;
159 193
160 my $proxy = $self->{w}{$id} = bless { 194 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
161 id => $id,
162 }, "ext::widget::proxy";
163
164 Scalar::Util::weaken ($proxy->{ws} = $self); 195 Scalar::Util::weaken ($proxy->{ws} = $self);
165 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
166 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
167 196
168 for my $ev (grep /^on_/, keys %args) { 197 for my $ev (grep /^on_/, keys %args) {
169 push @{$proxy->{ev}{$ev}}, $args{$ev}; 198 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
170 $args{$ev} = 0;
171 } 199 }
172 200
173 $self->msg (ws_c => 201 $self->msg (ws_c =>
174 ws => $self->{w}{id}, 202 $self->{id},
175 id => $id, 203 $proxy->{id},
176 class => $class, 204 $class,
177 args => \%args, 205 \%args,
178 ); 206 );
179 207
180 $proxy 208 $proxy
181} 209}
182 210
211sub find {
212 my ($self, @names) = @_;
213
214 my @res;
215 my @alloc;
216
217 for my $name (@names) {
218 push @res, $self->{ns}{widget_wkw}{$name} ||= do {
219 my $proxy = $self->alloc;
220
221 push @alloc, $proxy->{id} => $name;
222
223 $proxy
224 };
225 }
226
227 $self->msg (ws_a => @alloc)
228 if @alloc;
229
230 wantarray ? @res : $res[0]
231}
232
183############################################################################# 233#############################################################################
184 234
185package ext::widget::proxy; 235package ext::widget::proxy;
186 236
187sub DESTROY { 237sub DESTROY {
188 my ($self) = @_; 238 my ($self) = @_;
189 239
190 delete $self->{ns}{widget}{$self->{id}}; 240 delete $self->{ns}{widget}{$self->{id}};
191 241
192 if (my $ws = $self->{ws}) { 242 if (my $ws = $self->{ws}) {
243 $self->msg (w_c => 0, "destroy");
193 delete $ws->{w}{$self->{id}}; 244 delete $ws->{_w}{$self->{id}};
194 $self->msg (w_c => name => "destroy");
195 } 245 }
246}
247
248sub cb {
249 my ($self, $cb) = @_;
250
251 my $proxy = bless {
252 ns => $self->{ns},
253 id => $self->{ns}->alloc_wid,
254 }, "ext::widget::callback";
255
256 Scalar::Util::weaken $proxy->{ns};
257
258 $self->{ns}{widget_cb}{$proxy->{id}} = $cb;
259
260 $proxy
261}
262
263sub oneshot_cb {
264 my ($self, $cb) = @_;
265
266 if ("CODE" eq ref $cb) {
267 my $ocb = $cb;
268 $cb = $self->cb (sub {
269 undef $cb;
270 &$ocb
271 });
272 }
273
274 $cb
196} 275}
197 276
198sub msg { 277sub msg {
199 my ($self, $type, %msg) = @_; 278 my ($self, $type, @arg) = @_;
279
280 if (my $ns = $self->{ns}) {
281 my @msg = ($type, $self->{id}, @arg);
282 warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
283 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
284 }
285}
286
287sub msg_cb {
288 my ($self, $cb, $type, @arg) = @_;
200 289
201 if (my $ws = $self->{ws}) { 290 if (my $ws = $self->{ws}) {
202 $ws->msg ($type,
203 %msg,
204 id => $self->{id},
205 );
206 }
207}
208
209sub msg_cb {
210 my ($self, $cb, $type, %msg) = @_;
211
212 if (my $ws = $self->{ws}) {
213
214 my $rid = ++$ws->{ns}{id}; 291 my $rid = $ws->{ns}->alloc_wid;
215
216 $self->msg ($type, %msg, rid => $rid);
217 292
218 if ($cb) { 293 if ($cb) {
219 $ws->{ns}{widget_return}{$rid} = $cb; 294 $ws->{ns}{widget_cb}{$rid} = sub {
295 delete $ws->{ns}{widget_cb}{$rid};
296 $ws->{ns}->free_wid ($rid);
297 &$cb
298 };
299
300 $self->msg ($type, $rid, @arg);
220 } else { 301 } else {
221 # synchronous case 302 # synchronous case
222 my $wait = new Coro::Signal; 303 my $wait = new Coro::Signal;
223 my @res; 304 my @res;
224 305
225 $ws->{ns}{widget_return}{$rid} = sub { 306 $ws->{ns}{widget_cb}{$rid} = sub {
307 delete $ws->{ns}{widget_cb}{$rid};
308 $ws->{ns}->free_wid ($rid);
309
226 @res = @_; 310 @res = @_;
227 $wait->send; 311 $wait->send;
228 }; 312 };
313 $self->msg ($type, $rid, @arg);
229 $wait->wait; 314 $wait->wait;
230 315
231 return @res; 316 return @res;
232 } 317 }
233 } 318 }
234 319
235 () 320 ()
236} 321}
237 322
238sub set { 323sub set {
239 my ($self, $member, $value) = @_; 324 my ($self, @kv) = @_;
240 325
241 $self->msg (w_s => name => $member, value => $value); 326 $self->msg (w_s => \@kv);
242} 327}
243 328
244sub get { 329sub get {
245 my ($self, $member, $cb) = @_; 330 my ($self, $member, $cb) = @_;
246 331
247 $self->msg_cb ($cb, w_g => name => $member); 332 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
248} 333}
249 334
250sub TO_JSON { 335sub TO_JSON {
251 { __widget_ref__ => $_[0]{id} } 336 { "\fw" => $_[0]{id} }
252} 337}
253 338
254our $AUTOLOAD; 339our $AUTOLOAD;
255 340
256sub AUTOLOAD { 341sub AUTOLOAD {
257 $AUTOLOAD =~ s/^.*:// 342 $AUTOLOAD =~ s/^.*://
258 or return; 343 or return;
259 344
260 my $self = shift; 345 my $self = shift;
261 346
347 #TODO: handle non-void context
262 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 348 $self->msg (w_c => 0, $AUTOLOAD, @_);
263 349
264 () 350 ()
265} 351}
266 352
353package ext::widget::callback;
354
355sub DESTROY {
356 my ($self) = @_;
357
358 if (my $ns = $self->{ns}) {
359 delete $ns->{widget_cb}{$self->{id}};
360 $ns->free_wid ($self->{id});
361 }
362}
363
364sub TO_JSON {
365 { "\fc" => $_[0]{id} }
366}
367

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines