comparison 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
comparison
equal deleted inserted replaced
149:eab3e1e52497 150:3fc7cfa80771
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 12
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. 14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
15 import logging 15 import logging
16 from types import StringTypes
16 17
17 from fife.extensions.pychan.tools import callbackWithArguments as cbwa 18 from fife.extensions.pychan.tools import callbackWithArguments as cbwa
18 from fife.extensions import pychan 19 from fife.extensions import pychan
19 from fife.extensions.pychan.attrs import UnicodeAttr 20 from fife.extensions.pychan.attrs import UnicodeAttr
20 21
21 from parpg.gui import drag_drop_data as data_drag 22 from parpg.gui import drag_drop_data as data_drag
22 #from parpg.objects.base import Container
23 from parpg.gui.containergui_base import ContainerGUIBase 23 from parpg.gui.containergui_base import ContainerGUIBase
24 from parpg.objects.action import ACTIONS 24 from parpg.objects.action import ACTIONS
25 from parpg.components import equip 25 from parpg.components import equip
26 26
27 logger = logging.getLogger('action') 27 logger = logging.getLogger('action')
71 n_rows = property(fget=_getNRows, fset=_getNColumns) 71 n_rows = property(fget=_getNRows, fset=_getNColumns)
72 72
73 def _setGridSize(self, grid_size): 73 def _setGridSize(self, grid_size):
74 n_columns, n_rows = grid_size 74 n_columns, n_rows = grid_size
75 self.removeAllChildren() 75 self.removeAllChildren()
76 for row_n in range(n_rows): 76 for row_n in xrange(n_rows):
77 row_size = (n_columns * 50, 50) 77 row_size = (n_columns * 50, 50)
78 row = pychan.HBox(min_size=row_size, max_size=row_size, 78 row = pychan.HBox(min_size=row_size, max_size=row_size,
79 padding=self.padding) 79 padding=self.padding)
80 row.border_size = 1 80 row.border_size = 1
81 row.opaque = 0 81 row.opaque = 0
82 for column_n in range(n_columns): 82 for column_n in xrange(n_columns):
83 slot = pychan.Icon(min_size=(50, 50), max_size=(50, 50)) 83 slot = pychan.Icon(min_size=(50, 50), max_size=(50, 50))
84 slot.border_size = 1 84 slot.border_size = 1
85 slot.name = "Slot_%d" % (row_n * n_columns + column_n)
85 row.addChild(slot) 86 row.addChild(slot)
86 self.addChild(row) 87 self.addChild(row)
87 self.min_size = ((n_columns * 50) + 2, (n_rows * 50) + 2) 88 self.min_size = ((n_columns * 50) + 2, (n_rows * 50) + 2)
88 self.max_size = self.min_size 89 self.max_size = self.min_size
89 90
91 n_rows = len(self.children) 92 n_rows = len(self.children)
92 n_columns = len(self.children[0].children) 93 n_columns = len(self.children[0].children)
93 return (n_rows, n_columns) 94 return (n_rows, n_columns)
94 grid_size = property(fget=_getGridSize, fset=_setGridSize) 95 grid_size = property(fget=_getGridSize, fset=_setGridSize)
95 96
97 def getSlot(self, row_or_index, col=None):
98 if col:
99 index = row * self.n_columns + col
100 else:
101 index = row_or_index
102 return self.findChildByName("Slot_%d" % index)
103
96 def __init__(self, grid_size=(2, 2), padding=0, **kwargs): 104 def __init__(self, grid_size=(2, 2), padding=0, **kwargs):
97 pychan.VBox.__init__(self, padding=padding, **kwargs) 105 pychan.VBox.__init__(self, padding=padding, **kwargs)
98 self.opaque = 0 106 self.opaque = 0
99 self.grid_size = grid_size 107 self.grid_size = grid_size
100 self.border_size = 1 108 self.border_size = 1
101 109
102 110
103 class EquipmentGui(ContainerGUIBase): 111 class EquipmentGUI(ContainerGUIBase):
104 def __init__(self, controller, gui, equip, callbacks): 112 def __init__(self, controller, gui, equip, callbacks):
105 ContainerGUIBase.__init__(self, controller, gui) 113 ContainerGUIBase.__init__(self, controller, gui)
106 self.equip = equip 114 self.equip = equip
107 self.equip_to_gui = { 115 self.equip_to_gui = {
108 "head": "headSlot", 116 "head": "headSlot",
129 else: 137 else:
130 image = None 138 image = None
131 slot.image = image 139 slot.image = image
132 140
133 class InventoryGUI(ContainerGUIBase): 141 class InventoryGUI(ContainerGUIBase):
142 def __init__(self, controller, gui, container, callbacks):
143 ContainerGUIBase.__init__(self, controller, gui)
144 self.container = container
145
146 def updateImages(self):
147 grid = self.gui.findChildByName("Grid")
148 assert(isinstance(grid, InventoryGrid))
149 for index, child in enumerate(self.container.children):
150 slot = grid.getSlot(index)
151 if child:
152 slot.item = child.entity
153 else:
154 slot.item = None
155 self.updateImage(slot)
156
157 def updateImage(self, slot):
158 assert(isinstance(slot, pychan.Icon))
159 if (slot.item):
160 image = slot.item.containable.image
161 else:
162 image = None
163 slot.image = image
164
165 class CharacterGUI(object):
134 def __init__(self, controller, gui, container, equip, callbacks): 166 def __init__(self, controller, gui, container, equip, callbacks):
135 super(InventoryGUI, self).__init__(controller, gui)
136 self.engine = controller.engine 167 self.engine = controller.engine
137 self.inventory_shown = False 168 self.inventory_shown = False
169 if isinstance(gui, pychan.Widget):
170 self.gui = gui
171 elif isinstance(gui, StringTypes):
172 xml_file = vfs.VFS.open(gui)
173 self.gui = pychan.loadXML(xml_file)
174 else:
175 self.gui = pychan.loadXML(gui)
176
138 render_backend = self.engine.getRenderBackend() 177 render_backend = self.engine.getRenderBackend()
139 screen_mode = render_backend.getCurrentScreenMode() 178 screen_mode = render_backend.getCurrentScreenMode()
140 screen_width, screen_height = (screen_mode.getWidth(), 179 screen_width, screen_height = (screen_mode.getWidth(),
141 screen_mode.getHeight()) 180 screen_mode.getHeight())
142 widget_width, widget_height = self.gui.size 181 widget_width, widget_height = self.gui.size
143 self.gui.position = ((screen_width - widget_width) / 2, 182 self.gui.position = ((screen_width - widget_width) / 2,
144 (screen_height - widget_height) / 2) 183 (screen_height - widget_height) / 2)
145 self.equip_gui = EquipmentGui( 184 self.equip_gui = EquipmentGUI(
146 controller, 185 controller,
147 self.gui.findChildByName("equipmentPage"), 186 self.gui.findChildByName("equipmentPage"),
148 equip, callbacks 187 equip, callbacks
188 )
189 self.inv_gui = InventoryGUI(
190 controller,
191 self.gui.findChildByName("inventoryPage"),
192 container, callbacks
149 ) 193 )
150 194
151 def toggleInventory(self, toggleImage=True): 195 def toggleInventory(self, toggleImage=True):
152 """Pause the game and enter the inventory screen, or close the 196 """Pause the game and enter the inventory screen, or close the
153 inventory screen and resume the game. 197 inventory screen and resume the game.
165 self.closeInventory() 209 self.closeInventory()
166 self.inventory_shown = False 210 self.inventory_shown = False
167 211
168 def showInventory(self): 212 def showInventory(self):
169 self.equip_gui.updateImages() 213 self.equip_gui.updateImages()
214 self.inv_gui.updateImages()
170 self.gui.show() 215 self.gui.show()
171 216
172 def closeInventory(self): 217 def closeInventory(self):
173 self.gui.hide() 218 self.gui.hide()
174 219