diff --git a/framer/Layer.coffee b/framer/Layer.coffee index 1e3e7c426..68199b2a5 100644 --- a/framer/Layer.coffee +++ b/framer/Layer.coffee @@ -303,28 +303,55 @@ class exports.Layer extends BaseClass ############################################################## # Geometry + # @define "point", + # get: -> _.pick(@, ["x", "y"]) + # set: (point) -> + # return if not point + # point = {x: point, y: point} if _.isNumber(point) + # for k in ["x", "y"] + # @[k] = point[k] if point.hasOwnProperty(k) + + # @define "size", + # get: -> _.pick(@, ["width", "height"]) + # set: (size) -> + # return if not size + # size = {width: size, height: size} if _.isNumber(size) + # for k in ["width", "height"] + # @[k] = size[k] if size.hasOwnProperty(k) + + # @define "frame", + # get: -> _.pick(@, ["x", "y", "width", "height"]) + # set: (frame) -> + # return if not frame + # for k in ["x", "y", "width", "height"] + # @[k] = frame[k] if frame.hasOwnProperty(k) + + _setGeometryValues: (input, keys) -> + + # If this is a number, we set everything to that number + if _.isNumber(input) + for k in keys + @[k] = input + else + + # If there is nothing to work with we exit + return unless input + + # Set every numeric value for eacht key + for k in keys + @[k] = input[k] if _.isNumber(input[k]) + @define "point", - get: -> _.pick(@, ["x", "y"]) - set: (point) -> - return if not point - point = {x: point, y: point} if _.isNumber(point) - for k in ["x", "y"] - @[k] = point[k] if point.hasOwnProperty(k) + get: -> Utils.point(@) + set: (input) -> @_setGeometryValues(input, ["x", "y"]) @define "size", - get: -> _.pick(@, ["width", "height"]) - set: (size) -> - return if not size - size = {width: size, height: size} if _.isNumber(size) - for k in ["width", "height"] - @[k] = size[k] if size.hasOwnProperty(k) + get: -> Utils.size(@) + set: (input) -> @_setGeometryValues(input, ["width", "height"]) @define "frame", - get: -> _.pick(@, ["x", "y", "width", "height"]) - set: (frame) -> - return if not frame - for k in ["x", "y", "width", "height"] - @[k] = frame[k] if frame.hasOwnProperty(k) + get: -> Utils.frame(@) + set: (input) -> @_setGeometryValues(input, ["x", "y", "width", "height"]) @define "minX", importable: true diff --git a/framer/Utils.coffee b/framer/Utils.coffee index 59464f180..dc88808bc 100644 --- a/framer/Utils.coffee +++ b/framer/Utils.coffee @@ -577,6 +577,21 @@ Utils.loadImage = (url, callback, context) -> # Point +Utils.point = (input) -> + + return Utils.pointZero(input) if _.isNumber(input) + return Utils.pointZero() unless input + + result = Utils.sizeZero() + + for k in ["x", "y"] + result[k] = input[k] if _.isNumber(input[k]) + + return result + +Utils.pointZero = (n=0) -> + return {x:n, y:n} + Utils.pointDivide = (point, fraction) -> return point = x: point.x / fraction @@ -592,9 +607,6 @@ Utils.pointSubtract = (pointA, pointB) -> x: pointA.x - pointB.x y: pointA.y - pointB.y -Utils.pointZero = (args={}) -> - return _.defaults(args, {x:0, y:0}) - Utils.pointMin = -> points = Utils.arrayFromArguments arguments point = @@ -646,8 +658,20 @@ Utils.pointAngle = (pointA, pointB) -> # Size -Utils.sizeZero = (args={}) -> - return _.defaults(args, {width:0, height:0}) +Utils.size = (input) -> + + return Utils.sizeZero(input) if _.isNumber(input) + return Utils.sizeZero() unless input + + result = Utils.sizeZero() + + for k in ["width", "height"] + result[k] = input[k] if _.isNumber(input[k]) + + return result + +Utils.sizeZero = (n=0)-> + return {width:n, height:n} Utils.sizeMin = -> sizes = Utils.arrayFromArguments arguments @@ -709,8 +733,20 @@ Utils.frameGetMaxY = (frame) -> Utils.frameSetMaxY = (frame, value) -> frame.y = if frame.height is 0 then 0 else value - frame.height -Utils.frameZero = (args={}) -> - return _.defaults(args, {top:0, right:0, bottom:0, left:0}) +Utils.frame = (input) -> + + return Utils.frameZero(input) if _.isNumber(input) + return Utils.frameZero() unless input + + result = Utils.frameZero() + + for k in ["x", "y", "width", "height"] + result[k] = input[k] if _.isNumber(input[k]) + + return result + +Utils.frameZero = (n=0) -> + return {x:n, y:n} Utils.frameSize = (frame) -> size =