comparison src/parpg/gui/inventorygui.py @ 153:2ba7c6d8f738

Added drag and drop functionality to the players inventory grid.
author KarstenBock@gmx.net
date Thu, 06 Oct 2011 16:04:48 +0200
parents 3fc7cfa80771
children 50c248656348
comparison
equal deleted inserted replaced
152:df3896fe2bf8 153:2ba7c6d8f738
20 from fife.extensions.pychan.attrs import UnicodeAttr 20 from fife.extensions.pychan.attrs import UnicodeAttr
21 21
22 from parpg.gui import drag_drop_data as data_drag 22 from parpg.gui import drag_drop_data as data_drag
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, container
26 from parpg.entities import General
26 27
27 logger = logging.getLogger('action') 28 logger = logging.getLogger('action')
28 29
29 class EquipmentSlot(pychan.VBox): 30 class EquipmentSlot(pychan.VBox):
30 def __init__(self, min_size=(50, 50), 31 def __init__(self, min_size=(50, 50),
78 row = pychan.HBox(min_size=row_size, max_size=row_size, 79 row = pychan.HBox(min_size=row_size, max_size=row_size,
79 padding=self.padding) 80 padding=self.padding)
80 row.border_size = 1 81 row.border_size = 1
81 row.opaque = 0 82 row.opaque = 0
82 for column_n in xrange(n_columns): 83 for column_n in xrange(n_columns):
84 index = (row_n * n_columns + column_n)
83 slot = pychan.Icon(min_size=(50, 50), max_size=(50, 50)) 85 slot = pychan.Icon(min_size=(50, 50), max_size=(50, 50))
84 slot.border_size = 1 86 slot.border_size = 1
85 slot.name = "Slot_%d" % (row_n * n_columns + column_n) 87 slot.name = "Slot_%d" % index
88 slot.index = index
86 row.addChild(slot) 89 row.addChild(slot)
87 self.addChild(row) 90 self.addChild(row)
88 self.min_size = ((n_columns * 50) + 2, (n_rows * 50) + 2) 91 self.min_size = ((n_columns * 50) + 2, (n_rows * 50) + 2)
89 self.max_size = self.min_size 92 self.max_size = self.min_size
90 93
139 slot.image = image 142 slot.image = image
140 143
141 class InventoryGUI(ContainerGUIBase): 144 class InventoryGUI(ContainerGUIBase):
142 def __init__(self, controller, gui, container, callbacks): 145 def __init__(self, controller, gui, container, callbacks):
143 ContainerGUIBase.__init__(self, controller, gui) 146 ContainerGUIBase.__init__(self, controller, gui)
147 self.grid = self.gui.findChildByName("Grid")
148 assert(isinstance(self.grid, InventoryGrid))
144 self.container = container 149 self.container = container
150 self.setSlotEvents()
151
152 def setSlotEvents(self):
153 slot_count = self.grid.n_rows * self.grid.n_columns
154 events_to_map = {}
155 for counter in xrange(0, slot_count):
156 widget = self.grid.getSlot(counter)
157 slot_name = widget.name
158 widget.index = counter
159 events_to_map[slot_name + "/mousePressed"] = self.mousePressedOnSlot
160 #events_to_map[slot_name + "/mouseReleased"] = \
161 # self.showContextMenu
162
163 self.grid.mapEvents(events_to_map)
145 164
146 def updateImages(self): 165 def updateImages(self):
147 grid = self.gui.findChildByName("Grid")
148 assert(isinstance(grid, InventoryGrid))
149 for index, child in enumerate(self.container.children): 166 for index, child in enumerate(self.container.children):
150 slot = grid.getSlot(index) 167 slot = self.grid.getSlot(index)
151 if child: 168 if child:
152 slot.item = child.entity 169 slot.item = child.entity
153 else: 170 else:
154 slot.item = None 171 slot.item = None
155 self.updateImage(slot) 172 self.updateImage(slot)
159 if (slot.item): 176 if (slot.item):
160 image = slot.item.containable.image 177 image = slot.item.containable.image
161 else: 178 else:
162 image = None 179 image = None
163 slot.image = image 180 slot.image = image
181
182 def mousePressedOnSlot(self, event, widget):
183 if event.getButton() == event.LEFT:
184 self.dragDrop(widget.name)
185
186 def dragObject(self, obj):
187 """Drag the selected object.
188 @type obj: string
189 @param obj: The name of the object within
190 the dictionary 'self.buttons'
191 @return: None"""
192 # get the widget from the gui with the name obj
193 drag_widget = self.gui.findChild(name = obj)
194 drag_item = drag_widget.item
195 # only drag if the widget is not empty
196 if (drag_item != None):
197 if isinstance(drag_item, General):
198 drag_item = drag_item.containable
199 # get the image of the widget
200 image = drag_widget.image
201 self.setDragData(drag_item, image, image, self.container)
202 container.take_item(self.container, drag_item.slot)
203
204 # after dragging the 'item', set the widgets' images
205 # so that it has it's default 'empty' images
206 drag_widget.item = None
207 self.updateImage(drag_widget)
208
209 def dropObject(self, obj):
210 """Drops the object being dropped
211 @type obj: string
212 @param obj: The name of the object within
213 the dictionary 'self.buttons'
214 @return: None"""
215 try:
216 drop_widget = self.gui.findChildByName(obj)
217 drop_index = drop_widget.index
218 replace_item = None
219
220 if data_drag.dragging:
221 drag_item = data_drag.dragged_item
222 #this will get the replacement item and the data for drag_drop if
223 ## there is an item all ready occupying the slot
224 replace_item = (
225 container.put_item(self.container, drag_item, drop_index)
226 )
227
228 #if there was no item the stop dragging and reset cursor
229 if replace_item:
230 image = drop_widget.image
231 self.setDragData(replace_item, image, image, self.container)
232 else:
233 data_drag.dragging = False
234 #reset the mouse cursor to the normal cursor
235 self.controller.resetMouseCursor()
236 drop_widget.item = drag_item.entity
237 self.updateImage(drop_widget)
238 except (container.BulkLimitError):
239 #Do we want to notify the player why the item can't be dropped?
240 pass
164 241
165 class CharacterGUI(object): 242 class CharacterGUI(object):
166 def __init__(self, controller, gui, container, equip, callbacks): 243 def __init__(self, controller, gui, container, equip, callbacks):
167 self.engine = controller.engine 244 self.engine = controller.engine
168 self.inventory_shown = False 245 self.inventory_shown = False