diff --git a/examples/demo/touchtracer/main.py b/examples/demo/touchtracer/main.py index 84dc567c1..ad0399ca7 100644 --- a/examples/demo/touchtracer/main.py +++ b/examples/demo/touchtracer/main.py @@ -32,6 +32,7 @@ from kivy.app import App from kivy.uix.floatlayout import FloatLayout from kivy.uix.label import Label from kivy.graphics import Color, Rectangle, Point, GraphicException +from kivy.metrics import dp from random import random from math import sqrt @@ -54,14 +55,23 @@ def calculate_points(x1, y1, x2, y2, steps=5): class Touchtracer(FloatLayout): + def normalize_pressure(self, pressure): + print(pressure) + # this might mean we are on a device whose pressure value is + # incorrectly reported by SDL2, like recent iOS devices. + if pressure == 0.0: + return 1 + return dp(pressure * 10) + def on_touch_down(self, touch): win = self.get_parent_window() ud = touch.ud ud['group'] = g = str(touch.uid) pointsize = 5 + print(touch.profile) if 'pressure' in touch.profile: ud['pressure'] = touch.pressure - pointsize = (touch.pressure * 100000) ** 2 + pointsize = self.normalize_pressure(touch.pressure) ud['color'] = random() with self.canvas: @@ -92,16 +102,20 @@ class Touchtracer(FloatLayout): points = ud['lines'][index].points oldx, oldy = points[-2], points[-1] break - except: + except IndexError: index -= 1 points = calculate_points(oldx, oldy, touch.x, touch.y) # if pressure changed create a new point instruction if 'pressure' in ud: - if not .95 < (touch.pressure / ud['pressure']) < 1.05: + old_pressure = ud['pressure'] + if ( + not old_pressure + or not .99 < (touch.pressure / old_pressure) < 1.01 + ): g = ud['group'] - pointsize = (touch.pressure * 100000) ** 2 + pointsize = self.normalize_pressure(touch.pressure) with self.canvas: Color(ud['color'], 1, 1, mode='hsv', group=g) ud['lines'].append( diff --git a/kivy/core/window/_window_sdl2.pyx b/kivy/core/window/_window_sdl2.pyx index f4b9af601..672923405 100644 --- a/kivy/core/window/_window_sdl2.pyx +++ b/kivy/core/window/_window_sdl2.pyx @@ -641,13 +641,15 @@ cdef class _WindowSDL2Storage: fid = event.tfinger.fingerId x = event.tfinger.x y = event.tfinger.y - return ('fingermotion', fid, x, y) + pressure = event.tfinger.pressure + return ('fingermotion', fid, x, y, pressure) elif event.type == SDL_FINGERDOWN or event.type == SDL_FINGERUP: fid = event.tfinger.fingerId x = event.tfinger.x y = event.tfinger.y + pressure = event.tfinger.pressure action = 'fingerdown' if event.type == SDL_FINGERDOWN else 'fingerup' - return (action, fid, x, y) + return (action, fid, x, y, pressure) elif event.type == SDL_JOYAXISMOTION: return ( 'joyaxismotion', diff --git a/kivy/core/window/window_sdl2.py b/kivy/core/window/window_sdl2.py index b2f891b84..a6654fadb 100644 --- a/kivy/core/window/window_sdl2.py +++ b/kivy/core/window/window_sdl2.py @@ -107,8 +107,8 @@ SDLK_F15 = 1073741896 class SDL2MotionEvent(MotionEvent): def depack(self, args): self.is_touch = True - self.profile = ('pos', ) - self.sx, self.sy = args + self.profile = ('pos', 'pressure', ) + self.sx, self.sy, self.pressure = args win = EventLoop.window super(SDL2MotionEvent, self).depack(args) @@ -126,13 +126,15 @@ class SDL2MotionEventProvider(MotionEventProvider): except IndexError: return - action, fid, x, y = value + action, fid, x, y, pressure = value y = 1 - y if fid not in touchmap: - touchmap[fid] = me = SDL2MotionEvent('sdl', fid, (x, y)) + touchmap[fid] = me = SDL2MotionEvent( + 'sdl', fid, (x, y, pressure) + ) else: me = touchmap[fid] - me.move((x, y)) + me.move((x, y, pressure)) if action == 'fingerdown': dispatch_fn('begin', me) elif action == 'fingerup':