Mercurial > parpg-core
comparison src/parpg/gui/inventorygui.py @ 149:eab3e1e52497
Modified EquipmentSlot to display an image instead of a text.
Added EquipmentGui class, which handles the equipment slots of the player screen.
An EquipmentGui instance will be created in the InventoryGUI constructor.
The initializeInventory method of the Hud class supplies the players inventory and equipment to the InventoryGUI constructor.
author | KarstenBock@gmx.net |
---|---|
date | Wed, 05 Oct 2011 11:04:39 +0200 |
parents | a5ea5b8f63d4 |
children | 3fc7cfa80771 |
comparison
equal
deleted
inserted
replaced
148:5756e615b029 | 149:eab3e1e52497 |
---|---|
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 | 12 |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. | 14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>. |
15 import logging | |
15 | 16 |
16 from fife.extensions.pychan.tools import callbackWithArguments as cbwa | 17 from fife.extensions.pychan.tools import callbackWithArguments as cbwa |
17 from fife.extensions import pychan | 18 from fife.extensions import pychan |
18 from fife.extensions.pychan.attrs import UnicodeAttr | 19 from fife.extensions.pychan.attrs import UnicodeAttr |
19 | 20 |
20 from parpg.gui import drag_drop_data as data_drag | 21 from parpg.gui import drag_drop_data as data_drag |
21 #from parpg.objects.base import Container | 22 #from parpg.objects.base import Container |
22 from parpg.gui.containergui_base import ContainerGUIBase | 23 from parpg.gui.containergui_base import ContainerGUIBase |
23 from parpg.objects.action import ACTIONS | 24 from parpg.objects.action import ACTIONS |
24 | 25 from parpg.components import equip |
25 import logging | |
26 | 26 |
27 logger = logging.getLogger('action') | 27 logger = logging.getLogger('action') |
28 | 28 |
29 class EquipmentSlot(pychan.VBox): | 29 class EquipmentSlot(pychan.VBox): |
30 ATTRIBUTES = pychan.VBox.ATTRIBUTES + [UnicodeAttr('label_text')] | 30 def __init__(self, min_size=(50, 50), |
31 | |
32 def _setLabelText(self, text): | |
33 label = self.findChild() | |
34 label.text = unicode(text) | |
35 label.resizeToContent() | |
36 self.margins = ( | |
37 int((self.width - label.width) / 2.0), | |
38 int((self.height - label.height) / 2.0) | |
39 ) | |
40 | |
41 def _getLabelText(self): | |
42 label = self.findChild() | |
43 return label.text | |
44 | |
45 label_text = property(fget=_getLabelText, fset=_setLabelText) | |
46 | |
47 def __init__(self, label_text=u'equipment', min_size=(50, 50), | |
48 max_size=(50, 50), margins=None, | 31 max_size=(50, 50), margins=None, |
49 **kwargs): | 32 **kwargs): |
50 pychan.VBox.__init__(self, min_size=min_size, max_size=max_size, | 33 pychan.VBox.__init__(self, min_size=min_size, max_size=max_size, |
51 **kwargs) | 34 **kwargs) |
52 self.background_image = 'gui/inv_images/inv_background.png' | 35 self.background_image = 'gui/inv_images/inv_background.png' |
53 label = pychan.Label(text=unicode(label_text)) | 36 icon = pychan.Icon(name="Icon") |
54 self.addChild(label) | 37 self.addChild(icon) |
55 self.label_text = label_text | |
56 self.adaptLayout() | 38 self.adaptLayout() |
57 if self.parent is not None: | 39 if self.parent is not None: |
58 self.beforeShow() | 40 self.beforeShow() |
59 | 41 |
42 @property | |
43 def image(self): | |
44 icon = self.findChildByName("Icon") | |
45 return icon.image | |
46 | |
47 @image.setter | |
48 def image(self, image): | |
49 icon = self.findChildByName("Icon") | |
50 icon.image = image | |
60 | 51 |
61 class InventoryGrid(pychan.VBox): | 52 class InventoryGrid(pychan.VBox): |
62 ATTRIBUTES = pychan.VBox.ATTRIBUTES + [pychan.attrs.PointAttr('grid_size')] | 53 ATTRIBUTES = pychan.VBox.ATTRIBUTES + [pychan.attrs.PointAttr('grid_size')] |
63 | 54 |
64 def _setNColumns(self, n_columns): | 55 def _setNColumns(self, n_columns): |
107 self.opaque = 0 | 98 self.opaque = 0 |
108 self.grid_size = grid_size | 99 self.grid_size = grid_size |
109 self.border_size = 1 | 100 self.border_size = 1 |
110 | 101 |
111 | 102 |
103 class EquipmentGui(ContainerGUIBase): | |
104 def __init__(self, controller, gui, equip, callbacks): | |
105 ContainerGUIBase.__init__(self, controller, gui) | |
106 self.equip = equip | |
107 self.equip_to_gui = { | |
108 "head": "headSlot", | |
109 "neck": "neckSlot", | |
110 "body": "shirtSlot", | |
111 "belt": "beltSlot", | |
112 "leg": "pantsSlot", | |
113 "feet": "bootsSlot", | |
114 "l_arm": "leftHandSlot", | |
115 "r_arm": "rightHandSlot", | |
116 } | |
117 | |
118 def updateImages(self): | |
119 for eq_slot, gui_slot in self.equip_to_gui.iteritems(): | |
120 widget = self.gui.findChild(name=gui_slot) | |
121 equipable = equip.get_equipable(self.equip, eq_slot) | |
122 widget.item = equipable.entity if equipable else None | |
123 self.updateImage(widget) | |
124 | |
125 def updateImage(self, slot): | |
126 assert(isinstance(slot, EquipmentSlot)) | |
127 if (slot.item): | |
128 image = slot.item.containable.image | |
129 else: | |
130 image = None | |
131 slot.image = image | |
132 | |
112 class InventoryGUI(ContainerGUIBase): | 133 class InventoryGUI(ContainerGUIBase): |
113 def __init__(self, controller, inventory, callbacks): | 134 def __init__(self, controller, gui, container, equip, callbacks): |
114 super(InventoryGUI, self).__init__(controller, inventory) | 135 super(InventoryGUI, self).__init__(controller, gui) |
115 self.engine = controller.engine | 136 self.engine = controller.engine |
116 self.inventory_shown = False | 137 self.inventory_shown = False |
117 render_backend = self.engine.getRenderBackend() | 138 render_backend = self.engine.getRenderBackend() |
118 screen_mode = render_backend.getCurrentScreenMode() | 139 screen_mode = render_backend.getCurrentScreenMode() |
119 screen_width, screen_height = (screen_mode.getWidth(), | 140 screen_width, screen_height = (screen_mode.getWidth(), |
120 screen_mode.getHeight()) | 141 screen_mode.getHeight()) |
121 widget_width, widget_height = self.gui.size | 142 widget_width, widget_height = self.gui.size |
122 self.gui.position = ((screen_width - widget_width) / 2, | 143 self.gui.position = ((screen_width - widget_width) / 2, |
123 (screen_height - widget_height) / 2) | 144 (screen_height - widget_height) / 2) |
145 self.equip_gui = EquipmentGui( | |
146 controller, | |
147 self.gui.findChildByName("equipmentPage"), | |
148 equip, callbacks | |
149 ) | |
124 | 150 |
125 def toggleInventory(self, toggleImage=True): | 151 def toggleInventory(self, toggleImage=True): |
126 """Pause the game and enter the inventory screen, or close the | 152 """Pause the game and enter the inventory screen, or close the |
127 inventory screen and resume the game. | 153 inventory screen and resume the game. |
128 @type toggleImage: bool | 154 @type toggleImage: bool |
138 else: | 164 else: |
139 self.closeInventory() | 165 self.closeInventory() |
140 self.inventory_shown = False | 166 self.inventory_shown = False |
141 | 167 |
142 def showInventory(self): | 168 def showInventory(self): |
169 self.equip_gui.updateImages() | |
143 self.gui.show() | 170 self.gui.show() |
144 | 171 |
145 def closeInventory(self): | 172 def closeInventory(self): |
146 self.gui.hide() | 173 self.gui.hide() |
147 | 174 |