mirror of https://github.com/kivy/kivy.git
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:
parent
7206762b88
commit
b692b22763
|
@ -32,6 +32,7 @@ from kivy.app import App
|
||||||
from kivy.uix.floatlayout import FloatLayout
|
from kivy.uix.floatlayout import FloatLayout
|
||||||
from kivy.uix.label import Label
|
from kivy.uix.label import Label
|
||||||
from kivy.graphics import Color, Rectangle, Point, GraphicException
|
from kivy.graphics import Color, Rectangle, Point, GraphicException
|
||||||
|
from kivy.metrics import dp
|
||||||
from random import random
|
from random import random
|
||||||
from math import sqrt
|
from math import sqrt
|
||||||
|
|
||||||
|
@ -54,14 +55,23 @@ def calculate_points(x1, y1, x2, y2, steps=5):
|
||||||
|
|
||||||
class Touchtracer(FloatLayout):
|
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):
|
def on_touch_down(self, touch):
|
||||||
win = self.get_parent_window()
|
win = self.get_parent_window()
|
||||||
ud = touch.ud
|
ud = touch.ud
|
||||||
ud['group'] = g = str(touch.uid)
|
ud['group'] = g = str(touch.uid)
|
||||||
pointsize = 5
|
pointsize = 5
|
||||||
|
print(touch.profile)
|
||||||
if 'pressure' in touch.profile:
|
if 'pressure' in touch.profile:
|
||||||
ud['pressure'] = touch.pressure
|
ud['pressure'] = touch.pressure
|
||||||
pointsize = (touch.pressure * 100000) ** 2
|
pointsize = self.normalize_pressure(touch.pressure)
|
||||||
ud['color'] = random()
|
ud['color'] = random()
|
||||||
|
|
||||||
with self.canvas:
|
with self.canvas:
|
||||||
|
@ -92,16 +102,20 @@ class Touchtracer(FloatLayout):
|
||||||
points = ud['lines'][index].points
|
points = ud['lines'][index].points
|
||||||
oldx, oldy = points[-2], points[-1]
|
oldx, oldy = points[-2], points[-1]
|
||||||
break
|
break
|
||||||
except:
|
except IndexError:
|
||||||
index -= 1
|
index -= 1
|
||||||
|
|
||||||
points = calculate_points(oldx, oldy, touch.x, touch.y)
|
points = calculate_points(oldx, oldy, touch.x, touch.y)
|
||||||
|
|
||||||
# if pressure changed create a new point instruction
|
# if pressure changed create a new point instruction
|
||||||
if 'pressure' in ud:
|
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']
|
g = ud['group']
|
||||||
pointsize = (touch.pressure * 100000) ** 2
|
pointsize = self.normalize_pressure(touch.pressure)
|
||||||
with self.canvas:
|
with self.canvas:
|
||||||
Color(ud['color'], 1, 1, mode='hsv', group=g)
|
Color(ud['color'], 1, 1, mode='hsv', group=g)
|
||||||
ud['lines'].append(
|
ud['lines'].append(
|
||||||
|
|
|
@ -641,13 +641,15 @@ cdef class _WindowSDL2Storage:
|
||||||
fid = event.tfinger.fingerId
|
fid = event.tfinger.fingerId
|
||||||
x = event.tfinger.x
|
x = event.tfinger.x
|
||||||
y = event.tfinger.y
|
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:
|
elif event.type == SDL_FINGERDOWN or event.type == SDL_FINGERUP:
|
||||||
fid = event.tfinger.fingerId
|
fid = event.tfinger.fingerId
|
||||||
x = event.tfinger.x
|
x = event.tfinger.x
|
||||||
y = event.tfinger.y
|
y = event.tfinger.y
|
||||||
|
pressure = event.tfinger.pressure
|
||||||
action = 'fingerdown' if event.type == SDL_FINGERDOWN else 'fingerup'
|
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:
|
elif event.type == SDL_JOYAXISMOTION:
|
||||||
return (
|
return (
|
||||||
'joyaxismotion',
|
'joyaxismotion',
|
||||||
|
|
|
@ -107,8 +107,8 @@ SDLK_F15 = 1073741896
|
||||||
class SDL2MotionEvent(MotionEvent):
|
class SDL2MotionEvent(MotionEvent):
|
||||||
def depack(self, args):
|
def depack(self, args):
|
||||||
self.is_touch = True
|
self.is_touch = True
|
||||||
self.profile = ('pos', )
|
self.profile = ('pos', 'pressure', )
|
||||||
self.sx, self.sy = args
|
self.sx, self.sy, self.pressure = args
|
||||||
win = EventLoop.window
|
win = EventLoop.window
|
||||||
super(SDL2MotionEvent, self).depack(args)
|
super(SDL2MotionEvent, self).depack(args)
|
||||||
|
|
||||||
|
@ -126,13 +126,15 @@ class SDL2MotionEventProvider(MotionEventProvider):
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return
|
return
|
||||||
|
|
||||||
action, fid, x, y = value
|
action, fid, x, y, pressure = value
|
||||||
y = 1 - y
|
y = 1 - y
|
||||||
if fid not in touchmap:
|
if fid not in touchmap:
|
||||||
touchmap[fid] = me = SDL2MotionEvent('sdl', fid, (x, y))
|
touchmap[fid] = me = SDL2MotionEvent(
|
||||||
|
'sdl', fid, (x, y, pressure)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
me = touchmap[fid]
|
me = touchmap[fid]
|
||||||
me.move((x, y))
|
me.move((x, y, pressure))
|
||||||
if action == 'fingerdown':
|
if action == 'fingerdown':
|
||||||
dispatch_fn('begin', me)
|
dispatch_fn('begin', me)
|
||||||
elif action == 'fingerup':
|
elif action == 'fingerup':
|
||||||
|
|
Loading…
Reference in New Issue