transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / radiant / surfaceplugin.cpp
1 /*\r
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \r
7 GtkRadiant is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 GtkRadiant is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with GtkRadiant; if not, write to the Free Software\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
20 */\r
21 \r
22 //-----------------------------------------------------------------------------\r
23 //\r
24 // DESCRIPTION:\r
25 // implementation of isurfaceplugin-interface specifics\r
26 \r
27 #include "stdafx.h"\r
28 \r
29 void QERApp_GetTwoSelectedPatch( patchMesh_t **p1, patchMesh_t **p2 )\r
30 {\r
31   *p1 = NULL; *p2 = NULL;\r
32   for (brush_t *pb = selected_brushes.next ; pb != &selected_brushes ; pb = pb->next)\r
33   {\r
34     if (pb->patchBrush)\r
35     {\r
36       if (!(*p1))\r
37         *p1 = pb->pPatch;\r
38       else if (!(*p2))\r
39       {\r
40         *p2 = pb->pPatch;\r
41         return;\r
42       }\r
43     }\r
44   }\r
45 #ifdef _DEBUG\r
46   Sys_Printf("WARNING: QERApp_GetTwoSelectedPatch failed (did not find two patches)\n");\r
47 #endif\r
48   return;\r
49 }\r
50 \r
51 // Nurail: The following functions are used by the Surface Inspector module\r
52 \r
53 // Queries the number of faces from selected brushes\r
54 int SI_GetSelectedFaceCountfromBrushes(void)\r
55 {\r
56   face_t        *f;\r
57   brush_t       *b;\r
58   int           num_of_faces = 0;\r
59 \r
60   if(selected_brushes.next == &selected_brushes)\r
61     return(0);\r
62 \r
63   for(b=selected_brushes.next; b!=&selected_brushes; b=b->next)\r
64     if (!(b->patchBrush))\r
65       for(f=b->brush_faces; f ; f=f->next, num_of_faces++);\r
66 \r
67   return num_of_faces;\r
68 }\r
69 \r
70 void SI_GetSelFacesTexdef(texdef_to_face_t *allocd_block_texdef)\r
71 {\r
72   int           i;\r
73   face_t        *f;\r
74   brush_t       *b;\r
75   texdef_to_face_t *position, *prev_pos;\r
76 \r
77   if(selected_brushes.next != &selected_brushes)\r
78   {\r
79     prev_pos = position = allocd_block_texdef;\r
80     for(b=selected_brushes.next; b!=&selected_brushes; b=b->next)\r
81     {\r
82       if ( !(b->patchBrush) )\r
83       {\r
84         for(f=b->brush_faces; f ; f = f->next)\r
85         {\r
86             position->face = f;\r
87             position->brush = b;\r
88             position->texdef = f->texdef;\r
89             position->orig_texdef = f->texdef;\r
90             prev_pos->next = position;\r
91             prev_pos = position;\r
92             position++;\r
93               }\r
94               prev_pos->next = NULL;\r
95       }\r
96     }\r
97   }\r
98   else if(g_ptrSelectedFaces.GetSize() != 0)\r
99   {\r
100     f = (face_t *) g_ptrSelectedFaces.GetAt(0);\r
101     b = (brush_t *) g_ptrSelectedFaceBrushes.GetAt(0);\r
102     position = (texdef_to_face_t*) allocd_block_texdef;\r
103     position->face = f;\r
104     position->brush = b;\r
105     position->texdef = f->texdef;\r
106     position->orig_texdef = f->texdef;\r
107     prev_pos = position;\r
108     for(i=1; i<g_ptrSelectedFaces.GetSize(); i++)\r
109     {\r
110       f = (face_t *) g_ptrSelectedFaces.GetAt(i);\r
111       b = (brush_t *) g_ptrSelectedFaceBrushes.GetAt(i);\r
112       position = allocd_block_texdef + i;\r
113       position->face = f;\r
114       position->brush = b;\r
115       position->texdef = f->texdef;\r
116       position->orig_texdef = f->texdef;\r
117       prev_pos->next = position;\r
118       prev_pos = position;\r
119     }\r
120     position->next = NULL;\r
121   }\r
122 \r
123 }\r
124 \r
125 /*\r
126 SetFaceTexdef_Q2\r
127 \r
128 This doesn't mess with CONTENTS_DETAIL needed for Quake2 content flag\r
129 \r
130 */\r
131 void SetFaceTexdef_Q2 (face_t *f, texdef_t *texdef, bool bFitScale)\r
132 {\r
133 \r
134   if(strcmp(f->texdef.GetName(), texdef->GetName()) != 0) // set shader here instead of Brush_Build\r
135     Face_SetShader(f, texdef->GetName());\r
136 \r
137                 if (bFitScale)\r
138                 {\r
139                         f->texdef = *texdef;\r
140                         // fit the scaling of the texture on the actual plane\r
141                         vec3_t p1,p2,p3; // absolute coordinates\r
142                         // compute absolute coordinates\r
143                         ComputeAbsolute(f,p1,p2,p3);\r
144                         // compute the scale\r
145                         vec3_t vx,vy;\r
146                         VectorSubtract(p2,p1,vx);\r
147                         VectorNormalize(vx, vx);\r
148                         VectorSubtract(p3,p1,vy);\r
149                         VectorNormalize(vy, vy);\r
150                         // assign scale\r
151                         VectorScale(vx,texdef->scale[0],vx);\r
152                         VectorScale(vy,texdef->scale[1],vy);\r
153                         VectorAdd(p1,vx,p2);\r
154                         VectorAdd(p1,vy,p3);\r
155                         // compute back shift scale rot\r
156                         AbsoluteToLocal(f->plane,f,p1,p2,p3);\r
157                 }\r
158                 else\r
159     {\r
160                         f->texdef = *texdef;\r
161     }\r
162   }\r
163 \r
164 \r
165 \r
166 void SI_SetTexdef_FaceList(texdef_to_face_t* texdef_face_list, bool b_SetUndoPoint, bool bFit_to_Scale)\r
167 {\r
168   texdef_to_face_t* texdef_to_face;\r
169   bool b_isQuake2;\r
170 \r
171   if ( ( g_pGameDescription->mGameFile == "q2.game" ) || ( g_pGameDescription->mGameFile == "heretic2.game" ) )\r
172     b_isQuake2 = true;\r
173   else\r
174     b_isQuake2 = false;\r
175 \r
176   if (!texdef_face_list)\r
177     return;\r
178     \r
179  if (b_SetUndoPoint)\r
180  {\r
181     if(g_ptrSelectedFaces.GetSize() > 1)\r
182       Sys_FPrintf(SYS_WRN, "WARNING: Undo NOT supported for multiple face selections\n");\r
183     else if( (selected_brushes.next != &selected_brushes) || (g_ptrSelectedFaces.GetSize() == 1))\r
184     {\r
185       // Give something to undo to\r
186       for(texdef_to_face = texdef_face_list; texdef_to_face; texdef_to_face = texdef_to_face->next)\r
187       if (b_isQuake2)\r
188         SetFaceTexdef_Q2(texdef_to_face->face, &texdef_to_face->orig_texdef, bFit_to_Scale);\r
189       else\r
190         SetFaceTexdef(texdef_to_face->face, &texdef_to_face->orig_texdef, NULL);\r
191 \r
192       Undo_Start("set facelist texdefs");\r
193       \r
194       if( selected_brushes.next != &selected_brushes )\r
195         Undo_AddBrushList(&selected_brushes); \r
196       else\r
197         Undo_AddBrush(texdef_face_list->brush);\r
198       \r
199     }\r
200  }  \r
201     \r
202   for(texdef_to_face = texdef_face_list; texdef_to_face; texdef_to_face = texdef_to_face->next)\r
203   {\r
204     if (b_isQuake2)\r
205       SetFaceTexdef_Q2(texdef_to_face->face, &texdef_to_face->texdef,  bFit_to_Scale);\r
206     else\r
207       SetFaceTexdef(texdef_to_face->face, &texdef_to_face->texdef, NULL , bFit_to_Scale);\r
208     Brush_Build(texdef_to_face->brush); \r
209     if(bFit_to_Scale)\r
210       texdef_to_face->texdef = texdef_to_face->face->texdef;\r
211   }\r
212   \r
213   if ( b_SetUndoPoint )\r
214   {\r
215       if( (selected_brushes.next != &selected_brushes) || (g_ptrSelectedFaces.GetSize() == 1) )\r
216       {\r
217         if(selected_brushes.next != &selected_brushes)\r
218           Undo_EndBrushList(&selected_brushes); \r
219         else\r
220           Undo_EndBrush(texdef_face_list->brush);\r
221           \r
222         Undo_End();\r
223         // Over-write the orig_texdef list, cementing the change.\r
224         for(texdef_to_face = texdef_face_list; texdef_to_face; texdef_to_face = texdef_to_face->next)\r
225           texdef_to_face->orig_texdef = texdef_to_face->texdef;\r
226       }\r
227   }\r
228   \r
229   Sys_UpdateWindows (W_ALL);\r
230 }\r
231 \r
232 void SI_FaceList_FitTexture(texdef_to_face_t* si_texdef_face_list, int nHeight, int nWidth)\r
233 {\r
234   texdef_to_face_t* temp_texdef_face_list;\r
235 \r
236   if (!si_texdef_face_list)\r
237     return;\r
238 \r
239   for (temp_texdef_face_list = si_texdef_face_list; temp_texdef_face_list; temp_texdef_face_list = temp_texdef_face_list->next)\r
240   {\r
241     Face_FitTexture(temp_texdef_face_list->face, nHeight, nWidth);\r
242     Brush_Build(temp_texdef_face_list->brush,true,true,false,false); \r
243     // Write changes to our working Texdef list\r
244     temp_texdef_face_list->texdef = temp_texdef_face_list->face->texdef;\r
245   }\r
246 \r
247   Sys_UpdateWindows (W_CAMERA);\r
248 \r
249 }\r
250 \r
251 GtkWindow* SI_GetMainWindow(void)\r
252 {\r
253   return GTK_WINDOW(g_qeglobals_gui.d_main_window);\r
254 }\r
255 \r
256 void SI_SetWinPos_from_Prefs(GtkWidget *win)\r
257 {\r
258   load_window_pos (win, g_PrefsDlg.mWindowInfo.posSurfaceWnd);\r
259 }\r