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