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

Source Code for Module horizons.gui.mousetools.cursortool

  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  import logging 
 23  import math 
 24   
 25  from fife import fife 
 26   
 27  import horizons.globals 
 28  from horizons.util.shapes import Point 
 29   
 30  # round half towards plus infinity 
 31  # http://en.wikipedia.org/wiki/Rounding#Round_half_up 
 32  roundhalfplus = lambda x: int(round(math.floor(x + x) / 2.0 + 0.25)) 
 33   
 34   
35 -class CursorTool(fife.IMouseListener):
36 """Basic tool for cursors.""" 37 log = logging.getLogger("gui.mousetools") 38
39 - def __init__(self, session):
40 super().__init__() 41 assert isinstance(session, horizons.session.Session) 42 self.session = session 43 self.enable()
44
45 - def enable(self):
46 """Call this to get events.""" 47 horizons.globals.fife.eventmanager.addMouseListener(self)
48
49 - def disable(self):
50 """Call this to not get events.""" 51 horizons.globals.fife.eventmanager.removeMouseListener(self)
52
53 - def remove(self):
54 self.disable()
55
56 - def mousePressed(self, evt):
57 pass
58
59 - def mouseReleased(self, evt):
60 pass
61
62 - def mouseEntered(self, evt):
63 pass
64
65 - def mouseExited(self, evt):
66 pass
67
68 - def mouseClicked(self, evt):
69 pass
70
71 - def mouseWheelMovedUp(self, evt):
72 pass
73
74 - def mouseWheelMovedDown(self, evt):
75 pass
76
77 - def mouseWheelMovedLeft(self, evt):
78 pass
79
80 - def mouseWheelMovedRight(self, evt):
81 pass
82
83 - def mouseMoved(self, evt):
84 pass
85
86 - def mouseDragged(self, evt):
87 pass
88
89 - def _round_map_coords(self, map_x, map_y):
90 """Returns the coordinates of an event at the map. 91 92 Why roundhalfplus? 93 94 a b a-b round(a)-round(b) roundplus(a)-roundplus(b) 95 96 1.50 0.50 1.00 1.00 1.0 97 0.50 -0.49 0.99 1.00 1.0 98 -0.49 -1.49 1.00 1.00 1.0 99 Error: 0.50 -0.50 1.00 2.00 1.0 100 101 This error would result in fields at position 0 to be smaller than the others, 102 because both sides (-0.5 and 0.5) would be wrongly assigned to the other fields. 103 104 @return Point with int coordinates""" 105 return Point(roundhalfplus(map_x), roundhalfplus(map_y))
106
107 - def get_world_location(self, evt):
108 screenpoint = self._get_screenpoint(evt) 109 mapcoords = self.session.view.cam.toMapCoordinates(screenpoint, False) 110 return self._round_map_coords(mapcoords.x, mapcoords.y)
111
112 - def get_exact_world_location(self, evt):
113 """Returns the coordinates of an event at the map. 114 @return FifePoint with float coordinates or something with getX/getY""" 115 screenpoint = self._get_screenpoint(evt) 116 return self.session.view.cam.toMapCoordinates(screenpoint, False)
117
118 - def _get_screenpoint(self, arg):
119 """Python lacks polymorphism.""" 120 if isinstance(arg, fife.ScreenPoint): 121 return arg 122 else: 123 return fife.ScreenPoint(arg.getX(), arg.getY())
124
125 - def end(self):
126 self.session = None 127 self.helptext = None
128