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