source: trunk/game/scripts/popups.py @ 270

Revision 267, 10.9 KB checked in by Kaydeth_parpg, 10 years ago (diff)

Ticket #91: Patch by phoku. The drag and drop logic in ContainerGUI class and Inventory class needed to be corrected after some FIFE changes made in FIFE revision 3003. Also gui_container.py was removed as the the code was previously copied into popups.py and gui_container.py is no longer used.

  • 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 fife
19import pychan
20from scripts import drag_drop_data as data_drag
21from scripts.items import item_image_dict
22from pychan.tools import callbackWithArguments as cbwa
23
24class ContainerGUI():
25    def __init__(self, engine, title, data):
26        """
27        A class to create a window showing the contents of a container.
28       
29        @type engine: fife.Engine
30        @param engine: an instance of the fife engine
31        @type title: string
32        @param title: The title of the window
33        @type data: list or string
34        @param data: A list of 9 items to use for the slots 1 - 9
35                     OR
36                     one item to be used for all the slots
37        @return: None
38        """
39        self.engine = engine
40        pychan.init(self.engine, debug=True)
41       
42        self.container_gui = pychan.loadXML("gui/container_base.xml")
43        self.container_gui.findChild(name="topWindow").title = title
44   
45        data_drag.dragging = False
46        self.original_cursor_id = self.engine.getCursor().getId()
47       
48       
49        # Prepare slots 1 through 9
50        dataIsList = False
51        emptyImage = "gui/inv_images/inv_backpack.png"
52        slotCount = 9
53        self.empty_images = dict()
54        # Did this step because I'm unsure which is more costly , to check the
55        # type of object or the value of boolean object. Change as you see fit.
56        if type(data) == list:
57            dataIsList = True
58        for counter in range(1, slotCount):
59            slotName = "Slot%i" % counter
60            selectedData = None
61           
62            if dataIsList:
63                selectedData = data[counter-1]
64            else:
65                selectedData = data
66           
67            self.setContainerImage(slotName, item_image_dict[selectedData])
68            self.container_gui.findChild(name=slotName).item = selectedData
69            self.empty_images[slotName] = emptyImage
70       
71       
72        self.events_to_map = {}
73        self.buttons = {}
74
75        for key in self.empty_images:
76            self.buttons[key] = "main_inv"
77
78        for button in self.buttons:
79            # make every button's callback be self.dragDrop
80            self.events_to_map[button] = cbwa(self.dragDrop, button)
81
82        self.locations = ["main_inv"]
83
84        self.container_gui.mapEvents(self.events_to_map)   
85        self.resetMouseCursor()
86           
87
88    def setContainerImage(self, widget_name, image):
89        """
90        Sets the up, down, and hover images of an image button to image
91
92        @type widget_name: string
93        @param widget_name: the name of the widget
94        @type image: string
95        @param image: the path to the image
96        @return None
97        """
98        widget = self.container_gui.findChild(name=widget_name)
99        widget.up_image = image
100        widget.down_image = image
101        widget.hover_image = image
102        print 'Set all images on %s using %s' % (widget, image)
103       
104    def showContainer(self):
105        """
106        Show the container
107        @return: None
108        """
109        self.container_gui.show()
110
111    def hideContainer(self):
112        """
113        Hide the container
114        @return: None
115        """
116        self.container_gui.hide()
117       
118    def setMouseCursor(self, image, dummy_image, type = "native"): 
119        """Set the mouse cursor to an image.
120           @type image: string
121           @param image: The image you want to set the cursor to
122           @type dummy_image: string
123           @param dummy_image: ???
124           @type type: string
125           @param type: ???
126           @return: None"""
127        cursor = self.engine.getCursor()
128        cursor_type = fife.CURSOR_IMAGE
129        img_pool = self.engine.getImagePool()
130        if(type == "target"):
131            target_cursor_id = img_pool.addResourceFromFile(image) 
132            dummy_cursor_id = img_pool.addResourceFromFile(dummy_image)
133            cursor.set(cursor_type,target_dummy_cursor_id)
134            cursor.setDrag(cursor_type,target_cursor_id,-16,-16)
135        else:
136            cursor_type = fife.CURSOR_IMAGE
137            zero_cursor_id = img_pool.addResourceFromFile(image)
138            cursor.set(cursor_type,zero_cursor_id)
139            cursor.setDrag(cursor_type,zero_cursor_id)
140           
141    def resetMouseCursor(self):
142        """Reset cursor to default image.
143           @return: None"""
144        c = self.engine.getCursor()
145        img_pool = self.engine.getImagePool()
146        cursor_type = fife.CURSOR_NATIVE
147        # this is the path to the default image
148        cursor_id = self.original_cursor_id
149        c.setDrag(cursor_type, cursor_id)
150        c.set(cursor_type, cursor_id)
151       
152    def dragDrop(self, obj):
153        """Decide whether to drag or drop the image.
154           @type obj: string
155           @param obj: The name of the object within
156                       the dictionary 'self.buttons'
157           @return: None"""
158        if(data_drag.dragging == True):
159            self.dropObject(obj)
160        elif(data_drag.dragging == False):
161            self.dragObject(obj)
162               
163    def dragObject(self, obj):
164        """Drag the selected object.
165           @type obj: string
166           @param obj: The name of the object within
167                       the dictionary 'self.buttons'
168           @return: None"""
169        # get the widget from the container_gui with the name obj
170        drag_widget = self.container_gui.findChild(name = obj)
171        # only drag if the widget is not empty
172        if (drag_widget.up_image != self.empty_images[obj]):
173            # get it's type (e.g. main_inv)
174            data_drag.dragged_type = self.buttons[obj]
175            # get the item that the widget is 'storing'
176            data_drag.dragged_item = drag_widget.item
177            # get the up and down images of the widget
178            up_image = drag_widget.up_image
179            down_image = drag_widget.down_image
180            # set the mouse cursor to be the widget's image
181            self.setMouseCursor(up_image.source, down_image.source)
182            data_drag.dragged_image = up_image.source
183            data_drag.dragging = True
184            # after dragging the 'item', set the widgets' images
185            # so that it has it's default 'empty' images
186            drag_widget.up_image = self.empty_images[obj]
187            drag_widget.down_image = self.empty_images[obj]
188            drag_widget.hover_image = self.empty_images[obj]
189            # then set it's item to nothing
190            drag_widget.item = ""
191           
192    def dropObject(self, obj):
193        """Drops the object being dropped
194           @type obj: string
195           @param obj: The name of the object within
196                       the dictionary 'self.buttons'
197           @return: None"""
198        # find the type of the place that the object
199        # is being dropped onto
200        data_drag.dropped_type  =  self.buttons[obj]
201        # if the dragged obj or the place it is being dropped is
202        # in the main container_gui, drop the object
203        if((data_drag.dragged_type == 'main_inv') or
204           (data_drag.dropped_type == 'main_inv')):
205            drag_widget = self.container_gui.findChild(name = obj)
206            drag_widget.up_image = data_drag.dragged_image
207            drag_widget.down_image = data_drag.dragged_image
208            drag_widget.hover_image = data_drag.dragged_image
209            drag_widget.item = data_drag.dragged_item
210            print 'Item: ' + drag_widget.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.up_image = data_drag.dragged_image
226            drag_widget.down_image = data_drag.dragged_image
227            drag_widget.hover_image = data_drag.dragged_image
228            drag_widget.item = data_drag.dragged_item
229            print 'Item: ' + drag_widget.item
230            data_drag.dragging = False
231            # reset the mouse cursor
232            self.resetMouseCursor()
233            # if the object was dropped onto a ready slot, then
234            # update the hud
235            if(data_drag.dropped_type == 'ready'):
236                self.readyCallback()
237        # otherwise, we assume that the player is trying to
238        # drop an object onto an incompatible slot
239        else:
240            # reset the mouse cursor
241            self.resetMouseCursor()
242            data_drag.dragging = False
243
244class ExaminePopup():
245    """
246    Create a popup for when you click examine on an object
247    """
248    def __init__(self, engine, object_title, desc):
249        """
250        Initialize the popup
251       
252        @type engine: fife.Engine
253        @param engine: an instance of the fife engine
254        @type object_title: string
255        @param object_title: The title for the window, probably should just be the name of the object
256        @type desc: string
257        @param desc: The description of the object
258        @return: None
259        """
260        self.engine = engine
261        pychan.init(self.engine, debug=True)
262
263        self.examineWindow = pychan.widgets.Window(title=unicode(object_title),
264                                                   position_technique="center:center",
265                                                   min_size=(175,175))
266
267        self.scroll = pychan.widgets.ScrollArea(name='scroll', size=(150,150))
268        self.description = pychan.widgets.Label(name='descText', text=unicode(desc), wrap_text=True)
269        self.description.max_width = 170
270        self.scroll.addChild(self.description)
271        self.examineWindow.addChild(self.scroll)
272       
273        self.close_button = pychan.widgets.Button(name='closeButton', text=unicode('Close'))
274        self.examineWindow.addChild(self.close_button)
275
276        self.examineWindow.mapEvents({'closeButton':self.examineWindow.hide})
277
278    def closePopUp(self):
279        self.examineWindow.hide()
280   
281    def showPopUp(self):
282        self.examineWindow.show()
Note: See TracBrowser for help on using the repository browser.