From 012ad9680603863fc50ce4f6090e625a29e90708 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Wed, 9 Jul 2014 21:07:12 +0200 Subject: [PATCH] Texture chooser now supports atlas textures as well. --HG-- branch : gsoc2014-dfighter --- code/nel/include/nel/gui/view_renderer.h | 6 ++ code/nel/src/gui/view_renderer.cpp | 40 ++++++++ .../plugins/gui_editor/texture_chooser.cpp | 97 ++++++++++++++++--- .../src/plugins/gui_editor/texture_chooser.h | 13 ++- .../src/plugins/gui_editor/texture_chooser.ui | 8 +- 5 files changed, 146 insertions(+), 18 deletions(-) diff --git a/code/nel/include/nel/gui/view_renderer.h b/code/nel/include/nel/gui/view_renderer.h index 83ac4457e..fe5dad405 100644 --- a/code/nel/include/nel/gui/view_renderer.h +++ b/code/nel/include/nel/gui/view_renderer.h @@ -286,6 +286,12 @@ namespace NLGUI */ void flush (); + /// Retrives a texture + bool getTexture( NLMISC::CBitmap &bm, const std::string &name ); + + /// Retrieve the texture names + void getTextureNames( std::vector< std::string > &textures ); + /** * get a texture file pointer from a string name. O(logN) * \param id : the id of the texture diff --git a/code/nel/src/gui/view_renderer.cpp b/code/nel/src/gui/view_renderer.cpp index b2758c634..6e6675599 100644 --- a/code/nel/src/gui/view_renderer.cpp +++ b/code/nel/src/gui/view_renderer.cpp @@ -1064,6 +1064,46 @@ namespace NLGUI } } + bool CViewRenderer::getTexture( NLMISC::CBitmap &bm, const std::string &name ) + { + TTextureMap::const_iterator itr = _TextureMap.find( name ); + if( itr == _TextureMap.end() ) + return false; + + sint32 id = itr->second; + SImage *si = getSImage( id ); + NLMISC::CBitmap *src = si->GlobalTexturePtr->Texture->generateDatas(); + + if( src->getPixelFormat() != NLMISC::CBitmap::RGBA ) + return false; + + uint x0 = (uint)( si->UVMin.U * si->GlobalTexturePtr->Width ); + uint y0 = (uint)( si->UVMin.V * si->GlobalTexturePtr->Height ); + uint x1 = (uint)( si->UVMax.U * si->GlobalTexturePtr->Width ); + uint y1 = (uint)( si->UVMax.V * si->GlobalTexturePtr->Height ); + + if( x1 == x0 ) + return false; + + if( y1 == y0 ) + return false; + + bm.resize( x1 - x0, y1 - y0 ); + bm.blit( *src, x0, y0, ( x1 - x0 ), ( y1 - y0 ), 0, 0 ); + + return true; + } + + void CViewRenderer::getTextureNames( std::vector< std::string > &textures ) + { + TTextureMap::const_iterator itr = _TextureMap.begin(); + while( itr != _TextureMap.end() ) + { + textures.push_back( itr->first ); + ++itr; + } + } + /* * getTextureIdFromName */ diff --git a/code/studio/src/plugins/gui_editor/texture_chooser.cpp b/code/studio/src/plugins/gui_editor/texture_chooser.cpp index d857d9bdc..1c5e692ee 100644 --- a/code/studio/src/plugins/gui_editor/texture_chooser.cpp +++ b/code/studio/src/plugins/gui_editor/texture_chooser.cpp @@ -6,40 +6,81 @@ #include #include #include +#include + +#include "nel/gui/view_renderer.h" + +struct TextureChooserPrivate +{ + QListWidget *fileTextures; + QListWidget *atlasTextures; + + TextureChooserPrivate() + { + fileTextures = new QListWidget(); + atlasTextures = new QListWidget(); + } +}; TextureChooser::TextureChooser( QDialog *parent ) : QDialog( parent ) { setupUi( this ); + + d_ptr = new TextureChooserPrivate; + this->tabWidget->clear(); + this->tabWidget->addTab( d_ptr->fileTextures, tr( "File textures" ) ); + this->tabWidget->addTab( d_ptr->atlasTextures, tr( "Atlas texture" ) ); + setupConnections(); } TextureChooser::~TextureChooser() { + delete d_ptr; + d_ptr = NULL; } void TextureChooser::load() { - listWidget->clear(); + // Load the file textures + d_ptr->fileTextures->clear(); std::vector< std::string > textures; //NLMISC::CPath::getFileList( "tga", textures ); - NLMISC::CPath::getFileListByPath( "tga", "interfaces", textures ); + NLMISC::CPath::getFileListByPath( "dds", "interfaces", textures ); + NLMISC::CPath::getFileListByPath( "dds", "gamedev", textures ); + + std::sort( textures.begin(), textures.end() ); std::vector< std::string >::const_iterator itr = textures.begin(); while( itr != textures.end() ) { - listWidget->addItem( itr->c_str() ); + d_ptr->fileTextures->addItem( itr->c_str() ); ++itr; } - listWidget->setCurrentRow( 0 ); + // Now load the atlas textures + d_ptr->atlasTextures->clear(); + textures.clear(); + + NLGUI::CViewRenderer::getInstance()->getTextureNames( textures ); + itr = textures.begin(); + while( itr != textures.end() ) + { + d_ptr->atlasTextures->addItem( itr->c_str() ); + ++itr; + } + + // set the file textures row after the atlas, because they are shown first + d_ptr->atlasTextures->setCurrentRow( 0 ); + d_ptr->fileTextures->setCurrentRow( 0 ); } void TextureChooser::accept() { - QListWidgetItem *item = listWidget->currentItem(); + QListWidgetItem *item = d_ptr->fileTextures->currentItem(); if( item == NULL ) return; @@ -54,12 +95,12 @@ void TextureChooser::reject() QDialog::reject(); } -void TextureChooser::onCurrentRowChanged( int row ) +void TextureChooser::onFileTxtRowChanged( int row ) { if( row < 0 ) return; - QListWidgetItem *item = listWidget->item( row ); + QListWidgetItem *item = d_ptr->fileTextures->item( row ); QString fn = item->text(); std::string rfn = fn.toUtf8().constData(); @@ -81,11 +122,42 @@ void TextureChooser::onCurrentRowChanged( int row ) return; } - uint32 size = bm.getSize() * ( 32 / 8 ); // should be depth, but CBitmap always uses 32 bit to store the image + setPreviewImage( bm ); +} +void TextureChooser::onAtlasTxtRowChanged( int row ) +{ + if( row < 0 ) + return; + + QListWidgetItem *item = d_ptr->atlasTextures->item( row ); + QString fn = item->text(); + + std::string rfn = fn.toUtf8().constData(); + + NLMISC::CBitmap bm; + + bool b = NLGUI::CViewRenderer::getInstance()->getTexture( bm, rfn ); + if( !b ) + return; + + setPreviewImage( bm ); +} + + +void TextureChooser::setupConnections() +{ + connect( d_ptr->fileTextures, SIGNAL( currentRowChanged( int ) ), this, SLOT( onFileTxtRowChanged( int ) ) ); + connect( d_ptr->atlasTextures, SIGNAL( currentRowChanged( int ) ), this, SLOT( onAtlasTxtRowChanged( int ) ) ); +} + +void TextureChooser::setPreviewImage( NLMISC::CBitmap &bm ) +{ + // should be depth, but CBitmap always uses 32 bit to store the image + uint32 size = bm.getSize() * ( 32 / 8 ); uint8 *data = new uint8[ size ]; bm.getData( data ); - + /// Convert from ABGR to ARGB { int i = 0; @@ -107,13 +179,6 @@ void TextureChooser::onCurrentRowChanged( int row ) delete data; data = NULL; - -} - - -void TextureChooser::setupConnections() -{ - connect( listWidget, SIGNAL( currentRowChanged( int ) ), this, SLOT( onCurrentRowChanged( int ) ) ); } diff --git a/code/studio/src/plugins/gui_editor/texture_chooser.h b/code/studio/src/plugins/gui_editor/texture_chooser.h index bab1dd1dc..9a615c266 100644 --- a/code/studio/src/plugins/gui_editor/texture_chooser.h +++ b/code/studio/src/plugins/gui_editor/texture_chooser.h @@ -3,6 +3,13 @@ #include "ui_texture_chooser.h" +namespace NLMISC +{ + class CBitmap; +} + +struct TextureChooserPrivate; + class TextureChooser : public QDialog, public Ui::TextureChooser { Q_OBJECT @@ -19,12 +26,16 @@ public Q_SLOTS: void reject(); private Q_SLOTS: - void onCurrentRowChanged( int row ); + void onFileTxtRowChanged( int row ); + void onAtlasTxtRowChanged( int row ); private: void setupConnections(); + void setPreviewImage( NLMISC::CBitmap &bm ); QString selection; + + TextureChooserPrivate *d_ptr; }; #endif diff --git a/code/studio/src/plugins/gui_editor/texture_chooser.ui b/code/studio/src/plugins/gui_editor/texture_chooser.ui index 0f635e96f..a0837ee83 100644 --- a/code/studio/src/plugins/gui_editor/texture_chooser.ui +++ b/code/studio/src/plugins/gui_editor/texture_chooser.ui @@ -18,7 +18,13 @@ - + + + + tab + + +