Package horizons :: Package gui :: Package keylisteners :: Module ingamekeylistener
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.keylisteners.ingamekeylistener

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  from fife import fife 
 23   
 24  import horizons.globals 
 25  from horizons.util.living import LivingObject 
 26   
 27  from .keyconfig import KeyConfig 
 28   
 29   
30 -class IngameKeyListener(fife.IKeyListener, LivingObject):
31 """KeyListener Class to process key presses ingame""" 32
33 - def __init__(self, session):
34 super().__init__() 35 from horizons.session import Session 36 assert isinstance(session, Session) 37 self.session = session 38 self.keyconfig = KeyConfig() 39 horizons.globals.fife.eventmanager.addKeyListenerFront(self) 40 # Used to sum up the keyboard autoscrolling 41 self.key_scroll = [0, 0] 42 self.up_key_pressed = False 43 self.down_key_pressed = False 44 self.left_key_pressed = False 45 self.right_key_pressed = False 46 self.key_scroll_speed = 25 47 # Last event (to avoid double-firing) 48 self.last_evt = {'type': None, 'value': None}
49
50 - def end(self):
51 horizons.globals.fife.eventmanager.removeKeyListener(self) 52 self.session = None 53 super().end()
54
55 - def updateAutoscroll(self):
56 self.key_scroll = [0, 0] 57 if self.up_key_pressed: 58 self.key_scroll[1] -= self.key_scroll_speed 59 if self.down_key_pressed: 60 self.key_scroll[1] += self.key_scroll_speed 61 if self.left_key_pressed: 62 self.key_scroll[0] -= self.key_scroll_speed 63 if self.right_key_pressed: 64 self.key_scroll[0] += self.key_scroll_speed 65 66 self.session.view.autoscroll_keys(*self.key_scroll)
67
68 - def keyPressed(self, evt):
69 keyval = evt.getKey().getValue() 70 action = self.keyconfig.translate(evt) 71 72 _Actions = KeyConfig._Actions 73 74 if action == _Actions.UP: 75 self.up_key_pressed = True 76 if action == _Actions.DOWN: 77 self.down_key_pressed = True 78 if action == _Actions.LEFT: 79 self.left_key_pressed = True 80 if action == _Actions.RIGHT: 81 self.right_key_pressed = True 82 83 self.updateAutoscroll() 84 85 # if the current event is identical to the previous one, ignore it 86 if (evt.getType() == self.last_evt['type'] and 87 evt.getKey().getValue() == self.last_evt['value']): 88 evt.consume() # prevent other listeners from being called 89 return 90 91 if self.session.ingame_gui.on_key_press(action, evt): 92 evt.consume() # prevent other listeners from being called 93 94 # update last event 95 self.last_evt['type'] = evt.getType() 96 self.last_evt['value'] = evt.getKey().getValue()
97
98 - def keyReleased(self, evt):
99 keyval = evt.getKey().getValue() 100 _Actions = KeyConfig._Actions 101 action = self.keyconfig.translate(evt) 102 103 if action == _Actions.UP: 104 self.up_key_pressed = False 105 if action == _Actions.DOWN: 106 self.down_key_pressed = False 107 if action == _Actions.LEFT: 108 self.left_key_pressed = False 109 if action == _Actions.RIGHT: 110 self.right_key_pressed = False 111 112 self.updateAutoscroll() 113 114 # update last event 115 self.last_evt['type'] = evt.getType() 116 self.last_evt['value'] = evt.getKey().getValue()
117