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

Revision 313, 11.2 KB checked in by Kaydeth_parpg, 10 years ago (diff)

Ticket #2: Patch by Kaydeth. Updated the rest of the classes in the scripts directory to correct some obvious discrepancies with our coding standard. Still need to go through the common and objects sub directories. comment[s:trac, t:2]

  • 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        data_is_list = False
51        empty_image = "gui/inv_images/inv_backpack.png"
52        slot_count = 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            data_is_list = True
58        for counter in range(1, slot_count):
59            slot_name = "Slot%i" % counter
60            selected_data = None
61           
62            if data_is_list:
63                selected_data = data[counter-1]
64            else:
65                selected_data = data
66           
67            self.setContainerImage(slot_name, item_image_dict[selected_data])
68            self.container_gui.findChild(name=slot_name).item = selected_data
69            self.empty_images[slot_name] = empty_image
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        if self.container_gui.isVisible():
117            self.container_gui.hide()
118       
119    def setMouseCursor(self, image, dummy_image, type="native"): 
120        """Set the mouse cursor to an image.
121           @type image: string
122           @param image: The image you want to set the cursor to
123           @type dummy_image: string
124           @param dummy_image: ???
125           @type type: string
126           @param type: ???
127           @return: None"""
128        cursor = self.engine.getCursor()
129        cursor_type = fife.CURSOR_IMAGE
130        img_pool = self.engine.getImagePool()
131        if(type == "target"):
132            target_cursor_id = img_pool.addResourceFromFile(image) 
133            dummy_cursor_id = img_pool.addResourceFromFile(dummy_image)
134            cursor.set(cursor_type,target_dummy_cursor_id)
135            cursor.setDrag(cursor_type,target_cursor_id,-16,-16)
136        else:
137            cursor_type = fife.CURSOR_IMAGE
138            zero_cursor_id = img_pool.addResourceFromFile(image)
139            cursor.set(cursor_type,zero_cursor_id)
140            cursor.setDrag(cursor_type,zero_cursor_id)
141           
142    def resetMouseCursor(self):
143        """Reset cursor to default image.
144           @return: None"""
145        c = self.engine.getCursor()
146        img_pool = self.engine.getImagePool()
147        cursor_type = fife.CURSOR_NATIVE
148        # this is the path to the default image
149        cursor_id = self.original_cursor_id
150        c.setDrag(cursor_type, cursor_id)
151        c.set(cursor_type, cursor_id)
152       
153    def dragDrop(self, obj):
154        """Decide whether to drag or drop the image.
155           @type obj: string
156           @param obj: The name of the object within
157                       the dictionary 'self.buttons'
158           @return: None"""
159        if(data_drag.dragging == True):
160            self.dropObject(obj)
161        elif(data_drag.dragging == False):
162            self.dragObject(obj)
163               
164    def dragObject(self, obj):
165        """Drag the selected object.
166           @type obj: string
167           @param obj: The name of the object within
168                       the dictionary 'self.buttons'
169           @return: None"""
170        # get the widget from the container_gui with the name obj
171        drag_widget = self.container_gui.findChild(name = obj)
172        # only drag if the widget is not empty
173        if (drag_widget.up_image != self.empty_images[obj]):
174            # get it's type (e.g. main_inv)
175            data_drag.dragged_type = self.buttons[obj]
176            # get the item that the widget is 'storing'
177            data_drag.dragged_item = drag_widget.item
178            # get the up and down images of the widget
179            up_image = drag_widget.up_image
180            down_image = drag_widget.down_image
181            # set the mouse cursor to be the widget's image
182            self.setMouseCursor(up_image.source, down_image.source)
183            data_drag.dragged_image = up_image.source
184            data_drag.dragging = True
185            # after dragging the 'item', set the widgets' images
186            # so that it has it's default 'empty' images
187            drag_widget.up_image = self.empty_images[obj]
188            drag_widget.down_image = self.empty_images[obj]
189            drag_widget.hover_image = self.empty_images[obj]
190            # then set it's item to nothing
191            drag_widget.item = ""
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.up_image = data_drag.dragged_image
208            drag_widget.down_image = data_drag.dragged_image
209            drag_widget.hover_image = data_drag.dragged_image
210            drag_widget.item = data_drag.dragged_item
211            print 'Item: ' + drag_widget.item
212            data_drag.dragging = False
213            #reset the mouse cursor to the normal cursor
214            self.resetMouseCursor()
215            # if the object was dropped onto a ready slot, then
216            # update the hud
217            if (data_drag.dropped_type == 'ready'):
218                self.readyCallback()
219       
220        # if the dragged object's type is the same as the location to
221        # to drop it at's, and the dragged object's type is in
222        # self.locations, then drop the object
223        elif((data_drag.dragged_type == data_drag.dropped_type) and
224             (data_drag.dragged_type in self.locations)):
225            drag_widget = self.container_gui.findChild(name = obj)
226            drag_widget.up_image = data_drag.dragged_image
227            drag_widget.down_image = data_drag.dragged_image
228            drag_widget.hover_image = data_drag.dragged_image
229            drag_widget.item = data_drag.dragged_item
230            print 'Item: ' + drag_widget.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
244
245class ExaminePopup():
246    """
247    Create a popup for when you click examine on an object
248    """
249    def __init__(self, engine, object_title, desc):
250        """
251        Initialize the popup
252       
253        @type engine: fife.Engine
254        @param engine: an instance of the fife engine
255        @type object_title: string
256        @param object_title: The title for the window, probably should just be
257            the name of the object
258        @type desc: string
259        @param desc: The description of the object
260        @return: None
261        """
262        self.engine = engine
263        pychan.init(self.engine, debug=True)
264
265        self.examine_window = pychan.widgets.\
266                                Window(title=unicode(object_title),
267                                       position_technique="center:center",
268                                       min_size=(175,175))
269
270        self.scroll = pychan.widgets.ScrollArea(name='scroll', size=(150,150))
271        self.description = pychan.widgets.Label(name='descText',
272                                                text=unicode(desc),
273                                                wrap_text=True)
274        self.description.max_width = 170
275        self.scroll.addChild(self.description)
276        self.examine_window.addChild(self.scroll)
277       
278        self.close_button = pychan.widgets.Button(name='closeButton',
279                                                  text=unicode('Close'))
280        self.examine_window.addChild(self.close_button)
281
282        self.examine_window.mapEvents({'closeButton':self.examine_window.hide})
283
284    def closePopUp(self):
285        if self.examine_window.isVisible():
286            self.examine_window.hide()
287   
288    def showPopUp(self):
289        self.examine_window.show()
Note: See TracBrowser for help on using the repository browser.