From 95b67026bfa3b0e9967bf4200b52af96872bf322 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Wed, 30 Jul 2014 01:29:17 +0200 Subject: [PATCH] Implemented saving of the currently stored tilebank data. --HG-- branch : gsoc2014-dfighter --- code/studio/src/plugins/tile_editor/land.h | 14 ++ .../tile_editor/tile_editor_main_window.cpp | 2 +- .../tile_editor/tile_editor_main_window.h | 8 +- .../src/plugins/tile_editor/tile_item.cpp | 10 + .../src/plugins/tile_editor/tile_item.h | 1 + .../plugins/tile_editor/tilebank_saver.cpp | 204 +++++++++++++++++- .../src/plugins/tile_editor/tilebank_saver.h | 6 +- 7 files changed, 234 insertions(+), 11 deletions(-) create mode 100644 code/studio/src/plugins/tile_editor/land.h diff --git a/code/studio/src/plugins/tile_editor/land.h b/code/studio/src/plugins/tile_editor/land.h new file mode 100644 index 000000000..37b0d83c6 --- /dev/null +++ b/code/studio/src/plugins/tile_editor/land.h @@ -0,0 +1,14 @@ +#ifndef LAND_H +#define LAND_H + +#include + +struct Land +{ + QString name; + QStringList tilesets; +}; + + +#endif + diff --git a/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp b/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp index 0a30da403..91d81ea50 100644 --- a/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp +++ b/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp @@ -210,7 +210,7 @@ void TileEditorMainWindow::saveAs() } 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 ) { diff --git a/code/studio/src/plugins/tile_editor/tile_editor_main_window.h b/code/studio/src/plugins/tile_editor/tile_editor_main_window.h index 5f015ad6a..a491fc5f2 100644 --- a/code/studio/src/plugins/tile_editor/tile_editor_main_window.h +++ b/code/studio/src/plugins/tile_editor/tile_editor_main_window.h @@ -25,6 +25,8 @@ #include #include +#include "land.h" + namespace Ui { class TileEditorMainWindow; } @@ -108,12 +110,6 @@ private: TAB_DETAILS = 4 }; - struct Land - { - QString name; - QStringList tilesets; - }; - QString m_fileName; QList< Land > m_lands; }; diff --git a/code/studio/src/plugins/tile_editor/tile_item.cpp b/code/studio/src/plugins/tile_editor/tile_item.cpp index e228a6745..b6bb30ea2 100644 --- a/code/studio/src/plugins/tile_editor/tile_item.cpp +++ b/code/studio/src/plugins/tile_editor/tile_item.cpp @@ -272,6 +272,16 @@ void TileItemNode::setTileFilename(TileModel::TTileChannel channel, QString file 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 { // find some way to know which file/bitmap to display diff --git a/code/studio/src/plugins/tile_editor/tile_item.h b/code/studio/src/plugins/tile_editor/tile_item.h index f232acb4b..b66a2b6f3 100644 --- a/code/studio/src/plugins/tile_editor/tile_item.h +++ b/code/studio/src/plugins/tile_editor/tile_item.h @@ -106,6 +106,7 @@ public: QVariant data(int column, int role) const; int columnCount() const; void setTileFilename(TileModel::TTileChannel channel, QString filename); + QString getTileFilename(TileModel::TTileChannel channel); void setId( int id ){ m_tileId = id; } int id() const{ return m_tileId; } private: diff --git a/code/studio/src/plugins/tile_editor/tilebank_saver.cpp b/code/studio/src/plugins/tile_editor/tilebank_saver.cpp index db281d161..6bf13fdfe 100644 --- a/code/studio/src/plugins/tile_editor/tilebank_saver.cpp +++ b/code/studio/src/plugins/tile_editor/tilebank_saver.cpp @@ -17,21 +17,219 @@ #include "tilebank_saver.h" #include "tile_model.h" +#include "tile_item.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() { + p = new TileBankSaverPvt(); } 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; } diff --git a/code/studio/src/plugins/tile_editor/tilebank_saver.h b/code/studio/src/plugins/tile_editor/tilebank_saver.h index f86bfab60..2a2806fe1 100644 --- a/code/studio/src/plugins/tile_editor/tilebank_saver.h +++ b/code/studio/src/plugins/tile_editor/tilebank_saver.h @@ -20,18 +20,22 @@ #include #include +#include "land.h" class TileModel; +class TileBankSaverPvt; + class TileBankSaver { public: 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: + TileBankSaverPvt *p; }; #endif