Guest User

Untitled

a guest
Dec 16th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.10 KB | None | 0 0
  1. from random import random
  2. from math import sqrt
  3. import numpy
  4.  
  5. import kivy
  6. kivy.require('1.0.6')
  7. from kivy.app import App
  8. from kivy.uix.widget import Widget
  9. from kivy.uix.button import Button
  10. from kivy.uix.boxlayout import BoxLayout
  11. from kivy.uix.floatlayout import FloatLayout
  12. from kivy.uix.label import Label
  13. from kivy.graphics import Color, Rectangle, Point, GraphicException
  14.  
  15.  
  16.  
  17. def calculate_points(x1, y1, x2, y2, steps=5):
  18.     dx = x2 - x1
  19.     dy = y2 - y1
  20.     dist = sqrt(dx * dx + dy * dy)
  21.     if dist < steps:
  22.         return None
  23.     o = []
  24.     m = dist / steps
  25.     for i in xrange(1, int(m)):
  26.         mi = i / m
  27.         lastx = x1 + dx * mi
  28.         lasty = y1 + dy * mi
  29.         o.extend([lastx, lasty])
  30.     return o
  31.  
  32.  
  33. class Touchtracer(FloatLayout):
  34.  
  35.     def on_touch_down(self, touch):
  36.         win = self.get_parent_window()
  37.         ud = touch.ud
  38.         ud['group'] = g = str(touch.uid)
  39.         ud['group2'] = g2 = str(touch.uid * -1)
  40.         with self.canvas:
  41.             ud['color'] = Color(0.210, 0.76, 0.97, mode='rgb', group=g)
  42.             ud['lines'] = (
  43.                 #Rectangle(pos=(touch.x, 0), size=(1, win.height), group=g),
  44.                 #Rectangle(pos=(0, touch.y), size=(win.width, 1), group=g),
  45.                 Point(points=(touch.x, touch.y), source='particle.png',
  46.                       pointsize=5, group=g))
  47.             ud['cross'] = (
  48.                 Rectangle(pos=(touch.x, 0), size=(1, win.height), group=g2),
  49.                 Rectangle(pos=(0, touch.y), size=(win.width, 1), group=g2),
  50.                 #Point(points=(touch.x, touch.y),source='particle.png', pointsize=5, group=g)
  51.                 )
  52.            
  53.  
  54.         ud['label'] = Label(size_hint=(None, None))
  55.         self.update_touch_label(ud['label'], touch)
  56.         self.add_widget(ud['label'])
  57.         touch.grab(self)
  58.         return True
  59.  
  60.     def on_touch_move(self, touch):
  61.         #print(touch)
  62.         if touch.grab_current is not self:
  63.             return
  64.         ud = touch.ud
  65.         #ud['lines'].points = (touch.x, touch.y)
  66.         ud['cross'][0].pos = touch.x, 0
  67.         ud['cross'][1].pos = 0, touch.y
  68.  
  69.         points = ud['lines'].points
  70.         oldx, oldy = points[-2], points[-1]
  71.         points = calculate_points(oldx, oldy, touch.x, touch.y)
  72.         if points:
  73.             try:
  74.                 lp = ud['lines'].add_point
  75.                 for idx in xrange(0, len(points), 2):
  76.                     lp(points[idx], points[idx+1])
  77.             except GraphicException:
  78.                 pass
  79.  
  80.         ud['label'].pos = touch.pos
  81.         import time
  82.         t = int(time.time())
  83.         if t not in ud:
  84.             ud[t] = 1
  85.         else:
  86.             ud[t] += 1
  87.         self.update_touch_label(ud['label'], touch)
  88.  
  89.     def on_touch_up(self, touch):
  90.         if touch.grab_current is not self:
  91.             return
  92.         touch.ungrab(self)
  93.         ud = touch.ud
  94.         #self.canvas.remove_group(ud['group'])
  95.         self.canvas.remove_group(ud['group2'])
  96.         self.remove_widget(ud['label'])
  97.  
  98.     def update_touch_label(self, label, touch):
  99.         azimuth = (touch.x - 400) * 0.08
  100.         elevation = (touch.y + 90) * 0.075
  101.         label.text = 'Azimuth: %d \nElevation: %d' % (
  102.             azimuth, elevation)
  103.         label.texture_update()
  104.         label.pos = touch.pos
  105.         label.size = label.texture_size[0] + 20, label.texture_size[1] + 20
  106.        
  107.     def clear_canvas(self, touch):
  108.         self.canvas.remove_group(touch.ud['group'])
  109.  
  110.  
  111. class TouchtracerApp(App):
  112.     title = 'Touchtracer'
  113.     icon = 'icon.png'
  114.  
  115.     def build(self):
  116.         parent = FloatLayout(size=(200, 300))
  117.         #parent = Widget()
  118.         lineWidget = Touchtracer()
  119.         clearbuttonWidget = Button(text='Clear', size_hint=(.1, .05), pos=(20, 20))
  120.         parent.add_widget(lineWidget)
  121.         parent.add_widget(clearbuttonWidget)
  122.        
  123.         clearbuttonWidget.bind(on_release=Touchtracer.clear_canvas)
  124.        
  125.         return parent
  126.        
  127. #        return Touchtracer()
  128.  
  129.     def on_pause(self):
  130.         return True
  131.  
  132. if __name__ == '__main__':
  133.     TouchtracerApp().run()
Add Comment
Please, Sign In to add comment