﻿// --------------------------------------------------------------------------------
// Copyright (c) 2015 Ruggero Enrico Visintin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. 
// --------------------------------------------------------------------------------

console.log("MobileInputManager.js included");

function MobileInputManager(target) {

    if (!"ontouchstart" in document.documentElement) {
        console.log.error("MobileInputManager: not touch device")
        return null;
    }

    var mMouseX;
    var mMouseY;

    var mWheelDelta;
    var mMouseHorizontalDelta = 0;
    var mMouseVerticalDelta = 0;

    var touchesCount = 0;

    this.init = function () {
        target.addEventListener('touchstart', handleMouseDown, false);
        target.addEventListener('touchend', handleMouseUp, false);
        target.addEventListener('touchmove', handleMouseMove, false);
        target.addEventListener('gesturechange', handleWheelDelta, false);
    };

    this.postUpdate = function () {
        mWheelDelta             = 0;
        mMouseHorizontalDelta   = 0;
        mMouseVerticalDelta     = 0;
    };

    this.getWheelDelta = function () {
        return mWheelDelta;
    };

    this.getMousePosition = function () {
        return { x: mMouseX, y: mMouseY };
    };

    this.isLeftMouseDown = function () {
        return touchesCount == 1;
    };

    this.isRightMouseDown = function () {
        return touchesCount == 2;
    };

    this.isWheelMouseDown = function () {
        return mWheelMouseDown;
    };

    this.getMouseHorizontalDelta = function () {
        return mMouseHorizontalDelta;
    };

    this.getMouseVerticalDelta = function () {
        return mMouseVerticalDelta;
    };

    var handleMouseDown = function (event) {
        touchesCount++;

        mMouseX = event.touches[0].pageX;
        mMouseY = event.touches[0].pageY;

        event.preventDefault();
    };

    var handleMouseUp = function (event) {
        touchesCount--;
    };

    var handleMouseMove = function (event) {       
        mMouseHorizontalDelta = (mMouseX - event.touches[0].pageX) * 0.035;
        mMouseVerticalDelta = (mMouseY - event.touches[0].pageY) * 0.035;

        if (touchesCount == 2) {
            /*
            mWheelDelta = Math.sqrt(
                (event.touches[0].x - event.touches[1].x) * (event.touches[0].x - event.touches[1].x) +
                (event.touches[0].y - event.touches[1].y) * (event.touches[0].y - event.touches[1].y)) * 0.001 - mWheelDelta;

            if (mWheelDelta != 0) {
                mMouseHorizontalDelta = 0;
                mMouseVerticalDelta = 0;
            }*/
        }
         
        event.preventDefault();
    };

    var handleWheelDelta = function (event) {
        
    };

    return this;
}
@@ -3319,24 +3319,112 @@
 * 0.035;%0D%0A%0D%0A
+        mMouseX = event.touches%5B0%5D.pageX;%0D%0A        mMouseY = event.touches%5B0%5D.pageY;%0D%0A%0D%0A
         if (
@@ -4024,8 +4024,10 @@
 this;%0D%0A%7D
+%0D%0A

Update MobileInputManager.js