guiAudioCtrl.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gui/shiny/guiAudioCtrl.h"
  23. #include "console/engineAPI.h"
  24. #include "console/script.h"
  25. #include "sfx/sfxSystem.h"
  26. #include "sfx/sfxTrack.h"
  27. #include "sfx/sfxSource.h"
  28. #include "sfx/sfxTypes.h"
  29. #define TickMs 32
  30. IMPLEMENT_CONOBJECT( GuiAudioCtrl );
  31. ConsoleDocClass( GuiAudioCtrl,
  32. "@brief Brief Description.\n\n"
  33. "Audio PLayback.\n\n"
  34. "@ingroup GuiUtil\n");
  35. GuiAudioCtrl::GuiAudioCtrl()
  36. {
  37. INIT_ASSET(Sound);
  38. mTickPeriodMS = 100;
  39. mLastThink = 0;
  40. mCurrTick = 0;
  41. mSoundPlaying = NULL;
  42. mUseTrackDescriptionOnly = false;
  43. mVolume = 1;
  44. mPitch = 1;
  45. mFadeInTime = -1;
  46. mFadeOutTime = -1;
  47. mSourceGroup = NULL;
  48. }
  49. GuiAudioCtrl::~GuiAudioCtrl()
  50. {
  51. SFX_DELETE(mSoundPlaying);
  52. }
  53. bool GuiAudioCtrl::onWake()
  54. {
  55. bool awake = Parent::onWake();
  56. setProcessTicks();
  57. _update();
  58. return awake;
  59. }
  60. void GuiAudioCtrl::onSleep()
  61. {
  62. Parent::onSleep();
  63. _update();
  64. }
  65. void GuiAudioCtrl::onRemove()
  66. {
  67. Parent::onRemove();
  68. }
  69. void GuiAudioCtrl::processTick()
  70. {
  71. if (mLastThink + mTickPeriodMS < mCurrTick)
  72. {
  73. mCurrTick = 0;
  74. mLastThink = 0;
  75. if (isSoundValid())
  76. {
  77. _update();
  78. }
  79. }
  80. else
  81. {
  82. mCurrTick += TickMs;
  83. }
  84. }
  85. bool GuiAudioCtrl::testCondition()
  86. {
  87. if (mPlayIf.isEmpty())
  88. return true; //we've got no tests to run so just do it
  89. //test the mapper plugged in condition line
  90. String resVar = getIdString() + String(".result");
  91. Con::setBoolVariable(resVar.c_str(), false);
  92. String command = resVar + "=" + mPlayIf + ";";
  93. Con::evaluatef(command.c_str());
  94. if (Con::getBoolVariable(resVar.c_str()) == 1)
  95. {
  96. return true;
  97. }
  98. return false;
  99. }
  100. void GuiAudioCtrl::initPersistFields()
  101. {
  102. addGroup("Sounds");
  103. INITPERSISTFIELD_SOUNDASSET(Sound, GuiAudioCtrl, "Looping SoundAsset to play while GuiAudioCtrl is active.");
  104. addField("tickPeriodMS", TypeS32, Offset(mTickPeriodMS, GuiAudioCtrl),
  105. "@brief Time in milliseconds between calls to onTick().\n\n"
  106. "@see onTickTrigger()\n");
  107. addField("playIf", TypeCommand, Offset(mPlayIf, GuiAudioCtrl), "evaluation condition to trip playback (true/false)");
  108. addField("useTrackDescriptionOnly", TypeBool, Offset(mUseTrackDescriptionOnly, GuiAudioCtrl),
  109. "If this is true, all fields except for #playOnAdd and #track are ignored on the emitter object.\n"
  110. "This is useful to prevent fields in the #track's description from being overridden by emitter fields.");
  111. addField("sourceGroup", TypeSFXSourceName, Offset(mSourceGroup, GuiAudioCtrl),
  112. "The SFXSource to which to assign the sound of this emitter as a child.\n"
  113. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  114. "@see SFXDescription::sourceGroup");
  115. addField("volume", TypeF32, Offset(mVolume, GuiAudioCtrl),
  116. "Volume level to apply to the sound.\n"
  117. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  118. "@see SFXDescription::volume");
  119. addField("pitch", TypeF32, Offset(mPitch, GuiAudioCtrl),
  120. "Pitch shift to apply to the sound. Default is 1 = play at normal speed.\n"
  121. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  122. "@see SFXDescription::pitch");
  123. addField("fadeInTime", TypeF32, Offset(mFadeInTime, GuiAudioCtrl),
  124. "Number of seconds to gradually fade in volume from zero when playback starts.\n"
  125. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  126. "@see SFXDescription::fadeInTime");
  127. addField("fadeOutTime", TypeF32, Offset(mFadeOutTime, GuiAudioCtrl),
  128. "Number of seconds to gradually fade out volume down to zero when playback is stopped or paused.\n"
  129. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  130. "@see SFXDescription::fadeOutTime");
  131. endGroup("Sounds");
  132. Parent::initPersistFields();
  133. }
  134. void GuiAudioCtrl::_update()
  135. {
  136. if (testCondition() && isAwake())
  137. {
  138. bool useTrackDescriptionOnly = (mUseTrackDescriptionOnly && getSoundProfile());
  139. if (getSoundProfile())
  140. {
  141. if (mSoundPlaying == NULL)
  142. {
  143. mSoundPlaying = SFX->createSource(getSoundProfile(), &(SFX->getListener().getTransform()));
  144. }
  145. }
  146. if ( mSoundPlaying && !mSoundPlaying->isPlaying())
  147. {
  148. // The rest only applies if we have a source.
  149. if (!useTrackDescriptionOnly)
  150. {
  151. // Set the volume irrespective of the profile.
  152. if (mSourceGroup)
  153. {
  154. mSourceGroup->addObject(mSoundPlaying);
  155. mSoundPlaying->setVolume(mSourceGroup->getVolume() * mVolume);
  156. }
  157. else
  158. {
  159. mSoundPlaying->setVolume(mVolume);
  160. }
  161. mSoundPlaying->setPitch(mPitch);
  162. mSoundPlaying->setFadeTimes(mFadeInTime, mFadeOutTime);
  163. }
  164. else
  165. getSoundDescription()->mSourceGroup->addObject(mSoundPlaying);
  166. mSoundPlaying->play();
  167. }
  168. }
  169. else
  170. {
  171. if (mSoundPlaying != NULL)
  172. {
  173. SFX_DELETE(mSoundPlaying);
  174. setProcessTicks(false);
  175. }
  176. }
  177. }