| 1 | // Copyright (C) 2021 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 | #include <QtQml/qqmlinfo.h> |
| 5 | #include <QtCore/QtMath> |
| 6 | #include "qquickgravity_p.h" |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | /*! |
| 10 | \qmltype Gravity |
| 11 | \nativetype QQuickGravityAffector |
| 12 | \inqmlmodule QtQuick.Particles |
| 13 | \ingroup qtquick-particles |
| 14 | \inherits ParticleAffector |
| 15 | \brief For applying acceleration in an angle. |
| 16 | |
| 17 | This element will accelerate all affected particles to a vector of |
| 18 | the specified magnitude in the specified angle. If the angle and acceleration do |
| 19 | not vary, it is more efficient to set the specified acceleration on the Emitter. |
| 20 | |
| 21 | This element models the gravity of a massive object whose center of |
| 22 | gravity is far away (and thus the gravitational pull is effectively constant |
| 23 | across the scene). To model the gravity of an object near or inside the scene, |
| 24 | use PointAttractor. |
| 25 | */ |
| 26 | |
| 27 | /*! |
| 28 | \qmlproperty real QtQuick.Particles::Gravity::magnitude |
| 29 | |
| 30 | Pixels per second that objects will be accelerated by. |
| 31 | */ |
| 32 | void QQuickGravityAffector::setMagnitude(qreal arg) |
| 33 | { |
| 34 | if (m_magnitude != arg) { |
| 35 | m_magnitude = arg; |
| 36 | m_needRecalc = true; |
| 37 | emit magnitudeChanged(arg); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | qreal QQuickGravityAffector::magnitude() const |
| 42 | { |
| 43 | return m_magnitude; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /*! |
| 48 | \qmlproperty real QtQuick.Particles::Gravity::acceleration |
| 49 | \deprecated |
| 50 | |
| 51 | \warning The name for this property has changed to magnitude, use it instead. |
| 52 | */ |
| 53 | void QQuickGravityAffector::setAcceleration(qreal arg) |
| 54 | { |
| 55 | qmlWarning(me: this) << "The acceleration property is deprecated. Please use magnitude instead."; |
| 56 | setMagnitude(arg); |
| 57 | } |
| 58 | |
| 59 | /*! |
| 60 | \qmlproperty real QtQuick.Particles::Gravity::angle |
| 61 | |
| 62 | Angle of acceleration. |
| 63 | */ |
| 64 | void QQuickGravityAffector::setAngle(qreal arg) |
| 65 | { |
| 66 | if (m_angle != arg) { |
| 67 | m_angle = arg; |
| 68 | m_needRecalc = true; |
| 69 | emit angleChanged(arg); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | qreal QQuickGravityAffector::angle() const |
| 74 | { |
| 75 | return m_angle; |
| 76 | } |
| 77 | |
| 78 | QQuickGravityAffector::QQuickGravityAffector(QQuickItem *parent) : |
| 79 | QQuickParticleAffector(parent), m_magnitude(-10), m_angle(90), m_needRecalc(true) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | bool QQuickGravityAffector::affectParticle(QQuickParticleData *d, qreal dt) |
| 84 | { |
| 85 | if (!m_magnitude) |
| 86 | return false; |
| 87 | if (m_needRecalc) { |
| 88 | m_needRecalc = false; |
| 89 | m_dx = m_magnitude * qCos(v: qDegreesToRadians(degrees: m_angle)); |
| 90 | m_dy = m_magnitude * qSin(v: qDegreesToRadians(degrees: m_angle)); |
| 91 | } |
| 92 | |
| 93 | d->setInstantaneousVX(vx: d->curVX(particleSystem: m_system) + m_dx*dt, particleSystem: m_system); |
| 94 | d->setInstantaneousVY(vy: d->curVY(particleSystem: m_system) + m_dy*dt, particleSystem: m_system); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
| 100 | QT_END_NAMESPACE |
| 101 | |
| 102 | #include "moc_qquickgravity_p.cpp" |
| 103 |
