]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - radiant/entity.cpp
Merge commit '461d008daa6328113ea4ccda37e5604d3df14ba3' into garux-merge
[xonotic/netradiant.git] / radiant / entity.cpp
index 0704f50a9c49bdc59455f373c64cdc6b0e395954..3a7eddb258218c287da5bc146daeca682c1f6a0c 100644 (file)
 #include "select.h"
 #include "map.h"
 #include "preferences.h"
+#include "preferencesystem.h"
+#include "stringio.h"
 #include "gtkdlgs.h"
 #include "mainframe.h"
 #include "qe3.h"
 #include "commands.h"
 
+#include "brushmanip.h"
+#include "patchmanip.h"
+
+#include "uilib/uilib.h"
+
 struct entity_globals_t
 {
        Vector3 color_entity;
@@ -64,9 +71,11 @@ public:
 EntitySetKeyValueSelected( const char* key, const char* value )
        : m_key( key ), m_value( value ){
 }
+
 bool pre( const scene::Path& path, scene::Instance& instance ) const {
        return true;
 }
+
 void post( const scene::Path& path, scene::Instance& instance ) const {
        Entity* entity = Node_getEntity( path.top() );
        if ( entity != 0
@@ -94,17 +103,20 @@ void post( const scene::Path& path, scene::Instance& instance ) const {
 
                EntityCopyingVisitor visitor( *Node_getEntity( node ) );
 
-               entity->forEachKeyValue( visitor );
+               //entity->forEachKeyValue( visitor );
 
                NodeSmartReference child( path.top().get() );
                NodeSmartReference parent( path.parent().get() );
-               Node_getTraversable( parent )->erase( child );
+               //Node_getTraversable( parent )->erase( child );
                if ( Node_getTraversable( child ) != 0
                         && Node_getTraversable( node ) != 0
                         && node_is_group( node ) ) {
                        parentBrushes( child, node );
                }
                Node_getTraversable( parent )->insert( node );
+               /* 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 */
+               entity->forEachKeyValue( visitor );
+               Node_getTraversable( parent )->erase( child );
        }
 }
 };
@@ -150,11 +162,14 @@ class EntityFindSelected : public scene::Graph::Walker
 public:
 mutable const scene::Path *groupPath;
 mutable scene::Instance *groupInstance;
+
 EntityFindSelected() : groupPath( 0 ), groupInstance( 0 ){
 }
+
 bool pre( const scene::Path& path, scene::Instance& instance ) const {
        return true;
 }
+
 void post( const scene::Path& path, scene::Instance& instance ) const {
        Entity* entity = Node_getEntity( path.top() );
        if ( entity != 0
@@ -175,9 +190,11 @@ NodeSmartReference group, worldspawn;
 public:
 EntityGroupSelected( const scene::Path &p ) : group( p.top().get() ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
 }
+
 bool pre( const scene::Path& path, scene::Instance& instance ) const {
        return true;
 }
+
 void post( const scene::Path& path, scene::Instance& instance ) const {
        Selectable *selectable = Instance_getSelectable( instance );
        if ( selectable && selectable->isSelected() ) {
@@ -229,7 +246,6 @@ void Entity_groupSelected(){
 }
 
 
-
 void Entity_connectSelected(){
        if ( GlobalSelectionSystem().countSelected() == 2 ) {
                GlobalEntityCreator().connectEntities(
@@ -287,7 +303,7 @@ int g_iLastLightIntensity;
 void Entity_createFromSelection( const char* name, const Vector3& origin ){
 #if 0
        if ( string_equal_nocase( name, "worldspawn" ) ) {
-               gtk_MessageBox( GTK_WIDGET( MainFrame_getWindow() ), "Can't create an entity with worldspawn.", "info" );
+               ui::alert( MainFrame_getWindow( ), "Can't create an entity with worldspawn.", "info" );
                return;
        }
 #endif
@@ -296,10 +312,23 @@ void Entity_createFromSelection( const char* name, const Vector3& origin ){
 
        bool isModel = ( string_compare_nocase_n( name, "misc_", 5 ) == 0 && string_equal_nocase( name + string_length( name ) - 5, "model" ) ) // misc_*model (also misc_model)
                                   || string_equal_nocase( name, "model_static" )
-                                  || ( GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase( name, "func_static" ) );
+                                  || ( GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase( name, "func_static" ) && g_pGameDescription->mGameType == "doom3" );
 
        bool brushesSelected = Scene_countSelectedBrushes( GlobalSceneGraph() ) != 0;
 
+       //is important to have retexturing here; if doing in the end, undo doesn't succeed;
+       if ( string_compare_nocase_n( name, "trigger_", 8 ) == 0 && brushesSelected ){
+               const char* shader = g_pGameDescription->getKeyValue( "shader_trigger" );
+               if ( shader && *shader ){
+                       Scene_PatchSetShader_Selected( GlobalSceneGraph(), shader );
+                       Scene_BrushSetShader_Selected( GlobalSceneGraph(), shader );
+               }
+               else{
+                       Scene_PatchSetShader_Selected( GlobalSceneGraph(), "textures/common/trigger" );
+                       Scene_BrushSetShader_Selected( GlobalSceneGraph(), "textures/common/trigger" );
+               }
+       }
+
        if ( !( entityClass->fixedsize || isModel ) && !brushesSelected ) {
                globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
                return;
@@ -381,7 +410,7 @@ void Entity_createFromSelection( const char* name, const Vector3& origin ){
        }
 
        if ( isModel ) {
-               const char* model = misc_model_dialog( GTK_WIDGET( MainFrame_getWindow() ) );
+               const char* model = misc_model_dialog(MainFrame_getWindow());
                if ( model != 0 ) {
                        Node_getEntity( node )->setKeyValue( "model", model );
                }
@@ -390,7 +419,7 @@ void Entity_createFromSelection( const char* name, const Vector3& origin ){
 
 #if 0
 bool DoNormalisedColor( Vector3& color ){
-       if ( !color_dialog( GTK_WIDGET( MainFrame_getWindow() ), color ) ) {
+       if ( !color_dialog( MainFrame_getWindow( ), color ) ) {
                return false;
        }
        /*
@@ -462,6 +491,10 @@ void Entity_normalizeColor(){
                const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
                Entity* entity = Node_getEntity( path.top() );
 
+               if( entity == 0 && path.size() == 3 ){
+                       entity = Node_getEntity( path.parent() );
+               }
+
                if ( entity != 0 ) {
                        const char* strColor = entity->getKeyValue( "_color" );
                        if ( !string_empty( strColor ) ) {
@@ -475,6 +508,9 @@ void Entity_normalizeColor(){
                                                         g_entity_globals.color_entity[1],
                                                         g_entity_globals.color_entity[2] );
 
+                                       StringOutputStream command( 256 );
+                                       command << "entityNormalizeColour " << buffer;
+                                       UndoableCommand undo( command.c_str() );
                                        Scene_EntitySetKeyValue_Selected( "_color", buffer );
                                }
                        }
@@ -484,10 +520,13 @@ void Entity_normalizeColor(){
 
 void Entity_setColour(){
        if ( GlobalSelectionSystem().countSelected() != 0 ) {
-               bool normalize = false;
                const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
                Entity* entity = Node_getEntity( path.top() );
 
+               if( entity == 0 && path.size() == 3 ){
+                       entity = Node_getEntity( path.parent() );
+               }
+
                if ( entity != 0 ) {
                        const char* strColor = entity->getKeyValue( "_color" );
                        if ( !string_empty( strColor ) ) {
@@ -496,42 +535,47 @@ void Entity_setColour(){
                                        g_entity_globals.color_entity = rgb;
                                }
                        }
-
-                       if ( g_pGameDescription->mGameType == "doom3" ) {
-                               normalize = false;
-                       }
-
-                       if ( color_dialog( GTK_WIDGET( MainFrame_getWindow() ), g_entity_globals.color_entity ) ) {
-                               if ( normalize ) {
-                                       NormalizeColor( g_entity_globals.color_entity );
-                               }
-
+                       if ( color_dialog( MainFrame_getWindow(), g_entity_globals.color_entity ) ) {
                                char buffer[128];
                                sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
                                                 g_entity_globals.color_entity[1],
                                                 g_entity_globals.color_entity[2] );
 
+                               StringOutputStream command( 256 );
+                               command << "entitySetColour " << buffer;
+                               UndoableCommand undo( command.c_str() );
                                Scene_EntitySetKeyValue_Selected( "_color", buffer );
                        }
                }
        }
 }
 
-const char* misc_model_dialog( GtkWidget* parent ){
-       StringOutputStream buffer( 1024 );
-
-       buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
+CopiedString g_strLastModelFolder = "";
 
-       if ( !file_readable( buffer.c_str() ) ) {
-               // just go to fsmain
-               buffer.clear();
-               buffer << g_qeglobals.m_userGamePath.c_str() << "/";
+const char *getLastModelFolderPath(){
+       if ( g_strLastModelFolder.empty() ) {
+               GlobalPreferenceSystem().registerPreference( "LastModelFolder", make_property_string( g_strLastModelFolder ) );
+               if ( g_strLastModelFolder.empty() ) {
+                       StringOutputStream buffer( 1024 );
+                       buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
+                       if ( !file_readable( buffer.c_str() ) ) {
+                               // just go to fsmain
+                               buffer.clear();
+                               buffer << g_qeglobals.m_userGamePath.c_str() << "/";
+                       }
+                       g_strLastModelFolder = buffer.c_str();
+               }
        }
+       return g_strLastModelFolder.c_str();
+}
+
+const char *misc_model_dialog( ui::Widget parent ){
+       const char *filename = parent.file_dialog( TRUE, "Choose Model", getLastModelFolderPath(), ModelLoader::Name() );
 
-       const char *filename = file_dialog( parent, TRUE, "Choose Model", buffer.c_str(), ModelLoader::Name() );
-       if ( filename != 0 ) {
+       if ( filename != NULL ) {
+               g_strLastModelFolder = g_path_get_dirname( filename );
                // use VFS to get the correct relative path
-               const charrelative = path_make_relative( filename, GlobalFileSystem().findRoot( filename ) );
+               const char *relative = path_make_relative( filename, GlobalFileSystem().findRoot( filename ) );
                if ( relative == filename ) {
                        globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
                }
@@ -539,35 +583,48 @@ const char* misc_model_dialog( GtkWidget* parent ){
        }
        return 0;
 }
+/*
+struct LightRadii {
+       static void Export(const EntityCreator &self, const Callback<void(bool)> &returnz) {
+               returnz(self.getLightRadii());
+       }
 
-void LightRadiiImport( EntityCreator& self, bool value ){
-       self.setLightRadii( value );
-}
-typedef ReferenceCaller1<EntityCreator, bool, LightRadiiImport> LightRadiiImportCaller;
-
-void LightRadiiExport( EntityCreator& self, const BoolImportCallback& importer ){
-       importer( self.getLightRadii() );
-}
-typedef ReferenceCaller1<EntityCreator, const BoolImportCallback&, LightRadiiExport> LightRadiiExportCaller;
+       static void Import(EntityCreator &self, bool value) {
+               self.setLightRadii(value);
+       }
+};
 
 void Entity_constructPreferences( PreferencesPage& page ){
        page.appendCheckBox(
-               "Show", "Light Radii",
-               LightRadiiImportCaller( GlobalEntityCreator() ),
-               LightRadiiExportCaller( GlobalEntityCreator() )
-               );
+                       "Show", "Light Radii",
+                       make_property<LightRadii>(GlobalEntityCreator())
+       );
 }
 void Entity_constructPage( PreferenceGroup& group ){
        PreferencesPage page( group.createPage( "Entities", "Entity Display Preferences" ) );
        Entity_constructPreferences( page );
 }
+
 void Entity_registerPreferencesPage(){
-       PreferencesDialog_addDisplayPage( FreeCaller1<PreferenceGroup&, Entity_constructPage>() );
+       PreferencesDialog_addDisplayPage( makeCallbackF(Entity_constructPage) );
 }
+*/
 
+void ShowLightRadiiToggle(){
+       GlobalEntityCreator().setLightRadii( !GlobalEntityCreator().getLightRadii() );
+       UpdateAllWindows();
+}
+typedef FreeCaller<void(), ShowLightRadiiToggle> ShowLightRadiiToggleCaller;
+void ShowLightRadiiExport( const Callback<void(bool)> & importer ){
+       GlobalEntityCreator().getLightRadii();
+}
+typedef FreeCaller<void(const Callback<void(bool)> &), ShowLightRadiiExport> ShowLightRadiiExportCaller;
 
+ShowLightRadiiExportCaller g_show_lightradii_caller;
+Callback<void(const Callback<void(bool)> &)> g_show_lightradii_callback( g_show_lightradii_caller );
+ToggleItem g_show_lightradii( g_show_lightradii_callback );
 
-void Entity_constructMenu( GtkMenu* menu ){
+void Entity_constructMenu( ui::Menu menu ){
        create_menu_item_with_mnemonic( menu, "_Regroup", "GroupSelection" );
        create_menu_item_with_mnemonic( menu, "_Ungroup", "UngroupSelection" );
        create_menu_item_with_mnemonic( menu, "_Connect", "ConnectSelection" );
@@ -575,26 +632,24 @@ void Entity_constructMenu( GtkMenu* menu ){
                create_menu_item_with_mnemonic( menu, "_KillConnect", "KillConnectSelection" );
        }
        create_menu_item_with_mnemonic( menu, "_Select Color...", "EntityColor" );
-       create_menu_item_with_mnemonic( menu, "_Normalize Color...", "NormalizeColor" );
+       create_menu_item_with_mnemonic( menu, "_Normalize Color", "NormalizeColor" );
 }
 
 
-
-#include "preferencesystem.h"
-#include "stringio.h"
-
 void Entity_Construct(){
-       GlobalCommands_insert( "EntityColor", FreeCaller<Entity_setColour>(), Accelerator( 'K' ) );
-       GlobalCommands_insert( "NormalizeColor", FreeCaller<Entity_normalizeColor>() );
-       GlobalCommands_insert( "ConnectSelection", FreeCaller<Entity_connectSelected>(), Accelerator( 'K', (GdkModifierType)GDK_CONTROL_MASK ) );
-       GlobalCommands_insert( "KillConnectSelection", FreeCaller<Entity_killconnectSelected>(), Accelerator( 'K', (GdkModifierType)( GDK_SHIFT_MASK ) ) );
-       GlobalCommands_insert( "GroupSelection", FreeCaller<Entity_groupSelected>() );
-       GlobalCommands_insert( "UngroupSelection", FreeCaller<Entity_ungroupSelected>() );
+       GlobalCommands_insert( "EntityColor", makeCallbackF(Entity_setColour), Accelerator( 'K' ) );
+       GlobalCommands_insert( "NormalizeColor", makeCallbackF(Entity_normalizeColor) );
+       GlobalCommands_insert( "ConnectSelection", makeCallbackF(Entity_connectSelected), Accelerator( 'K', (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "KillConnectSelection", makeCallbackF(Entity_killconnectSelected), Accelerator( 'K', (GdkModifierType)( GDK_SHIFT_MASK ) ) );
+       GlobalCommands_insert( "GroupSelection", makeCallbackF(Entity_groupSelected) );
+       GlobalCommands_insert( "UngroupSelection", makeCallbackF(Entity_ungroupSelected) );
+
+       GlobalToggles_insert( "ShowLightRadiuses", makeCallbackF( ShowLightRadiiToggle ), ToggleItem::AddCallbackCaller( g_show_lightradii ) );
 
-       GlobalPreferenceSystem().registerPreference( "SI_Colors5", Vector3ImportStringCaller( g_entity_globals.color_entity ), Vector3ExportStringCaller( g_entity_globals.color_entity ) );
-       GlobalPreferenceSystem().registerPreference( "LastLightIntensity", IntImportStringCaller( g_iLastLightIntensity ), IntExportStringCaller( g_iLastLightIntensity ) );
+       GlobalPreferenceSystem().registerPreference( "SI_Colors5", make_property_string( g_entity_globals.color_entity ) );
+       GlobalPreferenceSystem().registerPreference( "LastLightIntensity", make_property_string( g_iLastLightIntensity ) );
 
-       Entity_registerPreferencesPage();
+//     Entity_registerPreferencesPage();
 }
 
 void Entity_Destroy(){