Implemented saving of the currently stored tilebank data.

This commit is contained in:
dfighter1985 2014-07-30 01:29:17 +02:00
parent fed92a47d3
commit a513af355d
7 changed files with 234 additions and 11 deletions

View file

@ -0,0 +1,14 @@
#ifndef LAND_H
#define LAND_H
#include <QStringList>
struct Land
{
QString name;
QStringList tilesets;
};
#endif

View file

@ -210,7 +210,7 @@ void TileEditorMainWindow::saveAs()
} }
TileBankSaver saver; TileBankSaver saver;
bool ok = saver.save( m_fileName.toUtf8().constData(), m_tileModel, landNames ); bool ok = saver.save( m_fileName.toUtf8().constData(), m_tileModel, m_lands );
if( !ok ) if( !ok )
{ {

View file

@ -25,6 +25,8 @@
#include <QtGui/QUndoStack> #include <QtGui/QUndoStack>
#include <QSignalMapper> #include <QSignalMapper>
#include "land.h"
namespace Ui { namespace Ui {
class TileEditorMainWindow; class TileEditorMainWindow;
} }
@ -108,12 +110,6 @@ private:
TAB_DETAILS = 4 TAB_DETAILS = 4
}; };
struct Land
{
QString name;
QStringList tilesets;
};
QString m_fileName; QString m_fileName;
QList< Land > m_lands; QList< Land > m_lands;
}; };

View file

@ -272,6 +272,16 @@ void TileItemNode::setTileFilename(TileModel::TTileChannel channel, QString file
m_tileFilename[channel] = filename; m_tileFilename[channel] = filename;
} }
QString TileItemNode::getTileFilename(TileModel::TTileChannel channel)
{
QMap< TileModel::TTileChannel, QString >::const_iterator itr
= m_tileFilename.find( channel );
if( itr == m_tileFilename.end() )
return "";
return itr.value();
}
QVariant TileItemNode::data(int column, int role) const QVariant TileItemNode::data(int column, int role) const
{ {
// find some way to know which file/bitmap to display // find some way to know which file/bitmap to display

View file

@ -106,6 +106,7 @@ public:
QVariant data(int column, int role) const; QVariant data(int column, int role) const;
int columnCount() const; int columnCount() const;
void setTileFilename(TileModel::TTileChannel channel, QString filename); void setTileFilename(TileModel::TTileChannel channel, QString filename);
QString getTileFilename(TileModel::TTileChannel channel);
void setId( int id ){ m_tileId = id; } void setId( int id ){ m_tileId = id; }
int id() const{ return m_tileId; } int id() const{ return m_tileId; }
private: private:

View file

@ -17,21 +17,219 @@
#include "tilebank_saver.h" #include "tilebank_saver.h"
#include "tile_model.h" #include "tile_model.h"
#include "tile_item.h"
#include "nel/3d/tile_bank.h" #include "nel/3d/tile_bank.h"
#include "nel/misc/file.h"
class TileBankSaverPvt
{
public:
NL3D::CTileBank bank;
static NL3D::CTile::TBitmap channelToTBitmap( TileModel::TTileChannel channel )
{
NL3D::CTile::TBitmap b = NL3D::CTile::bitmapCount;
switch( channel )
{
case TileModel::TileDiffuse: b = NL3D::CTile::diffuse; break;
case TileModel::TileAdditive: b = NL3D::CTile::additive; break;
case TileModel::TileAlpha: b = NL3D::CTile::alpha; break;
}
return b;
}
NL3D::CTile* addTileToSet( NL3D::CTileSet *set, TileModel::TNodeTileType type )
{
int idx = -1;
int bidx = -1;
switch( type )
{
case TileModel::Tile128:
{
set->addTile128( idx, bank );
bidx = set->getTile128( idx );
break;
}
case TileModel::Tile256:
{
set->addTile256( idx, bank );
bidx = set->getTile256( idx );
break;
}
}
if( idx == -1 )
return NULL;
return bank.getTile( bidx );
}
void addTilesToSet( NL3D::CTileSet *set, TileTypeNode *node )
{
TileModel::TNodeTileType type = node->getTileType();
for( int i = 0; i < node->childCount(); i++ )
{
TileItemNode *tin = static_cast< TileItemNode* >( node->child( i ) );
for( int j = TileModel::TileDiffuse; j <= TileModel::TileAlpha; j++ )
{
QString fn = tin->getTileFilename( TileModel::TTileChannel( j ) );
NL3D::CTile *tile = addTileToSet( set, type );
tile->setFileName( channelToTBitmap( TileModel::TTileChannel( j ) ) , fn.toUtf8().constData() );
}
}
}
void addTilesToSet( NL3D::CTileSet *set, TileSetNode *node )
{
for( int i = TileModel::Tile128; i <= TileModel::Tile256; i++ )
{
TileTypeNode *tn = static_cast< TileTypeNode* >( node->child( i ) );
addTilesToSet( set, tn );
}
}
void setupTransitionTile( NL3D::CTileSet *set, TileItemNode *node, int idx )
{
NL3D::CTileSetTransition *tr = set->getTransition( idx );
int tid = tr->getTile();
NL3D::CTile *tile = bank.getTile( tid );
if( tile == NULL )
return;
for( int i = TileModel::TileDiffuse; i <= TileModel::TileAlpha; i++ )
{
QString fn = node->getTileFilename( TileModel::TTileChannel( i ) );
tile->setFileName( channelToTBitmap( TileModel::TTileChannel( i ) ), fn.toUtf8().constData() );
}
}
void setupTransitionTiles( NL3D::CTileSet *set, TileTypeNode *node )
{
for( int i = 0; i < node->childCount(); i++ )
{
TileItemNode *tin = static_cast< TileItemNode* >( node->child( i ) );
setupTransitionTile( set, tin, i );
}
}
void setupTransitionTiles( NL3D::CTileSet *set, TileSetNode *node )
{
TileTypeNode *tn = static_cast< TileTypeNode* >( node->child( 2 ) );
setupTransitionTiles( set, tn );
}
void setupDisplacementTile( NL3D::CTileSet *set, TileItemNode *node, int idx )
{
set->setDisplacement( NL3D::CTileSet::TDisplacement( idx ),
node->getTileFilename( TileModel::TileDiffuse ).toUtf8().constData(),
bank );
}
void setupDisplacementTiles( NL3D::CTileSet *set, TileTypeNode *node )
{
for( int i = 0; i < node->childCount(); i++ )
{
TileItemNode *tin = static_cast< TileItemNode* >( node->child( i ) );
setupDisplacementTile( set, tin, i );
}
}
void setupDisplacementTiles( NL3D::CTileSet *set, TileSetNode *node )
{
TileTypeNode *tn = static_cast< TileTypeNode* >( node->child( 3 ) );
setupDisplacementTiles( set, tn );
}
void addLands( const QList< Land > &lands )
{
QListIterator< Land > itr( lands );
while( itr.hasNext() )
{
bank.addLand( itr.next().name.toUtf8().constData() );
}
}
void addTileSets( const TileModel* model, const QList< Land > &lands )
{
// Add the tilesets
for( int i = 0; i < model->rowCount(); i++ )
{
QModelIndex idx = model->index( i, 0 );
if( !idx.isValid() )
continue;
TileSetNode *n = reinterpret_cast< TileSetNode* >( idx.internalPointer() );
QString set = n->getTileSetName();
bank.addTileSet( set.toUtf8().constData() );
}
// Set the data to tilesets
for( int i = 0; i < bank.getTileSetCount(); i++ )
{
NL3D::CTileSet *set = bank.getTileSet( i );
QModelIndex idx = model->index( i, 0 );
if( !idx.isValid() )
continue;
TileSetNode *n = reinterpret_cast< TileSetNode* >( idx.internalPointer() );
addTilesToSet( set, n );
setupTransitionTiles( set, n );
setupDisplacementTiles( set, n );
}
// Add tilesets to lands
for( int i = 0; i < bank.getLandCount(); i++ )
{
NL3D::CTileLand *land = bank.getLand( i );
const Land &l = lands[ i ];
for( int j = 0; j < l.tilesets.count(); j++ )
{
land->addTileSet( l.tilesets[ j ].toUtf8().constData() );
}
}
}
};
TileBankSaver::TileBankSaver() TileBankSaver::TileBankSaver()
{ {
p = new TileBankSaverPvt();
} }
TileBankSaver::~TileBankSaver() TileBankSaver::~TileBankSaver()
{ {
delete p;
p = NULL;
} }
bool TileBankSaver::save( const char *fileName, const TileModel* model, const QList< QString > &lands ) bool TileBankSaver::save( const char *fileName, const TileModel* model, const QList< Land > &lands )
{ {
NL3D::CTileBank bank; p->addLands( lands );
p->addTileSets( model, lands );
return false; // Save to file
NLMISC::COFile f;
bool b = f.open( fileName, false, false, false );
if( !b )
return false;
p->bank.serial( f );
f.flush();
f.close();
return true;
} }

View file

@ -20,18 +20,22 @@
#include <QString> #include <QString>
#include <QList> #include <QList>
#include "land.h"
class TileModel; class TileModel;
class TileBankSaverPvt;
class TileBankSaver class TileBankSaver
{ {
public: public:
TileBankSaver(); TileBankSaver();
~TileBankSaver(); ~TileBankSaver();
bool save( const char *filename, const TileModel* model, const QList< QString > &lands ); bool save( const char *filename, const TileModel* model, const QList< Land > &lands );
private: private:
TileBankSaverPvt *p;
}; };
#endif #endif