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

Revision 610, 5.8 KB checked in by beliar, 9 years ago (diff)

Patch by Beliar.

  • Added "Open" action to InventoryItems?. This will open a ContainerGui? for now. Should make a gui that can reflect the actual possible size of the container.
  • Split OpenBoxAction? into a generic OpenAction?, for Containers that are not "openable" and a OpenBoxAction? for all Containers that are "openable" (The latter ones have an "open" method)
  • Moved SingleItemContainer? from composed to base and made it derive from the normal Container class.
  • Added a SingleItemCarryableContainer? class
  • MapItems? in the agent files can now have an attributes item.
  • Added container attribute to possible MapItem? attributes
  • MapItems? that have the "Container" attribute will have the "Open" action automatically added on creation.
  • Inventory now uses Container from base instead of CarryableContainer? from composed
  • Gave "Pot" the "Container" attribute
  • Added ItemSelf? exception to Container which will be raised when trying to place a container in itself.
  • ContainerGui? and InventoryGui? now catch exceptions from the Container
  • Removed debug print statements left over in inventory_gui
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
19from scripts.objects.base import Container
20
21class ContainerGUI(ContainerGUIBase):
22    def __init__(self, controller, title, container):
23        """A class to create a window showing the contents of a container.
24           @param controller: The current Controller
25           @type controller: Class derived from ControllerBase
26           @param title: The title of the window
27           @type title: string
28           @param container: A container to represent
29           @type container: Container
30           @return: None"""
31        super(ContainerGUI, self).__init__(controller, "gui/container_base.xml")
32        self.gui.findChild(name="topWindow").title = title
33       
34        self.empty_images = dict()
35        self.container = container
36        self.events_to_map = {}       
37        self.buttons = ("Slot1", "Slot2", "Slot3",
38                        "Slot4", "Slot5", "Slot6",
39                        "Slot7", "Slot8", "Slot9")         
40   
41    def updateImages(self):
42        for index, button in enumerate(self.buttons):
43            widget = self.gui.findChild(name=button)
44            widget.item = self.container.getItemAt(index)
45            self.updateImage(widget) 
46               
47    def updateImage(self, button):
48        if (button.item == None):
49            image = self.empty_images[button.name]
50        else:
51            image = button.item.getInventoryThumbnail()
52        button.up_image = image
53        button.down_image = image
54        button.hover_image = image
55
56    def dragObject(self, obj):
57        """Drag the selected object.
58           @type obj: string
59           @param obj: The name of the object within
60                       the dictionary 'self.buttons'
61           @return: None"""
62        # get the widget from the gui with the name obj
63        drag_widget = self.gui.findChild(name = obj)
64        drag_item = drag_widget.item
65        # only drag if the widget is not empty
66        if (drag_item != None):
67            # get the item that the widget is 'storing'
68            data_drag.dragged_item = drag_widget.item
69            # get the up and down images of the widget
70            up_image = drag_widget.up_image
71            down_image = drag_widget.down_image
72            # set the mouse cursor to be the widget's image
73            self.controller.setMouseCursor(up_image.source, down_image.source)
74            data_drag.dragged_image = up_image.source
75            data_drag.dragging = True
76            data_drag.dragged_widget = drag_widget
77
78            self.container.takeItem(drag_widget.item)
79           
80            # after dragging the 'item', set the widgets' images
81            # so that it has it's default 'empty' images
82            drag_widget.item = None
83            self.updateImage(drag_widget)
84           
85    def dropObject(self, obj):
86        """Drops the object being dropped
87           @type obj: string
88           @param obj: The name of the object within
89                       the dictionary 'self.buttons'
90           @return: None"""
91        try:
92            drop_widget = self.gui.findChild(name = obj)
93            drop_index = drop_widget.index
94            replace_item = drop_widget.item
95   
96            if data_drag.dragging:
97                container = self.container
98                drag_item = data_drag.dragged_item
99                #this will get the replacement item and the data for drag_drop if
100                ## there is an item all ready occupying the slot
101                if replace_item != None:
102                    self.dragObject(obj)
103                container.placeItem(drag_item, drop_index)
104               
105            drop_widget.item = drag_item
106            self.updateImage(drop_widget)
107            #if there was no item the stop dragging and reset cursor
108            if replace_item == None:
109                data_drag.dragging = False
110                #reset the mouse cursor to the normal cursor
111                self.controller.resetMouseCursor()
112        except (Container.SlotBusy, Container.TooBig, Container.ItemSelf):
113            #Do we want to notify the player why the item can't be dropped?
114            pass
115       
116    def showContainer(self):
117        """Show the container
118           @return: None"""
119        # Prepare slots 1 through 9
120        empty_image = "gui/inv_images/inv_backpack.png"
121        slot_count = 9
122        for counter in range(1, slot_count+1):
123            slot_name = "Slot%i" % counter
124            self.empty_images[slot_name] = empty_image
125            widget = self.gui.findChild(name=slot_name)
126            widget.item = self.container.items.get(counter-1)
127            widget.index = counter-1
128            self.updateImage(widget)
129            self.events_to_map[slot_name] = cbwa(self.dragDrop, slot_name)
130            self.events_to_map[slot_name + "/mouseReleased"] = \
131                                            self.showContextMenu
132
133        self.gui.mapEvents(self.events_to_map)
134        self.gui.show()
135       
136    def hideContainer(self):
137        """Hide the container
138           @return: None"""
139        if self.gui.isVisible():
140            self.gui.hide()     
Note: See TracBrowser for help on using the repository browser.