aboutsummaryrefslogtreecommitdiff
path: root/exemples/exemple_MMover_MInertie.pl
blob: fd1f4f1b16ade1aae6ff6752f70f60f798074d00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the GNU GPL General Public License
#	as published by the Free Software Foundation; either version 2
#	of the License, or (at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA,
#	or refer to http://www.gnu.org/copyleft/gpl.html
#

BEGIN {
	unshift @INC, ".", "./data", "../src"; 
}

use MTools;
use MTools::Comp::MMover;
use MTools::GUI::MRect;
use MTools::Comp::MInertie;

# Creation de la frame
new MTools (800, 600, 'Exemple MMover et MInertie');

# Creation des objets delimitant l'espace
new MTools::GUI::MRect (1, 0, 0, 650, 450, -fillcolor => "=axial 90 | #526266 | #667C80", -filled => 1);
new MTools::GUI::MRect (1, 250, 300, 400, 150, -linecolor => 'black');

# Creation de l'objet deplace
my $target = minstanciate ('Ampoule.svg#on', 1);

# Ajout du comportement MMover à l'objet
my $mover = new MTools::Comp::MMover($target, $target, 1, x_max => 600, y_max => 400, y_min => 0, x_min => 0, allower => \&allowmove);
# Ajout du comportement MInertie au comportement de mover
new MTools::Comp::MInertie ($mover);


# RUN
mrun;

# La fonction permet de conserver l'objet dans un espace controle
# Elle recoit en parametre l'ancienne position de l'objet ainsi que le deplacement suggerer
# Elle retourne un vecteur correspondant a la correction effectuee sur le deplacement
sub allowmove {
	my ($x, $y, $dx, $dy) = @_;
	if ($x > 200)
	{
		if ($y + $dy > 250)
		{
			$dy = $y + $dy - 250;
			return (0, $dy);
		}
	}
	elsif ($x + $dx > 200)
	{
		if ($y + $dy > 250)
		{
			$dx = $x + $dx - 200;
			return ($dx, 0);
		}
	}
	return (0, 0);
}