Skip to content

Commit b7ea7a2

Browse files
committed
Added TouchController class
1 parent caa452b commit b7ea7a2

File tree

3 files changed

+273
-0
lines changed

3 files changed

+273
-0
lines changed

bkcore.coffee/TouchController.coffee

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.TouchController
6+
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
7+
###
8+
class TouchController
9+
10+
###
11+
Creates a new TouchController
12+
13+
@param dom DOMElement The element that will listen to touch events
14+
@param stickMargin int The left margin in px for stick detection
15+
@param buttonCallback function Callback for non-stick touches
16+
###
17+
constructor: (@dom, @stickMargin, @buttonCallback) ->
18+
@active = true
19+
@touches = null
20+
@stickID = -1
21+
@stickPos = new Vec2(0, 0)
22+
@stickStartPos = new Vec2(0, 0)
23+
@stickVector = new Vec2(0, 0)
24+
25+
@dom.addEventListener('touchstart', ((e)=> @touchStart(e)), false)
26+
@dom.addEventListener('touchmove', ((e)=> @touchMove(e)), false)
27+
@dom.addEventListener('touchend', ((e)=> @touchEnd(e)), false)
28+
29+
###
30+
@private
31+
###
32+
touchStart: (event) ->
33+
for i in [0..event.changedTouches.length]
34+
touch = event.changedTouches[i]
35+
if @stickID < 0 and touch.clientX < @stickMargin
36+
@stickID = touch.identifier
37+
@stickStartPos.set(touch.clientX, touch.clientY)
38+
@stickPos.copy(@stickStartPos)
39+
@stickVector.set(0, 0)
40+
continue
41+
else
42+
@buttonCallback?(touch, event)
43+
@touches = event.touches
44+
false
45+
46+
###
47+
@private
48+
###
49+
touchMove: (event) ->
50+
event.preventDefault()
51+
for i in [0..event.changedTouches.length]
52+
touch = event.changedTouches[i]
53+
if @stickID is touch.identifier
54+
@stickPos.set(touch.clientX, touch.clientY)
55+
@stickVector.copy(@stickPos).substract(@stickStartPos)
56+
break
57+
@touches = event.touches
58+
false
59+
60+
###
61+
@private
62+
###
63+
touchEnd: (event) ->
64+
@touches = event.touches
65+
for i in [0..event.changedTouches.length]
66+
touch = event.changedTouches[i]
67+
if @stickID is touch.identifier
68+
@stickID = -1
69+
@stickVector.set(0, 0)
70+
break
71+
false
72+
73+
###
74+
Internal class used for vector2
75+
@class Vec2
76+
@private
77+
###
78+
class Vec2
79+
80+
constructor: (@x = 0, @y = 0) ->
81+
82+
substract: (vec) ->
83+
@x -= vec.x
84+
@y -= vec.y
85+
@
86+
87+
copy: (vec) ->
88+
@x = vec.x
89+
@y = vec.y
90+
@
91+
92+
set: (x, y) ->
93+
@x = x
94+
@y = y
95+
@
96+
97+
###
98+
Exports
99+
@package bkcore
100+
###
101+
exports = exports ? @
102+
exports.bkcore ||= {}
103+
exports.bkcore.TouchController = TouchController

bkcore.coffee/TouchController.js

+162
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

css/touchcontroller.css

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
* {
2+
-webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */
3+
-webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
4+
/* make transparent link selection, adjust last value opacity 0 to 1.0 */
5+
-webkit-tap-highlight-color: rgba(0,0,0,0);
6+
-webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */
7+
-webkit-tap-highlight-color: rgba(0,0,0,0);
8+
}

0 commit comments

Comments
 (0)