AudioControl.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <AudioControl.h>
  9. #include <ACETypes.h>
  10. #include <ATLControlsModel.h>
  11. #include <AudioControlsEditorPlugin.h>
  12. #include <AudioControlsEditorUndo.h>
  13. #include <IAudioSystemControl.h>
  14. #include <ImplementationManager.h>
  15. namespace AudioControls
  16. {
  17. //-------------------------------------------------------------------------------------------//
  18. CATLControl::CATLControl(const AZStd::string& controlName, CID id, EACEControlType type, CATLControlsModel* atlControlsModel)
  19. : m_name(controlName)
  20. , m_id(id)
  21. , m_type(type)
  22. , m_atlControlsModel(atlControlsModel)
  23. {
  24. }
  25. //-------------------------------------------------------------------------------------------//
  26. CATLControl::~CATLControl()
  27. {
  28. // Same as ClearConnections but without signalling that 'this' control is being modified...
  29. if (CAudioControlsEditorPlugin::GetImplementationManager())
  30. {
  31. if (IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetImplementationManager()->GetImplementation())
  32. {
  33. for (auto& connectionPtr : m_connectedControls)
  34. {
  35. if (IAudioSystemControl* middlewareControl = audioSystemImpl->GetControl(connectionPtr->GetID()))
  36. {
  37. audioSystemImpl->ConnectionRemoved(middlewareControl);
  38. SignalConnectionRemoved(middlewareControl);
  39. }
  40. }
  41. }
  42. }
  43. m_connectedControls.clear();
  44. }
  45. //-------------------------------------------------------------------------------------------//
  46. CID CATLControl::GetId() const
  47. {
  48. return m_id;
  49. }
  50. //-------------------------------------------------------------------------------------------//
  51. EACEControlType CATLControl::GetType() const
  52. {
  53. return m_type;
  54. }
  55. //-------------------------------------------------------------------------------------------//
  56. AZStd::string CATLControl::GetName() const
  57. {
  58. return m_name;
  59. }
  60. //-------------------------------------------------------------------------------------------//
  61. CATLControl* CATLControl::GetParent() const
  62. {
  63. return m_parent;
  64. }
  65. //-------------------------------------------------------------------------------------------//
  66. void CATLControl::SetId(CID id)
  67. {
  68. if (id != m_id)
  69. {
  70. SignalControlAboutToBeModified();
  71. m_id = id;
  72. SignalControlModified();
  73. }
  74. }
  75. //-------------------------------------------------------------------------------------------//
  76. void CATLControl::SetType(EACEControlType type)
  77. {
  78. if (type != m_type)
  79. {
  80. SignalControlAboutToBeModified();
  81. m_type = type;
  82. SignalControlModified();
  83. }
  84. }
  85. //-------------------------------------------------------------------------------------------//
  86. void CATLControl::SetName(const AZStd::string_view name)
  87. {
  88. if (name != m_name)
  89. {
  90. SignalControlAboutToBeModified();
  91. m_name = name;
  92. SignalControlModified();
  93. }
  94. }
  95. //-------------------------------------------------------------------------------------------//
  96. AZStd::string CATLControl::GetScope() const
  97. {
  98. return m_scope;
  99. }
  100. //-------------------------------------------------------------------------------------------//
  101. void CATLControl::SetScope(const AZStd::string_view scope)
  102. {
  103. if (m_scope != scope)
  104. {
  105. SignalControlAboutToBeModified();
  106. m_scope = scope;
  107. SignalControlModified();
  108. }
  109. }
  110. //-------------------------------------------------------------------------------------------//
  111. bool CATLControl::HasScope() const
  112. {
  113. return !m_scope.empty();
  114. }
  115. //-------------------------------------------------------------------------------------------//
  116. bool CATLControl::IsAutoLoad() const
  117. {
  118. return m_isAutoLoad;
  119. }
  120. //-------------------------------------------------------------------------------------------//
  121. void CATLControl::SetAutoLoad(bool isAutoLoad)
  122. {
  123. if (isAutoLoad != m_isAutoLoad)
  124. {
  125. SignalControlAboutToBeModified();
  126. m_isAutoLoad = isAutoLoad;
  127. SignalControlModified();
  128. }
  129. }
  130. //-------------------------------------------------------------------------------------------//
  131. size_t CATLControl::ConnectionCount() const
  132. {
  133. return m_connectedControls.size();
  134. }
  135. //-------------------------------------------------------------------------------------------//
  136. TConnectionPtr CATLControl::GetConnectionAt(size_t index) const
  137. {
  138. return (index < m_connectedControls.size() ? m_connectedControls[index] : nullptr);
  139. }
  140. //-------------------------------------------------------------------------------------------//
  141. TConnectionPtr CATLControl::GetConnection(CID id) const
  142. {
  143. if (id != ACE_INVALID_CID)
  144. {
  145. for (const TConnectionPtr& connection : m_connectedControls)
  146. {
  147. if (connection && connection->GetID() == id)
  148. {
  149. return connection;
  150. }
  151. }
  152. }
  153. return nullptr;
  154. }
  155. //-------------------------------------------------------------------------------------------//
  156. TConnectionPtr CATLControl::GetConnection(IAudioSystemControl* middlewareControl) const
  157. {
  158. return GetConnection(middlewareControl->GetId());
  159. }
  160. //-------------------------------------------------------------------------------------------//
  161. void CATLControl::AddConnection(TConnectionPtr connection)
  162. {
  163. if (connection)
  164. {
  165. SignalControlAboutToBeModified();
  166. m_connectedControls.push_back(connection);
  167. if (IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetImplementationManager()->GetImplementation())
  168. {
  169. if (IAudioSystemControl* middlewareControl = audioSystemImpl->GetControl(connection->GetID()))
  170. {
  171. SignalConnectionAdded(middlewareControl);
  172. }
  173. }
  174. SignalControlModified();
  175. }
  176. }
  177. //-------------------------------------------------------------------------------------------//
  178. void CATLControl::RemoveConnection(TConnectionPtr connection)
  179. {
  180. if (connection)
  181. {
  182. auto it = AZStd::find(m_connectedControls.begin(), m_connectedControls.end(), connection);
  183. if (it != m_connectedControls.end())
  184. {
  185. SignalControlAboutToBeModified();
  186. m_connectedControls.erase(it);
  187. if (IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetImplementationManager()->GetImplementation())
  188. {
  189. if (IAudioSystemControl* middlewareControl = audioSystemImpl->GetControl(connection->GetID()))
  190. {
  191. SignalConnectionRemoved(middlewareControl);
  192. }
  193. }
  194. SignalControlModified();
  195. }
  196. }
  197. }
  198. //-------------------------------------------------------------------------------------------//
  199. void CATLControl::ClearConnections()
  200. {
  201. SignalControlAboutToBeModified();
  202. if (IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetImplementationManager()->GetImplementation())
  203. {
  204. for (auto& connectionPtr : m_connectedControls)
  205. {
  206. if (IAudioSystemControl* middlewareControl = audioSystemImpl->GetControl(connectionPtr->GetID()))
  207. {
  208. audioSystemImpl->ConnectionRemoved(middlewareControl);
  209. SignalConnectionRemoved(middlewareControl);
  210. }
  211. }
  212. }
  213. m_connectedControls.clear();
  214. SignalControlModified();
  215. }
  216. //-------------------------------------------------------------------------------------------//
  217. bool CATLControl::IsFullyConnected() const
  218. {
  219. bool isConnected = ConnectionCount() > 0;
  220. // Switches have no connections. Their child Switch_States do.
  221. if (m_type == eACET_SWITCH)
  222. {
  223. isConnected = true;
  224. for (auto& childPtr : m_children)
  225. {
  226. if (!childPtr->IsFullyConnected())
  227. {
  228. isConnected = false;
  229. break;
  230. }
  231. }
  232. }
  233. else
  234. {
  235. if (IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetImplementationManager()->GetImplementation())
  236. {
  237. for (auto& connectionPtr : m_connectedControls)
  238. {
  239. if (IAudioSystemControl* middlewareControl = audioSystemImpl->GetControl(connectionPtr->GetID()))
  240. {
  241. if (!middlewareControl->IsConnected() || middlewareControl->IsPlaceholder())
  242. {
  243. isConnected = false;
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. return isConnected;
  251. }
  252. //-------------------------------------------------------------------------------------------//
  253. void CATLControl::RemoveConnection(IAudioSystemControl* middlewareControl)
  254. {
  255. if (middlewareControl)
  256. {
  257. const CID id = middlewareControl->GetId();
  258. auto it = m_connectedControls.begin();
  259. auto end = m_connectedControls.end();
  260. for (; it != end; ++it)
  261. {
  262. if ((*it)->GetID() == id)
  263. {
  264. SignalControlAboutToBeModified();
  265. m_connectedControls.erase(it);
  266. SignalConnectionRemoved(middlewareControl);
  267. SignalControlModified();
  268. return;
  269. }
  270. }
  271. }
  272. }
  273. //-------------------------------------------------------------------------------------------//
  274. void CATLControl::SignalControlModified()
  275. {
  276. if (m_atlControlsModel)
  277. {
  278. m_atlControlsModel->OnControlModified(this);
  279. }
  280. }
  281. //-------------------------------------------------------------------------------------------//
  282. void CATLControl::SignalControlAboutToBeModified()
  283. {
  284. if (!CUndo::IsSuspended())
  285. {
  286. CUndo undo("ATL Control Modified");
  287. CUndo::Record(new CUndoControlModified(GetId()));
  288. }
  289. }
  290. //-------------------------------------------------------------------------------------------//
  291. void CATLControl::SignalConnectionAdded(IAudioSystemControl* middlewareControl)
  292. {
  293. if (m_atlControlsModel)
  294. {
  295. m_atlControlsModel->OnConnectionAdded(this, middlewareControl);
  296. }
  297. }
  298. //-------------------------------------------------------------------------------------------//
  299. void CATLControl::SignalConnectionRemoved(IAudioSystemControl* middlewareControl)
  300. {
  301. if (m_atlControlsModel)
  302. {
  303. m_atlControlsModel->OnConnectionRemoved(this, middlewareControl);
  304. }
  305. }
  306. //-------------------------------------------------------------------------------------------//
  307. void CATLControl::ReloadConnections()
  308. {
  309. if (IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetImplementationManager()->GetImplementation())
  310. {
  311. for (auto& connectionNode : m_connectionNodes)
  312. {
  313. if (TConnectionPtr connection = audioSystemImpl->CreateConnectionFromXMLNode(connectionNode.m_xmlNode, m_type))
  314. {
  315. AddConnection(connection);
  316. connectionNode.m_isValid = true;
  317. }
  318. else
  319. {
  320. connectionNode.m_isValid = false;
  321. }
  322. }
  323. }
  324. }
  325. //-------------------------------------------------------------------------------------------//
  326. void CATLControl::SetParent(CATLControl* parent)
  327. {
  328. m_parent = parent;
  329. if (m_parent)
  330. {
  331. SetScope(m_parent->GetScope());
  332. }
  333. }
  334. bool CATLControl::SwitchStateConnectionCheck(IAudioSystemControl* middlewareControl)
  335. {
  336. if (IAudioSystemEditor* audioSystemImpl = CAudioControlsEditorPlugin::GetImplementationManager()->GetImplementation())
  337. {
  338. CID parentID = middlewareControl->GetParent()->GetId();
  339. EACEControlType compatibleType = audioSystemImpl->ImplTypeToATLType(middlewareControl->GetType());
  340. if (compatibleType == EACEControlType::eACET_SWITCH_STATE && m_type == EACEControlType::eACET_SWITCH)
  341. {
  342. for (auto& child : m_children)
  343. {
  344. for (int j = 0; child && j < child->ConnectionCount(); ++j)
  345. {
  346. TConnectionPtr tmpConnection = child->GetConnectionAt(j);
  347. if (tmpConnection)
  348. {
  349. IAudioSystemControl* tmpMiddlewareControl = audioSystemImpl->GetControl(tmpConnection->GetID());
  350. EACEControlType controlType = audioSystemImpl->ImplTypeToATLType(tmpMiddlewareControl->GetType());
  351. if (tmpMiddlewareControl && controlType == EACEControlType::eACET_SWITCH_STATE)
  352. {
  353. if (parentID != ACE_INVALID_CID && tmpMiddlewareControl->GetParent()->GetId() != parentID)
  354. {
  355. return false;
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. }
  363. return true;
  364. }
  365. } // namespace AudioControls