summaryrefslogtreecommitdiff
path: root/sphinxdoc/gesture.rst
blob: ddac29bcb81aefda737ae58a9a748d4e87644cfd (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
Gesture Support
===============

The namespace libavg.gesture exposes a group of configurable gesture recognizers. 

.. automodule:: libavg.gesture
    :no-members:

    .. inheritance-diagram:: DragRecognizer SwipeRecognizer TapRecognizer TransformRecognizer DoubletapRecognizer HoldRecognizer
        :parts: 1
    
    .. inheritance-diagram:: Transform
        :parts: 1


    .. autoclass:: DoubletapRecognizer(node, [maxTime=MAX_DOUBLETAP_TIME, maxDist=MAX_TAP_DIST, initialEvent=None, possibleHandler=None, failHandler=None, detectedHandler=None])

        A :py:class:`DoubletapRecognizer` detects doubletaps: Two short touches in quick
        succession without a large change of the cursor position.

        :param maxTime: The maximum time that each phase of the tap may take.

        :param maxDist: The maximum distance the contact may move in millimeters.


    .. autoclass:: DragRecognizer(eventNode, [coordSysNode=None, initialEvent=None, direction=ANY_DIRECTION, directionTolerance=DIRECTION_TOLERANCE, friction=-1, minDragDist=None, possibleHandler=None, failHandler=None, detectedHandler=None, moveHandler=None, upHandler=None, endHandler=None])

        A :py:class:`DragRecognizer` attaches itself to a node's cursor events and 
        delivers higher-level callbacks that can be used to implement dragging or 
        drag-like functionality.

        :py:class:`DragRecognizer` supports inertia after the node is released.
        
        :param avg.Node coordSysNode:    

            Used to determine the coordinate system for the offsets returned by the 
            callbacks. If :py:attr:`coordSysNode` is not given, :py:attr:`eventNode` is 
            used as default. The :py:class:`DragRecognizer` never modifies any nodes 
            itself. :py:attr:`coordSysNode` can be used to separate the node that
            is the 'handle' for the events from the node that is being moved - for 
            instance, to allow moving a window by dragging the title bar.

        :param direction:

            Can be used to constrain the recognizer to :py:const:`VERTICAL` or 
            :py:const:`HORIZONTAL` drags only. If one of these constants is passed as 
            :py:attr:`direction`, the recognizer invokes :py:meth:`onPossible`
            when the down event arrives, then determines whether the drag is a 
            predominantly horizontal or vertical drag and invokes either 
            :py:meth:`onDetected` or :py:meth:`onFail` depending on the result.

        :param float directionTolerance:

            A tolerance angle in radians for the detection of horizontal and vertical
            drags.

        :param avg.Node eventNode: 
        
            The node to attach to. The :py:class:`DragRecognizer` registers an event 
            handler to react to any contacts for this node. 
            
        :param float friction:

            If set, this parameter enables inertia processing. It describes how 
            quickly the drag comes to a stop after the cursor is released.

        :param float minDragDist:

            Minimum distance in mm that the cursor must move for the recognizer to switch
            from :py:const:`POSSIBLE` to :py:const:`DETECTED`. Default is either 0 (for 
            :py:const:`ANY_DIRECTION` recognizers) or :py:const:`MIN_DRAG_DIST` (for
            constrained recognizers).

        :param moveHandler:

            A shortcut for 
            :samp:`Recognizer.subscribe(Recognizer.MOTION, moveHandler)`.

        :param upHandler:

            A shortcut for 
            :samp:`Recognizer.subscribe(Recognizer.UP, upHandler)`.

        **Messages:**

            To get these messages, call :py:meth:`Publisher.subscribe`.

            .. py:method:: Recognizer.MOTION(offset)

                Emitted when the drag should cause a position change. This usually happens
                in response to a :py:const:`CURSORMOTION` event, but may also happen
                because of inertia.

                :param avg.Point2D offset: 
                
                    The current offset from the start of the drag in coordinates relative
                    to the :py:attr:`coordSysNode`'s parent.

            .. py:method:: Recognizer.UP(offset)

                Emitted when the cursor is released. If inertia is enabled, there may be 
                move events after the up event.

                :param avg.Point2D offset: 
                
                    The current offset from the start of the drag in coordinates relative
                    to the :py:class:`coordSysNode`'s parent.

        .. py:method:: abort()

            Aborts the present recognized gesture and sliding caused by inertia


    .. autoclass:: HoldRecognizer(node, [delay=HOLD_DELAY, maxDist=MAX_TAP_DIST, initialEvent=None, possibleHandler=None, failHandler=None, detectedHandler=None, stopHandler=None])

        A :py:class:`HoldRecognizer` detects if a touch is held for a certain amount of 
        time. Holds are continuous events: the :py:meth:`stopHandler` is called when the
        contact up event arrives.

        :param delay: The amount of time that has to pass before the hold is recognized.

        :param maxDist: The maximum distance the contact may move in millimeters.


    .. autoclass:: Recognizer(node, isContinuous, maxContacts, initialEvent[, possibleHandler=None, failHandler=None, detectedHandler=None, endHandler=None])

        Base class for gesture recognizers that attach to a node's cursor events and 
        emit higher-level events. Gesture recognizers have a standard set of states and
        callbacks, but derived classes may add their own callbacks and do not need to
        invoke all base class callbacks. The possible states vary depending on the value 
        of :py:attr:`isContinuous`:

        .. image:: Recognizer.png

        A usage example for the recognizers can be found under
        :samp:`src/samples/gestures.py`. Many of the recognizers have default timeouts 
        and distance limits which can be changed by modifying :file:`avgrc`. The sample
        file under :file:`src/avgrc` contains explanations.

        :param Node node: Node to attach to.

        :param bool isContinuous: 
            
            :py:const:`True` if the gesture stays active after it has been detected.
        

        :param maxContacts:

            The maximum number of contacts that the recognizer should handle. 
            :py:const:`None` if there is no maximum.

        :param initialEvent:

            A cursordown event to pass to the recognizer immediately.
            
        :param possibleHandler:

            A shortcut for 
            :samp:`Recognizer.subscribe(Recognizer.POSSIBLE, possibleHandler)`.

        :param failHandler:

            A shortcut for :samp:`Recognizer.subscribe(Recognizer.FAIL, failHandler)`.

        :param detectedHandler:

            A shortcut for 
            :samp:`Recognizer.subscribe(Recognizer.DETECTED, detectedHandler)`.

        :param endHandler:

            A shortcut for :samp:`Recognizer.subscribe(Recognizer.END, endHandler)`.

        **Messages:**

            Gesture recognizers emit messages whenever they change state - see the state
            diagrams above. The messages have a parameter of type :py:class:`CursorEvent`.

            To get these messages, call :py:meth:`Publisher.subscribe`.

            .. py:method:: POSSIBLE()

                Emit when gesture recognition begins - usually after a cursordown event.
                Some continuous gestures (such as unconstrained drags) never emit 
                :py:meth:`POSSIBLE` but emit :py:meth:`DETECTED` immediately.

            .. py:method:: FAILED() 

                Emitted when gesture recognition is rejected. For instance, in the case 
                of a :py:class:`DoubleTapRecognizer`, a :py:meth:`FAILED` message is
                emitted if the touch stays on the surface for too long.

            .. py:method:: DETECTED()

                Emitted when the gesture is recognized. For discrete gestures, this 
                signifies the end of gesture processing. 

            .. py:method:: END()

                Emitted when a continuous gesture ends. This is often a result of an
                up event, but e.g. in the case of inertia, :py:meth:`END` is emitted
                when movement stops.

        .. py:attribute:: contacts

            List of all contacts detected by the :py:class:`Recognizer`.

        .. py:method:: abort()

            Aborts the present recognized gesture.

        .. py:method:: enable(isEnabled)

            Enables or disables the :py:class:`Recognizer`.

        .. py:method:: getState() -> String

            Returns the state ("IDLE", "POSSIBLE" or "RUNNING") of the recognizer.


    .. autoclass:: SwipeRecognizer(node, direction, [numContacts=1, directionTolerance=SWIPE_DIRECTION_TOLERANCE, minDist=MIN_SWIPE_DIST, maxContactDist=MAX_SWIPE_CONTACT_DIST, initialEvent=None, possibleHandler=None, failHandler=None, detectedHandler=None])

        A :py:class:`SwipeRecognizer` detects movement of one or more contacts in a
        specified direction and with a minimal distance. Whether the gesture is recognized
        is determined when an up event occurs.

        :param direction: 
        
            One of :py:const:`SwipeRecognizer.UP`, :py:const:`DOWN`, :py:const:`LEFT` or 
            :py:const:`RIGHT`.

        :param numContacts: The minimum number of contacts for the swipe.

        :param directionTolerance: 
        
            Maximum deviation from the ideal direction that the touch(es) may have in
            radians.

        :param minDist: 
        
            Minimum distance between start position and end position of each contact in 
            millimeters.

        :param maxInterContactDist:

            Maximum distance between the start positions of the different contacts.


    .. autoclass:: TapRecognizer(node, [maxTime=MAX_TAP_TIME, maxDist=MAX_TAP_DIST, initialEvent=None, possibleHandler=None, failHandler=None, detectedHandler=None])

        A :py:class:`TapRecognizer` detects short touches without a large change of the 
        cursor position.

        :param maxTime: The maximum time that the tap may take in milliseconds.

        :param maxDist: The maximum distance the contact may move in millimeters.


    .. autoclass:: Transform(trans, [rot=0, scale=1, pivot=(0,0)])

        Encapsulates a coordinate transformation and can be used to change the position,
        rotation and scale of a node.

        .. py:attribute:: pivot

            The point around which rot and scale are applied.

        .. py:attribute:: rot

            Rotation in radians.

        .. py:attribute:: scale

            Multiplies the size of the node.

        .. py:attribute:: trans

            The translation.

        .. py:method:: moveNode(node)

            Changes a :py:attr:`node`'s pos, angle and size by applying the transform.


    .. autoclass:: TransformRecognizer(eventNode, [coordSysNode=None, initialEvent=None, friction=-1, detectedHandler=None, moveHandler=None, upHandler=None, endHandler=None])

        A :py:class:`TransformRecognizer` is used to support drag/zoom/rotate 
        functionality. From any number of touches on a node, it calculates an aggregate
        transform that can be used to change the position, size and angle of a node.
        The class supports intertia after the node is released.

        :param avg.Node eventNode: 
        
            The node to attach to. The :py:class:`TransformRecognizer` registers an event
            handler to react to any contacts for this node. 
            
        :param avg.Node coordSysNode: 

            Used to determine the coordinate system for the transforms returned by the 
            callbacks.  If :py:attr:`coordSysNode` is not given, :py:attr:`eventNode` is 
            used as default. The :py:class:`TransformRecognizer` never modifies any nodes 
            itself. :py:attr:`coordSysNode` can be used to separate the node that
            is the 'handle' for the events from the node that is being moved - for 
            instance, to allow moving and rotating a window by dragging the title bar.

        :param float friction:

            If set, this parameter enables inertia processing. It describes how 
            quickly the transform comes to a stop after the cursor is released.

        :param moveHandler:

            A shortcut for 
            :samp:`Recognizer.subscribe(Recognizer.MOTION, moveHandler)`.

        :param upHandler:

            A shortcut for 
            :samp:`Recognizer.subscribe(Recognizer.UP, upHandler)`.

        **Messages:**

            To get these messages, call :py:meth:`Publisher.subscribe`.

            .. py:method:: Recognizer.MOTION(transform)

                Emitted whenever the transform changes. This usually happens
                in response to one or more :py:const:`CURSORMOTION` events, but may also
                happen because of inertia.

                :param Transform transform:
                
                    The change in transformation since the last call of move or up.

            .. py:method:: Recognizer.UP(transform)

                Called when the last touch is released. If inertia is enabled, there may
                be move events after the up event.

                :param Transform transform:
                
                    The change in transformation since the last call of move.

        .. py:method:: abort()

            Aborts the present recognized gesture and sliding caused by inertia.