Mercurial > parpg-core
comparison src/parpg/gui/inventorygui.py @ 0:1fd2201f5c36
Initial commit of parpg-core.
author | M. George Hansen <technopolitica@gmail.com> |
---|---|
date | Sat, 14 May 2011 01:12:35 -0700 |
parents | |
children | d60f1dab8469 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1fd2201f5c36 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 # This file is part of PARPG. | |
4 | |
5 # PARPG is free software: you can redistribute it and/or modify | |
6 # it under the terms of the GNU General Public License as published by | |
7 # the Free Software Foundation, either version 3 of the License, or | |
8 # (at your option) any later version. | |
9 | |
10 # PARPG is distributed in the hope that it will be useful, | |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU General Public License for more details. | |
14 | |
15 # You should have received a copy of the GNU General Public License | |
16 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. | |
17 | |
18 from fife.extensions.pychan.tools import callbackWithArguments as cbwa | |
19 from fife.extensions import pychan | |
20 from fife.extensions.pychan.attrs import UnicodeAttr | |
21 | |
22 from parpg.gui import drag_drop_data as data_drag | |
23 from parpg.objects.base import Container | |
24 from parpg.gui.containergui_base import ContainerGUIBase | |
25 from parpg.objects.action import ACTIONS | |
26 | |
27 import logging | |
28 | |
29 logger = logging.getLogger('action') | |
30 | |
31 class EquipmentSlot(pychan.VBox): | |
32 ATTRIBUTES = pychan.VBox.ATTRIBUTES + [UnicodeAttr('label_text')] | |
33 | |
34 def _setLabelText(self, text): | |
35 label = self.findChild() | |
36 label.text = unicode(text) | |
37 label.resizeToContent() | |
38 self.margins = ( | |
39 int((self.width - label.width) / 2.0), | |
40 int((self.height - label.height) / 2.0) | |
41 ) | |
42 | |
43 def _getLabelText(self): | |
44 label = self.findChild() | |
45 return label.text | |
46 | |
47 label_text = property(fget=_getLabelText, fset=_setLabelText) | |
48 | |
49 def __init__(self, label_text=u'equipment', min_size=(50, 50), | |
50 max_size=(50, 50), margins=None, | |
51 background_image="gui/inv_images/inv_background.png", | |
52 **kwargs): | |
53 pychan.VBox.__init__(self, min_size=min_size, max_size=max_size, | |
54 **kwargs) | |
55 self.background_image = background_image | |
56 label = pychan.Label(text=unicode(label_text)) | |
57 self.addChild(label) | |
58 self.label_text = label_text | |
59 self.adaptLayout() | |
60 if self.parent is not None: | |
61 self.beforeShow() | |
62 | |
63 | |
64 class InventoryGrid(pychan.VBox): | |
65 ATTRIBUTES = pychan.VBox.ATTRIBUTES + [pychan.attrs.PointAttr('grid_size')] | |
66 | |
67 def _setNColumns(self, n_columns): | |
68 n_rows = self.grid_size[1] | |
69 self.grid_size = (n_columns, n_rows) | |
70 | |
71 def _getNColumns(self): | |
72 n_columns = self.grid_size[0] | |
73 return n_columns | |
74 n_columns = property(fget=_getNColumns, fset=_setNColumns) | |
75 | |
76 def _setNRows(self, n_rows): | |
77 n_columns = self.grid_size[0] | |
78 self.grid_size = (n_columns, n_rows) | |
79 | |
80 def _getNRows(self): | |
81 n_rows = self.grid_size[1] | |
82 return n_rows | |
83 n_rows = property(fget=_getNRows, fset=_getNColumns) | |
84 | |
85 def _setGridSize(self, grid_size): | |
86 n_columns, n_rows = grid_size | |
87 self.removeAllChildren() | |
88 for row_n in range(n_rows): | |
89 row_size = (n_columns * 50, 50) | |
90 row = pychan.HBox(min_size=row_size, max_size=row_size, | |
91 padding=self.padding) | |
92 row.border_size = 1 | |
93 row.opaque = 0 | |
94 for column_n in range(n_columns): | |
95 slot = pychan.Icon(min_size=(50, 50), max_size=(50, 50)) | |
96 slot.border_size = 1 | |
97 row.addChild(slot) | |
98 self.addChild(row) | |
99 self.min_size = ((n_columns * 50) + 2, (n_rows * 50) + 2) | |
100 self.max_size = self.min_size | |
101 | |
102 def _getGridSize(self): | |
103 n_rows = len(self.children) | |
104 n_columns = len(self.children[0].children) | |
105 return (n_rows, n_columns) | |
106 grid_size = property(fget=_getGridSize, fset=_setGridSize) | |
107 | |
108 def __init__(self, grid_size=(2, 2), padding=0, **kwargs): | |
109 pychan.VBox.__init__(self, padding=padding, **kwargs) | |
110 self.opaque = 0 | |
111 self.grid_size = grid_size | |
112 self.border_size = 1 | |
113 | |
114 | |
115 class InventoryGUI(ContainerGUIBase): | |
116 def __init__(self, controller, inventory, callbacks): | |
117 super(InventoryGUI, self).__init__(controller, "gui/inventory.xml") | |
118 self.engine = controller.engine | |
119 self.inventory_shown = False | |
120 render_backend = self.engine.getRenderBackend() | |
121 screen_mode = render_backend.getCurrentScreenMode() | |
122 screen_width, screen_height = (screen_mode.getWidth(), | |
123 screen_mode.getHeight()) | |
124 widget_width, widget_height = self.gui.size | |
125 self.gui.position = ((screen_width - widget_width) / 2, | |
126 (screen_height - widget_height) / 2) | |
127 | |
128 def toggleInventory(self, toggleImage=True): | |
129 """Pause the game and enter the inventory screen, or close the | |
130 inventory screen and resume the game. | |
131 @type toggleImage: bool | |
132 @param toggleImage: | |
133 Call toggleInventoryCallback if True. Toggling via a | |
134 keypress requires that we toggle the Hud inventory image | |
135 explicitly. Clicking on the Hud inventory button toggles the | |
136 image implicitly, so we don't change it. | |
137 @return: None""" | |
138 if not self.inventory_shown: | |
139 self.showInventory() | |
140 self.inventory_shown = True | |
141 else: | |
142 self.closeInventory() | |
143 self.inventory_shown = False | |
144 | |
145 def showInventory(self): | |
146 self.gui.show() | |
147 | |
148 def closeInventory(self): | |
149 self.gui.hide() | |
150 | |
151 | |
152 class _InventoryGUI(ContainerGUIBase): | |
153 """Inventory GUI class""" | |
154 def __init__(self, controller, inventory, callbacks): | |
155 """Initialise the instance. | |
156 @param controller: Current Controller | |
157 @type controller: Class derived from ControllerBase | |
158 @type inventory: Inventory | |
159 @param inventory: An inventory object to be displayed and manipulated | |
160 @type callbacks: dict | |
161 @param callbacks: a dict of callbacks | |
162 refreshReadyImages: | |
163 Function that will make the ready slots on the HUD | |
164 reflect those within the inventory | |
165 toggleInventoryButton: | |
166 Function that will toggle the state of the inventory button | |
167 @return: None""" | |
168 super(InventoryGUI, self).__init__(controller, "gui/inventory.xml") | |
169 self.engine = controller.engine | |
170 self.readyCallback = callbacks['refreshReadyImages'] | |
171 self.toggleInventoryButtonCallback = callbacks['toggleInventoryButton'] | |
172 self.original_cursor_id = self.engine.getCursor().getId() | |
173 | |
174 self.inventory_shown = False | |
175 events_to_map = {} | |
176 self.inventory_storage = inventory | |
177 | |
178 # Buttons of inventory arranged by slots | |
179 | |
180 self.slot_buttons = {'head': ('Head',), 'chest': ('Body',), | |
181 'left_arm': ('LeftHand',), | |
182 'right_arm': ('RightHand',), | |
183 'hips' : ('Belt',), 'left_leg': ('LeftFoot',), | |
184 'right_leg': ('RightFoot',), | |
185 'left_hand': ('LeftHeld',), | |
186 'right_hand': ('RightHeld',), | |
187 'backpack': ('A1', 'A2', 'A3', 'A4', 'A5', | |
188 'B1', 'B2', 'B3', 'B4', 'B5', | |
189 'C1', 'C2', 'C3', 'C4', 'C5', | |
190 'D1', 'D2', 'D3', 'D4', 'D5'), | |
191 'ready': ('Ready1', 'Ready2', 'Ready3', 'Ready4') | |
192 } | |
193 # the images that should be used for the buttons when they are "empty" | |
194 self.slot_empty_images = {'head':'gui/inv_images/inv_head.png', | |
195 'chest':'gui/inv_images/inv_torso.png', | |
196 'left_arm':'gui/inv_images/inv_lhand.png', | |
197 'right_arm':'gui/inv_images/inv_rhand.png', | |
198 'hips':'gui/inv_images/inv_belt.png', | |
199 'left_leg':'gui/inv_images/inv_lfoot.png', | |
200 'right_leg':'gui/inv_images/inv_rfoot.png', | |
201 'left_hand':'gui/inv_images/inv_litem.png', | |
202 'right_hand':'gui/inv_images/inv_ritem.png', | |
203 'backpack':'gui/inv_images/inv_backpack.png', | |
204 'ready':'gui/inv_images/inv_belt_pouches.png', | |
205 } | |
206 self.updateInventoryButtons() | |
207 | |
208 for slot in self.slot_buttons: | |
209 for _, button in enumerate(self.slot_buttons[slot]): | |
210 events_to_map[button] = cbwa(self.dragDrop, button) | |
211 events_to_map[button + "/mouseReleased"] = \ | |
212 self.showContextMenu | |
213 events_to_map['close_button'] = self.closeInventoryAndToggle | |
214 self.gui.mapEvents(events_to_map) | |
215 # TODO: Why the commented out code? | |
216 # self.resetMouseCursor() | |
217 | |
218 def updateImages(self): | |
219 self.updateInventoryButtons() | |
220 | |
221 def updateInventoryButtons (self): | |
222 for slot in self.slot_buttons: | |
223 for index, button in enumerate(self.slot_buttons[slot]): | |
224 widget = self.gui.findChild(name=button) | |
225 widget.slot = slot | |
226 widget.index = index | |
227 widget.item = self.inventory_storage.getItemsInSlot(widget.slot, | |
228 widget.index) | |
229 self.updateImage(widget) | |
230 | |
231 def updateImage(self, button): | |
232 if (button.item == None): | |
233 image = self.slot_empty_images[button.slot] | |
234 else: | |
235 image = button.item.getInventoryThumbnail() | |
236 button.up_image = image | |
237 button.down_image = image | |
238 button.hover_image = image | |
239 | |
240 def closeInventory(self): | |
241 """Close the inventory. | |
242 @return: None""" | |
243 self.gui.hide() | |
244 | |
245 def closeInventoryAndToggle(self): | |
246 """Close the inventory screen. | |
247 @return: None""" | |
248 self.closeInventory() | |
249 self.toggleInventoryButtonCallback() | |
250 self.inventory_shown = False | |
251 | |
252 def toggleInventory(self, toggleImage=True): | |
253 """Pause the game and enter the inventory screen, or close the | |
254 inventory screen and resume the game. | |
255 @type toggleImage: bool | |
256 @param toggleImage: | |
257 Call toggleInventoryCallback if True. Toggling via a | |
258 keypress requires that we toggle the Hud inventory image | |
259 explicitly. Clicking on the Hud inventory button toggles the | |
260 image implicitly, so we don't change it. | |
261 @return: None""" | |
262 if not self.inventory_shown: | |
263 self.showInventory() | |
264 self.inventory_shown = True | |
265 else: | |
266 self.closeInventory() | |
267 self.inventory_shown = False | |
268 | |
269 if toggleImage: | |
270 self.toggleInventoryButtonCallback() | |
271 | |
272 def showInventory(self): | |
273 """Show the inventory. | |
274 @return: None""" | |
275 self.updateInventoryButtons() | |
276 self.gui.show() | |
277 | |
278 def dragObject(self, obj): | |
279 """Drag the selected object. | |
280 @type obj: string | |
281 @param obj: The name of the object within | |
282 the dictionary 'self.buttons' | |
283 @return: None""" | |
284 # get the widget from the inventory with the name obj | |
285 drag_widget = self.gui.findChild(name = obj) | |
286 drag_item = drag_widget.item | |
287 # only drag if the widget is not empty | |
288 if (drag_item != None): | |
289 # get the item that the widget is 'storing' | |
290 data_drag.dragged_item = drag_widget.item | |
291 # get the up and down images of the widget | |
292 up_image = drag_widget.up_image | |
293 down_image = drag_widget.down_image | |
294 # set the mouse cursor to be the widget's image | |
295 self.controller.setMouseCursor(up_image.source,down_image.source) | |
296 data_drag.dragged_image = up_image.source | |
297 data_drag.dragging = True | |
298 data_drag.dragged_widget = drag_widget | |
299 data_drag.source_container = self.inventory_storage | |
300 | |
301 self.inventory_storage.takeItem(drag_widget.item) | |
302 # after dragging the 'item', set the widgets' images | |
303 # so that it has it's default 'empty' images | |
304 drag_widget.item = None | |
305 self.updateImage(drag_widget) | |
306 | |
307 | |
308 def dropObject(self, obj): | |
309 """Drops the object being dropped | |
310 @type obj: string | |
311 @param obj: The name of the object within | |
312 the dictionary 'self.buttons' | |
313 @return: None""" | |
314 drop_widget = self.gui.findChild(name = obj) | |
315 drop_slot, drop_index = drop_widget.slot, drop_widget.index | |
316 replace_item = None | |
317 try : | |
318 if data_drag.dragging: | |
319 inventory = self.inventory_storage | |
320 drag_item = data_drag.dragged_item | |
321 #this will get the replacement item and data for drag_drop if | |
322 ## there is an item All ready occupying the slot | |
323 if not inventory.isSlotEmpty(drop_slot, drop_index): | |
324 #get the item and then remove it from the inventory | |
325 replace_item = inventory.getItemsInSlot \ | |
326 (drop_slot, drop_index) | |
327 self.dragObject(obj) | |
328 self.inventory_storage.moveItemToSlot(drag_item, | |
329 drop_slot, | |
330 drop_index) | |
331 | |
332 if drop_widget.slot == 'ready': | |
333 self.readyCallback() | |
334 | |
335 if replace_item == None: | |
336 self.controller.resetMouseCursor() | |
337 data_drag.dragging = False | |
338 except Container.TooBig : | |
339 logger.warning("%s too big to fit " | |
340 "into %s" % (data_drag.dragged_item, | |
341 drop_widget.slot)) | |
342 except (Container.SlotBusy, Container.ItemSelf): | |
343 pass | |
344 self.updateInventoryButtons() | |
345 | |
346 def createMenuItems(self, item, actions): | |
347 """Creates context menu items for the InventoryGUI""" | |
348 menu_actions = super(InventoryGUI, self).createMenuItems(item, actions) | |
349 param_dict = {} | |
350 param_dict["controller"] = self.controller | |
351 param_dict["commands"] = {} | |
352 param_dict["item"] = item | |
353 param_dict["container_gui"] = self | |
354 menu_actions.append(["Drop", | |
355 "Drop", | |
356 self.executeMenuItem, | |
357 ACTIONS["DropFromInventory"](**param_dict)]) | |
358 return menu_actions | |
359 | |
360 def getImage(self, name): | |
361 """Return a current image from the inventory | |
362 @type name: string | |
363 @param name: name of image to get | |
364 @return: None""" | |
365 return self.gui.findChild(name = name) |