summaryrefslogtreecommitdiff
path: root/indra/newview/llviewerjoystick.cpp
blob: ae51726b5ee746ee1b990d374f58acfb535220d4 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/** 
 * @file llviewerjoystick.cpp
 * @brief Joystick functionality.
 *
 * Copyright (c) 2002-$CurrentYear$, Linden Research, Inc.
 * $License$
 */

#include "llviewerprecompiledheaders.h"
#include "llviewercontrol.h"
#include "llviewerwindow.h"
#include "llviewercamera.h"
#include "llviewerjoystick.h"
#include "viewer.h"
#include "llkeyboard.h"

static LLQuaternion sFlycamRotation;
static LLVector3 sFlycamPosition;
static F32		sFlycamZoom;

BOOL  LLViewerJoystick::sOverrideCamera = FALSE;

void LLViewerJoystick::updateCamera(BOOL reset)
{
	static F32 last_delta[] = {0,0,0,0,0,0,0};
	static F32 delta[] = { 0,0,0,0,0,0,0 };

	LLWindow* window = gViewerWindow->getWindow();

	F32 time = gFrameIntervalSeconds;

	S32 axis[] = 
	{
		gSavedSettings.getS32("FlycamAxis0"),
		gSavedSettings.getS32("FlycamAxis1"),
		gSavedSettings.getS32("FlycamAxis2"),
		gSavedSettings.getS32("FlycamAxis3"),
		gSavedSettings.getS32("FlycamAxis4"),
		gSavedSettings.getS32("FlycamAxis5"),
		gSavedSettings.getS32("FlycamAxis6")
	};

	F32 axis_scale[] =
	{
		gSavedSettings.getF32("FlycamAxisScale0"),
		gSavedSettings.getF32("FlycamAxisScale1"),
		gSavedSettings.getF32("FlycamAxisScale2"),
		gSavedSettings.getF32("FlycamAxisScale3"),
		gSavedSettings.getF32("FlycamAxisScale4"),
		gSavedSettings.getF32("FlycamAxisScale5"),
		gSavedSettings.getF32("FlycamAxisScale6")
	};

	F32 dead_zone[] =
	{
		gSavedSettings.getF32("FlycamAxisDeadZone0"),
		gSavedSettings.getF32("FlycamAxisDeadZone1"),
		gSavedSettings.getF32("FlycamAxisDeadZone2"),
		gSavedSettings.getF32("FlycamAxisDeadZone3"),
		gSavedSettings.getF32("FlycamAxisDeadZone4"),
		gSavedSettings.getF32("FlycamAxisDeadZone5"),
		gSavedSettings.getF32("FlycamAxisDeadZone6")
	};

	if (reset)
	{
		sFlycamPosition = gCamera->getOrigin();
		sFlycamRotation = gCamera->getQuaternion();
		sFlycamZoom = gCamera->getView();

		for (U32 i = 0; i < 7; i++)
		{
			last_delta[i] = -window->getJoystickAxis(axis[i]);
			delta[i] = 0.f;
		}
		return;
	}

	F32 cur_delta[7];
	F32 feather = gSavedSettings.getF32("FlycamFeathering");
	BOOL absolute = gSavedSettings.getBOOL("FlycamAbsolute");

	for (U32 i = 0; i < 7; i++)
	{
		cur_delta[i] = -window->getJoystickAxis(axis[i]);	
		F32 tmp = cur_delta[i];
		if (absolute)
		{
			cur_delta[i] = cur_delta[i] - last_delta[i];
		}
		last_delta[i] = tmp;

		if (cur_delta[i] > 0)
		{
			cur_delta[i] = llmax(cur_delta[i]-dead_zone[i], 0.f);
		}
		else
		{
			cur_delta[i] = llmin(cur_delta[i]+dead_zone[i], 0.f);
		}
		cur_delta[i] *= axis_scale[i];
		
		if (!absolute)
		{
			cur_delta[i] *= time;
		}

		delta[i] = delta[i] + (cur_delta[i]-delta[i])*time*feather;
	}
	
	sFlycamPosition += LLVector3(delta) * sFlycamRotation;

	LLMatrix3 rot_mat(delta[3],
					  delta[4],
					  delta[5]);
	
	sFlycamRotation = LLQuaternion(rot_mat)*sFlycamRotation;

	if (gSavedSettings.getBOOL("FlycamAutoLeveling"))
	{
		LLMatrix3 level(sFlycamRotation);

		LLVector3 x = LLVector3(level.mMatrix[0]);
		LLVector3 y = LLVector3(level.mMatrix[1]);
		LLVector3 z = LLVector3(level.mMatrix[2]);

		y.mV[2] = 0.f;
		y.normVec();

		level.setRows(x,y,z);
		level.orthogonalize();
				
		LLQuaternion quat = LLQuaternion(level);
		sFlycamRotation = nlerp(llmin(feather*time,1.f), sFlycamRotation, quat);
	}

	if (gSavedSettings.getBOOL("FlycamZoomDirect"))
	{
		sFlycamZoom = last_delta[6]*axis_scale[6]+dead_zone[6];
	}
	else
	{
		sFlycamZoom += delta[6];
	}

	LLMatrix3 mat(sFlycamRotation);

	gCamera->setView(sFlycamZoom);
	gCamera->setOrigin(sFlycamPosition);
	gCamera->mXAxis = LLVector3(mat.mMatrix[0]);
	gCamera->mYAxis = LLVector3(mat.mMatrix[1]);
	gCamera->mZAxis = LLVector3(mat.mMatrix[2]);
}


void LLViewerJoystick::scanJoystick()
{
	if (!sOverrideCamera)
	{
		static U32 joystick_state = 0;
		static U32 button_state = 0;

		F32 xval = gViewerWindow->getWindow()->getJoystickAxis(0);
		F32 yval = gViewerWindow->getWindow()->getJoystickAxis(1);

		if (xval <= -0.5f)
		{
			if (!(joystick_state & 0x1))
			{
				gKeyboard->handleTranslatedKeyDown(KEY_PAD_LEFT, 0);
				joystick_state |= 0x1;
			}
		}
		else 
		{
			if (joystick_state & 0x1)
			{
				gKeyboard->handleTranslatedKeyUp(KEY_PAD_LEFT, 0);
				joystick_state &= ~0x1;
			}
		}
		if (xval >= 0.5f)
		{
			if (!(joystick_state & 0x2))
			{
				gKeyboard->handleTranslatedKeyDown(KEY_PAD_RIGHT, 0);
				joystick_state |= 0x2;
			}
		}
		else 
		{
			if (joystick_state & 0x2)
			{
				gKeyboard->handleTranslatedKeyUp(KEY_PAD_RIGHT, 0);
				joystick_state &= ~0x2;
			}
		}
		if (yval <= -0.5f)
		{
			if (!(joystick_state & 0x4))
			{
				gKeyboard->handleTranslatedKeyDown(KEY_PAD_UP, 0);
				joystick_state |= 0x4;
			}
		}
		else 
		{
			if (joystick_state & 0x4)
			{
				gKeyboard->handleTranslatedKeyUp(KEY_PAD_UP, 0);
				joystick_state &= ~0x4;
			}
		}
		if (yval >=  0.5f)
		{
			if (!(joystick_state & 0x8))
			{
				gKeyboard->handleTranslatedKeyDown(KEY_PAD_DOWN, 0);
				joystick_state |= 0x8;
			}
		}
		else 
		{
			if (joystick_state & 0x8)
			{
				gKeyboard->handleTranslatedKeyUp(KEY_PAD_DOWN, 0);
				joystick_state &= ~0x8;
			}
		}

		for( int i = 0; i < 15; i++ )
		{
			if ( gViewerWindow->getWindow()->getJoystickButton(i) & 0x80 )
			{
				if (!(button_state & (1<<i)))
				{
					gKeyboard->handleTranslatedKeyDown(KEY_BUTTON1+i, 0);
					button_state |= (1<<i);
				}
			}
			else
			{
				if (button_state & (1<<i))
				{
					gKeyboard->handleTranslatedKeyUp(KEY_BUTTON1+i, 0);
					button_state &= ~(1<<i);
				}
			}
		}
	}
}