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 @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 | |
15 | our $DEBUG = 1; |
16 | our $DEBUG = 1; |
16 | |
17 | |
17 | cf::client->attach ( |
18 | cf::client->attach ( |
18 | on_connect => sub { |
19 | on_connect => sub { |
19 | my ($ns) = @_; |
20 | my ($ns) = @_; |
20 | |
21 | |
21 | Scalar::Util::weaken (my $weakns = $ns); |
22 | Scalar::Util::weaken (my $weakns = $ns); |
22 | |
23 | |
23 | $ns->{id} = "a"; |
24 | $ns->{id} = "a"; |
24 | $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { |
25 | $ns->{json_coder}->filter_json_single_key_object (__w_ => sub { |
25 | # cannot deserialise ATM |
26 | # cannot deserialise ATM |
26 | undef |
27 | undef |
27 | }); |
28 | }); |
28 | }, |
29 | }, |
29 | ); |
30 | ); |
… | |
… | |
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 | |
|
|
40 | sub 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 | |
39 | sub csc_start { |
51 | sub 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 | |
88 | cf::player->attach ( |
101 | cf::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 | |
101 | cf::register_exticmd w_e => sub { |
115 | cf::register_exticmd w_e => sub { |
102 | my ($ns, $pkt) = @_; |
116 | my ($ns, $id, $rid, @args) = @_; |
103 | |
117 | |
104 | if (my $w = $ns->{widget}{$pkt->{id}}) { |
118 | if (my $w = $ns->{widget}{$id}) { |
105 | if (my $cb = $w->{ev}{$pkt->{name}}) { |
119 | if (my $cb = $w->{ev}{$rid}) { |
106 | $_->($w, @{ $pkt->{args} || [] }) |
120 | $cb->($w, @args); |
107 | for @$cb; |
|
|
108 | } |
121 | } |
109 | } |
122 | } |
110 | |
123 | |
111 | () |
124 | () |
112 | }; |
125 | }; |
113 | |
126 | |
114 | cf::register_exticmd w_r => sub { |
127 | cf::register_exticmd w_r => sub { |
115 | my ($ns, $pkt) = @_; |
128 | my ($ns, $rid, $res) = @_; |
116 | |
129 | |
117 | if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { |
130 | if (my $cb = delete $ns->{widget_return}{$rid}) { |
118 | $cb->(@{$pkt->{res} || [] }); |
131 | $cb->(@$res); |
119 | } |
132 | } |
120 | |
133 | |
121 | () |
134 | () |
122 | }; |
135 | }; |
123 | |
136 | |
… | |
… | |
130 | id => $id, |
143 | id => $id, |
131 | ns => $self, |
144 | ns => $self, |
132 | _w => {}, |
145 | _w => {}, |
133 | }, "ext::widget::set"; |
146 | }, "ext::widget::set"; |
134 | |
147 | |
135 | $ws->msg (ws_n => id => $id); |
148 | $ws->msg (ws_n => $id); |
136 | |
149 | |
137 | $ws |
150 | $ws |
138 | } |
151 | } |
139 | |
152 | |
140 | ############################################################################# |
153 | ############################################################################# |
… | |
… | |
146 | } |
159 | } |
147 | |
160 | |
148 | sub destroy { |
161 | sub destroy { |
149 | my ($self) = @_; |
162 | my ($self) = @_; |
150 | |
163 | |
151 | $self->msg (ws_d => id => $self->{id}); |
164 | $self->msg (ws_d => $self->{id}); |
152 | delete $self->{ns}; |
165 | delete $self->{ns}; |
153 | $_->destroy |
166 | $_->destroy |
154 | for values %{ $self->{w} }; |
167 | for values %{ $self->{w} }; |
155 | } |
168 | } |
156 | |
169 | |
157 | sub msg { |
170 | sub msg { |
158 | my ($self, $type, %msg) = @_; |
171 | my ($self, @msg) = @_; |
159 | |
172 | |
160 | if (my $ns = shift->{ns}) { |
173 | if (my $ns = shift->{ns}) { |
161 | $msg{msgtype} = $type; |
|
|
162 | warn "msg " . $ns->{json_coder}->encode (\%msg) if $DEBUG;#d# |
174 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
163 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
175 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
164 | } |
176 | } |
165 | } |
177 | } |
166 | |
178 | |
167 | sub new { |
179 | sub alloc { |
168 | my ($self, $class, %args) = @_; |
180 | my ($self) = @_; |
169 | |
181 | |
170 | my $id = ++$self->{ns}{id}; |
182 | my $id = ++$self->{ns}{id}; |
171 | |
183 | |
172 | my $proxy = bless { |
184 | my $proxy = bless { |
173 | id => $id, |
185 | id => $id, |
174 | }, "ext::widget::proxy"; |
186 | }, "ext::widget::proxy"; |
175 | |
187 | |
176 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
|
|
177 | Scalar::Util::weaken ($proxy->{ws} = $self); |
|
|
178 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
188 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
179 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
189 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
180 | |
190 | |
|
|
191 | $proxy |
|
|
192 | } |
|
|
193 | |
|
|
194 | sub 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 | |
181 | for my $ev (grep /^on_/, keys %args) { |
202 | for my $ev (grep /^on_/, keys %args) { |
|
|
203 | my $rid = ++$self->{ns}{id}; |
182 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
204 | $proxy->{ev}{$rid} = $args{$ev}; |
183 | $args{$ev} = 0; |
205 | $args{$ev} = $rid; |
184 | } |
206 | } |
185 | |
207 | |
186 | $self->msg (ws_c => |
208 | $self->msg (ws_c => |
|
|
209 | $self->{id}, |
187 | ws => $proxy->{id}, |
210 | $proxy->{id}, |
188 | id => $id, |
211 | $class, |
189 | class => $class, |
212 | \%args, |
190 | args => \%args, |
|
|
191 | ); |
213 | ); |
192 | |
214 | |
193 | $proxy |
215 | $proxy |
|
|
216 | } |
|
|
217 | |
|
|
218 | sub 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] |
194 | } |
238 | } |
195 | |
239 | |
196 | ############################################################################# |
240 | ############################################################################# |
197 | |
241 | |
198 | package ext::widget::proxy; |
242 | package ext::widget::proxy; |
… | |
… | |
201 | my ($self) = @_; |
245 | my ($self) = @_; |
202 | |
246 | |
203 | delete $self->{ns}{widget}{$self->{id}}; |
247 | delete $self->{ns}{widget}{$self->{id}}; |
204 | |
248 | |
205 | if (my $ws = $self->{ws}) { |
249 | if (my $ws = $self->{ws}) { |
206 | $self->msg (w_c => name => "destroy"); |
250 | $self->msg (w_c => 0, "destroy"); |
207 | delete $ws->{_w}{$self->{id}}; |
251 | delete $ws->{_w}{$self->{id}}; |
208 | } |
252 | } |
209 | } |
253 | } |
210 | |
254 | |
211 | sub msg { |
255 | sub msg { |
212 | 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 | |
|
|
265 | sub msg_cb { |
|
|
266 | my ($self, $cb, $type, @arg) = @_; |
213 | |
267 | |
214 | if (my $ws = $self->{ws}) { |
268 | if (my $ws = $self->{ws}) { |
215 | $ws->msg ($type, |
|
|
216 | %msg, |
|
|
217 | id => $self->{id}, |
|
|
218 | ); |
|
|
219 | } |
|
|
220 | } |
|
|
221 | |
|
|
222 | sub msg_cb { |
|
|
223 | my ($self, $cb, $type, %msg) = @_; |
|
|
224 | |
|
|
225 | if (my $ws = $self->{ws}) { |
|
|
226 | |
|
|
227 | my $rid = ++$ws->{ns}{id}; |
269 | my $rid = ++$ws->{ns}{id}; |
228 | |
270 | |
229 | $self->msg ($type, %msg, rid => $rid); |
271 | $self->msg ($type, $rid, @arg); |
230 | |
272 | |
231 | if ($cb) { |
273 | if ($cb) { |
232 | $ws->{ns}{widget_return}{$rid} = $cb; |
274 | $ws->{ns}{widget_return}{$rid} = $cb; |
233 | } else { |
275 | } else { |
234 | # synchronous case |
276 | # synchronous case |
… | |
… | |
249 | } |
291 | } |
250 | |
292 | |
251 | sub set { |
293 | sub set { |
252 | my ($self, @kv) = @_; |
294 | my ($self, @kv) = @_; |
253 | |
295 | |
254 | $self->msg (w_s => attr => \@kv); |
296 | $self->msg (w_s => \@kv); |
255 | } |
297 | } |
256 | |
298 | |
257 | sub get { |
299 | sub get { |
258 | my ($self, $member, $cb) = @_; |
300 | my ($self, $member, $cb) = @_; |
259 | |
301 | |
260 | $self->msg_cb ($cb, w_g => attr => [$member]); |
302 | $self->msg_cb ($cb, w_g => [$member]); |
261 | } |
303 | } |
262 | |
304 | |
263 | sub TO_JSON { |
305 | sub TO_JSON { |
264 | { __widget_ref__ => $_[0]{id} } |
306 | { __w_ => $_[0]{id} } |
265 | } |
307 | } |
266 | |
308 | |
267 | our $AUTOLOAD; |
309 | our $AUTOLOAD; |
268 | |
310 | |
269 | sub AUTOLOAD { |
311 | sub AUTOLOAD { |
270 | $AUTOLOAD =~ s/^.*:// |
312 | $AUTOLOAD =~ s/^.*:// |
271 | or return; |
313 | or return; |
272 | |
314 | |
273 | my $self = shift; |
315 | my $self = shift; |
274 | |
316 | |
|
|
317 | #TODO: handle non-void context |
275 | $self->msg (w_c => name => $AUTOLOAD, args => \@_); |
318 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
276 | |
319 | |
277 | () |
320 | () |
278 | } |
321 | } |
279 | |
322 | |