aboutsummaryrefslogtreecommitdiff
path: root/src/MTools/Anim/MOpacity.pm
blob: 19c21082de2e24744c6822ce5adace929b9ae485 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package MTools::Anim::MOpacity;
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the GNU LGPL Libray 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 Library General Public License for more details.
#
#	You should have received a copy of the GNU Library 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/lgpl.html
#
##################################################################

# MOpacity permet de realiser une animation sur la transparence d'un objet
# 
# proprietes :
#	* -visible : permet d'activer ou stopper l'animation
#	* from_opacity : valeur initiale de l'opacite au depart de l'animation
#	* to_opacity : valeur de l'opacite a la fin de l'animation
# 	* duration : duree de l'animation
#	* loop : marque le caractere repetitif ou non de l'animation
#	* targets : objet ou tableau d'objets cible de cette animation (de type MGroup ou autre type avec un attribut -alpha.
# Evenements :
#	* ANIMATION_END : Notifie lorque l'annimation se termine
#	* OPACITY_CHANGED : Notifie lorsque la valeur de l'opacite est changee au cours de l'animation
#	* ANIMATION_ABORD : Notifie lorsque l'animation est stoppee avant la fin
# Fonctions : 
#	* start : demarre l'animation
#	* stop: arrete l'animation
#	* isRunning : test si l'animation est en cours

use strict;
use MTools;
use MTools::MObjet;

use Anim;
use Anim::Pacing::Linear;
use Anim::Path::Rectilinear;

use vars qw /@ISA/;

BEGIN 
{
	@ISA = qw /MTools::MObjet/;
}

use Tk;

sub new {
	my ($class, %options) = @_;
	my $self = new MTools::MObjet ();
	bless $self, $class;

	$self -> recordProperty ('-visible', 0);
	$self -> recordProperty ('from_opacity', 0);
	$self -> recordProperty ('to_opacity', 0);
	$self -> recordProperty ('duration', 1);
	$self -> recordProperty ('loop', 0);
	$self -> recordProperty ('targets', undef);
	$self -> recordEvent ('ANIMATION_END');
	$self -> recordEvent ('OPACITY_CHANGED');
	$self -> recordEvent ('ANIMATION_ABORD');
	$self -> plisten ('-visible', sub { 
		my ($src, $key, $val) = @_;
		if ($val == 0)
		{
			$self -> stop ();
		}
		else
		{
			$self -> start ();
		}
	});
	$self -> {__animation} = undef;
	$self -> mconfigure (%options);
	return $self;
}

sub start {
	my ($self) = @_;
	my $pacing = new Anim::Pacing::Linear (-duration => $self -> mget ('duration'));
	$self -> {__opacitydep} = my $opacitydep = $self -> mget ('from_opacity');

	my $animationpath = new Anim::Path::Rectilinear (
		-xdep => 0,
		-ydep => 0,
		-xdest => $self -> mget ('to_opacity') - $opacitydep,
		-ydest => 0,
	);
	$self -> {__opacity} = $opacitydep;
	$self -> {__animation} = my $animation = new Anim (
		-pacing => $pacing,
		-resources => [
			$animationpath,
			-command => sub { $self -> __event (@_)},
			-endcommand => sub {$self -> {__animation} = undef;
					    $self -> notify ('ANIMATION_END');},
		],
		-stopcommand => sub {$self -> {__animation} = undef;
				     $self -> notify ('ANIMATION_ABORD', $self -> {__opacity});},
		-loop => $self -> mget ('loop'),
	);
	$animation -> start ();
}

sub stop {
	my ($self) = @_;
	if (defined $self -> {__animation})
	{
		$self -> {__animation} -> stop ();
	}
}

sub __event {
	my ($self, $opacity) = @_;
	$opacity += $self -> {__opacitydep};
	my @targets = ();
	my $target = $self -> mget ('targets');
	if (ref ($target) eq 'ARRAY')
	{
		@targets = @{$target};
	}
	else
	{
		push (@targets, $target);
	}
	for (my $i = 0; $i < @targets; $i++)
	{
		if (ref($targets [$i]) eq '')
		{
			mconfigure ($targets[$i], '-alpha' => $opacity);
		}
		else
		{
			$targets [$i] -> mconfigure ('-alpha' => $opacity);
		}
	}
	$self -> {__opacity} = $opacity;
	$self -> notify ('OPACITY_CHANGED', $opacity);
}

sub isRunning {
	my ($self) = @_;
	return defined $self -> {__animation};
}

1;