1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
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 from game
.util
.inventory_widget
import ImageFillStatusButton
23 from game
.world
.building
.storages
import BranchOffice
24 class TradeWidget(object):
26 def __init__(self
, main_instance
):
27 self
.widget
= game
.main
.fife
.pychan
.loadXML('content/gui/ship/trade.xml')
28 self
.widget
.position
= (
29 game
.main
.session
.ingame_gui
.gui
['minimap'].position
[1] - game
.main
.session
.ingame_gui
.gui
['minimap'].size
[0] - 30 if game
.main
.fife
.settings
.getScreenWidth()/2 + self
.widget
.size
[0]/2 > game
.main
.session
.ingame_gui
.gui
['minimap'].position
[0] else game
.main
.fife
.settings
.getScreenWidth()/2 - self
.widget
.size
[0]/2,
30 game
.main
.fife
.settings
.getScreenHeight() - self
.widget
.size
[1] - 35
32 self
.widget
.stylize('menu')
33 self
.widget
.mapEvents({
34 'size_1' : game
.main
.fife
.pychan
.tools
.callbackWithArguments(self
.set_exchange
, 1),
35 'size_2' : game
.main
.fife
.pychan
.tools
.callbackWithArguments(self
.set_exchange
, 5),
36 'size_3' : game
.main
.fife
.pychan
.tools
.callbackWithArguments(self
.set_exchange
, 10),
37 'size_4' : game
.main
.fife
.pychan
.tools
.callbackWithArguments(self
.set_exchange
, 20),
38 'size_5' : game
.main
.fife
.pychan
.tools
.callbackWithArguments(self
.set_exchange
, 50),
40 self
.main_instance
= main_instance
45 def draw_widget(self
):
46 dropdown
= self
.widget
.findChild(name
='partners')
47 self
.partners
= self
.find_partner()
48 dropdown
.setInitialData([item
.settlement
.name
for item
in self
.partners
])
49 dropdown
.capture(game
.main
.fife
.pychan
.tools
.callbackWithArguments(self
.set_partner
, dropdown
.getData()))
50 if len(self
.partners
) > 0:
51 nearest_partner
= self
.get_nearest_partner(self
.partners
)
52 dropdown
.setData(nearest_partner
)
53 self
.partner
= self
.partners
[nearest_partner
]
54 inv_partner
= self
.widget
.findChild(name
='inventory_partner')
55 inv_partner
.inventory
= self
.partner
.inventory
56 for button
in self
.get_widgets_by_class(inv_partner
, ImageFillStatusButton
):
57 button
.button
.capture(game
.main
.fife
.pychan
.tools
.callbackWithArguments(self
.transfer
, button
.res_id
, self
.partner
, self
.main_instance
))
58 inv
= self
.widget
.findChild(name
='inventory_ship')
59 inv
.inventory
= self
.main_instance
.inventory
60 for button
in self
.get_widgets_by_class(inv
, ImageFillStatusButton
):
61 button
.button
.capture(game
.main
.fife
.pychan
.tools
.callbackWithArguments(self
.transfer
, button
.res_id
,self
.main_instance
, self
.partner
))
62 self
.widget
._recursiveResizeToContent
()
65 def set_partner(self
, partner_id
):
66 self
.partner
= self
.partners(partner_id
)
74 def set_exchange(self
, size
):
76 print 'TradeWidget debug: Exchange size now:', self
.exchange
78 def transfer(self
, res_id
, transfer_from
, transfer_to
):
79 if transfer_to
is not None and transfer_from
is not None:
80 print 'TradeWidget debug: Transfering', self
.exchange
, 't of resource', res_id
, 'from', transfer_from
.name
, 'to', transfer_to
.name
81 ret
= transfer_from
.inventory
.alter_inventory(res_id
, -self
.exchange
) #take ressources, ret = difference to exchange(might not have hat exchange number of res in store)
83 ret
= self
.exchange
if self
.exchange
< abs(ret
) else abs(ret
)
85 ret
= transfer_to
.inventory
.alter_inventory(res_id
, self
.exchange
-ret
) # give ressources
87 transfer_from
.inventory
.alter_inventory(res_id
, ret
) #return ressources that did not fit
90 def get_widgets_by_class(self
, parent_widget
, widget_class
):
91 """Gets all widget of a certain widget class from the tab. (e.g. pychan.widgets.Label for all labels)"""
93 def _find_widget(widget
):
94 if isinstance(widget
, widget_class
):
95 children
.append(widget
)
96 parent_widget
.deepApply(_find_widget
)
99 def find_partner(self
):
100 """find all partners in radius"""
102 for island
in game
.main
.session
.world
.islands
:
103 for building
in island
.buildings
:
104 if isinstance(building
, BranchOffice
) and building
.position
.distance(self
.main_instance
.position
) <= 4:
105 partners
.append(building
)
109 def get_nearest_partner(self
, partners
):
112 for partner
in partners
:
113 dist
= partner
.position
.distance(self
.main_instance
.position
)
114 nearest
= partners
.index(partner
) if dist
< nearest_dist
or nearest_dist
is None else nearest
115 nearest_dist
= dist
if dist
< nearest_dist
or nearest_dist
is None else nearest_dist