summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XEditor/XSkillEditor/XTouch.cs
blob: 6924e3958f18df4992dda5f7f78f5df945e03b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using XUtliPoolLib;
using UnityEngine;

namespace XEditor
{
    internal class XTouch : XSingleton<XTouch>
    {
        private bool _bEditorMode = false;
        private bool _bHasTouch = false;
        private Vector3 _lastMousePosition = Vector3.zero;

        private XTouchItem _touch = new XTouchItem();

        public XTouch()
        {
            _bEditorMode = (Application.platform == RuntimePlatform.WindowsEditor ||
                            Application.platform == RuntimePlatform.OSXEditor);
        }

        public XTouchItem GetTouch()
        {
            if (_bHasTouch) 
                return _touch;
            else 
                return null;
        }

        public void Update()
        {
            _bHasTouch = false;

            if (_bEditorMode)
            {
                if (Input.GetMouseButton(0))
                {
                    _touch.faketouch.fingerId = 10;
                    _touch.faketouch.position = Input.mousePosition;
                    _touch.faketouch.deltaTime = Time.deltaTime;
                    _touch.faketouch.deltaPosition = Input.mousePosition - _lastMousePosition;
                    _touch.faketouch.phase = (Input.GetMouseButtonDown(0) ? TouchPhase.Began :
                                        (_touch.faketouch.deltaPosition.sqrMagnitude > 1.0f ? TouchPhase.Moved : TouchPhase.Stationary));
                    _touch.faketouch.tapCount = 1;

                    _touch.Fake = true;

                    HandleTouch();
                }
                else if (Input.GetMouseButtonUp(0))
                {
                    _touch.faketouch.fingerId = 10;
                    _touch.faketouch.position = Input.mousePosition;
                    _touch.faketouch.deltaTime = Time.deltaTime;
                    _touch.faketouch.deltaPosition = Input.mousePosition - _lastMousePosition;
                    _touch.faketouch.phase = TouchPhase.Ended;
                    _touch.faketouch.tapCount = 1;

                    _touch.Fake = true;

                    HandleTouch();
                }
            }
            else 
            {
                if (Input.touchCount > 0)
                {
                    _touch.Fake = false;
                    _touch.touch = Input.GetTouch(0);

                    HandleTouch();
                }
            }
        }

        private void HandleTouch()
        {
            _bHasTouch = true;
        }
    }

    internal class XTouchItem
    {
        public bool Fake { get; set; }
        public Touch touch;
        public XFakeTouch faketouch = new XFakeTouch();

        public Vector2 DeltaPosition
        {
            get { return Fake ? faketouch.deltaPosition : touch.deltaPosition; }
        }
        public float DeltaTime
        {
            get { return Fake ? faketouch.deltaTime : touch.deltaTime; }
        }
        public int FingerId
        {
            get { return Fake ? faketouch.fingerId : touch.fingerId; }
        }
        public TouchPhase Phase
        {
            get { return Fake ? faketouch.phase : touch.phase; }
        }
        public Vector2 Position
        {
            get { return Fake ? faketouch.position : touch.position; }
        }
        public Vector2 RawPosition
        {
            get { return Fake ? faketouch.rawPosition : touch.rawPosition; }
        }
        public int TapCount
        {
            get { return Fake ? faketouch.tapCount : touch.tapCount; }
        }
    }

    internal struct XFakeTouch
    {
        public Vector2 deltaPosition { get; set; }
        public float deltaTime { get; set; }
        public int fingerId { get; set; }
        public TouchPhase phase { get; set; }
        public Vector2 position { get; set; }
        public Vector2 rawPosition { get; set; }
        public int tapCount { get; set; }
    }
}
#endif