Enable pressure for touches in android (and ios?) (#7684)

* enable pressure four touches in android (and some ios devices)

* style fixes
This commit is contained in:
Gabriel Pettier 2021-12-25 17:44:00 +01:00 committed by GitHub
parent 7206762b88
commit b692b22763
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 11 deletions

View File

@ -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(

View File

@ -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',

View File

@ -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':