SOLVED Adding text to CurrentGlyphView
It should not be a
CanvasGroup, it could also be a
vanilla.Group. In this example it shows how to draw something on top of a glyph view disconnected from the zoom level.
The delegate callbacks are related to a
CanvasGroup. for more info see mojo.canvas.CanvasGroup
and if you want to remove the view from the glyph editor:
jesentanadi last edited by
This post is deleted!
jesentanadi last edited by
Ok, seems that I can just use
vanilla.Groupto set text, since I don't need to draw anything.
(I asked earlier about disconnecting from the zoom level, but I assume that's what
Also, will more documentation be added to the page that lists possible delegate methods?
And one more thing... this is a bit of a side question, but would you ever use a separate object as a delegate?
from vanilla import * from mojo.events import addObserver, removeObserver from mojo.canvas import CanvasGroup class OverlayViewController(object): def __init__(self): addObserver(self, "observerGlyphWindowWillOpen", "glyphWindowWillOpen") addObserver(self, "observerGlyphWindowWillClose", "glyphWindowWillClose") def observerGlyphWindowWillOpen(self, notification): self.window = notification["window"] delegateObj = DelegateObj() self.view = CanvasGroup((0, -200, -0, -0), delegate=delegateObj) self.view.button1 = Button((-130, -40, 120, 22), "Hit Me") self.view.show(True) self.window.addGlyphEditorSubview(self.view) def observerGlyphWindowWillClose(self, notification): self.window.removeGlyphEditorSubview(self.view) removeObserver(self, "glyphWindowWillOpen") removeObserver(self, "glyphWindowWillClose") class DelegateObj: def acceptsFirstResponder(self): return False def shouldDrawBackground(self): return False def draw(self): print('eh') OverlayViewController()
Each view added with
window.addGlyphEditorSubviewwill be detached from zoom level or panning. The other option is to draw inside the glyph canvas in glyph coordinate space.
magnifyWithEventis an event that is called in a Canvas delegate whenever there is a pinch on a trackpad for example.
RafaŁ Buchner last edited by
Maybe this comment is unnecessary, but guys,
I LOVE THIS POST, please add it to the docs, it will be easier to find
gferreira last edited by
okaytype last edited by
Thanks again for this thread and the sample code. I've made a few tools based on it but have run into a problem I can't seem to solve on my own.
If you open more than one glyph window, either from a second font or the same font, the tool breaks in the older windows. Is there a way to make the sample code work with multiple glyph windows open?
I see, you need to keep a dictionary where the window is the key and the view is the item.
glyphWindowWillCloseget the window from the notification, remove the view and del the window item in the dictionary.
Hope this makes sense :)
okaytype last edited by gferreira
Thanks, Frederik. I couldn't figure that out, but I did figure out I could subclass the vanilla elements. I think this approach will work better for what I'm doing.
from vanilla import * from mojo.events import addObserver, removeObserver from AppKit import NSApp, NSColor, NSTextAlignmentCenter, NSTextAlignmentRight, NSTextAlignmentLeft debug = True class OverlayViewController(object): def __init__(self): windowname = 'debug window ui test' if debug == True: self.w = Window((500, 50), windowname) self.w.bind("close", self.windowClose) for window in [w for w in NSApp().orderedWindows() if w.isVisible()]: if window.title() == windowname: window.close() self.w.open() self.view = None addObserver(self, 'addToGlyphView', 'glyphWindowWillOpen') def windowClose(self, sender): print('removing observer') removeObserver(self, "glyphWindowWillOpen") def addToGlyphView(self, notification): self.window = notification["window"] x, y, w, h = xywh = (0, 0, -0, 22) this = self.testGV = Group(xywh) this.text1 = SubClassedEditText(xywh, '-', window=self.window, sizeStyle='mini', continuous=False) self.window.addGlyphEditorSubview(this) class SubClassedEditText(EditText): def __init__(self, *args, **kwargs): self.window = kwargs['window'] del kwargs['window'] super(SubClassedEditText, self).__init__(*args, **kwargs) this = self.getNSTextField() this.setBezeled_(False) this.setBackgroundColor_(NSColor.redColor()) this.setAlignment_(NSTextAlignmentCenter) addObserver(self, "currentGlyphChanged", "currentGlyphChanged") def currentGlyphChanged(self, info): try: g = self.window.getGlyph() if g is None: return f = g.getParent() self.set(f.info.styleName + ' ' + g.name) except: pass def _breakCycles(self): super(SubClassedEditText, self)._breakCycles() removeObserver(self, "currentGlyphChanged") OverlayViewController()