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

Revision 181, 9.9 KB checked in by bretzel_parpg, 10 years ago (diff)

When you press the open button on a crate, it now will move you to the crate and display a dialog with
9 slots to show the contents of the crate. The dialog will interface with the inventory.

RevLine 
[168]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 fife
19import pychan
[181]20from scripts import drag_drop_data as data_drag
[168]21from pychan.tools import callbackWithArguments as cbwa
22
23class ContainerGUI():
24    def __init__(self, engine, title, data):
25        """
26        A class to create a window showing the contents of a container.
27       
28        @type engine: fife.Engine
29        @param engine: an instance of the fife engine
30        @type title: string
31        @param title: The title of the window
32        @type data: list or string
33        @param data: A list of 9 images to use for the slots 1 - 9 OR a string for one image that will be used on all the slots
34        @return: None
35        """
[181]36        self.engine = engine
37        pychan.init(self.engine, debug=True)
[168]38       
39        self.container_gui = pychan.loadXML("gui/container_base.xml")
40        self.container_gui.findChild(name="topWindow").title = title
[181]41   
42        data_drag.dragging = False
43        self.original_cursor_id = self.engine.getCursor().getId()
[168]44
[181]45        self.empty_images = {"Slot1":"gui/inv_images/inv_backpack.png",
46                             "Slot2":"gui/inv_images/inv_backpack.png",
47                             "Slot3":"gui/inv_images/inv_backpack.png",
48                             "Slot4":"gui/inv_images/inv_backpack.png",
49                             "Slot5":"gui/inv_images/inv_backpack.png",
50                             "Slot6":"gui/inv_images/inv_backpack.png",
51                             "Slot7":"gui/inv_images/inv_backpack.png",
52                             "Slot8":"gui/inv_images/inv_backpack.png",
53                             "Slot9":"gui/inv_images/inv_backpack.png"}
54        self.buttons = {}
55        for key in self.empty_images:
56            self.buttons[key] = "main_inv"
57
58        self.locations = ["main_inv"]
59
60        self.events_to_map = {}
61        for button in self.buttons:
62            # make every button's callback be self.dragDrop
63            self.events_to_map[button] = cbwa(self.dragDrop, button)
64            ch = self.container_gui.findChild(name = button)
65            # make every slot's item be empty
66            ch.item = ""
67        self.container_gui.mapEvents(self.events_to_map)   
68        self.resetMouseCursor()
[168]69   
70        if type(data) == list:
71            self.setContainerImage("Slot1", data[1])
72            self.setContainerImage("Slot2", data[2])
73            self.setContainerImage("Slot3", data[3])
74            self.setContainerImage("Slot4", data[4])
75            self.setContainerImage("Slot5", data[5])
76            self.setContainerImage("Slot6", data[6])
77            self.setContainerImage("Slot7", data[7])
78            self.setContainerImage("Slot8", data[8])
79            self.setContainerImage("Slot9", data[9])
80
81        else:
82            self.setContainerImage("Slot1", data)
83            self.setContainerImage("Slot2", data)
84            self.setContainerImage("Slot3", data)
85            self.setContainerImage("Slot4", data)
86            self.setContainerImage("Slot5", data)
87            self.setContainerImage("Slot6", data)
88            self.setContainerImage("Slot7", data)
89            self.setContainerImage("Slot8", data)
90            self.setContainerImage("Slot9", data)
[181]91   
[168]92
93    def setContainerImage(self, widget_name, image):
94        """
95        Sets the up, down, and hover images of an image button to image
96
97        @type widget_name: string
98        @param widget_name: the name of the widget
99        @type image: string
100        @param image: the path to the image
101        @return None
102        """
103        widget = self.container_gui.findChild(name=widget_name)
104        widget.up_image = image
105        widget.down_image = image
106        widget.hover_image = image
107        print 'Set all images on %s using %s' % (widget, image)
108       
109    def showContainer(self):
[181]110        """
111        Show the container
112        @return: None
113        """
[168]114        self.container_gui.show()
115
116    def hideContainer(self):
[181]117        """
118        Hide the container
119        @return: None
120        """
[168]121        self.container_gui.hide()
122       
[181]123    def setMouseCursor(self, image, dummy_image, type = "native"): 
124        """Set the mouse cursor to an image.
125           @type image: string
126           @param image: The image you want to set the cursor to
127           @type dummy_image: string
128           @param dummy_image: ???
129           @type type: string
130           @param type: ???
131           @return: None"""
132        cursor = self.engine.getCursor()
133        cursor_type = fife.CURSOR_IMAGE
134        img_pool = self.engine.getImagePool()
135        if(type == "target"):
136            target_cursor_id = img_pool.addResourceFromFile(image) 
137            dummy_cursor_id = img_pool.addResourceFromFile(dummy_image)
138            cursor.set(cursor_type,target_dummy_cursor_id)
139            cursor.setDrag(cursor_type,target_cursor_id,-16,-16)
140        else:
141            cursor_type = fife.CURSOR_IMAGE
142            zero_cursor_id = img_pool.addResourceFromFile(image)
143            cursor.set(cursor_type,zero_cursor_id)
144            cursor.setDrag(cursor_type,zero_cursor_id)
145           
146    def resetMouseCursor(self):
147        """Reset cursor to default image.
148           @return: None"""
149        c = self.engine.getCursor()
150        img_pool = self.engine.getImagePool()
151        cursor_type = fife.CURSOR_NATIVE
152        # this is the path to the default image
153        cursor_id = self.original_cursor_id
154        c.setDrag(cursor_type, cursor_id)
155        c.set(cursor_type, cursor_id)
156       
157    def dragDrop(self, obj):
158        """Decide whether to drag or drop the image.
159           @type obj: string
160           @param obj: The name of the object within
161                       the dictionary 'self.buttons'
162           @return: None"""
163        if(data_drag.dragging == True):
164            self.dropObject(obj)
165        elif(data_drag.dragging == False):
166            self.dragObject(obj)
167               
168    def dragObject(self, obj):
169        """Drag the selected object.
170           @type obj: string
171           @param obj: The name of the object within
172                       the dictionary 'self.buttons'
173           @return: None"""
174        # get the widget from the container_gui with the name obj
175        drag_widget = self.container_gui.findChild(name = obj)
176        # get it's type (e.g. main_inv)
177        data_drag.dragged_type = self.buttons[obj]
178        # get the item that the widget is 'storing'
179        data_drag.dragged_item = drag_widget.item
180        # get the up and down images of the widget
181        up_image = drag_widget._getUpImage()
182        down_image = drag_widget._getDownImage()
183        # set the mouse cursor to be the widget's image
184        self.setMouseCursor(up_image,down_image)
185        data_drag.dragged_image = up_image
186        data_drag.dragging = True
187        # after dragging the 'item', set the widgets' images
188        # so that it has it's default 'empty' images
189        drag_widget._setUpImage(self.empty_images[obj])
190        drag_widget._setDownImage(self.empty_images[obj])
191        drag_widget._setHoverImage(self.empty_images[obj])
192       
193    def dropObject(self, obj):
194        """Drops the object being dropped
195           @type obj: string
196           @param obj: The name of the object within
197                       the dictionary 'self.buttons'
198           @return: None"""
199        # find the type of the place that the object
200        # is being dropped onto
201        data_drag.dropped_type  =  self.buttons[obj]
202        # if the dragged obj or the place it is being dropped is
203        # in the main container_gui, drop the object
204        if((data_drag.dragged_type == 'main_inv') or
205           (data_drag.dropped_type == 'main_inv')):
206            drag_widget = self.container_gui.findChild(name = obj)
207            drag_widget._setUpImage(data_drag.dragged_image)
208            drag_widget._setHoverImage(data_drag.dragged_image)
209            drag_widget._setDownImage(data_drag.dragged_image)
210            drag_widget.item = data_drag.dragged_item
211            data_drag.dragging = False
212            #reset the mouse cursor to the normal cursor
213            self.resetMouseCursor()
214            # if the object was dropped onto a ready slot, then
215            # update the hud
216            if (data_drag.dropped_type == 'ready'):
217                self.readyCallback()
218       
219        # if the dragged object's type is the same as the location to
220        # to drop it at's, and the dragged object's type is in
221        # self.locations, then drop the object
222        elif((data_drag.dragged_type == data_drag.dropped_type) and
223             (data_drag.dragged_type in self.locations)):
224            drag_widget = self.container_gui.findChild(name = obj)
225            drag_widget._setUpImage(data_drag.dragged_image)
226            drag_widget._setHoverImage(data_drag.dragged_image)
227            drag_widget._setDownImage(data_drag.dragged_image)
228            drag_widget.item = data_drag.dragged_item
229            data_drag.dragging = False
230            # reset the mouse cursor
231            self.resetMouseCursor()
232            # if the object was dropped onto a ready slot, then
233            # update the hud
234            if(data_drag.dropped_type == 'ready'):
235                self.readyCallback()
236        # otherwise, we assume that the player is trying to
237        # drop an object onto an incompatible slot
238        else:
239            # reset the mouse cursor
240            self.resetMouseCursor()
241            data_drag.dragging = False
Note: See TracBrowser for help on using the repository browser.