summaryrefslogtreecommitdiff
path: root/Runtime/Input/SimulateInputEvents.cpp
blob: 4802477c06181db4c0297b3cde2cd63995976eae (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "UnityPrefix.h"
#include "SimulateInputEvents.h"
#include "GetInput.h"
#include "Runtime/Graphics/ScreenManager.h"
#include "InputManager.h"
#include "Runtime/IMGUI/GUIManager.h"
#include <math.h>

#if UNITY_EDITOR && UNITY_OSX
#include "Editor/Src/RemoteInput/iPhoneRemoteImpl.h"
#endif

#if UNITY_EDITOR
#include "Editor/Src/RemoteInput/AndroidRemote.h"
#endif

#ifndef MAXFLOAT
	#define	MAXFLOAT	((float)3.40282346638528860e+38)
#endif

void CaptureEventMousePosition (InputEvent& e)
{
	e.Init();

	Vector2f p = GetInputManager().GetMousePosition();

#if ENABLE_NEW_EVENT_SYSTEM
	e.touch.pos = p;
	e.touch.pos.y = GetScreenManager().GetHeight() - p.y;
	e.touch.deltaPos = GetInputManager().GetMouseDelta();
#else
	e.mousePosition = p;
	e.mousePosition.y = GetScreenManager().GetHeight() - e.mousePosition.y;
	Vector3f d = GetInputManager().GetMouseDelta();
	e.delta = Vector2f(d.x, d.y);
#endif
	e.pressure = 1.0f;

    e.clickCount = 1;

    size_t touchCount = GetActiveTouchCount();

	for (int i = 0; i < touchCount; ++i)
	{
#if ENABLE_NEW_EVENT_SYSTEM
		Touch* touch = GetTouch(i);

		if (touch != NULL)
        {
			if (touch->tapCount > e.clickCount)
            {
                e.clickCount = touch->tapCount;
            }
        }
#else
		Touch touch;

		if (GetTouch(i, touch))
		{
			if (touch.tapCount > e.clickCount)
			{
				e.clickCount = touch.tapCount;
			}
		}
#endif
	}
}

// send event on button down/up
void GenerateAndSendInputDownUpEvent( const InputEvent::MouseButton button, const bool isDown )
{
	InputEvent ie;
	CaptureEventMousePosition (ie);
	ie.button = button;
	ie.type = isDown ? InputEvent::kMouseDown : InputEvent::kMouseUp;
#if UNITY_METRO
	ie.touchType = InputEvent::kFingerTouch;
#endif
	GetGUIManager().QueueEvent (ie);
	// If the touch has ended we need to "disable" the mouse hover state, by moving the mouse pointer "away"
	if (!isDown)
	{
		ie.Init();
		ie.type = InputEvent::kMouseUp;
#if ENABLE_NEW_EVENT_SYSTEM
		ie.touch.pos = Vector2f(MAXFLOAT,MAXFLOAT);
#else
		ie.mousePosition = Vector2f(MAXFLOAT,MAXFLOAT);
#endif
		GetGUIManager().QueueEvent (ie);
	}
}

void RestoreMouseState( const Vector2f& mousePosition )
{
	enum { MaxSimulatedMouseButtons = 3 };
	GetInputManager().SetMousePosition(mousePosition);
	if (GetActiveTouchCount() > 0)
	{
		for (int i = 0; i < MaxSimulatedMouseButtons; ++i)
			GetInputManager().SetMouseButton(i, false);
		GetInputManager().InputEndFrame();
	}
}

void SimulateMouseInput()
{
	enum { MaxSimulatedMouseButtons = 3 };
	static size_t prevTouchPointCount = 0;

#if UNITY_WINRT
	Vector2f originalPos = GetInputManager().GetMousePosition();
#endif

	for (int i = 0; i < MaxSimulatedMouseButtons; ++i)
	{
		if (i < GetActiveTouchCount())
			GetInputManager().SetMouseButton(i, true);
		else if (i < prevTouchPointCount)
			GetInputManager().SetMouseButton(i, false);
	}

	prevTouchPointCount = GetActiveTouchCount();

	size_t touchCount = GetTouchCount();
	Vector2f pos(0.0f, 0.0f);
	static Vector2f prevPos(0.0f, 0.0f);

	for (int i = 0; i < GetTouchCount(); ++i)
	{
#if ENABLE_NEW_EVENT_SYSTEM
		Touch* touch = GetTouch(i);
		if (touch != NULL)
			pos += touch->pos;
#else
		Touch touch;
		if (GetTouch(i, touch))
			pos += touch.pos;
#endif
	}

	if (touchCount > 0)
	{
		float invCount = 1.0f / (float)touchCount;
		pos.x *= invCount;
		pos.y *= invCount;

		GetInputManager().SetMousePosition(pos);
#if ENABLE_NEW_EVENT_SYSTEM
		GetInputManager().SetMouseDelta(Vector2f(pos.x - prevPos.x, pos.y - prevPos.y));
#else
		GetInputManager().SetMouseDelta(Vector3f(pos.x - prevPos.x,
		                                         pos.y - prevPos.y, 0.0f));
#endif
		prevPos = pos;
	}
	
#if UNITY_IPHONE || UNITY_ANDROID || UNITY_WINRT || UNITY_BB10 || UNITY_TIZEN
	SimulateInputEvents ();

#elif UNITY_EDITOR
	if (
#if UNITY_OSX
		iPhoneHasRemoteConnected () ||
#endif
		AndroidHasRemoteConnected())
	{
		SimulateInputEvents();
	}
#endif

#if UNITY_WINRT
	if (!GetInputManager().GetSimulateMouseWithTouches())
		RestoreMouseState(originalPos);
#endif
}

void SimulateInputEvents()
{
	InputEvent ie;

	static bool lastMouseB0 = false;
	static bool lastMouseB1 = false;

	if (SqrMagnitude(GetInputManager().GetMouseDelta()) > 1e-6)
	{
		CaptureEventMousePosition (ie);
		ie.type = InputEvent::kMouseMove;
		ie.button = 0;
#if UNITY_METRO
		ie.touchType = InputEvent::kFingerTouch;
#endif

		if (GetInputManager().GetMouseButton(0) && lastMouseB0)
		{
			ie.type = InputEvent::kMouseDrag;
			ie.button |= InputEvent::kLeftButton;
		}
		if (GetInputManager().GetMouseButton(1) && lastMouseB1)
		{
			ie.type = InputEvent::kMouseDrag;
			ie.button |= InputEvent::kRightButton;
		}

		GetGUIManager().QueueEvent (ie);
	}

	bool buttonDown = GetInputManager().GetMouseButton(0);
	if (buttonDown != lastMouseB0)
	{
		GenerateAndSendInputDownUpEvent( InputEvent::kLeftButton, buttonDown );
		lastMouseB0 = buttonDown;
	}

	buttonDown = GetInputManager().GetMouseButton(1);
	if (buttonDown != lastMouseB1)
	{
		GenerateAndSendInputDownUpEvent( InputEvent::kRightButton, buttonDown );
		lastMouseB1 = buttonDown;
	}
}