Skip to content

Commit 6bb8fe8

Browse files
authored
Merge pull request kivy#7785 from pythonic64/feature-assign_type_id_to_input_providers
Input providers: Assign type_id to MotionEvent subclasses
2 parents 542a813 + dcbf0c1 commit 6bb8fe8

File tree

14 files changed

+94
-101
lines changed

14 files changed

+94
-101
lines changed

kivy/core/window/window_sdl2.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,16 @@
105105

106106

107107
class SDL2MotionEvent(MotionEvent):
108+
109+
def __init__(self, *args, **kwargs):
110+
kwargs.setdefault('is_touch', True)
111+
kwargs.setdefault('type_id', 'touch')
112+
super().__init__(*args, **kwargs)
113+
self.profile = ('pos', 'pressure')
114+
108115
def depack(self, args):
109-
self.is_touch = True
110-
self.profile = ('pos', 'pressure', )
111116
self.sx, self.sy, self.pressure = args
112-
win = EventLoop.window
113-
super(SDL2MotionEvent, self).depack(args)
117+
super().depack(args)
114118

115119

116120
class SDL2MotionEventProvider(MotionEventProvider):

kivy/input/providers/androidjoystick.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@
3030

3131
class AndroidMotionEvent(MotionEvent):
3232

33-
def depack(self, args):
34-
self.is_touch = True
33+
def __init__(self, *args, **kwargs):
34+
kwargs.setdefault('is_touch', True)
35+
kwargs.setdefault('type_id', 'touch')
36+
super().__init__(*args, **kwargs)
3537
self.profile = ['pos', 'pressure', 'shape']
38+
39+
def depack(self, args):
3640
self.sx, self.sy, self.pressure, radius = args
3741
self.shape = ShapeRect()
3842
self.shape.width = radius
3943
self.shape.height = radius
40-
super(AndroidMotionEvent, self).depack(args)
44+
super().depack(args)
4145

4246

4347
class AndroidMotionEventProvider(MotionEventProvider):

kivy/input/providers/hidinput.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@
5959

6060
class HIDMotionEvent(MotionEvent):
6161

62+
def __init__(self, *args, **kwargs):
63+
kwargs.setdefault('is_touch', True)
64+
kwargs.setdefault('type_id', 'touch')
65+
super().__init__(*args, **kwargs)
66+
6267
def depack(self, args):
63-
self.is_touch = True
6468
self.sx = args['x']
6569
self.sy = args['y']
6670
self.profile = ['pos']
@@ -75,8 +79,7 @@ def depack(self, args):
7579
if 'button' in args:
7680
self.button = args['button']
7781
self.profile.append('button')
78-
79-
super(HIDMotionEvent, self).depack(args)
82+
super().depack(args)
8083

8184
def __str__(self):
8285
return '<HIDMotionEvent id=%d pos=(%f, %f) device=%s>' \

kivy/input/providers/leapfinger.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@ def normalize(value, a, b):
2222

2323
class LeapFingerEvent(MotionEvent):
2424

25+
def __init__(self, *args, **kwargs):
26+
kwargs.setdefault('is_touch', True)
27+
kwargs.setdefault('type_id', 'touch')
28+
super().__init__(*args, **kwargs)
29+
self.profile = ('pos', 'pos3d',)
30+
2531
def depack(self, args):
26-
super(LeapFingerEvent, self).depack(args)
32+
super().depack(args)
2733
if args[0] is None:
2834
return
29-
self.profile = ('pos', 'pos3d', )
3035
x, y, z = args
3136
self.sx = normalize(x, -150, 150)
3237
self.sy = normalize(y, 40, 460)
3338
self.sz = normalize(z, -350, 350)
3439
self.z = z
35-
self.is_touch = True
3640

3741

3842
class LeapFingerEventProvider(MotionEventProvider):

kivy/input/providers/linuxwacom.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@
3434

3535
class LinuxWacomMotionEvent(MotionEvent):
3636

37+
def __init__(self, *args, **kwargs):
38+
kwargs.setdefault('is_touch', True)
39+
kwargs.setdefault('type_id', 'touch')
40+
super().__init__(*args, **kwargs)
41+
3742
def depack(self, args):
38-
self.is_touch = True
3943
self.sx = args['x']
4044
self.sy = args['y']
4145
self.profile = ['pos']
@@ -47,7 +51,7 @@ def depack(self, args):
4751
if 'pressure' in args:
4852
self.pressure = args['pressure']
4953
self.profile.append('pressure')
50-
super(LinuxWacomMotionEvent, self).depack(args)
54+
super().depack(args)
5155

5256
def __str__(self):
5357
return '<LinuxWacomMotionEvent id=%d pos=(%f, %f) device=%s>' \

kivy/input/providers/mactouch.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,18 @@ class MacMotionEvent(MotionEvent):
9393
and shape profiles.
9494
'''
9595

96+
def __init__(self, *args, **kwargs):
97+
kwargs.setdefault('is_touch', True)
98+
kwargs.setdefault('type_id', 'touch')
99+
super().__init__(*args, **kwargs)
100+
self.profile = ('pos', 'shape')
101+
96102
def depack(self, args):
97-
self.is_touch = True
98103
self.shape = ShapeRect()
99104
self.sx, self.sy = args[0], args[1]
100105
self.shape.width = args[2]
101106
self.shape.height = args[2]
102-
self.profile = ('pos', 'shape')
103-
super(MacMotionEvent, self).depack(args)
107+
super().depack(args)
104108

105109
def __str__(self):
106110
return '<MacMotionEvent id=%d pos=(%f, %f) device=%s>' \

kivy/input/providers/mtdev.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@
5353

5454
class MTDMotionEvent(MotionEvent):
5555

56+
def __init__(self, *args, **kwargs):
57+
kwargs.setdefault('is_touch', True)
58+
kwargs.setdefault('type_id', 'touch')
59+
super().__init__(*args, **kwargs)
60+
5661
def depack(self, args):
57-
self.is_touch = True
5862
if 'x' in args:
5963
self.sx = args['x']
6064
else:
@@ -72,7 +76,7 @@ def depack(self, args):
7276
if 'pressure' in args:
7377
self.pressure = args['pressure']
7478
self.profile.append('pressure')
75-
super(MTDMotionEvent, self).depack(args)
79+
super().depack(args)
7680

7781
def __str__(self):
7882
i, sx, sy, d = (self.id, self.sx, self.sy, self.device)

kivy/input/providers/tuio.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ class TuioMotionEventProvider(MotionEventProvider):
6161
# Create a class to handle the new TUIO type/path
6262
# Replace NEWPATH with the pathname you want to handle
6363
class TuioNEWPATHMotionEvent(MotionEvent):
64-
def __init__(self, id, args):
65-
super(TuioNEWPATHMotionEvent, self).__init__(id, args)
6664
6765
def depack(self, args):
6866
# In this method, implement 'unpacking' for the received
@@ -73,7 +71,7 @@ def depack(self, args):
7371
self.sx, self.sy = args
7472
self.profile = ('pos', )
7573
self.sy = 1 - self.sy
76-
super(TuioNEWPATHMotionEvent, self).depack(args)
74+
super().depack(args)
7775
7876
# Register it with the TUIO MotionEvent provider.
7977
# You obviously need to replace the PATH placeholders appropriately.
@@ -90,9 +88,9 @@ def depack(self, args):
9088
__handlers__ = {}
9189

9290
def __init__(self, device, args):
93-
super(TuioMotionEventProvider, self).__init__(device, args)
91+
super().__init__(device, args)
9492
args = args.split(',')
95-
if len(args) <= 0:
93+
if len(args) == 0:
9694
Logger.error('Tuio: Invalid configuration for TUIO provider')
9795
Logger.error('Tuio: Format must be ip:port (eg. 127.0.0.1:3333)')
9896
err = 'Tuio: Current configuration is <%s>' % (str(','.join(args)))
@@ -220,8 +218,10 @@ class TuioMotionEvent(MotionEvent):
220218
'''
221219
__attrs__ = ('a', 'b', 'c', 'X', 'Y', 'Z', 'A', 'B', 'C', 'm', 'r')
222220

223-
def __init__(self, device, id, args):
224-
super(TuioMotionEvent, self).__init__(device, id, args)
221+
def __init__(self, *args, **kwargs):
222+
kwargs.setdefault('is_touch', True)
223+
kwargs.setdefault('type_id', 'touch')
224+
super().__init__(*args, **kwargs)
225225
# Default argument for TUIO touches
226226
self.a = 0.0
227227
self.b = 0.0
@@ -246,11 +246,7 @@ def __init__(self, device, id, args):
246246
class Tuio2dCurMotionEvent(TuioMotionEvent):
247247
'''A 2dCur TUIO touch.'''
248248

249-
def __init__(self, device, id, args):
250-
super(Tuio2dCurMotionEvent, self).__init__(device, id, args)
251-
252249
def depack(self, args):
253-
self.is_touch = True
254250
if len(args) < 5:
255251
self.sx, self.sy = list(map(float, args[0:2]))
256252
self.profile = ('pos', )
@@ -269,18 +265,14 @@ def depack(self, args):
269265
self.shape.width = width
270266
self.shape.height = height
271267
self.sy = 1 - self.sy
272-
super(Tuio2dCurMotionEvent, self).depack(args)
268+
super().depack(args)
273269

274270

275271
class Tuio2dObjMotionEvent(TuioMotionEvent):
276272
'''A 2dObj TUIO object.
277273
'''
278274

279-
def __init__(self, device, id, args):
280-
super(Tuio2dObjMotionEvent, self).__init__(device, id, args)
281-
282275
def depack(self, args):
283-
self.is_touch = True
284276
if len(args) < 5:
285277
self.sx, self.sy = args[0:2]
286278
self.profile = ('pos', )
@@ -301,7 +293,7 @@ def depack(self, args):
301293
self.shape.width = width
302294
self.shape.height = height
303295
self.sy = 1 - self.sy
304-
super(Tuio2dObjMotionEvent, self).depack(args)
296+
super().depack(args)
305297

306298

307299
class Tuio2dBlbMotionEvent(TuioMotionEvent):
@@ -311,22 +303,20 @@ class Tuio2dBlbMotionEvent(TuioMotionEvent):
311303
/tuio/2Dblb set s x y a w h f X Y A m r
312304
'''
313305

314-
def __init__(self, device, id, args):
315-
super(Tuio2dBlbMotionEvent, self).__init__(device, id, args)
306+
def __init__(self, *args, **kwargs):
307+
super().__init__(*args, **kwargs)
308+
self.profile = ('pos', 'angle', 'mov', 'rot', 'rotacc', 'acc', 'shape')
316309

317310
def depack(self, args):
318-
self.is_touch = True
319311
self.sx, self.sy, self.a, self.X, self.Y, sw, sh, sd, \
320312
self.A, self.m, self.r = args
321313
self.Y = -self.Y
322-
self.profile = ('pos', 'angle', 'mov', 'rot', 'rotacc',
323-
'acc', 'shape')
324314
if self.shape is None:
325315
self.shape = ShapeRect()
326316
self.shape.width = sw
327317
self.shape.height = sh
328318
self.sy = 1 - self.sy
329-
super(Tuio2dBlbMotionEvent, self).depack(args)
319+
super().depack(args)
330320

331321

332322
# registers

kivy/input/providers/wm_pen.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616
class WM_Pen(MotionEvent):
1717
'''MotionEvent representing the WM_Pen event. Supports the pos profile.'''
1818

19+
def __init__(self, *args, **kwargs):
20+
kwargs.setdefault('is_touch', True)
21+
kwargs.setdefault('type_id', 'touch')
22+
super().__init__(*args, **kwargs)
23+
self.profile = ['pos']
24+
1925
def depack(self, args):
20-
self.is_touch = True
2126
self.sx, self.sy = args[0], args[1]
22-
super(WM_Pen, self).depack(args)
27+
super().depack(args)
2328

2429
def __str__(self):
2530
i, u, s, d = (self.id, self.uid, str(self.spos), self.device)

kivy/input/providers/wm_touch.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@ class WM_MotionEvent(MotionEvent):
2222
'''
2323
__attrs__ = ('size', )
2424

25+
def __init__(self, *args, **kwargs):
26+
kwargs.setdefault('is_touch', True)
27+
kwargs.setdefault('type_id', 'touch')
28+
super().__init__(*args, **kwargs)
29+
self.profile = ('pos', 'shape', 'size')
30+
2531
def depack(self, args):
26-
self.is_touch = True
2732
self.shape = ShapeRect()
2833
self.sx, self.sy = args[0], args[1]
2934
self.shape.width = args[2][0]
3035
self.shape.height = args[2][1]
3136
self.size = self.shape.width * self.shape.height
32-
self.profile = ('pos', 'shape', 'size')
33-
34-
super(WM_MotionEvent, self).depack(args)
37+
super().depack(args)
3538

3639
def __str__(self):
3740
args = (self.id, self.uid, str(self.spos), self.device)

kivy/tests/common.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -427,18 +427,20 @@ def __init__(self, x, y):
427427
'''Create a MotionEvent instance with X and Y of the first
428428
position a touch is at.
429429
'''
430-
431430
from kivy.base import EventLoop
432431
self.eventloop = EventLoop
433432
win = EventLoop.window
434-
435433
super(UnitTestTouch, self).__init__(
436434
# device, (tuio) id, args
437435
self.__class__.__name__, 99, {
438436
"x": x / (win.width - 1.0),
439437
"y": y / (win.height - 1.0),
440-
}
438+
},
439+
is_touch=True,
440+
type_id='touch'
441441
)
442+
# set profile to accept x, y and pos properties
443+
self.profile = ['pos']
442444

443445
def touch_down(self, *args):
444446
self.eventloop.post_dispatch_input("begin", self)
@@ -455,27 +457,27 @@ def touch_up(self, *args):
455457
self.eventloop.post_dispatch_input("end", self)
456458

457459
def depack(self, args):
458-
# set MotionEvent to touch
459-
self.is_touch = True
460-
461460
# set sx/sy properties to ratio (e.g. X / win.width)
462461
self.sx = args['x']
463462
self.sy = args['y']
464-
465-
# set profile to accept x, y and pos properties
466-
self.profile = ['pos']
467-
468463
# run depack after we set the values
469-
super(UnitTestTouch, self).depack(args)
464+
super().depack(args)
470465

471466

467+
# https://gist.github.com/tito/f111b6916aa6a4ed0851
468+
# subclass for touch event in unit test
472469
class UTMotionEvent(MotionEvent):
470+
471+
def __init__(self, *args, **kwargs):
472+
kwargs.setdefault('is_touch', True)
473+
kwargs.setdefault('type_id', 'touch')
474+
super().__init__(*args, **kwargs)
475+
self.profile = ['pos']
476+
473477
def depack(self, args):
474-
self.is_touch = True
475478
self.sx = args['x']
476479
self.sy = args['y']
477-
self.profile = ['pos']
478-
super(UTMotionEvent, self).depack(args)
480+
super().depack(args)
479481

480482

481483
def async_run(func=None, app_cls_func=None):

0 commit comments

Comments
 (0)