]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/feedback.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / radiant / feedback.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 //
24 // DESCRIPTION:
25 // classes used for describing geometry information from q3map feedback
26 //
27
28 #include "feedback.h"
29
30 #include <gtk/gtk.h>
31
32 #include "debugging/debugging.h"
33
34 #include "igl.h"
35 #include "iselection.h"
36
37 #include "map.h"
38 #include "dialog.h"
39 #include "mainframe.h"
40
41
42 CDbgDlg g_DbgDlg;
43
44 void Feedback_draw2D(VIEWTYPE viewType)
45 {
46     g_DbgDlg.draw2D(viewType);
47 }
48
49 void CSelectMsg::saxStartElement(message_info_t *ctx, const xmlChar *name, const xmlChar **attrs)
50 {
51     if (string_equal(reinterpret_cast<const char *>( name ), "select")) {
52         // read the message
53         ESelectState = SELECT_MESSAGE;
54     } else {
55         // read the brush
56         ASSERT_MESSAGE(string_equal(reinterpret_cast<const char *>( name ), "brush"), "FEEDBACK PARSE ERROR");
57         ASSERT_MESSAGE(ESelectState == SELECT_MESSAGE, "FEEDBACK PARSE ERROR");
58         ESelectState = SELECT_BRUSH;
59         globalOutputStream() << message.c_str() << '\n';
60     }
61 }
62
63 void CSelectMsg::saxEndElement(message_info_t *ctx, const xmlChar *name)
64 {
65     if (string_equal(reinterpret_cast<const char *>( name ), "select")) {
66     }
67 }
68
69 void CSelectMsg::saxCharacters(message_info_t *ctx, const xmlChar *ch, int len)
70 {
71     if (ESelectState == SELECT_MESSAGE) {
72         message.write(reinterpret_cast<const char *>( ch ), len);
73     } else {
74         brush.write(reinterpret_cast<const char *>( ch ), len);
75     }
76 }
77
78 IGL2DWindow *CSelectMsg::Highlight()
79 {
80     GlobalSelectionSystem().setSelectedAll(false);
81     int entitynum, brushnum;
82     if (sscanf(reinterpret_cast<const char *>( brush.c_str()), "%i %i", &entitynum, &brushnum) == 2) {
83         SelectBrush(entitynum, brushnum);
84     }
85     return 0;
86 }
87
88 void CPointMsg::saxStartElement(message_info_t *ctx, const xmlChar *name, const xmlChar **attrs)
89 {
90     if (string_equal(reinterpret_cast<const char *>( name ), "pointmsg")) {
91         // read the message
92         EPointState = POINT_MESSAGE;
93     } else {
94         // read the brush
95         ASSERT_MESSAGE(string_equal(reinterpret_cast<const char *>( name ), "point"), "FEEDBACK PARSE ERROR");
96         ASSERT_MESSAGE(EPointState == POINT_MESSAGE, "FEEDBACK PARSE ERROR");
97         EPointState = POINT_POINT;
98         globalOutputStream() << message.c_str() << '\n';
99     }
100 }
101
102 void CPointMsg::saxEndElement(message_info_t *ctx, const xmlChar *name)
103 {
104     if (string_equal(reinterpret_cast<const char *>( name ), "pointmsg")) {
105     } else if (string_equal(reinterpret_cast<const char *>( name ), "point")) {
106         sscanf(point.c_str(), "%g %g %g", &(pt[0]), &(pt[1]), &(pt[2]));
107         point.clear();
108     }
109 }
110
111 void CPointMsg::saxCharacters(message_info_t *ctx, const xmlChar *ch, int len)
112 {
113     if (EPointState == POINT_MESSAGE) {
114         message.write(reinterpret_cast<const char *>( ch ), len);
115     } else {
116         ASSERT_MESSAGE(EPointState == POINT_POINT, "FEEDBACK PARSE ERROR");
117         point.write(reinterpret_cast<const char *>( ch ), len);
118     }
119 }
120
121 IGL2DWindow *CPointMsg::Highlight()
122 {
123     return this;
124 }
125
126 void CPointMsg::DropHighlight()
127 {
128 }
129
130 void CPointMsg::Draw2D(VIEWTYPE vt)
131 {
132     int nDim1 = (vt == YZ) ? 1 : 0;
133     int nDim2 = (vt == XY) ? 1 : 2;
134     glPointSize(4);
135     glColor3f(1.0f, 0.0f, 0.0f);
136     glBegin(GL_POINTS);
137     glVertex2f(pt[nDim1], pt[nDim2]);
138     glEnd();
139     glBegin(GL_LINE_LOOP);
140     glVertex2f(pt[nDim1] - 8, pt[nDim2] - 8);
141     glVertex2f(pt[nDim1] + 8, pt[nDim2] - 8);
142     glVertex2f(pt[nDim1] + 8, pt[nDim2] + 8);
143     glVertex2f(pt[nDim1] - 8, pt[nDim2] + 8);
144     glEnd();
145 }
146
147 void CWindingMsg::saxStartElement(message_info_t *ctx, const xmlChar *name, const xmlChar **attrs)
148 {
149     if (string_equal(reinterpret_cast<const char *>( name ), "windingmsg")) {
150         // read the message
151         EPointState = WINDING_MESSAGE;
152     } else {
153         // read the brush
154         ASSERT_MESSAGE(string_equal(reinterpret_cast<const char *>( name ), "winding"), "FEEDBACK PARSE ERROR");
155         ASSERT_MESSAGE(EPointState == WINDING_MESSAGE, "FEEDBACK PARSE ERROR");
156         EPointState = WINDING_WINDING;
157         globalOutputStream() << message.c_str() << '\n';
158     }
159 }
160
161 void CWindingMsg::saxEndElement(message_info_t *ctx, const xmlChar *name)
162 {
163     if (string_equal(reinterpret_cast<const char *>( name ), "windingmsg")) {
164     } else if (string_equal(reinterpret_cast<const char *>( name ), "winding")) {
165         const char *c = winding.c_str();
166         sscanf(c, "%i ", &numpoints);
167
168         int i = 0;
169         for (; i < numpoints; i++) {
170             c = strchr(c + 1, '(');
171             if (c) { // even if we are given the number of points when the cycle begins .. don't trust it too much
172                 sscanf(c, "(%g %g %g)", &wt[i][0], &wt[i][1], &wt[i][2]);
173             } else {
174                 break;
175             }
176         }
177         numpoints = i;
178     }
179 }
180
181 void CWindingMsg::saxCharacters(message_info_t *ctx, const xmlChar *ch, int len)
182 {
183     if (EPointState == WINDING_MESSAGE) {
184         message.write(reinterpret_cast<const char *>( ch ), len);
185     } else {
186         ASSERT_MESSAGE(EPointState == WINDING_WINDING, "FEEDBACK PARSE ERROR");
187         winding.write(reinterpret_cast<const char *>( ch ), len);
188     }
189 }
190
191 IGL2DWindow *CWindingMsg::Highlight()
192 {
193     return this;
194 }
195
196 void CWindingMsg::DropHighlight()
197 {
198 }
199
200 void CWindingMsg::Draw2D(VIEWTYPE vt)
201 {
202     int i;
203
204     int nDim1 = (vt == YZ) ? 1 : 0;
205     int nDim2 = (vt == XY) ? 1 : 2;
206     glColor3f(1.0f, 0.f, 0.0f);
207
208     glPointSize(4);
209     glBegin(GL_POINTS);
210     for (i = 0; i < numpoints; i++)
211         glVertex2f(wt[i][nDim1], wt[i][nDim2]);
212     glEnd();
213     glPointSize(1);
214
215     glEnable(GL_BLEND);
216     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
217     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
218     glColor4f(0.133f, 0.4f, 1.0f, 0.5f);
219     glBegin(GL_POLYGON);
220     for (i = 0; i < numpoints; i++)
221         glVertex2f(wt[i][nDim1], wt[i][nDim2]);
222     glEnd();
223     glDisable(GL_BLEND);
224 }
225
226 // triggered when the user selects an entry in the feedback box
227 static void feedback_selection_changed(ui::TreeSelection selection, gpointer data)
228 {
229     g_DbgDlg.DropHighlight();
230
231     GtkTreeModel *model;
232     GtkTreeIter selected;
233     if (gtk_tree_selection_get_selected(selection, &model, &selected)) {
234         auto path = gtk_tree_model_get_path(model, &selected);
235         g_DbgDlg.SetHighlight(gtk_tree_path_get_indices(path)[0]);
236         gtk_tree_path_free(path);
237     }
238 }
239
240 void CDbgDlg::DropHighlight()
241 {
242     if (m_pHighlight != 0) {
243         m_pHighlight->DropHighlight();
244         m_pHighlight = 0;
245         m_pDraw2D = 0;
246     }
247 }
248
249 void CDbgDlg::SetHighlight(gint row)
250 {
251     ISAXHandler *h = GetElement(row);
252     if (h != NULL) {
253         m_pDraw2D = h->Highlight();
254         m_pHighlight = h;
255     }
256 }
257
258 ISAXHandler *CDbgDlg::GetElement(std::size_t row)
259 {
260     return static_cast<ISAXHandler *>(g_ptr_array_index(m_pFeedbackElements, gint( row )) );
261 }
262
263 void CDbgDlg::Init()
264 {
265     DropHighlight();
266
267     // free all the ISAXHandler*, clean it
268     while (m_pFeedbackElements->len) {
269         static_cast<ISAXHandler *>(g_ptr_array_index(m_pFeedbackElements, 0) )->Release();
270         g_ptr_array_remove_index(m_pFeedbackElements, 0);
271     }
272
273     if (m_clist) {
274         m_clist.clear();
275     }
276 }
277
278 void CDbgDlg::Push(ISAXHandler *pHandler)
279 {
280     // push in the list
281     g_ptr_array_add(m_pFeedbackElements, (void *) pHandler);
282
283     if (!GetWidget()) {
284         Create();
285     }
286
287     // put stuff in the list
288     m_clist.clear();
289     for (std::size_t i = 0; i < static_cast<std::size_t>( m_pFeedbackElements->len ); ++i) {
290         m_clist.append(0, GetElement(i)->getName());
291     }
292
293     ShowDlg();
294 }
295
296 ui::Window CDbgDlg::BuildDialog()
297 {
298     auto window = MainFrame_getWindow().create_floating_window("Q3Map debug window");
299
300     auto scr = ui::ScrolledWindow(ui::New);
301     scr.show();
302     window.add(scr);
303     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scr), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
304     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scr), GTK_SHADOW_IN);
305
306     {
307         ui::ListStore store = ui::ListStore::from(gtk_list_store_new(1, G_TYPE_STRING));
308
309         auto view = ui::TreeView(ui::TreeModel::from(store._handle));
310         gtk_tree_view_set_headers_visible(view, FALSE);
311
312         {
313             auto renderer = ui::CellRendererText(ui::New);
314             auto column = ui::TreeViewColumn("", renderer, {{"text", 0}});
315             gtk_tree_view_append_column(view, column);
316         }
317
318         {
319             auto selection = ui::TreeSelection::from(gtk_tree_view_get_selection(view));
320             gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
321             selection.connect("changed", G_CALLBACK(feedback_selection_changed), NULL);
322         }
323
324         view.show();
325
326         scr.add(view);
327
328         store.unref();
329
330         m_clist = store;
331     }
332
333     return window;
334 }