source: trunk/PARPG/scripts/inventory.py @ 181

Revision 161, 10.8 KB checked in by bretzel_parpg, 10 years ago (diff)

Added comments and filled in some documentation

  • 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 pychan.tools import callbackWithArguments as cbwa
20
21class Inventory():
22    """Main inventory class"""
23    def __init__(self, engine, readyCallback):
24        """Initialise the instance.
25           @type engine: fife.Engine
26           @param engine: An instance of the fife engine
27           @type readyCallback: function
28           @param readyCallback: The function that will make the
29                                 ready slots on the HUD reflect those
30                                 within the inventory
31           @return: None"""
32        pychan.init(engine, debug = True)
33        self.engine = engine
34        self.readyCallback = readyCallback
35        self.dragging = False
36        self.dragged_image = None
37        self.dragged_type = None
38        self.dragged_item = None
39        self.dropped_type = None
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        for button in self.buttons:
96            # make every button's callback be self.dragDrop
97            self.events_to_map[button] = cbwa(self.dragDrop, button)
98            ch = self.inventory.findChild(name = button)
99            # make every slot's item be empty
100            ch.item = ""
101        self.inventory.mapEvents(self.events_to_map)   
102        self.resetMouseCursor()
103
104    def closeInventory(self):
105        """Close the inventory.
106           @return: None"""
107        self.inventory.hide()
108
109    def showInventory(self):
110        """Show the inventory.
111           @return: None"""
112        self.inventory.show()
113
114    def setMouseCursor(self, image, dummy_image, type = "native"): 
115        """Set the mouse cursor to an image.
116           @type image: string
117           @param image: The image you want to set the cursor to
118           @type dummy_image: string
119           @param dummy_image: ???
120           @type type: string
121           @param type: ???
122           @return: None"""
123        cursor = self.engine.getCursor()
124        cursor_type = fife.CURSOR_IMAGE
125        img_pool = self.engine.getImagePool()
126        if(type == "target"):
127            target_cursor_id = img_pool.addResourceFromFile(image) 
128            dummy_cursor_id = img_pool.addResourceFromFile(dummy_image)
129            cursor.set(cursor_type,target_dummy_cursor_id)
130            cursor.setDrag(cursor_type,target_cursor_id,-16,-16)
131        else:
132            cursor_type = fife.CURSOR_IMAGE
133            zero_cursor_id = img_pool.addResourceFromFile(image)
134            cursor.set(cursor_type,zero_cursor_id)
135            cursor.setDrag(cursor_type,zero_cursor_id)
136           
137    def resetMouseCursor(self):
138        """Reset cursor to default image.
139           @return: None"""
140        c = self.engine.getCursor()
141        img_pool = self.engine.getImagePool()
142        cursor_type = fife.CURSOR_NATIVE
143        # this is the path to the default image
144        cursor_id = self.original_cursor_id
145        c.setDrag(cursor_type, cursor_id)
146        c.set(cursor_type, cursor_id)
147       
148    def dragDrop(self, obj):
149        """Decide whether to drag or drop the image.
150           @type obj: string
151           @param obj: The name of the object within
152                       the dictionary 'self.buttons'
153           @return: None"""
154        if(self.dragging == True):
155            self.dropObject(obj)
156        elif(self.dragging == False):
157            self.dragObject(obj)
158               
159    def dragObject(self, obj):
160        """Drag the selected object.
161           @type obj: string
162           @param obj: The name of the object within
163                       the dictionary 'self.buttons'
164           @return: None"""
165        # get the widget from the inventory with the name obj
166        drag_widget = self.inventory.findChild(name = obj)
167        # get it's type (e.g. main_inv)
168        self.dragged_type = self.buttons[obj]
169        # get the item that the widget is 'storing'
170        self.dragged_item = drag_widget.item
171        # get the up and down images of the widget
172        up_image = drag_widget._getUpImage()
173        down_image = drag_widget._getDownImage()
174        # set the mouse cursor to be the widget's image
175        self.setMouseCursor(up_image,down_image)
176        self.dragged_image = up_image
177        self.dragging = True
178        # after dragging the 'item', set the widgets' images
179        # so that it has it's default 'empty' images
180        drag_widget._setUpImage(self.empty_images[obj])
181        drag_widget._setDownImage(self.empty_images[obj])
182        drag_widget._setHoverImage(self.empty_images[obj])
183       
184    def dropObject(self, obj):
185        """Drops the object being dropped
186           @type obj: string
187           @param obj: The name of the object within
188                       the dictionary 'self.buttons'
189           @return: None"""
190        # find the type of the place that the object
191        # is being dropped onto
192        self.dropped_type  =  self.buttons[obj]
193        # if the dragged obj or the place it is being dropped is
194        # in the main inventory, drop the object
195        if((self.dragged_type == 'main_inv') or
196           (self.dropped_type == 'main_inv')):
197            drag_widget = self.inventory.findChild(name = obj)
198            drag_widget._setUpImage(self.dragged_image)
199            drag_widget._setHoverImage(self.dragged_image)
200            drag_widget._setDownImage(self.dragged_image)
201            drag_widget.item = self.dragged_item
202            self.dragging = False
203            #reset the mouse cursor to the normal cursor
204            self.resetMouseCursor()
205            # if the object was dropped onto a ready slot, then
206            # update the hud
207            if (self.dropped_type == 'ready'):
208                self.readyCallback()
209       
210        # if the dragged object's type is the same as the location to
211        # to drop it at's, and the dragged object's type is in
212        # self.locations, then drop the object
213        elif((self.dragged_type == self.dropped_type) and
214             (self.dragged_type in self.locations)):
215            drag_widget = self.inventory.findChild(name = obj)
216            drag_widget._setUpImage(self.dragged_image)
217            drag_widget._setHoverImage(self.dragged_image)
218            drag_widget._setDownImage(self.dragged_image)
219            drag_widget.item = self.dragged_item
220            self.dragging = False
221            # reset the mouse cursor
222            self.resetMouseCursor()
223            # if the object was dropped onto a ready slot, then
224            # update the hud
225            if(self.dropped_type == 'ready'):
226                self.readyCallback()
227        # otherwise, we assume that the player is trying to
228        # drop an object onto an incompatible slot
229        else:
230            # reset the mouse cursor
231            self.resetMouseCursor()
232            self.dragging = False
233
Note: See TracBrowser for help on using the repository browser.