]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/entity.cpp
GTK: wrap GTK_WIDGET
[xonotic/netradiant.git] / radiant / entity.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 #include "entity.h"
23
24 #include "ientity.h"
25 #include "iselection.h"
26 #include "imodel.h"
27 #include "ifilesystem.h"
28 #include "iundo.h"
29 #include "editable.h"
30
31 #include "eclasslib.h"
32 #include "scenelib.h"
33 #include "os/path.h"
34 #include "os/file.h"
35 #include "stream/stringstream.h"
36 #include "stringio.h"
37
38 #include "gtkutil/filechooser.h"
39 #include "gtkmisc.h"
40 #include "select.h"
41 #include "map.h"
42 #include "preferences.h"
43 #include "gtkdlgs.h"
44 #include "mainframe.h"
45 #include "qe3.h"
46 #include "commands.h"
47
48 #include "uilib/uilib.h"
49
50 struct entity_globals_t
51 {
52         Vector3 color_entity;
53
54         entity_globals_t() :
55                 color_entity( 0.0f, 0.0f, 0.0f ){
56         }
57 };
58
59 entity_globals_t g_entity_globals;
60
61 class EntitySetKeyValueSelected : public scene::Graph::Walker
62 {
63 const char* m_key;
64 const char* m_value;
65 public:
66 EntitySetKeyValueSelected( const char* key, const char* value )
67         : m_key( key ), m_value( value ){
68 }
69 bool pre( const scene::Path& path, scene::Instance& instance ) const {
70         return true;
71 }
72 void post( const scene::Path& path, scene::Instance& instance ) const {
73         Entity* entity = Node_getEntity( path.top() );
74         if ( entity != 0
75                  && ( instance.childSelected() || Instance_getSelectable( instance )->isSelected() ) ) {
76                 entity->setKeyValue( m_key, m_value );
77         }
78 }
79 };
80
81 class EntitySetClassnameSelected : public scene::Graph::Walker
82 {
83 const char* m_classname;
84 public:
85 EntitySetClassnameSelected( const char* classname )
86         : m_classname( classname ){
87 }
88 bool pre( const scene::Path& path, scene::Instance& instance ) const {
89         return true;
90 }
91 void post( const scene::Path& path, scene::Instance& instance ) const {
92         Entity* entity = Node_getEntity( path.top() );
93         if ( entity != 0
94                  && ( instance.childSelected() || Instance_getSelectable( instance )->isSelected() ) ) {
95                 NodeSmartReference node( GlobalEntityCreator().createEntity( GlobalEntityClassManager().findOrInsert( m_classname, node_is_group( path.top() ) ) ) );
96
97                 EntityCopyingVisitor visitor( *Node_getEntity( node ) );
98
99                 entity->forEachKeyValue( visitor );
100
101                 NodeSmartReference child( path.top().get() );
102                 NodeSmartReference parent( path.parent().get() );
103                 Node_getTraversable( parent )->erase( child );
104                 if ( Node_getTraversable( child ) != 0
105                          && Node_getTraversable( node ) != 0
106                          && node_is_group( node ) ) {
107                         parentBrushes( child, node );
108                 }
109                 Node_getTraversable( parent )->insert( node );
110         }
111 }
112 };
113
114 void Scene_EntitySetKeyValue_Selected( const char* key, const char* value ){
115         GlobalSceneGraph().traverse( EntitySetKeyValueSelected( key, value ) );
116 }
117
118 void Scene_EntitySetClassname_Selected( const char* classname ){
119         GlobalSceneGraph().traverse( EntitySetClassnameSelected( classname ) );
120 }
121
122
123 void Entity_ungroupSelected(){
124         if ( GlobalSelectionSystem().countSelected() < 1 ) {
125                 return;
126         }
127
128         UndoableCommand undo( "ungroupSelectedEntities" );
129
130         scene::Path world_path( makeReference( GlobalSceneGraph().root() ) );
131         world_path.push( makeReference( Map_FindOrInsertWorldspawn( g_map ) ) );
132
133         scene::Instance &instance = GlobalSelectionSystem().ultimateSelected();
134         scene::Path path = instance.path();
135
136         if ( !Node_isEntity( path.top() ) ) {
137                 path.pop();
138         }
139
140         if ( Node_getEntity( path.top() ) != 0
141                  && node_is_group( path.top() ) ) {
142                 if ( world_path.top().get_pointer() != path.top().get_pointer() ) {
143                         parentBrushes( path.top(), world_path.top() );
144                         Path_deleteTop( path );
145                 }
146         }
147 }
148
149
150 class EntityFindSelected : public scene::Graph::Walker
151 {
152 public:
153 mutable const scene::Path *groupPath;
154 mutable scene::Instance *groupInstance;
155 EntityFindSelected() : groupPath( 0 ), groupInstance( 0 ){
156 }
157 bool pre( const scene::Path& path, scene::Instance& instance ) const {
158         return true;
159 }
160 void post( const scene::Path& path, scene::Instance& instance ) const {
161         Entity* entity = Node_getEntity( path.top() );
162         if ( entity != 0
163                  && Instance_getSelectable( instance )->isSelected()
164                  && node_is_group( path.top() )
165                  && !groupPath ) {
166                 groupPath = &path;
167                 groupInstance = &instance;
168         }
169 }
170 };
171
172 class EntityGroupSelected : public scene::Graph::Walker
173 {
174 NodeSmartReference group, worldspawn;
175 //typedef std::pair<NodeSmartReference, NodeSmartReference> DeletionPair;
176 //Stack<DeletionPair> deleteme;
177 public:
178 EntityGroupSelected( const scene::Path &p ) : group( p.top().get() ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
179 }
180 bool pre( const scene::Path& path, scene::Instance& instance ) const {
181         return true;
182 }
183 void post( const scene::Path& path, scene::Instance& instance ) const {
184         Selectable *selectable = Instance_getSelectable( instance );
185         if ( selectable && selectable->isSelected() ) {
186                 Entity* entity = Node_getEntity( path.top() );
187                 if ( entity == 0 && Node_isPrimitive( path.top() ) ) {
188                         NodeSmartReference child( path.top().get() );
189                         NodeSmartReference parent( path.parent().get() );
190
191                         if ( path.size() >= 3 && parent != worldspawn ) {
192                                 NodeSmartReference parentparent( path[path.size() - 3].get() );
193
194                                 Node_getTraversable( parent )->erase( child );
195                                 Node_getTraversable( group )->insert( child );
196
197                                 if ( Node_getTraversable( parent )->empty() ) {
198                                         //deleteme.push(DeletionPair(parentparent, parent));
199                                         Node_getTraversable( parentparent )->erase( parent );
200                                 }
201                         }
202                         else
203                         {
204                                 Node_getTraversable( parent )->erase( child );
205                                 Node_getTraversable( group )->insert( child );
206                         }
207                 }
208         }
209 }
210 };
211
212 void Entity_groupSelected(){
213         if ( GlobalSelectionSystem().countSelected() < 1 ) {
214                 return;
215         }
216
217         UndoableCommand undo( "groupSelectedEntities" );
218
219         scene::Path world_path( makeReference( GlobalSceneGraph().root() ) );
220         world_path.push( makeReference( Map_FindOrInsertWorldspawn( g_map ) ) );
221
222         EntityFindSelected fse;
223         GlobalSceneGraph().traverse( fse );
224         if ( fse.groupPath ) {
225                 GlobalSceneGraph().traverse( EntityGroupSelected( *fse.groupPath ) );
226         }
227         else
228         {
229                 GlobalSceneGraph().traverse( EntityGroupSelected( world_path ) );
230         }
231 }
232
233
234
235 void Entity_connectSelected(){
236         if ( GlobalSelectionSystem().countSelected() == 2 ) {
237                 GlobalEntityCreator().connectEntities(
238                         GlobalSelectionSystem().penultimateSelected().path(),
239                         GlobalSelectionSystem().ultimateSelected().path(),
240                         0
241                         );
242         }
243         else
244         {
245                 globalErrorStream() << "entityConnectSelected: exactly two instances must be selected\n";
246         }
247 }
248
249 void Entity_killconnectSelected(){
250         if ( GlobalSelectionSystem().countSelected() == 2 ) {
251                 GlobalEntityCreator().connectEntities(
252                         GlobalSelectionSystem().penultimateSelected().path(),
253                         GlobalSelectionSystem().ultimateSelected().path(),
254                         1
255                         );
256         }
257         else
258         {
259                 globalErrorStream() << "entityKillConnectSelected: exactly two instances must be selected\n";
260         }
261 }
262
263 AABB Doom3Light_getBounds( const AABB& workzone ){
264         AABB aabb( workzone );
265
266         Vector3 defaultRadius( 300, 300, 300 );
267         if ( !string_parse_vector3( EntityClass_valueForKey( *GlobalEntityClassManager().findOrInsert( "light", false ), "light_radius" ), defaultRadius ) ) {
268                 globalErrorStream() << "Doom3Light_getBounds: failed to parse default light radius\n";
269         }
270
271         if ( aabb.extents[0] == 0 ) {
272                 aabb.extents[0] = defaultRadius[0];
273         }
274         if ( aabb.extents[1] == 0 ) {
275                 aabb.extents[1] = defaultRadius[1];
276         }
277         if ( aabb.extents[2] == 0 ) {
278                 aabb.extents[2] = defaultRadius[2];
279         }
280
281         if ( aabb_valid( aabb ) ) {
282                 return aabb;
283         }
284         return AABB( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
285 }
286
287 int g_iLastLightIntensity;
288
289 void Entity_createFromSelection( const char* name, const Vector3& origin ){
290 #if 0
291         if ( string_equal_nocase( name, "worldspawn" ) ) {
292                 ui::alert( MainFrame_getWindow( ), "Can't create an entity with worldspawn.", "info" );
293                 return;
294         }
295 #endif
296
297         EntityClass* entityClass = GlobalEntityClassManager().findOrInsert( name, true );
298
299         bool isModel = ( string_compare_nocase_n( name, "misc_", 5 ) == 0 && string_equal_nocase( name + string_length( name ) - 5, "model" ) ) // misc_*model (also misc_model)
300                                    || string_equal_nocase( name, "model_static" )
301                                    || ( GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase( name, "func_static" ) );
302
303         bool brushesSelected = Scene_countSelectedBrushes( GlobalSceneGraph() ) != 0;
304
305         if ( !( entityClass->fixedsize || isModel ) && !brushesSelected ) {
306                 globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
307                 return;
308         }
309
310         AABB workzone( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
311
312
313         NodeSmartReference node( GlobalEntityCreator().createEntity( entityClass ) );
314
315         Node_getTraversable( GlobalSceneGraph().root() )->insert( node );
316
317         scene::Path entitypath( makeReference( GlobalSceneGraph().root() ) );
318         entitypath.push( makeReference( node.get() ) );
319         scene::Instance& instance = findInstance( entitypath );
320
321         if ( entityClass->fixedsize || ( isModel && !brushesSelected ) ) {
322                 Select_Delete();
323
324                 Transformable* transform = Instance_getTransformable( instance );
325                 if ( transform != 0 ) {
326                         transform->setType( TRANSFORM_PRIMITIVE );
327                         transform->setTranslation( origin );
328                         transform->freezeTransform();
329                 }
330
331                 GlobalSelectionSystem().setSelectedAll( false );
332
333                 Instance_setSelected( instance, true );
334         }
335         else
336         {
337                 if ( g_pGameDescription->mGameType == "doom3" ) {
338                         Node_getEntity( node )->setKeyValue( "model", Node_getEntity( node )->getKeyValue( "name" ) );
339                 }
340
341                 Scene_parentSelectedBrushesToEntity( GlobalSceneGraph(), node );
342                 Scene_forEachChildSelectable( SelectableSetSelected( true ), instance.path() );
343         }
344
345         // tweaking: when right clic dropping a light entity, ask for light value in a custom dialog box
346         // see SF bug 105383
347
348         if ( g_pGameDescription->mGameType == "hl" ) {
349                 // FIXME - Hydra: really we need a combined light AND color dialog for halflife.
350                 if ( string_equal_nocase( name, "light" )
351                          || string_equal_nocase( name, "light_environment" )
352                          || string_equal_nocase( name, "light_spot" ) ) {
353                         int intensity = g_iLastLightIntensity;
354
355                         if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
356                                 g_iLastLightIntensity = intensity;
357                                 char buf[30];
358                                 sprintf( buf, "255 255 255 %d", intensity );
359                                 Node_getEntity( node )->setKeyValue( "_light", buf );
360                         }
361                 }
362         }
363         else if ( string_equal_nocase( name, "light" ) ) {
364                 if ( g_pGameDescription->mGameType != "doom3" ) {
365                         int intensity = g_iLastLightIntensity;
366
367                         if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
368                                 g_iLastLightIntensity = intensity;
369                                 char buf[10];
370                                 sprintf( buf, "%d", intensity );
371                                 Node_getEntity( node )->setKeyValue( "light", buf );
372                         }
373                 }
374                 else if ( brushesSelected ) { // use workzone to set light position/size for doom3 lights, if there are brushes selected
375                         AABB bounds( Doom3Light_getBounds( workzone ) );
376                         StringOutputStream key( 64 );
377                         key << bounds.origin[0] << " " << bounds.origin[1] << " " << bounds.origin[2];
378                         Node_getEntity( node )->setKeyValue( "origin", key.c_str() );
379                         key.clear();
380                         key << bounds.extents[0] << " " << bounds.extents[1] << " " << bounds.extents[2];
381                         Node_getEntity( node )->setKeyValue( "light_radius", key.c_str() );
382                 }
383         }
384
385         if ( isModel ) {
386                 const char* model = misc_model_dialog(MainFrame_getWindow());
387                 if ( model != 0 ) {
388                         Node_getEntity( node )->setKeyValue( "model", model );
389                 }
390         }
391 }
392
393 #if 0
394 bool DoNormalisedColor( Vector3& color ){
395         if ( !color_dialog( MainFrame_getWindow( ), color ) ) {
396                 return false;
397         }
398         /*
399         ** scale colors so that at least one component is at 1.0F
400         */
401
402         float largest = 0.0F;
403
404         if ( color[0] > largest ) {
405                 largest = color[0];
406         }
407         if ( color[1] > largest ) {
408                 largest = color[1];
409         }
410         if ( color[2] > largest ) {
411                 largest = color[2];
412         }
413
414         if ( largest == 0.0F ) {
415                 color[0] = 1.0F;
416                 color[1] = 1.0F;
417                 color[2] = 1.0F;
418         }
419         else
420         {
421                 float scaler = 1.0F / largest;
422
423                 color[0] *= scaler;
424                 color[1] *= scaler;
425                 color[2] *= scaler;
426         }
427
428         return true;
429 }
430 #endif
431
432 void NormalizeColor( Vector3& color ){
433         // scale colors so that at least one component is at 1.0F
434
435         float largest = 0.0F;
436
437         if ( color[0] > largest ) {
438                 largest = color[0];
439         }
440         if ( color[1] > largest ) {
441                 largest = color[1];
442         }
443         if ( color[2] > largest ) {
444                 largest = color[2];
445         }
446
447         if ( largest == 0.0F ) {
448                 color[0] = 1.0F;
449                 color[1] = 1.0F;
450                 color[2] = 1.0F;
451         }
452         else
453         {
454                 float scaler = 1.0F / largest;
455
456                 color[0] *= scaler;
457                 color[1] *= scaler;
458                 color[2] *= scaler;
459         }
460 }
461
462 void Entity_normalizeColor(){
463         if ( GlobalSelectionSystem().countSelected() != 0 ) {
464                 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
465                 Entity* entity = Node_getEntity( path.top() );
466
467                 if ( entity != 0 ) {
468                         const char* strColor = entity->getKeyValue( "_color" );
469                         if ( !string_empty( strColor ) ) {
470                                 Vector3 rgb;
471                                 if ( string_parse_vector3( strColor, rgb ) ) {
472                                         g_entity_globals.color_entity = rgb;
473                                         NormalizeColor( g_entity_globals.color_entity );
474
475                                         char buffer[128];
476                                         sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
477                                                          g_entity_globals.color_entity[1],
478                                                          g_entity_globals.color_entity[2] );
479
480                                         Scene_EntitySetKeyValue_Selected( "_color", buffer );
481                                 }
482                         }
483                 }
484         }
485 }
486
487 void Entity_setColour(){
488         if ( GlobalSelectionSystem().countSelected() != 0 ) {
489                 bool normalize = false;
490                 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
491                 Entity* entity = Node_getEntity( path.top() );
492
493                 if ( entity != 0 ) {
494                         const char* strColor = entity->getKeyValue( "_color" );
495                         if ( !string_empty( strColor ) ) {
496                                 Vector3 rgb;
497                                 if ( string_parse_vector3( strColor, rgb ) ) {
498                                         g_entity_globals.color_entity = rgb;
499                                 }
500                         }
501
502                         if ( g_pGameDescription->mGameType == "doom3" ) {
503                                 normalize = false;
504                         }
505
506                         if ( color_dialog( MainFrame_getWindow(), g_entity_globals.color_entity ) ) {
507                                 if ( normalize ) {
508                                         NormalizeColor( g_entity_globals.color_entity );
509                                 }
510
511                                 char buffer[128];
512                                 sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
513                                                  g_entity_globals.color_entity[1],
514                                                  g_entity_globals.color_entity[2] );
515
516                                 Scene_EntitySetKeyValue_Selected( "_color", buffer );
517                         }
518                 }
519         }
520 }
521
522 const char* misc_model_dialog( ui::Widget parent ){
523         StringOutputStream buffer( 1024 );
524
525         buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
526
527         if ( !file_readable( buffer.c_str() ) ) {
528                 // just go to fsmain
529                 buffer.clear();
530                 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
531         }
532
533         const char *filename = parent.file_dialog( TRUE, "Choose Model", buffer.c_str(), ModelLoader::Name() );
534         if ( filename != 0 ) {
535                 // use VFS to get the correct relative path
536                 const char* relative = path_make_relative( filename, GlobalFileSystem().findRoot( filename ) );
537                 if ( relative == filename ) {
538                         globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
539                 }
540                 return relative;
541         }
542         return 0;
543 }
544
545 void LightRadiiImport( EntityCreator& self, bool value ){
546         self.setLightRadii( value );
547 }
548 typedef ReferenceCaller1<EntityCreator, bool, LightRadiiImport> LightRadiiImportCaller;
549
550 void LightRadiiExport( EntityCreator& self, const BoolImportCallback& importer ){
551         importer( self.getLightRadii() );
552 }
553 typedef ReferenceCaller1<EntityCreator, const BoolImportCallback&, LightRadiiExport> LightRadiiExportCaller;
554
555 void Entity_constructPreferences( PreferencesPage& page ){
556         page.appendCheckBox(
557                 "Show", "Light Radii",
558                 LightRadiiImportCaller( GlobalEntityCreator() ),
559                 LightRadiiExportCaller( GlobalEntityCreator() )
560                 );
561 }
562 void Entity_constructPage( PreferenceGroup& group ){
563         PreferencesPage page( group.createPage( "Entities", "Entity Display Preferences" ) );
564         Entity_constructPreferences( page );
565 }
566 void Entity_registerPreferencesPage(){
567         PreferencesDialog_addDisplayPage( FreeCaller1<PreferenceGroup&, Entity_constructPage>() );
568 }
569
570
571
572 void Entity_constructMenu( ui::Menu menu ){
573         create_menu_item_with_mnemonic( menu, "_Regroup", "GroupSelection" );
574         create_menu_item_with_mnemonic( menu, "_Ungroup", "UngroupSelection" );
575         create_menu_item_with_mnemonic( menu, "_Connect", "ConnectSelection" );
576         create_menu_item_with_mnemonic( menu, "_KillConnect", "KillConnectSelection" );
577         create_menu_item_with_mnemonic( menu, "_Select Color...", "EntityColor" );
578         create_menu_item_with_mnemonic( menu, "_Normalize Color...", "NormalizeColor" );
579 }
580
581
582
583 #include "preferencesystem.h"
584 #include "stringio.h"
585
586 void Entity_Construct(){
587         GlobalCommands_insert( "EntityColor", FreeCaller<Entity_setColour>(), Accelerator( 'K' ) );
588         GlobalCommands_insert( "NormalizeColor", FreeCaller<Entity_normalizeColor>() );
589         GlobalCommands_insert( "ConnectSelection", FreeCaller<Entity_connectSelected>(), Accelerator( 'K', (GdkModifierType)GDK_CONTROL_MASK ) );
590         GlobalCommands_insert( "KillConnectSelection", FreeCaller<Entity_killconnectSelected>(), Accelerator( 'K', (GdkModifierType)( GDK_SHIFT_MASK ) ) );
591         GlobalCommands_insert( "GroupSelection", FreeCaller<Entity_groupSelected>() );
592         GlobalCommands_insert( "UngroupSelection", FreeCaller<Entity_ungroupSelected>() );
593
594         GlobalPreferenceSystem().registerPreference( "SI_Colors5", Vector3ImportStringCaller( g_entity_globals.color_entity ), Vector3ExportStringCaller( g_entity_globals.color_entity ) );
595         GlobalPreferenceSystem().registerPreference( "LastLightIntensity", IntImportStringCaller( g_iLastLightIntensity ), IntExportStringCaller( g_iLastLightIntensity ) );
596
597         Entity_registerPreferencesPage();
598 }
599
600 void Entity_Destroy(){
601 }