File indexing completed on 2025-01-05 04:35:06

0001 /*
0002  * This file is part of the mouse gesture package.
0003  * Copyright (C) 2006 Johan Thelin <e8johan@gmail.com>
0004  * All rights reserved.
0005  *
0006  * Redistribution and use in source and binary forms, with or
0007  * without modification, are permitted provided that the
0008  * following conditions are met:
0009  *
0010  *   - Redistributions of source code must retain the above
0011  *     copyright notice, this list of conditions and the
0012  *     following disclaimer.
0013  *   - Redistributions in binary form must reproduce the
0014  *     above copyright notice, this list of conditions and
0015  *     the following disclaimer in the documentation and/or
0016  *     other materials provided with the distribution.
0017  *   - The names of its contributors may be used to endorse
0018  *     or promote products derived from this software without
0019  *     specific prior written permission.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
0022  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
0023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
0024  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0025  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0026  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0027  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0028  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
0029  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
0030  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0031  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0032  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0033  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  *
0036  */
0037 
0038 #ifndef MOUSEGESTURERECOGNIZER_H
0039 #define MOUSEGESTURERECOGNIZER_H
0040 
0041 #include <list>
0042 #include <vector>
0043 
0044 namespace Gesture
0045 {
0046 
0047 /*
0048  *  Sub-class and implement callback method and register along a gesture description.
0049  */
0050 class MouseGestureCallback
0051 {
0052 public:
0053     virtual void callback() = 0;
0054     virtual ~MouseGestureCallback() { }
0055 };
0056 
0057 /*
0058  *  The available directions.
0059  */
0060 typedef enum { Up, Down, Left, Right, AnyHorizontal, AnyVertical, UpLeft, UpRight, DownLeft, DownRight, NoMatch } Direction;
0061 /*
0062  *  A list of directions.
0063  */
0064 typedef std::list<Direction> DirectionList;
0065 
0066 /*
0067  *  Create lists of directions and connect to a callback.
0068  */
0069 struct GestureDefinition {
0070     GestureDefinition(const DirectionList &d, MouseGestureCallback* c) : directions(d), callbackClass(c) {}
0071 
0072     DirectionList directions;
0073     MouseGestureCallback* callbackClass;
0074 };
0075 
0076 /*
0077  *  Data types for internal use
0078  */
0079 struct Pos {
0080     Pos(int ix, int iy) : x(ix), y(iy) {}
0081 
0082     int x, y;
0083 };
0084 
0085 typedef std::vector<Pos> PosList;
0086 typedef std::vector<GestureDefinition> GestureList;
0087 
0088 class MouseGestureRecognizer
0089 {
0090 public:
0091     MouseGestureRecognizer(int minimumMovement = 5, double minimumMatch = 0.9, bool allowDiagonals = false);
0092     ~MouseGestureRecognizer();
0093 
0094     void addGestureDefinition(const GestureDefinition &gesture);
0095     void clearGestureDefinitions();
0096 
0097     void startGesture(int x, int y);
0098     void addPoint(int x, int y);
0099     bool endGesture(int x, int y);
0100     void abortGesture();
0101     PosList currentPath() const;
0102 private:
0103     bool recognizeGesture();
0104 
0105     static PosList limitDirections(const PosList &positions, bool allowDiagnonals);
0106     static PosList simplify(const PosList &positions);
0107     static PosList removeShortest(const PosList &positions);
0108     static int calcLength(const PosList &positions);
0109 
0110     struct Private;
0111     Private* d;
0112 };
0113 
0114 };
0115 
0116 #endif // MOUSEGESTURERECOGNIZER_H
0117