PageRenderTime 293ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 1ms

/Core/Dependencies/Wwise/samples/SoundFrame/CarSim/Win32/CarSettingsDlg.cpp

https://bitbucket.org/barakianc/nvidia-physx-and-apex-in-gge
C++ | 373 lines | 309 code | 54 blank | 10 comment | 43 complexity | 39b71e14ac36ffe67bddedaeb476b992 MD5 | raw file
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2006 Audiokinetic Inc. / All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "stdafx.h"
  7. #include "CarSettingsDlg.h"
  8. #include "Car.h"
  9. #include "CarSimEngine.h"
  10. #include "ICarSimUI.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #endif
  14. CarSettingsDlg::CarSettingsDlg( CarSimEngine* in_pCarSimeEngine, CWnd* pParent )
  15. : CDialog(CarSettingsDlg::IDD, pParent)
  16. , m_pCarSimEngine( in_pCarSimeEngine )
  17. , m_rCar( in_pCarSimeEngine->GetCar() )
  18. , m_bDirty( false )
  19. {
  20. // Modify this dialog if you change the max num of gears ...
  21. _ASSERT( CarParam::s_nMaxNumOfGear == 7 );
  22. }
  23. void CarSettingsDlg::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. DDX_Control(pDX, IDC_NUM_OF_GEARS_COMBO, m_numOfGearCombo);
  27. DDX_Control(pDX, IDC_GEAR_1_SLIDER, m_gearSliderArray[0]);
  28. DDX_Control(pDX, IDC_GEAR_2_SLIDER, m_gearSliderArray[1]);
  29. DDX_Control(pDX, IDC_GEAR_3_SLIDER, m_gearSliderArray[2]);
  30. DDX_Control(pDX, IDC_GEAR_4_SLIDER, m_gearSliderArray[3]);
  31. DDX_Control(pDX, IDC_GEAR_5_SLIDER, m_gearSliderArray[4]);
  32. DDX_Control(pDX, IDC_GEAR_6_SLIDER, m_gearSliderArray[5]);
  33. DDX_Control(pDX, IDC_GEAR_7_SLIDER, m_gearSliderArray[6]);
  34. DDX_Control(pDX, IDC_GEAR_1_EDIT, m_gearEditArray[0]);
  35. DDX_Control(pDX, IDC_GEAR_2_EDIT, m_gearEditArray[1]);
  36. DDX_Control(pDX, IDC_GEAR_3_EDIT, m_gearEditArray[2]);
  37. DDX_Control(pDX, IDC_GEAR_4_EDIT, m_gearEditArray[3]);
  38. DDX_Control(pDX, IDC_GEAR_5_EDIT, m_gearEditArray[4]);
  39. DDX_Control(pDX, IDC_GEAR_6_EDIT, m_gearEditArray[5]);
  40. DDX_Control(pDX, IDC_GEAR_7_EDIT, m_gearEditArray[6]);
  41. DDX_Control(pDX, IDC_WEIGHT_EDIT, m_weightEdit);
  42. DDX_Control(pDX, IDC_TORQUE_EDIT, m_torqueEdit);
  43. DDX_Control(pDX, IDC_DRAG_COEFFICIENT_EDIT, m_dragCoefficientEdit);
  44. DDX_Control(pDX, IDC_FRONT_SURFACE_EDIT, m_frontSurfaceEdit);
  45. DDX_Control(pDX, IDC_ROLLING_RESISTANCE_EDIT, m_rollingResistenceEdit);
  46. DDX_Control(pDX, IDC_DIFFERENTIAL_RATIO_EDIT, m_differentialRatioEdit);
  47. DDX_Control(pDX, IDC_WHEEL_RADIUS_EDIT, m_wheelRadiusEdit);
  48. DDX_Control(pDX, IDC_MINIMUM_RPM_EDIT, m_minRPMEdit);
  49. DDX_Control(pDX, IDC_RPM_CUT_OFF_EDIT, m_rpmCutOffEdit);
  50. }
  51. BEGIN_MESSAGE_MAP(CarSettingsDlg, CDialog)
  52. ON_WM_DESTROY()
  53. ON_BN_CLICKED(ID_DEFAULT_BUTTON, OnBnClickedDefault)
  54. ON_BN_CLICKED(ID_SAVE_BUTTON, OnBnClickedSave)
  55. ON_BN_CLICKED(ID_SAVE_AS_BUTTON, OnBnClickedSaveAs)
  56. ON_CBN_SELCHANGE(IDC_NUM_OF_GEARS_COMBO, OnCbnSelchangeNumOfGearCombo)
  57. ON_EN_KILLFOCUS(IDC_GEAR_1_EDIT, OnKillFocusGearRatioEdit)
  58. ON_EN_KILLFOCUS(IDC_GEAR_2_EDIT, OnKillFocusGearRatioEdit)
  59. ON_EN_KILLFOCUS(IDC_GEAR_3_EDIT, OnKillFocusGearRatioEdit)
  60. ON_EN_KILLFOCUS(IDC_GEAR_4_EDIT, OnKillFocusGearRatioEdit)
  61. ON_EN_KILLFOCUS(IDC_GEAR_5_EDIT, OnKillFocusGearRatioEdit)
  62. ON_EN_KILLFOCUS(IDC_GEAR_6_EDIT, OnKillFocusGearRatioEdit)
  63. ON_EN_KILLFOCUS(IDC_GEAR_7_EDIT, OnKillFocusGearRatioEdit)
  64. ON_EN_KILLFOCUS(IDC_WEIGHT_EDIT, OnKillFocusSettingsEdit)
  65. ON_EN_KILLFOCUS(IDC_TORQUE_EDIT, OnKillFocusSettingsEdit)
  66. ON_EN_KILLFOCUS(IDC_DRAG_COEFFICIENT_EDIT, OnKillFocusSettingsEdit)
  67. ON_EN_KILLFOCUS(IDC_FRONT_SURFACE_EDIT, OnKillFocusSettingsEdit)
  68. ON_EN_KILLFOCUS(IDC_ROLLING_RESISTANCE_EDIT, OnKillFocusSettingsEdit)
  69. ON_EN_KILLFOCUS(IDC_DIFFERENTIAL_RATIO_EDIT, OnKillFocusSettingsEdit)
  70. ON_EN_KILLFOCUS(IDC_WHEEL_RADIUS_EDIT, OnKillFocusSettingsEdit)
  71. ON_EN_KILLFOCUS(IDC_MINIMUM_RPM_EDIT, OnKillFocusSettingsEdit)
  72. ON_EN_KILLFOCUS(IDC_RPM_CUT_OFF_EDIT, OnKillFocusSettingsEdit)
  73. ON_WM_HSCROLL()
  74. END_MESSAGE_MAP()
  75. BOOL CarSettingsDlg::OnInitDialog()
  76. {
  77. CDialog::OnInitDialog();
  78. for( int i = 0; i < CarParam::s_nMaxNumOfGear; ++i )
  79. {
  80. m_gearSliderArray[i].SetRange( (int)( CarParam::s_dblMinGearRatio * 100.0 ), (int)( CarParam::s_dblMaxGearRatio * 100.0 ), TRUE );
  81. m_gearEditArray[i].Init( false, 2, CarParam::s_dblMinGearRatio, CarParam::s_dblMaxGearRatio );
  82. }
  83. m_weightEdit.Init( false, 1, CarParam::s_dblMinWeight, CarParam::s_dblMaxWeight );
  84. m_torqueEdit.Init( false, 1, CarParam::s_dblMinTorque, CarParam::s_dblMaxTorque );
  85. m_dragCoefficientEdit.Init( false, 2, CarParam::s_dblMinDragCoefficient, CarParam::s_dblMaxDragCoefficient );
  86. m_frontSurfaceEdit.Init( false, 2, CarParam::s_dblMinFrontSurface, CarParam::s_dblMaxFrontSurface );
  87. m_rollingResistenceEdit.Init( false, 3, CarParam::s_dblMinRollingResistence, CarParam::s_dblMaxRollingResistence );
  88. m_differentialRatioEdit.Init( false, 2, CarParam::s_dblMinDifferentialRatio, CarParam::s_dblMaxDifferentialRatio );
  89. m_wheelRadiusEdit.Init( false, 2, CarParam::s_dblMinWheelRadius, CarParam::s_dblMaxWheelRadius );
  90. m_minRPMEdit.Init( false, 0, CarParam::s_dblMinRPM, CarParam::s_dblMaxRPM );
  91. m_rpmCutOffEdit.Init( false, 0, CarParam::s_dblMinRPM, CarParam::s_dblMaxRPM );
  92. UpdateDisplay();
  93. return TRUE;
  94. }
  95. void CarSettingsDlg::OnOK()
  96. {
  97. // Do nothing so we don't close on enter key
  98. }
  99. void CarSettingsDlg::OnCancel()
  100. {
  101. AskForSave();
  102. DestroyWindow();
  103. }
  104. void CarSettingsDlg::OnDestroy()
  105. {
  106. m_bDirty = false;
  107. }
  108. void CarSettingsDlg::Open()
  109. {
  110. if( ! ::IsWindow( GetSafeHwnd() ) )
  111. {
  112. Create( CarSettingsDlg::IDD );
  113. ShowWindow( SW_SHOW );
  114. }
  115. }
  116. void CarSettingsDlg::Close()
  117. {
  118. if( ::IsWindow( GetSafeHwnd() ) )
  119. OnCancel();
  120. }
  121. void CarSettingsDlg::OnSettingsFileChanged()
  122. {
  123. if( ::IsWindow( GetSafeHwnd() ) )
  124. {
  125. SetDirty( false );
  126. UpdateDisplay();
  127. }
  128. }
  129. void CarSettingsDlg::AskForSave()
  130. {
  131. if( m_bDirty )
  132. {
  133. CString csMessage, csCaption;
  134. csMessage.LoadString( IDS_SAVE_SETTINGS_ON_CLOSE );
  135. csCaption.LoadString( IDS_MESSAGE_CAPTION );
  136. int result = MessageBox( csMessage, csCaption, MB_YESNO | MB_ICONQUESTION );
  137. if( result == IDYES )
  138. OnBnClickedSave();
  139. // If we are still dirty it means that the answer was no or that the save failed or was cancelled
  140. if( m_bDirty )
  141. {
  142. // Revert changes
  143. CString csFileName = m_pCarSimEngine->GetCarParamSerializer().GetCurrentFilePath();
  144. if( csFileName.IsEmpty() )
  145. OnBnClickedDefault();
  146. else
  147. m_pCarSimEngine->GetCarParamSerializer().Load( csFileName );
  148. }
  149. }
  150. }
  151. void CarSettingsDlg::OnBnClickedDefault()
  152. {
  153. m_rCar.SetDefaultCarParam();
  154. SetDirty( true );
  155. UpdateDisplay();
  156. }
  157. void CarSettingsDlg::OnBnClickedSave()
  158. {
  159. CString csFileName = m_pCarSimEngine->GetCarParamSerializer().GetCurrentFilePath();
  160. if( csFileName.IsEmpty() )
  161. OnBnClickedSaveAs();
  162. else
  163. {
  164. if( !m_pCarSimEngine->GetCarParamSerializer().Save( csFileName ) )
  165. {
  166. CString csError;
  167. csError.Format( IDS_SAVE_ERROR, csFileName );
  168. MessageBox( csError, NULL, MB_ICONERROR | MB_OK );
  169. }
  170. else
  171. SetDirty( false );
  172. }
  173. }
  174. void CarSettingsDlg::OnBnClickedSaveAs()
  175. {
  176. CString csFilterString;
  177. csFilterString.Format( L"Car Settings Files (*%s)|*%s||", m_pCarSimEngine->GetCarParamSerializer().GetFileExtension(),
  178. m_pCarSimEngine->GetCarParamSerializer().GetFileExtension() );
  179. CFileDialog fileDialog( FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR, csFilterString );
  180. if( fileDialog.DoModal() == IDOK )
  181. {
  182. CString csFileName = fileDialog.GetPathName();
  183. if( !m_pCarSimEngine->GetCarParamSerializer().Save( csFileName ) )
  184. {
  185. CString csError;
  186. csError.Format( IDS_SAVE_ERROR, csFileName );
  187. MessageBox( csError, NULL, MB_ICONERROR | MB_OK );
  188. }
  189. else
  190. {
  191. SetDirty( false );
  192. UpdateTitleBarText();
  193. ICarSimUI* pParent = dynamic_cast<ICarSimUI*>( GetParent() );
  194. if( pParent )
  195. pParent->OnSimulationSettingsChanged();
  196. }
  197. }
  198. }
  199. void CarSettingsDlg::OnCbnSelchangeNumOfGearCombo()
  200. {
  201. m_rCar.GetCarParam().m_nNumOfGear = m_numOfGearCombo.GetCurSel() + 1;
  202. m_rCar.SetCurrentGear( min( m_rCar.GetCurrentGear(), (size_t)m_numOfGearCombo.GetCurSel() ) );
  203. SetDirty( true );
  204. UpdateDisplay();
  205. }
  206. void CarSettingsDlg::OnKillFocusGearRatioEdit()
  207. {
  208. for( int i = 0; i < CarParam::s_nMaxNumOfGear; ++i )
  209. {
  210. if( ! DoubleEqual( m_rCar.GetCarParam().m_gearRatios[i], m_gearEditArray[i].GetValue() ) )
  211. {
  212. m_rCar.GetCarParam().m_gearRatios[i] = m_gearEditArray[i].GetValue();
  213. m_gearSliderArray[i].SetPos( (int)( m_rCar.GetCarParam().m_gearRatios[i] * 100.0 ) );
  214. SetDirty( true );
  215. }
  216. }
  217. }
  218. void CarSettingsDlg::OnKillFocusSettingsEdit()
  219. {
  220. if( ! DoubleEqual( m_rCar.GetCarParam().m_dblWeight, m_weightEdit.GetValue() ) )
  221. {
  222. m_rCar.GetCarParam().m_dblWeight = m_weightEdit.GetValue();
  223. SetDirty( true );
  224. }
  225. else if( ! DoubleEqual( m_rCar.GetCarParam().m_dblTorque, m_torqueEdit.GetValue() ) )
  226. {
  227. m_rCar.GetCarParam().m_dblTorque = m_torqueEdit.GetValue();
  228. SetDirty( true );
  229. }
  230. else if( ! DoubleEqual( m_rCar.GetCarParam().m_dblDragCoefficient, m_dragCoefficientEdit.GetValue() ) )
  231. {
  232. m_rCar.GetCarParam().m_dblDragCoefficient = m_dragCoefficientEdit.GetValue();
  233. SetDirty( true );
  234. }
  235. else if( ! DoubleEqual( m_rCar.GetCarParam().m_dblFrontSurface, m_frontSurfaceEdit.GetValue() ) )
  236. {
  237. m_rCar.GetCarParam().m_dblFrontSurface = m_frontSurfaceEdit.GetValue();
  238. SetDirty( true );
  239. }
  240. else if( ! DoubleEqual( m_rCar.GetCarParam().m_dblRollingResistence, m_rollingResistenceEdit.GetValue() ) )
  241. {
  242. m_rCar.GetCarParam().m_dblRollingResistence = m_rollingResistenceEdit.GetValue();
  243. SetDirty( true );
  244. }
  245. else if( ! DoubleEqual( m_rCar.GetCarParam().m_dblDifferentialRatio, m_differentialRatioEdit.GetValue() ) )
  246. {
  247. m_rCar.GetCarParam().m_dblDifferentialRatio = m_differentialRatioEdit.GetValue();
  248. SetDirty( true );
  249. }
  250. else if( ! DoubleEqual( m_rCar.GetCarParam().m_dblWheelRadius, m_wheelRadiusEdit.GetValue() ) )
  251. {
  252. m_rCar.GetCarParam().m_dblWheelRadius = m_wheelRadiusEdit.GetValue();
  253. SetDirty( true );
  254. }
  255. else if( ! DoubleEqual( m_rCar.GetCarParam().m_dblMinRPM, m_minRPMEdit.GetValue() ) )
  256. {
  257. m_rCar.GetCarParam().m_dblMinRPM = m_minRPMEdit.GetValue();
  258. SetDirty( true );
  259. }
  260. else if( ! DoubleEqual( m_rCar.GetCarParam().m_dblRPMCutOff, m_rpmCutOffEdit.GetValue() ) )
  261. {
  262. m_rCar.GetCarParam().m_dblRPMCutOff = m_rpmCutOffEdit.GetValue();
  263. SetDirty( true );
  264. }
  265. }
  266. void CarSettingsDlg::OnHScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar )
  267. {
  268. for( int i = 0; i < CarParam::s_nMaxNumOfGear; ++i )
  269. {
  270. if( pScrollBar->GetSafeHwnd() == m_gearSliderArray[i].GetSafeHwnd() )
  271. {
  272. m_rCar.GetCarParam().m_gearRatios[i] = ( (double)m_gearSliderArray[i].GetPos() ) / 100.0;
  273. m_gearEditArray[i].SetValue( m_rCar.GetCarParam().m_gearRatios[i] );
  274. SetDirty( true );
  275. }
  276. }
  277. }
  278. void CarSettingsDlg::UpdateTitleBarText()
  279. {
  280. CString csFileName = m_pCarSimEngine->GetCarParamSerializer().GetCurrentFileNameOnly();
  281. if( csFileName.IsEmpty() )
  282. csFileName.LoadString( IDS_DEFAULT_SETTINGS_FILE_NAME );
  283. CString csTitleBarText;
  284. csTitleBarText.Format( IDS_SETTINGS_TITLE_BAR_TEXT, csFileName );
  285. if( m_bDirty )
  286. csTitleBarText += L"*";
  287. SetWindowText( csTitleBarText );
  288. }
  289. void CarSettingsDlg::UpdateDisplay()
  290. {
  291. UpdateTitleBarText();
  292. const CarParams& rCarParams = m_rCar.GetCarParam();
  293. m_numOfGearCombo.SetCurSel( (int)( rCarParams.m_nNumOfGear - 1 ) );
  294. for( unsigned int i = 0; i < CarParam::s_nMaxNumOfGear; ++i )
  295. {
  296. m_gearSliderArray[i].SetPos( (int)( rCarParams.m_gearRatios[i] * 100.0 ) );
  297. m_gearEditArray[i].SetValue( rCarParams.m_gearRatios[i] );
  298. // Disable slider and edit that are over the current num of gears
  299. m_gearSliderArray[i].EnableWindow( ( i < rCarParams.m_nNumOfGear )? TRUE : FALSE );
  300. m_gearEditArray[i].EnableWindow( ( i < rCarParams.m_nNumOfGear )? TRUE : FALSE );
  301. }
  302. m_weightEdit.SetValue( rCarParams.m_dblWeight );
  303. m_torqueEdit.SetValue( rCarParams.m_dblTorque );
  304. m_dragCoefficientEdit.SetValue( rCarParams.m_dblDragCoefficient );
  305. m_frontSurfaceEdit.SetValue( rCarParams.m_dblFrontSurface );
  306. m_rollingResistenceEdit.SetValue( rCarParams.m_dblRollingResistence );
  307. m_differentialRatioEdit.SetValue( rCarParams.m_dblDifferentialRatio );
  308. m_wheelRadiusEdit.SetValue( rCarParams.m_dblWheelRadius );
  309. m_minRPMEdit.SetValue( rCarParams.m_dblMinRPM );
  310. m_rpmCutOffEdit.SetValue( rCarParams.m_dblRPMCutOff );
  311. }
  312. void CarSettingsDlg::SetDirty( bool in_bDirty )
  313. {
  314. if( m_bDirty != in_bDirty )
  315. {
  316. m_bDirty = in_bDirty;
  317. UpdateTitleBarText();
  318. }
  319. }