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

Source Code for Module horizons.gui.keylisteners.mainlistener

  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  import datetime 
 23  import os 
 24  import os.path 
 25  import shutil 
 26  import tempfile 
 27   
 28  from fife import fife 
 29   
 30  import horizons.globals 
 31  import horizons.main 
 32  from horizons.constants import PATHS 
 33  from horizons.util.living import LivingObject 
 34   
 35  from .keyconfig import KeyConfig 
 36   
 37   
38 -class MainListener(fife.IKeyListener, fife.ICommandListener, LivingObject):
39 """MainListener Class to process events of main window""" 40
41 - def __init__(self, gui):
42 super().__init__() 43 self.gui = gui 44 fife.IKeyListener.__init__(self) 45 horizons.globals.fife.eventmanager.addKeyListener(self) 46 fife.ICommandListener.__init__(self) 47 horizons.globals.fife.eventmanager.addCommandListener(self)
48
49 - def end(self):
50 horizons.globals.fife.eventmanager.removeKeyListener(self) 51 super().end()
52
53 - def keyPressed(self, evt):
54 if evt.isConsumed(): 55 return 56 57 action = KeyConfig().translate(evt) 58 _Actions = KeyConfig._Actions 59 keyval = evt.getKey().getValue() 60 61 key_event_handled = True 62 63 if action == _Actions.ESCAPE: 64 self.gui.on_escape() 65 elif keyval == fife.Key.ENTER: 66 self.gui.on_return() 67 elif action == _Actions.CONSOLE: 68 self.gui.fps_display.toggle() 69 elif action == _Actions.HELP: 70 self.gui.on_help() 71 elif action == _Actions.SCREENSHOT: 72 # save the screenshot into a temporary file because fife doesn't support unicode paths 73 temp_handle, temp_path = tempfile.mkstemp() 74 os.close(temp_handle) 75 horizons.globals.fife.engine.getRenderBackend().captureScreen(temp_path) 76 77 # move the screenshot into the final location 78 filename = datetime.datetime.now().isoformat('.').replace(":", "-") + ".png" 79 final_path = os.path.join(PATHS.SCREENSHOT_DIR, filename) 80 shutil.move(temp_path, final_path) 81 82 # ingame message if there is a session and it is fully initialized: 83 # pressing S on loading screen finds a session but no gui usually. 84 session = horizons.main.session 85 if session is not None and session.ingame_gui is not None: 86 session.ingame_gui.message_widget.add('SCREENSHOT', 87 message_dict={'file': final_path}) 88 elif action == _Actions.QUICKLOAD: 89 horizons.main._load_last_quicksave() 90 else: 91 key_event_handled = False # nope, nothing triggered 92 93 if key_event_handled: 94 evt.consume() # prevent other listeners from being called
95
96 - def keyReleased(self, evt):
97 pass
98
99 - def onCommand(self, command):
100 if command.getCommandType() == fife.CMD_QUIT_GAME: 101 # NOTE Sometimes we get two quit events from FIFE, ignore the second 102 # if we are already shutting down the game 103 if not horizons.globals.fife.quit_requested: 104 horizons.main.quit() 105 command.consume()
106