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"
43 #include "preferencesystem.h"
46 #include "mainframe.h"
50 #include "brushmanip.h"
51 #include "patchmanip.h"
53 #include "uilib/uilib.h"
55 struct entity_globals_t
60 color_entity( 0.0f, 0.0f, 0.0f ){
64 entity_globals_t g_entity_globals;
66 class EntitySetKeyValueSelected : public scene::Graph::Walker
71 EntitySetKeyValueSelected( const char* key, const char* value )
72 : m_key( key ), m_value( value ){
75 bool pre( const scene::Path& path, scene::Instance& instance ) const {
79 void post( const scene::Path& path, scene::Instance& instance ) const {
80 Entity* entity = Node_getEntity( path.top() );
82 && ( instance.childSelected() || Instance_getSelectable( instance )->isSelected() ) ) {
83 entity->setKeyValue( m_key, m_value );
88 class EntitySetClassnameSelected : public scene::Graph::Walker
90 const char* m_classname;
92 EntitySetClassnameSelected( const char* classname )
93 : m_classname( classname ){
95 bool pre( const scene::Path& path, scene::Instance& instance ) const {
98 void post( const scene::Path& path, scene::Instance& instance ) const {
99 Entity* entity = Node_getEntity( path.top() );
101 && ( instance.childSelected() || Instance_getSelectable( instance )->isSelected() ) ) {
102 NodeSmartReference node( GlobalEntityCreator().createEntity( GlobalEntityClassManager().findOrInsert( m_classname, node_is_group( path.top() ) ) ) );
104 EntityCopyingVisitor visitor( *Node_getEntity( node ) );
106 entity->forEachKeyValue( visitor );
108 NodeSmartReference child( path.top().get() );
109 NodeSmartReference parent( path.parent().get() );
110 Node_getTraversable( parent )->erase( child );
111 if ( Node_getTraversable( child ) != 0
112 && Node_getTraversable( node ) != 0
113 && node_is_group( node ) ) {
114 parentBrushes( child, node );
116 Node_getTraversable( parent )->insert( node );
121 void Scene_EntitySetKeyValue_Selected( const char* key, const char* value ){
122 GlobalSceneGraph().traverse( EntitySetKeyValueSelected( key, value ) );
125 void Scene_EntitySetClassname_Selected( const char* classname ){
126 GlobalSceneGraph().traverse( EntitySetClassnameSelected( classname ) );
130 void Entity_ungroupSelected(){
131 if ( GlobalSelectionSystem().countSelected() < 1 ) {
135 UndoableCommand undo( "ungroupSelectedEntities" );
137 scene::Path world_path( makeReference( GlobalSceneGraph().root() ) );
138 world_path.push( makeReference( Map_FindOrInsertWorldspawn( g_map ) ) );
140 scene::Instance &instance = GlobalSelectionSystem().ultimateSelected();
141 scene::Path path = instance.path();
143 if ( !Node_isEntity( path.top() ) ) {
147 if ( Node_getEntity( path.top() ) != 0
148 && node_is_group( path.top() ) ) {
149 if ( world_path.top().get_pointer() != path.top().get_pointer() ) {
150 parentBrushes( path.top(), world_path.top() );
151 Path_deleteTop( path );
157 class EntityFindSelected : public scene::Graph::Walker
160 mutable const scene::Path *groupPath;
161 mutable scene::Instance *groupInstance;
163 EntityFindSelected() : groupPath( 0 ), groupInstance( 0 ){
166 bool pre( const scene::Path& path, scene::Instance& instance ) const {
170 void post( const scene::Path& path, scene::Instance& instance ) const {
171 Entity* entity = Node_getEntity( path.top() );
173 && Instance_getSelectable( instance )->isSelected()
174 && node_is_group( path.top() )
177 groupInstance = &instance;
182 class EntityGroupSelected : public scene::Graph::Walker
184 NodeSmartReference group, worldspawn;
185 //typedef std::pair<NodeSmartReference, NodeSmartReference> DeletionPair;
186 //Stack<DeletionPair> deleteme;
188 EntityGroupSelected( const scene::Path &p ) : group( p.top().get() ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
191 bool pre( const scene::Path& path, scene::Instance& instance ) const {
195 void post( const scene::Path& path, scene::Instance& instance ) const {
196 Selectable *selectable = Instance_getSelectable( instance );
197 if ( selectable && selectable->isSelected() ) {
198 Entity* entity = Node_getEntity( path.top() );
199 if ( entity == 0 && Node_isPrimitive( path.top() ) ) {
200 NodeSmartReference child( path.top().get() );
201 NodeSmartReference parent( path.parent().get() );
203 if ( path.size() >= 3 && parent != worldspawn ) {
204 NodeSmartReference parentparent( path[path.size() - 3].get() );
206 Node_getTraversable( parent )->erase( child );
207 Node_getTraversable( group )->insert( child );
209 if ( Node_getTraversable( parent )->empty() ) {
210 //deleteme.push(DeletionPair(parentparent, parent));
211 Node_getTraversable( parentparent )->erase( parent );
216 Node_getTraversable( parent )->erase( child );
217 Node_getTraversable( group )->insert( child );
224 void Entity_groupSelected(){
225 if ( GlobalSelectionSystem().countSelected() < 1 ) {
229 UndoableCommand undo( "groupSelectedEntities" );
231 scene::Path world_path( makeReference( GlobalSceneGraph().root() ) );
232 world_path.push( makeReference( Map_FindOrInsertWorldspawn( g_map ) ) );
234 EntityFindSelected fse;
235 GlobalSceneGraph().traverse( fse );
236 if ( fse.groupPath ) {
237 GlobalSceneGraph().traverse( EntityGroupSelected( *fse.groupPath ) );
241 GlobalSceneGraph().traverse( EntityGroupSelected( world_path ) );
246 void Entity_connectSelected(){
247 if ( GlobalSelectionSystem().countSelected() == 2 ) {
248 GlobalEntityCreator().connectEntities(
249 GlobalSelectionSystem().penultimateSelected().path(),
250 GlobalSelectionSystem().ultimateSelected().path(),
256 globalErrorStream() << "entityConnectSelected: exactly two instances must be selected\n";
260 void Entity_killconnectSelected(){
261 if ( GlobalSelectionSystem().countSelected() == 2 ) {
262 GlobalEntityCreator().connectEntities(
263 GlobalSelectionSystem().penultimateSelected().path(),
264 GlobalSelectionSystem().ultimateSelected().path(),
270 globalErrorStream() << "entityKillConnectSelected: exactly two instances must be selected\n";
274 AABB Doom3Light_getBounds( const AABB& workzone ){
275 AABB aabb( workzone );
277 Vector3 defaultRadius( 300, 300, 300 );
278 if ( !string_parse_vector3( EntityClass_valueForKey( *GlobalEntityClassManager().findOrInsert( "light", false ), "light_radius" ), defaultRadius ) ) {
279 globalErrorStream() << "Doom3Light_getBounds: failed to parse default light radius\n";
282 if ( aabb.extents[0] == 0 ) {
283 aabb.extents[0] = defaultRadius[0];
285 if ( aabb.extents[1] == 0 ) {
286 aabb.extents[1] = defaultRadius[1];
288 if ( aabb.extents[2] == 0 ) {
289 aabb.extents[2] = defaultRadius[2];
292 if ( aabb_valid( aabb ) ) {
295 return AABB( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
298 int g_iLastLightIntensity;
300 void Entity_createFromSelection( const char* name, const Vector3& origin ){
302 if ( string_equal_nocase( name, "worldspawn" ) ) {
303 ui::alert( MainFrame_getWindow( ), "Can't create an entity with worldspawn.", "info" );
308 EntityClass* entityClass = GlobalEntityClassManager().findOrInsert( name, true );
310 bool isModel = ( string_compare_nocase_n( name, "misc_", 5 ) == 0 && string_equal_nocase( name + string_length( name ) - 5, "model" ) ) // misc_*model (also misc_model)
311 || string_equal_nocase( name, "model_static" )
312 || ( GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase( name, "func_static" ) && g_pGameDescription->mGameType == "doom3" );
314 bool brushesSelected = Scene_countSelectedBrushes( GlobalSceneGraph() ) != 0;
316 if ( !( entityClass->fixedsize || isModel ) && !brushesSelected ) {
317 globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
321 AABB workzone( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
324 NodeSmartReference node( GlobalEntityCreator().createEntity( entityClass ) );
326 Node_getTraversable( GlobalSceneGraph().root() )->insert( node );
328 scene::Path entitypath( makeReference( GlobalSceneGraph().root() ) );
329 entitypath.push( makeReference( node.get() ) );
330 scene::Instance& instance = findInstance( entitypath );
332 if ( entityClass->fixedsize || ( isModel && !brushesSelected ) ) {
335 Transformable* transform = Instance_getTransformable( instance );
336 if ( transform != 0 ) {
337 transform->setType( TRANSFORM_PRIMITIVE );
338 transform->setTranslation( origin );
339 transform->freezeTransform();
342 GlobalSelectionSystem().setSelectedAll( false );
344 Instance_setSelected( instance, true );
348 if ( g_pGameDescription->mGameType == "doom3" ) {
349 Node_getEntity( node )->setKeyValue( "model", Node_getEntity( node )->getKeyValue( "name" ) );
352 Scene_parentSelectedBrushesToEntity( GlobalSceneGraph(), node );
353 Scene_forEachChildSelectable( SelectableSetSelected( true ), instance.path() );
356 // tweaking: when right clic dropping a light entity, ask for light value in a custom dialog box
359 if ( g_pGameDescription->mGameType == "hl" ) {
360 // FIXME - Hydra: really we need a combined light AND color dialog for halflife.
361 if ( string_equal_nocase( name, "light" )
362 || string_equal_nocase( name, "light_environment" )
363 || string_equal_nocase( name, "light_spot" ) ) {
364 int intensity = g_iLastLightIntensity;
366 if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
367 g_iLastLightIntensity = intensity;
369 sprintf( buf, "255 255 255 %d", intensity );
370 Node_getEntity( node )->setKeyValue( "_light", buf );
374 else if ( string_equal_nocase( name, "light" ) ) {
375 if ( g_pGameDescription->mGameType != "doom3" ) {
376 int intensity = g_iLastLightIntensity;
378 if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
379 g_iLastLightIntensity = intensity;
381 sprintf( buf, "%d", intensity );
382 Node_getEntity( node )->setKeyValue( "light", buf );
385 else if ( brushesSelected ) { // use workzone to set light position/size for doom3 lights, if there are brushes selected
386 AABB bounds( Doom3Light_getBounds( workzone ) );
387 StringOutputStream key( 64 );
388 key << bounds.origin[0] << " " << bounds.origin[1] << " " << bounds.origin[2];
389 Node_getEntity( node )->setKeyValue( "origin", key.c_str() );
391 key << bounds.extents[0] << " " << bounds.extents[1] << " " << bounds.extents[2];
392 Node_getEntity( node )->setKeyValue( "light_radius", key.c_str() );
397 const char* model = misc_model_dialog(MainFrame_getWindow());
399 Node_getEntity( node )->setKeyValue( "model", model );
403 if ( string_compare_nocase_n( name, "trigger_", 8 ) == 0 && brushesSelected ){
404 const char* shader = g_pGameDescription->getKeyValue( "shader_trigger" );
405 if ( shader && *shader ){
406 Scene_PatchSetShader_Selected( GlobalSceneGraph(), shader );
407 Scene_BrushSetShader_Selected( GlobalSceneGraph(), shader );
410 Scene_PatchSetShader_Selected( GlobalSceneGraph(), "textures/common/trigger" );
411 Scene_BrushSetShader_Selected( GlobalSceneGraph(), "textures/common/trigger" );
417 bool DoNormalisedColor( Vector3& color ){
418 if ( !color_dialog( MainFrame_getWindow( ), color ) ) {
422 ** scale colors so that at least one component is at 1.0F
425 float largest = 0.0F;
427 if ( color[0] > largest ) {
430 if ( color[1] > largest ) {
433 if ( color[2] > largest ) {
437 if ( largest == 0.0F ) {
444 float scaler = 1.0F / largest;
455 void NormalizeColor( Vector3& color ){
456 // scale colors so that at least one component is at 1.0F
458 float largest = 0.0F;
460 if ( color[0] > largest ) {
463 if ( color[1] > largest ) {
466 if ( color[2] > largest ) {
470 if ( largest == 0.0F ) {
477 float scaler = 1.0F / largest;
485 void Entity_normalizeColor(){
486 if ( GlobalSelectionSystem().countSelected() != 0 ) {
487 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
488 Entity* entity = Node_getEntity( path.top() );
491 const char* strColor = entity->getKeyValue( "_color" );
492 if ( !string_empty( strColor ) ) {
494 if ( string_parse_vector3( strColor, rgb ) ) {
495 g_entity_globals.color_entity = rgb;
496 NormalizeColor( g_entity_globals.color_entity );
499 sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
500 g_entity_globals.color_entity[1],
501 g_entity_globals.color_entity[2] );
503 Scene_EntitySetKeyValue_Selected( "_color", buffer );
510 void Entity_setColour(){
511 if ( GlobalSelectionSystem().countSelected() != 0 ) {
512 bool normalize = false;
513 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
514 Entity* entity = Node_getEntity( path.top() );
517 const char* strColor = entity->getKeyValue( "_color" );
518 if ( !string_empty( strColor ) ) {
520 if ( string_parse_vector3( strColor, rgb ) ) {
521 g_entity_globals.color_entity = rgb;
525 if ( g_pGameDescription->mGameType == "doom3" ) {
529 if ( color_dialog( MainFrame_getWindow(), g_entity_globals.color_entity ) ) {
531 NormalizeColor( g_entity_globals.color_entity );
535 sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
536 g_entity_globals.color_entity[1],
537 g_entity_globals.color_entity[2] );
539 Scene_EntitySetKeyValue_Selected( "_color", buffer );
545 CopiedString g_strLastModelFolder = "";
547 const char *getLastModelFolderPath(){
548 if ( g_strLastModelFolder.empty() ) {
549 GlobalPreferenceSystem().registerPreference( "LastModelFolder", make_property_string( g_strLastModelFolder ) );
550 if ( g_strLastModelFolder.empty() ) {
551 StringOutputStream buffer( 1024 );
552 buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
553 if ( !file_readable( buffer.c_str() ) ) {
556 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
558 g_strLastModelFolder = buffer.c_str();
561 return g_strLastModelFolder.c_str();
564 const char *misc_model_dialog( ui::Widget parent ){
565 const char *filename = parent.file_dialog( TRUE, "Choose Model", getLastModelFolderPath(), ModelLoader::Name() );
567 if ( filename != NULL ) {
568 g_strLastModelFolder = g_path_get_dirname( filename );
569 // use VFS to get the correct relative path
570 const char *relative = path_make_relative( filename, GlobalFileSystem().findRoot( filename ) );
571 if ( relative == filename ) {
572 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
580 static void Export(const EntityCreator &self, const Callback<void(bool)> &returnz) {
581 returnz(self.getLightRadii());
584 static void Import(EntityCreator &self, bool value) {
585 self.setLightRadii(value);
589 void Entity_constructPreferences( PreferencesPage& page ){
591 "Show", "Light Radii",
592 make_property<LightRadii>(GlobalEntityCreator())
595 void Entity_constructPage( PreferenceGroup& group ){
596 PreferencesPage page( group.createPage( "Entities", "Entity Display Preferences" ) );
597 Entity_constructPreferences( page );
600 void Entity_registerPreferencesPage(){
601 PreferencesDialog_addDisplayPage( makeCallbackF(Entity_constructPage) );
605 void Entity_constructMenu( ui::Menu menu ){
606 create_menu_item_with_mnemonic( menu, "_Regroup", "GroupSelection" );
607 create_menu_item_with_mnemonic( menu, "_Ungroup", "UngroupSelection" );
608 create_menu_item_with_mnemonic( menu, "_Connect", "ConnectSelection" );
609 if ( g_pGameDescription->mGameType == "nexuiz" ) {
610 create_menu_item_with_mnemonic( menu, "_KillConnect", "KillConnectSelection" );
612 create_menu_item_with_mnemonic( menu, "_Select Color...", "EntityColor" );
613 create_menu_item_with_mnemonic( menu, "_Normalize Color...", "NormalizeColor" );
617 void Entity_Construct(){
618 GlobalCommands_insert( "EntityColor", makeCallbackF(Entity_setColour), Accelerator( 'K' ) );
619 GlobalCommands_insert( "NormalizeColor", makeCallbackF(Entity_normalizeColor) );
620 GlobalCommands_insert( "ConnectSelection", makeCallbackF(Entity_connectSelected), Accelerator( 'K', (GdkModifierType)GDK_CONTROL_MASK ) );
621 GlobalCommands_insert( "KillConnectSelection", makeCallbackF(Entity_killconnectSelected), Accelerator( 'K', (GdkModifierType)( GDK_SHIFT_MASK ) ) );
622 GlobalCommands_insert( "GroupSelection", makeCallbackF(Entity_groupSelected) );
623 GlobalCommands_insert( "UngroupSelection", makeCallbackF(Entity_ungroupSelected) );
625 GlobalPreferenceSystem().registerPreference( "SI_Colors5", make_property_string( g_entity_globals.color_entity ) );
626 GlobalPreferenceSystem().registerPreference( "LastLightIntensity", make_property_string( g_iLastLightIntensity ) );
628 Entity_registerPreferencesPage();
631 void Entity_Destroy(){