diff gamescenecontroller.py @ 4:bf1dd9c24a7e

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 06145a6ee387
children 708a6f651c31
line wrap: on
line diff
--- a/gamescenecontroller.py	Fri Jun 10 11:29:38 2011 -1000
+++ b/gamescenecontroller.py	Fri Jun 17 14:49:48 2011 -1000
@@ -82,11 +82,12 @@
         self.has_mouse_focus = True
         self.last_mousecoords = None
         self.mouse_callback = None
-        self.original_cursor_id = self.engine.getCursor().getId()        
-        self.scroll_direction = [0, 0]
-        self.scroll_timer = extensions.fife_timer.Timer(100,
-                                          lambda: self.view.moveCamera \
-                                                   (self.scroll_direction))    
+        self.original_cursor_id = self.engine.getCursor().getId()
+        self.scroll_data = {"mouse":[], "kb":[], "offset":[0,0]}
+        self.scroll_timer = extensions.fife_timer.Timer(
+            100,
+            lambda: self.view.moveCamera(self.scroll_data["offset"]),
+        )
         
         #this is temporary until we can set the native cursor
         self.resetMouseCursor()
@@ -180,8 +181,50 @@
             self.model.togglePause()
             self.pause(False)
         if(key_val == key.SPACE):
-            self.model.active_map.centerCameraOnPlayer() 
+            self.model.active_map.centerCameraOnPlayer()
+        
+        #alter scroll data if a directional key is hit
+        if(key_val == key.UP):
+            if not "up" in self.scroll_data["kb"]:
+                self.scroll_data["kb"].append("up")
+
+        if(key_val == key.RIGHT):
+            if not "right" in self.scroll_data["kb"]:
+                self.scroll_data["kb"].append("right")
+ 
+        if(key_val == key.DOWN): 
+            if not "down" in self.scroll_data["kb"]:
+                self.scroll_data["kb"].append("down")
+
+        if(key_val == key.LEFT):
+            if not "left" in self.scroll_data["kb"]:
+                self.scroll_data["kb"].append("left")
     
+    def keyReleased(self, evt):
+        """Whenever a key is pressed, fife calls this routine.
+           @type evt: fife.event
+           @param evt: The event that fife caught
+           @return: None"""
+        key = evt.getKey()
+        key_val = key.getValue()
+
+        #alter scroll data if a directional key is released
+        if(key_val == key.UP):
+            if "up" in self.scroll_data["kb"]:
+                self.scroll_data["kb"].remove("up")
+
+        if(key_val == key.RIGHT):
+            if "right" in self.scroll_data["kb"]:
+                self.scroll_data["kb"].remove("right")
+ 
+        if(key_val == key.DOWN): 
+            if "down" in self.scroll_data["kb"]:
+                self.scroll_data["kb"].remove("down")
+
+        if(key_val == key.LEFT):
+            if "left" in self.scroll_data["kb"]:
+                self.scroll_data["kb"].remove("left")
+        
     def mouseReleased(self, evt):
         """If a mouse button is released, fife calls this routine.
            We want to wait until the button is released, because otherwise
@@ -235,9 +278,8 @@
         #this can be helpful for IDEs code analysis
         if False:
             assert(isinstance(cursor, fife.Cursor))
-        self.last_mousecoords = fife.ScreenPoint(cursor.getX(), 
-                                                 cursor.getY())        
-        self.view.highlightFrontObject(self.last_mousecoords)       
+        self.last_mousecoords = fife.ScreenPoint(cursor.getX(), cursor.getY())
+        self.view.highlightFrontObject(self.last_mousecoords)
         
         #set the trigger area in pixles
         pixle_edge = 20
@@ -252,33 +294,29 @@
         
         
         #edge logic
-        self.scroll_direction = [0, 0]
         if self.has_mouse_focus:
-            direction = self.scroll_direction
+            direction = self.scroll_data["mouse"] = []
+            
             #up
-            if mouse_y <= pixle_edge: 
-                direction[0] += 1
-                direction[1] -= 1
+            if mouse_y <= pixle_edge:
+                direction.append("up")
                 image = '/'.join(['gui/cursors', settings.parpg.CursorUp])
                 
             #right
             if mouse_x >= screen_width - pixle_edge:
-                direction[0] += 1
-                direction[1] += 1
+                direction.append("right")
                 image = '/'.join(['gui/cursors', settings.parpg.CursorRight])
                 
             #down
             if mouse_y >= screen_height - pixle_edge:
-                direction[0] -= 1
-                direction[1] += 1
+                direction.append("down")
                 image = '/'.join(['gui/cursors', settings.parpg.CursorDown])
                 
             #left
             if mouse_x <= pixle_edge:
-                direction[0] -= 1
-                direction[1] -= 1
+                direction.append("left")
                 image = '/'.join(['gui/cursors', settings.parpg.CursorLeft])
-            
+                
             if image is not None and not data_drag.dragging:
                 self.setMouseCursor(image, image)
        
@@ -318,6 +356,50 @@
             self.view.hud.initializeInventory()         
             self.pause(False)
 
+    def handleScrolling(self):
+        """
+        Merge kb and mouse related scroll data, limit the speed and
+        move the camera.
+        """
+        #this is how many pxls the camera is moved in one time frame
+        scroll_offset = self.scroll_data["offset"] = [0,0]
+ 
+        mouse = self.scroll_data["mouse"]
+        keyboard = self.scroll_data["kb"]
+        speed = self.model.settings.parpg.ScrollSpeed
+
+        #adds a value to the offset depending on the contents of each
+        #  of the controllers: set() removes doubles
+        scroll_direction = set(mouse+keyboard)
+        for direction in scroll_direction:
+            if direction == "up":
+                scroll_offset[0] +=1
+                scroll_offset[1] -=1
+            elif direction == "right":
+                scroll_offset[0] +=1
+                scroll_offset[1] +=1
+            elif direction == "down":
+                scroll_offset[0] -=1
+                scroll_offset[1] +=1
+            elif direction == "left":
+                scroll_offset[0] -=1
+                scroll_offset[1] -=1
+
+        #keep the speed within bounds
+        if scroll_offset[0] > 0: scroll_offset[0] = speed
+        if scroll_offset[0] < 0: scroll_offset[0] = -speed
+        
+        if scroll_offset[1] > 0: scroll_offset[1] = speed
+        if scroll_offset[1] < 0: scroll_offset[1] = -speed
+        
+        #de/activate scrolling
+        if scroll_offset != [0, 0]:
+            self.scroll_timer.start()
+        else: 
+            self.scroll_timer.stop()
+            if not data_drag.dragging:
+                self.resetMouseCursor()
+
     def nullFunc(self, userdata):
         """Sample callback for the context menus."""
         logger.info(userdata)
@@ -433,7 +515,7 @@
             self.has_mouse_focus = True
         elif(command.getCommandType() == fife.CMD_MOUSE_FOCUS_LOST):
             self.has_mouse_focus = False
-      
+   
     def pump(self):
         """Routine called during each frame. Our main loop is in ./run.py"""
         # uncomment to instrument
@@ -444,12 +526,6 @@
         if self.model.active_map:
             self.view.highlightFrontObject(self.last_mousecoords)
             self.view.refreshTopLayerTransparencies()
-            if self.scroll_direction != [0, 0]:
-                self.scroll_timer.start()
-            else: 
-                self.scroll_timer.stop()
-                if not data_drag.dragging:
-                    self.resetMouseCursor()
-                
+            self.handleScrolling()
         self.handleCommands()
         # print "%05f" % (time.time()-t0,)