diff src/parpg/gui/inventorygui.py @ 150:3fc7cfa80771

Modified InventoryGrid to set a name for each slot containing the index. Added getSlot method to InventoryGrid. Renamed InventoryGUI class to CharacterGUI. Added InventoryGUI class which handles the inventory part of the CharacterGUI. An InventoryGUI instance is now created in CharacterGUI.
author KarstenBock@gmx.net
date Wed, 05 Oct 2011 12:59:22 +0200
parents eab3e1e52497
children 2ba7c6d8f738
line wrap: on
line diff
--- a/src/parpg/gui/inventorygui.py	Wed Oct 05 11:04:39 2011 +0200
+++ b/src/parpg/gui/inventorygui.py	Wed Oct 05 12:59:22 2011 +0200
@@ -13,13 +13,13 @@
 #   You should have received a copy of the GNU General Public License
 #   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
 import logging
+from types import StringTypes
 
 from fife.extensions.pychan.tools import callbackWithArguments as cbwa
 from fife.extensions import pychan
 from fife.extensions.pychan.attrs import UnicodeAttr
 
 from parpg.gui import drag_drop_data as data_drag
-#from parpg.objects.base import Container
 from parpg.gui.containergui_base import ContainerGUIBase
 from parpg.objects.action import ACTIONS
 from parpg.components import equip
@@ -73,15 +73,16 @@
     def _setGridSize(self, grid_size):
         n_columns, n_rows = grid_size
         self.removeAllChildren()
-        for row_n in range(n_rows):
+        for row_n in xrange(n_rows):
             row_size = (n_columns * 50, 50)
             row = pychan.HBox(min_size=row_size, max_size=row_size,
                               padding=self.padding)
             row.border_size = 1
             row.opaque = 0
-            for column_n in range(n_columns):
+            for column_n in xrange(n_columns):
                 slot = pychan.Icon(min_size=(50, 50), max_size=(50, 50))
                 slot.border_size = 1
+                slot.name = "Slot_%d" % (row_n * n_columns + column_n)
                 row.addChild(slot)
             self.addChild(row)
         self.min_size = ((n_columns * 50) + 2, (n_rows * 50) + 2)
@@ -93,6 +94,13 @@
         return (n_rows, n_columns)
     grid_size = property(fget=_getGridSize, fset=_setGridSize)
     
+    def getSlot(self, row_or_index, col=None):
+        if col:
+            index = row * self.n_columns + col
+        else:
+            index = row_or_index
+        return self.findChildByName("Slot_%d" % index)
+    
     def __init__(self, grid_size=(2, 2), padding=0, **kwargs):
         pychan.VBox.__init__(self, padding=padding, **kwargs)
         self.opaque = 0
@@ -100,7 +108,7 @@
         self.border_size = 1
 
 
-class EquipmentGui(ContainerGUIBase):
+class EquipmentGUI(ContainerGUIBase):
     def __init__(self, controller, gui, equip, callbacks):
         ContainerGUIBase.__init__(self, controller, gui)
         self.equip = equip
@@ -131,10 +139,41 @@
         slot.image = image
 
 class InventoryGUI(ContainerGUIBase):
+    def __init__(self, controller, gui, container, callbacks):
+        ContainerGUIBase.__init__(self, controller, gui)
+        self.container = container
+
+    def updateImages(self):
+        grid = self.gui.findChildByName("Grid")
+        assert(isinstance(grid, InventoryGrid))
+        for index, child in enumerate(self.container.children):
+            slot = grid.getSlot(index)
+            if child:
+                slot.item = child.entity
+            else:
+                slot.item = None
+            self.updateImage(slot)
+            
+    def updateImage(self, slot):
+        assert(isinstance(slot, pychan.Icon))
+        if (slot.item):
+            image = slot.item.containable.image
+        else:
+            image = None
+        slot.image = image
+
+class CharacterGUI(object):
     def __init__(self, controller, gui, container, equip, callbacks):
-        super(InventoryGUI, self).__init__(controller, gui)
         self.engine = controller.engine
         self.inventory_shown = False
+        if isinstance(gui, pychan.Widget):
+            self.gui = gui
+        elif isinstance(gui, StringTypes):
+            xml_file = vfs.VFS.open(gui)
+            self.gui = pychan.loadXML(xml_file)
+        else:
+            self.gui = pychan.loadXML(gui)
+        
         render_backend = self.engine.getRenderBackend()
         screen_mode = render_backend.getCurrentScreenMode()
         screen_width, screen_height = (screen_mode.getWidth(),
@@ -142,11 +181,16 @@
         widget_width, widget_height = self.gui.size
         self.gui.position = ((screen_width - widget_width) / 2,
                              (screen_height - widget_height) / 2)
-        self.equip_gui = EquipmentGui(
+        self.equip_gui = EquipmentGUI(
             controller,
             self.gui.findChildByName("equipmentPage"),
             equip, callbacks
         )
+        self.inv_gui = InventoryGUI(
+            controller,
+            self.gui.findChildByName("inventoryPage"),
+            container, callbacks
+        )
     
     def toggleInventory(self, toggleImage=True):
         """Pause the game and enter the inventory screen, or close the
@@ -167,6 +211,7 @@
     
     def showInventory(self):
         self.equip_gui.updateImages()
+        self.inv_gui.updateImages()
         self.gui.show()
     
     def closeInventory(self):