2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant 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 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 // Leonardo Zide (leo@lokigames.com)
28 #include "texwindow.h"
32 #include "debugging/debugging.h"
36 #include "ifilesystem.h"
40 #include "moduleobserver.h"
46 #include <uilib/uilib.h>
48 #include "signal/signal.h"
49 #include "math/vector.h"
50 #include "texturelib.h"
51 #include "string/string.h"
52 #include "shaderlib.h"
55 #include "stream/memstream.h"
56 #include "stream/textfilestream.h"
57 #include "stream/stringstream.h"
63 #include "gtkutil/menu.h"
64 #include "gtkutil/nonmodal.h"
65 #include "gtkutil/cursor.h"
66 #include "gtkutil/widget.h"
67 #include "gtkutil/glwidget.h"
68 #include "gtkutil/messagebox.h"
74 #include "brush_primit.h"
75 #include "brushmanip.h"
76 #include "patchmanip.h"
81 #include "mainframe.h"
82 #include "findtexturedialog.h"
83 #include "surfacedialog.h"
84 #include "patchdialog.h"
85 #include "groupdialog.h"
86 #include "preferences.h"
90 bool TextureBrowser_showWads(){
91 return !string_empty( g_pGameDescription->getKeyValue( "show_wads" ) );
94 void TextureBrowser_queueDraw( TextureBrowser& textureBrowser );
96 bool string_equal_start( const char* string, StringRange start ){
97 return string_equal_n( string, start.first, start.last - start.first );
100 typedef std::set<CopiedString> TextureGroups;
102 void TextureGroups_addWad( TextureGroups& groups, const char* archive ){
103 if ( extension_equal( path_get_extension( archive ), "wad" ) ) {
104 groups.insert( archive );
108 typedef ReferenceCaller<TextureGroups, void(const char*), TextureGroups_addWad> TextureGroupsAddWadCaller;
112 bool g_TextureBrowser_shaderlistOnly = false;
113 bool g_TextureBrowser_fixedSize = true;
114 bool g_TextureBrowser_filterMissing = false;
115 bool g_TextureBrowser_filterFallback = true;
116 bool g_TextureBrowser_enableAlpha = true;
119 CopiedString g_notex;
120 CopiedString g_shadernotex;
122 bool isMissing(const char* name);
124 bool isNotex(const char* name);
126 bool isMissing(const char* name){
127 if ( string_equal( g_notex.c_str(), name ) ) {
130 if ( string_equal( g_shadernotex.c_str(), name ) ) {
136 bool isNotex(const char* name){
137 if ( string_equal_suffix( name, "/" DEFAULT_NOTEX_BASENAME ) ) {
140 if ( string_equal_suffix( name, "/" DEFAULT_SHADERNOTEX_BASENAME ) ) {
146 void TextureGroups_addShader( TextureGroups& groups, const char* shaderName ){
147 const char* texture = path_make_relative( shaderName, "textures/" );
149 // hide notex / shadernotex images
150 if ( g_TextureBrowser_filterFallback ) {
151 if ( isNotex( shaderName ) ) {
154 if ( isNotex( texture ) ) {
159 if ( texture != shaderName ) {
160 const char* last = path_remove_directory( texture );
161 if ( !string_empty( last ) ) {
162 groups.insert( CopiedString( StringRange( texture, --last ) ) );
167 typedef ReferenceCaller<TextureGroups, void(const char*), TextureGroups_addShader> TextureGroupsAddShaderCaller;
169 void TextureGroups_addDirectory( TextureGroups& groups, const char* directory ){
170 groups.insert( directory );
172 typedef ReferenceCaller<TextureGroups, void(const char*), TextureGroups_addDirectory> TextureGroupsAddDirectoryCaller;
174 class DeferredAdjustment
179 typedef void ( *ValueChangedFunction )( void* data, gdouble value );
181 ValueChangedFunction m_function;
184 static gboolean deferred_value_changed( gpointer data ){
185 reinterpret_cast<DeferredAdjustment*>( data )->m_function(
186 reinterpret_cast<DeferredAdjustment*>( data )->m_data,
187 reinterpret_cast<DeferredAdjustment*>( data )->m_value
189 reinterpret_cast<DeferredAdjustment*>( data )->m_handler = 0;
190 reinterpret_cast<DeferredAdjustment*>( data )->m_value = 0;
195 DeferredAdjustment( ValueChangedFunction function, void* data ) : m_value( 0 ), m_handler( 0 ), m_function( function ), m_data( data ){
199 if ( m_handler != 0 ) {
200 g_source_remove( m_handler );
201 deferred_value_changed( this );
205 void value_changed( gdouble value ){
207 if ( m_handler == 0 ) {
208 m_handler = g_idle_add( deferred_value_changed, this );
212 static void adjustment_value_changed(ui::Adjustment adjustment, DeferredAdjustment* self ){
213 self->value_changed( gtk_adjustment_get_value(adjustment) );
218 class TextureBrowser;
220 typedef ReferenceCaller<TextureBrowser, void(), TextureBrowser_queueDraw> TextureBrowserQueueDrawCaller;
222 void TextureBrowser_scrollChanged( void* data, gdouble value );
227 STARTUPSHADERS_NONE = 0,
228 STARTUPSHADERS_COMMON,
231 void TextureBrowser_hideUnusedExport( const Callback<void(bool)> & importer );
233 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_hideUnusedExport> TextureBrowserHideUnusedExport;
235 void TextureBrowser_showShadersExport( const Callback<void(bool)> & importer );
237 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_showShadersExport> TextureBrowserShowShadersExport;
239 void TextureBrowser_showShaderlistOnly( const Callback<void(bool)> & importer );
241 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_showShaderlistOnly> TextureBrowserShowShaderlistOnlyExport;
243 void TextureBrowser_fixedSize( const Callback<void(bool)> & importer );
245 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_fixedSize> TextureBrowserFixedSizeExport;
247 void TextureBrowser_filterMissing( const Callback<void(bool)> & importer );
249 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_filterMissing> TextureBrowserFilterMissingExport;
251 void TextureBrowser_filterFallback( const Callback<void(bool)> & importer );
253 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_filterFallback> TextureBrowserFilterFallbackExport;
255 void TextureBrowser_enableAlpha( const Callback<void(bool)> & importer );
257 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_enableAlpha> TextureBrowserEnableAlphaExport;
268 ui::Window m_parent{ui::null};
269 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
270 ui::VBox m_vframe{ui::null};
271 ui::VBox m_vfiller{ui::null};
272 ui::HBox m_hframe{ui::null};
273 ui::HBox m_hfiller{ui::null};
274 #else // !WORKAROUND_MACOS_GTK2_GLWIDGET
275 ui::VBox m_frame{ui::null};
276 #endif // !WORKAROUND_MACOS_GTK2_GLWIDGET
277 ui::GLArea m_gl_widget{ui::null};
278 ui::Widget m_texture_scroll{ui::null};
279 ui::TreeView m_treeViewTree{ui::New};
280 ui::TreeView m_treeViewTags{ui::null};
281 ui::Frame m_tag_frame{ui::null};
282 ui::ListStore m_assigned_store{ui::null};
283 ui::ListStore m_available_store{ui::null};
284 ui::TreeView m_assigned_tree{ui::null};
285 ui::TreeView m_available_tree{ui::null};
286 ui::Widget m_scr_win_tree{ui::null};
287 ui::Widget m_scr_win_tags{ui::null};
288 ui::Widget m_tag_notebook{ui::null};
289 ui::Button m_search_button{ui::null};
290 ui::Widget m_shader_info_item{ui::null};
292 std::set<CopiedString> m_all_tags;
293 ui::ListStore m_all_tags_list{ui::null};
294 std::vector<CopiedString> m_copied_tags;
295 std::set<CopiedString> m_found_shaders;
297 ToggleItem m_hideunused_item;
298 ToggleItem m_hidenotex_item;
299 ToggleItem m_showshaders_item;
300 ToggleItem m_showshaderlistonly_item;
301 ToggleItem m_fixedsize_item;
302 ToggleItem m_filternotex_item;
303 ToggleItem m_enablealpha_item;
306 guint m_exposeHandler;
308 bool m_heightChanged;
309 bool m_originInvalid;
311 DeferredAdjustment m_scrollAdjustment;
312 FreezePointer m_freezePointer;
314 Vector3 color_textureback;
315 // the increment step we use against the wheel mouse
316 std::size_t m_mouseWheelScrollIncrement;
317 std::size_t m_textureScale;
318 // make the texture increments match the grid changes
320 bool m_showTextureScrollbar;
321 StartupShaders m_startupShaders;
322 // if true, the texture window will only display in-use shaders
323 // if false, all the shaders in memory are displayed
328 // The uniform size (in pixels) that textures are resized to when m_resizeTextures is true.
329 int m_uniformTextureSize;
331 // Return the display width of a texture in the texture browser
332 int getTextureWidth( qtexture_t* tex ){
334 if ( !g_TextureBrowser_fixedSize ) {
335 // Don't use uniform size
336 width = (int)( tex->width * ( (float)m_textureScale / 100 ) );
339 ( tex->width >= tex->height ) {
340 // Texture is square, or wider than it is tall
341 width = m_uniformTextureSize;
344 // Otherwise, preserve the texture's aspect ratio
345 width = (int)( m_uniformTextureSize * ( (float)tex->width / tex->height ) );
350 // Return the display height of a texture in the texture browser
351 int getTextureHeight( qtexture_t* tex ){
353 if ( !g_TextureBrowser_fixedSize ) {
354 // Don't use uniform size
355 height = (int)( tex->height * ( (float)m_textureScale / 100 ) );
357 else if ( tex->height >= tex->width ) {
358 // Texture is square, or taller than it is wide
359 height = m_uniformTextureSize;
362 // Otherwise, preserve the texture's aspect ratio
363 height = (int)( m_uniformTextureSize * ( (float)tex->height / tex->width ) );
369 m_texture_scroll( ui::null ),
370 m_hideunused_item( TextureBrowserHideUnusedExport() ),
371 m_hidenotex_item( TextureBrowserFilterFallbackExport() ),
372 m_showshaders_item( TextureBrowserShowShadersExport() ),
373 m_showshaderlistonly_item( TextureBrowserShowShaderlistOnlyExport() ),
374 m_fixedsize_item( TextureBrowserFixedSizeExport() ),
375 m_filternotex_item( TextureBrowserFilterMissingExport() ),
376 m_enablealpha_item( TextureBrowserEnableAlphaExport() ),
377 m_heightChanged( true ),
378 m_originInvalid( true ),
379 m_scrollAdjustment( TextureBrowser_scrollChanged, this ),
380 color_textureback( 0.25f, 0.25f, 0.25f ),
381 m_mouseWheelScrollIncrement( 64 ),
382 m_textureScale( 50 ),
383 m_showShaders( true ),
384 m_showTextureScrollbar( true ),
385 m_startupShaders( STARTUPSHADERS_NONE ),
386 m_hideUnused( false ),
387 m_rmbSelected( false ),
388 m_searchedTags( false ),
390 m_uniformTextureSize( 96 ){
394 void ( *TextureBrowser_textureSelected )( const char* shader );
397 void TextureBrowser_updateScroll( TextureBrowser& textureBrowser );
400 const char* TextureBrowser_getCommonShadersName(){
401 const char* value = g_pGameDescription->getKeyValue( "common_shaders_name" );
402 if ( !string_empty( value ) ) {
408 const char* TextureBrowser_getCommonShadersDir(){
409 const char* value = g_pGameDescription->getKeyValue( "common_shaders_dir" );
410 if ( !string_empty( value ) ) {
416 inline int TextureBrowser_fontHeight( TextureBrowser& textureBrowser ){
417 return GlobalOpenGL().m_font->getPixelHeight();
420 const char* TextureBrowser_GetSelectedShader( TextureBrowser& textureBrowser ){
421 return textureBrowser.shader.c_str();
424 void TextureBrowser_SetStatus( TextureBrowser& textureBrowser, const char* name ){
425 IShader* shader = QERApp_Shader_ForName( name );
426 qtexture_t* q = shader->getTexture();
427 StringOutputStream strTex( 256 );
428 strTex << name << " W: " << Unsigned( q->width ) << " H: " << Unsigned( q->height );
430 g_pParentWnd->SetStatusText( g_pParentWnd->m_texture_status, strTex.c_str() );
433 void TextureBrowser_Focus( TextureBrowser& textureBrowser, const char* name );
435 void TextureBrowser_SetSelectedShader( TextureBrowser& textureBrowser, const char* shader ){
436 textureBrowser.shader = shader;
437 TextureBrowser_SetStatus( textureBrowser, shader );
438 TextureBrowser_Focus( textureBrowser, shader );
440 if ( FindTextureDialog_isOpen() ) {
441 FindTextureDialog_selectTexture( shader );
444 // disable the menu item "shader info" if no shader was selected
445 IShader* ishader = QERApp_Shader_ForName( shader );
446 CopiedString filename = ishader->getShaderFileName();
448 if ( filename.empty() ) {
449 if ( textureBrowser.m_shader_info_item != NULL ) {
450 gtk_widget_set_sensitive( textureBrowser.m_shader_info_item, FALSE );
454 gtk_widget_set_sensitive( textureBrowser.m_shader_info_item, TRUE );
461 CopiedString g_TextureBrowser_currentDirectory;
464 ============================================================================
468 TTimo: now based on a rundown through all the shaders
469 NOTE: we expect the Active shaders count doesn't change during a Texture_StartPos .. Texture_NextPos cycle
470 otherwise we may need to rely on a list instead of an array storage
471 ============================================================================
477 // texture layout functions
478 // TTimo: now based on shaders
479 int current_x, current_y, current_row;
482 void Texture_StartPos( TextureLayout& layout ){
483 layout.current_x = 8;
484 layout.current_y = -8;
485 layout.current_row = 0;
488 void Texture_NextPos( TextureBrowser& textureBrowser, TextureLayout& layout, qtexture_t* current_texture, int *x, int *y ){
489 qtexture_t* q = current_texture;
491 int nWidth = textureBrowser.getTextureWidth( q );
492 int nHeight = textureBrowser.getTextureHeight( q );
493 if ( layout.current_x + nWidth > textureBrowser.width - 8 && layout.current_row ) { // go to the next row unless the texture is the first on the row
494 layout.current_x = 8;
495 layout.current_y -= layout.current_row + TextureBrowser_fontHeight( textureBrowser ) + 4;
496 layout.current_row = 0;
499 *x = layout.current_x;
500 *y = layout.current_y;
502 // Is our texture larger than the row? If so, grow the
503 // row height to match it
505 if ( layout.current_row < nHeight ) {
506 layout.current_row = nHeight;
509 // never go less than 96, or the names get all crunched up
510 layout.current_x += nWidth < 96 ? 96 : nWidth;
511 layout.current_x += 8;
514 bool TextureSearch_IsShown( const char* name ){
515 std::set<CopiedString>::iterator iter;
517 iter = GlobalTextureBrowser().m_found_shaders.find( name );
519 if ( iter == GlobalTextureBrowser().m_found_shaders.end() ) {
527 // if texture_showinuse jump over non in-use textures
528 bool Texture_IsShown( IShader* shader, bool show_shaders, bool hideUnused ){
529 // filter missing shaders
530 // ugly: filter on built-in fallback name after substitution
531 if ( g_TextureBrowser_filterMissing ) {
532 if ( isMissing( shader->getTexture()->name ) ) {
536 // filter the fallback (notex/shadernotex) for missing shaders or editor image
537 if ( g_TextureBrowser_filterFallback ) {
538 if ( isNotex( shader->getName() ) ) {
541 if ( isNotex( shader->getTexture()->name ) ) {
546 if ( g_TextureBrowser_currentDirectory == "Untagged" ) {
547 std::set<CopiedString>::iterator iter;
549 iter = GlobalTextureBrowser().m_found_shaders.find( shader->getName() );
551 if ( iter == GlobalTextureBrowser().m_found_shaders.end() ) {
559 if ( !shader_equal_prefix( shader->getName(), "textures/" ) ) {
563 if ( !show_shaders && !shader->IsDefault() ) {
567 if ( hideUnused && !shader->IsInUse() ) {
571 if ( GlobalTextureBrowser().m_searchedTags ) {
572 if ( !TextureSearch_IsShown( shader->getName() ) ) {
580 if ( TextureBrowser_showWads() )
582 if ( g_TextureBrowser_currentDirectory != ""
583 && !string_equal( shader->getWadName(), g_TextureBrowser_currentDirectory.c_str() ) )
588 else if ( !shader_equal_prefix( shader_get_textureName( shader->getName() ), g_TextureBrowser_currentDirectory.c_str() ) ) {
596 void TextureBrowser_heightChanged( TextureBrowser& textureBrowser ){
597 textureBrowser.m_heightChanged = true;
599 TextureBrowser_updateScroll( textureBrowser );
600 TextureBrowser_queueDraw( textureBrowser );
603 void TextureBrowser_evaluateHeight( TextureBrowser& textureBrowser ){
604 if ( textureBrowser.m_heightChanged ) {
605 textureBrowser.m_heightChanged = false;
607 textureBrowser.m_nTotalHeight = 0;
609 TextureLayout layout;
610 Texture_StartPos( layout );
611 for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
613 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
615 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
620 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
621 textureBrowser.m_nTotalHeight = std::max( textureBrowser.m_nTotalHeight, abs( layout.current_y ) + TextureBrowser_fontHeight( textureBrowser ) + textureBrowser.getTextureHeight( shader->getTexture() ) + 4 );
626 int TextureBrowser_TotalHeight( TextureBrowser& textureBrowser ){
627 TextureBrowser_evaluateHeight( textureBrowser );
628 return textureBrowser.m_nTotalHeight;
631 inline const int& min_int( const int& left, const int& right ){
632 return std::min( left, right );
635 void TextureBrowser_clampOriginY( TextureBrowser& textureBrowser ){
636 if ( textureBrowser.originy > 0 ) {
637 textureBrowser.originy = 0;
639 int lower = min_int( textureBrowser.height - TextureBrowser_TotalHeight( textureBrowser ), 0 );
640 if ( textureBrowser.originy < lower ) {
641 textureBrowser.originy = lower;
645 int TextureBrowser_getOriginY( TextureBrowser& textureBrowser ){
646 if ( textureBrowser.m_originInvalid ) {
647 textureBrowser.m_originInvalid = false;
648 TextureBrowser_clampOriginY( textureBrowser );
649 TextureBrowser_updateScroll( textureBrowser );
651 return textureBrowser.originy;
654 void TextureBrowser_setOriginY( TextureBrowser& textureBrowser, int originy ){
655 textureBrowser.originy = originy;
656 TextureBrowser_clampOriginY( textureBrowser );
657 TextureBrowser_updateScroll( textureBrowser );
658 TextureBrowser_queueDraw( textureBrowser );
662 Signal0 g_activeShadersChangedCallbacks;
664 void TextureBrowser_addActiveShadersChangedCallback( const SignalHandler& handler ){
665 g_activeShadersChangedCallbacks.connectLast( handler );
668 void TextureBrowser_constructTreeStore();
670 class ShadersObserver : public ModuleObserver
672 Signal0 m_realiseCallbacks;
675 m_realiseCallbacks();
676 TextureBrowser_constructTreeStore();
682 void insert( const SignalHandler& handler ){
683 m_realiseCallbacks.connectLast( handler );
689 ShadersObserver g_ShadersObserver;
692 void TextureBrowser_addShadersRealiseCallback( const SignalHandler& handler ){
693 g_ShadersObserver.insert( handler );
696 void TextureBrowser_activeShadersChanged( TextureBrowser& textureBrowser ){
697 TextureBrowser_heightChanged( textureBrowser );
698 textureBrowser.m_originInvalid = true;
700 g_activeShadersChangedCallbacks();
703 struct TextureBrowser_ShowScrollbar {
704 static void Export(const TextureBrowser &self, const Callback<void(bool)> &returnz) {
705 returnz(self.m_showTextureScrollbar);
708 static void Import(TextureBrowser &self, bool value) {
709 self.m_showTextureScrollbar = value;
710 if (self.m_texture_scroll) {
711 self.m_texture_scroll.visible(self.m_showTextureScrollbar);
712 TextureBrowser_updateScroll(self);
720 TextureBrowser_ShowDirectory
721 relies on texture_directory global for the directory to use
722 1) Load the shaders for the given directory
723 2) Scan the remaining texture, load them and assign them a default shader (the "noshader" shader)
724 NOTE: when writing a texture plugin, or some texture extensions, this function may need to be overriden, and made
725 available through the IShaders interface
726 NOTE: for texture window layout:
727 all shaders are stored with alphabetical order after load
728 previously loaded and displayed stuff is hidden, only in-use and newly loaded is shown
729 ( the GL textures are not flushed though)
733 bool endswith( const char *haystack, const char *needle ){
734 size_t lh = strlen( haystack );
735 size_t ln = strlen( needle );
739 return !memcmp( haystack + ( lh - ln ), needle, ln );
742 bool texture_name_ignore( const char* name ){
743 StringOutputStream strTemp( string_length( name ) );
744 strTemp << LowerCase( name );
747 endswith( strTemp.c_str(), ".specular" ) ||
748 endswith( strTemp.c_str(), ".glow" ) ||
749 endswith( strTemp.c_str(), ".bump" ) ||
750 endswith( strTemp.c_str(), ".diffuse" ) ||
751 endswith( strTemp.c_str(), ".blend" ) ||
752 endswith( strTemp.c_str(), ".alpha" ) ||
753 endswith( strTemp.c_str(), "_alpha" ) ||
755 endswith( strTemp.c_str(), "_h" ) ||
756 endswith( strTemp.c_str(), "_local" ) ||
757 endswith( strTemp.c_str(), "_nm" ) ||
758 endswith( strTemp.c_str(), "_s" ) ||
760 endswith( strTemp.c_str(), "_bump" ) ||
761 endswith( strTemp.c_str(), "_glow" ) ||
762 endswith( strTemp.c_str(), "_gloss" ) ||
763 endswith( strTemp.c_str(), "_luma" ) ||
764 endswith( strTemp.c_str(), "_norm" ) ||
765 endswith( strTemp.c_str(), "_pants" ) ||
766 endswith( strTemp.c_str(), "_shirt" ) ||
767 endswith( strTemp.c_str(), "_reflect" ) ||
769 endswith( strTemp.c_str(), "_d" ) ||
770 endswith( strTemp.c_str(), "_n" ) ||
771 endswith( strTemp.c_str(), "_p" ) ||
772 endswith( strTemp.c_str(), "_g" ) ||
773 endswith( strTemp.c_str(), "_a" ) ||
777 class LoadShaderVisitor : public Archive::Visitor
780 void visit( const char* name ){
781 IShader* shader = QERApp_Shader_ForName( CopiedString( StringRange( name, path_get_filename_base_end( name ) ) ).c_str() );
783 shader->setWadName( g_TextureBrowser_currentDirectory.c_str() );
787 void TextureBrowser_SetHideUnused( TextureBrowser& textureBrowser, bool hideUnused );
789 ui::Widget g_page_textures{ui::null};
791 void TextureBrowser_toggleShow(){
792 GroupDialog_showPage( g_page_textures );
796 void TextureBrowser_updateTitle(){
797 GroupDialog_updatePageTitle( g_page_textures );
801 class TextureCategoryLoadShader
803 const char* m_directory;
804 std::size_t& m_count;
806 using func = void(const char *);
808 TextureCategoryLoadShader( const char* directory, std::size_t& count )
809 : m_directory( directory ), m_count( count ){
813 void operator()( const char* name ) const {
814 if ( shader_equal_prefix( name, "textures/" )
815 && shader_equal_prefix( name + string_length( "textures/" ), m_directory ) ) {
817 // request the shader, this will load the texture if needed
818 // this Shader_ForName call is a kind of hack
819 IShader *pFoo = QERApp_Shader_ForName( name );
825 void TextureDirectory_loadTexture( const char* directory, const char* texture ){
826 // Doom3-like dds/ prefix (used by DarkPlaces).
827 // When we list dds/textures/ folder,
828 // store the texture names without dds/ prefix.
829 if ( !strncmp( "dds/", directory, 4 ) )
831 directory = &directory[ 4 ];
834 StringOutputStream name( 256 );
835 name << directory << StringRange( texture, path_get_filename_base_end( texture ) );
837 if ( texture_name_ignore( name.c_str() ) ) {
841 if ( !shader_valid( name.c_str() ) ) {
842 globalOutputStream() << "Skipping invalid texture name: [" << name.c_str() << "]\n";
846 // if a texture is already in use to represent a shader, ignore it
847 IShader* shader = QERApp_Shader_ForName( name.c_str() );
851 typedef ConstPointerCaller<char, void(const char*), TextureDirectory_loadTexture> TextureDirectoryLoadTextureCaller;
853 class LoadTexturesByTypeVisitor : public ImageModules::Visitor
855 const char* m_dirstring;
857 LoadTexturesByTypeVisitor( const char* dirstring )
858 : m_dirstring( dirstring ){
861 void visit( const char* minor, const _QERPlugImageTable& table ) const {
862 GlobalFileSystem().forEachFile( m_dirstring, minor, TextureDirectoryLoadTextureCaller( m_dirstring ) );
866 void TextureBrowser_ShowDirectory( TextureBrowser& textureBrowser, const char* directory ){
867 if ( TextureBrowser_showWads() ) {
868 g_TextureBrowser_currentDirectory = directory;
869 TextureBrowser_heightChanged( textureBrowser );
871 Archive* archive = GlobalFileSystem().getArchive( directory );
872 if ( archive != nullptr )
874 LoadShaderVisitor visitor;
875 archive->forEachFile( Archive::VisitorFunc( visitor, Archive::eFiles, 0 ), "textures/" );
877 // Doom3-like dds/ prefix (used by DarkPlaces).
878 archive->forEachFile( Archive::VisitorFunc( visitor, Archive::eFiles, 0 ), "dds/textures/" );
880 else if ( extension_equal_i( path_get_extension( directory ), "wad" ) )
882 globalErrorStream() << "Failed to load " << directory << "\n";
887 g_TextureBrowser_currentDirectory = directory;
888 TextureBrowser_heightChanged( textureBrowser );
890 std::size_t shaders_count;
891 GlobalShaderSystem().foreachShaderName(makeCallback( TextureCategoryLoadShader( directory, shaders_count ) ) );
892 globalOutputStream() << "Showing " << Unsigned( shaders_count ) << " shaders.\n";
894 if ( g_pGameDescription->mGameType != "doom3" ) {
895 // load remaining texture files
897 StringOutputStream dirstring( 64 );
898 dirstring << "textures/" << directory;
901 LoadTexturesByTypeVisitor visitor( dirstring.c_str() );
902 Radiant_getImageModules().foreachModule( visitor );
905 // Doom3-like dds/ prefix (used by DarkPlaces).
907 dirstring << "dds/textures/" << directory;
910 LoadTexturesByTypeVisitor visitor( dirstring.c_str() );
911 Radiant_getImageModules().foreachModule( visitor );
916 // we'll display the newly loaded textures + all the ones already in use
917 TextureBrowser_SetHideUnused( textureBrowser, false );
919 TextureBrowser_updateTitle();
922 void TextureBrowser_ShowTagSearchResult( TextureBrowser& textureBrowser, const char* directory ){
923 g_TextureBrowser_currentDirectory = directory;
924 TextureBrowser_heightChanged( textureBrowser );
926 std::size_t shaders_count;
927 GlobalShaderSystem().foreachShaderName(makeCallback( TextureCategoryLoadShader( directory, shaders_count ) ) );
928 globalOutputStream() << "Showing " << Unsigned( shaders_count ) << " shaders.\n";
930 if ( g_pGameDescription->mGameType != "doom3" ) {
931 // load remaining texture files
932 StringOutputStream dirstring( 64 );
933 dirstring << "textures/" << directory;
936 LoadTexturesByTypeVisitor visitor( dirstring.c_str() );
937 Radiant_getImageModules().foreachModule( visitor );
940 // Doom3-like dds/ prefix (used by DarkPlaces).
942 dirstring << "dds/textures/" << directory;
945 LoadTexturesByTypeVisitor visitor( dirstring.c_str() );
946 Radiant_getImageModules().foreachModule( visitor );
950 // we'll display the newly loaded textures + all the ones already in use
951 TextureBrowser_SetHideUnused( textureBrowser, false );
955 bool TextureBrowser_hideUnused();
957 void TextureBrowser_hideUnusedExport( const Callback<void(bool)> & importer ){
958 importer( TextureBrowser_hideUnused() );
961 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_hideUnusedExport> TextureBrowserHideUnusedExport;
963 void TextureBrowser_showShadersExport( const Callback<void(bool)> & importer ){
964 importer( GlobalTextureBrowser().m_showShaders );
967 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_showShadersExport> TextureBrowserShowShadersExport;
969 void TextureBrowser_showShaderlistOnly( const Callback<void(bool)> & importer ){
970 importer( g_TextureBrowser_shaderlistOnly );
973 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_showShaderlistOnly> TextureBrowserShowShaderlistOnlyExport;
975 void TextureBrowser_fixedSize( const Callback<void(bool)> & importer ){
976 importer( g_TextureBrowser_fixedSize );
979 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_fixedSize> TextureBrowser_FixedSizeExport;
981 void TextureBrowser_filterMissing( const Callback<void(bool)> & importer ){
982 importer( g_TextureBrowser_filterMissing );
985 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_filterMissing> TextureBrowser_filterMissingExport;
987 void TextureBrowser_filterFallback( const Callback<void(bool)> & importer ){
988 importer( g_TextureBrowser_filterFallback );
991 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_filterFallback> TextureBrowser_filterFallbackExport;
993 void TextureBrowser_enableAlpha( const Callback<void(bool)> & importer ){
994 importer( g_TextureBrowser_enableAlpha );
997 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_enableAlpha> TextureBrowser_enableAlphaExport;
999 void TextureBrowser_SetHideUnused( TextureBrowser& textureBrowser, bool hideUnused ){
1001 textureBrowser.m_hideUnused = true;
1005 textureBrowser.m_hideUnused = false;
1008 textureBrowser.m_hideunused_item.update();
1010 TextureBrowser_heightChanged( textureBrowser );
1011 textureBrowser.m_originInvalid = true;
1014 void TextureBrowser_ShowStartupShaders( TextureBrowser& textureBrowser ){
1015 if ( textureBrowser.m_startupShaders == STARTUPSHADERS_COMMON ) {
1016 TextureBrowser_ShowDirectory( textureBrowser, TextureBrowser_getCommonShadersDir() );
1021 //++timo NOTE: this is a mix of Shader module stuff and texture explorer
1022 // it might need to be split in parts or moved out .. dunno
1023 // scroll origin so the specified texture is completely on screen
1024 // if current texture is not displayed, nothing is changed
1025 void TextureBrowser_Focus( TextureBrowser& textureBrowser, const char* name ){
1026 TextureLayout layout;
1027 // scroll origin so the texture is completely on screen
1028 Texture_StartPos( layout );
1030 for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
1032 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
1034 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
1039 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
1040 qtexture_t* q = shader->getTexture();
1045 // we have found when texdef->name and the shader name match
1046 // NOTE: as everywhere else for our comparisons, we are not case sensitive
1047 if ( shader_equal( name, shader->getName() ) ) {
1048 int textureHeight = (int)( q->height * ( (float)textureBrowser.m_textureScale / 100 ) )
1049 + 2 * TextureBrowser_fontHeight( textureBrowser );
1051 int originy = TextureBrowser_getOriginY( textureBrowser );
1052 if ( y > originy ) {
1056 if ( y - textureHeight < originy - textureBrowser.height ) {
1057 originy = ( y - textureHeight ) + textureBrowser.height;
1060 TextureBrowser_setOriginY( textureBrowser, originy );
1066 IShader* Texture_At( TextureBrowser& textureBrowser, int mx, int my ){
1067 my += TextureBrowser_getOriginY( textureBrowser ) - textureBrowser.height;
1069 TextureLayout layout;
1070 Texture_StartPos( layout );
1071 for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
1073 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
1075 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
1080 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
1081 qtexture_t *q = shader->getTexture();
1086 int nWidth = textureBrowser.getTextureWidth( q );
1087 int nHeight = textureBrowser.getTextureHeight( q );
1088 if ( mx > x && mx - x < nWidth
1089 && my < y && y - my < nHeight + TextureBrowser_fontHeight( textureBrowser ) ) {
1104 void SelectTexture( TextureBrowser& textureBrowser, int mx, int my, bool bShift ){
1105 IShader* shader = Texture_At( textureBrowser, mx, my );
1106 if ( shader != 0 ) {
1108 if ( shader->IsDefault() ) {
1109 globalOutputStream() << "ERROR: " << shader->getName() << " is not a shader, it's a texture.\n";
1112 ViewShader( shader->getShaderFileName(), shader->getName() );
1117 TextureBrowser_SetSelectedShader( textureBrowser, shader->getName() );
1118 TextureBrowser_textureSelected( shader->getName() );
1120 if ( !FindTextureDialog_isOpen() && !textureBrowser.m_rmbSelected ) {
1121 UndoableCommand undo( "textureNameSetSelected" );
1122 Select_SetShader( shader->getName() );
1129 ============================================================================
1133 ============================================================================
1136 void TextureBrowser_trackingDelta( int x, int y, unsigned int state, void* data ){
1137 TextureBrowser& textureBrowser = *reinterpret_cast<TextureBrowser*>( data );
1141 if ( state & GDK_SHIFT_MASK ) {
1145 int originy = TextureBrowser_getOriginY( textureBrowser );
1146 originy += y * scale;
1147 TextureBrowser_setOriginY( textureBrowser, originy );
1151 void TextureBrowser_Tracking_MouseDown( TextureBrowser& textureBrowser ){
1152 textureBrowser.m_freezePointer.freeze_pointer( textureBrowser.m_gl_widget, TextureBrowser_trackingDelta, &textureBrowser );
1155 void TextureBrowser_Tracking_MouseUp( TextureBrowser& textureBrowser ){
1156 textureBrowser.m_freezePointer.unfreeze_pointer( textureBrowser.m_gl_widget );
1159 void TextureBrowser_Selection_MouseDown( TextureBrowser& textureBrowser, guint32 flags, int pointx, int pointy ){
1160 SelectTexture( textureBrowser, pointx, textureBrowser.height - 1 - pointy, ( flags & GDK_SHIFT_MASK ) != 0 );
1164 ============================================================================
1168 ============================================================================
1174 TTimo: relying on the shaders list to display the textures
1175 we must query all qtexture_t* to manage and display through the IShaders interface
1176 this allows a plugin to completely override the texture system
1179 void Texture_Draw( TextureBrowser& textureBrowser ){
1180 int originy = TextureBrowser_getOriginY( textureBrowser );
1182 glClearColor( textureBrowser.color_textureback[0],
1183 textureBrowser.color_textureback[1],
1184 textureBrowser.color_textureback[2],
1187 glViewport( 0, 0, textureBrowser.width, textureBrowser.height );
1188 glMatrixMode( GL_PROJECTION );
1191 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1192 glDisable( GL_DEPTH_TEST );
1193 if ( g_TextureBrowser_enableAlpha ) {
1194 glEnable( GL_BLEND );
1195 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1198 glDisable( GL_BLEND );
1200 glOrtho( 0, textureBrowser.width, originy - textureBrowser.height, originy, -100, 100 );
1201 glEnable( GL_TEXTURE_2D );
1203 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1205 int last_y = 0, last_height = 0;
1207 TextureLayout layout;
1208 Texture_StartPos( layout );
1209 for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
1211 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
1213 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
1218 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
1219 qtexture_t *q = shader->getTexture();
1224 int nWidth = textureBrowser.getTextureWidth( q );
1225 int nHeight = textureBrowser.getTextureHeight( q );
1227 if ( y != last_y ) {
1231 last_height = std::max( nHeight, last_height );
1233 // Is this texture visible?
1234 if ( ( y - nHeight - TextureBrowser_fontHeight( textureBrowser ) < originy )
1235 && ( y > originy - textureBrowser.height ) ) {
1237 // if it's the current texture, draw a thick red line, else:
1238 // shaders have a white border, simple textures don't
1239 // if !texture_showinuse: (some textures displayed may not be in use)
1240 // draw an additional square around with 0.5 1 0.5 color
1241 if ( shader_equal( TextureBrowser_GetSelectedShader( textureBrowser ), shader->getName() ) ) {
1243 if ( textureBrowser.m_rmbSelected ) {
1249 glDisable( GL_TEXTURE_2D );
1251 glBegin( GL_LINE_LOOP );
1252 glVertex2i( x - 4,y - TextureBrowser_fontHeight( textureBrowser ) + 4 );
1253 glVertex2i( x - 4,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight - 4 );
1254 glVertex2i( x + 4 + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight - 4 );
1255 glVertex2i( x + 4 + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) + 4 );
1258 glEnable( GL_TEXTURE_2D );
1265 if ( !shader->IsDefault() ) {
1267 glDisable( GL_TEXTURE_2D );
1269 glBegin( GL_LINE_LOOP );
1270 glVertex2i( x - 1,y + 1 - TextureBrowser_fontHeight( textureBrowser ) );
1271 glVertex2i( x - 1,y - nHeight - 1 - TextureBrowser_fontHeight( textureBrowser ) );
1272 glVertex2i( x + 1 + nWidth,y - nHeight - 1 - TextureBrowser_fontHeight( textureBrowser ) );
1273 glVertex2i( x + 1 + nWidth,y + 1 - TextureBrowser_fontHeight( textureBrowser ) );
1275 glEnable( GL_TEXTURE_2D );
1278 // highlight in-use textures
1279 if ( !textureBrowser.m_hideUnused && shader->IsInUse() ) {
1280 glColor3f( 0.5,1,0.5 );
1281 glDisable( GL_TEXTURE_2D );
1282 glBegin( GL_LINE_LOOP );
1283 glVertex2i( x - 3,y + 3 - TextureBrowser_fontHeight( textureBrowser ) );
1284 glVertex2i( x - 3,y - nHeight - 3 - TextureBrowser_fontHeight( textureBrowser ) );
1285 glVertex2i( x + 3 + nWidth,y - nHeight - 3 - TextureBrowser_fontHeight( textureBrowser ) );
1286 glVertex2i( x + 3 + nWidth,y + 3 - TextureBrowser_fontHeight( textureBrowser ) );
1288 glEnable( GL_TEXTURE_2D );
1292 // draw checkerboard for transparent textures
1293 if ( g_TextureBrowser_enableAlpha )
1295 glDisable( GL_TEXTURE_2D );
1296 glBegin( GL_QUADS );
1297 int font_height = TextureBrowser_fontHeight( textureBrowser );
1298 for ( int i = 0; i < nHeight; i += 8 )
1300 for ( int j = 0; j < nWidth; j += 8 )
1302 unsigned char color = (i + j) / 8 % 2 ? 0x66 : 0x99;
1303 glColor3ub( color, color, color );
1305 int right = std::min(j+8, nWidth);
1307 int bottom = std::min(i+8, nHeight);
1308 glVertex2i(x + right, y - nHeight - font_height + top);
1309 glVertex2i(x + left, y - nHeight - font_height + top);
1310 glVertex2i(x + left, y - nHeight - font_height + bottom);
1311 glVertex2i(x + right, y - nHeight - font_height + bottom);
1315 glEnable( GL_TEXTURE_2D );
1319 glBindTexture( GL_TEXTURE_2D, q->texture_number );
1320 GlobalOpenGL_debugAssertNoErrors();
1322 glBegin( GL_QUADS );
1323 glTexCoord2i( 0,0 );
1324 glVertex2i( x,y - TextureBrowser_fontHeight( textureBrowser ) );
1325 glTexCoord2i( 1,0 );
1326 glVertex2i( x + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) );
1327 glTexCoord2i( 1,1 );
1328 glVertex2i( x + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight );
1329 glTexCoord2i( 0,1 );
1330 glVertex2i( x,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight );
1333 // draw the texture name
1334 glDisable( GL_TEXTURE_2D );
1337 glRasterPos2i( x, y - TextureBrowser_fontHeight( textureBrowser ) + 5 );
1339 // don't draw the directory name
1340 const char* name = shader->getName();
1341 name += strlen( name );
1342 while ( name != shader->getName() && *( name - 1 ) != '/' && *( name - 1 ) != '\\' )
1345 GlobalOpenGL().drawString( name );
1346 glEnable( GL_TEXTURE_2D );
1349 //int totalHeight = abs(y) + last_height + TextureBrowser_fontHeight(textureBrowser) + 4;
1353 // reset the current texture
1354 glBindTexture( GL_TEXTURE_2D, 0 );
1358 void TextureBrowser_queueDraw( TextureBrowser& textureBrowser ){
1359 if ( textureBrowser.m_gl_widget ) {
1360 gtk_widget_queue_draw( textureBrowser.m_gl_widget );
1365 void TextureBrowser_setScale( TextureBrowser& textureBrowser, std::size_t scale ){
1366 textureBrowser.m_textureScale = scale;
1368 TextureBrowser_queueDraw( textureBrowser );
1371 void TextureBrowser_setUniformSize( TextureBrowser& textureBrowser, std::size_t scale ){
1372 textureBrowser.m_uniformTextureSize = scale;
1374 TextureBrowser_queueDraw( textureBrowser );
1378 void TextureBrowser_MouseWheel( TextureBrowser& textureBrowser, bool bUp ){
1379 int originy = TextureBrowser_getOriginY( textureBrowser );
1382 originy += int(textureBrowser.m_mouseWheelScrollIncrement);
1386 originy -= int(textureBrowser.m_mouseWheelScrollIncrement);
1389 TextureBrowser_setOriginY( textureBrowser, originy );
1392 XmlTagBuilder TagBuilder;
1400 void BuildStoreAssignedTags( ui::ListStore store, const char* shader, TextureBrowser* textureBrowser ){
1405 std::vector<CopiedString> assigned_tags;
1406 TagBuilder.GetShaderTags( shader, assigned_tags );
1408 for ( size_t i = 0; i < assigned_tags.size(); i++ )
1410 store.append(TAG_COLUMN, assigned_tags[i].c_str());
1414 void BuildStoreAvailableTags( ui::ListStore storeAvailable,
1415 ui::ListStore storeAssigned,
1416 const std::set<CopiedString>& allTags,
1417 TextureBrowser* textureBrowser ){
1418 GtkTreeIter iterAssigned;
1419 GtkTreeIter iterAvailable;
1420 std::set<CopiedString>::const_iterator iterAll;
1421 gchar* tag_assigned;
1423 storeAvailable.clear();
1425 bool row = gtk_tree_model_get_iter_first(storeAssigned, &iterAssigned ) != 0;
1427 if ( !row ) { // does the shader have tags assigned?
1428 for ( iterAll = allTags.begin(); iterAll != allTags.end(); ++iterAll )
1430 storeAvailable.append(TAG_COLUMN, (*iterAll).c_str());
1435 while ( row ) // available tags = all tags - assigned tags
1437 gtk_tree_model_get(storeAssigned, &iterAssigned, TAG_COLUMN, &tag_assigned, -1 );
1439 for ( iterAll = allTags.begin(); iterAll != allTags.end(); ++iterAll )
1441 if ( strcmp( (char*)tag_assigned, ( *iterAll ).c_str() ) != 0 ) {
1442 storeAvailable.append(TAG_COLUMN, (*iterAll).c_str());
1446 row = gtk_tree_model_iter_next(storeAssigned, &iterAssigned ) != 0;
1449 gtk_tree_model_get(storeAssigned, &iterAssigned, TAG_COLUMN, &tag_assigned, -1 );
1457 gboolean TextureBrowser_button_press( ui::Widget widget, GdkEventButton* event, TextureBrowser* textureBrowser ){
1458 if ( event->type == GDK_BUTTON_PRESS ) {
1459 if ( event->button == 3 ) {
1460 if ( textureBrowser->m_tags ) {
1461 textureBrowser->m_rmbSelected = true;
1462 TextureBrowser_Selection_MouseDown( *textureBrowser, event->state, static_cast<int>( event->x ), static_cast<int>( event->y ) );
1464 BuildStoreAssignedTags( textureBrowser->m_assigned_store, textureBrowser->shader.c_str(), textureBrowser );
1465 BuildStoreAvailableTags( textureBrowser->m_available_store, textureBrowser->m_assigned_store, textureBrowser->m_all_tags, textureBrowser );
1466 textureBrowser->m_heightChanged = true;
1467 textureBrowser->m_tag_frame.show();
1471 TextureBrowser_Focus( *textureBrowser, textureBrowser->shader.c_str() );
1475 TextureBrowser_Tracking_MouseDown( *textureBrowser );
1478 else if ( event->button == 1 ) {
1479 TextureBrowser_Selection_MouseDown( *textureBrowser, event->state, static_cast<int>( event->x ), static_cast<int>( event->y ) );
1481 if ( textureBrowser->m_tags ) {
1482 textureBrowser->m_rmbSelected = false;
1483 textureBrowser->m_tag_frame.hide();
1490 gboolean TextureBrowser_button_release( ui::Widget widget, GdkEventButton* event, TextureBrowser* textureBrowser ){
1491 if ( event->type == GDK_BUTTON_RELEASE ) {
1492 if ( event->button == 3 ) {
1493 if ( !textureBrowser->m_tags ) {
1494 TextureBrowser_Tracking_MouseUp( *textureBrowser );
1501 gboolean TextureBrowser_motion( ui::Widget widget, GdkEventMotion *event, TextureBrowser* textureBrowser ){
1505 gboolean TextureBrowser_scroll( ui::Widget widget, GdkEventScroll* event, TextureBrowser* textureBrowser ){
1506 if ( event->direction == GDK_SCROLL_UP ) {
1507 TextureBrowser_MouseWheel( *textureBrowser, true );
1509 else if ( event->direction == GDK_SCROLL_DOWN ) {
1510 TextureBrowser_MouseWheel( *textureBrowser, false );
1515 void TextureBrowser_scrollChanged( void* data, gdouble value ){
1516 //globalOutputStream() << "vertical scroll\n";
1517 TextureBrowser_setOriginY( *reinterpret_cast<TextureBrowser*>( data ), -(int)value );
1520 static void TextureBrowser_verticalScroll(ui::Adjustment adjustment, TextureBrowser* textureBrowser ){
1521 textureBrowser->m_scrollAdjustment.value_changed( gtk_adjustment_get_value(adjustment) );
1524 void TextureBrowser_updateScroll( TextureBrowser& textureBrowser ){
1525 if ( textureBrowser.m_showTextureScrollbar ) {
1526 int totalHeight = TextureBrowser_TotalHeight( textureBrowser );
1528 totalHeight = std::max( totalHeight, textureBrowser.height );
1530 auto vadjustment = gtk_range_get_adjustment( GTK_RANGE( textureBrowser.m_texture_scroll ) );
1532 gtk_adjustment_set_value(vadjustment, -TextureBrowser_getOriginY( textureBrowser ));
1533 gtk_adjustment_set_page_size(vadjustment, textureBrowser.height);
1534 gtk_adjustment_set_page_increment(vadjustment, textureBrowser.height / 2);
1535 gtk_adjustment_set_step_increment(vadjustment, 20);
1536 gtk_adjustment_set_lower(vadjustment, 0);
1537 gtk_adjustment_set_upper(vadjustment, totalHeight);
1539 g_signal_emit_by_name( G_OBJECT( vadjustment ), "changed" );
1543 gboolean TextureBrowser_size_allocate( ui::Widget widget, GtkAllocation* allocation, TextureBrowser* textureBrowser ){
1544 textureBrowser->width = allocation->width;
1545 textureBrowser->height = allocation->height;
1546 TextureBrowser_heightChanged( *textureBrowser );
1547 textureBrowser->m_originInvalid = true;
1548 TextureBrowser_queueDraw( *textureBrowser );
1552 void TextureBrowser_redraw( TextureBrowser* textureBrowser ){
1553 if ( glwidget_make_current( textureBrowser->m_gl_widget ) != FALSE ) {
1554 GlobalOpenGL_debugAssertNoErrors();
1555 TextureBrowser_evaluateHeight( *textureBrowser );
1556 Texture_Draw( *textureBrowser );
1557 GlobalOpenGL_debugAssertNoErrors();
1558 glwidget_swap_buffers( textureBrowser->m_gl_widget );
1562 gboolean TextureBrowser_expose( ui::Widget widget, GdkEventExpose* event, TextureBrowser* textureBrowser ){
1563 TextureBrowser_redraw( textureBrowser );
1567 TextureBrowser& GlobalTextureBrowser(){
1568 static TextureBrowser textureBrowser;
1569 return textureBrowser;
1572 bool TextureBrowser_hideUnused(){
1573 return GlobalTextureBrowser().m_hideUnused;
1576 void TextureBrowser_ToggleHideUnused(){
1577 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1578 if ( textureBrowser.m_hideUnused ) {
1579 TextureBrowser_SetHideUnused( textureBrowser, false );
1583 TextureBrowser_SetHideUnused( textureBrowser, true );
1587 const char* TextureGroups_transformDirName( const char* dirName, StringOutputStream *archiveName )
1589 if ( TextureBrowser_showWads() ) {
1590 archiveName->clear();
1591 *archiveName << StringRange( path_get_filename_start( dirName ), path_get_filename_base_end( dirName ) ) \
1592 << "." << path_get_extension( dirName );
1593 return archiveName->c_str();
1598 void TextureGroups_constructTreeModel( TextureGroups groups, ui::TreeStore store ){
1599 // put the information from the old textures menu into a treeview
1600 GtkTreeIter iter, child;
1602 TextureGroups::const_iterator i = groups.begin();
1603 while ( i != groups.end() )
1605 StringOutputStream archiveName;
1606 StringOutputStream nextArchiveName;
1607 const char* dirName = TextureGroups_transformDirName( ( *i ).c_str(), &archiveName );
1609 const char* firstUnderscore = strchr( dirName, '_' );
1610 StringRange dirRoot( dirName, ( firstUnderscore == 0 ) ? dirName : firstUnderscore + 1 );
1612 TextureGroups::const_iterator next = i;
1615 if ( firstUnderscore != 0
1616 && next != groups.end()
1617 && string_equal_start( TextureGroups_transformDirName( ( *next ).c_str(), &nextArchiveName ), dirRoot ) ) {
1618 gtk_tree_store_append( store, &iter, NULL );
1619 gtk_tree_store_set( store, &iter, 0, CopiedString( StringRange( dirName, firstUnderscore ) ).c_str(), -1 );
1622 while ( i != groups.end() && string_equal_start( TextureGroups_transformDirName( ( *i ).c_str(), &nextArchiveName ), dirRoot ) )
1624 gtk_tree_store_append( store, &child, &iter );
1625 gtk_tree_store_set( store, &child, 0, TextureGroups_transformDirName( ( *i ).c_str(), &nextArchiveName ), -1 );
1631 gtk_tree_store_append( store, &iter, NULL );
1632 gtk_tree_store_set( store, &iter, 0, dirName, -1 );
1638 TextureGroups TextureGroups_constructTreeView(){
1639 TextureGroups groups;
1641 if ( TextureBrowser_showWads() ) {
1642 GlobalFileSystem().forEachArchive( TextureGroupsAddWadCaller( groups ) );
1646 // scan texture dirs and pak files only if not restricting to shaderlist
1647 if ( g_pGameDescription->mGameType != "doom3" && !g_TextureBrowser_shaderlistOnly ) {
1648 GlobalFileSystem().forEachDirectory( "textures/", TextureGroupsAddDirectoryCaller( groups ) );
1651 GlobalShaderSystem().foreachShaderName( TextureGroupsAddShaderCaller( groups ) );
1657 void TextureBrowser_constructTreeStore(){
1658 TextureGroups groups = TextureGroups_constructTreeView();
1659 auto store = ui::TreeStore::from(gtk_tree_store_new( 1, G_TYPE_STRING ));
1660 TextureGroups_constructTreeModel( groups, store );
1662 gtk_tree_view_set_model(GlobalTextureBrowser().m_treeViewTree, store);
1664 g_object_unref( G_OBJECT( store ) );
1667 void TextureBrowser_constructTreeStoreTags(){
1668 TextureGroups groups;
1669 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1670 auto store = ui::TreeStore::from(gtk_tree_store_new( 1, G_TYPE_STRING ));
1671 auto model = GlobalTextureBrowser().m_all_tags_list;
1673 gtk_tree_view_set_model(GlobalTextureBrowser().m_treeViewTags, model );
1675 g_object_unref( G_OBJECT( store ) );
1678 void TreeView_onRowActivated( ui::TreeView treeview, ui::TreePath path, ui::TreeViewColumn col, gpointer userdata ){
1681 auto model = gtk_tree_view_get_model(treeview );
1683 if ( gtk_tree_model_get_iter( model, &iter, path ) ) {
1684 gchar dirName[1024];
1687 gtk_tree_model_get( model, &iter, 0, &buffer, -1 );
1688 strcpy( dirName, buffer );
1691 GlobalTextureBrowser().m_searchedTags = false;
1693 if ( !TextureBrowser_showWads() ) {
1694 strcat( dirName, "/" );
1697 ScopeDisableScreenUpdates disableScreenUpdates( dirName, "Loading Textures" );
1698 TextureBrowser_ShowDirectory( GlobalTextureBrowser(), dirName );
1699 TextureBrowser_queueDraw( GlobalTextureBrowser() );
1703 void TextureBrowser_createTreeViewTree(){
1704 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1705 gtk_tree_view_set_enable_search(textureBrowser.m_treeViewTree, FALSE );
1707 gtk_tree_view_set_headers_visible(textureBrowser.m_treeViewTree, FALSE );
1708 textureBrowser.m_treeViewTree.connect( "row-activated", (GCallback) TreeView_onRowActivated, NULL );
1710 auto renderer = ui::CellRendererText(ui::New);
1711 gtk_tree_view_insert_column_with_attributes(textureBrowser.m_treeViewTree, -1, "", renderer, "text", 0, NULL );
1713 TextureBrowser_constructTreeStore();
1716 void TextureBrowser_addTag();
1718 void TextureBrowser_renameTag();
1720 void TextureBrowser_deleteTag();
1722 void TextureBrowser_createContextMenu( ui::Widget treeview, GdkEventButton *event ){
1723 ui::Widget menu = ui::Menu(ui::New);
1725 ui::Widget menuitem = ui::MenuItem( "Add tag" );
1726 menuitem.connect( "activate", (GCallback)TextureBrowser_addTag, treeview );
1727 gtk_menu_shell_append( GTK_MENU_SHELL( menu ), menuitem );
1729 menuitem = ui::MenuItem( "Rename tag" );
1730 menuitem.connect( "activate", (GCallback)TextureBrowser_renameTag, treeview );
1731 gtk_menu_shell_append( GTK_MENU_SHELL( menu ), menuitem );
1733 menuitem = ui::MenuItem( "Delete tag" );
1734 menuitem.connect( "activate", (GCallback)TextureBrowser_deleteTag, treeview );
1735 gtk_menu_shell_append( GTK_MENU_SHELL( menu ), menuitem );
1737 gtk_widget_show_all( menu );
1739 gtk_menu_popup( GTK_MENU( menu ), NULL, NULL, NULL, NULL,
1740 ( event != NULL ) ? event->button : 0,
1741 gdk_event_get_time( (GdkEvent*)event ) );
1744 gboolean TreeViewTags_onButtonPressed( ui::TreeView treeview, GdkEventButton *event ){
1745 if ( event->type == GDK_BUTTON_PRESS && event->button == 3 ) {
1747 auto selection = gtk_tree_view_get_selection(treeview );
1749 if ( gtk_tree_view_get_path_at_pos(treeview, event->x, event->y, &path, NULL, NULL, NULL ) ) {
1750 gtk_tree_selection_unselect_all( selection );
1751 gtk_tree_selection_select_path( selection, path );
1752 gtk_tree_path_free( path );
1755 TextureBrowser_createContextMenu( treeview, event );
1761 void TextureBrowser_createTreeViewTags(){
1762 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1763 textureBrowser.m_treeViewTags = ui::TreeView(ui::New);
1764 gtk_tree_view_set_enable_search(textureBrowser.m_treeViewTags, FALSE );
1766 textureBrowser.m_treeViewTags.connect( "button-press-event", (GCallback)TreeViewTags_onButtonPressed, NULL );
1768 gtk_tree_view_set_headers_visible(textureBrowser.m_treeViewTags, FALSE );
1770 auto renderer = ui::CellRendererText(ui::New);
1771 gtk_tree_view_insert_column_with_attributes(textureBrowser.m_treeViewTags, -1, "", renderer, "text", 0, NULL );
1773 TextureBrowser_constructTreeStoreTags();
1776 ui::MenuItem TextureBrowser_constructViewMenu( ui::Menu menu ){
1777 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1778 ui::MenuItem textures_menu_item = ui::MenuItem(new_sub_menu_item_with_mnemonic( "_View" ));
1780 if ( g_Layout_enableDetachableMenus.m_value ) {
1781 menu_tearoff( menu );
1784 create_check_menu_item_with_mnemonic( menu, "Hide _Unused", "ShowInUse" );
1785 if ( string_empty( g_pGameDescription->getKeyValue( "show_wads" ) ) ) {
1786 create_check_menu_item_with_mnemonic( menu, "Hide Image Missing", "FilterMissing" );
1789 // hide notex and shadernotex on texture browser: no one wants to apply them
1790 create_check_menu_item_with_mnemonic( menu, "Hide Fallback", "FilterFallback" );
1792 menu_separator( menu );
1794 create_menu_item_with_mnemonic( menu, "Show All", "ShowAllTextures" );
1796 // we always want to show shaders but don't want a "Show Shaders" menu for doom3 and .wad file games
1797 if ( g_pGameDescription->mGameType == "doom3" || TextureBrowser_showWads() ) {
1798 textureBrowser.m_showShaders = true;
1802 create_check_menu_item_with_mnemonic( menu, "Show shaders", "ToggleShowShaders" );
1805 if ( g_pGameDescription->mGameType != "doom3" && string_empty( g_pGameDescription->getKeyValue( "show_wads" ) ) ) {
1806 create_check_menu_item_with_mnemonic( menu, "Shaders Only", "ToggleShowShaderlistOnly" );
1808 if ( textureBrowser.m_tags ) {
1809 create_menu_item_with_mnemonic( menu, "Show Untagged", "ShowUntagged" );
1812 menu_separator( menu );
1813 create_check_menu_item_with_mnemonic( menu, "Fixed Size", "FixedSize" );
1814 create_check_menu_item_with_mnemonic( menu, "Transparency", "EnableAlpha" );
1816 if ( string_empty( g_pGameDescription->getKeyValue( "show_wads" ) ) ) {
1817 menu_separator( menu );
1818 textureBrowser.m_shader_info_item = ui::Widget(create_menu_item_with_mnemonic( menu, "Shader Info", "ShaderInfo" ));
1819 gtk_widget_set_sensitive( textureBrowser.m_shader_info_item, FALSE );
1823 return textures_menu_item;
1826 ui::MenuItem TextureBrowser_constructToolsMenu( ui::Menu menu ){
1827 ui::MenuItem textures_menu_item = ui::MenuItem(new_sub_menu_item_with_mnemonic( "_Tools" ));
1829 if ( g_Layout_enableDetachableMenus.m_value ) {
1830 menu_tearoff( menu );
1833 create_menu_item_with_mnemonic( menu, "Flush & Reload Shaders", "RefreshShaders" );
1834 create_menu_item_with_mnemonic( menu, "Find / Replace...", "FindReplaceTextures" );
1836 return textures_menu_item;
1839 ui::MenuItem TextureBrowser_constructTagsMenu( ui::Menu menu ){
1840 ui::MenuItem textures_menu_item = ui::MenuItem(new_sub_menu_item_with_mnemonic( "T_ags" ));
1842 if ( g_Layout_enableDetachableMenus.m_value ) {
1843 menu_tearoff( menu );
1846 create_menu_item_with_mnemonic( menu, "Add tag", "AddTag" );
1847 create_menu_item_with_mnemonic( menu, "Rename tag", "RenameTag" );
1848 create_menu_item_with_mnemonic( menu, "Delete tag", "DeleteTag" );
1849 menu_separator( menu );
1850 create_menu_item_with_mnemonic( menu, "Copy tags from selected", "CopyTag" );
1851 create_menu_item_with_mnemonic( menu, "Paste tags to selected", "PasteTag" );
1853 return textures_menu_item;
1856 gboolean TextureBrowser_tagMoveHelper( ui::TreeModel model, ui::TreePath path, GtkTreeIter* iter, GSList** selected ){
1857 g_assert( selected != NULL );
1859 auto rowref = gtk_tree_row_reference_new( model, path );
1860 *selected = g_slist_append( *selected, rowref );
1865 void TextureBrowser_assignTags(){
1866 GSList* selected = NULL;
1868 gchar* tag_assigned;
1869 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1871 auto selection = gtk_tree_view_get_selection(textureBrowser.m_available_tree );
1873 gtk_tree_selection_selected_foreach( selection, (GtkTreeSelectionForeachFunc)TextureBrowser_tagMoveHelper, &selected );
1875 if ( selected != NULL ) {
1876 for ( node = selected; node != NULL; node = node->next )
1878 auto path = gtk_tree_row_reference_get_path( (GtkTreeRowReference*)node->data );
1883 if ( gtk_tree_model_get_iter(textureBrowser.m_available_store, &iter, path ) ) {
1884 gtk_tree_model_get(textureBrowser.m_available_store, &iter, TAG_COLUMN, &tag_assigned, -1 );
1885 if ( !TagBuilder.CheckShaderTag( textureBrowser.shader.c_str() ) ) {
1886 // create a custom shader/texture entry
1887 IShader* ishader = QERApp_Shader_ForName( textureBrowser.shader.c_str() );
1888 CopiedString filename = ishader->getShaderFileName();
1890 if ( filename.empty() ) {
1892 TagBuilder.AddShaderNode( textureBrowser.shader.c_str(), CUSTOM, TEXTURE );
1896 TagBuilder.AddShaderNode( textureBrowser.shader.c_str(), CUSTOM, SHADER );
1900 TagBuilder.AddShaderTag( textureBrowser.shader.c_str(), (char*)tag_assigned, TAG );
1902 gtk_list_store_remove( textureBrowser.m_available_store, &iter );
1903 textureBrowser.m_assigned_store.append(TAG_COLUMN, tag_assigned);
1908 g_slist_foreach( selected, (GFunc)gtk_tree_row_reference_free, NULL );
1911 TagBuilder.SaveXmlDoc();
1913 g_slist_free( selected );
1916 void TextureBrowser_removeTags(){
1917 GSList* selected = NULL;
1920 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1922 auto selection = gtk_tree_view_get_selection(textureBrowser.m_assigned_tree );
1924 gtk_tree_selection_selected_foreach( selection, (GtkTreeSelectionForeachFunc)TextureBrowser_tagMoveHelper, &selected );
1926 if ( selected != NULL ) {
1927 for ( node = selected; node != NULL; node = node->next )
1929 auto path = gtk_tree_row_reference_get_path( (GtkTreeRowReference*)node->data );
1934 if ( gtk_tree_model_get_iter(textureBrowser.m_assigned_store, &iter, path ) ) {
1935 gtk_tree_model_get(textureBrowser.m_assigned_store, &iter, TAG_COLUMN, &tag, -1 );
1936 TagBuilder.DeleteShaderTag( textureBrowser.shader.c_str(), tag );
1937 gtk_list_store_remove( textureBrowser.m_assigned_store, &iter );
1942 g_slist_foreach( selected, (GFunc)gtk_tree_row_reference_free, NULL );
1944 // Update the "available tags list"
1945 BuildStoreAvailableTags( textureBrowser.m_available_store, textureBrowser.m_assigned_store, textureBrowser.m_all_tags, &textureBrowser );
1948 TagBuilder.SaveXmlDoc();
1950 g_slist_free( selected );
1953 void TextureBrowser_buildTagList(){
1954 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1955 textureBrowser.m_all_tags_list.clear();
1957 std::set<CopiedString>::iterator iter;
1959 for ( iter = textureBrowser.m_all_tags.begin(); iter != textureBrowser.m_all_tags.end(); ++iter )
1961 textureBrowser.m_all_tags_list.append(TAG_COLUMN, (*iter).c_str());
1965 void TextureBrowser_searchTags(){
1966 GSList* selected = NULL;
1970 char tags_searched[256];
1971 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1973 auto selection = gtk_tree_view_get_selection(textureBrowser.m_treeViewTags );
1975 gtk_tree_selection_selected_foreach( selection, (GtkTreeSelectionForeachFunc)TextureBrowser_tagMoveHelper, &selected );
1977 if ( selected != NULL ) {
1978 strcpy( buffer, "/root/*/*[tag='" );
1979 strcpy( tags_searched, "[TAGS] " );
1981 for ( node = selected; node != NULL; node = node->next )
1983 auto path = gtk_tree_row_reference_get_path( (GtkTreeRowReference*)node->data );
1988 if ( gtk_tree_model_get_iter(textureBrowser.m_all_tags_list, &iter, path ) ) {
1989 gtk_tree_model_get(textureBrowser.m_all_tags_list, &iter, TAG_COLUMN, &tag, -1 );
1991 strcat( buffer, tag );
1992 strcat( tags_searched, tag );
1993 if ( node != g_slist_last( node ) ) {
1994 strcat( buffer, "' and tag='" );
1995 strcat( tags_searched, ", " );
2001 strcat( buffer, "']" );
2003 g_slist_foreach( selected, (GFunc)gtk_tree_row_reference_free, NULL );
2005 textureBrowser.m_found_shaders.clear(); // delete old list
2006 TagBuilder.TagSearch( buffer, textureBrowser.m_found_shaders );
2008 if ( !textureBrowser.m_found_shaders.empty() ) { // found something
2009 size_t shaders_found = textureBrowser.m_found_shaders.size();
2011 globalOutputStream() << "Found " << (unsigned int)shaders_found << " textures and shaders with " << tags_searched << "\n";
2012 ScopeDisableScreenUpdates disableScreenUpdates( "Searching...", "Loading Textures" );
2014 std::set<CopiedString>::iterator iter;
2016 for ( iter = textureBrowser.m_found_shaders.begin(); iter != textureBrowser.m_found_shaders.end(); iter++ )
2018 std::string path = ( *iter ).c_str();
2019 size_t pos = path.find_last_of( "/", path.size() );
2020 std::string name = path.substr( pos + 1, path.size() );
2021 path = path.substr( 0, pos + 1 );
2022 TextureDirectory_loadTexture( path.c_str(), name.c_str() );
2025 textureBrowser.m_searchedTags = true;
2026 g_TextureBrowser_currentDirectory = tags_searched;
2028 textureBrowser.m_nTotalHeight = 0;
2029 TextureBrowser_setOriginY( textureBrowser, 0 );
2030 TextureBrowser_heightChanged( textureBrowser );
2031 TextureBrowser_updateTitle();
2033 g_slist_free( selected );
2036 void TextureBrowser_toggleSearchButton(){
2037 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2038 gint page = gtk_notebook_get_current_page( GTK_NOTEBOOK( textureBrowser.m_tag_notebook ) );
2040 if ( page == 0 ) { // tag page
2041 gtk_widget_show_all( textureBrowser.m_search_button );
2044 textureBrowser.m_search_button.hide();
2048 void TextureBrowser_constructTagNotebook(){
2049 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2050 textureBrowser.m_tag_notebook = ui::Widget::from(gtk_notebook_new());
2051 ui::Widget labelTags = ui::Label( "Tags" );
2052 ui::Widget labelTextures = ui::Label( "Textures" );
2054 gtk_notebook_append_page( GTK_NOTEBOOK( textureBrowser.m_tag_notebook ), textureBrowser.m_scr_win_tree, labelTextures );
2055 gtk_notebook_append_page( GTK_NOTEBOOK( textureBrowser.m_tag_notebook ), textureBrowser.m_scr_win_tags, labelTags );
2057 textureBrowser.m_tag_notebook.connect( "switch-page", G_CALLBACK( TextureBrowser_toggleSearchButton ), NULL );
2059 gtk_widget_show_all( textureBrowser.m_tag_notebook );
2062 void TextureBrowser_constructSearchButton(){
2063 auto image = ui::Widget::from(gtk_image_new_from_stock( GTK_STOCK_FIND, GTK_ICON_SIZE_SMALL_TOOLBAR ));
2064 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2065 textureBrowser.m_search_button = ui::Button(ui::New);
2066 textureBrowser.m_search_button.connect( "clicked", G_CALLBACK( TextureBrowser_searchTags ), NULL );
2067 gtk_widget_set_tooltip_text(textureBrowser.m_search_button, "Search with selected tags");
2068 textureBrowser.m_search_button.add(image);
2071 void TextureBrowser_checkTagFile(){
2072 const char SHADERTAG_FILE[] = "shadertags.xml";
2073 CopiedString default_filename, rc_filename;
2074 StringOutputStream stream( 256 );
2075 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2077 stream << LocalRcPath_get();
2078 stream << SHADERTAG_FILE;
2079 rc_filename = stream.c_str();
2081 if ( file_exists( rc_filename.c_str() ) ) {
2082 textureBrowser.m_tags = TagBuilder.OpenXmlDoc( rc_filename.c_str() );
2084 if ( textureBrowser.m_tags ) {
2085 globalOutputStream() << "Loading tag file " << rc_filename.c_str() << ".\n";
2090 // load default tagfile
2092 stream << g_pGameDescription->mGameToolsPath.c_str();
2093 stream << SHADERTAG_FILE;
2094 default_filename = stream.c_str();
2096 if ( file_exists( default_filename.c_str() ) ) {
2097 textureBrowser.m_tags = TagBuilder.OpenXmlDoc( default_filename.c_str(), rc_filename.c_str() );
2099 if ( textureBrowser.m_tags ) {
2100 globalOutputStream() << "Loading default tag file " << default_filename.c_str() << ".\n";
2105 globalErrorStream() << "Unable to find default tag file " << default_filename.c_str() << ". No tag support.\n";
2110 void TextureBrowser_SetNotex(){
2111 IShader* notex = QERApp_Shader_ForName( DEFAULT_NOTEX_NAME );
2112 IShader* shadernotex = QERApp_Shader_ForName( DEFAULT_SHADERNOTEX_NAME );
2114 g_notex = notex->getTexture()->name;
2116 g_shadernotex = shadernotex->getTexture()->name;
2119 shadernotex->DecRef();
2122 static bool isGLWidgetConstructed = false;
2123 static bool isWindowConstructed = false;
2125 void TextureBrowser_constructGLWidget(){
2126 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2127 textureBrowser.m_gl_widget = glwidget_new( FALSE );
2128 g_object_ref( textureBrowser.m_gl_widget._handle );
2130 gtk_widget_set_events( textureBrowser.m_gl_widget, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK );
2131 gtk_widget_set_can_focus( textureBrowser.m_gl_widget, true );
2133 textureBrowser.m_sizeHandler = textureBrowser.m_gl_widget.connect( "size_allocate", G_CALLBACK( TextureBrowser_size_allocate ), &textureBrowser );
2134 textureBrowser.m_exposeHandler = textureBrowser.m_gl_widget.on_render( G_CALLBACK( TextureBrowser_expose ), &textureBrowser );
2136 textureBrowser.m_gl_widget.connect( "button_press_event", G_CALLBACK( TextureBrowser_button_press ), &textureBrowser );
2137 textureBrowser.m_gl_widget.connect( "button_release_event", G_CALLBACK( TextureBrowser_button_release ), &textureBrowser );
2138 textureBrowser.m_gl_widget.connect( "motion_notify_event", G_CALLBACK( TextureBrowser_motion ), &textureBrowser );
2139 textureBrowser.m_gl_widget.connect( "scroll_event", G_CALLBACK( TextureBrowser_scroll ), &textureBrowser );
2141 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
2142 textureBrowser.m_hframe.pack_start( textureBrowser.m_gl_widget, TRUE, TRUE, 0 );
2143 #else // !WORKAROUND_MACOS_GTK2_GLWIDGET
2144 textureBrowser.m_frame.pack_start( textureBrowser.m_gl_widget, TRUE, TRUE, 0 );
2145 #endif // !WORKAROUND_MACOS_GTK2_GLWIDGET
2147 textureBrowser.m_gl_widget.show();
2149 isGLWidgetConstructed = true;
2152 ui::Widget TextureBrowser_constructWindow( ui::Window toplevel ){
2153 // The gl_widget and the tag assignment frame should be packed into a GtkVPaned with the slider
2154 // position stored in local.pref. gtk_paned_get_position() and gtk_paned_set_position() don't
2155 // seem to work in gtk 2.4 and the arrow buttons don't handle GTK_FILL, so here's another thing
2156 // for the "once-the-gtk-libs-are-updated-TODO-list" :x
2157 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2159 TextureBrowser_checkTagFile();
2160 TextureBrowser_SetNotex();
2162 GlobalShaderSystem().setActiveShadersChangedNotify( ReferenceCaller<TextureBrowser, void(), TextureBrowser_activeShadersChanged>( textureBrowser ) );
2164 textureBrowser.m_parent = toplevel;
2166 auto table = ui::Table(3, 3, FALSE);
2167 auto vbox = ui::VBox(FALSE, 0);
2168 table.attach(vbox, {0, 1, 1, 3}, {GTK_FILL, GTK_FILL});
2171 ui::Widget menu_bar{ui::null};
2174 menu_bar = ui::Widget::from(gtk_menu_bar_new());
2175 auto menu_view = ui::Menu(ui::New);
2176 auto view_item = TextureBrowser_constructViewMenu( menu_view );
2177 gtk_menu_item_set_submenu( GTK_MENU_ITEM( view_item ), menu_view );
2178 gtk_menu_shell_append( GTK_MENU_SHELL( menu_bar ), view_item );
2180 auto menu_tools = ui::Menu(ui::New);
2181 auto tools_item = TextureBrowser_constructToolsMenu( menu_tools );
2182 gtk_menu_item_set_submenu( GTK_MENU_ITEM( tools_item ), menu_tools );
2183 gtk_menu_shell_append( GTK_MENU_SHELL( menu_bar ), tools_item );
2185 table.attach(menu_bar, {0, 3, 0, 1}, {GTK_FILL, GTK_SHRINK});
2188 { // Texture TreeView
2189 textureBrowser.m_scr_win_tree = ui::ScrolledWindow(ui::New);
2190 gtk_container_set_border_width( GTK_CONTAINER( textureBrowser.m_scr_win_tree ), 0 );
2192 // vertical only scrolling for treeview
2193 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( textureBrowser.m_scr_win_tree ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2195 textureBrowser.m_scr_win_tree.show();
2197 TextureBrowser_createTreeViewTree();
2199 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( textureBrowser.m_scr_win_tree ), textureBrowser.m_treeViewTree );
2200 textureBrowser.m_treeViewTree.show();
2202 { // gl_widget scrollbar
2203 auto w = ui::Widget::from(gtk_vscrollbar_new( ui::Adjustment( 0,0,0,1,1,0 ) ));
2204 table.attach(w, {2, 3, 1, 2}, {GTK_SHRINK, GTK_FILL});
2206 textureBrowser.m_texture_scroll = w;
2208 auto vadjustment = ui::Adjustment::from(gtk_range_get_adjustment( GTK_RANGE( textureBrowser.m_texture_scroll ) ));
2209 vadjustment.connect( "value_changed", G_CALLBACK( TextureBrowser_verticalScroll ), &textureBrowser );
2211 textureBrowser.m_texture_scroll.visible(textureBrowser.m_showTextureScrollbar);
2214 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
2215 textureBrowser.m_vframe = ui::VBox( FALSE, 0 );
2216 table.attach(textureBrowser.m_vframe, {1, 2, 1, 2});
2218 textureBrowser.m_vfiller = ui::VBox( FALSE, 0 );
2219 textureBrowser.m_vframe.pack_start( textureBrowser.m_vfiller, FALSE, FALSE, 0 );
2221 textureBrowser.m_hframe = ui::HBox( FALSE, 0 );
2222 textureBrowser.m_vframe.pack_start( textureBrowser.m_hframe, TRUE, TRUE, 0 );
2224 textureBrowser.m_hfiller = ui::HBox( FALSE, 0 );
2225 textureBrowser.m_hframe.pack_start( textureBrowser.m_hfiller, FALSE, FALSE, 0 );
2227 textureBrowser.m_vframe.show();
2228 textureBrowser.m_vfiller.show();
2229 textureBrowser.m_hframe.show(),
2230 textureBrowser.m_hfiller.show();
2231 #else // !WORKAROUND_MACOS_GTK2_GLWIDGET
2232 textureBrowser.m_frame = ui::VBox( FALSE, 0 );
2233 table.attach(textureBrowser.m_frame, {1, 2, 1, 2});
2234 textureBrowser.m_frame.show();
2235 #endif // !WORKAROUND_MACOS_GTK2_GLWIDGET
2237 TextureBrowser_constructGLWidget();
2241 if ( textureBrowser.m_tags ) {
2242 { // fill tag GtkListStore
2243 textureBrowser.m_all_tags_list = ui::ListStore::from(gtk_list_store_new( N_COLUMNS, G_TYPE_STRING ));
2244 auto sortable = GTK_TREE_SORTABLE( textureBrowser.m_all_tags_list );
2245 gtk_tree_sortable_set_sort_column_id( sortable, TAG_COLUMN, GTK_SORT_ASCENDING );
2247 TagBuilder.GetAllTags( textureBrowser.m_all_tags );
2248 TextureBrowser_buildTagList();
2251 auto menu_tags = ui::Menu(ui::New);
2252 auto tags_item = TextureBrowser_constructTagsMenu( menu_tags );
2253 gtk_menu_item_set_submenu( GTK_MENU_ITEM( tags_item ), menu_tags );
2254 gtk_menu_shell_append( GTK_MENU_SHELL( menu_bar ), tags_item );
2257 textureBrowser.m_scr_win_tags = ui::ScrolledWindow(ui::New);
2258 gtk_container_set_border_width( GTK_CONTAINER( textureBrowser.m_scr_win_tags ), 0 );
2260 // vertical only scrolling for treeview
2261 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( textureBrowser.m_scr_win_tags ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2263 TextureBrowser_createTreeViewTags();
2265 auto selection = gtk_tree_view_get_selection(textureBrowser.m_treeViewTags );
2266 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2268 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( textureBrowser.m_scr_win_tags ), textureBrowser.m_treeViewTags );
2269 textureBrowser.m_treeViewTags.show();
2271 { // Texture/Tag notebook
2272 TextureBrowser_constructTagNotebook();
2273 vbox.pack_start( textureBrowser.m_tag_notebook, TRUE, TRUE, 0 );
2275 { // Tag search button
2276 TextureBrowser_constructSearchButton();
2277 vbox.pack_end(textureBrowser.m_search_button, FALSE, FALSE, 0);
2279 auto frame_table = ui::Table(3, 3, FALSE);
2282 textureBrowser.m_tag_frame = ui::Frame( "Tag assignment" );
2283 gtk_frame_set_label_align( GTK_FRAME( textureBrowser.m_tag_frame ), 0.5, 0.5 );
2284 gtk_frame_set_shadow_type( GTK_FRAME( textureBrowser.m_tag_frame ), GTK_SHADOW_NONE );
2286 table.attach(textureBrowser.m_tag_frame, {1, 3, 2, 3}, {GTK_FILL, GTK_SHRINK});
2290 textureBrowser.m_tag_frame.add(frame_table);
2292 { // assigned tag list
2293 ui::Widget scrolled_win = ui::ScrolledWindow(ui::New);
2294 gtk_container_set_border_width( GTK_CONTAINER( scrolled_win ), 0 );
2295 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scrolled_win ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2297 textureBrowser.m_assigned_store = ui::ListStore::from(gtk_list_store_new( N_COLUMNS, G_TYPE_STRING ));
2299 auto sortable = GTK_TREE_SORTABLE( textureBrowser.m_assigned_store );
2300 gtk_tree_sortable_set_sort_column_id( sortable, TAG_COLUMN, GTK_SORT_ASCENDING );
2302 auto renderer = ui::CellRendererText(ui::New);
2304 textureBrowser.m_assigned_tree = ui::TreeView(ui::TreeModel::from(textureBrowser.m_assigned_store._handle));
2305 textureBrowser.m_assigned_store.unref();
2306 textureBrowser.m_assigned_tree.connect( "row-activated", (GCallback) TextureBrowser_removeTags, NULL );
2307 gtk_tree_view_set_headers_visible(textureBrowser.m_assigned_tree, FALSE );
2309 auto selection = gtk_tree_view_get_selection(textureBrowser.m_assigned_tree );
2310 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2312 auto column = ui::TreeViewColumn( "", renderer, {{"text", TAG_COLUMN}} );
2313 gtk_tree_view_append_column(textureBrowser.m_assigned_tree, column );
2314 textureBrowser.m_assigned_tree.show();
2316 scrolled_win.show();
2317 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( scrolled_win ), textureBrowser.m_assigned_tree );
2319 frame_table.attach(scrolled_win, {0, 1, 1, 3}, {GTK_FILL, GTK_FILL});
2321 { // available tag list
2322 ui::Widget scrolled_win = ui::ScrolledWindow(ui::New);
2323 gtk_container_set_border_width( GTK_CONTAINER( scrolled_win ), 0 );
2324 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scrolled_win ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2326 textureBrowser.m_available_store = ui::ListStore::from(gtk_list_store_new( N_COLUMNS, G_TYPE_STRING ));
2327 auto sortable = GTK_TREE_SORTABLE( textureBrowser.m_available_store );
2328 gtk_tree_sortable_set_sort_column_id( sortable, TAG_COLUMN, GTK_SORT_ASCENDING );
2330 auto renderer = ui::CellRendererText(ui::New);
2332 textureBrowser.m_available_tree = ui::TreeView(ui::TreeModel::from(textureBrowser.m_available_store._handle));
2333 textureBrowser.m_available_store.unref();
2334 textureBrowser.m_available_tree.connect( "row-activated", (GCallback) TextureBrowser_assignTags, NULL );
2335 gtk_tree_view_set_headers_visible(textureBrowser.m_available_tree, FALSE );
2337 auto selection = gtk_tree_view_get_selection(textureBrowser.m_available_tree );
2338 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2340 auto column = ui::TreeViewColumn( "", renderer, {{"text", TAG_COLUMN}} );
2341 gtk_tree_view_append_column(textureBrowser.m_available_tree, column );
2342 textureBrowser.m_available_tree.show();
2344 scrolled_win.show();
2345 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( scrolled_win ), textureBrowser.m_available_tree );
2347 frame_table.attach(scrolled_win, {2, 3, 1, 3}, {GTK_FILL, GTK_FILL});
2349 { // tag arrow buttons
2350 auto m_btn_left = ui::Button(ui::New);
2351 auto m_btn_right = ui::Button(ui::New);
2352 auto m_arrow_left = ui::Widget::from(gtk_arrow_new( GTK_ARROW_LEFT, GTK_SHADOW_OUT ));
2353 auto m_arrow_right = ui::Widget::from(gtk_arrow_new( GTK_ARROW_RIGHT, GTK_SHADOW_OUT ));
2354 m_btn_left.add(m_arrow_left);
2355 m_btn_right.add(m_arrow_right);
2357 // workaround. the size of the tag frame depends of the requested size of the arrow buttons.
2358 m_arrow_left.dimensions(-1, 68);
2359 m_arrow_right.dimensions(-1, 68);
2361 frame_table.attach(m_btn_left, {1, 2, 1, 2}, {GTK_SHRINK, GTK_EXPAND});
2362 frame_table.attach(m_btn_right, {1, 2, 2, 3}, {GTK_SHRINK, GTK_EXPAND});
2364 m_btn_left.connect( "clicked", G_CALLBACK( TextureBrowser_assignTags ), NULL );
2365 m_btn_right.connect( "clicked", G_CALLBACK( TextureBrowser_removeTags ), NULL );
2369 m_arrow_left.show();
2370 m_arrow_right.show();
2372 { // tag fram labels
2373 ui::Widget m_lbl_assigned = ui::Label( "Assigned" );
2374 ui::Widget m_lbl_unassigned = ui::Label( "Available" );
2376 frame_table.attach(m_lbl_assigned, {0, 1, 0, 1}, {GTK_EXPAND, GTK_SHRINK});
2377 frame_table.attach(m_lbl_unassigned, {2, 3, 0, 1}, {GTK_EXPAND, GTK_SHRINK});
2379 m_lbl_assigned.show();
2380 m_lbl_unassigned.show();
2383 else { // no tag support, show the texture tree only
2384 vbox.pack_start( textureBrowser.m_scr_win_tree, TRUE, TRUE, 0 );
2387 // TODO do we need this?
2388 //gtk_container_set_focus_chain(GTK_CONTAINER(hbox_table), NULL);
2390 isWindowConstructed = true;
2395 void TextureBrowser_destroyGLWidget(){
2396 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2397 if ( isGLWidgetConstructed )
2399 g_signal_handler_disconnect( G_OBJECT( textureBrowser.m_gl_widget ), textureBrowser.m_sizeHandler );
2400 g_signal_handler_disconnect( G_OBJECT( textureBrowser.m_gl_widget ), textureBrowser.m_exposeHandler );
2402 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
2403 textureBrowser.m_hframe.remove( textureBrowser.m_gl_widget );
2404 #else // !WORKAROUND_MACOS_GTK2_GLWIDGET
2405 textureBrowser.m_frame.remove( textureBrowser.m_gl_widget );
2406 #endif // !WORKAROUND_MACOS_GTK2_GLWIDGET
2408 textureBrowser.m_gl_widget.unref();
2410 isGLWidgetConstructed = false;
2414 void TextureBrowser_destroyWindow(){
2415 GlobalShaderSystem().setActiveShadersChangedNotify( Callback<void()>() );
2417 TextureBrowser_destroyGLWidget();
2420 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
2421 /* workaround for gtkglext on gtk 2 issue: OpenGL texture viewport being drawn over the other pages */
2422 /* this is very ugly: force the resizing of the viewport to a single bottom line by forcing the
2423 * resizing of the gl widget by expanding some empty boxes, so the widget area size is reduced
2424 * while covered by another page, so the texture viewport is still rendered over the other page
2425 * but does not annoy the user that much because it's just a line on the bottom that may even
2426 * be printed over existing bottom frame or very close to it. */
2427 void TextureBrowser_showGLWidget(){
2428 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2429 if ( isWindowConstructed && isGLWidgetConstructed )
2431 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_vfiller, FALSE, FALSE, 0, ui::Packing::START );
2432 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_hframe, TRUE, TRUE, 0, ui::Packing::START );
2433 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_hfiller, FALSE, FALSE, 0, ui::Packing::START );
2434 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_gl_widget, TRUE, TRUE, 0, ui::Packing::START );
2436 textureBrowser.m_gl_widget.show();
2440 void TextureBrowser_hideGLWidget(){
2441 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2442 if ( isWindowConstructed && isGLWidgetConstructed )
2444 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_vfiller, TRUE, TRUE, 0, ui::Packing::START);
2445 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_hframe, FALSE, FALSE, 0, ui::Packing::END );
2446 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_hfiller, TRUE, TRUE, 0, ui::Packing::START);
2447 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_gl_widget, FALSE, FALSE, 0, ui::Packing::END );
2449 // The hack needs the GL widget to not be hidden to work,
2450 // so resizing it triggers the redraw of it with the new size.
2451 // GlobalTextureBrowser().m_gl_widget.hide();
2453 // Trigger the redraw.
2454 TextureBrowser_redraw( &GlobalTextureBrowser() );
2458 #endif // WORKAROUND_MACOS_GTK2_GLWIDGET
2460 const Vector3& TextureBrowser_getBackgroundColour( TextureBrowser& textureBrowser ){
2461 return textureBrowser.color_textureback;
2464 void TextureBrowser_setBackgroundColour( TextureBrowser& textureBrowser, const Vector3& colour ){
2465 textureBrowser.color_textureback = colour;
2466 TextureBrowser_queueDraw( textureBrowser );
2469 void TextureBrowser_selectionHelper( ui::TreeModel model, ui::TreePath path, GtkTreeIter* iter, GSList** selected ){
2470 g_assert( selected != NULL );
2473 gtk_tree_model_get( model, iter, TAG_COLUMN, &name, -1 );
2474 *selected = g_slist_append( *selected, name );
2477 void TextureBrowser_shaderInfo(){
2478 const char* name = TextureBrowser_GetSelectedShader( GlobalTextureBrowser() );
2479 IShader* shader = QERApp_Shader_ForName( name );
2481 DoShaderInfoDlg( name, shader->getShaderFileName(), "Shader Info" );
2486 void TextureBrowser_addTag(){
2488 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2490 EMessageBoxReturn result = DoShaderTagDlg( &tag, "Add shader tag" );
2492 if ( result == eIDOK && !tag.empty() ) {
2494 textureBrowser.m_all_tags.insert( tag.c_str() );
2495 gtk_list_store_append( textureBrowser.m_available_store, &iter );
2496 gtk_list_store_set( textureBrowser.m_available_store, &iter, TAG_COLUMN, tag.c_str(), -1 );
2498 // Select the currently added tag in the available list
2499 auto selection = gtk_tree_view_get_selection(textureBrowser.m_available_tree );
2500 gtk_tree_selection_select_iter( selection, &iter );
2502 textureBrowser.m_all_tags_list.append(TAG_COLUMN, tag.c_str());
2506 void TextureBrowser_renameTag(){
2507 /* WORKAROUND: The tag treeview is set to GTK_SELECTION_MULTIPLE. Because
2508 gtk_tree_selection_get_selected() doesn't work with GTK_SELECTION_MULTIPLE,
2509 we need to count the number of selected rows first and use
2510 gtk_tree_selection_selected_foreach() then to go through the list of selected
2511 rows (which always containins a single row).
2514 GSList* selected = NULL;
2515 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2517 auto selection = gtk_tree_view_get_selection(textureBrowser.m_treeViewTags );
2518 gtk_tree_selection_selected_foreach( selection, GtkTreeSelectionForeachFunc( TextureBrowser_selectionHelper ), &selected );
2520 if ( g_slist_length( selected ) == 1 ) { // we only rename a single tag
2521 CopiedString newTag;
2522 EMessageBoxReturn result = DoShaderTagDlg( &newTag, "Rename shader tag" );
2524 if ( result == eIDOK && !newTag.empty() ) {
2525 GtkTreeIter iterList;
2527 gchar* oldTag = (char*)selected->data;
2529 bool row = gtk_tree_model_get_iter_first(textureBrowser.m_all_tags_list, &iterList ) != 0;
2533 gtk_tree_model_get(textureBrowser.m_all_tags_list, &iterList, TAG_COLUMN, &rowTag, -1 );
2535 if ( strcmp( rowTag, oldTag ) == 0 ) {
2536 gtk_list_store_set( textureBrowser.m_all_tags_list, &iterList, TAG_COLUMN, newTag.c_str(), -1 );
2538 row = gtk_tree_model_iter_next(textureBrowser.m_all_tags_list, &iterList ) != 0;
2541 TagBuilder.RenameShaderTag( oldTag, newTag.c_str() );
2543 textureBrowser.m_all_tags.erase( (CopiedString)oldTag );
2544 textureBrowser.m_all_tags.insert( newTag );
2546 BuildStoreAssignedTags( textureBrowser.m_assigned_store, textureBrowser.shader.c_str(), &textureBrowser );
2547 BuildStoreAvailableTags( textureBrowser.m_available_store, textureBrowser.m_assigned_store, textureBrowser.m_all_tags, &textureBrowser );
2552 ui::alert( textureBrowser.m_parent, "Select a single tag for renaming." );
2556 void TextureBrowser_deleteTag(){
2557 GSList* selected = NULL;
2558 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2560 auto selection = gtk_tree_view_get_selection(textureBrowser.m_treeViewTags );
2561 gtk_tree_selection_selected_foreach( selection, GtkTreeSelectionForeachFunc( TextureBrowser_selectionHelper ), &selected );
2563 if ( g_slist_length( selected ) == 1 ) { // we only delete a single tag
2564 auto result = ui::alert( textureBrowser.m_parent, "Are you sure you want to delete the selected tag?", "Delete Tag", ui::alert_type::YESNO, ui::alert_icon::Question );
2566 if ( result == ui::alert_response::YES ) {
2567 GtkTreeIter iterSelected;
2570 gchar* tagSelected = (char*)selected->data;
2572 bool row = gtk_tree_model_get_iter_first(textureBrowser.m_all_tags_list, &iterSelected ) != 0;
2576 gtk_tree_model_get(textureBrowser.m_all_tags_list, &iterSelected, TAG_COLUMN, &rowTag, -1 );
2578 if ( strcmp( rowTag, tagSelected ) == 0 ) {
2579 gtk_list_store_remove( textureBrowser.m_all_tags_list, &iterSelected );
2582 row = gtk_tree_model_iter_next(textureBrowser.m_all_tags_list, &iterSelected ) != 0;
2585 TagBuilder.DeleteTag( tagSelected );
2586 textureBrowser.m_all_tags.erase( (CopiedString)tagSelected );
2588 BuildStoreAssignedTags( textureBrowser.m_assigned_store, textureBrowser.shader.c_str(), &textureBrowser );
2589 BuildStoreAvailableTags( textureBrowser.m_available_store, textureBrowser.m_assigned_store, textureBrowser.m_all_tags, &textureBrowser );
2593 ui::alert( textureBrowser.m_parent, "Select a single tag for deletion." );
2597 void TextureBrowser_copyTag(){
2598 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2599 textureBrowser.m_copied_tags.clear();
2600 TagBuilder.GetShaderTags( textureBrowser.shader.c_str(), textureBrowser.m_copied_tags );
2603 void TextureBrowser_pasteTag(){
2604 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2605 IShader* ishader = QERApp_Shader_ForName( textureBrowser.shader.c_str() );
2606 CopiedString shader = textureBrowser.shader.c_str();
2608 if ( !TagBuilder.CheckShaderTag( shader.c_str() ) ) {
2609 CopiedString shaderFile = ishader->getShaderFileName();
2610 if ( shaderFile.empty() ) {
2612 TagBuilder.AddShaderNode( shader.c_str(), CUSTOM, TEXTURE );
2617 TagBuilder.AddShaderNode( shader.c_str(), CUSTOM, SHADER );
2620 for ( size_t i = 0; i < textureBrowser.m_copied_tags.size(); ++i )
2622 TagBuilder.AddShaderTag( shader.c_str(), textureBrowser.m_copied_tags[i].c_str(), TAG );
2627 for ( size_t i = 0; i < textureBrowser.m_copied_tags.size(); ++i )
2629 if ( !TagBuilder.CheckShaderTag( shader.c_str(), textureBrowser.m_copied_tags[i].c_str() ) ) {
2630 // the tag doesn't exist - let's add it
2631 TagBuilder.AddShaderTag( shader.c_str(), textureBrowser.m_copied_tags[i].c_str(), TAG );
2638 TagBuilder.SaveXmlDoc();
2639 BuildStoreAssignedTags( textureBrowser.m_assigned_store, shader.c_str(), &textureBrowser );
2640 BuildStoreAvailableTags( textureBrowser.m_available_store, textureBrowser.m_assigned_store, textureBrowser.m_all_tags, &textureBrowser );
2643 void TextureBrowser_RefreshShaders(){
2644 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2645 ScopeDisableScreenUpdates disableScreenUpdates( "Processing...", "Loading Shaders" );
2646 GlobalShaderSystem().refresh();
2648 auto selection = gtk_tree_view_get_selection(GlobalTextureBrowser().m_treeViewTree);
2649 GtkTreeModel* model = NULL;
2651 if ( gtk_tree_selection_get_selected (selection, &model, &iter) )
2653 gchar dirName[1024];
2656 gtk_tree_model_get( model, &iter, 0, &buffer, -1 );
2657 strcpy( dirName, buffer );
2659 if ( !TextureBrowser_showWads() ) {
2660 strcat( dirName, "/" );
2662 TextureBrowser_ShowDirectory( GlobalTextureBrowser(), dirName );
2663 TextureBrowser_queueDraw( GlobalTextureBrowser() );
2667 void TextureBrowser_ToggleShowShaders(){
2668 GlobalTextureBrowser().m_showShaders ^= 1;
2669 GlobalTextureBrowser().m_showshaders_item.update();
2670 TextureBrowser_queueDraw( GlobalTextureBrowser() );
2673 void TextureBrowser_ToggleShowShaderListOnly(){
2674 g_TextureBrowser_shaderlistOnly ^= 1;
2675 GlobalTextureBrowser().m_showshaderlistonly_item.update();
2677 TextureBrowser_constructTreeStore();
2680 void TextureBrowser_showAll(){
2681 g_TextureBrowser_currentDirectory = "";
2682 GlobalTextureBrowser().m_searchedTags = false;
2683 TextureBrowser_heightChanged( GlobalTextureBrowser() );
2684 TextureBrowser_updateTitle();
2687 void TextureBrowser_showUntagged(){
2688 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2689 auto result = ui::alert( textureBrowser.m_parent, "WARNING! This function might need a lot of memory and time. Are you sure you want to use it?", "Show Untagged", ui::alert_type::YESNO, ui::alert_icon::Warning );
2691 if ( result == ui::alert_response::YES ) {
2692 textureBrowser.m_found_shaders.clear();
2693 TagBuilder.GetUntagged( textureBrowser.m_found_shaders );
2694 std::set<CopiedString>::iterator iter;
2696 ScopeDisableScreenUpdates disableScreenUpdates( "Searching untagged textures...", "Loading Textures" );
2698 for ( iter = textureBrowser.m_found_shaders.begin(); iter != textureBrowser.m_found_shaders.end(); iter++ )
2700 std::string path = ( *iter ).c_str();
2701 size_t pos = path.find_last_of( "/", path.size() );
2702 std::string name = path.substr( pos + 1, path.size() );
2703 path = path.substr( 0, pos + 1 );
2704 TextureDirectory_loadTexture( path.c_str(), name.c_str() );
2705 globalErrorStream() << path.c_str() << name.c_str() << "\n";
2708 g_TextureBrowser_currentDirectory = "Untagged";
2709 TextureBrowser_queueDraw( GlobalTextureBrowser() );
2710 TextureBrowser_heightChanged( textureBrowser );
2711 TextureBrowser_updateTitle();
2715 void TextureBrowser_FixedSize(){
2716 g_TextureBrowser_fixedSize ^= 1;
2717 GlobalTextureBrowser().m_fixedsize_item.update();
2718 TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2721 void TextureBrowser_FilterMissing(){
2722 g_TextureBrowser_filterMissing ^= 1;
2723 GlobalTextureBrowser().m_filternotex_item.update();
2724 TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2725 TextureBrowser_RefreshShaders();
2728 void TextureBrowser_FilterFallback(){
2729 g_TextureBrowser_filterFallback ^= 1;
2730 GlobalTextureBrowser().m_hidenotex_item.update();
2731 TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2732 TextureBrowser_RefreshShaders();
2735 void TextureBrowser_EnableAlpha(){
2736 g_TextureBrowser_enableAlpha ^= 1;
2737 GlobalTextureBrowser().m_enablealpha_item.update();
2738 TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2741 void TextureBrowser_exportTitle( const Callback<void(const char *)> & importer ){
2742 StringOutputStream buffer( 64 );
2743 buffer << "Textures: ";
2744 if ( !string_empty( g_TextureBrowser_currentDirectory.c_str() ) ) {
2745 buffer << g_TextureBrowser_currentDirectory.c_str();
2751 importer( buffer.c_str() );
2754 struct TextureScale {
2755 static void Export(const TextureBrowser &self, const Callback<void(int)> &returnz) {
2756 switch (self.m_textureScale) {
2775 static void Import(TextureBrowser &self, int value) {
2778 TextureBrowser_setScale(self, 10);
2781 TextureBrowser_setScale(self, 25);
2784 TextureBrowser_setScale(self, 50);
2787 TextureBrowser_setScale(self, 100);
2790 TextureBrowser_setScale(self, 200);
2796 struct UniformTextureSize {
2797 static void Export(const TextureBrowser &self, const Callback<void(int)> &returnz) {
2798 returnz(GlobalTextureBrowser().m_uniformTextureSize);
2801 static void Import(TextureBrowser &self, int value) {
2803 TextureBrowser_setUniformSize(self, value);
2807 void TextureBrowser_constructPreferences( PreferencesPage& page ){
2808 page.appendCheckBox(
2809 "", "Texture scrollbar",
2810 make_property<TextureBrowser_ShowScrollbar>(GlobalTextureBrowser())
2813 const char* texture_scale[] = { "10%", "25%", "50%", "100%", "200%" };
2815 "Texture Thumbnail Scale",
2816 STRING_ARRAY_RANGE( texture_scale ),
2817 make_property<TextureScale>(GlobalTextureBrowser())
2821 "Texture Thumbnail Size",
2822 GlobalTextureBrowser().m_uniformTextureSize,
2823 GlobalTextureBrowser().m_uniformTextureSize,
2826 page.appendEntry( "Mousewheel Increment", GlobalTextureBrowser().m_mouseWheelScrollIncrement );
2828 const char* startup_shaders[] = { "None", TextureBrowser_getCommonShadersName() };
2829 page.appendCombo( "Load Shaders at Startup", reinterpret_cast<int&>( GlobalTextureBrowser().m_startupShaders ), STRING_ARRAY_RANGE( startup_shaders ) );
2832 void TextureBrowser_constructPage( PreferenceGroup& group ){
2833 PreferencesPage page( group.createPage( "Texture Browser", "Texture Browser Preferences" ) );
2834 TextureBrowser_constructPreferences( page );
2837 void TextureBrowser_registerPreferencesPage(){
2838 PreferencesDialog_addSettingsPage( makeCallbackF(TextureBrowser_constructPage) );
2842 #include "preferencesystem.h"
2843 #include "stringio.h"
2846 void TextureClipboard_textureSelected( const char* shader );
2848 void TextureBrowser_Construct(){
2849 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2851 GlobalCommands_insert( "ShaderInfo", makeCallbackF(TextureBrowser_shaderInfo) );
2852 GlobalCommands_insert( "ShowUntagged", makeCallbackF(TextureBrowser_showUntagged) );
2853 GlobalCommands_insert( "AddTag", makeCallbackF(TextureBrowser_addTag) );
2854 GlobalCommands_insert( "RenameTag", makeCallbackF(TextureBrowser_renameTag) );
2855 GlobalCommands_insert( "DeleteTag", makeCallbackF(TextureBrowser_deleteTag) );
2856 GlobalCommands_insert( "CopyTag", makeCallbackF(TextureBrowser_copyTag) );
2857 GlobalCommands_insert( "PasteTag", makeCallbackF(TextureBrowser_pasteTag) );
2858 GlobalCommands_insert( "RefreshShaders", makeCallbackF(VFS_Refresh) );
2859 GlobalToggles_insert( "ShowInUse", makeCallbackF(TextureBrowser_ToggleHideUnused), ToggleItem::AddCallbackCaller( textureBrowser.m_hideunused_item ), Accelerator( 'U' ) );
2860 GlobalCommands_insert( "ShowAllTextures", makeCallbackF(TextureBrowser_showAll), Accelerator( 'A', (GdkModifierType)GDK_CONTROL_MASK ) );
2861 GlobalCommands_insert( "ToggleTextures", makeCallbackF(TextureBrowser_toggleShow), Accelerator( 'T' ) );
2862 GlobalToggles_insert( "ToggleShowShaders", makeCallbackF(TextureBrowser_ToggleShowShaders), ToggleItem::AddCallbackCaller( textureBrowser.m_showshaders_item ) );
2863 GlobalToggles_insert( "ToggleShowShaderlistOnly", makeCallbackF(TextureBrowser_ToggleShowShaderListOnly), ToggleItem::AddCallbackCaller( textureBrowser.m_showshaderlistonly_item ) );
2864 GlobalToggles_insert( "FixedSize", makeCallbackF(TextureBrowser_FixedSize), ToggleItem::AddCallbackCaller( textureBrowser.m_fixedsize_item ) );
2865 GlobalToggles_insert( "FilterMissing", makeCallbackF(TextureBrowser_FilterMissing), ToggleItem::AddCallbackCaller( textureBrowser.m_filternotex_item ) );
2866 GlobalToggles_insert( "FilterFallback", makeCallbackF(TextureBrowser_FilterFallback), ToggleItem::AddCallbackCaller( textureBrowser.m_hidenotex_item ) );
2867 GlobalToggles_insert( "EnableAlpha", makeCallbackF(TextureBrowser_EnableAlpha), ToggleItem::AddCallbackCaller( textureBrowser.m_enablealpha_item ) );
2869 GlobalPreferenceSystem().registerPreference( "TextureScale", make_property_string<TextureScale>(textureBrowser) );
2870 GlobalPreferenceSystem().registerPreference( "UniformTextureSize", make_property_string<UniformTextureSize>(textureBrowser) );
2871 GlobalPreferenceSystem().registerPreference( "TextureScrollbar", make_property_string<TextureBrowser_ShowScrollbar>(textureBrowser));
2872 GlobalPreferenceSystem().registerPreference( "ShowShaders", make_property_string( textureBrowser.m_showShaders ) );
2873 GlobalPreferenceSystem().registerPreference( "ShowShaderlistOnly", make_property_string( g_TextureBrowser_shaderlistOnly ) );
2874 GlobalPreferenceSystem().registerPreference( "FixedSize", make_property_string( g_TextureBrowser_fixedSize ) );
2875 GlobalPreferenceSystem().registerPreference( "FilterMissing", make_property_string( g_TextureBrowser_filterMissing ) );
2876 GlobalPreferenceSystem().registerPreference( "EnableAlpha", make_property_string( g_TextureBrowser_enableAlpha ) );
2877 GlobalPreferenceSystem().registerPreference( "LoadShaders", make_property_string( reinterpret_cast<int&>( textureBrowser.m_startupShaders ) ) );
2878 GlobalPreferenceSystem().registerPreference( "WheelMouseInc", make_property_string( textureBrowser.m_mouseWheelScrollIncrement ) );
2879 GlobalPreferenceSystem().registerPreference( "SI_Colors0", make_property_string( textureBrowser.color_textureback ) );
2881 textureBrowser.shader = texdef_name_default();
2883 Textures_setModeChangedNotify( ReferenceCaller<TextureBrowser, void(), TextureBrowser_queueDraw>( textureBrowser ) );
2885 TextureBrowser_registerPreferencesPage();
2887 GlobalShaderSystem().attach( g_ShadersObserver );
2889 TextureBrowser_textureSelected = TextureClipboard_textureSelected;
2892 void TextureBrowser_Destroy(){
2893 GlobalShaderSystem().detach( g_ShadersObserver );
2895 Textures_setModeChangedNotify( Callback<void()>() );
2898 #if WORKAROUND_WINDOWS_GTK2_GLWIDGET
2899 ui::GLArea TextureBrowser_getGLWidget(){
2900 return GlobalTextureBrowser().m_gl_widget;
2902 #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET