Line data Source code
1 : /* Copyright (C) 2026 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 "graphics/Camera.h" 23 : #include "graphics/GameView.h" 24 : #include "maths/Matrix3D.h" 25 : #include "maths/Quaternion.h" 26 : #include "maths/Vector3D.h" 27 : #include "ps/Game.h" 28 : #include "tools/atlas/GameInterface/GameLoop.h" 29 : 30 : #include <cmath> 31 : 32 : static float g_ViewZoomSmoothness = 0.02f; 33 : static float g_ViewRotateScale = 0.02f; 34 : 35 0 : static void Rotate(CCamera& camera, float speed) 36 : { 37 0 : CVector3D upwards(0.0f, 1.0f, 0.0f); 38 0 : CVector3D origin = camera.m_Orientation.GetTranslation(); 39 0 : CVector3D pivot = camera.GetFocus(); 40 0 : CVector3D delta = origin - pivot; 41 : 42 : CQuaternion r; 43 0 : r.FromAxisAngle(upwards, speed); 44 0 : delta = r.Rotate(delta); 45 0 : camera.m_Orientation.Translate(-origin); 46 0 : camera.m_Orientation.Rotate(r); 47 0 : camera.m_Orientation.Translate(pivot + delta); 48 0 : } 49 : 50 0 : bool InputProcessor::ProcessInput(GameLoopState* state) 51 : { 52 0 : if (! g_Game) 53 : return false; 54 : 55 0 : CCamera camera{g_Game->GetView()->GetCamera()}; 56 : 57 0 : const CVector3D leftwards{camera.m_Orientation.GetLeft()}; 58 0 : const CVector3D inwards{camera.m_Orientation.GetIn()}; 59 : 60 : // Calculate a vector pointing forwards, parallel to the ground 61 0 : CVector3D forwards = inwards; 62 0 : forwards.Y = 0.0f; 63 0 : if (forwards.Length() < 0.001f) // be careful if the camera is looking straight down 64 0 : forwards = CVector3D(1.f, 0.f, 0.f); 65 : else 66 0 : forwards.Normalize(); 67 : 68 0 : bool moved = false; 69 : 70 0 : GameLoopState::Input& input = state->input; 71 : 72 0 : if (state->input.scrollSpeed[0] != 0.0f) 73 : { 74 0 : camera.m_Orientation.Translate(forwards * (input.scrollSpeed[0] * state->realFrameLength)); 75 0 : moved = true; 76 : } 77 : 78 0 : if (state->input.scrollSpeed[1] != 0.0f) 79 : { 80 0 : camera.m_Orientation.Translate(forwards * (-input.scrollSpeed[1] * state->realFrameLength)); 81 0 : moved = true; 82 : } 83 : 84 0 : if (state->input.scrollSpeed[2] != 0.0f) 85 : { 86 0 : camera.m_Orientation.Translate(leftwards * (input.scrollSpeed[2] * state->realFrameLength)); 87 0 : moved = true; 88 : } 89 : 90 0 : if (state->input.scrollSpeed[3] != 0.0f) 91 : { 92 0 : camera.m_Orientation.Translate(leftwards * (-input.scrollSpeed[3] * state->realFrameLength)); 93 0 : moved = true; 94 : } 95 : 96 0 : if (state->input.scrollSpeed[4] != 0.0f) 97 : { 98 0 : Rotate(camera, input.scrollSpeed[4] * state->realFrameLength * g_ViewRotateScale); 99 : moved = true; 100 : } 101 : 102 0 : if (state->input.scrollSpeed[5] != 0.0f) 103 : { 104 0 : Rotate(camera, -input.scrollSpeed[5] * state->realFrameLength * g_ViewRotateScale); 105 : moved = true; 106 : } 107 : 108 0 : if (state->input.zoomDelta != 0.0f) 109 : { 110 0 : float zoom_proportion = powf(g_ViewZoomSmoothness, state->realFrameLength); 111 0 : camera.m_Orientation.Translate(inwards * (input.zoomDelta * (1.0f - zoom_proportion))); 112 0 : input.zoomDelta *= zoom_proportion; 113 : 114 0 : if (fabsf(input.zoomDelta) < 0.1f) 115 0 : input.zoomDelta = 0.0f; 116 : 117 : moved = true; 118 : } 119 : 120 0 : if (state->input.rotateDelta != 0.f) 121 : { 122 0 : float angle{4 * state->realFrameLength}; 123 0 : if (input.rotateDelta > 0 ) 124 : { 125 0 : if (angle > input.rotateDelta) 126 : { 127 0 : angle = input.rotateDelta; 128 0 : input.rotateDelta = 0.f; 129 : } 130 : else 131 0 : input.rotateDelta -= angle; 132 : } 133 : else 134 : { 135 0 : angle *= -1; 136 0 : if (angle < input.rotateDelta) 137 : { 138 0 : angle = input.rotateDelta; 139 0 : input.rotateDelta = 0.f; 140 : } 141 : else 142 0 : input.rotateDelta -= angle; 143 : } 144 0 : Rotate(camera, angle); 145 : 146 : moved = true; 147 : } 148 : 149 0 : if (moved) 150 : { 151 0 : camera.UpdateFrustum(); 152 0 : g_Game->GetView()->SetCamera(camera); 153 : } 154 : 155 0 : return moved; 156 0 : }