I saw someone write a fileselect dialog a few days ago.
I think we should have more dialog type to use in our apps.
So, I write a color picker code below.
It can be converted easily into a dialog component.
1
2 // Usual code as always
3 import e32
4 from appuifw import *
5 from key_codes import *
6
7 class Keyboard(object):
8 def __init__(self):
9 self.state = {}
10 self.buffer= {}
11 def handle_event(self, event):
12 code = event['scancode']
13 if event['type'] == EEventKeyDown:
14 self.buffer[code]= 1
15 self.state[code] = 1
16 elif event['type'] == EEventKeyUp:
17 self.state[code] = 0
18 def pressing(self, code):
19 return self.state.get(code,0)
20 def pressed(self, code):
21 if self.buffer.get(code,0):
22 self.buffer[code] = 0
23 return 1
24 return 0
25
26 key = Keyboard()
27 app.body = canvas = Canvas(event_callback=key.handle_event)
28
29 def quit():
30 global running
31 running = 0
32
33 app.exit_key_handler = quit
34 running = 1
At first, I wanted to use the
standard palette of s60.
But later decide to just use 216 web-safe colors.
The code is simpler, so the concept is easier to understand.
1
2 ff00 = range(0xff, -1, -0x33)
3 pal = [(r,g,b) for r in ff00 for g in ff00 for b in ff00]
4 map_j = range(0,12,2)+range(11,0,-2)
5 for j in range(12):
6 for i in range(18):
7 k = 18*map_j[j] + i
8 canvas.rectangle([(9*i+1, 9*j+1), (9*i+9, 9*j+9)], None, pal[k])
9
10 def clear_box(color=0xffffff):
11 global x,y
12 canvas.rectangle([(9*x, 9*y), (9*x+10, 9*y+10)], color)
13
14 x, y = 0, 0
15 black_white = 0
16 while running:
17 if key.pressed(EScancodeLeftArrow):
18 clear_box()
19 if x > 0: x -= 1
20 if key.pressed(EScancodeRightArrow):
21 clear_box()
22 if x < 17: x += 1
23 if key.pressed(EScancodeUpArrow):
24 clear_box()
25 if y > 0: y -= 1
26 if key.pressed(EScancodeDownArrow):
27 clear_box()
28 if y < 11: y += 1
29 if key.pressed(EScancodeSelect):
30 color = pal[18*map_j[y] + x]
31 canvas.rectangle([(1,109), (17*9+9,130)], None, color)
32 black_white ^= 0xffffff
33 clear_box(black_white)
34 e32.ao_sleep(0.2)
See
Screenshot.