1 |
#! perl # mandatory depends=login |
2 |
|
3 |
# sends the following ext message types |
4 |
# ws_n ws # widgetset new |
5 |
# ws_d ws # widgetset destroy |
6 |
# ws_c ws id class args # widgetset create |
7 |
# w_c id rid name args # widget method call |
8 |
# w_s id @attr # widget member set |
9 |
# w_g id rid @attr # widget member get |
10 |
# |
11 |
# and expects the following exti message types |
12 |
# w_r rid res # widget call return |
13 |
# w_e id name args # widget_event |
14 |
|
15 |
our $DEBUG = 1; |
16 |
|
17 |
cf::client->attach ( |
18 |
on_connect => sub { |
19 |
my ($ns) = @_; |
20 |
|
21 |
Scalar::Util::weaken (my $weakns = $ns); |
22 |
|
23 |
$ns->{id} = "a"; |
24 |
$ns->{json_coder}->filter_json_single_key_object (__w_ => sub { |
25 |
# cannot deserialise ATM |
26 |
undef |
27 |
}); |
28 |
}, |
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 |
} |
87 |
|
88 |
cf::player->attach ( |
89 |
on_login => sub { |
90 |
my ($pl) = @_; |
91 |
|
92 |
return unless $cf::CFG{devel}; |
93 |
|
94 |
my $ns = $pl->ns; |
95 |
|
96 |
return unless $ns->{can_widget}; |
97 |
#csc_start $ns; |
98 |
}, |
99 |
); |
100 |
|
101 |
cf::register_exticmd w_e => sub { |
102 |
my ($ns, $id, $name, $args) = @_; |
103 |
|
104 |
if (my $w = $ns->{widget}{$id}) { |
105 |
if (my $cb = $w->{ev}{$name}) { |
106 |
$_->($w, @$args) |
107 |
for @$cb; |
108 |
} |
109 |
} |
110 |
|
111 |
() |
112 |
}; |
113 |
|
114 |
cf::register_exticmd w_r => sub { |
115 |
my ($ns, $rid, $res) = @_; |
116 |
|
117 |
if (my $cb = delete $ns->{widget_return}{$rid}) { |
118 |
$cb->(@$res); |
119 |
} |
120 |
|
121 |
() |
122 |
}; |
123 |
|
124 |
sub cf::client::new_widgetset { |
125 |
my ($self) = @_; |
126 |
|
127 |
my $id = ++$self->{id}; |
128 |
|
129 |
my $ws = bless { |
130 |
id => $id, |
131 |
ns => $self, |
132 |
_w => {}, |
133 |
}, "ext::widget::set"; |
134 |
|
135 |
$ws->msg (ws_n => $id); |
136 |
|
137 |
$ws |
138 |
} |
139 |
|
140 |
############################################################################# |
141 |
|
142 |
package ext::widget::set; |
143 |
|
144 |
sub DESTROY { |
145 |
$_[0]->destroy; |
146 |
} |
147 |
|
148 |
sub destroy { |
149 |
my ($self) = @_; |
150 |
|
151 |
$self->msg (ws_d => $self->{id}); |
152 |
delete $self->{ns}; |
153 |
$_->destroy |
154 |
for values %{ $self->{w} }; |
155 |
} |
156 |
|
157 |
sub msg { |
158 |
my ($self, @msg) = @_; |
159 |
|
160 |
if (my $ns = shift->{ns}) { |
161 |
warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
162 |
$ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
163 |
} |
164 |
} |
165 |
|
166 |
sub new { |
167 |
my ($self, $class, %args) = @_; |
168 |
|
169 |
my $id = ++$self->{ns}{id}; |
170 |
|
171 |
my $proxy = bless { |
172 |
id => $id, |
173 |
}, "ext::widget::proxy"; |
174 |
|
175 |
Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
176 |
Scalar::Util::weaken ($proxy->{ws} = $self); |
177 |
Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
178 |
Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
179 |
|
180 |
for my $ev (grep /^on_/, keys %args) { |
181 |
push @{$proxy->{ev}{$ev}}, $args{$ev}; |
182 |
$args{$ev} = 0; |
183 |
} |
184 |
|
185 |
$self->msg (ws_c => |
186 |
$self->{id}, |
187 |
$id, |
188 |
$class, |
189 |
\%args, |
190 |
); |
191 |
|
192 |
$proxy |
193 |
} |
194 |
|
195 |
############################################################################# |
196 |
|
197 |
package ext::widget::proxy; |
198 |
|
199 |
sub DESTROY { |
200 |
my ($self) = @_; |
201 |
|
202 |
delete $self->{ns}{widget}{$self->{id}}; |
203 |
|
204 |
if (my $ws = $self->{ws}) { |
205 |
$self->msg (w_c => 0, "destroy"); |
206 |
delete $ws->{_w}{$self->{id}}; |
207 |
} |
208 |
} |
209 |
|
210 |
sub msg { |
211 |
my ($self, $type, @arg) = @_; |
212 |
|
213 |
if (my $ws = $self->{ws}) { |
214 |
$ws->msg ($type, $self->{id}, @arg); |
215 |
} |
216 |
} |
217 |
|
218 |
sub msg_cb { |
219 |
my ($self, $cb, $type, @arg) = @_; |
220 |
|
221 |
if (my $ws = $self->{ws}) { |
222 |
my $rid = ++$ws->{ns}{id}; |
223 |
|
224 |
$self->msg ($type, $rid, @arg); |
225 |
|
226 |
if ($cb) { |
227 |
$ws->{ns}{widget_return}{$rid} = $cb; |
228 |
} else { |
229 |
# synchronous case |
230 |
my $wait = new Coro::Signal; |
231 |
my @res; |
232 |
|
233 |
$ws->{ns}{widget_return}{$rid} = sub { |
234 |
@res = @_; |
235 |
$wait->send; |
236 |
}; |
237 |
$wait->wait; |
238 |
|
239 |
return @res; |
240 |
} |
241 |
} |
242 |
|
243 |
() |
244 |
} |
245 |
|
246 |
sub set { |
247 |
my ($self, @kv) = @_; |
248 |
|
249 |
$self->msg (w_s => \@kv); |
250 |
} |
251 |
|
252 |
sub get { |
253 |
my ($self, $member, $cb) = @_; |
254 |
|
255 |
$self->msg_cb ($cb, w_g => [$member]); |
256 |
} |
257 |
|
258 |
sub TO_JSON { |
259 |
{ __w_ => $_[0]{id} } |
260 |
} |
261 |
|
262 |
our $AUTOLOAD; |
263 |
|
264 |
sub AUTOLOAD { |
265 |
$AUTOLOAD =~ s/^.*:// |
266 |
or return; |
267 |
|
268 |
my $self = shift; |
269 |
|
270 |
$self->msg (w_c => 0, $AUTOLOAD, \@_); |
271 |
|
272 |
() |
273 |
} |
274 |
|