Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.config import Config
  3. from kivy.uix.widget import Widget
  4. from kivy.uix.button import Button
  5. from kivy.graphics import Color, Ellipse, Line
  6. from kivy.lang import Builder
  7.  
  8. from enum import IntEnum
  9. from enum import Enum
  10.  
  11. from random import random
  12.  
  13. class Vertice(Widget):
  14. def __init__(self, pos=None):
  15.  
  16. self.pos = pos
  17. #print self.pos
  18. print self.size
  19.  
  20. class Type(IntEnum):
  21. RIGHT_TOP = 1
  22. RIGHT_BOTTOM = 2
  23. LEFT_BOTTOM = 3
  24. LEFT_TOP = 4
  25. UNDEFINED = 5
  26.  
  27. class Direction(Enum):
  28. CLOCKWISE = 1
  29. COUNTER_CLOCKWISE = 2
  30. UNDEFINED = 3
  31.  
  32. class MindMapWidget(Widget):
  33. circleShape = [Type.RIGHT_TOP, Type.RIGHT_BOTTOM, Type.LEFT_BOTTOM, Type.LEFT_TOP]
  34. editing = False
  35. bounds = []
  36. vertices = []
  37.  
  38. def getType(self, dx, dy, direction):
  39. result = Type.UNDEFINED
  40.  
  41. if direction != Direction.UNDEFINED:
  42. if dx > 0 and dy > 0:
  43. if (self.isClockwise(direction)):
  44. result = Type.RIGHT_TOP
  45. else:
  46. result = Type.LEFT_BOTTOM
  47. else:
  48. if dx < 0 and dy > 0:
  49. if (self.isClockwise(direction)):
  50. result = Type.RIGHT_BOTTOM
  51. else:
  52. result = Type.LEFT_TOP
  53. else:
  54. if dx < 0 and dy < 0:
  55. if (self.isClockwise(direction)):
  56. result = Type.LEFT_BOTTOM
  57. else:
  58. result = Type.RIGHT_TOP
  59. else:
  60. if dx > 0 and dy < 0:
  61. if (self.isClockwise(direction)):
  62. result = Type.LEFT_TOP
  63. else:
  64. result = Type.RIGHT_BOTTOM
  65.  
  66. print result
  67.  
  68. return result
  69.  
  70. def getPointsXAndY(self, points, index):
  71. startIndex = index * 2
  72.  
  73. return [points[startIndex], points[startIndex + 1]]
  74.  
  75. def isClockwise(self, direction):
  76. return direction == Direction.CLOCKWISE
  77.  
  78. def isCircle(self, points, direction):
  79. result = False
  80. shape = self.circleShape
  81. STEP = 5
  82. index = 0
  83. startIndex = 0
  84.  
  85. detected = [None] * 4
  86.  
  87. currentPoint = self.getPointsXAndY(points, 0)
  88. currentType = None
  89.  
  90. if direction != Direction.UNDEFINED:
  91. for x in range(STEP, len(points), STEP):
  92. try:
  93. nextPoint = self.getPointsXAndY(points, x)
  94. except:
  95. break
  96.  
  97. dx = nextPoint[0] - currentPoint[0]
  98. dy = -(nextPoint[1] - currentPoint[1])
  99.  
  100. if dx == 0 or dy == 0:
  101. continue
  102.  
  103. newType = self.getType(dx, dy, direction)
  104.  
  105. # get the first item, or if the direction has changed
  106. if currentType is None or currentType != newType:
  107. # if we have at least the first direction, check if the next direction is the next step in the circle drawing process
  108. if currentType is not None:
  109. if newType != shape[index]:
  110. break
  111.  
  112. # a circle is a distionct set of drawing directions, so we need to follow them in order, from where the user started drawing
  113. else:
  114. index = shape.index(newType)
  115. startIndex = index
  116.  
  117. self.bounds.insert(index, currentPoint)
  118.  
  119. # don't double up on actions, because we need to compare lists
  120. if detected.count(newType) == 0:
  121. print index
  122. #detected.insert(index, newType)
  123. detected[index] = newType
  124.  
  125. # because we may start anywhere in the shape, we may need to do circular traversal of the shape array
  126. if direction == Direction.CLOCKWISE:
  127. index = (index + 1) % len(shape)
  128. else:
  129. index = (index - 1) % len(shape)
  130.  
  131. currentType = newType
  132. currentPoint = nextPoint
  133.  
  134. if shape == detected:
  135. result = True
  136. break
  137.  
  138. return result
  139.  
  140.  
  141. """def reverseDirection(self, direction):
  142. result = None
  143.  
  144. if direction == Direction.UNDEFINED:
  145. result = direction
  146. else:
  147. if direction == Direction.CLOCKWISE:
  148. result = Direction.COUNTER_CLOCKWISE
  149. else:
  150. result = Direction.CLOCKWISE
  151.  
  152. return result"""
  153.  
  154. def determineDirection(self, points):
  155. result = Direction.UNDEFINED
  156. total = 0
  157. currentPoint = None
  158. previousPoint = None
  159.  
  160. for i in range(0, len(points)/2, 1):
  161. currentPoint = self.getPointsXAndY(points, i)
  162.  
  163. if previousPoint is not None:
  164. total += (currentPoint[0] - previousPoint[0]) * (currentPoint[1] + previousPoint[1])
  165.  
  166. previousPoint = currentPoint
  167.  
  168. if total > 0:
  169. result = Direction.CLOCKWISE
  170. else:
  171. if total < 0:
  172. result = Direction.COUNTER_CLOCKWISE
  173.  
  174. return result
  175.  
  176. def on_touch_down(self, touch):
  177. editing = True
  178. color = (random(), 1., 1.)
  179.  
  180. with self.canvas:
  181. Color(*color, mode='hsv')
  182. touch.ud['line'] = Line(points=(touch.x, touch.y))
  183.  
  184. def on_touch_move(self, touch):
  185. touch.ud['line'].points += [touch.x, touch.y]
  186.  
  187. def on_touch_up(self, touch):
  188. editing = False
  189. direction = None
  190.  
  191. if len(touch.ud['line'].points) > 0:
  192. points = touch.ud['line'].points
  193.  
  194. if self.isCircle(points, self.determineDirection(points)):
  195. print "circle"
  196.  
  197. with self.canvas:
  198. #Ellipse(pos=(self.bounds[0]), size=(50, 50))
  199. Vertice(pos=self.bounds[0])
  200. else:
  201. print "unknown"
  202.  
  203.  
  204. class MindMapApp(App):
  205.  
  206. def build(self):
  207. parent = Widget()
  208. painter = MindMapWidget()
  209. clearbtn = Button(text='Clear')
  210. parent.add_widget(painter)
  211. parent.add_widget(clearbtn)
  212.  
  213. def clear_canvas(obj):
  214. painter.canvas.clear()
  215.  
  216. clearbtn.bind(on_release=clear_canvas)
  217.  
  218. Config.set('input','multitouchscreen1','tuio,192.168.0.4:3333')
  219. return parent
  220.  
  221.  
  222. if __name__ == '__main__':
  223. MindMapApp().run()
  224.  
  225. #:kivy 1.8
  226.  
  227. <Vertice>:
  228. size: 50, 50
  229. canvas:
  230. Ellipse:
  231. pos: self.pos
  232. size: self.size
  233.  
  234. print self.size
  235.  
  236. [100,100]
  237.  
  238. [DEBUG ] [App ] Loading kv <./mindmap.kv>
  239.  
  240. if __name__ == '__main__':
  241. MindMapApp().run()
  242.  
  243. class Vertice(Widget):
  244. def __init__(self, pos=None):
  245. super(Vertice, self).__init__()
  246.  
  247. self.pos = pos
  248. #print self.pos
  249. print self.size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement