comparison src/parpg/gamescenecontroller.py @ 25:aa3d837024a3

Scrolling by keyboard is now possible. * Patch by DomtronVox. * Added scrolling with the arrow keys. * Added a slider bar in the settings menu to allow players to set the scrolling speed. * Fixed an error that occurred when the ok button in settings was pressed. * There is some kind of focus issue where opening the settings menu in-game causes scrolling to be disabled. Workaround is to change the scroll speed setting whenever you open the settings menu.
author DomtronVox
date Fri, 17 Jun 2011 14:49:48 -1000
parents d60f1dab8469
children 94cb5843dcbb
comparison
equal deleted inserted replaced
24:ca8b9588bb48 25:aa3d837024a3
80 self.action_number = 1 80 self.action_number = 1
81 81
82 self.has_mouse_focus = True 82 self.has_mouse_focus = True
83 self.last_mousecoords = None 83 self.last_mousecoords = None
84 self.mouse_callback = None 84 self.mouse_callback = None
85 self.original_cursor_id = self.engine.getCursor().getId() 85 self.original_cursor_id = self.engine.getCursor().getId()
86 self.scroll_direction = [0, 0] 86 self.scroll_data = {"mouse":[], "kb":[], "offset":[0,0]}
87 self.scroll_timer = extensions.fife_timer.Timer(100, 87 self.scroll_timer = extensions.fife_timer.Timer(
88 lambda: self.view.moveCamera \ 88 100,
89 (self.scroll_direction)) 89 lambda: self.view.moveCamera(self.scroll_data["offset"]),
90 )
90 91
91 #this is temporary until we can set the native cursor 92 #this is temporary until we can set the native cursor
92 self.resetMouseCursor() 93 self.resetMouseCursor()
93 self.paused = False 94 self.paused = False
94 95
178 if(key_val == key.PAUSE): 179 if(key_val == key.PAUSE):
179 # Pause pause/unpause the game 180 # Pause pause/unpause the game
180 self.model.togglePause() 181 self.model.togglePause()
181 self.pause(False) 182 self.pause(False)
182 if(key_val == key.SPACE): 183 if(key_val == key.SPACE):
183 self.model.active_map.centerCameraOnPlayer() 184 self.model.active_map.centerCameraOnPlayer()
185
186 #alter scroll data if a directional key is hit
187 if(key_val == key.UP):
188 if not "up" in self.scroll_data["kb"]:
189 self.scroll_data["kb"].append("up")
190
191 if(key_val == key.RIGHT):
192 if not "right" in self.scroll_data["kb"]:
193 self.scroll_data["kb"].append("right")
194
195 if(key_val == key.DOWN):
196 if not "down" in self.scroll_data["kb"]:
197 self.scroll_data["kb"].append("down")
198
199 if(key_val == key.LEFT):
200 if not "left" in self.scroll_data["kb"]:
201 self.scroll_data["kb"].append("left")
184 202
203 def keyReleased(self, evt):
204 """Whenever a key is pressed, fife calls this routine.
205 @type evt: fife.event
206 @param evt: The event that fife caught
207 @return: None"""
208 key = evt.getKey()
209 key_val = key.getValue()
210
211 #alter scroll data if a directional key is released
212 if(key_val == key.UP):
213 if "up" in self.scroll_data["kb"]:
214 self.scroll_data["kb"].remove("up")
215
216 if(key_val == key.RIGHT):
217 if "right" in self.scroll_data["kb"]:
218 self.scroll_data["kb"].remove("right")
219
220 if(key_val == key.DOWN):
221 if "down" in self.scroll_data["kb"]:
222 self.scroll_data["kb"].remove("down")
223
224 if(key_val == key.LEFT):
225 if "left" in self.scroll_data["kb"]:
226 self.scroll_data["kb"].remove("left")
227
185 def mouseReleased(self, evt): 228 def mouseReleased(self, evt):
186 """If a mouse button is released, fife calls this routine. 229 """If a mouse button is released, fife calls this routine.
187 We want to wait until the button is released, because otherwise 230 We want to wait until the button is released, because otherwise
188 pychan captures the release if a menu is opened. 231 pychan captures the release if a menu is opened.
189 @type evt: fife.event 232 @type evt: fife.event
233 return 276 return
234 cursor = self.engine.getCursor() 277 cursor = self.engine.getCursor()
235 #this can be helpful for IDEs code analysis 278 #this can be helpful for IDEs code analysis
236 if False: 279 if False:
237 assert(isinstance(cursor, fife.Cursor)) 280 assert(isinstance(cursor, fife.Cursor))
238 self.last_mousecoords = fife.ScreenPoint(cursor.getX(), 281 self.last_mousecoords = fife.ScreenPoint(cursor.getX(), cursor.getY())
239 cursor.getY()) 282 self.view.highlightFrontObject(self.last_mousecoords)
240 self.view.highlightFrontObject(self.last_mousecoords)
241 283
242 #set the trigger area in pixles 284 #set the trigger area in pixles
243 pixle_edge = 20 285 pixle_edge = 20
244 286
245 mouse_x = self.last_mousecoords.x 287 mouse_x = self.last_mousecoords.x
250 image = None 292 image = None
251 settings = self.model.settings 293 settings = self.model.settings
252 294
253 295
254 #edge logic 296 #edge logic
255 self.scroll_direction = [0, 0]
256 if self.has_mouse_focus: 297 if self.has_mouse_focus:
257 direction = self.scroll_direction 298 direction = self.scroll_data["mouse"] = []
299
258 #up 300 #up
259 if mouse_y <= pixle_edge: 301 if mouse_y <= pixle_edge:
260 direction[0] += 1 302 direction.append("up")
261 direction[1] -= 1
262 image = '/'.join(['gui/cursors', settings.parpg.CursorUp]) 303 image = '/'.join(['gui/cursors', settings.parpg.CursorUp])
263 304
264 #right 305 #right
265 if mouse_x >= screen_width - pixle_edge: 306 if mouse_x >= screen_width - pixle_edge:
266 direction[0] += 1 307 direction.append("right")
267 direction[1] += 1
268 image = '/'.join(['gui/cursors', settings.parpg.CursorRight]) 308 image = '/'.join(['gui/cursors', settings.parpg.CursorRight])
269 309
270 #down 310 #down
271 if mouse_y >= screen_height - pixle_edge: 311 if mouse_y >= screen_height - pixle_edge:
272 direction[0] -= 1 312 direction.append("down")
273 direction[1] += 1
274 image = '/'.join(['gui/cursors', settings.parpg.CursorDown]) 313 image = '/'.join(['gui/cursors', settings.parpg.CursorDown])
275 314
276 #left 315 #left
277 if mouse_x <= pixle_edge: 316 if mouse_x <= pixle_edge:
278 direction[0] -= 1 317 direction.append("left")
279 direction[1] -= 1
280 image = '/'.join(['gui/cursors', settings.parpg.CursorLeft]) 318 image = '/'.join(['gui/cursors', settings.parpg.CursorLeft])
281 319
282 if image is not None and not data_drag.dragging: 320 if image is not None and not data_drag.dragging:
283 self.setMouseCursor(image, image) 321 self.setMouseCursor(image, image)
284 322
285 323
286 def handleCommands(self): 324 def handleCommands(self):
315 # At this point we sync the contents of the ready slots 353 # At this point we sync the contents of the ready slots
316 # with the contents of the inventory. 354 # with the contents of the inventory.
317 self.view.hud.inventory = None 355 self.view.hud.inventory = None
318 self.view.hud.initializeInventory() 356 self.view.hud.initializeInventory()
319 self.pause(False) 357 self.pause(False)
358
359 def handleScrolling(self):
360 """
361 Merge kb and mouse related scroll data, limit the speed and
362 move the camera.
363 """
364 #this is how many pxls the camera is moved in one time frame
365 scroll_offset = self.scroll_data["offset"] = [0,0]
366
367 mouse = self.scroll_data["mouse"]
368 keyboard = self.scroll_data["kb"]
369 speed = self.model.settings.parpg.ScrollSpeed
370
371 #adds a value to the offset depending on the contents of each
372 # of the controllers: set() removes doubles
373 scroll_direction = set(mouse+keyboard)
374 for direction in scroll_direction:
375 if direction == "up":
376 scroll_offset[0] +=1
377 scroll_offset[1] -=1
378 elif direction == "right":
379 scroll_offset[0] +=1
380 scroll_offset[1] +=1
381 elif direction == "down":
382 scroll_offset[0] -=1
383 scroll_offset[1] +=1
384 elif direction == "left":
385 scroll_offset[0] -=1
386 scroll_offset[1] -=1
387
388 #keep the speed within bounds
389 if scroll_offset[0] > 0: scroll_offset[0] = speed
390 if scroll_offset[0] < 0: scroll_offset[0] = -speed
391
392 if scroll_offset[1] > 0: scroll_offset[1] = speed
393 if scroll_offset[1] < 0: scroll_offset[1] = -speed
394
395 #de/activate scrolling
396 if scroll_offset != [0, 0]:
397 self.scroll_timer.start()
398 else:
399 self.scroll_timer.stop()
400 if not data_drag.dragging:
401 self.resetMouseCursor()
320 402
321 def nullFunc(self, userdata): 403 def nullFunc(self, userdata):
322 """Sample callback for the context menus.""" 404 """Sample callback for the context menus."""
323 logger.info(userdata) 405 logger.info(userdata)
324 406
431 def onCommand(self, command): 513 def onCommand(self, command):
432 if(command.getCommandType() == fife.CMD_MOUSE_FOCUS_GAINED): 514 if(command.getCommandType() == fife.CMD_MOUSE_FOCUS_GAINED):
433 self.has_mouse_focus = True 515 self.has_mouse_focus = True
434 elif(command.getCommandType() == fife.CMD_MOUSE_FOCUS_LOST): 516 elif(command.getCommandType() == fife.CMD_MOUSE_FOCUS_LOST):
435 self.has_mouse_focus = False 517 self.has_mouse_focus = False
436 518
437 def pump(self): 519 def pump(self):
438 """Routine called during each frame. Our main loop is in ./run.py""" 520 """Routine called during each frame. Our main loop is in ./run.py"""
439 # uncomment to instrument 521 # uncomment to instrument
440 # t0 = time.time() 522 # t0 = time.time()
441 if self.paused: 523 if self.paused:
442 return 524 return
443 self.updateMouse() 525 self.updateMouse()
444 if self.model.active_map: 526 if self.model.active_map:
445 self.view.highlightFrontObject(self.last_mousecoords) 527 self.view.highlightFrontObject(self.last_mousecoords)
446 self.view.refreshTopLayerTransparencies() 528 self.view.refreshTopLayerTransparencies()
447 if self.scroll_direction != [0, 0]: 529 self.handleScrolling()
448 self.scroll_timer.start()
449 else:
450 self.scroll_timer.stop()
451 if not data_drag.dragging:
452 self.resetMouseCursor()
453
454 self.handleCommands() 530 self.handleCommands()
455 # print "%05f" % (time.time()-t0,) 531 # print "%05f" % (time.time()-t0,)