Skip to content

Commit

Permalink
fixed loading globals
Browse files Browse the repository at this point in the history
  • Loading branch information
RafalBuchner committed Jun 26, 2019
1 parent dc6e7bd commit 076625c
Showing 1 changed file with 122 additions and 106 deletions.
228 changes: 122 additions & 106 deletions mirroringPane.roboFontExt/lib/mirroredBackground.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,143 +3,150 @@
from mojo.events import addObserver, removeObserver
from mojo.UI import UpdateCurrentGlyphView
from mojo.drawingTools import *
from AppKit import NSSegmentStyleRoundRect, NSRoundRectBezelStyle
from AppKit import NSSegmentStyleRoundRect, NSRoundRectBezelStyle
from mojo.extensions import getExtensionDefault, setExtensionDefault


def dPoint(scale,p,s=6):
s = s*scale
r = s/2
def dPoint(scale, p, s=6):
s = s * scale
r = s / 2
x, y = p
rect(x-r,y-r,s,s)
rect(x - r, y - r, s, s)

class CustomInspectorExample(object):

class MirrorPane(object):
globalKey = "com.rafalbuchner.mirroringDrawingGlobal"
localkey = "com.rafalbuchner.mirroringDrawingLocal"
mirroringOptions = ["none","hor", "ver", "both"]
mirroringOptions = ["none", "hor", "ver", "both"]
mirroringOptionsDict = [dict(title=option) for option in mirroringOptions]
showOptions = ["fill", "nodes", "stroke"]
showOptionsDict = [dict(title=option) for option in showOptions]
defaultGlobal = dict(drawGlyph=1,showOptions=[0],fillColor=(0,0,1,.5),strokeColor=(0,0,1,.5))
defaultLocal = dict(mirroringOptions=3,offset=(0,0))
def __init__(self):
# subscribe to the moment when an inspector will be shown
defaultGlobal = dict(drawGlyph=1, showOptions=[0], colorFill=(0, 0, 1, .45), colorStroke=(0, 0, 1, .45))
defaultLocal = dict(mirroringOptions=3, offset=(0, 0))

addObserver(self, "inspectorWindowWillShowDescriptions", "inspectorWindowWillShowDescriptions")
addObserver(self, "saveSettingsCallback", "applicationWillTerminate")

self.drawFill = False
self.drawNodes = False
self.drawStroke = False
self.colorFill = (0,0,1)
self.colorStroke = (0,0,1)
def __init__(self):
self.glyph = CurrentGlyph()

self.initUI()
self.addObservers()
addObserver(self, "inspectorWindowWillShowDescriptions", "inspectorWindowWillShowDescriptions")

def initUI(self):
btnH = 20
txtH = 15
x,y,p = 10,10,10
self.view = Group((0,0,-0,-0))
self.view.drawChBox = CheckBox((x,y,-p,btnH), "draw mirrored glyph", callback=self.drawChBoxCallback,sizeStyle="mini")
y += btnH + p/2
self.view.mirroringOptionsTxt = TextBox((x,y,-p,txtH), "mirroring options",sizeStyle="small")
y += txtH + p/2
self.view.mirroringOptions = SegmentedButton((x,y,-p,btnH), self.mirroringOptionsDict, sizeStyle="mini", callback=self.mirroringOptionsCallback)
x, y, p = 10, 10, 10

self.loadSettings()
self.view = Group((0, 0, -0, -0))
self.view.drawChBox = CheckBox((x, y, -p, btnH), "draw mirrored glyph", callback=self.drawChBoxCallback,
sizeStyle="mini")
y += btnH + p / 2
self.view.mirroringOptionsTxt = TextBox((x, y, -p, txtH), "mirroring options", sizeStyle="small")
y += txtH + p / 2
self.view.mirroringOptions = SegmentedButton((x, y, -p, btnH), self.mirroringOptionsDict, sizeStyle="mini",
callback=self.mirroringOptionsCallback)
nsObj = self.view.mirroringOptions.getNSSegmentedButton()
nsObj.setSegmentStyle_(NSSegmentStyleRoundRect)
self.view.mirroringOptions.set([0])
y += p + btnH
self.view.showOptionsTxt = TextBox((x,y,-p,txtH), "show",sizeStyle="small")
y += txtH + p/2
self.view.showOptions = SegmentedButton((x,y,-p,btnH), self.showOptionsDict, sizeStyle="mini",callback=self.showOptionsCallback,selectionStyle="any")
self.view.showOptionsTxt = TextBox((x, y, -p, txtH), "show", sizeStyle="small")
y += txtH + p / 2
self.view.showOptions = SegmentedButton((x, y, -p, btnH), self.showOptionsDict, sizeStyle="mini",
callback=self.showOptionsCallback, selectionStyle="any")
nsObj = self.view.showOptions.getNSSegmentedButton()
nsObj.setSegmentStyle_(NSSegmentStyleRoundRect)
y += p + btnH
self.view.offsetTxt = TextBox((x,y,-p,txtH), "offset",sizeStyle="small")
y += txtH + p/2
self.view.offsetX = RBEditText((x,y,50,txtH), 0, sizeStyle="mini",placeholder="x value",callback=self.txtXCallback)
self.view.offsetY = RBEditText((x+50,y,50,txtH), 0, sizeStyle="mini",placeholder="y value",callback=self.txtYCallback)
y += btnH + p/2
self.view.fillTxt = TextBox((x,y,-p,txtH), "fill",sizeStyle="small")
self.view.fillColor = ColorWell((x+50, y, 50, txtH),
callback=self.colorFillEdit, color=rgb2NSColor(self.colorFill))
y += txtH + p/2
self.view.strokeTxt = TextBox((x,y,-p,txtH), "stroke",sizeStyle="small")
self.view.strokeColor = ColorWell((x+50, y, 50, txtH),
callback=self.colorStrokeEdit, color=rgb2NSColor(self.colorStroke))
self.view.offsetTxt = TextBox((x, y, -p, txtH), "offset", sizeStyle="small")
y += txtH + p / 2
self.view.offsetX = RBEditText((x, y, 50, txtH), 0, sizeStyle="mini", placeholder="x value",
callback=self.txtXCallback)
self.view.offsetY = RBEditText((x + 50, y, 50, txtH), 0, sizeStyle="mini", placeholder="y value",
callback=self.txtYCallback)
y += btnH + p / 2
self.view.fillTxt = TextBox((x, y, -p, txtH), "fill", sizeStyle="small")
self.view.colorFillCW = ColorWell((x + 50, y, 50, txtH),
callback=self.colorFillEdit, color=rgb2NSColor(self.colorFill))
y += txtH + p / 2
self.view.strokeTxt = TextBox((x, y, -p, txtH), "stroke", sizeStyle="small")
self.view.colorStroke = ColorWell((x + 50, y, 50, txtH),
callback=self.colorStrokeEdit, color=rgb2NSColor(self.colorStroke))
y += txtH + p
self.view.exportToLayer = Button((x,y,-p,btnH), "export to layer", sizeStyle="mini",callback=self.exportToLayerCallback)
self.view.exportToLayer = Button((x, y, -p, btnH), "export to layer", sizeStyle="mini",
callback=self.exportToLayerCallback)
nsObj = self.view.exportToLayer.getNSButton()
nsObj.setBezelStyle_(NSRoundRectBezelStyle)
y += btnH + p
self.height = y
self.loadSettings()
self.initCallbacksUI()

self.initLocalsUI()
self.initGlobalsUI()
self.loadGlyph()
self.intiCallbackRadios()
self.addObservers()

def determineMirroringOption(self):
if self.mirroringType == "none":
self.reflectionMatrix = [1,0,0,1,0,0]
self.reflectionMatrix = [1, 0, 0, 1, 0, 0]
if self.mirroringType == "both":
self.reflectionMatrix = [-1,0,0,-1,0,0]
self.reflectionMatrix = [-1, 0, 0, -1, 0, 0]
if self.mirroringType == "ver":
self.reflectionMatrix = [1,0,0,-1,0,0]
self.reflectionMatrix = [1, 0, 0, -1, 0, 0]
if self.mirroringType == "hor":
self.reflectionMatrix = [-1,0,0,1,0,0]
self.reflectionMatrix = [-1, 0, 0, 1, 0, 0]
# else:
# self.reflectionMatrix = [1,0,0,1,0,0]


def addObservers(self):
addObserver(self, "saveSettingsCallback", "fontWillClose")
addObserver(self, "currentGlyphChangedCallback", "currentGlyphChanged")
addObserver(self, "currentGlyphChangedCallback", "glyphWindowDidOpen")
addObserver(self, "drawBackgroundCallback", "drawBackground")

def removeObservers(self):
removeObserver(self, "inspectorWindowWillShowDescriptions")
removeObserver(self, "fontWillClose")
removeObserver(self, "currentGlyphChanged")
removeObserver(self, "glyphWindowDidOpen")
removeObserver(self, "drawBackground")

def initCallbacksUI(self):
def initLocalsUI(self):
self.txtXCallback(self.view.offsetX)
self.txtYCallback(self.view.offsetY)
self.colorFillEdit(self.view.fillColor)
self.colorStrokeEdit(self.view.strokeColor)
self.drawChBoxCallback(self.view.drawChBox)
# self.colorFillEdit(self.view.colorFillCW)
# self.colorStrokeEdit(self.view.colorStroke)
# self.drawChBoxCallback(self.view.drawChBox)

def intiCallbackRadios(self):
self.mirroringType = "none"

def drawAction(self, scale):
def _drawGlyph():
if self.drawNodes:
fill( *self.colorStroke )
fill(*self.colorStroke)
stroke(None)
for c in self.glyph:
for p in c.points:
dPoint(scale,p.position)
dPoint(scale, p.position)

strokeWidth(1 * scale)
if self.drawStroke: stroke( *self.colorStroke )
else: stroke( None )
if self.drawFill: fill ( *self.colorFill )
else: fill ( None )
if self.drawStroke:
stroke(*self.colorStroke)
else:
stroke(None)
if self.drawFill:
fill(*self.colorFill)
else:
fill(None)
drawGlyph(self.glyph)


x, y = self.drawingMeasurements

translate(x+self.offsetX,y+self.offsetY)
translate(x + self.offsetX, y + self.offsetY)
self.determineMirroringOption()

transform(self.reflectionMatrix)
translate(-x,-y)

translate(-x, -y)

_drawGlyph()


def loadGlyph(self):
if self.glyph is not None:
offset = (self.offsetX, self.offsetY)
Expand All @@ -149,11 +156,11 @@ def loadGlyph(self):
else:
# I will need to find another solution without exceptions:
try:
self.glyph.lib[self.localkey] = dict(offset=offset, mirroringOptions=self.view.mirroringOptions.get())
self.glyph.lib[self.localkey] = dict(offset=offset,
mirroringOptions=self.view.mirroringOptions.get())
except:
self.glyph.lib[self.localkey] = dict(offset=offset, mirroringOptions=[3])


self.glyph = CurrentGlyph()

if self.glyph is not None:
Expand All @@ -173,60 +180,57 @@ def loadGlyph(self):
self.txtXCallback(self.view.offsetX)
self.txtYCallback(self.view.offsetY)



def saveSettings(self):
settings = dict(
drawGlyph=self.view.drawChBox.get(),
showOptions=self.view.showOptions.get(),
fillColor=nsColor2RGB(self.view.fillColor.get()),
strokeColor=nsColor2RGB(self.view.strokeColor.get()),
colorFill=nsColor2RGB(self.view.colorFillCW.get()),
colorStroke=nsColor2RGB(self.view.colorStroke.get()),
)
setExtensionDefault(self.globalKey, settings)

def loadSettings(self):
print(getExtensionDefault(self.globalKey))
if getExtensionDefault(self.globalKey) is None:
self.settings = self.defaultGlobal
else:
self.settings = getExtensionDefault(globalKey)
self.settings = getExtensionDefault(self.globalKey)
drawGlyph = self.settings.get("drawGlyph")
showOptions = self.settings.get("showOptions")
fillColor = self.settings.get("fillColor")
strokeColor = self.settings.get("strokeColor")

colorFill = self.settings.get("colorFill")
colorStroke = self.settings.get("colorStroke")
if drawGlyph is not None:
self.view.drawChBox.set(drawGlyph)
self.drawGlyph = drawGlyph
else:
self.view.drawChBox.set( self.defaultGlobal['drawGlyph'] )

self.drawGlyph = self.defaultGlobal['drawGlyph']
if showOptions is not None:
self.view.showOptions.set(showOptions)
self.showOptions = showOptions
else:
self.view.showOptions.set( self.defaultGlobal['showOptions'] )

if fillColor is not None:
self.view.fillColor.set( rgb2NSColor(fillColor) )
self.showOptions = self.defaultGlobal['showOptions']
if colorFill is not None:
self.colorFill = colorFill
else:
self.view.fillColor.set( rgb2NSColor(self.defaultGlobal['fillColor']) )

if strokeColor is not None:
self.view.strokeColor.set( rgb2NSColor(strokeColor) )
self.colorFill = self.defaultGlobal['colorFill']
if colorStroke is not None:
self.colorStroke = colorStroke
else:
self.view.strokeColor.set( rgb2NSColor(self.defaultGlobal['strokeColor']) )
self.colorFillEdit(self.view.fillColor)
self.colorStrokeEdit(self.view.strokeColor)
self.colorStroke = self.defaultGlobal['colorStroke']

def initGlobalsUI(self):
self.view.drawChBox.set(self.drawGlyph)
self.view.showOptions.set(self.showOptions)
self.view.colorFillCW.set(rgb2NSColor(self.colorFill))
self.view.colorStroke.set(rgb2NSColor(self.colorStroke))
self.colorFillEdit(self.view.colorFillCW)
self.colorStrokeEdit(self.view.colorStroke)
self.drawChBoxCallback(self.view.drawChBox)
self.showOptionsCallback(self.view.showOptions)





# UI callbacks
def exportToLayerCallback(self,sender):
def exportToLayerCallback(self, sender):
pass

def colorFillEdit(self,sender):
def colorFillEdit(self, sender):
color = sender.get()
if isinstance(color, tuple):
self.colorFill = sender.get()
Expand Down Expand Up @@ -274,12 +278,12 @@ def mirroringOptionsCallback(self, sender):
UpdateCurrentGlyphView()

def drawChBoxCallback(self, sender):
self.drawGlyph = False
self.drawGlyph = 0
if sender.get() == 1:
self.drawGlyph = True
self.addObservers()
else:
self.removeObservers()
self.drawGlyph = 1
# self.addObservers()
# else:
# self.removeObservers()
UpdateCurrentGlyphView()

# observer events
Expand All @@ -290,16 +294,16 @@ def currentGlyphChangedCallback(self, info):
self.loadGlyph()

def drawBackgroundCallback(self, info):
if self.glyph is not None and self.drawGlyph:
if self.glyph is not None and self.drawGlyph == 1:
scale = info['scale']
save()
self.drawAction(scale)
restore()

@property
def drawingMeasurements(self):
_x,_y,x_,y_ = self.glyph.bounds
self.__drawingMeasurements = ((x_-_x)/2+_x,(y_-_y)/2+_y)
_x, _y, x_, y_ = self.glyph.bounds
self.__drawingMeasurements = ((x_ - _x) / 2 + _x, (y_ - _y) / 2 + _y)
return self.__drawingMeasurements

########## stuff
Expand All @@ -311,6 +315,18 @@ def inspectorWindowWillShowDescriptions(self, notification):
notification["descriptions"].insert(4, item)


mirrorPane = MirrorPane()
test = True
if test:
class Win:
def __init__(self, MirrorPane):
self.pane = MirrorPane
self.w = FloatingWindow((100, 100, 100, 100))
self.w.bind("close", self.close)
self.w.open()

def close(self, sender):
self.pane.removeObservers()


CustomInspectorExample()
Win(mirrorPane)

0 comments on commit 076625c

Please sign in to comment.