Imported Upstream version 2008.1+svn1656
[opeanno-debian-packaging.git] / game / util / inventory_widget.py
blob33e91b7dffe7bc6fddb6e44ced9b1283294cbe4a
1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
3 # team@openanno.org
4 # This file is part of OpenAnno.
6 # OpenAnno is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # ###################################################
22 import pychan
23 from game.world.storage import GenericStorage
24 import game.main
26 class Inventory(pychan.widgets.Container):
27 """The inventory widget is used to display a stock of items, namely a Storage class instance.
28 It makes use of the ImageFillStatusButton to display the icons for resources and the fill bar.
29 It can be used like any other widget inside of xml's, but for full functionality the inventory
30 has to be manually set, or use the TabWidget, which will autoset it (was made to be done this way).
32 XML use: <inventory />, can take all the parameters that pychan.widgets.Container can."""
33 icon_width = 50 # pixels a resource icon is wide
35 def __init__(self, **kwargs):
36 super(Inventory,self).__init__(**kwargs)
37 self._inventory = None
39 def _set_inventory(self, inv):
40 """Sets the inventory
41 @var inventory: Storage class inventory"""
42 assert(isinstance(inv, GenericStorage))
43 self._inventory = inv
44 self._draw()
46 def _get_inventory(self):
47 return self._inventory
49 inventory = property(_get_inventory, _set_inventory)
51 def isset_inventory(self):
52 """Returns whether the inventory is set or not"""
53 return True if inventory is not None else False
55 def _draw(self):
56 """Draws the inventory."""
57 if len(self.children) is not 0:
58 self.removeChildren(*self.children)
59 vbox = pychan.widgets.VBox(padding = 0)
60 vbox.width = self.width
61 current_hbox = pychan.widgets.HBox(padding = 0)
62 index = 0
63 for resid, amount in self._inventory._storage.iteritems():
64 icon, icon_disabled = game.main.db('SELECT icon, CASE WHEN (icon_disabled is null) THEN icon ELSE icon_disabled END from data.resource WHERE rowid=?', resid)[0]
65 button = ImageFillStatusButton(up_image=icon_disabled if amount == 0 else icon, down_image=icon_disabled if amount == 0 else icon, hover_image=icon_disabled if amount == 0 else icon, text=str(amount), size=(50,50),res_id = resid, opaque=False)
66 button.filled = int(float(amount)/float(self._inventory.limit)*100.0)
67 current_hbox.addChild(button)
68 if index % (vbox.width/(self.__class__.icon_width+10)) == 0 and index is not 0:
69 vbox.addChild(current_hbox)
70 current_hbox = pychan.widgets.HBox(padding=0)
71 index += 1
72 vbox.addChild(current_hbox)
73 self.addChild(vbox)
74 self.stylize('menu')
76 class ImageFillStatusButton(pychan.widgets.Container):
78 def __init__(self, up_image, down_image, hover_image, text, res_id, **kwargs):
79 """Represents the image in the ingame gui, with a bar to show how full the inventory is for that resource
80 Derives from pychan.widgets.Container, but also takes the args of the pychan.widgets.Imagebutton,
81 in order to display the image. The container is only used, because ImageButtons can't have children.
82 This is ment to be used with the Inventory widget."""
83 super(ImageFillStatusButton, self).__init__(**kwargs)
84 self.up_image, self.down_image, self.hover_image, self.text = up_image, down_image, hover_image, text
85 self._filled = 0
86 self.res_id = res_id
88 def _set_filled(self, percent):
89 """"@var percent: int percent that fillstatus will be green"""
90 self._filled = percent
91 self._draw()
93 def _get_filled(self):
94 return self._filled
96 filled = property(_get_filled, _set_filled)
98 def _draw(self):
99 """Draws the icon + bar."""
100 self.button = pychan.widgets.ImageButton(text=self.text, up_image=self.up_image, down_image=self.down_image, hover_image=self.hover_image)
101 self.button.size = (50,50)
102 bar = pychan.widgets.Icon("content/gui/tab_widget/green_line.png")
103 bar.position = (self.button.width-bar.width-1, self.button.height-int(self.button.height/100.0*self._filled))
104 self.addChildren(self.button, bar)