I’d like to make the
def drawTextAtPoint( self, text, textPosition, fontSize=10.0, fontColor=NSColor.colorWithCalibratedRed_green_blue_alpha_( 1, 1, 1, 1 ) )
So that it is aligned right to the top right corner of my text view for example. textPosition = (-50, -50)
like with Vanilla doesn’t work?
You Need to get the visibleRect() of the view and calculate the text position from it.
2 Likes
editTabGraphicView = self.controller.graphicView()
# visibleRect = editTabGraphicView.visibleRect()
( ( tX, tY ), ( tWidth, tHeight ) ) = editTabGraphicView.visibleRect()
textPosition = ( tX, tY )
Edit: figured it out, now just have to figure out how to keep it in the same spot… it still moves with the window…
Edit2: Found it! I have to divide the tX, tY by getScale()
Could you please help with this, at 1000Pt it works exaclty as I’d like, but when I zoom in or out, the position messes up: http://quick.as/64gDsQ8D8
See this line:
textPosition = ( ( math.floor(tX)+30 )/self.getScale(), ( (tY + tHeight + 30)/self.getScale() ) )
view = self.controller.graphicView()
Visible = view.visibleRect()
activePosition = view.activePosition()
# bottom left corner
textPosition = NSMakePoint(math.floor((Visible.origin.x + 30 - activePosition.x) / self.getScale()), math.floor((NSMinY(Visible) - activePosition.y) / self.getScale()))
# top left corner
textPosition = NSMakePoint(math.floor((Visible.origin.x + 30 - activePosition.x) / self.getScale()), math.floor((NSMaxY(Visible) - activePosition.y - 30) / self.getScale()))
self.drawTextAtPoint("text", textPosition, fontSize=20.0, fontColor=NSColor.colorWithCalibratedRed_green_blue_alpha_( 0, 0, 1, 1 ) )
...
def drawTextAtPoint( self, text, textPosition, fontSize=9.0, fontColor=NSColor.brownColor() ):
"""
Use self.drawTextAtPoint( "blabla", myNSPoint ) to display left-aligned text at myNSPoint.
"""
try:
glyphEditView = self.controller.graphicView()
currentZoom = self.getScale()
fontAttributes = {
NSFontAttributeName: NSFont.labelFontOfSize_( fontSize/currentZoom ),
NSForegroundColorAttributeName: fontColor }
displayText = NSAttributedString.alloc().initWithString_attributes_( text, fontAttributes )
displayText.drawAtPoint_(textPosition)
except Exception as e:
import traceback
print traceback.format_exc()