123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <ATLControlsModel.h>
- #include <AzCore/std/smart_ptr/make_shared.h>
- #include <AzCore/std/string/conversions.h>
- #include <AzCore/StringFunc/StringFunc.h>
- #include <AudioControlsEditorUndo.h>
- #include <IEditor.h>
- namespace AudioControls
- {
- //-------------------------------------------------------------------------------------------//
- CID CATLControlsModel::m_nextId = ACE_INVALID_CID;
- //-------------------------------------------------------------------------------------------//
- CATLControlsModel::CATLControlsModel()
- : m_suppressMessages(false)
- {
- ClearDirtyFlags();
- }
- //-------------------------------------------------------------------------------------------//
- CATLControlsModel::~CATLControlsModel()
- {
- Clear();
- }
- //-------------------------------------------------------------------------------------------//
- CATLControl* CATLControlsModel::CreateControl(const AZStd::string& controlName, EACEControlType type, CATLControl* parent)
- {
- AZStd::shared_ptr<CATLControl> control = AZStd::make_shared<CATLControl>(controlName, GenerateUniqueId(), type, this);
- if (control)
- {
- if (parent)
- {
- control->SetParent(parent);
- }
- InsertControl(control);
- if (!CUndo::IsSuspended())
- {
- CUndo undo("Audio Control Created");
- CUndo::Record(new CUndoControlAdd(control->GetId()));
- }
- }
- return control.get();
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::RemoveControl(CID id)
- {
- if (id != ACE_INVALID_CID)
- {
- for (auto it = m_controls.begin(); it != m_controls.end(); ++it)
- {
- AZStd::shared_ptr<CATLControl>& control = *it;
- if (control && control->GetId() == id)
- {
- control->ClearConnections();
- OnControlRemoved(control.get());
- // Remove control from parent
- CATLControl* parent = control->GetParent();
- if (parent)
- {
- parent->RemoveChild(control.get());
- }
- if (!CUndo::IsSuspended())
- {
- CUndo::Record(new CUndoControlRemove(control));
- }
- m_controls.erase(it, it + 1);
- break;
- }
- }
- }
- }
- //-------------------------------------------------------------------------------------------//
- CATLControl* CATLControlsModel::GetControlByID(CID id) const
- {
- if (id != ACE_INVALID_CID)
- {
- size_t size = m_controls.size();
- for (size_t i = 0; i < size; ++i)
- {
- if (m_controls[i]->GetId() == id)
- {
- return m_controls[i].get();
- }
- }
- }
- return nullptr;
- }
- //-------------------------------------------------------------------------------------------//
- bool CATLControlsModel::IsNameValid(const AZStd::string_view name, EACEControlType type, const AZStd::string_view scope, const CATLControl* const parent) const
- {
- const size_t size = m_controls.size();
- for (size_t i = 0; i < size; ++i)
- {
- if (m_controls[i]
- && m_controls[i]->GetType() == type
- && (AZ::StringFunc::Equal(m_controls[i]->GetName().c_str(), name.data()))
- && (m_controls[i]->GetScope().empty() || m_controls[i]->GetScope() == scope)
- && (m_controls[i]->GetParent() == parent))
- {
- return false;
- }
- }
- return true;
- }
- //-------------------------------------------------------------------------------------------//
- AZStd::string CATLControlsModel::GenerateUniqueName(const AZStd::string_view rootName, EACEControlType type, const AZStd::string_view scope, const CATLControl* const parent) const
- {
- AZStd::string uniqueName = rootName;
- AZ::u32 number = 1;
- while (!IsNameValid(uniqueName, type, scope, parent))
- {
- uniqueName = AZStd::string::format("%s_%u", rootName.data(), number++);
- }
- return uniqueName;
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::AddScope(AZStd::string scopeName, bool localOnly)
- {
- AZStd::to_lower(scopeName.begin(), scopeName.end());
- const size_t size = m_scopes.size();
- for (int i = 0; i < size; ++i)
- {
- if (m_scopes[i].name == scopeName)
- {
- return;
- }
- }
- m_scopes.push_back(SControlScope(scopeName, localOnly));
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::ClearScopes()
- {
- m_scopes.clear();
- }
- //-------------------------------------------------------------------------------------------//
- bool CATLControlsModel::ScopeExists(AZStd::string scopeName) const
- {
- AZStd::to_lower(scopeName.begin(), scopeName.end());
- const size_t size = m_scopes.size();
- for (size_t i = 0; i < size; ++i)
- {
- if (m_scopes[i].name == scopeName)
- {
- return true;
- }
- }
- return false;
- }
- //-------------------------------------------------------------------------------------------//
- size_t CATLControlsModel::GetScopeCount() const
- {
- return m_scopes.size();
- }
- //-------------------------------------------------------------------------------------------//
- SControlScope CATLControlsModel::GetScopeAt(size_t index) const
- {
- return (index < m_scopes.size() ? m_scopes[index] : SControlScope());
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::Clear()
- {
- m_controls.clear();
- m_scopes.clear();
- ClearDirtyFlags();
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::AddListener(IATLControlModelListener* modelListener)
- {
- if (AZStd::find(m_listeners.begin(), m_listeners.end(), modelListener) == m_listeners.end())
- {
- m_listeners.push_back(modelListener);
- }
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::RemoveListener(IATLControlModelListener* modelListener)
- {
- auto it = AZStd::find(m_listeners.begin(), m_listeners.end(), modelListener);
- if (it != m_listeners.end())
- {
- m_listeners.erase(it);
- }
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::OnControlAdded(CATLControl* control)
- {
- if (!m_suppressMessages)
- {
- for (auto listener : m_listeners)
- {
- listener->OnControlAdded(control);
- }
- m_isControlTypeModified[control->GetType()] = true;
- }
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::OnControlRemoved(CATLControl* control)
- {
- if (!m_suppressMessages)
- {
- for (auto listener : m_listeners)
- {
- listener->OnControlRemoved(control);
- }
- m_isControlTypeModified[control->GetType()] = true;
- }
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::OnConnectionAdded(CATLControl* control, IAudioSystemControl* middlewareControl)
- {
- if (!m_suppressMessages)
- {
- for (auto listener : m_listeners)
- {
- listener->OnConnectionAdded(control, middlewareControl);
- }
- }
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::OnConnectionRemoved(CATLControl* control, IAudioSystemControl* middlewareControl)
- {
- if (!m_suppressMessages)
- {
- for (auto listener : m_listeners)
- {
- listener->OnConnectionRemoved(control, middlewareControl);
- }
- }
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::OnControlModified(CATLControl* control)
- {
- if (!m_suppressMessages)
- {
- for (auto listener : m_listeners)
- {
- listener->OnControlModified(control);
- }
- m_isControlTypeModified[control->GetType()] = true;
- }
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::SetSuppressMessages(bool suppressMessages)
- {
- m_suppressMessages = suppressMessages;
- }
- //-------------------------------------------------------------------------------------------//
- bool CATLControlsModel::IsDirty()
- {
- for (int i = 0; i < eACET_NUM_TYPES; ++i)
- {
- if (m_isControlTypeModified[i])
- {
- return true;
- }
- }
- return false;
- }
- //-------------------------------------------------------------------------------------------//
- bool CATLControlsModel::IsTypeDirty(EACEControlType type)
- {
- if (type != eACET_NUM_TYPES)
- {
- return m_isControlTypeModified[type];
- }
- return true;
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::ClearDirtyFlags()
- {
- for (int i = 0; i < eACET_NUM_TYPES; ++i)
- {
- m_isControlTypeModified[i] = false;
- }
- }
- //-------------------------------------------------------------------------------------------//
- CATLControl* CATLControlsModel::FindControl(const AZStd::string_view controlName, EACEControlType type, const AZStd::string_view scope, CATLControl* parent) const
- {
- if (parent)
- {
- const size_t size = parent->ChildCount();
- for (size_t i = 0; i < size; ++i)
- {
- CATLControl* control = parent->GetChild(i);
- if (control
- && control->GetName() == controlName
- && control->GetType() == type
- && control->GetScope() == scope)
- {
- return control;
- }
- }
- }
- else
- {
- const size_t size = m_controls.size();
- for (size_t i = 0; i < size; ++i)
- {
- CATLControl* control = m_controls[i].get();
- if (control
- && control->GetName() == controlName
- && control->GetType() == type
- && control->GetScope() == scope)
- {
- return control;
- }
- }
- }
- return nullptr;
- }
- //-------------------------------------------------------------------------------------------//
- AZStd::shared_ptr<CATLControl> CATLControlsModel::TakeControl(CID id)
- {
- const size_t size = m_controls.size();
- for (size_t i = 0; i < size; ++i)
- {
- if (m_controls[i]->GetId() == id)
- {
- AZStd::shared_ptr<CATLControl> control = m_controls[i];
- RemoveControl(id);
- return control;
- }
- }
- return nullptr;
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::InsertControl(AZStd::shared_ptr<CATLControl> control)
- {
- if (control)
- {
- m_controls.push_back(control);
- CATLControl* parent = control->GetParent();
- if (parent)
- {
- parent->AddChild(control.get());
- }
- OnControlAdded(control.get());
- }
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::ClearAllConnections()
- {
- const size_t size = m_controls.size();
- for (size_t i = 0; i < size; ++i)
- {
- CATLControl* control = m_controls[i].get();
- if (control)
- {
- control->ClearConnections();
- }
- }
- }
- //-------------------------------------------------------------------------------------------//
- void CATLControlsModel::ReloadAllConnections()
- {
- const size_t size = m_controls.size();
- for (size_t i = 0; i < size; ++i)
- {
- CATLControl* control = m_controls[i].get();
- if (control)
- {
- control->ReloadConnections();
- }
- }
- }
- } // namespace AudioControls
|