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

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

Restructured the dragging and dropping data a bit

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