Permalink
Browse files

Implemented pipette tool (hotkey: o).

Refactored cursor management.
  • Loading branch information...
1 parent dca5d4d commit da80e964165f6ec4ac80845cc2509fe76e25900c @totycro totycro committed Nov 27, 2011
View
@@ -9,7 +9,7 @@ For more information visit http://www.gnu.org/copyleft/gpl.html .
The file
-content/gui/images/cursors/cursor_pipet.png
+content/gui/images/cursors/cursor_pipette.png
is licensed by the GIMP team ( http://gimp.org ) under the terms of the General Public License, version 2.
View
@@ -340,10 +340,13 @@ def init(self):
#Set game cursor
self.cursor = self.engine.getCursor()
- self.default_cursor_image = self.imagemanager.load('content/gui/images/cursors/cursor.png')
- self.tearing_cursor_image = self.imagemanager.load('content/gui/images/cursors/cursor_tear.png')
- self.attacking_cursor_image = self.imagemanager.load('content/gui/images/cursors/cursor_attack.png')
- self.cursor.set(self.default_cursor_image)
+ self.cursor_images = {
+ 'default': self.imagemanager.load('content/gui/images/cursors/cursor.png'),
+ 'tearing': self.imagemanager.load('content/gui/images/cursors/cursor_tear.png'),
+ 'attacking': self.imagemanager.load('content/gui/images/cursors/cursor_attack.png'),
+ 'pipette': self.imagemanager.load('content/gui/images/cursors/cursor_pipette.png')
+ }
+ self.cursor.set( self.cursor_images['default'] )
#init pychan
self.pychan.init(self.engine, debugPychan)
@@ -376,6 +379,11 @@ def init(self):
self._gotInited = True
self.setup_setting_extras()
+ def set_cursor(self, which="default"):
+ """Sets a certain cursor image.
+ See definition of cursor_images for reference."""
+ self.cursor.set( self.cursor_images[which] )
+
def setup_setting_extras(self):
slider_initial_data = {}
slider_event_map = {}
@@ -168,6 +168,13 @@ def keyPressed(self, evt):
# require shift to make it less likely that an ordinary user stumbles upon this
# this is done because the maps aren't usable without moving them to the right places
self.session.ingame_gui.show_save_map_dialog()
+ elif keystr == 'o':
+ # copy mode: pipette tool
+ from horizons.gui.mousetools import PipetteTool, SelectionTool
+ if isinstance(self.session.cursor, PipetteTool):
+ self.session.cursor = SelectionTool(self.session)
+ else:
+ self.session.cursor = PipetteTool(self.session)
else:
return
evt.consume()
@@ -25,3 +25,4 @@
from selectiontool import SelectionTool
from tearingtool import TearingTool
from attackingtool import AttackingTool
+from pipettetool import PipetteTool
@@ -60,9 +60,9 @@ def mouseMoved(self, evt):
super(AttackingTool, self).mouseMoved(evt)
target = self._get_attackable_instance(evt)
if target:
- horizons.main.fife.cursor.set(horizons.main.fife.attacking_cursor_image)
+ horizons.main.fife.set_cursor("attacking")
else:
- horizons.main.fife.cursor.set(horizons.main.fife.default_cursor_image)
+ horizons.main.fife.set_cursor("default")
def _get_attackable_instance(self, evt):
"""
@@ -0,0 +1,83 @@
+# ###################################################
+# Copyright (C) 2011 The Unknown Horizons Team
+# team@unknown-horizons.org
+# This file is part of Unknown Horizons.
+#
+# Unknown Horizons is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the
+# Free Software Foundation, Inc.,
+# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+# ###################################################
+
+from fife import fife
+
+import horizons.main
+
+from horizons.entities import Entities
+from horizons.gui.mousetools import SelectionTool, BuildingTool, NavigationTool
+
+
+class PipetteTool(NavigationTool):
+ """Tool to select buildings in order to build another building of
+ the type of the selected building"""
+ HIGHLIGHT_COLOR = (255, 0, 0)
+
+ def __init__(self, session):
+ super(PipetteTool, self).__init__(session)
+ self.session.gui.on_escape = self.on_escape
+ self.renderer = session.view.renderer['InstanceRenderer']
+ horizons.main.fife.set_cursor('pipette')
+
+ def end(self):
+ horizons.main.fife.set_cursor('default')
+ super(PipetteTool, self).end()
+
+ def on_escape(self):
+ self._remove_coloring()
+ self.session.cursor = SelectionTool(self.session)
+
+ def mouseMoved(self, evt):
+ self.update_coloring(evt)
+
+ def mousePressed(self, evt):
+ if evt.getButton() == fife.MouseEvent.LEFT:
+ obj = self._get_object(evt)
+ if obj:
+ # clean up before end, it could interfere with other colorings
+ self._remove_coloring()
+ self.session.cursor = BuildingTool(self.session, \
+ Entities.buildings[obj.id])
+ evt.consume()
+ elif evt.getButton() == fife.MouseEvent.RIGHT:
+ self.on_escape()
+ evt.consume()
+ else:
+ suer(PipetteTool, self).mouseClicked(evt)
+
+ def _get_object(self, evt):
+ for obj in self.get_hover_instances(evt):
+ if obj.id in Entities.buildings:
+ return obj
+ return None
+
+ def update_coloring(self, evt):
+ obj = self._get_object(evt)
+ if obj:
+ self._remove_coloring()
+ self._add_coloring(obj)
+
+ def _add_coloring(self, obj):
+ self.renderer.addColored(obj.fife_instance, *self.__class__.HIGHLIGHT_COLOR)
+
+ def _remove_coloring(self):
+ self.renderer.removeAllColored()
@@ -158,7 +158,7 @@ def apply_select(self):
self.session.cursor = AttackingTool(self.session)
if not attacking_unit_found and isinstance(self.session.cursor, AttackingTool):
self.session.cursor = SelectionTool(self.session)
- horizons.main.fife.cursor.set(horizons.main.fife.default_cursor_image)
+ horizons.main.fife.set_cursor('default')
def mousePressed(self, evt):
if evt.isConsumedByWidgets():
@@ -42,12 +42,12 @@ def __init__(self, session):
self.oldedges = None
self.tear_tool_active = True
self.session.gui.on_escape = self.on_escape
- horizons.main.fife.cursor.set(horizons.main.fife.tearing_cursor_image)
+ horizons.main.fife.set_cursor("tearing")
def end(self):
self._mark()
self.tear_tool_active = False
- horizons.main.fife.cursor.set(horizons.main.fife.default_cursor_image)
+ horizons.main.fife.set_cursor("default")
super(TearingTool, self).end()
def mouseDragged(self, evt):
View
@@ -203,7 +203,7 @@ def start_singleplayer(map_file, playername = "Player", playercolor = None, is_s
# remove cursor while loading
fife.cursor.set(fife_module.CURSOR_NONE)
fife.engine.pump()
- fife.cursor.set(fife.default_cursor_image)
+ fife.set_cursor('default')
# hide whatever is displayed before the game starts
_modules.gui.hide()
@@ -266,7 +266,7 @@ def prepare_multiplayer(game, trader_enabled = True, pirate_enabled = True, natu
# remove cursor while loading
fife.cursor.set(fife_module.CURSOR_NONE)
fife.engine.pump()
- fife.cursor.set(fife.default_cursor_image)
+ fife.set_cursor('default')
# hide whatever is displayed before the game starts
_modules.gui.hide()

0 comments on commit da80e96

Please sign in to comment.