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.1 by root, Mon Jun 25 05:43: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);
29
30sub csc_update_stats {
31 my ($ns) = @_;
32
33 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) {
34 $v->set_text ($ns->pl->ob->stats->$k);
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;
47}
48
49sub csc_start {
50 my ($ns) = @_;
51
52 my $ws = $ns->{csc} = $ns->new_widgetset;
53
54 my $w = $ws->new (Toplevel =>
55 min_w => 600,
56 min_h => 400,
57 x => "center",
58 y => "center",
59 title => "Character Creation",
60 has_close_button => 1,
61 on_delete => sub {
62 $ws->destroy;
63 },
64 );
65
66 $w->add (my $ntb = $ws->new (Notebook => expand => 1));
67
68 $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character");
69
70 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
71
72 for (
73 [0, "Str"],
74 [1, "Dex"],
75 [2, "Con"],
76 [3, "Int"],
77 [4, "Wis"],
78 [5, "Pow"],
79 [6, "Cha"],
80 ) {
81 my ($x, $label) = @$_;
82
83 $statstable->add_at ($x, 0, $ws->new (Label =>
84 can_hover => 1, can_events => 1,
85 align => +1, text => $label, tooltip => "#stat_$label",
86 ));
87 $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
88 can_hover => 1, can_events => 1,
89 align => +1, template => "88", tooltip => "#stat_$label",
90 ));
91 }
92
93 csc_update_stats $ns;
94
95 $ws->{tl} = $w;
96 $w->show;
97}
22 98
23cf::player->attach ( 99cf::player->attach (
24 on_login => sub { 100 on_login => sub {
25 my ($pl) = @_; 101 my ($pl) = @_;
26 102
27 #DEMO CODE 103 return unless $cf::CFG{devel};
28 return unless $pl->ob->name eq "schmorp";
29 104
30 my $ns = $pl->ns; 105 my $ns = $pl->ns;
31 106
32 return unless $ns->{can_widgetx}; 107 return unless $ns->{can_widget};
33 108 #csc_start $ns;
34 my $ws = $ns->new_widgetset; 109 #demo_start $ns;
35
36 $ns->async (sub {
37 Coro::Timer::sleep 20;
38 warn "undef\n";#d#
39 undef $ws;#
40 });#d#
41
42 my $w = $ws->new (Toplevel =>
43 x => "center",
44 y => "center",
45 title => "Server Query",
46 has_close_button => 1,
47 on_delete => sub {
48 warn "i was being d-e-l-e-t-e-d\n";
49 },
50 );
51
52 $w->add ($ws->new (Entry =>
53 on_changed => sub {
54 warn "i was changed<@_>\n";
55 }
56 ));
57
58 $ns->async (sub {
59 warn $w->get ("parent");
60 });
61
62 $w->show;
63
64 }, 110 },
65); 111);
66 112
67cf::register_exticmd w_e => sub { 113cf::register_exticmd w_e => sub {
68 my ($ns, $pkt) = @_; 114 my ($ns, $id, @args) = @_;
69 115
70 if (my $w = $ns->{widget}{$pkt->{id}}) { 116 if (my $cb = $ns->{widget_cb}{$id}) {
71 if (my $cb = $w->{ev}{$pkt->{name}}) { 117 $cb->(@args);
72 $_->($w, @{ $pkt->{args} || [] })
73 for @$cb;
74 }
75 }
76
77 ()
78};
79
80cf::register_exticmd w_r => sub {
81 my ($ns, $pkt) = @_;
82
83 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) {
84 $cb->(@{$pkt->{res} || [] });
85 } 118 }
86 119
87 () 120 ()
88}; 121};
89 122
93 my $id = ++$self->{id}; 126 my $id = ++$self->{id};
94 127
95 my $ws = bless { 128 my $ws = bless {
96 id => $id, 129 id => $id,
97 ns => $self, 130 ns => $self,
98 w => {}, 131 _w => {},
99 }, "ext::widget::set"; 132 }, "ext::widget::set";
100 133
101 $ws->msg (ws_n => id => $id); 134 $ws->msg (ws_n => $id);
102 135
103 $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];
104} 146}
105 147
106############################################################################# 148#############################################################################
107 149
108package ext::widget::set; 150package ext::widget::set;
112} 154}
113 155
114sub destroy { 156sub destroy {
115 my ($self) = @_; 157 my ($self) = @_;
116 158
117 $self->msg (ws_d => id => $self->{id}); 159 $self->msg (ws_d => $self->{id});
118 delete $self->{ns}; 160 delete $self->{ns};
119 $_->destroy 161 $_->destroy
120 for values %{ $self->{w} }; 162 for values %{ $self->{w} };
121} 163}
122 164
123sub msg { 165sub msg {
124 my ($self, $type, %msg) = @_; 166 my ($self, @msg) = @_;
125 167
126 if (my $ns = shift->{ns}) { 168 if (my $ns = shift->{ns}) {
127 $msg{msgtype} = $type; 169 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
128 $ns->send_packet ("ext " . cf::to_json \%msg); 170 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
129 } 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
130} 187}
131 188
132sub new { 189sub new {
133 my ($self, $class, %args) = @_; 190 my ($self, $class, %args) = @_;
134 191
135 my $id = ++$self->{ns}{id}; 192 my $proxy = $self->alloc;
136 193
137 my $proxy = $self->{w}{$id} = bless { 194 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
138 id => $id,
139 }, "ext::widget::proxy";
140
141 Scalar::Util::weaken ($proxy->{ws} = $self); 195 Scalar::Util::weaken ($proxy->{ws} = $self);
142 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
143 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
144 196
145 for my $ev (grep /^on_/, keys %args) { 197 for my $ev (grep /^on_/, keys %args) {
146 push @{$proxy->{ev}{$ev}}, $args{$ev}; 198 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
147 $args{$ev} = 0;
148 } 199 }
149 200
150 $self->msg (ws_c => 201 $self->msg (ws_c =>
151 ws => $self->{w}{id}, 202 $self->{id},
152 id => $id, 203 $proxy->{id},
153 class => $class, 204 $class,
154 args => \%args, 205 \%args,
155 ); 206 );
156 207
157 $proxy 208 $proxy
158} 209}
159 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
160############################################################################# 233#############################################################################
161 234
162package ext::widget::proxy; 235package ext::widget::proxy;
163 236
164sub DESTROY { 237sub DESTROY {
165 my ($self) = @_; 238 my ($self) = @_;
166 239
167 delete $self->{ns}{widget}{$self->{id}}; 240 delete $self->{ns}{widget}{$self->{id}};
168 241
169 if (my $ws = $self->{ws}) { 242 if (my $ws = $self->{ws}) {
243 $self->msg (w_c => 0, "destroy");
170 delete $ws->{w}{$self->{id}}; 244 delete $ws->{_w}{$self->{id}};
171 $self->msg (w_c => name => "destroy");
172 } 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
173} 275}
174 276
175sub msg { 277sub msg {
176 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) = @_;
177 289
178 if (my $ws = $self->{ws}) { 290 if (my $ws = $self->{ws}) {
179 $ws->msg ($type,
180 %msg,
181 id => $self->{id},
182 );
183 }
184}
185
186sub msg_cb {
187 my ($self, $cb, $type, %msg) = @_;
188
189 if (my $ws = $self->{ws}) {
190
191 my $rid = ++$ws->{ns}{id}; 291 my $rid = $ws->{ns}->alloc_wid;
192
193 $self->msg ($type, %msg, rid => $rid);
194 292
195 if ($cb) { 293 if ($cb) {
196 $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);
197 } else { 301 } else {
198 # synchronous case 302 # synchronous case
199 my $wait = new Coro::Signal; 303 my $wait = new Coro::Signal;
200 my @res; 304 my @res;
201 305
202 $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
203 @res = @_; 310 @res = @_;
204 $wait->send; 311 $wait->send;
205 }; 312 };
313 $self->msg ($type, $rid, @arg);
206 $wait->wait; 314 $wait->wait;
207 315
208 return @res; 316 return @res;
209 } 317 }
210 } 318 }
211 319
212 () 320 ()
213} 321}
214 322
215sub set { 323sub set {
216 my ($self, $member, $value) = @_; 324 my ($self, @kv) = @_;
217 325
218 $self->msg (w_s => name => $member, value => $value); 326 $self->msg (w_s => \@kv);
219} 327}
220 328
221sub get { 329sub get {
222 my ($self, $member, $cb) = @_; 330 my ($self, $member, $cb) = @_;
223 331
224 $self->msg_cb ($cb, w_g => name => $member); 332 $self->msg_cb ($cb, w_g => ref $member ? @$member : $member);
225} 333}
226 334
227sub TO_JSON { 335sub TO_JSON {
228 { __widget_ref__ => $_[0]{id} } 336 { "\fw" => $_[0]{id} }
229} 337}
230 338
231our $AUTOLOAD; 339our $AUTOLOAD;
232 340
233sub AUTOLOAD { 341sub AUTOLOAD {
234 $AUTOLOAD =~ s/^.*:// 342 $AUTOLOAD =~ s/^.*://
235 or return; 343 or return;
236 344
237 my $self = shift; 345 my $self = shift;
238 346
347 #TODO: handle non-void context
239 $self->msg (w_c => name => $AUTOLOAD, args => \@_); 348 $self->msg (w_c => 0, $AUTOLOAD, @_);
240 349
241 () 350 ()
242} 351}
243 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