--- deliantra/server/ext/widget.ext 2007/07/23 23:38:17 1.14 +++ deliantra/server/ext/widget.ext 2007/09/01 07:22:20 1.19 @@ -1,16 +1,16 @@ #! perl # mandatory depends=login # sends the following ext message types +# ws_a id name... # associate well-known widget with given id # ws_n ws # widgetset new # ws_d ws # widgetset destroy -# ws_c ws id class args # widgetset create -# w_c id rid name args # widget method call +# ws_c ws id class @args # widgetset create +# w_c id rid name @args # widget method call # w_s id @attr # widget member set # w_g id rid @attr # widget member get # # and expects the following exti message types -# w_r rid res # widget call return -# w_e id name args # widget_event +# w_e id @args # widget_call our $DEBUG = 1; @@ -21,9 +21,8 @@ Scalar::Util::weaken (my $weakns = $ns); $ns->{id} = "a"; - $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { - # cannot deserialise ATM - undef + $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { + $weakns->{widget}{$_[0]} }); }, ); @@ -36,6 +35,17 @@ } } +sub demo_start { + my ($ns) = @_; + + my $ws = $ns->{csc} = $ns->new_widgetset; + + $ws->{tab} = $ws->new (Label => text => "dumb tst", c_tab => ["hull"]); + + $ws->find ("setup_notebook")->add ($ws->{tab}); + $ws->find ("setup_dialog")->toggle_visibility; +} + sub csc_start { my ($ns) = @_; @@ -55,9 +65,9 @@ $w->add (my $ntb = $ws->new (Notebook => expand => 1)); - $ntb->add (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); + $ntb->add_tab (Statistics => (my $stats = $ws->new (Table => expand => 1)), "Basic statistics of your new character"); - $stats->add (0, 0, (my $statstable = $ws->new ("Table"))); + $stats->add_at (0, 0, (my $statstable = $ws->new ("Table"))); for ( [0, "Str"], @@ -82,6 +92,7 @@ csc_update_stats $ns; + $ws->{tl} = $w; $w->show; } @@ -95,27 +106,15 @@ return unless $ns->{can_widget}; #csc_start $ns; + #demo_start $ns; }, ); cf::register_exticmd w_e => sub { - my ($ns, $id, $name, $args) = @_; - - if (my $w = $ns->{widget}{$id}) { - if (my $cb = $w->{ev}{$name}) { - $_->($w, @$args) - for @$cb; - } - } + my ($ns, $id, @args) = @_; - () -}; - -cf::register_exticmd w_r => sub { - my ($ns, $rid, $res) = @_; - - if (my $cb = delete $ns->{widget_return}{$rid}) { - $cb->(@$res); + if (my $cb = $ns->{widget_cb}{$id}) { + $cb->(@args); } () @@ -137,6 +136,15 @@ $ws } +sub cf::client::alloc_wid { + pop @{ $_[0]{ids} } + or ++$_[0]{id} +} + +sub cf::client::free_wid { + push @{ $_[0]{ids} }, $_[1]; +} + ############################################################################# package ext::widget::set; @@ -163,28 +171,36 @@ } } -sub new { - my ($self, $class, %args) = @_; +sub alloc { + my ($self) = @_; - my $id = ++$self->{ns}{id}; + my $id = $self->{ns}->alloc_wid; my $proxy = bless { id => $id, }, "ext::widget::proxy"; - Scalar::Util::weaken ($self->{_w}{$id} = $proxy); - Scalar::Util::weaken ($proxy->{ws} = $self); Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); + $proxy +} + +sub new { + my ($self, $class, %args) = @_; + + my $proxy = $self->alloc; + + Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); + Scalar::Util::weaken ($proxy->{ws} = $self); + for my $ev (grep /^on_/, keys %args) { - push @{$proxy->{ev}{$ev}}, $args{$ev}; - $args{$ev} = 0; + $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); } $self->msg (ws_c => + $self->{id}, $proxy->{id}, - $id, $class, \%args, ); @@ -192,6 +208,28 @@ $proxy } +sub find { + my ($self, @names) = @_; + + my @res; + my @alloc; + + for my $name (@names) { + push @res, $self->{ns}{widget_wkw}{$name} ||= do { + my $proxy = $self->alloc; + + push @alloc, $proxy->{id} => $name; + + $proxy + }; + } + + $self->msg (ws_a => @alloc) + if @alloc; + + wantarray ? @res : $res[0] +} + ############################################################################# package ext::widget::proxy; @@ -207,11 +245,42 @@ } } +sub cb { + my ($self, $cb) = @_; + + my $proxy = bless { + ns => $self->{ns}, + id => $self->{ns}->alloc_wid, + }, "ext::widget::callback"; + + Scalar::Util::weaken $proxy->{ns}; + + $self->{ns}{widget_cb}{$proxy->{id}} = $cb; + + $proxy +} + +sub oneshot_cb { + my ($self, $cb) = @_; + + if ("CODE" eq ref $cb) { + my $ocb = $cb; + $cb = $self->cb (sub { + undef $cb; + &$ocb + }); + } + + $cb +} + sub msg { my ($self, $type, @arg) = @_; - if (my $ws = $self->{ws}) { - $ws->msg ($type, $self->{id}, @arg); + if (my $ns = $self->{ns}) { + my @msg = ($type, $self->{id}, @arg); + warn "MSG " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# + $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); } } @@ -219,21 +288,29 @@ my ($self, $cb, $type, @arg) = @_; if (my $ws = $self->{ws}) { - my $rid = ++$ws->{ns}{id}; - - $self->msg ($type, $rid, @arg); + my $rid = $ws->{ns}->alloc_wid; if ($cb) { - $ws->{ns}{widget_return}{$rid} = $cb; + $ws->{ns}{widget_cb}{$rid} = sub { + delete $ws->{ns}{widget_cb}{$rid}; + $ws->{ns}->free_wid ($rid); + &$cb + }; + + $self->msg ($type, $rid, @arg); } else { # synchronous case my $wait = new Coro::Signal; my @res; - $ws->{ns}{widget_return}{$rid} = sub { + $ws->{ns}{widget_cb}{$rid} = sub { + delete $ws->{ns}{widget_cb}{$rid}; + $ws->{ns}->free_wid ($rid); + @res = @_; $wait->send; }; + $self->msg ($type, $rid, @arg); $wait->wait; return @res; @@ -252,11 +329,11 @@ sub get { my ($self, $member, $cb) = @_; - $self->msg_cb ($cb, w_g => [$member]); + $self->msg_cb ($cb, w_g => ref $member ? @$member : $member); } sub TO_JSON { - { __widget_ref__ => $_[0]{id} } + { "\fw" => $_[0]{id} } } our $AUTOLOAD; @@ -267,8 +344,24 @@ my $self = shift; - $self->msg (w_c => 0, $AUTOLOAD, \@_); + #TODO: handle non-void context + $self->msg (w_c => 0, $AUTOLOAD, @_); () } +package ext::widget::callback; + +sub DESTROY { + my ($self) = @_; + + if (my $ns = $self->{ns}) { + delete $ns->{widget_cb}{$self->{id}}; + $ns->free_wid ($self->{id}); + } +} + +sub TO_JSON { + { "\fc" => $_[0]{id} } +} +