@@ -33,7 +33,7 @@ def __getattr__(self, attr):
33
33
BUTTON_COLUMN_WIDTH = (SCREEN_WIDTH / BUTTON_COLUMNS )
34
34
BUTTON_ROW_HEIGHT = (SCREEN_HEIGHT / BUTTON_ROWS )
35
35
36
- BOUNCE_LIMIT = 3
36
+ BUTTON_TIMEOUT = 0.3
37
37
38
38
test_button_layout = [
39
39
dotdict ({ 'x' :0 , 'y' :0 , 'width' :2 , 'height' :1 , 'text' :"Power" , 'code' :"RM-ED050-12 KEY_POWER;Phillips-HTS KEY_POWER" }),
@@ -90,6 +90,17 @@ def render_current_button(self):
90
90
self .dirty_rect .union_ip (self .current_button )
91
91
self .current_button .render (self .screen )
92
92
93
+ def release_current_button (self ):
94
+ self .current_button .highlight (False )
95
+ self .render_current_button ()
96
+ self .current_button = None
97
+
98
+ def update_display (self ):
99
+ if self .display_dirty :
100
+ pygame .display .update (self .dirty_rect )
101
+ self .display_dirty = False
102
+ self .dirty_rect .inflate_ip (- self .dirty_rect .w , - self .dirty_rect .h )
103
+
93
104
def run (self ):
94
105
signal .signal (signal .SIGTERM , signal_handler )
95
106
signal .signal (signal .SIGINT , signal_handler )
@@ -113,49 +124,52 @@ def run(self):
113
124
114
125
self .display_dirty = False
115
126
self .current_button = None
116
- self .loop_count = 0
117
127
self .dirty_rect = pygame .Rect (0 , 0 , 0 , 0 )
118
128
119
129
while running :
120
- self .loop_count += 1
121
- if self .display_dirty :
122
- pygame .display .update (self .dirty_rect )
123
- self .display_dirty = False
124
- self .dirty_rect .inflate_ip (- self .dirty_rect .w , - self .dirty_rect .h )
125
-
126
- # Can't use pygame.event.wait() as this blocks signals
127
- time .sleep (0.008 )
128
- event = pygame .event .poll ()
129
130
130
- while event .type != pygame .NOEVENT :
131
- if event .type == pygame .MOUSEBUTTONDOWN and self .current_button == None :
132
- #print self.loop_count,":",event
133
- for button in self .buttons :
134
- if button .hit_test (event .pos ):
135
- self .current_button = button
136
- button .highlight (True )
137
- self .render_current_button ()
138
- self .send_ir (self .current_button .code )
139
- break
140
-
141
- if event .type == pygame .MOUSEMOTION and self .current_button :
142
- if not self .current_button .hit_test (event .pos ):
143
- #print self.loop_count,":",event
144
- self .current_button .highlight (False )
145
- self .render_current_button ()
146
- self .current_button = None
147
-
148
- if event .type == pygame .MOUSEBUTTONUP :
149
- #print self.loop_count,":",event
150
- if self .current_button :
151
- self .current_button .highlight (False )
152
- self .render_current_button ()
153
- self .current_button = None
154
-
155
- if event .type == pygame .QUIT :
156
- running = False
157
-
131
+ waiting_for_input = True
132
+
133
+ while running and waiting_for_input :
134
+ self .update_display ()
135
+
136
+ # Can't use pygame.event.wait() as this blocks signals
137
+ time .sleep (0.008 )
158
138
event = pygame .event .poll ()
139
+
140
+ while event .type != pygame .NOEVENT :
141
+ if event .type == pygame .MOUSEBUTTONDOWN and self .current_button == None :
142
+ for button in self .buttons :
143
+ if button .hit_test (event .pos ):
144
+ self .current_button = button
145
+ button .highlight (True )
146
+ self .render_current_button ()
147
+ self .press_time = time .time ()
148
+ waiting_for_input = False
149
+ pygame .event .clear ()
150
+ break
151
+
152
+ if event .type == pygame .MOUSEBUTTONUP and self .current_button :
153
+ self .release_current_button ()
154
+
155
+ if event .type == pygame .MOUSEMOTION and self .current_button :
156
+ if not self .current_button .hit_test (event .pos ):
157
+ self .release_current_button ()
158
+
159
+ if event .type == pygame .QUIT :
160
+ running = False
161
+
162
+ event = pygame .event .poll ()
163
+
164
+ if self .current_button and time .time () - self .press_time > BUTTON_TIMEOUT :
165
+ self .release_current_button ()
166
+
167
+ if not waiting_for_input :
168
+ self .update_display ()
169
+ pygame .event .set_blocked ([pygame .MOUSEBUTTONDOWN , pygame .MOUSEBUTTONUP , pygame .MOUSEMOTION ])
170
+ self .send_ir (self .current_button .code )
171
+ time .sleep (0.1 )
172
+ pygame .event .set_allowed ([pygame .MOUSEBUTTONDOWN , pygame .MOUSEBUTTONUP , pygame .MOUSEMOTION ])
159
173
160
174
IR .deinit ()
161
175
0 commit comments