Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import random
- from math import sqrt
- import numpy
- import kivy
- kivy.require('1.0.6')
- from kivy.app import App
- from kivy.uix.widget import Widget
- from kivy.uix.button import Button
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.floatlayout import FloatLayout
- from kivy.uix.label import Label
- from kivy.graphics import Color, Rectangle, Point, GraphicException
- def calculate_points(x1, y1, x2, y2, steps=5):
- 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(1, int(m)):
- mi = i / m
- lastx = x1 + dx * mi
- lasty = y1 + dy * mi
- o.extend([lastx, lasty])
- return o
- class Touchtracer(FloatLayout):
- def on_touch_down(self, touch):
- win = self.get_parent_window()
- ud = touch.ud
- ud['group'] = g = str(touch.uid)
- ud['group2'] = g2 = str(touch.uid * -1)
- with self.canvas:
- ud['color'] = Color(0.210, 0.76, 0.97, mode='rgb', group=g)
- ud['lines'] = (
- #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['cross'] = (
- Rectangle(pos=(touch.x, 0), size=(1, win.height), group=g2),
- Rectangle(pos=(0, touch.y), size=(win.width, 1), group=g2),
- #Point(points=(touch.x, touch.y),source='particle.png', pointsize=5, group=g)
- )
- ud['label'] = Label(size_hint=(None, None))
- self.update_touch_label(ud['label'], touch)
- self.add_widget(ud['label'])
- touch.grab(self)
- return True
- def on_touch_move(self, touch):
- #print(touch)
- if touch.grab_current is not self:
- return
- ud = touch.ud
- #ud['lines'].points = (touch.x, touch.y)
- ud['cross'][0].pos = touch.x, 0
- ud['cross'][1].pos = 0, touch.y
- points = ud['lines'].points
- oldx, oldy = points[-2], points[-1]
- points = calculate_points(oldx, oldy, touch.x, touch.y)
- if points:
- try:
- lp = ud['lines'].add_point
- for idx in xrange(0, len(points), 2):
- lp(points[idx], points[idx+1])
- except GraphicException:
- pass
- ud['label'].pos = touch.pos
- import time
- t = int(time.time())
- if t not in ud:
- ud[t] = 1
- else:
- ud[t] += 1
- self.update_touch_label(ud['label'], touch)
- def on_touch_up(self, touch):
- if touch.grab_current is not self:
- return
- touch.ungrab(self)
- ud = touch.ud
- #self.canvas.remove_group(ud['group'])
- self.canvas.remove_group(ud['group2'])
- self.remove_widget(ud['label'])
- def update_touch_label(self, label, touch):
- azimuth = (touch.x - 400) * 0.08
- elevation = (touch.y + 90) * 0.075
- label.text = 'Azimuth: %d \nElevation: %d' % (
- azimuth, elevation)
- label.texture_update()
- label.pos = touch.pos
- label.size = label.texture_size[0] + 20, label.texture_size[1] + 20
- def clear_canvas(self, touch):
- self.canvas.remove_group(touch.ud['group'])
- class TouchtracerApp(App):
- title = 'Touchtracer'
- icon = 'icon.png'
- def build(self):
- parent = FloatLayout(size=(200, 300))
- #parent = Widget()
- lineWidget = Touchtracer()
- clearbuttonWidget = Button(text='Clear', size_hint=(.1, .05), pos=(20, 20))
- parent.add_widget(lineWidget)
- parent.add_widget(clearbuttonWidget)
- clearbuttonWidget.bind(on_release=Touchtracer.clear_canvas)
- return parent
- # return Touchtracer()
- def on_pause(self):
- return True
- if __name__ == '__main__':
- TouchtracerApp().run()
Add Comment
Please, Sign In to add comment