ATLControlsModel.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <ATLControlsModel.h>
  9. #include <AzCore/std/smart_ptr/make_shared.h>
  10. #include <AzCore/std/string/conversions.h>
  11. #include <AzCore/StringFunc/StringFunc.h>
  12. #include <AudioControlsEditorUndo.h>
  13. #include <IEditor.h>
  14. namespace AudioControls
  15. {
  16. //-------------------------------------------------------------------------------------------//
  17. CID CATLControlsModel::m_nextId = ACE_INVALID_CID;
  18. //-------------------------------------------------------------------------------------------//
  19. CATLControlsModel::CATLControlsModel()
  20. : m_suppressMessages(false)
  21. {
  22. ClearDirtyFlags();
  23. }
  24. //-------------------------------------------------------------------------------------------//
  25. CATLControlsModel::~CATLControlsModel()
  26. {
  27. Clear();
  28. }
  29. //-------------------------------------------------------------------------------------------//
  30. CATLControl* CATLControlsModel::CreateControl(const AZStd::string& controlName, EACEControlType type, CATLControl* parent)
  31. {
  32. AZStd::shared_ptr<CATLControl> control = AZStd::make_shared<CATLControl>(controlName, GenerateUniqueId(), type, this);
  33. if (control)
  34. {
  35. if (parent)
  36. {
  37. control->SetParent(parent);
  38. }
  39. InsertControl(control);
  40. if (!CUndo::IsSuspended())
  41. {
  42. CUndo undo("Audio Control Created");
  43. CUndo::Record(new CUndoControlAdd(control->GetId()));
  44. }
  45. }
  46. return control.get();
  47. }
  48. //-------------------------------------------------------------------------------------------//
  49. void CATLControlsModel::RemoveControl(CID id)
  50. {
  51. if (id != ACE_INVALID_CID)
  52. {
  53. for (auto it = m_controls.begin(); it != m_controls.end(); ++it)
  54. {
  55. AZStd::shared_ptr<CATLControl>& control = *it;
  56. if (control && control->GetId() == id)
  57. {
  58. control->ClearConnections();
  59. OnControlRemoved(control.get());
  60. // Remove control from parent
  61. CATLControl* parent = control->GetParent();
  62. if (parent)
  63. {
  64. parent->RemoveChild(control.get());
  65. }
  66. if (!CUndo::IsSuspended())
  67. {
  68. CUndo::Record(new CUndoControlRemove(control));
  69. }
  70. m_controls.erase(it, it + 1);
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. //-------------------------------------------------------------------------------------------//
  77. CATLControl* CATLControlsModel::GetControlByID(CID id) const
  78. {
  79. if (id != ACE_INVALID_CID)
  80. {
  81. size_t size = m_controls.size();
  82. for (size_t i = 0; i < size; ++i)
  83. {
  84. if (m_controls[i]->GetId() == id)
  85. {
  86. return m_controls[i].get();
  87. }
  88. }
  89. }
  90. return nullptr;
  91. }
  92. //-------------------------------------------------------------------------------------------//
  93. bool CATLControlsModel::IsNameValid(const AZStd::string_view name, EACEControlType type, const AZStd::string_view scope, const CATLControl* const parent) const
  94. {
  95. const size_t size = m_controls.size();
  96. for (size_t i = 0; i < size; ++i)
  97. {
  98. if (m_controls[i]
  99. && m_controls[i]->GetType() == type
  100. && (AZ::StringFunc::Equal(m_controls[i]->GetName().c_str(), name.data()))
  101. && (m_controls[i]->GetScope().empty() || m_controls[i]->GetScope() == scope)
  102. && (m_controls[i]->GetParent() == parent))
  103. {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. //-------------------------------------------------------------------------------------------//
  110. AZStd::string CATLControlsModel::GenerateUniqueName(const AZStd::string_view rootName, EACEControlType type, const AZStd::string_view scope, const CATLControl* const parent) const
  111. {
  112. AZStd::string uniqueName = rootName;
  113. AZ::u32 number = 1;
  114. while (!IsNameValid(uniqueName, type, scope, parent))
  115. {
  116. uniqueName = AZStd::string::format("%s_%u", rootName.data(), number++);
  117. }
  118. return uniqueName;
  119. }
  120. //-------------------------------------------------------------------------------------------//
  121. void CATLControlsModel::AddScope(AZStd::string scopeName, bool localOnly)
  122. {
  123. AZStd::to_lower(scopeName.begin(), scopeName.end());
  124. const size_t size = m_scopes.size();
  125. for (int i = 0; i < size; ++i)
  126. {
  127. if (m_scopes[i].name == scopeName)
  128. {
  129. return;
  130. }
  131. }
  132. m_scopes.push_back(SControlScope(scopeName, localOnly));
  133. }
  134. //-------------------------------------------------------------------------------------------//
  135. void CATLControlsModel::ClearScopes()
  136. {
  137. m_scopes.clear();
  138. }
  139. //-------------------------------------------------------------------------------------------//
  140. bool CATLControlsModel::ScopeExists(AZStd::string scopeName) const
  141. {
  142. AZStd::to_lower(scopeName.begin(), scopeName.end());
  143. const size_t size = m_scopes.size();
  144. for (size_t i = 0; i < size; ++i)
  145. {
  146. if (m_scopes[i].name == scopeName)
  147. {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. //-------------------------------------------------------------------------------------------//
  154. size_t CATLControlsModel::GetScopeCount() const
  155. {
  156. return m_scopes.size();
  157. }
  158. //-------------------------------------------------------------------------------------------//
  159. SControlScope CATLControlsModel::GetScopeAt(size_t index) const
  160. {
  161. return (index < m_scopes.size() ? m_scopes[index] : SControlScope());
  162. }
  163. //-------------------------------------------------------------------------------------------//
  164. void CATLControlsModel::Clear()
  165. {
  166. m_controls.clear();
  167. m_scopes.clear();
  168. ClearDirtyFlags();
  169. }
  170. //-------------------------------------------------------------------------------------------//
  171. void CATLControlsModel::AddListener(IATLControlModelListener* modelListener)
  172. {
  173. if (AZStd::find(m_listeners.begin(), m_listeners.end(), modelListener) == m_listeners.end())
  174. {
  175. m_listeners.push_back(modelListener);
  176. }
  177. }
  178. //-------------------------------------------------------------------------------------------//
  179. void CATLControlsModel::RemoveListener(IATLControlModelListener* modelListener)
  180. {
  181. auto it = AZStd::find(m_listeners.begin(), m_listeners.end(), modelListener);
  182. if (it != m_listeners.end())
  183. {
  184. m_listeners.erase(it);
  185. }
  186. }
  187. //-------------------------------------------------------------------------------------------//
  188. void CATLControlsModel::OnControlAdded(CATLControl* control)
  189. {
  190. if (!m_suppressMessages)
  191. {
  192. for (auto listener : m_listeners)
  193. {
  194. listener->OnControlAdded(control);
  195. }
  196. m_isControlTypeModified[control->GetType()] = true;
  197. }
  198. }
  199. //-------------------------------------------------------------------------------------------//
  200. void CATLControlsModel::OnControlRemoved(CATLControl* control)
  201. {
  202. if (!m_suppressMessages)
  203. {
  204. for (auto listener : m_listeners)
  205. {
  206. listener->OnControlRemoved(control);
  207. }
  208. m_isControlTypeModified[control->GetType()] = true;
  209. }
  210. }
  211. //-------------------------------------------------------------------------------------------//
  212. void CATLControlsModel::OnConnectionAdded(CATLControl* control, IAudioSystemControl* middlewareControl)
  213. {
  214. if (!m_suppressMessages)
  215. {
  216. for (auto listener : m_listeners)
  217. {
  218. listener->OnConnectionAdded(control, middlewareControl);
  219. }
  220. }
  221. }
  222. //-------------------------------------------------------------------------------------------//
  223. void CATLControlsModel::OnConnectionRemoved(CATLControl* control, IAudioSystemControl* middlewareControl)
  224. {
  225. if (!m_suppressMessages)
  226. {
  227. for (auto listener : m_listeners)
  228. {
  229. listener->OnConnectionRemoved(control, middlewareControl);
  230. }
  231. }
  232. }
  233. //-------------------------------------------------------------------------------------------//
  234. void CATLControlsModel::OnControlModified(CATLControl* control)
  235. {
  236. if (!m_suppressMessages)
  237. {
  238. for (auto listener : m_listeners)
  239. {
  240. listener->OnControlModified(control);
  241. }
  242. m_isControlTypeModified[control->GetType()] = true;
  243. }
  244. }
  245. //-------------------------------------------------------------------------------------------//
  246. void CATLControlsModel::SetSuppressMessages(bool suppressMessages)
  247. {
  248. m_suppressMessages = suppressMessages;
  249. }
  250. //-------------------------------------------------------------------------------------------//
  251. bool CATLControlsModel::IsDirty()
  252. {
  253. for (int i = 0; i < eACET_NUM_TYPES; ++i)
  254. {
  255. if (m_isControlTypeModified[i])
  256. {
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. //-------------------------------------------------------------------------------------------//
  263. bool CATLControlsModel::IsTypeDirty(EACEControlType type)
  264. {
  265. if (type != eACET_NUM_TYPES)
  266. {
  267. return m_isControlTypeModified[type];
  268. }
  269. return true;
  270. }
  271. //-------------------------------------------------------------------------------------------//
  272. void CATLControlsModel::ClearDirtyFlags()
  273. {
  274. for (int i = 0; i < eACET_NUM_TYPES; ++i)
  275. {
  276. m_isControlTypeModified[i] = false;
  277. }
  278. }
  279. //-------------------------------------------------------------------------------------------//
  280. CATLControl* CATLControlsModel::FindControl(const AZStd::string_view controlName, EACEControlType type, const AZStd::string_view scope, CATLControl* parent) const
  281. {
  282. if (parent)
  283. {
  284. const size_t size = parent->ChildCount();
  285. for (size_t i = 0; i < size; ++i)
  286. {
  287. CATLControl* control = parent->GetChild(i);
  288. if (control
  289. && control->GetName() == controlName
  290. && control->GetType() == type
  291. && control->GetScope() == scope)
  292. {
  293. return control;
  294. }
  295. }
  296. }
  297. else
  298. {
  299. const size_t size = m_controls.size();
  300. for (size_t i = 0; i < size; ++i)
  301. {
  302. CATLControl* control = m_controls[i].get();
  303. if (control
  304. && control->GetName() == controlName
  305. && control->GetType() == type
  306. && control->GetScope() == scope)
  307. {
  308. return control;
  309. }
  310. }
  311. }
  312. return nullptr;
  313. }
  314. //-------------------------------------------------------------------------------------------//
  315. AZStd::shared_ptr<CATLControl> CATLControlsModel::TakeControl(CID id)
  316. {
  317. const size_t size = m_controls.size();
  318. for (size_t i = 0; i < size; ++i)
  319. {
  320. if (m_controls[i]->GetId() == id)
  321. {
  322. AZStd::shared_ptr<CATLControl> control = m_controls[i];
  323. RemoveControl(id);
  324. return control;
  325. }
  326. }
  327. return nullptr;
  328. }
  329. //-------------------------------------------------------------------------------------------//
  330. void CATLControlsModel::InsertControl(AZStd::shared_ptr<CATLControl> control)
  331. {
  332. if (control)
  333. {
  334. m_controls.push_back(control);
  335. CATLControl* parent = control->GetParent();
  336. if (parent)
  337. {
  338. parent->AddChild(control.get());
  339. }
  340. OnControlAdded(control.get());
  341. }
  342. }
  343. //-------------------------------------------------------------------------------------------//
  344. void CATLControlsModel::ClearAllConnections()
  345. {
  346. const size_t size = m_controls.size();
  347. for (size_t i = 0; i < size; ++i)
  348. {
  349. CATLControl* control = m_controls[i].get();
  350. if (control)
  351. {
  352. control->ClearConnections();
  353. }
  354. }
  355. }
  356. //-------------------------------------------------------------------------------------------//
  357. void CATLControlsModel::ReloadAllConnections()
  358. {
  359. const size_t size = m_controls.size();
  360. for (size_t i = 0; i < size; ++i)
  361. {
  362. CATLControl* control = m_controls[i].get();
  363. if (control)
  364. {
  365. control->ReloadConnections();
  366. }
  367. }
  368. }
  369. } // namespace AudioControls