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