1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 from fife.extensions.pychan.tools import callbackWithArguments as cbwa
19
20 from scripts.gui import drag_drop_data as data_drag
21 from scripts.objects.base import Container
22 from scripts.gui.containergui_base import ContainerGUIBase
23 from scripts.objects.action import ACTIONS
24
26 """Inventory GUI class"""
27 - def __init__(self, controller, inventory, callbacks):
28 """Initialise the instance.
29 @param controller: Current Controller
30 @type controller: Class derived from ControllerBase
31 @type inventory: Inventory
32 @param inventory: An inventory object to be displayed and manipulated
33 @type callbacks: dict
34 @param callbacks: a dict of callbacks
35 refreshReadyImages:
36 Function that will make the ready slots on the HUD
37 reflect those within the inventory
38 toggleInventoryButton:
39 Function that will toggle the state of the inventory button
40 @return: None"""
41 super(InventoryGUI, self).__init__(controller, "gui/inventory.xml")
42 self.engine = controller.engine
43 self.readyCallback = callbacks['refreshReadyImages']
44 self.toggleInventoryButtonCallback = callbacks['toggleInventoryButton']
45 self.original_cursor_id = self.engine.getCursor().getId()
46
47 self.inventory_shown = False
48 events_to_map = {}
49 self.inventory_storage = inventory
50
51
52
53 self.slot_buttons = {'head': ('Head',), 'chest': ('Body',),
54 'left_arm': ('LeftHand',),
55 'right_arm': ('RightHand',),
56 'hips' : ('Belt',), 'left_leg': ('LeftFoot',),
57 'right_leg': ('RightFoot',),
58 'left_hand': ('LeftHeld',),
59 'right_hand': ('RightHeld',),
60 'backpack': ('A1', 'A2', 'A3', 'A4', 'A5',
61 'B1', 'B2', 'B3', 'B4', 'B5',
62 'C1', 'C2', 'C3', 'C4', 'C5',
63 'D1', 'D2', 'D3', 'D4', 'D5'),
64 'ready': ('Ready1', 'Ready2', 'Ready3', 'Ready4')
65 }
66
67 self.slot_empty_images = {'head':'gui/inv_images/inv_head.png',
68 'chest':'gui/inv_images/inv_torso.png',
69 'left_arm':'gui/inv_images/inv_lhand.png',
70 'right_arm':'gui/inv_images/inv_rhand.png',
71 'hips':'gui/inv_images/inv_belt.png',
72 'left_leg':'gui/inv_images/inv_lfoot.png',
73 'right_leg':'gui/inv_images/inv_rfoot.png',
74 'left_hand':'gui/inv_images/inv_litem.png',
75 'right_hand':'gui/inv_images/inv_ritem.png',
76 'backpack':'gui/inv_images/inv_backpack.png',
77 'ready':'gui/inv_images/inv_belt_pouches.png',
78 }
79 self.updateInventoryButtons()
80
81 for slot in self.slot_buttons:
82 for _, button in enumerate(self.slot_buttons[slot]):
83 events_to_map[button] = cbwa(self.dragDrop, button)
84 events_to_map[button + "/mouseReleased"] = \
85 self.showContextMenu
86 events_to_map['close_button'] = self.closeInventoryAndToggle
87 self.gui.mapEvents(events_to_map)
88
89
90
93
103
105 if (button.item == None):
106 image = self.slot_empty_images[button.slot]
107 else:
108 image = button.item.getInventoryThumbnail()
109 button.up_image = image
110 button.down_image = image
111 button.hover_image = image
112
114 """Close the inventory.
115 @return: None"""
116 self.gui.hide()
117
119 """Close the inventory screen.
120 @return: None"""
121 self.closeInventory()
122 self.toggleInventoryButtonCallback()
123 self.inventory_shown = False
124
126 """Pause the game and enter the inventory screen, or close the
127 inventory screen and resume the game.
128 @type toggleImage: bool
129 @param toggleImage:
130 Call toggleInventoryCallback if True. Toggling via a
131 keypress requires that we toggle the Hud inventory image
132 explicitly. Clicking on the Hud inventory button toggles the
133 image implicitly, so we don't change it.
134 @return: None"""
135 if not self.inventory_shown:
136 self.showInventory()
137 self.inventory_shown = True
138 else:
139 self.closeInventory()
140 self.inventory_shown = False
141
142 if toggleImage:
143 self.toggleInventoryButtonCallback()
144
150
152 """Drag the selected object.
153 @type obj: string
154 @param obj: The name of the object within
155 the dictionary 'self.buttons'
156 @return: None"""
157
158 drag_widget = self.gui.findChild(name = obj)
159 drag_item = drag_widget.item
160
161 if (drag_item != None):
162
163 data_drag.dragged_item = drag_widget.item
164
165 up_image = drag_widget.up_image
166 down_image = drag_widget.down_image
167
168 self.controller.setMouseCursor(up_image.source,down_image.source)
169 data_drag.dragged_image = up_image.source
170 data_drag.dragging = True
171 data_drag.dragged_widget = drag_widget
172 data_drag.source_container = self.inventory_storage
173
174 self.inventory_storage.takeItem(drag_widget.item)
175
176
177 drag_widget.item = None
178 self.updateImage(drag_widget)
179
180
182 """Drops the object being dropped
183 @type obj: string
184 @param obj: The name of the object within
185 the dictionary 'self.buttons'
186 @return: None"""
187 drop_widget = self.gui.findChild(name = obj)
188 drop_slot, drop_index = drop_widget.slot, drop_widget.index
189 replace_item = None
190 try :
191 if data_drag.dragging:
192 inventory = self.inventory_storage
193 drag_item = data_drag.dragged_item
194
195
196 if not inventory.isSlotEmpty(drop_slot, drop_index):
197
198 replace_item = inventory.getItemsInSlot \
199 (drop_slot, drop_index)
200 self.dragObject(obj)
201 self.inventory_storage.moveItemToSlot(drag_item,
202 drop_slot,
203 drop_index)
204
205 if drop_widget.slot == 'ready':
206 self.readyCallback()
207
208 if replace_item == None:
209 self.controller.resetMouseCursor()
210 data_drag.dragging = False
211 except Container.TooBig :
212 print("%s too big to fit into %s" % (data_drag.dragged_item,
213 drop_widget.slot))
214 except (Container.SlotBusy, Container.ItemSelf):
215 pass
216 self.updateInventoryButtons()
217
219 """Creates context menu items for the InventoryGUI"""
220 menu_actions = super(InventoryGUI, self).createMenuItems(item, actions)
221 param_dict = {}
222 param_dict["controller"] = self.controller
223 param_dict["commands"] = {}
224 param_dict["item"] = item
225 param_dict["container_gui"] = self
226 menu_actions.append(["Drop",
227 "Drop",
228 self.executeMenuItem,
229 ACTIONS["DropFromInventory"](**param_dict)])
230 return menu_actions
231
233 """Return a current image from the inventory
234 @type name: string
235 @param name: name of image to get
236 @return: None"""
237 return self.gui.findChild(name = name)
238