Mercurial > fife-parpg
view engine/python/fife/extensions/pychan/dialogs.py @ 697:ecaa4d98f05f tip
Abstracted the GUI code and refactored the GUIChan-specific code into its own module.
* Most of the GUIChan code has been refactored into its own gui/guichan module. However, references to the GuiFont class still persist in the Engine and GuiManager code and these will need further refactoring.
* GuiManager is now an abstract base class which specific implementations (e.g. GUIChan) should subclass.
* The GUIChan GUI code is now a concrete implementation of GuiManager, most of which is in the new GuiChanGuiManager class.
* The GUI code in the Console class has been refactored out of the Console and into the GUIChan module as its own GuiChanConsoleWidget class. The rest of the Console class related to executing commands was left largely unchanged.
* Existing client code may need to downcast the GuiManager pointer received from FIFE::Engine::getGuiManager() to GuiChanGuiManager, since not all functionality is represented in the GuiManager abstract base class. Python client code can use the new GuiChanGuiManager.castTo static method for this purpose.
author | M. George Hansen <technopolitica@gmail.com> |
---|---|
date | Sat, 18 Jun 2011 00:28:40 -1000 |
parents | e34b44afd428 |
children |
line wrap: on
line source
# -*- coding: utf-8 -*- # #################################################################### # Copyright (C) 2005-2009 by the FIFE team # http://www.fifengine.de # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the # Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # #################################################################### from fife.extensions import pychan from fife.extensions.pychan import loadXML import fife.extensions.pychan.tools as tools import widgets from internal import get_manager, screen_width, screen_height from StringIO import StringIO OK,YES,NO,CANCEL = True,True,False,None def print_event(**kwargs): print kwargs class XMLDialog(object): def __init__(self, xml, ok_field = None, cancel_field = None,initial_data={},data={}): self.gui = loadXML(xml) self.ok_field = ok_field self.cancel_field = cancel_field self.initial_data= initial_data self.data= data self.max_size=None self.min_size=None # self.gui.capture(print_event,"mouseEntered") def execute(self): self.gui.distributeInitialData(self.initial_data) self.gui.distributeData(self.data) screen_w, screen_h = screen_width(), screen_height() if self.max_size is None: self.max_size = screen_w/2, screen_h/3 if self.min_size is None: self.min_size = screen_w/2, screen_h/4 self.gui.max_size = self.max_size self.gui.min_size = self.min_size resultMap = {} if self.gui.findChild(name="okButton"): resultMap["okButton"] = OK if self.gui.findChild(name="cancelButton"): resultMap["cancelButton"] = CANCEL if self.gui.findChild(name="yesButton"): resultMap["noButton"] = NO if self.gui.findChild(name="yesButton"): resultMap["yesButton"] = YES ok = self.gui.execute(resultMap) if ok: return self.getOkResult() return self.getCancelResult() def getOkResult(self): if self.ok_field: return self.gui.collectData(self.ok_field) return True def getCancelResult(self): if self.cancel_field: return self.gui.collectData(self.cancel_field) return False MESSAGE_BOX_XML = """\ <Window name="window" title="Message"> <ScrollArea> <Label wrap_text="1" text="$MESSAGE" name="message" vexpand="1"/> </ScrollArea> <HBox> <Spacer/><Button min_size="50,0" name="okButton" text="OK"/> </HBox> </Window> """ YESNO_BOX_XML = """\ <Window name="window" title="Question"> <ScrollArea> <Label wrap_text="1" text="$MESSAGE" name="message" vexpand="1"/> </ScrollArea> <HBox> <Spacer/> <Button min_size="50,0" name="yesButton" text="Yes"/> <Button min_size="50,0" name="noButton" text="No"/> </HBox> </Window> """ YESNOCANCEL_BOX_XML = """\ <Window name="window" title="Question"> <ScrollArea> <Label wrap_text="1" text="$MESSAGE" name="message" vexpand="1"/> </ScrollArea> <HBox> <Spacer/> <Button min_width="50" name="yesButton" text="Yes"/> <Button min_width="50" name="noButton" text="No"/> <Button min_width="50" name="cancelButton" text="Cancel"/> </HBox> </Window> """ SELECT_BOX_XML = """\ <Window name="window" title="Select"> <Label wrap_text="1" text="$MESSAGE" name="message"/> <ScrollArea> <ListBox name="selection"> </ListBox> </ScrollArea> <HBox> <Spacer/> <Button min_width="50" name="okButton" text="Select"/> <Button min_width="50" name="cancelButton" text="Cancel"/> </HBox> </Window> """ EXCEPTION_CATCHER_XML="""\ <Window name="window" title="An exception occurred - what now?"> <VBox hexpand="1"> <Label wrap_text="1" max_size="400,90000" text="$MESSAGE" name="message"/> <ScrollArea> <Label text="$MESSAGE" name="traceback"/> </ScrollArea> </VBox> <HBox> <Spacer/> <Button name="yesButton" text="Retry"/> <Button name="noButton" text="Ignore"/> <Button name="cancelButton" text="Reraise"/> </HBox> </Window> """ def _make_text(message): if callable(message): message = message() if hasattr(message,"read"): message = message.read() return message def message(message="",caption="Message"): text = _make_text(message) dialog = XMLDialog(StringIO(MESSAGE_BOX_XML)) dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 dialog.gui.findChild(name="message").text = text dialog.gui.findChild(name="window").title = caption dialog.execute() def yesNo(message="",caption="Message"): text = _make_text(message) dialog = XMLDialog(StringIO(YESNO_BOX_XML)) dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 dialog.gui.findChild(name="message").text = text dialog.gui.findChild(name="window").title = caption return dialog.execute() def yesNoCancel(message="",caption="Message"): text = _make_text(message) dialog = XMLDialog(StringIO(YESNOCANCEL_BOX_XML)) dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 dialog.gui.findChild(name="message").text = text dialog.gui.findChild(name="window").title = caption return dialog.execute() def select(message="",options=[],caption="Message"): text = _make_text(message) dialog = XMLDialog(StringIO(SELECT_BOX_XML)) dialog.size = screen_width()/3, 2*screen_height()/3 dialog.gui.findChild(name="message").max_width = screen_width()/2 - 50 dialog.gui.findChild(name="message").text = text dialog.gui.findChild(name="window").title = caption listbox = dialog.gui.findChild(name="selection") listbox.items = options if dialog.execute(): return listbox.selected_item return None def trace(f): import sys, traceback def new_f(*args,**kwargs): try: return pychan.tools.applyOnlySuitable(f,*args,**kwargs) except Exception, e: dialog = XMLDialog(StringIO(EXCEPTION_CATCHER_XML)) dialog.gui.findChild(name="message").text = str(e) tb = traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback) dialog.gui.findChild(name="traceback").text = "".join(tb) dialog.min_size = screen_width()/2,3*screen_height()/4 dialog.max_size = screen_width()/2,3*screen_height()/4 result = dialog.execute() if result == YES: return new_f(*args,**kwargs) elif result == NO: return raise return new_f