File indexing completed on 2025-01-26 05:19:14

0001 /* Atelier KDE Printer Host for 3D Printing
0002     Copyright (C) <2018>
0003     Author: Kevin Ottens - ervin@kde.org
0004 
0005     This program is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU General Public License as
0007     published by the Free Software Foundation; either version 3 of
0008     the License or any later version accepted by the membership of
0009     KDE e.V. (or its successor approved by the membership of KDE
0010     e.V.), which shall act as a proxy defined in Section 14 of
0011     version 3 of the license.
0012 
0013     This program is distributed in the hope that it will be useful,
0014     but WITHOUT ANY WARRANTY; without even the implied warranty of
0015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016     GNU General Public License for more details.
0017 
0018     You should have received a copy of the GNU General Public License
0019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0020 */
0021 
0022 #include "cameracontroller.h"
0023 
0024 #include <Qt3DRender/QCamera>
0025 
0026 namespace
0027 {
0028 inline float clampInputs(float input1, float input2)
0029 {
0030     float axisValue = input1 + input2;
0031     return (axisValue < -1) ? -1 : (axisValue > 1) ? 1 : axisValue;
0032 }
0033 
0034 inline float zoomDistance(QVector3D firstPoint, QVector3D secondPoint)
0035 {
0036     return (secondPoint - firstPoint).lengthSquared();
0037 }
0038 }
0039 
0040 CameraController::CameraController(QNode *parent)
0041     : Qt3DExtras::QAbstractCameraController(parent)
0042 {
0043 }
0044 
0045 void CameraController::moveCamera(const Qt3DExtras::QAbstractCameraController::InputState &state, float dt)
0046 {
0047     auto cam = camera();
0048     if (!cam) {
0049         return;
0050     }
0051 
0052     // Mouse
0053     if (state.leftMouseButtonActive) {
0054         cam->pan(state.rxAxisValue * lookSpeed() * dt);
0055         cam->tilt(state.ryAxisValue * lookSpeed() * dt);
0056     }
0057 
0058     // Keyboard
0059     cam->panAboutViewCenter((state.txAxisValue * lookSpeed()) * dt, QVector3D(0.0f, 0.0f, 1.0f));
0060     cam->translate(QVector3D(0.0f, 0.0f, state.tyAxisValue * linearSpeed() * dt), Qt3DRender::QCamera::DontTranslateViewCenter);
0061 }