package MTools::Comp::MReconizer; # 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 # ################################################################## # Le composant permet de rendre un objet zinc sensible a la reco de geste # # Parametres : # * src : objet rendu sensible et donc source des evenements # * button : bouton de la souris utilise pour genere la reco de geste # * %options : table de hash permettant la configuration initiale des proprietes # Proprietes : # * animation_duration : duree de l'animation de disparition du feedback # * color : couleur du feedback # * callback : callback appelee sur reconnaissance # Evenements : # * START_GESTURE_RECO : evenement survenant lors du demarrage d'un geste # * RECONIZED : evenement survenant lorsque un geste est reconnu use strict; use MTools; use MTools::MObjet; use Recogestures; use MTools::Anim::MOpacity; use vars qw /@ISA/; BEGIN { @ISA = qw /MTools::MObjet/; } use Tk; sub new { my ($class, $src, $button, %options) = @_; my $self = new MTools::MObjet (); bless $self, $class; $button = 1 if ! defined $button; $self -> recordEvent ('RELEASED'); $self -> recordEvent ('RECONIZED'); $self -> recordEvent ('START_GESTURE_RECO'); $self -> recordProperty ('animation_duration', 0.5); $self -> recordProperty ('color', 'blue'); $self -> recordProperty ('callback', undef); $self -> recordProperty ('-visible', 1); $self -> mconfigure (%options); binding ($src, "", [\&__pressed, $self, Ev('x'), Ev('y')]); binding ($src, "", [\&__moved, $self, Ev('x'), Ev('y')]); binding ($src, "", [\&__released, $self, Ev('x'), Ev('y')]); $self -> binding ('RECONIZED', sub { my $methode = $self -> mget ('callback'); if (defined $methode) { executer ($methode, @_); } }); $self -> {__button} = $button; $self -> {__source} = $src; $self -> {__dessin} = undef; $self -> {__dessin} = my $dessin = new MTools::GUI::MGroup (1); $self -> {__fleche} = $zinc -> add ('curve', minstance($dessin), [[0,0],[0,0]], -linecolor => 'blue', -linewidth => 2, -priority => 10, -visible => 1, -sensitive => 0, ); $self -> {__anim__disparition} = new MTools::Anim::MOpacity ( duration => 0.8, targets => $dessin, from_opacity => 100, to_opacity => 0, ); binding ($self -> {__anim__disparition}, 'ANIMATION_END', [$self, \&__clear]); plink ([$self, 'color'], [$self -> {__fleche}, '-linecolor']); plink ([$self, 'animation_duration'], [$self -> {__anim__disparition}, 'duration']); return $self; } sub __clear () { my ($self, $x, $y) = @_; $self -> {__points} = (); $zinc -> coords ($self -> {__fleche}, 0, [[0,0],[0,0]]); $self -> {__dessin} -> mconfigure ( -alpha => 100, ); } sub __pressed { my ($self, $x, $y) = @_; if ($self -> mget ('-visible')) { raise ($self -> {__dessin}); ($x, $y) = $zinc -> transform ('device', minstance ($self -> {__dessin}), [$x, $y]); if (defined $self -> {__dessin}) { push (@{$self -> {__points}}, [$x, $y]); } $self -> notify ('START_GESTURE_RECO', $x, $y); } } sub __moved { my ($self, $x, $y) = @_; if ($self -> mget ('-visible')) { ($x, $y) = $zinc -> transform('device', minstance ($self -> {__dessin}), [$x, $y]); push (@{$self -> {trace}},$x,$y); if (defined $self -> {__dessin}) { push (@{$self -> {__points}}, [$x, $y]); my @pts = @{$self -> {__points}}; $zinc -> coords ($self -> {__fleche}, 0, \@pts); } } } sub __released { my ($self, $x, $y) = @_; if ($self -> mget ('-visible')) { my ($gesture, $explanation) = AnalyzeGesture (@{$self -> {trace}}); $self -> {trace} = (); $self -> notify ('RECONIZED', $gesture, $explanation); if (defined $self -> {__dessin}) { $self -> {__anim__disparition} -> start (); } } else { $self -> notify ('RELEASED', $x, $y); $self -> __clear (); } } sub getpoints { my ($self) = @_; return $self -> {__points}; } sub stopanim { my ($self) = @_; if ($self -> {__anim__disparition}) { $self -> {__anim__disparition}->stop(); } } sub reinitBindings { my ($self) = @_; my $button = $self->{__button}; binding ($self->{__source}, "", [\&__pressed, $self, Ev('x'), Ev('y')]); binding ($self->{__source}, "", [\&__moved, $self, Ev('x'), Ev('y')]); binding ($self->{__source}, "", [\&__released, $self, Ev('x'), Ev('y')]); } 1;