… | |
… | |
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 | |
|
|
15 | our $DEBUG = 1; |
14 | |
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 | |
… | |
… | |
30 | my ($ns) = @_; |
32 | my ($ns) = @_; |
31 | |
33 | |
32 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
34 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
33 | $v->set_text ($ns->pl->ob->stats->$k); |
35 | $v->set_text ($ns->pl->ob->stats->$k); |
34 | } |
36 | } |
35 | } |
|
|
36 | |
|
|
37 | sub demo_map { |
|
|
38 | my ($ns) = @_; |
|
|
39 | |
|
|
40 | my $ws = $ns->{csc} = $ns->new_widgetset; |
|
|
41 | |
|
|
42 | my $w = $ws->new (Toplevel => |
|
|
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 | ); |
|
|
53 | |
|
|
54 | my $face = cf::face::find "res/worldmap.jpg"; |
|
|
55 | $ns->send_face ($face); |
|
|
56 | |
|
|
57 | $w->add (my $sw = $ws->new (ScrolledWindow => scroll_x => 1, scroll_y => 1)); |
|
|
58 | $sw->add (my $fixed = $ws->new (Fixed => expand => 1)); |
|
|
59 | $fixed->add ($ws->new (Face => expand => 1, size_w => undef, size_h => undef, face => $face), abs => 0, 0, rel => 1, 1); |
|
|
60 | $fixed->add ($ws->new (Label => text => "lb1"), abs => 10, 10, rel => 1, 1); |
|
|
61 | |
|
|
62 | $w->show; |
|
|
63 | } |
37 | } |
64 | |
38 | |
65 | sub csc_start { |
39 | sub csc_start { |
66 | my ($ns) = @_; |
40 | my ($ns) = @_; |
67 | |
41 | |
… | |
… | |
94 | [5, "Pow"], |
68 | [5, "Pow"], |
95 | [6, "Cha"], |
69 | [6, "Cha"], |
96 | ) { |
70 | ) { |
97 | my ($x, $label) = @$_; |
71 | my ($x, $label) = @$_; |
98 | |
72 | |
99 | $statstable->add ($x, 0, $ws->new (Label => |
73 | $statstable->add_at ($x, 0, $ws->new (Label => |
100 | can_hover => 1, can_events => 1, |
74 | can_hover => 1, can_events => 1, |
101 | align => +1, text => $label, tooltip => "#stat_$label", |
75 | align => +1, text => $label, tooltip => "#stat_$label", |
102 | )); |
76 | )); |
103 | $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
77 | $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
104 | can_hover => 1, can_events => 1, |
78 | can_hover => 1, can_events => 1, |
105 | align => +1, template => "88", tooltip => "#stat_$label", |
79 | align => +1, template => "88", tooltip => "#stat_$label", |
106 | )); |
80 | )); |
107 | } |
81 | } |
108 | |
82 | |
… | |
… | |
118 | return unless $cf::CFG{devel}; |
92 | return unless $cf::CFG{devel}; |
119 | |
93 | |
120 | my $ns = $pl->ns; |
94 | my $ns = $pl->ns; |
121 | |
95 | |
122 | return unless $ns->{can_widget}; |
96 | return unless $ns->{can_widget}; |
123 | |
|
|
124 | demo_map $ns; |
|
|
125 | #csc_start $ns; |
97 | #csc_start $ns; |
126 | }, |
98 | }, |
127 | ); |
99 | ); |
128 | |
100 | |
129 | cf::register_exticmd w_e => sub { |
101 | cf::register_exticmd w_e => sub { |
… | |
… | |
155 | my $id = ++$self->{id}; |
127 | my $id = ++$self->{id}; |
156 | |
128 | |
157 | my $ws = bless { |
129 | my $ws = bless { |
158 | id => $id, |
130 | id => $id, |
159 | ns => $self, |
131 | ns => $self, |
160 | w => {}, |
132 | _w => {}, |
161 | }, "ext::widget::set"; |
133 | }, "ext::widget::set"; |
162 | |
134 | |
163 | $ws->msg (ws_n => id => $id); |
135 | $ws->msg (ws_n => id => $id); |
164 | |
136 | |
165 | $ws |
137 | $ws |
… | |
… | |
185 | sub msg { |
157 | sub msg { |
186 | my ($self, $type, %msg) = @_; |
158 | my ($self, $type, %msg) = @_; |
187 | |
159 | |
188 | if (my $ns = shift->{ns}) { |
160 | if (my $ns = shift->{ns}) { |
189 | $msg{msgtype} = $type; |
161 | $msg{msgtype} = $type; |
|
|
162 | warn "msg " . $ns->{json_coder}->encode (\%msg) if $DEBUG;#d# |
190 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
163 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
191 | } |
164 | } |
192 | } |
165 | } |
193 | |
166 | |
194 | sub new { |
167 | sub new { |
195 | my ($self, $class, %args) = @_; |
168 | my ($self, $class, %args) = @_; |
196 | |
169 | |
197 | my $id = ++$self->{ns}{id}; |
170 | my $id = ++$self->{ns}{id}; |
198 | |
171 | |
199 | my $proxy = $self->{w}{$id} = bless { |
172 | my $proxy = bless { |
200 | id => $id, |
173 | id => $id, |
201 | }, "ext::widget::proxy"; |
174 | }, "ext::widget::proxy"; |
202 | |
175 | |
|
|
176 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
203 | Scalar::Util::weaken ($proxy->{ws} = $self); |
177 | Scalar::Util::weaken ($proxy->{ws} = $self); |
204 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
178 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
205 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
179 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
206 | |
180 | |
207 | for my $ev (grep /^on_/, keys %args) { |
181 | for my $ev (grep /^on_/, keys %args) { |
208 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
182 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
209 | $args{$ev} = 0; |
183 | $args{$ev} = 0; |
210 | } |
184 | } |
211 | |
185 | |
212 | $self->msg (ws_c => |
186 | $self->msg (ws_c => |
213 | ws => $self->{w}{id}, |
187 | ws => $proxy->{id}, |
214 | id => $id, |
188 | id => $id, |
215 | class => $class, |
189 | class => $class, |
216 | args => \%args, |
190 | args => \%args, |
217 | ); |
191 | ); |
218 | |
192 | |
… | |
… | |
227 | my ($self) = @_; |
201 | my ($self) = @_; |
228 | |
202 | |
229 | delete $self->{ns}{widget}{$self->{id}}; |
203 | delete $self->{ns}{widget}{$self->{id}}; |
230 | |
204 | |
231 | if (my $ws = $self->{ws}) { |
205 | if (my $ws = $self->{ws}) { |
232 | delete $ws->{w}{$self->{id}}; |
|
|
233 | $self->msg (w_c => name => "destroy"); |
206 | $self->msg (w_c => name => "destroy"); |
|
|
207 | delete $ws->{_w}{$self->{id}}; |
234 | } |
208 | } |
235 | } |
209 | } |
236 | |
210 | |
237 | sub msg { |
211 | sub msg { |
238 | my ($self, $type, %msg) = @_; |
212 | my ($self, $type, %msg) = @_; |
… | |
… | |
273 | |
247 | |
274 | () |
248 | () |
275 | } |
249 | } |
276 | |
250 | |
277 | sub set { |
251 | sub set { |
278 | my ($self, $member, $value) = @_; |
252 | my ($self, @kv) = @_; |
279 | |
253 | |
280 | $self->msg (w_s => name => $member, value => $value); |
254 | $self->msg (w_s => attr => \@kv); |
281 | } |
255 | } |
282 | |
256 | |
283 | sub get { |
257 | sub get { |
284 | my ($self, $member, $cb) = @_; |
258 | my ($self, $member, $cb) = @_; |
285 | |
259 | |
286 | $self->msg_cb ($cb, w_g => name => $member); |
260 | $self->msg_cb ($cb, w_g => attr => [$member]); |
287 | } |
261 | } |
288 | |
262 | |
289 | sub TO_JSON { |
263 | sub TO_JSON { |
290 | { __widget_ref__ => $_[0]{id} } |
264 | { __widget_ref__ => $_[0]{id} } |
291 | } |
265 | } |