| 27 | |
| 28 | |
| 29 | class VectorPicker(wx.Window): |
| 30 | |
| 31 | myEVT_VECTOR_CHANGED = wx.NewEventType() |
| 32 | EVT_VECTOR_CHANGED = wx.PyEventBinder(myEVT_VECTOR_CHANGED, 1) |
| 33 | |
| 34 | def __init__(self, *args, **kwargs): |
| 35 | self._label = str(kwargs.pop('label', '')) |
| 36 | self._labelpos = int(kwargs.pop('labelpos', 0)) |
| 37 | self._offset = float(kwargs.pop('offset', 0)) |
| 38 | self._size = max(0, float(kwargs.pop('size', 50))) |
| 39 | self._directionOnly = kwargs.pop('directionOnly', False) |
| 40 | super().__init__(*args, **kwargs) |
| 41 | self._fontsize = max(1, float(kwargs.pop('fontsize', 8 / self.GetContentScaleFactor()))) |
| 42 | self._font = wx.Font(round(self._fontsize), wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False) |
| 43 | self._angle = 0 |
| 44 | self.__length = 1 |
| 45 | self._left = False |
| 46 | self._right = False |
| 47 | self._savedFocusedWindow = None |
| 48 | self.SetToolTip(wx.ToolTip(self._tooltip)) |
| 49 | self.Bind(wx.EVT_PAINT, self.OnPaint) |
| 50 | self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) |
| 51 | self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) |
| 52 | self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown) |
| 53 | self.Bind(wx.EVT_MOUSEWHEEL, self.OnWheel) |
| 54 | # Allows to focus these widgets on hover, needed to support |
| 55 | # vector length changing by scrolling |
| 56 | if 'wxMSW' in wx.PlatformInfo: |
| 57 | self.Bind(wx.EVT_MOTION, self.OnMouseMove) |
| 58 | self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave) |
| 59 | |
| 60 | @property |
| 61 | def _tooltip(self): |
| 62 | if self._directionOnly: |
| 63 | return 'Click to set angle\nShift-click or right-click to snap to 15% angle' |
| 64 | else: |
| 65 | return 'Click to set angle and velocity\nShift-click or right-click to snap to 15% angle/5% speed increments\nMouse wheel to change velocity only' |
| 66 | |
| 67 | @property |
| 68 | def _length(self): |
| 69 | if self._directionOnly: |
| 70 | return 1 |
| 71 | else: |
| 72 | return self.__length |
| 73 | |
| 74 | @_length.setter |
| 75 | def _length(self, newLength): |
| 76 | self.__length = newLength |
| 77 | |
| 78 | def DoGetBestSize(self): |
| 79 | return wx.Size(round(self._size), round(self._size)) |
| 80 | |
| 81 | def AcceptsFocusFromKeyboard(self): |
| 82 | return False |
| 83 | |
| 84 | def GetValue(self): |
| 85 | return self._angle, self._length |
| 86 | |