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.16 by root, Tue Jul 24 19:33:57 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 ws # widgetset new 5# ws_n ws # widgetset new
5# ws_d ws # 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 @attr # widget member set 9# w_s id @attr # widget member set
9# w_g id rid @attr # 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
14 15
15our $DEBUG = 1; 16our $DEBUG = 1;
16 17
17cf::client->attach ( 18cf::client->attach (
18 on_connect => sub { 19 on_connect => sub {
32 my ($ns) = @_; 33 my ($ns) = @_;
33 34
34 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { 35 while (my ($k, $v) = each %{ $ns->{csc}{stat} }) {
35 $v->set_text ($ns->pl->ob->stats->$k); 36 $v->set_text ($ns->pl->ob->stats->$k);
36 } 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;
37} 49}
38 50
39sub csc_start { 51sub csc_start {
40 my ($ns) = @_; 52 my ($ns) = @_;
41 53
53 }, 65 },
54 ); 66 );
55 67
56 $w->add (my $ntb = $ws->new (Notebook => expand => 1)); 68 $w->add (my $ntb = $ws->new (Notebook => expand => 1));
57 69
58 $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");
59 71
60 $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); 72 $stats->add_at (0, 0, (my $statstable = $ws->new ("Table")));
61 73
62 for ( 74 for (
63 [0, "Str"], 75 [0, "Str"],
64 [1, "Dex"], 76 [1, "Dex"],
65 [2, "Con"], 77 [2, "Con"],
80 )); 92 ));
81 } 93 }
82 94
83 csc_update_stats $ns; 95 csc_update_stats $ns;
84 96
97 $ws->{tl} = $w;
85 $w->show; 98 $w->show;
86} 99}
87 100
88cf::player->attach ( 101cf::player->attach (
89 on_login => sub { 102 on_login => sub {
93 106
94 my $ns = $pl->ns; 107 my $ns = $pl->ns;
95 108
96 return unless $ns->{can_widget}; 109 return unless $ns->{can_widget};
97 #csc_start $ns; 110 #csc_start $ns;
111 demo_start $ns;
98 }, 112 },
99); 113);
100 114
101cf::register_exticmd w_e => sub { 115cf::register_exticmd w_e => sub {
102 my ($ns, $id, $name, $args) = @_; 116 my ($ns, $id, $rid, @args) = @_;
103 117
104 if (my $w = $ns->{widget}{$id}) { 118 if (my $w = $ns->{widget}{$id}) {
105 if (my $cb = $w->{ev}{$name}) { 119 if (my $cb = $w->{ev}{$rid}) {
106 $_->($w, @$args) 120 $cb->($w, @args);
107 for @$cb;
108 } 121 }
109 } 122 }
110 123
111 () 124 ()
112}; 125};
161 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# 174 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
162 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); 175 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
163 } 176 }
164} 177}
165 178
166sub new { 179sub alloc {
167 my ($self, $class, %args) = @_; 180 my ($self) = @_;
168 181
169 my $id = ++$self->{ns}{id}; 182 my $id = ++$self->{ns}{id};
170 183
171 my $proxy = bless { 184 my $proxy = bless {
172 id => $id, 185 id => $id,
173 }, "ext::widget::proxy"; 186 }, "ext::widget::proxy";
174 187
175 Scalar::Util::weaken ($self->{_w}{$id} = $proxy);
176 Scalar::Util::weaken ($proxy->{ws} = $self);
177 Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); 188 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
178 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); 189 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
179 190
191 $proxy
192}
193
194sub new {
195 my ($self, $class, %args) = @_;
196
197 my $proxy = $self->alloc;
198
199 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy);
200 Scalar::Util::weaken ($proxy->{ws} = $self);
201
180 for my $ev (grep /^on_/, keys %args) { 202 for my $ev (grep /^on_/, keys %args) {
203 my $rid = ++$self->{ns}{id};
181 push @{$proxy->{ev}{$ev}}, $args{$ev}; 204 $proxy->{ev}{$rid} = $args{$ev};
182 $args{$ev} = 0; 205 $args{$ev} = $rid;
183 } 206 }
184 207
185 $self->msg (ws_c => 208 $self->msg (ws_c =>
186 $self->{id}, 209 $self->{id},
187 $id, 210 $proxy->{id},
188 $class, 211 $class,
189 \%args, 212 \%args,
190 ); 213 );
191 214
192 $proxy 215 $proxy
193} 216}
194 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]
238}
239
195############################################################################# 240#############################################################################
196 241
197package ext::widget::proxy; 242package ext::widget::proxy;
198 243
199sub DESTROY { 244sub DESTROY {
208} 253}
209 254
210sub msg { 255sub msg {
211 my ($self, $type, @arg) = @_; 256 my ($self, $type, @arg) = @_;
212 257
213 if (my $ws = $self->{ws}) { 258 if (my $ns = $self->{ns}) {
214 $ws->msg ($type, $self->{id}, @arg); 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));
215 } 262 }
216} 263}
217 264
218sub msg_cb { 265sub msg_cb {
219 my ($self, $cb, $type, @arg) = @_; 266 my ($self, $cb, $type, @arg) = @_;
265 $AUTOLOAD =~ s/^.*:// 312 $AUTOLOAD =~ s/^.*://
266 or return; 313 or return;
267 314
268 my $self = shift; 315 my $self = shift;
269 316
317 #TODO: handle non-void context
270 $self->msg (w_c => 0, $AUTOLOAD, \@_); 318 $self->msg (w_c => 0, $AUTOLOAD, @_);
271 319
272 () 320 ()
273} 321}
274 322

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines