… | |
… | |
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) = @_; |
… | |
… | |
37 | min_w => 600, |
43 | min_w => 600, |
38 | min_h => 400, |
44 | min_h => 400, |
39 | x => "center", |
45 | x => "center", |
40 | y => "center", |
46 | y => "center", |
41 | title => "Character Creation", |
47 | title => "Character Creation", |
|
|
48 | has_close_button => 1, |
|
|
49 | on_delete => sub { |
|
|
50 | $ws->destroy; |
|
|
51 | }, |
42 | ); |
52 | ); |
43 | |
53 | |
44 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
54 | $w->add (my $ntb = $ws->new (Notebook => expand => 1)); |
45 | |
55 | |
46 | $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); |
56 | $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); |
47 | |
57 | |
48 | $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); |
58 | $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); |
49 | |
59 | |
50 | for ( |
60 | for ( |
51 | [0, "Str", "Strength"], |
61 | [0, "Str"], |
52 | [1, "Dex", "Dexterity"], |
62 | [1, "Dex"], |
53 | [2, "Con", "Constitution"], |
63 | [2, "Con"], |
54 | [3, "Int", "Intelligence"], |
64 | [3, "Int"], |
55 | [4, "Wis", "Wisdom"], |
65 | [4, "Wis"], |
56 | [5, "Pow", "Power"], |
66 | [5, "Pow"], |
57 | [6, "Cha", "Charisma"], |
67 | [6, "Cha"], |
58 | ) { |
68 | ) { |
59 | my ($x, $label, $description) = @$_; |
69 | my ($x, $label) = @$_; |
60 | |
70 | |
61 | $statstable->add ($x, 0, $ws->new (Label => |
71 | $statstable->add_at ($x, 0, $ws->new (Label => |
62 | can_hover => 1, can_events => 1, |
72 | can_hover => 1, can_events => 1, |
63 | align => +1, text => $label, tooltip => "#stat_$label", |
73 | align => +1, text => $label, tooltip => "#stat_$label", |
64 | )); |
74 | )); |
65 | $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
75 | $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
66 | can_hover => 1, can_events => 1, |
76 | can_hover => 1, can_events => 1, |
67 | align => +1, template => "88", tooltip => "#stat_$label", |
77 | align => +1, template => "88", tooltip => "#stat_$label", |
68 | )); |
78 | )); |
69 | } |
79 | } |
70 | |
80 | |
… | |
… | |
80 | return unless $cf::CFG{devel}; |
90 | return unless $cf::CFG{devel}; |
81 | |
91 | |
82 | my $ns = $pl->ns; |
92 | my $ns = $pl->ns; |
83 | |
93 | |
84 | return unless $ns->{can_widget}; |
94 | return unless $ns->{can_widget}; |
85 | |
|
|
86 | csc_start $ns; |
95 | #csc_start $ns; |
87 | }, |
96 | }, |
88 | ); |
97 | ); |
89 | |
98 | |
90 | cf::register_exticmd w_e => sub { |
99 | cf::register_exticmd w_e => sub { |
91 | my ($ns, $pkt) = @_; |
100 | my ($ns, $pkt) = @_; |
… | |
… | |
116 | my $id = ++$self->{id}; |
125 | my $id = ++$self->{id}; |
117 | |
126 | |
118 | my $ws = bless { |
127 | my $ws = bless { |
119 | id => $id, |
128 | id => $id, |
120 | ns => $self, |
129 | ns => $self, |
121 | w => {}, |
130 | _w => {}, |
122 | }, "ext::widget::set"; |
131 | }, "ext::widget::set"; |
123 | |
132 | |
124 | $ws->msg (ws_n => id => $id); |
133 | $ws->msg (ws_n => id => $id); |
125 | |
134 | |
126 | $ws |
135 | $ws |
… | |
… | |
146 | sub msg { |
155 | sub msg { |
147 | my ($self, $type, %msg) = @_; |
156 | my ($self, $type, %msg) = @_; |
148 | |
157 | |
149 | if (my $ns = shift->{ns}) { |
158 | if (my $ns = shift->{ns}) { |
150 | $msg{msgtype} = $type; |
159 | $msg{msgtype} = $type; |
151 | $ns->send_packet ("ext " . cf::to_json \%msg); |
160 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
152 | } |
161 | } |
153 | } |
162 | } |
154 | |
163 | |
155 | sub new { |
164 | sub new { |
156 | my ($self, $class, %args) = @_; |
165 | my ($self, $class, %args) = @_; |
157 | |
166 | |
158 | my $id = ++$self->{ns}{id}; |
167 | my $id = ++$self->{ns}{id}; |
159 | |
168 | |
160 | my $proxy = $self->{w}{$id} = bless { |
169 | my $proxy = $self->{_w}{$id} = bless { |
161 | id => $id, |
170 | id => $id, |
162 | }, "ext::widget::proxy"; |
171 | }, "ext::widget::proxy"; |
163 | |
172 | |
164 | Scalar::Util::weaken ($proxy->{ws} = $self); |
173 | Scalar::Util::weaken ($proxy->{ws} = $self); |
165 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
174 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
… | |
… | |
169 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
178 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
170 | $args{$ev} = 0; |
179 | $args{$ev} = 0; |
171 | } |
180 | } |
172 | |
181 | |
173 | $self->msg (ws_c => |
182 | $self->msg (ws_c => |
174 | ws => $self->{w}{id}, |
183 | ws => $proxy->{id}, |
175 | id => $id, |
184 | id => $id, |
176 | class => $class, |
185 | class => $class, |
177 | args => \%args, |
186 | args => \%args, |
178 | ); |
187 | ); |
179 | |
188 | |
… | |
… | |
187 | sub DESTROY { |
196 | sub DESTROY { |
188 | my ($self) = @_; |
197 | my ($self) = @_; |
189 | |
198 | |
190 | delete $self->{ns}{widget}{$self->{id}}; |
199 | delete $self->{ns}{widget}{$self->{id}}; |
191 | |
200 | |
|
|
201 | #warn "DES<$self> $self->{ws}\n";#d# |
192 | if (my $ws = $self->{ws}) { |
202 | if (my $ws = $self->{ws}) { |
193 | delete $ws->{w}{$self->{id}}; |
|
|
194 | $self->msg (w_c => name => "destroy"); |
203 | $self->msg (w_c => name => "destroy"); |
|
|
204 | delete $ws->{_w}{$self->{id}}; |
195 | } |
205 | } |
196 | } |
206 | } |
197 | |
207 | |
198 | sub msg { |
208 | sub msg { |
199 | my ($self, $type, %msg) = @_; |
209 | my ($self, $type, %msg) = @_; |
… | |
… | |
234 | |
244 | |
235 | () |
245 | () |
236 | } |
246 | } |
237 | |
247 | |
238 | sub set { |
248 | sub set { |
239 | my ($self, $member, $value) = @_; |
249 | my ($self, @kv) = @_; |
240 | |
250 | |
241 | $self->msg (w_s => name => $member, value => $value); |
251 | $self->msg (w_s => attr => \@kv); |
242 | } |
252 | } |
243 | |
253 | |
244 | sub get { |
254 | sub get { |
245 | my ($self, $member, $cb) = @_; |
255 | my ($self, $member, $cb) = @_; |
246 | |
256 | |
247 | $self->msg_cb ($cb, w_g => name => $member); |
257 | $self->msg_cb ($cb, w_g => attr => [$member]); |
248 | } |
258 | } |
249 | |
259 | |
250 | sub TO_JSON { |
260 | sub TO_JSON { |
251 | { __widget_ref__ => $_[0]{id} } |
261 | { __widget_ref__ => $_[0]{id} } |
252 | } |
262 | } |