… | |
… | |
3 | # sends the following ext message types |
3 | # sends the following ext message types |
4 | # ws_n id # widgetset new |
4 | # ws_n id # widgetset new |
5 | # ws_d id # widgetset destroy |
5 | # ws_d id # 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 | cf::client->attach ( |
15 | cf::client->attach ( |
16 | on_connect => sub { |
16 | on_connect => sub { |
17 | my ($ns) = @_; |
17 | my ($ns) = @_; |
18 | |
18 | |
|
|
19 | Scalar::Util::weaken (my $weakns = $ns); |
|
|
20 | |
19 | $ns->{id} = "a"; |
21 | $ns->{id} = "a"; |
|
|
22 | $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { |
|
|
23 | # cannot deserialise ATM |
|
|
24 | undef |
|
|
25 | }); |
20 | }, |
26 | }, |
21 | ); |
27 | ); |
22 | |
28 | |
23 | sub csc_update_stats { |
29 | sub csc_update_stats { |
24 | my ($ns) = @_; |
30 | my ($ns) = @_; |
25 | |
31 | |
26 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
32 | while (my ($k, $v) = each %{ $ns->{csc}{stat} }) { |
27 | $v->set_text ($ns->pl->ob->stats->$k); |
33 | $v->set_text ($ns->pl->ob->stats->$k); |
28 | } |
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; |
29 | } |
63 | } |
30 | |
64 | |
31 | sub csc_start { |
65 | sub csc_start { |
32 | my ($ns) = @_; |
66 | my ($ns) = @_; |
33 | |
67 | |
… | |
… | |
60 | [5, "Pow"], |
94 | [5, "Pow"], |
61 | [6, "Cha"], |
95 | [6, "Cha"], |
62 | ) { |
96 | ) { |
63 | my ($x, $label) = @$_; |
97 | my ($x, $label) = @$_; |
64 | |
98 | |
65 | $statstable->add ($x, 0, $ws->new (Label => |
99 | $statstable->add_at ($x, 0, $ws->new (Label => |
66 | can_hover => 1, can_events => 1, |
100 | can_hover => 1, can_events => 1, |
67 | align => +1, text => $label, tooltip => "#stat_$label", |
101 | align => +1, text => $label, tooltip => "#stat_$label", |
68 | )); |
102 | )); |
69 | $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
103 | $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
70 | can_hover => 1, can_events => 1, |
104 | can_hover => 1, can_events => 1, |
71 | align => +1, template => "88", tooltip => "#stat_$label", |
105 | align => +1, template => "88", tooltip => "#stat_$label", |
72 | )); |
106 | )); |
73 | } |
107 | } |
74 | |
108 | |
… | |
… | |
85 | |
119 | |
86 | my $ns = $pl->ns; |
120 | my $ns = $pl->ns; |
87 | |
121 | |
88 | return unless $ns->{can_widget}; |
122 | return unless $ns->{can_widget}; |
89 | |
123 | |
|
|
124 | demo_map $ns; |
90 | csc_start $ns; |
125 | #csc_start $ns; |
91 | }, |
126 | }, |
92 | ); |
127 | ); |
93 | |
128 | |
94 | cf::register_exticmd w_e => sub { |
129 | cf::register_exticmd w_e => sub { |
95 | my ($ns, $pkt) = @_; |
130 | my ($ns, $pkt) = @_; |
… | |
… | |
150 | sub msg { |
185 | sub msg { |
151 | my ($self, $type, %msg) = @_; |
186 | my ($self, $type, %msg) = @_; |
152 | |
187 | |
153 | if (my $ns = shift->{ns}) { |
188 | if (my $ns = shift->{ns}) { |
154 | $msg{msgtype} = $type; |
189 | $msg{msgtype} = $type; |
155 | $ns->send_packet ("ext " . cf::to_json \%msg); |
190 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
156 | } |
191 | } |
157 | } |
192 | } |
158 | |
193 | |
159 | sub new { |
194 | sub new { |
160 | my ($self, $class, %args) = @_; |
195 | my ($self, $class, %args) = @_; |
… | |
… | |
240 | } |
275 | } |
241 | |
276 | |
242 | sub set { |
277 | sub set { |
243 | my ($self, $member, $value) = @_; |
278 | my ($self, $member, $value) = @_; |
244 | |
279 | |
245 | $self->msg (w_s => name => $member, value => $value); |
280 | $self->msg (w_s => attr => [ [$member, $value] ]); |
246 | } |
281 | } |
247 | |
282 | |
248 | sub get { |
283 | sub get { |
249 | my ($self, $member, $cb) = @_; |
284 | my ($self, $member, $cb) = @_; |
250 | |
285 | |
251 | $self->msg_cb ($cb, w_g => name => $member); |
286 | $self->msg_cb ($cb, w_g => attr => [$member]); |
252 | } |
287 | } |
253 | |
288 | |
254 | sub TO_JSON { |
289 | sub TO_JSON { |
255 | { __widget_ref__ => $_[0]{id} } |
290 | { __widget_ref__ => $_[0]{id} } |
256 | } |
291 | } |