--- deliantra/server/ext/widget.ext 2007/07/16 14:09:40 1.7 +++ deliantra/server/ext/widget.ext 2007/12/27 18:35:48 1.20 @@ -1,16 +1,19 @@ #! perl # mandatory depends=login # sends the following ext message types -# ws_n id # widgetset new -# ws_d id # widgetset destroy -# ws_c ws id class args # widgetset create -# w_c id [rid] name args # widget method call -# w_s id name value # widget member set -# w_g id rid name # widget member get +# 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 +# ws_ct ws templateface \%cfg # widgetset create from template +# 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; cf::client->attach ( on_connect => sub { @@ -19,9 +22,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]} }); }, ); @@ -34,32 +36,27 @@ } } -sub demo_map { +sub demo_start { my ($ns) = @_; my $ws = $ns->{csc} = $ns->new_widgetset; - my $w = $ws->new (Toplevel => - w => 200, - h => 200, - x => "center", - y => "center", - title => "Worldmap", - has_close_button => 1, - on_delete => sub { - $ws->destroy; + my ($tl, $entry) = $ws->template (undef, + toplevel => {}, + entry => { + text => "xyz", + on_changed => sub { + warn "changed<@_>\n";#d# + }, }, ); - my $face = cf::face::find "res/worldmap.jpg"; - $ns->send_face ($face); - - $w->add (my $sw = $ws->new (ScrolledWindow => scroll_x => 1, scroll_y => 1)); - $sw->add (my $fixed = $ws->new (Fixed => expand => 1)); - $fixed->add ($ws->new (Face => expand => 1, size_w => undef, size_h => undef, face => $face), abs => 0, 0, rel => 1, 1); - $fixed->add ($ws->new (Label => text => "lb1"), abs => 10, 10, rel => 1, 1); + $tl->show; - $w->show; + $ns->{xxxw} = [$tl, $entry];#d# + +# $ws->find ("setup_notebook")->add ($ws->{tab}); +# $ws->find ("setup_dialog")->toggle_visibility; } sub csc_start { @@ -81,9 +78,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"], @@ -96,11 +93,11 @@ ) { my ($x, $label) = @$_; - $statstable->add ($x, 0, $ws->new (Label => + $statstable->add_at ($x, 0, $ws->new (Label => can_hover => 1, can_events => 1, align => +1, text => $label, tooltip => "#stat_$label", )); - $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => + $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => can_hover => 1, can_events => 1, align => +1, template => "88", tooltip => "#stat_$label", )); @@ -108,6 +105,7 @@ csc_update_stats $ns; + $ws->{tl} = $w; $w->show; } @@ -115,35 +113,21 @@ on_login => sub { my ($pl) = @_; - #return unless $cf::CFG{devel}; + return unless $cf::CFG{devel}; my $ns = $pl->ns; return unless $ns->{can_widget}; - - demo_map $ns; #csc_start $ns; + demo_start $ns; }, ); cf::register_exticmd w_e => sub { - my ($ns, $pkt) = @_; - - if (my $w = $ns->{widget}{$pkt->{id}}) { - if (my $cb = $w->{ev}{$pkt->{name}}) { - $_->($w, @{ $pkt->{args} || [] }) - for @$cb; - } - } - - () -}; - -cf::register_exticmd w_r => sub { - my ($ns, $pkt) = @_; + my ($ns, $id, @args) = @_; - if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { - $cb->(@{$pkt->{res} || [] }); + if (my $cb = $ns->{widget_cb}{$id}) { + $cb->(@args); } () @@ -157,14 +141,23 @@ my $ws = bless { id => $id, ns => $self, - w => {}, + _w => {}, }, "ext::widget::set"; - $ws->msg (ws_n => id => $id); + $ws->msg (ws_n => $id); $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; @@ -176,49 +169,115 @@ sub destroy { my ($self) = @_; - $self->msg (ws_d => id => $self->{id}); + $self->msg (ws_d => $self->{id}); delete $self->{ns}; $_->destroy for values %{ $self->{w} }; } sub msg { - my ($self, $type, %msg) = @_; + my ($self, @msg) = @_; if (my $ns = shift->{ns}) { - $msg{msgtype} = $type; - $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); + warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# + $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); } } -sub new { - my ($self, $class, %args) = @_; +sub alloc { + my ($self) = @_; - my $id = ++$self->{ns}{id}; + my $id = $self->{ns}->alloc_wid; - my $proxy = $self->{w}{$id} = bless { + my $proxy = bless { id => $id, }, "ext::widget::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 => - ws => $self->{w}{id}, - id => $id, - class => $class, - args => \%args, + $self->{id}, + $proxy->{id}, + $class, + \%args, ); $proxy } +sub template { + my ($self, $template, @args) = @_; + + my %cfg; + my @res; + + while (@args) { + my ($name, $args) = splice @args, 0, 2, (); + + 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) { + $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); + } + + $cfg{$name} = { + %$args, + id => $proxy->{id}, + }; + + push @res, $proxy; + } + + $self->msg (ws_ct => + $self->{id}, + $template, + \%cfg, + ); + + @res +} + +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; @@ -229,42 +288,77 @@ delete $self->{ns}{widget}{$self->{id}}; if (my $ws = $self->{ws}) { - delete $ws->{w}{$self->{id}}; - $self->msg (w_c => name => "destroy"); + $self->msg (w_c => 0, "destroy"); + delete $ws->{_w}{$self->{id}}; } } +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, %msg) = @_; + my ($self, $type, @arg) = @_; - if (my $ws = $self->{ws}) { - $ws->msg ($type, - %msg, - id => $self->{id}, - ); + 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)); } } sub msg_cb { - my ($self, $cb, $type, %msg) = @_; + my ($self, $cb, $type, @arg) = @_; if (my $ws = $self->{ws}) { - - my $rid = ++$ws->{ns}{id}; - - $self->msg ($type, %msg, rid => $rid); + 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; @@ -275,19 +369,19 @@ } sub set { - my ($self, $member, $value) = @_; + my ($self, @kv) = @_; - $self->msg (w_s => name => $member, value => $value); + $self->msg (w_s => \@kv); } sub get { my ($self, $member, $cb) = @_; - $self->msg_cb ($cb, w_g => name => $member); + $self->msg_cb ($cb, w_g => ref $member ? @$member : $member); } sub TO_JSON { - { __widget_ref__ => $_[0]{id} } + { "\fw" => $_[0]{id} } } our $AUTOLOAD; @@ -298,8 +392,24 @@ my $self = shift; - $self->msg (w_c => name => $AUTOLOAD, args => \@_); + #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} } +} +