changeset 174:ee4c1fe78be1

Added drag and drop and context menu to the equipment part of the character screen.
author KarstenBock@gmx.net
date Sun, 09 Oct 2011 19:24:37 +0200
parents fadc02e77ae1
children c50a7adeae85
files src/parpg/gui/inventorygui.py
diffstat 1 files changed, 83 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/parpg/gui/inventorygui.py	Sun Oct 09 19:23:59 2011 +0200
+++ b/src/parpg/gui/inventorygui.py	Sun Oct 09 19:24:37 2011 +0200
@@ -22,7 +22,7 @@
 from parpg.gui import drag_drop_data as data_drag
 from parpg.gui.containergui_base import ContainerGUIBase
 from parpg.objects.action import ACTIONS
-from parpg.components import equip, container
+from parpg.components import equip, equipable, container
 from parpg.entities import General
 
 logger = logging.getLogger('action')
@@ -125,10 +125,11 @@
             "l_arm": "leftHandSlot",
             "r_arm": "rightHandSlot",
         }
+        self.setSlotEvents()
         
     def updateImages(self):
         for eq_slot, gui_slot in self.equip_to_gui.iteritems():
-            widget = self.gui.findChild(name=gui_slot)
+            widget = self.gui.findChildByName(gui_slot)            
             equipable = equip.get_equipable(self.equip, eq_slot)
             widget.item = equipable.entity if equipable else None
             self.updateImage(widget) 
@@ -140,7 +141,85 @@
         else:
             image = None
         slot.image = image
+                
+    def dragObject(self, obj):
+        """Drag the selected object.
+           @type obj: string
+           @param obj: The name of the object
+           @return: None"""
+        # get the widget from the gui with the name obj
+        drag_widget = self.gui.findChildByName(obj)
+        drag_item = drag_widget.item
+        # only drag if the widget is not empty
+        if (drag_item != None):
+            if isinstance(drag_item, General):
+                drag_item = drag_item.containable
+            elif isinstance(drag_item, equipable):
+                drag_item = drag_item.entity.containable
+            drag_eq = drag_item.entity.equipable
+            # get the image of the widget
+            image = drag_widget.image
+            self.setDragData(drag_item, image, image)
+            equip.take_equipable(self.equip, drag_eq.in_slot)
+            
+            # after dragging the 'item', set the widgets' images
+            # so that it has it's default 'empty' images
+            drag_widget.item = None
+            self.updateImage(drag_widget)
 
+    def dropObject(self, obj):
+        """Drops the object being dropped
+           @type obj: string
+           @param obj: The name of the object
+           @return: None"""
+        try:
+            drop_widget = self.gui.findChildByName(obj)
+            drop_slot = drop_widget.slot
+            replace_item = None
+    
+            if data_drag.dragging:
+                drag_item = data_drag.dragged_item.entity
+                if drag_item.equipable:
+                    drag_item = drag_item.equipable
+                else:
+                    return                
+                #this will get the replacement item and the data for drag_drop
+                #if there is an item all ready occupying the slot
+                replace_item = (
+                    equip.equip(self.equip, drag_item, drop_slot)
+                )
+                
+            #if there was no item the stop dragging and reset cursor
+            if replace_item:
+                image = drop_widget.image
+                self.setDragData(replace_item.entity.containable, image, image)
+            else:
+                data_drag.dragging = False
+                #reset the mouse cursor to the normal cursor
+                self.controller.resetMouseCursor()
+            drop_widget.item = drag_item.entity
+            self.updateImage(drop_widget)
+        except (equip.AlreadyEquippedError, equip.CannotBeEquippedInSlot):
+            #Do we want to notify the player why the item can't be dropped?
+            pass
+
+    def mousePressedOnSlot(self, event, widget):
+        if event.getButton() == event.LEFT:
+            self.dragDrop(widget.name)
+
+    def setSlotEvents(self):
+        events_to_map = {}
+        for eq_slot, gui_slot in self.equip_to_gui.iteritems():
+            widget = self.gui.findChildByName(gui_slot)
+            slot_name = widget.name
+            widget.slot = eq_slot
+            events_to_map[slot_name + "/mousePressed"] = (
+                self.mousePressedOnSlot
+            )
+            events_to_map[slot_name + "/mouseReleased"] = self.showContextMenu
+
+        self.gui.mapEvents(events_to_map)            
+        
 class InventoryGUI(ContainerGUIBase):
     def __init__(self, controller, gui, container, callbacks):
         ContainerGUIBase.__init__(self, controller, gui)
@@ -187,8 +266,7 @@
     def dragObject(self, obj):
         """Drag the selected object.
            @type obj: string
-           @param obj: The name of the object within
-                       the dictionary 'self.buttons'
+           @param obj: The name of the object
            @return: None"""
         # get the widget from the gui with the name obj
         drag_widget = self.gui.findChild(name = obj)
@@ -210,8 +288,7 @@
     def dropObject(self, obj):
         """Drops the object being dropped
            @type obj: string
-           @param obj: The name of the object within
-                       the dictionary 'self.buttons' 
+           @param obj: The name of the object
            @return: None"""
         try:
             drop_widget = self.gui.findChildByName(obj)