1 // Ryzom Core Studio GUI Editor plugin <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "texture_chooser.h"
21 #include "nel/misc/path.h"
22 #include "nel/misc/bitmap.h"
23 #include "nel/misc/file.h"
28 #include <QListWidget>
30 #include "nel/gui/view_renderer.h"
32 struct TextureChooserPrivate
34 QListWidget
*fileTextures
;
35 QListWidget
*atlasTextures
;
37 TextureChooserPrivate()
39 fileTextures
= new QListWidget();
40 atlasTextures
= new QListWidget();
44 TextureChooser::TextureChooser( QDialog
*parent
) :
49 d_ptr
= new TextureChooserPrivate
;
50 this->tabWidget
->clear();
51 this->tabWidget
->addTab( d_ptr
->fileTextures
, tr( "File textures" ) );
52 this->tabWidget
->addTab( d_ptr
->atlasTextures
, tr( "Atlas texture" ) );
57 TextureChooser::~TextureChooser()
64 void TextureChooser::load()
66 // Load the file textures
67 d_ptr
->fileTextures
->clear();
69 std::vector
< std::string
> textures
;
70 //NLMISC::CPath::getFileList( "tga", textures );
71 NLMISC::CPath::getFileListByPath( "dds", "interfaces", textures
);
72 NLMISC::CPath::getFileListByPath( "dds", "gamedev", textures
);
74 std::sort( textures
.begin(), textures
.end() );
76 std::vector
< std::string
>::const_iterator itr
= textures
.begin();
77 while( itr
!= textures
.end() )
79 d_ptr
->fileTextures
->addItem( itr
->c_str() );
83 // Now load the atlas textures
84 d_ptr
->atlasTextures
->clear();
87 NLGUI::CViewRenderer::getInstance()->getTextureNames( textures
);
88 itr
= textures
.begin();
89 while( itr
!= textures
.end() )
91 d_ptr
->atlasTextures
->addItem( itr
->c_str() );
95 // set the file textures row after the atlas, because they are shown first
96 d_ptr
->atlasTextures
->setCurrentRow( 0 );
97 d_ptr
->fileTextures
->setCurrentRow( 0 );
100 void TextureChooser::accept()
102 QListWidgetItem
*item
= d_ptr
->fileTextures
->currentItem();
106 selection
= item
->text();
110 void TextureChooser::reject()
117 void TextureChooser::onFileTxtRowChanged( int row
)
122 QListWidgetItem
*item
= d_ptr
->fileTextures
->item( row
);
123 QString fn
= item
->text();
125 std::string rfn
= fn
.toUtf8().constData();
126 rfn
= NLMISC::CPath::lookup( rfn
);
129 bool b
= f
.open( rfn
);
136 uint8 depth
= bm
.load( f
);
138 b
= bm
.convertToType( NLMISC::CBitmap::RGBA
);
144 setPreviewImage( bm
);
147 void TextureChooser::onAtlasTxtRowChanged( int row
)
152 QListWidgetItem
*item
= d_ptr
->atlasTextures
->item( row
);
153 QString fn
= item
->text();
155 std::string rfn
= fn
.toUtf8().constData();
159 bool b
= NLGUI::CViewRenderer::getInstance()->getTexture( bm
, rfn
);
163 setPreviewImage( bm
);
167 void TextureChooser::setupConnections()
169 connect( d_ptr
->fileTextures
, SIGNAL( currentRowChanged( int ) ), this, SLOT( onFileTxtRowChanged( int ) ) );
170 connect( d_ptr
->atlasTextures
, SIGNAL( currentRowChanged( int ) ), this, SLOT( onAtlasTxtRowChanged( int ) ) );
173 void TextureChooser::setPreviewImage( NLMISC::CBitmap
&bm
)
175 // should be depth, but CBitmap always uses 32 bit to store the image
176 uint32 size
= bm
.getSize() * ( 32 / 8 );
177 uint8
*data
= new uint8
[ size
];
180 /// Convert from ABGR to ARGB
189 data
[ i
] = data
[ i
+ 2 ];
196 QImage
img( data
, bm
.getWidth(), bm
.getHeight(), QImage::Format_ARGB32
);
197 label
->setPixmap( QPixmap::fromImage( img
) );