Advertisement
here2share

### pys60demos.py ^ 2016.07

Mar 8th, 2016
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### pys60demos.py ^ 2016 07
  2.  
  3. try:
  4.     from appuifw import *
  5.     from graphics import *
  6.     from key_codes import *
  7. except: pass
  8. from random import *
  9.  
  10. s60mX='appuifw e32 sensor key_codes os sys re time math random graphics audio urllib urllib2 base64 copy tempfile'.split()
  11. def z_import(ii):
  12.     s60m=''
  13.     for i in ii:
  14.         s60m+='\nimport '+i
  15.     return s60m
  16. exec z_import(s60mX)
  17.  
  18. lg_scr = 576
  19.  
  20. img=None # temp reference
  21.  
  22. def scr(msg):
  23.     txt.set(unicode(msg))
  24.  
  25. z_Colors =  ((u'RGB_BLACK',(0,0,0)),
  26.             (u'RGB_WHITE',(255,255,255)),
  27.             (u'RGB_GRAYLIGHT',(211,211,211)),
  28.             (u'RGB_GRAY',(128,128,128)),
  29.             (u'RGB_GRAYDARK',(169,169,169)),
  30.             (u'RGB_RED',(255,0,0)),
  31.             (u'RGB_ORANGE',(255,165,0)),
  32.             (u'RGB_YELLOW',(255,255,0)),
  33.             (u'RGB_GREEN',(0,128,0)),
  34.             (u'RGB_BLUE',(0,0,255)),
  35.             (u'RGB_CYAN',(0,255,255)),
  36.             (u'RGB_PURPLE',(128,0,255)),
  37.  
  38.             (u'RGB_PINK',(255,148,238)),
  39.             (u'RGB_DARKBLUE',(0,0,139)),
  40.             (u'RGB_DARKGREEN',(0,100,0)),
  41.             (u'RGB_DEEPPINK',(255,20,147)),
  42.             (u'RGB_INDIGO',(75,0,130)),
  43.             (u'RGB_LIGHTBLUE',(173,216,230)),
  44.             (u'RGB_LIME',(0,255,0)),
  45.             (u'RGB_OLIVE',(107,142,35)),
  46.  
  47.             (u'RGB_BROWN',(128,64,0)),
  48.             (u'RGB_GOLD',(255,215,0)),
  49.             (u'RGB_SILVER',(192,192,192)))
  50. #
  51. rgb_menu="rgb_menu=["
  52. for rgb in z_Colors:
  53.     i,ii=rgb[0].replace('RGB_',''),str(rgb[1])
  54.     rgb_menu += "(u'"+i+"',c_"+i+"),"
  55.     exec 'def c_'+i+'(): cv.color='+ii
  56.     exec rgb[0]+'='+ii
  57. rgb_menu += ']'
  58. def z_bg():
  59.     cv.bg=cv.color
  60.     cv.t=time.time()+0.5
  61.     cv.color=RGB_WHITE
  62.     if (cv.bg[0] > 180 or cv.bg[1] > 148 or cv.bg[2] > 180) and sum(cv.bg) > 250: cv.color=RGB_BLACK
  63.     img.clear(cv.bg)
  64. rainbow=[]
  65. def z(r,g,b):
  66.     rainbow.append((r,g,b))
  67. r,g,b=255,0,0
  68. for g in range(256):
  69.     z(r,g,b)
  70. for r in range(254, -1, -1):
  71.     z(r,g,b)
  72. for b in range(256):
  73.     z(r,g,b)
  74. for g in range(254, -1, -1):
  75.     z(r,g,b)
  76. for r in range(256):
  77.     z(r,g,b)
  78. for b in range(254, -1, -1):
  79.     z(r,g,b)
  80. max_rgb=len(rainbow)-1
  81. #
  82. alnu=u'one two three four five'.split()
  83.  
  84. def dts(x=0):
  85.     import datetime
  86.     dt=datetime.datetime.now()
  87.     ampm=' am'
  88.     if dt.hour > 11 and dt.hour != 24: ampm=' pm'
  89.     d=dt.hour%12, dt.minute, dt.second
  90.     d="%02d:%02d:%02d" % (d)
  91.     if x:
  92.         d2= dt.strftime("%A %d %B %Y ")
  93.         if x is 2:
  94.             d2 += d+ampm
  95.         elif x is 3:
  96.             d2=d2.split()[:-1] + d.split(':') + [ampm]
  97.         return  d2
  98.     return  d+ampm
  99. #
  100.  
  101. mask_z="""
  102. def automask(im):
  103.     width, height=im.size
  104.     mask=Image.new(im.size, '1') # black and white
  105.     tran=im.getpixel((0,0))[0] # transparent top-left
  106.     for y in range(height):
  107.         line=im.getpixel([(x, y) for x in range(width)])
  108.         for x in range(width):
  109.             if line[x] == tran:
  110.                 mask.point((x,y), 0) # mask on the point
  111.     return mask
  112.  
  113. #Converts the specified "area" of the image into an image mask
  114. #Transparent color is the color found in the upper left pixel
  115. def automaskArea(im, area):
  116.     width=area[2]-area[0]
  117.     height=area[3]-area[1]
  118.     startX=area[0]
  119.     startY=area[1]
  120.  
  121.     mask=Image.new((width, height), '1') # black and white
  122.     tran=im.getpixel((startX,startY))[0] # transparent top-left
  123.        
  124.     for y in range(height):
  125.         for x in range(width):
  126.             if im.getpixel((startX+x, startY+y))[0] == tran:
  127.                 mask.point((x,y),0)
  128.     return mask
  129. """
  130.  
  131. def z_text():
  132.     msg=appuifw.query(u'Enter text','text' )
  133.     scr(msg)
  134. def z_number():
  135.     msg=appuifw.query(u'Enter number','number' )
  136.     scr(msg)
  137. def z_date():
  138.     msg=appuifw.query(u'Enter date','date' )
  139.     scr(msg)
  140. def z_time():
  141.     msg=appuifw.query(u'Enter time','time' )
  142.     scr(msg)
  143. def z_code():
  144.     msg=appuifw.query(u'Enter code','code' )
  145.     scr(msg)
  146. def z_multiquery(): # accept only 2 entries at a time
  147.     try:
  148.         first_name,last_name=appuifw.multi_query(u'Enter first name',u'Enter last name')
  149.         scr('Hello, %s %s.\nThis is a demo.' %(first_name,last_name))
  150.     except:
  151.         pass
  152. def z_query():
  153.     msg=appuifw.query(u'query','query' )
  154.     scr(msg)
  155. def z_popup():
  156.     items=[u'item 1',u'item 2',u'item 3',u'item 4',u'item 5',u'item 6',u'item 7',u'item 8',u'item 9']
  157.     index=appuifw.popup_menu(items,u'*** Select Item ***')
  158.     scr('popup '+items[index]+' was selected')
  159. def note_info(): # type: info
  160.     # create a pop-up note: appuifw.note(label,type)
  161.     appuifw.note(u'information note','info')
  162. def note_error(): # type: error  
  163.     appuifw.note(u'error note','error')
  164. def z_checkbox(): # note: when none is checked,the highlighted is then selected
  165.     items=[u'item 1',u'item 2',u'item 3',u'item 4',u'item 5']
  166.     chosen=appuifw.multi_selection_list(items,style='checkbox',search_field=0)
  167.     chosen=sorted(chosen)
  168.     z_selected(chosen,items)
  169. def z_checkmark(): # note: when none is checked, the highlighted is then selected
  170.     items=[u'item 1',u'item 2',u'item 3',u'item 4',u'item 5']
  171.     chosen=appuifw.multi_selection_list(items,style='checkmark',search_field=1)
  172.     chosen=sorted(chosen)
  173.     z_selected(chosen,items)
  174. def z_selected(chosen,items):
  175.     selected=u'Selected: --'
  176.     for i in chosen:
  177.         selected=selected+'\n\t\t'+items[i]
  178.     txt.set(selected)
  179. def z_regex():
  180.     print
  181.     print
  182.     print "\nCommon image references--"
  183.     imgReg = '''["']([a-zA-Z0-9_$]+\.(?:jpe?g|gif|png|bmp))["']'''
  184.     print imgReg
  185.     print
  186.  
  187.     def okay(test):
  188.         print test, '=',
  189.         print re.findall(imgReg, test, re.IGNORECASE)
  190.     okay('''abc="jpg''')
  191.     okay('''abc=".jpg"''')
  192.     okay('''abc="2.jpg"''')
  193.     okay('''abc="_.jpg"''')
  194.     okay('''abc="$.jpg"''')
  195.     okay('''abc="*.jpg"''')
  196.     okay('''abc="+.jpg"''')
  197.     okay('''abc="/.jpg"''')
  198.     okay('''abc='test.test.png"''')
  199.     okay('''abc="test.jpg"''')
  200.     okay('''abc="test.jpeg"''')
  201.     okay('''abc="test.JPEG"''')
  202.     okay('''abc="TEST.gif"''')
  203.     okay('''abc="test.png"''')
  204.     okay('''abc="test.xjpg"''')
  205.     okay('''abc="test.jpgx"''')
  206.     okay('''abc="test..jpg"''')
  207. def z_dl():
  208.     URL = "http://www.python.org/images/python-logo.gif"
  209.    
  210.     if appuifw.query(u'Might take quite a long while to download the 2.48kb image','query' ): ### ???
  211.         dest_file = u"E:\\Images\\python-logo.gif"
  212.         print
  213.         print "Downloading Image...",
  214.         urllib.urlretrieve(URL, dest_file)
  215.         appuifw.note(u'Opening Viewer To Showcase Image Downloaded','info')
  216.         print "Completed!"
  217.         viewer = appuifw.Content_handler()
  218.         viewer.open(dest_file)
  219. def z_savedata():
  220.     dir_x = u'E:\\'
  221.     folder = u'Python\\demos\\'
  222.     try: os.mkdir(dir_x+folder)
  223.     except: pass
  224.     filename = 'demofile.txt'
  225.     fullpath = dir_x + folder + filename
  226.  
  227.     print fullpath
  228.  
  229.     # Open the file for writing -- Note if to append the data... change the 'w' to 'a'
  230.     #if not os.path.isfile(fullpath):
  231.     f=open(fullpath,'wb')
  232.     f.write("Python is a great language!\n\n-- End of Demo --\n")
  233.     f.close
  234.  
  235.     # Open the file for reading
  236.     f = open(fullpath, 'rb')
  237.  
  238.     # Read the file
  239.     db_x = f.read()
  240.     print db_x
  241.  
  242.     # Close the output file... as for good measure
  243.     f.close()
  244. #
  245.  
  246. newtabs=alnu
  247. def z_tabexit():
  248.     app.set_tabs([],None)
  249.     z_mainmenu()
  250. def z_5tabs(): # Note: Substitute changes were made since the tabs did not work properly for the Nokia 500
  251.     # normalize screen
  252.     app.screen='normal'
  253.    
  254.     app.menu=[(u'End tab demo',z_tabexit)]
  255.     # create a function that handles switching between tabs
  256.     def tab_handler(i):
  257.         global newtabs
  258.         i=alnu.index(newtabs[i])
  259.         newtabs=alnu[i:]+[alnu[0]]
  260.         app.set_tabs(newtabs,tab_handler)
  261.         # set the tabs
  262.  
  263.         # switch to the tab according to index
  264.         if(i==0):
  265.             demo='This is tab #1'
  266.         elif(i==1):
  267.             demo='This is the second tab'
  268.         elif(i==2):
  269.             demo='Now tab #3'
  270.         elif(i==3):
  271.             demo='Showing fourth tab'
  272.         else:
  273.             demo='*** Finally, tab 5 ***'
  274.  
  275.         app.body=appuifw.Text(unicode(demo))
  276.     # open the first tab
  277.     tab_handler(0)
  278. #
  279. def z_submenu_demo():
  280.     # create the application menu including submenus
  281.     msg=[(u'item 1',z_item1),(u'item 2',z_item2),
  282.         (u'Submenu 1',((u'sub item 1',z_subitem1),(u'sub item 2',z_subitem2),(u'sub item 3',z_subitem3))),
  283.         g_menu]
  284.     scr('Submenu Demo')
  285.     app.menu=msg
  286.  
  287. def setTabs(self):
  288.     app.set_tabs([u'Main',u'Text',u'List',u'Canvas'],z_tabs)
  289.  
  290. def text_color(COLOR):
  291.     txt.color=COLOR
  292.  
  293. def text_hlight(COLOR):
  294.     txt.highlight_color=COLOR
  295.  
  296. def text_font():
  297.     txt.font=(u'Nokia Hindi S60',25,None)
  298.  
  299. def abc(demo):
  300.     if type(demo) is tuple: demo=demo[0]  
  301.     txt.add(u''+demo+' 0123456789 ABCDEFG\n')
  302.     txt.add(u'abcdefghijklmnopqrstuvwxy\n')
  303.    
  304. def z_text():
  305.     mem_style=txt.style
  306.     mem_color=txt.color
  307.     txt.clear()
  308.     txt.font=("annotation")
  309.     txt.color=(255,0,0)
  310.     abc(txt.font)
  311.     txt.font=("annotation",42,None)
  312.     txt.add(u'annotation @ 42\n')
  313.     txt.font="title"
  314.     txt.color=(0,255,255)
  315.     abc(txt.font)
  316.     txt.font="legend"
  317.     txt.color=(0,255,0)
  318.     abc(txt.font)
  319.     txt.font="dense"
  320.     txt.color=(0,0,255)
  321.     abc(txt.font)
  322.     txt.font="normal"
  323.     txt.color=(0,0,0)
  324.     abc(txt.font)
  325.  
  326.     txt.style=(STYLE_BOLD)
  327.     txt.add(u'STYLE_BOLD\n')
  328.     txt.style=(STYLE_ITALIC)
  329.     txt.add(u'STYLE_ITALIC\n')
  330.     txt.style=(STYLE_UNDERLINE)
  331.     txt.add(u'STYLE_UNDERLINE\n\n')
  332.     txt.style=(STYLE_BOLD|STYLE_ITALIC|STYLE_UNDERLINE)
  333.     txt.add(u'All 3 Styles\n')
  334.     txt.style=mem_style
  335.     txt.color=mem_color
  336.  
  337. styles=(    STYLE_BOLD, STYLE_ITALIC, STYLE_UNDERLINE, STYLE_STRIKETHROUGH,
  338.             HIGHLIGHT_STANDARD, HIGHLIGHT_SHADOW, HIGHLIGHT_ROUNDED )
  339. def style_toggle(a_style): ### ZZZ
  340.     ''' Toggle style (on/off) '''
  341.     if txt.style & a_style:
  342.         txt.style = txt.style ^ a_style
  343.     else:
  344.         txt.style = txt.style | a_style
  345. def no_style(a_style):
  346.     ''' Clear all text style attributes '''
  347.     txt.style = 0
  348.  
  349. '''
  350. set_pos(cursor_pos)
  351.  
  352. len()
  353.  
  354. set(text)
  355.  
  356. delete([pos=0,length=len()])
  357.  
  358. get_pos()
  359.  
  360. get([pos=0,length=len()])
  361. '''
  362.  
  363. def addtext(msg):
  364.     txt.add(unicode(msg))
  365.  
  366. # create the callback functions for the application menu and its submenus
  367. def z_item1():
  368.     scr('item one was selected')
  369. def z_item2():
  370.     scr('item two was selected')
  371. def z_subitem1():
  372.     scr('subitem one was selected')
  373. def z_subitem2():
  374.     scr('subitem two was selected')
  375. def z_subitem3():
  376.     scr('subitem three was selected')
  377.  
  378. def z_find(reverse=False,beginning=False):
  379.     '''find text within a document'''
  380.     string=appuifw.query(u'Find text:','text',unicode(last_find))
  381.     saved_string=string[:]
  382.     if (string != None):
  383.         try:
  384.             text=text.get()
  385.             if settings.config['casesensitive'][0] == 'no':
  386.                 text=text.lower()
  387.                 string=string.lower()
  388.             if beginning:
  389.                 if reverse:
  390.                     cur_pos=text.rindex(string)
  391.                 else:
  392.                     cur_pos=text.index(string)
  393.             else:
  394.                 if reverse:
  395.                     cur_pos=text.rindex(string,0,(text.get_pos() -1))
  396.                 else:
  397.                     cur_pos=text.index(string,(text.get_pos() +1))
  398.             text.set_pos(cur_pos)
  399.         except:
  400.             appuifw.note(u'Search string not found.','info')
  401.             last_find=saved_string
  402.  
  403. def z_replace():
  404.     '''find and replace all instances of a given string'''
  405.     replace=appuifw.multi_query(u'Replace :',u'with : ')
  406.     if replace != None:
  407.         text=text.get()
  408.         cur_pos=text.get_pos()
  409.         text.set(text.replace(replace[0],replace[1]))
  410.         text.set_pos(cur_pos+text[0:cur_pos].incr(replace[0])*(len(replace[1])-len(replace[0])))
  411.         appuifw.note(unicode(text.incr(replace[0]))+' instances replaced')
  412.  
  413. def _rename():
  414.     '''rename a file/folder'''
  415.     path=dir_iter.entry(browse.current())
  416.     filename=unicode(os.path.basename(path))
  417.     if appuifw.query(u'Rename '+filename+' ?','query'):
  418.         newname=appuifw.query(u'Rename '+filename,'text',filename)
  419.         if newname != None:
  420.             try:
  421.                 os.rename(path,os.path.dirname(path)+str(newname))
  422.                 appuifw.note(u'File renamed','info')
  423.             except:
  424.                 appuifw.note(u'Error renaming file!','error')
  425.  
  426. def screen():
  427.     '''change the screen size'''
  428.     current_screen=config['screen']
  429.     config['screen']=appuifw.popup_menu([\
  430.         u'Normal',\
  431.         u'Large',\
  432.         u'Fullscreen',\
  433.     ],u'Screen size:')
  434.     if (config['screen'] == 0):
  435.         app.screen='normal'
  436.         config['screen']='normal'
  437.     elif (config['screen'] == 1):
  438.         app.screen='large'
  439.         config['screen']='large'
  440.     elif (config['screen'] == 2):
  441.         app.screen=full
  442.         config['screen']=full
  443.     else:
  444.         config['screen']=current_screen
  445.  
  446. def z_speech(data):
  447.     if data !='':
  448.         e32.ao_sleep(0.2)
  449.         audio.say(data)
  450. #
  451. def t(): pass
  452. def m(): pass
  453. def delay(var,sec): ### moderate speed ^ if not delay
  454.     code="""if 1:
  455.     try:
  456.         t.@@@=m.@@@-time.time()
  457.     except:
  458.         m.@@@=time.time()+sec
  459.     t.@@@=m.@@@-time.time()
  460.     if int(t.@@@) < 0.000000001: t.@@@=0""".replace('@@@',var)
  461.     exec code
  462.  
  463. def z_betw(x,y,betw):
  464.     if betw == int(betw):
  465.         if betw < 0:
  466.             l,r,t,b=betw,abs(betw),betw,abs(betw)
  467.         else:
  468.             l,r,t,b=0,betw,0,betw
  469.     elif len(betw) == 2:
  470.         l,r,t,b=0,betw[0],0,betw[1]
  471.     else:
  472.         l,r,t,b=betw
  473.     if x < l: x=l
  474.     elif x > r: x=r
  475.     if y < t: y=t
  476.     elif y > b: y=b
  477.     return x,y
  478. #
  479. xy_outline=(-5,5),(-5,-5),(5,-5),(5,5),(0,-5),(0,5),(-5,0),(5,0),(0,0)
  480. def z_outline(x,y, msg,fg=RGB_LIME,bg=RGB_BLACK):
  481.     color=bg
  482.     for xy in xy_outline:
  483.         if xy == (0,0): color=fg
  484.         img.text((x+xy[0],y+xy[1]), unicode(msg),fill=color,font=(u'Nokia Hindi S60',100,STYLE_BOLD))
  485. #
  486. def star_5pt(x,y,s):
  487.     a,b,c,d,e,f,g,h,j,k,m,n,p=0,180,192,250,308,500,340,294,402,476,357,96,157
  488.     plot_xy=[(a,b),(c,b),(d,a),(e,b),(f,b),(g,h),(j,k),(d,m),(n,k),(p,h)]
  489.     nova=[]
  490.     for xy in plot_xy:
  491.         xy=(xy[0]*0.002)*s+x,(xy[1]*0.002)*s+y
  492.         nova.append(xy)
  493.     return nova
  494. #
  495. def btns_med(x=4,y=7,xy=(0,0),sz=(90,90)):
  496.     code='if 1:\n'
  497.     i=0
  498.     if xy is 1: xy=(0,38)
  499.     for y2 in range(y):
  500.         for x2 in range(x):
  501.             xx=str(sz[0]*x2+xy[0])
  502.             yy=str(sz[1]*y2+xy[1])
  503.             n=str(i+1).zfill(2)
  504.             code += '\txy_'+n+'=(('+xx+','+yy+'), ('+xx+'+'+str(sz[0])+','+yy+'+'+str(sz[1])+'))\n'
  505.             code += '\tdef box_'+n+'(event):\n\t\tcv.xy,cv.btn=xy_'+n+','+str(i+1)
  506.             code += '\n\tcanvas.bind(key_codes.EDrag, box_'+n+', ((xy_'+n+')))\n'
  507.     return code
  508. #
  509. def mosaic(s=90):
  510.     for x in range(0,s*4,s):
  511.         for y in range(0,s*7,s):
  512.             img.rectangle((x,y,x+s,y+s),fill=(random.randint(80,255),random.randint(80,255),random.randint(80,255)))
  513. #
  514. def sCapture(scr):
  515.     z=graphics.screenshot()
  516.     if scr[-4:] not in ['.png','.jpg','.gif']: scr += '.png'
  517.     z.save(u"e:\\python\\"+scr)
  518.     z.save(u"e:\\python\\_.png") # should immediately unlock the saved image
  519.     appuifw.note(u'Screenshot Has Been Saved As: '+scr+'\n... Hopefully.','conf')
  520. #
  521. def z_loop():
  522.     for i in range(1, 23):
  523.         print 'for num in range(1, 23) >>> '+str(i)
  524.     print '*** time.sleep(10) # 10 seconds'
  525.     time.sleep(5)
  526.     for i in range(1, 10):
  527.         print 'scrolling...'
  528. def z_dts():
  529.     scr(dts()+'\n'+dts(1)+'\n'+dts(2)+'\n'+str(dts(3)))
  530. #
  531. def z_enum():
  532.     scr('for item in enumerate('+str(alnu)+')\n\n>>>\n')
  533.     for item in enumerate(alnu):
  534.         print(item)
  535. #
  536. def slidebar(lengthof=lg_scr):
  537.     yy2=lengthof/9
  538.     cv.sVy=cv.y/yy2
  539.     img.rectangle(((350,0,358,640)), fill=RGB_BLACK)
  540.     for sVyy in xrange(0,9,2):
  541.         yy=yy2*sVyy+2
  542.         img.rectangle(((350,yy,358,yy+yy2)), fill=RGB_WHITE)
  543. #
  544. def z_tools():
  545.     index=appuifw.popup_menu(tools,u'*** Select Tool ***')
  546.     cv.val=tool_val[index]
  547. #
  548. def pr_menu(z):
  549.     msg=''
  550.     for i in z:
  551.         msg+=str(i[0])+'\n'
  552.     scr(msg)
  553.     app.menu=z
  554. #
  555. def z_textmenu():
  556.     pr_menu(t_menu)
  557. #
  558. def z_textmenu2():
  559.     pr_menu(t2menu)
  560. #
  561. class cv():
  562.     go='+++'
  563.     color=RGB_WHITE
  564.     xyz=(0,0,0)
  565.     x=0
  566.     y=0
  567.     light=0
  568.     evt=None
  569.     tmp=0
  570.     sw=0
  571.     init=1
  572.     padx,pady=0,0
  573.     up=-1
  574. cv=cv()
  575. arrow_points=[( 0.0, -6.0), ( 3.0,  0.0), ( 1.0,  0.0),
  576.                 ( 1.0,  6.0), (-1.0,  6.0), (-1.0,  0.0),
  577.                 (-3.0,  0.0)]
  578. #
  579. def draw(rect):
  580.     if img:
  581.         canvas.blit(img)
  582. #
  583. def e_callback(event):
  584.     x,y=event['pos']
  585.     try: timer.cancel()
  586.     except: pass
  587.     if cv.x > 270:
  588.         try: cv.incr=increment[int(cv.y/yy2)]
  589.         except: cv.incr=0
  590.         cv.down=[cv.incr]*10
  591.     if event['type'] == key_codes.EButton1Down:
  592.         if cv.sz == full:
  593.             cv.sz=large
  594.             cv_set()
  595.         cv.jot=(x, y, x+1, y+1)
  596.     elif event['type'] == key_codes.EDrag:
  597.         pass # see def line_plot()
  598.     elif event['type'] == key_codes.EButton1Up:
  599.         cv.down=None
  600.     cv.x=x
  601.     cv.y=y
  602.  
  603. def line_plot():
  604.     x1,y1,x2,y2=cv.jot
  605.     cv.jot=x2,y2,cv.x,cv.y
  606.  
  607. step=10 ### ZZZ
  608. def pieplot():
  609.     exec ('cv.'+cv.val+'+='+str(pdeg[cv.deg]))
  610. def circle(x,y,radius,fill,outline=0,width=0):
  611.     img.ellipse((x-radius,y-radius,x+radius,y+radius),outline=outline,fill=fill,width=width)
  612. def square(loc,color):
  613.     try: cv.sq
  614.     except: cv.sq=90
  615.     img.rectangle((loc[0]*step,cv.sq+loc[1]*step,loc[0]*step+step,cv.sq+loc[1]*step+step),fill=color)
  616. def radian(d):
  617.     return d*3.14/180.0
  618. def switch(): # boolean
  619.     cv.sw=(1,0)[cv.sw]
  620. def portal(direction,max,min=0):
  621.     if direction < min: direction += max
  622.     if direction > max: direction -= max
  623.     return direction
  624. #
  625. def draw_score():
  626.     img.rectangle((0,0,360,20),fill=(0,0,0))
  627.     img.text((2,14),u"Score: %d   Speed: %d%s"%(score,1200-speed,'inches/hr'),(0,192,0))
  628. def draw_field():
  629.     img.rectangle((0,20,360,y_max*step),fill=fieldcolor)
  630. #
  631. def xyzsens():
  632.     try:
  633.         cv.xyz=(sens.x,sens.y,sens.z)
  634.     except: pass
  635. full,large,normal='full large normal'.split()
  636. def cv_set(i=0):
  637.     global img
  638.     if i == 0:
  639.         cv.ttl='Pys60 Demos'
  640.         cv.sz=large # (full,normal)
  641.         cv.ori='portrait' # (landscape)
  642.         cv.dpad=False
  643.         cv.color=RGB_WHITE
  644.         cv.font='normal'
  645.         cv.padx,cv.pady=0,0
  646.        
  647.     forLight() # ??? ZZZ not working
  648.    
  649.     app.title=unicode(cv.ttl)
  650.     app.orientation=cv.ori
  651.     app.screen=cv.sz
  652.     app.directional_pad=cv.dpad
  653.     app.body=cv.mode
  654.     img=Image.new(canvas.size)
  655. #
  656. def forLight():
  657.     timer=appuifw.e32.Ao_timer()
  658.     appuifw.e32.reset_inactivity()
  659.     if cv.light:
  660.         timer.after(10,forLight)
  661. #
  662. def z_xyzircle():
  663.     cv.go='$xyzircle'
  664. #
  665. def z_360arrow():
  666.     cv.angle=0
  667.     cv.zoom=29.0
  668.     cv.of_angles=600
  669.     cv.go='$360arrow'
  670. #
  671. def z_progress():
  672.     cv.bar=0
  673.     cv.go='$progress'
  674. #
  675. def z_SnakeGame():
  676.     cv.sq=20
  677.     cv_set(1)
  678.     cv.go='$SnakeGame'
  679. #
  680. def z_3Dspin():
  681.     cv_set()
  682.     cv.go='$3Dspin'
  683. #
  684. def z_snowfall():
  685.     cv_set()
  686.     cv.go='$snowfall'
  687. #
  688. def z_bouncySq():
  689.     cv_set()
  690.     cv.go='$bouncySq'
  691. #
  692. def z_star_5pt():
  693.     cv_set()
  694.     cv.t=time.time()+2
  695.     cv.go='$star_5pt'
  696. #
  697. def z_captureDemo():
  698.     cv_set()
  699.     cv.go='$captureDemo'
  700. #
  701. def z_sqdance():
  702.     cv_set()
  703.     cv.go='$sqdance'
  704. #
  705. def z_btn_matrix():
  706.     cv_set()
  707.     cv.xy=(0,0)
  708.     cv.padx,cv.pady=0,38
  709.     cv.btn99=1
  710.     cv.init=1
  711.     cv.go='$btn_matrix'
  712. #
  713. def z_sketchpad():
  714.     cv_set()
  715.     cv.color=RGB_RED
  716.     cv.bg=RGB_WHITE
  717.     img.clear(cv.bg)
  718.     z_pad_menu()
  719.     cv.go='$sketchpad'
  720. #
  721. def z_slider():
  722.     cv_set()
  723.     cv.tmp=max_rgb
  724.     cv.incr=0
  725.     cv.down=(0)
  726.     cv.go='$slider'
  727. #
  728. def z_blend_colors():
  729.     cv_set()
  730.     cv.RGB_mark=(10,140,20,150)
  731.     cv.x=1
  732.     cv.Lcolor = (0,0,255)
  733.     cv.Rcolor = (255,255,0)
  734.     cv.color = cv.prev_color = (0,0,0)
  735.     app.menu=[(u'<<<', z_pad_menu)]+rgb_menu
  736.     cv.go='$blend_colors'
  737. #
  738. def z_pieslice():
  739.     cv_set()
  740.     cv.xp,cv.yp=20,60; cv.rx,cv.ry=90,180
  741.     cv.x2=cv.y2=300
  742.     cv.xyt=cv.rxy=0
  743.     cv.deg=4
  744.     cv.up=-1
  745.     cv.val='rx'
  746.     app.menu=[(u'Select Tool',z_tools), g_menu]
  747.     cv.go='$pieslice'
  748. #
  749. def z_text2fit():
  750.     cv_set(1)
  751.     cv.init=1
  752.     app.menu=[(u'Rotate Display',z_ori), g_menu]
  753.     cv.go='$text2fit'
  754. #
  755. def z_textscroll():
  756.     cv_set(1)
  757.     cv.init=1
  758.     cv.go='$textscroll'
  759. #
  760. def z_ori():
  761.     if cv.ori is 'portrait': cv.ori='landscape'
  762.     else: cv.ori='portrait'
  763.     z_text2fit()
  764. def z_fit(a,b,c):
  765.     pl=canvas.measure_text(a,font=b)[1] # pixel length
  766.     cl=canvas.measure_text(a,font=b,maxwidth=c)[2] # chr length
  767.     ### note: maxwidth is not generic
  768.     return cl,pl
  769. def text2wrap(prev,new=None,gap=20,setfont=(u"dense",30)):
  770.     if type(prev) in [int,tuple]:
  771.         color=prev
  772.         if new:
  773.             if gap is not 20:
  774.                 setfont=gap
  775.             gap=new
  776.         y=40
  777.         for gen in wrapped:
  778.             img.text((gap,y),u''+gen,font=setfont,fill=color)
  779.             y += 36
  780.     else:
  781.         ww=canvas.size[0]-gap*2
  782.         if type(prev) != list: prev=[]
  783.         def wrap_gen(line):
  784.             tmp=''
  785.             if prev: tmp=prev.pop(-1)+' '
  786.             while 1:
  787.                 cl,pl=z_fit(tmp+line,setfont,ww)
  788.                 if pl > ww:
  789.                     if tmp:
  790.                         prev.append(tmp)
  791.                         tmp=''
  792.                     else:
  793.                         prev.append(line[:cl])
  794.                         line=line[cl:]
  795.                 else:
  796.                     break
  797.             prev.append(tmp+line)
  798.  
  799.         if ' ' in new: new=new.split()
  800.         else: new=[new]
  801.         for data in new: wrap_gen(u''+data)
  802.         _gv = globals()
  803.         _gv['wrapped']=prev[:]
  804. #
  805. animals=[u'dog', u'cat', u'cow']
  806. def z_rec2play():
  807.     scr('Record animal sounds')
  808.     for animal in animals:
  809.         fullpath='e:\\'+animal+'.wav'
  810.         try: os.remove(fullpath)
  811.         except: pass
  812.         rec="sound of a "+animal
  813.         z_record(fullpath,rec)
  814.     z_playaudio(animals)
  815.     for animal in animals:
  816.         fullpath='e:\\'+animal+'.wav'
  817.         try: os.remove(fullpath)
  818.         except: pass
  819.     print 'Done!'
  820. def z_record(fullpath,rec=''):
  821.         sounds=audio.Sound.open(fullpath)
  822.         if appuifw.query(u"Record "+rec, "query"):
  823.             sounds.record()
  824.             appuifw.query(u"Press OK to stop recording", "query")
  825.             sounds.stop()
  826.             sounds.close()
  827. def z_playaudio(menu):
  828.     while True:
  829.         index=appuifw.popup_menu(menu, u"Select sound...")
  830.         if index == None:
  831.             break #
  832.         else:
  833.             sound=u'e:\\'+menu[index]+'.wav'
  834.             sound=audio.Sound.open(sound)
  835.             sound.play()
  836. def z_readtext():
  837.     phrases=[u'Here is an All-In-One demo',u'Thank you very much',u'This PyLang is awesome!']
  838.     index=appuifw.popup_menu(phrases,u'*** Select Phrase ***')
  839.     data=phrases[index]
  840.     scr(data)
  841.     z_speech(data)
  842. #
  843. def menuX():
  844.     cv.go='+++'
  845.     cv_set()
  846.     full_bind()
  847.     cv.mode=txt
  848.     app.exit_key_handler=mainexit
  849. def z_mainmenu(null=''):
  850.     menuX()
  851.     scr('Main Menu --\nPress Options\n\n')
  852.     app.menu=mainmenu
  853. def z_gfxmenu():
  854.     menuX()
  855.     scr('Graphics Demo Menu --\nPress Options\n\n')
  856.     app.menu=g2menu
  857. def z_rgbmenu():
  858.     app.menu=[(u'<<<', z_pad_menu)]+rgb_menu
  859. def z_pad_menu():
  860.     pad__=pad_menu[:]
  861.     if cv.go not in ['$sketchpad']: pad__.pop(1)
  862.     app.menu=pad__
  863. def z_audiomenu(): ### ZZZ
  864.     cv_set()
  865.     scr('Audio Demo --\nPress Options\n\n')
  866.     app.menu=s_menu
  867. def z_exitmenu():
  868.     app.menu=[m_menu]
  869. #
  870. m_menu= u'*** Main Menu',z_mainmenu
  871. g_menu= u'*** GFX Menu',z_gfxmenu
  872. g2menu= [(u'XYZCircle',z_xyzircle),
  873.         (u'Snake Game',z_SnakeGame),
  874.         (u'Blend Colors',z_blend_colors),
  875.         (u'3D Spin',z_3Dspin),
  876.         (u'360arrow',z_360arrow),
  877.         (u'Basic Buttons',z_btn_matrix),
  878.         (u'Color Slider',z_slider),
  879.         (u'Sketchpad',z_sketchpad),
  880.         (u'Pie-Arc-Key',z_pieslice),
  881.         (u'5pt Star',z_star_5pt),
  882.         (u'Snowfall',z_snowfall),
  883.         (u'Bouncy Square',z_bouncySq),
  884.         (u'Capture Demo',z_captureDemo),
  885.         (u'Progress Bar',z_progress),
  886.         (u'Mosaic Dance',z_sqdance),
  887.         (u'Text Scroll',z_textscroll),
  888.         (u'Text Wrap',z_text2fit),
  889.         m_menu]
  890. s_menu= [(u'rec and play',z_rec2play),
  891.         (u'text-to-speech',z_readtext),
  892.         m_menu]
  893. t_menu= [(u'text',z_text),
  894.         (u'number',z_number),
  895.         (u'date',z_date),
  896.         (u'time',z_time),
  897.         (u'code',z_code),
  898.         (u'query',z_query),
  899.         (u'multi-query',z_multiquery),
  900.         (u'checkbox: multi-select',z_checkbox),
  901.         (u'checkmark: multi-select',z_checkmark),
  902.         (u'popup menu',z_popup),
  903.         (u'tabs (5)*',z_5tabs),
  904.         (u'Sub Menu',z_submenu_demo),
  905.         m_menu]
  906. t2menu= [(u'loop',z_loop),
  907.         (u'enumerate',z_enum),
  908.         (u'datetime',z_dts),
  909.         (u'basic image regex',z_regex),
  910.         (u'download image',z_dl),
  911.         (u'save text data',z_savedata),
  912.         m_menu]
  913.         #(u'textwrap*',z_textwrap),
  914. mainmenu=[(u'Text Demo',z_textmenu),
  915.         (u'Text Demo 2',z_textmenu2),
  916.         (u'Graphics Demo',z_gfxmenu),
  917.         (u'Audio Demo',z_audiomenu)]
  918. exec rgb_menu
  919. pad_menu=[(u'-- Color Selection',z_rgbmenu),(u"-- Set Background",(z_bg)),(g_menu)]
  920.  
  921.  
  922. img=None
  923. app.screen = 'large'
  924. app.orientation = 'portrait'
  925. app.directional_pad = False
  926. app.body = canvas = Canvas(redraw_callback=draw, event_callback=e_callback) #
  927. img = Image.new(canvas.size)
  928.  
  929. txt=appuifw.Text()
  930. x_scr=canvas.size[0]
  931. y_scr=canvas.size[1]
  932. x_mid=x_scr / 2
  933. y_mid=y_scr / 2
  934. cv.mode=txt
  935. cv.t=0
  936. app_lock=e32.Ao_lock()
  937.  
  938.  
  939. if 1: # b64_img
  940.     py_png_b64='''\
  941.     R0lGODlhQABAAPcAAAAAAAAPMwAXNwAaOAAYQwAbQQAdQQAbTwAeSwAWUQMmQgAjSwEnSwYuTwoo
  942.     SggtSAwrSAAlVAAgWQAmWAAqVgAuVwAuXQcuWAw1TQs5Xhk0SQAsYwAxZAA1YQA0bAA6bAA/dyw/
  943.     UAxAbgxGeh5BYRxHbRRKdBRNfi5IXC5OaCJQdCNUfzdTajlQYj1UaTJWdDZge0BXaUZZaE1dakJa
  944.     cEBffElhc0lieUlmfU9leQZJgBRVjB5bjypehzNXhzFbgTlchiZjljhkhzRplixtozNwozF1qzp4
  945.     q0BehERmhENli0dshUZvjktylk1wkE92kltvgVBwiVp1g1lyiVl1il95j1B3l1V3klh6lmB2iWt7
  946.     lUWDuUaIvEqCskmGul6Col2FqVSLtVONvVqMtl+NsWaInmmAlGyGnHOGlHGHmXGKnnqJk3mMm3uR
  947.     lX2Qn2SGpmqIoW+Pp2yOrGeRuXGLoneOoHaPo3STp3eSrXyRonWZuX2gvmOWwW2aw2meynOfwnGh
  948.     yXuhw3yly3iqz3ypz3Sl0HCp1pt+Ha6ED6SAE6yDGqeRF7OEBbqTCriTFr6fEKyJLaODMKWWJqWS
  949.     M7WTI7aWN62iJrqlKrekOJiOR5uRTKSTRqaXVa2bV7mZR7mdVKmeZ62da62ec7Cfd7ujRrCmXrGt
  950.     W7yoWq6jaqyicbWga7OpbbqpbbSrebmueMGXC8SaFcSZJcOcNcqjGtSoHc2pJ8yrOcG1P9iuKdG4
  951.     LdC5PtmwPNq5MN29PcurScqtWtG8S9uwS9G8X921Xsmvbc2yd9C9cN/AUNHAfeHKRePMUuDDYOTP
  952.     ZODFcubPdOLIfoKPmIGZq4qcrIGasYedsYOgs4eiv5euvoSjwIapx4KozYSrzI6wzoaw1Iux1Y+1
  953.     1oq12JSyz5K00Ji305i72qC706C92Z3B3JnA4aTC26vF3KXI5K/Q6s/CjtTOgdvGjtLHmtPNltTP
  954.     odTQreXRgunXk+nZp+/ki+zimu/lqQAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAP8ALAAAAABAAEAA
  955.     AAj/AP8JHEiwoEGB16qdiVMmzh1p1KhNi1bnzBk809Ic3MixI0czNKhUK5du3bl06cqpVGlOnTpz
  956.     5M60cOOxps2BaSDI+faNGzdx38B92zYunE+f4cQdNTdFg52bUA+mMTCHULdAe7JtA1SIkCBtgsIS
  957.     8hookLay274I0Bi17b8GFgqN+XFmWpQvgPj48ZNXr19Affj0yetHhQK3UeEs+CBGBbSBV4SImSyG
  958.     Dx/KYsLwGZN5cpcAZhDflENgAgG2AxEE2SJmi+stXMQc6UK7y5HbRwgcFl3zzYTfjwl2kLCDyBYj
  959.     Ro4kx32EyG0iQwoEyMPbo5XfE1ALnODBwwcdO8IH/9kRpHx5HkF6iCAQAEP1jlh+W4AR/N+ZCRs+
  960.     6Peuvz//Dx2Yxp577230hQUfnCACGNhY84YIGXwgggggiKBfB/oh6MEEFkRAwIcCEFigQW+ssEIQ
  961.     K4jgww8liGDiixNeiOAH+VGwwAIftjfiQXGUMEQRQwzxYnpDCCFkihd+YAEHHFhgwQQIgCjijgPF
  962.     QcILLwjxgwo9/PCCl1i+oIIJImA4o5MdQklAiFQWBA0ba8S5RhppoNEGGmiwkQYdU2DIJJpoYkfA
  963.     AFO2CVUaC8gHYAdocvDboIW6NcUUL6DQggssXMrCppyG6SmWKpSG4AQdMFoBBRQ8GcEC7qWCCies
  964.     oP8CCiquhOLRFBgIoIQe5KCUjjnnoIMOT910sw0hgCRr2RhkdDGECE9y4J2Tp1KwqgLucWIMPdzW
  965.     Q487nIDC0QsFMCAOOy+xRI43xnaDbF+XieHFbLiNwKGSS6JZwbXuZYJMPQADzO0pmxz0wod9oDPO
  966.     UdgEIgi8Y4xBG25FmMfDDiMw2eSSFjJqQQULYPuPJr9wazI9zjATCScFTVHaBoYM8kcgllnW2sRH
  967.     BHneCico2F8ESl4A4YQQZsDoBQ70SwozzjTdDDPLLGPLIZ0QtIAEEnhAWRiyKUcEEUHw0PMJI/gs
  968.     4YUZWJB2ixmUoIIIb0N4AQMP9CtJL8ooc8zex/T/Yksikww0xQQSJDBBcl+XdwIIIPynHwj6ES35
  969.     hCaU4LYKKrz4dgYN1P3PJos80svoyfDCSy+xIHLIKQJlQDjWG4S3A+MfQP444yOIcIIJCpL5NubA
  970.     Yw6DEEL44MMKJWSAAQZt/DOJI4/UYjouvMxCiyOMJML6FAUcAPsGHNB+du0jfMD7CS+mn/kKmP8g
  971.     BAwwLMGEE0og8ULyywuECSWzzFILLrjo3ywawQhFbI8AB8AP+G4nIRGMoEI9S58QekC8ClpQCE/I
  972.     IBawMIUaiIlzMPjHKSpxiVrUgha5oEX1ZvEKRiBiEqdw2W82wB/GiQ8EZTMRBS3IhB768AlNsIIV
  973.     /56wQSxcYQkwUAEJRKABgQiDEiasRS5wQQtc1GIWsIAFIiJxCiHM8D82tF3ZeqACIwkhiFbwYQ8z
  974.     aIUgGjGDTogC/FRQggtAQYSY8F8Ac8ELAPYvi46gxClOQMPu0C6M5jOBiYj3hCGq0YfEGwL9lJCE
  975.     JRBvS5gjAQ0EcgooApB6VOQFLWjBQkcYsHGGPGTtGOeiMl7wBTD4VJiA8AIayJIEJKiDQJAxCUyY
  976.     sI/ToyIps0iJYOyndo/rTwNfdEn44WAKVciCGbJATWpCAQpTgEIWpIBNLUhjIKzYxC+MIYwp9lGF
  977.     n4QFFh2xC2UqU3wiUGQPViCEF+CAGvVxyyk4cf8KetwDH87YBQCr+EkrYhEWtVCmd2ynTDKZSAVq
  978.     IIg84iGPilJUHvOoqEbj0Q55wMMVoHBFwOpxD2D0AoDBpGIWYeEIWVjIA+IbX39MYAISREMg8Qgn
  979.     rJDRDnf4lKPtQIZQh2qMbQlMYM7wBSj7+MlRZjEWuzjBMR23qAll4AwCMcUkYIiPfOQDH2ANaz3w
  980.     4a2TmZVbzjhGQakoTFK+AhaxCEYQ3ImhUi2qVC/IqiUsoYx70ANg+OCWX9FqMmegrBnOaEbUoHYM
  981.     gS51lG3FIiWG0Sd3fqB8F+rABaYgwklQ4hZMu0fTRsu0lDHjtFCL2jKOkbe99cKxbIXsQGnBUkj/
  982.     ULYCyIyRVDvWARIIZBMl1AVqUZu34jKjuKztW99Gd9JPnpOgTSVmJSgbIQmdr2e5gxBnT4GJS+Qi
  983.     GcVVRjLGC97xMve8vTAdU1MK3U/GgraviMUnWKeBDEwofSZSUAZigMdc8JG85k1Gek+X3gGrd3pM
  984.     pR46P7kLgb4XFq+gBDEEgoMMjABz7xPC+uooAxHewr+88K+I+RhilFLvwCjmhUB30ccGB+PFDa5F
  985.     LFgoSIFMwb4r6AEMmCCEJQDhB8izgYdHPGLTkZjA50Xy6HbB3Ab3IhjDUG0wdhGLLFaCICnAMAYz
  986.     qKUgi1AXulDviHHBRz6el29oPsZqlxGMNUet/xmKXa0tYhGLGguOBItsAhO4zKIbfLl0ySjygUeX
  987.     XGWodhlwfvOh4wxnxQ7DwZ8wyBRIAAQgCEEJPA5TDkQIDF0QedC6AK8yUDva0dIDsaOFc2IbLQxg
  988.     zLkSrJM0Cl5AS0+hoMOnCHOIRYziXgBDzY1umsme8Qx6PAPOxCZ2o43xC19UghSxPgg0X5ACLLEA
  989.     BSGYAac9zeteq9kYQw23uMVNjHKbmxjFiPZGJgWNdrv7Mbn+dIp38YtSrOLe+M53vlsxilbwuxWh
  990.     GIWt3hNvXgM6pb5YhaH0yW0Q86J06jWhLU7BioqzwhWsUIXFK+4KUYxi4ZwEsxR7bbpZ4OIYwv82
  991.     GcDeETB71OMdHwf5KTp95BQH8ORRUzOik31sYrNi4AsvuOkMPDpR/tGEo9OFL37BilOseRifALqh
  992.     cq0L9J6Xeib841OXgW9S2GIXwYiE1Ns08zQnd7m9iOIVZxHXaJ/CE3SGRNVkDoxfH/e0x0ita/2G
  993.     C1jQIhbNKAgx6FyJsVPpFOQ8bamdFucX2wIXdKZHQX5BiVh4Qt2GIoYwmHYyw6LMGTnfhQkp4Yto
  994.     t6ISlJAF5qduDGYYw/NmPXbObWGLWlTiGac4xSpIkXpPgLwgyNhWsY9tbGKjNee6oH0lfvELT1Ri
  995.     +auX+SmUzfNkN+PYwzgG7Xchi+57ghjR/70rCKfPc24pO87L8MUwfLH08It/ILkPqjEaTf9hFBX8
  996.     7n9/QXLP//73f0cBAQA7'''
  997.  
  998.     b64mask='''\
  999.     R0lGODlhQABAAPcAAAAAAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1000.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1001.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1002.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1003.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1004.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1005.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1006.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1007.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1008.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1009.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1010.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1011.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  1012.     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAP8ALAAAAABAAEAA
  1013.     AAj/AP8JHEiwoEGBARIqXMhQ4cGHECNCbEixYkKJGDMOtMixosaPBzuKbAiyJMKRKAOYBEnyZEqG
  1014.     KzXCJPhyZkyJNl3WvHgzYs5/Oxf29GkzqNChD42KRJpUKUemIZ1ahGpQ6lSqNK1SxLqy5lCtL4F6
  1015.     JAo2pdixUcua7ZhWLcqzVze6DTsy69y3dXXeXfoW7l62eP/mDSwYcGGweg8r9avYaOLGXhlDNvtY
  1016.     8M3JdieHlax5cOedlT8bFr2ZtFfTFwmLDg0TNWuhpjNPjS13dOeqtjW33bq6qcfPOH9DRhqZK8u7
  1017.     xl8vTr43OWerzpszd1rb4XTHsqMvr65SO3a/zpUXEw+vNrzs7ea5j09fEDT74GyZBgQAOw=='''
  1018.  
  1019.     tf = tempfile.mktemp(".png", "tmp_x")
  1020.     print tf
  1021.  
  1022.     g = open(tf, 'wb')
  1023.     g.write(base64.decodestring(py_png_b64))
  1024.     g.close()
  1025.  
  1026.     png = Image.open(tf)
  1027.  
  1028.     g = open(tf, 'wb')
  1029.     g.write(base64.decodestring(b64mask))
  1030.     g.close()
  1031.  
  1032.     png_mask = Image.open(tf)
  1033.  
  1034.     img_mask=Image.new(png_mask.size, mode='1')
  1035.     img_mask.blit(png_mask)
  1036.  
  1037.  
  1038. def mainexit():
  1039.     z_tabexit()
  1040.     app_lock.signal()
  1041.     cv.go=False
  1042. app.exit_key_handler=mainexit
  1043.  
  1044. timer=appuifw.e32.Ao_timer()
  1045. app_lock=appuifw.e32.Ao_lock()
  1046.  
  1047. import sensor
  1048. sens=sensor.AccelerometerXYZAxisData(data_filter=sensor.LowPassFilter())
  1049. sens.set_callback(data_callback=xyzsens)
  1050. sens.start_listening()
  1051.  
  1052. ### joypad
  1053. mv=0
  1054. jxa,jya=70,350 ### <<< jpad xy axis point
  1055. jxb,jyb=jxa+220,jya+220
  1056. jx1,jy1=jxa+70,jya+70
  1057. jx2,jy2=jxa+150,jya+150
  1058. def mU(event):
  1059.     global mv
  1060.     if mv <> 1:
  1061.         mv=0
  1062. def mD(event):
  1063.     global mv
  1064.     if mv <> 0:
  1065.         mv=1
  1066. def mL(event):
  1067.     global mv
  1068.     if mv <> 3:
  1069.         mv=2
  1070. def mR(event):
  1071.     global mv
  1072.     if mv <> 2:
  1073.         mv=3
  1074. def bind_jpad():
  1075.     canvas.bind(EButton1Down,mU,((jx1,jya),(jx2,jy1)))   # up
  1076.     canvas.bind(EButton1Down,mD,((jx1,jy2),(jx2,jyb)))   # down
  1077.     canvas.bind(EButton1Down,mL,((jxa,jy1),(jx1,jy2)))   # left
  1078.     canvas.bind(EButton1Down,mR,((jx2,jy1),(jxb,jy2)))   # right
  1079. def jpad():
  1080.     grey=(220,220,220)
  1081.     img.rectangle((jxa,jy1,jxb,jy2),fill=grey) # w
  1082.     img.rectangle((jx1,jya,jx2,jyb),fill=grey) # h
  1083. def jpad_key(): ### bonus feature!
  1084.     if   mv == 0:
  1085.         xy_=(jx1,jya,jx2,jy1) # U
  1086.     elif mv == 1:
  1087.         xy_=(jx1,jy2,jx2,jyb) # D
  1088.     elif mv == 2:
  1089.         xy_=(jxa,jy1,jx1,jy2) # L
  1090.     elif mv == 3:
  1091.         xy_=(jx2,jy1,jxb,jy2) # R
  1092.     else: return
  1093.     img.rectangle(xy_,fill=0xffff00)
  1094. #
  1095. if not appuifw.touch_enabled():
  1096.     appuifw.note(u"This application only works on devices that support touch input")
  1097. #
  1098. def up_event(event):
  1099.     cv.up=-1
  1100. def dn_event(event):
  1101.     cv.up=1
  1102. #
  1103. def mv_event(event):
  1104.     x=event[0]
  1105.     y=event[1]
  1106.     jx=x/cv.w
  1107.     jy=y/cv.h
  1108.     cv.btn99=jx+(jy*4)+1
  1109.     cv.xy=(cv.w*jx,cv.h*jy)
  1110.     cv.up=time.time()+1
  1111. #
  1112. def btn_pad(xx=4,yy=7):
  1113.     for y2 in range(yy):
  1114.         for x2 in range(xx):
  1115.             img.text((x2*cv.w+5,y2*cv.h+75+cv.pady), unicode(x2+(y2*4)+1), fill=RGB_GRAY,font=(u'Nokia Hindi S60',80,appuifw.STYLE_BOLD))
  1116. def btn_rpl(target,btn_99,sq=RGB_WHITE,btn_label=RGB_GRAY):
  1117.     a,b,c,d=target
  1118.     return (a,b+cv.pady,c,d+cv.pady), (a+cv.padx,b+cv.pady), sq, btn_label
  1119. #
  1120. def full_bind():
  1121.     canvas.bind(key_codes.EButton1Up, up_event, ((0,0), (x_scr,720)))
  1122.     canvas.bind(key_codes.EButton1Down, dn_event, ((0,0), (x_scr,720)))
  1123.     canvas.bind(key_codes.EDrag, mv_event, ((0,0), (x_scr,720)))
  1124. full_bind()
  1125. cv_set()
  1126. z_mainmenu()
  1127.  
  1128. jxa,jya=70,350 ### <<< jpad xy axis point
  1129. jxb,jyb=jxa+220,jya+220
  1130. jx1,jy1=jxa+70,jya+70
  1131. jx2,jy2=jxa+150,jya+150
  1132.  
  1133. mv=0
  1134. def mU(event):
  1135.     global mv
  1136.     if mv <> 1:
  1137.         mv=0
  1138. def mD(event):
  1139.     global mv
  1140.     if mv <> 0:
  1141.         mv=1
  1142. def mL(event):
  1143.     global mv
  1144.     if mv <> 3:
  1145.         mv=2
  1146. def mR(event):
  1147.     global mv
  1148.     if mv <> 2:
  1149.         mv=3
  1150.  
  1151. if 1: # snakegame
  1152.     step=10
  1153.     _ww,_hh=(360/step,540/step) ### ZZZ
  1154.     bodycolor=(32,180,32)
  1155.     headcolor=(0,128,0)
  1156.     fieldcolor=(192,192,128)
  1157.     foodcolor=(255,0,0)
  1158.     padding=4
  1159.     deltas=((0,-1),(0,1),(-1,0),(1,0)) # up down left right
  1160.     y_max=(jya-padding)/step
  1161.     defaultloc=w,h=[_ww/2,y_max/2]
  1162.     defaultsnake=12
  1163.     arraydefault=[]
  1164.     for i in range(defaultsnake,0,-1): # body (excluding the snake head)
  1165.         arraydefault.append([w,h+i])
  1166.     loc='init'
  1167. #
  1168. if 1: # 3Dspin
  1169.     xm,ym,x2,y2=0,0,0,0
  1170.     halfResX=x_mid
  1171.     halfResY=y_mid
  1172.     distance=180
  1173.     PX=0
  1174.     PY=0
  1175.     PZ=200
  1176.     Size=10
  1177.     xyz=80
  1178.     VX=[-xyz, xyz, -xyz, xyz, -xyz, xyz, -xyz, xyz]
  1179.     VY=[-xyz, -xyz, xyz, xyz, -xyz, -xyz, xyz, xyz]
  1180.     VZ=[-xyz, -xyz, -xyz, -xyz, xyz, xyz, xyz, xyz]
  1181.     ANGLEX=0
  1182.     ANGLEY=0
  1183.     ANGLEZ=0
  1184.     record=[]
  1185. #
  1186. if 1: # snowfall
  1187.     nflakes=120
  1188.     imgSnow=8
  1189.     moveX=range(nflakes)
  1190.     moveY=range(nflakes)
  1191.     XX=range(nflakes)
  1192.     YY=range(nflakes)
  1193.     for i in range(nflakes):
  1194.         moveY[i]=random.randint(5,20)
  1195.         dir=random.choice([-1,1])
  1196.         moveX[i]=dir*random.randint(1,3)
  1197.         XX[i]=random.randint(0,x_scr)
  1198.         YY[i]=random.randint(0,640)
  1199. #
  1200. if 1: # bouncySq
  1201.     sq=100
  1202.     dy=20
  1203.     dx=20
  1204. #
  1205. if 1: # btn_matrix
  1206.     cv.w,cv.h=90,90
  1207. #
  1208. if 1: # sketchpad
  1209.     thickness=10
  1210.     cv.jot=-99,-99,-99,-99
  1211. #
  1212. if 1: # slider
  1213.     increment=[100,50,10,1,0,-1,-10,-50,-100]
  1214.     yy2=lg_scr/9
  1215. #
  1216. if 1: # pieslice
  1217.     tools=[u'x move',u'y move',u'x scale',u'y scale',u'x arm',u'y arm',u'x+y scale',u'seg rotate']
  1218.     tool_val=u'xp yp x2 y2 rx ry xyt rxy'.split()
  1219.     pdeg=[30,10,5,1,0,-1,-5,-10,-30]
  1220. #
  1221. palref='''
  1222. cv.Lcolor = (hh(cv.Lcolor[0],1), cv.Lcolor[1], cv.Lcolor[2])
  1223. cv.Lcolor = (cv.Lcolor[0], hh(cv.Lcolor[1],1), cv.Lcolor[2])
  1224. cv.Lcolor = (cv.Lcolor[0], cv.Lcolor[1], hh(cv.Lcolor[2],1))
  1225. cv.Rcolor = (hh(cv.Rcolor[0],1), cv.Rcolor[1], cv.Rcolor[2])
  1226. cv.Rcolor = (cv.Rcolor[0], hh(cv.Rcolor[1],1), cv.Rcolor[2])
  1227. cv.Rcolor = (cv.Rcolor[0], cv.Rcolor[1], hh(cv.Rcolor[2],1))'''.split('\n')[1:]
  1228. cv.active_palette=palref[0]
  1229. if 1: # blend_colors
  1230.  
  1231.     def rgb_btns(x,y,x2=360,y2=50):
  1232.         xx=x2/3+1
  1233.         rgb=RGB_RED,RGB_GREEN,RGB_BLUE
  1234.         for i in xrange(3):
  1235.             xy2=(x+xx*i,y,x+xx*(i+1),y+y2)
  1236.             if cv.x:
  1237.                 xv,yv=cv.x,cv.y
  1238.                 if yv > y and yv < y+y2:
  1239.                     if xv > xy2[0] and xv < xy2[2]:
  1240.                         cv.active_palette=palref[palette+i]
  1241.                         cv.RGB_mark=(x+xx*i,y,x+xx*i+10,y+10)
  1242.             img.rectangle((xy2), fill=rgb[i])
  1243.     #
  1244.     def mixColors(): ### ZZZ yellow + blue = (0,128,0)
  1245.         def rgbfix(R):
  1246.             aaa=(LC[R]*-1)+(RC[R])
  1247.             tmp=[0,1,2]; tmp.pop(R)
  1248.             bbb=LC[tmp[0]]+LC[tmp[1]]
  1249.             ccc=int((RC[tmp[0]]+RC[tmp[1]]))*-1
  1250.             tmp=abs(aaa+bbb+ccc)-637
  1251.             if tmp > 0: return tmp
  1252.             return 0
  1253.        
  1254.         LC,RC = cv.Lcolor,cv.Rcolor
  1255.         rgb = [(LC[0]+1)/2, (LC[1]+1)/2, (LC[2]+1)/2] # L
  1256.         rgb = [rgb[0]+((RC[0]+1)/2), rgb[1]+((RC[1]+1)/2), rgb[2]+((RC[2]+1)/2)] # R
  1257.        
  1258.         # color fix for mixing yellow and blue to correspondingly make green
  1259.         r,g,b = rgb[0]-rgbfix(2),rgb[1],rgb[2]-rgbfix(2)
  1260.         return ((hh(r),hh(g),hh(b)))
  1261.     #
  1262.     def rgb_box(box,x=10,y=210,w=60,h=60):
  1263.         i='LRM'.find(box)
  1264.         return ((x,y,350-x-w,y+h*3),(x+w,y+h,350-x,y+h*4),(x+w,y+h,350-x-w,y+h*3))[i]
  1265.     #
  1266.     def hh(limit,incr=0,min=None,max=None):
  1267.         if incr: limit+=cv.incr
  1268.         if not min:
  1269.             min,max=0,255
  1270.         elif not max:
  1271.             min,max=0,min
  1272.         if limit < min: limit=min
  1273.         if limit > max: limit=max
  1274.         return limit
  1275. #
  1276. cv.sVy=-1
  1277. go='-'
  1278. skip=0
  1279. while cv.go:
  1280.     go=cv.go
  1281.     if go and time.time() > skip:
  1282.         skip=time.time()+0.05
  1283.         if go[0] is '$': ### graphics mode
  1284.             try: sens.start_listening()
  1285.             except: pass
  1286.             if cv.mode is txt:
  1287.                 cv.mode=canvas
  1288.                 cv_set(1)
  1289.         if loc is 'init':
  1290.             loc=w,h=defaultloc
  1291.             score=0
  1292.             mv=0
  1293.             foodloc=[5,5]
  1294.             sp=10000.00
  1295.             speed=1200.00
  1296.             snakelength=12
  1297.             fillarray=arraydefault[:]
  1298.             bind_jpad()
  1299.         elif go == '$SnakeGame':
  1300.             if loc == foodloc:
  1301.                 score+=1
  1302.                 a,b=(   random.randint(2,_ww/2)+loc[0]+_ww/4,
  1303.                         random.randint(2,y_max/2)+loc[1]+y_max/4)
  1304.                 if a > _ww: a-=_ww
  1305.                 if b > y_max-4: b-=y_max-4 # minus padding?
  1306.                 while 1:
  1307.                     if a > _ww-2:
  1308.                         a=2
  1309.                         if b > y_max-2: b=2
  1310.                     if [a,b] not in list(fillarray+loc):
  1311.                         break
  1312.                     a+=1
  1313.                     b+=3
  1314.                 foodloc=[a,b]
  1315.             else:
  1316.                 fillarray.pop(0)
  1317.             fillarray.append(loc)
  1318.             if -1 in loc: loc=fillarray[0]
  1319.             try:
  1320.                 e32.ao_sleep(speed/sp)
  1321.                 speed-=0.9
  1322.             except: pass
  1323.             jpad_key()
  1324.             h+=deltas[mv][1]
  1325.             w+=deltas[mv][0]
  1326.             loc=[w,h]
  1327.             if loc in fillarray or loc[0]>=_ww or loc[0]<0 or loc[1]>=y_max-2 or loc[1]<0:
  1328.                 print 'end of game'
  1329.                 loc='init'
  1330.                 go=appuifw.query(u'Score: %d  Speed: %d\nPlay Again?'%(score,1200-speed),'query')
  1331.                 if not go:
  1332.                     z_gfxmenu()
  1333.             else:
  1334.                 if 1200-speed < 2: img.clear(RGB_BLACK)
  1335.                 img.rectangle((0,20,360,y_max*step),fill=fieldcolor)
  1336.                 square(foodloc,foodcolor)
  1337.                 for bloc in fillarray:
  1338.                     square(bloc,bodycolor)
  1339.                 square(loc,headcolor)
  1340.                 img.rectangle((0,0,360,20),fill=RGB_BLACK)
  1341.                 img.text((2,14),u"Score: %d   Speed: %d"%(score,1200-speed),(0,192,0))
  1342.                 jpad()
  1343.         elif go == '$3Dspin':
  1344.             xm,ym,zm=cv.xyz
  1345.             xm,ym=z_betw(xm,ym,(-20))
  1346.             ANGLEX+=ym
  1347.             ANGLEY+=xm
  1348.             rax=ANGLEX*math.pi/180
  1349.             ray=ANGLEY*math.pi/180
  1350.             raz=ANGLEZ*math.pi/180
  1351.  
  1352.             sinx=math.sin(rax)
  1353.             cosx=math.cos(rax)
  1354.             siny=math.sin(ray)
  1355.             cosy=math.cos(ray)
  1356.             sinz=math.sin(raz)
  1357.             cosz=math.cos(raz)
  1358.            
  1359.             """
  1360.             buf=graphics.Image.new((640,360)) ### not sure if this is the pythonic double buffer, but it works like a charm
  1361.             img=graphics.Image.new((640,360))
  1362.             """
  1363.            
  1364.             record=[]
  1365.             for n in range(8):
  1366.                 ZX=VX[n]*cosz - VY[n]*sinz - VX[n]
  1367.                 ZY=VX[n]*sinz + VY[n]*cosz - VY[n]
  1368.                 YX=(VX[n]+ZX)*cosy - VZ[n]*siny - (VX[n]+ZX)
  1369.                 YZ=(VX[n]+ZX)*siny + VZ[n]*cosy - VZ[n]
  1370.                 XY=(VY[n]+ZY)*cosx - (VZ[n]+YZ)*sinx - (VY[n]+ZY)
  1371.                 XZ=(VY[n]+ZY)*sinx + (VZ[n]+YZ)*cosx - (VZ[n]+YZ)
  1372.                 XR=YX+ZX
  1373.                 YR=ZY+XY
  1374.                 ZR=XZ+YZ
  1375.                 z=(VZ[n]+PZ+ZR)/distance
  1376.                 x=((VX[n]+PX+XR)/z)+halfResX
  1377.                 y=((VY[n]+PY+YR)/z)+halfResY
  1378.                 z=int(Size/z)
  1379.                 record.append((x,y,z))
  1380.                 img.clear(RGB_LIGHTBLUE)
  1381.                 circle(PX+halfResX,PY+halfResY,Size*3,0xff0000)
  1382.                 for n in record:
  1383.                     x,y,z=n
  1384.                     circle(x,y,z,0x0000ff)
  1385.                 img.text((20,40),'X:'+unicode(xm),0)
  1386.                 img.text((20,80),'Y:'+unicode(ym),0)
  1387.         elif go == '$bouncySq':
  1388.             if time.time() > cv.t:
  1389.                 cv.x += dx
  1390.                 cv.y += dy
  1391.                 x,y=cv.x,cv.y
  1392.                 if (x < -50)or(x > x_scr - 50):
  1393.                     dx *= -1
  1394.                 if (y < -50)or(y > 560 - 50): # y_scr
  1395.                     dy *= -1
  1396.                 cv.color=randrange(0xffffff)
  1397.                 cv.t=time.time()+0.05
  1398.                 img.rectangle((x,y,x+sq,y+sq), fill=cv.color)
  1399.         elif go == '$blend_colors':
  1400.             if cv.prev_color <> cv.color:
  1401.                 exec (cv.active_palette[:9]+'='+str(cv.color))
  1402.                 cv.prev_color=cv.color
  1403.                 cv.x=1
  1404.             if cv.x:
  1405.                 img.clear(RGB_GRAY)
  1406.                 slidebar()
  1407.                 exec (cv.active_palette)
  1408.                 cv.subcolor = mixColors()
  1409.                 img.rectangle(((0,0,350,640)), fill=RGB_GRAY)
  1410.                 img.rectangle(((0,0,340,92)), fill=RGB_WHITE)
  1411.                 palette=0
  1412.                 rgb_btns(10,140,280)
  1413.                 palette=3
  1414.                 rgb_btns(10,500,280)
  1415.                 img.rectangle((cv.RGB_mark ), fill=RGB_WHITE)
  1416.                 img.rectangle((rgb_box('L')), fill=cv.Lcolor)
  1417.                 img.rectangle((rgb_box('R')), fill=cv.Rcolor)
  1418.                 img.rectangle((rgb_box('L')), width=3, outline=RGB_BLACK)
  1419.                 img.rectangle((rgb_box('R')), width=3, outline=RGB_BLACK)
  1420.                 img.rectangle((rgb_box('M')), fill=cv.subcolor)
  1421.                 img.text((10,36), u"RGB Result Of A And B:", fill = RGB_BLACK,font=(u'Nokia Hindi S60',29,STYLE_BOLD))
  1422.                 img.text((10,68), u""+str(cv.subcolor), fill = RGB_BLACK,font=(u'Nokia Hindi S60',29,STYLE_BOLD))
  1423.                 img.text((10,128), u"RGB Of A: "+str(cv.Lcolor), fill = RGB_BLACK,font=(u'Nokia Hindi S60',29,STYLE_BOLD))
  1424.                 img.text((10,488), u"RGB Of B: "+str(cv.Rcolor), fill = RGB_BLACK,font=(u'Nokia Hindi S60',29,STYLE_BOLD))
  1425.                 cv.x=0
  1426.         elif go == '$progress':
  1427.             if cv.bar < 1:
  1428.                 img.clear(RGB_BLACK)
  1429.                 ybarA=y_scr-60
  1430.                 ybarB=ybarA+20
  1431.             cv.bar+=1
  1432.             i=cv.bar
  1433.             c=int(((255.0*2)/x_scr)*i)
  1434.             if not c > 255:
  1435.                 # red marker
  1436.                 color=(255,c,0)
  1437.             else:
  1438.                 # green marker
  1439.                 c -= 255
  1440.                 color=(255-c,255,0)
  1441.             img.rectangle((i,ybarA,i+5,ybarB),fill=color)
  1442.             # frame
  1443.             img.rectangle((0,ybarA,x_scr,ybarB+2),0xffffff)
  1444.             if cv.bar > x_scr:
  1445.                 cv.bar=0
  1446.         elif go == '$captureDemo':
  1447.             cv.go='-'
  1448.             def saveImg():
  1449.                 sCapture('demoImage')
  1450.             id=unicode(random.randint(100000,999999))
  1451.             print id
  1452.             mosaic()
  1453.             img.text((148,60),id,font=('dense',60))
  1454.             app.menu=[(u'Save Image',saveImg), g_menu]
  1455.         elif go == '$btn_matrix':
  1456.             if cv.init:
  1457.                 img.clear()
  1458.                 btn_pad()
  1459.                 draw(())
  1460.                 e32.ao_sleep(0.1) ###
  1461.                 bg=graphics.screenshot()
  1462.                 cv.init=0
  1463.             a,b=cv.xy
  1464.             btn_=(a,b+cv.pady ,a+cv.w,b+cv.pady+cv.h)
  1465.             txt_=(a+cv.padx+5,b+cv.pady+75)
  1466.             sq_color, btn_label = RGB_GREEN, RGB_BLACK
  1467.             if cv.up < time.time():
  1468.                 sq_color, btn_label = RGB_RED, RGB_WHITE
  1469.             img.blit(bg,source=(0,0))
  1470.             img.rectangle((btn_), fill=sq_color)
  1471.             img.text((txt_), unicode(cv.btn99), fill=btn_label,font=(u'Nokia Hindi S60',80,STYLE_BOLD))
  1472.         elif go == '$sketchpad': # ZZZ
  1473.             line_plot()
  1474.             img.line((cv.jot), cv.color, width=thickness)
  1475.         elif go == '$slider':
  1476.             img.clear(RGB_GRAY)
  1477.             slidebar()
  1478.             if cv.down:
  1479.                 msg=cv.down.pop()
  1480.             else: msg=0
  1481.             if cv.t < time.time():
  1482.                 cv.tmp = portal(cv.tmp+msg, max_rgb)
  1483.                 cv.t=time.time()+0.4
  1484.             rgb=rainbow[cv.tmp]
  1485.             img.rectangle((120,2,348,50), fill=rgb)
  1486.             img.text((10,44), u"Color:", fill = RGB_BLACK,font=(u'Nokia Hindi S60',40,STYLE_BOLD))
  1487.             img.text((10,90), unicode(rgb), fill = RGB_BLACK,font=(u'Nokia Hindi S60',30,STYLE_BOLD))
  1488.             img.text((10,130), unicode(msg), fill = RGB_BLACK,font=(u'Nokia Hindi S60',30,STYLE_BOLD))
  1489.             z_outline(254,100, (cv.sVy-4)*-1)
  1490.         elif go == '$star_5pt':
  1491.             img.clear((0,128,255))
  1492.             star=star_5pt(30,60,300)
  1493.             img.polygon((star), outline=RGB_GREEN, width=10, fill=RGB_YELLOW)
  1494.             if cv.t < time.time():
  1495.                 cv.t=time.time()+1
  1496.                 switch()
  1497.             if cv.sw:
  1498.                 img.polygon((star), outline=RGB_PURPLE, width=9) # outline in pys60 does not round properly over 9 width
  1499.             img.blit(png, target=(100,120), mask=img_mask) ### b64_img
  1500.         elif go == '$pieslice':
  1501.             cv.deg=cv.y/yy2
  1502.             if time.time() > cv.up and cv.x > 300:
  1503.                 pieplot()
  1504.                 cv.up=time.time()+0.5
  1505.             img.clear(RGB_GRAY)
  1506.             slidebar()
  1507.             plots=(cv.xp, cv.yp, cv.xp+cv.x2+cv.xyt, cv.yp+cv.y2+cv.xyt)
  1508.             img.pieslice(plots, radian(180-cv.rx+cv.rxy),radian(180-cv.ry+cv.rxy), outline=RGB_BLUE,fill=RGB_YELLOW,width=10)
  1509.             cv.x=0
  1510.             cv.rx=cv.rx%360
  1511.             cv.ry=cv.ry%360
  1512.             img.text((10,20),u''+str(plots),RGB_WHITE)
  1513.             img.text((10,40),u''+str((cv.rx+cv.rxy,cv.ry+cv.rxy)),RGB_WHITE)
  1514.         elif go == '$sqdance':
  1515.             mosaic()
  1516.         elif go == '$xyzircle':
  1517.             img.clear(RGB_BLACK)
  1518.             radius=(cv.xyz[2]*-2)+120
  1519.             point_x=(-cv.xyz[0]+60) * x_scr / 120
  1520.             point_y=(cv.xyz[1]+60) * y_scr / 120
  1521.             circle(point_x,point_y,radius,(255,0,0))
  1522.         elif go == '$360arrow':
  1523.             img.clear(RGB_PURPLE)
  1524.             angle_rad=cv.angle * 2 * math.pi / cv.of_angles
  1525.             rot_points=[]
  1526.             for point in arrow_points:
  1527.                 x, y=point
  1528.                 rot_x=math.cos(angle_rad) * x-math.sin(angle_rad) * y
  1529.                 rot_y=math.sin(angle_rad) * x+math.cos(angle_rad) * y
  1530.                 rot_x *= cv.zoom
  1531.                 rot_y *= cv.zoom
  1532.                 rot_x += x_mid
  1533.                 rot_y += y_mid
  1534.                 rot_points.append((rot_x, rot_y))
  1535.             if not delay('skip',0.01):
  1536.                 del t.skip
  1537.                 cv.angle=(cv.angle+3) % cv.of_angles
  1538.             img.polygon(rot_points, fill=RGB_YELLOW, width=2)
  1539.         elif go == '$snowfall':
  1540.             img.clear(0x000080)
  1541.             for i in range(nflakes):
  1542.                 dir=random.choice([-1,1])
  1543.                 moveX[i]=dir*moveX[i]
  1544.                 XX[i]=XX[i]+moveX[i]
  1545.                 YY[i]=YY[i]+moveY[i]
  1546.                 if((XX[i]>x_scr)or(XX[i]<0)):
  1547.                     moveX[i]=-moveX[i]
  1548.                 if(YY[i]>640): # y_scr
  1549.                     YY[i]=moveY[i]=random.randint(1,10)
  1550.                 img.ellipse([(XX[i],YY[i]),(XX[i]+imgSnow,YY[i]+imgSnow)],RGB_WHITE,fill=RGB_WHITE)
  1551.         elif go == '$textscroll':
  1552.             if cv.init:
  1553.                 W,G,Y=((255,255,255),(0,255,0),(255,255,0))
  1554.                 f1,f2=(u'Normal', 30), (u'Normal', 25)
  1555.                 t1=u'Programming PyS60'
  1556.                 t2=u'A Vertical Text Scroll Demo'
  1557.                 print img.measure_text(t1,f1)
  1558.                 print img.measure_text(t2,f2)
  1559.                 a=img.measure_text(t1,f1)[0][2]
  1560.                 b=img.measure_text(t2,f2)[0][2]
  1561.                 i=cv.init=0
  1562.                 w,h=x_scr,y_scr
  1563.                 distance=h/2-30
  1564.             t2h=(h-25)-i*5
  1565.             color=int(255+i*2)
  1566.             Bg=(0,color,255)
  1567.             img.clear(Bg)
  1568.             img.text((5,20),u'* Length: %s'% [t2h,distance], Y)
  1569.             img.text(((w-b)/2,(h-25)-i*5),t2,0,f2)
  1570.             img.text(((w-b)/2-2,(h-25-1)-i*5-2),t2,G,f2)
  1571.             img.text(((w-a)/2,35+i*5),t1,0,f1)
  1572.             img.text(((w-a)/2-2,35-1+i*5-2),t1,Y,f1)
  1573.             img.rectangle((0,0,w,h),Y)
  1574.             if t2h < distance:
  1575.                 e32.ao_sleep(2)
  1576.                 i=0
  1577.             i+=1
  1578.         elif go == '$text2fit' and cv.init:
  1579.             img.clear(0x5555ff)
  1580.             o1st_entry='This is a "measure_text" wrap method example whereas...'
  1581.             o2nd_entry=['blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah',
  1582.                     'should','appear','segmented','to','stay','within','the','given','boundary.']
  1583.             #
  1584.             text2wrap('',o1st_entry)
  1585.             for divided in o2nd_entry:
  1586.                 text2wrap(wrapped,divided)
  1587.             text2wrap(0xffffff)
  1588.             cv.init=0
  1589.         elif go == '-':
  1590.             pass
  1591.         draw(())
  1592.         cv.incr=0
  1593.  
  1594.     e32.ao_yield()
  1595.  
  1596. try:
  1597.     sens.stop_listening()
  1598. except: pass
  1599. try:
  1600.     timer.cancel()
  1601. except: pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement