ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/widget.ext
Revision: 1.13
Committed: Mon Jul 23 21:02:50 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
Changes since 1.12: +5 -2 lines
Log Message:
- new extcmd version, much streamlined
- new npc dialogue protocol
- older cfplus clients are supported, without cfplus_support (no dialogue),
  for some time.
- improved worldmap support.

File Contents

# Content
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 @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 (__widget_ref__ => 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, $pkt) = @_;
103
104 if (my $w = $ns->{widget}{$pkt->{id}}) {
105 if (my $cb = $w->{ev}{$pkt->{name}}) {
106 $_->($w, @{ $pkt->{args} || [] })
107 for @$cb;
108 }
109 }
110
111 ()
112 };
113
114 cf::register_exticmd w_r => sub {
115 my ($ns, $pkt) = @_;
116
117 if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) {
118 $cb->(@{$pkt->{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 => $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 => id => $self->{id});
152 delete $self->{ns};
153 $_->destroy
154 for values %{ $self->{w} };
155 }
156
157 sub msg {
158 my ($self, $type, %msg) = @_;
159
160 if (my $ns = shift->{ns}) {
161 $msg{msgtype} = $type;
162 warn "msg " . $ns->{json_coder}->encode (\%msg) if $DEBUG;#d#
163 $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg));
164 }
165 }
166
167 sub new {
168 my ($self, $class, %args) = @_;
169
170 my $id = ++$self->{ns}{id};
171
172 my $proxy = bless {
173 id => $id,
174 }, "ext::widget::proxy";
175
176 Scalar::Util::weaken ($self->{_w}{$id} = $proxy);
177 Scalar::Util::weaken ($proxy->{ws} = $self);
178 Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
179 Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
180
181 for my $ev (grep /^on_/, keys %args) {
182 push @{$proxy->{ev}{$ev}}, $args{$ev};
183 $args{$ev} = 0;
184 }
185
186 $self->msg (ws_c =>
187 ws => $proxy->{id},
188 id => $id,
189 class => $class,
190 args => \%args,
191 );
192
193 $proxy
194 }
195
196 #############################################################################
197
198 package ext::widget::proxy;
199
200 sub DESTROY {
201 my ($self) = @_;
202
203 delete $self->{ns}{widget}{$self->{id}};
204
205 if (my $ws = $self->{ws}) {
206 $self->msg (w_c => name => "destroy");
207 delete $ws->{_w}{$self->{id}};
208 }
209 }
210
211 sub msg {
212 my ($self, $type, %msg) = @_;
213
214 if (my $ws = $self->{ws}) {
215 $ws->msg ($type,
216 %msg,
217 id => $self->{id},
218 );
219 }
220 }
221
222 sub msg_cb {
223 my ($self, $cb, $type, %msg) = @_;
224
225 if (my $ws = $self->{ws}) {
226
227 my $rid = ++$ws->{ns}{id};
228
229 $self->msg ($type, %msg, rid => $rid);
230
231 if ($cb) {
232 $ws->{ns}{widget_return}{$rid} = $cb;
233 } else {
234 # synchronous case
235 my $wait = new Coro::Signal;
236 my @res;
237
238 $ws->{ns}{widget_return}{$rid} = sub {
239 @res = @_;
240 $wait->send;
241 };
242 $wait->wait;
243
244 return @res;
245 }
246 }
247
248 ()
249 }
250
251 sub set {
252 my ($self, @kv) = @_;
253
254 $self->msg (w_s => attr => \@kv);
255 }
256
257 sub get {
258 my ($self, $member, $cb) = @_;
259
260 $self->msg_cb ($cb, w_g => attr => [$member]);
261 }
262
263 sub TO_JSON {
264 { __widget_ref__ => $_[0]{id} }
265 }
266
267 our $AUTOLOAD;
268
269 sub AUTOLOAD {
270 $AUTOLOAD =~ s/^.*://
271 or return;
272
273 my $self = shift;
274
275 $self->msg (w_c => name => $AUTOLOAD, args => \@_);
276
277 ()
278 }
279