Example #1
0
        public Stage()
            : base()
        {
            _inst = this;
            soundVolume = 1;

            _updateContext = new UpdateContext();
            stageWidth = Screen.width;
            stageHeight = Screen.height;
            _frameGotHitTarget = -1;

            touchScreen = Input.touchSupported;

            _touches = new TouchInfo[5];
            for (int i = 0; i < _touches.Length; i++)
                _touches[i] = new TouchInfo();

            if (!touchScreen)
                _touches[0].touchId = 0;

            _rollOutChain = new List<DisplayObject>();
            _rollOverChain = new List<DisplayObject>();

            onStageResized = new EventListener(this, "onStageResized");
            onTouchMove = new EventListener(this, "onTouchMove");
            onCopy = new EventListener(this, "onCopy");
            onPaste = new EventListener(this, "onPaste");

            StageEngine engine = GameObject.FindObjectOfType<StageEngine>();
            if (engine != null)
                this.gameObject = engine.gameObject;
            else
            {
                int layer = LayerMask.NameToLayer(StageCamera.LayerName);

                this.gameObject = new GameObject("Stage");
                this.gameObject.hideFlags = HideFlags.None;
                this.gameObject.layer = layer;
                this.gameObject.AddComponent<StageEngine>();
                this.gameObject.AddComponent<UIContentScaler>();
            }
            this.cachedTransform = gameObject.transform;
            this.cachedTransform.localScale = new Vector3(StageCamera.UnitsPerPixel, StageCamera.UnitsPerPixel, StageCamera.UnitsPerPixel);
            this.gameObject.SetActive(true);
            UnityEngine.Object.DontDestroyOnLoad(this.gameObject);

            EnableSound();

            inputCaret = new InputCaret();
            highlighter = new Highlighter();

            Timers.inst.Add(5, 0, RunTextureCollector);

            #if UNITY_WEBPLAYER || UNITY_WEBGL || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR
            CopyPastePatch.Apply();
            #endif
        }
Example #2
0
        void HandleMouseEvents()
        {
            TouchInfo touch = _touches[0];

            if (touch.x != _touchPosition.x || touch.y != _touchPosition.y)
            {
                touch.x = _touchPosition.x;
                touch.y = _touchPosition.y;
                touch.Move();
            }

            if (touch.lastRollOver != touch.target)
            {
                HandleRollOver(touch);
            }

            if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))
            {
                if (!touch.began)
                {
                    _touchCount = 1;
                    touch.Begin();
                    touch.button = Input.GetMouseButtonDown(2) ? 2 : (Input.GetMouseButtonDown(1) ? 1 : 0);
                    this.focus   = touch.target;

                    touch.UpdateEvent();
                    touch.target.onTouchBegin.BubbleCall(touch.evt);
                }
            }
            if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(2))
            {
                if (touch.began)
                {
                    _touchCount = 0;
                    touch.End();

                    DisplayObject clickTarget = touch.ClickTest();
                    if (clickTarget != null)
                    {
                        touch.UpdateEvent();

                        if (Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(2))
                        {
                            clickTarget.onRightClick.BubbleCall(touch.evt);
                        }
                        else
                        {
                            clickTarget.onClick.BubbleCall(touch.evt);
                        }
                    }

                    touch.button = -1;
                }
            }
        }
Example #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="touchId"></param>
 public void CancelClick(int touchId)
 {
     for (int j = 0; j < 5; j++)
     {
         TouchInfo touch = _touches[j];
         if (touch.touchId == touchId)
         {
             touch.clickCancelled = true;
         }
     }
 }
Example #4
0
 public void RemoveTouchMonitor(EventDispatcher target)
 {
     for (int j = 0; j < 5; j++)
     {
         TouchInfo touch = _touches[j];
         int       i     = touch.touchMonitors.IndexOf(target);
         if (i != -1)
         {
             touch.touchMonitors[i] = null;
         }
     }
 }
Example #5
0
        public void ResetInputState()
        {
            for (int j = 0; j < 5; j++)
            {
                TouchInfo touch = _touches[j];
                touch.Reset();
            }

            if (!touchScreen)
            {
                _touches[0].touchId = 0;
            }
        }
Example #6
0
        void HandleCustomInput()
        {
            Vector2 pos = _customInputPos;

            pos.y = stageHeight - pos.y;
            TouchInfo touch = _touches[0];

            if (touch.x != pos.x || touch.y != pos.y)
            {
                touch.x = pos.x;
                touch.y = pos.y;
                touch.UpdateEvent();
                onTouchMove.Call(touch.evt);

                if (touch.lastRollOver != touch.target)
                {
                    HandleRollOver(touch);
                }
            }

            if (_customInputButtonDown)
            {
                if (!touch.began)
                {
                    _touchCount = 1;
                    touch.begin();
                    this.focus = touch.target;

                    if (touch.target != null)
                    {
                        touch.UpdateEvent();
                        touch.target.onTouchBegin.BubbleCall(touch.evt);
                    }
                }
            }
            else if (touch.began)
            {
                _touchCount = 0;
                touch.end();

                DisplayObject clickTarget = touch.ClickTest();
                if (clickTarget != null)
                {
                    touch.UpdateEvent();
                    clickTarget.onClick.BubbleCall(touch.evt);
                }
            }
        }
Example #7
0
        public void AddTouchMonitor(int touchId, EventDispatcher target)
        {
            TouchInfo touch = null;

            for (int j = 0; j < 5; j++)
            {
                touch = _touches[j];
                if (touchId == -1 && touch.touchId != -1 ||
                    touchId != -1 && touch.touchId == touchId)
                {
                    break;
                }
            }
            if (touch.touchMonitors.IndexOf(target) == -1)
            {
                touch.touchMonitors.Add(target);
            }
        }
Example #8
0
        public Vector2 GetTouchPosition(int touchId)
        {
            if (touchId < 0)
            {
                return(new Vector2(mouseX, mouseY));
            }

            for (int j = 0; j < 5; j++)
            {
                TouchInfo touch = _touches[j];
                if (touch.touchId == touchId)
                {
                    return(new Vector2(touch.x, touch.y));
                }
            }

            return(new Vector2(mouseX, mouseY));
        }
 DisplayObject clickTest(TouchInfo touch)
 {
     if (!touch.clickCancelled &&
         Mathf.Abs(touch.x - touch.downX) < 50 && Mathf.Abs(touch.y - touch.downY) < 50)
     {
         if (touch.downTarget != null && touch.downTarget.stage != null)
         {
             return(touch.downTarget);
         }
         else
         {
             return(touch.target);
         }
     }
     else
     {
         return(null);
     }
 }
Example #10
0
        internal void HandleGUIEvents(Event evt)
        {
            if (evt.rawType == EventType.KeyDown)
            {
                TouchInfo touch = _touches[0];
                touch.keyCode        = evt.keyCode;
                touch.modifiers      = evt.modifiers;
                touch.character      = evt.character;
                InputEvent.shiftDown = (evt.modifiers & EventModifiers.Shift) != 0;

                touch.UpdateEvent();
                DisplayObject f = this.focus;
                if (f != null)
                {
                    f.BubbleEvent("onKeyDown", touch.evt);
                }
                else
                {
                    DispatchEvent("onKeyDown", touch.evt);
                }
            }
            else if (evt.rawType == EventType.KeyUp)
            {
                TouchInfo touch = _touches[0];
                touch.modifiers = evt.modifiers;
            }
#if UNITY_2017_1_OR_NEWER
            else if (evt.type == EventType.ScrollWheel)
#else
            else if (evt.type == EventType.scrollWheel)
#endif
            {
                if (_touchTarget != null)
                {
                    TouchInfo touch = _touches[0];
                    touch.mouseWheelDelta = (int)evt.delta.y;
                    touch.UpdateEvent();
                    _touchTarget.BubbleEvent("onMouseWheel", touch.evt);
                    touch.mouseWheelDelta = 0;
                }
            }
        }
Example #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="touchId"></param>
        /// <returns></returns>
        public Vector2 GetTouchPosition(int touchId)
        {
            UpdateTouchPosition();

            if (touchId < 0)
            {
                return(_touchPosition);
            }

            for (int j = 0; j < 5; j++)
            {
                TouchInfo touch = _touches[j];
                if (touch.touchId == touchId)
                {
                    return(new Vector2(touch.x, touch.y));
                }
            }

            return(_touchPosition);
        }
Example #12
0
        public int[] GetAllTouch(int[] result)
        {
            if (result == null)
            {
                result = new int[_touchCount];
            }
            int i = 0;

            for (int j = 0; j < 5; j++)
            {
                TouchInfo touch = _touches[j];
                if (touch.touchId != -1)
                {
                    result[i++] = touch.touchId;
                    if (i >= result.Length)
                    {
                        break;
                    }
                }
            }
            return(result);
        }
Example #13
0
        internal void HandleGUIEvents(Event evt)
        {
            if (evt.rawType == EventType.KeyDown)
            {
                TouchInfo touch = _touches[0];
                touch.keyCode        = evt.keyCode;
                touch.modifiers      = evt.modifiers;
                touch.character      = evt.character;
                InputEvent.shiftDown = (evt.modifiers & EventModifiers.Shift) != 0;

                touch.UpdateEvent();
                DisplayObject f = this.focus;
                if (f != null)
                {
                    f.onKeyDown.BubbleCall(touch.evt);
                }
                else
                {
                    this.onKeyDown.Call(touch.evt);
                }
            }
            else if (evt.rawType == EventType.KeyUp)
            {
                TouchInfo touch = _touches[0];
                touch.modifiers = evt.modifiers;
            }
            else if (evt.type == EventType.scrollWheel)
            {
                if (_touchTarget != null)
                {
                    TouchInfo touch = _touches[0];
                    touch.mouseWheelDelta = (int)evt.delta.y;
                    touch.UpdateEvent();
                    _touchTarget.onMouseWheel.BubbleCall(touch.evt);
                    touch.mouseWheelDelta = 0;
                }
            }
        }
Example #14
0
        public int GetFirstTouchId()
        {
            if (_frameGotHitTarget != Time.frameCount)
            {
                GetHitTarget();
            }

            for (int j = 0; j < 5; j++)
            {
                TouchInfo touch = _touches[j];
#if UNITY_EDITOR
                if (Input.GetKey("z") && j == touchCount - 1)
                {
                    return(FguiTouchSimulator.simulateTouchPointId);
                }
#endif
                if (touch.touchId != -1)
                {
                    return(touch.touchId);
                }
            }

            return(-1);
        }
Example #15
0
        internal void AddTouchEndMonitor(int touchId, EventDispatcher target)
        {
            TouchInfo touch = null;

            if (touchId == -1)
            {
                touch = _touches[0];
            }
            else
            {
                for (int j = 0; j < 5; j++)
                {
                    touch = _touches[j];
                    if (touch.touchId == touchId)
                    {
                        break;
                    }
                }
            }
            if (touch.touchEndMonitors.IndexOf(target) == -1)
            {
                touch.touchEndMonitors.Add(target);
            }
        }
Example #16
0
        void HandleMouseEvents()
        {
            bool    hitTested     = false;
            Vector2 mousePosition = Input.mousePosition;

            mousePosition.y = stageHeight - mousePosition.y;
            TouchInfo touch = _touches[0];

            if (mousePosition.x >= 0 && mousePosition.y >= 0)
            {
                if (touch.x != mousePosition.x || touch.y != mousePosition.y)
                {
                    mouseX  = mousePosition.x;
                    mouseY  = mousePosition.y;
                    touch.x = mouseX;
                    touch.y = mouseY;

                    _objectUnderMouse = HitTest(mousePosition, true);
                    hitTested         = true;
                    touch.target      = _objectUnderMouse;

                    touch.UpdateEvent();
                    onMouseMove.Call(touch.evt);

                    if (touch.lastRollOver != _objectUnderMouse)
                    {
                        HandleRollOver(touch);
                    }
                }
            }
            else
            {
                mousePosition = new Vector2(mouseX, mouseY);
            }

            if (Input.GetMouseButtonDown(0))
            {
                if (!touch.began)
                {
                    touch.began          = true;
                    touch.clickCancelled = false;
                    touch.downX          = touch.x;
                    touch.downY          = touch.y;

                    if (!hitTested)
                    {
                        _objectUnderMouse = HitTest(mousePosition, true);
                        hitTested         = true;
                        touch.target      = _objectUnderMouse;
                    }

                    this.focus = _objectUnderMouse;

                    if (_objectUnderMouse != null)
                    {
                        touch.UpdateEvent();
                        _objectUnderMouse.onMouseDown.BubbleCall(touch.evt);
                    }
                }
            }
            if (Input.GetMouseButtonUp(0))
            {
                if (touch.began)
                {
                    touch.began = false;

                    if (!hitTested)
                    {
                        _objectUnderMouse = HitTest(mousePosition, true);
                        hitTested         = true;
                        touch.target      = _objectUnderMouse;
                    }

                    if (_objectUnderMouse != null)
                    {
                        touch.UpdateEvent();
                        _objectUnderMouse.onMouseUp.BubbleCall(touch.evt);

                        if (!touch.clickCancelled && Mathf.Abs(touch.x - touch.downX) < 10 && Mathf.Abs(touch.y - touch.downY) < 10)
                        {
                            if (Time.realtimeSinceStartup - touch.lastClickTime < 0.35f)
                            {
                                if (touch.clickCount == 2)
                                {
                                    touch.clickCount = 1;
                                }
                                else
                                {
                                    touch.clickCount++;
                                }
                            }
                            else
                            {
                                touch.clickCount = 1;
                            }
                            touch.lastClickTime = Time.realtimeSinceStartup;
                            touch.UpdateEvent();
                            _objectUnderMouse.onClick.BubbleCall(touch.evt);
                        }
                    }
                }
            }
            if (Input.GetMouseButtonUp(1))
            {
                if (!hitTested)
                {
                    _objectUnderMouse = HitTest(mousePosition, true);
                    hitTested         = true;
                    touch.target      = _objectUnderMouse;
                }

                if (_objectUnderMouse != null)
                {
                    touch.UpdateEvent();
                    _objectUnderMouse.onRightClick.BubbleCall(touch.evt);
                }
            }
        }
Example #17
0
        /// <summary>
        ///
        /// </summary>
        public Stage()
            : base()
        {
            _inst       = this;
            soundVolume = 1;

            _updateContext     = new UpdateContext();
            stageWidth         = Screen.width;
            stageHeight        = Screen.height;
            _frameGotHitTarget = -1;

            if (Application.platform == RuntimePlatform.WindowsPlayer ||
                Application.platform == RuntimePlatform.WindowsEditor ||
                Application.platform == RuntimePlatform.OSXPlayer ||
                Application.platform == RuntimePlatform.OSXEditor)
            {
                touchScreen = false;
            }
            else
            {
                touchScreen = Input.touchSupported && SystemInfo.deviceType != DeviceType.Desktop;
            }

            _touches = new TouchInfo[5];
            for (int i = 0; i < _touches.Length; i++)
            {
                _touches[i] = new TouchInfo();
            }

            if (!touchScreen)
            {
                _touches[0].touchId = 0;
            }

            _rollOutChain  = new List <DisplayObject>();
            _rollOverChain = new List <DisplayObject>();

            onStageResized = new EventListener(this, "onStageResized");
            onTouchMove    = new EventListener(this, "onTouchMove");

            StageEngine engine = GameObject.FindObjectOfType <StageEngine>();

            if (engine != null)
            {
                Object.Destroy(engine.gameObject);
            }

            this.gameObject.name  = "Stage";
            this.gameObject.layer = LayerMask.NameToLayer(StageCamera.LayerName);
            this.gameObject.AddComponent <StageEngine>();
            this.gameObject.AddComponent <UIContentScaler>();
            this.gameObject.SetActive(true);
            Object.DontDestroyOnLoad(this.gameObject);

            this.cachedTransform.localScale = new Vector3(StageCamera.UnitsPerPixel, StageCamera.UnitsPerPixel, StageCamera.UnitsPerPixel);

            EnableSound();

            if (touchScreen)
            {
#if !(UNITY_WEBPLAYER || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR)
                _keyboard     = new FairyGUI.TouchScreenKeyboard();
                keyboardInput = true;
#endif
            }

            Timers.inst.Add(5, 0, RunTextureCollector);

#if UNITY_5_4_OR_NEWER
            SceneManager.sceneLoaded += SceneManager_sceneLoaded;
#endif
            _focusRemovedDelegate = OnFocusRemoved;
        }
Example #18
0
        void HandleTouchEvents()
        {
            int tc = Input.touchCount;

            for (int i = 0; i < tc; ++i)
            {
                Touch uTouch = Input.GetTouch(i);

                if (uTouch.phase == TouchPhase.Stationary)
                {
                    continue;
                }

                Vector2 pos = uTouch.position;
                pos.y = stageHeight - pos.y;

                TouchInfo touch = null;
                for (int j = 0; j < 5; j++)
                {
                    if (_touches[j].touchId == uTouch.fingerId)
                    {
                        touch = _touches[j];
                        break;
                    }
                }
                if (touch == null)
                {
                    continue;
                }

                if (touch.x != pos.x || touch.y != pos.y)
                {
                    touch.x = pos.x;
                    touch.y = pos.y;
                    if (touch.began)
                    {
                        touch.Move();
                    }
                }

                if (touch.lastRollOver != touch.target)
                {
                    HandleRollOver(touch);
                }

                if (uTouch.phase == TouchPhase.Began)
                {
                    if (!touch.began)
                    {
                        _touchCount++;
                        touch.Begin();
                        touch.button = 0;
                        this.focus   = touch.target;

                        touch.UpdateEvent();
                        touch.target.BubbleEvent("onTouchBegin", touch.evt);
                    }
                }
                else if (uTouch.phase == TouchPhase.Canceled || uTouch.phase == TouchPhase.Ended)
                {
                    if (touch.began)
                    {
                        _touchCount--;
                        touch.End();

                        if (uTouch.phase != TouchPhase.Canceled)
                        {
                            DisplayObject clickTarget = touch.ClickTest();
                            if (clickTarget != null)
                            {
                                touch.clickCount = uTouch.tapCount;
                                touch.UpdateEvent();
                                clickTarget.BubbleEvent("onClick", touch.evt);
                            }
                        }

                        touch.target = null;
                        HandleRollOver(touch);

                        touch.touchId = -1;
                    }
                }
            }
        }
Example #19
0
        void HandleTouchEvents()
        {
            for (int i = 0; i < Input.touchCount; ++i)
            {
                Touch uTouch = Input.GetTouch(i);

                if (uTouch.phase == TouchPhase.Stationary)
                {
                    continue;
                }

                bool    hitTested     = false;
                Vector2 touchPosition = uTouch.position;
                touchPosition.y = stageHeight - touchPosition.y;
                TouchInfo touch = null;
                for (int j = 0; j < 5; j++)
                {
                    if (_touches[j].touchId == uTouch.fingerId)
                    {
                        touch = _touches[j];
                        break;
                    }

                    if (_touches[j].touchId == -1)
                    {
                        touch = _touches[j];
                    }
                }
                if (touch == null)
                {
                    continue;
                }

                touch.touchId = uTouch.fingerId;
                mouseX        = touchPosition.x;
                mouseY        = touchPosition.y;

                if (touch.x != mouseX || touch.y != mouseY)
                {
                    touch.x = mouseX;
                    touch.y = mouseY;

                    _objectUnderMouse = HitTest(touchPosition, true);
                    hitTested         = true;
                    touch.target      = _objectUnderMouse;

                    touch.UpdateEvent();
                    onMouseMove.Call(touch.evt);

                    //no rollover/rollout on mobile
                    //if (evt.lastRollOver != _objectUnderMouse)
                    //HandleRollOver(evt);
                }

                if (uTouch.phase == TouchPhase.Began)
                {
                    if (!touch.began)
                    {
                        touch.began          = true;
                        touch.clickCancelled = false;
                        touch.downX          = touch.x;
                        touch.downY          = touch.y;

                        if (!hitTested)
                        {
                            _objectUnderMouse = HitTest(touchPosition, true);
                            hitTested         = true;
                            touch.target      = _objectUnderMouse;
                        }

                        this.focus = _objectUnderMouse;

                        if (_objectUnderMouse != null)
                        {
                            touch.UpdateEvent();
                            _objectUnderMouse.onMouseDown.BubbleCall(touch.evt);
                        }
                    }
                }
                else if (uTouch.phase == TouchPhase.Canceled || uTouch.phase == TouchPhase.Ended)
                {
                    if (touch.began)
                    {
                        touch.began = false;

                        if (!hitTested)
                        {
                            _objectUnderMouse = HitTest(touchPosition, true);
                            hitTested         = true;
                            touch.target      = _objectUnderMouse;
                        }

                        if (_objectUnderMouse != null)
                        {
                            touch.UpdateEvent();
                            _objectUnderMouse.onMouseUp.BubbleCall(touch.evt);

                            if (!touch.clickCancelled && Mathf.Abs(touch.x - touch.downX) < 50 && Mathf.Abs(touch.y - touch.downY) < 50)
                            {
                                touch.clickCount = uTouch.tapCount;
                                touch.UpdateEvent();
                                _objectUnderMouse.onClick.BubbleCall(touch.evt);
                            }
                        }
                    }

                    touch.Reset();
                }
            }
        }
Example #20
0
        void GetHitTarget()
        {
            if (_frameGotHitTarget == Time.frameCount)
            {
                return;
            }

            _frameGotHitTarget = Time.frameCount;

            if (_customInput)
            {
                Vector2 pos = _customInputPos;
                pos.y = stageHeight - pos.y;

                TouchInfo touch = _touches[0];
                _touchTarget = HitTest(pos, true);
                touch.target = _touchTarget;
            }
            else if (touchScreen)
            {
                _touchTarget = null;
                for (int i = 0; i < Input.touchCount; ++i)
                {
                    Touch uTouch = Input.GetTouch(i);

                    Vector2 pos = uTouch.position;
                    pos.y = stageHeight - pos.y;

                    TouchInfo touch = null;
                    TouchInfo free  = null;
                    for (int j = 0; j < 5; j++)
                    {
                        if (_touches[j].touchId == uTouch.fingerId)
                        {
                            touch = _touches[j];
                            break;
                        }

                        if (_touches[j].touchId == -1)
                        {
                            free = _touches[j];
                        }
                    }
                    if (touch == null)
                    {
                        touch = free;
                        if (touch == null || uTouch.phase != TouchPhase.Began)
                        {
                            continue;
                        }

                        touch.touchId = uTouch.fingerId;
                    }

                    if (uTouch.phase == TouchPhase.Stationary)
                    {
                        _touchTarget = touch.target;
                    }
                    else
                    {
                        _touchTarget = HitTest(pos, true);
                        touch.target = _touchTarget;
                    }
                }
            }
            else
            {
                Vector2 pos;
                int     displayIndex;
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
                if (Display.displays.Length > 1)
                {
                    Vector3 p = Display.RelativeMouseAt(Input.mousePosition);
                    pos          = p;
                    displayIndex = (int)p.z;
                    pos.y        = Display.displays[displayIndex].renderingHeight - pos.y;
                }
                else
#endif
                {
                    pos          = Input.mousePosition;
                    pos.y        = stageHeight - pos.y;
                    displayIndex = -1;
                }

                TouchInfo touch = _touches[0];
                if (pos.x < 0 || pos.y < 0)                 //outside of the window
                {
                    _touchTarget = this;
                }
                else
                {
                    _touchTarget = HitTest(pos, true, displayIndex);
                }
                touch.target = _touchTarget;
            }

            HitTestContext.ClearRaycastHitCache();
        }
Example #21
0
        public Stage(int layer, int cameraDepth)
            : base()
        {
            inst         = this;
            defaultLayer = layer;
            soundVolume  = 1;

            _updateContext = new UpdateContext();
            stageWidth     = Screen.width;
            stageHeight    = Screen.height;

            gameObject.name      = "Stage";
            gameObject.hideFlags = HideFlags.None;
            gameObject.SetActive(true);
            _engine = gameObject.AddComponent <StageEngine>();
            Object.DontDestroyOnLoad(gameObject);

            _cameraObject           = new GameObject("Camera");
            _cameraObject.hideFlags = HideFlags.None;
            _cameraObject.layer     = defaultLayer;
            Object.DontDestroyOnLoad(_cameraObject);

            camera = _cameraObject.AddComponent <Camera>();
            camera.nearClipPlane    = -8;
            camera.farClipPlane     = 1;
            camera.depth            = cameraDepth;
            camera.cullingMask      = 1 << defaultLayer;
            camera.clearFlags       = CameraClearFlags.Depth;
            camera.orthographic     = true;
            camera.orthographicSize = 1;
            camera.transform.parent = cachedTransform;

            _halfPixelOffset = (Application.platform == RuntimePlatform.WindowsPlayer ||
                                Application.platform == RuntimePlatform.XBOX360 ||
                                Application.platform == RuntimePlatform.WindowsWebPlayer ||
                                Application.platform == RuntimePlatform.WindowsEditor);

            // Only DirectX 9 needs the half-pixel offset
            if (_halfPixelOffset)
            {
                _halfPixelOffset = (SystemInfo.graphicsShaderLevel < 40);
            }

            if (Application.platform == RuntimePlatform.IPhonePlayer ||
                Application.platform == RuntimePlatform.Android ||
                Application.platform == RuntimePlatform.WP8Player)
            {
                touchScreen = true;
            }

            AdjustCamera();
            EnableSound();

            inputCaret  = new InputCaret();
            highlighter = new Highlighter();

            _touches = new TouchInfo[5];
            for (int i = 0; i < _touches.Length; i++)
            {
                _touches[i] = new TouchInfo();
            }

            if (!touchScreen)
            {
                _touches[0].touchId = 0;
            }

            _rollOutChain  = new List <DisplayObject>();
            _rollOverChain = new List <DisplayObject>();

            onStageResized = new EventListener(this, "onStageResized");
            onMouseMove    = new EventListener(this, "onMouseMove");
            onPostUpdate   = new EventListener(this, "onPostUpdate");
        }
Example #22
0
        public Stage()
            : base()
        {
            _inst       = this;
            soundVolume = 1;

            _updateContext     = new UpdateContext();
            stageWidth         = Screen.width;
            stageHeight        = Screen.height;
            _frameGotHitTarget = -1;

            touchScreen = Input.touchSupported;

            _touches = new TouchInfo[5];
            for (int i = 0; i < _touches.Length; i++)
            {
                _touches[i] = new TouchInfo();
            }

            if (!touchScreen)
            {
                _touches[0].touchId = 0;
            }

            _rollOutChain  = new List <DisplayObject>();
            _rollOverChain = new List <DisplayObject>();

            onStageResized = new EventListener(this, "onStageResized");
            onTouchMove    = new EventListener(this, "onTouchMove");
            onCopy         = new EventListener(this, "onCopy");
            onPaste        = new EventListener(this, "onPaste");

            StageEngine engine = GameObject.FindObjectOfType <StageEngine>();

            if (engine != null)
            {
                this.gameObject = engine.gameObject;
            }
            else
            {
                int layer = LayerMask.NameToLayer(StageCamera.LayerName);

                this.gameObject           = new GameObject("Stage");
                this.gameObject.hideFlags = HideFlags.None;
                this.gameObject.layer     = layer;
                this.gameObject.AddComponent <StageEngine>();
                this.gameObject.AddComponent <UIContentScaler>();
            }
            this.cachedTransform            = gameObject.transform;
            this.cachedTransform.localScale = new Vector3(StageCamera.UnitsPerPixel, StageCamera.UnitsPerPixel, StageCamera.UnitsPerPixel);
            this.gameObject.SetActive(true);
            UnityEngine.Object.DontDestroyOnLoad(this.gameObject);

            EnableSound();

            inputCaret  = new InputCaret();
            highlighter = new Highlighter();

            Timers.inst.Add(5, 0, RunTextureCollector);

#if UNITY_WEBPLAYER || UNITY_WEBGL || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR
            CopyPastePatch.Apply();
#endif
        }
Example #23
0
        void GetHitTarget()
        {
            //Debug.Log("GetHitTarget  :"+ _frameGotHitTarget +"   :"+ Time.frameCount);
            if (_frameGotHitTarget == Time.frameCount)
            {
                return;
            }

            _frameGotHitTarget = Time.frameCount;

            if (_customInput)
            {
                Vector2 pos = _customInputPos;
                pos.y = stageHeight - pos.y;

                TouchInfo touch = _touches[0];
                _touchTarget = HitTest(pos, true);
                touch.target = _touchTarget;
                //Debug.Log("_customInput  :" + _customInput);
            }
            else if (touchScreen)
            {
                _touchTarget = null;

                for (int i = 0; i < Input.touchCount; ++i)
                {
                    Touch uTouch = Input.GetTouch(i);
                    //Debug.Log("Input.touchCount  :" + Input.touchCount);

                    Vector2 pos = uTouch.position;
                    pos.y = stageHeight - pos.y;

                    TouchInfo touch = null;
                    TouchInfo free  = null;
                    for (int j = 0; j < 5; j++)
                    {
                        if (_touches[j].touchId == uTouch.fingerId)
                        {
                            touch = _touches[j];
                            break;
                        }

                        if (_touches[j].touchId == -1)
                        {
                            free = _touches[j];
                        }
                    }
                    if (touch == null)
                    {
                        touch = free;
                        if (touch == null || uTouch.phase != TouchPhase.Began)
                        {
                            continue;
                        }

                        touch.touchId = uTouch.fingerId;
                    }

                    if (uTouch.phase == TouchPhase.Stationary)
                    {
                        _touchTarget = touch.target;
                    }
                    else
                    {
                        _touchTarget = HitTest(pos, true);
                        //Debug.Log("hittest:" + _touchTarget);
                        touch.target = _touchTarget;
                    }
                }
            }
            else
            {
                Vector2 pos = Input.mousePosition;
                pos.y = stageHeight - pos.y;

                TouchInfo touch = _touches[0];
                if (pos.x < 0 || pos.y < 0)                 //outside of the window
                {
                    _touchTarget = this;
                }
                else
                {
                    _touchTarget = HitTest(pos, true);
                }
                touch.target = _touchTarget;
            }

            HitTestContext.ClearRaycastHitCache();
        }
Example #24
0
        void HandleMouseEvents()
        {
            TouchInfo touch = _touches[0];

            if (touch.x != _touchPosition.x || touch.y != _touchPosition.y)
            {
                touch.x = _touchPosition.x;
                touch.y = _touchPosition.y;
                touch.UpdateEvent();
                onTouchMove.Call(touch.evt);

                if (touch.lastRollOver != touch.target)
                {
                    HandleRollOver(touch);
                }
            }

            if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))
            {
                if (!touch.began)
                {
                    touch.began          = true;
                    _touchCount          = 1;
                    touch.clickCancelled = false;
                    touch.downX          = touch.x;
                    touch.downY          = touch.y;
                    touch.downTarget     = touch.target;
                    touch.button         = Input.GetMouseButtonDown(2) ? 2 : (Input.GetMouseButtonDown(1) ? 1 : 0);
                    this.focus           = touch.target;

                    if (touch.target != null)
                    {
                        touch.UpdateEvent();
                        touch.target.onTouchBegin.BubbleCall(touch.evt);
                    }
                }
            }
            if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(2))
            {
                if (touch.began)
                {
                    touch.began = false;
                    _touchCount = 0;

                    if (touch.target != null)
                    {
                        touch.UpdateEvent();
                        touch.CallTouchEnd();
                    }

                    DisplayObject clickTarget = clickTest(touch);
                    if (clickTarget != null)
                    {
                        if (Time.realtimeSinceStartup - touch.lastClickTime < 0.35f)
                        {
                            if (touch.clickCount == 2)
                            {
                                touch.clickCount = 1;
                            }
                            else
                            {
                                touch.clickCount++;
                            }
                        }
                        else
                        {
                            touch.clickCount = 1;
                        }
                        touch.lastClickTime = Time.realtimeSinceStartup;
                        touch.UpdateEvent();

                        if (Input.GetMouseButtonUp(1))
                        {
                            clickTarget.onRightClick.BubbleCall(touch.evt);
                        }
                        else
                        {
                            clickTarget.onClick.BubbleCall(touch.evt);
                        }
                    }
                }
            }
        }
Example #25
0
        public Stage(int layer, int cameraDepth)
            : base()
        {
            inst = this;
            defaultLayer = layer;
            soundVolume = 1;

            _updateContext = new UpdateContext();
            stageWidth = Screen.width;
            stageHeight = Screen.height;

            gameObject.name = "Stage";
            gameObject.hideFlags = HideFlags.None;
            gameObject.SetActive(true);
            _engine = gameObject.AddComponent<StageEngine>();
            Object.DontDestroyOnLoad(gameObject);

            _cameraObject = new GameObject("Camera");
            _cameraObject.hideFlags = HideFlags.None;
            _cameraObject.layer = defaultLayer;
            Object.DontDestroyOnLoad(_cameraObject);

            camera = _cameraObject.AddComponent<Camera>();
            camera.nearClipPlane = -8;
            camera.farClipPlane = 1;
            camera.depth = cameraDepth;
            camera.cullingMask = 1 << defaultLayer;
            camera.clearFlags = CameraClearFlags.Depth;
            camera.orthographic = true;
            camera.orthographicSize = 1;
            camera.transform.parent = cachedTransform;

            _halfPixelOffset = (Application.platform == RuntimePlatform.WindowsPlayer ||
                Application.platform == RuntimePlatform.XBOX360 ||
                Application.platform == RuntimePlatform.WindowsWebPlayer ||
                Application.platform == RuntimePlatform.WindowsEditor);

            // Only DirectX 9 needs the half-pixel offset
            if (_halfPixelOffset)
                _halfPixelOffset = (SystemInfo.graphicsShaderLevel < 40);

            if (Application.platform == RuntimePlatform.IPhonePlayer
                || Application.platform == RuntimePlatform.Android
                || Application.platform == RuntimePlatform.WP8Player)
                touchScreen = true;

            AdjustCamera();
            EnableSound();

            inputCaret = new InputCaret();
            highlighter = new Highlighter();

            _touches = new TouchInfo[5];
            for (int i = 0; i < _touches.Length; i++)
                _touches[i] = new TouchInfo();

            if (!touchScreen)
                _touches[0].touchId = 0;

            _rollOutChain = new List<DisplayObject>();
            _rollOverChain = new List<DisplayObject>();

            onStageResized = new EventListener(this, "onStageResized");
            onMouseMove = new EventListener(this, "onMouseMove");
            onPostUpdate = new EventListener(this, "onPostUpdate");
        }
Example #26
0
        void HandleRollOver(TouchInfo touch)
        {
            DisplayObject element;
            element = touch.lastRollOver;
            while (element != null)
            {
                _rollOutChain.Add(element);
                element = element.parent;
            }

            touch.lastRollOver = touch.target;

            element = touch.target;
            int i;
            while (element != null)
            {
                i = _rollOutChain.IndexOf(element);
                if (i != -1)
                {
                    _rollOutChain.RemoveRange(i, _rollOutChain.Count - i);
                    break;
                }
                _rollOverChain.Add(element);

                element = element.parent;
            }

            int cnt = _rollOutChain.Count;
            if (cnt > 0)
            {
                for (i = 0; i < cnt; i++)
                {
                    element = _rollOutChain[i];
                    if (element.stage != null)
                        element.onRollOut.Call();
                }
                _rollOutChain.Clear();
            }

            cnt = _rollOverChain.Count;
            if (cnt > 0)
            {
                for (i = 0; i < cnt; i++)
                {
                    element = _rollOverChain[i];
                    if (element.stage != null)
                        element.onRollOver.Call();
                }
                _rollOverChain.Clear();
            }
        }
Example #27
0
        void HandleMouseEvents()
        {
            TouchInfo touch = _touches[0];

            if (touch.x != _touchPosition.x || touch.y != _touchPosition.y)
            {
                touch.x = _touchPosition.x;
                touch.y = _touchPosition.y;
                touch.UpdateEvent();
                onTouchMove.Call(touch.evt);

                if (touch.lastRollOver != touch.target)
                {
                    HandleRollOver(touch);
                }
            }

            if (Input.GetMouseButtonDown(0))
            {
                if (!touch.began)
                {
                    touch.began = true;
                    _touchCount++;
                    touch.clickCancelled = false;
                    touch.downX          = touch.x;
                    touch.downY          = touch.y;
                    this.focus           = touch.target;

                    if (touch.target != null)
                    {
                        touch.UpdateEvent();
                        touch.target.onTouchBegin.BubbleCall(touch.evt);
                    }
                }
            }
            if (Input.GetMouseButtonUp(0))
            {
                if (touch.began)
                {
                    touch.began = false;
                    _touchCount--;

                    if (touch.target != null)
                    {
                        touch.UpdateEvent();
                        touch.target.onTouchEnd.BubbleCall(touch.evt);

                        if (!touch.clickCancelled && Mathf.Abs(touch.x - touch.downX) < 50 && Mathf.Abs(touch.y - touch.downY) < 50)
                        {
                            if (Time.realtimeSinceStartup - touch.lastClickTime < 0.35f)
                            {
                                if (touch.clickCount == 2)
                                {
                                    touch.clickCount = 1;
                                }
                                else
                                {
                                    touch.clickCount++;
                                }
                            }
                            else
                            {
                                touch.clickCount = 1;
                            }
                            touch.lastClickTime = Time.realtimeSinceStartup;
                            touch.UpdateEvent();
                            touch.target.onClick.BubbleCall(touch.evt);
                        }
                    }
                }
            }
            if (Input.GetMouseButtonUp(1))
            {
                if (touch.target != null)
                {
                    touch.UpdateEvent();
                    touch.target.onRightClick.BubbleCall(touch.evt);
                }
            }
        }
Example #28
0
        void HandleTouchEvents()
        {
            for (int i = 0; i < Input.touchCount; ++i)
            {
                Touch uTouch = Input.GetTouch(i);

                if (uTouch.phase == TouchPhase.Stationary)
                {
                    continue;
                }

                Vector2 pos = uTouch.position;
                pos.y = stageHeight - pos.y;

                TouchInfo touch = null;
                for (int j = 0; j < 5; j++)
                {
                    if (_touches[j].touchId == uTouch.fingerId)
                    {
                        touch = _touches[j];
                        break;
                    }
                }
                if (touch == null)
                {
                    continue;
                }

                if (touch.x != pos.x || touch.y != pos.y)
                {
                    touch.x = pos.x;
                    touch.y = pos.y;
                    touch.UpdateEvent();
                    onTouchMove.Call(touch.evt);

                    //no rollover/rollout on mobile
                }

                if (uTouch.phase == TouchPhase.Began)
                {
                    if (!touch.began)
                    {
                        touch.began = true;
                        _touchCount++;
                        touch.clickCancelled = false;
                        touch.downX          = touch.x;
                        touch.downY          = touch.y;
                        this.focus           = touch.target;

                        if (touch.target != null)
                        {
                            touch.UpdateEvent();
                            touch.target.onTouchBegin.BubbleCall(touch.evt);
                        }
                    }
                }
                else if (uTouch.phase == TouchPhase.Canceled || uTouch.phase == TouchPhase.Ended)
                {
                    if (touch.began)
                    {
                        touch.began = false;
                        _touchCount--;

                        if (touch.target != null)
                        {
                            touch.UpdateEvent();
                            touch.target.onTouchEnd.BubbleCall(touch.evt);

                            if (!touch.clickCancelled && Mathf.Abs(touch.x - touch.downX) < 50 && Mathf.Abs(touch.y - touch.downY) < 50)
                            {
                                touch.clickCount = uTouch.tapCount;
                                touch.UpdateEvent();
                                touch.target.onClick.BubbleCall(touch.evt);
                            }
                        }
                    }

                    touch.Reset();
                }
            }
        }
Example #29
0
        void GetHitTarget()
        {
            if (_frameGotHitTarget == Time.frameCount)
            {
                return;
            }

            _frameGotHitTarget = Time.frameCount;

            if (_customInput)
            {
                Vector2 pos = _customInputPos;
                pos.y = stageHeight - pos.y;

                TouchInfo touch = _touches[0];
                _touchTarget = HitTest(pos, true);
                touch.target = _touchTarget;
            }
            else if (touchScreen)
            {
                for (int i = 0; i < Input.touchCount; ++i)
                {
                    Touch uTouch = Input.GetTouch(i);
                    if (uTouch.phase == TouchPhase.Stationary)
                    {
                        continue;
                    }

                    Vector2 pos = uTouch.position;
                    pos.y = stageHeight - pos.y;

                    _touchTarget = null;
                    TouchInfo touch = null;
                    for (int j = 0; j < 5; j++)
                    {
                        if (_touches[j].touchId == uTouch.fingerId)
                        {
                            touch = _touches[j];
                            break;
                        }

                        if (_touches[j].touchId == -1)
                        {
                            touch = _touches[j];
                            //下面的赋值避免了touchMove在touchDown前触发
                            touch.x = uTouch.position.x;
                            touch.y = stageHeight - uTouch.position.y;
                        }
                    }
                    if (touch == null)
                    {
                        return;
                    }

                    touch.touchId = uTouch.fingerId;
                    DisplayObject ht = HitTest(pos, true);
                    touch.target = ht;
                    if (ht != null)
                    {
                        _touchTarget = ht;
                    }
                }
            }
            else
            {
                Vector2 pos = Input.mousePosition;
                pos.y = stageHeight - pos.y;

                TouchInfo touch = _touches[0];
                if (pos.x < 0 || pos.y < 0)
                {
                    pos.x = touch.x;
                    pos.y = touch.y;
                }

                _touchTarget = HitTest(pos, true);
                touch.target = _touchTarget;
            }

            HitTestContext.ClearRaycastHitCache();
        }
Example #30
0
        void HandleCustomInput()
        {
            Vector2 pos = _customInputPos;

            pos.y = stageHeight - pos.y;
            TouchInfo touch = _touches[0];

            if (touch.x != pos.x || touch.y != pos.y)
            {
                touch.x = pos.x;
                touch.y = pos.y;
                touch.UpdateEvent();
                onTouchMove.Call(touch.evt);

                if (touch.lastRollOver != touch.target)
                {
                    HandleRollOver(touch);
                }
            }

            if (_customInputButtonDown)
            {
                if (!touch.began)
                {
                    touch.began          = true;
                    _touchCount          = 1;
                    touch.clickCancelled = false;
                    touch.downX          = touch.x;
                    touch.downY          = touch.y;
                    touch.downTarget     = touch.target;
                    this.focus           = touch.target;

                    if (touch.target != null)
                    {
                        touch.UpdateEvent();
                        touch.target.onTouchBegin.BubbleCall(touch.evt);
                    }
                }
            }
            else if (touch.began)
            {
                touch.began = false;
                _touchCount = 0;

                if (touch.target != null)
                {
                    touch.UpdateEvent();
                    touch.CallTouchEnd();

                    DisplayObject clickTarget = clickTest(touch);
                    if (clickTarget != null)
                    {
                        if (Time.realtimeSinceStartup - touch.lastClickTime < 0.35f)
                        {
                            if (touch.clickCount == 2)
                            {
                                touch.clickCount = 1;
                            }
                            else
                            {
                                touch.clickCount++;
                            }
                        }
                        else
                        {
                            touch.clickCount = 1;
                        }
                        touch.lastClickTime = Time.realtimeSinceStartup;
                        touch.UpdateEvent();
                        clickTarget.onClick.BubbleCall(touch.evt);
                    }
                }
            }
        }
Example #31
0
        void HandleTouchEvents()
        {
            int tc = Input.touchCount;

            for (int i = 0; i < tc; ++i)
            {
                Touch uTouch = Input.GetTouch(i);

                if (uTouch.phase == TouchPhase.Stationary)
                {
                    continue;
                }

                Vector2 pos = uTouch.position;
                pos.y = stageHeight - pos.y;

                TouchInfo touch = null;
                for (int j = 0; j < 5; j++)
                {
                    if (_touches[j].touchId == uTouch.fingerId)
                    {
                        touch = _touches[j];
                        break;
                    }
                }
                if (touch == null)
                {
                    continue;
                }

                if (touch.x != pos.x || touch.y != pos.y)
                {
                    touch.x = pos.x;
                    touch.y = pos.y;
                    touch.UpdateEvent();
                    onTouchMove.Call(touch.evt);

                    //no rollover/rollout on mobile
                }

                if (uTouch.phase == TouchPhase.Began)
                {
                    if (!touch.began)
                    {
                        _touchCount++;
                        touch.begin();
                        this.focus = touch.target;

                        if (touch.target != null)
                        {
                            touch.UpdateEvent();
                            touch.target.onTouchBegin.BubbleCall(touch.evt);
                        }
                    }
                }
                else if (uTouch.phase == TouchPhase.Canceled || uTouch.phase == TouchPhase.Ended)
                {
                    if (touch.began)
                    {
                        _touchCount--;
                        touch.end();

                        DisplayObject clickTarget = touch.ClickTest();
                        if (clickTarget != null)
                        {
                            touch.clickCount = uTouch.tapCount;
                            touch.UpdateEvent();
                            clickTarget.onClick.BubbleCall(touch.evt);
                        }
                    }

                    touch.Reset();
                }
            }
        }
Example #32
0
		/// <summary>
		/// 
		/// </summary>
		public Stage()
			: base()
		{
			_inst = this;
			soundVolume = 1;

			_updateContext = new UpdateContext();
			stageWidth = Screen.width;
			stageHeight = Screen.height;
			_frameGotHitTarget = -1;

			if (Application.platform == RuntimePlatform.WindowsPlayer
				|| Application.platform == RuntimePlatform.WindowsPlayer
				|| Application.platform == RuntimePlatform.OSXPlayer
				|| Application.platform == RuntimePlatform.OSXEditor)
				touchScreen = false;
			else
				touchScreen = Input.touchSupported;

			_touches = new TouchInfo[5];
			for (int i = 0; i < _touches.Length; i++)
				_touches[i] = new TouchInfo();

			if (!touchScreen)
				_touches[0].touchId = 0;

			_rollOutChain = new List<DisplayObject>();
			_rollOverChain = new List<DisplayObject>();

			onStageResized = new EventListener(this, "onStageResized");
			onTouchMove = new EventListener(this, "onTouchMove");

			StageEngine engine = GameObject.FindObjectOfType<StageEngine>();
			if (engine != null)
				Object.Destroy(engine.gameObject);

			this.gameObject.name = "Stage";
			this.gameObject.layer = LayerMask.NameToLayer(StageCamera.LayerName);
			this.gameObject.AddComponent<StageEngine>();
			this.gameObject.AddComponent<UIContentScaler>();
			this.gameObject.SetActive(true);
			Object.DontDestroyOnLoad(this.gameObject);

			this.cachedTransform.localScale = new Vector3(StageCamera.UnitsPerPixel, StageCamera.UnitsPerPixel, StageCamera.UnitsPerPixel);

			EnableSound();

			if (touchScreen)
			{
#if !(UNITY_WEBPLAYER || UNITY_WEBGL || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR)
				_keyboard = new FairyGUI.TouchScreenKeyboard();
				keyboardInput = true;
#endif
			}

			Timers.inst.Add(5, 0, RunTextureCollector);

#if UNITY_5_4_OR_NEWER
			SceneManager.sceneLoaded += SceneManager_sceneLoaded;
#endif
			_focusRemovedDelegate = OnFocusRemoved;
		}