Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Drawing an arrow cursor

Calling polygon may be faster than a blit.
   1  
   2  x,y = 20,20
   3  arrow = [(0,0), (0,10), (2,8), (4,12), (6,11), (4,7), (7,7)]
   4  canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)

Here's an example app. It let you move cursor around.
When you press the select key, a red circle is drawn.
The longer you press, the bigger the circle.

Start with the usual stuff
   1  
   2  import e32
   3  from appuifw import *
   4  from key_codes import *
   5  
   6  class Keyboard(object):
   7      def __init__(self):
   8          self.state = {}  # is this key pressing ?
   9          self.buffer= {}  # is it waiting to be processed ?
  10      def handle_event(self, event): # for event_callback
  11          code = event['scancode']
  12          if event['type'] == EEventKeyDown:
  13              self.buffer[code]= 1   # put into queue
  14              self.state[code] = 1
  15          elif event['type'] == EEventKeyUp:
  16              self.state[code] = 0
  17      def pressing(self, code):      # just check
  18          return self.state.get(code,0)
  19      def pressed(self, code):       # check and process the event
  20          if self.buffer.get(code,0):
  21              self.buffer[code] = 0  # take out of queue
  22              return 1
  23          return 0
  24  
  25  key = Keyboard()
  26  app.body = canvas = Canvas(event_callback=key.handle_event)
  27  
  28  def quit():
  29      global running
  30      running = 0
  31  
  32  app.exit_key_handler = quit
  33  running = 1

Then the main part of this specific app.
   1  
   2  x,y = 20,20
   3  arrow = [(0,0), (0,10), (2,8), (4,12), (6,11), (4,7), (7,7)]
   4  
   5  while running:
   6      if key.pressing(EScancodeUpArrow):
   7          y -= 1
   8      if key.pressing(EScancodeDownArrow):
   9          y += 1
  10      if key.pressing(EScancodeLeftArrow):
  11          x -= 1
  12      if key.pressing(EScancodeRightArrow):
  13          x += 1
  14      if key.pressed(EScancodeSelect):
  15          r = 1
  16          while key.pressing(EScancodeSelect):
  17              r += 1       # bigger red circle
  18              canvas.ellipse([(x-r,y-r),(x+r,y+r)], fill=0xff0000)
  19              canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)
  20              e32.ao_sleep(0.03)
  21  
  22      canvas.clear()
  23      canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)
  24      e32.ao_sleep(0.01)

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS