Permalink
Browse files

Added geometry functions: utile.convertPoint

  • Loading branch information...
koenbok committed Jan 14, 2013
1 parent 53fd22e commit c1ab12d1c27919751e4e5463139e3f8a4370bcb0
Showing with 94 additions and 56 deletions.
  1. +8 −0 src/primitives/frame.coffee
  2. +40 −46 src/utils.coffee
  3. +46 −10 src/views/view.coffee
@@ -58,6 +58,14 @@ class Frame extends EventEmitter
throw Error "Width is zero" if @height is 0
@y = value - @height
merge: (r2) ->
r1 = @
frame =
x: Math.min(r1.x, r2.x)
y: Math.min(r1.y, r2.y)
width: Math.max(r1.width, r2.width)
height: Math.max(r1.height, r2.height)
return new Frame frame
Frame.Properties =
x: 0
@@ -1,5 +1,5 @@
Function::define = (prop, desc) ->
Object.defineProperty(@prototype, prop, desc)
Object.defineProperty @prototype, prop, desc
Object.__
exports.keys = (a) ->
@@ -61,8 +61,7 @@ exports.interval = (time, f) ->
exports.remove = (a, e) ->
a.splice(t,1)[0] if (t = a.indexOf(e)) > -1
a
exports.debounce = (func, threshold, execAsap) ->
timeout = null
(args...) ->
@@ -85,29 +84,46 @@ exports.throttle = (fn, delay) ->
setTimeout (-> timer = false), delay unless delay is -1
fn arguments...
exports.convertPoint = (point, view1, view2) ->
point = exports.extend {}, point
traverse = (view) ->
currentView = view
superViews = []
while currentView and currentView.superView
superViews.push currentView.superView
currentView = currentView.superView
return superViews
superViews1 = traverse view1
superViews2 = traverse view2
superViews2.push view2 if view2
for view in superViews1
point.x += view.x
point.y += view.y
# exports.copy = (a, propertyList) ->
# b = {}
# if propertyList
# for key in propertyList
# b[key] = a[key] if a[key] isnt null
# else
# for key, value of a
# b[key] = value if value isnt null
# return b
# a =
# x: 0
# y: 0
#
# b =
# x: 10
#
# c =
# y: 10
#
# console.log exports.filter a, (k, v) -> k in ["x"]
if view.scrollFrame
point.x -= view.scrollFrame.x
point.y -= view.scrollFrame.y
for view in superViews2
point.x -= view.x
point.y -= view.y
if view.scrollFrame
point.x += view.scrollFrame.x
point.y += view.scrollFrame.y
# console.log "superViewsB1", (v.name for v in superViews1)
# console.log "superViewsB2", (v.name for v in superViews2)
return point
exports.max = (obj) ->
for n in obj
@@ -124,25 +140,3 @@ exports.sum = (a) ->
a.reduce (x, y) -> x + y
else
0
# exports.clone = (obj) ->
# if not obj? or typeof obj isnt 'object'
# return obj
# if obj instanceof Date
# return new Date(obj.getTime())
# if obj instanceof RegExp
# flags = ''
# flags += 'g' if obj.global?
# flags += 'i' if obj.ignoreCase?
# flags += 'm' if obj.multiline?
# flags += 'y' if obj.sticky?
# return new RegExp(obj.source, flags)
# newInstance = new obj.constructor()
# for key of obj
# newInstance[key] = exports.clone obj[key]
# return newInstance
@@ -28,9 +28,9 @@ class View extends Frame
# @addClass "textureBacked"
# @addClass "animated"
@properties = args
@clip = args.clip or View.Properties.clip
@clip = true
@properties = args
if not args.superView
@_insertElement()
@@ -44,7 +44,15 @@ class View extends Frame
_postCreate: ->
# Helpers
@define "name"
get: ->
@_name or @id
set: (value) ->
@_name = value
@_element.setAttribute "name", @_name
@define "properties"
get: ->
p = {}
@@ -55,7 +63,8 @@ class View extends Frame
set: (args) ->
for key, value of View.Properties
@[key] = args[key] if args[key]
if args[key] not in [null, undefined]
@[key] = args[key]
for key, value of Frame.CalculatedProperties
@[key] = args[key] if args[key] not in [null, undefined]
@@ -65,6 +74,7 @@ class View extends Frame
@define "frame"
get: -> new Frame {x:@x, y:@y, width:@width, height:@height}
set: (value) ->
return if not value
for p in ["x", "y", "width", "height"]
@[p] = value[p]
@@ -135,7 +145,15 @@ class View extends Frame
@__matrix = m
return @_element.style.webkitTransform = value
convertPoint: (point) ->
# Convert a point on screen to this views coordinate system
utils.convertPoint point, null, @
screenFrame: ->
# Get this views absolute frame on the screen
utils.convertPoint @frame, @, null
# Scale, Opacity
@define "opacity"
@@ -160,23 +178,35 @@ class View extends Frame
@define "clip"
get: ->
@_clip or true
set: (value) ->
@_clip
set: (value) ->
@_clip = value
@style.overflow = "hidden" if value is true
@style.overflow = "visible" if value is false
@emit "change:clip"
@define "visible"
get: ->
@_visible
set: (value) ->
@_visible = value
@style.display = "block" if value is true
@style.display = "none" if value is false
@emit "change:visible"
# Hierarchy
removeFromSuperview: ->
@_superView = null
@superView = null
@define "superView"
get: -> @_superView or null
set: (value) ->
return if value is @_superView
document.removeEventListener "DOMContentLoaded", @__insertElement
# Remove from previous superview subviews
if @_superView
@_superView._element.removeChild @_element
@@ -185,6 +215,8 @@ class View extends Frame
if value
value._element.appendChild @_element
value._subViews.push @
else
@__insertElement()
@_superView = value
@emit "change:superView"
@@ -278,8 +310,10 @@ class View extends Frame
@emit "change:class"
_insertElement: ->
document.addEventListener "DOMContentLoaded", =>
document.body.appendChild @_element
document.addEventListener "DOMContentLoaded", @__insertElement
__insertElement: =>
document.body.appendChild @_element
# Dom element events
@@ -305,13 +339,15 @@ class View extends Frame
View.Properties = utils.extend Frame.Properties,
frame: null
clip: true
scale: 1.0
opacity: 1.0
rotation: 0
style: null
html: null
class: ""
superView: null
visible: true
View.Views = []

0 comments on commit c1ab12d

Please sign in to comment.