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 );
117 /* must do this after inserting node, otherwise problem: targeted + having model + not loaded b4 new entities aren't selectable normally + rendered only while 0 0 0 is rendered */
118 entity->forEachKeyValue( visitor );
119 Node_getTraversable( parent )->erase( child );
124 void Scene_EntitySetKeyValue_Selected( const char* key, const char* value ){
125 GlobalSceneGraph().traverse( EntitySetKeyValueSelected( key, value ) );
128 void Scene_EntitySetClassname_Selected( const char* classname ){
129 GlobalSceneGraph().traverse( EntitySetClassnameSelected( classname ) );
133 void Entity_ungroupSelected(){
134 if ( GlobalSelectionSystem().countSelected() < 1 ) {
138 UndoableCommand undo( "ungroupSelectedEntities" );
140 scene::Path world_path( makeReference( GlobalSceneGraph().root() ) );
141 world_path.push( makeReference( Map_FindOrInsertWorldspawn( g_map ) ) );
143 scene::Instance &instance = GlobalSelectionSystem().ultimateSelected();
144 scene::Path path = instance.path();
146 if ( !Node_isEntity( path.top() ) ) {
150 if ( Node_getEntity( path.top() ) != 0
151 && node_is_group( path.top() ) ) {
152 if ( world_path.top().get_pointer() != path.top().get_pointer() ) {
153 parentBrushes( path.top(), world_path.top() );
154 Path_deleteTop( path );
160 class EntityFindSelected : public scene::Graph::Walker
163 mutable const scene::Path *groupPath;
164 mutable scene::Instance *groupInstance;
166 EntityFindSelected() : groupPath( 0 ), groupInstance( 0 ){
169 bool pre( const scene::Path& path, scene::Instance& instance ) const {
173 void post( const scene::Path& path, scene::Instance& instance ) const {
174 Entity* entity = Node_getEntity( path.top() );
176 && Instance_getSelectable( instance )->isSelected()
177 && node_is_group( path.top() )
180 groupInstance = &instance;
185 class EntityGroupSelected : public scene::Graph::Walker
187 NodeSmartReference group, worldspawn;
188 //typedef std::pair<NodeSmartReference, NodeSmartReference> DeletionPair;
189 //Stack<DeletionPair> deleteme;
191 EntityGroupSelected( const scene::Path &p ) : group( p.top().get() ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
194 bool pre( const scene::Path& path, scene::Instance& instance ) const {
198 void post( const scene::Path& path, scene::Instance& instance ) const {
199 Selectable *selectable = Instance_getSelectable( instance );
200 if ( selectable && selectable->isSelected() ) {
201 Entity* entity = Node_getEntity( path.top() );
202 if ( entity == 0 && Node_isPrimitive( path.top() ) ) {
203 NodeSmartReference child( path.top().get() );
204 NodeSmartReference parent( path.parent().get() );
206 if ( path.size() >= 3 && parent != worldspawn ) {
207 NodeSmartReference parentparent( path[path.size() - 3].get() );
209 Node_getTraversable( parent )->erase( child );
210 Node_getTraversable( group )->insert( child );
212 if ( Node_getTraversable( parent )->empty() ) {
213 //deleteme.push(DeletionPair(parentparent, parent));
214 Node_getTraversable( parentparent )->erase( parent );
219 Node_getTraversable( parent )->erase( child );
220 Node_getTraversable( group )->insert( child );
227 void Entity_groupSelected(){
228 if ( GlobalSelectionSystem().countSelected() < 1 ) {
232 UndoableCommand undo( "groupSelectedEntities" );
234 scene::Path world_path( makeReference( GlobalSceneGraph().root() ) );
235 world_path.push( makeReference( Map_FindOrInsertWorldspawn( g_map ) ) );
237 EntityFindSelected fse;
238 GlobalSceneGraph().traverse( fse );
239 if ( fse.groupPath ) {
240 GlobalSceneGraph().traverse( EntityGroupSelected( *fse.groupPath ) );
244 GlobalSceneGraph().traverse( EntityGroupSelected( world_path ) );
249 void Entity_connectSelected(){
250 if ( GlobalSelectionSystem().countSelected() == 2 ) {
251 GlobalEntityCreator().connectEntities(
252 GlobalSelectionSystem().penultimateSelected().path(),
253 GlobalSelectionSystem().ultimateSelected().path(),
259 globalErrorStream() << "entityConnectSelected: exactly two instances must be selected\n";
263 void Entity_killconnectSelected(){
264 if ( GlobalSelectionSystem().countSelected() == 2 ) {
265 GlobalEntityCreator().connectEntities(
266 GlobalSelectionSystem().penultimateSelected().path(),
267 GlobalSelectionSystem().ultimateSelected().path(),
273 globalErrorStream() << "entityKillConnectSelected: exactly two instances must be selected\n";
277 AABB Doom3Light_getBounds( const AABB& workzone ){
278 AABB aabb( workzone );
280 Vector3 defaultRadius( 300, 300, 300 );
281 if ( !string_parse_vector3( EntityClass_valueForKey( *GlobalEntityClassManager().findOrInsert( "light", false ), "light_radius" ), defaultRadius ) ) {
282 globalErrorStream() << "Doom3Light_getBounds: failed to parse default light radius\n";
285 if ( aabb.extents[0] == 0 ) {
286 aabb.extents[0] = defaultRadius[0];
288 if ( aabb.extents[1] == 0 ) {
289 aabb.extents[1] = defaultRadius[1];
291 if ( aabb.extents[2] == 0 ) {
292 aabb.extents[2] = defaultRadius[2];
295 if ( aabb_valid( aabb ) ) {
298 return AABB( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
301 int g_iLastLightIntensity;
303 void Entity_createFromSelection( const char* name, const Vector3& origin ){
305 if ( string_equal_nocase( name, "worldspawn" ) ) {
306 ui::alert( MainFrame_getWindow( ), "Can't create an entity with worldspawn.", "info" );
311 EntityClass* entityClass = GlobalEntityClassManager().findOrInsert( name, true );
313 bool isModel = ( string_compare_nocase_n( name, "misc_", 5 ) == 0 && string_equal_nocase( name + string_length( name ) - 5, "model" ) ) // misc_*model (also misc_model)
314 || string_equal_nocase( name, "model_static" )
315 || ( GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase( name, "func_static" ) && g_pGameDescription->mGameType == "doom3" );
317 bool brushesSelected = Scene_countSelectedBrushes( GlobalSceneGraph() ) != 0;
319 //is important to have retexturing here; if doing in the end, undo doesn't succeed;
320 if ( string_compare_nocase_n( name, "trigger_", 8 ) == 0 && brushesSelected ){
321 const char* shader = g_pGameDescription->getKeyValue( "shader_trigger" );
322 if ( shader && *shader ){
323 Scene_PatchSetShader_Selected( GlobalSceneGraph(), shader );
324 Scene_BrushSetShader_Selected( GlobalSceneGraph(), shader );
327 Scene_PatchSetShader_Selected( GlobalSceneGraph(), "textures/common/trigger" );
328 Scene_BrushSetShader_Selected( GlobalSceneGraph(), "textures/common/trigger" );
332 if ( !( entityClass->fixedsize || isModel ) && !brushesSelected ) {
333 globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
337 AABB workzone( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
340 NodeSmartReference node( GlobalEntityCreator().createEntity( entityClass ) );
342 Node_getTraversable( GlobalSceneGraph().root() )->insert( node );
344 scene::Path entitypath( makeReference( GlobalSceneGraph().root() ) );
345 entitypath.push( makeReference( node.get() ) );
346 scene::Instance& instance = findInstance( entitypath );
348 if ( entityClass->fixedsize || ( isModel && !brushesSelected ) ) {
351 Transformable* transform = Instance_getTransformable( instance );
352 if ( transform != 0 ) {
353 transform->setType( TRANSFORM_PRIMITIVE );
354 transform->setTranslation( origin );
355 transform->freezeTransform();
358 GlobalSelectionSystem().setSelectedAll( false );
360 Instance_setSelected( instance, true );
364 if ( g_pGameDescription->mGameType == "doom3" ) {
365 Node_getEntity( node )->setKeyValue( "model", Node_getEntity( node )->getKeyValue( "name" ) );
368 Scene_parentSelectedBrushesToEntity( GlobalSceneGraph(), node );
369 Scene_forEachChildSelectable( SelectableSetSelected( true ), instance.path() );
372 // tweaking: when right clic dropping a light entity, ask for light value in a custom dialog box
375 if ( g_pGameDescription->mGameType == "hl" ) {
376 // FIXME - Hydra: really we need a combined light AND color dialog for halflife.
377 if ( string_equal_nocase( name, "light" )
378 || string_equal_nocase( name, "light_environment" )
379 || string_equal_nocase( name, "light_spot" ) ) {
380 int intensity = g_iLastLightIntensity;
382 if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
383 g_iLastLightIntensity = intensity;
385 sprintf( buf, "255 255 255 %d", intensity );
386 Node_getEntity( node )->setKeyValue( "_light", buf );
390 else if ( string_equal_nocase( name, "light" ) ) {
391 if ( g_pGameDescription->mGameType != "doom3" ) {
392 int intensity = g_iLastLightIntensity;
394 if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
395 g_iLastLightIntensity = intensity;
397 sprintf( buf, "%d", intensity );
398 Node_getEntity( node )->setKeyValue( "light", buf );
401 else if ( brushesSelected ) { // use workzone to set light position/size for doom3 lights, if there are brushes selected
402 AABB bounds( Doom3Light_getBounds( workzone ) );
403 StringOutputStream key( 64 );
404 key << bounds.origin[0] << " " << bounds.origin[1] << " " << bounds.origin[2];
405 Node_getEntity( node )->setKeyValue( "origin", key.c_str() );
407 key << bounds.extents[0] << " " << bounds.extents[1] << " " << bounds.extents[2];
408 Node_getEntity( node )->setKeyValue( "light_radius", key.c_str() );
413 const char* model = misc_model_dialog(MainFrame_getWindow());
415 Node_getEntity( node )->setKeyValue( "model", model );
421 bool DoNormalisedColor( Vector3& color ){
422 if ( !color_dialog( MainFrame_getWindow( ), color ) ) {
426 ** scale colors so that at least one component is at 1.0F
429 float largest = 0.0F;
431 if ( color[0] > largest ) {
434 if ( color[1] > largest ) {
437 if ( color[2] > largest ) {
441 if ( largest == 0.0F ) {
448 float scaler = 1.0F / largest;
459 void NormalizeColor( Vector3& color ){
460 // scale colors so that at least one component is at 1.0F
462 float largest = 0.0F;
464 if ( color[0] > largest ) {
467 if ( color[1] > largest ) {
470 if ( color[2] > largest ) {
474 if ( largest == 0.0F ) {
481 float scaler = 1.0F / largest;
489 void Entity_normalizeColor(){
490 if ( GlobalSelectionSystem().countSelected() != 0 ) {
491 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
492 Entity* entity = Node_getEntity( path.top() );
494 if( entity == 0 && path.size() == 3 ){
495 entity = Node_getEntity( path.parent() );
499 const char* strColor = entity->getKeyValue( "_color" );
500 if ( !string_empty( strColor ) ) {
502 if ( string_parse_vector3( strColor, rgb ) ) {
503 g_entity_globals.color_entity = rgb;
504 NormalizeColor( g_entity_globals.color_entity );
507 sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
508 g_entity_globals.color_entity[1],
509 g_entity_globals.color_entity[2] );
511 StringOutputStream command( 256 );
512 command << "entityNormalizeColour " << buffer;
513 UndoableCommand undo( command.c_str() );
514 Scene_EntitySetKeyValue_Selected( "_color", buffer );
521 void Entity_setColour(){
522 if ( GlobalSelectionSystem().countSelected() != 0 ) {
523 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
524 Entity* entity = Node_getEntity( path.top() );
526 if( entity == 0 && path.size() == 3 ){
527 entity = Node_getEntity( path.parent() );
531 const char* strColor = entity->getKeyValue( "_color" );
532 if ( !string_empty( strColor ) ) {
534 if ( string_parse_vector3( strColor, rgb ) ) {
535 g_entity_globals.color_entity = rgb;
538 if ( color_dialog( MainFrame_getWindow(), g_entity_globals.color_entity ) ) {
540 sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
541 g_entity_globals.color_entity[1],
542 g_entity_globals.color_entity[2] );
544 StringOutputStream command( 256 );
545 command << "entitySetColour " << buffer;
546 UndoableCommand undo( command.c_str() );
547 Scene_EntitySetKeyValue_Selected( "_color", buffer );
553 CopiedString g_strLastModelFolder = "";
555 const char *getLastModelFolderPath(){
556 if ( g_strLastModelFolder.empty() ) {
557 GlobalPreferenceSystem().registerPreference( "LastModelFolder", make_property_string( g_strLastModelFolder ) );
558 if ( g_strLastModelFolder.empty() ) {
559 StringOutputStream buffer( 1024 );
560 buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
561 if ( !file_readable( buffer.c_str() ) ) {
564 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
566 g_strLastModelFolder = buffer.c_str();
569 return g_strLastModelFolder.c_str();
572 const char *misc_model_dialog( ui::Widget parent ){
573 const char *filename = parent.file_dialog( TRUE, "Choose Model", getLastModelFolderPath(), ModelLoader::Name() );
575 if ( filename != NULL ) {
576 g_strLastModelFolder = g_path_get_dirname( filename );
577 // use VFS to get the correct relative path
578 const char *relative = path_make_relative( filename, GlobalFileSystem().findRoot( filename ) );
579 if ( relative == filename ) {
580 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
588 static void Export(const EntityCreator &self, const Callback<void(bool)> &returnz) {
589 returnz(self.getLightRadii());
592 static void Import(EntityCreator &self, bool value) {
593 self.setLightRadii(value);
597 void Entity_constructPreferences( PreferencesPage& page ){
599 "Show", "Light Radii",
600 make_property<LightRadii>(GlobalEntityCreator())
603 void Entity_constructPage( PreferenceGroup& group ){
604 PreferencesPage page( group.createPage( "Entities", "Entity Display Preferences" ) );
605 Entity_constructPreferences( page );
608 void Entity_registerPreferencesPage(){
609 PreferencesDialog_addDisplayPage( makeCallbackF(Entity_constructPage) );
613 void ShowLightRadiiToggle(){
614 GlobalEntityCreator().setLightRadii( !GlobalEntityCreator().getLightRadii() );
617 typedef FreeCaller<void(), ShowLightRadiiToggle> ShowLightRadiiToggleCaller;
618 void ShowLightRadiiExport( const Callback<void(bool)> & importer ){
619 GlobalEntityCreator().getLightRadii();
621 typedef FreeCaller<void(const Callback<void(bool)> &), ShowLightRadiiExport> ShowLightRadiiExportCaller;
623 ShowLightRadiiExportCaller g_show_lightradii_caller;
624 Callback<void(const Callback<void(bool)> &)> g_show_lightradii_callback( g_show_lightradii_caller );
625 ToggleItem g_show_lightradii( g_show_lightradii_callback );
627 void Entity_constructMenu( ui::Menu menu ){
628 create_menu_item_with_mnemonic( menu, "_Regroup", "GroupSelection" );
629 create_menu_item_with_mnemonic( menu, "_Ungroup", "UngroupSelection" );
630 create_menu_item_with_mnemonic( menu, "_Connect", "ConnectSelection" );
631 if ( g_pGameDescription->mGameType == "nexuiz" ) {
632 create_menu_item_with_mnemonic( menu, "_KillConnect", "KillConnectSelection" );
634 create_menu_item_with_mnemonic( menu, "_Select Color...", "EntityColor" );
635 create_menu_item_with_mnemonic( menu, "_Normalize Color", "NormalizeColor" );
639 void Entity_Construct(){
640 GlobalCommands_insert( "EntityColor", makeCallbackF(Entity_setColour), Accelerator( 'K' ) );
641 GlobalCommands_insert( "NormalizeColor", makeCallbackF(Entity_normalizeColor) );
642 GlobalCommands_insert( "ConnectSelection", makeCallbackF(Entity_connectSelected), Accelerator( 'K', (GdkModifierType)GDK_CONTROL_MASK ) );
643 GlobalCommands_insert( "KillConnectSelection", makeCallbackF(Entity_killconnectSelected), Accelerator( 'K', (GdkModifierType)( GDK_SHIFT_MASK ) ) );
644 GlobalCommands_insert( "GroupSelection", makeCallbackF(Entity_groupSelected) );
645 GlobalCommands_insert( "UngroupSelection", makeCallbackF(Entity_ungroupSelected) );
647 GlobalToggles_insert( "ShowLightRadiuses", makeCallbackF( ShowLightRadiiToggle ), ToggleItem::AddCallbackCaller( g_show_lightradii ) );
649 GlobalPreferenceSystem().registerPreference( "SI_Colors5", make_property_string( g_entity_globals.color_entity ) );
650 GlobalPreferenceSystem().registerPreference( "LastLightIntensity", make_property_string( g_iLastLightIntensity ) );
652 // Entity_registerPreferencesPage();
655 void Entity_Destroy(){