1 |
#! perl # mandatory depends=login |
2 |
|
3 |
# sends the following ext message types |
4 |
# ws_n id # widgetset new |
5 |
# ws_d id # 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 name value # widget member set |
9 |
# w_g id rid name # 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 |
cf::client->attach ( |
16 |
on_connect => sub { |
17 |
my ($ns) = @_; |
18 |
|
19 |
Scalar::Util::weaken (my $weakns = $ns); |
20 |
|
21 |
$ns->{id} = "a"; |
22 |
$ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { |
23 |
# cannot deserialise ATM |
24 |
undef |
25 |
}); |
26 |
}, |
27 |
); |
28 |
|
29 |
sub csc_update_stats { |
30 |
my ($ns) = @_; |
31 |
|
32 |
while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
33 |
$v->set_text ($ns->pl->ob->stats->$k); |
34 |
} |
35 |
} |
36 |
|
37 |
sub demo_map { |
38 |
my ($ns) = @_; |
39 |
|
40 |
my $ws = $ns->{csc} = $ns->new_widgetset; |
41 |
|
42 |
my $w = $ws->new (Toplevel => |
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 |
); |
53 |
|
54 |
my $face = cf::face::find "res/worldmap.jpg"; |
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 |
} |
64 |
|
65 |
sub csc_start { |
66 |
my ($ns) = @_; |
67 |
|
68 |
my $ws = $ns->{csc} = $ns->new_widgetset; |
69 |
|
70 |
my $w = $ws->new (Toplevel => |
71 |
min_w => 600, |
72 |
min_h => 400, |
73 |
x => "center", |
74 |
y => "center", |
75 |
title => "Character Creation", |
76 |
has_close_button => 1, |
77 |
on_delete => sub { |
78 |
$ws->destroy; |
79 |
}, |
80 |
); |
81 |
|
82 |
$w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
83 |
|
84 |
$ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); |
85 |
|
86 |
$stats->add (0, 0, (my $statstable = $ws->new ("Table"))); |
87 |
|
88 |
for ( |
89 |
[0, "Str"], |
90 |
[1, "Dex"], |
91 |
[2, "Con"], |
92 |
[3, "Int"], |
93 |
[4, "Wis"], |
94 |
[5, "Pow"], |
95 |
[6, "Cha"], |
96 |
) { |
97 |
my ($x, $label) = @$_; |
98 |
|
99 |
$statstable->add_at ($x, 0, $ws->new (Label => |
100 |
can_hover => 1, can_events => 1, |
101 |
align => +1, text => $label, tooltip => "#stat_$label", |
102 |
)); |
103 |
$statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
104 |
can_hover => 1, can_events => 1, |
105 |
align => +1, template => "88", tooltip => "#stat_$label", |
106 |
)); |
107 |
} |
108 |
|
109 |
csc_update_stats $ns; |
110 |
|
111 |
$w->show; |
112 |
} |
113 |
|
114 |
cf::player->attach ( |
115 |
on_login => sub { |
116 |
my ($pl) = @_; |
117 |
|
118 |
return unless $cf::CFG{devel}; |
119 |
|
120 |
my $ns = $pl->ns; |
121 |
|
122 |
return unless $ns->{can_widget}; |
123 |
|
124 |
demo_map $ns; |
125 |
#csc_start $ns; |
126 |
}, |
127 |
); |
128 |
|
129 |
cf::register_exticmd w_e => sub { |
130 |
my ($ns, $pkt) = @_; |
131 |
|
132 |
if (my $w = $ns->{widget}{$pkt->{id}}) { |
133 |
if (my $cb = $w->{ev}{$pkt->{name}}) { |
134 |
$_->($w, @{ $pkt->{args} || [] }) |
135 |
for @$cb; |
136 |
} |
137 |
} |
138 |
|
139 |
() |
140 |
}; |
141 |
|
142 |
cf::register_exticmd w_r => sub { |
143 |
my ($ns, $pkt) = @_; |
144 |
|
145 |
if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { |
146 |
$cb->(@{$pkt->{res} || [] }); |
147 |
} |
148 |
|
149 |
() |
150 |
}; |
151 |
|
152 |
sub cf::client::new_widgetset { |
153 |
my ($self) = @_; |
154 |
|
155 |
my $id = ++$self->{id}; |
156 |
|
157 |
my $ws = bless { |
158 |
id => $id, |
159 |
ns => $self, |
160 |
w => {}, |
161 |
}, "ext::widget::set"; |
162 |
|
163 |
$ws->msg (ws_n => id => $id); |
164 |
|
165 |
$ws |
166 |
} |
167 |
|
168 |
############################################################################# |
169 |
|
170 |
package ext::widget::set; |
171 |
|
172 |
sub DESTROY { |
173 |
$_[0]->destroy; |
174 |
} |
175 |
|
176 |
sub destroy { |
177 |
my ($self) = @_; |
178 |
|
179 |
$self->msg (ws_d => id => $self->{id}); |
180 |
delete $self->{ns}; |
181 |
$_->destroy |
182 |
for values %{ $self->{w} }; |
183 |
} |
184 |
|
185 |
sub msg { |
186 |
my ($self, $type, %msg) = @_; |
187 |
|
188 |
if (my $ns = shift->{ns}) { |
189 |
$msg{msgtype} = $type; |
190 |
$ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
191 |
} |
192 |
} |
193 |
|
194 |
sub new { |
195 |
my ($self, $class, %args) = @_; |
196 |
|
197 |
my $id = ++$self->{ns}{id}; |
198 |
|
199 |
my $proxy = $self->{w}{$id} = bless { |
200 |
id => $id, |
201 |
}, "ext::widget::proxy"; |
202 |
|
203 |
Scalar::Util::weaken ($proxy->{ws} = $self); |
204 |
Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
205 |
Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
206 |
|
207 |
for my $ev (grep /^on_/, keys %args) { |
208 |
push @{$proxy->{ev}{$ev}}, $args{$ev}; |
209 |
$args{$ev} = 0; |
210 |
} |
211 |
|
212 |
$self->msg (ws_c => |
213 |
ws => $self->{w}{id}, |
214 |
id => $id, |
215 |
class => $class, |
216 |
args => \%args, |
217 |
); |
218 |
|
219 |
$proxy |
220 |
} |
221 |
|
222 |
############################################################################# |
223 |
|
224 |
package ext::widget::proxy; |
225 |
|
226 |
sub DESTROY { |
227 |
my ($self) = @_; |
228 |
|
229 |
delete $self->{ns}{widget}{$self->{id}}; |
230 |
|
231 |
if (my $ws = $self->{ws}) { |
232 |
delete $ws->{w}{$self->{id}}; |
233 |
$self->msg (w_c => name => "destroy"); |
234 |
} |
235 |
} |
236 |
|
237 |
sub msg { |
238 |
my ($self, $type, %msg) = @_; |
239 |
|
240 |
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}; |
254 |
|
255 |
$self->msg ($type, %msg, rid => $rid); |
256 |
|
257 |
if ($cb) { |
258 |
$ws->{ns}{widget_return}{$rid} = $cb; |
259 |
} else { |
260 |
# synchronous case |
261 |
my $wait = new Coro::Signal; |
262 |
my @res; |
263 |
|
264 |
$ws->{ns}{widget_return}{$rid} = sub { |
265 |
@res = @_; |
266 |
$wait->send; |
267 |
}; |
268 |
$wait->wait; |
269 |
|
270 |
return @res; |
271 |
} |
272 |
} |
273 |
|
274 |
() |
275 |
} |
276 |
|
277 |
sub set { |
278 |
my ($self, $member, $value) = @_; |
279 |
|
280 |
$self->msg (w_s => name => $member, value => $value); |
281 |
} |
282 |
|
283 |
sub get { |
284 |
my ($self, $member, $cb) = @_; |
285 |
|
286 |
$self->msg_cb ($cb, w_g => name => $member); |
287 |
} |
288 |
|
289 |
sub TO_JSON { |
290 |
{ __widget_ref__ => $_[0]{id} } |
291 |
} |
292 |
|
293 |
our $AUTOLOAD; |
294 |
|
295 |
sub AUTOLOAD { |
296 |
$AUTOLOAD =~ s/^.*:// |
297 |
or return; |
298 |
|
299 |
my $self = shift; |
300 |
|
301 |
$self->msg (w_c => name => $AUTOLOAD, args => \@_); |
302 |
|
303 |
() |
304 |
} |
305 |
|