| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). | 
| 4 | ** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies). | 
| 5 | ** Contact: https://www.qt.io/licensing/ | 
| 6 | ** | 
| 7 | ** This file is part of the Qt3D module of the Qt Toolkit. | 
| 8 | ** | 
| 9 | ** $QT_BEGIN_LICENSE:BSD$ | 
| 10 | ** Commercial License Usage | 
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 12 | ** accordance with the commercial license agreement provided with the | 
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 16 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 17 | ** | 
| 18 | ** BSD License Usage | 
| 19 | ** Alternatively, you may use this file under the terms of the BSD license | 
| 20 | ** as follows: | 
| 21 | ** | 
| 22 | ** "Redistribution and use in source and binary forms, with or without | 
| 23 | ** modification, are permitted provided that the following conditions are | 
| 24 | ** met: | 
| 25 | **   * Redistributions of source code must retain the above copyright | 
| 26 | **     notice, this list of conditions and the following disclaimer. | 
| 27 | **   * Redistributions in binary form must reproduce the above copyright | 
| 28 | **     notice, this list of conditions and the following disclaimer in | 
| 29 | **     the documentation and/or other materials provided with the | 
| 30 | **     distribution. | 
| 31 | **   * Neither the name of The Qt Company Ltd nor the names of its | 
| 32 | **     contributors may be used to endorse or promote products derived | 
| 33 | **     from this software without specific prior written permission. | 
| 34 | ** | 
| 35 | ** | 
| 36 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 37 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| 38 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| 39 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 40 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 41 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 42 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| 43 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| 44 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 45 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| 46 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
| 47 | ** | 
| 48 | ** $QT_END_LICENSE$ | 
| 49 | ** | 
| 50 | ****************************************************************************/ | 
| 51 |  | 
| 52 | #include "orbittransformcontroller.h" | 
| 53 |  | 
| 54 | #include <Qt3DCore/qtransform.h> | 
| 55 |  | 
| 56 | QT_BEGIN_NAMESPACE | 
| 57 |  | 
| 58 | OrbitTransformController::OrbitTransformController(QObject *parent) | 
| 59 |     : QObject(parent) | 
| 60 |     , m_target(nullptr) | 
| 61 |     , m_matrix() | 
| 62 |     , m_radius(1.0f) | 
| 63 |     , m_angle(0.0f) | 
| 64 | { | 
| 65 | } | 
| 66 |  | 
| 67 | void OrbitTransformController::setTarget(Qt3DCore::QTransform *target) | 
| 68 | { | 
| 69 |     if (m_target != target) { | 
| 70 |         m_target = target; | 
| 71 |         emit targetChanged(); | 
| 72 |     } | 
| 73 | } | 
| 74 |  | 
| 75 | Qt3DCore::QTransform *OrbitTransformController::target() const | 
| 76 | { | 
| 77 |     return m_target; | 
| 78 | } | 
| 79 |  | 
| 80 | void OrbitTransformController::setRadius(float radius) | 
| 81 | { | 
| 82 |     if (!qFuzzyCompare(p1: radius, p2: m_radius)) { | 
| 83 |         m_radius = radius; | 
| 84 |         updateMatrix(); | 
| 85 |         emit radiusChanged(); | 
| 86 |     } | 
| 87 | } | 
| 88 |  | 
| 89 | float OrbitTransformController::radius() const | 
| 90 | { | 
| 91 |     return m_radius; | 
| 92 | } | 
| 93 |  | 
| 94 | void OrbitTransformController::setAngle(float angle) | 
| 95 | { | 
| 96 |     if (!qFuzzyCompare(p1: angle, p2: m_angle)) { | 
| 97 |         m_angle = angle; | 
| 98 |         updateMatrix(); | 
| 99 |         emit angleChanged(); | 
| 100 |     } | 
| 101 | } | 
| 102 |  | 
| 103 | float OrbitTransformController::angle() const | 
| 104 | { | 
| 105 |     return m_angle; | 
| 106 | } | 
| 107 |  | 
| 108 | void OrbitTransformController::updateMatrix() | 
| 109 | { | 
| 110 |     m_matrix.setToIdentity(); | 
| 111 |     m_matrix.rotate(angle: m_angle, vector: QVector3D(0.0f, 1.0f, 0.0f)); | 
| 112 |     m_matrix.translate(x: m_radius, y: 0.0f, z: 0.0f); | 
| 113 |     m_target->setMatrix(m_matrix); | 
| 114 | } | 
| 115 |  | 
| 116 | QT_END_NAMESPACE | 
| 117 |  |