… | |
… | |
3 | # sends the following ext message types |
3 | # sends the following ext message types |
4 | # ws_n id # widgetset new |
4 | # ws_n id # widgetset new |
5 | # ws_d id # widgetset destroy |
5 | # ws_d id # widgetset destroy |
6 | # ws_c ws id class args # widgetset create |
6 | # ws_c ws id class args # widgetset create |
7 | # w_c id [rid] name args # widget method call |
7 | # w_c id [rid] name args # widget method call |
8 | # w_s id name value # widget member set |
8 | # w_s id @attr # widget member set |
9 | # w_g id rid name # widget member get |
9 | # w_g id rid @attr # widget member get |
10 | # |
10 | # |
11 | # and expects the following exti message types |
11 | # and expects the following exti message types |
12 | # w_r rid res # widget call return |
12 | # w_r rid res # widget call return |
13 | # w_e id name args # widget_event |
13 | # w_e id name args # widget_event |
14 | |
14 | |
|
|
15 | our $DEBUG = 1; |
|
|
16 | |
15 | cf::client->attach ( |
17 | cf::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 (__widget_ref__ => sub { |
|
|
25 | # cannot deserialise ATM |
|
|
26 | undef |
|
|
27 | }); |
20 | }, |
28 | }, |
21 | ); |
29 | ); |
22 | |
30 | |
23 | sub csc_update_stats { |
31 | sub csc_update_stats { |
24 | my ($ns) = @_; |
32 | my ($ns) = @_; |
… | |
… | |
60 | [5, "Pow"], |
68 | [5, "Pow"], |
61 | [6, "Cha"], |
69 | [6, "Cha"], |
62 | ) { |
70 | ) { |
63 | my ($x, $label) = @$_; |
71 | my ($x, $label) = @$_; |
64 | |
72 | |
65 | $statstable->add ($x, 0, $ws->new (Label => |
73 | $statstable->add_at ($x, 0, $ws->new (Label => |
66 | can_hover => 1, can_events => 1, |
74 | can_hover => 1, can_events => 1, |
67 | align => +1, text => $label, tooltip => "#stat_$label", |
75 | align => +1, text => $label, tooltip => "#stat_$label", |
68 | )); |
76 | )); |
69 | $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
77 | $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
70 | can_hover => 1, can_events => 1, |
78 | can_hover => 1, can_events => 1, |
71 | align => +1, template => "88", tooltip => "#stat_$label", |
79 | align => +1, template => "88", tooltip => "#stat_$label", |
72 | )); |
80 | )); |
73 | } |
81 | } |
74 | |
82 | |
… | |
… | |
84 | return unless $cf::CFG{devel}; |
92 | return unless $cf::CFG{devel}; |
85 | |
93 | |
86 | my $ns = $pl->ns; |
94 | my $ns = $pl->ns; |
87 | |
95 | |
88 | return unless $ns->{can_widget}; |
96 | return unless $ns->{can_widget}; |
89 | |
|
|
90 | csc_start $ns; |
97 | #csc_start $ns; |
91 | }, |
98 | }, |
92 | ); |
99 | ); |
93 | |
100 | |
94 | cf::register_exticmd w_e => sub { |
101 | cf::register_exticmd w_e => sub { |
95 | my ($ns, $pkt) = @_; |
102 | my ($ns, $pkt) = @_; |
… | |
… | |
120 | my $id = ++$self->{id}; |
127 | my $id = ++$self->{id}; |
121 | |
128 | |
122 | my $ws = bless { |
129 | my $ws = bless { |
123 | id => $id, |
130 | id => $id, |
124 | ns => $self, |
131 | ns => $self, |
125 | w => {}, |
132 | _w => {}, |
126 | }, "ext::widget::set"; |
133 | }, "ext::widget::set"; |
127 | |
134 | |
128 | $ws->msg (ws_n => id => $id); |
135 | $ws->msg (ws_n => id => $id); |
129 | |
136 | |
130 | $ws |
137 | $ws |
… | |
… | |
150 | sub msg { |
157 | sub msg { |
151 | my ($self, $type, %msg) = @_; |
158 | my ($self, $type, %msg) = @_; |
152 | |
159 | |
153 | if (my $ns = shift->{ns}) { |
160 | if (my $ns = shift->{ns}) { |
154 | $msg{msgtype} = $type; |
161 | $msg{msgtype} = $type; |
155 | $ns->send_packet ("ext " . cf::to_json \%msg); |
162 | warn "msg " . $ns->{json_coder}->encode (\%msg) if $DEBUG;#d# |
|
|
163 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
156 | } |
164 | } |
157 | } |
165 | } |
158 | |
166 | |
159 | sub new { |
167 | sub new { |
160 | my ($self, $class, %args) = @_; |
168 | my ($self, $class, %args) = @_; |
161 | |
169 | |
162 | my $id = ++$self->{ns}{id}; |
170 | my $id = ++$self->{ns}{id}; |
163 | |
171 | |
164 | my $proxy = $self->{w}{$id} = bless { |
172 | my $proxy = bless { |
165 | id => $id, |
173 | id => $id, |
166 | }, "ext::widget::proxy"; |
174 | }, "ext::widget::proxy"; |
167 | |
175 | |
|
|
176 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
168 | Scalar::Util::weaken ($proxy->{ws} = $self); |
177 | Scalar::Util::weaken ($proxy->{ws} = $self); |
169 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
178 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
170 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
179 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
171 | |
180 | |
172 | for my $ev (grep /^on_/, keys %args) { |
181 | for my $ev (grep /^on_/, keys %args) { |
173 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
182 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
174 | $args{$ev} = 0; |
183 | $args{$ev} = 0; |
175 | } |
184 | } |
176 | |
185 | |
177 | $self->msg (ws_c => |
186 | $self->msg (ws_c => |
178 | ws => $self->{w}{id}, |
187 | ws => $proxy->{id}, |
179 | id => $id, |
188 | id => $id, |
180 | class => $class, |
189 | class => $class, |
181 | args => \%args, |
190 | args => \%args, |
182 | ); |
191 | ); |
183 | |
192 | |
… | |
… | |
192 | my ($self) = @_; |
201 | my ($self) = @_; |
193 | |
202 | |
194 | delete $self->{ns}{widget}{$self->{id}}; |
203 | delete $self->{ns}{widget}{$self->{id}}; |
195 | |
204 | |
196 | if (my $ws = $self->{ws}) { |
205 | if (my $ws = $self->{ws}) { |
197 | delete $ws->{w}{$self->{id}}; |
|
|
198 | $self->msg (w_c => name => "destroy"); |
206 | $self->msg (w_c => name => "destroy"); |
|
|
207 | delete $ws->{_w}{$self->{id}}; |
199 | } |
208 | } |
200 | } |
209 | } |
201 | |
210 | |
202 | sub msg { |
211 | sub msg { |
203 | my ($self, $type, %msg) = @_; |
212 | my ($self, $type, %msg) = @_; |
… | |
… | |
238 | |
247 | |
239 | () |
248 | () |
240 | } |
249 | } |
241 | |
250 | |
242 | sub set { |
251 | sub set { |
243 | my ($self, $member, $value) = @_; |
252 | my ($self, @kv) = @_; |
244 | |
253 | |
245 | $self->msg (w_s => name => $member, value => $value); |
254 | $self->msg (w_s => attr => \@kv); |
246 | } |
255 | } |
247 | |
256 | |
248 | sub get { |
257 | sub get { |
249 | my ($self, $member, $cb) = @_; |
258 | my ($self, $member, $cb) = @_; |
250 | |
259 | |
251 | $self->msg_cb ($cb, w_g => name => $member); |
260 | $self->msg_cb ($cb, w_g => attr => [$member]); |
252 | } |
261 | } |
253 | |
262 | |
254 | sub TO_JSON { |
263 | sub TO_JSON { |
255 | { __widget_ref__ => $_[0]{id} } |
264 | { __widget_ref__ => $_[0]{id} } |
256 | } |
265 | } |