1 | // Copyright (C) 2024 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QSGTRANSFORM_P_H |
5 | #define QSGTRANSFORM_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists for the convenience |
12 | // of a number of Qt sources files. This header file may change from |
13 | // version to version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QSharedPointer> |
19 | #include <QMatrix4x4> |
20 | #include <QtQuick/qtquickexports.h> |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | class Q_QUICK_EXPORT QSGTransform |
25 | { |
26 | public: |
27 | void setMatrix(const QMatrix4x4 &matrix) |
28 | { |
29 | if (matrix.isIdentity()) |
30 | m_matrixPtr.clear(); |
31 | else |
32 | m_matrixPtr = QSharedPointer<QMatrix4x4>::create(arguments: matrix); |
33 | m_invertedPtr.clear(); |
34 | } |
35 | |
36 | QMatrix4x4 matrix() const |
37 | { |
38 | return m_matrixPtr ? *m_matrixPtr : m_identity; |
39 | } |
40 | |
41 | bool isIdentity() const |
42 | { |
43 | return !m_matrixPtr; |
44 | } |
45 | |
46 | bool operator==(const QMatrix4x4 &other) const |
47 | { |
48 | return m_matrixPtr ? (other == *m_matrixPtr) : other.isIdentity(); |
49 | } |
50 | |
51 | bool operator!=(const QMatrix4x4 &other) const |
52 | { |
53 | return !(*this == other); |
54 | } |
55 | |
56 | bool operator==(const QSGTransform &other) const |
57 | { |
58 | return (m_matrixPtr == other.m_matrixPtr) |
59 | || (m_matrixPtr && other.m_matrixPtr && *m_matrixPtr == *other.m_matrixPtr); |
60 | } |
61 | |
62 | bool operator!=(const QSGTransform &other) const |
63 | { |
64 | return !(*this == other); |
65 | } |
66 | |
67 | int compareTo(const QSGTransform &other) const |
68 | { |
69 | int diff = 0; |
70 | if (m_matrixPtr != other.m_matrixPtr) { |
71 | if (m_matrixPtr.isNull()) { |
72 | diff = -1; |
73 | } else if (other.m_matrixPtr.isNull()) { |
74 | diff = 1; |
75 | } else { |
76 | const float *ptr1 = m_matrixPtr->constData(); |
77 | const float *ptr2 = other.m_matrixPtr->constData(); |
78 | for (int i = 0; i < 16 && !diff; i++) { |
79 | float d = ptr1[i] - ptr2[i]; |
80 | if (d != 0) |
81 | diff = (d > 0) ? 1 : -1; |
82 | } |
83 | } |
84 | } |
85 | return diff; |
86 | } |
87 | |
88 | const float *invertedData() const |
89 | { |
90 | if (!m_matrixPtr) |
91 | return m_identity.constData(); |
92 | if (!m_invertedPtr) |
93 | m_invertedPtr = QSharedPointer<QMatrix4x4>::create(arguments: m_matrixPtr->inverted()); |
94 | return m_invertedPtr->constData(); |
95 | } |
96 | |
97 | private: |
98 | static QMatrix4x4 m_identity; |
99 | QSharedPointer<QMatrix4x4> m_matrixPtr; |
100 | mutable QSharedPointer<QMatrix4x4> m_invertedPtr; |
101 | }; |
102 | |
103 | QT_END_NAMESPACE |
104 | |
105 | #endif // QSGTRANSFORM_P_H |
106 |