/
polygonSelectionTool.py
59 lines (46 loc) · 1.53 KB
/
polygonSelectionTool.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
from mojo.events import EditingTool, installTool
from mojo.drawingTools import *
from fontTools.pens.cocoaPen import CocoaPen
class PolygonSelectionTool(EditingTool):
def setup(self):
self.pen = None
self._oldPen = None
def mouseDown(self, point, clickCount):
if self.selection.hasSelection():
return
if not self.optionDown:
self.pen = CocoaPen(None)
else:
self.pen = self._oldPen
self.pen.moveTo((point.x, point.y))
def mouseDragged(self, point, delta):
if self.pen is None:
return
self.pen.lineTo((point.x, point.y))
def mouseUp(self, point):
if self.pen is None:
return
self.pen.closePath()
glyph = self.getGlyph()
path = self.pen.path
for contour in glyph:
for point in contour.points:
result = path.containsPoint_((point.x, point.y))
if self.controlDown:
point.selected = not result
else:
point.selected = result
self._oldPen = self.pen
self.pen = None
def draw(self, scale):
if self.pen is None:
return
fill(0, .1)
stroke(0, .6)
strokeWidth(scale)
drawPath(self.pen.path)
def canSelectWithMarque(self):
return False
def getToolbarTip(self):
return "Polygon Selection Tool"
installTool(PolygonSelectionTool())