guiAudioCtrl.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include "console/typeValidators.h"
  30. #define TickMs 32
  31. IMPLEMENT_CONOBJECT( GuiAudioCtrl );
  32. ConsoleDocClass( GuiAudioCtrl,
  33. "@brief Brief Description.\n\n"
  34. "Audio PLayback.\n\n"
  35. "@ingroup GuiUtil\n");
  36. GuiAudioCtrl::GuiAudioCtrl()
  37. {
  38. INIT_ASSET(Sound);
  39. mTickPeriodMS = 100;
  40. mLastThink = 0;
  41. mCurrTick = 0;
  42. mSoundPlaying = NULL;
  43. mUseTrackDescriptionOnly = false;
  44. mVolume = 1;
  45. mPitch = 1;
  46. mFadeInTime = -1;
  47. mFadeOutTime = -1;
  48. mSourceGroup = NULL;
  49. }
  50. GuiAudioCtrl::~GuiAudioCtrl()
  51. {
  52. SFX_DELETE(mSoundPlaying);
  53. }
  54. bool GuiAudioCtrl::onWake()
  55. {
  56. bool awake = Parent::onWake();
  57. setProcessTicks();
  58. _update();
  59. return awake;
  60. }
  61. void GuiAudioCtrl::onSleep()
  62. {
  63. Parent::onSleep();
  64. _update();
  65. }
  66. void GuiAudioCtrl::onRemove()
  67. {
  68. Parent::onRemove();
  69. }
  70. void GuiAudioCtrl::processTick()
  71. {
  72. if (mLastThink + mTickPeriodMS < mCurrTick)
  73. {
  74. mCurrTick = 0;
  75. mLastThink = 0;
  76. if (isSoundValid())
  77. {
  78. _update();
  79. }
  80. }
  81. else
  82. {
  83. mCurrTick += TickMs;
  84. }
  85. }
  86. bool GuiAudioCtrl::testCondition()
  87. {
  88. if (mPlayIf.isEmpty())
  89. return true; //we've got no tests to run so just do it
  90. //test the mapper plugged in condition line
  91. String resVar = getIdString() + String(".result");
  92. Con::setBoolVariable(resVar.c_str(), false);
  93. String command = resVar + "=" + mPlayIf + ";";
  94. Con::evaluatef(command.c_str());
  95. if (Con::getBoolVariable(resVar.c_str()) == 1)
  96. {
  97. return true;
  98. }
  99. return false;
  100. }
  101. void GuiAudioCtrl::initPersistFields()
  102. {
  103. addGroup("Sounds");
  104. INITPERSISTFIELD_SOUNDASSET(Sound, GuiAudioCtrl, "Looping SoundAsset to play while GuiAudioCtrl is active.");
  105. addFieldV("tickPeriodMS", TypeRangedS32, Offset(mTickPeriodMS, GuiAudioCtrl), &CommonValidators::MSTickRange,
  106. "@brief Time in milliseconds between calls to onTick().\n\n"
  107. "@see onTickTrigger()\n");
  108. addField("playIf", TypeCommand, Offset(mPlayIf, GuiAudioCtrl), "evaluation condition to trip playback (true/false)");
  109. addField("useTrackDescriptionOnly", TypeBool, Offset(mUseTrackDescriptionOnly, GuiAudioCtrl),
  110. "If this is true, all fields except for #playOnAdd and #track are ignored on the emitter object.\n"
  111. "This is useful to prevent fields in the #track's description from being overridden by emitter fields.");
  112. addField("sourceGroup", TypeSFXSourceName, Offset(mSourceGroup, GuiAudioCtrl),
  113. "The SFXSource to which to assign the sound of this emitter as a child.\n"
  114. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  115. "@see SFXDescription::sourceGroup");
  116. addFieldV("volume", TypeRangedF32, Offset(mVolume, GuiAudioCtrl), &CommonValidators::PositiveFloat,
  117. "Volume level to apply to the sound.\n"
  118. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  119. "@see SFXDescription::volume");
  120. addFieldV("pitch", TypeRangedF32, Offset(mPitch, GuiAudioCtrl), &CommonValidators::PositiveFloat,
  121. "Pitch shift to apply to the sound. Default is 1 = play at normal speed.\n"
  122. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  123. "@see SFXDescription::pitch");
  124. addFieldV("fadeInTime", TypeRangedF32, Offset(mFadeInTime, GuiAudioCtrl), &CommonValidators::PositiveFloat,
  125. "Number of seconds to gradually fade in volume from zero when playback starts.\n"
  126. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  127. "@see SFXDescription::fadeInTime");
  128. addFieldV("fadeOutTime", TypeRangedF32, Offset(mFadeOutTime, GuiAudioCtrl), &CommonValidators::PositiveFloat,
  129. "Number of seconds to gradually fade out volume down to zero when playback is stopped or paused.\n"
  130. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  131. "@see SFXDescription::fadeOutTime");
  132. endGroup("Sounds");
  133. Parent::initPersistFields();
  134. }
  135. void GuiAudioCtrl::_update()
  136. {
  137. if (testCondition() && isAwake())
  138. {
  139. bool useTrackDescriptionOnly = (mUseTrackDescriptionOnly && getSoundProfile());
  140. if (getSoundProfile())
  141. {
  142. if (mSoundPlaying == NULL)
  143. {
  144. mSoundPlaying = SFX->createSource(getSoundProfile(), &(SFX->getListener().getTransform()));
  145. }
  146. }
  147. if ( mSoundPlaying && !mSoundPlaying->isPlaying())
  148. {
  149. // The rest only applies if we have a source.
  150. if (!useTrackDescriptionOnly)
  151. {
  152. // Set the volume irrespective of the profile.
  153. if (mSourceGroup)
  154. {
  155. mSourceGroup->addObject(mSoundPlaying);
  156. mSoundPlaying->setVolume(mSourceGroup->getVolume() * mVolume);
  157. }
  158. else
  159. {
  160. mSoundPlaying->setVolume(mVolume);
  161. }
  162. mSoundPlaying->setPitch(mPitch);
  163. mSoundPlaying->setFadeTimes(mFadeInTime, mFadeOutTime);
  164. }
  165. else
  166. getSoundDescription()->mSourceGroup->addObject(mSoundPlaying);
  167. mSoundPlaying->play();
  168. }
  169. }
  170. else
  171. {
  172. if (mSoundPlaying != NULL)
  173. {
  174. SFX_DELETE(mSoundPlaying);
  175. setProcessTicks(false);
  176. }
  177. }
  178. }