Refactor origin swap code to be a bit more traditional. I don't like overriding functions unless necessary.

This commit is contained in:
Simo Kinnunen
2014-10-29 15:29:22 +09:00
parent 33d4c70820
commit f3c61983ea

View File

@@ -95,34 +95,38 @@ module.exports = syrup.serial()
socket.write(command)
}
var getX = function(point) {
return Math.floor(point.x * socket.maxX)
}
var getY = function(point) {
return Math.floor(point.y * socket.maxY)
}
switch (flags.get('forceTouchOrigin', 'top left')) {
case 'bottom left':
// So far the only device we've seen exhibiting this behavior
// is Yoga Tablet 8.
log.info('Touch origin is bottom left')
getX = function(point) {
return Math.floor((1 - point.y) * socket.maxX)
}
getY = function(point) {
return Math.floor(point.x * socket.maxY)
}
break
}
// Usually the touch origin is the same as the display's origin,
// but sometimes it might not be.
var getters = (function(origin) {
log.info('Touch origin is %s', origin)
return {
'top left': {
x: function(point) {
return Math.floor(point.x * socket.maxX)
}
, y: function(point) {
return Math.floor(point.y * socket.maxY)
}
}
// So far the only device we've seen exhibiting this behavior
// is Yoga Tablet 8.
, 'bottom left': {
x: function(point) {
return Math.floor((1 - point.y) * socket.maxX)
},
y: function(point) {
return Math.floor(point.x * socket.maxY)
}
}
}[origin]
})(flags.get('forceTouchOrigin', 'top left'))
plugin.touchDown = function(point) {
send(util.format(
'd %s %s %s %s\n'
, point.contact
, getX(point)
, getY(point)
, getters.x(point)
, getters.y(point)
, Math.floor((point.pressure || 0.5) * socket.maxPressure)
))
}
@@ -131,8 +135,8 @@ module.exports = syrup.serial()
send(util.format(
'm %s %s %s %s\n'
, point.contact
, getX(point)
, getY(point)
, getters.x(point)
, getters.y(point)
, Math.floor((point.pressure || 0.5) * socket.maxPressure)
))
}