Skip to content

Commit 28046f4

Browse files
committed
Latest local changes for branching.
1 parent a9161ab commit 28046f4

File tree

4 files changed

+497
-0
lines changed

4 files changed

+497
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
###
2+
OrientationController (Orientation + buttons) for touch devices
3+
4+
@class bkcore.OrientationController
5+
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
6+
###
7+
class OrientationController
8+
9+
@isCompatible: ->
10+
return ('DeviceOrientationEvent' of window)
11+
12+
###
13+
Creates a new OrientationController
14+
15+
@param dom DOMElement The element that will listen to touch events
16+
@param registerTouch bool Enable touch detection
17+
@param touchCallback function Callback for touches
18+
###
19+
constructor: (@dom, @registerTouch=true, @touchCallback=null) ->
20+
@active = true
21+
@alpha = 0.0
22+
@beta = 0.0
23+
@gamma = 0.0
24+
@dalpha = null
25+
@dbeta = null
26+
@dgamma = null
27+
@touches = null
28+
29+
window.addEventListener('deviceorientation', ((e)=> @orientationChange(e)), false)
30+
if @registerTouch
31+
@dom.addEventListener('touchstart', ((e)=> @touchStart(e)), false)
32+
@dom.addEventListener('touchend', ((e)=> @touchEnd(e)), false)
33+
34+
###
35+
@private
36+
###
37+
orientationChange: (event) ->
38+
return if not @active
39+
if(@dalpha == null)
40+
console.log "calbrate", event.beta
41+
@dalpha = event.alpha
42+
@dbeta = event.beta
43+
@dgamma = event.gamma
44+
@alpha = event.alpha - @dalpha
45+
@beta = event.beta - @dbeta
46+
@gamma = event.gamma - @dgamma
47+
false
48+
49+
###
50+
@private
51+
###
52+
touchStart: (event) ->
53+
return if not @active
54+
for touch in event.changedTouches
55+
@touchCallback?(on, touch, event)
56+
@touches = event.touches
57+
false
58+
59+
###
60+
@private
61+
###
62+
touchEnd: (event) ->
63+
return if not @active
64+
for touch in event.changedTouches
65+
@touchCallback?(on, touch, event)
66+
@touches = event.touches
67+
false
68+
69+
exports = exports ? @
70+
exports.bkcore ||= {}
71+
exports.bkcore.controllers ||= {}
72+
exports.bkcore.controllers.OrientationController = OrientationController

bkcore.coffee/controllers/OrientationController.js

+130
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
###
2+
TouchController (stick + buttons) for touch devices
3+
Based on the touch demo by Seb Lee-Delisle <http://seb.ly/>
4+
5+
@class bkcore.controllers.TouchController
6+
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
7+
###
8+
class TouchController
9+
10+
@isCompatible: ->
11+
return ('ontouchstart' of document.documentElement);
12+
13+
###
14+
Creates a new TouchController
15+
16+
@param dom DOMElement The element that will listen to touch events
17+
@param stickMargin int The left margin in px for stick detection
18+
@param buttonCallback function Callback for non-stick touches
19+
###
20+
constructor: (@dom, @stickMargin=200, @buttonCallback=null) ->
21+
@active = true
22+
@touches = null
23+
@stickID = -1
24+
@stickPos = new Vec2(0, 0)
25+
@stickStartPos = new Vec2(0, 0)
26+
@stickVector = new Vec2(0, 0)
27+
28+
@dom.addEventListener('touchstart', ((e)=> @touchStart(e)), false)
29+
@dom.addEventListener('touchmove', ((e)=> @touchMove(e)), false)
30+
@dom.addEventListener('touchend', ((e)=> @touchEnd(e)), false)
31+
32+
###
33+
@private
34+
###
35+
touchStart: (event) ->
36+
return if not @active
37+
for touch in event.changedTouches
38+
if @stickID < 0 and touch.clientX < @stickMargin
39+
@stickID = touch.identifier
40+
@stickStartPos.set(touch.clientX, touch.clientY)
41+
@stickPos.copy(@stickStartPos)
42+
@stickVector.set(0, 0)
43+
continue
44+
else
45+
@buttonCallback?(on, touch, event)
46+
@touches = event.touches
47+
false
48+
49+
###
50+
@private
51+
###
52+
touchMove: (event) ->
53+
event.preventDefault()
54+
return if not @active
55+
for touch in event.changedTouches
56+
if @stickID is touch.identifier
57+
@stickPos.set(touch.clientX, touch.clientY)
58+
@stickVector.copy(@stickPos).substract(@stickStartPos)
59+
break
60+
@touches = event.touches
61+
false
62+
63+
###
64+
@private
65+
###
66+
touchEnd: (event) ->
67+
return if not @active
68+
@touches = event.touches
69+
for touch in event.changedTouches
70+
if @stickID is touch.identifier
71+
@stickID = -1
72+
@stickVector.set(0, 0)
73+
break
74+
else
75+
@buttonCallback?(off, touch, event)
76+
false
77+
78+
###
79+
Internal class used for vector2
80+
@class Vec2
81+
@private
82+
###
83+
class Vec2
84+
85+
constructor: (@x = 0, @y = 0) ->
86+
87+
substract: (vec) ->
88+
@x -= vec.x
89+
@y -= vec.y
90+
@
91+
92+
copy: (vec) ->
93+
@x = vec.x
94+
@y = vec.y
95+
@
96+
97+
set: (x, y) ->
98+
@x = x
99+
@y = y
100+
@
101+
102+
###
103+
Exports
104+
@package bkcore
105+
###
106+
exports = exports ? @
107+
exports.bkcore ||= {}
108+
exports.bkcore.controllers ||= {}
109+
exports.bkcore.controllers.TouchController = TouchController

0 commit comments

Comments
 (0)