Advertisement
here2share

# count_down_reset.py

Jan 9th, 2015
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # count_down_reset.py
  2.  
  3. # Copyright (c) 2008 Jouni Miettunen
  4. # http://jouni.miettunen.googlepages.com/
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. #     http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17.  
  18. import e32
  19. import appuifw
  20. import graphics
  21. import key_codes
  22.  
  23. VERSION = 1.1
  24.  
  25. # Color definitions
  26. RGB_BLACK = (0,0,0)
  27.  
  28. RGB_Shift = [
  29.     (255,0,0),
  30.     (255,45,0),
  31.     (255,90,0),
  32.     (255,135,0),
  33.     (255,180,0),
  34.     (255,225,0),
  35.     (255,255,0),
  36.     (160,255,0),
  37.     (80,255,0),
  38.     (0,255,0)]
  39.  
  40. # Global variables with safe default values
  41. canvas = None
  42. img = None
  43. gCount = 9
  44. gColor = RGB_Shift[9]
  45.  
  46. # Create timer
  47. gTimer = e32.Ao_timer()
  48.  
  49. # Speed up drawing by calculating points beforehands
  50. #   a1   a2
  51. #   b1   b2
  52. #   c1   c2
  53. gPointA1 = (0,0)
  54. gPointA2 = (0,0)
  55. gPointB1 = (0,0)
  56. gPointB2 = (0,0)
  57. gPointC1 = (0,0)
  58. gPointC2 = (0,0)
  59.  
  60. def draw(rect):
  61.     try: canvas.blit(img)
  62.     except: pass
  63.  
  64. def draw_count(aValue):
  65.    # Draw a number, stop at negative values
  66.    if aValue < 0: return
  67.    global gCount
  68.    gCount = aValue
  69.  
  70.    # Set coordinates for drawing new number
  71.    points = []
  72.    if gCount == 0:
  73.       points += gPointA1 + gPointA2 + gPointB2 + gPointC2 + gPointC1 + gPointB1 + gPointA1
  74.    elif gCount == 1:
  75.       points += gPointA2 + gPointB2 + gPointC2
  76.    elif gCount == 2:
  77.       points += gPointA1 + gPointA2 + gPointB2 + gPointB1 + gPointC1 + gPointC2
  78.    elif gCount == 3:
  79.       points += gPointA1 + gPointA2 + gPointB2 + gPointB1 + gPointB2 + gPointC2 + gPointC1
  80.    elif gCount == 4:
  81.       points += gPointA1 + gPointB1 + gPointB2 + gPointA2 + gPointC2
  82.    elif gCount == 5:
  83.       points += gPointA2 + gPointA1 + gPointB1 + gPointB2 + gPointC2 + gPointC1
  84.    elif gCount == 6:
  85.       points += gPointA2 + gPointA1 + gPointC1 + gPointC2 + gPointB2 + gPointB1
  86.    elif gCount == 7:
  87.       points += gPointA1 + gPointA2 + gPointC2
  88.    elif gCount == 8:
  89.       points += gPointB1 + gPointA1 + gPointA2 + gPointC2 + gPointC1 + gPointB1 + gPointB2
  90.    elif gCount == 9:
  91.       points += gPointB2 + gPointB1 + gPointA1 + gPointA2 + gPointC2 + gPointC1
  92.    else:
  93.       # Should never get here, but avoid problems anyway
  94.       gCount = 0
  95.       points = gPointA1
  96.  
  97.    # Color depends on number, just for fun
  98.    gColor = RGB_Shift[gCount]
  99.  
  100.    # Remove old and draw new number
  101.    # Version 1.0 colors
  102.    # img.clear(RGB_BLACK)
  103.    # img.line(points, width=40, outline=gColor)
  104.    # Version 1.1 colors
  105.    img.clear(gColor)
  106.    img.line(points, width=40, outline=RGB_BLACK)
  107.    draw(())
  108.  
  109.    # Must reset timer, user can restart at any time
  110.    gTimer.cancel()
  111.    # Next number in one second
  112.    gTimer.after(1,lambda:draw_count(gCount-1))
  113.  
  114. def cb_handle_redraw(aRect=(0,0,0,0)):
  115.    if not canvas: return
  116.    if img: canvas.blit(img)
  117.  
  118. def cb_handle_resize(aSize=(0,0,0,0)):
  119.    global canvas, img
  120.    if not canvas: return
  121.  
  122.    # Initialize drawing coordinate
  123.    if img: del img
  124.    img = graphics.Image.new(canvas.size)
  125.    x,y = canvas.size
  126.    x1 = 40       # my draw limit at left column
  127.    x2 = x - 40   # my draw limit at right column
  128.    x5 = x/2      # my draw limit at middle column
  129.    y1 = 40       # my draw limit at top row
  130.    y2 = y - 40   # my draw limit at bottom row
  131.    y5 = y/2      # my draw limit at middle row
  132.  
  133.    # Define new drawing coordinates in advance
  134.    global gPointA1, gPointA2, gPointB1, gPointB2, gPointC1, gPointC2
  135.    gPointA1 = (x1, y1)
  136.    gPointA2 = (x2, y1)
  137.    gPointB1 = (x1, y5)
  138.    gPointB2 = (x2, y5)
  139.    gPointC1 = (x1, y2)
  140.    gPointC2 = (x2, y2)
  141.  
  142.    # Draw last number again due screen resize request
  143.    draw_count(gCount)
  144.    cb_handle_redraw()
  145.  
  146. def menu_start():
  147.   draw_count(9)
  148.  
  149. def cb_quit():
  150.   gTimer.cancel()
  151.   app_lock.signal()
  152.  
  153. #############################################################
  154.  
  155. canvas = appuifw.Canvas(
  156.        resize_callback = cb_handle_resize,
  157.        redraw_callback = cb_handle_redraw)
  158.  
  159. appuifw.app.directional_pad = False
  160. appuifw.app.screen = 'large'
  161. appuifw.app.title = u'Count Down'
  162. appuifw.app.exit_key_handler = cb_quit
  163.  
  164. # Calls automatically resize_callback and redraw_callback
  165. appuifw.app.body = canvas
  166. img = graphics.Image.new(canvas.size)
  167.  
  168. appuifw.app.menu = [
  169.     (u"Start", menu_start),
  170.     (u"Exit", cb_quit)
  171.     ]
  172.  
  173. # Key handling
  174. canvas.bind(key_codes.EKeyEnter, menu_start)
  175. canvas.bind(key_codes.EKeySelect, menu_start)
  176. canvas.bind(key_codes.EKey0, lambda: draw_count(0))
  177. canvas.bind(key_codes.EKey1, lambda: draw_count(1))
  178. canvas.bind(key_codes.EKey2, lambda: draw_count(2))
  179. canvas.bind(key_codes.EKey3, lambda: draw_count(3))
  180. canvas.bind(key_codes.EKey4, lambda: draw_count(4))
  181. canvas.bind(key_codes.EKey5, lambda: draw_count(5))
  182. canvas.bind(key_codes.EKey6, lambda: draw_count(6))
  183. canvas.bind(key_codes.EKey7, lambda: draw_count(7))
  184. canvas.bind(key_codes.EKey8, lambda: draw_count(8))
  185. canvas.bind(key_codes.EKey9, lambda: draw_count(9))
  186.  
  187. #############################################################
  188. app_lock = e32.Ao_lock()
  189. app_lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement