Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore.coffee/controllers/OrientationController.coffee
4627 views
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
73