]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/brushexport/callbacks.cpp
Merge commit '830125fad042fad35dc029b6eb57c8156ad7e176'
[xonotic/netradiant.git] / contrib / brushexport / callbacks.cpp
1 #include <glib.h>
2 #include <gtk/gtk.h>
3 #include <set>
4
5 #include "qerplugin.h"
6 #include "debugging/debugging.h"
7 #include "support.h"
8 #include "export.h"
9
10 // stuff from interface.cpp
11 void DestroyWindow();
12
13
14 namespace callbacks {
15
16 void OnDestroy( GtkWidget* w, gpointer data ){
17         DestroyWindow();
18 }
19
20 void OnExportClicked( GtkButton* button, gpointer user_data ){
21         GtkWidget* window = lookup_widget( GTK_WIDGET( button ), "w_plugplug2" );
22         ASSERT_NOTNULL( window );
23         const char* cpath = GlobalRadiant().m_pfnFileDialog( window, false, "Save as Obj", 0, 0, false, false, true );
24         if ( !cpath ) {
25                 return;
26         }
27
28         std::string path( cpath );
29
30         // get ignore list from ui
31         std::set<std::string> ignore;
32
33         GtkTreeView* view = GTK_TREE_VIEW( lookup_widget( GTK_WIDGET( button ), "t_materialist" ) );
34         GtkListStore* list = GTK_LIST_STORE( gtk_tree_view_get_model( view ) );
35
36         GtkTreeIter iter;
37         gboolean valid = gtk_tree_model_get_iter_first( GTK_TREE_MODEL( list ), &iter );
38         while ( valid )
39         {
40                 gchar* data;
41                 gtk_tree_model_get( GTK_TREE_MODEL( list ), &iter, 0, &data, -1 );
42                 globalOutputStream() << data << "\n";
43                 ignore.insert( std::string( data ) );
44                 g_free( data );
45                 valid = gtk_tree_model_iter_next( GTK_TREE_MODEL( list ), &iter );
46         }
47
48         for ( std::set<std::string>::iterator it( ignore.begin() ); it != ignore.end(); ++it )
49                 globalOutputStream() << it->c_str() << "\n";
50
51         // collapse mode
52         collapsemode mode = COLLAPSE_NONE;
53
54         GtkWidget* radio = lookup_widget( GTK_WIDGET( button ), "r_collapse" );
55         ASSERT_NOTNULL( radio );
56
57         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( radio ) ) ) {
58                 mode = COLLAPSE_ALL;
59         }
60         else
61         {
62                 radio = lookup_widget( GTK_WIDGET( button ), "r_collapsebymaterial" );
63                 ASSERT_NOTNULL( radio );
64                 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( radio ) ) ) {
65                         mode = COLLAPSE_BY_MATERIAL;
66                 }
67                 else
68                 {
69                         radio = lookup_widget( GTK_WIDGET( button ), "r_nocollapse" );
70                         ASSERT_NOTNULL( radio );
71                         ASSERT_NOTNULL( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( radio ) ) );
72                         mode = COLLAPSE_NONE;
73                 }
74         }
75
76         // export materials?
77         GtkWidget* toggle = lookup_widget( GTK_WIDGET( button ), "t_exportmaterials" );
78         ASSERT_NOTNULL( toggle );
79
80         bool exportmat = FALSE;
81
82         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( toggle ) ) ) {
83                 exportmat = TRUE;
84         }
85
86         // limit material names?
87         toggle = lookup_widget( GTK_WIDGET( button ), "t_limitmatnames" );
88         ASSERT_NOTNULL( toggle );
89
90         bool limitMatNames = FALSE;
91
92         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( toggle ) ) && exportmat ) {
93                 limitMatNames = TRUE;
94         }
95
96         // create objects instead of groups?
97         toggle = lookup_widget( GTK_WIDGET( button ), "t_objects" );
98         ASSERT_NOTNULL( toggle );
99
100         bool objects = FALSE;
101
102         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( toggle ) )  && exportmat ) {
103                 objects = TRUE;
104         }
105
106         // export
107         ExportSelection( ignore, mode, exportmat, path, limitMatNames, objects );
108 }
109
110 void OnAddMaterial( GtkButton* button, gpointer user_data ){
111         GtkEntry* edit = GTK_ENTRY( lookup_widget( GTK_WIDGET( button ), "ed_materialname" ) );
112         ASSERT_NOTNULL( edit );
113
114         const gchar* name = gtk_entry_get_text( edit );
115         if ( g_utf8_strlen( name, -1 ) > 0 ) {
116                 GtkListStore* list = GTK_LIST_STORE( gtk_tree_view_get_model( GTK_TREE_VIEW( lookup_widget( GTK_WIDGET( button ), "t_materialist" ) ) ) );
117                 GtkTreeIter iter;
118                 gtk_list_store_append( list, &iter );
119                 gtk_list_store_set( list, &iter, 0, name, -1 );
120                 gtk_entry_set_text( edit, "" );
121         }
122 }
123
124 void OnRemoveMaterial( GtkButton* button, gpointer user_data ){
125         GtkTreeView* view = GTK_TREE_VIEW( lookup_widget( GTK_WIDGET( button ), "t_materialist" ) );
126         GtkListStore* list = GTK_LIST_STORE( gtk_tree_view_get_model( view ) );
127         GtkTreeSelection* sel = gtk_tree_view_get_selection( view );
128
129         GtkTreeIter iter;
130         if ( gtk_tree_selection_get_selected( sel, 0, &iter ) ) {
131                 gtk_list_store_remove( list, &iter );
132         }
133 }
134
135 void OnExportMatClicked( GtkButton* button, gpointer user_data ){
136         GtkWidget* toggleLimit = lookup_widget( GTK_WIDGET( button ), "t_limitmatnames" );
137         GtkWidget* toggleObject = lookup_widget( GTK_WIDGET( button ), "t_objects" );
138
139         if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( button ) ) ) {
140                 gtk_widget_set_sensitive( GTK_WIDGET( toggleLimit ), TRUE );
141                 gtk_widget_set_sensitive( GTK_WIDGET( toggleObject ), TRUE );
142         }
143         else {
144                 gtk_widget_set_sensitive( GTK_WIDGET( toggleLimit ), FALSE );
145                 gtk_widget_set_sensitive( GTK_WIDGET( toggleObject ), FALSE );
146         }
147 }
148
149 } // callbacks