source: trunk/game/scripts/gui/containergui.py @ 605

Revision 605, 5.1 KB checked in by beliar, 9 years ago (diff)

Patch by Domtron_Vox and Beliar.

  • Items being dragged will now be removed from the "real" container when dragging them rather when dropping them. (Beliar)
  • Items from other containers can now be switched with items in the Inventory when dropped on top of them. Does not work the other way around yet. (Domtron_Vox)
Line 
1#!/usr/bin/env python
2
3#   This program is free software: you can redistribute it and/or modify
4#   it under the terms of the GNU General Public License as published by
5#   the Free Software Foundation, either version 3 of the License, or
6#   (at your option) any later version.
7
8#   This program is distributed in the hope that it will be useful,
9#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#   GNU General Public License for more details.
12
13#   You should have received a copy of the GNU General Public License
14#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
15from fife.extensions.pychan.tools import callbackWithArguments as cbwa
16
17from scripts.gui.containergui_base import ContainerGUIBase
18from scripts.gui import drag_drop_data as data_drag
19
20class ContainerGUI(ContainerGUIBase):
21    def __init__(self, controller, title, container):
22        """A class to create a window showing the contents of a container.
23           @param controller: The current Controller
24           @type controller: Class derived from ControllerBase
25           @param title: The title of the window
26           @type title: string
27           @param container: A container to represent
28           @type container: Container
29           @return: None"""
30        super(ContainerGUI, self).__init__(controller, "gui/container_base.xml")
31        self.gui.findChild(name="topWindow").title = title
32       
33        self.empty_images = dict()
34        self.container = container
35        self.events_to_map = {}       
36        self.buttons = ("Slot1", "Slot2", "Slot3",
37                        "Slot4", "Slot5", "Slot6",
38                        "Slot7", "Slot8", "Slot9")         
39   
40    def updateImages(self):
41        for index, button in enumerate(self.buttons):
42            widget = self.gui.findChild(name=button)
43            widget.item = self.container.getItemAt(index)
44            self.updateImage(widget) 
45               
46    def updateImage(self, button):
47        if (button.item == None):
48            image = self.empty_images[button.name]
49        else:
50            image = button.item.getInventoryThumbnail()
51        button.up_image = image
52        button.down_image = image
53        button.hover_image = image
54
55    def dragObject(self, obj):
56        """Drag the selected object.
57           @type obj: string
58           @param obj: The name of the object within
59                       the dictionary 'self.buttons'
60           @return: None"""
61        # get the widget from the gui with the name obj
62        drag_widget = self.gui.findChild(name = obj)
63        drag_item = drag_widget.item
64        # only drag if the widget is not empty
65        if (drag_item != None):
66            # get the item that the widget is 'storing'
67            data_drag.dragged_item = drag_widget.item
68            # get the up and down images of the widget
69            up_image = drag_widget.up_image
70            down_image = drag_widget.down_image
71            # set the mouse cursor to be the widget's image
72            self.controller.setMouseCursor(up_image.source, down_image.source)
73            data_drag.dragged_image = up_image.source
74            data_drag.dragging = True
75            data_drag.dragged_widget = drag_widget
76
77            self.container.takeItem(drag_widget.item)
78           
79            # after dragging the 'item', set the widgets' images
80            # so that it has it's default 'empty' images
81            drag_widget.item = None
82            self.updateImage(drag_widget)
83           
84    def dropObject(self, obj):
85        """Drops the object being dropped
86           @type obj: string
87           @param obj: The name of the object within
88                       the dictionary 'self.buttons'
89           @return: None"""
90        drop_widget = self.gui.findChild(name = obj)
91        if drop_widget != data_drag.dragged_widget:
92            self.container.placeItem(data_drag.dragged_item, drop_widget.index)
93        drop_widget.item = data_drag.dragged_item
94        self.updateImage(drop_widget)
95        data_drag.dragging = False
96        #reset the mouse cursor to the normal cursor
97        self.controller.resetMouseCursor()
98       
99    def showContainer(self):
100        """Show the container
101           @return: None"""
102        # Prepare slots 1 through 9
103        empty_image = "gui/inv_images/inv_backpack.png"
104        slot_count = 9
105        for counter in range(1, slot_count+1):
106            slot_name = "Slot%i" % counter
107            self.empty_images[slot_name] = empty_image
108            widget = self.gui.findChild(name=slot_name)
109            widget.item = self.container.items.get(counter-1)
110            widget.index = counter-1
111            self.updateImage(widget)
112            self.events_to_map[slot_name] = cbwa(self.dragDrop, slot_name)
113            self.events_to_map[slot_name + "/mouseReleased"] = \
114                                            self.showContextMenu
115
116        self.gui.mapEvents(self.events_to_map)
117        self.gui.show()
118       
119    def hideContainer(self):
120        """Hide the container
121           @return: None"""
122        if self.gui.isVisible():
123            self.gui.hide()     
Note: See TracBrowser for help on using the repository browser.