Go to the documentation of this file.00001 #include "arenaelementtype.h"
00002 #include "arenaelement.h"
00003
00004 #include <QDebug>
00005
00006 ArenaElementType::ArenaElementType()
00007 : m_instanceCount(0)
00008 {
00009 }
00010
00011 bool ArenaElementType::load(const QDir &folderDir)
00012 {
00013 m_name = folderDir.dirName();
00014
00015 QFile in(folderDir.filePath("properties.xml"));
00016 Q_ASSERT(in.exists());
00017 in.open(QFile::ReadOnly);
00018 QDomDocument doc;
00019 doc.setContent(&in);
00020 in.close();
00021
00022 QDomElement node = doc.firstChild().toElement();
00023 Q_ASSERT(!node.isNull());
00024 if (node.tagName() != "properties")
00025 {
00026 qDebug() << "Error loading properties.xml in " << QFileInfo(in).filePath() << ": Root element must be <property>!";
00027 return false;
00028 }
00029
00030 QDomNodeList childNodes = node.childNodes();
00031 for (int i = 0; i < childNodes.size(); i++)
00032 {
00033 QDomElement child = childNodes.at(i).toElement();
00034 Q_ASSERT(!child.isNull());
00035
00036
00037
00038
00039
00040 if (child.tagName() == "type")
00041 {
00042 if (child.text() == "floor")
00043 m_type = FloorType;
00044 else if (child.text() == "wall")
00045 m_type = WallType;
00046 else if (child.text() == "mountable-item")
00047 m_type = MountableItemType;
00048 else if (child.text() == "item")
00049 m_type = ItemType;
00050 else
00051 {
00052 qDebug() << "Error loading properties.xml in " << QFileInfo(in).filePath() << ": Unknown element type!";
00053 return false;
00054 }
00055 }
00056 else if (child.tagName() == "pixmap")
00057 {
00058 m_pixmap = QPixmap(folderDir.filePath(child.text()));
00059 }
00060 else if (child.tagName() == "mesh")
00061 {
00062 m_mesh = child.text();
00063 }
00064 else if (child.tagName() == "meta-info")
00065 {
00066 QDomNodeList metaInfos = child.childNodes();
00067 for (int i = 0; i < metaInfos.size(); i++)
00068 {
00069 QDomElement metaInfo = metaInfos.at(i).toElement();
00070 Q_ASSERT(!metaInfo.isNull());
00071
00072 if (metaInfo.tagName() == "mi")
00073 {
00074 if (!metaInfo.hasAttribute("desc"))
00075 {
00076 qDebug() << "Warning: meta-info element is missing \"desc\" attribute in "
00077 << QFileInfo(in).filePath();
00078 continue;
00079 }
00080 QString desc = metaInfo.attribute("desc");
00081
00082 if (metaInfo.text().isEmpty())
00083 {
00084 qDebug() << "Warning: Empty meta-info element in "
00085 << QFileInfo(in).filePath();
00086 continue;
00087 }
00088 QString value = metaInfo.text();
00089 m_metaInfos.append(QStringPair(desc, value));
00090 }
00091 }
00092 }
00093 else if (child.tagName() == "item-mount-points")
00094 {
00095 QDomNodeList itemMountPoints = child.childNodes();
00096 for (int i = 0; i < itemMountPoints.size(); i++)
00097 {
00098 QDomElement itemMountPoint = itemMountPoints.at(i).toElement();
00099 Q_ASSERT(!itemMountPoint.isNull());
00100
00101 if (itemMountPoint.tagName() == "item-mount-point")
00102 {
00103 QPointF pos;
00104 QString desc;
00105 if (itemMountPoint.hasAttribute("x"))
00106 pos.rx() = itemMountPoint.attribute("x").toFloat();
00107 if (itemMountPoint.hasAttribute("y"))
00108 pos.ry() = itemMountPoint.attribute("y").toFloat();
00109 if (itemMountPoint.hasAttribute("desc"))
00110 desc = itemMountPoint.attribute("desc");
00111 m_itemMountPoints.push_back(ItemMountPoint(desc, pos));
00112 }
00113 }
00114 }
00115 }
00116 return true;
00117 }
00118
00119 QString ArenaElementType::humanReadableName() const
00120 {
00121 foreach (QStringPair metaInfo, m_metaInfos)
00122 if (metaInfo.first == "Name")
00123 return metaInfo.second;
00124 return name();
00125 }
00126
00127 ArenaElement* ArenaElementType::createInstance() const
00128 {
00129 ArenaElement *element = new ArenaElement(this, m_instanceCount++);
00130 return element;
00131 }