source: trunk/game/scripts/inventory.py @ 253

Revision 208, 12.3 KB checked in by bretzel_parpg, 10 years ago (diff)

Added a getItems function to inventory.py that will return a dictionary that is arranged
like: {slot:item, slot:item}. Also fixed an issue with the title in world.createBoxGUI

  • Property svn:eol-style set to native
Line 
1#/usr/bin/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
18import sys, os, fife, fifelog, pychan
19from scripts import drag_drop_data as data_drag
20from pychan.tools import callbackWithArguments as cbwa
21from scripts.items import item_image_dict
22
23class Inventory():
24    """Main inventory class"""
25    def __init__(self, engine, items, readyCallback):
26        """Initialise the instance.
27           @type engine: fife.Engine
28           @param engine: An instance of the fife engine
29           @type items: dict
30           @param items: A dictionary for every slot that goes '{slot:item, slot:item}'
31                         if a slot is not included in the dict, it is assumed to be empty
32           @type readyCallback: function
33           @param readyCallback: The function that will make the
34                                 ready slots on the HUD reflect those
35                                 within the inventory
36           @return: None"""
37        pychan.init(engine, debug = True)
38        self.engine = engine
39        self.readyCallback = readyCallback
40        self.original_cursor_id = self.engine.getCursor().getId()
41        # TODO: remove hard-coded string?
42        self.inventory = pychan.loadXML("gui/inventory.xml")
43        self.events_to_map = {}
44        # the images that should be used for the buttons when they are "empty"
45        self.empty_images = {'A1':'gui/inv_images/inv_backpack.png',
46                             'A2':'gui/inv_images/inv_backpack.png',
47                             'A3':'gui/inv_images/inv_backpack.png',
48                             'A4':'gui/inv_images/inv_backpack.png',
49                             'A5':'gui/inv_images/inv_backpack.png',
50                             'B1':'gui/inv_images/inv_backpack.png',
51                             'B2':'gui/inv_images/inv_backpack.png',
52                             'B3':'gui/inv_images/inv_backpack.png',
53                             'B4':'gui/inv_images/inv_backpack.png',
54                             'B5':'gui/inv_images/inv_backpack.png',
55                             'C1':'gui/inv_images/inv_backpack.png',
56                             'C2':'gui/inv_images/inv_backpack.png',
57                             'C3':'gui/inv_images/inv_backpack.png',
58                             'C4':'gui/inv_images/inv_backpack.png',
59                             'C5':'gui/inv_images/inv_backpack.png',
60                             'D1':'gui/inv_images/inv_backpack.png',
61                             'D2':'gui/inv_images/inv_backpack.png',
62                             'D3':'gui/inv_images/inv_backpack.png',
63                             'D4':'gui/inv_images/inv_backpack.png',
64                             'D5':'gui/inv_images/inv_backpack.png',
65                             'Head':'gui/inv_images/inv_head.png',
66                             'LeftHeld':'gui/inv_images/inv_litem.png',
67                             'RightHeld':'gui/inv_images/inv_ritem.png',
68                             'LeftHand':'gui/inv_images/inv_lhand.png',
69                             'RightHand':'gui/inv_images/inv_rhand.png',
70                             'Body':'gui/inv_images/inv_torso.png',
71                             'Belt':'gui/inv_images/inv_belt.png',
72                             'Ready1':'gui/inv_images/inv_belt_pouches.png',
73                             'Ready2':'gui/inv_images/inv_belt_pouches.png',
74                             'Ready3':'gui/inv_images/inv_belt_pouches.png',
75                             'Ready4':'gui/inv_images/inv_belt_pouches.png',
76                             'LeftFoot':'gui/inv_images/inv_lfoot.png',
77                             'RightFoot':'gui/inv_images/inv_rfoot.png'}
78        # every button on the inventory and its category
79        self.buttons = {'A1':'main_inv', 'A2':'main_inv', 'A3':'main_inv',
80                        'A4':'main_inv', 'A5':'main_inv', 'B1':'main_inv',
81                        'B2':'main_inv', 'B3':'main_inv', 'B4':'main_inv',
82                        'B5':'main_inv', 'C1':'main_inv', 'C2':'main_inv',
83                        'C3':'main_inv', 'C4':'main_inv', 'C5':'main_inv',
84                        'D1':'main_inv', 'D2':'main_inv', 'D3':'main_inv',
85                        'D4':'main_inv', 'D5':'main_inv',
86                        'LeftFoot':'foot', 'RightFoot':'foot',
87                        'LeftHand':'hand', 'RightHand':'hand',
88                        'Head':'head', 'Ready1':'ready', 
89                        'Ready2':'ready', 'Ready3':'ready', 
90                        'Ready4':'ready', 'Belt':'belt', 'LeftHeld':'held',
91                        'RightHeld':'held', 'Body':'body'}
92        # all possible categories
93        self.locations = ['ready', 'head', 'foot', 'hand',
94                          'belt', 'held', 'body']
95
96        for key in items:
97            widget = self.inventory.findChild(name=key)
98            item = items[key]
99            image = item_image_dict[item]
100
101            widget.item = item
102            widget.up_image = image
103            widget.down_image = image
104            widget.hover_image = image           
105
106        for button in self.buttons:
107            # make every button's callback be self.dragDrop
108            self.events_to_map[button] = cbwa(self.dragDrop, button)
109            ch = self.inventory.findChild(name = button)
110            # make every slot's item be none if it has not already been set
111            if button not in items:
112                ch.item = ""
113
114        self.inventory.mapEvents(self.events_to_map)   
115        self.resetMouseCursor()
116
117    def closeInventory(self):
118        """Close the inventory.
119           @return: None"""
120        self.inventory.hide()
121
122    def showInventory(self):
123        """Show the inventory.
124           @return: None"""
125        self.inventory.show()
126
127    def setMouseCursor(self, image, dummy_image, type = "native"): 
128        """Set the mouse cursor to an image.
129           @type image: string
130           @param image: The image you want to set the cursor to
131           @type dummy_image: string
132           @param dummy_image: ???
133           @type type: string
134           @param type: ???
135           @return: None"""
136        cursor = self.engine.getCursor()
137        cursor_type = fife.CURSOR_IMAGE
138        img_pool = self.engine.getImagePool()
139        if(type == "target"):
140            target_cursor_id = img_pool.addResourceFromFile(image) 
141            dummy_cursor_id = img_pool.addResourceFromFile(dummy_image)
142            cursor.set(cursor_type,target_dummy_cursor_id)
143            cursor.setDrag(cursor_type,target_cursor_id,-16,-16)
144        else:
145            cursor_type = fife.CURSOR_IMAGE
146            zero_cursor_id = img_pool.addResourceFromFile(image)
147            cursor.set(cursor_type,zero_cursor_id)
148            cursor.setDrag(cursor_type,zero_cursor_id)
149           
150    def resetMouseCursor(self):
151        """Reset cursor to default image.
152           @return: None"""
153        c = self.engine.getCursor()
154        img_pool = self.engine.getImagePool()
155        cursor_type = fife.CURSOR_NATIVE
156        # this is the path to the default image
157        cursor_id = self.original_cursor_id
158        c.setDrag(cursor_type, cursor_id)
159        c.set(cursor_type, cursor_id)
160       
161    def dragDrop(self, obj):
162        """Decide whether to drag or drop the image.
163           @type obj: string
164           @param obj: The name of the object within
165                       the dictionary 'self.buttons'
166           @return: None"""
167        if(data_drag.dragging == True):
168            self.dropObject(obj)
169        elif(data_drag.dragging == False):
170            self.dragObject(obj)
171               
172    def dragObject(self, obj):
173        """Drag the selected object.
174           @type obj: string
175           @param obj: The name of the object within
176                       the dictionary 'self.buttons'
177           @return: None"""
178        # get the widget from the inventory with the name obj
179        drag_widget = self.inventory.findChild(name = obj)
180        # only drag if the widget is not empty
181        if (drag_widget.up_image != self.empty_images[obj]):
182            # get it's type (e.g. main_inv)
183            data_drag.dragged_type = self.buttons[obj]
184            # get the item that the widget is 'storing'
185            data_drag.dragged_item = drag_widget.item
186            # get the up and down images of the widget
187            up_image = drag_widget._getUpImage()
188            down_image = drag_widget._getDownImage()
189            # set the mouse cursor to be the widget's image
190            self.setMouseCursor(up_image,down_image)
191            data_drag.dragged_image = up_image
192            data_drag.dragging = True
193            # after dragging the 'item', set the widgets' images
194            # so that it has it's default 'empty' images
195            drag_widget._setUpImage(self.empty_images[obj])
196            drag_widget._setDownImage(self.empty_images[obj])
197            drag_widget._setHoverImage(self.empty_images[obj])
198            # then set it's item to nothing
199            drag_widget.item = ""
200           
201    def dropObject(self, obj):
202        """Drops the object being dropped
203           @type obj: string
204           @param obj: The name of the object within
205                       the dictionary 'self.buttons'
206           @return: None"""
207        # find the type of the place that the object
208        # is being dropped onto
209        data_drag.dropped_type  =  self.buttons[obj]
210        # if the dragged obj or the place it is being dropped is
211        # in the main inventory, drop the object
212        if((data_drag.dragged_type == 'main_inv') or
213           (data_drag.dropped_type == 'main_inv')):
214            drag_widget = self.inventory.findChild(name = obj)
215            drag_widget._setUpImage(data_drag.dragged_image)
216            drag_widget._setHoverImage(data_drag.dragged_image)
217            drag_widget._setDownImage(data_drag.dragged_image)
218            drag_widget.item = data_drag.dragged_item
219            print 'Item: ' + drag_widget.item
220            data_drag.dragging = False
221            #reset the mouse cursor to the normal cursor
222            self.resetMouseCursor()
223            # if the object was dropped onto a ready slot, then
224            # update the hud
225            if (data_drag.dropped_type == 'ready'):
226                self.readyCallback()
227       
228        # if the dragged object's type is the same as the location to
229        # to drop it at's, and the dragged object's type is in
230        # self.locations, then drop the object
231        elif((data_drag.dragged_type == data_drag.dropped_type) and
232             (data_drag.dragged_type in self.locations)):
233            drag_widget = self.inventory.findChild(name = obj)
234            drag_widget._setUpImage(data_drag.dragged_image)
235            drag_widget._setHoverImage(data_drag.dragged_image)
236            drag_widget._setDownImage(data_drag.dragged_image)
237            drag_widget.item = data_drag.dragged_item
238            print 'Item: ' + drag_widget.item
239            data_drag.dragging = False
240            # reset the mouse cursor
241            self.resetMouseCursor()
242            # if the object was dropped onto a ready slot, then
243            # update the hud
244            if(data_drag.dropped_type == 'ready'):
245                self.readyCallback()
246        # otherwise, we assume that the player is trying to
247        # drop an object onto an incompatible slot
248        else:
249            # reset the mouse cursor
250            self.resetMouseCursor()
251            data_drag.dragging = False
252
253    def getItems(self):
254        """
255        Get the items in the inventory slots. If there is no item in the slot, it is skipped
256       
257        @rtype: dict
258        @return: The items in the inventory
259        """
260        items = {}
261
262        for button in self.buttons:
263            widget = self.inventory.findChild(name=button)
264            if (widget.item != ""):
265                items[button] = widget.item
266
267        return items
268               
269
Note: See TracBrowser for help on using the repository browser.