Package horizons :: Package gui :: Package mousetools :: Module pipettetool
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.mousetools.pipettetool

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons 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. 
 10  # 
 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. 
 15  # 
 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  # ################################################### 
 21   
 22  from fife import fife 
 23   
 24  import horizons.globals 
 25  from horizons.component.ambientsoundcomponent import AmbientSoundComponent 
 26  from horizons.constants import LAYERS 
 27  from horizons.entities import Entities 
 28  from horizons.gui.tabs.buildtabs import BuildTab 
 29   
 30  from .navigationtool import NavigationTool 
 31   
 32   
33 -class PipetteTool(NavigationTool):
34 """Tool to select buildings in order to build another building of 35 the type of the selected building""" 36 HIGHLIGHT_COLOR = (0, 200, 90) 37 HIGHLIGHT_NOT_POSSIBLE_COLOR = (200, 90, 90) 38
39 - def __init__(self, session):
40 super().__init__(session) 41 self.renderer = session.view.renderer['InstanceRenderer'] 42 horizons.globals.fife.set_cursor_image('pipette')
43
44 - def remove(self):
45 self._remove_coloring() 46 horizons.globals.fife.set_cursor_image('default') 47 super().remove()
48
49 - def on_escape(self):
51
52 - def mouseMoved(self, evt):
53 self.update_coloring(evt)
54
55 - def mousePressed(self, evt):
56 if evt.getButton() == fife.MouseEvent.LEFT and not evt.isConsumedByWidgets(): 57 obj = self._get_object(evt) 58 if obj and self._is_buildable(obj.id): 59 self.session.ingame_gui.set_cursor('building', Entities.buildings[obj.id]) 60 elif obj: # object that is not buildable 61 AmbientSoundComponent.play_special('error') 62 self.on_escape() 63 else: 64 self.on_escape() 65 evt.consume() 66 elif evt.getButton() == fife.MouseEvent.RIGHT: 67 self.on_escape() 68 evt.consume() 69 else: 70 super().mouseClicked(evt)
71
72 - def _get_object(self, evt):
73 for obj in self.get_hover_instances(evt, layers=[LAYERS.FIELDS, LAYERS.OBJECTS]): 74 if obj.id in Entities.buildings: 75 return obj 76 return None
77
78 - def update_coloring(self, evt):
79 obj = self._get_object(evt) 80 self._remove_coloring() 81 if obj: 82 self._add_coloring(obj)
83
84 - def _is_buildable(self, building_id):
85 building_tiers = BuildTab.get_building_tiers() 86 return building_id in building_tiers and \ 87 building_tiers[building_id] <= self.session.world.player.settler_level
88
89 - def _add_coloring(self, obj):
90 if self._is_buildable(obj.id): 91 self.renderer.addColored(obj.fife_instance, 92 *self.__class__.HIGHLIGHT_COLOR) 93 else: 94 self.renderer.addColored(obj.fife_instance, 95 *self.__class__.HIGHLIGHT_NOT_POSSIBLE_COLOR)
96
97 - def _remove_coloring(self):
98 self.renderer.removeAllColored()
99