diff --git a/chunk.cpp b/chunk.cpp index 0ef19982..c4f9827e 100644 --- a/chunk.cpp +++ b/chunk.cpp @@ -43,8 +43,8 @@ void Chunk::load(NBT &nbt) highest=0; Tag *level=nbt.at("Level"); - chunkX = level->at("xPos")->toInt(); - chunkZ = level->at("zPos")->toInt(); + chunkX = level->at("xPos")->toInt(); + chunkZ = level->at("zPos")->toInt(); Tag *biomes=level->at("Biomes"); memcpy(this->biomes,biomes->toByteArray(),biomes->length()); @@ -74,14 +74,14 @@ void Chunk::load(NBT &nbt) loaded=true; - Tag* entitylist = level->at("Entities"); - int numEntities = entitylist->length(); - for (int i = 0; i < numEntities; ++i) - { - Entity e; - e.load(entitylist->at(i)); - entities.insertMulti("Entity", e); - } + Tag* entitylist = level->at("Entities"); + int numEntities = entitylist->length(); + for (int i = 0; i < numEntities; ++i) + { + Entity e; + e.load(entitylist->at(i)); + entities.insertMulti("Entity", e); + } for (int i=15;i>=0;i--) //check for the highest block in this chunk @@ -93,7 +93,7 @@ void Chunk::load(NBT &nbt) highest=i*16+(j>>8); return; } - } + } } Chunk::~Chunk() { @@ -109,28 +109,28 @@ Chunk::~Chunk() quint16 ChunkSection::getBlock(int x, int y, int z) { - int xoffset = x; - int yoffset = (y&0x0f)<<8; - int zoffset = z<<4; - return blocks[xoffset+yoffset+zoffset]; + int xoffset = x; + int yoffset = (y&0x0f)<<8; + int zoffset = z<<4; + return blocks[xoffset+yoffset+zoffset]; } quint8 ChunkSection::getData(int x, int y, int z) { - int xoffset = x; - int yoffset = (y&0x0f)<<8; - int zoffset = z<<4; - int value = data[(xoffset+yoffset+zoffset)/2]; - if (x&1) value>>=4; - return value&0x0f; + int xoffset = x; + int yoffset = (y&0x0f)<<8; + int zoffset = z<<4; + int value = data[(xoffset+yoffset+zoffset)/2]; + if (x&1) value>>=4; + return value&0x0f; } quint8 ChunkSection::getLight(int x, int y, int z) { - int xoffset = x; - int yoffset = (y&0x0f)<<8; - int zoffset = z<<4; - int value = light[(xoffset+yoffset+zoffset)/2]; - if (x&1) value>>=4; - return value&0x0f; + int xoffset = x; + int yoffset = (y&0x0f)<<8; + int zoffset = z<<4; + int value = light[(xoffset+yoffset+zoffset)/2]; + if (x&1) value>>=4; + return value&0x0f; } diff --git a/chunk.h b/chunk.h index 24df9989..ad0a417e 100644 --- a/chunk.h +++ b/chunk.h @@ -60,12 +60,12 @@ class Chunk ChunkSection *sections[16]; int renderedAt; int renderedFlags; - QSet renderedSpecialBlockTypes; + QSet renderedSpecialBlockTypes; bool loaded; uchar image[16*16*4]; //cached render - QMap entities; - int chunkX; - int chunkZ; + QMap entities; + int chunkX; + int chunkZ; friend class MapView; friend class ChunkCache; friend class WorldSave; diff --git a/entity.cpp b/entity.cpp index 799743ee..784c7ecb 100644 --- a/entity.cpp +++ b/entity.cpp @@ -3,24 +3,24 @@ #include "nbt.h" Entity::Entity(qint64 nx, qint64 ny, qint64 nz, QString nid): - id(nid), - x(nx), - y(ny), - z(nz) + id(nid), + x(nx), + y(ny), + z(nz) { } void Entity::load(Tag *nbt) { - Tag* pos = nbt->at("Pos"); - x = (qint64)pos->at(0)->toDouble(); - y = (qint64)pos->at(1)->toDouble(); - z = (qint64)pos->at(2)->toDouble(); - Tag* id = nbt->at("id"); - if (id) - { - this->id = id->toString(); - properties = nbt->getData(); - } + Tag* pos = nbt->at("Pos"); + x = (qint64)pos->at(0)->toDouble(); + y = (qint64)pos->at(1)->toDouble(); + z = (qint64)pos->at(2)->toDouble(); + Tag* id = nbt->at("id"); + if (id) + { + this->id = id->toString(); + properties = nbt->getData(); + } } diff --git a/entity.h b/entity.h index c3dee9b0..bb503ffd 100644 --- a/entity.h +++ b/entity.h @@ -9,22 +9,22 @@ class Tag; class Entity { public: - Entity(qint64 nx = 0, qint64 ny = 0, qint64 nz = 0, QString nid = ""); + Entity(qint64 nx = 0, qint64 ny = 0, qint64 nz = 0, QString nid = ""); - void load(Tag *t); + void load(Tag *t); - const QString& getId() const { return id; } - const QVariant& getProperties() const { return properties; } - qint64 getX() const { return x; } - qint64 getY() const { return y; } - qint64 getZ() const { return z; } + const QString& getId() const { return id; } + const QVariant& getProperties() const { return properties; } + qint64 getX() const { return x; } + qint64 getY() const { return y; } + qint64 getZ() const { return z; } private: - QString id; - QVariant properties; - qint64 x; - qint64 y; - qint64 z; + QString id; + QVariant properties; + qint64 x; + qint64 y; + qint64 z; }; #endif // ENTITY_H diff --git a/mapview.cpp b/mapview.cpp index 7c4c164f..8a425fb8 100755 --- a/mapview.cpp +++ b/mapview.cpp @@ -111,17 +111,17 @@ void MapView::setDepth(int depth) void MapView::setFlags(int flags) { - this->flags=flags; + this->flags=flags; } void MapView::addSpecialBlockType(QString type) { - specialBlockTypes.insert(type); + specialBlockTypes.insert(type); } void MapView::clearSpecialBlockTypes() { - specialBlockTypes.clear(); + specialBlockTypes.clear(); } @@ -146,9 +146,9 @@ static int lastX=-1,lastY=-1; static bool dragging=false; void MapView::mousePressEvent(QMouseEvent *event) { - lastX=event->x(); - lastY=event->y(); - dragging=true; + lastX=event->x(); + lastY=event->y(); + dragging=true; } void MapView::mouseMoveEvent(QMouseEvent *event) @@ -183,22 +183,22 @@ void MapView::mouseReleaseEvent(QMouseEvent *) void MapView::mouseDoubleClickEvent(QMouseEvent*event) { - int centerblockx=floor(this->x); - int centerblockz=floor(this->z); + int centerblockx=floor(this->x); + int centerblockz=floor(this->z); - int centerx=image.width()/2; - int centery=image.height()/2; + int centerx=image.width()/2; + int centery=image.height()/2; - centerx-=(this->x-centerblockx)*zoom; - centery-=(this->z-centerblockz)*zoom; + centerx-=(this->x-centerblockx)*zoom; + centery-=(this->z-centerblockz)*zoom; - int mx=floor(centerblockx-(centerx-event->x())/zoom); - int mz=floor(centerblockz-(centery-event->y())/zoom); + int mx=floor(centerblockx-(centerx-event->x())/zoom); + int mz=floor(centerblockz-(centery-event->y())/zoom); - //get the y coordinate - int my = getY(mx, mz); + //get the y coordinate + int my = getY(mx, mz); - emit showProperties(mx, my, mz); + emit showProperties(mx, my, mz); } void MapView::wheelEvent(QWheelEvent *event) @@ -322,24 +322,24 @@ void MapView::drawChunk(int x, int z) //fetch the chunk Chunk *chunk=cache.fetch(x,z); - if (chunk && (chunk->renderedAt!=depth || chunk->renderedFlags!=flags || chunk->renderedSpecialBlockTypes != specialBlockTypes)) - { - if (chunk->renderedAt == -1) - { - //this should probably not be in the drawing code? - QPair::iterator, QMap::iterator> range = chunk->entities.equal_range("Entity"); - for (QMap::iterator it = range.first; it != range.second; ++it) - { - emit foundSpecialBlock(it->getX(), - it->getY(), - it->getZ(), - tr("Entity"), - it->getId(), - it->getProperties()); - } - } - renderChunk(chunk); - } + if (chunk && (chunk->renderedAt!=depth || chunk->renderedFlags!=flags || chunk->renderedSpecialBlockTypes != specialBlockTypes)) + { + if (chunk->renderedAt == -1) + { + //this should probably not be in the drawing code? + QPair::iterator, QMap::iterator> range = chunk->entities.equal_range("Entity"); + for (QMap::iterator it = range.first; it != range.second; ++it) + { + emit foundSpecialBlock(it->getX(), + it->getY(), + it->getZ(), + tr("Entity"), + it->getId(), + it->getProperties()); + } + } + renderChunk(chunk); + } //this figures out where on the screen this chunk should be drawn @@ -428,15 +428,15 @@ void MapView::renderChunk(Chunk *chunk) // get data value int data = section->getData(x,y,z); - // get BlockInfo from block value + // get BlockInfo from block value BlockInfo &block=blocks->getBlock(section->getBlock(x,y,z),data); - if (block.alpha==0.0) continue; + if (block.alpha==0.0) continue; // get light value from one block above int light = 0; ChunkSection *section1=NULL; if (y<255) - section1 = chunk->sections[(y+1)>>4]; + section1 = chunk->sections[(y+1)>>4]; if (section1) light = section1->getLight(x,y+1,z); int light1 = light; @@ -467,13 +467,13 @@ void MapView::renderChunk(Chunk *chunk) { // get block info from 1 and 2 above and 1 below quint16 blid1(0), blid2(0), blidB(0); // default to air - int data1(0), data2(0), dataB(0); // default variant + int data1(0), data2(0), dataB(0); // default variant ChunkSection *section2=NULL; ChunkSection *sectionB=NULL; if (y<254) - section2 = chunk->sections[(y+2)>>4]; + section2 = chunk->sections[(y+2)>>4]; if (y>0) - sectionB = chunk->sections[(y-1)>>4]; + sectionB = chunk->sections[(y-1)>>4]; if (section1) { blid1 = section1->getBlock(x,y+1,z); @@ -534,28 +534,28 @@ void MapView::renderChunk(Chunk *chunk) alpha+=block.alpha*(1.0-alpha); } - QSet::iterator itType, itEndType = specialBlockTypes.end(); - for (itType = specialBlockTypes.begin(); itType != itEndType; ++itType) - { - int xPos = chunk->chunkX << 4; - int zPos = chunk->chunkZ << 4; - - QPair::iterator, QMap::iterator> range = chunk->entities.equal_range(*itType); - for (QMap::iterator it = range.first; it != range.second; ++it) - { - if (it->getX() == x + xPos && - it->getZ() == z + zPos && - it->getY() > y - 1 && //since we skip transparent blocks, we want to - it->getY() < y + 3 //allow entities in the air as well - ) - { - //blend it with a bright purple - r = r / 3 + 170; - b = b / 3 + 170; - - } - } - } + QSet::iterator itType, itEndType = specialBlockTypes.end(); + for (itType = specialBlockTypes.begin(); itType != itEndType; ++itType) + { + int xPos = chunk->chunkX << 4; + int zPos = chunk->chunkZ << 4; + + QPair::iterator, QMap::iterator> range = chunk->entities.equal_range(*itType); + for (QMap::iterator it = range.first; it != range.second; ++it) + { + if (it->getX() == x + xPos && + it->getZ() == z + zPos && + it->getY() > y - 1 && //since we skip transparent blocks, we want to + it->getY() < y + 3 //allow entities in the air as well + ) + { + //blend it with a bright purple + r = r / 3 + 170; + b = b / 3 + 170; + + } + } + } // finish deepth (Y) scanning when color is saturated enough if (block.alpha==1.0 || alpha>0.9) @@ -566,16 +566,16 @@ void MapView::renderChunk(Chunk *chunk) *bits++=g; *bits++=r; *bits++=0xff; - } + } } chunk->renderedAt=depth; chunk->renderedFlags=flags; - chunk->renderedSpecialBlockTypes=specialBlockTypes; + chunk->renderedSpecialBlockTypes=specialBlockTypes; } void MapView::getToolTip(int x, int z) { - int cx=floor(x/16.0); + int cx=floor(x/16.0); int cz=floor(z/16.0); Chunk *chunk=cache.fetch(cx,cz); int offset=(x&0xf)+(z&0xf)*16; @@ -583,15 +583,15 @@ void MapView::getToolTip(int x, int z) QString name="Unknown"; QString biome="Unknown Biome"; - QMap entityIds; + QMap entityIds; if (chunk) { int top=depth; if (top>chunk->highest) top=chunk->highest; - int y = 0; - for (y=top;y>=0;y--) + int y = 0; + for (y=top;y>=0;y--) { int sec=y>>4; ChunkSection *section=chunk->sections[sec]; @@ -608,101 +608,101 @@ void MapView::getToolTip(int x, int z) //found block name=block.name; id=section->blocks[offset+yoffset]; - bd=data&0xf; + bd=data&0xf; break; } BiomeInfo &bi=biomes->getBiome(chunk->biomes[(x&0xf)+(z&0xf)*16]); - biome=bi.name; - - QSet::iterator itType, itEndType = specialBlockTypes.end(); - for (itType = specialBlockTypes.begin(); itType != itEndType; ++itType) - { - - QPair::iterator, QMap::iterator> range = chunk->entities.equal_range(*itType); - for (QMap::iterator it = range.first; it != range.second; ++it) - { - if (it->getX() == x && - it->getZ() == z && - it->getY() > y - 1 && //since we skip transparent blocks, we want to - it->getY() < y + 3 //allow entities in the air as well - ) - { - entityIds[it->getId()]++; - } - } - } + biome=bi.name; + + QSet::iterator itType, itEndType = specialBlockTypes.end(); + for (itType = specialBlockTypes.begin(); itType != itEndType; ++itType) + { + + QPair::iterator, QMap::iterator> range = chunk->entities.equal_range(*itType); + for (QMap::iterator it = range.first; it != range.second; ++it) + { + if (it->getX() == x && + it->getZ() == z && + it->getY() > y - 1 && //since we skip transparent blocks, we want to + it->getY() < y + 3 //allow entities in the air as well + ) + { + entityIds[it->getId()]++; + } + } + } } - QString entityStr; - if (!entityIds.empty()) - { - QStringList entities; - QMap::iterator it, itEnd = entityIds.end(); - for (it = entityIds.begin(); it != itEnd; ++it) - { - if (it.value() > 1) - { - entities << it.key() + ":" + QString::number(it.value()); - } - else - { - entities << it.key(); - } - } - entityStr = entities.join(", "); - } - - emit hoverTextChanged(tr("X:%1 Z:%2 - %3 - %4 (%5:%6) %7") + QString entityStr; + if (!entityIds.empty()) + { + QStringList entities; + QMap::iterator it, itEnd = entityIds.end(); + for (it = entityIds.begin(); it != itEnd; ++it) + { + if (it.value() > 1) + { + entities << it.key() + ":" + QString::number(it.value()); + } + else + { + entities << it.key(); + } + } + entityStr = entities.join(", "); + } + + emit hoverTextChanged(tr("X:%1 Z:%2 - %3 - %4 (%5:%6) %7") .arg(x) .arg(z) .arg(biome) .arg(name) .arg(id) - .arg(bd) - .arg(entityStr)); + .arg(bd) + .arg(entityStr)); } void MapView::markBlock(int x, int y, int z, QString type, QString message) { - int cx=floor(x/16.0); - int cz=floor(z/16.0); - Chunk *chunk=cache.fetch(cx,cz); - if (chunk) - { - chunk->entities.insertMulti(type, Entity(x, y, z, message)); - } + int cx=floor(x/16.0); + int cz=floor(z/16.0); + Chunk *chunk=cache.fetch(cx,cz); + if (chunk) + { + chunk->entities.insertMulti(type, Entity(x, y, z, message)); + } } int MapView::getY(int x, int z) { - int cx=floor(x/16.0); - int cz=floor(z/16.0); - Chunk *chunk=cache.fetch(cx,cz); - int offset=(x&0xf)+(z&0xf)*16; - int y = 0; - if (chunk) - { - int top=depth; - if (top>chunk->highest) - top=chunk->highest; - - for (y=top;y>=0;y--) - { - int sec=y>>4; - ChunkSection *section=chunk->sections[sec]; - if (!section) - { - y=(sec<<4)-1; //skip entire section - continue; - } - int yoffset=(y&0xf)<<8; - int data=section->data[(offset+yoffset)/2]; - if (x&1) data>>=4; - BlockInfo &block=blocks->getBlock(section->blocks[offset+yoffset],data&0xf); - if (block.alpha==0.0) continue; - break; - } - } - return y; + int cx=floor(x/16.0); + int cz=floor(z/16.0); + Chunk *chunk=cache.fetch(cx,cz); + int offset=(x&0xf)+(z&0xf)*16; + int y = 0; + if (chunk) + { + int top=depth; + if (top>chunk->highest) + top=chunk->highest; + + for (y=top;y>=0;y--) + { + int sec=y>>4; + ChunkSection *section=chunk->sections[sec]; + if (!section) + { + y=(sec<<4)-1; //skip entire section + continue; + } + int yoffset=(y&0xf)<<8; + int data=section->data[(offset+yoffset)/2]; + if (x&1) data>>=4; + BlockInfo &block=blocks->getBlock(section->blocks[offset+yoffset],data&0xf); + if (block.alpha==0.0) continue; + break; + } + } + return y; } diff --git a/mapview.h b/mapview.h index ed7fc02d..4e7f6dea 100644 --- a/mapview.h +++ b/mapview.h @@ -46,8 +46,8 @@ class MapView : public QWidget flgLighting = 1, flgMobSpawn = 2, flgCaveMode = 4, - flgDepthShading = 8, - flgShowEntities = 16 + flgDepthShading = 8, + flgShowEntities = 16 }; @@ -61,8 +61,8 @@ class MapView : public QWidget void setLocation(double x,double z); void setDimension(QString path,int scale); void setFlags(int flags); - void addSpecialBlockType(QString type); - void clearSpecialBlockTypes(); + void addSpecialBlockType(QString type); + void clearSpecialBlockTypes(); // public for saving the png void renderChunk(Chunk *chunk); @@ -73,7 +73,7 @@ public slots: void setDepth(int depth); void chunkUpdated(int x,int z); void redraw(); - void markBlock(int x, int y, int z, QString type, QString message); + void markBlock(int x, int y, int z, QString type, QString message); /// Clears the cache and redraws, causing all chunks to be re-loaded; but keeps the viewport void clearCache(); @@ -81,14 +81,14 @@ public slots: signals: void hoverTextChanged(QString text); void demandDepthChange(int value); - void foundSpecialBlock(int x, int y, int z, QString type, QString display, QVariant properties); - void showProperties(int x, int y, int z); + void foundSpecialBlock(int x, int y, int z, QString type, QString display, QVariant properties); + void showProperties(int x, int y, int z); protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *); - void mouseDoubleClickEvent(QMouseEvent*event); + void mouseDoubleClickEvent(QMouseEvent*event); void wheelEvent(QWheelEvent *event); void keyPressEvent(QKeyEvent *event); void resizeEvent(QResizeEvent *); @@ -98,7 +98,7 @@ public slots: void drawChunk(int x,int z); void getToolTip(int x,int z); - int getY(int x, int z); + int getY(int x, int z); int depth; double x,z; @@ -111,7 +111,7 @@ public slots: BlockIdentifier *blocks; BiomeIdentifier *biomes; uchar placeholder[16*16*4]; // no chunk found placeholder - QSet specialBlockTypes; + QSet specialBlockTypes; }; #endif diff --git a/minutor.cpp b/minutor.cpp index da5c4b68..7cdeff28 100644 --- a/minutor.cpp +++ b/minutor.cpp @@ -56,7 +56,7 @@ Minutor::Minutor() connect(mapview, SIGNAL(foundSpecialBlock(int,int,int,QString,QString,QVariant)), this, SLOT(specialBlock(int,int,int,QString,QString,QVariant))); connect(mapview, SIGNAL(showProperties(int,int,int)), - this, SLOT(showProperties(int,int,int))); + this, SLOT(showProperties(int,int,int))); dm=new DefinitionManager(this); mapview->attach(dm); connect(dm, SIGNAL(packsChanged()), @@ -214,7 +214,6 @@ void Minutor::toggleFlags() if (mobSpawnAct->isChecked()) flags |= MapView::flgMobSpawn; if (caveModeAct->isChecked()) flags |= MapView::flgCaveMode; if (depthShadingAct->isChecked()) flags |= MapView::flgDepthShading; - //if (entitiesAct->isChecked()) flags |= MapView::flgShowEntities; mapview->setFlags(flags); mapview->clearSpecialBlockTypes(); @@ -491,7 +490,7 @@ void Minutor::loadWorld(QDir path) p->setData(locations.count()); locations.append(Location(posX, posZ)); connect(p, SIGNAL(triggered()), - this, SLOT(jumpToLocation())); + this, SLOT(jumpToLocation())); players.append(p); if (player.has("SpawnX")) //player has a bed { @@ -551,8 +550,6 @@ void Minutor::specialBlock(int x, int y, int z, QString type, QString display, Q void Minutor::showProperties(int x, int y, int z) { - //qDebug("and here is where we would show the properties"); - QMap values; EntityMap::iterator it, itEnd = entities.end(); for (it = entities.begin(); it != itEnd; ++it) diff --git a/minutor.h b/minutor.h index f7517531..138cb633 100644 --- a/minutor.h +++ b/minutor.h @@ -83,8 +83,8 @@ private slots: void rescanWorlds(); void saveProgress(QString status,double value); void saveFinished(); - void specialBlock(int x, int y, int z, QString type, QString display, QVariant properties); - void showProperties(int x, int y, int z); + void specialBlock(int x, int y, int z, QString type, QString display, QVariant properties); + void showProperties(int x, int y, int z); signals: void worldLoaded(bool isLoaded); @@ -104,7 +104,7 @@ private slots: QMenu *fileMenu, *worldMenu; QMenu *viewMenu, *jumpMenu, *dimMenu; QMenu *helpMenu; - QMenu *entitiesMenu; + QMenu *entitiesMenu; QListworlds; QAction *openAct, *reloadAct, *saveAct, *exitAct; @@ -116,7 +116,7 @@ private slots: QAction *aboutAct; QAction *settingsAct; QAction *updatesAct; - QList entityActions; + QList entityActions; //loaded world data QList locations; @@ -125,18 +125,18 @@ private slots: Dimensions *dimensions; QDir currentWorld; - //special entities and objects with properties - struct Entity - { - int x, y, z; - QString type; - QString display; - QVariant properties; - }; - // type x z - typedef QMap, Entity> > EntityMap; - EntityMap entities; - Properties * propView; + //special entities and objects with properties + struct Entity + { + int x, y, z; + QString type; + QString display; + QVariant properties; + }; + // type x z + typedef QMap, Entity> > EntityMap; + EntityMap entities; + Properties * propView; }; #endif diff --git a/nbt.cpp b/nbt.cpp index 78202cee..cf71ab38 100644 --- a/nbt.cpp +++ b/nbt.cpp @@ -182,8 +182,8 @@ const qint32 *Tag::toIntArray() } QVariant Tag::getData() { - qWarning()<<"Unhandled getData"; - return QVariant(); + qWarning()<<"Unhandled getData"; + return QVariant(); } Tag_Byte::Tag_Byte(TagDataStream &s) @@ -197,12 +197,12 @@ int Tag_Byte::toInt() QString Tag_Byte::toString() { - return QString::number(data); + return QString::number(data); } QVariant Tag_Byte::getData() { - return data; + return data; } Tag_Short::Tag_Short(TagDataStream &s) @@ -212,18 +212,18 @@ Tag_Short::Tag_Short(TagDataStream &s) int Tag_Short::toInt() { - return data; + return data; } QString Tag_Short::toString() { - return QString::number(data); + return QString::number(data); } QVariant Tag_Short::getData() { - return data; + return data; } Tag_Int::Tag_Int(TagDataStream &s) @@ -237,12 +237,12 @@ qint32 Tag_Int::toInt() QString Tag_Int::toString() { - return QString::number(data); + return QString::number(data); } QVariant Tag_Int::getData() { - return data; + return data; } double Tag_Int::toDouble() @@ -258,22 +258,22 @@ Tag_Long::Tag_Long(TagDataStream &s) double Tag_Long::toDouble() { - return (double)data; + return (double)data; } qint32 Tag_Long::toInt() { - return (double)data; + return (double)data; } QString Tag_Long::toString() { - return QString::number(data); + return QString::number(data); } QVariant Tag_Long::getData() { - return data; + return data; } Tag_Float::Tag_Float(TagDataStream &s) @@ -284,17 +284,17 @@ Tag_Float::Tag_Float(TagDataStream &s) } double Tag_Float::toDouble() { - return data; + return data; } QString Tag_Float::toString() { - return QString::number(data); + return QString::number(data); } QVariant Tag_Float::getData() { - return data; + return data; } Tag_Double::Tag_Double(TagDataStream &s) @@ -310,12 +310,12 @@ double Tag_Double::toDouble() QVariant Tag_Double::getData() { - return data; + return data; } QString Tag_Double::toString() { - return QString::number(data); + return QString::number(data); } Tag_Byte_Array::Tag_Byte_Array(TagDataStream &s) @@ -338,21 +338,21 @@ const quint8 *Tag_Byte_Array::toByteArray() QVariant Tag_Byte_Array::getData() { - return QByteArray(reinterpret_cast(data), len); + return QByteArray(reinterpret_cast(data), len); } QString Tag_Byte_Array::toString() { - try - { - return QString::fromLatin1((char*)data); - } - catch(...) - { + try + { + return QString::fromLatin1((char*)data); + } + catch(...) + { - } + } - return ""; + return ""; } Tag_String::Tag_String(TagDataStream &s) @@ -367,7 +367,7 @@ QString Tag_String::toString() QVariant Tag_String::getData() { - return data; + return data; } template @@ -381,8 +381,8 @@ Tag_List::Tag_List(TagDataStream &s) { quint8 type=s.r8(); int len=s.r32(); - if (len==0) //empty list, type is invalid - return; + if (len==0) //empty list, type is invalid + return; switch (type) { @@ -419,27 +419,27 @@ Tag *Tag_List::at(int index) QString Tag_List::toString() { - QStringList ret; - ret << "["; - QList::iterator it, end = data.end(); - for (it = data.begin(); it != end; ++it) - { - ret << (*it)->toString(); - ret << ", "; - } - ret.last() = "]"; - return ret.join(""); + QStringList ret; + ret << "["; + QList::iterator it, end = data.end(); + for (it = data.begin(); it != end; ++it) + { + ret << (*it)->toString(); + ret << ", "; + } + ret.last() = "]"; + return ret.join(""); } QVariant Tag_List::getData() { - QList list; - QList::iterator it, end = data.end(); - for (it = data.begin(); it != end; ++it) - { - list << (*it)->getData(); - } - return list; + QList list; + QList::iterator it, end = data.end(); + for (it = data.begin(); it != end; ++it) + { + list << (*it)->getData(); + } + return list; } Tag_Compound::Tag_Compound(TagDataStream &s) @@ -487,26 +487,26 @@ Tag *Tag_Compound::at(const QString key) QString Tag_Compound::toString() { - QStringList ret; - ret << "{\n"; - QHash::iterator it, end = children.end(); - for (it = children.begin(); it != end; ++it) - { - ret << "\t" << it.key() << " = '" << it.value()->toString() << "',\n"; - } - ret.last() = "}"; - return ret.join(""); + QStringList ret; + ret << "{\n"; + QHash::iterator it, end = children.end(); + for (it = children.begin(); it != end; ++it) + { + ret << "\t" << it.key() << " = '" << it.value()->toString() << "',\n"; + } + ret.last() = "}"; + return ret.join(""); } QVariant Tag_Compound::getData() { - QMap map; - QHash::iterator it, end = children.end(); - for (it = children.begin(); it != end; ++it) - { - map.insert(it.key(), it.value()->getData()); - } - return map; + QMap map; + QHash::iterator it, end = children.end(); + for (it = children.begin(); it != end; ++it) + { + map.insert(it.key(), it.value()->getData()); + } + return map; } Tag_Int_Array::Tag_Int_Array(TagDataStream &s) @@ -531,19 +531,19 @@ int Tag_Int_Array::length() QString Tag_Int_Array::toString() { - QStringList ret; - ret << "["; - for (int i = 0; i < len; ++i) - { - ret << QString::number(data[i]) << ","; - } - ret.last() = "]"; - return ret.join(""); + QStringList ret; + ret << "["; + for (int i = 0; i < len; ++i) + { + ret << QString::number(data[i]) << ","; + } + ret.last() = "]"; + return ret.join(""); } QVariant Tag_Int_Array::getData() { - return toString(); + return toString(); } TagDataStream::TagDataStream(const char *data,int len) diff --git a/nbt.h b/nbt.h index ee8274c7..be869220 100644 --- a/nbt.h +++ b/nbt.h @@ -65,7 +65,7 @@ class Tag virtual double toDouble(); virtual const quint8 *toByteArray(); virtual const qint32 *toIntArray(); - virtual QVariant getData(); + virtual QVariant getData(); }; class NBT @@ -88,8 +88,8 @@ class Tag_Byte : public Tag public: Tag_Byte(TagDataStream &s); int toInt(); - virtual QString toString(); - virtual QVariant getData(); + virtual QString toString(); + virtual QVariant getData(); private: quint8 data; }; @@ -98,9 +98,9 @@ class Tag_Short : public Tag { public: Tag_Short(TagDataStream &s); - int toInt(); - virtual QString toString(); - virtual QVariant getData(); + int toInt(); + virtual QString toString(); + virtual QVariant getData(); private: qint16 data; }; @@ -111,8 +111,8 @@ class Tag_Int : public Tag Tag_Int(TagDataStream &s); qint32 toInt(); double toDouble(); - virtual QString toString(); - virtual QVariant getData(); + virtual QString toString(); + virtual QVariant getData(); private: qint32 data; }; @@ -121,10 +121,10 @@ class Tag_Long : public Tag { public: Tag_Long(TagDataStream &s); - qint32 toInt(); - double toDouble(); - virtual QString toString(); - virtual QVariant getData(); + qint32 toInt(); + double toDouble(); + virtual QString toString(); + virtual QVariant getData(); private: qint64 data; }; @@ -132,11 +132,11 @@ class Tag_Long : public Tag class Tag_Float : public Tag { public: - Tag_Float(TagDataStream &s); + Tag_Float(TagDataStream &s); - virtual double toDouble(); - virtual QString toString(); - virtual QVariant getData(); + virtual double toDouble(); + virtual QString toString(); + virtual QVariant getData(); private: float data; @@ -147,8 +147,8 @@ class Tag_Double : public Tag public: Tag_Double(TagDataStream &s); double toDouble(); - virtual QString toString(); - virtual QVariant getData(); + virtual QString toString(); + virtual QVariant getData(); private: double data; }; @@ -160,8 +160,8 @@ class Tag_Byte_Array : public Tag ~Tag_Byte_Array(); int length(); const quint8 *toByteArray(); - virtual QString toString(); - virtual QVariant getData(); + virtual QString toString(); + virtual QVariant getData(); private: const quint8 *data; int len; @@ -172,7 +172,7 @@ class Tag_String : public Tag public: Tag_String(TagDataStream &s); QString toString(); - virtual QVariant getData(); + virtual QVariant getData(); private: QString data; }; @@ -184,8 +184,8 @@ class Tag_List : public Tag ~Tag_List(); Tag *at(int index); int length(); - virtual QString toString(); - virtual QVariant getData(); + virtual QString toString(); + virtual QVariant getData(); private: QList data; }; @@ -197,8 +197,8 @@ class Tag_Compound : public Tag ~Tag_Compound(); bool has(const QString key); Tag *at(const QString key); - virtual QString toString(); - virtual QVariant getData(); + virtual QString toString(); + virtual QVariant getData(); private: QHash children; }; @@ -210,8 +210,8 @@ class Tag_Int_Array : public Tag ~Tag_Int_Array(); int length(); const qint32 *toIntArray(); - virtual QString toString(); - virtual QVariant getData(); + virtual QString toString(); + virtual QVariant getData(); private: int len; qint32 *data; diff --git a/properties.cpp b/properties.cpp index d005f1de..1ea91ebf 100644 --- a/properties.cpp +++ b/properties.cpp @@ -4,207 +4,207 @@ Properties::Properties(QWidget *parent) : - QDialog(parent), - ui(new Ui::Properties) + QDialog(parent), + ui(new Ui::Properties) { - ui->setupUi(this); + ui->setupUi(this); - //TODO: populate this from a config file? - summary.insert("", "{id} ({Pos.[0]}, {Pos.[1]}, {Pos.[2]})"); - summary.insert("Pos", "({[0]}, {[1]}, {[2]})"); - summary.insert("Attributes[]", "{Name} = {Base}"); + //TODO: populate this from a config file? + summary.insert("", "{id} ({Pos.[0]}, {Pos.[1]}, {Pos.[2]})"); + summary.insert("Pos", "({[0]}, {[1]}, {[2]})"); + summary.insert("Attributes[]", "{Name} = {Base}"); } Properties::~Properties() { - delete ui; + delete ui; } template void Properties::ParseIterable(QTreeWidgetItem* node, const IterableT& seq) { - typename IterableT::const_iterator it, itEnd = seq.end(); - for(it = seq.begin(); it != itEnd; ++it) - { - QTreeWidgetItem* child = new QTreeWidgetItem(); - child->setData(0, Qt::DisplayRole, it.key()); - child->setData(1, Qt::DisplayRole, GetSummary(it.key(), it.value())); - CreateTree(child, it.value()); - if (node) - node->addChild(child); - else - ui->propertyView->addTopLevelItem(child); - } + typename IterableT::const_iterator it, itEnd = seq.end(); + for(it = seq.begin(); it != itEnd; ++it) + { + QTreeWidgetItem* child = new QTreeWidgetItem(); + child->setData(0, Qt::DisplayRole, it.key()); + child->setData(1, Qt::DisplayRole, GetSummary(it.key(), it.value())); + CreateTree(child, it.value()); + if (node) + node->addChild(child); + else + ui->propertyView->addTopLevelItem(child); + } } template void Properties::ParseList(QTreeWidgetItem* node, const IterableT& seq) { - typename IterableT::const_iterator it, itEnd = seq.end(); - int i = 0; - //skip 1 sized arrays - if (seq.size() <= 1) - { - CreateTree(node, seq.first()); - node->setData(1, Qt::DisplayRole, GetSummary("[0]", seq.first())); - } - else - { - for(it = seq.begin(); it != itEnd; ++it) - { - QTreeWidgetItem* child = new QTreeWidgetItem(); - QString key = QString("[%1]").arg(i++); - child->setData(0, Qt::DisplayRole, key); - child->setData(1, Qt::DisplayRole, GetSummary(key, *it)); - CreateTree(child, *it); - - if (node) - node->addChild(child); - else - ui->propertyView->addTopLevelItem(child); - } - } + typename IterableT::const_iterator it, itEnd = seq.end(); + int i = 0; + //skip 1 sized arrays + if (seq.size() <= 1) + { + CreateTree(node, seq.first()); + node->setData(1, Qt::DisplayRole, GetSummary("[0]", seq.first())); + } + else + { + for(it = seq.begin(); it != itEnd; ++it) + { + QTreeWidgetItem* child = new QTreeWidgetItem(); + QString key = QString("[%1]").arg(i++); + child->setData(0, Qt::DisplayRole, key); + child->setData(1, Qt::DisplayRole, GetSummary(key, *it)); + CreateTree(child, *it); + + if (node) + node->addChild(child); + else + ui->propertyView->addTopLevelItem(child); + } + } } void Properties::DisplayProperties(QVariant p) { - //get current property - QString propertyName; - QTreeWidgetItem* item = ui->propertyView->currentItem(); - if (item) - { - propertyName = item->data(0, Qt::DisplayRole).toString(); - } - - ui->propertyView->clear(); - - //only support QVariantMap or QVariantHash at this level - switch (p.type()) - { - case QMetaType::QVariantMap: - ParseIterable(NULL, p.toMap()); - break; - case QMetaType::QVariantHash: - ParseIterable(NULL, p.toHash()); - break; - case QMetaType::QVariantList: - ParseList(NULL, p.toList()); - break; - default: - qWarning("Trying to display scalar value as a property"); - break; - } - - //expand at least the first level - ui->propertyView->expandToDepth(0); - - if (propertyName.size() != 0) - { - //try to restore the path - QList items = ui->propertyView->findItems(propertyName, Qt::MatchRecursive); - if (items.size()) - items.front()->setSelected(true); - } + //get current property + QString propertyName; + QTreeWidgetItem* item = ui->propertyView->currentItem(); + if (item) + { + propertyName = item->data(0, Qt::DisplayRole).toString(); + } + + ui->propertyView->clear(); + + //only support QVariantMap or QVariantHash at this level + switch (p.type()) + { + case QMetaType::QVariantMap: + ParseIterable(NULL, p.toMap()); + break; + case QMetaType::QVariantHash: + ParseIterable(NULL, p.toHash()); + break; + case QMetaType::QVariantList: + ParseList(NULL, p.toList()); + break; + default: + qWarning("Trying to display scalar value as a property"); + break; + } + + //expand at least the first level + ui->propertyView->expandToDepth(0); + + if (propertyName.size() != 0) + { + //try to restore the path + QList items = ui->propertyView->findItems(propertyName, Qt::MatchRecursive); + if (items.size()) + items.front()->setSelected(true); + } } void Properties::CreateTree(QTreeWidgetItem* node, const QVariant& v) -{ - switch (v.type()) - { - case QMetaType::QVariantMap: - ParseIterable(node, v.toMap()); - break; - case QMetaType::QVariantHash: - ParseIterable(node, v.toHash()); - break; - case QMetaType::QVariantList: - ParseList(node, v.toList()); - break; - default: - node->setData(1, Qt::DisplayRole, v.toString()); - break; - } +{ + switch (v.type()) + { + case QMetaType::QVariantMap: + ParseIterable(node, v.toMap()); + break; + case QMetaType::QVariantHash: + ParseIterable(node, v.toHash()); + break; + case QMetaType::QVariantList: + ParseList(node, v.toList()); + break; + default: + node->setData(1, Qt::DisplayRole, v.toString()); + break; + } } QString EvaluateSubExpression(const QString& subexpr, const QVariant& v) { - if (subexpr.size() == 0) - { - //limit the displayed decimal places - if ((QMetaType::Type)v.type() == QMetaType::Double) - { - return QString::number(v.toDouble(), 'f', 2); - } - return v.toString(); - } - else if (subexpr.at(0) == '[') - { - int rightbracket = subexpr.indexOf(']'); - if (rightbracket > 0) - { - bool ok = false; - int index = subexpr.mid(1, rightbracket-1).toInt(&ok); - if (ok && (QMetaType::Type)v.type() == QMetaType::QVariantList) - { - return EvaluateSubExpression(subexpr.mid(rightbracket + 1), v.toList().at(index)); - } - } - } - else - { - int dot = subexpr.indexOf('.'); - QString key = subexpr.mid(0, dot); - if ((QMetaType::Type)v.type() == QMetaType::QVariantHash) - { - QHash h = v.toHash(); - QHash::const_iterator it = h.find(key); - if (it != h.end()) - return EvaluateSubExpression(subexpr.mid(key.length() + 1), *it); - } - else if ((QMetaType::Type)v.type() == QMetaType::QVariantMap) - { - QMap h = v.toMap(); - QMap::const_iterator it = h.find(key); - if (it != h.end()) - return EvaluateSubExpression(subexpr.mid(key.length() + 1), *it); - } - } - - return ""; + if (subexpr.size() == 0) + { + //limit the displayed decimal places + if ((QMetaType::Type)v.type() == QMetaType::Double) + { + return QString::number(v.toDouble(), 'f', 2); + } + return v.toString(); + } + else if (subexpr.at(0) == '[') + { + int rightbracket = subexpr.indexOf(']'); + if (rightbracket > 0) + { + bool ok = false; + int index = subexpr.mid(1, rightbracket-1).toInt(&ok); + if (ok && (QMetaType::Type)v.type() == QMetaType::QVariantList) + { + return EvaluateSubExpression(subexpr.mid(rightbracket + 1), v.toList().at(index)); + } + } + } + else + { + int dot = subexpr.indexOf('.'); + QString key = subexpr.mid(0, dot); + if ((QMetaType::Type)v.type() == QMetaType::QVariantHash) + { + QHash h = v.toHash(); + QHash::const_iterator it = h.find(key); + if (it != h.end()) + return EvaluateSubExpression(subexpr.mid(key.length() + 1), *it); + } + else if ((QMetaType::Type)v.type() == QMetaType::QVariantMap) + { + QMap h = v.toMap(); + QMap::const_iterator it = h.find(key); + if (it != h.end()) + return EvaluateSubExpression(subexpr.mid(key.length() + 1), *it); + } + } + + return ""; } QString Properties::GetSummary(const QString& key, const QVariant& v) { - QString ret; - QMap::const_iterator it = summary.find(key); - if (it != summary.end()) - { - ret = *it; - QRegularExpression re("(\\{.*?\\})"); - QRegularExpressionMatchIterator matches = re.globalMatch(*it); - while (matches.hasNext()) - { - QRegularExpressionMatch m = matches.next(); - QString pattern = m.captured(0); - QString evaluated = EvaluateSubExpression(pattern.mid(1, pattern.size()-2), v); - //if a lookup fails, then don't return anything - if (evaluated.size() == 0) - { - ret = ""; - break; - } - ret.replace(pattern, evaluated); - } - } - else if ((QMetaType::Type)v.type() == QMetaType::QVariantList) - { - ret = QString("(%1 items)").arg(v.toList().size()); - } - else if ((QMetaType::Type)v.type() == QMetaType::QVariantMap) - { - ret = GetSummary("", v); - } - return ret; + QString ret; + QMap::const_iterator it = summary.find(key); + if (it != summary.end()) + { + ret = *it; + QRegularExpression re("(\\{.*?\\})"); + QRegularExpressionMatchIterator matches = re.globalMatch(*it); + while (matches.hasNext()) + { + QRegularExpressionMatch m = matches.next(); + QString pattern = m.captured(0); + QString evaluated = EvaluateSubExpression(pattern.mid(1, pattern.size()-2), v); + //if a lookup fails, then don't return anything + if (evaluated.size() == 0) + { + ret = ""; + break; + } + ret.replace(pattern, evaluated); + } + } + else if ((QMetaType::Type)v.type() == QMetaType::QVariantList) + { + ret = QString("(%1 items)").arg(v.toList().size()); + } + else if ((QMetaType::Type)v.type() == QMetaType::QVariantMap) + { + ret = GetSummary("", v); + } + return ret; } diff --git a/properties.h b/properties.h index 39835efb..83d3036b 100644 --- a/properties.h +++ b/properties.h @@ -12,26 +12,26 @@ class QTreeWidgetItem; class Properties : public QDialog { - Q_OBJECT + Q_OBJECT public: - explicit Properties(QWidget *parent = 0); - ~Properties(); + explicit Properties(QWidget *parent = 0); + ~Properties(); - void DisplayProperties(QVariant p); - + void DisplayProperties(QVariant p); + protected: - void CreateTree(QTreeWidgetItem *node, const QVariant& v); - QString GetSummary(const QString& key, const QVariant& v); + void CreateTree(QTreeWidgetItem *node, const QVariant& v); + QString GetSummary(const QString& key, const QVariant& v); - template - void ParseIterable(QTreeWidgetItem* node, const IterableT& seq); - template - void ParseList(QTreeWidgetItem* node, const IterableT& seq); + template + void ParseIterable(QTreeWidgetItem* node, const IterableT& seq); + template + void ParseList(QTreeWidgetItem* node, const IterableT& seq); private: - Ui::Properties *ui; - QMap summary; + Ui::Properties *ui; + QMap summary; }; #endif // PROPERTIES_H