Mercurial > parpg-core
comparison src/parpg/gui/inventorygui.py @ 174:ee4c1fe78be1
Added drag and drop and context menu to the equipment part of the character screen.
author | KarstenBock@gmx.net |
---|---|
date | Sun, 09 Oct 2011 19:24:37 +0200 |
parents | 2d671e57badb |
children | 94857239b890 |
comparison
equal
deleted
inserted
replaced
173:fadc02e77ae1 | 174:ee4c1fe78be1 |
---|---|
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, container | 25 from parpg.components import equip, equipable, container |
26 from parpg.entities import General | 26 from parpg.entities import General |
27 | 27 |
28 logger = logging.getLogger('action') | 28 logger = logging.getLogger('action') |
29 | 29 |
30 class EquipmentSlot(pychan.VBox): | 30 class EquipmentSlot(pychan.VBox): |
123 "leg": "pantsSlot", | 123 "leg": "pantsSlot", |
124 "feet": "bootsSlot", | 124 "feet": "bootsSlot", |
125 "l_arm": "leftHandSlot", | 125 "l_arm": "leftHandSlot", |
126 "r_arm": "rightHandSlot", | 126 "r_arm": "rightHandSlot", |
127 } | 127 } |
128 self.setSlotEvents() | |
128 | 129 |
129 def updateImages(self): | 130 def updateImages(self): |
130 for eq_slot, gui_slot in self.equip_to_gui.iteritems(): | 131 for eq_slot, gui_slot in self.equip_to_gui.iteritems(): |
131 widget = self.gui.findChild(name=gui_slot) | 132 widget = self.gui.findChildByName(gui_slot) |
132 equipable = equip.get_equipable(self.equip, eq_slot) | 133 equipable = equip.get_equipable(self.equip, eq_slot) |
133 widget.item = equipable.entity if equipable else None | 134 widget.item = equipable.entity if equipable else None |
134 self.updateImage(widget) | 135 self.updateImage(widget) |
135 | 136 |
136 def updateImage(self, slot): | 137 def updateImage(self, slot): |
138 if (slot.item): | 139 if (slot.item): |
139 image = slot.item.containable.image | 140 image = slot.item.containable.image |
140 else: | 141 else: |
141 image = None | 142 image = None |
142 slot.image = image | 143 slot.image = image |
143 | 144 |
145 def dragObject(self, obj): | |
146 """Drag the selected object. | |
147 @type obj: string | |
148 @param obj: The name of the object | |
149 @return: None""" | |
150 # get the widget from the gui with the name obj | |
151 drag_widget = self.gui.findChildByName(obj) | |
152 drag_item = drag_widget.item | |
153 # only drag if the widget is not empty | |
154 if (drag_item != None): | |
155 if isinstance(drag_item, General): | |
156 drag_item = drag_item.containable | |
157 elif isinstance(drag_item, equipable): | |
158 drag_item = drag_item.entity.containable | |
159 drag_eq = drag_item.entity.equipable | |
160 # get the image of the widget | |
161 image = drag_widget.image | |
162 self.setDragData(drag_item, image, image) | |
163 equip.take_equipable(self.equip, drag_eq.in_slot) | |
164 | |
165 # after dragging the 'item', set the widgets' images | |
166 # so that it has it's default 'empty' images | |
167 drag_widget.item = None | |
168 self.updateImage(drag_widget) | |
169 | |
170 def dropObject(self, obj): | |
171 """Drops the object being dropped | |
172 @type obj: string | |
173 @param obj: The name of the object | |
174 @return: None""" | |
175 try: | |
176 drop_widget = self.gui.findChildByName(obj) | |
177 drop_slot = drop_widget.slot | |
178 replace_item = None | |
179 | |
180 if data_drag.dragging: | |
181 drag_item = data_drag.dragged_item.entity | |
182 if drag_item.equipable: | |
183 drag_item = drag_item.equipable | |
184 else: | |
185 return | |
186 #this will get the replacement item and the data for drag_drop | |
187 #if there is an item all ready occupying the slot | |
188 replace_item = ( | |
189 equip.equip(self.equip, drag_item, drop_slot) | |
190 ) | |
191 | |
192 #if there was no item the stop dragging and reset cursor | |
193 if replace_item: | |
194 image = drop_widget.image | |
195 self.setDragData(replace_item.entity.containable, image, image) | |
196 else: | |
197 data_drag.dragging = False | |
198 #reset the mouse cursor to the normal cursor | |
199 self.controller.resetMouseCursor() | |
200 drop_widget.item = drag_item.entity | |
201 self.updateImage(drop_widget) | |
202 except (equip.AlreadyEquippedError, equip.CannotBeEquippedInSlot): | |
203 #Do we want to notify the player why the item can't be dropped? | |
204 pass | |
205 | |
206 def mousePressedOnSlot(self, event, widget): | |
207 if event.getButton() == event.LEFT: | |
208 self.dragDrop(widget.name) | |
209 | |
210 def setSlotEvents(self): | |
211 events_to_map = {} | |
212 for eq_slot, gui_slot in self.equip_to_gui.iteritems(): | |
213 widget = self.gui.findChildByName(gui_slot) | |
214 slot_name = widget.name | |
215 widget.slot = eq_slot | |
216 events_to_map[slot_name + "/mousePressed"] = ( | |
217 self.mousePressedOnSlot | |
218 ) | |
219 events_to_map[slot_name + "/mouseReleased"] = self.showContextMenu | |
220 | |
221 self.gui.mapEvents(events_to_map) | |
222 | |
144 class InventoryGUI(ContainerGUIBase): | 223 class InventoryGUI(ContainerGUIBase): |
145 def __init__(self, controller, gui, container, callbacks): | 224 def __init__(self, controller, gui, container, callbacks): |
146 ContainerGUIBase.__init__(self, controller, gui) | 225 ContainerGUIBase.__init__(self, controller, gui) |
147 self.grid = self.gui.findChildByName("Grid") | 226 self.grid = self.gui.findChildByName("Grid") |
148 assert(isinstance(self.grid, InventoryGrid)) | 227 assert(isinstance(self.grid, InventoryGrid)) |
185 self.dragDrop(widget.name) | 264 self.dragDrop(widget.name) |
186 | 265 |
187 def dragObject(self, obj): | 266 def dragObject(self, obj): |
188 """Drag the selected object. | 267 """Drag the selected object. |
189 @type obj: string | 268 @type obj: string |
190 @param obj: The name of the object within | 269 @param obj: The name of the object |
191 the dictionary 'self.buttons' | |
192 @return: None""" | 270 @return: None""" |
193 # get the widget from the gui with the name obj | 271 # get the widget from the gui with the name obj |
194 drag_widget = self.gui.findChild(name = obj) | 272 drag_widget = self.gui.findChild(name = obj) |
195 drag_item = drag_widget.item | 273 drag_item = drag_widget.item |
196 # only drag if the widget is not empty | 274 # only drag if the widget is not empty |
208 self.updateImage(drag_widget) | 286 self.updateImage(drag_widget) |
209 | 287 |
210 def dropObject(self, obj): | 288 def dropObject(self, obj): |
211 """Drops the object being dropped | 289 """Drops the object being dropped |
212 @type obj: string | 290 @type obj: string |
213 @param obj: The name of the object within | 291 @param obj: The name of the object |
214 the dictionary 'self.buttons' | |
215 @return: None""" | 292 @return: None""" |
216 try: | 293 try: |
217 drop_widget = self.gui.findChildByName(obj) | 294 drop_widget = self.gui.findChildByName(obj) |
218 drop_index = drop_widget.index | 295 drop_index = drop_widget.index |
219 replace_item = None | 296 replace_item = None |