From 1f4653d961dc2902800efefa325d92ab895385c1 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Sat, 15 Jan 2011 03:02:46 +0100 Subject: [PATCH] touchdebug: use new Point() and remove_group() --- examples/touchdebug.py | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/examples/touchdebug.py b/examples/touchdebug.py index 3238a642c..9f1411ae2 100644 --- a/examples/touchdebug.py +++ b/examples/touchdebug.py @@ -1,34 +1,59 @@ from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.label import Label -from kivy.graphics import Color, Rectangle +from kivy.graphics import Color, Rectangle, Point +from random import random +from math import sqrt + +def calculate_points(x1, y1, x2, y2, steps=2): + dx = x2 - x1 + dy = y2 - y1 + dist = sqrt(dx * dx + dy * dy) + if dist < steps: + return None + o = [] + m = dist / steps + for i in xrange(int(m)): + mi = i / m + o.extend([x1 + dx * mi, y1 + dy * mi]) + return o + class Touchdebug(Widget): def on_touch_down(self, touch): win = self.get_parent_window() ud = touch.userdata + ud['group'] = g = str(touch.uid) with self.canvas: - ud['color'] = Color(1, 1, 1) + ud['color'] = Color(random(), 1, 1, mode='hsv', group=g) ud['lines'] = ( - Rectangle(pos=(touch.x, 0), size=(1, win.height)), - Rectangle(pos=(0, touch.y), size=(win.width, 1))) + Rectangle(pos=(touch.x, 0), size=(1, win.height), group=g), + Rectangle(pos=(0, touch.y), size=(win.width, 1), group=g), + Point(points=(touch.x, touch.y), source='particle.png', + pointsize=5, group=g) + ) ud['label'] = Label() self.update_touch_label(ud['label'], touch) self.add_widget(ud['label']) + ud['oldtouch'] = touch.x, touch.y def on_touch_move(self, touch): ud = touch.userdata ud['lines'][0].pos = touch.x, 0 ud['lines'][1].pos = 0, touch.y + + oldx, oldy = ud['oldtouch'] + points = calculate_points(oldx, oldy, touch.x, touch.y) + if points: + ud['oldtouch'] = touch.x, touch.y + ud['lines'][2].points = ud['lines'][2].points + points ud['label'].pos = touch.pos self.update_touch_label(ud['label'], touch) def on_touch_up(self, touch): ud = touch.userdata - self.canvas.remove(ud['color']) - self.canvas.remove(ud['lines'][0]) - self.canvas.remove(ud['lines'][1]) + self.canvas.remove_group(ud['group']) self.remove_widget(ud['label']) def update_touch_label(self, label, touch):