source: trunk/game/scripts/gui/inventorygui.py @ 444

Revision 444, 10.2 KB checked in by b0rland_parpg, 10 years ago (diff)

Ticket #107: patch by b0rland
Ready slots in hud are now updated in sync with inventory

  • Property svn:eol-style set to native
RevLine 
[161]1#/usr/bin/python
[74]2
[143]3#   This file is part of PARPG.
[74]4
[143]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
[346]18import fife
19import pychan
[372]20from scripts.gui import drag_drop_data as data_drag
[74]21from pychan.tools import callbackWithArguments as cbwa
22
[346]23class InventoryGUI(object):
24    """Inventory GUI class"""
[424]25    def __init__(self, engine, inventory, callbacks):
[143]26        """Initialise the instance.
27           @type engine: fife.Engine
[144]28           @param engine: An instance of the fife engine
[424]29           @type inventory: Inventory
30           @param inventory: An inventory object to be displayed and manipulated
[270]31           @type callbacks: dict
32           @param callbacks: a dict of callbacks
33               refreshReadyImages:
34                   Function that will make the ready slots on the HUD
35                   reflect those within the inventory
36               toggleInventoryButton:
37                   Function that will toggle the state of the inventory button
[143]38           @return: None"""
[74]39        pychan.init(engine, debug = True)
40        self.engine = engine
[270]41        self.readyCallback = callbacks['refreshReadyImages']
42        self.toggleInventoryButtonCallback = callbacks['toggleInventoryButton']
[74]43        self.original_cursor_id = self.engine.getCursor().getId()
[424]44
[74]45        self.inventory = pychan.loadXML("gui/inventory.xml")
[346]46        self.inventory_shown = False
[424]47        events_to_map = {}
48        self.inventory_storage = inventory
49       
50        # Buttons of inventory arranged by slots
51
52        self.slot_buttons = {'head': ('Head',), 'chest': ('Body',),
53                             'left_arm': ('LeftHand',),
54                             'right_arm': ('RightHand',),
55                             'hips' : ('Belt',), 'left_leg': ('LeftFoot',),
56                             'right_leg': ('RightFoot',),
57                             'left_hand': ('LeftHeld',),
58                             'right_hand': ('RightHeld',),
59                             'backpack': ('A1', 'A2', 'A3', 'A4', 'A5',
60                                          'B1', 'B2', 'B3', 'B4', 'B5',
61                                          'C1', 'C2', 'C3', 'C4', 'C5',
62                                          'D1', 'D2', 'D3', 'D4', 'D5'),
63                             'ready': ('Ready1', 'Ready2', 'Ready3', 'Ready4')
64        }
[74]65        # the images that should be used for the buttons when they are "empty"
[424]66        self.slot_empty_images = {'head':'gui/inv_images/inv_head.png',
67                                  'chest':'gui/inv_images/inv_torso.png',
68                                  'left_arm':'gui/inv_images/inv_lhand.png',
69                                  'right_arm':'gui/inv_images/inv_rhand.png',
70                                  'hips':'gui/inv_images/inv_belt.png',
71                                  'left_leg':'gui/inv_images/inv_lfoot.png',
72                                  'right_leg':'gui/inv_images/inv_rfoot.png',
73                                  'left_hand':'gui/inv_images/inv_litem.png',
74                                  'right_hand':'gui/inv_images/inv_ritem.png',
75                                  'backpack':'gui/inv_images/inv_backpack.png',
76                                  'ready':'gui/inv_images/inv_belt_pouches.png',
77                                  }
[435]78        self.updateInventoryButtons()
[205]79
[424]80        for slot in self.slot_buttons:
81            for index, button in enumerate(self.slot_buttons[slot]):
[435]82                events_to_map[button] = cbwa(self.dragDrop, button)
83        events_to_map['close_button'] = self.closeInventoryAndToggle
84        self.inventory.mapEvents(events_to_map)
85#        self.resetMouseCursor()
86
87    def updateInventoryButtons (self):
88        for slot in self.slot_buttons:
89            for index, button in enumerate(self.slot_buttons[slot]):
[424]90                widget = self.inventory.findChild(name=button)
91                widget.slot = slot
92                widget.index = index
93                widget.item = self.inventory_storage.getItemsInSlot(widget.slot,
94                                                                   widget.index)
95                self.updateImage(widget)
[205]96
[424]97    def updateImage(self, button):
98        if (button.item == None):
99            image = self.slot_empty_images[button.slot]
100        else:
101            image = button.item.getInventoryThumbnail()
102        button.up_image = image
103        button.down_image = image
104        button.hover_image = image
[205]105
[65]106    def closeInventory(self):
[143]107        """Close the inventory.
108           @return: None"""
[74]109        self.inventory.hide()
110
[261]111    def closeInventoryAndToggle(self):
112        """Close the inventory screen.
113           @return: None"""
114        self.closeInventory()
115        self.toggleInventoryButtonCallback()
[346]116        self.inventory_shown = False
[261]117
[270]118    def toggleInventory(self, toggleImage=True):
[261]119        """Pause the game and enter the inventory screen, or close the
[270]120           inventory screen and resume the game.
121           @type toggleImage: bool
122           @param toggleImage:
123               Call toggleInventoryCallback if True. Toggling via a
124               keypress requires that we toggle the Hud inventory image
125               explicitly. Clicking on the Hud inventory button toggles the
126               image implicitly, so we don't change it.
[261]127           @return: None"""
[346]128        if not self.inventory_shown:
[261]129            self.showInventory()
[346]130            self.inventory_shown = True
[261]131        else:
132            self.closeInventory()
[346]133            self.inventory_shown = False
[270]134
135        if toggleImage:
[261]136            self.toggleInventoryButtonCallback()
137
[65]138    def showInventory(self):
[143]139        """Show the inventory.
140           @return: None"""
[74]141        self.inventory.show()
142
[346]143    def setMouseCursor(self, image, dummy_image, type="native"): 
[143]144        """Set the mouse cursor to an image.
[161]145           @type image: string
146           @param image: The image you want to set the cursor to
147           @type dummy_image: string
[143]148           @param dummy_image: ???
149           @type type: string
150           @param type: ???
151           @return: None"""
[74]152        cursor = self.engine.getCursor()
153        cursor_type = fife.CURSOR_IMAGE
154        img_pool = self.engine.getImagePool()
155        if(type == "target"):
156            target_cursor_id = img_pool.addResourceFromFile(image) 
157            dummy_cursor_id = img_pool.addResourceFromFile(dummy_image)
[424]158            cursor.set(cursor_type,dummy_cursor_id)
[74]159            cursor.setDrag(cursor_type,target_cursor_id,-16,-16)
160        else:
161            cursor_type = fife.CURSOR_IMAGE
162            zero_cursor_id = img_pool.addResourceFromFile(image)
163            cursor.set(cursor_type,zero_cursor_id)
164            cursor.setDrag(cursor_type,zero_cursor_id)
165           
[65]166    def resetMouseCursor(self):
[143]167        """Reset cursor to default image.
168           @return: None"""
[74]169        c = self.engine.getCursor()
170        cursor_type = fife.CURSOR_NATIVE
171        # this is the path to the default image
172        cursor_id = self.original_cursor_id
173        c.setDrag(cursor_type, cursor_id)
174        c.set(cursor_type, cursor_id)
175       
176    def dragDrop(self, obj):
[143]177        """Decide whether to drag or drop the image.
[161]178           @type obj: string
179           @param obj: The name of the object within
180                       the dictionary 'self.buttons'
[143]181           @return: None"""
[202]182        if(data_drag.dragging == True):
[74]183            self.dropObject(obj)
[202]184        elif(data_drag.dragging == False):
[74]185            self.dragObject(obj)
186               
187    def dragObject(self, obj):
[143]188        """Drag the selected object.
[161]189           @type obj: string
190           @param obj: The name of the object within
191                       the dictionary 'self.buttons'
[143]192           @return: None"""
[161]193        # get the widget from the inventory with the name obj
[74]194        drag_widget = self.inventory.findChild(name = obj)
[424]195        drag_item = drag_widget.item
[202]196        # only drag if the widget is not empty
[424]197        if (drag_item != None):
[202]198            # get the item that the widget is 'storing'
199            data_drag.dragged_item = drag_widget.item
200            # get the up and down images of the widget
[267]201            up_image = drag_widget.up_image
202            down_image = drag_widget.down_image
[202]203            # set the mouse cursor to be the widget's image
[267]204            self.setMouseCursor(up_image.source,down_image.source)
205            data_drag.dragged_image = up_image.source
[202]206            data_drag.dragging = True
[424]207            data_drag.dragged_widget = drag_widget
208            data_drag.dragged_container=self.inventory_storage
[202]209            # after dragging the 'item', set the widgets' images
210            # so that it has it's default 'empty' images
[424]211            drag_widget.item = None
212            self.updateImage(drag_widget)
[202]213           
[424]214           
[74]215    def dropObject(self, obj):
[143]216        """Drops the object being dropped
[161]217           @type obj: string
218           @param obj: The name of the object within
219                       the dictionary 'self.buttons'
[143]220           @return: None"""
221
[424]222        drop_widget = self.inventory.findChild(name = obj)
223        if drop_widget != data_drag.dragged_widget:
224            if data_drag.dragged_container != self.inventory_storage:
225                data_drag.dragged_container.takeItem(data_drag.dragged_item)
226            self.inventory_storage.moveItemToSlot(data_drag.dragged_item,
227                                                  drop_widget.slot, drop_widget.index)
[444]228        if drop_widget.slot == 'ready':
229            self.readyCallback()
[443]230        self.updateInventoryButtons()
[424]231        self.resetMouseCursor()
232        data_drag.dragging = False
233             
[270]234    def getImage(self, name):
235        """Return a current image from the inventory
236           @type name: string
237           @param name: name of image to get
238           @return: None"""
239        return self.inventory.findChild(name=name)
[208]240
Note: See TracBrowser for help on using the repository browser.