Package scripts :: Package gui :: Script inventorygui
[hide private]
[frames] | no frames]

Source Code for Script scripts.gui.inventorygui

  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   
 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   
25 -class InventoryGUI(ContainerGUIBase):
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 # Buttons of inventory arranged by slots 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 # the images that should be used for the buttons when they are "empty" 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 # TODO: Why the commented out code? 89 # self.resetMouseCursor() 90
91 - def updateImages(self):
93
94 - def updateInventoryButtons (self):
95 for slot in self.slot_buttons: 96 for index, button in enumerate(self.slot_buttons[slot]): 97 widget = self.gui.findChild(name=button) 98 widget.slot = slot 99 widget.index = index 100 widget.item = self.inventory_storage.getItemsInSlot(widget.slot, 101 widget.index) 102 self.updateImage(widget)
103
104 - def updateImage(self, button):
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
113 - def closeInventory(self):
114 """Close the inventory. 115 @return: None""" 116 self.gui.hide()
117
118 - def closeInventoryAndToggle(self):
119 """Close the inventory screen. 120 @return: None""" 121 self.closeInventory() 122 self.toggleInventoryButtonCallback() 123 self.inventory_shown = False
124
125 - def toggleInventory(self, toggleImage=True):
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
145 - def showInventory(self):
146 """Show the inventory. 147 @return: None""" 148 self.updateInventoryButtons() 149 self.gui.show()
150
151 - def dragObject(self, obj):
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 # get the widget from the inventory with the name obj 158 drag_widget = self.gui.findChild(name = obj) 159 drag_item = drag_widget.item 160 # only drag if the widget is not empty 161 if (drag_item != None): 162 # get the item that the widget is 'storing' 163 data_drag.dragged_item = drag_widget.item 164 # get the up and down images of the widget 165 up_image = drag_widget.up_image 166 down_image = drag_widget.down_image 167 # set the mouse cursor to be the widget's image 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 # after dragging the 'item', set the widgets' images 176 # so that it has it's default 'empty' images 177 drag_widget.item = None 178 self.updateImage(drag_widget)
179 180
181 - def dropObject(self, obj):
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 #this will get the replacement item and data for drag_drop if 195 ## there is an item All ready occupying the slot 196 if not inventory.isSlotEmpty(drop_slot, drop_index): 197 #get the item and then remove it from the inventory 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
218 - def createMenuItems(self, item, actions):
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
232 - def getImage(self, name):
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