/
texwindow.cpp
2895 lines (2366 loc) · 103 KB
/
texwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1999-2006 Id Software, Inc. and contributors.
For a list of contributors, see the accompanying CONTRIBUTORS file.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//
// Texture Window
//
// Leonardo Zide (leo@lokigames.com)
//
#include "texwindow.h"
#include "debugging/debugging.h"
#include "warnings.h"
#include "ifilesystem.h"
#include "iundo.h"
#include "igl.h"
#include "iarchive.h"
#include "moduleobserver.h"
#include <set>
#include <string>
#include <vector>
#include <gtk/gtk.h>
#include <gtk/gtkrange.h>
#include <gtk/gtkframe.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkvscrollbar.h>
#include "signal/signal.h"
#include "math/vector.h"
#include "texturelib.h"
#include "string/string.h"
#include "shaderlib.h"
#include "os/file.h"
#include "os/path.h"
#include "stream/memstream.h"
#include "stream/textfilestream.h"
#include "stream/stringstream.h"
#include "cmdlib.h"
#include "texmanip.h"
#include "textures.h"
#include "convert.h"
#include "gtkutil/menu.h"
#include "gtkutil/nonmodal.h"
#include "gtkutil/cursor.h"
#include "gtkutil/widget.h"
#include "gtkutil/glwidget.h"
#include "gtkutil/messagebox.h"
#include "error.h"
#include "map.h"
#include "qgl.h"
#include "select.h"
#include "brush_primit.h"
#include "brushmanip.h"
#include "patchmanip.h"
#include "plugin.h"
#include "qe3.h"
#include "gtkdlgs.h"
#include "gtkmisc.h"
#include "mainframe.h"
#include "findtexturedialog.h"
#include "surfacedialog.h"
#include "patchdialog.h"
#include "groupdialog.h"
#include "preferences.h"
#include "shaders.h"
#include "commands.h"
void TextureBrowser_queueDraw( TextureBrowser& textureBrowser );
bool string_equal_start( const char* string, StringRange start ){
return string_equal_n( string, start.first, start.last - start.first );
}
typedef std::set<CopiedString> TextureGroups;
void TextureGroups_addWad( TextureGroups& groups, const char* archive ){
if ( extension_equal( path_get_extension( archive ), "wad" ) ) {
#if 1
groups.insert( archive );
#else
CopiedString archiveBaseName( path_get_filename_start( archive ), path_get_filename_base_end( archive ) );
groups.insert( archiveBaseName );
#endif
}
}
typedef ReferenceCaller1<TextureGroups, const char*, TextureGroups_addWad> TextureGroupsAddWadCaller;
void TextureGroups_addShader( TextureGroups& groups, const char* shaderName ){
const char* texture = path_make_relative( shaderName, "textures/" );
if ( texture != shaderName ) {
const char* last = path_remove_directory( texture );
if ( !string_empty( last ) ) {
groups.insert( CopiedString( StringRange( texture, --last ) ) );
}
}
}
typedef ReferenceCaller1<TextureGroups, const char*, TextureGroups_addShader> TextureGroupsAddShaderCaller;
void TextureGroups_addDirectory( TextureGroups& groups, const char* directory ){
groups.insert( directory );
}
typedef ReferenceCaller1<TextureGroups, const char*, TextureGroups_addDirectory> TextureGroupsAddDirectoryCaller;
namespace
{
bool g_TextureBrowser_shaderlistOnly = false;
bool g_TextureBrowser_fixedSize = true;
bool g_TextureBrowser_filterNotex = false;
bool g_TextureBrowser_enableAlpha = false;
bool g_TextureBrowser_filter_searchFromStart = false;
}
class DeferredAdjustment
{
gdouble m_value;
guint m_handler;
typedef void ( *ValueChangedFunction )( void* data, gdouble value );
ValueChangedFunction m_function;
void* m_data;
static gboolean deferred_value_changed( gpointer data ){
reinterpret_cast<DeferredAdjustment*>( data )->m_function(
reinterpret_cast<DeferredAdjustment*>( data )->m_data,
reinterpret_cast<DeferredAdjustment*>( data )->m_value
);
reinterpret_cast<DeferredAdjustment*>( data )->m_handler = 0;
reinterpret_cast<DeferredAdjustment*>( data )->m_value = 0;
return FALSE;
}
public:
DeferredAdjustment( ValueChangedFunction function, void* data ) : m_value( 0 ), m_handler( 0 ), m_function( function ), m_data( data ){
}
void flush(){
if ( m_handler != 0 ) {
g_source_remove( m_handler );
deferred_value_changed( this );
}
}
void value_changed( gdouble value ){
m_value = value;
if ( m_handler == 0 ) {
m_handler = g_idle_add( deferred_value_changed, this );
}
}
static void adjustment_value_changed( GtkAdjustment *adjustment, DeferredAdjustment* self ){
self->value_changed( adjustment->value );
}
};
class TextureBrowser;
typedef ReferenceCaller<TextureBrowser, TextureBrowser_queueDraw> TextureBrowserQueueDrawCaller;
void TextureBrowser_scrollChanged( void* data, gdouble value );
enum StartupShaders
{
STARTUPSHADERS_NONE = 0,
STARTUPSHADERS_COMMON,
};
void TextureBrowser_hideUnusedExport( const BoolImportCallback& importer );
typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_hideUnusedExport> TextureBrowserHideUnusedExport;
void TextureBrowser_showShadersExport( const BoolImportCallback& importer );
typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_showShadersExport> TextureBrowserShowShadersExport;
void TextureBrowser_showTexturesExport( const BoolImportCallback& importer );
typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_showTexturesExport> TextureBrowserShowTexturesExport;
void TextureBrowser_showShaderlistOnly( const BoolImportCallback& importer ){
importer( g_TextureBrowser_shaderlistOnly );
}
typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_showShaderlistOnly> TextureBrowserShowShaderlistOnlyExport;
void TextureBrowser_fixedSize( const BoolImportCallback& importer ){
importer( g_TextureBrowser_fixedSize );
}
typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_fixedSize> TextureBrowserFixedSizeExport;
void TextureBrowser_filterNotex( const BoolImportCallback& importer ){
importer( g_TextureBrowser_filterNotex );
}
typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_filterNotex> TextureBrowserFilterNotexExport;
void TextureBrowser_enableAlpha( const BoolImportCallback& importer ){
importer( g_TextureBrowser_enableAlpha );
}
typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_enableAlpha> TextureBrowserEnableAlphaExport;
void TextureBrowser_filter_searchFromStart( const BoolImportCallback& importer ){
importer( g_TextureBrowser_filter_searchFromStart );
}
typedef FreeCaller1<const BoolImportCallback&, TextureBrowser_filter_searchFromStart> TextureBrowser_filter_searchFromStartExport;
class TextureBrowser
{
public:
int width, height;
int originy;
int m_nTotalHeight;
CopiedString shader;
GtkWindow* m_parent;
GtkWidget* m_gl_widget;
GtkWidget* m_texture_scroll;
GtkWidget* m_treeViewTree;
GtkWidget* m_treeViewTags;
GtkWidget* m_tag_frame;
GtkListStore* m_assigned_store;
GtkListStore* m_available_store;
GtkWidget* m_assigned_tree;
GtkWidget* m_available_tree;
GtkWidget* m_scr_win_tree;
GtkWidget* m_scr_win_tags;
GtkWidget* m_tag_notebook;
GtkWidget* m_search_button;
GtkWidget* m_shader_info_item;
GtkWidget* m_filter_entry;
std::set<CopiedString> m_all_tags;
GtkListStore* m_all_tags_list;
std::vector<CopiedString> m_copied_tags;
std::set<CopiedString> m_found_shaders;
ToggleItem m_hideunused_item;
ToggleItem m_showshaders_item;
ToggleItem m_showtextures_item;
ToggleItem m_showshaderlistonly_item;
ToggleItem m_fixedsize_item;
ToggleItem m_filternotex_item;
ToggleItem m_enablealpha_item;
ToggleItem m_filter_searchFromStart_item;
guint m_sizeHandler;
guint m_exposeHandler;
bool m_heightChanged;
bool m_originInvalid;
DeferredAdjustment m_scrollAdjustment;
FreezePointer m_freezePointer;
Vector3 color_textureback;
// the increment step we use against the wheel mouse
std::size_t m_mouseWheelScrollIncrement;
std::size_t m_textureScale;
// make the texture increments match the grid changes
bool m_showShaders;
bool m_showTextures;
bool m_showTextureScrollbar;
StartupShaders m_startupShaders;
// if true, the texture window will only display in-use shaders
// if false, all the shaders in memory are displayed
bool m_hideUnused;
bool m_rmbSelected;
bool m_searchedTags;
bool m_tags;
bool m_move_started;
int m_move_amount;
BasicVector2<int> m_move_start;
// The uniform size (in pixels) that textures are resized to when m_resizeTextures is true.
int m_uniformTextureSize;
int m_uniformTextureMinSize;
bool m_hideNonShadersInCommon;
static bool wads;
// Return the display width of a texture in the texture browser
void getTextureWH( qtexture_t* tex, int &W, int &H ){
// Don't use uniform size
W = (int)( tex->width * ( (float)m_textureScale / 100 ) );
H = (int)( tex->height * ( (float)m_textureScale / 100 ) );
if ( W < 1 ) W = 1;
if ( H < 1 ) H = 1;
if ( g_TextureBrowser_fixedSize ){
if ( W >= H ) {
// Texture is square, or wider than it is tall
if ( W >= m_uniformTextureSize ){
H = m_uniformTextureSize * H / W;
W = m_uniformTextureSize;
}
else if ( W <= m_uniformTextureMinSize ){
H = m_uniformTextureMinSize * H / W;
W = m_uniformTextureMinSize;
}
}
else {
// Texture taller than it is wide
if ( H >= m_uniformTextureSize ){
W = m_uniformTextureSize * W / H;
H = m_uniformTextureSize;
}
else if ( H <= m_uniformTextureMinSize ){
W = m_uniformTextureMinSize * W / H;
H = m_uniformTextureMinSize;
}
}
}
}
TextureBrowser() :
m_texture_scroll( 0 ),
m_hideunused_item( TextureBrowserHideUnusedExport() ),
m_showshaders_item( TextureBrowserShowShadersExport() ),
m_showtextures_item( TextureBrowserShowTexturesExport() ),
m_showshaderlistonly_item( TextureBrowserShowShaderlistOnlyExport() ),
m_fixedsize_item( TextureBrowserFixedSizeExport() ),
m_filternotex_item( TextureBrowserFilterNotexExport() ),
m_enablealpha_item( TextureBrowserEnableAlphaExport() ),
m_filter_searchFromStart_item( TextureBrowser_filter_searchFromStartExport() ),
m_heightChanged( true ),
m_originInvalid( true ),
m_scrollAdjustment( TextureBrowser_scrollChanged, this ),
color_textureback( 0.25f, 0.25f, 0.25f ),
m_mouseWheelScrollIncrement( 64 ),
m_textureScale( 50 ),
m_showShaders( true ),
m_showTextures( true ),
m_showTextureScrollbar( true ),
m_startupShaders( STARTUPSHADERS_NONE ),
m_hideUnused( false ),
m_rmbSelected( false ),
m_searchedTags( false ),
m_tags( false ),
m_move_started( false ),
m_uniformTextureSize( 160 ),
m_uniformTextureMinSize( 48 ),
m_hideNonShadersInCommon( true ){
}
};
bool TextureBrowser::wads = false;
void ( *TextureBrowser_textureSelected )( const char* shader );
void TextureBrowser_updateScroll( TextureBrowser& textureBrowser );
const char* TextureBrowser_getComonShadersName(){
const char* value = g_pGameDescription->getKeyValue( "common_shaders_name" );
if ( !string_empty( value ) ) {
return value;
}
return "Common";
}
const char* TextureBrowser_getComonShadersDir(){
const char* value = g_pGameDescription->getKeyValue( "common_shaders_dir" );
if ( !string_empty( value ) ) {
return value;
}
return "common/";
}
inline int TextureBrowser_fontHeight( TextureBrowser& textureBrowser ){
return GlobalOpenGL().m_font->getPixelHeight();
}
const char* TextureBrowser_GetSelectedShader( TextureBrowser& textureBrowser ){
return textureBrowser.shader.c_str();
}
void TextureBrowser_SetStatus( TextureBrowser& textureBrowser, const char* name ){
IShader* shader = QERApp_Shader_ForName( name );
qtexture_t* q = shader->getTexture();
StringOutputStream strTex( 256 );
strTex << ( string_equal_prefix_nocase( name, "textures/" )? name + 9 : name ) << " W: " << Unsigned( q->width ) << " H: " << Unsigned( q->height );
shader->DecRef();
g_pParentWnd->SetStatusText( c_status_texture, strTex.c_str() );
}
void TextureBrowser_Focus( TextureBrowser& textureBrowser, const char* name );
void TextureBrowser_SetSelectedShader( TextureBrowser& textureBrowser, const char* shader ){
textureBrowser.shader = shader;
TextureBrowser_SetStatus( textureBrowser, shader );
TextureBrowser_Focus( textureBrowser, shader );
if ( FindTextureDialog_isOpen() ) {
FindTextureDialog_selectTexture( shader );
}
// disable the menu item "shader info" if no shader was selected
if ( textureBrowser.m_shader_info_item == NULL ){
return;
}
IShader* ishader = QERApp_Shader_ForName( shader );
CopiedString filename = ishader->getShaderFileName();
if ( filename.empty() ) {
gtk_widget_set_sensitive( textureBrowser.m_shader_info_item, FALSE );
}
else {
gtk_widget_set_sensitive( textureBrowser.m_shader_info_item, TRUE );
}
ishader->DecRef();
}
CopiedString g_TextureBrowser_currentDirectory;
/*
============================================================================
TEXTURE LAYOUT
TTimo: now based on a rundown through all the shaders
NOTE: we expect the Active shaders count doesn't change during a Texture_StartPos .. Texture_NextPos cycle
otherwise we may need to rely on a list instead of an array storage
============================================================================
*/
class TextureLayout
{
public:
// texture layout functions
// TTimo: now based on shaders
int current_x, current_y, current_row;
};
void Texture_StartPos( TextureLayout& layout ){
layout.current_x = 8;
layout.current_y = -8;
layout.current_row = 0;
}
void Texture_NextPos( TextureBrowser& textureBrowser, TextureLayout& layout, qtexture_t* current_texture, int *x, int *y ){
qtexture_t* q = current_texture;
int nWidth, nHeight;
textureBrowser.getTextureWH( q, nWidth, nHeight );
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
layout.current_x = 8;
layout.current_y -= layout.current_row + TextureBrowser_fontHeight( textureBrowser ) + 5;//+4
layout.current_row = 0;
}
*x = layout.current_x;
*y = layout.current_y;
// Is our texture larger than the row? If so, grow the
// row height to match it
if ( layout.current_row < nHeight ) {
layout.current_row = nHeight;
}
// never go less than 96, or the names get all crunched up
layout.current_x += nWidth < 96 ? 96 : nWidth;
layout.current_x += 8;
}
bool TextureSearch_IsShown( const char* name ){
std::set<CopiedString>::iterator iter;
iter = GlobalTextureBrowser().m_found_shaders.find( name );
return iter != GlobalTextureBrowser().m_found_shaders.end();
}
bool Texture_filtered( const char* name, TextureBrowser& textureBrowser ){
const char* filter = gtk_entry_get_text( GTK_ENTRY( textureBrowser.m_filter_entry ) );
if( string_empty( filter ) ){
return false;
}
if( g_TextureBrowser_filter_searchFromStart ){
if( string_equal_prefix_nocase( name, filter ) ){
return false;
}
}
else{
if( string_in_string_nocase( name, filter ) != 0 ){
return false;
}
}
return true;
}
CopiedString g_notex;
CopiedString g_shadernotex;
// if texture_showinuse jump over non in-use textures
/*
bool show_shaders, bool show_textures, bool hideUnused, bool hideNonShadersInCommon
textureBrowser.m_showShaders, textureBrowser.m_showTextures, textureBrowser.m_hideUnused, textureBrowser.m_hideNonShadersInCommon
*/
bool Texture_IsShown( IShader* shader, TextureBrowser& textureBrowser ){
// filter notex / shadernotex images
if ( g_TextureBrowser_filterNotex && ( string_equal( g_notex.c_str(), shader->getTexture()->name ) || string_equal( g_shadernotex.c_str(), shader->getTexture()->name ) ) ) {
return false;
}
if ( g_TextureBrowser_currentDirectory == "Untagged" ) {
std::set<CopiedString>::iterator iter;
iter = textureBrowser.m_found_shaders.find( shader->getName() );
if ( iter == textureBrowser.m_found_shaders.end() ) {
return false;
}
else {
return true;
}
}
if ( !shader_equal_prefix( shader->getName(), "textures/" ) ) {
return false;
}
if ( !textureBrowser.m_showShaders && !shader->IsDefault() ) {
return false;
}
if ( !textureBrowser.m_showTextures && shader->IsDefault() ) {
return false;
}
if ( textureBrowser.m_hideUnused && !shader->IsInUse() ) {
return false;
}
if( textureBrowser.m_hideNonShadersInCommon && shader->IsDefault() && !shader->IsInUse() //&& g_TextureBrowser_currentDirectory != ""
&& shader_equal_prefix( shader_get_textureName( shader->getName() ), TextureBrowser_getComonShadersDir() ) ){
return false;
}
if ( textureBrowser.m_searchedTags ) {
return TextureSearch_IsShown( shader->getName() );
}
else {
if ( !shader_equal_prefix( shader_get_textureName( shader->getName() ), g_TextureBrowser_currentDirectory.c_str() ) ) {
return false;
}
}
if( Texture_filtered( path_get_filename_start( shader->getName() ), textureBrowser ) ){
return false;
}
return true;
}
void TextureBrowser_heightChanged( TextureBrowser& textureBrowser ){
textureBrowser.m_heightChanged = true;
TextureBrowser_updateScroll( textureBrowser );
TextureBrowser_queueDraw( textureBrowser );
}
void TextureBrowser_evaluateHeight( TextureBrowser& textureBrowser ){
if ( textureBrowser.m_heightChanged ) {
textureBrowser.m_heightChanged = false;
textureBrowser.m_nTotalHeight = 0;
TextureLayout layout;
Texture_StartPos( layout );
for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
{
IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
if ( !Texture_IsShown( shader, textureBrowser ) ) {
continue;
}
int x, y;
Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
int nWidth, nHeight;
textureBrowser.getTextureWH( shader->getTexture(), nWidth, nHeight );
textureBrowser.m_nTotalHeight = std::max( textureBrowser.m_nTotalHeight, abs( layout.current_y ) + TextureBrowser_fontHeight( textureBrowser ) + nHeight + 4 );
}
}
}
int TextureBrowser_TotalHeight( TextureBrowser& textureBrowser ){
TextureBrowser_evaluateHeight( textureBrowser );
return textureBrowser.m_nTotalHeight;
}
inline const int& min_int( const int& left, const int& right ){
return std::min( left, right );
}
void TextureBrowser_clampOriginY( TextureBrowser& textureBrowser ){
if ( textureBrowser.originy > 0 ) {
textureBrowser.originy = 0;
}
int lower = min_int( textureBrowser.height - TextureBrowser_TotalHeight( textureBrowser ), 0 );
if ( textureBrowser.originy < lower ) {
textureBrowser.originy = lower;
}
}
int TextureBrowser_getOriginY( TextureBrowser& textureBrowser ){
if ( textureBrowser.m_originInvalid ) {
textureBrowser.m_originInvalid = false;
TextureBrowser_clampOriginY( textureBrowser );
TextureBrowser_updateScroll( textureBrowser );
}
return textureBrowser.originy;
}
void TextureBrowser_setOriginY( TextureBrowser& textureBrowser, int originy ){
textureBrowser.originy = originy;
TextureBrowser_clampOriginY( textureBrowser );
TextureBrowser_updateScroll( textureBrowser );
TextureBrowser_queueDraw( textureBrowser );
}
Signal0 g_activeShadersChangedCallbacks;
void TextureBrowser_addActiveShadersChangedCallback( const SignalHandler& handler ){
g_activeShadersChangedCallbacks.connectLast( handler );
}
void TextureBrowser_constructTreeStore();
class ShadersObserver : public ModuleObserver
{
Signal0 m_realiseCallbacks;
public:
void realise(){
m_realiseCallbacks();
}
void unrealise(){
}
void insert( const SignalHandler& handler ){
m_realiseCallbacks.connectLast( handler );
}
};
namespace
{
ShadersObserver g_ShadersObserver;
}
void TextureBrowser_addShadersRealiseCallback( const SignalHandler& handler ){
g_ShadersObserver.insert( handler );
}
void TextureBrowser_activeShadersChanged( TextureBrowser& textureBrowser ){
TextureBrowser_heightChanged( textureBrowser );
textureBrowser.m_originInvalid = true;
g_activeShadersChangedCallbacks();
}
void TextureBrowser_importShowScrollbar( TextureBrowser& textureBrowser, bool value ){
textureBrowser.m_showTextureScrollbar = value;
if ( textureBrowser.m_texture_scroll != 0 ) {
widget_set_visible( textureBrowser.m_texture_scroll, textureBrowser.m_showTextureScrollbar );
TextureBrowser_updateScroll( textureBrowser );
}
}
typedef ReferenceCaller1<TextureBrowser, bool, TextureBrowser_importShowScrollbar> TextureBrowserImportShowScrollbarCaller;
/*
==============
TextureBrowser_ShowDirectory
relies on texture_directory global for the directory to use
1) Load the shaders for the given directory
2) Scan the remaining texture, load them and assign them a default shader (the "noshader" shader)
NOTE: when writing a texture plugin, or some texture extensions, this function may need to be overriden, and made
available through the IShaders interface
NOTE: for texture window layout:
all shaders are stored with alphabetical order after load
previously loaded and displayed stuff is hidden, only in-use and newly loaded is shown
( the GL textures are not flushed though)
==============
*/
bool endswith( const char *haystack, const char *needle ){
size_t lh = strlen( haystack );
size_t ln = strlen( needle );
if ( lh < ln ) {
return false;
}
return !memcmp( haystack + ( lh - ln ), needle, ln );
}
bool texture_name_ignore( const char* name ){
StringOutputStream strTemp( string_length( name ) );
strTemp << LowerCase( name );
return
endswith( strTemp.c_str(), ".specular" ) ||
endswith( strTemp.c_str(), ".glow" ) ||
endswith( strTemp.c_str(), ".bump" ) ||
endswith( strTemp.c_str(), ".diffuse" ) ||
endswith( strTemp.c_str(), ".blend" ) ||
endswith( strTemp.c_str(), ".alpha" ) ||
endswith( strTemp.c_str(), "_norm" ) ||
endswith( strTemp.c_str(), "_bump" ) ||
endswith( strTemp.c_str(), "_glow" ) ||
endswith( strTemp.c_str(), "_gloss" ) ||
endswith( strTemp.c_str(), "_pants" ) ||
endswith( strTemp.c_str(), "_shirt" ) ||
endswith( strTemp.c_str(), "_reflect" ) ||
endswith( strTemp.c_str(), "_alpha" ) ||
0;
}
class LoadShaderVisitor : public Archive::Visitor
{
public:
void visit( const char* name ){
IShader* shader = QERApp_Shader_ForName( CopiedString( StringRange( name, path_get_filename_base_end( name ) ) ).c_str() );
shader->DecRef();
}
};
void TextureBrowser_SetHideUnused( TextureBrowser& textureBrowser, bool hideUnused );
GtkWidget* g_page_textures;
void TextureBrowser_toggleShow(){
GroupDialog_showPage( g_page_textures );
}
void TextureBrowser_updateTitle(){
GroupDialog_updatePageTitle( g_page_textures );
}
class TextureCategoryLoadShader
{
const char* m_directory;
std::size_t& m_count;
public:
typedef const char* first_argument_type;
TextureCategoryLoadShader( const char* directory, std::size_t& count )
: m_directory( directory ), m_count( count ){
m_count = 0;
}
void operator()( const char* name ) const {
if ( shader_equal_prefix( name, "textures/" )
&& shader_equal_prefix( name + string_length( "textures/" ), m_directory ) ) {
++m_count;
// request the shader, this will load the texture if needed
// this Shader_ForName call is a kind of hack
IShader *pFoo = QERApp_Shader_ForName( name );
pFoo->DecRef();
}
}
};
void TextureDirectory_loadTexture( const char* directory, const char* texture ){
StringOutputStream name( 256 );
name << directory << StringRange( texture, path_get_filename_base_end( texture ) );
if ( texture_name_ignore( name.c_str() ) ) {
return;
}
if ( !shader_valid( name.c_str() ) ) {
globalWarningStream() << "Skipping invalid texture name: [" << name.c_str() << "]\n";
return;
}
// if a texture is already in use to represent a shader, ignore it
IShader* shader = QERApp_Shader_ForName( name.c_str() );
shader->DecRef();
}
typedef ConstPointerCaller1<char, const char*, TextureDirectory_loadTexture> TextureDirectoryLoadTextureCaller;
class LoadTexturesByTypeVisitor : public ImageModules::Visitor
{
const char* m_dirstring;
public:
LoadTexturesByTypeVisitor( const char* dirstring )
: m_dirstring( dirstring ){
}
void visit( const char* minor, const _QERPlugImageTable& table ) const {
GlobalFileSystem().forEachFile( m_dirstring, minor, TextureDirectoryLoadTextureCaller( m_dirstring ) );
}
};
void TextureBrowser_ShowDirectory( TextureBrowser& textureBrowser, const char* directory ){
textureBrowser.m_searchedTags = false;
if ( TextureBrowser::wads ) {
Archive* archive = GlobalFileSystem().getArchive( directory );
//ASSERT_NOTNULL( archive );
if( archive ){
globalOutputStream() << "Loading " << makeQuoted( directory ) << " wad file.\n";
LoadShaderVisitor visitor;
archive->forEachFile( Archive::VisitorFunc( visitor, Archive::eFiles, 0 ), "textures/" );
}
else{
globalErrorStream() << "Attempted to load " << makeQuoted( directory ) << " wad file.\n";
}
}
else
{
g_TextureBrowser_currentDirectory = directory;
std::size_t shaders_count;
GlobalShaderSystem().foreachShaderName( makeCallback1( TextureCategoryLoadShader( directory, shaders_count ) ) );
globalOutputStream() << "Showing " << Unsigned( shaders_count ) << " shaders.\n";
if ( g_pGameDescription->mGameType != "doom3" ) {
// load remaining texture files
StringOutputStream dirstring( 64 );
dirstring << "textures/" << directory;
Radiant_getImageModules().foreachModule( LoadTexturesByTypeVisitor( dirstring.c_str() ) );
}
}
TextureBrowser_SetHideUnused( textureBrowser, false );
TextureBrowser_updateTitle();
}
void TextureBrowser_ShowTagSearchResult( TextureBrowser& textureBrowser, const char* directory ){
g_TextureBrowser_currentDirectory = directory;
TextureBrowser_heightChanged( textureBrowser );
std::size_t shaders_count;
GlobalShaderSystem().foreachShaderName( makeCallback1( TextureCategoryLoadShader( directory, shaders_count ) ) );
globalOutputStream() << "Showing " << Unsigned( shaders_count ) << " shaders.\n";
if ( g_pGameDescription->mGameType != "doom3" ) {
// load remaining texture files
StringOutputStream dirstring( 64 );
dirstring << "textures/" << directory;
{
LoadTexturesByTypeVisitor visitor( dirstring.c_str() );
Radiant_getImageModules().foreachModule( visitor );
}
}
// we'll display the newly loaded textures + all the ones already in use
TextureBrowser_SetHideUnused( textureBrowser, false );
}
void TextureBrowser_hideUnusedExport( const BoolImportCallback& importer ){
importer( GlobalTextureBrowser().m_hideUnused );
}
void TextureBrowser_showShadersExport( const BoolImportCallback& importer ){
importer( GlobalTextureBrowser().m_showShaders );
}
void TextureBrowser_showTexturesExport( const BoolImportCallback& importer ){
importer( GlobalTextureBrowser().m_showTextures );
}
void TextureBrowser_SetHideUnused( TextureBrowser& textureBrowser, bool hideUnused ){
textureBrowser.m_hideUnused = hideUnused;
textureBrowser.m_hideunused_item.update();
TextureBrowser_heightChanged( textureBrowser );
textureBrowser.m_originInvalid = true;
}
void TextureBrowser_ShowStartupShaders( TextureBrowser& textureBrowser ){
if ( textureBrowser.m_startupShaders == STARTUPSHADERS_COMMON ) {
TextureBrowser_ShowDirectory( textureBrowser, TextureBrowser_getComonShadersDir() );
}
}
//++timo NOTE: this is a mix of Shader module stuff and texture explorer
// it might need to be split in parts or moved out .. dunno
// scroll origin so the specified texture is completely on screen
// if current texture is not displayed, nothing is changed
void TextureBrowser_Focus( TextureBrowser& textureBrowser, const char* name ){
TextureLayout layout;
// scroll origin so the texture is completely on screen
Texture_StartPos( layout );
for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
{
IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
if ( !Texture_IsShown( shader, textureBrowser ) ) {
continue;
}
int x, y;
Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
qtexture_t* q = shader->getTexture();
if ( !q ) {
break;
}
// we have found when texdef->name and the shader name match
// NOTE: as everywhere else for our comparisons, we are not case sensitive
if ( shader_equal( name, shader->getName() ) ) {
//int textureHeight = (int)( q->height * ( (float)textureBrowser.m_textureScale / 100 ) ) + 2 * TextureBrowser_fontHeight( textureBrowser );
int textureWidth, textureHeight;
textureBrowser.getTextureWH( q, textureWidth, textureHeight );
textureHeight += 2 * TextureBrowser_fontHeight( textureBrowser );
int originy = TextureBrowser_getOriginY( textureBrowser );
if ( y > originy ) {
originy = y + 4;
}
if ( y - textureHeight < originy - textureBrowser.height ) {
originy = ( y - textureHeight ) + textureBrowser.height;
}
TextureBrowser_setOriginY( textureBrowser, originy );
return;
}
}
}
IShader* Texture_At( TextureBrowser& textureBrowser, int mx, int my ){
my += TextureBrowser_getOriginY( textureBrowser ) - textureBrowser.height;
TextureLayout layout;
Texture_StartPos( layout );
for ( QERApp_ActiveShaders_IteratorBegin(); !QERApp_ActiveShaders_IteratorAtEnd(); QERApp_ActiveShaders_IteratorIncrement() )
{
IShader* shader = QERApp_ActiveShaders_IteratorCurrent();
if ( !Texture_IsShown( shader, textureBrowser ) ) {
continue;
}
int x, y;
Texture_NextPos( textureBrowser, layout, shader->getTexture(), &x, &y );
qtexture_t *q = shader->getTexture();
if ( !q ) {
break;
}
int nWidth, nHeight;
textureBrowser.getTextureWH( q, nWidth, nHeight );
if ( mx > x && mx - x < nWidth
&& my < y && y - my < nHeight + TextureBrowser_fontHeight( textureBrowser ) ) {
return shader;
}
}
return 0;
}
/*
==============
SelectTexture
By mouse click
==============
*/
void SelectTexture( TextureBrowser& textureBrowser, int mx, int my, guint32 flags, bool texturizeSelection ){
if ( ( flags & GDK_SHIFT_MASK ) == 0 ) {
IShader* shader = Texture_At( textureBrowser, mx, my );
if ( shader != 0 ) {
TextureBrowser_SetSelectedShader( textureBrowser, shader->getName() );
TextureBrowser_textureSelected( shader->getName() );
if ( !FindTextureDialog_isOpen() && !textureBrowser.m_rmbSelected && !texturizeSelection ) {
Select_SetShader_Undo( shader->getName() );
}