Advertisement
here2share

# rgbtouch2move.py

Nov 29th, 2014
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # rgbtouch2move.py
  2.  
  3. import appuifw, graphics, key_codes, math
  4.  
  5. r_dot=(100,100)
  6. g_dot=(200,200)
  7. b_dot=(300,300)
  8. cr=(255,0,0)
  9. cg=(0,255,0)
  10. cb=(0,0,255)
  11. overlap=''
  12. rgb_pt=0
  13. layer=[(r_dot, cr), (g_dot, cg), (b_dot, cb)]
  14. vr=60
  15.  
  16. def z_draw(rect):
  17.     global img
  18.     img=graphics.Image.new(canvas.size)
  19.     img.clear(0x000000)
  20.     img.ellipse(((layer[2][0][0]-vr,layer[2][0][1]-vr),(layer[2][0][0]+vr,layer[2][0][1]+vr)),layer[2][1],layer[2][1])
  21.     img.ellipse(((layer[1][0][0]-vr,layer[1][0][1]-vr),(layer[1][0][0]+vr,layer[1][0][1]+vr)),layer[1][1],layer[1][1])
  22.     img.ellipse(((layer[0][0][0]-vr,layer[0][0][1]-vr),(layer[0][0][0]+vr,layer[0][0][1]+vr)),layer[0][1],layer[0][1])
  23.     img.text( (20,30),'X:'+unicode(r_dot[0])+' Y:'+unicode(r_dot[1]),(240,0,0))
  24.     img.text((130,30),'X:'+unicode(g_dot[0])+' Y:'+unicode(g_dot[1]),(0,240,0))
  25.     img.text((240,30),'X:'+unicode(b_dot[0])+' Y:'+unicode(b_dot[1]),(0,0,240))
  26.     img.text( (20,50),unicode(overlap),(0xFFFFFF))
  27.     canvas.blit(img,(0,0))
  28.  
  29. def backlight():
  30.     appuifw.e32.reset_inactivity()
  31.     timer.after(20,backlight)
  32.  
  33. def z_xy(pos):
  34.     global r_dot, g_dot, b_dot, prev_xy, overlap, layer
  35.     if rgb_pt==1:
  36.         prev_xy=r_dot
  37.         layer.remove((r_dot, cr))
  38.         r_dot=pos
  39.         layer.insert(0,(pos, cr))
  40.     if rgb_pt==2:
  41.         prev_xy=g_dot
  42.         layer.remove((g_dot, cg))
  43.         g_dot=pos
  44.         layer.insert(0,(pos, cg))
  45.     if rgb_pt==3:
  46.         prev_xy=b_dot
  47.         layer.remove((b_dot, cb))
  48.         b_dot=pos
  49.         layer.insert(0,(pos, cb))
  50.     objects = [circle("R", r_dot[0], r_dot[1], vr), circle("G", g_dot[0], g_dot[1], vr), circle("B", b_dot[0], b_dot[1], vr)]
  51.     rm_objects = objects
  52.     objx=""
  53.     overlap=""
  54.     for obj in objects:
  55.         for obj2 in rm_objects:
  56.             if obj <> obj2:
  57.                 if obj.isColliding(obj2):
  58.                     objx=objx+unicode('  '+obj.name+":"+obj2.name)
  59.                     overlap='Overlap Detected:'+objx
  60.         rm_objects.pop(0)
  61.     z_draw(canvas.size)
  62.  
  63. def z_dot(pos):
  64.     global r_dot, g_dot, b_dot, rgb_pt, layer
  65.     if img.getpixel(pos)[0]==cr:
  66.         rgb_pt=1
  67.     elif img.getpixel(pos)[0]==cg:
  68.         rgb_pt=2
  69.     elif img.getpixel(pos)[0]==cb:
  70.         rgb_pt=3
  71.     elif pos[1]<30:
  72.         if pos[0]<130:
  73.             layer.remove((r_dot, cr))
  74.             layer.insert(0,(r_dot, cr))
  75.         elif pos[0]>130 and pos[0]<240:
  76.             layer.remove((g_dot, cg))
  77.             layer.insert(0,(g_dot, cg))
  78.         elif pos[0]>240:
  79.             layer.remove((b_dot, cb))
  80.             layer.insert(0,(b_dot, cb))
  81.     z_draw(canvas.size)
  82.  
  83.  
  84. def z_notouch(pos):
  85.     global rgb_pt
  86.     z_xy(pos)
  87.     rgb_pt=0
  88.  
  89. # This is the generic circle class.
  90. class circle:
  91.   # Initialize with a name, the position in cartesian coordinates x, y and its radius
  92.   def __init__(self, name, x, y, radius):
  93.     self.name = name
  94.     self.x = x
  95.     self.y = y
  96.     self.radius = radius
  97.  
  98.   def isColliding(self, obj2):
  99.     distance = self.distanceTo(obj2)
  100.     if distance < (self.radius + obj2.radius):
  101.       return True
  102.     return False
  103.  
  104.   def distanceTo(self, obj2):
  105.     dx = abs(obj2.x - self.x)
  106.     dy = abs(obj2.y - self.y)
  107.     return ( math.sqrt( pow(dx, 2) + pow(dy, 2) ) )
  108.  
  109. def exit():
  110.     app_lock.signal()
  111.  
  112. appuifw.app.directional_pad=False
  113. canvas=appuifw.Canvas(redraw_callback=z_draw)
  114. appuifw.app.body=canvas
  115. appuifw.app.exit_key_handler=exit
  116. canvas.bind(key_codes.EButton1Up,z_notouch)
  117. canvas.bind(key_codes.EButton1Down,z_dot)
  118. canvas.bind(key_codes.EDrag,z_xy)
  119.  
  120. timer=appuifw.e32.Ao_timer()
  121. timer.after(1,backlight)
  122.  
  123. app_lock=appuifw.e32.Ao_lock()
  124. app_lock.wait()
  125. timer.cancel()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement