Line data Source code
1 : /* Copyright (C) 2010 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "InputProcessor.h"
21 :
22 : #include "ps/Game.h"
23 : #include "graphics/Camera.h"
24 : #include "graphics/GameView.h"
25 : #include "maths/Quaternion.h"
26 :
27 : static float g_ViewZoomSmoothness = 0.02f;
28 : static float g_ViewRotateScale = 0.02f;
29 :
30 0 : static void Rotate(CCamera& camera, float speed)
31 : {
32 0 : CVector3D upwards(0.0f, 1.0f, 0.0f);
33 0 : CVector3D origin = camera.m_Orientation.GetTranslation();
34 0 : CVector3D pivot = camera.GetFocus();
35 0 : CVector3D delta = origin - pivot;
36 :
37 0 : CQuaternion r;
38 0 : r.FromAxisAngle(upwards, speed);
39 0 : delta = r.Rotate(delta);
40 0 : camera.m_Orientation.Translate(-origin);
41 0 : camera.m_Orientation.Rotate(r);
42 0 : camera.m_Orientation.Translate(pivot + delta);
43 0 : }
44 :
45 0 : bool InputProcessor::ProcessInput(GameLoopState* state)
46 : {
47 0 : if (! g_Game)
48 0 : return false;
49 :
50 0 : CCamera* camera = g_Game->GetView()->GetCamera();
51 :
52 0 : CVector3D leftwards = camera->m_Orientation.GetLeft();
53 :
54 0 : CVector3D inwards = camera->m_Orientation.GetIn();
55 :
56 : // Calculate a vector pointing forwards, parallel to the ground
57 0 : CVector3D forwards = inwards;
58 0 : forwards.Y = 0.0f;
59 0 : if (forwards.Length() < 0.001f) // be careful if the camera is looking straight down
60 0 : forwards = CVector3D(1.f, 0.f, 0.f);
61 : else
62 0 : forwards.Normalize();
63 :
64 0 : bool moved = false;
65 :
66 0 : GameLoopState::Input& input = state->input;
67 :
68 0 : if (state->input.scrollSpeed[0] != 0.0f)
69 : {
70 0 : camera->m_Orientation.Translate(forwards * (input.scrollSpeed[0] * state->realFrameLength));
71 0 : moved = true;
72 : }
73 :
74 0 : if (state->input.scrollSpeed[1] != 0.0f)
75 : {
76 0 : camera->m_Orientation.Translate(forwards * (-input.scrollSpeed[1] * state->realFrameLength));
77 0 : moved = true;
78 : }
79 :
80 0 : if (state->input.scrollSpeed[2] != 0.0f)
81 : {
82 0 : camera->m_Orientation.Translate(leftwards * (input.scrollSpeed[2] * state->realFrameLength));
83 0 : moved = true;
84 : }
85 :
86 0 : if (state->input.scrollSpeed[3] != 0.0f)
87 : {
88 0 : camera->m_Orientation.Translate(leftwards * (-input.scrollSpeed[3] * state->realFrameLength));
89 0 : moved = true;
90 : }
91 :
92 0 : if (state->input.scrollSpeed[4] != 0.0f)
93 : {
94 0 : Rotate(*camera, input.scrollSpeed[4] * state->realFrameLength * g_ViewRotateScale);
95 0 : moved = true;
96 : }
97 :
98 0 : if (state->input.scrollSpeed[5] != 0.0f)
99 : {
100 0 : Rotate(*camera, -input.scrollSpeed[5] * state->realFrameLength * g_ViewRotateScale);
101 0 : moved = true;
102 : }
103 :
104 0 : if (state->input.zoomDelta != 0.0f)
105 : {
106 0 : float zoom_proportion = powf(g_ViewZoomSmoothness, state->realFrameLength);
107 0 : camera->m_Orientation.Translate(inwards * (input.zoomDelta * (1.0f - zoom_proportion)));
108 0 : input.zoomDelta *= zoom_proportion;
109 :
110 0 : if (fabsf(input.zoomDelta) < 0.1f)
111 0 : input.zoomDelta = 0.0f;
112 :
113 0 : moved = true;
114 : }
115 :
116 0 : if (moved)
117 0 : camera->UpdateFrustum();
118 :
119 0 : return moved;
120 : }
|