source: trunk/game/scripts/gui/popups.py @ 444

Revision 424, 8.5 KB checked in by b0rland_parpg, 10 years ago (diff)

Ticket #107: patch by b0rland

  • Reworked inventory gui to use new inventory model
  • Made various additions and fixes to inventory model and base classes
  • Improved drag-and-drop support
  • Possibly broke dialog gui :(
  • 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.gui 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, container):
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        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        self.container = container
47       
48        self.resetMouseCursor()
49           
50    def updateImage(self, button):
51        if (button.item == None):
52            image = self.empty_images[button.name]
53        else:
54            image = button.item.getInventoryThumbnail()
55        button.up_image = image
56        button.down_image = image
57        button.hover_image = image
58     
59    def showContainer(self):
60        """
61        Show the container
62        @return: None
63        """
64        # Prepare slots 1 through 9
65        empty_image = "gui/inv_images/inv_backpack.png"
66        slot_count = 9
67        self.empty_images = dict()
68        self.events_to_map = {}
69        for counter in range(1, slot_count):
70            slot_name = "Slot%i" % counter
71            self.empty_images[slot_name] = empty_image
72            widget = self.container_gui.findChild(name=slot_name)
73            widget.item = self.container.items.get(counter-1)
74            widget.index = counter-1
75            self.updateImage(widget)
76            self.events_to_map[slot_name] = cbwa(self.dragDrop, slot_name)
77
78        self.container_gui.mapEvents(self.events_to_map)
79
80        self.container_gui.show()
81
82    def hideContainer(self):
83        """
84        Hide the container
85        @return: None
86        """
87        if self.container_gui.isVisible():
88            self.container_gui.hide()
89       
90    def setMouseCursor(self, image, dummy_image, mc_type="native"): 
91        """Set the mouse cursor to an image.
92           @type image: string
93           @param image: The image you want to set the cursor to
94           @type dummy_image: string
95           @param dummy_image: ???
96           @type type: string
97           @param type: ???
98           @return: None"""
99        cursor = self.engine.getCursor()
100        cursor_type = fife.CURSOR_IMAGE
101        img_pool = self.engine.getImagePool()
102        if(mc_type == "target"):
103            target_cursor_id = img_pool.addResourceFromFile(image) 
104            dummy_cursor_id = img_pool.addResourceFromFile(dummy_image)
105            cursor.set(cursor_type, dummy_cursor_id)
106            cursor.setDrag(cursor_type, target_cursor_id,-16,-16)
107        else:
108            cursor_type = fife.CURSOR_IMAGE
109            zero_cursor_id = img_pool.addResourceFromFile(image)
110            cursor.set(cursor_type, zero_cursor_id)
111            cursor.setDrag(cursor_type, zero_cursor_id)
112           
113    def resetMouseCursor(self):
114        """Reset cursor to default image.
115           @return: None"""
116        c = self.engine.getCursor()
117        img_pool = self.engine.getImagePool()
118        cursor_type = fife.CURSOR_NATIVE
119        # this is the path to the default image
120        cursor_id = self.original_cursor_id
121        c.setDrag(cursor_type, cursor_id)
122        c.set(cursor_type, cursor_id)
123       
124    def dragDrop(self, obj):
125        """Decide whether to drag or drop the image.
126           @type obj: string
127           @param obj: The name of the object within
128                       the dictionary 'self.buttons'
129           @return: None"""
130        if(data_drag.dragging == True):
131            self.dropObject(obj)
132        elif(data_drag.dragging == False):
133            self.dragObject(obj)
134               
135    def dragObject(self, obj):
136        """Drag the selected object.
137           @type obj: string
138           @param obj: The name of the object within
139                       the dictionary 'self.buttons'
140           @return: None"""
141        # get the widget from the container_gui with the name obj
142        drag_widget = self.container_gui.findChild(name = obj)
143        drag_item = drag_widget.item
144        # only drag if the widget is not empty
145        if (drag_item != None):
146            # get the item that the widget is 'storing'
147            data_drag.dragged_item = drag_widget.item
148            # get the up and down images of the widget
149            up_image = drag_widget.up_image
150            down_image = drag_widget.down_image
151            # set the mouse cursor to be the widget's image
152            self.setMouseCursor(up_image.source, down_image.source)
153            data_drag.dragged_image = up_image.source
154            data_drag.dragging = True
155            data_drag.dragged_widget = drag_widget
156            data_drag.dragged_container=self.container
157
158            # after dragging the 'item', set the widgets' images
159            # so that it has it's default 'empty' images
160            drag_widget.item = None
161            self.updateImage(drag_widget)
162           
163    def dropObject(self, obj):
164        """Drops the object being dropped
165           @type obj: string
166           @param obj: The name of the object within
167                       the dictionary 'self.buttons'
168           @return: None"""
169        drop_widget = self.container_gui.findChild(name = obj)
170        if drop_widget != data_drag.dragged_widget:
171            data_drag.dragged_container.takeItem(data_drag.dragged_item)
172            self.container.placeItem(data_drag.dragged_item, drop_widget.index)
173        drop_widget.item = data_drag.dragged_item
174        self.updateImage(drop_widget)
175        data_drag.dragging = False
176        #reset the mouse cursor to the normal cursor
177        self.resetMouseCursor()
178
179
180class ExaminePopup():
181    """
182    Create a popup for when you click examine on an object
183    """
184    def __init__(self, engine, object_title, desc):
185        """
186        Initialize the popup
187       
188        @type engine: fife.Engine
189        @param engine: an instance of the fife engine
190        @type object_title: string
191        @param object_title: The title for the window, probably should just be
192            the name of the object
193        @type desc: string
194        @param desc: The description of the object
195        @return: None
196        """
197        self.engine = engine
198        pychan.init(self.engine, debug=True)
199
200        self.examine_window = pychan.widgets.\
201                                Window(title=unicode(object_title),
202                                       position_technique="center:center",
203                                       min_size=(175,175))
204
205        self.scroll = pychan.widgets.ScrollArea(name='scroll', size=(150,150))
206        self.description = pychan.widgets.Label(name='descText',
207                                                text=unicode(desc),
208                                                wrap_text=True)
209        self.description.max_width = 170
210        self.scroll.addChild(self.description)
211        self.examine_window.addChild(self.scroll)
212       
213        self.close_button = pychan.widgets.Button(name='closeButton',
214                                                  text=unicode('Close'))
215        self.examine_window.addChild(self.close_button)
216
217        self.examine_window.mapEvents({'closeButton':self.examine_window.hide})
218
219    def closePopUp(self):
220        if self.examine_window.isVisible():
221            self.examine_window.hide()
222   
223    def showPopUp(self):
224        self.examine_window.show()
Note: See TracBrowser for help on using the repository browser.