Beispiel #1
0
        /// <summary>
        /// </summary>
        /// <param name="element">
        /// </param>
        /// <param name="noPreventDefault">
        /// </param>
        public override void attachControl(HTMLElement element, bool noPreventDefault = false)
        {
            PositionCoord previousPosition = null;
            var           engine           = this.getEngine();

            if (this._attachedElement != null)
            {
                return;
            }

            this._attachedElement = element;
            if (this._onMouseDown == null)
            {
                this._onMouseDown = (e) =>
                {
                    var evt = (MouseEvent)e;

                    previousPosition = new PositionCoord {
                        x = evt.clientX, y = evt.clientY
                    };
                    if (!noPreventDefault)
                    {
                        evt.preventDefault();
                    }
                };
                this._onMouseUp = (evt) =>
                {
                    previousPosition = null;
                    if (!noPreventDefault)
                    {
                        evt.preventDefault();
                    }
                };
                this._onMouseOut = (evt) =>
                {
                    previousPosition = null;
                    this._keys       = new Array <int>();
                    if (!noPreventDefault)
                    {
                        evt.preventDefault();
                    }
                };
                this._onMouseMove = (e) =>
                {
                    var evt = (MouseEvent)e;

                    if (previousPosition == null && !engine.isPointerLock)
                    {
                        return;
                    }

                    int offsetX;
                    int offsetY;
                    if (!engine.isPointerLock)
                    {
                        offsetX = evt.clientX - previousPosition.x;
                        offsetY = evt.clientY - previousPosition.y;
                    }
                    else
                    {
                        offsetX = evt.offsetX;
                        offsetY = evt.offsetY;
                    }

                    this.cameraRotation.y += offsetX / this.angularSensibility;
                    this.cameraRotation.x += offsetY / this.angularSensibility;
                    previousPosition       = new PositionCoord {
                        x = evt.clientX, y = evt.clientY
                    };
                    if (!noPreventDefault)
                    {
                        evt.preventDefault();
                    }
                };
                this._onKeyDown = (e) =>
                {
                    var evt = (KeyboardEvent)e;

                    if (this.keysUp.IndexOf(evt.keyCode) != -1 || this.keysDown.IndexOf(evt.keyCode) != -1 || this.keysLeft.IndexOf(evt.keyCode) != -1 ||
                        this.keysRight.IndexOf(evt.keyCode) != -1)
                    {
                        var index = this._keys.IndexOf(evt.keyCode);
                        if (index == -1)
                        {
                            this._keys.Add(evt.keyCode);
                        }

                        if (!noPreventDefault)
                        {
                            evt.preventDefault();
                        }
                    }
                };
                this._onKeyUp = (e) =>
                {
                    var evt = (KeyboardEvent)e;

                    if (this.keysUp.IndexOf(evt.keyCode) != -1 || this.keysDown.IndexOf(evt.keyCode) != -1 || this.keysLeft.IndexOf(evt.keyCode) != -1 ||
                        this.keysRight.IndexOf(evt.keyCode) != -1)
                    {
                        var index = this._keys.IndexOf(evt.keyCode);
                        if (index >= 0)
                        {
                            this._keys.RemoveAt(index);
                        }

                        if (!noPreventDefault)
                        {
                            evt.preventDefault();
                        }
                    }
                };
                this._onLostFocus = (e) => { this._keys = new Array <int>(); };
                this._reset       = () =>
                {
                    this._keys           = new Array <int>();
                    previousPosition     = null;
                    this.cameraDirection = new Vector3(0, 0, 0);
                    this.cameraRotation  = new Vector2(0, 0);
                };
            }

            element.addEventListener("mousedown", this._onMouseDown, false);
            element.addEventListener("mouseup", this._onMouseUp, false);
            element.addEventListener("mouseout", this._onMouseOut, false);
            element.addEventListener("mousemove", this._onMouseMove, false);
            Tools.RegisterTopRootEvents(
                new Array <EventDts>(
                    new EventDts {
                name = "keydown", handler = this._onKeyDown
            },
                    new EventDts {
                name = "keyup", handler = this._onKeyUp
            },
                    new EventDts {
                name = "blur", handler = this._onLostFocus
            }));
        }
        /// <summary>
        /// </summary>
        /// <param name="element">
        /// </param>
        /// <param name="noPreventDefault">
        /// </param>
        public override void attachControl(HTMLElement element, bool noPreventDefault = false)
        {
            PositionCoord previousPosition = null;
            var           pointerId        = 0;

            if (this._attachedElement != null)
            {
                return;
            }

            this._attachedElement = element;
            var engine = this.getEngine();

            if (this._onPointerDown == null)
            {
                this._onPointerDown = (e) =>
                {
                    if (pointerId > 0)
                    {
                        return;
                    }

                    var evt = (PointerEvent)e;
                    pointerId = evt.pointerId;

                    previousPosition = new PositionCoord {
                        x = evt.clientX, y = evt.clientY
                    };
                    if (!noPreventDefault)
                    {
                        evt.preventDefault();
                    }
                };
                this._onPointerUp = (e) =>
                {
                    var evt = (PointerEvent)e;

                    previousPosition = null;
                    pointerId        = 0;
                    if (!noPreventDefault)
                    {
                        evt.preventDefault();
                    }
                };
                this._onPointerMove = (e) =>
                {
                    var evt = (PointerEvent)e;

                    if (previousPosition == null)
                    {
                        return;
                    }

                    if (pointerId != evt.pointerId)
                    {
                        return;
                    }

                    var offsetX = evt.clientX - previousPosition.x;
                    var offsetY = evt.clientY - previousPosition.y;
                    this.inertialAlphaOffset -= offsetX / this.angularSensibility;
                    this.inertialBetaOffset  -= offsetY / this.angularSensibility;
                    previousPosition          = new PositionCoord {
                        x = evt.clientX, y = evt.clientY
                    };
                    if (!noPreventDefault)
                    {
                        evt.preventDefault();
                    }
                };
                this._onMouseMove = (e) =>
                {
                    if (!engine.isPointerLock)
                    {
                        return;
                    }

                    var evt = (MouseEvent)e;

                    var offsetX = evt.offsetX;
                    var offsetY = evt.offsetY;
                    this.inertialAlphaOffset -= offsetX / this.angularSensibility;
                    this.inertialBetaOffset  -= offsetY / this.angularSensibility;
                    if (!noPreventDefault)
                    {
                        evt.preventDefault();
                    }
                };
                this._wheel = (e) =>
                {
                    var _event = (MouseWheelEvent)e;

                    var delta = 0.0;
                    delta = _event.wheelDelta / (this.wheelPrecision * 40);
                    if (delta > 0.0)
                    {
                        this.inertialRadiusOffset += delta;
                    }

                    if (!noPreventDefault)
                    {
                        _event.preventDefault();
                    }
                };
                this._onKeyDown = (e) =>
                {
                    var evt = (KeyboardEvent)e;

                    if (this.keysUp.IndexOf(evt.keyCode) != -1 || this.keysDown.IndexOf(evt.keyCode) != -1 || this.keysLeft.IndexOf(evt.keyCode) != -1 ||
                        this.keysRight.IndexOf(evt.keyCode) != -1)
                    {
                        var index = this._keys.IndexOf(evt.keyCode);
                        if (index == -1)
                        {
                            this._keys.Add(evt.keyCode);
                        }

                        evt.preventDefault();
                    }
                };
                this._onKeyUp = (e) =>
                {
                    var evt = (KeyboardEvent)e;

                    if (this.keysUp.IndexOf(evt.keyCode) != -1 || this.keysDown.IndexOf(evt.keyCode) != -1 || this.keysLeft.IndexOf(evt.keyCode) != -1 ||
                        this.keysRight.IndexOf(evt.keyCode) != -1)
                    {
                        var index = this._keys.IndexOf(evt.keyCode);
                        if (index >= 0)
                        {
                            this._keys.RemoveAt(index);
                        }

                        evt.preventDefault();
                    }
                };
                this._onLostFocus = (e) =>
                {
                    this._keys = new Array <int>();
                    pointerId  = 0;
                };

                /*
                 * this._onGestureStart = (e) =>
                 * {
                 *  if (window.MSGesture == null)
                 *  {
                 *      return;
                 *  }
                 *  if (this._MSGestureHandler == null)
                 *  {
                 *      this._MSGestureHandler = new MSGesture();
                 *      this._MSGestureHandler.target = element;
                 *  }
                 *  this._MSGestureHandler.addPointer(e.pointerId);
                 * };
                 * this._onGesture = (e) =>
                 * {
                 *  this.radius *= e.scale;
                 *  if (!noPreventDefault)
                 *  {
                 *      e.stopPropagation();
                 *      e.preventDefault();
                 *  }
                 * };
                 */
                this._reset = () =>
                {
                    this._keys = new Array <int>();
                    this.inertialAlphaOffset  = 0;
                    this.inertialBetaOffset   = 0;
                    this.inertialRadiusOffset = 0;
                    previousPosition          = null;
                    pointerId = 0;
                };
            }

            element.addEventListener(this.eventPrefix + "down", this._onPointerDown, false);
            element.addEventListener(this.eventPrefix + "up", this._onPointerUp, false);
            element.addEventListener(this.eventPrefix + "out", this._onPointerUp, false);
            element.addEventListener(this.eventPrefix + "move", this._onPointerMove, false);
            element.addEventListener("mousemove", this._onMouseMove, false);

            // element.addEventListener("MSPointerDown", this._onGestureStart, false);
            // element.addEventListener("MSGestureChange", this._onGesture, false);
            element.addEventListener("mousewheel", this._wheel, false);
            element.addEventListener("DOMMouseScroll", this._wheel, false);
            Tools.RegisterTopRootEvents(
                new Array <EventDts>(
                    new EventDts {
                name = "keydown", handler = this._onKeyDown
            },
                    new EventDts {
                name = "keyup", handler = this._onKeyUp
            },
                    new EventDts {
                name = "blur", handler = this._onLostFocus
            }));
        }