Last active
February 1, 2023 18:36
-
-
Save okay-type/b0dd101938470db06715305fce5e6bde to your computer and use it in GitHub Desktop.
a window with a ui for adjusting the horizontal position of anchors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from AppKit import NSScreen | |
from vanilla import * | |
import merz | |
import mojo.subscriber as subs | |
from mojo.subscriber import Subscriber, registerRoboFontSubscriber, unregisterRoboFontSubscriber, listRegisteredSubscribers | |
from mojo.extensions import registerExtensionDefaults, setExtensionDefault, getExtensionDefault, removeExtensionDefault | |
from mojo.drawingTools import * | |
from math import radians, tan, pi | |
from AppKit import NSColor | |
from fontTools.pens.basePen import BasePen | |
from mojo.UI import getDefault, setDefault, SetCurrentGlyphByName | |
import fnmatch | |
from AppKit import NSEvent, NSShiftKeyMask | |
''' | |
anchorHelperXpos v 0.2 | |
ok@yty.pe | |
a window with a ui for adjusting the horizontal position of anchors | |
- detects the anchors in the current glyph | |
- makes a slider to adjust the x-position | |
bugs | |
- can't get glyphEditorGlyphDidChangeAnchors to work (to update slider position when dragging an actual anchor) | |
improvements that would be nice to add... | |
- resizeable window | |
- shift-down to reduce scale of movement (would need to use a lot more math because of how the sliders are built) | |
- throw focus back to edit glyph window on mouse up | |
- delete an anchor | |
- reorder anchors in the UI so they appear in the correct visual order, top to bottom or... | |
- reorder anchors in the actual order in the ufo | |
''' | |
class anchorHelperXpos(Subscriber): | |
debug = False | |
def build(self): | |
print('building anchorHelperXpos') | |
# ui | |
self.x = 0 | |
self.w = 180 | |
self.unit = 30 | |
self.added_sliders = [] | |
# build window | |
self.size = (500, 200) | |
self.w = Window(self.size, title='anchor xpos') | |
self.w.open() | |
self.w.bind('close', self.windowClose) | |
self.windowOpen() | |
self.updateAnchorXposWindow() | |
@property | |
def pref(self): | |
self.prefKey = 'com.okay.anchorHelperXpos' | |
return self.prefKey + '.' + 'posSize' | |
def windowOpen(self): | |
posSize = self.w.getPosSize() | |
initialDefaults = { | |
self.pref: 'posSize', | |
} | |
registerExtensionDefaults(initialDefaults) | |
v = getExtensionDefault(self.pref) | |
w = self.w.getNSWindow() | |
w.setFrame_display_animate_(v, True, False) | |
def windowClose(self, sender): | |
window = sender.getNSWindow() | |
posSize = window.frame() | |
(wminx, wminy), (areaW, areaH) = window.frame() | |
posSize = (wminx, wminy), self.size | |
setExtensionDefault(self.prefKey + '.posSize', posSize) | |
self.destroy() | |
registered_subscribers = listRegisteredSubscribers(subscriberClassName='anchorHelperXpos') | |
if len(registered_subscribers) > 0: | |
for target_subscriber in registered_subscribers: | |
unregisterRoboFontSubscriber(target_subscriber) | |
def destroy(self): | |
print('destroying anchorHelperXpos') | |
# | |
# | |
# events | |
# | |
def glyphEditorDidSetGlyph(self, info): | |
## shouldn't have to do this in next beta | |
self.glyph = info['glyph'] | |
if self.glyph is None: | |
return | |
self.updateAnchorXposWindow() | |
def glyphEditorGlyphDidChangeAnchors(self, info): | |
print('glyphEditorGlyphDidChangeAnchors', info) | |
self.glyph = info['glyph'] | |
if self.glyph is None: | |
return | |
self.updateAnchorXposWindow() | |
def updateAnchorXposWindow(self): | |
# remove old sliders | |
for added in self.added_sliders: | |
if hasattr(self.w, added): | |
delattr(self.w, added) | |
glyph = CurrentGlyph() | |
y = 5 | |
self.added_sliders = [] | |
if glyph is not None: | |
# resize window | |
if len(glyph.anchors) > 0: | |
# ideally sort by y position | |
for i, anchor in enumerate(glyph.anchors): | |
this = Group([0, y, -0, self.unit]) | |
this.label = TextBox((-90,2,-8,-0), anchor.name) | |
this.slider = okSlider((8,0,-100,-0), | |
minValue=0, | |
maxValue=glyph.width, | |
value=anchor.x, | |
anchor=anchor, | |
callback=self.sliderCallback) | |
name = 'anchor_%s' % i | |
setattr(self.w, name, this) | |
self.added_sliders.append(name) | |
y += self.unit | |
(wminx, wminy), (areaW, areaH) = self.w.getNSWindow().frame() | |
h = 40 + len(glyph.anchors)*self.unit | |
posSize = (wminx, wminy), (self.size[0], h) | |
self.w.getNSWindow().setFrame_display_animate_(posSize, True, False) | |
def sliderCallback(self, sender): | |
# if NSEvent.modifierFlags() & NSShiftKeyMask: | |
# print('reduce scale of drag') | |
newx = int(sender.get()) | |
sender.anchor.x = newx | |
class okSlider(Slider): | |
def __init__(self, *args, **kwargs): | |
self.anchor = kwargs['anchor'] | |
del kwargs['anchor'] | |
super(okSlider, self).__init__(*args, **kwargs) | |
if __name__ == '__main__': | |
registerRoboFontSubscriber(anchorHelperXpos) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment