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

Revision 606, 5.5 KB checked in by beliar, 9 years ago (diff)

Patch by Domtron_Vox.

  • Items from the inventory can now be switched with items in containers too when dropped on top of them.
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        drop_index = drop_widget.index
92        replace_item = drop_widget.item
93
94        if data_drag.dragging:
95            container = self.container
96            drag_item = data_drag.dragged_item
97            #this will get the replacement item and the data for drag_drop if
98            ## there is an item all ready occupying the slot
99            if replace_item != None:
100                self.dragObject(obj)
101               
102            container.placeItem(drag_item, drop_index)
103           
104        drop_widget.item = drag_item
105        self.updateImage(drop_widget)
106        #if there was no item the stop dragging and reset cursor
107        if replace_item == None:
108            data_drag.dragging = False
109            #reset the mouse cursor to the normal cursor
110            self.controller.resetMouseCursor()
111       
112    def showContainer(self):
113        """Show the container
114           @return: None"""
115        # Prepare slots 1 through 9
116        empty_image = "gui/inv_images/inv_backpack.png"
117        slot_count = 9
118        for counter in range(1, slot_count+1):
119            slot_name = "Slot%i" % counter
120            self.empty_images[slot_name] = empty_image
121            widget = self.gui.findChild(name=slot_name)
122            widget.item = self.container.items.get(counter-1)
123            widget.index = counter-1
124            self.updateImage(widget)
125            self.events_to_map[slot_name] = cbwa(self.dragDrop, slot_name)
126            self.events_to_map[slot_name + "/mouseReleased"] = \
127                                            self.showContextMenu
128
129        self.gui.mapEvents(self.events_to_map)
130        self.gui.show()
131       
132    def hideContainer(self):
133        """Hide the container
134           @return: None"""
135        if self.gui.isVisible():
136            self.gui.hide()     
Note: See TracBrowser for help on using the repository browser.