… | |
… | |
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 | 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 (__widget_ref__ => 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, $pkt) = @_; |
… | |
… | |
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 => $id); |
102 | |
136 | |
103 | $ws |
137 | $ws |
… | |
… | |
123 | sub msg { |
157 | sub msg { |
124 | my ($self, $type, %msg) = @_; |
158 | my ($self, $type, %msg) = @_; |
125 | |
159 | |
126 | if (my $ns = shift->{ns}) { |
160 | if (my $ns = shift->{ns}) { |
127 | $msg{msgtype} = $type; |
161 | $msg{msgtype} = $type; |
128 | $ns->send_packet ("ext " . cf::to_json \%msg); |
162 | warn "msg " . $ns->{json_coder}->encode (\%msg) if $DEBUG;#d# |
|
|
163 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
129 | } |
164 | } |
130 | } |
165 | } |
131 | |
166 | |
132 | sub new { |
167 | sub new { |
133 | my ($self, $class, %args) = @_; |
168 | my ($self, $class, %args) = @_; |
134 | |
169 | |
135 | my $id = ++$self->{ns}{id}; |
170 | my $id = ++$self->{ns}{id}; |
136 | |
171 | |
137 | my $proxy = $self->{w}{$id} = bless { |
172 | my $proxy = bless { |
138 | id => $id, |
173 | id => $id, |
139 | }, "ext::widget::proxy"; |
174 | }, "ext::widget::proxy"; |
140 | |
175 | |
|
|
176 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
141 | Scalar::Util::weaken ($proxy->{ws} = $self); |
177 | Scalar::Util::weaken ($proxy->{ws} = $self); |
142 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
178 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
143 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
179 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
144 | |
180 | |
145 | for my $ev (grep /^on_/, keys %args) { |
181 | for my $ev (grep /^on_/, keys %args) { |
146 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
182 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
147 | $args{$ev} = 0; |
183 | $args{$ev} = 0; |
148 | } |
184 | } |
149 | |
185 | |
150 | $self->msg (ws_c => |
186 | $self->msg (ws_c => |
151 | ws => $self->{w}{id}, |
187 | ws => $proxy->{id}, |
152 | id => $id, |
188 | id => $id, |
153 | class => $class, |
189 | class => $class, |
154 | args => \%args, |
190 | args => \%args, |
155 | ); |
191 | ); |
156 | |
192 | |
… | |
… | |
165 | my ($self) = @_; |
201 | my ($self) = @_; |
166 | |
202 | |
167 | delete $self->{ns}{widget}{$self->{id}}; |
203 | delete $self->{ns}{widget}{$self->{id}}; |
168 | |
204 | |
169 | if (my $ws = $self->{ws}) { |
205 | if (my $ws = $self->{ws}) { |
170 | delete $ws->{w}{$self->{id}}; |
|
|
171 | $self->msg (w_c => name => "destroy"); |
206 | $self->msg (w_c => name => "destroy"); |
|
|
207 | delete $ws->{_w}{$self->{id}}; |
172 | } |
208 | } |
173 | } |
209 | } |
174 | |
210 | |
175 | sub msg { |
211 | sub msg { |
176 | my ($self, $type, %msg) = @_; |
212 | my ($self, $type, %msg) = @_; |
… | |
… | |
211 | |
247 | |
212 | () |
248 | () |
213 | } |
249 | } |
214 | |
250 | |
215 | sub set { |
251 | sub set { |
216 | my ($self, $member, $value) = @_; |
252 | my ($self, @kv) = @_; |
217 | |
253 | |
218 | $self->msg (w_s => name => $member, value => $value); |
254 | $self->msg (w_s => attr => \@kv); |
219 | } |
255 | } |
220 | |
256 | |
221 | sub get { |
257 | sub get { |
222 | my ($self, $member, $cb) = @_; |
258 | my ($self, $member, $cb) = @_; |
223 | |
259 | |
224 | $self->msg_cb ($cb, w_g => name => $member); |
260 | $self->msg_cb ($cb, w_g => attr => [$member]); |
225 | } |
261 | } |
226 | |
262 | |
227 | sub TO_JSON { |
263 | sub TO_JSON { |
228 | { __widget_ref__ => $_[0]{id} } |
264 | { __widget_ref__ => $_[0]{id} } |
229 | } |
265 | } |