Added support for enum properties

This commit is contained in:
parax0 2015-10-19 04:35:05 -06:00
parent 0df6ca100b
commit 1dcfa63f3c
10 changed files with 283 additions and 19 deletions

View File

@ -377,6 +377,27 @@ void CTemplateWriter::SaveStructTemplate(CStructTemplate *pTemp, CMasterTemplate
structXML.SaveFile(outFile.c_str()); structXML.SaveFile(outFile.c_str());
} }
void CTemplateWriter::SaveEnumTemplate(CEnumTemplate *pTemp, const std::string& dir)
{
// Create directory
std::string outFile = dir + pTemp->mSourceFile;
std::string outDir = StringUtil::GetFileDirectory(outFile);
std::string name = StringUtil::GetFileName(pTemp->mSourceFile);
boost::filesystem::create_directory(outDir);
// Create new document and write enumerators to it
XMLDocument enumXML;
XMLDeclaration *pDecl = enumXML.NewDeclaration();
enumXML.LinkEndChild(pDecl);
XMLElement *pBase = enumXML.NewElement("enum");
SaveEnumerators(&enumXML, pBase, pTemp);
enumXML.LinkEndChild(pBase);
enumXML.SaveFile(outFile.c_str());
}
void CTemplateWriter::SaveProperties(XMLDocument *pDoc, XMLElement *pParent, CStructTemplate *pTemp, CMasterTemplate *pMaster, const std::string& dir) void CTemplateWriter::SaveProperties(XMLDocument *pDoc, XMLElement *pParent, CStructTemplate *pTemp, CMasterTemplate *pMaster, const std::string& dir)
{ {
for (u32 iProp = 0; iProp < pTemp->Count(); iProp++) for (u32 iProp = 0; iProp < pTemp->Count(); iProp++)
@ -422,6 +443,30 @@ void CTemplateWriter::SaveProperties(XMLDocument *pDoc, XMLElement *pParent, CSt
pParent->LinkEndChild(pElem); pParent->LinkEndChild(pElem);
} }
else if (pProp->Type() == eEnumProperty)
{
CEnumTemplate *pEnumTemp = static_cast<CEnumTemplate*>(pProp);
bool isExternal = (!pEnumTemp->mSourceFile.empty());
XMLElement *pElem = pDoc->NewElement("enum");
pElem->SetAttribute("ID", strID.c_str());
if ((!pMaster->HasPropertyList()) || (pProp->PropertyID() == -1))
pElem->SetAttribute("name", pProp->Name().c_str());
if (isExternal)
{
SaveEnumTemplate(pEnumTemp, dir);
pElem->SetAttribute("template", pEnumTemp->mSourceFile.c_str());
}
else
{
SaveEnumerators(pDoc, pElem, pEnumTemp);
}
pParent->LinkEndChild(pElem);
}
else else
{ {
XMLElement *pElem = pDoc->NewElement("property"); XMLElement *pElem = pDoc->NewElement("property");
@ -456,3 +501,13 @@ void CTemplateWriter::SaveProperties(XMLDocument *pDoc, XMLElement *pParent, CSt
} }
} }
void CTemplateWriter::SaveEnumerators(XMLDocument *pDoc, XMLElement *pParent, CEnumTemplate *pTemp)
{
for (u32 iEnum = 0; iEnum < pTemp->NumEnumerators(); iEnum++)
{
XMLElement *pElem = pDoc->NewElement("enumerator");
pElem->SetAttribute("ID", StringUtil::ToHexString(pTemp->EnumeratorID(iEnum), true, true, 8).c_str());
pElem->SetAttribute("name", pTemp->EnumeratorName(iEnum).c_str());
pParent->LinkEndChild(pElem);
}
}

View File

@ -14,7 +14,9 @@ public:
static void SavePropertyList(CMasterTemplate *pMaster, const std::string& dir); static void SavePropertyList(CMasterTemplate *pMaster, const std::string& dir);
static void SaveScriptTemplate(CScriptTemplate *pTemp, const std::string& dir); static void SaveScriptTemplate(CScriptTemplate *pTemp, const std::string& dir);
static void SaveStructTemplate(CStructTemplate *pTemp, CMasterTemplate *pMaster, const std::string& dir); static void SaveStructTemplate(CStructTemplate *pTemp, CMasterTemplate *pMaster, const std::string& dir);
static void SaveEnumTemplate(CEnumTemplate *pTemp, const std::string& dir);
static void SaveProperties(tinyxml2::XMLDocument *pDoc, tinyxml2::XMLElement *pParent, CStructTemplate *pTemp, CMasterTemplate *pMaster, const std::string& dir); static void SaveProperties(tinyxml2::XMLDocument *pDoc, tinyxml2::XMLElement *pParent, CStructTemplate *pTemp, CMasterTemplate *pMaster, const std::string& dir);
static void SaveEnumerators(tinyxml2::XMLDocument *pDoc, tinyxml2::XMLElement *pParent, CEnumTemplate *pTemp);
// todo: save enum templates // todo: save enum templates
}; };

View File

@ -58,6 +58,13 @@ CPropertyStruct* CScriptLoader::LoadStructMP1(CInputStream& SCLY, CStructTemplat
pProp = new CLongProperty(v); pProp = new CLongProperty(v);
break; break;
} }
case eEnumProperty: {
CEnumTemplate *pTemp = static_cast<CEnumTemplate*>(pPropTmp);
long ID = SCLY.ReadLong();
long index = pTemp->EnumeratorIndex(ID);
pProp = new CEnumProperty(index);
break;
}
case eFloatProperty: { case eFloatProperty: {
float v = SCLY.ReadFloat(); float v = SCLY.ReadFloat();
pProp = new CFloatProperty(v); pProp = new CFloatProperty(v);
@ -265,6 +272,15 @@ void CScriptLoader::LoadStructMP2(CInputStream& SCLY, CPropertyStruct *pStruct,
break; break;
} }
case eEnumProperty: {
CEnumProperty *pEnumCast = static_cast<CEnumProperty*>(pProp);
CEnumTemplate *pTemp = static_cast<CEnumTemplate*>(pPropTemp);
long ID = SCLY.ReadLong();
long index = pTemp->EnumeratorIndex(ID);
pEnumCast->Set(index);
break;
}
case eFloatProperty: { case eFloatProperty: {
CFloatProperty *pFloatCast = static_cast<CFloatProperty*>(pProp); CFloatProperty *pFloatCast = static_cast<CFloatProperty*>(pProp);
pFloatCast->Set(SCLY.ReadFloat()); pFloatCast->Set(SCLY.ReadFloat());

View File

@ -2,6 +2,31 @@
#include "CWorldLoader.h" #include "CWorldLoader.h"
#include <Core/Log.h> #include <Core/Log.h>
void CTemplateLoader::LoadEnumerators(tinyxml2::XMLElement *pElem, CEnumTemplate *pTemp, const std::string& templateName)
{
tinyxml2::XMLElement *pChild = pElem->FirstChildElement("enumerator");
while (pChild)
{
const char *kpID = pChild->Attribute("value");
const char *kpName = pChild->Attribute("name");
if (kpID && kpName)
pTemp->mEnumerators.push_back(CEnumTemplate::SEnumerator(kpName, StringUtil::ToInt32(kpID)));
else
{
std::string LogErrorBase = "Couldn't parse enumerator in " + templateName + "; ";
if (!kpID && kpName) Log::Error(LogErrorBase + "no valid ID (" + kpName + ")");
if (kpID && !kpName) Log::Error(LogErrorBase + "no valid name (ID " + kpID + ")");
else Log::Error(LogErrorBase + "no valid ID or name");
}
pChild = pChild->NextSiblingElement("enumerator");
}
}
void CTemplateLoader::LoadStructProperties(tinyxml2::XMLElement *pElem, CStructTemplate *pTemp, const std::string& templateName) void CTemplateLoader::LoadStructProperties(tinyxml2::XMLElement *pElem, CStructTemplate *pTemp, const std::string& templateName)
{ {
tinyxml2::XMLElement *pChild = pElem->FirstChildElement(); tinyxml2::XMLElement *pChild = pElem->FirstChildElement();
@ -135,6 +160,48 @@ CPropertyTemplate* CTemplateLoader::LoadPropertyTemplate(tinyxml2::XMLElement *p
return pStruct; return pStruct;
} }
// Load Enum
else if (strcmp(pElem->Name(), "enum") == 0)
{
CEnumTemplate *pEnum = new CEnumTemplate(ID);
// Read children enumerators
// Priority: [Embedded] -> [Template]
// Embedded
if (!pElem->NoChildren())
LoadEnumerators(pElem, pEnum, templateName);
// Template
else if (kpTemplateStr)
{
std::string tempPath = mMasterDir + kpTemplateStr;
tinyxml2::XMLDocument enumXML;
enumXML.LoadFile(tempPath.c_str());
if (enumXML.Error())
Log::Error("Couldn't open enum XML: " + mMasterDir + kpTemplateStr);
else
{
tinyxml2::XMLElement *pRoot = enumXML.FirstChildElement("enum");
pEnum->mSourceFile = kpTemplateStr;
if (pRoot->Attribute("name"))
pEnum->mPropName = pRoot->Attribute("name");
LoadEnumerators(pRoot, pEnum, kpTemplateStr );
}
// Name
if (!name.empty())
pEnum->mPropName = name;
return pEnum;
}
}
return nullptr; return nullptr;
} }

View File

@ -15,6 +15,7 @@ class CTemplateLoader
CTemplateLoader(const std::string& templatesDir) : mTemplatesDir(templatesDir) {} CTemplateLoader(const std::string& templatesDir) : mTemplatesDir(templatesDir) {}
// Load Property // Load Property
void LoadEnumerators(tinyxml2::XMLElement *pElem, CEnumTemplate *pTemp, const std::string& templateName);
void LoadStructProperties(tinyxml2::XMLElement *pElem, CStructTemplate *pTemp, const std::string& templateName); void LoadStructProperties(tinyxml2::XMLElement *pElem, CStructTemplate *pTemp, const std::string& templateName);
CPropertyTemplate* LoadPropertyTemplate(tinyxml2::XMLElement *pElem, const std::string& templateName); CPropertyTemplate* LoadPropertyTemplate(tinyxml2::XMLElement *pElem, const std::string& templateName);

View File

@ -100,6 +100,7 @@ CPropertyStruct* CPropertyStruct::CopyFromTemplate(CStructTemplate *pTemp)
case eByteProperty: pProp = new CByteProperty(0); break; case eByteProperty: pProp = new CByteProperty(0); break;
case eShortProperty: pProp = new CShortProperty(0); break; case eShortProperty: pProp = new CShortProperty(0); break;
case eLongProperty: pProp = new CLongProperty(0); break; case eLongProperty: pProp = new CLongProperty(0); break;
case eEnumProperty: pProp = new CEnumProperty(0); break;
case eFloatProperty: pProp = new CFloatProperty(0.f); break; case eFloatProperty: pProp = new CFloatProperty(0.f); break;
case eStringProperty: pProp = new CStringProperty(""); break; case eStringProperty: pProp = new CStringProperty(""); break;
case eVector3Property: pProp = new CVector3Property(CVector3f::skZero); break; case eVector3Property: pProp = new CVector3Property(CVector3f::skZero); break;

View File

@ -59,6 +59,7 @@ typedef __CProperty<bool, eBoolProperty> CBoolProperty;
typedef __CProperty<char, eByteProperty> CByteProperty; typedef __CProperty<char, eByteProperty> CByteProperty;
typedef __CProperty<short, eShortProperty> CShortProperty; typedef __CProperty<short, eShortProperty> CShortProperty;
typedef __CProperty<long, eLongProperty> CLongProperty; typedef __CProperty<long, eLongProperty> CLongProperty;
typedef __CProperty<long, eEnumProperty> CEnumProperty;
typedef __CProperty<float, eFloatProperty> CFloatProperty; typedef __CProperty<float, eFloatProperty> CFloatProperty;
typedef __CProperty<std::string, eStringProperty> CStringProperty; typedef __CProperty<std::string, eStringProperty> CStringProperty;
typedef __CProperty<CVector3f, eVector3Property> CVector3Property; typedef __CProperty<CVector3f, eVector3Property> CVector3Property;

View File

@ -7,6 +7,7 @@ EPropertyType PropStringToPropEnum(std::string prop)
if (prop == "byte") return eByteProperty; if (prop == "byte") return eByteProperty;
if (prop == "short") return eShortProperty; if (prop == "short") return eShortProperty;
if (prop == "long") return eLongProperty; if (prop == "long") return eLongProperty;
if (prop == "enum") return eEnumProperty;
if (prop == "float") return eFloatProperty; if (prop == "float") return eFloatProperty;
if (prop == "string") return eStringProperty; if (prop == "string") return eStringProperty;
if (prop == "color") return eColorProperty; if (prop == "color") return eColorProperty;
@ -27,6 +28,7 @@ std::string PropEnumToPropString(EPropertyType prop)
case eByteProperty: return "byte"; case eByteProperty: return "byte";
case eShortProperty: return "short"; case eShortProperty: return "short";
case eLongProperty: return "long"; case eLongProperty: return "long";
case eEnumProperty: return "enum";
case eFloatProperty: return "float"; case eFloatProperty: return "float";
case eStringProperty: return "string"; case eStringProperty: return "string";
case eColorProperty: return "color"; case eColorProperty: return "color";

View File

@ -17,13 +17,37 @@ protected:
std::string mPropName; std::string mPropName;
u32 mPropID; u32 mPropID;
public: public:
CPropertyTemplate(u32 ID) { mPropID = ID; } CPropertyTemplate(u32 ID)
CPropertyTemplate(EPropertyType type, std::string name, u32 ID) : mPropType(type), mPropName(name), mPropID(ID) {} : mPropID(ID)
{
}
virtual EPropertyType Type() const { return mPropType; } CPropertyTemplate(EPropertyType type, std::string name, u32 ID)
inline std::string Name() const { return mPropName; } : mPropType(type),
inline u32 PropertyID() const { return mPropID; } mPropName(name),
inline void SetName(const std::string& Name) { mPropName = Name; } mPropID(ID)
{
}
virtual EPropertyType Type() const
{
return mPropType;
}
inline std::string Name() const
{
return mPropName;
}
inline u32 PropertyID() const
{
return mPropID;
}
inline void SetName(const std::string& Name)
{
mPropName = Name;
}
}; };
class CFileTemplate : public CPropertyTemplate class CFileTemplate : public CPropertyTemplate
@ -35,13 +59,87 @@ class CFileTemplate : public CPropertyTemplate
public: public:
CFileTemplate(u32 ID) : CPropertyTemplate(ID) { mPropType = eFileProperty; } CFileTemplate(u32 ID) : CPropertyTemplate(ID) { mPropType = eFileProperty; }
CFileTemplate(std::string name, u32 ID, const CStringList& extensions) CFileTemplate(const std::string& name, u32 ID, const CStringList& extensions)
: CPropertyTemplate(ID) { : CPropertyTemplate(ID)
mPropType = eFileProperty; mPropName = name; mAcceptedExtensions = extensions; {
mPropType = eFileProperty;
mPropName = name;
mAcceptedExtensions = extensions;
} }
EPropertyType Type() const { return eFileProperty; } EPropertyType Type() const
const CStringList& Extensions() const { return mAcceptedExtensions; } {
return eFileProperty;
}
const CStringList& Extensions() const
{
return mAcceptedExtensions;
}
};
class CEnumTemplate : public CPropertyTemplate
{
friend class CTemplateLoader;
friend class CTemplateWriter;
struct SEnumerator
{
std::string Name;
u32 ID;
SEnumerator(const std::string& _name, u32 _ID)
: Name(_name), ID(_ID) {}
};
std::vector<SEnumerator> mEnumerators;
std::string mSourceFile;
public:
CEnumTemplate(u32 ID)
: CPropertyTemplate(ID)
{
mPropType = eEnumProperty;
}
CEnumTemplate(const std::string& name, u32 ID)
: CPropertyTemplate(eEnumProperty, name, ID)
{}
EPropertyType Type() const
{
return eEnumProperty;
}
u32 NumEnumerators()
{
return mEnumerators.size();
}
u32 EnumeratorIndex(u32 enumID)
{
for (u32 iEnum = 0; iEnum < mEnumerators.size(); iEnum++)
{
if (mEnumerators[iEnum].ID == enumID)
return iEnum;
}
return -1;
}
u32 EnumeratorID(u32 enumIndex)
{
if (mEnumerators.size() > enumIndex)
return mEnumerators[enumIndex].ID;
else return -1;
}
std::string EnumeratorName(u32 enumIndex)
{
if (mEnumerators.size() > enumIndex)
return mEnumerators[enumIndex].Name;
else return "INVALID ENUM INDEX";
}
}; };
class CStructTemplate : public CPropertyTemplate class CStructTemplate : public CPropertyTemplate

View File

@ -135,6 +135,27 @@ void WPropertyEditor::CreateEditor()
break; break;
} }
// Enum - QComboBox
case eEnumProperty:
{
CEnumProperty *pEnumCast = static_cast<CEnumProperty*>(mpProperty);
CEnumTemplate *pTemplate = static_cast<CEnumTemplate*>(pEnumCast->Template());
QComboBox *pComboBox = new QComboBox(this);
for (u32 iEnum = 0; iEnum < pTemplate->NumEnumerators(); iEnum++)
{
std::string name = pTemplate->EnumeratorName(iEnum);
pComboBox->addItem(QString::fromStdString(name));
}
pComboBox->setCurrentIndex(pEnumCast->Get());
pComboBox->setFocusPolicy(Qt::StrongFocus);
pComboBox->setContextMenuPolicy(Qt::NoContextMenu);
mUI.EditorWidget = pComboBox;
break;
}
// Float - WDraggableSpinBox // Float - WDraggableSpinBox
case eFloatProperty: case eFloatProperty:
{ {
@ -199,11 +220,6 @@ void WPropertyEditor::CreateEditor()
break; break;
} }
// Enum - todo (will be QComboBox)
case eEnumProperty:
mUI.EditorWidget = new QLabel("[placeholder]", this);
break;
// File - WResourceSelector // File - WResourceSelector
case eFileProperty: case eFileProperty:
{ {
@ -311,6 +327,14 @@ void WPropertyEditor::UpdateEditor()
break; break;
} }
case eEnumProperty:
{
CEnumProperty *pEnumCast = static_cast<CEnumProperty*>(mpProperty);
QComboBox *pComboBox = static_cast<QComboBox*>(mUI.EditorWidget);
pComboBox->setCurrentIndex(pEnumCast->Get());
break;
}
case eFloatProperty: case eFloatProperty:
{ {
CFloatProperty *pFloatCast = static_cast<CFloatProperty*>(mpProperty); CFloatProperty *pFloatCast = static_cast<CFloatProperty*>(mpProperty);
@ -350,9 +374,6 @@ void WPropertyEditor::UpdateEditor()
break; break;
} }
case eEnumProperty:
break;
case eFileProperty: case eFileProperty:
{ {
CFileProperty *pFileCast = static_cast<CFileProperty*>(mpProperty); CFileProperty *pFileCast = static_cast<CFileProperty*>(mpProperty);