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