ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/widget.ext
Revision: 1.15
Committed: Tue Jul 24 00:26:42 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
Changes since 1.14: +2 -2 lines
Log Message:
compression

File Contents

# User Rev Content
1 root 1.1 #! perl # mandatory depends=login
2    
3     # sends the following ext message types
4 root 1.14 # ws_n ws # widgetset new
5     # ws_d ws # widgetset destroy
6 root 1.1 # ws_c ws id class args # widgetset create
7 root 1.14 # w_c id rid name args # widget method call
8 root 1.10 # w_s id @attr # widget member set
9     # w_g id rid @attr # widget member get
10 root 1.1 #
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 root 1.13 our $DEBUG = 1;
16    
17 root 1.1 cf::client->attach (
18     on_connect => sub {
19     my ($ns) = @_;
20    
21 root 1.4 Scalar::Util::weaken (my $weakns = $ns);
22    
23 root 1.1 $ns->{id} = "a";
24 root 1.15 $ns->{json_coder}->filter_json_single_key_object (__w_ => sub {
25 root 1.4 # cannot deserialise ATM
26     undef
27     });
28 root 1.1 },
29     );
30    
31 root 1.2 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 root 1.3 has_close_button => 1,
51     on_delete => sub {
52     $ws->destroy;
53     },
54 root 1.2 );
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 root 1.3 [0, "Str"],
64     [1, "Dex"],
65     [2, "Con"],
66     [3, "Int"],
67     [4, "Wis"],
68     [5, "Pow"],
69     [6, "Cha"],
70 root 1.2 ) {
71 root 1.3 my ($x, $label) = @$_;
72 root 1.2
73 root 1.9 $statstable->add_at ($x, 0, $ws->new (Label =>
74 root 1.2 can_hover => 1, can_events => 1,
75     align => +1, text => $label, tooltip => "#stat_$label",
76     ));
77 root 1.9 $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label =>
78 root 1.2 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 root 1.1 cf::player->attach (
89     on_login => sub {
90     my ($pl) = @_;
91    
92 root 1.8 return unless $cf::CFG{devel};
93 root 1.1
94     my $ns = $pl->ns;
95    
96 root 1.2 return unless $ns->{can_widget};
97 root 1.5 #csc_start $ns;
98 root 1.1 },
99     );
100    
101     cf::register_exticmd w_e => sub {
102 root 1.14 my ($ns, $id, $name, $args) = @_;
103 root 1.1
104 root 1.14 if (my $w = $ns->{widget}{$id}) {
105     if (my $cb = $w->{ev}{$name}) {
106     $_->($w, @$args)
107 root 1.1 for @$cb;
108     }
109     }
110    
111     ()
112     };
113    
114     cf::register_exticmd w_r => sub {
115 root 1.14 my ($ns, $rid, $res) = @_;
116 root 1.1
117 root 1.14 if (my $cb = delete $ns->{widget_return}{$rid}) {
118     $cb->(@$res);
119 root 1.1 }
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 root 1.12 _w => {},
133 root 1.1 }, "ext::widget::set";
134    
135 root 1.14 $ws->msg (ws_n => $id);
136 root 1.1
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 root 1.14 $self->msg (ws_d => $self->{id});
152 root 1.1 delete $self->{ns};
153     $_->destroy
154     for values %{ $self->{w} };
155     }
156    
157     sub msg {
158 root 1.14 my ($self, @msg) = @_;
159 root 1.1
160     if (my $ns = shift->{ns}) {
161 root 1.14 warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d#
162     $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg));
163 root 1.1 }
164     }
165    
166     sub new {
167     my ($self, $class, %args) = @_;
168    
169     my $id = ++$self->{ns}{id};
170    
171 root 1.13 my $proxy = bless {
172 root 1.1 id => $id,
173     }, "ext::widget::proxy";
174    
175 root 1.13 Scalar::Util::weaken ($self->{_w}{$id} = $proxy);
176 root 1.1 Scalar::Util::weaken ($proxy->{ws} = $self);
177     Scalar::Util::weaken ($proxy->{ns} = $self->{ns});
178     Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy);
179    
180     for my $ev (grep /^on_/, keys %args) {
181     push @{$proxy->{ev}{$ev}}, $args{$ev};
182     $args{$ev} = 0;
183     }
184    
185     $self->msg (ws_c =>
186 root 1.14 $proxy->{id},
187     $id,
188     $class,
189     \%args,
190 root 1.1 );
191    
192     $proxy
193     }
194    
195     #############################################################################
196    
197     package ext::widget::proxy;
198    
199     sub DESTROY {
200     my ($self) = @_;
201    
202     delete $self->{ns}{widget}{$self->{id}};
203    
204     if (my $ws = $self->{ws}) {
205 root 1.14 $self->msg (w_c => 0, "destroy");
206 root 1.12 delete $ws->{_w}{$self->{id}};
207 root 1.1 }
208     }
209    
210     sub msg {
211 root 1.14 my ($self, $type, @arg) = @_;
212 root 1.1
213     if (my $ws = $self->{ws}) {
214 root 1.14 $ws->msg ($type, $self->{id}, @arg);
215 root 1.1 }
216     }
217    
218     sub msg_cb {
219 root 1.14 my ($self, $cb, $type, @arg) = @_;
220 root 1.1
221     if (my $ws = $self->{ws}) {
222     my $rid = ++$ws->{ns}{id};
223    
224 root 1.14 $self->msg ($type, $rid, @arg);
225 root 1.1
226     if ($cb) {
227     $ws->{ns}{widget_return}{$rid} = $cb;
228     } else {
229     # synchronous case
230     my $wait = new Coro::Signal;
231     my @res;
232    
233     $ws->{ns}{widget_return}{$rid} = sub {
234     @res = @_;
235     $wait->send;
236     };
237     $wait->wait;
238    
239     return @res;
240     }
241     }
242    
243     ()
244     }
245    
246     sub set {
247 root 1.12 my ($self, @kv) = @_;
248 root 1.1
249 root 1.14 $self->msg (w_s => \@kv);
250 root 1.1 }
251    
252     sub get {
253     my ($self, $member, $cb) = @_;
254    
255 root 1.14 $self->msg_cb ($cb, w_g => [$member]);
256 root 1.1 }
257    
258     sub TO_JSON {
259 root 1.15 { __w_ => $_[0]{id} }
260 root 1.1 }
261    
262     our $AUTOLOAD;
263    
264     sub AUTOLOAD {
265     $AUTOLOAD =~ s/^.*://
266     or return;
267    
268     my $self = shift;
269    
270 root 1.14 $self->msg (w_c => 0, $AUTOLOAD, \@_);
271 root 1.1
272     ()
273     }
274