2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
25 #include "iselection.h"
27 #include "ifilesystem.h"
31 #include "eclasslib.h"
35 #include "stream/stringstream.h"
38 #include "gtkutil/filechooser.h"
42 #include "preferences.h"
44 #include "mainframe.h"
48 #include "brushmanip.h"
49 #include "patchmanip.h"
51 struct entity_globals_t
56 color_entity( 0.0f, 0.0f, 0.0f ){
60 entity_globals_t g_entity_globals;
62 class EntitySetKeyValueSelected : public scene::Graph::Walker
67 EntitySetKeyValueSelected( const char* key, const char* value )
68 : m_key( key ), m_value( value ){
70 bool pre( const scene::Path& path, scene::Instance& instance ) const {
73 void post( const scene::Path& path, scene::Instance& instance ) const {
74 Entity* entity = Node_getEntity( path.top() );
76 && ( instance.childSelected() || Instance_getSelectable( instance )->isSelected() ) ) {
77 entity->setKeyValue( m_key, m_value );
82 class EntitySetClassnameSelected : public scene::Graph::Walker
84 const char* m_classname;
86 EntitySetClassnameSelected( const char* classname )
87 : m_classname( classname ){
89 bool pre( const scene::Path& path, scene::Instance& instance ) const {
92 void post( const scene::Path& path, scene::Instance& instance ) const {
93 Entity* entity = Node_getEntity( path.top() );
95 && ( instance.childSelected() || Instance_getSelectable( instance )->isSelected() ) ) {
96 NodeSmartReference node( GlobalEntityCreator().createEntity( GlobalEntityClassManager().findOrInsert( m_classname, node_is_group( path.top() ) ) ) );
98 EntityCopyingVisitor visitor( *Node_getEntity( node ) );
100 entity->forEachKeyValue( visitor );
102 NodeSmartReference child( path.top().get() );
103 NodeSmartReference parent( path.parent().get() );
104 Node_getTraversable( parent )->erase( child );
105 if ( Node_getTraversable( child ) != 0
106 && Node_getTraversable( node ) != 0
107 && node_is_group( node ) ) {
108 parentBrushes( child, node );
110 Node_getTraversable( parent )->insert( node );
115 void Scene_EntitySetKeyValue_Selected( const char* key, const char* value ){
116 GlobalSceneGraph().traverse( EntitySetKeyValueSelected( key, value ) );
119 void Scene_EntitySetClassname_Selected( const char* classname ){
120 GlobalSceneGraph().traverse( EntitySetClassnameSelected( classname ) );
124 void Entity_ungroupSelected(){
125 if ( GlobalSelectionSystem().countSelected() < 1 ) {
129 UndoableCommand undo( "ungroupSelectedEntities" );
131 scene::Path world_path( makeReference( GlobalSceneGraph().root() ) );
132 world_path.push( makeReference( Map_FindOrInsertWorldspawn( g_map ) ) );
134 scene::Instance &instance = GlobalSelectionSystem().ultimateSelected();
135 scene::Path path = instance.path();
137 if ( !Node_isEntity( path.top() ) ) {
141 if ( Node_getEntity( path.top() ) != 0
142 && node_is_group( path.top() ) ) {
143 if ( world_path.top().get_pointer() != path.top().get_pointer() ) {
144 parentBrushes( path.top(), world_path.top() );
145 Path_deleteTop( path );
151 class EntityFindSelected : public scene::Graph::Walker
154 mutable const scene::Path *groupPath;
155 mutable scene::Instance *groupInstance;
156 EntityFindSelected() : groupPath( 0 ), groupInstance( 0 ){
158 bool pre( const scene::Path& path, scene::Instance& instance ) const {
161 void post( const scene::Path& path, scene::Instance& instance ) const {
162 Entity* entity = Node_getEntity( path.top() );
164 && Instance_getSelectable( instance )->isSelected()
165 && node_is_group( path.top() )
168 groupInstance = &instance;
173 class EntityGroupSelected : public scene::Graph::Walker
175 NodeSmartReference group, worldspawn;
176 //typedef std::pair<NodeSmartReference, NodeSmartReference> DeletionPair;
177 //Stack<DeletionPair> deleteme;
179 EntityGroupSelected( const scene::Path &p ) : group( p.top().get() ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
181 bool pre( const scene::Path& path, scene::Instance& instance ) const {
184 void post( const scene::Path& path, scene::Instance& instance ) const {
185 Selectable *selectable = Instance_getSelectable( instance );
186 if ( selectable && selectable->isSelected() ) {
187 Entity* entity = Node_getEntity( path.top() );
188 if ( entity == 0 && Node_isPrimitive( path.top() ) ) {
189 NodeSmartReference child( path.top().get() );
190 NodeSmartReference parent( path.parent().get() );
192 if ( path.size() >= 3 && parent != worldspawn ) {
193 NodeSmartReference parentparent( path[path.size() - 3].get() );
195 Node_getTraversable( parent )->erase( child );
196 Node_getTraversable( group )->insert( child );
198 if ( Node_getTraversable( parent )->empty() ) {
199 //deleteme.push(DeletionPair(parentparent, parent));
200 Node_getTraversable( parentparent )->erase( parent );
205 Node_getTraversable( parent )->erase( child );
206 Node_getTraversable( group )->insert( child );
213 void Entity_groupSelected(){
214 if ( GlobalSelectionSystem().countSelected() < 1 ) {
218 UndoableCommand undo( "groupSelectedEntities" );
220 scene::Path world_path( makeReference( GlobalSceneGraph().root() ) );
221 world_path.push( makeReference( Map_FindOrInsertWorldspawn( g_map ) ) );
223 EntityFindSelected fse;
224 GlobalSceneGraph().traverse( fse );
225 if ( fse.groupPath ) {
226 GlobalSceneGraph().traverse( EntityGroupSelected( *fse.groupPath ) );
230 GlobalSceneGraph().traverse( EntityGroupSelected( world_path ) );
236 void Entity_connectSelected(){
237 if ( GlobalSelectionSystem().countSelected() == 2 ) {
238 GlobalEntityCreator().connectEntities(
239 GlobalSelectionSystem().penultimateSelected().path(),
240 GlobalSelectionSystem().ultimateSelected().path(),
246 globalErrorStream() << "entityConnectSelected: exactly two instances must be selected\n";
250 void Entity_killconnectSelected(){
251 if ( GlobalSelectionSystem().countSelected() == 2 ) {
252 GlobalEntityCreator().connectEntities(
253 GlobalSelectionSystem().penultimateSelected().path(),
254 GlobalSelectionSystem().ultimateSelected().path(),
260 globalErrorStream() << "entityKillConnectSelected: exactly two instances must be selected\n";
264 AABB Doom3Light_getBounds( const AABB& workzone ){
265 AABB aabb( workzone );
267 Vector3 defaultRadius( 300, 300, 300 );
268 if ( !string_parse_vector3( EntityClass_valueForKey( *GlobalEntityClassManager().findOrInsert( "light", false ), "light_radius" ), defaultRadius ) ) {
269 globalErrorStream() << "Doom3Light_getBounds: failed to parse default light radius\n";
272 if ( aabb.extents[0] == 0 ) {
273 aabb.extents[0] = defaultRadius[0];
275 if ( aabb.extents[1] == 0 ) {
276 aabb.extents[1] = defaultRadius[1];
278 if ( aabb.extents[2] == 0 ) {
279 aabb.extents[2] = defaultRadius[2];
282 if ( aabb_valid( aabb ) ) {
285 return AABB( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
288 int g_iLastLightIntensity;
290 void Entity_createFromSelection( const char* name, const Vector3& origin ){
292 if ( string_equal_nocase( name, "worldspawn" ) ) {
293 gtk_MessageBox( GTK_WIDGET( MainFrame_getWindow() ), "Can't create an entity with worldspawn.", "info" );
298 EntityClass* entityClass = GlobalEntityClassManager().findOrInsert( name, true );
300 bool isModel = ( string_compare_nocase_n( name, "misc_", 5 ) == 0 && string_equal_nocase( name + string_length( name ) - 5, "model" ) ) // misc_*model (also misc_model)
301 || string_equal_nocase( name, "model_static" )
302 || ( GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase( name, "func_static" ) );
304 bool brushesSelected = Scene_countSelectedBrushes( GlobalSceneGraph() ) != 0;
306 if ( !( entityClass->fixedsize || isModel ) && !brushesSelected ) {
307 globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
311 AABB workzone( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
314 NodeSmartReference node( GlobalEntityCreator().createEntity( entityClass ) );
316 Node_getTraversable( GlobalSceneGraph().root() )->insert( node );
318 scene::Path entitypath( makeReference( GlobalSceneGraph().root() ) );
319 entitypath.push( makeReference( node.get() ) );
320 scene::Instance& instance = findInstance( entitypath );
322 if ( entityClass->fixedsize || ( isModel && !brushesSelected ) ) {
325 Transformable* transform = Instance_getTransformable( instance );
326 if ( transform != 0 ) {
327 transform->setType( TRANSFORM_PRIMITIVE );
328 transform->setTranslation( origin );
329 transform->freezeTransform();
332 GlobalSelectionSystem().setSelectedAll( false );
334 Instance_setSelected( instance, true );
338 if ( g_pGameDescription->mGameType == "doom3" ) {
339 Node_getEntity( node )->setKeyValue( "model", Node_getEntity( node )->getKeyValue( "name" ) );
342 Scene_parentSelectedBrushesToEntity( GlobalSceneGraph(), node );
343 Scene_forEachChildSelectable( SelectableSetSelected( true ), instance.path() );
346 // tweaking: when right clic dropping a light entity, ask for light value in a custom dialog box
349 if ( g_pGameDescription->mGameType == "hl" ) {
350 // FIXME - Hydra: really we need a combined light AND color dialog for halflife.
351 if ( string_equal_nocase( name, "light" )
352 || string_equal_nocase( name, "light_environment" )
353 || string_equal_nocase( name, "light_spot" ) ) {
354 int intensity = g_iLastLightIntensity;
356 if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
357 g_iLastLightIntensity = intensity;
359 sprintf( buf, "255 255 255 %d", intensity );
360 Node_getEntity( node )->setKeyValue( "_light", buf );
364 else if ( string_equal_nocase( name, "light" ) ) {
365 if ( g_pGameDescription->mGameType != "doom3" ) {
366 int intensity = g_iLastLightIntensity;
368 if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
369 g_iLastLightIntensity = intensity;
371 sprintf( buf, "%d", intensity );
372 Node_getEntity( node )->setKeyValue( "light", buf );
375 else if ( brushesSelected ) { // use workzone to set light position/size for doom3 lights, if there are brushes selected
376 AABB bounds( Doom3Light_getBounds( workzone ) );
377 StringOutputStream key( 64 );
378 key << bounds.origin[0] << " " << bounds.origin[1] << " " << bounds.origin[2];
379 Node_getEntity( node )->setKeyValue( "origin", key.c_str() );
381 key << bounds.extents[0] << " " << bounds.extents[1] << " " << bounds.extents[2];
382 Node_getEntity( node )->setKeyValue( "light_radius", key.c_str() );
387 const char* model = misc_model_dialog( GTK_WIDGET( MainFrame_getWindow() ) );
389 Node_getEntity( node )->setKeyValue( "model", model );
393 if ( string_compare_nocase_n( name, "trigger_", 8 ) == 0 && brushesSelected ){
394 const char* shader = g_pGameDescription->getKeyValue( "shader_trigger" );
395 if ( shader && *shader ){
396 Scene_PatchSetShader_Selected( GlobalSceneGraph(), shader );
397 Scene_BrushSetShader_Selected( GlobalSceneGraph(), shader );
400 Scene_PatchSetShader_Selected( GlobalSceneGraph(), "textures/common/trigger" );
401 Scene_BrushSetShader_Selected( GlobalSceneGraph(), "textures/common/trigger" );
407 bool DoNormalisedColor( Vector3& color ){
408 if ( !color_dialog( GTK_WIDGET( MainFrame_getWindow() ), color ) ) {
412 ** scale colors so that at least one component is at 1.0F
415 float largest = 0.0F;
417 if ( color[0] > largest ) {
420 if ( color[1] > largest ) {
423 if ( color[2] > largest ) {
427 if ( largest == 0.0F ) {
434 float scaler = 1.0F / largest;
445 void NormalizeColor( Vector3& color ){
446 // scale colors so that at least one component is at 1.0F
448 float largest = 0.0F;
450 if ( color[0] > largest ) {
453 if ( color[1] > largest ) {
456 if ( color[2] > largest ) {
460 if ( largest == 0.0F ) {
467 float scaler = 1.0F / largest;
475 void Entity_normalizeColor(){
476 if ( GlobalSelectionSystem().countSelected() != 0 ) {
477 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
478 Entity* entity = Node_getEntity( path.top() );
481 const char* strColor = entity->getKeyValue( "_color" );
482 if ( !string_empty( strColor ) ) {
484 if ( string_parse_vector3( strColor, rgb ) ) {
485 g_entity_globals.color_entity = rgb;
486 NormalizeColor( g_entity_globals.color_entity );
489 sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
490 g_entity_globals.color_entity[1],
491 g_entity_globals.color_entity[2] );
493 Scene_EntitySetKeyValue_Selected( "_color", buffer );
500 void Entity_setColour(){
501 if ( GlobalSelectionSystem().countSelected() != 0 ) {
502 bool normalize = false;
503 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
504 Entity* entity = Node_getEntity( path.top() );
507 const char* strColor = entity->getKeyValue( "_color" );
508 if ( !string_empty( strColor ) ) {
510 if ( string_parse_vector3( strColor, rgb ) ) {
511 g_entity_globals.color_entity = rgb;
515 if ( g_pGameDescription->mGameType == "doom3" ) {
519 if ( color_dialog( GTK_WIDGET( MainFrame_getWindow() ), g_entity_globals.color_entity ) ) {
521 NormalizeColor( g_entity_globals.color_entity );
525 sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
526 g_entity_globals.color_entity[1],
527 g_entity_globals.color_entity[2] );
529 Scene_EntitySetKeyValue_Selected( "_color", buffer );
535 const char* misc_model_dialog( GtkWidget* parent ){
536 StringOutputStream buffer( 1024 );
538 buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
540 if ( !file_readable( buffer.c_str() ) ) {
543 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
546 const char *filename = file_dialog( parent, TRUE, "Choose Model", buffer.c_str(), ModelLoader::Name() );
547 if ( filename != 0 ) {
548 // use VFS to get the correct relative path
549 const char* relative = path_make_relative( filename, GlobalFileSystem().findRoot( filename ) );
550 if ( relative == filename ) {
551 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
558 void LightRadiiImport( EntityCreator& self, bool value ){
559 self.setLightRadii( value );
561 typedef ReferenceCaller1<EntityCreator, bool, LightRadiiImport> LightRadiiImportCaller;
563 void LightRadiiExport( EntityCreator& self, const BoolImportCallback& importer ){
564 importer( self.getLightRadii() );
566 typedef ReferenceCaller1<EntityCreator, const BoolImportCallback&, LightRadiiExport> LightRadiiExportCaller;
568 void Entity_constructPreferences( PreferencesPage& page ){
570 "Show", "Light Radii",
571 LightRadiiImportCaller( GlobalEntityCreator() ),
572 LightRadiiExportCaller( GlobalEntityCreator() )
575 void Entity_constructPage( PreferenceGroup& group ){
576 PreferencesPage page( group.createPage( "Entities", "Entity Display Preferences" ) );
577 Entity_constructPreferences( page );
579 void Entity_registerPreferencesPage(){
580 PreferencesDialog_addDisplayPage( FreeCaller1<PreferenceGroup&, Entity_constructPage>() );
585 void Entity_constructMenu( GtkMenu* menu ){
586 create_menu_item_with_mnemonic( menu, "_Regroup", "GroupSelection" );
587 create_menu_item_with_mnemonic( menu, "_Ungroup", "UngroupSelection" );
588 create_menu_item_with_mnemonic( menu, "_Connect", "ConnectSelection" );
589 if ( g_pGameDescription->mGameType == "nexuiz" ) {
590 create_menu_item_with_mnemonic( menu, "_KillConnect", "KillConnectSelection" );
592 create_menu_item_with_mnemonic( menu, "_Select Color...", "EntityColor" );
593 create_menu_item_with_mnemonic( menu, "_Normalize Color...", "NormalizeColor" );
598 #include "preferencesystem.h"
599 #include "stringio.h"
601 void Entity_Construct(){
602 GlobalCommands_insert( "EntityColor", FreeCaller<Entity_setColour>(), Accelerator( 'K' ) );
603 GlobalCommands_insert( "NormalizeColor", FreeCaller<Entity_normalizeColor>() );
604 GlobalCommands_insert( "ConnectSelection", FreeCaller<Entity_connectSelected>(), Accelerator( 'K', (GdkModifierType)GDK_CONTROL_MASK ) );
605 GlobalCommands_insert( "KillConnectSelection", FreeCaller<Entity_killconnectSelected>(), Accelerator( 'K', (GdkModifierType)( GDK_SHIFT_MASK ) ) );
606 GlobalCommands_insert( "GroupSelection", FreeCaller<Entity_groupSelected>() );
607 GlobalCommands_insert( "UngroupSelection", FreeCaller<Entity_ungroupSelected>() );
609 GlobalPreferenceSystem().registerPreference( "SI_Colors5", Vector3ImportStringCaller( g_entity_globals.color_entity ), Vector3ExportStringCaller( g_entity_globals.color_entity ) );
610 GlobalPreferenceSystem().registerPreference( "LastLightIntensity", IntImportStringCaller( g_iLastLightIntensity ), IntExportStringCaller( g_iLastLightIntensity ) );
612 Entity_registerPreferencesPage();
615 void Entity_Destroy(){