Bonjour,
Je cherche un exemple de code permettant d'afficher un ou deux objet 2D et de les deplacer à la souris.
L'ideal serai en C ou C++, mais n'importe quel langage ferai tres bien l'affaire.
Le but etant d'avoir un code d'exemple minimal.
Merci d'avance !
#!perl
use strict;
use Tk;
my $mw=MainWindow->new();
my $can=$mw->Scrolled('Canvas',-background=>'darkgreen',-scrollregion=>[-100,-100,1024,768])->pack(-expand=>1,-fill=>'both');
my ($x,$y);
#affiche 20 ovals bleu
for (1..20) {
my ($cx,$cy)=(int(rand()*1000-75),int(rand()*600-75));
$can->createOval($cx,$cy,$cx+int(rand()*40+12),$cy+int(rand()*40+12),-fill=>'blue',-tag=>["oval","blue"]);
}
$can->bind('all','<ButtonPress-1>',\&lock); # all : tag universel ("s'agoute" a oval et blue)
$can->bind('all','<ButtonRelease-1>',\&free);
#$can->bind('all','<Motion>',\&mvt);
MainLoop;
sub mvt {
my ($dx,$dy)=($Tk::event->x, $Tk::event->y);
$can->move('current',$dx-$x,$dy-$y); # current : tag implicite de l'objet pointé par la souris
($x,$y)=($dx,$dy);
}
sub lock {
$can->bind('current','<Motion>',\&mvt);
($x,$y)=($Tk::event->x, $Tk::event->y);
}
sub free {
$can->bind('current','<Motion>',undef);
}
__END__