SoundEditDialog.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : W3DView *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/W3DView/SoundEditDialog.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 10/16/00 11:48a $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "soundeditdialog.h"
  37. #include "soundrobj.h"
  38. #include "audiblesound.h"
  39. #include "utils.h"
  40. #include "sound3d.h"
  41. #include "playsounddialog.h"
  42. #include "w3dviewdoc.h"
  43. #include "datatreeview.h"
  44. #include "assetmgr.h"
  45. #ifdef _DEBUG
  46. #define new DEBUG_NEW
  47. #undef THIS_FILE
  48. static char THIS_FILE[] = __FILE__;
  49. #endif
  50. /////////////////////////////////////////////////////////////////////////////
  51. //
  52. // SoundEditDialogClass
  53. //
  54. /////////////////////////////////////////////////////////////////////////////
  55. SoundEditDialogClass::SoundEditDialogClass (CWnd *parent)
  56. : SoundRObj (NULL),
  57. CDialog (SoundEditDialogClass::IDD, parent)
  58. {
  59. //{{AFX_DATA_INIT(SoundEditDialogClass)
  60. //}}AFX_DATA_INIT
  61. return ;
  62. }
  63. /////////////////////////////////////////////////////////////////////////////
  64. //
  65. // ~SoundEditDialogClass
  66. //
  67. /////////////////////////////////////////////////////////////////////////////
  68. SoundEditDialogClass::~SoundEditDialogClass (void)
  69. {
  70. REF_PTR_RELEASE (SoundRObj);
  71. return;
  72. }
  73. /////////////////////////////////////////////////////////////////////////////
  74. //
  75. // DoDataExchange
  76. //
  77. /////////////////////////////////////////////////////////////////////////////
  78. void
  79. SoundEditDialogClass::DoDataExchange (CDataExchange* pDX)
  80. {
  81. CDialog::DoDataExchange(pDX);
  82. //{{AFX_DATA_MAP(SoundEditDialogClass)
  83. DDX_Control(pDX, IDC_VOLUME_SLIDER, VolumeSlider);
  84. DDX_Control(pDX, IDC_PRIORITY_SLIDER, PrioritySlider);
  85. //}}AFX_DATA_MAP
  86. return ;
  87. }
  88. BEGIN_MESSAGE_MAP(SoundEditDialogClass, CDialog)
  89. //{{AFX_MSG_MAP(SoundEditDialogClass)
  90. ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
  91. ON_BN_CLICKED(IDC_2D_RADIO, On2DRadio)
  92. ON_BN_CLICKED(IDC_3D_RADIO, On3DRadio)
  93. ON_BN_CLICKED(IDC_PLAY, OnPlay)
  94. ON_WM_HSCROLL()
  95. //}}AFX_MSG_MAP
  96. END_MESSAGE_MAP()
  97. /////////////////////////////////////////////////////////////////////////////
  98. //
  99. // SoundEditDialogClass
  100. //
  101. /////////////////////////////////////////////////////////////////////////////
  102. #ifdef _DEBUG
  103. void SoundEditDialogClass::AssertValid() const
  104. {
  105. CDialog::AssertValid();
  106. }
  107. void SoundEditDialogClass::Dump(CDumpContext& dc) const
  108. {
  109. CDialog::Dump(dc);
  110. }
  111. #endif //_DEBUG
  112. /////////////////////////////////////////////////////////////////////////////
  113. //
  114. // OnInitDialog
  115. //
  116. /////////////////////////////////////////////////////////////////////////////
  117. BOOL
  118. SoundEditDialogClass::OnInitDialog (void)
  119. {
  120. CDialog::OnInitDialog ();
  121. //
  122. // Create the reneder object if we don't already have one
  123. //
  124. if (SoundRObj == NULL) {
  125. SoundRObj = new SoundRenderObjClass;
  126. }
  127. //
  128. // Choose default settings
  129. //
  130. OldName = SoundRObj->Get_Name ();
  131. bool stop_on_hide = SoundRObj->Get_Flag (SoundRenderObjClass::FLAG_STOP_WHEN_HIDDEN);
  132. StringClass filename;
  133. float drop_off_radius = 100;
  134. float max_vol_radius = 10;
  135. float priority = 0.5F;
  136. bool is_3d = true;
  137. bool is_music = false;
  138. float loop_count = 1;
  139. float volume = 1.0F;
  140. //
  141. // Get the real settings from the sound object (if we have one)
  142. //
  143. AudibleSoundClass *sound = SoundRObj->Peek_Sound ();
  144. if (sound != NULL) {
  145. Sound3DClass *sound_3d = sound->As_Sound3DClass ();
  146. filename = sound->Get_Filename ();
  147. drop_off_radius = sound->Get_DropOff_Radius ();
  148. priority = sound->Peek_Priority ();
  149. is_3d = (sound_3d != NULL);
  150. is_music = (sound->Get_Type () == AudibleSoundClass::TYPE_MUSIC);
  151. loop_count = sound->Get_Loop_Count ();
  152. volume = sound->Get_Volume ();
  153. if (sound_3d != NULL) {
  154. max_vol_radius = sound_3d->Get_Max_Vol_Radius ();
  155. }
  156. }
  157. //
  158. // Fill the edit controls
  159. //
  160. SetDlgItemText (IDC_NAME_EDIT, OldName);
  161. SetDlgItemText (IDC_FILENAME_EDIT, filename);
  162. //
  163. // Check the appropriate controls
  164. //
  165. SendDlgItemMessage (IDC_INFINITE_LOOPS_CHECK, BM_SETCHECK, (WPARAM)(loop_count == 0));
  166. SendDlgItemMessage (IDC_3D_RADIO, BM_SETCHECK, (WPARAM)is_3d);
  167. SendDlgItemMessage (IDC_2D_RADIO, BM_SETCHECK, (WPARAM)(is_3d == false));
  168. SendDlgItemMessage (IDC_MUSIC_RADIO, BM_SETCHECK, (WPARAM)is_music);
  169. SendDlgItemMessage (IDC_SOUNDEFFECT_RADIO, BM_SETCHECK, (WPARAM)(is_music == false));
  170. SendDlgItemMessage (IDC_STOP_WHEN_HIDDEN_CHECK, BM_SETCHECK, (WPARAM)(stop_on_hide));
  171. //
  172. // Set up the sliders
  173. //
  174. VolumeSlider.SetRange (0, 100);
  175. PrioritySlider.SetRange (0, 100);
  176. VolumeSlider.SetPos (int(volume * 100.00F));
  177. PrioritySlider.SetPos (int(priority * 100.00F));
  178. //
  179. // Put the attenuation factors into the edit fields
  180. //
  181. ::SetDlgItemFloat (m_hWnd, IDC_DROP_OFF_EDIT, drop_off_radius);
  182. ::SetDlgItemFloat (m_hWnd, IDC_MAX_VOL_EDIT, max_vol_radius);
  183. ::SetDlgItemFloat (m_hWnd, IDC_TRIGGER_RADIUS_EDIT, drop_off_radius);
  184. //
  185. // Make sure the appropriate controls are enabled/disabled
  186. //
  187. Update_Enable_State ();
  188. return TRUE;
  189. }
  190. /////////////////////////////////////////////////////////////////////////////
  191. //
  192. // OnOK
  193. //
  194. /////////////////////////////////////////////////////////////////////////////
  195. void
  196. SoundEditDialogClass::OnOK (void)
  197. {
  198. CString name;
  199. GetDlgItemText (IDC_NAME_EDIT, name);
  200. //
  201. // Early exit if the name isn't valid
  202. //
  203. if (name.GetLength () == 0) {
  204. ::MessageBox (m_hWnd, "Invalid object name. Please enter a new name.", "Invalid settings", MB_ICONEXCLAMATION | MB_OK);
  205. return ;
  206. }
  207. //
  208. // Create a sound definition from the settings on the dialog
  209. //
  210. AudibleSoundClass *sound = Create_Sound_Object ();
  211. AudibleSoundDefinitionClass definition;
  212. definition.Initialize_From_Sound (sound);
  213. REF_PTR_RELEASE (sound);
  214. //
  215. // Pass the sound definition onto the render object
  216. //
  217. SoundRObj->Set_Sound (&definition);
  218. //
  219. // Pass the flags onto the render object
  220. //
  221. if (SendDlgItemMessage (IDC_STOP_WHEN_HIDDEN_CHECK, BM_GETCHECK) == 1) {
  222. SoundRObj->Set_Flags (SoundRenderObjClass::FLAG_STOP_WHEN_HIDDEN);
  223. } else {
  224. SoundRObj->Set_Flags (0);
  225. }
  226. //
  227. // Name the render object
  228. //
  229. SoundRObj->Set_Name (name);
  230. //
  231. // Add this sound object to the viewer
  232. //
  233. CW3DViewDoc *doc = ::GetCurrentDocument ();
  234. if (doc != NULL) {
  235. //
  236. // Create a new prototype for this emitter and add it to the asset manager
  237. //
  238. SoundRenderObjDefClass *definition = new SoundRenderObjDefClass (*SoundRObj);
  239. SoundRenderObjPrototypeClass *prototype = new SoundRenderObjPrototypeClass (definition);
  240. //
  241. // Update the asset manager with the new prototype
  242. //
  243. if (OldName.Get_Length () > 0) {
  244. WW3DAssetManager::Get_Instance()->Remove_Prototype (OldName);
  245. }
  246. WW3DAssetManager::Get_Instance()->Add_Prototype (prototype);
  247. //
  248. // Add the new object to the data tree
  249. //
  250. CDataTreeView *data_tree = doc->GetDataTreeView ();
  251. data_tree->Refresh_Asset (name, OldName, TypeSound);
  252. //
  253. // Display the emitter
  254. //
  255. doc->Reload_Displayed_Object ();
  256. OldName = name;
  257. }
  258. CDialog::OnOK ();
  259. return ;
  260. }
  261. /////////////////////////////////////////////////////////////////////////////
  262. //
  263. // Create_Sound_Object
  264. //
  265. /////////////////////////////////////////////////////////////////////////////
  266. AudibleSoundClass *
  267. SoundEditDialogClass::Create_Sound_Object (void)
  268. {
  269. AudibleSoundClass *sound = NULL;
  270. //
  271. // Get the filename
  272. //
  273. CString filename;
  274. GetDlgItemText (IDC_FILENAME_EDIT, filename);
  275. filename = ::Get_Filename_From_Path (filename);
  276. //
  277. // Try to create the sound either as a 3D object or a 2D object
  278. //
  279. bool is_3d = bool (SendDlgItemMessage (IDC_3D_RADIO, BM_GETCHECK) == 1);
  280. if (is_3d) {
  281. sound = WWAudioClass::Get_Instance ()->Create_3D_Sound (filename);
  282. } else {
  283. sound = WWAudioClass::Get_Instance ()->Create_Sound_Effect (filename);
  284. }
  285. if (sound != NULL) {
  286. //
  287. // Pass the new volume and priority onto the sound
  288. //
  289. float priority = ((float)PrioritySlider.GetPos ()) / 100.0F;
  290. float volume = ((float)VolumeSlider.GetPos ()) / 100.0F;
  291. sound->Set_Priority (priority);
  292. sound->Set_Volume (volume);
  293. //
  294. // Pass the loop count and type onto the sound
  295. //
  296. int loop_count = SendDlgItemMessage (IDC_INFINITE_LOOPS_CHECK, BM_GETCHECK) ? 0 : 1;
  297. sound->Set_Loop_Count (loop_count);
  298. bool is_music = bool (SendDlgItemMessage (IDC_MUSIC_RADIO, BM_GETCHECK) == 1);
  299. sound->Set_Type (is_music ? AudibleSoundClass::TYPE_MUSIC : AudibleSoundClass::TYPE_SOUND_EFFECT);
  300. //
  301. // Pass the new drop off and maximum volume radii to the sound
  302. //
  303. float drop_off = 0;
  304. float max_vol = 0;
  305. if (is_3d) {
  306. drop_off = ::GetDlgItemFloat (m_hWnd, IDC_DROP_OFF_EDIT);
  307. max_vol = ::GetDlgItemFloat (m_hWnd, IDC_MAX_VOL_EDIT);
  308. } else {
  309. drop_off = ::GetDlgItemFloat (m_hWnd, IDC_TRIGGER_RADIUS_EDIT);
  310. max_vol = ::GetDlgItemFloat (m_hWnd, IDC_TRIGGER_RADIUS_EDIT);
  311. }
  312. sound->Set_DropOff_Radius (drop_off);
  313. //
  314. // Only 3D sounds have a maximum volume radius
  315. //
  316. if (is_3d) {
  317. ((Sound3DClass *)sound)->Set_Max_Vol_Radius (max_vol);
  318. }
  319. }
  320. return sound;
  321. }
  322. /////////////////////////////////////////////////////////////////////////////
  323. //
  324. // OnBrowse
  325. //
  326. /////////////////////////////////////////////////////////////////////////////
  327. void
  328. SoundEditDialogClass::OnBrowse (void)
  329. {
  330. //
  331. // Determine what filename and path to initially display in the dialog
  332. //
  333. CString default_filename;
  334. CString path;
  335. if (GetDlgItemText (IDC_FILENAME_EDIT, default_filename) > 0) {
  336. path = ::Strip_Filename_From_Path (default_filename);
  337. }
  338. CFileDialog dialog (TRUE,
  339. ".wav",
  340. default_filename,
  341. OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER,
  342. "All Sound Files|*.wav;*.mp3|WAV File (*.wav)|*.wav|MP3 File (*.mp3)|*.mp3||",
  343. this);
  344. //
  345. // Set the path, so it opens in the correct directory
  346. //
  347. dialog.m_ofn.lpstrInitialDir = path;
  348. //
  349. // Ask the user what new sound file to use
  350. //
  351. if (dialog.DoModal () == IDOK) {
  352. //
  353. // Put the selected filename into the dialog control
  354. //
  355. SetDlgItemText (IDC_FILENAME_EDIT, ::Get_Filename_From_Path (dialog.GetPathName ()));
  356. }
  357. return ;
  358. }
  359. /////////////////////////////////////////////////////////////////////////////
  360. //
  361. // On2DRadio
  362. //
  363. /////////////////////////////////////////////////////////////////////////////
  364. void
  365. SoundEditDialogClass::On2DRadio (void)
  366. {
  367. Update_Enable_State ();
  368. return ;
  369. }
  370. /////////////////////////////////////////////////////////////////////////////
  371. //
  372. // On3DRadio
  373. //
  374. /////////////////////////////////////////////////////////////////////////////
  375. void
  376. SoundEditDialogClass::On3DRadio (void)
  377. {
  378. Update_Enable_State ();
  379. return ;
  380. }
  381. /////////////////////////////////////////////////////////////////////////////
  382. //
  383. // Update_Enable_State
  384. //
  385. /////////////////////////////////////////////////////////////////////////////
  386. void
  387. SoundEditDialogClass::Update_Enable_State (void)
  388. {
  389. bool enable_3d = (SendDlgItemMessage (IDC_3D_RADIO, BM_GETCHECK) == 1);
  390. ::EnableWindow (::GetDlgItem (m_hWnd, IDC_MAX_VOL_EDIT), enable_3d);
  391. ::EnableWindow (::GetDlgItem (m_hWnd, IDC_DROP_OFF_EDIT), enable_3d);
  392. ::EnableWindow (::GetDlgItem (m_hWnd, IDC_TRIGGER_RADIUS_EDIT), !enable_3d);
  393. return ;
  394. }
  395. /////////////////////////////////////////////////////////////////////////////
  396. //
  397. // OnPlay
  398. //
  399. /////////////////////////////////////////////////////////////////////////////
  400. void
  401. SoundEditDialogClass::OnPlay (void)
  402. {
  403. //
  404. // Get the current filename
  405. //
  406. CString filename;
  407. GetDlgItemText (IDC_FILENAME_EDIT, filename);
  408. //
  409. // Show the 'play sound' dialog
  410. //
  411. PlaySoundDialogClass dialog (filename, this);
  412. dialog.DoModal ();
  413. return ;
  414. }
  415. /////////////////////////////////////////////////////////////////////////////
  416. //
  417. // OnCancel
  418. //
  419. /////////////////////////////////////////////////////////////////////////////
  420. void
  421. SoundEditDialogClass::OnCancel (void)
  422. {
  423. CDialog::OnCancel ();
  424. return ;
  425. }