Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # rgbtouch2move.py
- import appuifw, graphics, key_codes, math
- r_dot=(100,100)
- g_dot=(200,200)
- b_dot=(300,300)
- cr=(255,0,0)
- cg=(0,255,0)
- cb=(0,0,255)
- overlap=''
- rgb_pt=0
- layer=[(r_dot, cr), (g_dot, cg), (b_dot, cb)]
- vr=60
- def z_draw(rect):
- global img
- img=graphics.Image.new(canvas.size)
- img.clear(0x000000)
- 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])
- 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])
- 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])
- img.text( (20,30),'X:'+unicode(r_dot[0])+' Y:'+unicode(r_dot[1]),(240,0,0))
- img.text((130,30),'X:'+unicode(g_dot[0])+' Y:'+unicode(g_dot[1]),(0,240,0))
- img.text((240,30),'X:'+unicode(b_dot[0])+' Y:'+unicode(b_dot[1]),(0,0,240))
- img.text( (20,50),unicode(overlap),(0xFFFFFF))
- canvas.blit(img,(0,0))
- def backlight():
- appuifw.e32.reset_inactivity()
- timer.after(20,backlight)
- def z_xy(pos):
- global r_dot, g_dot, b_dot, prev_xy, overlap, layer
- if rgb_pt==1:
- prev_xy=r_dot
- layer.remove((r_dot, cr))
- r_dot=pos
- layer.insert(0,(pos, cr))
- if rgb_pt==2:
- prev_xy=g_dot
- layer.remove((g_dot, cg))
- g_dot=pos
- layer.insert(0,(pos, cg))
- if rgb_pt==3:
- prev_xy=b_dot
- layer.remove((b_dot, cb))
- b_dot=pos
- layer.insert(0,(pos, cb))
- 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)]
- rm_objects = objects
- objx=""
- overlap=""
- for obj in objects:
- for obj2 in rm_objects:
- if obj <> obj2:
- if obj.isColliding(obj2):
- objx=objx+unicode(' '+obj.name+":"+obj2.name)
- overlap='Overlap Detected:'+objx
- rm_objects.pop(0)
- z_draw(canvas.size)
- def z_dot(pos):
- global r_dot, g_dot, b_dot, rgb_pt, layer
- if img.getpixel(pos)[0]==cr:
- rgb_pt=1
- elif img.getpixel(pos)[0]==cg:
- rgb_pt=2
- elif img.getpixel(pos)[0]==cb:
- rgb_pt=3
- elif pos[1]<30:
- if pos[0]<130:
- layer.remove((r_dot, cr))
- layer.insert(0,(r_dot, cr))
- elif pos[0]>130 and pos[0]<240:
- layer.remove((g_dot, cg))
- layer.insert(0,(g_dot, cg))
- elif pos[0]>240:
- layer.remove((b_dot, cb))
- layer.insert(0,(b_dot, cb))
- z_draw(canvas.size)
- def z_notouch(pos):
- global rgb_pt
- z_xy(pos)
- rgb_pt=0
- # This is the generic circle class.
- class circle:
- # Initialize with a name, the position in cartesian coordinates x, y and its radius
- def __init__(self, name, x, y, radius):
- self.name = name
- self.x = x
- self.y = y
- self.radius = radius
- def isColliding(self, obj2):
- distance = self.distanceTo(obj2)
- if distance < (self.radius + obj2.radius):
- return True
- return False
- def distanceTo(self, obj2):
- dx = abs(obj2.x - self.x)
- dy = abs(obj2.y - self.y)
- return ( math.sqrt( pow(dx, 2) + pow(dy, 2) ) )
- def exit():
- app_lock.signal()
- appuifw.app.directional_pad=False
- canvas=appuifw.Canvas(redraw_callback=z_draw)
- appuifw.app.body=canvas
- appuifw.app.exit_key_handler=exit
- canvas.bind(key_codes.EButton1Up,z_notouch)
- canvas.bind(key_codes.EButton1Down,z_dot)
- canvas.bind(key_codes.EDrag,z_xy)
- timer=appuifw.e32.Ao_timer()
- timer.after(1,backlight)
- app_lock=appuifw.e32.Ao_lock()
- app_lock.wait()
- timer.cancel()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement