Advertisement
here2share

# 4x6_full_buttonlayout_demo.py

Jul 15th, 2017
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 4x6_full_buttonlayout_demo.py
  2.  
  3. from appuifw import *
  4. import e32,audio,os,sys,key_codes,time,math,random,graphics,sensor
  5.  
  6. RGB_GREEN        = (000,255,000)
  7. RGB_RED          = (255,000,000)
  8. RGB_BLACK        = (000,000,000)
  9. RGB_WHITE        = (255,255,255)
  10.  
  11. def draw(rect):
  12.     try: canvas.blit(img)
  13.     except: pass
  14.  
  15. app.directional_pad = False
  16. app.orientation='portrait'
  17. app.screen='large'
  18. app.body=canvas=Canvas(redraw_callback=draw)
  19. img=graphics.Image.new(canvas.size)
  20.  
  21. Total_x, Total_y = canvas.size
  22. x=y=90
  23. yy = Total_y-(y*6) # for bottom layout
  24.  
  25. def cv(): pass
  26.  
  27. keyb={}
  28. keyb['num']='''
  29. 123+
  30. 456-
  31. 789*
  32. .0=/
  33. $,:%
  34. ()<>
  35. '''
  36.  
  37. def layout(v):
  38.     z=keyb[v].strip().splitlines()
  39.     for y in range(len(z)):
  40.         for x in range(len(z[y])):
  41.             keyb[(v,x,y)]=z[y][x]
  42.  
  43. s=list(keyb)
  44. for z in s: layout(z)
  45.  
  46. def btn_pad(_x,_y,z,c):
  47.     img.text((_x+15,_y+20+y), unicode(z), fill=c,font=(u'Nokia Hindi S60',80,STYLE_BOLD))
  48.  
  49. xy = {}
  50. for _y in range(6):
  51.     for _x in range(4):
  52.         xy[(_x,_y)] = x*_x,  y*_y
  53.  
  54. for x2,y2 in xy:
  55.     btn_pad(x2*90,y2*90,(keyb[('num',x2,y2)]),RGB_BLACK)
  56. draw(())
  57. timer = e32.Ao_timer()
  58. timer.after(0.2) # delay might be req'd for capture
  59. num_pad = graphics.screenshot()
  60. #
  61. def up_event(event):
  62.     img.blit(num_pad, target=(0,0))
  63.     rbtn(RGB_RED)
  64.     draw(())
  65. def dn_event(event):
  66.     global prev_xy
  67.     prev_xy=time.time()
  68.     mv_event(event)
  69. def mv_event(event):
  70.     global Xp,Yp
  71.     x,y=event
  72.     if y > yy: Xp,Yp=x/90,(y-yy)/90
  73. #
  74. canvas.bind(key_codes.EButton1Up, up_event, ((x*0,y*0), (Total_x,Total_y)))
  75. canvas.bind(key_codes.EButton1Down, dn_event, ((x*0,y*0), (Total_x,Total_y)))
  76. canvas.bind(key_codes.EDrag, mv_event, ((x*0,y*0), (Total_x,Total_y)))
  77.  
  78. def exit():
  79.     global process
  80.     process = 0
  81. #
  82. app.exit_key_handler=exit
  83. app_lock=e32.Ao_lock()
  84. Xp=Yp=None
  85. prev_xy=None
  86.  
  87. def btxt(txt):
  88.     img.text((10,Total_y-15),unicode(txt),font=('normal',20),fill=0)
  89.  
  90. def rbtn(c):
  91.     x2,y2=xy[(Xp,Yp)]
  92.     img.rectangle((x2,y2+yy,x2+x,y2+y+yy), fill=c)
  93.     img.text((10,25),unicode((Xp,Yp)),font=('normal',20),fill=0)
  94.     btn_pad(x2,y2,keyb[('num',Xp,Yp)],RGB_WHITE)
  95.    
  96. process = 1
  97. delay = time.time()+0.02
  98. while process:
  99.     if (Xp,Yp) in xy:
  100.         if (Xp,Yp) <> prev_xy:
  101.             prev_xy=Xp,Yp
  102.             if time.time() > delay:
  103.                 #img.clear()
  104.                 img.blit(num_pad, target=(0,0))
  105.                 rbtn(RGB_GREEN)
  106.                 delay = time.time()+0.2
  107.                 draw(())
  108.     e32.ao_yield() # bottom of _running_loop_
  109. """ """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement