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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines