1 | // Copyright (C) 2016 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 "qquickspritegoal_p.h" |
5 | #include <private/qquickspriteengine_p.h> |
6 | #include <private/qquicksprite_p.h> |
7 | #include "qquickimageparticle_p.h" |
8 | #include <QDebug> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | /*! |
13 | \qmltype SpriteGoal |
14 | \instantiates QQuickSpriteGoalAffector |
15 | \inqmlmodule QtQuick.Particles |
16 | \ingroup qtquick-images-sprites |
17 | \inherits Affector |
18 | \brief For changing the state of a sprite particle. |
19 | |
20 | */ |
21 | /*! |
22 | \qmlproperty string QtQuick.Particles::SpriteGoal::goalState |
23 | |
24 | The name of the Sprite which the affected particles should move to. |
25 | |
26 | Sprite states have defined durations and transitions between them, setting goalState |
27 | will cause it to disregard any path weightings (including 0) and head down the path |
28 | which will reach the goalState quickest. It will pass through intermediate states |
29 | on that path. |
30 | */ |
31 | /*! |
32 | \qmlproperty bool QtQuick.Particles::SpriteGoal::jump |
33 | |
34 | If true, affected sprites will jump directly to the goal state instead of taking the |
35 | shortest valid path to get there. They will also not finish their current state, |
36 | but immediately move to the beginning of the goal state. |
37 | |
38 | Default is false. |
39 | */ |
40 | /*! |
41 | \qmlproperty bool QtQuick.Particles::SpriteGoal::systemStates |
42 | |
43 | deprecated, use GroupGoal instead |
44 | */ |
45 | |
46 | QQuickSpriteGoalAffector::QQuickSpriteGoalAffector(QQuickItem *parent) : |
47 | QQuickParticleAffector(parent), |
48 | m_goalIdx(-1), |
49 | m_lastEngine(nullptr), |
50 | m_jump(false), |
51 | m_systemStates(false), |
52 | m_notUsingEngine(false) |
53 | { |
54 | m_ignoresTime = true; |
55 | } |
56 | |
57 | void QQuickSpriteGoalAffector::updateStateIndex(QQuickStochasticEngine* e) |
58 | { |
59 | if (m_systemStates){ |
60 | m_goalIdx = m_system->groupIds[m_goalState]; |
61 | }else{ |
62 | m_lastEngine = e; |
63 | for (int i=0; i<e->stateCount(); i++){ |
64 | if (e->state(idx: i)->name() == m_goalState){ |
65 | m_goalIdx = i; |
66 | return; |
67 | } |
68 | } |
69 | m_goalIdx = -1;//Can't find it |
70 | } |
71 | } |
72 | |
73 | void QQuickSpriteGoalAffector::setGoalState(const QString &arg) |
74 | { |
75 | if (m_goalState != arg) { |
76 | m_goalState = arg; |
77 | emit goalStateChanged(arg); |
78 | if (m_goalState.isEmpty()) |
79 | m_goalIdx = -1; |
80 | else |
81 | m_goalIdx = -2; |
82 | } |
83 | } |
84 | |
85 | bool QQuickSpriteGoalAffector::affectParticle(QQuickParticleData *d, qreal dt) |
86 | { |
87 | Q_UNUSED(dt); |
88 | QQuickStochasticEngine *engine = nullptr; |
89 | if (!m_systemStates){ |
90 | //TODO: Affect all engines |
91 | for (QQuickParticlePainter *p : m_system->groupData[d->groupId]->painters) { |
92 | if (qobject_cast<QQuickImageParticle*>(object: p)) |
93 | engine = qobject_cast<QQuickImageParticle*>(object: p)->spriteEngine(); |
94 | } |
95 | } else { |
96 | engine = m_system->stateEngine; |
97 | if (!engine) |
98 | m_notUsingEngine = true; |
99 | } |
100 | if (!engine && !m_notUsingEngine) |
101 | return false; |
102 | |
103 | if (m_goalIdx == -2 || engine != m_lastEngine) |
104 | updateStateIndex(e: engine); |
105 | int index = d->index; |
106 | if (m_systemStates) |
107 | index = d->systemIndex; |
108 | if (m_notUsingEngine){//systemStates && no stochastic states defined. So cut out the engine |
109 | //TODO: It's possible to move to a group that is intermediate and not used by painters or emitters - but right now that will redirect to the default group |
110 | m_system->moveGroups(d, newGIdx: m_goalIdx); |
111 | }else if (engine->curState(index) != m_goalIdx){ |
112 | engine->setGoal(state: m_goalIdx, sprite: index, jump: m_jump); |
113 | return true; //Doesn't affect particle data, but necessary for onceOff |
114 | } |
115 | return false; |
116 | } |
117 | |
118 | QT_END_NAMESPACE |
119 | |
120 | #include "moc_qquickspritegoal_p.cpp" |
121 | |