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