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 "surfacedialog.h"
32 #include "debugging/debugging.h"
35 #include "iscenegraph.h"
38 #include "iselection.h"
40 #include <gdk/gdkkeysyms.h>
42 #include "signal/isignal.h"
43 #include "generic/object.h"
44 #include "math/vector.h"
45 #include "texturelib.h"
46 #include "shaderlib.h"
49 #include "gtkutil/idledraw.h"
50 #include "gtkutil/dialog.h"
51 #include "gtkutil/entry.h"
52 #include "gtkutil/nonmodal.h"
53 #include "gtkutil/pointer.h"
54 #include "gtkutil/glwidget.h" //Shamus: For Textool
55 #include "gtkutil/button.h"
58 #include "patchmanip.h"
59 #include "brushmanip.h"
60 #include "patchdialog.h"
61 #include "preferences.h"
62 #include "brush_primit.h"
64 #include "mainframe.h"
67 #include "brush.h" //Shamus: for Textool
70 #include "stream/stringstream.h"
72 #include "textureentry.h"
74 //NOTE: Proper functioning of Textool currently requires that the "#if 1" lines in
75 // brush_primit.h be changed to "#if 0". add/removeScale screws this up ATM. :-)
76 // Plus, Radiant seems to work just fine without that stuff. ;-)
78 #define TEXTOOL_ENABLED 0
85 //Shamus: Textool function prototypes
86 gboolean size_allocate( ui::Widget, GtkAllocation *, gpointer );
87 gboolean expose( ui::Widget, GdkEventExpose *, gpointer );
88 gboolean button_press( ui::Widget, GdkEventButton *, gpointer );
89 gboolean button_release( ui::Widget, GdkEventButton *, gpointer );
90 gboolean motion( ui::Widget, GdkEventMotion *, gpointer );
91 void flipX( ui::ToggleButton, gpointer );
92 void flipY( ui::ToggleButton, gpointer );
94 //End Textool function prototypes
96 //Shamus: Textool globals
97 ui::Widget g_textoolWin;
101 gtk_widget_queue_draw( g_textoolWin );
108 inline void spin_button_set_step( ui::SpinButton spin, gfloat step ){
110 gtk_adjustment_set_step_increment(gtk_spin_button_get_adjustment( spin ), step);
112 GValue gvalue = GValue_default();
113 g_value_init( &gvalue, G_TYPE_DOUBLE );
114 g_value_set_double( &gvalue, step );
115 g_object_set( G_OBJECT( gtk_spin_button_get_adjustment( spin ) ), "step-increment", &gvalue, NULL );
123 ui::SpinButton m_spin;
125 Increment( float& f ) : m_f( f ), m_spin( ui::null ), m_entry( ui::null ){
128 entry_set_float( m_entry, m_f );
130 typedef MemberCaller<Increment, void(), &Increment::cancel> CancelCaller;
132 m_f = static_cast<float>( entry_get_float( m_entry ) );
133 spin_button_set_step( m_spin, m_f );
135 typedef MemberCaller<Increment, void(), &Increment::apply> ApplyCaller;
138 void SurfaceInspector_GridChange();
140 class SurfaceInspector : public Dialog
142 ui::Window BuildDialog();
144 NonModalEntry m_textureEntry;
145 NonModalSpinner m_hshiftSpinner;
146 NonModalEntry m_hshiftEntry;
147 NonModalSpinner m_vshiftSpinner;
148 NonModalEntry m_vshiftEntry;
149 NonModalSpinner m_hscaleSpinner;
150 NonModalEntry m_hscaleEntry;
151 NonModalSpinner m_vscaleSpinner;
152 NonModalEntry m_vscaleEntry;
153 NonModalSpinner m_rotateSpinner;
154 NonModalEntry m_rotateEntry;
158 GtkCheckButton* m_surfaceFlags[32];
159 GtkCheckButton* m_contentFlags[32];
161 NonModalEntry m_valueEntry;
162 ui::Entry m_valueEntryWidget{ui::null};
164 WindowPositionTracker m_positionTracker;
167 float m_fitHorizontal;
170 Increment m_hshiftIncrement;
171 Increment m_vshiftIncrement;
172 Increment m_hscaleIncrement;
173 Increment m_vscaleIncrement;
174 Increment m_rotateIncrement;
175 ui::Entry m_texture{ui::null};
178 m_textureEntry( ApplyShaderCaller( *this ), UpdateCaller( *this ) ),
179 m_hshiftSpinner( ApplyTexdefCaller( *this ), UpdateCaller( *this ) ),
180 m_hshiftEntry( Increment::ApplyCaller( m_hshiftIncrement ), Increment::CancelCaller( m_hshiftIncrement ) ),
181 m_vshiftSpinner( ApplyTexdefCaller( *this ), UpdateCaller( *this ) ),
182 m_vshiftEntry( Increment::ApplyCaller( m_vshiftIncrement ), Increment::CancelCaller( m_vshiftIncrement ) ),
183 m_hscaleSpinner( ApplyTexdefCaller( *this ), UpdateCaller( *this ) ),
184 m_hscaleEntry( Increment::ApplyCaller( m_hscaleIncrement ), Increment::CancelCaller( m_hscaleIncrement ) ),
185 m_vscaleSpinner( ApplyTexdefCaller( *this ), UpdateCaller( *this ) ),
186 m_vscaleEntry( Increment::ApplyCaller( m_vscaleIncrement ), Increment::CancelCaller( m_vscaleIncrement ) ),
187 m_rotateSpinner( ApplyTexdefCaller( *this ), UpdateCaller( *this ) ),
188 m_rotateEntry( Increment::ApplyCaller( m_rotateIncrement ), Increment::CancelCaller( m_rotateIncrement ) ),
189 m_idleDraw( UpdateCaller( *this ) ),
190 m_valueEntry( ApplyFlagsCaller( *this ), UpdateCaller( *this ) ),
191 m_hshiftIncrement( g_si_globals.shift[0] ),
192 m_vshiftIncrement( g_si_globals.shift[1] ),
193 m_hscaleIncrement( g_si_globals.scale[0] ),
194 m_vscaleIncrement( g_si_globals.scale[1] ),
195 m_rotateIncrement( g_si_globals.rotate ){
198 m_positionTracker.setPosition( c_default_window_pos );
201 void constructWindow( ui::Window main_window ){
202 m_parent = main_window;
204 AddGridChangeCallback( FreeCaller<void(), SurfaceInspector_GridChange>() );
206 void destroyWindow(){
210 return GetWidget().visible();
214 m_idleDraw.queueDraw();
219 typedef MemberCaller<SurfaceInspector, void(), &SurfaceInspector::Update> UpdateCaller;
221 typedef MemberCaller<SurfaceInspector, void(), &SurfaceInspector::ApplyShader> ApplyShaderCaller;
223 typedef MemberCaller<SurfaceInspector, void(), &SurfaceInspector::ApplyTexdef> ApplyTexdefCaller;
225 typedef MemberCaller<SurfaceInspector, void(), &SurfaceInspector::ApplyFlags> ApplyFlagsCaller;
230 SurfaceInspector* g_SurfaceInspector;
232 inline SurfaceInspector& getSurfaceInspector(){
233 ASSERT_NOTNULL( g_SurfaceInspector );
234 return *g_SurfaceInspector;
238 void SurfaceInspector_constructWindow( ui::Window main_window ){
239 getSurfaceInspector().constructWindow( main_window );
241 void SurfaceInspector_destroyWindow(){
242 getSurfaceInspector().destroyWindow();
245 void SurfaceInspector_queueDraw(){
246 getSurfaceInspector().queueDraw();
251 CopiedString g_selectedShader;
252 TextureProjection g_selectedTexdef;
253 ContentsFlagsValue g_selectedFlags;
254 size_t g_selectedShaderSize[2];
257 void SurfaceInspector_SetSelectedShader( const char* shader ){
258 g_selectedShader = shader;
259 SurfaceInspector_queueDraw();
262 void SurfaceInspector_SetSelectedTexdef( const TextureProjection& projection ){
263 g_selectedTexdef = projection;
264 SurfaceInspector_queueDraw();
267 void SurfaceInspector_SetSelectedFlags( const ContentsFlagsValue& flags ){
268 g_selectedFlags = flags;
269 SurfaceInspector_queueDraw();
272 static bool s_texture_selection_dirty = false;
274 void SurfaceInspector_updateSelection(){
275 s_texture_selection_dirty = true;
276 SurfaceInspector_queueDraw();
279 if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
280 TexTool::queueDraw();
281 //globalOutputStream() << "textool texture changed..\n";
286 void SurfaceInspector_SelectionChanged( const Selectable& selectable ){
287 SurfaceInspector_updateSelection();
290 void SurfaceInspector_SetCurrent_FromSelected(){
291 if ( s_texture_selection_dirty == true ) {
292 s_texture_selection_dirty = false;
293 if ( !g_SelectedFaceInstances.empty() ) {
294 TextureProjection projection;
295 //This *may* be the point before it fucks up... Let's see!
296 //Yep, there was a call to removeScale in there...
297 Scene_BrushGetTexdef_Component_Selected( GlobalSceneGraph(), projection );
299 SurfaceInspector_SetSelectedTexdef( projection );
301 Scene_BrushGetShaderSize_Component_Selected( GlobalSceneGraph(), g_selectedShaderSize[0], g_selectedShaderSize[1] );
302 g_selectedTexdef.m_brushprimit_texdef.coords[0][2] = float_mod( g_selectedTexdef.m_brushprimit_texdef.coords[0][2], (float)g_selectedShaderSize[0] );
303 g_selectedTexdef.m_brushprimit_texdef.coords[1][2] = float_mod( g_selectedTexdef.m_brushprimit_texdef.coords[1][2], (float)g_selectedShaderSize[1] );
306 Scene_BrushGetShader_Component_Selected( GlobalSceneGraph(), name );
307 if ( string_not_empty( name.c_str() ) ) {
308 SurfaceInspector_SetSelectedShader( name.c_str() );
311 ContentsFlagsValue flags;
312 Scene_BrushGetFlags_Component_Selected( GlobalSceneGraph(), flags );
313 SurfaceInspector_SetSelectedFlags( flags );
317 TextureProjection projection;
318 Scene_BrushGetTexdef_Selected( GlobalSceneGraph(), projection );
319 SurfaceInspector_SetSelectedTexdef( projection );
322 Scene_BrushGetShader_Selected( GlobalSceneGraph(), name );
323 if ( string_empty( name.c_str() ) ) {
324 Scene_PatchGetShader_Selected( GlobalSceneGraph(), name );
326 if ( string_not_empty( name.c_str() ) ) {
327 SurfaceInspector_SetSelectedShader( name.c_str() );
330 ContentsFlagsValue flags( 0, 0, 0, false );
331 Scene_BrushGetFlags_Selected( GlobalSceneGraph(), flags );
332 SurfaceInspector_SetSelectedFlags( flags );
337 const char* SurfaceInspector_GetSelectedShader(){
338 SurfaceInspector_SetCurrent_FromSelected();
339 return g_selectedShader.c_str();
342 const TextureProjection& SurfaceInspector_GetSelectedTexdef(){
343 SurfaceInspector_SetCurrent_FromSelected();
344 return g_selectedTexdef;
347 const ContentsFlagsValue& SurfaceInspector_GetSelectedFlags(){
348 SurfaceInspector_SetCurrent_FromSelected();
349 return g_selectedFlags;
355 ===================================================
359 ===================================================
362 si_globals_t g_si_globals;
365 // make the shift increments match the grid settings
366 // the objective being that the shift+arrows shortcuts move the texture by the corresponding grid size
367 // this depends on a scale value if you have selected a particular texture on which you want it to work:
368 // we move the textures in pixels, not world units. (i.e. increment values are in pixel)
369 // depending on the texture scale it doesn't take the same amount of pixels to move of GetGridSize()
370 // increment * scale = gridsize
371 // hscale and vscale are optional parameters, if they are zero they will be set to the default scale
372 // NOTE: the default scale depends if you are using BP mode or regular.
373 // For regular it's 0.5f (128 pixels cover 64 world units), for BP it's simply 1.0f
375 void DoSnapTToGrid( float hscale, float vscale ){
376 g_si_globals.shift[0] = static_cast<float>( float_to_integer( static_cast<float>( GetGridSize() ) / hscale ) );
377 g_si_globals.shift[1] = static_cast<float>( float_to_integer( static_cast<float>( GetGridSize() ) / vscale ) );
378 getSurfaceInspector().queueDraw();
381 void SurfaceInspector_GridChange(){
382 if ( g_si_globals.m_bSnapTToGrid ) {
383 DoSnapTToGrid( Texdef_getDefaultTextureScale(), Texdef_getDefaultTextureScale() );
387 // make the shift increments match the grid settings
388 // the objective being that the shift+arrows shortcuts move the texture by the corresponding grid size
389 // this depends on the current texture scale used?
390 // we move the textures in pixels, not world units. (i.e. increment values are in pixel)
391 // depending on the texture scale it doesn't take the same amount of pixels to move of GetGridSize()
392 // increment * scale = gridsize
393 static void OnBtnMatchGrid( ui::Widget widget, gpointer data ){
394 float hscale, vscale;
395 hscale = static_cast<float>( gtk_spin_button_get_value( getSurfaceInspector().m_hscaleIncrement.m_spin ) );
396 vscale = static_cast<float>( gtk_spin_button_get_value( getSurfaceInspector().m_vscaleIncrement.m_spin ) );
398 if ( hscale == 0.0f || vscale == 0.0f ) {
399 globalOutputStream() << "ERROR: unexpected scale == 0.0f\n";
403 DoSnapTToGrid( hscale, vscale );
406 // DoSurface will always try to show the surface inspector
407 // or update it because something new has been selected
408 // Shamus: It does get called when the SI is hidden, but not when you select something new. ;-)
409 void DoSurface( void ){
410 if ( !getSurfaceInspector().GetWidget() ) {
411 getSurfaceInspector().Create();
414 getSurfaceInspector().Update();
415 getSurfaceInspector().importData();
416 getSurfaceInspector().ShowDlg();
419 void SurfaceInspector_toggleShown(){
420 if ( getSurfaceInspector().visible() ) {
421 getSurfaceInspector().HideDlg();
429 void SurfaceInspector_FitTexture(){
430 UndoableCommand undo( "textureAutoFit" );
431 Select_FitTexture( getSurfaceInspector().m_fitHorizontal, getSurfaceInspector().m_fitVertical );
434 static void OnBtnPatchdetails( ui::Widget widget, gpointer data ){
438 static void OnBtnPatchnatural( ui::Widget widget, gpointer data ){
439 Patch_NaturalTexture();
442 static void OnBtnPatchreset( ui::Widget widget, gpointer data ){
443 Patch_ResetTexture();
446 static void OnBtnPatchFit( ui::Widget widget, gpointer data ){
450 static void OnBtnAxial( ui::Widget widget, gpointer data ){
451 //globalOutputStream() << "--> [OnBtnAxial]...\n";
452 UndoableCommand undo( "textureDefault" );
453 TextureProjection projection;
454 //globalOutputStream() << " TexDef_Construct_Default()...\n";
455 TexDef_Construct_Default( projection );
456 //globalOutputStream() << " Select_SetTexdef()...\n";
461 if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
462 // Scale up texture width/height if in BP mode...
463 //NOTE: This may not be correct any more! :-P
464 if ( !g_SelectedFaceInstances.empty() ) {
465 Face & face = g_SelectedFaceInstances.last().getFace();
466 float x = face.getShader().m_state->getTexture().width;
467 float y = face.getShader().m_state->getTexture().height;
468 projection.m_brushprimit_texdef.coords[0][0] /= x;
469 projection.m_brushprimit_texdef.coords[0][1] /= y;
470 projection.m_brushprimit_texdef.coords[1][0] /= x;
471 projection.m_brushprimit_texdef.coords[1][1] /= y;
476 Select_SetTexdef( projection );
479 static void OnBtnFaceFit( ui::Widget widget, gpointer data ){
480 getSurfaceInspector().exportData();
481 SurfaceInspector_FitTexture();
484 typedef const char* FlagName;
486 const FlagName surfaceflagNamesDefault[32] = {
521 const FlagName contentflagNamesDefault[32] = {
556 const char* getSurfaceFlagName( std::size_t bit ){
557 const char* value = g_pGameDescription->getKeyValue( surfaceflagNamesDefault[bit] );
558 if ( string_empty( value ) ) {
559 return surfaceflagNamesDefault[bit];
564 const char* getContentFlagName( std::size_t bit ){
565 const char* value = g_pGameDescription->getKeyValue( contentflagNamesDefault[bit] );
566 if ( string_empty( value ) ) {
567 return contentflagNamesDefault[bit];
573 // =============================================================================
574 // SurfaceInspector class
576 guint togglebutton_connect_toggled( ui::ToggleButton button, const Callback<void()>& callback ){
577 return g_signal_connect_swapped( G_OBJECT( button ), "toggled", G_CALLBACK( callback.getThunk() ), callback.getEnvironment() );
580 ui::Window SurfaceInspector::BuildDialog(){
581 ui::Window window = ui::Window(create_floating_window( "Surface Inspector", m_parent ));
583 m_positionTracker.connect( window );
585 global_accel_connect_window( window );
587 window_connect_focus_in_clear_focus_widget( window );
591 // replaced by only the vbox:
592 auto vbox = ui::VBox( FALSE, 5 );
595 gtk_container_set_border_width( GTK_CONTAINER( vbox ), 5 );
598 auto hbox2 = ui::HBox( FALSE, 5 );
600 vbox.pack_start( hbox2, FALSE, FALSE, 0 );
603 ui::Widget label = ui::Label( "Texture" );
605 hbox2.pack_start( label, FALSE, TRUE, 0 );
608 auto entry = ui::Entry(ui::New);
610 hbox2.pack_start( entry, TRUE, TRUE, 0 );
612 m_textureEntry.connect( entry );
613 GlobalTextureEntryCompletion::instance().connect( entry );
619 auto table = ui::Table(6, 4, FALSE);
621 vbox.pack_start( table, FALSE, FALSE, 0 );
622 gtk_table_set_row_spacings(table, 5);
623 gtk_table_set_col_spacings(table, 5);
625 ui::Widget label = ui::Label( "Horizontal shift" );
627 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
628 table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
631 auto spin = ui::SpinButton( ui::Adjustment( 0, -8192, 8192, 2, 8, 0 ), 0, 2 );
632 m_hshiftIncrement.m_spin = spin;
633 m_hshiftSpinner.connect( spin );
635 table.attach(spin, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
636 spin.dimensions(60, -1);
639 ui::Widget label = ui::Label( "Step" );
641 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
642 table.attach(label, {2, 3, 0, 1}, {GTK_FILL, 0});
645 auto entry = ui::Entry(ui::New);
647 table.attach(entry, {3, 4, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
648 entry.dimensions(50, -1);
649 m_hshiftIncrement.m_entry = entry;
650 m_hshiftEntry.connect( entry );
653 ui::Widget label = ui::Label( "Vertical shift" );
655 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
656 table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
659 auto spin = ui::SpinButton( ui::Adjustment( 0, -8192, 8192, 2, 8, 0 ), 0, 2 );
660 m_vshiftIncrement.m_spin = spin;
661 m_vshiftSpinner.connect( spin );
663 table.attach(spin, {1, 2, 1, 2}, {GTK_FILL, 0});
664 spin.dimensions(60, -1);
667 ui::Widget label = ui::Label( "Step" );
669 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
670 table.attach(label, {2, 3, 1, 2}, {GTK_FILL, 0});
673 auto entry = ui::Entry(ui::New);
675 table.attach(entry, {3, 4, 1, 2}, {GTK_FILL, 0});
676 entry.dimensions(50, -1);
677 m_vshiftIncrement.m_entry = entry;
678 m_vshiftEntry.connect( entry );
681 ui::Widget label = ui::Label( "Horizontal stretch" );
683 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
684 table.attach(label, {0, 1, 2, 3}, {GTK_FILL, 0});
687 auto spin = ui::SpinButton( ui::Adjustment( 0, -8192, 8192, 2, 8, 0 ), 0, 5 );
688 m_hscaleIncrement.m_spin = spin;
689 m_hscaleSpinner.connect( spin );
691 table.attach(spin, {1, 2, 2, 3}, {GTK_FILL, 0});
692 spin.dimensions(60, -1);
695 ui::Widget label = ui::Label( "Step" );
697 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
698 table.attach(label, {2, 3, 2, 3}, {GTK_FILL, 0});
701 auto entry = ui::Entry(ui::New);
703 table.attach(entry, {3, 4, 2, 3}, {GTK_FILL, 0});
704 entry.dimensions(50, -1);
705 m_hscaleIncrement.m_entry = entry;
706 m_hscaleEntry.connect( entry );
709 ui::Widget label = ui::Label( "Vertical stretch" );
711 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
712 table.attach(label, {0, 1, 3, 4}, {GTK_FILL, 0});
715 auto spin = ui::SpinButton( ui::Adjustment( 0, -8192, 8192, 2, 8, 0 ), 0, 5 );
716 m_vscaleIncrement.m_spin = spin;
717 m_vscaleSpinner.connect( spin );
719 table.attach(spin, {1, 2, 3, 4}, {GTK_FILL, 0});
720 spin.dimensions(60, -1);
723 ui::Widget label = ui::Label( "Step" );
725 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
726 table.attach(label, {2, 3, 3, 4}, {GTK_FILL, 0});
729 auto entry = ui::Entry(ui::New);
731 table.attach(entry, {3, 4, 3, 4}, {GTK_FILL, 0});
732 entry.dimensions(50, -1);
733 m_vscaleIncrement.m_entry = entry;
734 m_vscaleEntry.connect( entry );
737 ui::Widget label = ui::Label( "Rotate" );
739 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
740 table.attach(label, {0, 1, 4, 5}, {GTK_FILL, 0});
743 auto spin = ui::SpinButton( ui::Adjustment( 0, -8192, 8192, 2, 8, 0 ), 0, 2 );
744 m_rotateIncrement.m_spin = spin;
745 m_rotateSpinner.connect( spin );
747 table.attach(spin, {1, 2, 4, 5}, {GTK_FILL, 0});
748 spin.dimensions(60, -1);
749 gtk_spin_button_set_wrap( spin, TRUE );
752 ui::Widget label = ui::Label( "Step" );
754 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
755 table.attach(label, {2, 3, 4, 5}, {GTK_FILL, 0});
758 auto entry = ui::Entry(ui::New);
760 table.attach(entry, {3, 4, 4, 5}, {GTK_FILL, 0});
761 entry.dimensions(50, -1);
762 m_rotateIncrement.m_entry = entry;
763 m_rotateEntry.connect( entry );
767 ui::Widget button = ui::Button( "Match Grid" );
769 table.attach(button, {2, 4, 5, 6}, {GTK_EXPAND | GTK_FILL, 0});
770 button.connect( "clicked", G_CALLBACK( OnBtnMatchGrid ), 0 );
775 auto frame = ui::Frame( "Texturing" );
777 vbox.pack_start( frame, FALSE, FALSE, 0 );
779 auto table = ui::Table(4, 4, FALSE);
782 gtk_table_set_row_spacings(table, 5);
783 gtk_table_set_col_spacings(table, 5);
784 gtk_container_set_border_width( GTK_CONTAINER( table ), 5 );
786 ui::Widget label = ui::Label( "Brush" );
788 table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
791 ui::Widget label = ui::Label( "Patch" );
793 table.attach(label, {0, 1, 2, 3}, {GTK_FILL, 0});
796 ui::Widget label = ui::Label( "Width" );
798 table.attach(label, {2, 3, 0, 1}, {GTK_FILL, 0});
801 ui::Widget label = ui::Label( "Height" );
803 table.attach(label, {3, 4, 0, 1}, {GTK_FILL, 0});
806 ui::Widget button = ui::Button( "Axial" );
808 table.attach(button, {0, 1, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
809 button.connect( "clicked",
810 G_CALLBACK( OnBtnAxial ), 0 );
811 button.dimensions(60, -1);
814 ui::Widget button = ui::Button( "Fit" );
816 table.attach(button, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
817 button.connect( "clicked",
818 G_CALLBACK( OnBtnFaceFit ), 0 );
819 button.dimensions(60, -1);
822 ui::Widget button = ui::Button( "CAP" );
824 table.attach(button, {0, 1, 3, 4}, {GTK_EXPAND | GTK_FILL, 0});
825 button.connect( "clicked",
826 G_CALLBACK( OnBtnPatchdetails ), 0 );
827 button.dimensions(60, -1);
830 ui::Widget button = ui::Button( "Set..." );
832 table.attach(button, {1, 2, 3, 4}, {GTK_EXPAND | GTK_FILL, 0});
833 button.connect( "clicked",
834 G_CALLBACK( OnBtnPatchreset ), 0 );
835 button.dimensions(60, -1);
838 ui::Widget button = ui::Button( "Natural" );
840 table.attach(button, {2, 3, 3, 4}, {GTK_EXPAND | GTK_FILL, 0});
841 button.connect( "clicked",
842 G_CALLBACK( OnBtnPatchnatural ), 0 );
843 button.dimensions(60, -1);
846 ui::Widget button = ui::Button( "Fit" );
848 table.attach(button, {3, 4, 3, 4}, {GTK_EXPAND | GTK_FILL, 0});
849 button.connect( "clicked",
850 G_CALLBACK( OnBtnPatchFit ), 0 );
851 button.dimensions(60, -1);
854 auto spin = ui::SpinButton( ui::Adjustment( 1, 0, 1 << 16, 1, 10, 0 ), 0, 6 );
856 table.attach(spin, {2, 3, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
857 spin.dimensions(60, -1);
858 AddDialogData( spin, m_fitHorizontal );
861 auto spin = ui::SpinButton( ui::Adjustment( 1, 0, 1 << 16, 1, 10, 0 ), 0, 6 );
863 table.attach(spin, {3, 4, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
864 spin.dimensions(60, -1);
865 AddDialogData( spin, m_fitVertical );
869 if ( !string_empty( g_pGameDescription->getKeyValue( "si_flags" ) ) ) {
871 auto frame = ui::Frame( "Surface Flags" );
873 vbox.pack_start( frame, TRUE, TRUE, 0 );
875 auto vbox3 = ui::VBox( FALSE, 4 );
876 //gtk_container_set_border_width(GTK_CONTAINER(vbox3), 4);
880 auto table = ui::Table( 8, 4, FALSE );
882 vbox3.pack_start( table, TRUE, TRUE, 0 );
883 gtk_table_set_row_spacings( table, 0 );
884 gtk_table_set_col_spacings( table, 0 );
886 GtkCheckButton** p = m_surfaceFlags;
888 for (unsigned int c = 0; c != 4; ++c)
890 for (unsigned int r = 0; r != 8; ++r)
892 auto check = ui::CheckButton( getSurfaceFlagName( c * 8 + r ) );
894 table.attach(check, {c, c + 1, r, r + 1}, {GTK_EXPAND | GTK_FILL, 0});
896 guint handler_id = togglebutton_connect_toggled( check, ApplyFlagsCaller( *this ) );
897 g_object_set_data( G_OBJECT( check ), "handler", gint_to_pointer( handler_id ) );
904 auto frame = ui::Frame( "Content Flags" );
906 vbox.pack_start( frame, TRUE, TRUE, 0 );
908 auto vbox3 = ui::VBox( FALSE, 4 );
909 //gtk_container_set_border_width(GTK_CONTAINER(vbox3), 4);
914 auto table = ui::Table( 8, 4, FALSE );
916 vbox3.pack_start( table, TRUE, TRUE, 0 );
917 gtk_table_set_row_spacings( table, 0 );
918 gtk_table_set_col_spacings( table, 0 );
920 GtkCheckButton** p = m_contentFlags;
922 for (unsigned int c = 0; c != 4; ++c)
924 for (unsigned int r = 0; r != 8; ++r)
926 auto check = ui::CheckButton( getContentFlagName( c * 8 + r ) );
928 table.attach(check, {c, c + 1, r, r + 1}, {GTK_EXPAND | GTK_FILL, 0});
930 guint handler_id = togglebutton_connect_toggled( check, ApplyFlagsCaller( *this ) );
931 g_object_set_data( G_OBJECT( check ), "handler", gint_to_pointer( handler_id ) );
935 // not allowed to modify detail flag using Surface Inspector
936 gtk_widget_set_sensitive( ui::CheckButton::from(m_contentFlags[BRUSH_DETAIL_FLAG]), FALSE );
941 auto frame = ui::Frame( "Value" );
943 vbox.pack_start( frame, TRUE, TRUE, 0 );
945 auto vbox3 = ui::VBox( FALSE, 4 );
946 gtk_container_set_border_width( GTK_CONTAINER( vbox3 ), 4 );
951 auto entry = ui::Entry(ui::New);
953 vbox3.pack_start( entry, TRUE, TRUE, 0 );
954 m_valueEntryWidget = entry;
955 m_valueEntry.connect( entry );
962 if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
963 // Shamus: Textool goodies...
964 ui::Widget frame = ui::Frame( "Textool" );
966 vbox.pack_start( frame , FALSE, FALSE, 0 );
968 //Prolly should make this a member or global var, so the SI can draw on it...
969 TexTool::g_textoolWin = glwidget_new( FALSE );
970 // --> Dunno, but this stuff may be necessary... (Looks like it!)
971 g_object_ref( TexTool::g_textoolWin );
972 gtk_widget_set_events( TexTool::g_textoolWin, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK );
973 gtk_widget_set_can_focus( TexTool::g_textoolWin, true );
975 TexTool::g_textoolWin.show();
976 TexTool::g_textoolWin.dimensions( -1, 240 ); //Yeah!
977 frame.add(TexTool::g_textoolWin);
979 TexTool::g_textoolWin.connect( "size_allocate", G_CALLBACK( TexTool::size_allocate ), NULL );
980 TexTool::g_textoolWin.connect( "expose_event", G_CALLBACK( TexTool::expose ), NULL );
981 TexTool::g_textoolWin.connect( "button_press_event", G_CALLBACK( TexTool::button_press ), NULL );
982 TexTool::g_textoolWin.connect( "button_release_event", G_CALLBACK( TexTool::button_release ), NULL );
983 TexTool::g_textoolWin.connect( "motion_notify_event", G_CALLBACK( TexTool::motion ), NULL );
986 ui::Widget hbox = ui::HBox( FALSE, 5 );
988 vbox.pack_start( hbox , FALSE, FALSE, 0 );
989 // Checkboxes go here... (Flip X/Y)
990 ui::Widget flipX = ui::CheckButton( "Flip X axis" );
991 ui::Widget flipY = ui::CheckButton( "Flip Y axis" );
994 hbox.pack_start( flipX, FALSE, FALSE, 0 );
995 hbox.pack_start( flipY, FALSE, FALSE, 0 );
997 //Instead of this, we probably need to create a vbox to put into the frame, then the
998 //window, then the hbox. !!! FIX !!!
1001 //Hmm. Do we really need g_object_set_data? Mebbe not... And we don't! :-)
1002 // g_object_set_data(G_OBJECT(flipX), "handler", gint_to_pointer(flipX.connect("toggled", G_CALLBACK(TexTool::flipX), 0)));
1003 // g_object_set_data(G_OBJECT(flipY), "handler", gint_to_pointer(flipY.connect("toggled", G_CALLBACK(TexTool::flipY), 0)));
1005 flipX.connect( "toggled", G_CALLBACK( TexTool::flipX ), NULL );
1006 flipY.connect( "toggled", G_CALLBACK( TexTool::flipY ), NULL );
1019 Set the fields to the current texdef (i.e. map/texdef -> dialog widgets)
1020 if faces selected (instead of brushes) -> will read this face texdef, else current texdef
1021 if only patches selected, will read the patch texdef
1025 void spin_button_set_value_no_signal( ui::SpinButton spin, gdouble value ){
1026 guint handler_id = gpointer_to_int( g_object_get_data( G_OBJECT( spin ), "handler" ) );
1027 g_signal_handler_block( G_OBJECT( gtk_spin_button_get_adjustment( spin ) ), handler_id );
1028 gtk_spin_button_set_value( spin, value );
1029 g_signal_handler_unblock( G_OBJECT( gtk_spin_button_get_adjustment( spin ) ), handler_id );
1032 void spin_button_set_step_increment( ui::SpinButton spin, gdouble value ){
1033 auto adjust = gtk_spin_button_get_adjustment( spin );
1034 gtk_adjustment_set_step_increment(adjust, value);
1037 void SurfaceInspector::Update(){
1038 const char * name = SurfaceInspector_GetSelectedShader();
1040 if ( shader_is_texture( name ) ) {
1041 m_texture.text(shader_get_textureName(name));
1048 texdef_t shiftScaleRotate;
1049 //Shamus: This is where we get into trouble--the BP code tries to convert to a "faked"
1050 //shift, rotate & scale values from the brush face, which seems to screw up for some reason.
1052 /*globalOutputStream() << "--> SI::Update. About to do ShiftScaleRotate_fromFace()...\n";
1053 SurfaceInspector_GetSelectedBPTexdef();
1054 globalOutputStream() << "BP: (" << g_selectedBrushPrimitTexdef.coords[0][0] << ", " << g_selectedBrushPrimitTexdef.coords[0][1] << ")("
1055 << g_selectedBrushPrimitTexdef.coords[1][0] << ", " << g_selectedBrushPrimitTexdef.coords[1][1] << ")("
1056 << g_selectedBrushPrimitTexdef.coords[0][2] << ", " << g_selectedBrushPrimitTexdef.coords[1][2] << ") SurfaceInspector::Update\n";//*/
1057 //Ok, it's screwed up *before* we get here...
1058 ShiftScaleRotate_fromFace( shiftScaleRotate, SurfaceInspector_GetSelectedTexdef() );
1060 // normalize again to hide the ridiculously high scale values that get created when using texlock
1061 shiftScaleRotate.shift[0] = float_mod( shiftScaleRotate.shift[0], (float)g_selectedShaderSize[0] );
1062 shiftScaleRotate.shift[1] = float_mod( shiftScaleRotate.shift[1], (float)g_selectedShaderSize[1] );
1065 spin_button_set_value_no_signal( m_hshiftIncrement.m_spin, shiftScaleRotate.shift[0] );
1066 spin_button_set_step_increment( m_hshiftIncrement.m_spin, g_si_globals.shift[0] );
1067 entry_set_float( m_hshiftIncrement.m_entry, g_si_globals.shift[0] );
1071 spin_button_set_value_no_signal( m_vshiftIncrement.m_spin, shiftScaleRotate.shift[1] );
1072 spin_button_set_step_increment( m_vshiftIncrement.m_spin, g_si_globals.shift[1] );
1073 entry_set_float( m_vshiftIncrement.m_entry, g_si_globals.shift[1] );
1077 spin_button_set_value_no_signal( m_hscaleIncrement.m_spin, shiftScaleRotate.scale[0] );
1078 spin_button_set_step_increment( m_hscaleIncrement.m_spin, g_si_globals.scale[0] );
1079 entry_set_float( m_hscaleIncrement.m_entry, g_si_globals.scale[0] );
1083 spin_button_set_value_no_signal( m_vscaleIncrement.m_spin, shiftScaleRotate.scale[1] );
1084 spin_button_set_step_increment( m_vscaleIncrement.m_spin, g_si_globals.scale[1] );
1085 entry_set_float( m_vscaleIncrement.m_entry, g_si_globals.scale[1] );
1089 spin_button_set_value_no_signal( m_rotateIncrement.m_spin, shiftScaleRotate.rotate );
1090 spin_button_set_step_increment( m_rotateIncrement.m_spin, g_si_globals.rotate );
1091 entry_set_float( m_rotateIncrement.m_entry, g_si_globals.rotate );
1094 if ( !string_empty( g_pGameDescription->getKeyValue( "si_flags" ) ) ) {
1095 ContentsFlagsValue flags( SurfaceInspector_GetSelectedFlags() );
1097 entry_set_int( m_valueEntryWidget, flags.m_value );
1099 for ( GtkCheckButton** p = m_surfaceFlags; p != m_surfaceFlags + 32; ++p )
1101 toggle_button_set_active_no_signal( ui::CheckButton::from( *p ), flags.m_surfaceFlags & ( 1 << ( p - m_surfaceFlags ) ) );
1104 for ( GtkCheckButton** p = m_contentFlags; p != m_contentFlags + 32; ++p )
1106 toggle_button_set_active_no_signal( ui::CheckButton::from( *p ), flags.m_contentFlags & ( 1 << ( p - m_contentFlags ) ) );
1115 Reads the fields to get the current texdef (i.e. widgets -> MAP)
1116 in brush primitive mode, grab the fake shift scale rot and compute a new texture matrix
1119 void SurfaceInspector::ApplyShader(){
1120 StringOutputStream name( 256 );
1121 name << GlobalTexturePrefix_get() << gtk_entry_get_text( m_texture );
1123 // TTimo: detect and refuse invalid texture names (at least the ones with spaces)
1124 if ( !texdef_name_valid( name.c_str() ) ) {
1125 globalErrorStream() << "invalid texture name '" << name.c_str() << "'\n";
1126 SurfaceInspector_queueDraw();
1130 UndoableCommand undo( "textureNameSetSelected" );
1131 Select_SetShader( name.c_str() );
1134 void SurfaceInspector::ApplyTexdef(){
1135 texdef_t shiftScaleRotate;
1137 shiftScaleRotate.shift[0] = static_cast<float>( gtk_spin_button_get_value( m_hshiftIncrement.m_spin ) );
1138 shiftScaleRotate.shift[1] = static_cast<float>( gtk_spin_button_get_value( m_vshiftIncrement.m_spin ) );
1139 shiftScaleRotate.scale[0] = static_cast<float>( gtk_spin_button_get_value( m_hscaleIncrement.m_spin ) );
1140 shiftScaleRotate.scale[1] = static_cast<float>( gtk_spin_button_get_value( m_vscaleIncrement.m_spin ) );
1141 shiftScaleRotate.rotate = static_cast<float>( gtk_spin_button_get_value( m_rotateIncrement.m_spin ) );
1143 TextureProjection projection;
1144 //Shamus: This is the other place that screws up, it seems, since it doesn't seem to do the
1145 //conversion from the face (I think) and so bogus values end up in the thing... !!! FIX !!!
1146 //This is actually OK. :-P
1147 ShiftScaleRotate_toFace( shiftScaleRotate, projection );
1149 UndoableCommand undo( "textureProjectionSetSelected" );
1150 Select_SetTexdef( projection );
1153 void SurfaceInspector::ApplyFlags(){
1154 unsigned int surfaceflags = 0;
1155 for ( GtkCheckButton** p = m_surfaceFlags; p != m_surfaceFlags + 32; ++p )
1157 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( *p ) ) ) {
1158 surfaceflags |= ( 1 << ( p - m_surfaceFlags ) );
1162 unsigned int contentflags = 0;
1163 for ( GtkCheckButton** p = m_contentFlags; p != m_contentFlags + 32; ++p )
1165 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( *p ) ) ) {
1166 contentflags |= ( 1 << ( p - m_contentFlags ) );
1170 int value = entry_get_int( m_valueEntryWidget );
1172 UndoableCommand undo( "flagsSetSelected" );
1173 Select_SetFlags( ContentsFlagsValue( surfaceflags, contentflags, value, true ) );
1177 void Face_getTexture( Face& face, CopiedString& shader, TextureProjection& projection, ContentsFlagsValue& flags ){
1178 shader = face.GetShader();
1179 face.GetTexdef( projection );
1180 flags = face.getShader().m_flags;
1182 typedef Function<void(Face&, CopiedString&, TextureProjection&, ContentsFlagsValue&), Face_getTexture> FaceGetTexture;
1184 void Face_setTexture( Face& face, const char* shader, const TextureProjection& projection, const ContentsFlagsValue& flags ){
1185 face.SetShader( shader );
1186 face.SetTexdef( projection );
1187 face.SetFlags( flags );
1189 typedef Function<void(Face&, const char*, const TextureProjection&, const ContentsFlagsValue&), Face_setTexture> FaceSetTexture;
1192 void Patch_getTexture( Patch& patch, CopiedString& shader, TextureProjection& projection, ContentsFlagsValue& flags ){
1193 shader = patch.GetShader();
1194 projection = TextureProjection( texdef_t(), brushprimit_texdef_t(), Vector3( 0, 0, 0 ), Vector3( 0, 0, 0 ) );
1195 flags = ContentsFlagsValue( 0, 0, 0, false );
1197 typedef Function<void(Patch&, CopiedString&, TextureProjection&, ContentsFlagsValue&), Patch_getTexture> PatchGetTexture;
1199 void Patch_setTexture( Patch& patch, const char* shader, const TextureProjection& projection, const ContentsFlagsValue& flags ){
1200 patch.SetShader( shader );
1202 typedef Function<void(Patch&, const char*, const TextureProjection&, const ContentsFlagsValue&), Patch_setTexture> PatchSetTexture;
1205 typedef Callback<void(CopiedString&, TextureProjection&, ContentsFlagsValue&)> GetTextureCallback;
1206 typedef Callback<void(const char*, const TextureProjection&, const ContentsFlagsValue&)> SetTextureCallback;
1210 GetTextureCallback getTexture;
1211 SetTextureCallback setTexture;
1215 void Face_getClosest( Face& face, SelectionTest& test, SelectionIntersection& bestIntersection, Texturable& texturable ){
1216 SelectionIntersection intersection;
1217 face.testSelect( test, intersection );
1218 if ( intersection.valid()
1219 && SelectionIntersection_closer( intersection, bestIntersection ) ) {
1220 bestIntersection = intersection;
1221 texturable.setTexture = makeCallback( FaceSetTexture(), face );
1222 texturable.getTexture = makeCallback( FaceGetTexture(), face );
1227 class OccludeSelector : public Selector
1229 SelectionIntersection& m_bestIntersection;
1232 OccludeSelector( SelectionIntersection& bestIntersection, bool& occluded ) : m_bestIntersection( bestIntersection ), m_occluded( occluded ){
1235 void pushSelectable( Selectable& selectable ){
1237 void popSelectable(){
1239 void addIntersection( const SelectionIntersection& intersection ){
1240 if ( SelectionIntersection_closer( intersection, m_bestIntersection ) ) {
1241 m_bestIntersection = intersection;
1247 class BrushGetClosestFaceVisibleWalker : public scene::Graph::Walker
1249 SelectionTest& m_test;
1250 Texturable& m_texturable;
1251 mutable SelectionIntersection m_bestIntersection;
1253 BrushGetClosestFaceVisibleWalker( SelectionTest& test, Texturable& texturable ) : m_test( test ), m_texturable( texturable ){
1255 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1256 if ( path.top().get().visible() ) {
1257 BrushInstance* brush = Instance_getBrush( instance );
1259 m_test.BeginMesh( brush->localToWorld() );
1261 for ( Brush::const_iterator i = brush->getBrush().begin(); i != brush->getBrush().end(); ++i )
1263 Face_getClosest( *( *i ), m_test, m_bestIntersection, m_texturable );
1268 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
1269 if ( selectionTestable ) {
1271 OccludeSelector selector( m_bestIntersection, occluded );
1272 selectionTestable->testSelect( selector, m_test );
1274 Patch* patch = Node_getPatch( path.top() );
1276 m_texturable.setTexture = makeCallback( PatchSetTexture(), *patch );
1277 m_texturable.getTexture = makeCallback( PatchGetTexture(), *patch );
1281 m_texturable = Texturable();
1291 Texturable Scene_getClosestTexturable( scene::Graph& graph, SelectionTest& test ){
1292 Texturable texturable;
1293 graph.traverse( BrushGetClosestFaceVisibleWalker( test, texturable ) );
1297 bool Scene_getClosestTexture( scene::Graph& graph, SelectionTest& test, CopiedString& shader, TextureProjection& projection, ContentsFlagsValue& flags ){
1298 Texturable texturable = Scene_getClosestTexturable( graph, test );
1299 if ( texturable.getTexture != GetTextureCallback() ) {
1300 texturable.getTexture( shader, projection, flags );
1306 void Scene_setClosestTexture( scene::Graph& graph, SelectionTest& test, const char* shader, const TextureProjection& projection, const ContentsFlagsValue& flags ){
1307 Texturable texturable = Scene_getClosestTexturable( graph, test );
1308 if ( texturable.setTexture != SetTextureCallback() ) {
1309 texturable.setTexture( shader, projection, flags );
1317 TextureProjection m_projection;
1318 ContentsFlagsValue m_flags;
1321 FaceTexture g_faceTextureClipboard;
1323 void FaceTextureClipboard_setDefault(){
1324 g_faceTextureClipboard.m_flags = ContentsFlagsValue( 0, 0, 0, false );
1325 TexDef_Construct_Default( g_faceTextureClipboard.m_projection );
1328 void TextureClipboard_textureSelected( const char* shader ){
1329 FaceTextureClipboard_setDefault();
1332 class TextureBrowser;
1333 extern TextureBrowser g_TextureBrowser;
1334 void TextureBrowser_SetSelectedShader( TextureBrowser& textureBrowser, const char* shader );
1335 const char* TextureBrowser_GetSelectedShader( TextureBrowser& textureBrowser );
1337 void Scene_copyClosestTexture( SelectionTest& test ){
1338 CopiedString shader;
1339 if ( Scene_getClosestTexture( GlobalSceneGraph(), test, shader, g_faceTextureClipboard.m_projection, g_faceTextureClipboard.m_flags ) ) {
1340 TextureBrowser_SetSelectedShader( g_TextureBrowser, shader.c_str() );
1344 void Scene_applyClosestTexture( SelectionTest& test ){
1345 UndoableCommand command( "facePaintTexture" );
1347 Scene_setClosestTexture( GlobalSceneGraph(), test, TextureBrowser_GetSelectedShader( g_TextureBrowser ), g_faceTextureClipboard.m_projection, g_faceTextureClipboard.m_flags );
1349 SceneChangeNotify();
1356 void SelectedFaces_copyTexture(){
1357 if ( !g_SelectedFaceInstances.empty() ) {
1358 Face& face = g_SelectedFaceInstances.last().getFace();
1359 face.GetTexdef( g_faceTextureClipboard.m_projection );
1360 g_faceTextureClipboard.m_flags = face.getShader().m_flags;
1362 TextureBrowser_SetSelectedShader( g_TextureBrowser, face.getShader().getShader() );
1366 void FaceInstance_pasteTexture( FaceInstance& faceInstance ){
1367 faceInstance.getFace().SetTexdef( g_faceTextureClipboard.m_projection );
1368 faceInstance.getFace().SetShader( TextureBrowser_GetSelectedShader( g_TextureBrowser ) );
1369 faceInstance.getFace().SetFlags( g_faceTextureClipboard.m_flags );
1370 SceneChangeNotify();
1373 bool SelectedFaces_empty(){
1374 return g_SelectedFaceInstances.empty();
1377 void SelectedFaces_pasteTexture(){
1378 UndoableCommand command( "facePasteTexture" );
1379 g_SelectedFaceInstances.foreach( FaceInstance_pasteTexture );
1384 void SurfaceInspector_constructPreferences( PreferencesPage& page ){
1385 page.appendCheckBox( "", "Surface Inspector Increments Match Grid", g_si_globals.m_bSnapTToGrid );
1387 void SurfaceInspector_constructPage( PreferenceGroup& group ){
1388 PreferencesPage page( group.createPage( "Surface Inspector", "Surface Inspector Preferences" ) );
1389 SurfaceInspector_constructPreferences( page );
1391 void SurfaceInspector_registerPreferencesPage(){
1392 PreferencesDialog_addSettingsPage( makeCallbackF(SurfaceInspector_constructPage) );
1395 void SurfaceInspector_registerCommands(){
1396 GlobalCommands_insert( "FitTexture", makeCallbackF(SurfaceInspector_FitTexture), Accelerator( 'B', (GdkModifierType)GDK_SHIFT_MASK ) );
1397 GlobalCommands_insert( "SurfaceInspector", makeCallbackF(SurfaceInspector_toggleShown), Accelerator( 'S' ) );
1399 GlobalCommands_insert( "FaceCopyTexture", makeCallbackF(SelectedFaces_copyTexture) );
1400 GlobalCommands_insert( "FacePasteTexture", makeCallbackF(SelectedFaces_pasteTexture) );
1404 #include "preferencesystem.h"
1407 void SurfaceInspector_Construct(){
1408 g_SurfaceInspector = new SurfaceInspector;
1410 SurfaceInspector_registerCommands();
1412 FaceTextureClipboard_setDefault();
1414 GlobalPreferenceSystem().registerPreference( "SurfaceWnd", make_property<WindowPositionTracker_String>( getSurfaceInspector().m_positionTracker) );
1415 GlobalPreferenceSystem().registerPreference( "SI_SurfaceTexdef_Scale1", make_property_string( g_si_globals.scale[0] ) );
1416 GlobalPreferenceSystem().registerPreference( "SI_SurfaceTexdef_Scale2", make_property_string( g_si_globals.scale[1] ) );
1417 GlobalPreferenceSystem().registerPreference( "SI_SurfaceTexdef_Shift1", make_property_string( g_si_globals.shift[0] ) );
1418 GlobalPreferenceSystem().registerPreference( "SI_SurfaceTexdef_Shift2", make_property_string( g_si_globals.shift[1] ) );
1419 GlobalPreferenceSystem().registerPreference( "SI_SurfaceTexdef_Rotate", make_property_string( g_si_globals.rotate ) );
1420 GlobalPreferenceSystem().registerPreference( "SnapTToGrid", make_property_string( g_si_globals.m_bSnapTToGrid ) );
1422 typedef FreeCaller<void(const Selectable&), SurfaceInspector_SelectionChanged> SurfaceInspectorSelectionChangedCaller;
1423 GlobalSelectionSystem().addSelectionChangeCallback( SurfaceInspectorSelectionChangedCaller() );
1424 typedef FreeCaller<void(), SurfaceInspector_updateSelection> SurfaceInspectorUpdateSelectionCaller;
1425 Brush_addTextureChangedCallback( SurfaceInspectorUpdateSelectionCaller() );
1426 Patch_addTextureChangedCallback( SurfaceInspectorUpdateSelectionCaller() );
1428 SurfaceInspector_registerPreferencesPage();
1430 void SurfaceInspector_Destroy(){
1431 delete g_SurfaceInspector;
1438 namespace TexTool { // namespace hides these symbols from other object-files
1440 //Shamus: Textool functions, including GTK+ callbacks
1443 //NOTE: Black screen when TT first comes up is caused by an uninitialized Extent... !!! FIX !!!
1444 // But... You can see down below that it *is* initialized! WTF?
1447 float minX, minY, maxX, maxY;
1448 float width( void ) { return fabs( maxX - minX ); }
1449 float height( void ) { return fabs( maxY - minY ); }
1452 //This seems to control the texture scale... (Yep! ;-)
1453 Extent extents = { -2.0f, -2.0f, +2.0f, +2.0f };
1454 brushprimit_texdef_t tm; // Texture transform matrix
1455 Vector2 pts[c_brush_maxFaces];
1459 Vector2 textureSize;
1461 #define VP_PADDING 1.2
1462 #define PI 3.14159265358979
1463 bool lButtonDown = false;
1464 bool rButtonDown = false;
1467 bool haveAnchor = false;
1468 brushprimit_texdef_t currentBP;
1469 brushprimit_texdef_t origBP; // Original brush primitive (before we muck it up)
1470 float controlRadius = 5.0f;
1471 float rotationAngle = 0.0f;
1472 float rotationAngle2 = 0.0f;
1473 float oldRotationAngle;
1474 Vector2 rotationPoint;
1475 bool translatingX = false; // Widget state variables
1476 bool translatingY = false;
1477 bool scalingX = false;
1478 bool scalingY = false;
1479 bool rotating = false;
1480 bool resizingX = false; // Not sure what this means... :-/
1481 bool resizingY = false;
1482 float origAngle, origScaleX, origScaleY;
1486 // Function prototypes (move up to top later...)
1488 void DrawCircularArc( Vector2 ctr, float startAngle, float endAngle, float radius );
1491 void CopyPointsFromSelectedFace( void ){
1492 // Make sure that there's a face and winding to get!
1494 if ( g_SelectedFaceInstances.empty() ) {
1499 Face & face = g_SelectedFaceInstances.last().getFace();
1500 textureNum = face.getShader().m_state->getTexture().texture_number;
1501 textureSize.x() = face.getShader().m_state->getTexture().width;
1502 textureSize.y() = face.getShader().m_state->getTexture().height;
1503 //globalOutputStream() << "--> Texture #" << textureNum << ": " << textureSize.x() << " x " << textureSize.y() << "...\n";
1505 currentBP = SurfaceInspector_GetSelectedTexdef().m_brushprimit_texdef;
1507 face.EmitTextureCoordinates();
1508 Winding & w = face.getWinding();
1511 for ( Winding::const_iterator i = w.begin(); i != w.end(); i++ )
1513 //globalOutputStream() << (*i).texcoord.x() << " " << (*i).texcoord.y() << ", ";
1514 pts[count].x() = ( *i ).texcoord.x();
1515 pts[count].y() = ( *i ).texcoord.y();
1521 //globalOutputStream() << " ..copied points\n";
1524 brushprimit_texdef_t bp;
1525 //This approach is probably wrongheaded and just not right anyway. So, !!! FIX !!! [DONE]
1526 void CommitChanges( void ){
1527 texdef_t t; // Throwaway, since this is BP only
1529 bp.coords[0][0] = tm.coords[0][0] * origBP.coords[0][0] + tm.coords[0][1] * origBP.coords[1][0];
1530 bp.coords[0][1] = tm.coords[0][0] * origBP.coords[0][1] + tm.coords[0][1] * origBP.coords[1][1];
1531 bp.coords[0][2] = tm.coords[0][0] * origBP.coords[0][2] + tm.coords[0][1] * origBP.coords[1][2] + tm.coords[0][2];
1532 //Ok, this works for translation...
1533 // bp.coords[0][2] = tm.coords[0][0] * origBP.coords[0][2] + tm.coords[0][1] * origBP.coords[1][2] + tm.coords[0][2] * textureSize.x();
1534 bp.coords[1][0] = tm.coords[1][0] * origBP.coords[0][0] + tm.coords[1][1] * origBP.coords[1][0];
1535 bp.coords[1][1] = tm.coords[1][0] * origBP.coords[0][1] + tm.coords[1][1] * origBP.coords[1][1];
1536 bp.coords[1][2] = tm.coords[1][0] * origBP.coords[0][2] + tm.coords[1][1] * origBP.coords[1][2] + tm.coords[1][2];
1537 // bp.coords[1][2] = tm.coords[1][0] * origBP.coords[0][2] + tm.coords[1][1] * origBP.coords[1][2] + tm.coords[1][2] * textureSize.y();
1539 //This doesn't work: g_brush_texture_changed();
1541 //Note: We should only set an undo *after* the button has been released... !!! FIX !!!
1542 //Definitely *should* have an undo, though!
1543 // UndoableCommand undo("textureProjectionSetSelected");
1544 Select_SetTexdef( TextureProjection( t, bp, Vector3( 0, 0, 0 ), Vector3( 0, 0, 0 ) ) );
1545 //This is working, but for some reason the translate is causing the rest of the SI
1546 //widgets to yield bad readings... !!! FIX !!!
1547 //I.e., click on textool window, translate face wireframe, then controls go crazy. Dunno why.
1548 //It's because there were some uncommented out add/removeScale functions in brush.h and a
1549 //removeScale in brushmanip.cpp... :-/
1550 //Translate isn't working at all now... :-(
1551 //It's because we need to multiply in some scaling factor (prolly the texture width/height)
1555 void UpdateControlPoints( void ){
1558 // Init texture transform matrix
1560 tm.coords[0][0] = 1.0f; tm.coords[0][1] = 0.0f; tm.coords[0][2] = 0.0f;
1561 tm.coords[1][0] = 0.0f; tm.coords[1][1] = 1.0f; tm.coords[1][2] = 0.0f;
1566 For shifting we have:
1569 The code that should provide reasonable defaults, but doesn't for some reason:
1570 It's scaling the BP by 128 for some reason, between the time it's created and the
1571 time we get back to the SI widgets:
1573 static void OnBtnAxial(GtkWidget *widget, gpointer data)
1575 UndoableCommand undo("textureDefault");
1576 TextureProjection projection;
1577 TexDef_Construct_Default(projection);
1578 Select_SetTexdef(projection);
1581 Select_SetTexdef() calls Scene_BrushSetTexdef_Component_Selected(GlobalSceneGraph(), projection)
1582 which is in brushmanip.h: This eventually calls
1583 Texdef_Assign(m_texdef, texdef, m_brushprimit_texdef, brushprimit_texdef) in class Face...
1584 which just copies from brushpr to m_brushpr...
1587 //Small problem with this thing: It's scaled to the texture which is all screwed up... !!! FIX !!! [DONE]
1588 //Prolly should separate out the grid drawing so that we can draw it behind the polygon.
1589 const float gridWidth = 1.3f; // Let's try an absolute height... WORKS!!!
1590 // NOTE that 2.0 is the height of the viewport. Dunno why... Should make collision
1591 // detection easier...
1592 const float gridRadius = gridWidth * 0.5f;
1594 typedef const float WidgetColor[3];
1595 const WidgetColor widgetColor[10] = {
1596 { 1.0000f, 0.2000f, 0.0000f }, // Red
1597 { 0.9137f, 0.9765f, 0.4980f }, // Yellow
1598 { 0.0000f, 0.6000f, 0.3216f }, // Green
1599 { 0.6157f, 0.7726f, 0.8196f }, // Cyan
1600 { 0.4980f, 0.5000f, 0.4716f }, // Grey
1603 { 1.0000f, 0.6000f, 0.4000f }, // Light Red
1604 { 1.0000f, 1.0000f, 0.8980f }, // Light Yellow
1605 { 0.4000f, 1.0000f, 0.7216f }, // Light Green
1606 { 1.0000f, 1.0000f, 1.0000f }, // Light Cyan
1607 { 0.8980f, 0.9000f, 0.8716f } // Light Grey
1611 #define COLOR_YELLOW 1
1612 #define COLOR_GREEN 2
1613 #define COLOR_CYAN 3
1614 #define COLOR_GREY 4
1615 #define COLOR_LT_RED 5
1616 #define COLOR_LT_YELLOW 6
1617 #define COLOR_LT_GREEN 7
1618 #define COLOR_LT_CYAN 8
1619 #define COLOR_LT_GREY 9
1621 void DrawControlWidgets( void ){
1622 //Note that the grid should go *behind* the face outline... !!! FIX !!!
1624 float xStart = center.x() - ( gridWidth / 2.0f );
1625 float yStart = center.y() - ( gridWidth / 2.0f );
1626 float xScale = ( extents.height() / extents.width() ) * ( textureSize.y() / textureSize.x() );
1629 //Small problem with this approach: Changing the center point in the TX code doesn't seem to
1630 //change anything here--prolly because we load a new identity matrix. A couple of ways to fix
1631 //this would be to get rid of that code, or change the center to a new point by taking into
1632 //account the transforms that we toss with the new identity matrix. Dunno which is better.
1634 glScalef( xScale, 1.0, 1.0 ); // Will that square it up? Yup.
1635 glRotatef( static_cast<float>( radians_to_degrees( atan2( -currentBP.coords[0][1], currentBP.coords[0][0] ) ) ), 0.0, 0.0, -1.0 );
1636 glTranslatef( -center.x(), -center.y(), 0.0 );
1639 glColor3fv( translatingX && translatingY ? widgetColor[COLOR_LT_YELLOW] : widgetColor[COLOR_YELLOW] );
1640 glBegin( GL_LINE_LOOP );
1641 DrawCircularArc( center, 0, 2.0f * PI, gridRadius * 0.16 );
1646 glBegin( GL_LINES );
1647 glColor3fv( translatingY && !translatingX ? widgetColor[COLOR_LT_GREEN] : widgetColor[COLOR_GREEN] );
1648 glVertex2f( center.x(), center.y() + ( gridRadius * 0.16 ) );
1649 glVertex2f( center.x(), center.y() + ( gridRadius * 1.00 ) );
1650 glColor3fv( translatingX && !translatingY ? widgetColor[COLOR_LT_RED] : widgetColor[COLOR_RED] );
1651 glVertex2f( center.x() + ( gridRadius * 0.16 ), center.y() );
1652 glVertex2f( center.x() + ( gridRadius * 1.00 ), center.y() );
1656 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1657 glBegin( GL_TRIANGLES );
1658 glColor3fv( translatingY && !translatingX ? widgetColor[COLOR_LT_GREEN] : widgetColor[COLOR_GREEN] );
1659 glVertex2f( center.x(), center.y() + ( gridRadius * 1.10 ) );
1660 glVertex2f( center.x() + ( gridRadius * 0.06 ), center.y() + ( gridRadius * 0.94 ) );
1661 glVertex2f( center.x() - ( gridRadius * 0.06 ), center.y() + ( gridRadius * 0.94 ) );
1662 glColor3fv( translatingX && !translatingY ? widgetColor[COLOR_LT_RED] : widgetColor[COLOR_RED] );
1663 glVertex2f( center.x() + ( gridRadius * 1.10 ), center.y() );
1664 glVertex2f( center.x() + ( gridRadius * 0.94 ), center.y() + ( gridRadius * 0.06 ) );
1665 glVertex2f( center.x() + ( gridRadius * 0.94 ), center.y() - ( gridRadius * 0.06 ) );
1669 glBegin( GL_LINE_STRIP );
1670 glColor3fv( rotating ? widgetColor[COLOR_LT_CYAN] : widgetColor[COLOR_CYAN] );
1671 DrawCircularArc( center, 0.03f * PI, 0.47f * PI, gridRadius * 0.90 );
1675 glColor3fv( scalingY && !scalingX ? widgetColor[COLOR_LT_GREEN] : widgetColor[COLOR_GREEN] );
1676 glBegin( GL_LINES );
1677 glVertex2f( center.x() + ( gridRadius * 0.20 ), center.y() + ( gridRadius * 1.50 ) );
1678 glVertex2f( center.x() - ( gridRadius * 0.20 ), center.y() + ( gridRadius * 1.50 ) );
1680 glBegin( GL_LINE_LOOP );
1681 glVertex2f( center.x() + ( gridRadius * 0.10 ), center.y() + ( gridRadius * 1.40 ) );
1682 glVertex2f( center.x() - ( gridRadius * 0.10 ), center.y() + ( gridRadius * 1.40 ) );
1683 glVertex2f( center.x() - ( gridRadius * 0.10 ), center.y() + ( gridRadius * 1.20 ) );
1684 glVertex2f( center.x() + ( gridRadius * 0.10 ), center.y() + ( gridRadius * 1.20 ) );
1687 glColor3fv( scalingX && !scalingY ? widgetColor[COLOR_LT_RED] : widgetColor[COLOR_RED] );
1688 glBegin( GL_LINES );
1689 glVertex2f( center.x() + ( gridRadius * 1.50 ), center.y() + ( gridRadius * 0.20 ) );
1690 glVertex2f( center.x() + ( gridRadius * 1.50 ), center.y() - ( gridRadius * 0.20 ) );
1692 glBegin( GL_LINE_LOOP );
1693 glVertex2f( center.x() + ( gridRadius * 1.40 ), center.y() + ( gridRadius * 0.10 ) );
1694 glVertex2f( center.x() + ( gridRadius * 1.40 ), center.y() - ( gridRadius * 0.10 ) );
1695 glVertex2f( center.x() + ( gridRadius * 1.20 ), center.y() - ( gridRadius * 0.10 ) );
1696 glVertex2f( center.x() + ( gridRadius * 1.20 ), center.y() + ( gridRadius * 0.10 ) );
1699 glColor3fv( scalingX && scalingY ? widgetColor[COLOR_LT_CYAN] : widgetColor[COLOR_CYAN] );
1700 glBegin( GL_LINE_STRIP );
1701 glVertex2f( center.x() + ( gridRadius * 1.50 ), center.y() + ( gridRadius * 1.10 ) );
1702 glVertex2f( center.x() + ( gridRadius * 1.50 ), center.y() + ( gridRadius * 1.50 ) );
1703 glVertex2f( center.x() + ( gridRadius * 1.10 ), center.y() + ( gridRadius * 1.50 ) );
1705 glBegin( GL_LINE_LOOP );
1706 glVertex2f( center.x() + ( gridRadius * 1.40 ), center.y() + ( gridRadius * 1.40 ) );
1707 glVertex2f( center.x() + ( gridRadius * 1.40 ), center.y() + ( gridRadius * 1.20 ) );
1708 glVertex2f( center.x() + ( gridRadius * 1.20 ), center.y() + ( gridRadius * 1.20 ) );
1709 glVertex2f( center.x() + ( gridRadius * 1.20 ), center.y() + ( gridRadius * 1.40 ) );
1715 void DrawControlPoints( void ){
1716 glColor3f( 1, 1, 1 );
1717 glBegin( GL_LINE_LOOP );
1719 for ( int i = 0; i < numPts; i++ )
1720 glVertex2f( pts[i].x(), pts[i].y() );
1725 // Note: Setup and all that jazz must be done by the caller!
1727 void DrawCircularArc( Vector2 ctr, float startAngle, float endAngle, float radius ){
1728 float stepSize = ( 2.0f * PI ) / 200.0f;
1730 for ( float angle = startAngle; angle <= endAngle; angle += stepSize )
1731 glVertex2f( ctr.x() + radius * cos( angle ), ctr.y() + radius * sin( angle ) );
1736 if ( numPts == 0 ) {
1740 // Find selected texture's extents...
1742 extents.minX = extents.maxX = pts[0].x(),
1743 extents.minY = extents.maxY = pts[0].y();
1745 for ( int i = 1; i < numPts; i++ )
1747 if ( pts[i].x() < extents.minX ) {
1748 extents.minX = pts[i].x();
1750 if ( pts[i].x() > extents.maxX ) {
1751 extents.maxX = pts[i].x();
1753 if ( pts[i].y() < extents.minY ) {
1754 extents.minY = pts[i].y();
1756 if ( pts[i].y() > extents.maxY ) {
1757 extents.maxY = pts[i].y();
1761 // Do some viewport fitting stuff...
1763 //globalOutputStream() << "--> Center: " << center.x() << ", " << center.y() << "\n";
1764 //globalOutputStream() << "--> Extents (stage 1): " << extents.minX << ", "
1765 // << extents.maxX << ", " << extents.minY << ", " << extents.maxY << "\n";
1766 // TTimo: Apply a ratio to get the area we'll draw.
1767 center.x() = 0.5f * ( extents.minX + extents.maxX ),
1768 center.y() = 0.5f * ( extents.minY + extents.maxY );
1769 extents.minX = center.x() + VP_PADDING * ( extents.minX - center.x() ),
1770 extents.minY = center.y() + VP_PADDING * ( extents.minY - center.y() ),
1771 extents.maxX = center.x() + VP_PADDING * ( extents.maxX - center.x() ),
1772 extents.maxY = center.y() + VP_PADDING * ( extents.maxY - center.y() );
1773 //globalOutputStream() << "--> Extents (stage 2): " << extents.minX << ", "
1774 // << extents.maxX << ", " << extents.minY << ", " << extents.maxY << "\n";
1776 // TTimo: We want a texture with the same X / Y ratio.
1777 // TTimo: Compute XY space / window size ratio.
1778 float SSize = extents.width(), TSize = extents.height();
1779 float ratioX = textureSize.x() * extents.width() / windowSize.x(),
1780 ratioY = textureSize.y() * extents.height() / windowSize.y();
1781 //globalOutputStream() << "--> Texture size: " << textureSize.x() << ", " << textureSize.y() << "\n";
1782 //globalOutputStream() << "--> Window size: " << windowSize.x() << ", " << windowSize.y() << "\n";
1784 if ( ratioX > ratioY ) {
1785 TSize = ( windowSize.y() * ratioX ) / textureSize.y();
1786 // TSize = extents.width() * (windowSize.y() / windowSize.x()) * (textureSize.x() / textureSize.y());
1790 SSize = ( windowSize.x() * ratioY ) / textureSize.x();
1791 // SSize = extents.height() * (windowSize.x() / windowSize.y()) * (textureSize.y() / textureSize.x());
1794 extents.minX = center.x() - 0.5f * SSize, extents.maxX = center.x() + 0.5f * SSize,
1795 extents.minY = center.y() - 0.5f * TSize, extents.maxY = center.y() + 0.5f * TSize;
1796 //globalOutputStream() << "--> Extents (stage 3): " << extents.minX << ", "
1797 // << extents.maxX << ", " << extents.minY << ", " << extents.maxY << "\n";
1800 gboolean size_allocate( ui::Widget win, GtkAllocation * a, gpointer ){
1801 windowSize.x() = a->width;
1802 windowSize.y() = a->height;
1807 gboolean expose( ui::Widget win, GdkEventExpose * e, gpointer ){
1808 // globalOutputStream() << "--> Textool Window was exposed!\n";
1809 // globalOutputStream() << " (window width/height: " << cc << "/" << e->area.height << ")\n";
1811 // windowSize.x() = e->area.width, windowSize.y() = e->area.height;
1812 //This needs to go elsewhere...
1815 if ( glwidget_make_current( win ) == FALSE ) {
1816 globalOutputStream() << " FAILED to make current! Oh, the agony! :-(\n";
1820 CopyPointsFromSelectedFace();
1822 if ( !lButtonDown ) {
1826 // Probably should init button/anchor states here as well...
1827 // rotationAngle = 0.0f;
1828 glClearColor( 0, 0, 0, 0 );
1829 glViewport( 0, 0, e->area.width, e->area.height );
1830 glMatrixMode( GL_PROJECTION );
1834 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1835 glDisable( GL_DEPTH_TEST );
1836 glDisable( GL_BLEND );
1838 glOrtho( extents.minX, extents.maxX, extents.maxY, extents.minY, -1, 1 );
1840 glColor3f( 1, 1, 1 );
1841 // draw the texture background
1842 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1843 glBindTexture( GL_TEXTURE_2D, textureNum );
1845 glEnable( GL_TEXTURE_2D );
1846 glBegin( GL_QUADS );
1847 glTexCoord2f( extents.minX, extents.minY );
1848 glVertex2f( extents.minX, extents.minY );
1849 glTexCoord2f( extents.maxX, extents.minY );
1850 glVertex2f( extents.maxX, extents.minY );
1851 glTexCoord2f( extents.maxX, extents.maxY );
1852 glVertex2f( extents.maxX, extents.maxY );
1853 glTexCoord2f( extents.minX, extents.maxY );
1854 glVertex2f( extents.minX, extents.maxY );
1856 glDisable( GL_TEXTURE_2D );
1858 // draw the texture-space grid
1859 glColor3fv( widgetColor[COLOR_GREY] );
1860 glBegin( GL_LINES );
1862 const int gridSubdivisions = 8;
1863 const float gridExtents = 4.0f;
1865 for ( int i = 0; i < gridSubdivisions + 1; ++i )
1867 float y = i * ( gridExtents / float(gridSubdivisions) );
1868 float x = i * ( gridExtents / float(gridSubdivisions) );
1870 glVertex2f( gridExtents, y );
1872 glVertex2f( x, gridExtents );
1877 DrawControlPoints();
1878 DrawControlWidgets();
1880 // reset the current texture
1881 // glBindTexture(GL_TEXTURE_2D, 0);
1883 glwidget_swap_buffers( win );
1888 /*int FindSelectedPoint(int x, int y)
1890 for(int i=0; i<numPts; i++)
1892 int nx = (int)(windowSize.x() * (pts[i].x() - extents.minX) / extents.width());
1893 int ny = (int)(windowSize.y() * (pts[i].y() - extents.minY) / extents.height());
1895 if (abs(nx - x) <= 3 && abs(ny - y) <= 3)
1904 Vector2 dragPoint; // Defined in terms of window space (+x/-y)
1906 gboolean button_press( ui::Widget win, GdkEventButton * e, gpointer ){
1907 // globalOutputStream() << "--> Textool button press...\n";
1909 if ( e->button == 1 ) {
1911 GlobalUndoSystem().start();
1915 //globalOutputStream() << "--> Original BP: [" << origBP.coords[0][0] << "][" << origBP.coords[0][1] << "][" << origBP.coords[0][2] << "]\n";
1916 //globalOutputStream() << " [" << origBP.coords[1][0] << "][" << origBP.coords[1][1] << "][" << origBP.coords[1][2] << "]\n";
1917 //float angle = atan2(origBP.coords[0][1], origBP.coords[0][0]) * 180.0f / 3.141592653589f;
1918 origAngle = ( origBP.coords[0][1] > 0 ? PI : -PI ); // Could also be -PI... !!! FIX !!! [DONE]
1920 if ( origBP.coords[0][0] != 0.0f ) {
1921 origAngle = atan( origBP.coords[0][1] / origBP.coords[0][0] );
1924 origScaleX = origBP.coords[0][0] / cos( origAngle );
1925 origScaleY = origBP.coords[1][1] / cos( origAngle );
1926 rotationAngle = origAngle;
1927 oldCenter[0] = oldCenter[1] = 0;
1929 //globalOutputStream() << "--> BP stats: ang=" << origAngle * RAD_TO_DEG << ", scale=" << origScaleX << "/" << origScaleY << "\n";
1930 //Should also set the Flip X/Y checkboxes here as well... !!! FIX !!!
1931 //Also: should reverse texture left/right up/down instead of flipping the points...
1934 //float nx = windowSize.x() * (e->x - extents.minX) / (extents.maxX - extents.minX);
1935 //float ny = windowSize.y() * (e->y - extents.minY) / (extents.maxY - extents.minY);
1937 //But I want it to scroll the texture window, not the points... !!! FIX !!!
1938 //Actually, should scroll the texture window only when mouse is down on no widgets...
1939 float nx = e->x / windowSize.x() * extents.width() + extents.minX;
1940 float ny = e->y / windowSize.y() * extents.height() + extents.minY;
1941 trans.x() = -tm.coords[0][0] * nx - tm.coords[0][1] * ny;
1942 trans.y() = -tm.coords[1][0] * nx - tm.coords[1][1] * ny;
1944 dragPoint.x() = e->x, dragPoint.y() = e->y;
1945 trans2.x() = nx, trans2.y() = ny;
1946 oldRotationAngle = rotationAngle;
1947 // oldTrans.x() = tm.coords[0][2] - nx * textureSize.x();
1948 // oldTrans.y() = tm.coords[1][2] - ny * textureSize.y();
1949 oldTrans.x() = tm.coords[0][2];
1950 oldTrans.y() = tm.coords[1][2];
1951 oldCenter.x() = center.x();
1952 oldCenter.y() = center.y();
1958 /* else if (e->button == 3)
1963 //globalOutputStream() << "(" << (haveAnchor ? "anchor" : "released") << ")\n";
1968 gboolean button_release( ui::Widget win, GdkEventButton * e, gpointer ){
1969 // globalOutputStream() << "--> Textool button release...\n";
1971 if ( e->button == 1 ) {
1972 /* float ptx = e->x / windowSize.x() * extents.width() + extents.minX;
1973 float pty = e->y / windowSize.y() * extents.height() + extents.minY;
1975 //This prolly should go into the mouse move code...
1976 //Doesn't work correctly anyway...
1977 if (translatingX || translatingY)
1978 center.x() = ptx, center.y() = pty;//*/
1980 lButtonDown = false;
1982 if ( translatingX || translatingY ) {
1983 GlobalUndoSystem().finish( "translateTexture" );
1985 else if ( rotating ) {
1986 GlobalUndoSystem().finish( "rotateTexture" );
1988 else if ( scalingX || scalingY ) {
1989 GlobalUndoSystem().finish( "scaleTexture" );
1991 else if ( resizingX || resizingY ) {
1992 GlobalUndoSystem().finish( "resizeTexture" );
1996 GlobalUndoSystem().finish( "textoolUnknown" );
1999 rotating = translatingX = translatingY = scalingX = scalingY
2000 = resizingX = resizingY = false;
2004 else if ( e->button == 3 ) {
2005 rButtonDown = false;
2012 void C2DView::GridForWindow( float c[2], int x, int y)
2014 SpaceForWindow( c, x, y );
2017 c[0] /= m_GridStep[0];
2018 c[1] /= m_GridStep[1];
2019 c[0] = (float)floor( c[0] + 0.5f );
2020 c[1] = (float)floor( c[1] + 0.5f );
2021 c[0] *= m_GridStep[0];
2022 c[1] *= m_GridStep[1];
2024 void C2DView::SpaceForWindow( float c[2], int x, int y)
2026 c[0] = ((float)(x))/((float)(m_rect.right-m_rect.left))*(m_Maxs[0]-m_Mins[0])+m_Mins[0];
2027 c[1] = ((float)(y))/((float)(m_rect.bottom-m_rect.top))*(m_Maxs[1]-m_Mins[1])+m_Mins[1];
2030 gboolean motion( ui::Widget win, GdkEventMotion * e, gpointer ){
2031 // globalOutputStream() << "--> Textool motion...\n";
2033 if ( lButtonDown ) {
2034 if ( translatingX || translatingY ) {
2035 float ptx = e->x / windowSize.x() * extents.width() + extents.minX;
2036 float pty = e->y / windowSize.y() * extents.height() + extents.minY;
2038 //Need to fix this to take the rotation angle into account, so that it moves along
2039 //the rotated X/Y axis...
2040 if ( translatingX ) {
2041 // tm.coords[0][2] = (trans.x() + ptx) * textureSize.x();
2042 //This works, but only when the angle is zero. !!! FIX !!! [DONE]
2043 // tm.coords[0][2] = oldCenter.x() + (ptx * textureSize.x());
2044 tm.coords[0][2] = oldTrans.x() + ( ptx - trans2.x() ) * textureSize.x();
2045 // center.x() = oldCenter.x() + (ptx - trans2.x());
2048 if ( translatingY ) {
2049 // tm.coords[1][2] = (trans.y() + pty) * textureSize.y();
2050 // tm.coords[1][2] = oldCenter.y() + (pty * textureSize.y());
2051 tm.coords[1][2] = oldTrans.y() + ( pty - trans2.y() ) * textureSize.y();
2052 // center.y() = oldCenter.y() + (pty - trans2.y());
2055 //Need to update center.x/y() so that the widget translates as well. Also, oldCenter
2056 //is badly named... Should be oldTrans or something like that... !!! FIX !!!
2057 //Changing center.x/y() here doesn't seem to change anything... :-/
2058 UpdateControlPoints();
2060 else if ( rotating ) {
2061 // Shamus: New rotate code
2062 int cx = (int)( windowSize.x() * ( center.x() - extents.minX ) / extents.width() );
2063 int cy = (int)( windowSize.y() * ( center.y() - extents.minY ) / extents.height() );
2064 Vector3 v1( dragPoint.x() - cx, dragPoint.y() - cy, 0 ), v2( e->x - cx, e->y - cy, 0 );
2066 vector3_normalise( v1 );
2067 vector3_normalise( v2 );
2068 float c = vector3_dot( v1, v2 );
2069 Vector3 cross = vector3_cross( v1, v2 );
2070 float s = vector3_length( cross );
2072 if ( cross[2] > 0 ) {
2076 // Problem with this: arcsin/cos seems to only return -90 to 90 and 0 to 180...
2077 // Can't derive angle from that!
2079 //rotationAngle = asin(s);// * 180.0f / 3.141592653589f;
2080 rotationAngle = acos( c );
2081 //rotationAngle2 = asin(s);
2082 if ( cross[2] < 0 ) {
2083 rotationAngle = -rotationAngle;
2086 //NO! DOESN'T WORK! rotationAngle -= 45.0f * DEG_TO_RAD;
2089 /*c = cos(rotationAngle - oldRotationAngle);
2090 s = sin(rotationAngle - oldRotationAngle);
2091 rotationAngle += oldRotationAngle;
2092 //c += cos(oldRotationAngle);
2093 //s += sin(oldRotationAngle);
2094 //rotationAngle += oldRotationAngle;
2097 //rotationAngle %= 2.0 * PI;//*/
2099 //This is wrong... Hmm...
2100 //It seems to shear the texture instead of rotating it... !!! FIX !!!
2101 // Now it rotates correctly. Seems TTimo was overcomplicating things here... ;-)
2103 // Seems like what needs to happen here is multiplying these rotations by tm... !!! FIX !!!
2105 // See brush_primit.cpp line 244 (Texdef_EmitTextureCoordinates()) for where texcoords come from...
2107 tm.coords[0][0] = c;
2108 tm.coords[0][1] = s;
2109 tm.coords[1][0] = -s;
2110 tm.coords[1][1] = c;
2111 //It doesn't work anymore... Dunno why...
2112 //tm.coords[0][2] = -trans.x(); // This works!!! Yeah!!!
2113 //tm.coords[1][2] = -trans.y();
2115 //tm.coords[0][2] = rotationPoint.x(); // This works, but strangely...
2116 //tm.coords[1][2] = rotationPoint.y();
2117 //tm.coords[0][2] = 0;// center.x() / 2.0f;
2118 //tm.coords[1][2] = 0;// center.y() / 2.0f;
2120 //tm.coords[0][2] = -(center.x() * textureSize.x());
2121 //tm.coords[1][2] = -(center.y() * textureSize.y());
2122 //Eh? No, but seems to be getting closer...
2123 /*float ptx = e->x / windowSize.x() * extents.width() + extents.minX;
2124 float pty = e->y / windowSize.y() * extents.height() + extents.minY;
2125 tm.coords[0][2] = -c * center.x() - s * center.y() + ptx;
2126 tm.coords[1][2] = s * center.x() - c * center.x() + pty;//*/
2127 //Kinda works, but center drifts around on non-square textures...
2128 /*tm.coords[0][2] = (-c * center.x() - s * center.y()) * textureSize.x();
2129 tm.coords[1][2] = ( s * center.x() - c * center.y()) * textureSize.y();//*/
2130 //Rotates correctly, but not around the actual center of the face's points...
2131 /*tm.coords[0][2] = -c * center.x() * textureSize.x() - s * center.y() * textureSize.y();
2132 tm.coords[1][2] = s * center.x() * textureSize.x() - c * center.y() * textureSize.y();//*/
2134 tm.coords[0][2] = ( -c * center.x() * textureSize.x() - s * center.y() * textureSize.y() ) + center.x() * textureSize.x();
2135 tm.coords[1][2] = ( s * center.x() * textureSize.x() - c * center.y() * textureSize.y() ) + center.y() * textureSize.y(); //*/
2136 //This doesn't work...
2137 //And this is the wrong place for this anyway (I'm pretty sure).
2138 /*tm.coords[0][2] += oldCenter.x();
2139 tm.coords[1][2] += oldCenter.y();//*/
2140 UpdateControlPoints(); // will cause a redraw
2145 else // Check for widget mouseovers
2148 float nx = e->x / windowSize.x() * extents.width() + extents.minX;
2149 float ny = e->y / windowSize.y() * extents.height() + extents.minY;
2150 // Translate nx/y to the "center" point...
2153 ny = -ny; // Flip Y-axis so that increasing numbers move up
2155 tran.x() = tm.coords[0][0] * nx + tm.coords[0][1] * ny;
2156 tran.y() = tm.coords[1][0] * nx + tm.coords[1][1] * ny;
2157 //This doesn't seem to generate a valid distance from the center--for some reason it
2158 //calculates a fixed number every time
2159 //Look at nx/y above: they're getting fixed there! !!! FIX !!! [DONE]
2160 float dist = sqrt( ( nx * nx ) + ( ny * ny ) );
2161 // Normalize to the 2.0 = height standard (for now)
2162 //globalOutputStream() << "--> Distance before: " << dist;
2163 dist = dist * 2.0f / extents.height();
2164 //globalOutputStream() << ". After: " << dist;
2165 tran.x() = tran.x() * 2.0f / extents.height();
2166 tran.y() = tran.y() * 2.0f / extents.height();
2167 //globalOutputStream() << ". Trans: " << tran.x() << ", " << tran.y() << "\n";
2169 //Let's try this instead...
2170 //Interesting! It seems that e->x/y are rotated
2171 //(no, they're not--the TM above is what's doing it...)
2172 nx = ( ( e->x / windowSize.y() ) * 2.0f ) - ( windowSize.x() / windowSize.y() );
2173 ny = ( ( e->y / windowSize.y() ) * 2.0f ) - ( windowSize.y() / windowSize.y() );
2175 //Cool! It works! Now just need to do rotation...
2177 rotating = translatingX = translatingY = scalingX = scalingY
2178 = resizingX = resizingY = false;
2180 if ( dist < ( gridRadius * 0.16f ) ) {
2181 translatingX = translatingY = true;
2183 else if ( dist > ( gridRadius * 0.16f ) && dist < ( gridRadius * 1.10f )
2184 && fabs( ny ) < ( gridRadius * 0.05f ) && nx > 0 ) {
2185 translatingX = true;
2187 else if ( dist > ( gridRadius * 0.16f ) && dist < ( gridRadius * 1.10f )
2188 && fabs( nx ) < ( gridRadius * 0.05f ) && ny > 0 ) {
2189 translatingY = true;
2191 // Should tighten up the angle on this, or put this test after the axis tests...
2192 else if ( tran.x() > 0 && tran.y() > 0
2193 && ( dist > ( gridRadius * 0.82f ) && dist < ( gridRadius * 0.98f ) ) ) {
2205 //It seems the fake tex coords conversion is screwing this stuff up... !!! FIX !!!
2206 //This is still wrong... Prolly need to do something with the oldScaleX/Y stuff...
2207 void flipX( ui::ToggleButton, gpointer ){
2208 // globalOutputStream() << "--> Flip X...\n";
2210 // SurfaceInspector_GetSelectedBPTexdef(); // Refresh g_selectedBrushPrimitTexdef...
2211 // tm.coords[0][0] = -tm.coords[0][0];
2212 // tm.coords[1][0] = -tm.coords[1][0];
2213 // tm.coords[0][0] = -tm.coords[0][0]; // This should be correct now...Nope.
2214 // tm.coords[1][1] = -tm.coords[1][1];
2215 tm.coords[0][0] = -tm.coords[0][0]; // This should be correct now...
2216 tm.coords[1][0] = -tm.coords[1][0];
2217 // tm.coords[2][0] = -tm.coords[2][0];//wil wok? no.
2218 UpdateControlPoints();
2221 void flipY( ui::ToggleButton, gpointer ){
2222 // globalOutputStream() << "--> Flip Y...\n";
2223 // tm.coords[0][1] = -tm.coords[0][1];
2224 // tm.coords[1][1] = -tm.coords[1][1];
2225 // tm.coords[0][1] = -tm.coords[0][1]; // This should be correct now...Nope.
2226 // tm.coords[1][0] = -tm.coords[1][0];
2227 tm.coords[0][1] = -tm.coords[0][1]; // This should be correct now...
2228 tm.coords[1][1] = -tm.coords[1][1];
2229 // tm.coords[2][1] = -tm.coords[2][1];//wil wok? no.
2230 UpdateControlPoints();
2233 } // end namespace TexTool