Support <repeat> in the GUI XML files, as a primitive mechanism to simplify the definition of repetitive structures (like lists of buttons)

This was SVN commit r7336.
This commit is contained in:
Ykkrosh 2010-02-28 21:36:25 +00:00
parent 275d22742d
commit 633705c832
2 changed files with 56 additions and 8 deletions

View File

@ -1042,6 +1042,8 @@ void CGUI::Xeromyces_ReadRootObjects(XMBElement Element, CXeromyces* pFile, std:
{
int el_script = pFile->GetElementID("script");
std::vector<std::pair<CStr, CStr> > subst;
// Iterate main children
// they should all be <object> or <script> elements
XMBElementList children = Element.GetChildNodes();
@ -1055,7 +1057,7 @@ void CGUI::Xeromyces_ReadRootObjects(XMBElement Element, CXeromyces* pFile, std:
Xeromyces_ReadScript(child, pFile, Paths);
else
// Read in this whole object into the GUI
Xeromyces_ReadObject(child, pFile, m_BaseObject, Paths);
Xeromyces_ReadObject(child, pFile, m_BaseObject, subst, Paths);
}
}
@ -1126,7 +1128,7 @@ void CGUI::Xeromyces_ReadRootSetup(XMBElement Element, CXeromyces* pFile)
}
}
void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent, std::set<VfsPath>& Paths)
void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent, const std::vector<std::pair<CStr, CStr> >& NameSubst, std::set<VfsPath>& Paths)
{
debug_assert(pParent);
int i;
@ -1158,6 +1160,7 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
#define ATTR(x) int attr_##x = pFile->GetAttributeID(#x)
ELMT(object);
ELMT(action);
ELMT(repeat);
ATTR(style);
ATTR(type);
ATTR(name);
@ -1214,7 +1217,13 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
// Also the name needs some special attention
if (attr.Name == attr_name)
{
object->SetName(CStr(attr.Value));
CStr name (attr.Value);
// Apply the requested substitutions
for (size_t j = 0; j < NameSubst.size(); ++j)
name.Replace(NameSubst[j].first, NameSubst[j].second);
object->SetName(name);
NameSet = true;
continue;
}
@ -1274,7 +1283,7 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
if (element_name == elmt_object)
{
// Call this function on the child
Xeromyces_ReadObject(child, pFile, object, Paths);
Xeromyces_ReadObject(child, pFile, object, NameSubst, Paths);
}
else if (element_name == elmt_action)
{
@ -1305,6 +1314,10 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
CStr action = CStr(child.GetAttributes().GetNamedItem(attr_on));
object->RegisterScriptHandler(action.LowerCase(), code, this);
}
else if (element_name == elmt_repeat)
{
Xeromyces_ReadRepeat(child, pFile, object, Paths);
}
else
{
// Try making the object read the tag.
@ -1355,6 +1368,32 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
}
}
void CGUI::Xeromyces_ReadRepeat(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent, std::set<VfsPath>& Paths)
{
#define ELMT(x) int elmt_##x = pFile->GetElementID(#x)
#define ATTR(x) int attr_##x = pFile->GetAttributeID(#x)
ELMT(object);
ATTR(count);
XMBAttributeList attributes = Element.GetAttributes();
int count = CStr(attributes.GetNamedItem(attr_count)).ToInt();
for (int n = 0; n < count; ++n)
{
std::vector<std::pair<CStr, CStr> > subst;
subst.push_back(std::make_pair(CStr("[n]"), "[" + CStr(n) + "]"));
XERO_ITER_EL(Element, child)
{
if (child.GetNodeName() == elmt_object)
{
Xeromyces_ReadObject(child, pFile, pParent, subst, Paths);
}
}
}
}
void CGUI::Xeromyces_ReadScript(XMBElement Element, CXeromyces* pFile, std::set<VfsPath>& Paths)
{
// Check for a 'file' parameter

View File

@ -420,23 +420,32 @@ private:
* in other stuff that can be found within an object. Check the
* tree in the beginning of this class' Xeromyces_* section.
*
* Reads in the root element <sprites> (the DOMElement).
*
* @param Element The Xeromyces object that represents
* the object-tag.
* @param pFile The Xeromyces object for the file being read
* @param pParent Parent to add this object as child in.
* @param NameSubst A set of substitution strings that will be
* applied to all object names within this object.
* @param Paths Output set of file paths that this GUI object
* relies on.
*
* @see LoadXmlFile()
*/
void Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent, std::set<VfsPath>& Paths);
void Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent, const std::vector<std::pair<CStr, CStr> >& NameSubst, std::set<VfsPath>& Paths);
/**
* Reads in the element <repeat>, which repeats its child <object>s
* 'count' times, replacing the string "[n]" in its descendants' names
* with "[0]", "[1]", etc.
*/
void Xeromyces_ReadRepeat(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent, std::set<VfsPath>& Paths);
/**
* Reads in the element <script> (the XMBElement) and executes
* the script's code.
*
* @param Element The Xeromyces object that represents
* the sprite-tag.
* the script-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXmlFile()