comparison gui/containergui.py @ 0:7a89ea5404b1

Initial commit of parpg-core.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 14 May 2011 01:12:35 -0700
parents
children 100a39fa64a2
comparison
equal deleted inserted replaced
-1:000000000000 0:7a89ea5404b1
1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 from fife.extensions.pychan.tools import callbackWithArguments as cbwa
14
15 from parpg.gui.containergui_base import ContainerGUIBase
16 from parpg.gui import drag_drop_data as data_drag
17 from parpg.objects.base import Container
18
19 class ContainerGUI(ContainerGUIBase):
20 def __init__(self, controller, title, container):
21 """A class to create a window showing the contents of a container.
22 @param controller: The current Controller
23 @type controller: Class derived from ControllerBase
24 @param title: The title of the window
25 @type title: string
26 @param container: A container to represent
27 @type container: Container
28 @return: None"""
29 super(ContainerGUI, self).__init__(controller, "gui/container_base.xml")
30 self.gui.findChild(name="topWindow").title = title
31
32 self.empty_images = dict()
33 self.container = container
34 self.events_to_map = {}
35 self.buttons = ("Slot1", "Slot2", "Slot3",
36 "Slot4", "Slot5", "Slot6",
37 "Slot7", "Slot8", "Slot9")
38
39 def updateImages(self):
40 for index, button in enumerate(self.buttons):
41 widget = self.gui.findChild(name=button)
42 widget.item = self.container.getItemAt(index)
43 self.updateImage(widget)
44
45 def updateImage(self, button):
46 if (button.item == None):
47 image = self.empty_images[button.name]
48 else:
49 image = button.item.getInventoryThumbnail()
50 button.up_image = image
51 button.down_image = image
52 button.hover_image = image
53
54 def dragObject(self, obj):
55 """Drag the selected object.
56 @type obj: string
57 @param obj: The name of the object within
58 the dictionary 'self.buttons'
59 @return: None"""
60 # get the widget from the gui with the name obj
61 drag_widget = self.gui.findChild(name = obj)
62 drag_item = drag_widget.item
63 # only drag if the widget is not empty
64 if (drag_item != None):
65 # get the item that the widget is 'storing'
66 data_drag.dragged_item = drag_widget.item
67 # get the up and down images of the widget
68 up_image = drag_widget.up_image
69 down_image = drag_widget.down_image
70 # set the mouse cursor to be the widget's image
71 self.controller.setMouseCursor(up_image.source, down_image.source)
72 data_drag.dragged_image = up_image.source
73 data_drag.dragging = True
74 data_drag.dragged_widget = drag_widget
75 data_drag.source_container = self.container
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 try:
91 drop_widget = self.gui.findChild(name = obj)
92 drop_index = drop_widget.index
93 replace_item = drop_widget.item
94
95 if data_drag.dragging:
96 container = self.container
97 drag_item = data_drag.dragged_item
98 #this will get the replacement item and the data for drag_drop if
99 ## there is an item all ready occupying the slot
100 if replace_item != None:
101 self.dragObject(obj)
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 except (Container.SlotBusy, Container.TooBig, Container.ItemSelf):
112 #Do we want to notify the player why the item can't be dropped?
113 pass
114
115 def showContainer(self):
116 """Show the container
117 @return: None"""
118 # Prepare slots 1 through 9
119 empty_image = "gui/inv_images/inv_backpack.png"
120 slot_count = 9
121 for counter in range(1, slot_count+1):
122 slot_name = "Slot%i" % counter
123 self.empty_images[slot_name] = empty_image
124 widget = self.gui.findChild(name=slot_name)
125 widget.item = self.container.items.get(counter-1)
126 widget.index = counter-1
127 self.updateImage(widget)
128 self.events_to_map[slot_name] = cbwa(self.dragDrop, slot_name)
129 self.events_to_map[slot_name + "/mouseReleased"] = \
130 self.showContextMenu
131
132 self.gui.mapEvents(self.events_to_map)
133 self.gui.show()
134
135 def hideContainer(self):
136 """Hide the container
137 @return: None"""
138 if self.gui.isVisible():
139 self.gui.hide()