Advertisement
here2share

# modeswitch.py

Nov 29th, 2014
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # modeswitch.py
  2.  
  3. import e32, audio, appuifw, graphics, os, sys, key_codes, time, math, random
  4.  
  5. app_lock=e32.Ao_lock()
  6. appuifw.app.orientation='landscape'
  7. appuifw.app.directional_pad=False
  8. appuifw.app.screen='large' # 'large' for testing
  9. appuifw.app.title=u"Text and Graphic Mode Toggle"
  10.  
  11. btn_t,btn_b=270,360
  12. btn01=(20,btn_t,120,btn_b) # YES
  13. btn02=(120,btn_t,220,btn_b) # NO
  14. btn03=(520,0,640,69) # quit button
  15.  
  16. def e_callback(event):
  17.     global select,btn_t
  18.     if event['type'] == key_codes.EButton1Down:
  19.         if event['pos'][1] > btn_t:
  20.             if event['pos'][0] > 20 and event['pos'][0] < 120:
  21.                 select=0 # means NO
  22.             elif event['pos'][0] > 120 and event['pos'][0] < 220:
  23.                 select=1 # means YES
  24.         elif event['pos'][0] > 520 and event['pos'][1] < 69:
  25.             quit()
  26.  
  27. def quit():
  28.     gfx_on(0)
  29. appuifw.app.exit_key_handler=quit
  30.  
  31. canvas=img=None
  32.  
  33. def cb_resize(aSize=(0,0,0,0)):
  34.     global img
  35.     img=graphics.Image.new((640,360)) ### ZZZ to be canvas.size instead of 640:360
  36.  
  37. def handle_redraw(rect):
  38.     if img: ### Skips only at first pass as "None"
  39.         canvas.blit(img)
  40. canvas=appuifw.Canvas(resize_callback=cb_resize,
  41.         event_callback=e_callback,redraw_callback=handle_redraw)
  42. appuifw.app.body=canvas
  43. txt=appuifw.Text()
  44. appuifw.app.body=txt
  45.  
  46. txt.clear()
  47. txt.set(u"'Select Option' > 'Graphic Mode' to activate the graphics mode \
  48. and then the green rectangle at the top left should switch it right back to \
  49. this unaltered 'Text Mode'\n\n")
  50.  
  51. COLOR_BLACK=(0,0,0)
  52. COLOR_WHITE=(255,255,255)
  53. COLOR_GRAYLIGHT=(211,211,211)
  54. COLOR_GRAY=(128,128,128)
  55. COLOR_GRAYDARK=(169,169,169)
  56. COLOR_REDDARK=(169,0,0)
  57. COLOR_RED=(255,0,0)
  58. COLOR_ORANGE=(255,165,0)
  59. COLOR_YELLOW=(255,255,0)
  60. COLOR_GREEN=(0,128,0)
  61. COLOR_BLUE=(0,0,255)
  62. COLOR_CYAN=(0,255,255)
  63. COLOR_PURPLE=(128,0,128)
  64. COLOR_DARKBLUE=(0,0,139)
  65. COLOR_DARKGREEN=(0,100,0)
  66. COLOR_DEEPPINK=(255,20,147)
  67. COLOR_PINK=(255,192,203)
  68. COLOR_INDIGO=(75,0,130)
  69. COLOR_LIGHTBLUE=(173,216,230)
  70. COLOR_LIME=(0,255,0)
  71. COLOR_OLIVE=(107,142,35)
  72.  
  73. COLOR_TAN=(210,180,140)
  74. COLOR_BROWN=(139,69,19)
  75. COLOR_GOLD=(255,215,0)
  76. COLOR_SILVER=(192,192,192)
  77.  
  78. def circle(x,y,radius=5,outline=0,fill=0xffff00,width=1):
  79.     c.ellipse((x-radius,y-radius,x+radius,y+radius),outline,fill,width)
  80.  
  81. PYTHON_ROOT=u"e:\\data\\Python\\000misc\\"
  82. BkgdImage=graphics.Image.open(PYTHON_ROOT+u"640x360img.png")
  83.  
  84. def gfx_on(mode):
  85.     if mode == 1:
  86.         appuifw.app.screen='full'
  87.         appuifw.app.body=canvas
  88.     if mode == 0:
  89.         appuifw.app.screen='large'
  90.         appuifw.app.body=txt
  91.  
  92. def star5pt((x,y),size,outline=None,fill=None,width=1):
  93.     s=size
  94.     img.polygon(((0+x,0.7*s+y),(0.75*s+x,0.7*s+y),(1*s+x,0+y),(1.25*s+x,0.7*s+y),(2*s+x,0.7*s+y),
  95.             (1.4*s+x,1.15*s+y),(1.65*s+x,1.95*s+y),(1*s+x,1.45*s+y),(0.35*s+x,1.95*s+y),(0.6*s+x,1.15*s+y)),
  96.             outline,fill,width)
  97.  
  98. def exit():
  99.     app_lock.signal()
  100. def graphics_mode():
  101.     gfx_on(1)
  102.     img.blit(BkgdImage,target=(0,0))
  103.     img.rectangle((btn03),outline=None,fill=COLOR_GREEN)
  104.     img.line((250,250,480,320),COLOR_PURPLE,width=5)
  105.     star5pt((390,5),60,fill=COLOR_GOLD)
  106.     img.point((140,50),COLOR_BLACK,width=80)
  107.     img.ellipse((520,210,620,340),outline=COLOR_DARKBLUE,fill=COLOR_CYAN,width=2)
  108.     img.line(((40,275),(200,152),(174,259),(320,80),(374,240),(400,152),(569,274)),
  109.             outline=COLOR_ORANGE,width=2)
  110.     img.pieslice((200,20,320,80),0,320,width=1,outline=COLOR_BROWN,fill=COLOR_TAN)
  111.     img.arc((60,180,180,300),320,90,width=10,outline=COLOR_DARKBLUE) # ??? *
  112.     ### * re: arc/pieslice -- for bypassing the zero degree mark... 90 degrees as to what?
  113.     ### may have to write a method to correct this
  114.     img.text((100,320),u"Sample Text On Canvas",fill=(255,0,0),font='title')
  115.     img.text((536,30),u"Touch Here")
  116.     img.text((536,50),u"To Switch")
  117.     handle_redraw(())
  118. appuifw.app.menu=[(u"Graphic On",graphics_mode),(u"Exit",exit)]
  119.  
  120. app_lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement