initial port for Cevo game in C++ (libs)
This commit is contained in:
commit
8436242032
375 files changed
+46282
No files matched your search
@@ -0,0 +1,97 @@
|
||||
#include "Cevo/CevoGraphicSet.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QXmlStreamReader>
|
||||
#include <QXmlStreamWriter>
|
||||
#include <QtDebug>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
void GraphicSetItem::setBoundsRect(const QRect &rect)
|
||||
{
|
||||
left = rect.left();
|
||||
top = rect.top();
|
||||
width = rect.width();
|
||||
height = rect.height();
|
||||
}
|
||||
|
||||
GraphicSet::~GraphicSet()
|
||||
{
|
||||
qDeleteAll(m_items);
|
||||
}
|
||||
|
||||
GraphicSetItem *GraphicSet::searchByName(const QString &itemName) const
|
||||
{
|
||||
for (GraphicSetItem *item : m_items) {
|
||||
if (item->name == itemName)
|
||||
return item;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
GraphicSetItem *GraphicSet::item(const QString &itemName) const
|
||||
{
|
||||
GraphicSetItem *found = searchByName(itemName);
|
||||
if (!found)
|
||||
qWarning("Graphic item %s not found in graphic set %s.", qUtf8Printable(itemName), qUtf8Printable(name));
|
||||
return found;
|
||||
}
|
||||
|
||||
GraphicSetItem *GraphicSet::addNew(const QString &itemName)
|
||||
{
|
||||
auto *newItem = new GraphicSetItem();
|
||||
newItem->name = itemName;
|
||||
newItem->graphicSet = this;
|
||||
m_items.append(newItem);
|
||||
return newItem;
|
||||
}
|
||||
|
||||
void GraphicSet::loadFromFile(const QString &fileName)
|
||||
{
|
||||
qDeleteAll(m_items);
|
||||
m_items.clear();
|
||||
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
return;
|
||||
|
||||
QXmlStreamReader xml(&file);
|
||||
while (!xml.atEnd()) {
|
||||
xml.readNext();
|
||||
if (xml.isStartElement() && xml.name() == QLatin1String("Item")) {
|
||||
const QXmlStreamAttributes attrs = xml.attributes();
|
||||
GraphicSetItem *newItem = addNew(attrs.value("Name").toString());
|
||||
newItem->left = attrs.value("Left").toInt();
|
||||
newItem->top = attrs.value("Top").toInt();
|
||||
newItem->width = attrs.value("Width").toInt();
|
||||
newItem->height = attrs.value("Height").toInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GraphicSet::saveToFile(const QString &fileName) const
|
||||
{
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::WriteOnly))
|
||||
return;
|
||||
|
||||
QXmlStreamWriter xml(&file);
|
||||
xml.setAutoFormatting(true);
|
||||
xml.writeStartDocument();
|
||||
xml.writeStartElement(QLatin1String(GraphicSetFileRootNode));
|
||||
xml.writeStartElement(QLatin1String("Items"));
|
||||
for (GraphicSetItem *item : m_items) {
|
||||
xml.writeStartElement(QLatin1String("Item"));
|
||||
xml.writeAttribute(QLatin1String("Name"), item->name);
|
||||
xml.writeAttribute(QLatin1String("Left"), QString::number(item->left));
|
||||
xml.writeAttribute(QLatin1String("Top"), QString::number(item->top));
|
||||
xml.writeAttribute(QLatin1String("Width"), QString::number(item->width));
|
||||
xml.writeAttribute(QLatin1String("Height"), QString::number(item->height));
|
||||
xml.writeEndElement();
|
||||
}
|
||||
xml.writeEndElement();
|
||||
xml.writeEndElement();
|
||||
xml.writeEndDocument();
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
Reference in new issue
Block a user