summaryrefslogtreecommitdiff
path: root/Runtime/Input/InputAxis.cpp
blob: 62cf9d10ec0bbf1d57885bc0609d71aff3284236 (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
223
224
225
226
#include "UnityPrefix.h"
#include "InputAxis.h"
#include "TimeManager.h"
#include "InputManager.h"

using namespace std;

InputAxis::InputAxis () {
	positiveButton = 0;
	negativeButton = 0;
	altPositiveButton = 0;
	altNegativeButton = 0;
	joyNum = 0;
	type=kAxisButton;
	dead = 0.001f;
	gravity = 0.0;
	sensitivity= .1f;
	invert = false;
	snap = false;
	value = 0.0f;
	axis = 0;
	descriptiveName="";
	descriptiveNegativeName="";
}

InputAxis::InputAxis (const string &name) {
	positiveButton = 0;
	negativeButton = 0;
	altPositiveButton = 0;
	altNegativeButton = 0;
	joyNum = 0;
	type=kAxisButton;
	dead = 0.001f;
	gravity = 0.0;
	sensitivity= .1f;
	invert = false;
	snap = false;
	value = 0.0f;
	axis = 0;
	m_Name = name;
	descriptiveName="";
	descriptiveNegativeName="";
}

void InputAxis::DoGravity (float time) {
	// Handle gravity
	if (gravity) {
		if (value > 0) {
			value -= gravity * time;
			if (value < 0)
				value = 0;
		} else if (value < 0) {
			value += gravity * time;
			if (value > 0)
				value = 0;
		}
	}
}

void InputAxis::Update () {
	float time = GetDeltaTime();
	Vector2f pos;
#if !ENABLE_NEW_EVENT_SYSTEM
	Vector3f delta;
#endif
	
//	Vector2f pos;
	int posFlag, negFlag;

	if (invert) 
		value = -value;
		
	switch (type) {
	case kAxisButton: 
		posFlag = GetInputManager().GetKey (positiveButton)||GetInputManager().GetKey (altPositiveButton);
		negFlag = GetInputManager().GetKey (negativeButton)||GetInputManager().GetKey (altNegativeButton);
		rawValue = 0.0F;

		// Lock if both up and down are held
		if (!(posFlag && negFlag)) {
			if (posFlag) {
				if (snap && value < 0.0)
					value = 0.0;
				else
					value += sensitivity * time;
				if (value < 0.0)
					value += gravity * time;
				
				value = min (1.0F, value);
				rawValue = 1.0F;
				
			}
			else if (negFlag) {
				if (snap && value > 0.0)
					value = 0.0;
				else
					value -= sensitivity * time;
				if (value > 0.0)
					value -= gravity * time;

				value = max (-1.0F, value);
				rawValue = -1.0F;

			} else 
				DoGravity (time);
		}
		
		break;

	case kAxisMouse:
#if ENABLE_NEW_EVENT_SYSTEM
		if (axis == 0)
			value = GetInputManager().GetMouseDelta().x;
		else if (axis == 1)
			value = GetInputManager().GetMouseDelta().y;
		else
			value = GetInputManager().GetMouseScroll().x;
#else
		delta = GetInputManager().GetMouseDelta ();
		if (axis == 0)
			value = delta.x;
		else if (axis == 1)
			value = delta.y;
		else
			value = delta.z;
#endif
		
		rawValue = value;
		value *= sensitivity;

		break;
	case kAxisJoystick:
		value = GetInputManager().GetJoystickPosition (joyNum, axis);
		rawValue = value;
		value *= sensitivity;
		
		
		if (value > 1.0F)
			value = 1.0F;
		else if (value < -1.0F)
			value = -1.0F;
		else if (value < dead && value > -dead) 
			value = 0.0F;
		else if (value > 0.0F)
			value = Lerp (0.0F, 1.0F, (value - dead) / (1.0F - dead));
		else 
			value = Lerp (0.0F, -1.0F, (-value - dead) / (1.0F - dead));
		break;
	}

	if (invert)
	{
		value = -value;
		rawValue = -rawValue;
	}
}


void InputAxis::MakeAnalogKey (int pos, int neg, int altpos, int altnegpos) {
	positiveButton = pos;
	negativeButton = neg;
	altPositiveButton = altpos;
	altNegativeButton = altnegpos;
	type = kAxisButton;
	sensitivity = 3;
	gravity = 3;
	snap = true;
}

void InputAxis::MakeButton (int button, int altbutton) {
	positiveButton = button;
	negativeButton = 0;
	altPositiveButton = altbutton;
	altNegativeButton = 0;
	type = kAxisButton;
	sensitivity = 1000;
	gravity = 1000;
	snap = false;
}

void InputAxis::MakeMouse (int a) {
	type = kAxisMouse; 
	axis = a;
	dead = 0.0;
	sensitivity = 0.1f;
}

void InputAxis::MakeJoystick (int a) {
	type = kAxisJoystick; 
	axis = a;
	sensitivity = 1.0F;
	dead = 0.19F;
	gravity = 0.0F;
	snap = false;
	invert = false;
}

UnityStr InputAxis::GetDescriptiveName(bool neg) {
	if(neg)
		if(descriptiveNegativeName.length())
			return descriptiveNegativeName;
		else 
			if(descriptiveName.length())
				return descriptiveName+" (-)";
			else
				return m_Name+" (-)";
	else
		if(descriptiveName.length())
			if(negativeButton&&!descriptiveNegativeName.length())
				return descriptiveName+" (+)";
			else
				return descriptiveName;
		else
			if(negativeButton)
				return m_Name+" (+)";
			else
				return m_Name;
}

float InputAxis::GetValueRaw () const
{
	if (type == kAxisButton)
		return rawValue;
	else
		return value;
}