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 );
173 typedef ReferenceCaller<TextureGroups, void(const char*), TextureGroups_addDirectory> TextureGroupsAddDirectoryCaller;
175 class DeferredAdjustment
180 typedef void ( *ValueChangedFunction )( void* data, gdouble value );
182 ValueChangedFunction m_function;
185 static gboolean deferred_value_changed( gpointer data ){
186 reinterpret_cast<DeferredAdjustment*>( data )->m_function(
187 reinterpret_cast<DeferredAdjustment*>( data )->m_data,
188 reinterpret_cast<DeferredAdjustment*>( data )->m_value
190 reinterpret_cast<DeferredAdjustment*>( data )->m_handler = 0;
191 reinterpret_cast<DeferredAdjustment*>( data )->m_value = 0;
196 DeferredAdjustment( ValueChangedFunction function, void* data ) : m_value( 0 ), m_handler( 0 ), m_function( function ), m_data( data ){
200 if ( m_handler != 0 ) {
201 g_source_remove( m_handler );
202 deferred_value_changed( this );
206 void value_changed( gdouble value ){
208 if ( m_handler == 0 ) {
209 m_handler = g_idle_add( deferred_value_changed, this );
213 static void adjustment_value_changed(ui::Adjustment adjustment, DeferredAdjustment* self ){
214 self->value_changed( gtk_adjustment_get_value(adjustment) );
219 class TextureBrowser;
221 typedef ReferenceCaller<TextureBrowser, void(), TextureBrowser_queueDraw> TextureBrowserQueueDrawCaller;
223 void TextureBrowser_scrollChanged( void* data, gdouble value );
228 STARTUPSHADERS_NONE = 0,
229 STARTUPSHADERS_COMMON,
232 void TextureBrowser_hideUnusedExport( const Callback<void(bool)> & importer );
234 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_hideUnusedExport> TextureBrowserHideUnusedExport;
236 void TextureBrowser_showShadersExport( const Callback<void(bool)> & importer );
238 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_showShadersExport> TextureBrowserShowShadersExport;
240 void TextureBrowser_showShaderlistOnly( const Callback<void(bool)> & importer );
242 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_showShaderlistOnly> TextureBrowserShowShaderlistOnlyExport;
244 void TextureBrowser_fixedSize( const Callback<void(bool)> & importer );
246 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_fixedSize> TextureBrowserFixedSizeExport;
248 void TextureBrowser_filterMissing( const Callback<void(bool)> & importer );
250 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_filterMissing> TextureBrowserFilterMissingExport;
252 void TextureBrowser_filterFallback( const Callback<void(bool)> & importer );
254 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_filterFallback> TextureBrowserFilterFallbackExport;
256 void TextureBrowser_enableAlpha( const Callback<void(bool)> & importer );
258 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_enableAlpha> TextureBrowserEnableAlphaExport;
269 ui::Window m_parent{ui::null};
270 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
271 ui::VBox m_vframe{ui::null};
272 ui::VBox m_vfiller{ui::null};
273 ui::HBox m_hframe{ui::null};
274 ui::HBox m_hfiller{ui::null};
275 #else // !WORKAROUND_MACOS_GTK2_GLWIDGET
276 ui::VBox m_frame{ui::null};
277 #endif // !WORKAROUND_MACOS_GTK2_GLWIDGET
278 ui::GLArea m_gl_widget{ui::null};
279 ui::Widget m_texture_scroll{ui::null};
280 ui::TreeView m_treeViewTree{ui::New};
281 ui::TreeView m_treeViewTags{ui::null};
282 ui::Frame m_tag_frame{ui::null};
283 ui::ListStore m_assigned_store{ui::null};
284 ui::ListStore m_available_store{ui::null};
285 ui::TreeView m_assigned_tree{ui::null};
286 ui::TreeView m_available_tree{ui::null};
287 ui::Widget m_scr_win_tree{ui::null};
288 ui::Widget m_scr_win_tags{ui::null};
289 ui::Widget m_tag_notebook{ui::null};
290 ui::Button m_search_button{ui::null};
291 ui::Widget m_shader_info_item{ui::null};
293 std::set<CopiedString> m_all_tags;
294 ui::ListStore m_all_tags_list{ui::null};
295 std::vector<CopiedString> m_copied_tags;
296 std::set<CopiedString> m_found_shaders;
298 ToggleItem m_hideunused_item;
299 ToggleItem m_hidenotex_item;
300 ToggleItem m_showshaders_item;
301 ToggleItem m_showshaderlistonly_item;
302 ToggleItem m_fixedsize_item;
303 ToggleItem m_filternotex_item;
304 ToggleItem m_enablealpha_item;
307 guint m_exposeHandler;
309 bool m_heightChanged;
310 bool m_originInvalid;
312 DeferredAdjustment m_scrollAdjustment;
313 FreezePointer m_freezePointer;
315 Vector3 color_textureback;
316 // the increment step we use against the wheel mouse
317 std::size_t m_mouseWheelScrollIncrement;
318 std::size_t m_textureScale;
319 // make the texture increments match the grid changes
321 bool m_showTextureScrollbar;
322 StartupShaders m_startupShaders;
323 // if true, the texture window will only display in-use shaders
324 // if false, all the shaders in memory are displayed
329 // The uniform size (in pixels) that textures are resized to when m_resizeTextures is true.
330 int m_uniformTextureSize;
331 int m_uniformTextureMinSize;
333 // Return the display width of a texture in the texture browser
334 /*void getTextureWH( qtexture_t* tex, int *width, int *height ){
335 if ( !g_TextureBrowser_fixedSize ) {
336 // Don't use uniform size
337 *width = (int)( tex->width * ( (float)m_textureScale / 100 ) );
338 *height = (int)( tex->height * ( (float)m_textureScale / 100 ) );
341 else if ( tex->width >= tex->height ) {
342 // Texture is square, or wider than it is tall
343 if ( tex->width >= m_uniformTextureSize ){
344 *width = m_uniformTextureSize;
345 *height = (int)( m_uniformTextureSize * ( (float)tex->height / tex->width ) );
347 else if ( tex->width <= m_uniformTextureMinSize ){
348 *width = m_uniformTextureMinSize;
349 *height = (int)( m_uniformTextureMinSize * ( (float)tex->height / tex->width ) );
353 *height = tex->height;
357 // Texture taller than it is wide
358 if ( tex->height >= m_uniformTextureSize ){
359 *height = m_uniformTextureSize;
360 *width = (int)( m_uniformTextureSize * ( (float)tex->width / tex->height ) );
362 else if ( tex->height <= m_uniformTextureMinSize ){
363 *height = m_uniformTextureMinSize;
364 *width = (int)( m_uniformTextureMinSize * ( (float)tex->width / tex->height ) );
368 *height = tex->height;
374 void getTextureWH( qtexture_t* tex, int *width, int *height ){
375 // Don't use uniform size
376 *width = (int)( tex->width * ( (float)m_textureScale / 100 ) );
377 *height = (int)( tex->height * ( (float)m_textureScale / 100 ) );
379 if ( g_TextureBrowser_fixedSize ){
383 // Texture is square, or wider than it is tall
384 if ( W >= m_uniformTextureSize ){
385 *width = m_uniformTextureSize;
386 *height = m_uniformTextureSize * H / W;
388 else if ( W <= m_uniformTextureMinSize ){
389 *width = m_uniformTextureMinSize;
390 *height = m_uniformTextureMinSize * H / W;
394 // Texture taller than it is wide
395 if ( H >= m_uniformTextureSize ){
396 *height = m_uniformTextureSize;
397 *width = m_uniformTextureSize * W / H;
399 else if ( H <= m_uniformTextureMinSize ){
400 *height = m_uniformTextureMinSize;
401 *width = m_uniformTextureMinSize * W / H;
408 m_texture_scroll( ui::null ),
409 m_hideunused_item( TextureBrowserHideUnusedExport() ),
410 m_hidenotex_item( TextureBrowserFilterFallbackExport() ),
411 m_showshaders_item( TextureBrowserShowShadersExport() ),
412 m_showshaderlistonly_item( TextureBrowserShowShaderlistOnlyExport() ),
413 m_fixedsize_item( TextureBrowserFixedSizeExport() ),
414 m_filternotex_item( TextureBrowserFilterMissingExport() ),
415 m_enablealpha_item( TextureBrowserEnableAlphaExport() ),
416 m_heightChanged( true ),
417 m_originInvalid( true ),
418 m_scrollAdjustment( TextureBrowser_scrollChanged, this ),
419 color_textureback( 0.25f, 0.25f, 0.25f ),
420 m_mouseWheelScrollIncrement( 64 ),
421 m_textureScale( 50 ),
422 m_showShaders( true ),
423 m_showTextureScrollbar( true ),
424 m_startupShaders( STARTUPSHADERS_NONE ),
425 m_hideUnused( false ),
426 m_rmbSelected( false ),
427 m_searchedTags( false ),
429 m_uniformTextureSize( 160 ),
430 m_uniformTextureMinSize( 48 ){
434 void ( *TextureBrowser_textureSelected )( const char* shader );
437 void TextureBrowser_updateScroll( TextureBrowser& textureBrowser );
440 const char* TextureBrowser_getCommonShadersName(){
441 const char* value = g_pGameDescription->getKeyValue( "common_shaders_name" );
442 if ( !string_empty( value ) ) {
448 const char* TextureBrowser_getCommonShadersDir(){
449 const char* value = g_pGameDescription->getKeyValue( "common_shaders_dir" );
450 if ( !string_empty( value ) ) {
456 inline int TextureBrowser_fontHeight( TextureBrowser& textureBrowser ){
457 return GlobalOpenGL().m_font->getPixelHeight();
460 const char* TextureBrowser_GetSelectedShader( TextureBrowser& textureBrowser ){
461 return textureBrowser.shader.c_str();
464 void TextureBrowser_SetStatus( TextureBrowser& textureBrowser, const char* name ){
465 IShader* shader = QERApp_Shader_ForName( name );
466 qtexture_t* q = shader->getTexture();
467 StringOutputStream strTex( 256 );
468 strTex << name << " W: " << Unsigned( q->width ) << " H: " << Unsigned( q->height );
470 g_pParentWnd->SetStatusText( g_pParentWnd->m_texture_status, strTex.c_str() );
473 void TextureBrowser_Focus( TextureBrowser& textureBrowser, const char* name );
475 void TextureBrowser_SetSelectedShader( TextureBrowser& textureBrowser, const char* shader ){
476 textureBrowser.shader = shader;
477 TextureBrowser_SetStatus( textureBrowser, shader );
478 TextureBrowser_Focus( textureBrowser, shader );
480 if ( FindTextureDialog_isOpen() ) {
481 FindTextureDialog_selectTexture( shader );
484 // disable the menu item "shader info" if no shader was selected
485 IShader* ishader = QERApp_Shader_ForName( shader );
486 CopiedString filename = ishader->getShaderFileName();
488 if ( filename.empty() ) {
489 if ( textureBrowser.m_shader_info_item != NULL ) {
490 gtk_widget_set_sensitive( textureBrowser.m_shader_info_item, FALSE );
494 gtk_widget_set_sensitive( textureBrowser.m_shader_info_item, TRUE );
501 CopiedString g_TextureBrowser_currentDirectory;
504 ============================================================================
508 TTimo: now based on a rundown through all the shaders
509 NOTE: we expect the Active shaders count doesn't change during a Texture_StartPos .. Texture_NextPos cycle
510 otherwise we may need to rely on a list instead of an array storage
511 ============================================================================
517 // texture layout functions
518 // TTimo: now based on shaders
519 int current_x, current_y, current_row;
522 void Texture_StartPos( TextureLayout& layout ){
523 layout.current_x = 8;
524 layout.current_y = -8;
525 layout.current_row = 0;
528 void Texture_NextPos( TextureBrowser& textureBrowser, TextureLayout& layout, qtexture_t* current_texture, int *x, int *y ){
529 qtexture_t* q = current_texture;
532 textureBrowser.getTextureWH( q, &nWidth, &nHeight );
533 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
534 layout.current_x = 8;
535 layout.current_y -= layout.current_row + TextureBrowser_fontHeight( textureBrowser ) + 4;
536 layout.current_row = 0;
539 *x = layout.current_x;
540 *y = layout.current_y;
542 // Is our texture larger than the row? If so, grow the
543 // row height to match it
545 if ( layout.current_row < nHeight ) {
546 layout.current_row = nHeight;
549 // never go less than 96, or the names get all crunched up
550 layout.current_x += nWidth < 96 ? 96 : nWidth;
551 layout.current_x += 8;
554 bool TextureSearch_IsShown( const char* name ){
555 std::set<CopiedString>::iterator iter;
557 iter = GlobalTextureBrowser().m_found_shaders.find( name );
559 if ( iter == GlobalTextureBrowser().m_found_shaders.end() ) {
567 // if texture_showinuse jump over non in-use textures
568 bool Texture_IsShown( IShader* shader, bool show_shaders, bool hideUnused ){
569 // filter missing shaders
570 // ugly: filter on built-in fallback name after substitution
571 if ( g_TextureBrowser_filterMissing ) {
572 if ( isMissing( shader->getTexture()->name ) ) {
576 // filter the fallback (notex/shadernotex) for missing shaders or editor image
577 if ( g_TextureBrowser_filterFallback ) {
578 if ( isNotex( shader->getName() ) ) {
581 if ( isNotex( shader->getTexture()->name ) ) {
586 if ( g_TextureBrowser_currentDirectory == "Untagged" ) {
587 std::set<CopiedString>::iterator iter;
589 iter = GlobalTextureBrowser().m_found_shaders.find( shader->getName() );
591 if ( iter == GlobalTextureBrowser().m_found_shaders.end() ) {
599 if ( !shader_equal_prefix( shader->getName(), "textures/" ) ) {
603 if ( !show_shaders && !shader->IsDefault() ) {
607 if ( hideUnused && !shader->IsInUse() ) {
611 if ( GlobalTextureBrowser().m_searchedTags ) {
612 if ( !TextureSearch_IsShown( shader->getName() ) ) {
620 if ( TextureBrowser_showWads() )
622 if ( g_TextureBrowser_currentDirectory != ""
623 && !string_equal( shader->getWadName(), g_TextureBrowser_currentDirectory.c_str() ) )
628 else if ( !shader_equal_prefix( shader_get_textureName( shader->getName() ), g_TextureBrowser_currentDirectory.c_str() ) ) {
636 void TextureBrowser_heightChanged( TextureBrowser& textureBrowser ){
637 textureBrowser.m_heightChanged = true;
639 TextureBrowser_updateScroll( textureBrowser );
640 TextureBrowser_queueDraw( textureBrowser );
643 void TextureBrowser_evaluateHeight( TextureBrowser& textureBrowser ){
644 if ( textureBrowser.m_heightChanged ) {
645 textureBrowser.m_heightChanged = false;
647 textureBrowser.m_nTotalHeight = 0;
649 TextureLayout layout;
650 Texture_StartPos( layout );
651 for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
653 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
655 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
660 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
662 textureBrowser.getTextureWH( shader->getTexture(), &nWidth, &nHeight );
663 textureBrowser.m_nTotalHeight = std::max( textureBrowser.m_nTotalHeight, abs( layout.current_y ) + TextureBrowser_fontHeight( textureBrowser ) + nHeight + 4 );
668 int TextureBrowser_TotalHeight( TextureBrowser& textureBrowser ){
669 TextureBrowser_evaluateHeight( textureBrowser );
670 return textureBrowser.m_nTotalHeight;
673 inline const int& min_int( const int& left, const int& right ){
674 return std::min( left, right );
677 void TextureBrowser_clampOriginY( TextureBrowser& textureBrowser ){
678 if ( textureBrowser.originy > 0 ) {
679 textureBrowser.originy = 0;
681 int lower = min_int( textureBrowser.height - TextureBrowser_TotalHeight( textureBrowser ), 0 );
682 if ( textureBrowser.originy < lower ) {
683 textureBrowser.originy = lower;
687 int TextureBrowser_getOriginY( TextureBrowser& textureBrowser ){
688 if ( textureBrowser.m_originInvalid ) {
689 textureBrowser.m_originInvalid = false;
690 TextureBrowser_clampOriginY( textureBrowser );
691 TextureBrowser_updateScroll( textureBrowser );
693 return textureBrowser.originy;
696 void TextureBrowser_setOriginY( TextureBrowser& textureBrowser, int originy ){
697 textureBrowser.originy = originy;
698 TextureBrowser_clampOriginY( textureBrowser );
699 TextureBrowser_updateScroll( textureBrowser );
700 TextureBrowser_queueDraw( textureBrowser );
704 Signal0 g_activeShadersChangedCallbacks;
706 void TextureBrowser_addActiveShadersChangedCallback( const SignalHandler& handler ){
707 g_activeShadersChangedCallbacks.connectLast( handler );
710 void TextureBrowser_constructTreeStore();
712 class ShadersObserver : public ModuleObserver
714 Signal0 m_realiseCallbacks;
717 m_realiseCallbacks();
718 TextureBrowser_constructTreeStore();
724 void insert( const SignalHandler& handler ){
725 m_realiseCallbacks.connectLast( handler );
731 ShadersObserver g_ShadersObserver;
734 void TextureBrowser_addShadersRealiseCallback( const SignalHandler& handler ){
735 g_ShadersObserver.insert( handler );
738 void TextureBrowser_activeShadersChanged( TextureBrowser& textureBrowser ){
739 TextureBrowser_heightChanged( textureBrowser );
740 textureBrowser.m_originInvalid = true;
742 g_activeShadersChangedCallbacks();
745 struct TextureBrowser_ShowScrollbar {
746 static void Export(const TextureBrowser &self, const Callback<void(bool)> &returnz) {
747 returnz(self.m_showTextureScrollbar);
750 static void Import(TextureBrowser &self, bool value) {
751 self.m_showTextureScrollbar = value;
752 if (self.m_texture_scroll) {
753 self.m_texture_scroll.visible(self.m_showTextureScrollbar);
754 TextureBrowser_updateScroll(self);
762 TextureBrowser_ShowDirectory
763 relies on texture_directory global for the directory to use
764 1) Load the shaders for the given directory
765 2) Scan the remaining texture, load them and assign them a default shader (the "noshader" shader)
766 NOTE: when writing a texture plugin, or some texture extensions, this function may need to be overriden, and made
767 available through the IShaders interface
768 NOTE: for texture window layout:
769 all shaders are stored with alphabetical order after load
770 previously loaded and displayed stuff is hidden, only in-use and newly loaded is shown
771 ( the GL textures are not flushed though)
775 bool endswith( const char *haystack, const char *needle ){
776 size_t lh = strlen( haystack );
777 size_t ln = strlen( needle );
781 return !memcmp( haystack + ( lh - ln ), needle, ln );
784 bool texture_name_ignore( const char* name ){
785 StringOutputStream strTemp( string_length( name ) );
786 strTemp << LowerCase( name );
789 endswith( strTemp.c_str(), ".specular" ) ||
790 endswith( strTemp.c_str(), ".glow" ) ||
791 endswith( strTemp.c_str(), ".bump" ) ||
792 endswith( strTemp.c_str(), ".diffuse" ) ||
793 endswith( strTemp.c_str(), ".blend" ) ||
794 endswith( strTemp.c_str(), ".alpha" ) ||
795 endswith( strTemp.c_str(), "_alpha" ) ||
797 endswith( strTemp.c_str(), "_h" ) ||
798 endswith( strTemp.c_str(), "_local" ) ||
799 endswith( strTemp.c_str(), "_nm" ) ||
800 endswith( strTemp.c_str(), "_s" ) ||
802 endswith( strTemp.c_str(), "_bump" ) ||
803 endswith( strTemp.c_str(), "_glow" ) ||
804 endswith( strTemp.c_str(), "_gloss" ) ||
805 endswith( strTemp.c_str(), "_luma" ) ||
806 endswith( strTemp.c_str(), "_norm" ) ||
807 endswith( strTemp.c_str(), "_pants" ) ||
808 endswith( strTemp.c_str(), "_shirt" ) ||
809 endswith( strTemp.c_str(), "_reflect" ) ||
811 endswith( strTemp.c_str(), "_d" ) ||
812 endswith( strTemp.c_str(), "_n" ) ||
813 endswith( strTemp.c_str(), "_p" ) ||
814 endswith( strTemp.c_str(), "_g" ) ||
815 endswith( strTemp.c_str(), "_a" ) ||
819 class LoadShaderVisitor : public Archive::Visitor
822 void visit( const char* name ){
823 IShader* shader = QERApp_Shader_ForName( CopiedString( StringRange( name, path_get_filename_base_end( name ) ) ).c_str() );
825 shader->setWadName( g_TextureBrowser_currentDirectory.c_str() );
829 void TextureBrowser_SetHideUnused( TextureBrowser& textureBrowser, bool hideUnused );
831 ui::Widget g_page_textures{ui::null};
833 void TextureBrowser_toggleShow(){
834 GroupDialog_showPage( g_page_textures );
838 void TextureBrowser_updateTitle(){
839 GroupDialog_updatePageTitle( g_page_textures );
843 class TextureCategoryLoadShader
845 const char* m_directory;
846 std::size_t& m_count;
848 using func = void(const char *);
850 TextureCategoryLoadShader( const char* directory, std::size_t& count )
851 : m_directory( directory ), m_count( count ){
855 void operator()( const char* name ) const {
856 if ( shader_equal_prefix( name, "textures/" )
857 && shader_equal_prefix( name + string_length( "textures/" ), m_directory ) ) {
859 // request the shader, this will load the texture if needed
860 // this Shader_ForName call is a kind of hack
861 IShader *pFoo = QERApp_Shader_ForName( name );
867 void TextureDirectory_loadTexture( const char* directory, const char* texture ){
868 // Doom3-like dds/ prefix (used by DarkPlaces).
869 // When we list dds/textures/ folder,
870 // store the texture names without dds/ prefix.
871 if ( !strncmp( "dds/", directory, 4 ) )
873 directory = &directory[ 4 ];
876 StringOutputStream name( 256 );
877 name << directory << StringRange( texture, path_get_filename_base_end( texture ) );
879 if ( texture_name_ignore( name.c_str() ) ) {
883 if ( !shader_valid( name.c_str() ) ) {
884 globalOutputStream() << "Skipping invalid texture name: [" << name.c_str() << "]\n";
888 // if a texture is already in use to represent a shader, ignore it
889 IShader* shader = QERApp_Shader_ForName( name.c_str() );
893 typedef ConstPointerCaller<char, void(const char*), TextureDirectory_loadTexture> TextureDirectoryLoadTextureCaller;
895 class LoadTexturesByTypeVisitor : public ImageModules::Visitor
897 const char* m_dirstring;
899 LoadTexturesByTypeVisitor( const char* dirstring )
900 : m_dirstring( dirstring ){
903 void visit( const char* minor, const _QERPlugImageTable& table ) const {
904 GlobalFileSystem().forEachFile( m_dirstring, minor, TextureDirectoryLoadTextureCaller( m_dirstring ) );
908 void TextureBrowser_ShowDirectory( TextureBrowser& textureBrowser, const char* directory ){
909 if ( TextureBrowser_showWads() ) {
910 g_TextureBrowser_currentDirectory = directory;
911 TextureBrowser_heightChanged( textureBrowser );
913 Archive* archive = GlobalFileSystem().getArchive( directory );
914 if ( archive != nullptr )
916 LoadShaderVisitor visitor;
917 archive->forEachFile( Archive::VisitorFunc( visitor, Archive::eFiles, 0 ), "textures/" );
919 // Doom3-like dds/ prefix (used by DarkPlaces).
920 archive->forEachFile( Archive::VisitorFunc( visitor, Archive::eFiles, 0 ), "dds/textures/" );
922 else if ( extension_equal_i( path_get_extension( directory ), "wad" ) )
924 globalErrorStream() << "Failed to load " << directory << "\n";
929 g_TextureBrowser_currentDirectory = directory;
930 TextureBrowser_heightChanged( textureBrowser );
932 std::size_t shaders_count;
933 GlobalShaderSystem().foreachShaderName(makeCallback( TextureCategoryLoadShader( directory, shaders_count ) ) );
934 globalOutputStream() << "Showing " << Unsigned( shaders_count ) << " shaders.\n";
936 if ( g_pGameDescription->mGameType != "doom3" ) {
937 // load remaining texture files
939 StringOutputStream dirstring( 64 );
940 dirstring << "textures/" << directory;
943 LoadTexturesByTypeVisitor visitor( dirstring.c_str() );
944 Radiant_getImageModules().foreachModule( visitor );
947 // Doom3-like dds/ prefix (used by DarkPlaces).
949 dirstring << "dds/textures/" << directory;
952 LoadTexturesByTypeVisitor visitor( dirstring.c_str() );
953 Radiant_getImageModules().foreachModule( visitor );
958 // we'll display the newly loaded textures + all the ones already in use
959 TextureBrowser_SetHideUnused( textureBrowser, false );
961 TextureBrowser_updateTitle();
964 void TextureBrowser_ShowTagSearchResult( TextureBrowser& textureBrowser, const char* directory ){
965 g_TextureBrowser_currentDirectory = directory;
966 TextureBrowser_heightChanged( textureBrowser );
968 std::size_t shaders_count;
969 GlobalShaderSystem().foreachShaderName(makeCallback( TextureCategoryLoadShader( directory, shaders_count ) ) );
970 globalOutputStream() << "Showing " << Unsigned( shaders_count ) << " shaders.\n";
972 if ( g_pGameDescription->mGameType != "doom3" ) {
973 // load remaining texture files
974 StringOutputStream dirstring( 64 );
975 dirstring << "textures/" << directory;
978 LoadTexturesByTypeVisitor visitor( dirstring.c_str() );
979 Radiant_getImageModules().foreachModule( visitor );
982 // Doom3-like dds/ prefix (used by DarkPlaces).
984 dirstring << "dds/textures/" << directory;
987 LoadTexturesByTypeVisitor visitor( dirstring.c_str() );
988 Radiant_getImageModules().foreachModule( visitor );
992 // we'll display the newly loaded textures + all the ones already in use
993 TextureBrowser_SetHideUnused( textureBrowser, false );
997 bool TextureBrowser_hideUnused();
999 void TextureBrowser_hideUnusedExport( const Callback<void(bool)> & importer ){
1000 importer( TextureBrowser_hideUnused() );
1003 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_hideUnusedExport> TextureBrowserHideUnusedExport;
1005 void TextureBrowser_showShadersExport( const Callback<void(bool)> & importer ){
1006 importer( GlobalTextureBrowser().m_showShaders );
1009 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_showShadersExport> TextureBrowserShowShadersExport;
1011 void TextureBrowser_showShaderlistOnly( const Callback<void(bool)> & importer ){
1012 importer( g_TextureBrowser_shaderlistOnly );
1015 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_showShaderlistOnly> TextureBrowserShowShaderlistOnlyExport;
1017 void TextureBrowser_fixedSize( const Callback<void(bool)> & importer ){
1018 importer( g_TextureBrowser_fixedSize );
1021 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_fixedSize> TextureBrowser_FixedSizeExport;
1023 void TextureBrowser_filterMissing( const Callback<void(bool)> & importer ){
1024 importer( g_TextureBrowser_filterMissing );
1027 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_filterMissing> TextureBrowser_filterMissingExport;
1029 void TextureBrowser_filterFallback( const Callback<void(bool)> & importer ){
1030 importer( g_TextureBrowser_filterFallback );
1033 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_filterFallback> TextureBrowser_filterFallbackExport;
1035 void TextureBrowser_enableAlpha( const Callback<void(bool)> & importer ){
1036 importer( g_TextureBrowser_enableAlpha );
1039 typedef FreeCaller<void(const Callback<void(bool)> &), TextureBrowser_enableAlpha> TextureBrowser_enableAlphaExport;
1041 void TextureBrowser_SetHideUnused( TextureBrowser& textureBrowser, bool hideUnused ){
1043 textureBrowser.m_hideUnused = true;
1047 textureBrowser.m_hideUnused = false;
1050 textureBrowser.m_hideunused_item.update();
1052 TextureBrowser_heightChanged( textureBrowser );
1053 textureBrowser.m_originInvalid = true;
1056 void TextureBrowser_ShowStartupShaders( TextureBrowser& textureBrowser ){
1057 if ( textureBrowser.m_startupShaders == STARTUPSHADERS_COMMON ) {
1058 TextureBrowser_ShowDirectory( textureBrowser, TextureBrowser_getCommonShadersDir() );
1063 //++timo NOTE: this is a mix of Shader module stuff and texture explorer
1064 // it might need to be split in parts or moved out .. dunno
1065 // scroll origin so the specified texture is completely on screen
1066 // if current texture is not displayed, nothing is changed
1067 void TextureBrowser_Focus( TextureBrowser& textureBrowser, const char* name ){
1068 TextureLayout layout;
1069 // scroll origin so the texture is completely on screen
1070 Texture_StartPos( layout );
1072 for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
1074 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
1076 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
1081 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
1082 qtexture_t* q = shader->getTexture();
1087 // we have found when texdef->name and the shader name match
1088 // NOTE: as everywhere else for our comparisons, we are not case sensitive
1089 if ( shader_equal( name, shader->getName() ) ) {
1090 int textureHeight = (int)( q->height * ( (float)textureBrowser.m_textureScale / 100 ) )
1091 + 2 * TextureBrowser_fontHeight( textureBrowser );
1093 int originy = TextureBrowser_getOriginY( textureBrowser );
1094 if ( y > originy ) {
1098 if ( y - textureHeight < originy - textureBrowser.height ) {
1099 originy = ( y - textureHeight ) + textureBrowser.height;
1102 TextureBrowser_setOriginY( textureBrowser, originy );
1108 IShader* Texture_At( TextureBrowser& textureBrowser, int mx, int my ){
1109 my += TextureBrowser_getOriginY( textureBrowser ) - textureBrowser.height;
1111 TextureLayout layout;
1112 Texture_StartPos( layout );
1113 for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
1115 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
1117 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
1122 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
1123 qtexture_t *q = shader->getTexture();
1128 int nWidth, nHeight;
1129 textureBrowser.getTextureWH( q, &nWidth, &nHeight );
1130 if ( mx > x && mx - x < nWidth
1131 && my < y && y - my < nHeight + TextureBrowser_fontHeight( textureBrowser ) ) {
1146 void SelectTexture( TextureBrowser& textureBrowser, int mx, int my, bool bShift ){
1147 IShader* shader = Texture_At( textureBrowser, mx, my );
1148 if ( shader != 0 ) {
1150 if ( shader->IsDefault() ) {
1151 globalOutputStream() << "ERROR: " << shader->getName() << " is not a shader, it's a texture.\n";
1154 ViewShader( shader->getShaderFileName(), shader->getName() );
1159 TextureBrowser_SetSelectedShader( textureBrowser, shader->getName() );
1160 TextureBrowser_textureSelected( shader->getName() );
1162 if ( !FindTextureDialog_isOpen() && !textureBrowser.m_rmbSelected ) {
1163 UndoableCommand undo( "textureNameSetSelected" );
1164 Select_SetShader( shader->getName() );
1171 ============================================================================
1175 ============================================================================
1178 void TextureBrowser_trackingDelta( int x, int y, unsigned int state, void* data ){
1179 TextureBrowser& textureBrowser = *reinterpret_cast<TextureBrowser*>( data );
1183 if ( state & GDK_SHIFT_MASK ) {
1187 int originy = TextureBrowser_getOriginY( textureBrowser );
1188 originy += y * scale;
1189 TextureBrowser_setOriginY( textureBrowser, originy );
1193 void TextureBrowser_Tracking_MouseDown( TextureBrowser& textureBrowser ){
1194 textureBrowser.m_freezePointer.freeze_pointer( textureBrowser.m_gl_widget, TextureBrowser_trackingDelta, &textureBrowser );
1197 void TextureBrowser_Tracking_MouseUp( TextureBrowser& textureBrowser ){
1198 textureBrowser.m_freezePointer.unfreeze_pointer( textureBrowser.m_gl_widget );
1201 void TextureBrowser_Selection_MouseDown( TextureBrowser& textureBrowser, guint32 flags, int pointx, int pointy ){
1202 SelectTexture( textureBrowser, pointx, textureBrowser.height - 1 - pointy, ( flags & GDK_SHIFT_MASK ) != 0 );
1206 ============================================================================
1210 ============================================================================
1216 TTimo: relying on the shaders list to display the textures
1217 we must query all qtexture_t* to manage and display through the IShaders interface
1218 this allows a plugin to completely override the texture system
1221 void Texture_Draw( TextureBrowser& textureBrowser ){
1222 int originy = TextureBrowser_getOriginY( textureBrowser );
1224 glClearColor( textureBrowser.color_textureback[0],
1225 textureBrowser.color_textureback[1],
1226 textureBrowser.color_textureback[2],
1229 glViewport( 0, 0, textureBrowser.width, textureBrowser.height );
1230 glMatrixMode( GL_PROJECTION );
1233 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1234 glDisable( GL_DEPTH_TEST );
1235 if ( g_TextureBrowser_enableAlpha ) {
1236 glEnable( GL_BLEND );
1237 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1240 glDisable( GL_BLEND );
1242 glOrtho( 0, textureBrowser.width, originy - textureBrowser.height, originy, -100, 100 );
1243 glEnable( GL_TEXTURE_2D );
1245 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1247 int last_y = 0, last_height = 0;
1249 TextureLayout layout;
1250 Texture_StartPos( layout );
1251 for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
1253 IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
1255 if ( !Texture_IsShown( shader, textureBrowser.m_showShaders, textureBrowser.m_hideUnused ) ) {
1260 Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
1261 qtexture_t *q = shader->getTexture();
1266 int nWidth, nHeight;
1267 textureBrowser.getTextureWH( q, &nWidth, &nHeight );
1269 if ( y != last_y ) {
1273 last_height = std::max( nHeight, last_height );
1275 // Is this texture visible?
1276 if ( ( y - nHeight - TextureBrowser_fontHeight( textureBrowser ) < originy )
1277 && ( y > originy - textureBrowser.height ) ) {
1279 // if it's the current texture, draw a thick red line, else:
1280 // shaders have a white border, simple textures don't
1281 // if !texture_showinuse: (some textures displayed may not be in use)
1282 // draw an additional square around with 0.5 1 0.5 color
1283 if ( shader_equal( TextureBrowser_GetSelectedShader( textureBrowser ), shader->getName() ) ) {
1285 if ( textureBrowser.m_rmbSelected ) {
1291 glDisable( GL_TEXTURE_2D );
1293 glBegin( GL_LINE_LOOP );
1294 glVertex2i( x - 4,y - TextureBrowser_fontHeight( textureBrowser ) + 4 );
1295 glVertex2i( x - 4,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight - 4 );
1296 glVertex2i( x + 4 + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight - 4 );
1297 glVertex2i( x + 4 + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) + 4 );
1300 glEnable( GL_TEXTURE_2D );
1307 if ( !shader->IsDefault() ) {
1309 glDisable( GL_TEXTURE_2D );
1311 glBegin( GL_LINE_LOOP );
1312 glVertex2i( x - 1,y + 1 - TextureBrowser_fontHeight( textureBrowser ) );
1313 glVertex2i( x - 1,y - nHeight - 1 - TextureBrowser_fontHeight( textureBrowser ) );
1314 glVertex2i( x + 1 + nWidth,y - nHeight - 1 - TextureBrowser_fontHeight( textureBrowser ) );
1315 glVertex2i( x + 1 + nWidth,y + 1 - TextureBrowser_fontHeight( textureBrowser ) );
1317 glEnable( GL_TEXTURE_2D );
1320 // highlight in-use textures
1321 if ( !textureBrowser.m_hideUnused && shader->IsInUse() ) {
1322 glColor3f( 0.5,1,0.5 );
1323 glDisable( GL_TEXTURE_2D );
1324 glBegin( GL_LINE_LOOP );
1325 glVertex2i( x - 3,y + 3 - TextureBrowser_fontHeight( textureBrowser ) );
1326 glVertex2i( x - 3,y - nHeight - 3 - TextureBrowser_fontHeight( textureBrowser ) );
1327 glVertex2i( x + 3 + nWidth,y - nHeight - 3 - TextureBrowser_fontHeight( textureBrowser ) );
1328 glVertex2i( x + 3 + nWidth,y + 3 - TextureBrowser_fontHeight( textureBrowser ) );
1330 glEnable( GL_TEXTURE_2D );
1334 // draw checkerboard for transparent textures
1335 if ( g_TextureBrowser_enableAlpha )
1337 glDisable( GL_TEXTURE_2D );
1338 glBegin( GL_QUADS );
1339 int font_height = TextureBrowser_fontHeight( textureBrowser );
1340 for ( int i = 0; i < nHeight; i += 8 )
1342 for ( int j = 0; j < nWidth; j += 8 )
1344 unsigned char color = (i + j) / 8 % 2 ? 0x66 : 0x99;
1345 glColor3ub( color, color, color );
1347 int right = std::min(j+8, nWidth);
1349 int bottom = std::min(i+8, nHeight);
1350 glVertex2i(x + right, y - nHeight - font_height + top);
1351 glVertex2i(x + left, y - nHeight - font_height + top);
1352 glVertex2i(x + left, y - nHeight - font_height + bottom);
1353 glVertex2i(x + right, y - nHeight - font_height + bottom);
1357 glEnable( GL_TEXTURE_2D );
1361 glBindTexture( GL_TEXTURE_2D, q->texture_number );
1362 GlobalOpenGL_debugAssertNoErrors();
1364 glBegin( GL_QUADS );
1365 glTexCoord2i( 0,0 );
1366 glVertex2i( x,y - TextureBrowser_fontHeight( textureBrowser ) );
1367 glTexCoord2i( 1,0 );
1368 glVertex2i( x + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) );
1369 glTexCoord2i( 1,1 );
1370 glVertex2i( x + nWidth,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight );
1371 glTexCoord2i( 0,1 );
1372 glVertex2i( x,y - TextureBrowser_fontHeight( textureBrowser ) - nHeight );
1375 // draw the texture name
1376 glDisable( GL_TEXTURE_2D );
1379 glRasterPos2i( x, y - TextureBrowser_fontHeight( textureBrowser ) + 5 );
1381 // don't draw the directory name
1382 const char* name = shader->getName();
1383 name += strlen( name );
1384 while ( name != shader->getName() && *( name - 1 ) != '/' && *( name - 1 ) != '\\' )
1387 GlobalOpenGL().drawString( name );
1388 glEnable( GL_TEXTURE_2D );
1391 //int totalHeight = abs(y) + last_height + TextureBrowser_fontHeight(textureBrowser) + 4;
1395 // reset the current texture
1396 glBindTexture( GL_TEXTURE_2D, 0 );
1400 void TextureBrowser_queueDraw( TextureBrowser& textureBrowser ){
1401 if ( textureBrowser.m_gl_widget ) {
1402 gtk_widget_queue_draw( textureBrowser.m_gl_widget );
1407 void TextureBrowser_setScale( TextureBrowser& textureBrowser, std::size_t scale ){
1408 textureBrowser.m_textureScale = scale;
1410 TextureBrowser_queueDraw( textureBrowser );
1413 void TextureBrowser_setUniformSize( TextureBrowser& textureBrowser, std::size_t scale ){
1414 textureBrowser.m_uniformTextureSize = scale;
1416 TextureBrowser_queueDraw( textureBrowser );
1419 void TextureBrowser_setUniformMinSize( TextureBrowser& textureBrowser, std::size_t scale ){
1420 textureBrowser.m_uniformTextureMinSize = scale;
1422 TextureBrowser_queueDraw( textureBrowser );
1425 void TextureBrowser_MouseWheel( TextureBrowser& textureBrowser, bool bUp ){
1426 int originy = TextureBrowser_getOriginY( textureBrowser );
1429 originy += int(textureBrowser.m_mouseWheelScrollIncrement);
1433 originy -= int(textureBrowser.m_mouseWheelScrollIncrement);
1436 TextureBrowser_setOriginY( textureBrowser, originy );
1439 XmlTagBuilder TagBuilder;
1447 void BuildStoreAssignedTags( ui::ListStore store, const char* shader, TextureBrowser* textureBrowser ){
1452 std::vector<CopiedString> assigned_tags;
1453 TagBuilder.GetShaderTags( shader, assigned_tags );
1455 for ( size_t i = 0; i < assigned_tags.size(); i++ )
1457 store.append(TAG_COLUMN, assigned_tags[i].c_str());
1461 void BuildStoreAvailableTags( ui::ListStore storeAvailable,
1462 ui::ListStore storeAssigned,
1463 const std::set<CopiedString>& allTags,
1464 TextureBrowser* textureBrowser ){
1465 GtkTreeIter iterAssigned;
1466 GtkTreeIter iterAvailable;
1467 std::set<CopiedString>::const_iterator iterAll;
1468 gchar* tag_assigned;
1470 storeAvailable.clear();
1472 bool row = gtk_tree_model_get_iter_first(storeAssigned, &iterAssigned ) != 0;
1474 if ( !row ) { // does the shader have tags assigned?
1475 for ( iterAll = allTags.begin(); iterAll != allTags.end(); ++iterAll )
1477 storeAvailable.append(TAG_COLUMN, (*iterAll).c_str());
1482 while ( row ) // available tags = all tags - assigned tags
1484 gtk_tree_model_get(storeAssigned, &iterAssigned, TAG_COLUMN, &tag_assigned, -1 );
1486 for ( iterAll = allTags.begin(); iterAll != allTags.end(); ++iterAll )
1488 if ( strcmp( (char*)tag_assigned, ( *iterAll ).c_str() ) != 0 ) {
1489 storeAvailable.append(TAG_COLUMN, (*iterAll).c_str());
1493 row = gtk_tree_model_iter_next(storeAssigned, &iterAssigned ) != 0;
1496 gtk_tree_model_get(storeAssigned, &iterAssigned, TAG_COLUMN, &tag_assigned, -1 );
1504 gboolean TextureBrowser_button_press( ui::Widget widget, GdkEventButton* event, TextureBrowser* textureBrowser ){
1505 if ( event->type == GDK_BUTTON_PRESS ) {
1506 if ( event->button == 3 ) {
1507 if ( textureBrowser->m_tags ) {
1508 textureBrowser->m_rmbSelected = true;
1509 TextureBrowser_Selection_MouseDown( *textureBrowser, event->state, static_cast<int>( event->x ), static_cast<int>( event->y ) );
1511 BuildStoreAssignedTags( textureBrowser->m_assigned_store, textureBrowser->shader.c_str(), textureBrowser );
1512 BuildStoreAvailableTags( textureBrowser->m_available_store, textureBrowser->m_assigned_store, textureBrowser->m_all_tags, textureBrowser );
1513 textureBrowser->m_heightChanged = true;
1514 textureBrowser->m_tag_frame.show();
1518 TextureBrowser_Focus( *textureBrowser, textureBrowser->shader.c_str() );
1522 TextureBrowser_Tracking_MouseDown( *textureBrowser );
1525 else if ( event->button == 1 ) {
1526 TextureBrowser_Selection_MouseDown( *textureBrowser, event->state, static_cast<int>( event->x ), static_cast<int>( event->y ) );
1528 if ( textureBrowser->m_tags ) {
1529 textureBrowser->m_rmbSelected = false;
1530 textureBrowser->m_tag_frame.hide();
1537 gboolean TextureBrowser_button_release( ui::Widget widget, GdkEventButton* event, TextureBrowser* textureBrowser ){
1538 if ( event->type == GDK_BUTTON_RELEASE ) {
1539 if ( event->button == 3 ) {
1540 if ( !textureBrowser->m_tags ) {
1541 TextureBrowser_Tracking_MouseUp( *textureBrowser );
1548 gboolean TextureBrowser_motion( ui::Widget widget, GdkEventMotion *event, TextureBrowser* textureBrowser ){
1552 gboolean TextureBrowser_scroll( ui::Widget widget, GdkEventScroll* event, TextureBrowser* textureBrowser ){
1553 if ( event->direction == GDK_SCROLL_UP ) {
1554 TextureBrowser_MouseWheel( *textureBrowser, true );
1556 else if ( event->direction == GDK_SCROLL_DOWN ) {
1557 TextureBrowser_MouseWheel( *textureBrowser, false );
1562 void TextureBrowser_scrollChanged( void* data, gdouble value ){
1563 //globalOutputStream() << "vertical scroll\n";
1564 TextureBrowser_setOriginY( *reinterpret_cast<TextureBrowser*>( data ), -(int)value );
1567 static void TextureBrowser_verticalScroll(ui::Adjustment adjustment, TextureBrowser* textureBrowser ){
1568 textureBrowser->m_scrollAdjustment.value_changed( gtk_adjustment_get_value(adjustment) );
1571 void TextureBrowser_updateScroll( TextureBrowser& textureBrowser ){
1572 if ( textureBrowser.m_showTextureScrollbar ) {
1573 int totalHeight = TextureBrowser_TotalHeight( textureBrowser );
1575 totalHeight = std::max( totalHeight, textureBrowser.height );
1577 auto vadjustment = gtk_range_get_adjustment( GTK_RANGE( textureBrowser.m_texture_scroll ) );
1579 gtk_adjustment_set_value(vadjustment, -TextureBrowser_getOriginY( textureBrowser ));
1580 gtk_adjustment_set_page_size(vadjustment, textureBrowser.height);
1581 gtk_adjustment_set_page_increment(vadjustment, textureBrowser.height / 2);
1582 gtk_adjustment_set_step_increment(vadjustment, 20);
1583 gtk_adjustment_set_lower(vadjustment, 0);
1584 gtk_adjustment_set_upper(vadjustment, totalHeight);
1586 g_signal_emit_by_name( G_OBJECT( vadjustment ), "changed" );
1590 gboolean TextureBrowser_size_allocate( ui::Widget widget, GtkAllocation* allocation, TextureBrowser* textureBrowser ){
1591 textureBrowser->width = allocation->width;
1592 textureBrowser->height = allocation->height;
1593 TextureBrowser_heightChanged( *textureBrowser );
1594 textureBrowser->m_originInvalid = true;
1595 TextureBrowser_queueDraw( *textureBrowser );
1599 void TextureBrowser_redraw( TextureBrowser* textureBrowser ){
1600 if ( glwidget_make_current( textureBrowser->m_gl_widget ) != FALSE ) {
1601 GlobalOpenGL_debugAssertNoErrors();
1602 TextureBrowser_evaluateHeight( *textureBrowser );
1603 Texture_Draw( *textureBrowser );
1604 GlobalOpenGL_debugAssertNoErrors();
1605 glwidget_swap_buffers( textureBrowser->m_gl_widget );
1609 gboolean TextureBrowser_expose( ui::Widget widget, GdkEventExpose* event, TextureBrowser* textureBrowser ){
1610 TextureBrowser_redraw( textureBrowser );
1614 TextureBrowser& GlobalTextureBrowser(){
1615 static TextureBrowser textureBrowser;
1616 return textureBrowser;
1619 bool TextureBrowser_hideUnused(){
1620 return GlobalTextureBrowser().m_hideUnused;
1623 void TextureBrowser_ToggleHideUnused(){
1624 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1625 if ( textureBrowser.m_hideUnused ) {
1626 TextureBrowser_SetHideUnused( textureBrowser, false );
1630 TextureBrowser_SetHideUnused( textureBrowser, true );
1634 const char* TextureGroups_transformDirName( const char* dirName, StringOutputStream *archiveName )
1636 if ( TextureBrowser_showWads() ) {
1637 archiveName->clear();
1638 *archiveName << StringRange( path_get_filename_start( dirName ), path_get_filename_base_end( dirName ) ) \
1639 << "." << path_get_extension( dirName );
1640 return archiveName->c_str();
1645 void TextureGroups_constructTreeModel( TextureGroups groups, ui::TreeStore store ){
1646 // put the information from the old textures menu into a treeview
1647 GtkTreeIter iter, child;
1649 TextureGroups::const_iterator i = groups.begin();
1650 while ( i != groups.end() )
1652 StringOutputStream archiveName;
1653 StringOutputStream nextArchiveName;
1654 const char* dirName = TextureGroups_transformDirName( ( *i ).c_str(), &archiveName );
1656 const char* firstUnderscore = strchr( dirName, '_' );
1657 StringRange dirRoot( dirName, ( firstUnderscore == 0 ) ? dirName : firstUnderscore + 1 );
1659 TextureGroups::const_iterator next = i;
1662 if ( firstUnderscore != 0
1663 && next != groups.end()
1664 && string_equal_start( TextureGroups_transformDirName( ( *next ).c_str(), &nextArchiveName ), dirRoot ) ) {
1665 gtk_tree_store_append( store, &iter, NULL );
1666 gtk_tree_store_set( store, &iter, 0, CopiedString( StringRange( dirName, firstUnderscore ) ).c_str(), -1 );
1669 while ( i != groups.end() && string_equal_start( TextureGroups_transformDirName( ( *i ).c_str(), &nextArchiveName ), dirRoot ) )
1671 gtk_tree_store_append( store, &child, &iter );
1672 gtk_tree_store_set( store, &child, 0, TextureGroups_transformDirName( ( *i ).c_str(), &nextArchiveName ), -1 );
1678 gtk_tree_store_append( store, &iter, NULL );
1679 gtk_tree_store_set( store, &iter, 0, dirName, -1 );
1685 TextureGroups TextureGroups_constructTreeView(){
1686 TextureGroups groups;
1688 if ( TextureBrowser_showWads() ) {
1689 GlobalFileSystem().forEachArchive( TextureGroupsAddWadCaller( groups ) );
1693 // scan texture dirs and pak files only if not restricting to shaderlist
1694 if ( g_pGameDescription->mGameType != "doom3" && !g_TextureBrowser_shaderlistOnly ) {
1695 GlobalFileSystem().forEachDirectory( "textures/", TextureGroupsAddDirectoryCaller( groups ) );
1698 GlobalShaderSystem().foreachShaderName( TextureGroupsAddShaderCaller( groups ) );
1704 void TextureBrowser_constructTreeStore(){
1705 TextureGroups groups = TextureGroups_constructTreeView();
1706 auto store = ui::TreeStore::from(gtk_tree_store_new( 1, G_TYPE_STRING ));
1707 TextureGroups_constructTreeModel( groups, store );
1709 gtk_tree_view_set_model(GlobalTextureBrowser().m_treeViewTree, store);
1711 g_object_unref( G_OBJECT( store ) );
1714 void TextureBrowser_constructTreeStoreTags(){
1715 TextureGroups groups;
1716 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1717 auto store = ui::TreeStore::from(gtk_tree_store_new( 1, G_TYPE_STRING ));
1718 auto model = GlobalTextureBrowser().m_all_tags_list;
1720 gtk_tree_view_set_model(GlobalTextureBrowser().m_treeViewTags, model );
1722 g_object_unref( G_OBJECT( store ) );
1725 void TreeView_onRowActivated( ui::TreeView treeview, ui::TreePath path, ui::TreeViewColumn col, gpointer userdata ){
1728 auto model = gtk_tree_view_get_model(treeview );
1730 if ( gtk_tree_model_get_iter( model, &iter, path ) ) {
1731 gchar dirName[1024];
1734 gtk_tree_model_get( model, &iter, 0, &buffer, -1 );
1735 strcpy( dirName, buffer );
1738 GlobalTextureBrowser().m_searchedTags = false;
1740 if ( !TextureBrowser_showWads() ) {
1741 strcat( dirName, "/" );
1744 ScopeDisableScreenUpdates disableScreenUpdates( dirName, "Loading Textures" );
1745 TextureBrowser_ShowDirectory( GlobalTextureBrowser(), dirName );
1746 TextureBrowser_queueDraw( GlobalTextureBrowser() );
1747 //deactivate, so SPACE and RETURN wont be broken for 2d
1748 gtk_window_set_focus( GTK_WINDOW( gtk_widget_get_toplevel( GTK_WIDGET( treeview ) ) ), NULL );
1752 void TextureBrowser_createTreeViewTree(){
1753 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1754 gtk_tree_view_set_enable_search(textureBrowser.m_treeViewTree, FALSE );
1756 gtk_tree_view_set_headers_visible(textureBrowser.m_treeViewTree, FALSE );
1757 textureBrowser.m_treeViewTree.connect( "row-activated", (GCallback) TreeView_onRowActivated, NULL );
1759 auto renderer = ui::CellRendererText(ui::New);
1760 gtk_tree_view_insert_column_with_attributes(textureBrowser.m_treeViewTree, -1, "", renderer, "text", 0, NULL );
1762 TextureBrowser_constructTreeStore();
1765 void TextureBrowser_addTag();
1767 void TextureBrowser_renameTag();
1769 void TextureBrowser_deleteTag();
1771 void TextureBrowser_createContextMenu( ui::Widget treeview, GdkEventButton *event ){
1772 ui::Widget menu = ui::Menu(ui::New);
1774 ui::Widget menuitem = ui::MenuItem( "Add tag" );
1775 menuitem.connect( "activate", (GCallback)TextureBrowser_addTag, treeview );
1776 gtk_menu_shell_append( GTK_MENU_SHELL( menu ), menuitem );
1778 menuitem = ui::MenuItem( "Rename tag" );
1779 menuitem.connect( "activate", (GCallback)TextureBrowser_renameTag, treeview );
1780 gtk_menu_shell_append( GTK_MENU_SHELL( menu ), menuitem );
1782 menuitem = ui::MenuItem( "Delete tag" );
1783 menuitem.connect( "activate", (GCallback)TextureBrowser_deleteTag, treeview );
1784 gtk_menu_shell_append( GTK_MENU_SHELL( menu ), menuitem );
1786 gtk_widget_show_all( menu );
1788 gtk_menu_popup( GTK_MENU( menu ), NULL, NULL, NULL, NULL,
1789 ( event != NULL ) ? event->button : 0,
1790 gdk_event_get_time( (GdkEvent*)event ) );
1793 gboolean TreeViewTags_onButtonPressed( ui::TreeView treeview, GdkEventButton *event ){
1794 if ( event->type == GDK_BUTTON_PRESS && event->button == 3 ) {
1796 auto selection = gtk_tree_view_get_selection(treeview );
1798 if ( gtk_tree_view_get_path_at_pos(treeview, event->x, event->y, &path, NULL, NULL, NULL ) ) {
1799 gtk_tree_selection_unselect_all( selection );
1800 gtk_tree_selection_select_path( selection, path );
1801 gtk_tree_path_free( path );
1804 TextureBrowser_createContextMenu( treeview, event );
1810 void TextureBrowser_createTreeViewTags(){
1811 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1812 textureBrowser.m_treeViewTags = ui::TreeView(ui::New);
1813 gtk_tree_view_set_enable_search(textureBrowser.m_treeViewTags, FALSE );
1815 textureBrowser.m_treeViewTags.connect( "button-press-event", (GCallback)TreeViewTags_onButtonPressed, NULL );
1817 gtk_tree_view_set_headers_visible(textureBrowser.m_treeViewTags, FALSE );
1819 auto renderer = ui::CellRendererText(ui::New);
1820 gtk_tree_view_insert_column_with_attributes(textureBrowser.m_treeViewTags, -1, "", renderer, "text", 0, NULL );
1822 TextureBrowser_constructTreeStoreTags();
1825 ui::MenuItem TextureBrowser_constructViewMenu( ui::Menu menu ){
1826 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1827 ui::MenuItem textures_menu_item = ui::MenuItem(new_sub_menu_item_with_mnemonic( "_View" ));
1829 if ( g_Layout_enableDetachableMenus.m_value ) {
1830 menu_tearoff( menu );
1833 create_check_menu_item_with_mnemonic( menu, "Hide _Unused", "ShowInUse" );
1834 if ( string_empty( g_pGameDescription->getKeyValue( "show_wads" ) ) ) {
1835 create_check_menu_item_with_mnemonic( menu, "Hide Image Missing", "FilterMissing" );
1838 // hide notex and shadernotex on texture browser: no one wants to apply them
1839 create_check_menu_item_with_mnemonic( menu, "Hide Fallback", "FilterFallback" );
1841 menu_separator( menu );
1843 create_menu_item_with_mnemonic( menu, "Show All", "ShowAllTextures" );
1845 // we always want to show shaders but don't want a "Show Shaders" menu for doom3 and .wad file games
1846 if ( g_pGameDescription->mGameType == "doom3" || TextureBrowser_showWads() ) {
1847 textureBrowser.m_showShaders = true;
1851 create_check_menu_item_with_mnemonic( menu, "Show shaders", "ToggleShowShaders" );
1854 if ( g_pGameDescription->mGameType != "doom3" && string_empty( g_pGameDescription->getKeyValue( "show_wads" ) ) ) {
1855 create_check_menu_item_with_mnemonic( menu, "Shaders Only", "ToggleShowShaderlistOnly" );
1857 if ( textureBrowser.m_tags ) {
1858 create_menu_item_with_mnemonic( menu, "Show Untagged", "ShowUntagged" );
1861 menu_separator( menu );
1862 create_check_menu_item_with_mnemonic( menu, "Fixed Size", "FixedSize" );
1863 create_check_menu_item_with_mnemonic( menu, "Transparency", "EnableAlpha" );
1865 if ( string_empty( g_pGameDescription->getKeyValue( "show_wads" ) ) ) {
1866 menu_separator( menu );
1867 textureBrowser.m_shader_info_item = ui::Widget(create_menu_item_with_mnemonic( menu, "Shader Info", "ShaderInfo" ));
1868 gtk_widget_set_sensitive( textureBrowser.m_shader_info_item, FALSE );
1872 return textures_menu_item;
1875 ui::MenuItem TextureBrowser_constructToolsMenu( ui::Menu menu ){
1876 ui::MenuItem textures_menu_item = ui::MenuItem(new_sub_menu_item_with_mnemonic( "_Tools" ));
1878 if ( g_Layout_enableDetachableMenus.m_value ) {
1879 menu_tearoff( menu );
1882 create_menu_item_with_mnemonic( menu, "Flush & Reload Shaders", "RefreshShaders" );
1883 create_menu_item_with_mnemonic( menu, "Find / Replace...", "FindReplaceTextures" );
1885 return textures_menu_item;
1888 ui::MenuItem TextureBrowser_constructTagsMenu( ui::Menu menu ){
1889 ui::MenuItem textures_menu_item = ui::MenuItem(new_sub_menu_item_with_mnemonic( "T_ags" ));
1891 if ( g_Layout_enableDetachableMenus.m_value ) {
1892 menu_tearoff( menu );
1895 create_menu_item_with_mnemonic( menu, "Add tag", "AddTag" );
1896 create_menu_item_with_mnemonic( menu, "Rename tag", "RenameTag" );
1897 create_menu_item_with_mnemonic( menu, "Delete tag", "DeleteTag" );
1898 menu_separator( menu );
1899 create_menu_item_with_mnemonic( menu, "Copy tags from selected", "CopyTag" );
1900 create_menu_item_with_mnemonic( menu, "Paste tags to selected", "PasteTag" );
1902 return textures_menu_item;
1905 gboolean TextureBrowser_tagMoveHelper( ui::TreeModel model, ui::TreePath path, GtkTreeIter* iter, GSList** selected ){
1906 g_assert( selected != NULL );
1908 auto rowref = gtk_tree_row_reference_new( model, path );
1909 *selected = g_slist_append( *selected, rowref );
1914 void TextureBrowser_assignTags(){
1915 GSList* selected = NULL;
1917 gchar* tag_assigned;
1918 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1920 auto selection = gtk_tree_view_get_selection(textureBrowser.m_available_tree );
1922 gtk_tree_selection_selected_foreach( selection, (GtkTreeSelectionForeachFunc)TextureBrowser_tagMoveHelper, &selected );
1924 if ( selected != NULL ) {
1925 for ( node = selected; node != NULL; node = node->next )
1927 auto path = gtk_tree_row_reference_get_path( (GtkTreeRowReference*)node->data );
1932 if ( gtk_tree_model_get_iter(textureBrowser.m_available_store, &iter, path ) ) {
1933 gtk_tree_model_get(textureBrowser.m_available_store, &iter, TAG_COLUMN, &tag_assigned, -1 );
1934 if ( !TagBuilder.CheckShaderTag( textureBrowser.shader.c_str() ) ) {
1935 // create a custom shader/texture entry
1936 IShader* ishader = QERApp_Shader_ForName( textureBrowser.shader.c_str() );
1937 CopiedString filename = ishader->getShaderFileName();
1939 if ( filename.empty() ) {
1941 TagBuilder.AddShaderNode( textureBrowser.shader.c_str(), CUSTOM, TEXTURE );
1945 TagBuilder.AddShaderNode( textureBrowser.shader.c_str(), CUSTOM, SHADER );
1949 TagBuilder.AddShaderTag( textureBrowser.shader.c_str(), (char*)tag_assigned, TAG );
1951 gtk_list_store_remove( textureBrowser.m_available_store, &iter );
1952 textureBrowser.m_assigned_store.append(TAG_COLUMN, tag_assigned);
1957 g_slist_foreach( selected, (GFunc)gtk_tree_row_reference_free, NULL );
1960 TagBuilder.SaveXmlDoc();
1962 g_slist_free( selected );
1965 void TextureBrowser_removeTags(){
1966 GSList* selected = NULL;
1969 TextureBrowser &textureBrowser = GlobalTextureBrowser();
1971 auto selection = gtk_tree_view_get_selection(textureBrowser.m_assigned_tree );
1973 gtk_tree_selection_selected_foreach( selection, (GtkTreeSelectionForeachFunc)TextureBrowser_tagMoveHelper, &selected );
1975 if ( selected != NULL ) {
1976 for ( node = selected; node != NULL; node = node->next )
1978 auto path = gtk_tree_row_reference_get_path( (GtkTreeRowReference*)node->data );
1983 if ( gtk_tree_model_get_iter(textureBrowser.m_assigned_store, &iter, path ) ) {
1984 gtk_tree_model_get(textureBrowser.m_assigned_store, &iter, TAG_COLUMN, &tag, -1 );
1985 TagBuilder.DeleteShaderTag( textureBrowser.shader.c_str(), tag );
1986 gtk_list_store_remove( textureBrowser.m_assigned_store, &iter );
1991 g_slist_foreach( selected, (GFunc)gtk_tree_row_reference_free, NULL );
1993 // Update the "available tags list"
1994 BuildStoreAvailableTags( textureBrowser.m_available_store, textureBrowser.m_assigned_store, textureBrowser.m_all_tags, &textureBrowser );
1997 TagBuilder.SaveXmlDoc();
1999 g_slist_free( selected );
2002 void TextureBrowser_buildTagList(){
2003 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2004 textureBrowser.m_all_tags_list.clear();
2006 std::set<CopiedString>::iterator iter;
2008 for ( iter = textureBrowser.m_all_tags.begin(); iter != textureBrowser.m_all_tags.end(); ++iter )
2010 textureBrowser.m_all_tags_list.append(TAG_COLUMN, (*iter).c_str());
2014 void TextureBrowser_searchTags(){
2015 GSList* selected = NULL;
2019 char tags_searched[256];
2020 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2022 auto selection = gtk_tree_view_get_selection(textureBrowser.m_treeViewTags );
2024 gtk_tree_selection_selected_foreach( selection, (GtkTreeSelectionForeachFunc)TextureBrowser_tagMoveHelper, &selected );
2026 if ( selected != NULL ) {
2027 strcpy( buffer, "/root/*/*[tag='" );
2028 strcpy( tags_searched, "[TAGS] " );
2030 for ( node = selected; node != NULL; node = node->next )
2032 auto path = gtk_tree_row_reference_get_path( (GtkTreeRowReference*)node->data );
2037 if ( gtk_tree_model_get_iter(textureBrowser.m_all_tags_list, &iter, path ) ) {
2038 gtk_tree_model_get(textureBrowser.m_all_tags_list, &iter, TAG_COLUMN, &tag, -1 );
2040 strcat( buffer, tag );
2041 strcat( tags_searched, tag );
2042 if ( node != g_slist_last( node ) ) {
2043 strcat( buffer, "' and tag='" );
2044 strcat( tags_searched, ", " );
2050 strcat( buffer, "']" );
2052 g_slist_foreach( selected, (GFunc)gtk_tree_row_reference_free, NULL );
2054 textureBrowser.m_found_shaders.clear(); // delete old list
2055 TagBuilder.TagSearch( buffer, textureBrowser.m_found_shaders );
2057 if ( !textureBrowser.m_found_shaders.empty() ) { // found something
2058 size_t shaders_found = textureBrowser.m_found_shaders.size();
2060 globalOutputStream() << "Found " << (unsigned int)shaders_found << " textures and shaders with " << tags_searched << "\n";
2061 ScopeDisableScreenUpdates disableScreenUpdates( "Searching...", "Loading Textures" );
2063 std::set<CopiedString>::iterator iter;
2065 for ( iter = textureBrowser.m_found_shaders.begin(); iter != textureBrowser.m_found_shaders.end(); iter++ )
2067 std::string path = ( *iter ).c_str();
2068 size_t pos = path.find_last_of( "/", path.size() );
2069 std::string name = path.substr( pos + 1, path.size() );
2070 path = path.substr( 0, pos + 1 );
2071 TextureDirectory_loadTexture( path.c_str(), name.c_str() );
2074 textureBrowser.m_searchedTags = true;
2075 g_TextureBrowser_currentDirectory = tags_searched;
2077 textureBrowser.m_nTotalHeight = 0;
2078 TextureBrowser_setOriginY( textureBrowser, 0 );
2079 TextureBrowser_heightChanged( textureBrowser );
2080 TextureBrowser_updateTitle();
2082 g_slist_free( selected );
2085 void TextureBrowser_toggleSearchButton(){
2086 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2087 gint page = gtk_notebook_get_current_page( GTK_NOTEBOOK( textureBrowser.m_tag_notebook ) );
2089 if ( page == 0 ) { // tag page
2090 gtk_widget_show_all( textureBrowser.m_search_button );
2093 textureBrowser.m_search_button.hide();
2097 void TextureBrowser_constructTagNotebook(){
2098 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2099 textureBrowser.m_tag_notebook = ui::Widget::from(gtk_notebook_new());
2100 ui::Widget labelTags = ui::Label( "Tags" );
2101 ui::Widget labelTextures = ui::Label( "Textures" );
2103 gtk_notebook_append_page( GTK_NOTEBOOK( textureBrowser.m_tag_notebook ), textureBrowser.m_scr_win_tree, labelTextures );
2104 gtk_notebook_append_page( GTK_NOTEBOOK( textureBrowser.m_tag_notebook ), textureBrowser.m_scr_win_tags, labelTags );
2106 textureBrowser.m_tag_notebook.connect( "switch-page", G_CALLBACK( TextureBrowser_toggleSearchButton ), NULL );
2108 gtk_widget_show_all( textureBrowser.m_tag_notebook );
2111 void TextureBrowser_constructSearchButton(){
2112 auto image = ui::Widget::from(gtk_image_new_from_stock( GTK_STOCK_FIND, GTK_ICON_SIZE_SMALL_TOOLBAR ));
2113 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2114 textureBrowser.m_search_button = ui::Button(ui::New);
2115 textureBrowser.m_search_button.connect( "clicked", G_CALLBACK( TextureBrowser_searchTags ), NULL );
2116 gtk_widget_set_tooltip_text(textureBrowser.m_search_button, "Search with selected tags");
2117 textureBrowser.m_search_button.add(image);
2120 void TextureBrowser_checkTagFile(){
2121 const char SHADERTAG_FILE[] = "shadertags.xml";
2122 CopiedString default_filename, rc_filename;
2123 StringOutputStream stream( 256 );
2124 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2126 stream << LocalRcPath_get();
2127 stream << SHADERTAG_FILE;
2128 rc_filename = stream.c_str();
2130 if ( file_exists( rc_filename.c_str() ) ) {
2131 textureBrowser.m_tags = TagBuilder.OpenXmlDoc( rc_filename.c_str() );
2133 if ( textureBrowser.m_tags ) {
2134 globalOutputStream() << "Loading tag file " << rc_filename.c_str() << ".\n";
2139 // load default tagfile
2141 stream << g_pGameDescription->mGameToolsPath.c_str();
2142 stream << SHADERTAG_FILE;
2143 default_filename = stream.c_str();
2145 if ( file_exists( default_filename.c_str() ) ) {
2146 textureBrowser.m_tags = TagBuilder.OpenXmlDoc( default_filename.c_str(), rc_filename.c_str() );
2148 if ( textureBrowser.m_tags ) {
2149 globalOutputStream() << "Loading default tag file " << default_filename.c_str() << ".\n";
2154 globalErrorStream() << "Unable to find default tag file " << default_filename.c_str() << ". No tag support.\n";
2159 void TextureBrowser_SetNotex(){
2160 IShader* notex = QERApp_Shader_ForName( DEFAULT_NOTEX_NAME );
2161 IShader* shadernotex = QERApp_Shader_ForName( DEFAULT_SHADERNOTEX_NAME );
2163 g_notex = notex->getTexture()->name;
2165 g_shadernotex = shadernotex->getTexture()->name;
2168 shadernotex->DecRef();
2171 static bool isGLWidgetConstructed = false;
2172 static bool isWindowConstructed = false;
2174 void TextureBrowser_constructGLWidget(){
2175 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2176 textureBrowser.m_gl_widget = glwidget_new( FALSE );
2177 g_object_ref( textureBrowser.m_gl_widget._handle );
2179 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 );
2180 gtk_widget_set_can_focus( textureBrowser.m_gl_widget, true );
2182 textureBrowser.m_sizeHandler = textureBrowser.m_gl_widget.connect( "size_allocate", G_CALLBACK( TextureBrowser_size_allocate ), &textureBrowser );
2183 textureBrowser.m_exposeHandler = textureBrowser.m_gl_widget.on_render( G_CALLBACK( TextureBrowser_expose ), &textureBrowser );
2185 textureBrowser.m_gl_widget.connect( "button_press_event", G_CALLBACK( TextureBrowser_button_press ), &textureBrowser );
2186 textureBrowser.m_gl_widget.connect( "button_release_event", G_CALLBACK( TextureBrowser_button_release ), &textureBrowser );
2187 textureBrowser.m_gl_widget.connect( "motion_notify_event", G_CALLBACK( TextureBrowser_motion ), &textureBrowser );
2188 textureBrowser.m_gl_widget.connect( "scroll_event", G_CALLBACK( TextureBrowser_scroll ), &textureBrowser );
2190 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
2191 textureBrowser.m_hframe.pack_start( textureBrowser.m_gl_widget, TRUE, TRUE, 0 );
2192 #else // !WORKAROUND_MACOS_GTK2_GLWIDGET
2193 textureBrowser.m_frame.pack_start( textureBrowser.m_gl_widget, TRUE, TRUE, 0 );
2194 #endif // !WORKAROUND_MACOS_GTK2_GLWIDGET
2196 textureBrowser.m_gl_widget.show();
2198 isGLWidgetConstructed = true;
2201 ui::Widget TextureBrowser_constructWindow( ui::Window toplevel ){
2202 // The gl_widget and the tag assignment frame should be packed into a GtkVPaned with the slider
2203 // position stored in local.pref. gtk_paned_get_position() and gtk_paned_set_position() don't
2204 // seem to work in gtk 2.4 and the arrow buttons don't handle GTK_FILL, so here's another thing
2205 // for the "once-the-gtk-libs-are-updated-TODO-list" :x
2206 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2208 TextureBrowser_checkTagFile();
2209 TextureBrowser_SetNotex();
2211 GlobalShaderSystem().setActiveShadersChangedNotify( ReferenceCaller<TextureBrowser, void(), TextureBrowser_activeShadersChanged>( textureBrowser ) );
2213 textureBrowser.m_parent = toplevel;
2215 auto table = ui::Table(3, 3, FALSE);
2216 auto vbox = ui::VBox(FALSE, 0);
2217 table.attach(vbox, {0, 1, 1, 3}, {GTK_FILL, GTK_FILL});
2220 ui::Widget menu_bar{ui::null};
2223 menu_bar = ui::Widget::from(gtk_menu_bar_new());
2224 auto menu_view = ui::Menu(ui::New);
2225 auto view_item = TextureBrowser_constructViewMenu( menu_view );
2226 gtk_menu_item_set_submenu( GTK_MENU_ITEM( view_item ), menu_view );
2227 gtk_menu_shell_append( GTK_MENU_SHELL( menu_bar ), view_item );
2229 auto menu_tools = ui::Menu(ui::New);
2230 auto tools_item = TextureBrowser_constructToolsMenu( menu_tools );
2231 gtk_menu_item_set_submenu( GTK_MENU_ITEM( tools_item ), menu_tools );
2232 gtk_menu_shell_append( GTK_MENU_SHELL( menu_bar ), tools_item );
2234 table.attach(menu_bar, {0, 3, 0, 1}, {GTK_FILL, GTK_SHRINK});
2237 { // Texture TreeView
2238 textureBrowser.m_scr_win_tree = ui::ScrolledWindow(ui::New);
2239 gtk_container_set_border_width( GTK_CONTAINER( textureBrowser.m_scr_win_tree ), 0 );
2241 // vertical only scrolling for treeview
2242 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( textureBrowser.m_scr_win_tree ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2244 textureBrowser.m_scr_win_tree.show();
2246 TextureBrowser_createTreeViewTree();
2248 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( textureBrowser.m_scr_win_tree ), textureBrowser.m_treeViewTree );
2249 textureBrowser.m_treeViewTree.show();
2251 { // gl_widget scrollbar
2252 auto w = ui::Widget::from(gtk_vscrollbar_new( ui::Adjustment( 0,0,0,1,1,0 ) ));
2253 table.attach(w, {2, 3, 1, 2}, {GTK_SHRINK, GTK_FILL});
2255 textureBrowser.m_texture_scroll = w;
2257 auto vadjustment = ui::Adjustment::from(gtk_range_get_adjustment( GTK_RANGE( textureBrowser.m_texture_scroll ) ));
2258 vadjustment.connect( "value_changed", G_CALLBACK( TextureBrowser_verticalScroll ), &textureBrowser );
2260 textureBrowser.m_texture_scroll.visible(textureBrowser.m_showTextureScrollbar);
2263 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
2264 textureBrowser.m_vframe = ui::VBox( FALSE, 0 );
2265 table.attach(textureBrowser.m_vframe, {1, 2, 1, 2});
2267 textureBrowser.m_vfiller = ui::VBox( FALSE, 0 );
2268 textureBrowser.m_vframe.pack_start( textureBrowser.m_vfiller, FALSE, FALSE, 0 );
2270 textureBrowser.m_hframe = ui::HBox( FALSE, 0 );
2271 textureBrowser.m_vframe.pack_start( textureBrowser.m_hframe, TRUE, TRUE, 0 );
2273 textureBrowser.m_hfiller = ui::HBox( FALSE, 0 );
2274 textureBrowser.m_hframe.pack_start( textureBrowser.m_hfiller, FALSE, FALSE, 0 );
2276 textureBrowser.m_vframe.show();
2277 textureBrowser.m_vfiller.show();
2278 textureBrowser.m_hframe.show(),
2279 textureBrowser.m_hfiller.show();
2280 #else // !WORKAROUND_MACOS_GTK2_GLWIDGET
2281 textureBrowser.m_frame = ui::VBox( FALSE, 0 );
2282 table.attach(textureBrowser.m_frame, {1, 2, 1, 2});
2283 textureBrowser.m_frame.show();
2284 #endif // !WORKAROUND_MACOS_GTK2_GLWIDGET
2286 TextureBrowser_constructGLWidget();
2290 if ( textureBrowser.m_tags ) {
2291 { // fill tag GtkListStore
2292 textureBrowser.m_all_tags_list = ui::ListStore::from(gtk_list_store_new( N_COLUMNS, G_TYPE_STRING ));
2293 auto sortable = GTK_TREE_SORTABLE( textureBrowser.m_all_tags_list );
2294 gtk_tree_sortable_set_sort_column_id( sortable, TAG_COLUMN, GTK_SORT_ASCENDING );
2296 TagBuilder.GetAllTags( textureBrowser.m_all_tags );
2297 TextureBrowser_buildTagList();
2300 auto menu_tags = ui::Menu(ui::New);
2301 auto tags_item = TextureBrowser_constructTagsMenu( menu_tags );
2302 gtk_menu_item_set_submenu( GTK_MENU_ITEM( tags_item ), menu_tags );
2303 gtk_menu_shell_append( GTK_MENU_SHELL( menu_bar ), tags_item );
2306 textureBrowser.m_scr_win_tags = ui::ScrolledWindow(ui::New);
2307 gtk_container_set_border_width( GTK_CONTAINER( textureBrowser.m_scr_win_tags ), 0 );
2309 // vertical only scrolling for treeview
2310 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( textureBrowser.m_scr_win_tags ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2312 TextureBrowser_createTreeViewTags();
2314 auto selection = gtk_tree_view_get_selection(textureBrowser.m_treeViewTags );
2315 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2317 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( textureBrowser.m_scr_win_tags ), textureBrowser.m_treeViewTags );
2318 textureBrowser.m_treeViewTags.show();
2320 { // Texture/Tag notebook
2321 TextureBrowser_constructTagNotebook();
2322 vbox.pack_start( textureBrowser.m_tag_notebook, TRUE, TRUE, 0 );
2324 { // Tag search button
2325 TextureBrowser_constructSearchButton();
2326 vbox.pack_end(textureBrowser.m_search_button, FALSE, FALSE, 0);
2328 auto frame_table = ui::Table(3, 3, FALSE);
2331 textureBrowser.m_tag_frame = ui::Frame( "Tag assignment" );
2332 gtk_frame_set_label_align( GTK_FRAME( textureBrowser.m_tag_frame ), 0.5, 0.5 );
2333 gtk_frame_set_shadow_type( GTK_FRAME( textureBrowser.m_tag_frame ), GTK_SHADOW_NONE );
2335 table.attach(textureBrowser.m_tag_frame, {1, 3, 2, 3}, {GTK_FILL, GTK_SHRINK});
2339 textureBrowser.m_tag_frame.add(frame_table);
2341 { // assigned tag list
2342 ui::Widget scrolled_win = ui::ScrolledWindow(ui::New);
2343 gtk_container_set_border_width( GTK_CONTAINER( scrolled_win ), 0 );
2344 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scrolled_win ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2346 textureBrowser.m_assigned_store = ui::ListStore::from(gtk_list_store_new( N_COLUMNS, G_TYPE_STRING ));
2348 auto sortable = GTK_TREE_SORTABLE( textureBrowser.m_assigned_store );
2349 gtk_tree_sortable_set_sort_column_id( sortable, TAG_COLUMN, GTK_SORT_ASCENDING );
2351 auto renderer = ui::CellRendererText(ui::New);
2353 textureBrowser.m_assigned_tree = ui::TreeView(ui::TreeModel::from(textureBrowser.m_assigned_store._handle));
2354 textureBrowser.m_assigned_store.unref();
2355 textureBrowser.m_assigned_tree.connect( "row-activated", (GCallback) TextureBrowser_removeTags, NULL );
2356 gtk_tree_view_set_headers_visible(textureBrowser.m_assigned_tree, FALSE );
2358 auto selection = gtk_tree_view_get_selection(textureBrowser.m_assigned_tree );
2359 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2361 auto column = ui::TreeViewColumn( "", renderer, {{"text", TAG_COLUMN}} );
2362 gtk_tree_view_append_column(textureBrowser.m_assigned_tree, column );
2363 textureBrowser.m_assigned_tree.show();
2365 scrolled_win.show();
2366 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( scrolled_win ), textureBrowser.m_assigned_tree );
2368 frame_table.attach(scrolled_win, {0, 1, 1, 3}, {GTK_FILL, GTK_FILL});
2370 { // available tag list
2371 ui::Widget scrolled_win = ui::ScrolledWindow(ui::New);
2372 gtk_container_set_border_width( GTK_CONTAINER( scrolled_win ), 0 );
2373 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scrolled_win ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
2375 textureBrowser.m_available_store = ui::ListStore::from(gtk_list_store_new( N_COLUMNS, G_TYPE_STRING ));
2376 auto sortable = GTK_TREE_SORTABLE( textureBrowser.m_available_store );
2377 gtk_tree_sortable_set_sort_column_id( sortable, TAG_COLUMN, GTK_SORT_ASCENDING );
2379 auto renderer = ui::CellRendererText(ui::New);
2381 textureBrowser.m_available_tree = ui::TreeView(ui::TreeModel::from(textureBrowser.m_available_store._handle));
2382 textureBrowser.m_available_store.unref();
2383 textureBrowser.m_available_tree.connect( "row-activated", (GCallback) TextureBrowser_assignTags, NULL );
2384 gtk_tree_view_set_headers_visible(textureBrowser.m_available_tree, FALSE );
2386 auto selection = gtk_tree_view_get_selection(textureBrowser.m_available_tree );
2387 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2389 auto column = ui::TreeViewColumn( "", renderer, {{"text", TAG_COLUMN}} );
2390 gtk_tree_view_append_column(textureBrowser.m_available_tree, column );
2391 textureBrowser.m_available_tree.show();
2393 scrolled_win.show();
2394 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( scrolled_win ), textureBrowser.m_available_tree );
2396 frame_table.attach(scrolled_win, {2, 3, 1, 3}, {GTK_FILL, GTK_FILL});
2398 { // tag arrow buttons
2399 auto m_btn_left = ui::Button(ui::New);
2400 auto m_btn_right = ui::Button(ui::New);
2401 auto m_arrow_left = ui::Widget::from(gtk_arrow_new( GTK_ARROW_LEFT, GTK_SHADOW_OUT ));
2402 auto m_arrow_right = ui::Widget::from(gtk_arrow_new( GTK_ARROW_RIGHT, GTK_SHADOW_OUT ));
2403 m_btn_left.add(m_arrow_left);
2404 m_btn_right.add(m_arrow_right);
2406 // workaround. the size of the tag frame depends of the requested size of the arrow buttons.
2407 m_arrow_left.dimensions(-1, 68);
2408 m_arrow_right.dimensions(-1, 68);
2410 frame_table.attach(m_btn_left, {1, 2, 1, 2}, {GTK_SHRINK, GTK_EXPAND});
2411 frame_table.attach(m_btn_right, {1, 2, 2, 3}, {GTK_SHRINK, GTK_EXPAND});
2413 m_btn_left.connect( "clicked", G_CALLBACK( TextureBrowser_assignTags ), NULL );
2414 m_btn_right.connect( "clicked", G_CALLBACK( TextureBrowser_removeTags ), NULL );
2418 m_arrow_left.show();
2419 m_arrow_right.show();
2421 { // tag fram labels
2422 ui::Widget m_lbl_assigned = ui::Label( "Assigned" );
2423 ui::Widget m_lbl_unassigned = ui::Label( "Available" );
2425 frame_table.attach(m_lbl_assigned, {0, 1, 0, 1}, {GTK_EXPAND, GTK_SHRINK});
2426 frame_table.attach(m_lbl_unassigned, {2, 3, 0, 1}, {GTK_EXPAND, GTK_SHRINK});
2428 m_lbl_assigned.show();
2429 m_lbl_unassigned.show();
2432 else { // no tag support, show the texture tree only
2433 vbox.pack_start( textureBrowser.m_scr_win_tree, TRUE, TRUE, 0 );
2436 // TODO do we need this?
2437 //gtk_container_set_focus_chain(GTK_CONTAINER(hbox_table), NULL);
2439 isWindowConstructed = true;
2444 void TextureBrowser_destroyGLWidget(){
2445 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2446 if ( isGLWidgetConstructed )
2448 g_signal_handler_disconnect( G_OBJECT( textureBrowser.m_gl_widget ), textureBrowser.m_sizeHandler );
2449 g_signal_handler_disconnect( G_OBJECT( textureBrowser.m_gl_widget ), textureBrowser.m_exposeHandler );
2451 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
2452 textureBrowser.m_hframe.remove( textureBrowser.m_gl_widget );
2453 #else // !WORKAROUND_MACOS_GTK2_GLWIDGET
2454 textureBrowser.m_frame.remove( textureBrowser.m_gl_widget );
2455 #endif // !WORKAROUND_MACOS_GTK2_GLWIDGET
2457 textureBrowser.m_gl_widget.unref();
2459 isGLWidgetConstructed = false;
2463 void TextureBrowser_destroyWindow(){
2464 GlobalShaderSystem().setActiveShadersChangedNotify( Callback<void()>() );
2466 TextureBrowser_destroyGLWidget();
2469 #ifdef WORKAROUND_MACOS_GTK2_GLWIDGET
2470 /* workaround for gtkglext on gtk 2 issue: OpenGL texture viewport being drawn over the other pages */
2471 /* this is very ugly: force the resizing of the viewport to a single bottom line by forcing the
2472 * resizing of the gl widget by expanding some empty boxes, so the widget area size is reduced
2473 * while covered by another page, so the texture viewport is still rendered over the other page
2474 * but does not annoy the user that much because it's just a line on the bottom that may even
2475 * be printed over existing bottom frame or very close to it. */
2476 void TextureBrowser_showGLWidget(){
2477 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2478 if ( isWindowConstructed && isGLWidgetConstructed )
2480 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_vfiller, FALSE, FALSE, 0, ui::Packing::START );
2481 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_hframe, TRUE, TRUE, 0, ui::Packing::START );
2482 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_hfiller, FALSE, FALSE, 0, ui::Packing::START );
2483 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_gl_widget, TRUE, TRUE, 0, ui::Packing::START );
2485 textureBrowser.m_gl_widget.show();
2489 void TextureBrowser_hideGLWidget(){
2490 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2491 if ( isWindowConstructed && isGLWidgetConstructed )
2493 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_vfiller, TRUE, TRUE, 0, ui::Packing::START);
2494 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_hframe, FALSE, FALSE, 0, ui::Packing::END );
2495 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_hfiller, TRUE, TRUE, 0, ui::Packing::START);
2496 textureBrowser.m_vframe.set_child_packing( textureBrowser.m_gl_widget, FALSE, FALSE, 0, ui::Packing::END );
2498 // The hack needs the GL widget to not be hidden to work,
2499 // so resizing it triggers the redraw of it with the new size.
2500 // GlobalTextureBrowser().m_gl_widget.hide();
2502 // Trigger the redraw.
2503 TextureBrowser_redraw( &GlobalTextureBrowser() );
2507 #endif // WORKAROUND_MACOS_GTK2_GLWIDGET
2509 const Vector3& TextureBrowser_getBackgroundColour( TextureBrowser& textureBrowser ){
2510 return textureBrowser.color_textureback;
2513 void TextureBrowser_setBackgroundColour( TextureBrowser& textureBrowser, const Vector3& colour ){
2514 textureBrowser.color_textureback = colour;
2515 TextureBrowser_queueDraw( textureBrowser );
2518 void TextureBrowser_selectionHelper( ui::TreeModel model, ui::TreePath path, GtkTreeIter* iter, GSList** selected ){
2519 g_assert( selected != NULL );
2522 gtk_tree_model_get( model, iter, TAG_COLUMN, &name, -1 );
2523 *selected = g_slist_append( *selected, name );
2526 void TextureBrowser_shaderInfo(){
2527 const char* name = TextureBrowser_GetSelectedShader( GlobalTextureBrowser() );
2528 IShader* shader = QERApp_Shader_ForName( name );
2530 DoShaderInfoDlg( name, shader->getShaderFileName(), "Shader Info" );
2535 void TextureBrowser_addTag(){
2537 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2539 EMessageBoxReturn result = DoShaderTagDlg( &tag, "Add shader tag" );
2541 if ( result == eIDOK && !tag.empty() ) {
2543 textureBrowser.m_all_tags.insert( tag.c_str() );
2544 gtk_list_store_append( textureBrowser.m_available_store, &iter );
2545 gtk_list_store_set( textureBrowser.m_available_store, &iter, TAG_COLUMN, tag.c_str(), -1 );
2547 // Select the currently added tag in the available list
2548 auto selection = gtk_tree_view_get_selection(textureBrowser.m_available_tree );
2549 gtk_tree_selection_select_iter( selection, &iter );
2551 textureBrowser.m_all_tags_list.append(TAG_COLUMN, tag.c_str());
2555 void TextureBrowser_renameTag(){
2556 /* WORKAROUND: The tag treeview is set to GTK_SELECTION_MULTIPLE. Because
2557 gtk_tree_selection_get_selected() doesn't work with GTK_SELECTION_MULTIPLE,
2558 we need to count the number of selected rows first and use
2559 gtk_tree_selection_selected_foreach() then to go through the list of selected
2560 rows (which always containins a single row).
2563 GSList* selected = NULL;
2564 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2566 auto selection = gtk_tree_view_get_selection(textureBrowser.m_treeViewTags );
2567 gtk_tree_selection_selected_foreach( selection, GtkTreeSelectionForeachFunc( TextureBrowser_selectionHelper ), &selected );
2569 if ( g_slist_length( selected ) == 1 ) { // we only rename a single tag
2570 CopiedString newTag;
2571 EMessageBoxReturn result = DoShaderTagDlg( &newTag, "Rename shader tag" );
2573 if ( result == eIDOK && !newTag.empty() ) {
2574 GtkTreeIter iterList;
2576 gchar* oldTag = (char*)selected->data;
2578 bool row = gtk_tree_model_get_iter_first(textureBrowser.m_all_tags_list, &iterList ) != 0;
2582 gtk_tree_model_get(textureBrowser.m_all_tags_list, &iterList, TAG_COLUMN, &rowTag, -1 );
2584 if ( strcmp( rowTag, oldTag ) == 0 ) {
2585 gtk_list_store_set( textureBrowser.m_all_tags_list, &iterList, TAG_COLUMN, newTag.c_str(), -1 );
2587 row = gtk_tree_model_iter_next(textureBrowser.m_all_tags_list, &iterList ) != 0;
2590 TagBuilder.RenameShaderTag( oldTag, newTag.c_str() );
2592 textureBrowser.m_all_tags.erase( (CopiedString)oldTag );
2593 textureBrowser.m_all_tags.insert( newTag );
2595 BuildStoreAssignedTags( textureBrowser.m_assigned_store, textureBrowser.shader.c_str(), &textureBrowser );
2596 BuildStoreAvailableTags( textureBrowser.m_available_store, textureBrowser.m_assigned_store, textureBrowser.m_all_tags, &textureBrowser );
2601 ui::alert( textureBrowser.m_parent, "Select a single tag for renaming." );
2605 void TextureBrowser_deleteTag(){
2606 GSList* selected = NULL;
2607 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2609 auto selection = gtk_tree_view_get_selection(textureBrowser.m_treeViewTags );
2610 gtk_tree_selection_selected_foreach( selection, GtkTreeSelectionForeachFunc( TextureBrowser_selectionHelper ), &selected );
2612 if ( g_slist_length( selected ) == 1 ) { // we only delete a single tag
2613 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 );
2615 if ( result == ui::alert_response::YES ) {
2616 GtkTreeIter iterSelected;
2619 gchar* tagSelected = (char*)selected->data;
2621 bool row = gtk_tree_model_get_iter_first(textureBrowser.m_all_tags_list, &iterSelected ) != 0;
2625 gtk_tree_model_get(textureBrowser.m_all_tags_list, &iterSelected, TAG_COLUMN, &rowTag, -1 );
2627 if ( strcmp( rowTag, tagSelected ) == 0 ) {
2628 gtk_list_store_remove( textureBrowser.m_all_tags_list, &iterSelected );
2631 row = gtk_tree_model_iter_next(textureBrowser.m_all_tags_list, &iterSelected ) != 0;
2634 TagBuilder.DeleteTag( tagSelected );
2635 textureBrowser.m_all_tags.erase( (CopiedString)tagSelected );
2637 BuildStoreAssignedTags( textureBrowser.m_assigned_store, textureBrowser.shader.c_str(), &textureBrowser );
2638 BuildStoreAvailableTags( textureBrowser.m_available_store, textureBrowser.m_assigned_store, textureBrowser.m_all_tags, &textureBrowser );
2642 ui::alert( textureBrowser.m_parent, "Select a single tag for deletion." );
2646 void TextureBrowser_copyTag(){
2647 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2648 textureBrowser.m_copied_tags.clear();
2649 TagBuilder.GetShaderTags( textureBrowser.shader.c_str(), textureBrowser.m_copied_tags );
2652 void TextureBrowser_pasteTag(){
2653 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2654 IShader* ishader = QERApp_Shader_ForName( textureBrowser.shader.c_str() );
2655 CopiedString shader = textureBrowser.shader.c_str();
2657 if ( !TagBuilder.CheckShaderTag( shader.c_str() ) ) {
2658 CopiedString shaderFile = ishader->getShaderFileName();
2659 if ( shaderFile.empty() ) {
2661 TagBuilder.AddShaderNode( shader.c_str(), CUSTOM, TEXTURE );
2666 TagBuilder.AddShaderNode( shader.c_str(), CUSTOM, SHADER );
2669 for ( size_t i = 0; i < textureBrowser.m_copied_tags.size(); ++i )
2671 TagBuilder.AddShaderTag( shader.c_str(), textureBrowser.m_copied_tags[i].c_str(), TAG );
2676 for ( size_t i = 0; i < textureBrowser.m_copied_tags.size(); ++i )
2678 if ( !TagBuilder.CheckShaderTag( shader.c_str(), textureBrowser.m_copied_tags[i].c_str() ) ) {
2679 // the tag doesn't exist - let's add it
2680 TagBuilder.AddShaderTag( shader.c_str(), textureBrowser.m_copied_tags[i].c_str(), TAG );
2687 TagBuilder.SaveXmlDoc();
2688 BuildStoreAssignedTags( textureBrowser.m_assigned_store, shader.c_str(), &textureBrowser );
2689 BuildStoreAvailableTags( textureBrowser.m_available_store, textureBrowser.m_assigned_store, textureBrowser.m_all_tags, &textureBrowser );
2692 void TextureBrowser_RefreshShaders(){
2693 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2694 ScopeDisableScreenUpdates disableScreenUpdates( "Processing...", "Loading Shaders" );
2695 GlobalShaderSystem().refresh();
2697 auto selection = gtk_tree_view_get_selection(GlobalTextureBrowser().m_treeViewTree);
2698 GtkTreeModel* model = NULL;
2700 if ( gtk_tree_selection_get_selected (selection, &model, &iter) )
2702 gchar dirName[1024];
2705 gtk_tree_model_get( model, &iter, 0, &buffer, -1 );
2706 strcpy( dirName, buffer );
2708 if ( !TextureBrowser_showWads() ) {
2709 strcat( dirName, "/" );
2711 TextureBrowser_ShowDirectory( GlobalTextureBrowser(), dirName );
2712 TextureBrowser_queueDraw( GlobalTextureBrowser() );
2716 void TextureBrowser_ToggleShowShaders(){
2717 GlobalTextureBrowser().m_showShaders ^= 1;
2718 GlobalTextureBrowser().m_showshaders_item.update();
2719 TextureBrowser_queueDraw( GlobalTextureBrowser() );
2722 void TextureBrowser_ToggleShowShaderListOnly(){
2723 g_TextureBrowser_shaderlistOnly ^= 1;
2724 GlobalTextureBrowser().m_showshaderlistonly_item.update();
2726 TextureBrowser_constructTreeStore();
2729 void TextureBrowser_showAll(){
2730 g_TextureBrowser_currentDirectory = "";
2731 GlobalTextureBrowser().m_searchedTags = false;
2732 TextureBrowser_heightChanged( GlobalTextureBrowser() );
2733 TextureBrowser_updateTitle();
2736 void TextureBrowser_showUntagged(){
2737 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2738 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 );
2740 if ( result == ui::alert_response::YES ) {
2741 textureBrowser.m_found_shaders.clear();
2742 TagBuilder.GetUntagged( textureBrowser.m_found_shaders );
2743 std::set<CopiedString>::iterator iter;
2745 ScopeDisableScreenUpdates disableScreenUpdates( "Searching untagged textures...", "Loading Textures" );
2747 for ( iter = textureBrowser.m_found_shaders.begin(); iter != textureBrowser.m_found_shaders.end(); iter++ )
2749 std::string path = ( *iter ).c_str();
2750 size_t pos = path.find_last_of( "/", path.size() );
2751 std::string name = path.substr( pos + 1, path.size() );
2752 path = path.substr( 0, pos + 1 );
2753 TextureDirectory_loadTexture( path.c_str(), name.c_str() );
2754 globalErrorStream() << path.c_str() << name.c_str() << "\n";
2757 g_TextureBrowser_currentDirectory = "Untagged";
2758 TextureBrowser_queueDraw( GlobalTextureBrowser() );
2759 TextureBrowser_heightChanged( textureBrowser );
2760 TextureBrowser_updateTitle();
2764 void TextureBrowser_FixedSize(){
2765 g_TextureBrowser_fixedSize ^= 1;
2766 GlobalTextureBrowser().m_fixedsize_item.update();
2767 TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2770 void TextureBrowser_FilterMissing(){
2771 g_TextureBrowser_filterMissing ^= 1;
2772 GlobalTextureBrowser().m_filternotex_item.update();
2773 TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2774 TextureBrowser_RefreshShaders();
2777 void TextureBrowser_FilterFallback(){
2778 g_TextureBrowser_filterFallback ^= 1;
2779 GlobalTextureBrowser().m_hidenotex_item.update();
2780 TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2781 TextureBrowser_RefreshShaders();
2784 void TextureBrowser_EnableAlpha(){
2785 g_TextureBrowser_enableAlpha ^= 1;
2786 GlobalTextureBrowser().m_enablealpha_item.update();
2787 TextureBrowser_activeShadersChanged( GlobalTextureBrowser() );
2790 void TextureBrowser_exportTitle( const Callback<void(const char *)> & importer ){
2791 StringOutputStream buffer( 64 );
2792 buffer << "Textures: ";
2793 if ( !string_empty( g_TextureBrowser_currentDirectory.c_str() ) ) {
2794 buffer << g_TextureBrowser_currentDirectory.c_str();
2800 importer( buffer.c_str() );
2803 struct TextureScale {
2804 static void Export(const TextureBrowser &self, const Callback<void(int)> &returnz) {
2805 switch (self.m_textureScale) {
2824 static void Import(TextureBrowser &self, int value) {
2827 TextureBrowser_setScale(self, 10);
2830 TextureBrowser_setScale(self, 25);
2833 TextureBrowser_setScale(self, 50);
2836 TextureBrowser_setScale(self, 100);
2839 TextureBrowser_setScale(self, 200);
2845 struct UniformTextureSize {
2846 static void Export(const TextureBrowser &self, const Callback<void(int)> &returnz) {
2847 returnz(GlobalTextureBrowser().m_uniformTextureSize);
2850 static void Import(TextureBrowser &self, int value) {
2852 TextureBrowser_setUniformSize(self, value);
2856 struct UniformTextureMinSize {
2857 static void Export(const TextureBrowser &self, const Callback<void(int)> &returnz) {
2858 returnz(GlobalTextureBrowser().m_uniformTextureMinSize);
2861 static void Import(TextureBrowser &self, int value) {
2863 TextureBrowser_setUniformSize(self, value);
2867 void TextureBrowser_constructPreferences( PreferencesPage& page ){
2868 page.appendCheckBox(
2869 "", "Texture scrollbar",
2870 make_property<TextureBrowser_ShowScrollbar>(GlobalTextureBrowser())
2873 const char* texture_scale[] = { "10%", "25%", "50%", "100%", "200%" };
2875 "Texture Thumbnail Scale",
2876 STRING_ARRAY_RANGE( texture_scale ),
2877 make_property<TextureScale>(GlobalTextureBrowser())
2880 page.appendSpinner( "Thumbnails Max Size", GlobalTextureBrowser().m_uniformTextureSize, GlobalTextureBrowser().m_uniformTextureSize, 16, 8192 );
2881 page.appendSpinner( "Thumbnails Min Size", GlobalTextureBrowser().m_uniformTextureMinSize, GlobalTextureBrowser().m_uniformTextureMinSize, 16, 8192 );
2882 page.appendEntry( "Mousewheel Increment", GlobalTextureBrowser().m_mouseWheelScrollIncrement );
2884 const char* startup_shaders[] = { "None", TextureBrowser_getCommonShadersName() };
2885 page.appendCombo( "Load Shaders at Startup", reinterpret_cast<int&>( GlobalTextureBrowser().m_startupShaders ), STRING_ARRAY_RANGE( startup_shaders ) );
2889 void TextureBrowser_constructPage( PreferenceGroup& group ){
2890 PreferencesPage page( group.createPage( "Texture Browser", "Texture Browser Preferences" ) );
2891 TextureBrowser_constructPreferences( page );
2894 void TextureBrowser_registerPreferencesPage(){
2895 PreferencesDialog_addSettingsPage( makeCallbackF(TextureBrowser_constructPage) );
2899 #include "preferencesystem.h"
2900 #include "stringio.h"
2903 void TextureClipboard_textureSelected( const char* shader );
2905 void TextureBrowser_Construct(){
2906 TextureBrowser &textureBrowser = GlobalTextureBrowser();
2908 GlobalCommands_insert( "ShaderInfo", makeCallbackF(TextureBrowser_shaderInfo) );
2909 GlobalCommands_insert( "ShowUntagged", makeCallbackF(TextureBrowser_showUntagged) );
2910 GlobalCommands_insert( "AddTag", makeCallbackF(TextureBrowser_addTag) );
2911 GlobalCommands_insert( "RenameTag", makeCallbackF(TextureBrowser_renameTag) );
2912 GlobalCommands_insert( "DeleteTag", makeCallbackF(TextureBrowser_deleteTag) );
2913 GlobalCommands_insert( "CopyTag", makeCallbackF(TextureBrowser_copyTag) );
2914 GlobalCommands_insert( "PasteTag", makeCallbackF(TextureBrowser_pasteTag) );
2915 GlobalCommands_insert( "RefreshShaders", makeCallbackF(VFS_Refresh) );
2916 GlobalToggles_insert( "ShowInUse", makeCallbackF(TextureBrowser_ToggleHideUnused), ToggleItem::AddCallbackCaller( textureBrowser.m_hideunused_item ), Accelerator( 'U' ) );
2917 GlobalCommands_insert( "ShowAllTextures", makeCallbackF(TextureBrowser_showAll), Accelerator( 'A', (GdkModifierType)GDK_CONTROL_MASK ) );
2918 GlobalCommands_insert( "ToggleTextures", makeCallbackF(TextureBrowser_toggleShow), Accelerator( 'T' ) );
2919 GlobalToggles_insert( "ToggleShowShaders", makeCallbackF(TextureBrowser_ToggleShowShaders), ToggleItem::AddCallbackCaller( textureBrowser.m_showshaders_item ) );
2920 GlobalToggles_insert( "ToggleShowShaderlistOnly", makeCallbackF(TextureBrowser_ToggleShowShaderListOnly), ToggleItem::AddCallbackCaller( textureBrowser.m_showshaderlistonly_item ) );
2921 GlobalToggles_insert( "FixedSize", makeCallbackF(TextureBrowser_FixedSize), ToggleItem::AddCallbackCaller( textureBrowser.m_fixedsize_item ) );
2922 GlobalToggles_insert( "FilterMissing", makeCallbackF(TextureBrowser_FilterMissing), ToggleItem::AddCallbackCaller( textureBrowser.m_filternotex_item ) );
2923 GlobalToggles_insert( "FilterFallback", makeCallbackF(TextureBrowser_FilterFallback), ToggleItem::AddCallbackCaller( textureBrowser.m_hidenotex_item ) );
2924 GlobalToggles_insert( "EnableAlpha", makeCallbackF(TextureBrowser_EnableAlpha), ToggleItem::AddCallbackCaller( textureBrowser.m_enablealpha_item ) );
2926 GlobalPreferenceSystem().registerPreference( "TextureScale", make_property_string<TextureScale>(textureBrowser) );
2927 GlobalPreferenceSystem().registerPreference( "UniformTextureSize", make_property_string<UniformTextureSize>(textureBrowser) );
2928 GlobalPreferenceSystem().registerPreference( "UniformTextureMinSize", make_property_string<UniformTextureMinSize>(textureBrowser) );
2929 GlobalPreferenceSystem().registerPreference( "TextureScrollbar", make_property_string<TextureBrowser_ShowScrollbar>(textureBrowser));
2930 GlobalPreferenceSystem().registerPreference( "ShowShaders", make_property_string( textureBrowser.m_showShaders ) );
2931 GlobalPreferenceSystem().registerPreference( "ShowShaderlistOnly", make_property_string( g_TextureBrowser_shaderlistOnly ) );
2932 GlobalPreferenceSystem().registerPreference( "FixedSize", make_property_string( g_TextureBrowser_fixedSize ) );
2933 GlobalPreferenceSystem().registerPreference( "FilterMissing", make_property_string( g_TextureBrowser_filterMissing ) );
2934 GlobalPreferenceSystem().registerPreference( "EnableAlpha", make_property_string( g_TextureBrowser_enableAlpha ) );
2935 GlobalPreferenceSystem().registerPreference( "LoadShaders", make_property_string( reinterpret_cast<int&>( textureBrowser.m_startupShaders ) ) );
2936 GlobalPreferenceSystem().registerPreference( "WheelMouseInc", make_property_string( textureBrowser.m_mouseWheelScrollIncrement ) );
2937 GlobalPreferenceSystem().registerPreference( "SI_Colors0", make_property_string( textureBrowser.color_textureback ) );
2939 textureBrowser.shader = texdef_name_default();
2941 Textures_setModeChangedNotify( ReferenceCaller<TextureBrowser, void(), TextureBrowser_queueDraw>( textureBrowser ) );
2943 TextureBrowser_registerPreferencesPage();
2945 GlobalShaderSystem().attach( g_ShadersObserver );
2947 TextureBrowser_textureSelected = TextureClipboard_textureSelected;
2950 void TextureBrowser_Destroy(){
2951 GlobalShaderSystem().detach( g_ShadersObserver );
2953 Textures_setModeChangedNotify( Callback<void()>() );
2956 #if WORKAROUND_WINDOWS_GTK2_GLWIDGET
2957 ui::GLArea TextureBrowser_getGLWidget(){
2958 return GlobalTextureBrowser().m_gl_widget;
2960 #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET