source: branches/active/character_customization/game/parpg/gui/containergui.py @ 736

Revision 736, 5.9 KB checked in by aspidites, 8 years ago (diff)

Patch by Aspidites:

  • renamed scripts package to parpg
  • renamed parpg module to application
  • removed packaging and other related files (kept locally for reference, will reintroduce similar scripts to resolve bug #275
  • updated all import statements to respect changes above
  • Property svn:eol-style set to native
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 parpg.gui.containergui_base import ContainerGUIBase
18from parpg.gui import drag_drop_data as data_drag
19from parpg.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            data_drag.source_container = self.container
78
79            self.container.takeItem(drag_widget.item)
80           
81            # after dragging the 'item', set the widgets' images
82            # so that it has it's default 'empty' images
83            drag_widget.item = None
84            self.updateImage(drag_widget)
85           
86    def dropObject(self, obj):
87        """Drops the object being dropped
88           @type obj: string
89           @param obj: The name of the object within
90                       the dictionary 'self.buttons'
91           @return: None"""
92        try:
93            drop_widget = self.gui.findChild(name = obj)
94            drop_index = drop_widget.index
95            replace_item = drop_widget.item
96   
97            if data_drag.dragging:
98                container = self.container
99                drag_item = data_drag.dragged_item
100                #this will get the replacement item and the data for drag_drop if
101                ## there is an item all ready occupying the slot
102                if replace_item != None:
103                    self.dragObject(obj)
104                container.placeItem(drag_item, drop_index)
105               
106            drop_widget.item = drag_item
107            self.updateImage(drop_widget)
108            #if there was no item the stop dragging and reset cursor
109            if replace_item == None:
110                data_drag.dragging = False
111                #reset the mouse cursor to the normal cursor
112                self.controller.resetMouseCursor()
113        except (Container.SlotBusy, Container.TooBig, Container.ItemSelf):
114            #Do we want to notify the player why the item can't be dropped?
115            pass
116       
117    def showContainer(self):
118        """Show the container
119           @return: None"""
120        # Prepare slots 1 through 9
121        empty_image = "gui/inv_images/inv_backpack.png"
122        slot_count = 9
123        for counter in range(1, slot_count+1):
124            slot_name = "Slot%i" % counter
125            self.empty_images[slot_name] = empty_image
126            widget = self.gui.findChild(name=slot_name)
127            widget.item = self.container.items.get(counter-1)
128            widget.index = counter-1
129            self.updateImage(widget)
130            self.events_to_map[slot_name] = cbwa(self.dragDrop, slot_name)
131            self.events_to_map[slot_name + "/mouseReleased"] = \
132                                            self.showContextMenu
133
134        self.gui.mapEvents(self.events_to_map)
135        self.gui.show()
136       
137    def hideContainer(self):
138        """Hide the container
139           @return: None"""
140        if self.gui.isVisible():
141            self.gui.hide()     
Note: See TracBrowser for help on using the repository browser.