summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XEditor/XSkillEditor/XGesture.cs
blob: 5c7b1eb10cfeed622c8c8e426c1617ee8befd7c7 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using XUtliPoolLib;
using UnityEngine;

namespace XEditor
{
    internal class XGesture : XSingleton<XGesture>
    {
        public enum SwypeDirectionType
        {
            Left, Right
        }

        private bool _one = false;
        private bool _double = false;
        private bool _bswype = false;

        private float _last_swype_at = 0;

        private float _start_at = 0;
        //private float _swype_start_at = 0;

        private Vector2 _start = Vector2.zero;
        private Vector2 _swype_start = Vector2.zero;

        private Vector2 _end = Vector2.zero;
        private XTouchItem _touch;

        private Vector3 _swypedir = Vector3.zero;
        private Vector3 _touchpos = Vector3.zero;

        private float _last_touch_at = 0;

        private SwypeDirectionType _swype_type;

        public bool Gestured
        {
            get { return _bswype || _one || _double; }
        }

        public bool OneTouch
        {
            get { return _one; }
        }

        public bool DoubleTouch
        {
            get { return _double; }
        }

        public bool Swype
        {
            get { return _bswype; }
        }

        public float LastSwypeAt
        {
            get { return _last_swype_at; }
        }

        public Vector3 SwypeDirection
        {
            get { return -_swypedir; }
        }

        public SwypeDirectionType SwypeType
        {
            get { return _swype_type; }
        }

        public Vector3 TouchPosition
        {
            get { return _touchpos; }
        }

        public void Update()
        {
            XTouch.singleton.Update();
            _touch = XTouch.singleton.GetTouch();

            if (_touch != null)
            {
                if (_touch.Phase == TouchPhase.Began)
                {
                    _start = _touch.Position;
                    _swype_start = _start;

                    _start_at = Time.time;
                    //_swype_start_at = _start_at;
                }

                _bswype = SwypeUpdate();
                _one = OneUpdate();
                _double = DoubleUpdate();
            }
            else 
            {
                Clear();
            }
        }

        private void Clear()
        {
            _one = false;
            _double = false;
            _bswype = false;
        }

        private bool OneUpdate()
        {
            TouchPhase phase = _touch.Phase;

            if ((phase == TouchPhase.Ended ||
                 phase == TouchPhase.Canceled))
            {
                Vector2 delta = _touch.Position - _start;

                float dist = Mathf.Sqrt(Mathf.Pow(delta.x, 2) + Mathf.Pow(delta.y, 2));
                float duration = Time.time - _start_at;

                if (dist < 5 && duration < 0.2f / Time.timeScale)
                {
                    _last_touch_at = Time.time;
                    _touchpos = _touch.Position;

                    return true;
                }
                else
                    return false;
            }

            return false;
        }

        private bool DoubleUpdate()
        {
            if (_touch.Phase == TouchPhase.Began)
            {
                float deltaT = Time.time - _last_touch_at;
                if (XCommon.singleton.IsLess(deltaT, 0.5f / Time.timeScale))
                {
                    Vector2 delta;
                    delta.x = _start.x - TouchPosition.x;
                    delta.y = _start.y - TouchPosition.y;

                    float dist = Mathf.Sqrt(Mathf.Pow(delta.x, 2) + Mathf.Pow(delta.y, 2));

                    if (dist < 50.0f)
                    {
                        _touchpos = _touch.Position;
                        _one = false;

                        return true;
                    }
                    else
                        return false;
                }
            }

            return false;
        }

        private bool SwypeUpdate()
        {
            TouchPhase phase = _touch.Phase;

            if (phase == TouchPhase.Moved)
            {
                _end = _touch.Position;
                Vector2 delta = _end - _swype_start;

                float endAt = Time.time;

                float dist = Mathf.Sqrt(Mathf.Pow(delta.x, 2) + Mathf.Pow(delta.y, 2));
                //float angle = Mathf.Atan(delta.y / delta.x) * (180.0f / Mathf.PI);
                //float duration = endAt - _swype_start_at;
                //float speed = dist / duration;

                if (dist > 50.0f)
                {
                    _swype_type = XCommon.singleton.IsGreater(_swype_start.x, Screen.width * 0.5f) ? XGesture.SwypeDirectionType.Right : XGesture.SwypeDirectionType.Left;

                    _swype_start = _end;
                    //_swype_start_at = Time.time;

                    _swypedir.x = delta.x;
                    _swypedir.y = 0;
                    _swypedir.z = delta.y;

                    _swypedir.Normalize();
                    _touchpos = _end;

                    _last_swype_at = endAt; // _swype_start_at;

                    return true;
                }
            }

            return false;
        }
    }
}
#endif