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