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
|
package MTools::MState;
# 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
#
##################################################################
use strict;
use MTools;
use MTools::MObjet;
use MTools::SVG::SVGLoader;
use vars qw /@ISA/;
BEGIN
{
@ISA = qw /MTools::MObjet/;
}
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
$self -> {instance} = -1;
my %events = %{$options {events}};
$self -> {__transitions} = $options {transitions};
my %transitions = %{$self -> {__transitions}};
$self -> recordProperty ('state', $options {current_state});
$self -> recordEvent('SET_STATE');
# Cr?er les bind soit aupr?s de zinc, soit au niveau des objets MObjet
while ( my ($evt, $desc) = each (%events) )
{
my ($src, $spec) = @{$desc};
if ($spec =~ /<(.*)>/)
{
binding ($src, $spec, sub {$self -> evt ($evt, @_); });
}
else
{
$src -> binding ($spec, sub {$self -> evt ($evt, @_); });
}
}
# Abonne les switchs ecoutant les changements d'etat
my $sws = $options {switchs};
if (defined $sws)
{
my @abonnes;
if (ref ($sws) eq 'ARRAY')
{
@abonnes = @{$sws};
}
else
{
$abonnes [0] = $sws;
}
for (my $i = 0; $i < @abonnes; $i++)
{
$self -> binding ('SET_STATE', [$abonnes [$i], 'setState']);
}
$self -> notify ('SET_STATE', $self -> mget ('state'));
}
# Enregistre les outputs
while ( my ($src, $trans) = each (%transitions) )
{
my %trs = %{$trans};
while ( my ($evt, $desc) = each (%trs) )
{
if (defined $desc -> {notify})
{
my @evts;
if (ref ($desc -> {notify}) eq 'ARRAY')
{
@evts = @{$desc -> {notify}};
}
else
{
$evts [0] = $desc -> {notify};
}
for (my $i = 0; $i < @evts; $i++)
{
$self -> recordEvent($evts [$i]);
}
}
}
}
return $self;
}
sub evt {
my ($self, $evt, @args) = @_;
if (defined $self -> {__transitions} -> {$self -> mget ('state')} -> {$evt})
{
my $trans = $self -> {__transitions} -> {$self -> mget ('state')} -> {$evt};
if( defined $trans -> {do_before} )
{
executer ($trans -> {do_before}, @args);
}
if( defined $trans -> {to} )
{
$self -> mconfigure ('state', $self -> {__transitions} -> {$self -> mget ('state')} -> {$evt} -> {to});
$self -> notify ('SET_STATE', $self -> mget ('state'));
}
if( defined $trans -> {do_after} )
{
executer ($trans -> {do_after}, @args);
}
if( defined $trans -> {notify} )
{
my @evts;
if (ref ($trans -> {notify}) eq 'ARRAY')
{
@evts = @{$trans -> {notify}};
}
else
{
$evts [0] = $trans -> {notify};
}
for (my $i = 0; $i < @evts; $i++)
{
$self -> notify ($evts [$i], @args);
}
}
}
}
sub setState {
print "################## MTools::MState TODO\n";
}
1;
|