|
1 # Copyright (c) 2005 Nokia Corporation |
|
2 # |
|
3 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 # you may not use this file except in compliance with the License. |
|
5 # You may obtain a copy of the License at |
|
6 # |
|
7 # http://www.apache.org/licenses/LICENSE-2.0 |
|
8 # |
|
9 # Unless required by applicable law or agreed to in writing, software |
|
10 # distributed under the License is distributed on an "AS IS" BASIS, |
|
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 # See the License for the specific language governing permissions and |
|
13 # limitations under the License. |
|
14 |
|
15 import appuifw |
|
16 from graphics import * |
|
17 import e32 |
|
18 from key_codes import * |
|
19 |
|
20 class Keyboard(object): |
|
21 def __init__(self,onevent=lambda:None): |
|
22 self._keyboard_state={} |
|
23 self._downs={} |
|
24 self._onevent=onevent |
|
25 def handle_event(self,event): |
|
26 if event['type'] == appuifw.EEventKeyDown: |
|
27 code=event['scancode'] |
|
28 if not self.is_down(code): |
|
29 self._downs[code]=self._downs.get(code,0)+1 |
|
30 self._keyboard_state[code]=1 |
|
31 elif event['type'] == appuifw.EEventKeyUp: |
|
32 self._keyboard_state[event['scancode']]=0 |
|
33 self._onevent() |
|
34 def is_down(self,scancode): |
|
35 return self._keyboard_state.get(scancode,0) |
|
36 def pressed(self,scancode): |
|
37 if self._downs.get(scancode,0): |
|
38 self._downs[scancode]-=1 |
|
39 return True |
|
40 return False |
|
41 keyboard=Keyboard() |
|
42 |
|
43 appuifw.app.screen='full' |
|
44 img=None |
|
45 def handle_redraw(rect): |
|
46 if img: |
|
47 canvas.blit(img) |
|
48 appuifw.app.body=canvas=appuifw.Canvas( |
|
49 event_callback=keyboard.handle_event, |
|
50 redraw_callback=handle_redraw) |
|
51 img=Image.new(canvas.size) |
|
52 |
|
53 running=1 |
|
54 def quit(): |
|
55 global running |
|
56 running=0 |
|
57 appuifw.app.exit_key_handler=quit |
|
58 |
|
59 location=[img.size[0]/2,img.size[1]/2] |
|
60 speed=[0.,0.] |
|
61 blobsize=16 |
|
62 xs,ys=img.size[0]-blobsize,img.size[1]-blobsize |
|
63 gravity=0.03 |
|
64 acceleration=0.05 |
|
65 |
|
66 import time |
|
67 start_time=time.clock() |
|
68 n_frames=0 |
|
69 # To speed things up, we prerender the text. |
|
70 labeltext=u'Use arrows to move ball' |
|
71 textrect=img.measure_text(labeltext, font='normal')[0] |
|
72 text_img=Image.new((textrect[2]-textrect[0],textrect[3]-textrect[1])) |
|
73 text_img.clear(0) |
|
74 text_img.text((-textrect[0],-textrect[1]),labeltext,fill=0xffffff,font='normal') |
|
75 |
|
76 while running: |
|
77 img.clear(0) |
|
78 img.blit(text_img, (0,0)) |
|
79 img.point((location[0]+blobsize/2,location[1]+blobsize/2), |
|
80 0x00ff00,width=blobsize) |
|
81 handle_redraw(()) |
|
82 e32.ao_yield() |
|
83 speed[0]*=0.999 |
|
84 speed[1]*=0.999 |
|
85 speed[1]+=gravity |
|
86 location[0]+=speed[0] |
|
87 location[1]+=speed[1] |
|
88 if location[0]>xs: |
|
89 location[0]=xs-(location[0]-xs) |
|
90 speed[0]=-0.80*speed[0] |
|
91 speed[1]=0.90*speed[1] |
|
92 if location[0]<0: |
|
93 location[0]=-location[0] |
|
94 speed[0]=-0.80*speed[0] |
|
95 speed[1]=0.90*speed[1] |
|
96 if location[1]>ys: |
|
97 location[1]=ys-(location[1]-ys) |
|
98 speed[0]=0.90*speed[0] |
|
99 speed[1]=-0.80*speed[1] |
|
100 if location[1]<0: |
|
101 location[1]=-location[1] |
|
102 speed[0]=0.90*speed[0] |
|
103 speed[1]=-0.80*speed[1] |
|
104 |
|
105 if keyboard.is_down(EScancodeLeftArrow): speed[0] -= acceleration |
|
106 if keyboard.is_down(EScancodeRightArrow): speed[0] += acceleration |
|
107 if keyboard.is_down(EScancodeDownArrow): speed[1] += acceleration |
|
108 if keyboard.is_down(EScancodeUpArrow): speed[1] -= acceleration |
|
109 if keyboard.pressed(EScancodeHash): |
|
110 filename=u'e:\\screenshot.png' |
|
111 canvas.text((0,32),u'Saving screenshot to:',fill=0xffff00) |
|
112 canvas.text((0,48),filename,fill=0xffff00) |
|
113 img.save(filename) |
|
114 |
|
115 n_frames+=1 |
|
116 end_time=time.clock() |
|
117 total=end_time-start_time |
|
118 |
|
119 print "%d frames, %f seconds, %f FPS, %f ms/frame."%(n_frames,total, |
|
120 n_frames/total, |
|
121 total/n_frames*1000.) |