1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsCorePrerequisites.h"
6#include "Audio/BsAudioSource.h"
7#include "Scene/BsComponent.h"
8
9namespace bs
10{
11 /** @addtogroup Components-Core
12 * @{
13 */
14
15 /**
16 * @copydoc AudioSource
17 *
18 * @note Wraps AudioSource as a Component.
19 */
20 class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Audio,n:AudioSource) CAudioSource : public Component
21 {
22 public:
23 CAudioSource(const HSceneObject& parent);
24 virtual ~CAudioSource() = default;
25
26 /** @copydoc AudioSource::setClip */
27 BS_SCRIPT_EXPORT(n:Clip,pr:setter)
28 void setClip(const HAudioClip& clip);
29
30 /** @copydoc AudioSource::getClip */
31 BS_SCRIPT_EXPORT(n:Clip,pr:getter)
32 HAudioClip getClip() const { return mAudioClip; }
33
34 /** @copydoc AudioSource::setVolume */
35 BS_SCRIPT_EXPORT(n:Volume,pr:setter,range:[0,1],slider)
36 void setVolume(float volume);
37
38 /** @copydoc AudioSource::getVolume */
39 BS_SCRIPT_EXPORT(n:Volume,pr:getter)
40 float getVolume() const { return mVolume; }
41
42 /** @copydoc AudioSource::setPitch */
43 BS_SCRIPT_EXPORT(n:Pitch,pr:setter)
44 void setPitch(float pitch);
45
46 /** @copydoc AudioSource::getPitch */
47 BS_SCRIPT_EXPORT(n:Pitch,pr:getter)
48 float getPitch() const { return mPitch; }
49
50 /** @copydoc AudioSource::setIsLooping */
51 BS_SCRIPT_EXPORT(n:Loop,pr:setter)
52 void setIsLooping(bool loop);
53
54 /** @copydoc AudioSource::getIsLooping */
55 BS_SCRIPT_EXPORT(n:Loop,pr:getter)
56 bool getIsLooping() const { return mLoop; }
57
58 /** @copydoc AudioSource::setPriority */
59 BS_SCRIPT_EXPORT(n:Priority,pr:setter)
60 void setPriority(UINT32 priority);
61
62 /** @copydoc AudioSource::getPriority */
63 BS_SCRIPT_EXPORT(n:Priority,pr:getter)
64 UINT32 getPriority() const { return mPriority; }
65
66 /** @copydoc AudioSource::setMinDistance */
67 BS_SCRIPT_EXPORT(n:MinDistance,pr:setter)
68 void setMinDistance(float distance);
69
70 /** @copydoc AudioSource::getMinDistance */
71 BS_SCRIPT_EXPORT(n:MinDistance,pr:getter)
72 float getMinDistance() const { return mMinDistance; }
73
74 /** @copydoc AudioSource::setAttenuation */
75 BS_SCRIPT_EXPORT(n:Attenuation,pr:setter)
76 void setAttenuation(float attenuation);
77
78 /** @copydoc AudioSource::getAttenuation */
79 BS_SCRIPT_EXPORT(n:Attenuation,pr:getter)
80 float getAttenuation() const { return mAttenuation; }
81
82 /** @copydoc AudioSource::setTime */
83 BS_SCRIPT_EXPORT(n:Time,pr:setter,hide)
84 void setTime(float time);
85
86 /** @copydoc AudioSource::getTime */
87 BS_SCRIPT_EXPORT(n:Time,pr:getter,hide)
88 float getTime() const;
89
90 /** Sets whether playback should start as soon as the component is enabled. */
91 BS_SCRIPT_EXPORT(n:PlayOnStart,pr:setter)
92 void setPlayOnStart(bool enable) { mPlayOnStart = enable; }
93
94 /** Determines should playback start as soon as the component is enabled. */
95 BS_SCRIPT_EXPORT(n:PlayOnStart,pr:getter)
96 bool getPlayOnStart() const { return mPlayOnStart; }
97
98 /** @copydoc AudioSource::play */
99 BS_SCRIPT_EXPORT(n:Play)
100 void play();
101
102 /** @copydoc AudioSource::pause */
103 BS_SCRIPT_EXPORT(n:Pause)
104 void pause();
105
106 /** @copydoc AudioSource::stop */
107 BS_SCRIPT_EXPORT(n:Stop)
108 void stop();
109
110 /** @copydoc AudioSource::getState */
111 BS_SCRIPT_EXPORT(n:State,pr:getter)
112 AudioSourceState getState() const;
113
114 /** @name Internal
115 * @{
116 */
117
118 /** Returns the AudioSource implementation wrapped by this component. */
119 AudioSource* _getInternal() const { return mInternal.get(); }
120
121 /** @} */
122
123 /************************************************************************/
124 /* COMPONENT OVERRIDES */
125 /************************************************************************/
126 protected:
127 friend class SceneObject;
128
129 /** @copydoc Component::onInitialized() */
130 void onInitialized() override;
131
132 /** @copydoc Component::onDestroyed() */
133 void onDestroyed() override;
134
135 /** @copydoc Component::onDisabled() */
136 void onDisabled() override;
137
138 /** @copydoc Component::onEnabled() */
139 void onEnabled() override;
140
141 /** @copydoc Component::onTransformChanged() */
142 void onTransformChanged(TransformChangedFlags flags) override;
143
144 /** @copydoc Component::update() */
145 void update() override;
146 protected:
147 using Component::destroyInternal;
148
149 /** Creates the internal representation of the AudioSource and restores the values saved by the Component. */
150 void restoreInternal();
151
152 /** Destroys the internal AudioSource representation. */
153 void destroyInternal();
154
155 /**
156 * Updates the transform of the internal AudioSource representation from the transform of the component's scene
157 * object.
158 */
159 void updateTransform();
160
161 SPtr<AudioSource> mInternal;
162 Vector3 mLastPosition = Vector3::ZERO;
163 Vector3 mVelocity = Vector3::ZERO;
164
165 HAudioClip mAudioClip;
166 float mVolume = 1.0f;
167 float mPitch = 1.0f;
168 bool mLoop = false;
169 UINT32 mPriority = 0;
170 float mMinDistance = 1.0f;
171 float mAttenuation = 1.0f;
172 bool mPlayOnStart = true;
173
174 /************************************************************************/
175 /* RTTI */
176 /************************************************************************/
177 public:
178 friend class CAudioSourceRTTI;
179 static RTTITypeBase* getRTTIStatic();
180 RTTITypeBase* getRTTI() const override;
181
182 protected:
183 CAudioSource(); // Serialization only
184 };
185
186 /** @} */
187}