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( GtkToggleButton *, gpointer );
92 void flipY( GtkToggleButton *, 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( GtkSpinButton* 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 GtkSpinButton* m_spin;
125 Increment( float& f ) : m_f( f ), m_spin( 0 ), m_entry( nullptr ){
128 entry_set_float( m_entry, m_f );
130 typedef MemberCaller<Increment, &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, &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{nullptr};
164 WindowPositionTracker m_positionTracker;
165 WindowPositionTrackerImportStringCaller m_importPosition;
166 WindowPositionTrackerExportStringCaller m_exportPosition;
169 float m_fitHorizontal;
172 Increment m_hshiftIncrement;
173 Increment m_vshiftIncrement;
174 Increment m_hscaleIncrement;
175 Increment m_vscaleIncrement;
176 Increment m_rotateIncrement;
180 m_textureEntry( ApplyShaderCaller( *this ), UpdateCaller( *this ) ),
181 m_hshiftSpinner( ApplyTexdefCaller( *this ), UpdateCaller( *this ) ),
182 m_hshiftEntry( Increment::ApplyCaller( m_hshiftIncrement ), Increment::CancelCaller( m_hshiftIncrement ) ),
183 m_vshiftSpinner( ApplyTexdefCaller( *this ), UpdateCaller( *this ) ),
184 m_vshiftEntry( Increment::ApplyCaller( m_vshiftIncrement ), Increment::CancelCaller( m_vshiftIncrement ) ),
185 m_hscaleSpinner( ApplyTexdefCaller( *this ), UpdateCaller( *this ) ),
186 m_hscaleEntry( Increment::ApplyCaller( m_hscaleIncrement ), Increment::CancelCaller( m_hscaleIncrement ) ),
187 m_vscaleSpinner( ApplyTexdefCaller( *this ), UpdateCaller( *this ) ),
188 m_vscaleEntry( Increment::ApplyCaller( m_vscaleIncrement ), Increment::CancelCaller( m_vscaleIncrement ) ),
189 m_rotateSpinner( ApplyTexdefCaller( *this ), UpdateCaller( *this ) ),
190 m_rotateEntry( Increment::ApplyCaller( m_rotateIncrement ), Increment::CancelCaller( m_rotateIncrement ) ),
191 m_idleDraw( UpdateCaller( *this ) ),
192 m_valueEntry( ApplyFlagsCaller( *this ), UpdateCaller( *this ) ),
193 m_importPosition( m_positionTracker ),
194 m_exportPosition( m_positionTracker ),
195 m_hshiftIncrement( g_si_globals.shift[0] ),
196 m_vshiftIncrement( g_si_globals.shift[1] ),
197 m_hscaleIncrement( g_si_globals.scale[0] ),
198 m_vscaleIncrement( g_si_globals.scale[1] ),
199 m_rotateIncrement( g_si_globals.rotate ){
202 m_positionTracker.setPosition( c_default_window_pos );
205 void constructWindow( ui::Window main_window ){
206 m_parent = main_window;
208 AddGridChangeCallback( FreeCaller<SurfaceInspector_GridChange>() );
210 void destroyWindow(){
213 bool visible() const {
214 return gtk_widget_get_visible( GetWidget() );
218 m_idleDraw.queueDraw();
223 typedef MemberCaller<SurfaceInspector, &SurfaceInspector::Update> UpdateCaller;
225 typedef MemberCaller<SurfaceInspector, &SurfaceInspector::ApplyShader> ApplyShaderCaller;
227 typedef MemberCaller<SurfaceInspector, &SurfaceInspector::ApplyTexdef> ApplyTexdefCaller;
229 typedef MemberCaller<SurfaceInspector, &SurfaceInspector::ApplyFlags> ApplyFlagsCaller;
234 SurfaceInspector* g_SurfaceInspector;
236 inline SurfaceInspector& getSurfaceInspector(){
237 ASSERT_NOTNULL( g_SurfaceInspector );
238 return *g_SurfaceInspector;
242 void SurfaceInspector_constructWindow( ui::Window main_window ){
243 getSurfaceInspector().constructWindow( main_window );
245 void SurfaceInspector_destroyWindow(){
246 getSurfaceInspector().destroyWindow();
249 void SurfaceInspector_queueDraw(){
250 getSurfaceInspector().queueDraw();
255 CopiedString g_selectedShader;
256 TextureProjection g_selectedTexdef;
257 ContentsFlagsValue g_selectedFlags;
258 size_t g_selectedShaderSize[2];
261 void SurfaceInspector_SetSelectedShader( const char* shader ){
262 g_selectedShader = shader;
263 SurfaceInspector_queueDraw();
266 void SurfaceInspector_SetSelectedTexdef( const TextureProjection& projection ){
267 g_selectedTexdef = projection;
268 SurfaceInspector_queueDraw();
271 void SurfaceInspector_SetSelectedFlags( const ContentsFlagsValue& flags ){
272 g_selectedFlags = flags;
273 SurfaceInspector_queueDraw();
276 static bool s_texture_selection_dirty = false;
278 void SurfaceInspector_updateSelection(){
279 s_texture_selection_dirty = true;
280 SurfaceInspector_queueDraw();
283 if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
284 TexTool::queueDraw();
285 //globalOutputStream() << "textool texture changed..\n";
290 void SurfaceInspector_SelectionChanged( const Selectable& selectable ){
291 SurfaceInspector_updateSelection();
294 void SurfaceInspector_SetCurrent_FromSelected(){
295 if ( s_texture_selection_dirty == true ) {
296 s_texture_selection_dirty = false;
297 if ( !g_SelectedFaceInstances.empty() ) {
298 TextureProjection projection;
299 //This *may* be the point before it fucks up... Let's see!
300 //Yep, there was a call to removeScale in there...
301 Scene_BrushGetTexdef_Component_Selected( GlobalSceneGraph(), projection );
303 SurfaceInspector_SetSelectedTexdef( projection );
305 Scene_BrushGetShaderSize_Component_Selected( GlobalSceneGraph(), g_selectedShaderSize[0], g_selectedShaderSize[1] );
306 g_selectedTexdef.m_brushprimit_texdef.coords[0][2] = float_mod( g_selectedTexdef.m_brushprimit_texdef.coords[0][2], (float)g_selectedShaderSize[0] );
307 g_selectedTexdef.m_brushprimit_texdef.coords[1][2] = float_mod( g_selectedTexdef.m_brushprimit_texdef.coords[1][2], (float)g_selectedShaderSize[1] );
310 Scene_BrushGetShader_Component_Selected( GlobalSceneGraph(), name );
311 if ( string_not_empty( name.c_str() ) ) {
312 SurfaceInspector_SetSelectedShader( name.c_str() );
315 ContentsFlagsValue flags;
316 Scene_BrushGetFlags_Component_Selected( GlobalSceneGraph(), flags );
317 SurfaceInspector_SetSelectedFlags( flags );
321 TextureProjection projection;
322 Scene_BrushGetTexdef_Selected( GlobalSceneGraph(), projection );
323 SurfaceInspector_SetSelectedTexdef( projection );
326 Scene_BrushGetShader_Selected( GlobalSceneGraph(), name );
327 if ( string_empty( name.c_str() ) ) {
328 Scene_PatchGetShader_Selected( GlobalSceneGraph(), name );
330 if ( string_not_empty( name.c_str() ) ) {
331 SurfaceInspector_SetSelectedShader( name.c_str() );
334 ContentsFlagsValue flags( 0, 0, 0, false );
335 Scene_BrushGetFlags_Selected( GlobalSceneGraph(), flags );
336 SurfaceInspector_SetSelectedFlags( flags );
341 const char* SurfaceInspector_GetSelectedShader(){
342 SurfaceInspector_SetCurrent_FromSelected();
343 return g_selectedShader.c_str();
346 const TextureProjection& SurfaceInspector_GetSelectedTexdef(){
347 SurfaceInspector_SetCurrent_FromSelected();
348 return g_selectedTexdef;
351 const ContentsFlagsValue& SurfaceInspector_GetSelectedFlags(){
352 SurfaceInspector_SetCurrent_FromSelected();
353 return g_selectedFlags;
359 ===================================================
363 ===================================================
366 si_globals_t g_si_globals;
369 // make the shift increments match the grid settings
370 // the objective being that the shift+arrows shortcuts move the texture by the corresponding grid size
371 // this depends on a scale value if you have selected a particular texture on which you want it to work:
372 // we move the textures in pixels, not world units. (i.e. increment values are in pixel)
373 // depending on the texture scale it doesn't take the same amount of pixels to move of GetGridSize()
374 // increment * scale = gridsize
375 // hscale and vscale are optional parameters, if they are zero they will be set to the default scale
376 // NOTE: the default scale depends if you are using BP mode or regular.
377 // For regular it's 0.5f (128 pixels cover 64 world units), for BP it's simply 1.0f
379 void DoSnapTToGrid( float hscale, float vscale ){
380 g_si_globals.shift[0] = static_cast<float>( float_to_integer( static_cast<float>( GetGridSize() ) / hscale ) );
381 g_si_globals.shift[1] = static_cast<float>( float_to_integer( static_cast<float>( GetGridSize() ) / vscale ) );
382 getSurfaceInspector().queueDraw();
385 void SurfaceInspector_GridChange(){
386 if ( g_si_globals.m_bSnapTToGrid ) {
387 DoSnapTToGrid( Texdef_getDefaultTextureScale(), Texdef_getDefaultTextureScale() );
391 // make the shift increments match the grid settings
392 // the objective being that the shift+arrows shortcuts move the texture by the corresponding grid size
393 // this depends on the current texture scale used?
394 // we move the textures in pixels, not world units. (i.e. increment values are in pixel)
395 // depending on the texture scale it doesn't take the same amount of pixels to move of GetGridSize()
396 // increment * scale = gridsize
397 static void OnBtnMatchGrid( ui::Widget widget, gpointer data ){
398 float hscale, vscale;
399 hscale = static_cast<float>( gtk_spin_button_get_value( getSurfaceInspector().m_hscaleIncrement.m_spin ) );
400 vscale = static_cast<float>( gtk_spin_button_get_value( getSurfaceInspector().m_vscaleIncrement.m_spin ) );
402 if ( hscale == 0.0f || vscale == 0.0f ) {
403 globalOutputStream() << "ERROR: unexpected scale == 0.0f\n";
407 DoSnapTToGrid( hscale, vscale );
410 // DoSurface will always try to show the surface inspector
411 // or update it because something new has been selected
412 // Shamus: It does get called when the SI is hidden, but not when you select something new. ;-)
413 void DoSurface( void ){
414 if ( !getSurfaceInspector().GetWidget() ) {
415 getSurfaceInspector().Create();
418 getSurfaceInspector().Update();
419 getSurfaceInspector().importData();
420 getSurfaceInspector().ShowDlg();
423 void SurfaceInspector_toggleShown(){
424 if ( getSurfaceInspector().visible() ) {
425 getSurfaceInspector().HideDlg();
433 void SurfaceInspector_FitTexture(){
434 UndoableCommand undo( "textureAutoFit" );
435 Select_FitTexture( getSurfaceInspector().m_fitHorizontal, getSurfaceInspector().m_fitVertical );
438 static void OnBtnPatchdetails( ui::Widget widget, gpointer data ){
442 static void OnBtnPatchnatural( ui::Widget widget, gpointer data ){
443 Patch_NaturalTexture();
446 static void OnBtnPatchreset( ui::Widget widget, gpointer data ){
447 Patch_ResetTexture();
450 static void OnBtnPatchFit( ui::Widget widget, gpointer data ){
454 static void OnBtnAxial( ui::Widget widget, gpointer data ){
455 //globalOutputStream() << "--> [OnBtnAxial]...\n";
456 UndoableCommand undo( "textureDefault" );
457 TextureProjection projection;
458 //globalOutputStream() << " TexDef_Construct_Default()...\n";
459 TexDef_Construct_Default( projection );
460 //globalOutputStream() << " Select_SetTexdef()...\n";
465 if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
466 // Scale up texture width/height if in BP mode...
467 //NOTE: This may not be correct any more! :-P
468 if ( !g_SelectedFaceInstances.empty() ) {
469 Face & face = g_SelectedFaceInstances.last().getFace();
470 float x = face.getShader().m_state->getTexture().width;
471 float y = face.getShader().m_state->getTexture().height;
472 projection.m_brushprimit_texdef.coords[0][0] /= x;
473 projection.m_brushprimit_texdef.coords[0][1] /= y;
474 projection.m_brushprimit_texdef.coords[1][0] /= x;
475 projection.m_brushprimit_texdef.coords[1][1] /= y;
480 Select_SetTexdef( projection );
483 static void OnBtnFaceFit( ui::Widget widget, gpointer data ){
484 getSurfaceInspector().exportData();
485 SurfaceInspector_FitTexture();
488 typedef const char* FlagName;
490 const FlagName surfaceflagNamesDefault[32] = {
525 const FlagName contentflagNamesDefault[32] = {
560 const char* getSurfaceFlagName( std::size_t bit ){
561 const char* value = g_pGameDescription->getKeyValue( surfaceflagNamesDefault[bit] );
562 if ( string_empty( value ) ) {
563 return surfaceflagNamesDefault[bit];
568 const char* getContentFlagName( std::size_t bit ){
569 const char* value = g_pGameDescription->getKeyValue( contentflagNamesDefault[bit] );
570 if ( string_empty( value ) ) {
571 return contentflagNamesDefault[bit];
577 // =============================================================================
578 // SurfaceInspector class
580 guint togglebutton_connect_toggled( GtkToggleButton* button, const Callback& callback ){
581 return g_signal_connect_swapped( G_OBJECT( button ), "toggled", G_CALLBACK( callback.getThunk() ), callback.getEnvironment() );
584 ui::Window SurfaceInspector::BuildDialog(){
585 ui::Window window = ui::Window(create_floating_window( "Surface Inspector", m_parent ));
587 m_positionTracker.connect( window );
589 global_accel_connect_window( window );
591 window_connect_focus_in_clear_focus_widget( window );
595 // replaced by only the vbox:
596 ui::Widget vbox = ui::VBox( FALSE, 5 );
599 gtk_container_set_border_width( GTK_CONTAINER( vbox ), 5 );
602 ui::Widget hbox2 = ui::HBox( FALSE, 5 );
604 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( hbox2 ), FALSE, FALSE, 0 );
607 ui::Widget label = ui::Label( "Texture" );
609 gtk_box_pack_start( GTK_BOX( hbox2 ), label, FALSE, TRUE, 0 );
612 auto entry = ui::Entry();
614 gtk_box_pack_start( GTK_BOX( hbox2 ), GTK_WIDGET( entry ), TRUE, TRUE, 0 );
616 m_textureEntry.connect( entry );
617 GlobalTextureEntryCompletion::instance().connect( entry );
623 ui::Widget table = ui::Table( 6, 4, FALSE );
625 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( table ), FALSE, FALSE, 0 );
626 gtk_table_set_row_spacings( GTK_TABLE( table ), 5 );
627 gtk_table_set_col_spacings( GTK_TABLE( table ), 5 );
629 ui::Widget label = ui::Label( "Horizontal shift" );
631 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
632 gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 0, 1,
633 (GtkAttachOptions) ( GTK_FILL ),
634 (GtkAttachOptions) ( 0 ), 0, 0 );
637 auto spin = ui::SpinButton( ui::Adjustment( 0, -8192, 8192, 2, 8, 0 ), 0, 2 );
638 m_hshiftIncrement.m_spin = spin;
639 m_hshiftSpinner.connect( spin );
641 gtk_table_attach( GTK_TABLE( table ), GTK_WIDGET( spin ), 1, 2, 0, 1,
642 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
643 (GtkAttachOptions) ( 0 ), 0, 0 );
644 gtk_widget_set_size_request( GTK_WIDGET( spin ), 60, -1 );
647 ui::Widget label = ui::Label( "Step" );
649 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
650 gtk_table_attach( GTK_TABLE( table ), label, 2, 3, 0, 1,
651 (GtkAttachOptions) ( GTK_FILL ),
652 (GtkAttachOptions) ( 0 ), 0, 0 );
655 auto entry = ui::Entry();
657 gtk_table_attach( GTK_TABLE( table ), GTK_WIDGET( entry ), 3, 4, 0, 1,
658 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
659 (GtkAttachOptions) ( 0 ), 0, 0 );
660 gtk_widget_set_size_request( GTK_WIDGET( entry ), 50, -1 );
661 m_hshiftIncrement.m_entry = entry;
662 m_hshiftEntry.connect( entry );
665 ui::Widget label = ui::Label( "Vertical shift" );
667 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
668 gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 1, 2,
669 (GtkAttachOptions) ( GTK_FILL ),
670 (GtkAttachOptions) ( 0 ), 0, 0 );
673 auto spin = ui::SpinButton( ui::Adjustment( 0, -8192, 8192, 2, 8, 0 ), 0, 2 );
674 m_vshiftIncrement.m_spin = spin;
675 m_vshiftSpinner.connect( spin );
677 gtk_table_attach( GTK_TABLE( table ), GTK_WIDGET( spin ), 1, 2, 1, 2,
678 (GtkAttachOptions) ( GTK_FILL ),
679 (GtkAttachOptions) ( 0 ), 0, 0 );
680 gtk_widget_set_size_request( GTK_WIDGET( spin ), 60, -1 );
683 ui::Widget label = ui::Label( "Step" );
685 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
686 gtk_table_attach( GTK_TABLE( table ), label, 2, 3, 1, 2,
687 (GtkAttachOptions) ( GTK_FILL ),
688 (GtkAttachOptions) ( 0 ), 0, 0 );
691 auto entry = ui::Entry();
693 gtk_table_attach( GTK_TABLE( table ), GTK_WIDGET( entry ), 3, 4, 1, 2,
694 (GtkAttachOptions) ( GTK_FILL ),
695 (GtkAttachOptions) ( 0 ), 0, 0 );
696 gtk_widget_set_size_request( GTK_WIDGET( entry ), 50, -1 );
697 m_vshiftIncrement.m_entry = entry;
698 m_vshiftEntry.connect( entry );
701 ui::Widget label = ui::Label( "Horizontal stretch" );
703 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
704 gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 2, 3,
705 (GtkAttachOptions) ( GTK_FILL ),
706 (GtkAttachOptions) ( 0 ), 0, 0 );
709 auto spin = ui::SpinButton( ui::Adjustment( 0, -8192, 8192, 2, 8, 0 ), 0, 5 );
710 m_hscaleIncrement.m_spin = spin;
711 m_hscaleSpinner.connect( spin );
713 gtk_table_attach( GTK_TABLE( table ), GTK_WIDGET( spin ), 1, 2, 2, 3,
714 (GtkAttachOptions) ( GTK_FILL ),
715 (GtkAttachOptions) ( 0 ), 0, 0 );
716 gtk_widget_set_size_request( GTK_WIDGET( spin ), 60, -1 );
719 ui::Widget label = ui::Label( "Step" );
721 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
722 gtk_table_attach( GTK_TABLE( table ), label, 2, 3, 2, 3,
723 (GtkAttachOptions) ( GTK_FILL ),
724 (GtkAttachOptions) ( 0 ), 2, 3 );
727 auto entry = ui::Entry();
729 gtk_table_attach( GTK_TABLE( table ), GTK_WIDGET( entry ), 3, 4, 2, 3,
730 (GtkAttachOptions) ( GTK_FILL ),
731 (GtkAttachOptions) ( 0 ), 2, 3 );
732 gtk_widget_set_size_request( GTK_WIDGET( entry ), 50, -1 );
733 m_hscaleIncrement.m_entry = entry;
734 m_hscaleEntry.connect( entry );
737 ui::Widget label = ui::Label( "Vertical stretch" );
739 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
740 gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 3, 4,
741 (GtkAttachOptions) ( GTK_FILL ),
742 (GtkAttachOptions) ( 0 ), 0, 0 );
745 auto spin = ui::SpinButton( ui::Adjustment( 0, -8192, 8192, 2, 8, 0 ), 0, 5 );
746 m_vscaleIncrement.m_spin = spin;
747 m_vscaleSpinner.connect( spin );
749 gtk_table_attach( GTK_TABLE( table ), GTK_WIDGET( spin ), 1, 2, 3, 4,
750 (GtkAttachOptions) ( GTK_FILL ),
751 (GtkAttachOptions) ( 0 ), 0, 0 );
752 gtk_widget_set_size_request( GTK_WIDGET( spin ), 60, -1 );
755 ui::Widget label = ui::Label( "Step" );
757 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
758 gtk_table_attach( GTK_TABLE( table ), label, 2, 3, 3, 4,
759 (GtkAttachOptions) ( GTK_FILL ),
760 (GtkAttachOptions) ( 0 ), 0, 0 );
763 auto entry = ui::Entry();
765 gtk_table_attach( GTK_TABLE( table ), GTK_WIDGET( entry ), 3, 4, 3, 4,
766 (GtkAttachOptions) ( GTK_FILL ),
767 (GtkAttachOptions) ( 0 ), 0, 0 );
768 gtk_widget_set_size_request( GTK_WIDGET( entry ), 50, -1 );
769 m_vscaleIncrement.m_entry = entry;
770 m_vscaleEntry.connect( entry );
773 ui::Widget label = ui::Label( "Rotate" );
775 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
776 gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 4, 5,
777 (GtkAttachOptions) ( GTK_FILL ),
778 (GtkAttachOptions) ( 0 ), 0, 0 );
781 auto spin = ui::SpinButton( ui::Adjustment( 0, -8192, 8192, 2, 8, 0 ), 0, 2 );
782 m_rotateIncrement.m_spin = spin;
783 m_rotateSpinner.connect( spin );
785 gtk_table_attach( GTK_TABLE( table ), GTK_WIDGET( spin ), 1, 2, 4, 5,
786 (GtkAttachOptions) ( GTK_FILL ),
787 (GtkAttachOptions) ( 0 ), 0, 0 );
788 gtk_widget_set_size_request( GTK_WIDGET( spin ), 60, -1 );
789 gtk_spin_button_set_wrap( spin, TRUE );
792 ui::Widget label = ui::Label( "Step" );
794 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
795 gtk_table_attach( GTK_TABLE( table ), label, 2, 3, 4, 5,
796 (GtkAttachOptions) ( GTK_FILL ),
797 (GtkAttachOptions) ( 0 ), 0, 0 );
800 auto entry = ui::Entry();
802 gtk_table_attach( GTK_TABLE( table ), GTK_WIDGET( entry ), 3, 4, 4, 5,
803 (GtkAttachOptions) ( GTK_FILL ),
804 (GtkAttachOptions) ( 0 ), 0, 0 );
805 gtk_widget_set_size_request( GTK_WIDGET( entry ), 50, -1 );
806 m_rotateIncrement.m_entry = entry;
807 m_rotateEntry.connect( entry );
811 ui::Widget button = ui::Button( "Match Grid" );
813 gtk_table_attach( GTK_TABLE( table ), button, 2, 4, 5, 6,
814 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
815 (GtkAttachOptions) ( 0 ), 0, 0 );
816 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( OnBtnMatchGrid ), 0 );
821 auto frame = ui::Frame( "Texturing" );
823 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( frame ), FALSE, FALSE, 0 );
825 ui::Widget table = ui::Table( 4, 4, FALSE );
828 gtk_table_set_row_spacings( GTK_TABLE( table ), 5 );
829 gtk_table_set_col_spacings( GTK_TABLE( table ), 5 );
830 gtk_container_set_border_width( GTK_CONTAINER( table ), 5 );
832 ui::Widget label = ui::Label( "Brush" );
834 gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 0, 1,
835 (GtkAttachOptions) ( GTK_FILL ),
836 (GtkAttachOptions) ( 0 ), 0, 0 );
839 ui::Widget label = ui::Label( "Patch" );
841 gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 2, 3,
842 (GtkAttachOptions) ( GTK_FILL ),
843 (GtkAttachOptions) ( 0 ), 0, 0 );
846 ui::Widget label = ui::Label( "Width" );
848 gtk_table_attach( GTK_TABLE( table ), label, 2, 3, 0, 1,
849 (GtkAttachOptions) ( GTK_FILL ),
850 (GtkAttachOptions) ( 0 ), 0, 0 );
853 ui::Widget label = ui::Label( "Height" );
855 gtk_table_attach( GTK_TABLE( table ), label, 3, 4, 0, 1,
856 (GtkAttachOptions) ( GTK_FILL ),
857 (GtkAttachOptions) ( 0 ), 0, 0 );
860 ui::Widget button = ui::Button( "Axial" );
862 gtk_table_attach( GTK_TABLE( table ), button, 0, 1, 1, 2,
863 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
864 (GtkAttachOptions) ( 0 ), 0, 0 );
865 g_signal_connect( G_OBJECT( button ), "clicked",
866 G_CALLBACK( OnBtnAxial ), 0 );
867 gtk_widget_set_size_request( button, 60, -1 );
870 ui::Widget button = ui::Button( "Fit" );
872 gtk_table_attach( GTK_TABLE( table ), button, 1, 2, 1, 2,
873 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
874 (GtkAttachOptions) ( 0 ), 0, 0 );
875 g_signal_connect( G_OBJECT( button ), "clicked",
876 G_CALLBACK( OnBtnFaceFit ), 0 );
877 gtk_widget_set_size_request( button, 60, -1 );
880 ui::Widget button = ui::Button( "CAP" );
882 gtk_table_attach( GTK_TABLE( table ), button, 0, 1, 3, 4,
883 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
884 (GtkAttachOptions) ( 0 ), 0, 0 );
885 g_signal_connect( G_OBJECT( button ), "clicked",
886 G_CALLBACK( OnBtnPatchdetails ), 0 );
887 gtk_widget_set_size_request( button, 60, -1 );
890 ui::Widget button = ui::Button( "Set..." );
892 gtk_table_attach( GTK_TABLE( table ), button, 1, 2, 3, 4,
893 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
894 (GtkAttachOptions) ( 0 ), 0, 0 );
895 g_signal_connect( G_OBJECT( button ), "clicked",
896 G_CALLBACK( OnBtnPatchreset ), 0 );
897 gtk_widget_set_size_request( button, 60, -1 );
900 ui::Widget button = ui::Button( "Natural" );
902 gtk_table_attach( GTK_TABLE( table ), button, 2, 3, 3, 4,
903 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
904 (GtkAttachOptions) ( 0 ), 0, 0 );
905 g_signal_connect( G_OBJECT( button ), "clicked",
906 G_CALLBACK( OnBtnPatchnatural ), 0 );
907 gtk_widget_set_size_request( button, 60, -1 );
910 ui::Widget button = ui::Button( "Fit" );
912 gtk_table_attach( GTK_TABLE( table ), button, 3, 4, 3, 4,
913 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
914 (GtkAttachOptions) ( 0 ), 0, 0 );
915 g_signal_connect( G_OBJECT( button ), "clicked",
916 G_CALLBACK( OnBtnPatchFit ), 0 );
917 gtk_widget_set_size_request( button, 60, -1 );
920 auto spin = ui::SpinButton( ui::Adjustment( 1, 0, 1 << 16, 1, 10, 0 ), 0, 6 );
922 gtk_table_attach( GTK_TABLE( table ), spin, 2, 3, 1, 2,
923 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
924 (GtkAttachOptions) ( 0 ), 0, 0 );
925 gtk_widget_set_size_request( spin, 60, -1 );
926 AddDialogData( *GTK_SPIN_BUTTON( spin ), m_fitHorizontal );
929 auto spin = ui::SpinButton( ui::Adjustment( 1, 0, 1 << 16, 1, 10, 0 ), 0, 6 );
931 gtk_table_attach( GTK_TABLE( table ), spin, 3, 4, 1, 2,
932 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
933 (GtkAttachOptions) ( 0 ), 0, 0 );
934 gtk_widget_set_size_request( spin, 60, -1 );
935 AddDialogData( *GTK_SPIN_BUTTON( spin ), m_fitVertical );
939 if ( !string_empty( g_pGameDescription->getKeyValue( "si_flags" ) ) ) {
941 auto frame = ui::Frame( "Surface Flags" );
943 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( frame ), TRUE, TRUE, 0 );
945 auto vbox3 = ui::VBox( FALSE, 4 );
946 //gtk_container_set_border_width(GTK_CONTAINER(vbox3), 4);
950 auto table = ui::Table( 8, 4, FALSE );
952 gtk_box_pack_start( GTK_BOX( vbox3 ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
953 gtk_table_set_row_spacings( table, 0 );
954 gtk_table_set_col_spacings( table, 0 );
956 GtkCheckButton** p = m_surfaceFlags;
958 for ( int c = 0; c != 4; ++c )
960 for ( int r = 0; r != 8; ++r )
962 auto check = ui::CheckButton( getSurfaceFlagName( c * 8 + r ) );
964 gtk_table_attach( table, GTK_WIDGET( check ), c, c + 1, r, r + 1,
965 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
966 (GtkAttachOptions)( 0 ), 0, 0 );
968 guint handler_id = togglebutton_connect_toggled( GTK_TOGGLE_BUTTON( check ), ApplyFlagsCaller( *this ) );
969 g_object_set_data( G_OBJECT( check ), "handler", gint_to_pointer( handler_id ) );
976 auto frame = ui::Frame( "Content Flags" );
978 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( frame ), TRUE, TRUE, 0 );
980 auto vbox3 = ui::VBox( FALSE, 4 );
981 //gtk_container_set_border_width(GTK_CONTAINER(vbox3), 4);
986 auto table = ui::Table( 8, 4, FALSE );
988 gtk_box_pack_start( GTK_BOX( vbox3 ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
989 gtk_table_set_row_spacings( table, 0 );
990 gtk_table_set_col_spacings( table, 0 );
992 GtkCheckButton** p = m_contentFlags;
994 for ( int c = 0; c != 4; ++c )
996 for ( int r = 0; r != 8; ++r )
998 auto check = ui::CheckButton( getContentFlagName( c * 8 + r ) );
1000 gtk_table_attach( table, GTK_WIDGET( check ), c, c + 1, r, r + 1,
1001 (GtkAttachOptions)( GTK_EXPAND | GTK_FILL ),
1002 (GtkAttachOptions)( 0 ), 0, 0 );
1004 guint handler_id = togglebutton_connect_toggled( GTK_TOGGLE_BUTTON( check ), ApplyFlagsCaller( *this ) );
1005 g_object_set_data( G_OBJECT( check ), "handler", gint_to_pointer( handler_id ) );
1009 // not allowed to modify detail flag using Surface Inspector
1010 gtk_widget_set_sensitive( GTK_WIDGET( m_contentFlags[BRUSH_DETAIL_FLAG] ), FALSE );
1015 auto frame = ui::Frame( "Value" );
1017 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( frame ), TRUE, TRUE, 0 );
1019 auto vbox3 = ui::VBox( FALSE, 4 );
1020 gtk_container_set_border_width( GTK_CONTAINER( vbox3 ), 4 );
1025 auto entry = ui::Entry();
1027 gtk_box_pack_start( GTK_BOX( vbox3 ), GTK_WIDGET( entry ), TRUE, TRUE, 0 );
1028 m_valueEntryWidget = entry;
1029 m_valueEntry.connect( entry );
1036 if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
1037 // Shamus: Textool goodies...
1038 ui::Widget frame = ui::Frame( "Textool" );
1040 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( frame ), FALSE, FALSE, 0 );
1042 //Prolly should make this a member or global var, so the SI can draw on it...
1043 TexTool::g_textoolWin = glwidget_new( FALSE );
1044 // --> Dunno, but this stuff may be necessary... (Looks like it!)
1045 g_object_ref( TexTool::g_textoolWin );
1046 gtk_widget_set_events( TexTool::g_textoolWin, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK );
1047 gtk_widget_set_can_focus( TexTool::g_textoolWin, true );
1049 TexTool::g_textoolWin.show();
1050 gtk_widget_set_size_request( TexTool::g_textoolWin, -1, 240 ); //Yeah!
1051 frame.add(TexTool::g_textoolWin);
1053 g_signal_connect( G_OBJECT( TexTool::g_textoolWin ), "size_allocate", G_CALLBACK( TexTool::size_allocate ), NULL );
1054 g_signal_connect( G_OBJECT( TexTool::g_textoolWin ), "expose_event", G_CALLBACK( TexTool::expose ), NULL );
1055 g_signal_connect( G_OBJECT( TexTool::g_textoolWin ), "button_press_event", G_CALLBACK( TexTool::button_press ), NULL );
1056 g_signal_connect( G_OBJECT( TexTool::g_textoolWin ), "button_release_event", G_CALLBACK( TexTool::button_release ), NULL );
1057 g_signal_connect( G_OBJECT( TexTool::g_textoolWin ), "motion_notify_event", G_CALLBACK( TexTool::motion ), NULL );
1060 ui::Widget hbox = ui::HBox( FALSE, 5 );
1062 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( hbox ), FALSE, FALSE, 0 );
1063 // Checkboxes go here... (Flip X/Y)
1064 ui::Widget flipX = ui::CheckButton( "Flip X axis" );
1065 ui::Widget flipY = ui::CheckButton( "Flip Y axis" );
1068 gtk_box_pack_start( GTK_BOX( hbox ), flipX, FALSE, FALSE, 0 );
1069 gtk_box_pack_start( GTK_BOX( hbox ), flipY, FALSE, FALSE, 0 );
1071 //Instead of this, we probably need to create a vbox to put into the frame, then the
1072 //window, then the hbox. !!! FIX !!!
1075 //Hmm. Do we really need g_object_set_data? Mebbe not... And we don't! :-)
1076 // g_object_set_data(G_OBJECT(flipX), "handler", gint_to_pointer(g_signal_connect(G_OBJECT(flipX), "toggled", G_CALLBACK(TexTool::flipX), 0)));
1077 // g_object_set_data(G_OBJECT(flipY), "handler", gint_to_pointer(g_signal_connect(G_OBJECT(flipY), "toggled", G_CALLBACK(TexTool::flipY), 0)));
1079 g_signal_connect( G_OBJECT( flipX ), "toggled", G_CALLBACK( TexTool::flipX ), NULL );
1080 g_signal_connect( G_OBJECT( flipY ), "toggled", G_CALLBACK( TexTool::flipY ), NULL );
1093 Set the fields to the current texdef (i.e. map/texdef -> dialog widgets)
1094 if faces selected (instead of brushes) -> will read this face texdef, else current texdef
1095 if only patches selected, will read the patch texdef
1099 void spin_button_set_value_no_signal( GtkSpinButton* spin, gdouble value ){
1100 guint handler_id = gpointer_to_int( g_object_get_data( G_OBJECT( spin ), "handler" ) );
1101 g_signal_handler_block( G_OBJECT( gtk_spin_button_get_adjustment( spin ) ), handler_id );
1102 gtk_spin_button_set_value( spin, value );
1103 g_signal_handler_unblock( G_OBJECT( gtk_spin_button_get_adjustment( spin ) ), handler_id );
1106 void spin_button_set_step_increment( GtkSpinButton* spin, gdouble value ){
1107 GtkAdjustment* adjust = gtk_spin_button_get_adjustment( spin );
1108 gtk_adjustment_set_step_increment(adjust, value);
1111 void SurfaceInspector::Update(){
1112 const char * name = SurfaceInspector_GetSelectedShader();
1114 if ( shader_is_texture( name ) ) {
1115 gtk_entry_set_text( m_texture, shader_get_textureName( name ) );
1119 gtk_entry_set_text( m_texture, "" );
1122 texdef_t shiftScaleRotate;
1123 //Shamus: This is where we get into trouble--the BP code tries to convert to a "faked"
1124 //shift, rotate & scale values from the brush face, which seems to screw up for some reason.
1126 /*globalOutputStream() << "--> SI::Update. About to do ShiftScaleRotate_fromFace()...\n";
1127 SurfaceInspector_GetSelectedBPTexdef();
1128 globalOutputStream() << "BP: (" << g_selectedBrushPrimitTexdef.coords[0][0] << ", " << g_selectedBrushPrimitTexdef.coords[0][1] << ")("
1129 << g_selectedBrushPrimitTexdef.coords[1][0] << ", " << g_selectedBrushPrimitTexdef.coords[1][1] << ")("
1130 << g_selectedBrushPrimitTexdef.coords[0][2] << ", " << g_selectedBrushPrimitTexdef.coords[1][2] << ") SurfaceInspector::Update\n";//*/
1131 //Ok, it's screwed up *before* we get here...
1132 ShiftScaleRotate_fromFace( shiftScaleRotate, SurfaceInspector_GetSelectedTexdef() );
1134 // normalize again to hide the ridiculously high scale values that get created when using texlock
1135 shiftScaleRotate.shift[0] = float_mod( shiftScaleRotate.shift[0], (float)g_selectedShaderSize[0] );
1136 shiftScaleRotate.shift[1] = float_mod( shiftScaleRotate.shift[1], (float)g_selectedShaderSize[1] );
1139 spin_button_set_value_no_signal( m_hshiftIncrement.m_spin, shiftScaleRotate.shift[0] );
1140 spin_button_set_step_increment( m_hshiftIncrement.m_spin, g_si_globals.shift[0] );
1141 entry_set_float( m_hshiftIncrement.m_entry, g_si_globals.shift[0] );
1145 spin_button_set_value_no_signal( m_vshiftIncrement.m_spin, shiftScaleRotate.shift[1] );
1146 spin_button_set_step_increment( m_vshiftIncrement.m_spin, g_si_globals.shift[1] );
1147 entry_set_float( m_vshiftIncrement.m_entry, g_si_globals.shift[1] );
1151 spin_button_set_value_no_signal( m_hscaleIncrement.m_spin, shiftScaleRotate.scale[0] );
1152 spin_button_set_step_increment( m_hscaleIncrement.m_spin, g_si_globals.scale[0] );
1153 entry_set_float( m_hscaleIncrement.m_entry, g_si_globals.scale[0] );
1157 spin_button_set_value_no_signal( m_vscaleIncrement.m_spin, shiftScaleRotate.scale[1] );
1158 spin_button_set_step_increment( m_vscaleIncrement.m_spin, g_si_globals.scale[1] );
1159 entry_set_float( m_vscaleIncrement.m_entry, g_si_globals.scale[1] );
1163 spin_button_set_value_no_signal( m_rotateIncrement.m_spin, shiftScaleRotate.rotate );
1164 spin_button_set_step_increment( m_rotateIncrement.m_spin, g_si_globals.rotate );
1165 entry_set_float( m_rotateIncrement.m_entry, g_si_globals.rotate );
1168 if ( !string_empty( g_pGameDescription->getKeyValue( "si_flags" ) ) ) {
1169 ContentsFlagsValue flags( SurfaceInspector_GetSelectedFlags() );
1171 entry_set_int( m_valueEntryWidget, flags.m_value );
1173 for ( GtkCheckButton** p = m_surfaceFlags; p != m_surfaceFlags + 32; ++p )
1175 toggle_button_set_active_no_signal( ui::CheckButton( *p ), flags.m_surfaceFlags & ( 1 << ( p - m_surfaceFlags ) ) );
1178 for ( GtkCheckButton** p = m_contentFlags; p != m_contentFlags + 32; ++p )
1180 toggle_button_set_active_no_signal( ui::CheckButton( *p ), flags.m_contentFlags & ( 1 << ( p - m_contentFlags ) ) );
1189 Reads the fields to get the current texdef (i.e. widgets -> MAP)
1190 in brush primitive mode, grab the fake shift scale rot and compute a new texture matrix
1193 void SurfaceInspector::ApplyShader(){
1194 StringOutputStream name( 256 );
1195 name << GlobalTexturePrefix_get() << gtk_entry_get_text( m_texture );
1197 // TTimo: detect and refuse invalid texture names (at least the ones with spaces)
1198 if ( !texdef_name_valid( name.c_str() ) ) {
1199 globalErrorStream() << "invalid texture name '" << name.c_str() << "'\n";
1200 SurfaceInspector_queueDraw();
1204 UndoableCommand undo( "textureNameSetSelected" );
1205 Select_SetShader( name.c_str() );
1208 void SurfaceInspector::ApplyTexdef(){
1209 texdef_t shiftScaleRotate;
1211 shiftScaleRotate.shift[0] = static_cast<float>( gtk_spin_button_get_value( m_hshiftIncrement.m_spin ) );
1212 shiftScaleRotate.shift[1] = static_cast<float>( gtk_spin_button_get_value( m_vshiftIncrement.m_spin ) );
1213 shiftScaleRotate.scale[0] = static_cast<float>( gtk_spin_button_get_value( m_hscaleIncrement.m_spin ) );
1214 shiftScaleRotate.scale[1] = static_cast<float>( gtk_spin_button_get_value( m_vscaleIncrement.m_spin ) );
1215 shiftScaleRotate.rotate = static_cast<float>( gtk_spin_button_get_value( m_rotateIncrement.m_spin ) );
1217 TextureProjection projection;
1218 //Shamus: This is the other place that screws up, it seems, since it doesn't seem to do the
1219 //conversion from the face (I think) and so bogus values end up in the thing... !!! FIX !!!
1220 //This is actually OK. :-P
1221 ShiftScaleRotate_toFace( shiftScaleRotate, projection );
1223 UndoableCommand undo( "textureProjectionSetSelected" );
1224 Select_SetTexdef( projection );
1227 void SurfaceInspector::ApplyFlags(){
1228 unsigned int surfaceflags = 0;
1229 for ( GtkCheckButton** p = m_surfaceFlags; p != m_surfaceFlags + 32; ++p )
1231 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( *p ) ) ) {
1232 surfaceflags |= ( 1 << ( p - m_surfaceFlags ) );
1236 unsigned int contentflags = 0;
1237 for ( GtkCheckButton** p = m_contentFlags; p != m_contentFlags + 32; ++p )
1239 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( *p ) ) ) {
1240 contentflags |= ( 1 << ( p - m_contentFlags ) );
1244 int value = entry_get_int( m_valueEntryWidget );
1246 UndoableCommand undo( "flagsSetSelected" );
1247 Select_SetFlags( ContentsFlagsValue( surfaceflags, contentflags, value, true ) );
1251 void Face_getTexture( Face& face, CopiedString& shader, TextureProjection& projection, ContentsFlagsValue& flags ){
1252 shader = face.GetShader();
1253 face.GetTexdef( projection );
1254 flags = face.getShader().m_flags;
1256 typedef Function4<Face&, CopiedString&, TextureProjection&, ContentsFlagsValue&, void, Face_getTexture> FaceGetTexture;
1258 void Face_setTexture( Face& face, const char* shader, const TextureProjection& projection, const ContentsFlagsValue& flags ){
1259 face.SetShader( shader );
1260 face.SetTexdef( projection );
1261 face.SetFlags( flags );
1263 typedef Function4<Face&, const char*, const TextureProjection&, const ContentsFlagsValue&, void, Face_setTexture> FaceSetTexture;
1266 void Patch_getTexture( Patch& patch, CopiedString& shader, TextureProjection& projection, ContentsFlagsValue& flags ){
1267 shader = patch.GetShader();
1268 projection = TextureProjection( texdef_t(), brushprimit_texdef_t(), Vector3( 0, 0, 0 ), Vector3( 0, 0, 0 ) );
1269 flags = ContentsFlagsValue( 0, 0, 0, false );
1271 typedef Function4<Patch&, CopiedString&, TextureProjection&, ContentsFlagsValue&, void, Patch_getTexture> PatchGetTexture;
1273 void Patch_setTexture( Patch& patch, const char* shader, const TextureProjection& projection, const ContentsFlagsValue& flags ){
1274 patch.SetShader( shader );
1276 typedef Function4<Patch&, const char*, const TextureProjection&, const ContentsFlagsValue&, void, Patch_setTexture> PatchSetTexture;
1279 typedef Callback3<CopiedString&, TextureProjection&, ContentsFlagsValue&> GetTextureCallback;
1280 typedef Callback3<const char*, const TextureProjection&, const ContentsFlagsValue&> SetTextureCallback;
1284 GetTextureCallback getTexture;
1285 SetTextureCallback setTexture;
1289 void Face_getClosest( Face& face, SelectionTest& test, SelectionIntersection& bestIntersection, Texturable& texturable ){
1290 SelectionIntersection intersection;
1291 face.testSelect( test, intersection );
1292 if ( intersection.valid()
1293 && SelectionIntersection_closer( intersection, bestIntersection ) ) {
1294 bestIntersection = intersection;
1295 texturable.setTexture = makeCallback3( FaceSetTexture(), face );
1296 texturable.getTexture = makeCallback3( FaceGetTexture(), face );
1301 class OccludeSelector : public Selector
1303 SelectionIntersection& m_bestIntersection;
1306 OccludeSelector( SelectionIntersection& bestIntersection, bool& occluded ) : m_bestIntersection( bestIntersection ), m_occluded( occluded ){
1309 void pushSelectable( Selectable& selectable ){
1311 void popSelectable(){
1313 void addIntersection( const SelectionIntersection& intersection ){
1314 if ( SelectionIntersection_closer( intersection, m_bestIntersection ) ) {
1315 m_bestIntersection = intersection;
1321 class BrushGetClosestFaceVisibleWalker : public scene::Graph::Walker
1323 SelectionTest& m_test;
1324 Texturable& m_texturable;
1325 mutable SelectionIntersection m_bestIntersection;
1327 BrushGetClosestFaceVisibleWalker( SelectionTest& test, Texturable& texturable ) : m_test( test ), m_texturable( texturable ){
1329 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1330 if ( path.top().get().visible() ) {
1331 BrushInstance* brush = Instance_getBrush( instance );
1333 m_test.BeginMesh( brush->localToWorld() );
1335 for ( Brush::const_iterator i = brush->getBrush().begin(); i != brush->getBrush().end(); ++i )
1337 Face_getClosest( *( *i ), m_test, m_bestIntersection, m_texturable );
1342 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
1343 if ( selectionTestable ) {
1345 OccludeSelector selector( m_bestIntersection, occluded );
1346 selectionTestable->testSelect( selector, m_test );
1348 Patch* patch = Node_getPatch( path.top() );
1350 m_texturable.setTexture = makeCallback3( PatchSetTexture(), *patch );
1351 m_texturable.getTexture = makeCallback3( PatchGetTexture(), *patch );
1355 m_texturable = Texturable();
1365 Texturable Scene_getClosestTexturable( scene::Graph& graph, SelectionTest& test ){
1366 Texturable texturable;
1367 graph.traverse( BrushGetClosestFaceVisibleWalker( test, texturable ) );
1371 bool Scene_getClosestTexture( scene::Graph& graph, SelectionTest& test, CopiedString& shader, TextureProjection& projection, ContentsFlagsValue& flags ){
1372 Texturable texturable = Scene_getClosestTexturable( graph, test );
1373 if ( texturable.getTexture != GetTextureCallback() ) {
1374 texturable.getTexture( shader, projection, flags );
1380 void Scene_setClosestTexture( scene::Graph& graph, SelectionTest& test, const char* shader, const TextureProjection& projection, const ContentsFlagsValue& flags ){
1381 Texturable texturable = Scene_getClosestTexturable( graph, test );
1382 if ( texturable.setTexture != SetTextureCallback() ) {
1383 texturable.setTexture( shader, projection, flags );
1391 TextureProjection m_projection;
1392 ContentsFlagsValue m_flags;
1395 FaceTexture g_faceTextureClipboard;
1397 void FaceTextureClipboard_setDefault(){
1398 g_faceTextureClipboard.m_flags = ContentsFlagsValue( 0, 0, 0, false );
1399 TexDef_Construct_Default( g_faceTextureClipboard.m_projection );
1402 void TextureClipboard_textureSelected( const char* shader ){
1403 FaceTextureClipboard_setDefault();
1406 class TextureBrowser;
1407 extern TextureBrowser g_TextureBrowser;
1408 void TextureBrowser_SetSelectedShader( TextureBrowser& textureBrowser, const char* shader );
1409 const char* TextureBrowser_GetSelectedShader( TextureBrowser& textureBrowser );
1411 void Scene_copyClosestTexture( SelectionTest& test ){
1412 CopiedString shader;
1413 if ( Scene_getClosestTexture( GlobalSceneGraph(), test, shader, g_faceTextureClipboard.m_projection, g_faceTextureClipboard.m_flags ) ) {
1414 TextureBrowser_SetSelectedShader( g_TextureBrowser, shader.c_str() );
1418 void Scene_applyClosestTexture( SelectionTest& test ){
1419 UndoableCommand command( "facePaintTexture" );
1421 Scene_setClosestTexture( GlobalSceneGraph(), test, TextureBrowser_GetSelectedShader( g_TextureBrowser ), g_faceTextureClipboard.m_projection, g_faceTextureClipboard.m_flags );
1423 SceneChangeNotify();
1430 void SelectedFaces_copyTexture(){
1431 if ( !g_SelectedFaceInstances.empty() ) {
1432 Face& face = g_SelectedFaceInstances.last().getFace();
1433 face.GetTexdef( g_faceTextureClipboard.m_projection );
1434 g_faceTextureClipboard.m_flags = face.getShader().m_flags;
1436 TextureBrowser_SetSelectedShader( g_TextureBrowser, face.getShader().getShader() );
1440 void FaceInstance_pasteTexture( FaceInstance& faceInstance ){
1441 faceInstance.getFace().SetTexdef( g_faceTextureClipboard.m_projection );
1442 faceInstance.getFace().SetShader( TextureBrowser_GetSelectedShader( g_TextureBrowser ) );
1443 faceInstance.getFace().SetFlags( g_faceTextureClipboard.m_flags );
1444 SceneChangeNotify();
1447 bool SelectedFaces_empty(){
1448 return g_SelectedFaceInstances.empty();
1451 void SelectedFaces_pasteTexture(){
1452 UndoableCommand command( "facePasteTexture" );
1453 g_SelectedFaceInstances.foreach( FaceInstance_pasteTexture );
1458 void SurfaceInspector_constructPreferences( PreferencesPage& page ){
1459 page.appendCheckBox( "", "Surface Inspector Increments Match Grid", g_si_globals.m_bSnapTToGrid );
1461 void SurfaceInspector_constructPage( PreferenceGroup& group ){
1462 PreferencesPage page( group.createPage( "Surface Inspector", "Surface Inspector Preferences" ) );
1463 SurfaceInspector_constructPreferences( page );
1465 void SurfaceInspector_registerPreferencesPage(){
1466 PreferencesDialog_addSettingsPage( FreeCaller1<PreferenceGroup&, SurfaceInspector_constructPage>() );
1469 void SurfaceInspector_registerCommands(){
1470 GlobalCommands_insert( "FitTexture", FreeCaller<SurfaceInspector_FitTexture>(), Accelerator( 'B', (GdkModifierType)GDK_SHIFT_MASK ) );
1471 GlobalCommands_insert( "SurfaceInspector", FreeCaller<SurfaceInspector_toggleShown>(), Accelerator( 'S' ) );
1473 GlobalCommands_insert( "FaceCopyTexture", FreeCaller<SelectedFaces_copyTexture>() );
1474 GlobalCommands_insert( "FacePasteTexture", FreeCaller<SelectedFaces_pasteTexture>() );
1478 #include "preferencesystem.h"
1481 void SurfaceInspector_Construct(){
1482 g_SurfaceInspector = new SurfaceInspector;
1484 SurfaceInspector_registerCommands();
1486 FaceTextureClipboard_setDefault();
1488 GlobalPreferenceSystem().registerPreference( "SurfaceWnd", getSurfaceInspector().m_importPosition, getSurfaceInspector().m_exportPosition );
1489 GlobalPreferenceSystem().registerPreference( "SI_SurfaceTexdef_Scale1", FloatImportStringCaller( g_si_globals.scale[0] ), FloatExportStringCaller( g_si_globals.scale[0] ) );
1490 GlobalPreferenceSystem().registerPreference( "SI_SurfaceTexdef_Scale2", FloatImportStringCaller( g_si_globals.scale[1] ), FloatExportStringCaller( g_si_globals.scale[1] ) );
1491 GlobalPreferenceSystem().registerPreference( "SI_SurfaceTexdef_Shift1", FloatImportStringCaller( g_si_globals.shift[0] ), FloatExportStringCaller( g_si_globals.shift[0] ) );
1492 GlobalPreferenceSystem().registerPreference( "SI_SurfaceTexdef_Shift2", FloatImportStringCaller( g_si_globals.shift[1] ), FloatExportStringCaller( g_si_globals.shift[1] ) );
1493 GlobalPreferenceSystem().registerPreference( "SI_SurfaceTexdef_Rotate", FloatImportStringCaller( g_si_globals.rotate ), FloatExportStringCaller( g_si_globals.rotate ) );
1494 GlobalPreferenceSystem().registerPreference( "SnapTToGrid", BoolImportStringCaller( g_si_globals.m_bSnapTToGrid ), BoolExportStringCaller( g_si_globals.m_bSnapTToGrid ) );
1496 typedef FreeCaller1<const Selectable&, SurfaceInspector_SelectionChanged> SurfaceInspectorSelectionChangedCaller;
1497 GlobalSelectionSystem().addSelectionChangeCallback( SurfaceInspectorSelectionChangedCaller() );
1498 typedef FreeCaller<SurfaceInspector_updateSelection> SurfaceInspectorUpdateSelectionCaller;
1499 Brush_addTextureChangedCallback( SurfaceInspectorUpdateSelectionCaller() );
1500 Patch_addTextureChangedCallback( SurfaceInspectorUpdateSelectionCaller() );
1502 SurfaceInspector_registerPreferencesPage();
1504 void SurfaceInspector_Destroy(){
1505 delete g_SurfaceInspector;
1512 namespace TexTool { // namespace hides these symbols from other object-files
1514 //Shamus: Textool functions, including GTK+ callbacks
1517 //NOTE: Black screen when TT first comes up is caused by an uninitialized Extent... !!! FIX !!!
1518 // But... You can see down below that it *is* initialized! WTF?
1521 float minX, minY, maxX, maxY;
1522 float width( void ) { return fabs( maxX - minX ); }
1523 float height( void ) { return fabs( maxY - minY ); }
1526 //This seems to control the texture scale... (Yep! ;-)
1527 Extent extents = { -2.0f, -2.0f, +2.0f, +2.0f };
1528 brushprimit_texdef_t tm; // Texture transform matrix
1529 Vector2 pts[c_brush_maxFaces];
1533 Vector2 textureSize;
1535 #define VP_PADDING 1.2
1536 #define PI 3.14159265358979
1537 bool lButtonDown = false;
1538 bool rButtonDown = false;
1541 bool haveAnchor = false;
1542 brushprimit_texdef_t currentBP;
1543 brushprimit_texdef_t origBP; // Original brush primitive (before we muck it up)
1544 float controlRadius = 5.0f;
1545 float rotationAngle = 0.0f;
1546 float rotationAngle2 = 0.0f;
1547 float oldRotationAngle;
1548 Vector2 rotationPoint;
1549 bool translatingX = false; // Widget state variables
1550 bool translatingY = false;
1551 bool scalingX = false;
1552 bool scalingY = false;
1553 bool rotating = false;
1554 bool resizingX = false; // Not sure what this means... :-/
1555 bool resizingY = false;
1556 float origAngle, origScaleX, origScaleY;
1560 // Function prototypes (move up to top later...)
1562 void DrawCircularArc( Vector2 ctr, float startAngle, float endAngle, float radius );
1565 void CopyPointsFromSelectedFace( void ){
1566 // Make sure that there's a face and winding to get!
1568 if ( g_SelectedFaceInstances.empty() ) {
1573 Face & face = g_SelectedFaceInstances.last().getFace();
1574 textureNum = face.getShader().m_state->getTexture().texture_number;
1575 textureSize.x() = face.getShader().m_state->getTexture().width;
1576 textureSize.y() = face.getShader().m_state->getTexture().height;
1577 //globalOutputStream() << "--> Texture #" << textureNum << ": " << textureSize.x() << " x " << textureSize.y() << "...\n";
1579 currentBP = SurfaceInspector_GetSelectedTexdef().m_brushprimit_texdef;
1581 face.EmitTextureCoordinates();
1582 Winding & w = face.getWinding();
1585 for ( Winding::const_iterator i = w.begin(); i != w.end(); i++ )
1587 //globalOutputStream() << (*i).texcoord.x() << " " << (*i).texcoord.y() << ", ";
1588 pts[count].x() = ( *i ).texcoord.x();
1589 pts[count].y() = ( *i ).texcoord.y();
1595 //globalOutputStream() << " ..copied points\n";
1598 brushprimit_texdef_t bp;
1599 //This approach is probably wrongheaded and just not right anyway. So, !!! FIX !!! [DONE]
1600 void CommitChanges( void ){
1601 texdef_t t; // Throwaway, since this is BP only
1603 bp.coords[0][0] = tm.coords[0][0] * origBP.coords[0][0] + tm.coords[0][1] * origBP.coords[1][0];
1604 bp.coords[0][1] = tm.coords[0][0] * origBP.coords[0][1] + tm.coords[0][1] * origBP.coords[1][1];
1605 bp.coords[0][2] = tm.coords[0][0] * origBP.coords[0][2] + tm.coords[0][1] * origBP.coords[1][2] + tm.coords[0][2];
1606 //Ok, this works for translation...
1607 // 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();
1608 bp.coords[1][0] = tm.coords[1][0] * origBP.coords[0][0] + tm.coords[1][1] * origBP.coords[1][0];
1609 bp.coords[1][1] = tm.coords[1][0] * origBP.coords[0][1] + tm.coords[1][1] * origBP.coords[1][1];
1610 bp.coords[1][2] = tm.coords[1][0] * origBP.coords[0][2] + tm.coords[1][1] * origBP.coords[1][2] + tm.coords[1][2];
1611 // 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();
1613 //This doesn't work: g_brush_texture_changed();
1615 //Note: We should only set an undo *after* the button has been released... !!! FIX !!!
1616 //Definitely *should* have an undo, though!
1617 // UndoableCommand undo("textureProjectionSetSelected");
1618 Select_SetTexdef( TextureProjection( t, bp, Vector3( 0, 0, 0 ), Vector3( 0, 0, 0 ) ) );
1619 //This is working, but for some reason the translate is causing the rest of the SI
1620 //widgets to yield bad readings... !!! FIX !!!
1621 //I.e., click on textool window, translate face wireframe, then controls go crazy. Dunno why.
1622 //It's because there were some uncommented out add/removeScale functions in brush.h and a
1623 //removeScale in brushmanip.cpp... :-/
1624 //Translate isn't working at all now... :-(
1625 //It's because we need to multiply in some scaling factor (prolly the texture width/height)
1629 void UpdateControlPoints( void ){
1632 // Init texture transform matrix
1634 tm.coords[0][0] = 1.0f; tm.coords[0][1] = 0.0f; tm.coords[0][2] = 0.0f;
1635 tm.coords[1][0] = 0.0f; tm.coords[1][1] = 1.0f; tm.coords[1][2] = 0.0f;
1640 For shifting we have:
1643 The code that should provide reasonable defaults, but doesn't for some reason:
1644 It's scaling the BP by 128 for some reason, between the time it's created and the
1645 time we get back to the SI widgets:
1647 static void OnBtnAxial(GtkWidget *widget, gpointer data)
1649 UndoableCommand undo("textureDefault");
1650 TextureProjection projection;
1651 TexDef_Construct_Default(projection);
1652 Select_SetTexdef(projection);
1655 Select_SetTexdef() calls Scene_BrushSetTexdef_Component_Selected(GlobalSceneGraph(), projection)
1656 which is in brushmanip.h: This eventually calls
1657 Texdef_Assign(m_texdef, texdef, m_brushprimit_texdef, brushprimit_texdef) in class Face...
1658 which just copies from brushpr to m_brushpr...
1661 //Small problem with this thing: It's scaled to the texture which is all screwed up... !!! FIX !!! [DONE]
1662 //Prolly should separate out the grid drawing so that we can draw it behind the polygon.
1663 const float gridWidth = 1.3f; // Let's try an absolute height... WORKS!!!
1664 // NOTE that 2.0 is the height of the viewport. Dunno why... Should make collision
1665 // detection easier...
1666 const float gridRadius = gridWidth * 0.5f;
1668 typedef const float WidgetColor[3];
1669 const WidgetColor widgetColor[10] = {
1670 { 1.0000f, 0.2000f, 0.0000f }, // Red
1671 { 0.9137f, 0.9765f, 0.4980f }, // Yellow
1672 { 0.0000f, 0.6000f, 0.3216f }, // Green
1673 { 0.6157f, 0.7726f, 0.8196f }, // Cyan
1674 { 0.4980f, 0.5000f, 0.4716f }, // Grey
1677 { 1.0000f, 0.6000f, 0.4000f }, // Light Red
1678 { 1.0000f, 1.0000f, 0.8980f }, // Light Yellow
1679 { 0.4000f, 1.0000f, 0.7216f }, // Light Green
1680 { 1.0000f, 1.0000f, 1.0000f }, // Light Cyan
1681 { 0.8980f, 0.9000f, 0.8716f } // Light Grey
1685 #define COLOR_YELLOW 1
1686 #define COLOR_GREEN 2
1687 #define COLOR_CYAN 3
1688 #define COLOR_GREY 4
1689 #define COLOR_LT_RED 5
1690 #define COLOR_LT_YELLOW 6
1691 #define COLOR_LT_GREEN 7
1692 #define COLOR_LT_CYAN 8
1693 #define COLOR_LT_GREY 9
1695 void DrawControlWidgets( void ){
1696 //Note that the grid should go *behind* the face outline... !!! FIX !!!
1698 float xStart = center.x() - ( gridWidth / 2.0f );
1699 float yStart = center.y() - ( gridWidth / 2.0f );
1700 float xScale = ( extents.height() / extents.width() ) * ( textureSize.y() / textureSize.x() );
1703 //Small problem with this approach: Changing the center point in the TX code doesn't seem to
1704 //change anything here--prolly because we load a new identity matrix. A couple of ways to fix
1705 //this would be to get rid of that code, or change the center to a new point by taking into
1706 //account the transforms that we toss with the new identity matrix. Dunno which is better.
1708 glScalef( xScale, 1.0, 1.0 ); // Will that square it up? Yup.
1709 glRotatef( static_cast<float>( radians_to_degrees( atan2( -currentBP.coords[0][1], currentBP.coords[0][0] ) ) ), 0.0, 0.0, -1.0 );
1710 glTranslatef( -center.x(), -center.y(), 0.0 );
1713 glColor3fv( translatingX && translatingY ? widgetColor[COLOR_LT_YELLOW] : widgetColor[COLOR_YELLOW] );
1714 glBegin( GL_LINE_LOOP );
1715 DrawCircularArc( center, 0, 2.0f * PI, gridRadius * 0.16 );
1720 glBegin( GL_LINES );
1721 glColor3fv( translatingY && !translatingX ? widgetColor[COLOR_LT_GREEN] : widgetColor[COLOR_GREEN] );
1722 glVertex2f( center.x(), center.y() + ( gridRadius * 0.16 ) );
1723 glVertex2f( center.x(), center.y() + ( gridRadius * 1.00 ) );
1724 glColor3fv( translatingX && !translatingY ? widgetColor[COLOR_LT_RED] : widgetColor[COLOR_RED] );
1725 glVertex2f( center.x() + ( gridRadius * 0.16 ), center.y() );
1726 glVertex2f( center.x() + ( gridRadius * 1.00 ), center.y() );
1730 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1731 glBegin( GL_TRIANGLES );
1732 glColor3fv( translatingY && !translatingX ? widgetColor[COLOR_LT_GREEN] : widgetColor[COLOR_GREEN] );
1733 glVertex2f( center.x(), center.y() + ( gridRadius * 1.10 ) );
1734 glVertex2f( center.x() + ( gridRadius * 0.06 ), center.y() + ( gridRadius * 0.94 ) );
1735 glVertex2f( center.x() - ( gridRadius * 0.06 ), center.y() + ( gridRadius * 0.94 ) );
1736 glColor3fv( translatingX && !translatingY ? widgetColor[COLOR_LT_RED] : widgetColor[COLOR_RED] );
1737 glVertex2f( center.x() + ( gridRadius * 1.10 ), center.y() );
1738 glVertex2f( center.x() + ( gridRadius * 0.94 ), center.y() + ( gridRadius * 0.06 ) );
1739 glVertex2f( center.x() + ( gridRadius * 0.94 ), center.y() - ( gridRadius * 0.06 ) );
1743 glBegin( GL_LINE_STRIP );
1744 glColor3fv( rotating ? widgetColor[COLOR_LT_CYAN] : widgetColor[COLOR_CYAN] );
1745 DrawCircularArc( center, 0.03f * PI, 0.47f * PI, gridRadius * 0.90 );
1749 glColor3fv( scalingY && !scalingX ? widgetColor[COLOR_LT_GREEN] : widgetColor[COLOR_GREEN] );
1750 glBegin( GL_LINES );
1751 glVertex2f( center.x() + ( gridRadius * 0.20 ), center.y() + ( gridRadius * 1.50 ) );
1752 glVertex2f( center.x() - ( gridRadius * 0.20 ), center.y() + ( gridRadius * 1.50 ) );
1754 glBegin( GL_LINE_LOOP );
1755 glVertex2f( center.x() + ( gridRadius * 0.10 ), center.y() + ( gridRadius * 1.40 ) );
1756 glVertex2f( center.x() - ( gridRadius * 0.10 ), center.y() + ( gridRadius * 1.40 ) );
1757 glVertex2f( center.x() - ( gridRadius * 0.10 ), center.y() + ( gridRadius * 1.20 ) );
1758 glVertex2f( center.x() + ( gridRadius * 0.10 ), center.y() + ( gridRadius * 1.20 ) );
1761 glColor3fv( scalingX && !scalingY ? widgetColor[COLOR_LT_RED] : widgetColor[COLOR_RED] );
1762 glBegin( GL_LINES );
1763 glVertex2f( center.x() + ( gridRadius * 1.50 ), center.y() + ( gridRadius * 0.20 ) );
1764 glVertex2f( center.x() + ( gridRadius * 1.50 ), center.y() - ( gridRadius * 0.20 ) );
1766 glBegin( GL_LINE_LOOP );
1767 glVertex2f( center.x() + ( gridRadius * 1.40 ), center.y() + ( gridRadius * 0.10 ) );
1768 glVertex2f( center.x() + ( gridRadius * 1.40 ), center.y() - ( gridRadius * 0.10 ) );
1769 glVertex2f( center.x() + ( gridRadius * 1.20 ), center.y() - ( gridRadius * 0.10 ) );
1770 glVertex2f( center.x() + ( gridRadius * 1.20 ), center.y() + ( gridRadius * 0.10 ) );
1773 glColor3fv( scalingX && scalingY ? widgetColor[COLOR_LT_CYAN] : widgetColor[COLOR_CYAN] );
1774 glBegin( GL_LINE_STRIP );
1775 glVertex2f( center.x() + ( gridRadius * 1.50 ), center.y() + ( gridRadius * 1.10 ) );
1776 glVertex2f( center.x() + ( gridRadius * 1.50 ), center.y() + ( gridRadius * 1.50 ) );
1777 glVertex2f( center.x() + ( gridRadius * 1.10 ), center.y() + ( gridRadius * 1.50 ) );
1779 glBegin( GL_LINE_LOOP );
1780 glVertex2f( center.x() + ( gridRadius * 1.40 ), center.y() + ( gridRadius * 1.40 ) );
1781 glVertex2f( center.x() + ( gridRadius * 1.40 ), center.y() + ( gridRadius * 1.20 ) );
1782 glVertex2f( center.x() + ( gridRadius * 1.20 ), center.y() + ( gridRadius * 1.20 ) );
1783 glVertex2f( center.x() + ( gridRadius * 1.20 ), center.y() + ( gridRadius * 1.40 ) );
1789 void DrawControlPoints( void ){
1790 glColor3f( 1, 1, 1 );
1791 glBegin( GL_LINE_LOOP );
1793 for ( int i = 0; i < numPts; i++ )
1794 glVertex2f( pts[i].x(), pts[i].y() );
1799 // Note: Setup and all that jazz must be done by the caller!
1801 void DrawCircularArc( Vector2 ctr, float startAngle, float endAngle, float radius ){
1802 float stepSize = ( 2.0f * PI ) / 200.0f;
1804 for ( float angle = startAngle; angle <= endAngle; angle += stepSize )
1805 glVertex2f( ctr.x() + radius * cos( angle ), ctr.y() + radius * sin( angle ) );
1810 if ( numPts == 0 ) {
1814 // Find selected texture's extents...
1816 extents.minX = extents.maxX = pts[0].x(),
1817 extents.minY = extents.maxY = pts[0].y();
1819 for ( int i = 1; i < numPts; i++ )
1821 if ( pts[i].x() < extents.minX ) {
1822 extents.minX = pts[i].x();
1824 if ( pts[i].x() > extents.maxX ) {
1825 extents.maxX = pts[i].x();
1827 if ( pts[i].y() < extents.minY ) {
1828 extents.minY = pts[i].y();
1830 if ( pts[i].y() > extents.maxY ) {
1831 extents.maxY = pts[i].y();
1835 // Do some viewport fitting stuff...
1837 //globalOutputStream() << "--> Center: " << center.x() << ", " << center.y() << "\n";
1838 //globalOutputStream() << "--> Extents (stage 1): " << extents.minX << ", "
1839 // << extents.maxX << ", " << extents.minY << ", " << extents.maxY << "\n";
1840 // TTimo: Apply a ratio to get the area we'll draw.
1841 center.x() = 0.5f * ( extents.minX + extents.maxX ),
1842 center.y() = 0.5f * ( extents.minY + extents.maxY );
1843 extents.minX = center.x() + VP_PADDING * ( extents.minX - center.x() ),
1844 extents.minY = center.y() + VP_PADDING * ( extents.minY - center.y() ),
1845 extents.maxX = center.x() + VP_PADDING * ( extents.maxX - center.x() ),
1846 extents.maxY = center.y() + VP_PADDING * ( extents.maxY - center.y() );
1847 //globalOutputStream() << "--> Extents (stage 2): " << extents.minX << ", "
1848 // << extents.maxX << ", " << extents.minY << ", " << extents.maxY << "\n";
1850 // TTimo: We want a texture with the same X / Y ratio.
1851 // TTimo: Compute XY space / window size ratio.
1852 float SSize = extents.width(), TSize = extents.height();
1853 float ratioX = textureSize.x() * extents.width() / windowSize.x(),
1854 ratioY = textureSize.y() * extents.height() / windowSize.y();
1855 //globalOutputStream() << "--> Texture size: " << textureSize.x() << ", " << textureSize.y() << "\n";
1856 //globalOutputStream() << "--> Window size: " << windowSize.x() << ", " << windowSize.y() << "\n";
1858 if ( ratioX > ratioY ) {
1859 TSize = ( windowSize.y() * ratioX ) / textureSize.y();
1860 // TSize = extents.width() * (windowSize.y() / windowSize.x()) * (textureSize.x() / textureSize.y());
1864 SSize = ( windowSize.x() * ratioY ) / textureSize.x();
1865 // SSize = extents.height() * (windowSize.x() / windowSize.y()) * (textureSize.y() / textureSize.x());
1868 extents.minX = center.x() - 0.5f * SSize, extents.maxX = center.x() + 0.5f * SSize,
1869 extents.minY = center.y() - 0.5f * TSize, extents.maxY = center.y() + 0.5f * TSize;
1870 //globalOutputStream() << "--> Extents (stage 3): " << extents.minX << ", "
1871 // << extents.maxX << ", " << extents.minY << ", " << extents.maxY << "\n";
1874 gboolean size_allocate( ui::Widget win, GtkAllocation * a, gpointer ){
1875 windowSize.x() = a->width;
1876 windowSize.y() = a->height;
1881 gboolean expose( ui::Widget win, GdkEventExpose * e, gpointer ){
1882 // globalOutputStream() << "--> Textool Window was exposed!\n";
1883 // globalOutputStream() << " (window width/height: " << cc << "/" << e->area.height << ")\n";
1885 // windowSize.x() = e->area.width, windowSize.y() = e->area.height;
1886 //This needs to go elsewhere...
1889 if ( glwidget_make_current( win ) == FALSE ) {
1890 globalOutputStream() << " FAILED to make current! Oh, the agony! :-(\n";
1894 CopyPointsFromSelectedFace();
1896 if ( !lButtonDown ) {
1900 // Probably should init button/anchor states here as well...
1901 // rotationAngle = 0.0f;
1902 glClearColor( 0, 0, 0, 0 );
1903 glViewport( 0, 0, e->area.width, e->area.height );
1904 glMatrixMode( GL_PROJECTION );
1908 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1909 glDisable( GL_DEPTH_TEST );
1910 glDisable( GL_BLEND );
1912 glOrtho( extents.minX, extents.maxX, extents.maxY, extents.minY, -1, 1 );
1914 glColor3f( 1, 1, 1 );
1915 // draw the texture background
1916 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1917 glBindTexture( GL_TEXTURE_2D, textureNum );
1919 glEnable( GL_TEXTURE_2D );
1920 glBegin( GL_QUADS );
1921 glTexCoord2f( extents.minX, extents.minY );
1922 glVertex2f( extents.minX, extents.minY );
1923 glTexCoord2f( extents.maxX, extents.minY );
1924 glVertex2f( extents.maxX, extents.minY );
1925 glTexCoord2f( extents.maxX, extents.maxY );
1926 glVertex2f( extents.maxX, extents.maxY );
1927 glTexCoord2f( extents.minX, extents.maxY );
1928 glVertex2f( extents.minX, extents.maxY );
1930 glDisable( GL_TEXTURE_2D );
1932 // draw the texture-space grid
1933 glColor3fv( widgetColor[COLOR_GREY] );
1934 glBegin( GL_LINES );
1936 const int gridSubdivisions = 8;
1937 const float gridExtents = 4.0f;
1939 for ( int i = 0; i < gridSubdivisions + 1; ++i )
1941 float y = i * ( gridExtents / float(gridSubdivisions) );
1942 float x = i * ( gridExtents / float(gridSubdivisions) );
1944 glVertex2f( gridExtents, y );
1946 glVertex2f( x, gridExtents );
1951 DrawControlPoints();
1952 DrawControlWidgets();
1954 // reset the current texture
1955 // glBindTexture(GL_TEXTURE_2D, 0);
1957 glwidget_swap_buffers( win );
1962 /*int FindSelectedPoint(int x, int y)
1964 for(int i=0; i<numPts; i++)
1966 int nx = (int)(windowSize.x() * (pts[i].x() - extents.minX) / extents.width());
1967 int ny = (int)(windowSize.y() * (pts[i].y() - extents.minY) / extents.height());
1969 if (abs(nx - x) <= 3 && abs(ny - y) <= 3)
1978 Vector2 dragPoint; // Defined in terms of window space (+x/-y)
1980 gboolean button_press( ui::Widget win, GdkEventButton * e, gpointer ){
1981 // globalOutputStream() << "--> Textool button press...\n";
1983 if ( e->button == 1 ) {
1985 GlobalUndoSystem().start();
1989 //globalOutputStream() << "--> Original BP: [" << origBP.coords[0][0] << "][" << origBP.coords[0][1] << "][" << origBP.coords[0][2] << "]\n";
1990 //globalOutputStream() << " [" << origBP.coords[1][0] << "][" << origBP.coords[1][1] << "][" << origBP.coords[1][2] << "]\n";
1991 //float angle = atan2(origBP.coords[0][1], origBP.coords[0][0]) * 180.0f / 3.141592653589f;
1992 origAngle = ( origBP.coords[0][1] > 0 ? PI : -PI ); // Could also be -PI... !!! FIX !!! [DONE]
1994 if ( origBP.coords[0][0] != 0.0f ) {
1995 origAngle = atan( origBP.coords[0][1] / origBP.coords[0][0] );
1998 origScaleX = origBP.coords[0][0] / cos( origAngle );
1999 origScaleY = origBP.coords[1][1] / cos( origAngle );
2000 rotationAngle = origAngle;
2001 oldCenter[0] = oldCenter[1] = 0;
2003 //globalOutputStream() << "--> BP stats: ang=" << origAngle * RAD_TO_DEG << ", scale=" << origScaleX << "/" << origScaleY << "\n";
2004 //Should also set the Flip X/Y checkboxes here as well... !!! FIX !!!
2005 //Also: should reverse texture left/right up/down instead of flipping the points...
2008 //float nx = windowSize.x() * (e->x - extents.minX) / (extents.maxX - extents.minX);
2009 //float ny = windowSize.y() * (e->y - extents.minY) / (extents.maxY - extents.minY);
2011 //But I want it to scroll the texture window, not the points... !!! FIX !!!
2012 //Actually, should scroll the texture window only when mouse is down on no widgets...
2013 float nx = e->x / windowSize.x() * extents.width() + extents.minX;
2014 float ny = e->y / windowSize.y() * extents.height() + extents.minY;
2015 trans.x() = -tm.coords[0][0] * nx - tm.coords[0][1] * ny;
2016 trans.y() = -tm.coords[1][0] * nx - tm.coords[1][1] * ny;
2018 dragPoint.x() = e->x, dragPoint.y() = e->y;
2019 trans2.x() = nx, trans2.y() = ny;
2020 oldRotationAngle = rotationAngle;
2021 // oldTrans.x() = tm.coords[0][2] - nx * textureSize.x();
2022 // oldTrans.y() = tm.coords[1][2] - ny * textureSize.y();
2023 oldTrans.x() = tm.coords[0][2];
2024 oldTrans.y() = tm.coords[1][2];
2025 oldCenter.x() = center.x();
2026 oldCenter.y() = center.y();
2032 /* else if (e->button == 3)
2037 //globalOutputStream() << "(" << (haveAnchor ? "anchor" : "released") << ")\n";
2042 gboolean button_release( ui::Widget win, GdkEventButton * e, gpointer ){
2043 // globalOutputStream() << "--> Textool button release...\n";
2045 if ( e->button == 1 ) {
2046 /* float ptx = e->x / windowSize.x() * extents.width() + extents.minX;
2047 float pty = e->y / windowSize.y() * extents.height() + extents.minY;
2049 //This prolly should go into the mouse move code...
2050 //Doesn't work correctly anyway...
2051 if (translatingX || translatingY)
2052 center.x() = ptx, center.y() = pty;//*/
2054 lButtonDown = false;
2056 if ( translatingX || translatingY ) {
2057 GlobalUndoSystem().finish( "translateTexture" );
2059 else if ( rotating ) {
2060 GlobalUndoSystem().finish( "rotateTexture" );
2062 else if ( scalingX || scalingY ) {
2063 GlobalUndoSystem().finish( "scaleTexture" );
2065 else if ( resizingX || resizingY ) {
2066 GlobalUndoSystem().finish( "resizeTexture" );
2070 GlobalUndoSystem().finish( "textoolUnknown" );
2073 rotating = translatingX = translatingY = scalingX = scalingY
2074 = resizingX = resizingY = false;
2078 else if ( e->button == 3 ) {
2079 rButtonDown = false;
2086 void C2DView::GridForWindow( float c[2], int x, int y)
2088 SpaceForWindow( c, x, y );
2091 c[0] /= m_GridStep[0];
2092 c[1] /= m_GridStep[1];
2093 c[0] = (float)floor( c[0] + 0.5f );
2094 c[1] = (float)floor( c[1] + 0.5f );
2095 c[0] *= m_GridStep[0];
2096 c[1] *= m_GridStep[1];
2098 void C2DView::SpaceForWindow( float c[2], int x, int y)
2100 c[0] = ((float)(x))/((float)(m_rect.right-m_rect.left))*(m_Maxs[0]-m_Mins[0])+m_Mins[0];
2101 c[1] = ((float)(y))/((float)(m_rect.bottom-m_rect.top))*(m_Maxs[1]-m_Mins[1])+m_Mins[1];
2104 gboolean motion( ui::Widget win, GdkEventMotion * e, gpointer ){
2105 // globalOutputStream() << "--> Textool motion...\n";
2107 if ( lButtonDown ) {
2108 if ( translatingX || translatingY ) {
2109 float ptx = e->x / windowSize.x() * extents.width() + extents.minX;
2110 float pty = e->y / windowSize.y() * extents.height() + extents.minY;
2112 //Need to fix this to take the rotation angle into account, so that it moves along
2113 //the rotated X/Y axis...
2114 if ( translatingX ) {
2115 // tm.coords[0][2] = (trans.x() + ptx) * textureSize.x();
2116 //This works, but only when the angle is zero. !!! FIX !!! [DONE]
2117 // tm.coords[0][2] = oldCenter.x() + (ptx * textureSize.x());
2118 tm.coords[0][2] = oldTrans.x() + ( ptx - trans2.x() ) * textureSize.x();
2119 // center.x() = oldCenter.x() + (ptx - trans2.x());
2122 if ( translatingY ) {
2123 // tm.coords[1][2] = (trans.y() + pty) * textureSize.y();
2124 // tm.coords[1][2] = oldCenter.y() + (pty * textureSize.y());
2125 tm.coords[1][2] = oldTrans.y() + ( pty - trans2.y() ) * textureSize.y();
2126 // center.y() = oldCenter.y() + (pty - trans2.y());
2129 //Need to update center.x/y() so that the widget translates as well. Also, oldCenter
2130 //is badly named... Should be oldTrans or something like that... !!! FIX !!!
2131 //Changing center.x/y() here doesn't seem to change anything... :-/
2132 UpdateControlPoints();
2134 else if ( rotating ) {
2135 // Shamus: New rotate code
2136 int cx = (int)( windowSize.x() * ( center.x() - extents.minX ) / extents.width() );
2137 int cy = (int)( windowSize.y() * ( center.y() - extents.minY ) / extents.height() );
2138 Vector3 v1( dragPoint.x() - cx, dragPoint.y() - cy, 0 ), v2( e->x - cx, e->y - cy, 0 );
2140 vector3_normalise( v1 );
2141 vector3_normalise( v2 );
2142 float c = vector3_dot( v1, v2 );
2143 Vector3 cross = vector3_cross( v1, v2 );
2144 float s = vector3_length( cross );
2146 if ( cross[2] > 0 ) {
2150 // Problem with this: arcsin/cos seems to only return -90 to 90 and 0 to 180...
2151 // Can't derive angle from that!
2153 //rotationAngle = asin(s);// * 180.0f / 3.141592653589f;
2154 rotationAngle = acos( c );
2155 //rotationAngle2 = asin(s);
2156 if ( cross[2] < 0 ) {
2157 rotationAngle = -rotationAngle;
2160 //NO! DOESN'T WORK! rotationAngle -= 45.0f * DEG_TO_RAD;
2163 /*c = cos(rotationAngle - oldRotationAngle);
2164 s = sin(rotationAngle - oldRotationAngle);
2165 rotationAngle += oldRotationAngle;
2166 //c += cos(oldRotationAngle);
2167 //s += sin(oldRotationAngle);
2168 //rotationAngle += oldRotationAngle;
2171 //rotationAngle %= 2.0 * PI;//*/
2173 //This is wrong... Hmm...
2174 //It seems to shear the texture instead of rotating it... !!! FIX !!!
2175 // Now it rotates correctly. Seems TTimo was overcomplicating things here... ;-)
2177 // Seems like what needs to happen here is multiplying these rotations by tm... !!! FIX !!!
2179 // See brush_primit.cpp line 244 (Texdef_EmitTextureCoordinates()) for where texcoords come from...
2181 tm.coords[0][0] = c;
2182 tm.coords[0][1] = s;
2183 tm.coords[1][0] = -s;
2184 tm.coords[1][1] = c;
2185 //It doesn't work anymore... Dunno why...
2186 //tm.coords[0][2] = -trans.x(); // This works!!! Yeah!!!
2187 //tm.coords[1][2] = -trans.y();
2189 //tm.coords[0][2] = rotationPoint.x(); // This works, but strangely...
2190 //tm.coords[1][2] = rotationPoint.y();
2191 //tm.coords[0][2] = 0;// center.x() / 2.0f;
2192 //tm.coords[1][2] = 0;// center.y() / 2.0f;
2194 //tm.coords[0][2] = -(center.x() * textureSize.x());
2195 //tm.coords[1][2] = -(center.y() * textureSize.y());
2196 //Eh? No, but seems to be getting closer...
2197 /*float ptx = e->x / windowSize.x() * extents.width() + extents.minX;
2198 float pty = e->y / windowSize.y() * extents.height() + extents.minY;
2199 tm.coords[0][2] = -c * center.x() - s * center.y() + ptx;
2200 tm.coords[1][2] = s * center.x() - c * center.x() + pty;//*/
2201 //Kinda works, but center drifts around on non-square textures...
2202 /*tm.coords[0][2] = (-c * center.x() - s * center.y()) * textureSize.x();
2203 tm.coords[1][2] = ( s * center.x() - c * center.y()) * textureSize.y();//*/
2204 //Rotates correctly, but not around the actual center of the face's points...
2205 /*tm.coords[0][2] = -c * center.x() * textureSize.x() - s * center.y() * textureSize.y();
2206 tm.coords[1][2] = s * center.x() * textureSize.x() - c * center.y() * textureSize.y();//*/
2208 tm.coords[0][2] = ( -c * center.x() * textureSize.x() - s * center.y() * textureSize.y() ) + center.x() * textureSize.x();
2209 tm.coords[1][2] = ( s * center.x() * textureSize.x() - c * center.y() * textureSize.y() ) + center.y() * textureSize.y(); //*/
2210 //This doesn't work...
2211 //And this is the wrong place for this anyway (I'm pretty sure).
2212 /*tm.coords[0][2] += oldCenter.x();
2213 tm.coords[1][2] += oldCenter.y();//*/
2214 UpdateControlPoints(); // will cause a redraw
2219 else // Check for widget mouseovers
2222 float nx = e->x / windowSize.x() * extents.width() + extents.minX;
2223 float ny = e->y / windowSize.y() * extents.height() + extents.minY;
2224 // Translate nx/y to the "center" point...
2227 ny = -ny; // Flip Y-axis so that increasing numbers move up
2229 tran.x() = tm.coords[0][0] * nx + tm.coords[0][1] * ny;
2230 tran.y() = tm.coords[1][0] * nx + tm.coords[1][1] * ny;
2231 //This doesn't seem to generate a valid distance from the center--for some reason it
2232 //calculates a fixed number every time
2233 //Look at nx/y above: they're getting fixed there! !!! FIX !!! [DONE]
2234 float dist = sqrt( ( nx * nx ) + ( ny * ny ) );
2235 // Normalize to the 2.0 = height standard (for now)
2236 //globalOutputStream() << "--> Distance before: " << dist;
2237 dist = dist * 2.0f / extents.height();
2238 //globalOutputStream() << ". After: " << dist;
2239 tran.x() = tran.x() * 2.0f / extents.height();
2240 tran.y() = tran.y() * 2.0f / extents.height();
2241 //globalOutputStream() << ". Trans: " << tran.x() << ", " << tran.y() << "\n";
2243 //Let's try this instead...
2244 //Interesting! It seems that e->x/y are rotated
2245 //(no, they're not--the TM above is what's doing it...)
2246 nx = ( ( e->x / windowSize.y() ) * 2.0f ) - ( windowSize.x() / windowSize.y() );
2247 ny = ( ( e->y / windowSize.y() ) * 2.0f ) - ( windowSize.y() / windowSize.y() );
2249 //Cool! It works! Now just need to do rotation...
2251 rotating = translatingX = translatingY = scalingX = scalingY
2252 = resizingX = resizingY = false;
2254 if ( dist < ( gridRadius * 0.16f ) ) {
2255 translatingX = translatingY = true;
2257 else if ( dist > ( gridRadius * 0.16f ) && dist < ( gridRadius * 1.10f )
2258 && fabs( ny ) < ( gridRadius * 0.05f ) && nx > 0 ) {
2259 translatingX = true;
2261 else if ( dist > ( gridRadius * 0.16f ) && dist < ( gridRadius * 1.10f )
2262 && fabs( nx ) < ( gridRadius * 0.05f ) && ny > 0 ) {
2263 translatingY = true;
2265 // Should tighten up the angle on this, or put this test after the axis tests...
2266 else if ( tran.x() > 0 && tran.y() > 0
2267 && ( dist > ( gridRadius * 0.82f ) && dist < ( gridRadius * 0.98f ) ) ) {
2279 //It seems the fake tex coords conversion is screwing this stuff up... !!! FIX !!!
2280 //This is still wrong... Prolly need to do something with the oldScaleX/Y stuff...
2281 void flipX( GtkToggleButton *, gpointer ){
2282 // globalOutputStream() << "--> Flip X...\n";
2284 // SurfaceInspector_GetSelectedBPTexdef(); // Refresh g_selectedBrushPrimitTexdef...
2285 // tm.coords[0][0] = -tm.coords[0][0];
2286 // tm.coords[1][0] = -tm.coords[1][0];
2287 // tm.coords[0][0] = -tm.coords[0][0]; // This should be correct now...Nope.
2288 // tm.coords[1][1] = -tm.coords[1][1];
2289 tm.coords[0][0] = -tm.coords[0][0]; // This should be correct now...
2290 tm.coords[1][0] = -tm.coords[1][0];
2291 // tm.coords[2][0] = -tm.coords[2][0];//wil wok? no.
2292 UpdateControlPoints();
2295 void flipY( GtkToggleButton *, gpointer ){
2296 // globalOutputStream() << "--> Flip Y...\n";
2297 // tm.coords[0][1] = -tm.coords[0][1];
2298 // tm.coords[1][1] = -tm.coords[1][1];
2299 // tm.coords[0][1] = -tm.coords[0][1]; // This should be correct now...Nope.
2300 // tm.coords[1][0] = -tm.coords[1][0];
2301 tm.coords[0][1] = -tm.coords[0][1]; // This should be correct now...
2302 tm.coords[1][1] = -tm.coords[1][1];
2303 // tm.coords[2][1] = -tm.coords[2][1];//wil wok? no.
2304 UpdateControlPoints();
2307 } // end namespace TexTool