view src/parpg/gui/containergui_base.py @ 12:d60f1dab8469

Fixed resource path dependencies issue that caused PARPG to crash on start. * PARPG should now run without issue (system installation not tested). * Utilized FIFE's VFS module to remove path dependencies from most PARPG modules. * The new parpg.vfs module is a singleton with a single global variable, VFS, which is a reference to the global VFS instance. Although a singleton is not ideal it should be replaced once PARPG's core code is refactored. * The parpg.vfs singleton is initialized in the parpg.applicaiton.PARPGApplication class with the absolute path to the data directory via the parpg.settings module and corresponding configuration file. * A new DataPath entry was added to the default system configuration file template under the [parpg] section to support the new parpg.vfs module. * Updated the parpg-assets subrepo to revision 3 to fix some dialog file format issues (for details see commit message for parpg-assets). * Fixed a few bugs in the parpg.dialogueparsers.YAMLDialogueParser class related to exception handling.
author M. George Hansen <technopolitica@gmail.com>
date Mon, 06 Jun 2011 15:56:14 -1000
parents 1fd2201f5c36
children 5756e615b029
line wrap: on
line source

#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.

#   This program 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 General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
from copy import deepcopy
from types import StringTypes

from fife import fife
from fife.extensions import pychan

from parpg import vfs
from parpg.gui import drag_drop_data as data_drag
from parpg.objects.action import ACTIONS

class ContainerGUIBase(object):
    """
    Base class for windows that show the content of a container
    """


    def __init__(self, controller, gui_file):
        self.controller = controller
        if isinstance(gui_file, StringTypes):
            xml_file = vfs.VFS.open(gui_file)
            self.gui = pychan.loadXML(xml_file)
        else:
            self.gui = pychan.loadXML(gui_file)

    def dragDrop(self, obj):
        """Decide whether to drag or drop the image.
           @type obj: string
           @param obj: The name of the object within 
                       the dictionary 'self.buttons'
           @return: None"""
        if(data_drag.dragging == True):
            self.dropObject(obj)
        elif(data_drag.dragging == False):
            self.dragObject(obj)
                
    def dragObject(self, obj):
        """Drag the selected object.
           @type obj: string
           @param obj: The name of the object within
                       the dictionary 'self.buttons'
           @return: None"""           
        pass
       
    def dropObject(self, obj):
        """Drops the object being dropped
           @type obj: string
           @param obj: The name of the object within
                       the dictionary 'self.buttons' 
           @return: None"""
        pass    
    

    def createMenuItems(self, item, actions):
        """Creates context menu items for all classes based on ContainerGUI"""
        menu_actions = []
        for action_name in actions:
            display_name = action_name
            if action_name in ACTIONS:
                param_dict = {}
                param_dict["controller"] = self.controller
                param_dict["commands"] = {}
                if action_name == "Look":
                    param_dict["examine_name"] = item.name
                    param_dict["examine_desc"] = actions[action_name].\
                                                                pop("text")
                if action_name == "Read":
                    param_dict["text_name"] = item.name
                    param_dict["text"] = ""
                if action_name == "Use":
                    param_dict["item"] = item
                    display_name = actions[action_name].pop("text")
                if action_name == "Open":
                    param_dict["container"] = item
                if action_name == "BrewBeer":
                    param_dict["pot"] = item
                    display_name = "Brew beer"
                if actions[action_name]:
                    param_dict.update(actions[action_name])
                menu_actions.append([action_name, 
                                     display_name, 
                                     self.executeMenuItem, 
                                     ACTIONS[action_name]\
                                                (**param_dict)])        
        return menu_actions

    def showContextMenu(self, event, widget):
        """Decide whether to drag or drop the image.
           @type obj: string
           @param obj: The name of the object within 
                       the dictionary 'self.buttons'
           @return: None"""
        if event.getButton() == event.RIGHT:
            item = widget.item
            if item and item.trueAttr("usable"):
                actions = deepcopy(item.actions)
                if not actions:
                    return
                x_pos, y_pos = widget.getAbsolutePos()
                x_pos += event.getX()
                y_pos += event.getY()
                menu_actions = self.createMenuItems(item, actions)
                self.controller.view.hud.hideContextMenu()
                self.controller.view.hud.showContextMenu(menu_actions,
                                                 (x_pos, 
                                                  y_pos)
                                                  )

    def executeMenuItem(self, action):
        """Executes the items action
        @param action: The action to run
        @type action: Class derived from parpg.objects.action.Action
        """
        action.execute()
    
    def updateImages(self):
        pass