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 | |
|
|
16 | our $DEBUG = 1; |
14 | |
17 | |
15 | cf::client->attach ( |
18 | cf::client->attach ( |
16 | on_connect => sub { |
19 | on_connect => sub { |
17 | my ($ns) = @_; |
20 | my ($ns) = @_; |
18 | |
21 | |
|
|
22 | Scalar::Util::weaken (my $weakns = $ns); |
|
|
23 | |
19 | $ns->{id} = "a"; |
24 | $ns->{id} = "a"; |
|
|
25 | $ns->{json_coder}->filter_json_single_key_object (__w_ => sub { |
|
|
26 | # cannot deserialise ATM |
|
|
27 | undef |
|
|
28 | }); |
20 | }, |
29 | }, |
21 | ); |
30 | ); |
22 | |
31 | |
23 | sub csc_update_stats { |
32 | sub csc_update_stats { |
24 | my ($ns) = @_; |
33 | my ($ns) = @_; |
25 | |
34 | |
26 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
35 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
27 | $v->set_text ($ns->pl->ob->stats->$k); |
36 | $v->set_text ($ns->pl->ob->stats->$k); |
28 | } |
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; |
29 | } |
49 | } |
30 | |
50 | |
31 | sub csc_start { |
51 | sub csc_start { |
32 | my ($ns) = @_; |
52 | my ($ns) = @_; |
33 | |
53 | |
… | |
… | |
45 | }, |
65 | }, |
46 | ); |
66 | ); |
47 | |
67 | |
48 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
68 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
49 | |
69 | |
50 | $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"); |
51 | |
71 | |
52 | $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); |
72 | $stats->add_at (0, 0, (my $statstable = $ws->new ("Table"))); |
53 | |
73 | |
54 | for ( |
74 | for ( |
55 | [0, "Str"], |
75 | [0, "Str"], |
56 | [1, "Dex"], |
76 | [1, "Dex"], |
57 | [2, "Con"], |
77 | [2, "Con"], |
… | |
… | |
60 | [5, "Pow"], |
80 | [5, "Pow"], |
61 | [6, "Cha"], |
81 | [6, "Cha"], |
62 | ) { |
82 | ) { |
63 | my ($x, $label) = @$_; |
83 | my ($x, $label) = @$_; |
64 | |
84 | |
65 | $statstable->add ($x, 0, $ws->new (Label => |
85 | $statstable->add_at ($x, 0, $ws->new (Label => |
66 | can_hover => 1, can_events => 1, |
86 | can_hover => 1, can_events => 1, |
67 | align => +1, text => $label, tooltip => "#stat_$label", |
87 | align => +1, text => $label, tooltip => "#stat_$label", |
68 | )); |
88 | )); |
69 | $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
89 | $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
70 | can_hover => 1, can_events => 1, |
90 | can_hover => 1, can_events => 1, |
71 | align => +1, template => "88", tooltip => "#stat_$label", |
91 | align => +1, template => "88", tooltip => "#stat_$label", |
72 | )); |
92 | )); |
73 | } |
93 | } |
74 | |
94 | |
75 | csc_update_stats $ns; |
95 | csc_update_stats $ns; |
76 | |
96 | |
|
|
97 | $ws->{tl} = $w; |
77 | $w->show; |
98 | $w->show; |
78 | } |
99 | } |
79 | |
100 | |
80 | cf::player->attach ( |
101 | cf::player->attach ( |
81 | on_login => sub { |
102 | on_login => sub { |
… | |
… | |
84 | return unless $cf::CFG{devel}; |
105 | return unless $cf::CFG{devel}; |
85 | |
106 | |
86 | my $ns = $pl->ns; |
107 | my $ns = $pl->ns; |
87 | |
108 | |
88 | return unless $ns->{can_widget}; |
109 | return unless $ns->{can_widget}; |
89 | |
|
|
90 | csc_start $ns; |
110 | #csc_start $ns; |
|
|
111 | demo_start $ns; |
91 | }, |
112 | }, |
92 | ); |
113 | ); |
93 | |
114 | |
94 | cf::register_exticmd w_e => sub { |
115 | cf::register_exticmd w_e => sub { |
95 | my ($ns, $pkt) = @_; |
116 | my ($ns, $id, $rid, @args) = @_; |
96 | |
117 | |
97 | if (my $w = $ns->{widget}{$pkt->{id}}) { |
118 | if (my $w = $ns->{widget}{$id}) { |
98 | if (my $cb = $w->{ev}{$pkt->{name}}) { |
119 | if (my $cb = $w->{ev}{$rid}) { |
99 | $_->($w, @{ $pkt->{args} || [] }) |
120 | $cb->($w, @args); |
100 | for @$cb; |
|
|
101 | } |
121 | } |
102 | } |
122 | } |
103 | |
123 | |
104 | () |
124 | () |
105 | }; |
125 | }; |
106 | |
126 | |
107 | cf::register_exticmd w_r => sub { |
127 | cf::register_exticmd w_r => sub { |
108 | my ($ns, $pkt) = @_; |
128 | my ($ns, $rid, $res) = @_; |
109 | |
129 | |
110 | if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { |
130 | if (my $cb = delete $ns->{widget_return}{$rid}) { |
111 | $cb->(@{$pkt->{res} || [] }); |
131 | $cb->(@$res); |
112 | } |
132 | } |
113 | |
133 | |
114 | () |
134 | () |
115 | }; |
135 | }; |
116 | |
136 | |
… | |
… | |
120 | my $id = ++$self->{id}; |
140 | my $id = ++$self->{id}; |
121 | |
141 | |
122 | my $ws = bless { |
142 | my $ws = bless { |
123 | id => $id, |
143 | id => $id, |
124 | ns => $self, |
144 | ns => $self, |
125 | w => {}, |
145 | _w => {}, |
126 | }, "ext::widget::set"; |
146 | }, "ext::widget::set"; |
127 | |
147 | |
128 | $ws->msg (ws_n => id => $id); |
148 | $ws->msg (ws_n => $id); |
129 | |
149 | |
130 | $ws |
150 | $ws |
131 | } |
151 | } |
132 | |
152 | |
133 | ############################################################################# |
153 | ############################################################################# |
… | |
… | |
139 | } |
159 | } |
140 | |
160 | |
141 | sub destroy { |
161 | sub destroy { |
142 | my ($self) = @_; |
162 | my ($self) = @_; |
143 | |
163 | |
144 | $self->msg (ws_d => id => $self->{id}); |
164 | $self->msg (ws_d => $self->{id}); |
145 | delete $self->{ns}; |
165 | delete $self->{ns}; |
146 | $_->destroy |
166 | $_->destroy |
147 | for values %{ $self->{w} }; |
167 | for values %{ $self->{w} }; |
148 | } |
168 | } |
149 | |
169 | |
150 | sub msg { |
170 | sub msg { |
151 | my ($self, $type, %msg) = @_; |
171 | my ($self, @msg) = @_; |
152 | |
172 | |
153 | if (my $ns = shift->{ns}) { |
173 | if (my $ns = shift->{ns}) { |
154 | $msg{msgtype} = $type; |
174 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
155 | $ns->send_packet ("ext " . cf::to_json \%msg); |
175 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
156 | } |
176 | } |
|
|
177 | } |
|
|
178 | |
|
|
179 | sub 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 |
157 | } |
192 | } |
158 | |
193 | |
159 | sub new { |
194 | sub new { |
160 | my ($self, $class, %args) = @_; |
195 | my ($self, $class, %args) = @_; |
161 | |
196 | |
162 | my $id = ++$self->{ns}{id}; |
197 | my $proxy = $self->alloc; |
163 | |
198 | |
164 | my $proxy = $self->{w}{$id} = bless { |
199 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
165 | id => $id, |
|
|
166 | }, "ext::widget::proxy"; |
|
|
167 | |
|
|
168 | Scalar::Util::weaken ($proxy->{ws} = $self); |
200 | Scalar::Util::weaken ($proxy->{ws} = $self); |
169 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
|
|
170 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
|
|
171 | |
201 | |
172 | for my $ev (grep /^on_/, keys %args) { |
202 | for my $ev (grep /^on_/, keys %args) { |
|
|
203 | my $rid = ++$self->{ns}{id}; |
173 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
204 | $proxy->{ev}{$rid} = $args{$ev}; |
174 | $args{$ev} = 0; |
205 | $args{$ev} = $rid; |
175 | } |
206 | } |
176 | |
207 | |
177 | $self->msg (ws_c => |
208 | $self->msg (ws_c => |
178 | ws => $self->{w}{id}, |
209 | $self->{id}, |
179 | id => $id, |
210 | $proxy->{id}, |
180 | class => $class, |
211 | $class, |
181 | args => \%args, |
212 | \%args, |
182 | ); |
213 | ); |
183 | |
214 | |
184 | $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] |
185 | } |
238 | } |
186 | |
239 | |
187 | ############################################################################# |
240 | ############################################################################# |
188 | |
241 | |
189 | package ext::widget::proxy; |
242 | package ext::widget::proxy; |
… | |
… | |
192 | my ($self) = @_; |
245 | my ($self) = @_; |
193 | |
246 | |
194 | delete $self->{ns}{widget}{$self->{id}}; |
247 | delete $self->{ns}{widget}{$self->{id}}; |
195 | |
248 | |
196 | if (my $ws = $self->{ws}) { |
249 | if (my $ws = $self->{ws}) { |
|
|
250 | $self->msg (w_c => 0, "destroy"); |
197 | delete $ws->{w}{$self->{id}}; |
251 | delete $ws->{_w}{$self->{id}}; |
198 | $self->msg (w_c => name => "destroy"); |
|
|
199 | } |
252 | } |
200 | } |
253 | } |
201 | |
254 | |
202 | sub msg { |
255 | sub msg { |
203 | 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) = @_; |
204 | |
267 | |
205 | if (my $ws = $self->{ws}) { |
268 | if (my $ws = $self->{ws}) { |
206 | $ws->msg ($type, |
|
|
207 | %msg, |
|
|
208 | id => $self->{id}, |
|
|
209 | ); |
|
|
210 | } |
|
|
211 | } |
|
|
212 | |
|
|
213 | sub msg_cb { |
|
|
214 | my ($self, $cb, $type, %msg) = @_; |
|
|
215 | |
|
|
216 | if (my $ws = $self->{ws}) { |
|
|
217 | |
|
|
218 | my $rid = ++$ws->{ns}{id}; |
269 | my $rid = ++$ws->{ns}{id}; |
219 | |
270 | |
220 | $self->msg ($type, %msg, rid => $rid); |
271 | $self->msg ($type, $rid, @arg); |
221 | |
272 | |
222 | if ($cb) { |
273 | if ($cb) { |
223 | $ws->{ns}{widget_return}{$rid} = $cb; |
274 | $ws->{ns}{widget_return}{$rid} = $cb; |
224 | } else { |
275 | } else { |
225 | # synchronous case |
276 | # synchronous case |
… | |
… | |
238 | |
289 | |
239 | () |
290 | () |
240 | } |
291 | } |
241 | |
292 | |
242 | sub set { |
293 | sub set { |
243 | my ($self, $member, $value) = @_; |
294 | my ($self, @kv) = @_; |
244 | |
295 | |
245 | $self->msg (w_s => name => $member, value => $value); |
296 | $self->msg (w_s => \@kv); |
246 | } |
297 | } |
247 | |
298 | |
248 | sub get { |
299 | sub get { |
249 | my ($self, $member, $cb) = @_; |
300 | my ($self, $member, $cb) = @_; |
250 | |
301 | |
251 | $self->msg_cb ($cb, w_g => name => $member); |
302 | $self->msg_cb ($cb, w_g => [$member]); |
252 | } |
303 | } |
253 | |
304 | |
254 | sub TO_JSON { |
305 | sub TO_JSON { |
255 | { __widget_ref__ => $_[0]{id} } |
306 | { __w_ => $_[0]{id} } |
256 | } |
307 | } |
257 | |
308 | |
258 | our $AUTOLOAD; |
309 | our $AUTOLOAD; |
259 | |
310 | |
260 | sub AUTOLOAD { |
311 | sub AUTOLOAD { |
261 | $AUTOLOAD =~ s/^.*:// |
312 | $AUTOLOAD =~ s/^.*:// |
262 | or return; |
313 | or return; |
263 | |
314 | |
264 | my $self = shift; |
315 | my $self = shift; |
265 | |
316 | |
|
|
317 | #TODO: handle non-void context |
266 | $self->msg (w_c => name => $AUTOLOAD, args => \@_); |
318 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
267 | |
319 | |
268 | () |
320 | () |
269 | } |
321 | } |
270 | |
322 | |