예제 #1
0
 public void moved()
 {
     onMoved.Invoke();
 }
        private void Update()
        {
            if (active)
            {
                if (this._pressed)
                {
                    ///Debug.Log($"Application.isMobilePlatform {Application.isMobilePlatform} Input.touchSupported: {Input.touchSupported} Application.isEditor {Application.isEditor}");
                    if (Application.isMobilePlatform && Input.touchSupported && !Application.isEditor)
                    {
                        if (this._touchId < 0)
                        {
                            return;
                        }
                        var touch = GetTouch(Input.touches, this._touchId);
                        if (touch.fingerId < 0)
                        {
                            return;
                        }
                        this.handle.transform.position = touch.position; //Input.touches[this._touchId].position;
                    }
                    else
                    {
                        this.handle.transform.position = Input.mousePosition;
                    }

                    //_dir = new Vector2(Mathf.Clamp((this.Handle.transform.position.x - this._defaultPos.x) / speedMul, -1.0f, 1.0f),
                    //                      Mathf.Clamp((this.Handle.transform.position.y - this._defaultPos.y) / speedMul, -1.0f, 1.0f));
                    // Mathf.Clamp throws exceptions on WebGL mobile, rewrite the above without using it

                    var xVal = (this.handle.transform.position.x - this._defaultPos.x) / speedMul;
                    xVal = xVal < -1 ? -1 : xVal;
                    xVal = xVal > 1 ? 1 : xVal;
                    var yVal = (this.handle.transform.position.y - this._defaultPos.y) / speedMul;
                    yVal = yVal < -1 ? -1 : yVal;
                    yVal = yVal > 1 ? 1 : yVal;
                    _dir = new Vector2(xVal, yVal);

                    //invoke the event when moved
                    MovedEvent?.Invoke(_dir);
                    //Debug.Log(_dir);
                }
                else
                {
                    //restore the default position
                    //this.handle.transform.position = this._defaultPos;
                    this.handle.transform.position = this._defaultPos;
                    this._dir = Vector2.zero;
                    //invoke the moved event with a zero Vector2
                    MovedEvent?.Invoke(Vector2.zero);
                }

                // Debug.Log($"[{this.name}] " +
                //$"touches: {Input.touches.Length}," +
                //$"active {this.active} " +
                //$"_pressed {_pressed}, " +
                //$"_touchId {_touchId}, " +
                //$"_startPos {_startPos}, " +
                //$"_dir {_dir}");
            }

            // hack to avoid pointer remaining stucked in wrong positions
            if (Input.touches.Length == 0)
            {
                if (_pressed)
                {
                    Debug.Log($"{this.name} no touches, restoring handle position");
                    //restore the default position
                    this._pressed  = false;
                    this._touchId  = -1;
                    this._startPos = Vector2.zero;
                    this.handle.transform.position = this._defaultPos;
                    this._dir = Vector2.zero;

                    //invoke the moved event with a zero Vector2
                    MovedEvent?.Invoke(Vector2.zero);
                }
            }
        }
 internal virtual void MovedEventSender(MoveStatisticsEventargs args)
 {
     MovedEvent?.Invoke(this, args);
 }