diff --git a/code/nel/include/nel/3d/tile_bank.h b/code/nel/include/nel/3d/tile_bank.h index 9253c5f45..f391554d3 100644 --- a/code/nel/include/nel/3d/tile_bank.h +++ b/code/nel/include/nel/3d/tile_bank.h @@ -161,6 +161,9 @@ public: { return _Name; }; + + std::set getTileSets() const{ return _TileSet; } + void setName (const std::string& name); void addTileSet (const std::string& name); void removeTileSet (const std::string& name); 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 1e43fa003..427494ab4 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 @@ -33,6 +33,7 @@ #include "tile_item_delegate.h" #include "tilebank_saver.h" +#include "tilebank_loader.h" #include "land_edit_dialog.h" @@ -159,9 +160,12 @@ TileEditorMainWindow::TileEditorMainWindow(QWidget *parent) saveAction->setEnabled( true ); QAction *saveAsAction = Core::ICore::instance()->menuManager()->action( Core::Constants::SAVE_AS ); saveAsAction->setEnabled( true ); + QAction *openAction = Core::ICore::instance()->menuManager()->action( Core::Constants::OPEN ); + openAction->setEnabled( true ); connect( m_ui->actionSaveTileBank, SIGNAL( triggered() ), this, SLOT( save() ) ); connect( m_ui->actionSaveTileBankAs, SIGNAL( triggered() ), this, SLOT( saveAs() ) ); + connect( m_ui->actionOpenTileBank, SIGNAL( triggered() ), this, SLOT( open() ) ); connect( m_ui->orientedCheckBox, SIGNAL( stateChanged( int ) ), this, SLOT( onOrientedStateChanged( int ) ) ); } @@ -222,6 +226,30 @@ void TileEditorMainWindow::saveAs() } } +void TileEditorMainWindow::open() +{ + QString fn = QFileDialog::getOpenFileName( this, + tr( "Loading tilebank" ), + "", + tr( "tilebank files (*.tilebank)" ) ); + + if( fn.isEmpty() ) + return; + + TileBankLoader loader; + bool b = loader.load( fn.toUtf8().constData(), m_tileModel, m_lands ); + + if( !b ) + { + QMessageBox::critical( this, + tr( "Loading tilebank" ), + tr( "Failed to load tilebank %1" ).arg( fn ) ); + } + + // Put the loaded data into the GUI + onTileBankLoaded(); +} + void TileEditorMainWindow::onZoomFactor(int level) { int tile128Scaled=TileModel::TILE_128_BASE_SIZE; @@ -307,7 +335,6 @@ void TileEditorMainWindow::onTileSetAdd() TileSetNode *tileSet = model->createTileSetNode(text); // Retrieve how many rows there currently are and set the current index using that. - m_ui->tileSetLV->reset(); uint32 rows = model->rowCount(); m_ui->tileSetLV->setCurrentIndex(model->index(rows-1, 0)); } @@ -703,6 +730,20 @@ void TileEditorMainWindow::onTileSetRenamed( const QString &oldname, const QStri } } +void TileEditorMainWindow::onTileBankLoaded() +{ + m_ui->landLW->clear(); + QListIterator< Land > itr( m_lands ); + while( itr.hasNext() ) + { + m_ui->landLW->addItem( itr.next().name ); + } + + m_ui->listView128->reset(); + m_ui->listView256->reset(); + m_ui->listViewTransition->reset(); +} + TileModel* TileEditorMainWindow::createTileModel() { QStringList headers; @@ -749,6 +790,8 @@ void TileEditorMainWindow::changeActiveTileSet(const QModelIndex &newIndex, cons m_ui->chooseVegetPushButton->setText( vegetSet ); else m_ui->chooseVegetPushButton->setText( "..." ); + + m_ui->orientedCheckBox->setChecked( newNode->isOriented() ); } else { 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 2d1a9e154..022348ab5 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 @@ -47,6 +47,7 @@ public: public Q_SLOTS: void save(); void saveAs(); + void open(); private Q_SLOTS: void onActionAddTile(bool triggered); @@ -82,6 +83,7 @@ private: void onTileSetRemoved( const QString &set ); void onTileSetRenamed( const QString &oldname, const QString &newname ); + void onTileBankLoaded(); TileModel* createTileModel(); QListView* getListViewByTab( int tab ) const; diff --git a/code/studio/src/plugins/tile_editor/tile_editor_plugin.h b/code/studio/src/plugins/tile_editor/tile_editor_plugin.h index 024e6a6e2..70fb38e77 100644 --- a/code/studio/src/plugins/tile_editor/tile_editor_plugin.h +++ b/code/studio/src/plugins/tile_editor/tile_editor_plugin.h @@ -93,6 +93,7 @@ public: virtual void open() { + m_tileEditorMainWindow->open(); } void save() diff --git a/code/studio/src/plugins/tile_editor/tile_item.cpp b/code/studio/src/plugins/tile_editor/tile_item.cpp index 78c3afae2..76bf7c67e 100644 --- a/code/studio/src/plugins/tile_editor/tile_item.cpp +++ b/code/studio/src/plugins/tile_editor/tile_item.cpp @@ -40,6 +40,7 @@ Node::~Node() void Node::appendChild(Node *item) { + item->setParent( this ); m_childItems.append(item); } @@ -162,6 +163,13 @@ void Node::swapRows( int a, int b ) m_childItems[ b ] = temp; } +void Node::clear() +{ + qDeleteAll( m_childItems ); + m_childItems.clear(); + m_itemData.clear(); +} + /////////////////////////////////////////////////// TileSetNode::TileSetNode(QString tileSetName, Node *parent) : m_tileSetName(tileSetName) diff --git a/code/studio/src/plugins/tile_editor/tile_item.h b/code/studio/src/plugins/tile_editor/tile_item.h index 9538afa3b..3fad517c3 100644 --- a/code/studio/src/plugins/tile_editor/tile_item.h +++ b/code/studio/src/plugins/tile_editor/tile_item.h @@ -55,6 +55,8 @@ public: void swapRows( int a, int b ); + void clear(); + protected: QList m_childItems; QVector m_itemData; diff --git a/code/studio/src/plugins/tile_editor/tile_model.cpp b/code/studio/src/plugins/tile_editor/tile_model.cpp index d68650b8f..99cc557e3 100644 --- a/code/studio/src/plugins/tile_editor/tile_model.cpp +++ b/code/studio/src/plugins/tile_editor/tile_model.cpp @@ -137,7 +137,13 @@ void TileModel::appendRow(const QList &items) void TileModel::appendRow(Node *item) { + int c = rootItem->childCount(); + + beginInsertRows( QModelIndex(), c, c ); + rootItem->appendRow(item); + + endInsertRows(); } bool TileModel::removeRows( int row, int count, const QModelIndex &parent ) @@ -272,6 +278,15 @@ bool TileModel::hasTileSet( const QString &name ) return false; } +void TileModel::clear() +{ + int c = rootItem->childCount(); + if( c == 0 ) + return; + + removeRows( 0, c ); +} + void TileModel::selectFilenameDisplay(bool selected) { m_fileDisplay = selected; diff --git a/code/studio/src/plugins/tile_editor/tile_model.h b/code/studio/src/plugins/tile_editor/tile_model.h index 56d7b792b..f18e60fd3 100644 --- a/code/studio/src/plugins/tile_editor/tile_model.h +++ b/code/studio/src/plugins/tile_editor/tile_model.h @@ -93,6 +93,8 @@ public: bool hasTileSet( const QString &name ); + void clear(); + public Q_SLOTS: void selectFilenameDisplay(bool selected); void selectIndexDisplay(bool selected); diff --git a/code/studio/src/plugins/tile_editor/tilebank_loader.cpp b/code/studio/src/plugins/tile_editor/tilebank_loader.cpp new file mode 100644 index 000000000..6b56dcebe --- /dev/null +++ b/code/studio/src/plugins/tile_editor/tilebank_loader.cpp @@ -0,0 +1,168 @@ +#include "tilebank_loader.h" + +#include "tile_model.h" +#include "tile_item.h" + +#include "nel/3d/tile_bank.h" +#include "nel/misc/file.h" + +class TileBankLoaderPvt +{ +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; + } + + void loadLands( QList< Land > &lands ) + { + lands.clear(); + + Land l; + + int c = bank.getLandCount(); + for( int i = 0; i < c; i++ ) + { + NL3D::CTileLand *land = bank.getLand( i ); + l.name = land->getName().c_str(); + + + std::set< std::string > sets = land->getTileSets(); + std::set< std::string >::const_iterator itr = sets.begin(); + while( itr != sets.end() ) + { + l.tilesets.push_back( itr->c_str() ); + ++itr; + } + + lands.push_back( l ); + } + } + + void loadTiles128( NL3D::CTileSet *set, TileTypeNode *node ) + { + int c = set->getNumTile128(); + for( int i = 0; i < c; i++ ) + { + int idx = set->getTile128( i ); + NL3D::CTile *tile = bank.getTile( idx ); + + TileItemNode *tin = new TileItemNode( i, TileModel::TileDiffuse, "" ); + + for( int i = TileModel::TileDiffuse; i <= TileModel::TileAlpha; i++ ) + { + tin->setTileFilename( TileModel::TTileChannel( i ), tile->getRelativeFileName( channelToTBitmap( TileModel::TTileChannel( i ) ) ).c_str() ); + } + + node->appendChild( tin ); + } + } + + void loadTiles256( NL3D::CTileSet *set, TileTypeNode *node ) + { + int c = set->getNumTile256(); + for( int i = 0; i < c; i++ ) + { + int idx = set->getTile256( i ); + NL3D::CTile *tile = bank.getTile( idx ); + + TileItemNode *tin = new TileItemNode( i, TileModel::TileDiffuse, "" ); + + for( int i = TileModel::TileDiffuse; i <= TileModel::TileAlpha; i++ ) + { + tin->setTileFilename( TileModel::TTileChannel( i ), tile->getRelativeFileName( channelToTBitmap( TileModel::TTileChannel( i ) ) ).c_str() ); + } + + node->appendChild( tin ); + } + } + + void loadTilesTransition( NL3D::CTileSet *set, TileTypeNode *node ) + { + for( int i = 0; i < NL3D::CTileSet::count; i++ ) + { + const NL3D::CTileSetTransition *tr = set->getTransition( i ); + int idx = tr->getTile(); + NL3D::CTile *tile = bank.getTile( idx ); + + TileItemNode *tin = static_cast< TileItemNode* >( node->child( i ) ); + + for( int j = TileModel::TileDiffuse; j <= TileModel::TileAlpha; j++ ) + { + tin->setTileFilename( TileModel::TTileChannel( i ), tile->getRelativeFileName( channelToTBitmap( TileModel::TTileChannel( i ) ) ).c_str() ); + } + } + } + + void loadTilesDisplacement( NL3D::CTileSet *set, TileTypeNode *node ) + { + for( int i = 0; i < NL3D::CTileSet::CountDisplace; i++ ) + { + uint did = set->getDisplacementTile( NL3D::CTileSet::TDisplacement( i ) ); + const char *fn = bank.getDisplacementMap( did ); + + TileItemNode *tin = static_cast< TileItemNode* >( node->child( i ) ); + tin->setTileFilename( TileModel::TileDiffuse, fn ); + } + } + + void loadTileSet( NL3D::CTileSet *set, TileSetNode *node ) + { + loadTiles128( set, static_cast< TileTypeNode* >( node->child( 0 ) ) ); + loadTiles256( set, static_cast< TileTypeNode* >( node->child( 1 ) ) ); + loadTilesTransition( set, static_cast< TileTypeNode* >( node->child( 2 ) ) ); + loadTilesDisplacement( set, static_cast< TileTypeNode* >( node->child( 3 ) ) ); + + node->setOriented( set->getOriented() ); + } + + void loadTileSets( TileModel *model ) + { + model->clear(); + + int c = bank.getTileSetCount(); + for( int i = 0; i < c; i++ ) + { + NL3D::CTileSet *set = bank.getTileSet( i ); + TileSetNode *node = model->createTileSetNode( set->getName().c_str() ); + loadTileSet( set, node ); + } + } +}; + + +TileBankLoader::TileBankLoader() +{ + p = new TileBankLoaderPvt; +} + +TileBankLoader::~TileBankLoader() +{ + delete p; + p = NULL; +} + +bool TileBankLoader::load( const char *filename, TileModel *model, QList< Land > &lands ) +{ + NLMISC::CIFile file; + if( !file.open( filename, false ) ) + return false; + + p->bank.serial( file ); + + p->loadLands( lands ); + p->loadTileSets( model ); + + return false; +} diff --git a/code/studio/src/plugins/tile_editor/tilebank_loader.h b/code/studio/src/plugins/tile_editor/tilebank_loader.h new file mode 100644 index 000000000..b213cb309 --- /dev/null +++ b/code/studio/src/plugins/tile_editor/tilebank_loader.h @@ -0,0 +1,23 @@ +#ifndef TILEBANK_LOADER_H +#define TILEBANK_LOADER_H + +#include "land.h" + +class TileModel; +class TileBankLoaderPvt; + +class TileBankLoader +{ +public: + TileBankLoader(); + ~TileBankLoader(); + + bool load( const char *filename, TileModel *model, QList< Land > &lands ); + +private: + TileBankLoaderPvt *p; +}; + + +#endif +