]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - radiant/entityinspector.cpp
Merge branch 'master' into master-merge
[xonotic/netradiant.git] / radiant / entityinspector.cpp
index a2291f8fd59fdeb04a449887a1336a0460686dc5..deb7680098dbebabd84d217c5d1642eb0c0b708b 100644 (file)
@@ -36,7 +36,6 @@
 #include <gdk/gdkkeysyms.h>
 #include <uilib/uilib.h>
 
-
 #include "os/path.h"
 #include "eclasslib.h"
 #include "scenelib.h"
@@ -64,6 +63,8 @@
 #include "textureentry.h"
 #include "groupdialog.h"
 
+#include "select.h"
+
 ui::Entry numeric_entry_new(){
        auto entry = ui::Entry(ui::New);
        entry.show();
@@ -140,7 +141,7 @@ void release(){
        delete this;
 }
 void apply(){
-       Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), m_check.active() ? "1" : "0" );
+       Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), m_check.active() ? "1" : "" );
 }
 typedef MemberCaller<BooleanAttribute, void(), &BooleanAttribute::apply> ApplyCaller;
 
@@ -209,6 +210,52 @@ ShaderAttribute( const char* key ) : StringAttribute( key ){
 };
 
 
+class ColorAttribute : public EntityAttribute
+{
+CopiedString m_key;
+BrowsedPathEntry m_entry;
+NonModalEntry m_nonModal;
+public:
+ColorAttribute( const char* key ) :
+       m_key( key ),
+       m_entry( BrowseCaller( *this ) ),
+       m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
+       m_nonModal.connect( m_entry.m_entry.m_entry );
+}
+void release(){
+       delete this;
+}
+ui::Widget getWidget() const {
+       return ui::Widget( m_entry.m_entry.m_frame );
+}
+void apply(){
+       StringOutputStream value( 64 );
+       value << gtk_entry_get_text( GTK_ENTRY( m_entry.m_entry.m_entry ) );
+       Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), value.c_str() );
+}
+typedef MemberCaller<ColorAttribute, void(), &ColorAttribute::apply> ApplyCaller;
+void update(){
+       StringOutputStream value( 64 );
+       value << SelectedEntity_getValueForKey( m_key.c_str() );
+       gtk_entry_set_text( GTK_ENTRY( m_entry.m_entry.m_entry ), value.c_str() );
+}
+typedef MemberCaller<ColorAttribute, void(), &ColorAttribute::update> UpdateCaller;
+void browse( const BrowsedPathEntry::SetPathCallback& setPath ){
+       //const char *filename = misc_model_dialog( gtk_widget_get_toplevel( GTK_WIDGET( m_entry.m_entry.m_frame ) ) );
+
+       /* hijack BrowsedPathEntry to call colour chooser */
+       Entity_setColour();
+
+//     if ( filename != 0 ) {
+//             setPath( filename );
+//             apply();
+//     }
+       update();
+}
+typedef MemberCaller<ColorAttribute, void(const BrowsedPathEntry::SetPathCallback&), &ColorAttribute::browse> BrowseCaller;
+};
+
+
 class ModelAttribute : public EntityAttribute
 {
 CopiedString m_key;
@@ -720,6 +767,8 @@ GtkCheckButton* g_entitySpawnflagsCheck[MAX_FLAGS];
 ui::Entry g_entityKeyEntry{ui::null};
 ui::Entry g_entityValueEntry{ui::null};
 
+GtkToggleButton* g_focusToggleButton;
+
 ui::ListStore g_entlist_store{ui::null};
 ui::ListStore g_entprops_store{ui::null};
 const EntityClass* g_current_flags = 0;
@@ -833,6 +882,36 @@ void SetComment( EntityClass* eclass ){
        g_current_comment = eclass;
 
        g_entityClassComment.text(eclass->comments());
+
+       GtkTextBuffer* buffer = gtk_text_view_get_buffer( g_entityClassComment );
+       //gtk_text_buffer_set_text( buffer, eclass->comments(), -1 );
+       const char* comment = eclass->comments(), *c;
+       int offset = 0, pattern_start = -1, spaces = 0;
+
+       gtk_text_buffer_set_text( buffer, comment, -1 );
+
+       // Catch patterns like "\nstuff :" used to describe keys and spawnflags, and make them bold for readability.
+
+       for( c = comment; *c; ++c, ++offset ) {
+               if( *c == '\n' ) {
+                       pattern_start = offset;
+                       spaces = 0;
+               }
+               else if( pattern_start >= 0 && ( *c < 'a' || *c > 'z' ) && ( *c < 'A' || *c > 'Z' ) && ( *c < '0' || *c > '9' ) && ( *c != '_' ) ) {
+                       if( *c == ':' && spaces <= 1 ) {
+                               GtkTextIter iter_start, iter_end;
+
+                               gtk_text_buffer_get_iter_at_offset( buffer, &iter_start, pattern_start );
+                               gtk_text_buffer_get_iter_at_offset( buffer, &iter_end, offset );
+                               gtk_text_buffer_apply_tag_by_name( buffer, "bold", &iter_start, &iter_end );
+                       }
+
+                       if( *c == ' ' )
+                               ++spaces;
+                       else
+                               pattern_start = -1;
+               }
+       }
 }
 
 void SurfaceFlags_setEntityClass( EntityClass* eclass ){
@@ -933,7 +1012,7 @@ Creators m_creators;
 public:
 EntityAttributeFactory(){
        m_creators.insert( Creators::value_type( "string", &StatelessAttributeCreator<StringAttribute>::create ) );
-       m_creators.insert( Creators::value_type( "color", &StatelessAttributeCreator<StringAttribute>::create ) );
+       m_creators.insert( Creators::value_type( "color", &StatelessAttributeCreator<ColorAttribute>::create ) );
        m_creators.insert( Creators::value_type( "integer", &StatelessAttributeCreator<StringAttribute>::create ) );
        m_creators.insert( Creators::value_type( "real", &StatelessAttributeCreator<StringAttribute>::create ) );
        m_creators.insert( Creators::value_type( "shader", &StatelessAttributeCreator<ShaderAttribute>::create ) );
@@ -1156,17 +1235,8 @@ void EntityInspector_clearKeyValue(){
 }
 
 static gint EntityInspector_clearKeyValueKB( GtkEntry* widget, GdkEventKey* event, gpointer data ){
-       if ( event->keyval == GDK_Delete ) {
-               // Get current selection text
-               StringOutputStream key( 64 );
-               key << gtk_entry_get_text( g_entityKeyEntry );
-
-               if ( strcmp( key.c_str(), "classname" ) != 0 ) {
-                       StringOutputStream command;
-                       command << "entityDeleteKey -key " << key.c_str();
-                       UndoableCommand undo( command.c_str() );
-                       Scene_EntitySetKeyValue_Selected( key.c_str(), "" );
-               }
+       if ( event->keyval == GDK_KEY_Delete ) {
+               EntityInspector_clearKeyValue();
                return TRUE;
        }
        return FALSE;
@@ -1208,14 +1278,14 @@ static gint EntityClassList_button_press( ui::Widget widget, GdkEventButton *eve
 }
 
 static gint EntityClassList_keypress( ui::Widget widget, GdkEventKey* event, gpointer data ){
-       unsigned int code = gdk_keyval_to_upper( event->keyval );
-
        if ( event->keyval == GDK_KEY_Return ) {
                EntityClassList_createEntity();
                return TRUE;
        }
 
        // select the entity that starts with the key pressed
+/*
+       unsigned int code = gdk_keyval_to_upper( event->keyval );
        if ( code <= 'Z' && code >= 'A' && event->state == 0 ) {
                auto view = ui::TreeView(g_entityClassList);
                GtkTreeModel* model;
@@ -1249,6 +1319,7 @@ static gint EntityClassList_keypress( ui::Widget widget, GdkEventKey* event, gpo
 
                return TRUE;
        }
+*/
        return FALSE;
 }
 
@@ -1278,7 +1349,7 @@ static void SpawnflagCheck_toggled( ui::Widget widget, gpointer data ){
 static gint EntityEntry_keypress( ui::Entry widget, GdkEventKey* event, gpointer data ){
        if ( event->keyval == GDK_KEY_Return ) {
                if ( widget._handle == g_entityKeyEntry._handle ) {
-                       g_entityValueEntry.text( "" );
+                       // g_entityValueEntry.text( "" );
                        gtk_window_set_focus( widget.window(), g_entityValueEntry  );
                }
                else
@@ -1287,7 +1358,7 @@ static gint EntityEntry_keypress( ui::Entry widget, GdkEventKey* event, gpointer
                }
                return TRUE;
        }
-       if ( event->keyval == GDK_Tab ) {
+       if ( event->keyval == GDK_KEY_Tab ) {
                if ( widget._handle == g_entityKeyEntry._handle ) {
                        gtk_window_set_focus( widget.window(), g_entityValueEntry );
                }
@@ -1297,7 +1368,7 @@ static gint EntityEntry_keypress( ui::Entry widget, GdkEventKey* event, gpointer
                }
                return TRUE;
        }
-       if ( event->keyval == GDK_Escape ) {
+       if ( event->keyval == GDK_KEY_Escape ) {
                // gtk_window_set_focus( widget.window(), NULL );
                return TRUE;
        }
@@ -1314,20 +1385,35 @@ void EntityInspector_destroyWindow( ui::Widget widget, gpointer data ){
 }
 
 static gint EntityInspector_hideWindowKB( GtkWidget* widget, GdkEventKey* event, gpointer data ){
-       //if ( event->keyval == GDK_Escape && GTK_WIDGET_VISIBLE( GTK_WIDGET( widget ) ) ) {
-       if ( event->keyval == GDK_Escape  ) {
+       //if ( event->keyval == GDK_KEY_Escape && GTK_WIDGET_VISIBLE( GTK_WIDGET( widget ) ) ) {
+       if ( event->keyval == GDK_KEY_Escape  ) {
                //GroupDialog_showPage( g_page_entity );
                gtk_widget_hide( GTK_WIDGET( GroupDialog_getWindow() ) );
                return TRUE;
        }
        /* this doesn't work, if tab is bound (func is not called then) */
-       if ( event->keyval == GDK_Tab ) {
+       if ( event->keyval == GDK_KEY_Tab ) {
                gtk_window_set_focus( GTK_WINDOW( gtk_widget_get_toplevel( GTK_WIDGET( widget ) ) ), GTK_WIDGET( g_entityKeyEntry ) );
                return TRUE;
        }
        return FALSE;
 }
 
+void EntityInspector_selectTargeting( GtkButton *button, gpointer user_data ){
+       bool focus = gtk_toggle_button_get_active( g_focusToggleButton );
+       Select_ConnectedEntities( true, false, focus );
+}
+
+void EntityInspector_selectTargets( GtkButton *button, gpointer user_data ){
+       bool focus = gtk_toggle_button_get_active( g_focusToggleButton );
+       Select_ConnectedEntities( false, true, focus );
+}
+
+void EntityInspector_selectConnected( GtkButton *button, gpointer user_data ){
+       bool focus = gtk_toggle_button_get_active( g_focusToggleButton );
+       Select_ConnectedEntities( true, true, focus );
+}
+
 ui::Widget EntityInspector_constructWindow( ui::Window toplevel ){
     auto vbox = ui::VBox( FALSE, 2 );
        vbox.show();
@@ -1364,7 +1450,7 @@ ui::Widget EntityInspector_constructWindow( ui::Window toplevel ){
                                        ui::ListStore store = ui::ListStore::from(gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_POINTER ));
 
                                        auto view = ui::TreeView( ui::TreeModel::from( store._handle ));
-                                       gtk_tree_view_set_enable_search(view, FALSE );
+                                       // gtk_tree_view_set_enable_search(view, FALSE );
                                        gtk_tree_view_set_headers_visible( view, FALSE );
                                        view.connect( "button_press_event", G_CALLBACK( EntityClassList_button_press ), 0 );
                                        view.connect( "key_press_event", G_CALLBACK( EntityClassList_keypress ), 0 );
@@ -1406,6 +1492,10 @@ ui::Widget EntityInspector_constructWindow( ui::Window toplevel ){
                                        text.show();
                                        scr.add(text);
                                        g_entityClassComment = text;
+                                       {
+                                               GtkTextBuffer *buffer = gtk_text_view_get_buffer( text );
+                                               gtk_text_buffer_create_tag( buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL );
+                                       }
                                }
                        }
                }
@@ -1524,22 +1614,73 @@ ui::Widget EntityInspector_constructWindow( ui::Window toplevel ){
                                }
 
                                {
-                                       auto hbox = ui::HBox( TRUE, 4 );
+                                       auto hbox = ui::HBox( FALSE, 4 );
                                        hbox.show();
                                        vbox2.pack_start( hbox, FALSE, TRUE, 0 );
 
                                        {
                                                auto button = ui::Button( "Clear All" );
+#define GARUX_DISABLE_BUTTON_NOFOCUS
+#ifndef GARUX_DISABLE_BUTTON_NOFOCUS
+                                               GTK_WIDGET_UNSET_FLAGS( GTK_WIDGET( button ), GTK_CAN_FOCUS );
+#endif // GARUX_DISABLE_BUTTON_NOFOCUS
                                                button.show();
                                                button.connect( "clicked", G_CALLBACK( EntityInspector_clearAllKeyValues ), 0 );
                                                hbox.pack_start( button, TRUE, TRUE, 0 );
                                        }
                                        {
                                                auto button = ui::Button( "Delete Key" );
+#ifndef GARUX_DISABLE_BUTTON_NOFOCUS
+                                               GTK_WIDGET_UNSET_FLAGS( GTK_WIDGET( button ), GTK_CAN_FOCUS );
+#endif // GARUX_DISABLE_BUTTON_NOFOCUS
                                                button.show();
                                                button.connect( "clicked", G_CALLBACK( EntityInspector_clearKeyValue ), 0 );
                                                hbox.pack_start( button, TRUE, TRUE, 0 );
                                        }
+                                       {
+                                               GtkButton* button = GTK_BUTTON( gtk_button_new_with_label( "<" ) );
+                                               gtk_widget_set_tooltip_text( GTK_WIDGET( button ), "Select targeting entities" );
+#ifndef GARUX_DISABLE_BUTTON_NOFOCUS
+                                               GTK_WIDGET_UNSET_FLAGS( GTK_WIDGET( button ), GTK_CAN_FOCUS );
+#endif // GARUX_DISABLE_BUTTON_NOFOCUS
+                                               gtk_widget_show( GTK_WIDGET( button ) );
+                                               g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( EntityInspector_selectTargeting ), 0 );
+                                               gtk_box_pack_start( hbox, GTK_WIDGET( button ), FALSE, FALSE, 0 );
+                                       }
+                                       {
+                                               GtkButton* button = GTK_BUTTON( gtk_button_new_with_label( ">" ) );
+                                               gtk_widget_set_tooltip_text( GTK_WIDGET( button ), "Select targets" );
+#ifndef GARUX_DISABLE_BUTTON_NOFOCUS
+                                               GTK_WIDGET_UNSET_FLAGS( GTK_WIDGET( button ), GTK_CAN_FOCUS );
+#endif // GARUX_DISABLE_BUTTON_NOFOCUS
+                                               gtk_widget_show( GTK_WIDGET( button ) );
+                                               g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( EntityInspector_selectTargets ), 0 );
+                                               gtk_box_pack_start( hbox, GTK_WIDGET( button ), FALSE, FALSE, 0 );
+                                       }
+                                       {
+                                               GtkButton* button = GTK_BUTTON( gtk_button_new_with_label( "<->" ) );
+                                               gtk_widget_set_tooltip_text( GTK_WIDGET( button ), "Select connected entities" );
+#ifndef GARUX_DISABLE_BUTTON_NOFOCUS
+                                               GTK_WIDGET_UNSET_FLAGS( GTK_WIDGET( button ), GTK_CAN_FOCUS );
+#endif // GARUX_DISABLE_BUTTON_NOFOCUS
+                                               gtk_widget_show( GTK_WIDGET( button ) );
+                                               g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( EntityInspector_selectConnected ), 0 );
+                                               gtk_box_pack_start( hbox, GTK_WIDGET( button ), FALSE, FALSE, 0 );
+                                       }
+                                       {
+                                               GtkWidget* button = gtk_toggle_button_new();
+                                               GtkImage* image = GTK_IMAGE( gtk_image_new_from_stock( GTK_STOCK_ZOOM_IN, GTK_ICON_SIZE_SMALL_TOOLBAR ) );
+                                               gtk_widget_show( GTK_WIDGET( image ) );
+                                               gtk_container_add( GTK_CONTAINER( button ), GTK_WIDGET( image ) );
+                                               gtk_button_set_relief( GTK_BUTTON( button ), GTK_RELIEF_NONE );
+#ifndef GARUX_DISABLE_BUTTON_NOFOCUS
+                                               GTK_WIDGET_UNSET_FLAGS( button, GTK_CAN_FOCUS );
+#endif // GARUX_DISABLE_BUTTON_NOFOCUS
+                                               gtk_box_pack_start( hbox, button, FALSE, FALSE, 0 );
+                                               gtk_widget_set_tooltip_text( button, "Focus on Selected" );
+                                               gtk_widget_show( button );
+                                               g_focusToggleButton = GTK_TOGGLE_BUTTON( button );
+                                       }
                                }
                        }