Texture chooser now supports atlas textures as well.
This commit is contained in:
parent
559968ba41
commit
2ca6c73d09
5 changed files with 146 additions and 18 deletions
|
@ -286,6 +286,12 @@ namespace NLGUI
|
||||||
*/
|
*/
|
||||||
void flush ();
|
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)
|
* get a texture file pointer from a string name. O(logN)
|
||||||
* \param id : the id of the texture
|
* \param id : the id of the texture
|
||||||
|
|
|
@ -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
|
* getTextureIdFromName
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -6,40 +6,81 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <QPixMap>
|
#include <QPixMap>
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
|
#include <QListWidget>
|
||||||
|
|
||||||
|
#include "nel/gui/view_renderer.h"
|
||||||
|
|
||||||
|
struct TextureChooserPrivate
|
||||||
|
{
|
||||||
|
QListWidget *fileTextures;
|
||||||
|
QListWidget *atlasTextures;
|
||||||
|
|
||||||
|
TextureChooserPrivate()
|
||||||
|
{
|
||||||
|
fileTextures = new QListWidget();
|
||||||
|
atlasTextures = new QListWidget();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
TextureChooser::TextureChooser( QDialog *parent ) :
|
TextureChooser::TextureChooser( QDialog *parent ) :
|
||||||
QDialog( parent )
|
QDialog( parent )
|
||||||
{
|
{
|
||||||
setupUi( this );
|
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();
|
setupConnections();
|
||||||
}
|
}
|
||||||
|
|
||||||
TextureChooser::~TextureChooser()
|
TextureChooser::~TextureChooser()
|
||||||
{
|
{
|
||||||
|
delete d_ptr;
|
||||||
|
d_ptr = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void TextureChooser::load()
|
void TextureChooser::load()
|
||||||
{
|
{
|
||||||
listWidget->clear();
|
// Load the file textures
|
||||||
|
d_ptr->fileTextures->clear();
|
||||||
|
|
||||||
std::vector< std::string > textures;
|
std::vector< std::string > textures;
|
||||||
//NLMISC::CPath::getFileList( "tga", 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();
|
std::vector< std::string >::const_iterator itr = textures.begin();
|
||||||
while( itr != textures.end() )
|
while( itr != textures.end() )
|
||||||
{
|
{
|
||||||
listWidget->addItem( itr->c_str() );
|
d_ptr->fileTextures->addItem( itr->c_str() );
|
||||||
++itr;
|
++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()
|
void TextureChooser::accept()
|
||||||
{
|
{
|
||||||
QListWidgetItem *item = listWidget->currentItem();
|
QListWidgetItem *item = d_ptr->fileTextures->currentItem();
|
||||||
if( item == NULL )
|
if( item == NULL )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -54,12 +95,12 @@ void TextureChooser::reject()
|
||||||
QDialog::reject();
|
QDialog::reject();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextureChooser::onCurrentRowChanged( int row )
|
void TextureChooser::onFileTxtRowChanged( int row )
|
||||||
{
|
{
|
||||||
if( row < 0 )
|
if( row < 0 )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QListWidgetItem *item = listWidget->item( row );
|
QListWidgetItem *item = d_ptr->fileTextures->item( row );
|
||||||
QString fn = item->text();
|
QString fn = item->text();
|
||||||
|
|
||||||
std::string rfn = fn.toUtf8().constData();
|
std::string rfn = fn.toUtf8().constData();
|
||||||
|
@ -81,11 +122,42 @@ void TextureChooser::onCurrentRowChanged( int row )
|
||||||
return;
|
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 ];
|
uint8 *data = new uint8[ size ];
|
||||||
bm.getData( data );
|
bm.getData( data );
|
||||||
|
|
||||||
/// Convert from ABGR to ARGB
|
/// Convert from ABGR to ARGB
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
@ -107,13 +179,6 @@ void TextureChooser::onCurrentRowChanged( int row )
|
||||||
|
|
||||||
delete data;
|
delete data;
|
||||||
data = NULL;
|
data = NULL;
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void TextureChooser::setupConnections()
|
|
||||||
{
|
|
||||||
connect( listWidget, SIGNAL( currentRowChanged( int ) ), this, SLOT( onCurrentRowChanged( int ) ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,13 @@
|
||||||
|
|
||||||
#include "ui_texture_chooser.h"
|
#include "ui_texture_chooser.h"
|
||||||
|
|
||||||
|
namespace NLMISC
|
||||||
|
{
|
||||||
|
class CBitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TextureChooserPrivate;
|
||||||
|
|
||||||
class TextureChooser : public QDialog, public Ui::TextureChooser
|
class TextureChooser : public QDialog, public Ui::TextureChooser
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -19,12 +26,16 @@ public Q_SLOTS:
|
||||||
void reject();
|
void reject();
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void onCurrentRowChanged( int row );
|
void onFileTxtRowChanged( int row );
|
||||||
|
void onAtlasTxtRowChanged( int row );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setupConnections();
|
void setupConnections();
|
||||||
|
void setPreviewImage( NLMISC::CBitmap &bm );
|
||||||
|
|
||||||
QString selection;
|
QString selection;
|
||||||
|
|
||||||
|
TextureChooserPrivate *d_ptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -18,7 +18,13 @@
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
<item row="0" column="0">
|
<item row="0" column="0">
|
||||||
<widget class="QListWidget" name="listWidget"/>
|
<widget class="QTabWidget" name="tabWidget">
|
||||||
|
<widget class="QWidget" name="tab">
|
||||||
|
<attribute name="title">
|
||||||
|
<string>tab</string>
|
||||||
|
</attribute>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="0" column="1">
|
<item row="0" column="1">
|
||||||
<widget class="QLabel" name="label">
|
<widget class="QLabel" name="label">
|
||||||
|
|
Loading…
Reference in a new issue