Skip to content

Commit

Permalink
Clean up point, size, frame utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
koenbok committed Apr 30, 2016
1 parent ff8eb4d commit ae6df89
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 24 deletions.
61 changes: 44 additions & 17 deletions framer/Layer.coffee
Expand Up @@ -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
Expand Down
50 changes: 43 additions & 7 deletions framer/Utils.coffee
Expand Up @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit ae6df89

Please sign in to comment.