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