]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/prtview/prtview.cpp
Merge commit 'd079351041fe957ab2cf6b36dab38e5546003dc3' into garux-merge
[xonotic/netradiant.git] / contrib / prtview / prtview.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
21 #include "prtview.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <uilib/uilib.h>
26
27 #include "profile/profile.h"
28
29 #include "qerplugin.h"
30 #include "iscenegraph.h"
31 #include "iglrender.h"
32 #include "iplugin.h"
33 #include "stream/stringstream.h"
34
35 #include "portals.h"
36 #include "AboutDialog.h"
37 #include "ConfigDialog.h"
38 #include "LoadPortalFileDialog.h"
39
40 #define Q3R_CMD_SPLITTER "-"
41 #define Q3R_CMD_ABOUT "About Portal Viewer"
42 #define Q3R_CMD_LOAD "Load .prt file"
43 #define Q3R_CMD_RELEASE "Unload .prt file"
44 #define Q3R_CMD_SHOW_3D "Toggle portals (3D)"
45 #define Q3R_CMD_SHOW_2D "Toggle portals (2D)"
46 #define Q3R_CMD_OPTIONS "Configure Portal Viewer"
47
48 CopiedString INIfn;
49
50 /////////////////////////////////////////////////////////////////////////////
51 // CPrtViewApp construction
52
53 const char *RENDER_2D = "Render2D";
54 const char *WIDTH_2D = "Width2D";
55 const char *AA_2D = "AntiAlias2D";
56 const char *COLOR_2D = "Color2D";
57
58 const char *RENDER_3D = "Render3D";
59 const char *WIDTH_3D = "Width3D";
60 const char *AA_3D = "AntiAlias3D";
61 const char *COLOR_3D = "Color3D";
62 const char *COLOR_FOG = "ColorFog";
63 const char *FOG = "Fog";
64 const char *ZBUFFER = "ZBuffer";
65 const char *POLYGON = "Polygons";
66 const char *LINE = "Lines";
67 const char *TRANS_3D = "Transparency";
68 const char *CLIP_RANGE = "ClipRange";
69 const char *CLIP = "Clip";
70
71
72 void PrtView_construct(){
73         StringOutputStream tmp( 64 );
74         tmp << GlobalRadiant().getSettingsPath() << "prtview.ini";
75         INIfn = tmp.c_str();
76
77         portals.show_2d = INIGetInt( RENDER_2D, FALSE ) ? true : false;
78         portals.aa_2d = INIGetInt( AA_2D, FALSE ) ? true : false;
79         portals.width_2d = (float)INIGetInt( WIDTH_2D, 10 );
80         portals.color_2d = (PackedColour)INIGetInt( COLOR_2D, RGB( 0, 0, 255 ) ) & 0xFFFFFF;
81
82         if ( portals.width_2d > 40.0f ) {
83                 portals.width_2d = 40.0f;
84         }
85         else if ( portals.width_2d < 2.0f ) {
86                 portals.width_2d = 2.0f;
87         }
88
89         portals.show_3d = INIGetInt( RENDER_3D, TRUE ) ? true : false;
90
91         portals.zbuffer = INIGetInt( ZBUFFER, 1 );
92         portals.fog = INIGetInt( FOG, FALSE ) ? true : false;
93         portals.polygons = INIGetInt( POLYGON, TRUE );
94         portals.lines = INIGetInt( LINE, TRUE );
95         portals.aa_3d = INIGetInt( AA_3D, FALSE ) ? true : false;
96         portals.width_3d = (float)INIGetInt( WIDTH_3D, 4 );
97         portals.color_3d = (PackedColour)INIGetInt( COLOR_3D, RGB( 255, 255, 0 ) ) & 0xFFFFFF;
98         portals.color_fog = (PackedColour)INIGetInt( COLOR_FOG, RGB( 127, 127, 127 ) ) & 0xFFFFFF;
99         portals.trans_3d = (float)INIGetInt( TRANS_3D, 50 );
100         portals.clip = INIGetInt( CLIP, FALSE ) ? true : false;
101         portals.clip_range = (float)INIGetInt( CLIP_RANGE, 16 );
102
103         if ( portals.clip_range < 1 ) {
104                 portals.clip_range = 1;
105         }
106         else if ( portals.clip_range > 128 ) {
107                 portals.clip_range = 128;
108         }
109
110         if ( portals.zbuffer < 0 ) {
111                 portals.zbuffer = 0;
112         }
113         else if ( portals.zbuffer > 2 ) {
114                 portals.zbuffer = 0;
115         }
116
117         if ( portals.width_3d > 40.0f ) {
118                 portals.width_3d = 40.0f;
119         }
120         else if ( portals.width_3d < 2.0f ) {
121                 portals.width_3d = 2.0f;
122         }
123
124         if ( portals.trans_3d > 100.0f ) {
125                 portals.trans_3d = 100.0f;
126         }
127         else if ( portals.trans_3d < 0.0f ) {
128                 portals.trans_3d = 0.0f;
129         }
130
131         SaveConfig();
132
133         portals.FixColors();
134
135         Portals_constructShaders();
136         GlobalShaderCache().attachRenderable( render );
137 }
138
139 void PrtView_destroy(){
140         GlobalShaderCache().detachRenderable( render );
141         Portals_destroyShaders();
142 }
143
144 void SaveConfig(){
145         INISetInt( RENDER_2D, portals.show_2d, "Draw in 2D windows" );
146         INISetInt( WIDTH_2D, (int)portals.width_2d, "Width of lines in 2D windows (in units of 1/2)" );
147         INISetInt( COLOR_2D, (int)portals.color_2d, "Color of lines in 2D windows" );
148         INISetInt( AA_2D, portals.aa_2d, "Draw lines in 2D window anti-aliased" );
149
150         INISetInt( ZBUFFER, portals.zbuffer, "ZBuffer level in 3D window" );
151         INISetInt( FOG, portals.fog, "Use depth cueing in 3D window" );
152         INISetInt( POLYGON, portals.polygons, "Render using polygons polygons in 3D window" );
153         INISetInt( LINE, portals.polygons, "Render using lines in 3D window" );
154         INISetInt( RENDER_3D, portals.show_3d, "Draw in 3D windows" );
155         INISetInt( WIDTH_3D, (int)portals.width_3d, "Width of lines in 3D window (in units of 1/2)" );
156         INISetInt( COLOR_3D, (int)portals.color_3d, "Color of lines/polygons in 3D window" );
157         INISetInt( COLOR_FOG, (int)portals.color_fog, "Color of distant lines/polygons in 3D window" );
158         INISetInt( AA_3D, portals.aa_3d, "Draw lines in 3D window anti-aliased" );
159         INISetInt( TRANS_3D, (int)portals.trans_3d, "Transparency in 3d view (0 = solid, 100 = invisible)" );
160         INISetInt( CLIP, portals.clip, "Cubic clipper active for portal viewer" );
161         INISetInt( CLIP_RANGE, (int)portals.clip_range, "Portal viewer cubic clip distance (in units of 64)" );
162 }
163
164
165 const char *CONFIG_SECTION = "Configuration";
166
167 int INIGetInt( const char *key, int def ){
168         char value[1024];
169
170         if ( read_var( INIfn.c_str(), CONFIG_SECTION, key, value ) ) {
171                 return atoi( value );
172         }
173         else{
174                 return def;
175         }
176 }
177
178 void INISetInt( const char *key, int val, const char *comment /* = NULL */ ){
179         char s[1000];
180
181         if ( comment ) {
182                 sprintf( s, "%d        ; %s", val, comment );
183         }
184         else{
185                 sprintf( s, "%d", val );
186         }
187         save_var( INIfn.c_str(), CONFIG_SECTION, key, s );
188 }
189
190
191 // plugin name
192 static const char *PLUGIN_NAME = "Portal Viewer";
193 // commands in the menu
194 static const char *PLUGIN_COMMANDS =
195         Q3R_CMD_ABOUT ";"
196         Q3R_CMD_SPLITTER ";"
197         Q3R_CMD_OPTIONS ";"
198         Q3R_CMD_SPLITTER ";"
199         Q3R_CMD_SHOW_2D ";"
200         Q3R_CMD_SHOW_3D ";"
201         Q3R_CMD_SPLITTER ";"
202         Q3R_CMD_RELEASE ";"
203         Q3R_CMD_LOAD;
204
205
206 ui::Widget g_pRadiantWnd{ui::null};
207
208 const char* QERPlug_Init( void *hApp, void* pMainWidget ){
209         g_pRadiantWnd = ui::Window::from(pMainWidget);
210         return "Portal Viewer for Q3Radiant";
211 }
212
213 const char* QERPlug_GetName(){
214         return PLUGIN_NAME;
215 }
216
217 const char* QERPlug_GetCommandList(){
218         return PLUGIN_COMMANDS;
219 }
220
221
222 const char* QERPlug_GetCommandTitleList(){
223         return "";
224 }
225
226
227 void QERPlug_Dispatch( const char* p, float* vMin, float* vMax, bool bSingleBrush ){
228         globalOutputStream() << MSG_PREFIX "Command \"" << p << "\"\n";
229
230         if ( !strcmp( p,Q3R_CMD_ABOUT ) ) {
231                 DoAboutDlg();
232         }
233         else if ( !strcmp( p,Q3R_CMD_LOAD ) ) {
234                 if ( DoLoadPortalFileDialog() == IDOK ) {
235                         portals.Load();
236                         SceneChangeNotify();
237                 }
238                 else
239                 {
240                         globalOutputStream() << MSG_PREFIX "Portal file load aborted.\n";
241                 }
242         }
243         else if ( !strcmp( p,Q3R_CMD_RELEASE ) ) {
244                 portals.Purge();
245
246                 SceneChangeNotify();
247
248                 globalOutputStream() << MSG_PREFIX "Portals unloaded.\n";
249         }
250         else if ( !strcmp( p,Q3R_CMD_SHOW_2D ) ) {
251                 portals.show_2d = !portals.show_2d;
252
253                 SceneChangeNotify();
254                 SaveConfig();
255
256                 if ( portals.show_2d ) {
257                         globalOutputStream() << MSG_PREFIX "Portals will be rendered in 2D view.\n";
258                 }
259                 else{
260                         globalOutputStream() << MSG_PREFIX "Portals will NOT be rendered in 2D view.\n";
261                 }
262         }
263         else if ( !strcmp( p,Q3R_CMD_SHOW_3D ) ) {
264                 portals.show_3d = !portals.show_3d;
265                 SaveConfig();
266
267                 SceneChangeNotify();
268
269                 if ( portals.show_3d ) {
270                         globalOutputStream() << MSG_PREFIX "Portals will be rendered in 3D view.\n";
271                 }
272                 else{
273                         globalOutputStream() << MSG_PREFIX "Portals will NOT be rendered in 3D view.\n";
274                 }
275         }
276         else if ( !strcmp( p,Q3R_CMD_OPTIONS ) ) {
277                 DoConfigDialog();
278                 SaveConfig();
279
280                 SceneChangeNotify();
281         }
282 }
283
284
285 #include "modulesystem/singletonmodule.h"
286
287 class PrtViewPluginDependencies :
288         public GlobalSceneGraphModuleRef,
289         public GlobalRadiantModuleRef,
290         public GlobalShaderCacheModuleRef,
291         public GlobalOpenGLModuleRef,
292         public GlobalOpenGLStateLibraryModuleRef
293 {
294 };
295
296 class PrtViewPluginModule
297 {
298 _QERPluginTable m_plugin;
299 public:
300 typedef _QERPluginTable Type;
301 STRING_CONSTANT( Name, "prtview" );
302
303 PrtViewPluginModule(){
304         m_plugin.m_pfnQERPlug_Init = QERPlug_Init;
305         m_plugin.m_pfnQERPlug_GetName = QERPlug_GetName;
306         m_plugin.m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
307         m_plugin.m_pfnQERPlug_GetCommandTitleList = QERPlug_GetCommandTitleList;
308         m_plugin.m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
309
310         PrtView_construct();
311 }
312 ~PrtViewPluginModule(){
313         PrtView_destroy();
314 }
315 _QERPluginTable* getTable(){
316         return &m_plugin;
317 }
318 };
319
320 typedef SingletonModule<PrtViewPluginModule, PrtViewPluginDependencies> SingletonPrtViewPluginModule;
321
322 SingletonPrtViewPluginModule g_PrtViewPluginModule;
323
324
325 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules( ModuleServer& server ){
326         initialiseModule( server );
327
328         g_PrtViewPluginModule.selfRegister();
329 }