Re: Scribble Example



On Wed, 09 Jul 2008 15:01:30 +0200
Mario Kemper <mario kemper googlemail com> wrote:

I am playing around with muppet's scribble example and it's working like
a charm but if i move the mouse cursor very fast there are only single
rectangles drawn. 
It seems to me that the needed events (motion_notify) are not passed
fast enough. Is there any way to prevent this or a workaround? 

Greetings
Mario

Draw lines instead of the rectangle. You start with a 0 line, and as
you move the mouse, push the new x,y coords onto the line. It
will appear smooth.

I have a crude example on a Gnome2::Canvas, you can probably adapt
it to the Drawing area, and fix the glitches. It draws a continous line, starting at 0,0.
You probably want to initiate a new line at the first button click position, and
make a new line on each button click.  Maybe instead of a single line,
have multiple lines in %lines.

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

my $draw_flag = 0;

my $window   = Gtk2::Window->new;
$window->signal_connect( destroy => sub { exit } );
$window->set_default_size( 500, 500 );

my $vbox = Gtk2::VBox->new;
$vbox->set_border_width(4);
$vbox->show;

my $hbox = Gtk2::HBox->new(FALSE, 4);
$vbox->pack_start($hbox, FALSE, FALSE, 0);
$hbox->show;

$window->add($vbox);

my $scroller = Gtk2::ScrolledWindow->new;
my $canvas   = Gnome2::Canvas->new();
my $white = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xFFFF);
$canvas->modify_bg('normal',$white);

$scroller->add( $canvas );
$vbox->pack_start($scroller, 1, 1, 0); 

$canvas->set_scroll_region( 0, 0, 700, 700 );
$window->show_all;

my $root = $canvas->root;

my $text = Gnome2::Canvas::Item->new(
    $root, 'Gnome2::Canvas::Text',
   x          => 20,
   y          => 15,
   fill_color => 'black',
   font       => 'Sans 14',
   anchor     => 'GTK_ANCHOR_NW',
   text       => 'Click to start drawing, release to stop'
);


$canvas->signal_connect (event => \&event_handler);

my $points = [0,0,0,0];  #need at least 2 points to start

my $line2= Gnome2::Canvas::Item->new ($root,
                'Gnome2::Canvas::Line',
                points => $points,
                fill_color => "red",
                width_units => 3.0,
                cap_style => 'projecting',
                join_style => 'miter',
                );

my $p = $line2->get('points');
print "@$p\n";
my ($x,$y) = @$p;


# Zoom
my $z = Gtk2::Label->new("Zoom:");
$hbox->pack_start($z, FALSE, FALSE, 0);
$z->show;

my $adj = Gtk2::Adjustment->new(1, 0.05, 100, 0.05, 0.5, 0.5);
my $sb = Gtk2::SpinButton->new($adj, 0, 2);
$adj->signal_connect("value-changed", \&zoom_changed, $canvas);
$sb->set_size_request(60, -1);
$hbox->pack_start($sb, FALSE, FALSE, 10);
$sb->show;

    
# Create PDF                                                                          
my $bpdf = Gtk2::Button->new_with_label('screenshot');                                       
$hbox->pack_start($bpdf, FALSE, FALSE, 0);                                               
$bpdf->show;                                                                             
$bpdf->signal_connect("clicked", \&write_screenshot, $canvas);         

$window->show_all();

Gtk2->main;

##############################

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

    if ( $event->type eq "button-press" ) {
        $draw_flag = 1;
    }

    if ( $event->type eq "button-release" ) {
        $draw_flag = 0;
    }



    if ( $event->type eq "focus-change" ) {
        return 0;
    }
    
    if ( $event->type eq "expose" ) {
        return 0;
    }
    
    
  if($draw_flag){    
    #left with motion-notify
    if ( $event->type eq "motion-notify"){
    my ($x,$y) = ($event->x,$event->y);
    print "$x  $y\n";


     my $scale = $adj->get_value;
     print "scale->$scale\n"; 
     
     my $scaled_x = $scale * $x;
     my $scaled_y = $scale * $y;
     print 'scaled  ',$scaled_x,'  ',$scaled_y,"\n";
      
     push @$points,$x/$scale,$y/$scale;
     $line2->set(points=>$points);

        my $p = $line2->get('points');
        print "@$p\n";
    }
  }        
}


sub zoom_changed {
    my ($adj, $canvas) = @_;
    $canvas->set_pixels_per_unit($adj->get_value);
}


__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]