VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
InterpolationLogic.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef INTERPOLATIONLOGIC_H
18 #define INTERPOLATIONLOGIC_H
19 
20 #include <QSet>
21 #include <memory>
22 #include <queue>
23 #include "Logic.h"
24 #include "audio/SoundEffectInstance.h"
25 
26 class TriggerEvent;
27 
39 class InterpolationLogic : public Logic {
40  Q_OBJECT
41 
45  Q_PROPERTY(QObject* target READ getTarget WRITE setTarget NOTIFY targetChanged)
62  Q_PROPERTY(float forwardDuration READ getForwardDuration WRITE setForwardDuration
68  Q_PROPERTY(float backwardDuration READ getBackwardDuration WRITE setBackwardDuration
76  Q_PROPERTY(float smoothing READ getSmoothing WRITE setSmoothing
81  Q_PROPERTY(State state READ getState WRITE setState NOTIFY stateChanged)
87 public:
91  Q_ENUMS(State)
92  enum State {
96  Paused = 0,
105  };
106 
111  explicit InterpolationLogic(QObject* parent = nullptr);
112 
116  QObject* getTarget() const { return mUnresolvedTarget; }
121  void setTarget(QObject* value);
125  const QString& getTargetProperty() const { return mUnresolvedTargetProperty; }
130  void setTargetProperty(const QString& value);
134  QVariant getBeginValue() const { return mBeginValue; }
139  void setBeginValue(QVariant value);
143  QVariant getEndValue() const { return mEndValue; }
148  void setEndValue(QVariant value);
152  float getForwardDuration() const { return mForwardDuration; }
157  void setForwardDuration(float value);
161  float getBackwardDuration() const { return mBackwardDuration; }
166  void setBackwardDuration(float value);
170  float getSmoothing() const { return mSmoothing; }
175  void setSmoothing(float value);
179  State getState() const { return mState; }
184  void setState(State value);
188  const QString& getInterpolatingSound() const { return mInterpolatingSound; }
193  void setInterpolatingSound(const QString& value);
194 
208  virtual void init() override;
213  virtual void update() override;
214 
215 signals:
219  void targetChanged();
223  void targetPropertyChanged();
227  void beginValueChanged();
231  void endValueChanged();
235  void forwardDurationChanged();
243  void smoothingChanged();
247  void stateChanged();
252 
253 private:
254  enum Behavior {
255  Bidirectional = 0,
256  ForwardOnly,
257  BackwardOnly,
258  };
259 
260  void resolveTargetAndProperty();
261  QVariant getInterpolatedValue();
262  QVariant getInterpolatedValue(int primitiveType);
263  void updateInterpolatingSound();
264 
265  QObject* mUnresolvedTarget = nullptr;
266  QString mUnresolvedTargetProperty;
267  QObject* mTarget = nullptr;
268  QString mTargetProperty;
269  QString mTargetNonPrimitiveProperty;
270  QVariant mBeginValue;
271  QVariant mEndValue;
272  float mForwardDuration = 1.0f;
273  float mBackwardDuration = 1.0f;
274  bool mHasSmoothing = false;
275  float mSmoothing = 0.0f;
276  State mState = Paused;
277  Behavior mBehavior = Bidirectional;
278  float mTime = 0.0f;
279  QString mInterpolatingSound;
280  SoundEffectInstance mInterpolatingSoundInstance;
281 
282  const static QSet<int> sValidPrimitiveTypes;
283  const static QSet<int> sValidNonPrimitiveTypes;
284 };
285 Q_DECLARE_METATYPE(InterpolationLogic*)
286 
287 #endif // INTERPOLATIONLOGIC_H
QVariant endValue
Value at which to end the interpolation of target's targetProperty.
Definition: InterpolationLogic.h:58
void forwardDurationChanged()
Emitted when forwardDuration changes.
void interpolatingSoundChanged()
Emitted when interpolatingSound changes.
float getBackwardDuration() const
Returns backwardDuration.
Definition: InterpolationLogic.h:161
State state
Current state of the interpolation.
Definition: InterpolationLogic.h:81
An instance of a sound effect.
Definition: SoundEffectInstance.h:36
QVariant getBeginValue() const
Returns beginValue.
Definition: InterpolationLogic.h:134
void setBeginValue(QVariant value)
Sets beginValue.
float forwardDuration
Time, in seconds, over which to perform the InterpolationLogic::Forward interpolation.
Definition: InterpolationLogic.h:63
virtual void update() override
Performs an Engine::TIME_STEP_S based step in the interpolation if state is not InterpolationLogic::P...
QString interpolatingSound
Name of sound asset to play and loop while interpolating.
Definition: InterpolationLogic.h:86
void setState(State value)
Sets state.
void targetChanged()
Emitted when target changes.
float backwardDuration
Time, in seconds, over which to perform the InterpolationLogic::Backward interpolation.
Definition: InterpolationLogic.h:69
void setSmoothing(float value)
Sets smoothing.
void setInterpolatingSound(const QString &value)
Sets interpolatingSound.
void setTarget(QObject *value)
Sets target.
void setBackwardDuration(float value)
Sets backwardDuration.
void backwardDurationChanged()
Emitted when backwardDuration changes.
float getSmoothing() const
Returns smoothing.
Definition: InterpolationLogic.h:170
QVariant beginValue
Value at which to start the interpolation of target's targetProperty.
Definition: InterpolationLogic.h:54
void beginValueChanged()
Emitted when beginValue changes.
virtual void init() override
Resolves target and targetProperty.
Abstract base class for events which can be handled by Triggers.
Definition: Trigger.h:68
QObject target
Target object whose targetProperty value will be interpolated.
Definition: InterpolationLogic.h:45
QString targetProperty
Q_PROPERTY of target whose value will be interpolated.
Definition: InterpolationLogic.h:50
const QString & getTargetProperty() const
Returns targetProperty.
Definition: InterpolationLogic.h:125
QVariant getEndValue() const
Returns endValue.
Definition: InterpolationLogic.h:143
Non-visual entities in the QML item tree that define behavior for their parent Actor.
Definition: Logic.h:31
void setEndValue(QVariant value)
Sets endValue.
void stateChanged()
Emitted when state changes.
void endValueChanged()
Emitted when endValue changes.
Interpolation is currently paused, not changing targetProperty on update().
Definition: InterpolationLogic.h:96
void setTargetProperty(const QString &value)
Sets targetProperty.
const QString & getInterpolatingSound() const
Returns interpolatingSound.
Definition: InterpolationLogic.h:188
State getState() const
Returns state.
Definition: InterpolationLogic.h:179
InterpolationLogic(QObject *parent=nullptr)
Constructs an InterpolationLogic.
QObject * getTarget() const
Returns target.
Definition: InterpolationLogic.h:116
Interpolates target's targetProperty value between beginValue and endValue.
Definition: InterpolationLogic.h:39
void smoothingChanged()
Emitted when smoothing changes.
Currently interpolating targetProperty from beginValue to endValue.
Definition: InterpolationLogic.h:100
void setForwardDuration(float value)
Sets forwardDuration.
State
State of the interpolation.
Definition: InterpolationLogic.h:92
QObject * parent() const
Currently interpolating targetProperty from endValue to beginValue.
Definition: InterpolationLogic.h:104
float smoothing
Factor between linear and smooth interpolation.
Definition: InterpolationLogic.h:77
float getForwardDuration() const
Returns forwardDuration.
Definition: InterpolationLogic.h:152
void targetPropertyChanged()
Emitted when targetProperty changes.