GTK: wrap gtk_widget_set_size_request
[xonotic/netradiant.git] / radiant / patchdialog.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 //
23 // Patch Dialog
24 //
25 // Leonardo Zide (leo@lokigames.com)
26 //
27
28 #include "patchdialog.h"
29
30 #include <gtk/gtk.h>
31
32 #include "itexdef.h"
33
34 #include "debugging/debugging.h"
35
36 #include "gtkutil/idledraw.h"
37 #include "gtkutil/entry.h"
38 #include "gtkutil/button.h"
39 #include "gtkutil/nonmodal.h"
40 #include "dialog.h"
41 #include "gtkdlgs.h"
42 #include "mainframe.h"
43 #include "patchmanip.h"
44 #include "patch.h"
45 #include "commands.h"
46 #include "preferences.h"
47 #include "signal/isignal.h"
48
49
50 #include <gdk/gdkkeysyms.h>
51
52 // the increment we are using for the patch inspector (this is saved in the prefs)
53 struct pi_globals_t
54 {
55         float shift[2];
56         float scale[2];
57         float rotate;
58
59         pi_globals_t(){
60                 shift[0] = 8.0f;
61                 shift[1] = 8.0f;
62                 scale[0] = 0.5f;
63                 scale[1] = 0.5f;
64                 rotate = 45.0f;
65         }
66 };
67
68 pi_globals_t g_pi_globals;
69
70 class PatchFixedSubdivisions
71 {
72 public:
73 PatchFixedSubdivisions() : m_enabled( false ), m_x( 0 ), m_y( 0 ){
74 }
75 PatchFixedSubdivisions( bool enabled, std::size_t x, std::size_t y ) : m_enabled( enabled ), m_x( x ), m_y( y ){
76 }
77 bool m_enabled;
78 std::size_t m_x;
79 std::size_t m_y;
80 };
81
82 void Patch_getFixedSubdivisions( const Patch& patch, PatchFixedSubdivisions& subdivisions ){
83         subdivisions.m_enabled = patch.m_patchDef3;
84         subdivisions.m_x = patch.m_subdivisions_x;
85         subdivisions.m_y = patch.m_subdivisions_y;
86 }
87
88 const std::size_t MAX_PATCH_SUBDIVISIONS = 32;
89
90 void Patch_setFixedSubdivisions( Patch& patch, const PatchFixedSubdivisions& subdivisions ){
91         patch.undoSave();
92
93         patch.m_patchDef3 = subdivisions.m_enabled;
94         patch.m_subdivisions_x = subdivisions.m_x;
95         patch.m_subdivisions_y = subdivisions.m_y;
96
97         if ( patch.m_subdivisions_x == 0 ) {
98                 patch.m_subdivisions_x = 4;
99         }
100         else if ( patch.m_subdivisions_x > MAX_PATCH_SUBDIVISIONS ) {
101                 patch.m_subdivisions_x = MAX_PATCH_SUBDIVISIONS;
102         }
103         if ( patch.m_subdivisions_y == 0 ) {
104                 patch.m_subdivisions_y = 4;
105         }
106         else if ( patch.m_subdivisions_y > MAX_PATCH_SUBDIVISIONS ) {
107                 patch.m_subdivisions_y = MAX_PATCH_SUBDIVISIONS;
108         }
109
110         SceneChangeNotify();
111         Patch_textureChanged();
112         patch.controlPointsChanged();
113 }
114
115 class PatchGetFixedSubdivisions
116 {
117 PatchFixedSubdivisions& m_subdivisions;
118 public:
119 PatchGetFixedSubdivisions( PatchFixedSubdivisions& subdivisions ) : m_subdivisions( subdivisions ){
120 }
121 void operator()( Patch& patch ){
122         Patch_getFixedSubdivisions( patch, m_subdivisions );
123         SceneChangeNotify();
124 }
125 };
126
127 void Scene_PatchGetFixedSubdivisions( PatchFixedSubdivisions& subdivisions ){
128 #if 1
129         if ( GlobalSelectionSystem().countSelected() != 0 ) {
130                 Patch* patch = Node_getPatch( GlobalSelectionSystem().ultimateSelected().path().top() );
131                 if ( patch != 0 ) {
132                         Patch_getFixedSubdivisions( *patch, subdivisions );
133                 }
134         }
135 #else
136         Scene_forEachVisibleSelectedPatch( PatchGetFixedSubdivisions( subdivisions ) );
137 #endif
138 }
139
140 class PatchSetFixedSubdivisions
141 {
142 const PatchFixedSubdivisions& m_subdivisions;
143 public:
144 PatchSetFixedSubdivisions( const PatchFixedSubdivisions& subdivisions ) : m_subdivisions( subdivisions ){
145 }
146 void operator()( Patch& patch ) const {
147         Patch_setFixedSubdivisions( patch, m_subdivisions );
148 }
149 };
150
151 void Scene_PatchSetFixedSubdivisions( const PatchFixedSubdivisions& subdivisions ){
152         UndoableCommand command( "patchSetFixedSubdivisions" );
153         Scene_forEachVisibleSelectedPatch( PatchSetFixedSubdivisions( subdivisions ) );
154 }
155
156
157 class Subdivisions
158 {
159 public:
160 ui::CheckButton m_enabled;
161 ui::Entry m_horizontal;
162 ui::Entry m_vertical;
163 Subdivisions() : m_enabled( (GtkCheckButton *) 0 ), m_horizontal( ui::null ), m_vertical( ui::null ){
164 }
165 void update(){
166         PatchFixedSubdivisions subdivisions;
167         Scene_PatchGetFixedSubdivisions( subdivisions );
168
169         toggle_button_set_active_no_signal( m_enabled, subdivisions.m_enabled );
170
171         if ( subdivisions.m_enabled ) {
172                 entry_set_int( m_horizontal, static_cast<int>( subdivisions.m_x ) );
173                 entry_set_int( m_vertical, static_cast<int>( subdivisions.m_y ) );
174                 gtk_widget_set_sensitive( m_horizontal , TRUE );
175                 gtk_widget_set_sensitive( m_vertical , TRUE );
176         }
177         else
178         {
179                 m_horizontal.text("");
180                 m_vertical.text("");
181                 gtk_widget_set_sensitive( m_horizontal , FALSE );
182                 gtk_widget_set_sensitive( m_vertical , FALSE );
183         }
184 }
185 void cancel(){
186         update();
187 }
188 typedef MemberCaller<Subdivisions, &Subdivisions::cancel> CancelCaller;
189 void apply(){
190         Scene_PatchSetFixedSubdivisions(
191                 PatchFixedSubdivisions(
192                         gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( m_enabled ) ) != FALSE,
193                         static_cast<std::size_t>( entry_get_int( m_horizontal ) ),
194                         static_cast<std::size_t>( entry_get_int( m_vertical ) )
195                         )
196                 );
197 }
198 typedef MemberCaller<Subdivisions, &Subdivisions::apply> ApplyCaller;
199 static void applyGtk( GtkToggleButton* toggle, Subdivisions* self ){
200         self->apply();
201 }
202 };
203
204 class PatchInspector : public Dialog
205 {
206 ui::Window BuildDialog();
207 Subdivisions m_subdivisions;
208 NonModalEntry m_horizontalSubdivisionsEntry;
209 NonModalEntry m_verticalSubdivisionsEntry;
210 public:
211 IdleDraw m_idleDraw;
212 WindowPositionTracker m_position_tracker;
213
214 Patch *m_Patch;
215
216 CopiedString m_strName;
217 float m_fS;
218 float m_fT;
219 float m_fX;
220 float m_fY;
221 float m_fZ;
222 /*  float       m_fHScale;
223    float        m_fHShift;
224    float        m_fRotate;
225    float        m_fVScale;
226    float        m_fVShift; */
227 int m_nCol;
228 int m_nRow;
229 ui::ComboBoxText m_pRowCombo{ui::null};
230 ui::ComboBoxText m_pColCombo{ui::null};
231 std::size_t m_countRows;
232 std::size_t m_countCols;
233
234 // turn on/off processing of the "changed" "value_changed" messages
235 // (need to turn off when we are feeding data in)
236 // NOTE: much more simple than blocking signals
237 bool m_bListenChanged;
238
239 PatchInspector() :
240         m_horizontalSubdivisionsEntry( Subdivisions::ApplyCaller( m_subdivisions ), Subdivisions::CancelCaller( m_subdivisions ) ),
241         m_verticalSubdivisionsEntry( Subdivisions::ApplyCaller( m_subdivisions ), Subdivisions::CancelCaller( m_subdivisions ) ),
242         m_idleDraw( MemberCaller<PatchInspector, &PatchInspector::GetPatchInfo>( *this ) ){
243         m_fS = 0.0f;
244         m_fT = 0.0f;
245         m_fX = 0.0f;
246         m_fY = 0.0f;
247         m_fZ = 0.0f;
248         m_nCol = 0;
249         m_nRow = 0;
250         m_countRows = 0;
251         m_countCols = 0;
252         m_Patch = 0;
253         m_bListenChanged = true;
254
255         m_position_tracker.setPosition( c_default_window_pos );
256 }
257
258 bool visible(){
259         return GetWidget().visible();
260 }
261
262 //  void UpdateInfo();
263 //  void SetPatchInfo();
264 void GetPatchInfo();
265 void UpdateSpinners( bool bUp, int nID );
266 // read the current patch on map and initialize m_fX m_fY accordingly
267 void UpdateRowColInfo();
268 // sync the dialog our internal data structures
269 // depending on the flag it will read or write
270 // we use m_nCol m_nRow m_fX m_fY m_fZ m_fS m_fT m_strName
271 // (NOTE: this doesn't actually commit stuff to the map or read from it)
272 void importData();
273 void exportData();
274 };
275
276 PatchInspector g_PatchInspector;
277
278 void PatchInspector_constructWindow( ui::Window main_window ){
279         g_PatchInspector.m_parent = main_window;
280         g_PatchInspector.Create();
281 }
282 void PatchInspector_destroyWindow(){
283         g_PatchInspector.Destroy();
284 }
285
286 void PatchInspector_queueDraw(){
287         if ( g_PatchInspector.visible() ) {
288                 g_PatchInspector.m_idleDraw.queueDraw();
289         }
290 }
291
292 void DoPatchInspector(){
293         g_PatchInspector.GetPatchInfo();
294         if ( !g_PatchInspector.visible() ) {
295                 g_PatchInspector.ShowDlg();
296         }
297 }
298
299 void PatchInspector_toggleShown(){
300         if ( g_PatchInspector.visible() ) {
301                 g_PatchInspector.m_Patch = 0;
302                 g_PatchInspector.HideDlg();
303         }
304         else{
305                 DoPatchInspector();
306         }
307 }
308
309
310 // =============================================================================
311 // static functions
312
313 // memorize the current state (that is don't try to undo our do before changing something else)
314 static void OnApply( ui::Widget widget, gpointer data ){
315         g_PatchInspector.exportData();
316         if ( g_PatchInspector.m_Patch != 0 ) {
317                 UndoableCommand command( "patchSetTexture" );
318                 g_PatchInspector.m_Patch->undoSave();
319
320                 if ( !texdef_name_valid( g_PatchInspector.m_strName.c_str() ) ) {
321                         globalErrorStream() << "invalid texture name '" << g_PatchInspector.m_strName.c_str() << "'\n";
322                         g_PatchInspector.m_strName = texdef_name_default();
323                 }
324                 g_PatchInspector.m_Patch->SetShader( g_PatchInspector.m_strName.c_str() );
325
326                 std::size_t r = g_PatchInspector.m_nRow;
327                 std::size_t c = g_PatchInspector.m_nCol;
328                 if ( r < g_PatchInspector.m_Patch->getHeight()
329                          && c < g_PatchInspector.m_Patch->getWidth() ) {
330                         PatchControl& p = g_PatchInspector.m_Patch->ctrlAt( r,c );
331                         p.m_vertex[0] = g_PatchInspector.m_fX;
332                         p.m_vertex[1] = g_PatchInspector.m_fY;
333                         p.m_vertex[2] = g_PatchInspector.m_fZ;
334                         p.m_texcoord[0] = g_PatchInspector.m_fS;
335                         p.m_texcoord[1] = g_PatchInspector.m_fT;
336                         g_PatchInspector.m_Patch->controlPointsChanged();
337                 }
338         }
339 }
340
341 static void OnSelchangeComboColRow( ui::Widget widget, gpointer data ){
342         if ( !g_PatchInspector.m_bListenChanged ) {
343                 return;
344         }
345         // retrieve the current m_nRow and m_nCol, other params are not relevant
346         g_PatchInspector.exportData();
347         // read the changed values ourselves
348         g_PatchInspector.UpdateRowColInfo();
349         // now reflect our changes
350         g_PatchInspector.importData();
351 }
352
353 class PatchSetTextureRepeat
354 {
355 float m_s, m_t;
356 public:
357 PatchSetTextureRepeat( float s, float t ) : m_s( s ), m_t( t ){
358 }
359 void operator()( Patch& patch ) const {
360         patch.SetTextureRepeat( m_s, m_t );
361 }
362 };
363
364 void Scene_PatchTileTexture_Selected( scene::Graph& graph, float s, float t ){
365         Scene_forEachVisibleSelectedPatch( PatchSetTextureRepeat( s, t ) );
366         SceneChangeNotify();
367 }
368
369 static void OnBtnPatchdetails( ui::Widget widget, gpointer data ){
370         Patch_CapTexture();
371 }
372
373 static void OnBtnPatchfit( ui::Widget widget, gpointer data ){
374         Patch_FitTexture();
375 }
376
377 static void OnBtnPatchnatural( ui::Widget widget, gpointer data ){
378         Patch_NaturalTexture();
379 }
380
381 static void OnBtnPatchreset( ui::Widget widget, gpointer data ){
382         Patch_ResetTexture();
383 }
384
385 static void OnBtnPatchFlipX( ui::Widget widget, gpointer data ){
386         Patch_FlipTextureX();
387 }
388
389 static void OnBtnPatchFlipY( ui::Widget widget, gpointer data ){
390         Patch_FlipTextureY();
391 }
392
393 struct PatchRotateTexture
394 {
395         float m_angle;
396 public:
397         PatchRotateTexture( float angle ) : m_angle( angle ){
398         }
399         void operator()( Patch& patch ) const {
400                 patch.RotateTexture( m_angle );
401         }
402 };
403
404 void Scene_PatchRotateTexture_Selected( scene::Graph& graph, float angle ){
405         Scene_forEachVisibleSelectedPatch( PatchRotateTexture( angle ) );
406 }
407
408 class PatchScaleTexture
409 {
410 float m_s, m_t;
411 public:
412 PatchScaleTexture( float s, float t ) : m_s( s ), m_t( t ){
413 }
414 void operator()( Patch& patch ) const {
415         patch.ScaleTexture( m_s, m_t );
416 }
417 };
418
419 float Patch_convertScale( float scale ){
420         if ( scale > 0 ) {
421                 return scale;
422         }
423         if ( scale < 0 ) {
424                 return -1 / scale;
425         }
426         return 1;
427 }
428
429 void Scene_PatchScaleTexture_Selected( scene::Graph& graph, float s, float t ){
430         Scene_forEachVisibleSelectedPatch( PatchScaleTexture( Patch_convertScale( s ), Patch_convertScale( t ) ) );
431 }
432
433 class PatchTranslateTexture
434 {
435 float m_s, m_t;
436 public:
437 PatchTranslateTexture( float s, float t )
438         : m_s( s ), m_t( t ){
439 }
440 void operator()( Patch& patch ) const {
441         patch.TranslateTexture( m_s, m_t );
442 }
443 };
444
445 void Scene_PatchTranslateTexture_Selected( scene::Graph& graph, float s, float t ){
446         Scene_forEachVisibleSelectedPatch( PatchTranslateTexture( s, t ) );
447 }
448
449 static void OnBtnPatchAutoCap( ui::Widget widget, gpointer data ){
450         Patch_AutoCapTexture();
451 }
452
453 static void OnSpinChanged( GtkAdjustment *adj, gpointer data ){
454         texdef_t td;
455
456         td.rotate = 0;
457         td.scale[0] = td.scale[1] = 0;
458         td.shift[0] = td.shift[1] = 0;
459
460         if ( gtk_adjustment_get_value(adj) == 0 ) {
461                 return;
462         }
463
464         if ( adj == g_object_get_data( G_OBJECT( g_PatchInspector.GetWidget() ), "hshift_adj" ) ) {
465                 g_pi_globals.shift[0] = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( data ) ) ) );
466
467                 if ( gtk_adjustment_get_value(adj) > 0 ) {
468                         td.shift[0] = g_pi_globals.shift[0];
469                 }
470                 else{
471                         td.shift[0] = -g_pi_globals.shift[0];
472                 }
473         }
474         else if ( adj == g_object_get_data( G_OBJECT( g_PatchInspector.GetWidget() ), "vshift_adj" ) ) {
475                 g_pi_globals.shift[1] = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( data ) ) ) );
476
477                 if ( gtk_adjustment_get_value(adj) > 0 ) {
478                         td.shift[1] = g_pi_globals.shift[1];
479                 }
480                 else{
481                         td.shift[1] = -g_pi_globals.shift[1];
482                 }
483         }
484         else if ( adj == g_object_get_data( G_OBJECT( g_PatchInspector.GetWidget() ), "hscale_adj" ) ) {
485                 g_pi_globals.scale[0] = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( data ) ) ) );
486                 if ( g_pi_globals.scale[0] == 0.0f ) {
487                         return;
488                 }
489                 if ( gtk_adjustment_get_value(adj) > 0 ) {
490                         td.scale[0] = g_pi_globals.scale[0];
491                 }
492                 else{
493                         td.scale[0] = -g_pi_globals.scale[0];
494                 }
495         }
496         else if ( adj == g_object_get_data( G_OBJECT( g_PatchInspector.GetWidget() ), "vscale_adj" ) ) {
497                 g_pi_globals.scale[1] = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( data ) ) ) );
498                 if ( g_pi_globals.scale[1] == 0.0f ) {
499                         return;
500                 }
501                 if ( gtk_adjustment_get_value(adj) > 0 ) {
502                         td.scale[1] = g_pi_globals.scale[1];
503                 }
504                 else{
505                         td.scale[1] = -g_pi_globals.scale[1];
506                 }
507         }
508         else if ( adj == g_object_get_data( G_OBJECT( g_PatchInspector.GetWidget() ), "rotate_adj" ) ) {
509                 g_pi_globals.rotate = static_cast<float>( atof( gtk_entry_get_text( GTK_ENTRY( data ) ) ) );
510
511                 if ( gtk_adjustment_get_value(adj) > 0 ) {
512                         td.rotate = g_pi_globals.rotate;
513                 }
514                 else{
515                         td.rotate = -g_pi_globals.rotate;
516                 }
517         }
518
519         gtk_adjustment_set_value(adj, 0);
520
521         // will scale shift rotate the patch accordingly
522
523
524         if ( td.shift[0] || td.shift[1] ) {
525                 UndoableCommand command( "patchTranslateTexture" );
526                 Scene_PatchTranslateTexture_Selected( GlobalSceneGraph(), td.shift[0], td.shift[1] );
527         }
528         else if ( td.scale[0] || td.scale[1] ) {
529                 UndoableCommand command( "patchScaleTexture" );
530                 Scene_PatchScaleTexture_Selected( GlobalSceneGraph(), td.scale[0], td.scale[1] );
531         }
532         else if ( td.rotate ) {
533                 UndoableCommand command( "patchRotateTexture" );
534                 Scene_PatchRotateTexture_Selected( GlobalSceneGraph(), td.rotate );
535         }
536
537         // update the point-by-point view
538         OnSelchangeComboColRow( ui::root ,0 );
539 }
540
541 static gint OnDialogKey( ui::Widget widget, GdkEventKey* event, gpointer data ){
542         if ( event->keyval == GDK_KEY_Return ) {
543                 OnApply( ui::root, 0 );
544                 return TRUE;
545         }
546         else if ( event->keyval == GDK_KEY_Escape ) {
547                 g_PatchInspector.GetPatchInfo();
548                 return TRUE;
549         }
550         return FALSE;
551 }
552
553 // =============================================================================
554 // PatchInspector class
555
556 ui::Window PatchInspector::BuildDialog(){
557         ui::Window window = ui::Window(create_floating_window( "Patch Properties", m_parent ));
558
559         m_position_tracker.connect( window );
560
561         global_accel_connect_window( window );
562
563         window_connect_focus_in_clear_focus_widget( window );
564
565
566         {
567                 auto vbox = ui::VBox( FALSE, 5 );
568                 gtk_container_set_border_width( GTK_CONTAINER( vbox ), 5 );
569                 vbox.show();
570                 window.add(vbox);
571                 {
572                         auto hbox = ui::HBox( FALSE, 5 );
573                         hbox.show();
574                         vbox.pack_start( hbox, TRUE, TRUE, 0 );
575                         {
576                                 auto vbox2 = ui::VBox( FALSE, 0 );
577                                 gtk_container_set_border_width( GTK_CONTAINER( vbox2 ), 0 );
578                                 vbox2.show();
579                                 hbox.pack_start( vbox2, TRUE, TRUE, 0 );
580                                 {
581                                         auto frame = ui::Frame( "Details" );
582                                         frame.show();
583                                         vbox2.pack_start( frame, TRUE, TRUE, 0 );
584                                         {
585                                                 auto vbox3 = ui::VBox( FALSE, 5 );
586                                                 gtk_container_set_border_width( GTK_CONTAINER( vbox3 ), 5 );
587                                                 vbox3.show();
588                                                 frame.add(vbox3);
589                                                 {
590                                                         auto table = ui::Table( 2, 2, FALSE );
591                                                         table.show();
592                                                         vbox3.pack_start( table, TRUE, TRUE, 0 );
593                                                         gtk_table_set_row_spacings( table, 5 );
594                                                         gtk_table_set_col_spacings( table, 5 );
595                                                         {
596                                                                 auto label = ui::Label( "Row:" );
597                                                                 label.show();
598                                 table.attach(label, {0, 1, 0, 1}, {(GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0)}, {0, 0});
599                                                         }
600                                                         {
601                                                                 auto label = ui::Label( "Column:" );
602                                                                 label.show();
603                                 table.attach(label, {1, 2, 0, 1}, {(GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0)}, {0, 0});
604                                                         }
605                                                         {
606                                                                 auto combo = ui::ComboBoxText(ui::New);
607                                                                 combo.connect( "changed", G_CALLBACK( OnSelchangeComboColRow ), this );
608                                                                 AddDialogData( *GTK_COMBO_BOX(combo), m_nRow );
609
610                                                                 combo.show();
611                                 table.attach(combo, {0, 1, 1, 2}, {(GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0)}, {0, 0});
612                                                                 combo.dimensions(60, -1);
613                                                                 m_pRowCombo = combo;
614                                                         }
615
616                                                         {
617                                                                 auto combo = ui::ComboBoxText(ui::New);
618                                                                 combo.connect( "changed", G_CALLBACK( OnSelchangeComboColRow ), this );
619                                                                 AddDialogData( *GTK_COMBO_BOX(combo), m_nCol );
620
621                                                                 combo.show();
622                                 table.attach(combo, {1, 2, 1, 2}, {(GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (0)}, {0, 0});
623                                                                 combo.dimensions(60, -1);
624                                                                 m_pColCombo = combo;
625                                                         }
626                                                 }
627                                                 auto table = ui::Table( 5, 2, FALSE );
628                                                 table.show();
629                                                 vbox3.pack_start( table, TRUE, TRUE, 0 );
630                                                 gtk_table_set_row_spacings( table, 5 );
631                                                 gtk_table_set_col_spacings( table, 5 );
632                                                 {
633                                                         auto label = ui::Label( "X:" );
634                                                         label.show();
635                             table.attach(label, {0, 1, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
636                                                 }
637                                                 {
638                                                         auto label = ui::Label( "Y:" );
639                                                         label.show();
640                             table.attach(label, {0, 1, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
641                                                 }
642                                                 {
643                                                         auto label = ui::Label( "Z:" );
644                                                         label.show();
645                             table.attach(label, {0, 1, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
646                                                 }
647                                                 {
648                                                         auto label = ui::Label( "S:" );
649                                                         label.show();
650                             table.attach(label, {0, 1, 3, 4}, {GTK_EXPAND | GTK_FILL, 0});
651                                                 }
652                                                 {
653                                                         auto label = ui::Label( "T:" );
654                                                         label.show();
655                             table.attach(label, {0, 1, 4, 5}, {GTK_EXPAND | GTK_FILL, 0});
656                                                 }
657                                                 {
658                                                         auto entry = ui::Entry(ui::New);
659                                                         entry.show();
660                             table.attach(entry, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
661                                                         AddDialogData( *GTK_ENTRY(entry), m_fX );
662
663                                                         entry.connect( "key_press_event", G_CALLBACK( OnDialogKey ), 0 );
664                                                 }
665                                                 {
666                                                         auto entry = ui::Entry(ui::New);
667                                                         entry.show();
668                             table.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
669                                                         AddDialogData( *GTK_ENTRY(entry), m_fY );
670
671                                                         entry.connect( "key_press_event", G_CALLBACK( OnDialogKey ), 0 );
672                                                 }
673                                                 {
674                                                         auto entry = ui::Entry(ui::New);
675                                                         entry.show();
676                             table.attach(entry, {1, 2, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
677                                                         AddDialogData( *GTK_ENTRY(entry), m_fZ );
678
679                                                         entry.connect( "key_press_event", G_CALLBACK( OnDialogKey ), 0 );
680                                                 }
681                                                 {
682                                                         auto entry = ui::Entry(ui::New);
683                                                         entry.show();
684                             table.attach(entry, {1, 2, 3, 4}, {GTK_EXPAND | GTK_FILL, 0});
685                                                         AddDialogData( *GTK_ENTRY(entry), m_fS );
686
687                                                         entry.connect( "key_press_event", G_CALLBACK( OnDialogKey ), 0 );
688                                                 }
689                                                 {
690                                                         auto entry = ui::Entry(ui::New);
691                                                         entry.show();
692                             table.attach(entry, {1, 2, 4, 5}, {GTK_EXPAND | GTK_FILL, 0});
693                                                         AddDialogData( *GTK_ENTRY(entry), m_fT );
694
695                                                         entry.connect( "key_press_event", G_CALLBACK( OnDialogKey ), 0 );
696                                                 }
697                                         }
698                                 }
699                                 if ( g_pGameDescription->mGameType == "doom3" ) {
700                                         auto frame = ui::Frame( "Tesselation" );
701                                         frame.show();
702                                         vbox2.pack_start( frame, TRUE, TRUE, 0 );
703                                         {
704                                                 auto vbox3 = ui::VBox( FALSE, 5 );
705                                                 gtk_container_set_border_width( GTK_CONTAINER( vbox3 ), 5 );
706                                                 vbox3.show();
707                                                 frame.add(vbox3);
708                                                 {
709                                                         auto table = ui::Table( 3, 2, FALSE );
710                                                         table.show();
711                                                         vbox3.pack_start( table, TRUE, TRUE, 0 );
712                                                         gtk_table_set_row_spacings( table, 5 );
713                                                         gtk_table_set_col_spacings( table, 5 );
714                                                         {
715                                                                 auto label = ui::Label( "Fixed" );
716                                                                 label.show();
717                                 table.attach(label, {0, 1, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
718                                                         }
719                                                         {
720                                                                 auto check = ui::CheckButton(GTK_CHECK_BUTTON( gtk_check_button_new() ));
721                                                                 check.show();
722                                 table.attach(check, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
723                                                                 m_subdivisions.m_enabled = check;
724                                                                 guint handler_id = check.connect( "toggled", G_CALLBACK( &Subdivisions::applyGtk ), &m_subdivisions );
725                                                                 g_object_set_data( G_OBJECT( check ), "handler", gint_to_pointer( handler_id ) );
726                                                         }
727                                                         {
728                                                                 auto label = ui::Label( "Horizontal" );
729                                                                 label.show();
730                                 table.attach(label, {0, 1, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
731                                                         }
732                                                         {
733                                                                 auto entry = ui::Entry(ui::New);
734                                                                 entry.show();
735                                 table.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
736                                                                 m_subdivisions.m_horizontal = entry;
737                                                                 m_horizontalSubdivisionsEntry.connect( entry );
738                                                         }
739                                                         {
740                                                                 auto label = ui::Label( "Vertical" );
741                                                                 label.show();
742                                 table.attach(label, {0, 1, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
743                                                         }
744                                                         {
745                                                                 auto entry = ui::Entry(ui::New);
746                                                                 entry.show();
747                                 table.attach(entry, {1, 2, 2, 3}, {GTK_EXPAND | GTK_FILL, 0});
748                                                                 m_subdivisions.m_vertical = entry;
749                                                                 m_verticalSubdivisionsEntry.connect( entry );
750                                                         }
751                                                 }
752                                         }
753                                 }
754                         }
755                         {
756                                 auto frame = ui::Frame( "Texturing" );
757                                 frame.show();
758                                 hbox.pack_start( frame, TRUE, TRUE, 0 );
759                                 {
760                                         auto vbox2 = ui::VBox( FALSE, 5 );
761                                         vbox2.show();
762                                         frame.add(vbox2);
763                                         gtk_container_set_border_width( GTK_CONTAINER( vbox2 ), 5 );
764                                         {
765                                                 auto label = ui::Label( "Name:" );
766                                                 label.show();
767                                                 vbox2.pack_start( label, TRUE, TRUE, 0 );
768                                                 gtk_label_set_justify( label, GTK_JUSTIFY_LEFT );
769                                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
770                                         }
771                                         {
772                                                 auto entry = ui::Entry(ui::New);
773                                                 //  gtk_editable_set_editable (GTK_ENTRY (entry), false);
774                                                 entry.show();
775                                                 vbox2.pack_start( entry, TRUE, TRUE, 0 );
776                                                 AddDialogData( *GTK_ENTRY(entry), m_strName );
777
778                                                 entry.connect( "key_press_event", G_CALLBACK( OnDialogKey ), 0 );
779                                         }
780                                         {
781                                                 auto table = ui::Table( 5, 4, FALSE );
782                                                 table.show();
783                                                 vbox2.pack_start( table, TRUE, TRUE, 0 );
784                                                 gtk_table_set_row_spacings( table, 5 );
785                                                 gtk_table_set_col_spacings( table, 5 );
786                                                 {
787                                                         auto label = ui::Label( "Horizontal Shift Step" );
788                                                         label.show();
789                             table.attach(label, {2, 4, 0, 1}, {GTK_FILL | GTK_EXPAND, 0});
790                                                         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
791                                                 }
792                                                 {
793                                                         auto label = ui::Label( "Vertical Shift Step" );
794                                                         label.show();
795                             table.attach(label, {2, 4, 1, 2}, {GTK_FILL | GTK_EXPAND, 0});
796                                                         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
797                                                 }
798                                                 {
799                                                         auto label = ui::Label( "Horizontal Stretch Step" );
800                                                         label.show();
801                             table.attach(label, {2, 3, 2, 3}, {GTK_FILL | GTK_EXPAND, 0});
802                                                         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
803                                                 }
804                                                 {
805                                                         auto button = ui::Button( "Flip" );
806                                                         button.show();
807                             table.attach(button, {3, 4, 2, 3}, {GTK_FILL, 0});
808                                                         button.connect( "clicked", G_CALLBACK( OnBtnPatchFlipX ), 0 );
809                                                         button.dimensions(60, -1);
810                                                 }
811                                                 {
812                                                         auto label = ui::Label( "Vertical Stretch Step" );
813                                                         label.show();
814                             table.attach(label, {2, 3, 3, 4}, {GTK_FILL | GTK_EXPAND, 0});
815                                                         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
816                                                 }
817                                                 {
818                                                         auto button = ui::Button( "Flip" );
819                                                         button.show();
820                             table.attach(button, {3, 4, 3, 4}, {GTK_FILL, 0});
821                                                         button.connect( "clicked", G_CALLBACK( OnBtnPatchFlipY ), 0 );
822                                                         button.dimensions(60, -1);
823                                                 }
824                                                 {
825                                                         auto label = ui::Label( "Rotate Step" );
826                                                         label.show();
827                             table.attach(label, {2, 4, 4, 5}, {GTK_FILL | GTK_EXPAND, 0});
828                                                         gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
829                                                 }
830                                                 {
831                                                         auto entry = ui::Entry(ui::New);
832                                                         entry.show();
833                             table.attach(entry, {0, 1, 0, 1}, {GTK_FILL, 0});
834                                                         entry.dimensions(50, -1);
835                                                         g_object_set_data( G_OBJECT( window ), "hshift_entry", (void *) entry );
836                                                         // we fill in this data, if no patch is selected the widgets are unmodified when the inspector is raised
837                                                         // so we need to have at least one initialisation somewhere
838                                                         entry_set_float( entry, g_pi_globals.shift[0] );
839
840                                                         auto adj = ui::Adjustment( 0, -8192, 8192, 1, 1, 0 );
841                                                         adj.connect( "value_changed", G_CALLBACK( OnSpinChanged ), (gpointer) entry );
842                                                         g_object_set_data( G_OBJECT( window ), "hshift_adj", (gpointer) adj );
843
844                                                         auto spin = ui::SpinButton( adj, 1, 0 );
845                                                         spin.show();
846                             table.attach(spin, {1, 2, 0, 1}, {0, 0});
847                                                         spin.dimensions(10, -1);
848                                                         gtk_widget_set_can_focus( spin, false );
849                                                 }
850                                                 {
851                                                         auto entry = ui::Entry(ui::New);
852                                                         entry.show();
853                             table.attach(entry, {0, 1, 1, 2}, {GTK_FILL, 0});
854                                                         entry.dimensions(50, -1);
855                                                         entry_set_float( entry, g_pi_globals.shift[1] );
856
857                                                         auto adj = ui::Adjustment( 0, -8192, 8192, 1, 1, 0 );
858                                                         adj.connect( "value_changed", G_CALLBACK( OnSpinChanged ), entry );
859                                                         g_object_set_data( G_OBJECT( window ), "vshift_adj", (gpointer) adj );
860
861                                                         auto spin = ui::SpinButton( adj, 1, 0 );
862                                                         spin.show();
863                             table.attach(spin, {1, 2, 1, 2}, {0, 0});
864                                                         spin.dimensions(10, -1);
865                                                         gtk_widget_set_can_focus( spin, false );
866                                                 }
867                                                 {
868                                                         auto entry = ui::Entry(ui::New);
869                                                         entry.show();
870                             table.attach(entry, {0, 1, 2, 3}, {GTK_FILL, 0});
871                                                         entry.dimensions(50, -1);
872                                                         entry_set_float( entry, g_pi_globals.scale[0] );
873
874                                                         auto adj = ui::Adjustment( 0, -1000, 1000, 1, 1, 0 );
875                                                         adj.connect( "value_changed", G_CALLBACK( OnSpinChanged ), entry );
876                                                         g_object_set_data( G_OBJECT( window ), "hscale_adj", (gpointer) adj );
877
878                                                         auto spin = ui::SpinButton( adj, 1, 0 );
879                                                         spin.show();
880                             table.attach(spin, {1, 2, 2, 3}, {0, 0});
881                                                         spin.dimensions(10, -1);
882                                                         gtk_widget_set_can_focus( spin, false );
883                                                 }
884                                                 {
885                                                         auto entry = ui::Entry(ui::New);
886                                                         entry.show();
887                             table.attach(entry, {0, 1, 3, 4}, {GTK_FILL, 0});
888                                                         entry.dimensions(50, -1);
889                                                         entry_set_float( entry, g_pi_globals.scale[1] );
890
891                                                         auto adj = ui::Adjustment( 0, -1000, 1000, 1, 1, 0 );
892                                                         adj.connect( "value_changed", G_CALLBACK( OnSpinChanged ), entry );
893                                                         g_object_set_data( G_OBJECT( window ), "vscale_adj", (gpointer) adj );
894
895                                                         auto spin = ui::SpinButton( adj, 1, 0 );
896                                                         spin.show();
897                             table.attach(spin, {1, 2, 3, 4}, {0, 0});
898                                                         spin.dimensions(10, -1);
899                                                         gtk_widget_set_can_focus( spin, false );
900                                                 }
901                                                 {
902                                                         auto entry = ui::Entry(ui::New);
903                                                         entry.show();
904                             table.attach(entry, {0, 1, 4, 5}, {GTK_FILL, 0});
905                                                         entry.dimensions(50, -1);
906                                                         entry_set_float( entry, g_pi_globals.rotate );
907
908                                                         auto adj = ui::Adjustment( 0, -1000, 1000, 1, 1, 0 ); // NOTE: Arnout - this really should be 360 but can't change it anymore as it could break existing maps
909                                                         adj.connect( "value_changed", G_CALLBACK( OnSpinChanged ), entry );
910                                                         g_object_set_data( G_OBJECT( window ), "rotate_adj", (gpointer) adj );
911
912                                                         auto spin = ui::SpinButton( adj, 1, 0 );
913                                                         spin.show();
914                             table.attach(spin, {1, 2, 4, 5}, {0, 0});
915                                                         spin.dimensions(10, -1);
916                                                         gtk_widget_set_can_focus( spin, false );
917                                                 }
918                                         }
919                                         auto hbox2 = ui::HBox( TRUE, 5 );
920                                         hbox2.show();
921                                         vbox2.pack_start( hbox2, TRUE, FALSE, 0 );
922                                         {
923                                                 auto button = ui::Button( "Auto Cap" );
924                                                 button.show();
925                                                 hbox2.pack_end(button, TRUE, FALSE, 0);
926                                                 button.connect( "clicked", G_CALLBACK( OnBtnPatchAutoCap ), 0 );
927                                                 button.dimensions(60, -1);
928                                         }
929                                         {
930                                                 auto button = ui::Button( "CAP" );
931                                                 button.show();
932                                                 hbox2.pack_end(button, TRUE, FALSE, 0);
933                                                 button.connect( "clicked", G_CALLBACK( OnBtnPatchdetails ), 0 );
934                                                 button.dimensions(60, -1);
935                                         }
936                                         {
937                                                 auto button = ui::Button( "Set..." );
938                                                 button.show();
939                                                 hbox2.pack_end(button, TRUE, FALSE, 0);
940                                                 button.connect( "clicked", G_CALLBACK( OnBtnPatchreset ), 0 );
941                                                 button.dimensions(60, -1);
942                                         }
943                                         {
944                                                 auto button = ui::Button( "Natural" );
945                                                 button.show();
946                                                 hbox2.pack_end(button, TRUE, FALSE, 0);
947                                                 button.connect( "clicked", G_CALLBACK( OnBtnPatchnatural ), 0 );
948                                                 button.dimensions(60, -1);
949                                         }
950                                         {
951                                                 auto button = ui::Button( "Fit" );
952                                                 button.show();
953                                                 hbox2.pack_end(button, TRUE, FALSE, 0);
954                                                 button.connect( "clicked", G_CALLBACK( OnBtnPatchfit ), 0 );
955                                                 button.dimensions(60, -1);
956                                         }
957                                 }
958                         }
959                 }
960         }
961
962         return window;
963 }
964
965 // sync the dialog our internal data structures
966 void PatchInspector::exportData(){
967         m_bListenChanged = false;
968         Dialog::exportData();
969         m_bListenChanged = true;
970 }
971 void PatchInspector::importData(){
972         m_bListenChanged = false;
973         Dialog::importData();
974         m_bListenChanged = true;
975 }
976
977 // read the map and feed in the stuff to the dialog box
978 void PatchInspector::GetPatchInfo(){
979         if ( g_pGameDescription->mGameType == "doom3" ) {
980                 m_subdivisions.update();
981         }
982
983         if ( GlobalSelectionSystem().countSelected() == 0 ) {
984                 m_Patch = 0;
985         }
986         else
987         {
988                 m_Patch = Node_getPatch( GlobalSelectionSystem().ultimateSelected().path().top() );
989         }
990
991         if ( m_Patch != 0 ) {
992                 m_strName = m_Patch->GetShader();
993
994                 // fill in the numbers for Row / Col selection
995                 m_bListenChanged = false;
996
997                 {
998                         gtk_combo_box_set_active( m_pRowCombo, 0 );
999
1000                         for ( std::size_t i = 0; i < m_countRows; ++i )
1001                         {
1002                                 gtk_combo_box_text_remove( m_pRowCombo, gint( m_countRows - i - 1 ) );
1003                         }
1004
1005                         m_countRows = m_Patch->getHeight();
1006                         for ( std::size_t i = 0; i < m_countRows; ++i )
1007                         {
1008                                 char buffer[16];
1009                                 sprintf( buffer, "%u", Unsigned( i ) );
1010                                 gtk_combo_box_text_append_text( m_pRowCombo, buffer );
1011                         }
1012
1013                         gtk_combo_box_set_active( m_pRowCombo, 0 );
1014                 }
1015
1016                 {
1017                         gtk_combo_box_set_active( m_pColCombo, 0 );
1018
1019                         for ( std::size_t i = 0; i < m_countCols; ++i )
1020                         {
1021                                 gtk_combo_box_text_remove( m_pColCombo, gint( m_countCols - i - 1 ) );
1022                         }
1023
1024                         m_countCols = m_Patch->getWidth();
1025                         for ( std::size_t i = 0; i < m_countCols; ++i )
1026                         {
1027                                 char buffer[16];
1028                                 sprintf( buffer, "%u", Unsigned( i ) );
1029                                 gtk_combo_box_text_append_text( m_pColCombo, buffer );
1030                         }
1031
1032                         gtk_combo_box_set_active( m_pColCombo, 0 );
1033                 }
1034
1035                 m_bListenChanged = true;
1036
1037         }
1038         else
1039         {
1040                 //globalOutputStream() << "WARNING: no patch\n";
1041         }
1042         // fill in our internal structs
1043         m_nRow = 0; m_nCol = 0;
1044         UpdateRowColInfo();
1045         // now update the dialog box
1046         importData();
1047 }
1048
1049 // read the current patch on map and initialize m_fX m_fY accordingly
1050 // NOTE: don't call UpdateData in there, it's not meant for
1051 void PatchInspector::UpdateRowColInfo(){
1052         m_fX = m_fY = m_fZ = m_fS = m_fT = 0.0;
1053
1054         if ( m_Patch != 0 ) {
1055                 // we rely on whatever active row/column has been set before we get called
1056                 std::size_t r = m_nRow;
1057                 std::size_t c = m_nCol;
1058                 if ( r < m_Patch->getHeight()
1059                          && c < m_Patch->getWidth() ) {
1060                         const PatchControl& p = m_Patch->ctrlAt( r,c );
1061                         m_fX = p.m_vertex[0];
1062                         m_fY = p.m_vertex[1];
1063                         m_fZ = p.m_vertex[2];
1064                         m_fS = p.m_texcoord[0];
1065                         m_fT = p.m_texcoord[1];
1066                 }
1067         }
1068 }
1069
1070
1071 void PatchInspector_SelectionChanged( const Selectable& selectable ){
1072         PatchInspector_queueDraw();
1073 }
1074
1075
1076 #include "preferencesystem.h"
1077
1078
1079 void PatchInspector_Construct(){
1080         GlobalCommands_insert( "PatchInspector", FreeCaller<PatchInspector_toggleShown>(), Accelerator( 'S', (GdkModifierType)GDK_SHIFT_MASK ) );
1081
1082         GlobalPreferenceSystem().registerPreference( "PatchWnd", WindowPositionTrackerImportStringCaller( g_PatchInspector.m_position_tracker ), WindowPositionTrackerExportStringCaller( g_PatchInspector.m_position_tracker ) );
1083         GlobalPreferenceSystem().registerPreference( "SI_PatchTexdef_Scale1", FloatImportStringCaller( g_pi_globals.scale[0] ), FloatExportStringCaller( g_pi_globals.scale[0] ) );
1084         GlobalPreferenceSystem().registerPreference( "SI_PatchTexdef_Scale2", FloatImportStringCaller( g_pi_globals.scale[1] ), FloatExportStringCaller( g_pi_globals.scale[1] ) );
1085         GlobalPreferenceSystem().registerPreference( "SI_PatchTexdef_Shift1", FloatImportStringCaller( g_pi_globals.shift[0] ), FloatExportStringCaller( g_pi_globals.shift[0] ) );
1086         GlobalPreferenceSystem().registerPreference( "SI_PatchTexdef_Shift2", FloatImportStringCaller( g_pi_globals.shift[1] ), FloatExportStringCaller( g_pi_globals.shift[1] ) );
1087         GlobalPreferenceSystem().registerPreference( "SI_PatchTexdef_Rotate", FloatImportStringCaller( g_pi_globals.rotate ), FloatExportStringCaller( g_pi_globals.rotate ) );
1088
1089         typedef FreeCaller1<const Selectable&, PatchInspector_SelectionChanged> PatchInspectorSelectionChangedCaller;
1090         GlobalSelectionSystem().addSelectionChangeCallback( PatchInspectorSelectionChangedCaller() );
1091         typedef FreeCaller<PatchInspector_queueDraw> PatchInspectorQueueDrawCaller;
1092         Patch_addTextureChangedCallback( PatchInspectorQueueDrawCaller() );
1093 }
1094 void PatchInspector_Destroy(){
1095 }