Created
May 27, 2015 18:27
-
-
Save spinningD20/a2aa2a6828b1e0b3f4ba to your computer and use it in GitHub Desktop.
PinchLabel to StackLayout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StackLayout: | |
PinchLabel: | |
text: 'hello' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kivy | |
kivy.require('1.9.0') | |
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.uix.label import Label | |
from kivy.vector import Vector | |
from kivy.properties import BoundedNumericProperty | |
class PinchLabel(Label): | |
font_size_min = BoundedNumericProperty(2, min=0) | |
font_size_max = BoundedNumericProperty(1e100, min=0) | |
def __init__(self, **kwargs): | |
super(PinchLabel, self).__init__(**kwargs) | |
self._touches = [] | |
self._last_touch_pos = {} | |
def on_touch_down(self, touch): | |
if not self.collide_point(*touch.pos): | |
return | |
if 'multitouch_sim' in touch.profile: | |
touch.multitouch_sim = True | |
touch.grab(self) | |
self._touches.append(touch) | |
self._last_touch_pos[touch] = touch.pos | |
return True | |
def on_touch_move(self, touch): | |
if touch in self._touches and touch.grab_current == self: | |
self.scale_with_touch(touch) | |
self._last_touch_pos[touch] = touch.pos | |
if self.collide_point(*touch.pos): | |
return True | |
def on_touch_up(self, touch): | |
if touch in self._touches and touch.grab_state: | |
touch.ungrab(self) | |
del self._last_touch_pos[touch] | |
self._touches.remove(touch) | |
if self.collide_point(*touch.pos): | |
return True | |
def scale_with_touch(self, touch): | |
if len(self._touches) == 1: | |
return False | |
points = [Vector(self._last_touch_pos[t]) for t in self._touches if t is not touch] | |
points.append(Vector(touch.pos)) | |
anchor = max(points[:-1], key=lambda p: p.distance(touch.pos)) | |
farthest = max(points, key=anchor.distance) | |
if farthest is not points[-1]: | |
return False | |
old_line = Vector(*touch.ppos) - anchor | |
new_line = Vector(*touch.pos) - anchor | |
if not old_line.length(): | |
return False | |
scale = new_line.length() / old_line.length() | |
new_scale = scale * self.font_size | |
new_scale = min(self.font_size_max, max(self.font_size_min, new_scale)) | |
self.font_size = new_scale | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[INFO ] [Base ] Leaving application in progress... | |
Traceback (most recent call last): | |
File "C:/Users/mike/Dropbox/salesBerry/rev2.py", line 219, in <module> | |
Rev2App().run() | |
File "C:\Users\mike\Kivy\kivy27\kivy\app.py", line 824, in run | |
runTouchApp() | |
File "C:\Users\mike\Kivy\kivy27\kivy\base.py", line 487, in runTouchApp | |
EventLoop.window.mainloop() | |
File "C:\Users\mike\Kivy\kivy27\kivy\core\window\window_sdl2.py", line 525, in mainloop | |
self._mainloop() | |
File "C:\Users\mike\Kivy\kivy27\kivy\core\window\window_sdl2.py", line 290, in _mainloop | |
EventLoop.idle() | |
File "C:\Users\mike\Kivy\kivy27\kivy\base.py", line 327, in idle | |
Clock.tick() | |
File "C:\Users\mike\Kivy\kivy27\kivy\clock.py", line 483, in tick | |
self._process_events() | |
File "C:\Users\mike\Kivy\kivy27\kivy\clock.py", line 615, in _process_events | |
event.tick(self._last_tick, remove) | |
File "C:\Users\mike\Kivy\kivy27\kivy\clock.py", line 374, in tick | |
ret = callback(self._dt) | |
File "C:\Users\mike\Kivy\kivy27\kivy\uix\stacklayout.py", line 263, in do_layout | |
c2.pos[innerattr] = u | |
TypeError: 'tuple' object does not support item assignment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment