Mercurial > fife-parpg
view engine/python/fife/extensions/pychan/autoposition.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 | 64738befdf3b |
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 # #################################################################### """ Automatic widget positioning ============================ You can use the C{position_techinque} attribute on top level widgets which can also be set from xml. For direct use call L{placeWidget}. """ from internal import screen_width, screen_height from exceptions import PyChanException EXPLICIT = "explicit" AUTOMATIC = "automatic" TOP = "top" LEFT = "left" RIGHT = "right" CENTER = "center" BOTTOM = "bottom" def _splicePosition(p): if "+" in p: technique,delta = p.split("+") elif "-" in p: technique,delta = p.split("-") delta = '-' + delta else: technique,delta = p,0 delta = int(delta) return technique,delta def _parsePosition(position): try: if position == AUTOMATIC: position = "center+0:center+0" x_pos,y_pos = position.split(":") x_pos, x_delta = _splicePosition(x_pos) y_pos, y_delta = _splicePosition(y_pos) if x_pos not in [EXPLICIT,LEFT,CENTER,RIGHT]: raise if y_pos not in [EXPLICIT,TOP,CENTER,BOTTOM]: raise except: raise PyChanException("Malformed position definition: " + repr(position)) return x_pos,x_delta,y_pos,y_delta def placeWidget(widget,position): """ Place a widget according to a string defining relative coordinates to screen borders. The position definition has to be of the form: C{"<x_pos><x_delta>:<y_pos><y_delta>"} C{<x_pos>} may be one of: - left - right - center - explicit C{<y_pos>} may be one of: - top - bottom - center - explicit C{explicit} means that the widgets x or y position will not be touched. The rest should be self explanatory. C{<x_delta>} and C{<y_delta>} must be of the form: +pixel_number or -pixel_number. Or comletely omitted. Note that the sign has to be there for for positive deltas, too. For brevity two shortcuts exist: - "explict" -> "explict:explict" - "automatic" -> "center:center" A few examples:: "right-20:top" "center:top+10" "center:center" @param widget: The PyChan widget. @param position: A position definition. If the position cannot be parsed a L{PyChanException} is thrown. """ if position == EXPLICIT: return x_pos,x_delta,y_pos,y_delta = _parsePosition(position) x,y = widget.position w,h = widget.size if x_pos == CENTER: x = (screen_width()-w)/2 + x_delta if y_pos == CENTER: y = (screen_height()-h)/2 + y_delta if x_pos == LEFT: x = x_delta if y_pos == TOP: y = y_delta if x_pos == RIGHT: x = screen_width() - w + x_delta if y_pos == BOTTOM: y = screen_height() - h + y_delta widget.position = x,y