-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcode.py
65 lines (55 loc) · 2.37 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# coding=utf-8
from vanilla import *
from defconAppKit.windows.baseWindow import BaseWindowController
from mojo.events import addObserver, removeObserver
import math
class ShowMouseCoordinatesTextBox(TextBox):
"""
A vanilla text box with some goodies about the mouse.
"""
def __init__(self, *args, **kwargs):
self.observers = {"mouseMoved", "mouseDragged", "mouseUp"}
# initialize the parent object, and add all of the observers
super(ShowMouseCoordinatesTextBox, self).__init__(*args, **kwargs)
addObserver(self, "mouseMoved", "mouseMoved")
addObserver(self, "mouseDragged", "mouseDragged")
addObserver(self, "mouseUp", "mouseUp")
def mouseMoved(self, info):
point = info["point"]
text = u"%.0f %.0f" % (point.x, point.y)
self.set(text)
def mouseDragged(self, info):
point = info["point"]
positionSymbol = unichr(8982)
deltaPoint = info["delta"]
angle = math.degrees(math.atan2(deltaPoint.y, deltaPoint.x))
distance = math.hypot(deltaPoint.x, deltaPoint.y)
text = u"%.0f %.0f %.0f %.0f %.2f° %.0f" % (point.x, point.y, deltaPoint.x, deltaPoint.y, angle, distance)
self.set(text)
def mouseUp(self, info):
point = info["point"]
text = u"%.0f %.0f" % (point.x, point.y)
self.set(text)
def _breakCycles(self):
super(ShowMouseCoordinatesTextBox, self)._breakCycles()
removeObserver(self, "mouseMoved")
removeObserver(self, "mouseDragged")
removeObserver(self, "mouseUp")
class ShowMouseCoordinates(BaseWindowController):
"""
Attach a vanilla text box to a window.
"""
def __init__(self):
addObserver(self, "glyphWindowDidOpen", "glyphWindowDidOpen")
def glyphWindowDidOpen(self, info):
window = info["window"]
vanillaView = ShowMouseCoordinatesTextBox((20, -30, -20, 22), "", alignment="left", sizeStyle="mini")
superview = window.editGlyphView.enclosingScrollView().superview()
view = vanillaView.getNSTextField()
frame = superview.frame()
vanillaView._setFrame(frame)
superview.addSubview_(view)
def windowCloseCallback(self, sender):
super(ShowMouseCoordinatesTextBox, self).windowCloseCallback(sender)
removeObserver(self, "glyphWindowDidOpen")
ShowMouseCoordinates()