Re: Goo::Canvas updating a PolyLine



On Tue, 3 Jun 2008 14:24:51 -0400
zentara <zentara1 sbcglobal net> wrote:

Hi, I'm trying to convert a line-segment (waypoint) generator
that I made on a plain Gnome2::Canvas, to the Goo::Canvas.

I can't seem to find the right method to update a line, and I'm
wondering if it's possible?  The following won't allow some new
points to be added to a line, but I can undef the line and rebuild it.
That is not very efficient.

I tried using $line->set(points=>), but it seems that the list of coordinate pairs
are an anonymous array_ref......  there dosn't seem to be a way of addressing
them.  

Anyone know how to update points in a PolyLine?

See attachment.

Thanks,

zentara

Well, I found a solution, and it involves a bit of juggling.

First, you need to make a Goo::Canvas::Points object to hold
the points, THEN you need to make the PolyLine with undef for
the points, and set the points after line creation.... this gives the
points array_ref the named property  'points'.

Here is working code, with comments describing the solution.

#!/usr/bin/perl -w
use strict;
use warnings;
use Goo::Canvas;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);

my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(640, 600);

my $swin = Gtk2::ScrolledWindow->new;
$swin->set_shadow_type('in');
$window->add($swin);

my $canvas = Goo::Canvas->new();
$canvas->set_size_request(600, 450);
$canvas->set_bounds(0, 0, 1000, 1000);
$swin->add($canvas);

my $root = $canvas->get_root_item();

$canvas->signal_connect('button-press-event',
                      \&on_can_button_press);

my $way_ref = [ 340, 170,
          340, 230,
          390, 230,
          390, 170 ];

push @$way_ref,200,300; # shows pushing works

#my $points = Goo::Canvas::Points->new([100, 100, 200, 200]);
# instead of specifying anonymous array_ref in the PolyLine,
# we create a Points object
my $points = Goo::Canvas::Points->new($way_ref);

my $line = Goo::Canvas::Polyline->new(
        $root, FALSE,
         undef,      # points need to be set after creation
        'stroke-color' => 'midnightblue',
        'line-width' => 3,
        'start-arrow' => TRUE,
        'end-arrow' => TRUE,
        'arrow-tip-length' => 3,
        'arrow-length' => 4,
        'arrow-width' => 3.5
    );

# setting after line creation, sets the 'points' property by name
$line->set(points => $points);

$window->show_all();
Gtk2->main;

sub on_can_button_press {
     my ( $widget, $event ) = @_;
#     print $widget ,' ',$event->type,"\n";

      my ($x,$y) = ($event->x,$event->y);
      print "$x  $y\n";
      push @$way_ref,$x,$y;
      
      # won't work
      #$points->set(points => $way_ref); # Points can't be changed
      
      # need to create a new Points object
      my $points = Goo::Canvas::Points->new($way_ref);
    
      # now works since 'points' was added as a property above    
      $line->set(points => $points);

    return TRUE;
}

__END__


zentara

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/CandyGram_for_Mongo.html 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]