]> git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/prtview/ConfigDialog.cpp
Merge commit 'c845c5cd8f427d39665d6a8b1f6eeff401370d80' into garux-merge
[xonotic/netradiant.git] / contrib / prtview / ConfigDialog.cpp
1 /*
2    PrtView plugin for GtkRadiant
3    Copyright (C) 2001 Geoffrey Dewan, Loki software and qeradiant.com
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "ConfigDialog.h"
21 #include <stdio.h>
22 #include <gtk/gtk.h>
23 #include <uilib/uilib.h>
24 #include "gtkutil/pointer.h"
25
26 #include "iscenegraph.h"
27
28 #include "prtview.h"
29 #include "portals.h"
30
31 static void dialog_button_callback( ui::Widget widget, gpointer data ){
32         int *loop, *ret;
33
34         auto parent = widget.window();
35         loop = (int*)g_object_get_data( G_OBJECT( parent ), "loop" );
36         ret = (int*)g_object_get_data( G_OBJECT( parent ), "ret" );
37
38         *loop = 0;
39         *ret = gpointer_to_int( data );
40 }
41
42 static gint dialog_delete_callback( ui::Widget widget, GdkEvent* event, gpointer data ){
43         widget.hide();
44         int *loop = (int *) g_object_get_data(G_OBJECT(widget), "loop");
45         *loop = 0;
46         return TRUE;
47 }
48
49 // =============================================================================
50 // Color selection dialog
51
52 static int DoColor( PackedColour *c ){
53         GdkColor clr;
54         int loop = 1, ret = IDCANCEL;
55
56         clr.red = (guint16) (GetRValue(*c) * (65535 / 255));
57         clr.blue = (guint16) (GetGValue(*c) * (65535 / 255));
58         clr.green = (guint16) (GetBValue(*c) * (65535 / 255));
59
60         auto dlg = ui::Widget::from(gtk_color_selection_dialog_new( "Choose Color" ));
61
62         gtk_window_set_transient_for( GTK_WINDOW( dlg ), GTK_WINDOW( g_pRadiantWnd ) );
63         gtk_window_set_position( GTK_WINDOW( dlg ),GTK_WIN_POS_CENTER_ON_PARENT );
64         gtk_window_set_modal( GTK_WINDOW( dlg ), TRUE );
65         gtk_color_selection_set_current_color( GTK_COLOR_SELECTION( gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(dlg)) ), &clr );
66         dlg.connect( "delete_event", G_CALLBACK( dialog_delete_callback ), NULL );
67         dlg.connect( "destroy", G_CALLBACK( gtk_widget_destroy ), NULL );
68
69         GtkWidget *ok_button, *cancel_button;
70         g_object_get(dlg, "ok-button", &ok_button, "cancel-button", &cancel_button, nullptr);
71
72         ui::Widget::from(ok_button).connect( "clicked", G_CALLBACK( dialog_button_callback ), GINT_TO_POINTER( IDOK ) );
73         ui::Widget::from(cancel_button).connect( "clicked", G_CALLBACK( dialog_button_callback ), GINT_TO_POINTER( IDCANCEL ) );
74         g_object_set_data( G_OBJECT( dlg ), "loop", &loop );
75         g_object_set_data( G_OBJECT( dlg ), "ret", &ret );
76
77         dlg.show();
78         gtk_grab_add( dlg );
79
80         while ( loop )
81                 gtk_main_iteration();
82
83         gtk_color_selection_get_current_color( GTK_COLOR_SELECTION( gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(dlg)) ), &clr );
84
85         gtk_grab_remove( dlg );
86         dlg.destroy();
87
88         if ( ret == IDOK ) {
89                 *c = RGB( clr.red / (65535 / 255), clr.green / (65535 / 255), clr.blue / (65535 / 255));
90         }
91
92         return ret;
93 }
94
95 static void Set2DText(ui::Widget label ){
96         char s[40];
97
98         sprintf( s, "Line Width = %6.3f", portals.width_2d * 0.5f );
99
100         gtk_label_set_text( GTK_LABEL( label ), s );
101 }
102
103 static void Set3DText(ui::Widget label ){
104         char s[40];
105
106         sprintf( s, "Line Width = %6.3f", portals.width_3d * 0.5f );
107
108         gtk_label_set_text( GTK_LABEL( label ), s );
109 }
110
111 static void Set3DTransText(ui::Widget label ){
112         char s[40];
113
114         sprintf( s, "Polygon transparency = %d%%", (int)portals.trans_3d );
115
116         gtk_label_set_text( GTK_LABEL( label ), s );
117 }
118
119 static void SetClipText(ui::Widget label ){
120         char s[40];
121
122         sprintf( s, "Cubic clip range = %d", (int)portals.clip_range * 64 );
123
124         gtk_label_set_text( GTK_LABEL( label ), s );
125 }
126
127 static void OnScroll2d(ui::Adjustment adj, gpointer data ){
128         portals.width_2d = static_cast<float>( gtk_adjustment_get_value(adj) );
129         Set2DText( ui::Widget::from(data) );
130
131         Portals_shadersChanged();
132         SceneChangeNotify();
133 }
134
135 static void OnScroll3d(ui::Adjustment adj, gpointer data ){
136         portals.width_3d = static_cast<float>( gtk_adjustment_get_value(adj) );
137         Set3DText( ui::Widget::from( data ) );
138
139         SceneChangeNotify();
140 }
141
142 static void OnScrollTrans(ui::Adjustment adj, gpointer data ){
143         portals.trans_3d = static_cast<float>( gtk_adjustment_get_value(adj) );
144         Set3DTransText( ui::Widget::from( data ) );
145
146         SceneChangeNotify();
147 }
148
149 static void OnScrollClip(ui::Adjustment adj, gpointer data ){
150         portals.clip_range = static_cast<float>( gtk_adjustment_get_value(adj) );
151         SetClipText( ui::Widget::from( data ) );
152
153         SceneChangeNotify();
154 }
155
156 static void OnAntiAlias2d(ui::Widget widget, gpointer data ){
157         portals.aa_2d = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( widget ) ) ? true : false;
158
159         Portals_shadersChanged();
160
161         SceneChangeNotify();
162 }
163
164 static void OnConfig2d(ui::Widget widget, gpointer data ){
165         portals.show_2d = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( widget ) ) ? true : false;
166
167         SceneChangeNotify();
168 }
169
170 static void OnColor2d(ui::Widget widget, gpointer data ){
171         if ( DoColor( &portals.color_2d ) == IDOK ) {
172                 Portals_shadersChanged();
173
174                 SceneChangeNotify();
175         }
176 }
177
178 static void OnConfig3d(ui::Widget widget, gpointer data ){
179         portals.show_3d = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( widget ) ) ? true : false;
180
181         SceneChangeNotify();
182 }
183
184
185 static void OnAntiAlias3d(ui::Widget widget, gpointer data ){
186         portals.aa_3d = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( widget ) ) ? true : false;
187
188         Portals_shadersChanged();
189         SceneChangeNotify();
190 }
191
192 static void OnColor3d(ui::Widget widget, gpointer data ){
193         if ( DoColor( &portals.color_3d ) == IDOK ) {
194                 Portals_shadersChanged();
195
196                 SceneChangeNotify();
197         }
198 }
199
200 static void OnColorFog(ui::Widget widget, gpointer data ){
201         if ( DoColor( &portals.color_fog ) == IDOK ) {
202                 Portals_shadersChanged();
203
204                 SceneChangeNotify();
205         }
206 }
207
208 static void OnFog(ui::Widget widget, gpointer data ){
209         portals.fog = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( widget ) ) ? true : false;
210
211         Portals_shadersChanged();
212         SceneChangeNotify();
213 }
214
215 static void OnSelchangeZbuffer(ui::Widget widget, gpointer data ){
216         portals.zbuffer = gpointer_to_int( data );
217
218         Portals_shadersChanged();
219         SceneChangeNotify();
220 }
221
222 static void OnPoly(ui::Widget widget, gpointer data ){
223         portals.polygons = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( widget ) );
224
225         SceneChangeNotify();
226 }
227
228 static void OnLines(ui::Widget widget, gpointer data ){
229         portals.lines = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( widget ) );
230
231         SceneChangeNotify();
232 }
233
234 static void OnClip(ui::Widget widget, gpointer data ){
235         portals.clip = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( widget ) ) ? true : false;
236
237         SceneChangeNotify();
238 }
239
240 void DoConfigDialog(){
241         int loop = 1, ret = IDCANCEL;
242
243         auto dlg = ui::Window( ui::window_type::TOP );
244
245         gtk_window_set_transient_for( GTK_WINDOW( dlg ), GTK_WINDOW( g_pRadiantWnd ) );
246         gtk_window_set_position( GTK_WINDOW( dlg ),GTK_WIN_POS_CENTER_ON_PARENT );
247         gtk_window_set_modal( GTK_WINDOW( dlg ), TRUE );
248
249         gtk_window_set_title( dlg, "Portal Viewer Configuration" );
250         dlg.connect( "delete_event",
251                                                 G_CALLBACK( dialog_delete_callback ), NULL );
252         dlg.connect( "destroy",
253                                                 G_CALLBACK( gtk_widget_destroy ), NULL );
254         g_object_set_data( G_OBJECT( dlg ), "loop", &loop );
255         g_object_set_data( G_OBJECT( dlg ), "ret", &ret );
256
257         auto vbox = ui::VBox( FALSE, 5 );
258         vbox.show();
259         dlg.add(vbox);
260         gtk_container_set_border_width( GTK_CONTAINER( vbox ), 5 );
261
262         auto frame = ui::Frame( "3D View" );
263         frame.show();
264         vbox.pack_start( frame, TRUE, TRUE, 0 );
265
266         auto vbox2 = ui::VBox( FALSE, 5 );
267         vbox2.show();
268         frame.add(vbox2);
269         gtk_container_set_border_width( GTK_CONTAINER( vbox2 ), 5 );
270
271         auto hbox = ui::HBox( FALSE, 5 );
272         hbox.show();
273         vbox2.pack_start( hbox, TRUE, TRUE, 0 );
274
275         auto adj = ui::Adjustment( portals.width_3d, 2, 40, 1, 1, 0 );
276         auto lw3slider = ui::HScale( adj );
277         lw3slider.show();
278         hbox.pack_start( lw3slider, TRUE, TRUE, 0 );
279         gtk_scale_set_draw_value( GTK_SCALE( lw3slider ), FALSE );
280
281         auto lw3label = ui::Label( "" );
282         lw3label.show();
283         hbox.pack_start( lw3label, FALSE, TRUE, 0 );
284         adj.connect( "value_changed", G_CALLBACK( OnScroll3d ), lw3label );
285
286         auto table = ui::Table( 2, 4, FALSE );
287         table.show();
288         vbox2.pack_start( table, TRUE, TRUE, 0 );
289     gtk_table_set_row_spacings(table, 5);
290     gtk_table_set_col_spacings(table, 5);
291
292         auto button = ui::Button( "Color" );
293         button.show();
294     table.attach(button, {0, 1, 0, 1}, {GTK_FILL, 0});
295         button.connect( "clicked", G_CALLBACK( OnColor3d ), NULL );
296
297         button = ui::Button( "Depth Color" );
298         button.show();
299     table.attach(button, {0, 1, 1, 2}, {GTK_FILL, 0});
300         button.connect( "clicked", G_CALLBACK( OnColorFog ), NULL );
301
302         auto aa3check = ui::CheckButton( "Anti-Alias (May not work on some video cards)" );
303         aa3check.show();
304     table.attach(aa3check, {1, 4, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
305         aa3check.connect( "toggled", G_CALLBACK( OnAntiAlias3d ), NULL );
306
307         auto depthcheck = ui::CheckButton( "Depth Cue" );
308         depthcheck.show();
309     table.attach(depthcheck, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
310         depthcheck.connect( "toggled", G_CALLBACK( OnFog ), NULL );
311
312         auto linescheck = ui::CheckButton( "Lines" );
313         linescheck.show();
314     table.attach(linescheck, {2, 3, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
315         linescheck.connect( "toggled", G_CALLBACK( OnLines ), NULL );
316
317         auto polyscheck = ui::CheckButton( "Polygons" );
318         polyscheck.show();
319     table.attach(polyscheck, {3, 4, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
320         polyscheck.connect( "toggled", G_CALLBACK( OnPoly ), NULL );
321
322         auto zlist = ui::ComboBoxText(ui::New);
323         zlist.show();
324         vbox2.pack_start( zlist, TRUE, FALSE, 0 );
325
326         gtk_combo_box_text_append_text(zlist, "Z-Buffer Test and Write (recommended for solid or no polygons)");
327         gtk_combo_box_text_append_text(zlist, "Z-Buffer Test Only (recommended for transparent polygons)");
328         gtk_combo_box_text_append_text(zlist, "Z-Buffer Off");
329
330         zlist.connect("changed", G_CALLBACK(+[](ui::ComboBox self, void *) {
331                 OnSelchangeZbuffer(self, GINT_TO_POINTER(gtk_combo_box_get_active(self)));
332         }), nullptr);
333
334         table = ui::Table( 2, 2, FALSE );
335         table.show();
336         vbox2.pack_start( table, TRUE, TRUE, 0 );
337     gtk_table_set_row_spacings(table, 5);
338     gtk_table_set_col_spacings(table, 5);
339
340         adj = ui::Adjustment( portals.trans_3d, 0, 100, 1, 1, 0 );
341         auto transslider = ui::HScale( adj );
342         transslider.show();
343     table.attach(transslider, {0, 1, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
344         gtk_scale_set_draw_value( GTK_SCALE( transslider ), FALSE );
345
346         auto translabel = ui::Label( "" );
347         translabel.show();
348     table.attach(translabel, {1, 2, 0, 1}, {GTK_FILL, 0});
349         gtk_misc_set_alignment( GTK_MISC( translabel ), 0.0, 0.0 );
350         adj.connect( "value_changed", G_CALLBACK( OnScrollTrans ), translabel );
351
352         adj = ui::Adjustment( portals.clip_range, 1, 128, 1, 1, 0 );
353         auto clipslider = ui::HScale( adj );
354         clipslider.show();
355     table.attach(clipslider, {0, 1, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
356         gtk_scale_set_draw_value( GTK_SCALE( clipslider ), FALSE );
357
358         auto cliplabel = ui::Label( "" );
359         cliplabel.show();
360     table.attach(cliplabel, {1, 2, 1, 2}, {GTK_FILL, 0});
361         gtk_misc_set_alignment( GTK_MISC( cliplabel ), 0.0, 0.0 );
362         adj.connect( "value_changed", G_CALLBACK( OnScrollClip ), cliplabel );
363
364         hbox = ui::HBox( TRUE, 5 );
365         hbox.show();
366         vbox2.pack_start( hbox, TRUE, FALSE, 0 );
367
368         auto show3check = ui::CheckButton( "Show" );
369         show3check.show();
370         hbox.pack_start( show3check, TRUE, TRUE, 0 );
371         show3check.connect( "toggled", G_CALLBACK( OnConfig3d ), NULL );
372
373         auto portalcheck = ui::CheckButton( "Portal cubic clipper" );
374         portalcheck.show();
375         hbox.pack_start( portalcheck, TRUE, TRUE, 0 );
376         portalcheck.connect( "toggled", G_CALLBACK( OnClip ), NULL );
377
378         frame = ui::Frame( "2D View" );
379         frame.show();
380         vbox.pack_start( frame, TRUE, TRUE, 0 );
381
382         vbox2 = ui::VBox( FALSE, 5 );
383         vbox2.show();
384         frame.add(vbox2);
385         gtk_container_set_border_width( GTK_CONTAINER( vbox2 ), 5 );
386
387         hbox = ui::HBox( FALSE, 5 );
388         hbox.show();
389         vbox2.pack_start( hbox, TRUE, FALSE, 0 );
390
391         adj = ui::Adjustment( portals.width_2d, 2, 40, 1, 1, 0 );
392         auto lw2slider = ui::HScale( adj );
393         lw2slider.show();
394         hbox.pack_start( lw2slider, TRUE, TRUE, 0 );
395         gtk_scale_set_draw_value( GTK_SCALE( lw2slider ), FALSE );
396
397         auto lw2label = ui::Label( "" );
398         lw2label.show();
399         hbox.pack_start( lw2label, FALSE, TRUE, 0 );
400         adj.connect( "value_changed", G_CALLBACK( OnScroll2d ), lw2label );
401
402         hbox = ui::HBox( FALSE, 5 );
403         hbox.show();
404         vbox2.pack_start( hbox, TRUE, FALSE, 0 );
405
406         button = ui::Button( "Color" );
407         button.show();
408         hbox.pack_start( button, FALSE, FALSE, 0 );
409         button.connect( "clicked", G_CALLBACK( OnColor2d ), NULL );
410         button.dimensions(60, -1);
411
412         auto aa2check = ui::CheckButton( "Anti-Alias (May not work on some video cards)" );
413         aa2check.show();
414         hbox.pack_start( aa2check, TRUE, TRUE, 0 );
415         aa2check.connect( "toggled", G_CALLBACK( OnAntiAlias2d ), NULL );
416
417         hbox = ui::HBox( FALSE, 5 );
418         hbox.show();
419         vbox2.pack_start( hbox, TRUE, FALSE, 0 );
420
421         auto show2check = ui::CheckButton( "Show" );
422         show2check.show();
423         hbox.pack_start( show2check, FALSE, FALSE, 0 );
424         show2check.connect( "toggled", G_CALLBACK( OnConfig2d ), NULL );
425
426         hbox = ui::HBox( FALSE, 5 );
427         hbox.show();
428         vbox.pack_start( hbox, FALSE, FALSE, 0 );
429
430         button = ui::Button( "OK" );
431         button.show();
432         hbox.pack_end(button, FALSE, FALSE, 0);
433         button.connect( "clicked",
434                                                 G_CALLBACK( dialog_button_callback ), GINT_TO_POINTER( IDOK ) );
435         button.dimensions(60, -1);
436
437         // initialize dialog
438         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( show2check ), portals.show_2d );
439         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( aa2check ), portals.aa_2d );
440         Set2DText( lw2label );
441
442         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( show3check ), portals.show_3d );
443         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( depthcheck ), portals.fog );
444         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( polyscheck ), portals.polygons );
445         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( linescheck ), portals.lines );
446         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( aa3check ), portals.aa_3d );
447         gtk_combo_box_set_active(zlist, portals.zbuffer);
448         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( portalcheck ), portals.clip );
449
450         Set3DText( lw3label );
451         Set3DTransText( translabel );
452         SetClipText( cliplabel );
453
454         gtk_grab_add( dlg );
455         dlg.show();
456
457         while ( loop )
458                 gtk_main_iteration();
459
460         gtk_grab_remove( dlg );
461         dlg.destroy();
462 }