Tool examples

Simple tool example (1)

"""
Example of a simple tool.

Doesn't do more then drawing an oval on the mouse down position.

"""
from mojo.events import BaseEventTool, installTool
from mojo.drawingTools import *

class MyTool(BaseEventTool):
    
    def setup(self):
        self.position = None
    
    def mouseDown(self, point, clickCount):
        self.position = point
    
    def mouseDragged(self, point, delta):
        self.position = point
    
    def mouseUp(self, point):
        self.position = None
    
    def draw(self, scale):
        if self.position is not None:
            size = 10
            x = self.position.x - size
            y = self.position.y - size
            fill(None)
            stroke(1, 0, 0)
            oval(x, y, size*2, size*2)
    
    def getToolbarTip(self):
        return "My Tool Tip"
    
    

installTool(MyTool())

Simple tool example (2)

from mojo.events import BaseEventTool, installTool
from AppKit import *

class MyEvent(BaseEventTool):

    def becomeActive(self):
        print "active"

    def becomeInactive(self):
        print "inactive"

    def mouseDown(self, point, clickCount):
        # getGLyph returns the current glyph as robofab object
        print "mouseDown", self.getGlyph(), clickCount

    def rightMouseDown(self, point, event):
        print "rightMouseDown"

    def mouseDragged(self, point, delta):
        print "mousedragged"

    def rightMouseDragged(self, point, delta):
        print "rightMouseDragged"

    def mouseUp(self, point):
        print "mouseup"

    def keyDown(self, event):
        # a dict of all modifiers, shift, command, alt, option
        print "keyDown", self.getModifiers() 

    def keyUp(self, event):
        print "keyUp"

    def modifiersChanged(self):
        print "modifiersChanged"

    def draw(self, scale):
        print "draw", self.isDragging()
        if self.isDragging():
            ## draw a red dot when dragging
            r = 50
            NSColor.redColor().set()
            x, y = self.currentPoint
            NSBezierPath.bezierPathWithOvalInRect_(((x-r, y-r), (r*2, r*2))).fill()

    def drawBackground(self, scale):
        print "drawBackground here"

    #def getDefaultCursor(self):
    #   this will be the cursor default is an arrow
    #   return aNSCursor
    #def getToolbarIcon(self):
    #   this is setting the icon in the toolbar default is an arrow
    #   return aNSImage

    def getToolbarTip(self):
        return "My Event Tool Bar Tip"

    #notifications

    def viewDidChangeGlyph(self):
        print "view changed glyph"

    def preferencesChanged(self):
        print "prefs changed"

installTool(MyEvent())

Polygonal selection tool

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())

Preview multiple fonts

import vanilla
from defconAppKit.windows.baseWindow import BaseWindowController
from defconAppKit.controls.glyphLineView import GlyphLineView
from mojo import events


class MultiFontPreview(BaseWindowController):

    def __init__(self):
        self.w = vanilla.Window((400, 400), minSize=(100, 100))
        self.w.glyphLineView = GlyphLineView((0, 0, 0, 0), pointSize=None, autohideScrollers=False, showPointSizePlacard=True)
        events.addObserver(self, "glyphChanged", "currentGlyphChanged")
        self.glyphChanged(dict(glyph=CurrentGlyph()))
        self.setUpBaseWindowBehavior()
        self.w.open()

    def windowCloseCallback(self, sender):
        events.removeObserver(self, "currentGlyphChanged")
        super(MultiFontPreview, self).windowCloseCallback(sender)

    def glyphChanged(self, info):
        glyph = CurrentGlyph()
        if glyph is None:
            glyphs = []
        else:
            glyphName = glyph.name
            glyphs = [font[glyphName].naked() for font in AllFonts() if glyphName in font]
        self.w.glyphLineView.set(glyphs)

MultiFontPreview()