Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. from kivy.uix.floatlayout import FloatLayout
  2. from kivy.graphics import Color, Point, GraphicException
  3. from math import sqrt
  4.  
  5.  
  6. def calculate_points(x1, y1, x2, y2, steps=1):
  7. dx = x2 - x1
  8. dy = y2 - y1
  9. dist = sqrt(dx * dx + dy * dy)
  10. if dist < steps:
  11. return
  12. o = []
  13. m = dist / steps
  14. for i in range(1, int(m)):
  15. mi = i / m
  16. lastx = x1 + dx * mi
  17. lasty = y1 + dy * mi
  18. o.extend([lastx, lasty])
  19. return o
  20.  
  21.  
  22. class DrawPad(FloatLayout):
  23. def __init__(self, ** kwargs):
  24. super(DrawPad, self).__init__(** kwargs)
  25. self.in_pad = False
  26.  
  27. def on_touch_down(self, touch):
  28. ud = touch.ud
  29. if (touch.pos[0] > self.pos[0]
  30. and touch.pos[0] < self.pos[0] + self.size[0]
  31. and touch.pos[1] > self.pos[1]
  32. and touch.pos[1] < self.pos[1] + self.size[1]):
  33. self.in_pad = True
  34. ud['group'] = g = str(touch.uid)
  35. pointsize = 1
  36. ud['color'] = 0
  37. with self.canvas.before:
  38. Color(0, 0, 0)
  39. ud['lines'] = [
  40. Point(points=(touch.x, touch.y),
  41. pointsize=pointsize, group=g)]
  42. return True
  43. else:
  44. ud['lines'] = []
  45.  
  46. def on_touch_move(self, touch):
  47. if(self.in_pad and touch.pos[0] > self.pos[0]
  48. and touch.pos[0] < self.pos[0] + self.size[0]
  49. and touch.pos[1] > self.pos[1]
  50. and touch.pos[1] < self.pos[1] + self.size[1]):
  51. ud = touch.ud
  52.  
  53. points = ud['lines'][-1].points
  54. oldx, oldy = points[-2], points[-1]
  55. points = calculate_points(oldx, oldy, touch.x, touch.y)
  56. if points:
  57. try:
  58. lp = ud['lines'][-1].add_point
  59. for idx in range(0, len(points), 2):
  60. lp(points[idx], points[idx + 1])
  61. except GraphicException:
  62. pass
  63.  
  64. def on_touch_up(self, touch):
  65. ud = touch.ud
  66. if self.in_pad:
  67. self.in_pad = False
  68. #return (self.canvas.get_group(ud['group'])[1].points)
  69.  
  70. def click(self):
  71. print('click')
  72.  
  73. def buttonImage(self):
  74. return 'assets/graphics/clear1.png'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement