123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- /*
- * 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 "EditorDefs.h"
- #include "Variable.h"
- #include "UsedResources.h" // for CUsedResources
- //////////////////////////////////////////////////////////////////////////
- CVarBlock* CVarBlock::Clone(bool bRecursive) const
- {
- CVarBlock* vb = new CVarBlock;
- for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- vb->AddVariable(var->Clone(bRecursive));
- }
- return vb;
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::CopyValues(const CVarBlock* fromVarBlock)
- {
- // Copy all variables.
- int numSrc = fromVarBlock->GetNumVariables();
- int numTrg = GetNumVariables();
- for (int i = 0; i < numSrc && i < numTrg; i++)
- {
- GetVariable(i)->CopyValue(fromVarBlock->GetVariable(i));
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::CopyValuesByName(CVarBlock* fromVarBlock)
- {
- // Copy values using saving and loading to/from xml.
- XmlNodeRef node = XmlHelpers::CreateXmlNode("Temp");
- fromVarBlock->Serialize(node, false);
- Serialize(node, true);
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::OnSetValues()
- {
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- var->OnSetValue(true);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::SetRecreateSplines()
- {
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- var->SetFlagRecursive(IVariable::UI_CREATE_SPLINE);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::AddVariable(IVariable* var)
- {
- //assert( !strstr(var->GetName(), " ") ); // spaces not allowed because of serialization
- m_vars.push_back(var);
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::AddVariable(IVariable* pVar, const char* varName, unsigned char dataType)
- {
- if (varName)
- {
- pVar->SetName(varName);
- }
- pVar->SetDataType(dataType);
- AddVariable(pVar);
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::AddVariable(CVariableBase& var, const char* varName, unsigned char dataType)
- {
- if (varName)
- {
- var.SetName(varName);
- }
- var.SetDataType(dataType);
- AddVariable(&var);
- }
- //////////////////////////////////////////////////////////////////////////
- bool CVarBlock::DeleteVariable(IVariable* var, bool bRecursive)
- {
- bool found = stl::find_and_erase(m_vars, var);
- if (!found && bRecursive)
- {
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- if ((*it)->DeleteVariable(var, bRecursive))
- {
- return true;
- }
- }
- }
- return found;
- }
- //////////////////////////////////////////////////////////////////////////
- bool CVarBlock::IsContainsVariable(IVariable* pVar, bool bRecursive) const
- {
- for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- if (*it == pVar)
- {
- return true;
- }
- }
- // If not found search childs.
- if (bRecursive)
- {
- // Search all top level variables.
- for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- if ((*it)->IsContainsVariable(pVar))
- {
- return true;
- }
- }
- }
- return false;
- }
- namespace
- {
- IVariable* FindVariable(const char* name, bool bRecursive, bool bHumanName, const std::vector<IVariablePtr>& vars)
- {
- // Search all top level variables.
- for (std::vector<IVariablePtr>::const_iterator it = vars.begin(); it != vars.end(); ++it)
- {
- IVariable* var = *it;
- if (bHumanName && QString::compare(var->GetHumanName(), name, Qt::CaseInsensitive) == 0)
- {
- return var;
- }
- else if (!bHumanName && QString::compare(var->GetName(), name) == 0)
- {
- return var;
- }
- }
- // If not found search childs.
- if (bRecursive)
- {
- // Search all top level variables.
- for (std::vector<IVariablePtr>::const_iterator it = vars.begin(); it != vars.end(); ++it)
- {
- IVariable* var = *it;
- IVariable* found = var->FindVariable(name, bRecursive, bHumanName);
- if (found)
- {
- return found;
- }
- }
- }
- return nullptr;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- IVariable* CVarBlock::FindVariable(const char* name, bool bRecursive, bool bHumanName) const
- {
- return ::FindVariable(name, bRecursive, bHumanName, m_vars);
- }
- //////////////////////////////////////////////////////////////////////////
- IVariable* CVariableArray::FindVariable(const char* name, bool bRecursive, bool bHumanName) const
- {
- return ::FindVariable(name, bRecursive, bHumanName, m_vars);
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::Serialize(XmlNodeRef vbNode, bool load)
- {
- if (load)
- {
- // Loading.
- QString name;
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- if (var->GetNumVariables())
- {
- XmlNodeRef child = vbNode->findChild(var->GetName().toUtf8().data());
- if (child)
- {
- var->Serialize(child, load);
- }
- }
- else
- {
- var->Serialize(vbNode, load);
- }
- }
- }
- else
- {
- // Saving.
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- if (var->GetNumVariables())
- {
- XmlNodeRef child = vbNode->newChild(var->GetName().toUtf8().data());
- var->Serialize(child, load);
- }
- else
- {
- var->Serialize(vbNode, load);
- }
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::ReserveNumVariables(int numVars)
- {
- m_vars.reserve(numVars);
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::WireVar(IVariable* src, IVariable* trg, bool bWire)
- {
- if (bWire)
- {
- src->Wire(trg);
- }
- else
- {
- src->Unwire(trg);
- }
- int numSrcVars = src->GetNumVariables();
- if (numSrcVars > 0)
- {
- int numTrgVars = trg->GetNumVariables();
- for (int i = 0; i < numSrcVars && i < numTrgVars; i++)
- {
- WireVar(src->GetVariable(i), trg->GetVariable(i), bWire);
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::Wire(CVarBlock* toVarBlock)
- {
- Variables::iterator tit = toVarBlock->m_vars.begin();
- Variables::iterator sit = m_vars.begin();
- for (; sit != m_vars.end() && tit != toVarBlock->m_vars.end(); ++sit, ++tit)
- {
- IVariable* src = *sit;
- IVariable* trg = *tit;
- WireVar(src, trg, true);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::Unwire(CVarBlock* toVarBlock)
- {
- Variables::iterator tit = toVarBlock->m_vars.begin();
- Variables::iterator sit = m_vars.begin();
- for (; sit != m_vars.end() && tit != toVarBlock->m_vars.end(); ++sit, ++tit)
- {
- IVariable* src = *sit;
- IVariable* trg = *tit;
- WireVar(src, trg, false);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::AddOnSetCallback(IVariable::OnSetCallback* func)
- {
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- SetCallbackToVar(func, var, true);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::RemoveOnSetCallback(IVariable::OnSetCallback* func)
- {
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- SetCallbackToVar(func, var, false);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::SetCallbackToVar(IVariable::OnSetCallback* func, IVariable* pVar, bool bAdd)
- {
- if (bAdd)
- {
- pVar->AddOnSetCallback(func);
- }
- else
- {
- pVar->RemoveOnSetCallback(func);
- }
- int numVars = pVar->GetNumVariables();
- if (numVars > 0)
- {
- for (int i = 0; i < numVars; i++)
- {
- SetCallbackToVar(func, pVar->GetVariable(i), bAdd);
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::GatherUsedResources(CUsedResources& resources)
- {
- for (int i = 0; i < GetNumVariables(); i++)
- {
- IVariable* pVar = GetVariable(i);
- GatherUsedResourcesInVar(pVar, resources);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::EnableUpdateCallbacks(bool boEnable)
- {
- for (int i = 0; i < GetNumVariables(); i++)
- {
- IVariable* pVar = GetVariable(i);
- pVar->EnableUpdateCallbacks(boEnable);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::GatherUsedResourcesInVar(IVariable* pVar, CUsedResources& resources)
- {
- int type = pVar->GetDataType();
- if (type == IVariable::DT_TEXTURE)
- {
- // this is file.
- QString filename;
- pVar->Get(filename);
- if (!filename.isEmpty())
- {
- resources.Add(filename.toUtf8().data());
- }
- }
- for (int i = 0; i < pVar->GetNumVariables(); i++)
- {
- GatherUsedResourcesInVar(pVar->GetVariable(i), resources);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- inline bool CompareNames(const IVariable* pVar1, const IVariable* pVar2)
- {
- return (QString::compare(pVar1->GetHumanName(), pVar2->GetHumanName(), Qt::CaseInsensitive) < 0);
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarBlock::Sort()
- {
- std::sort(m_vars.begin(), m_vars.end(), CompareNames);
- }
- //////////////////////////////////////////////////////////////////////////
- CVarObject::CVarObject()
- {}
- //////////////////////////////////////////////////////////////////////////
- CVarObject::~CVarObject()
- {}
- //////////////////////////////////////////////////////////////////////////
- void CVarObject::AddVariable(CVariableBase& var, const QString& varName, VarOnSetCallback* cb, unsigned char dataType)
- {
- if (!m_vars)
- {
- m_vars = new CVarBlock;
- }
- var.AddRef(); // Variables are local and must not be released by CVarBlock.
- var.SetName(varName);
- var.SetDataType(dataType);
- if (cb)
- {
- var.AddOnSetCallback(cb);
- }
- m_vars->AddVariable(&var);
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarObject::AddVariable(CVariableBase& var, const QString& varName, const QString& varHumanName, VarOnSetCallback* cb, unsigned char dataType)
- {
- if (!m_vars)
- {
- m_vars = new CVarBlock;
- }
- var.AddRef(); // Variables are local and must not be released by CVarBlock.
- var.SetName(varName);
- var.SetHumanName(varHumanName);
- var.SetDataType(dataType);
- if (cb)
- {
- var.AddOnSetCallback(cb);
- }
- m_vars->AddVariable(&var);
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarObject::AddVariable(CVariableArray& table, CVariableBase& var, const QString& varName, const QString& varHumanName, VarOnSetCallback* cb, unsigned char dataType)
- {
- if (!m_vars)
- {
- m_vars = new CVarBlock;
- }
- var.AddRef(); // Variables are local and must not be released by CVarBlock.
- var.SetName(varName);
- var.SetHumanName(varHumanName);
- var.SetDataType(dataType);
- if (cb)
- {
- var.AddOnSetCallback(cb);
- }
- table.AddVariable(&var);
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarObject::RemoveVariable(IVariable* var)
- {
- if (m_vars != nullptr)
- {
- m_vars->DeleteVariable(var);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarObject::EnableUpdateCallbacks(bool boEnable)
- {
- if (m_vars != nullptr)
- {
- m_vars->EnableUpdateCallbacks(boEnable);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarObject::OnSetValues()
- {
- if (m_vars != nullptr)
- {
- m_vars->OnSetValues();
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarObject::ReserveNumVariables(int numVars)
- {
- if (m_vars != nullptr)
- {
- m_vars->ReserveNumVariables(numVars);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarObject::CopyVariableValues(CVarObject* sourceObject)
- {
- // Check if compatible types.
- assert(metaObject() == sourceObject->metaObject());
- if (m_vars != nullptr && sourceObject->m_vars != nullptr)
- {
- m_vars->CopyValues(sourceObject->m_vars);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CVarObject::Serialize(XmlNodeRef node, bool load)
- {
- if (m_vars)
- {
- m_vars->Serialize(node, load);
- }
- }
|