***Drag and drop fun!***
authorWilliam Reynish <william@reynish.com>
Sun, 9 May 2010 18:07:17 +0000 (18:07 +0000)
committerWilliam Reynish <william@reynish.com>
Sun, 9 May 2010 18:07:17 +0000 (18:07 +0000)
Added ability to drag images and movies directly onto objects to assign them as textures.

You can drag them from the file browser, directly from the OS or even from other apps. Here's a video to demonstrate:

http://www.youtube.com/watch?v=fGe2U8F_JvE

Ton wanted to show me how to add it, but he ended up doing almost all of the coding himself ;)

Ton/Matt: Dropping a text file in the Text Editor fails for some reason. It aught to work - probably a keymap conflict of some sorts?

source/blender/editors/mesh/mesh_data.c
source/blender/editors/space_outliner/outliner_ops.c
source/blender/editors/space_sequencer/space_sequencer.c
source/blender/editors/space_text/space_text.c
source/blender/editors/space_view3d/space_view3d.c

index 70352e9..4eae254 100644 (file)
@@ -43,6 +43,7 @@
 #include "BKE_depsgraph.h"
 #include "BKE_displist.h"
 #include "BKE_global.h"
+#include "BKE_image.h"
 #include "BKE_library.h"
 #include "BKE_material.h"
 #include "BKE_mesh.h"
@@ -319,11 +320,25 @@ static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
        char name[32];
        
        /* check input variables */
-       RNA_string_get(op->ptr, "name", name);
-       ima= (Image *)find_id("IM", name);
-       if(base==NULL || base->object->type!=OB_MESH || ima==NULL) {
-               BKE_report(op->reports, RPT_ERROR, "Not a Mesh or no Image.");
-               return OPERATOR_CANCELLED;
+       if(RNA_property_is_set(op->ptr, "path")) {
+               char path[FILE_MAX];
+               
+               RNA_string_get(op->ptr, "path", path);
+               ima= BKE_add_image_file(path, 
+                                                               scene ? scene->r.cfra : 1);
+               
+               if(!ima) {
+                       BKE_report(op->reports, RPT_ERROR, "Not an Image.");
+                       return OPERATOR_CANCELLED;
+               }
+       }
+       else {
+               RNA_string_get(op->ptr, "name", name);
+               ima= (Image *)find_id("IM", name);
+               if(base==NULL || base->object->type!=OB_MESH || ima==NULL) {
+                       BKE_report(op->reports, RPT_ERROR, "Not a Mesh or no Image.");
+                       return OPERATOR_CANCELLED;
+               }
        }
        
        /* turn mesh in editmode */
@@ -368,6 +383,7 @@ void MESH_OT_drop_named_image(wmOperatorType *ot)
        
        /* properties */
        RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Image name to assign.");
+       RNA_def_string(ot->srna, "path", "Path", FILE_MAX, "Filepath", "Path to image file");
 }
 
 static int uv_texture_remove_exec(bContext *C, wmOperator *op)
index fcd6e27..d23e950 100644 (file)
@@ -75,6 +75,8 @@ void outliner_keymap(wmKeyConfig *keyconf)
 {
        wmKeyMap *keymap= WM_keymap_find(keyconf, "Outliner", SPACE_OUTLINER, 0);
        
+       WM_keymap_add_item(keymap, "OUTLINER_OT_item_rename", LEFTMOUSE, KM_DBL_CLICK, 0, 0);
+       
        RNA_boolean_set(WM_keymap_add_item(keymap, "OUTLINER_OT_item_activate", LEFTMOUSE, KM_PRESS, 0, 0)->ptr, "extend", 0);
        RNA_boolean_set(WM_keymap_add_item(keymap, "OUTLINER_OT_item_activate", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", 1);
        
index 0737439..b19e865 100644 (file)
@@ -306,7 +306,7 @@ static int image_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
 static int movie_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
 {
        if(drag->type==WM_DRAG_PATH)
-               if(ELEM(drag->icon, ICON_FILE_MOVIE, ICON_FILE_BLANK))  /* rule might not work? */
+               if(ELEM3(drag->icon, 0, ICON_FILE_MOVIE, ICON_FILE_BLANK))      /* rule might not work? */
                        return 1;
        return 0;
 }
index 2e78a50..8b46617 100644 (file)
@@ -368,6 +368,36 @@ static void text_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
        WM_cursor_set(win, BC_TEXTEDITCURSOR);
 }
 
+
+
+/* ************* dropboxes ************* */
+
+static int text_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
+{
+       if(drag->type==WM_DRAG_PATH)
+               if(ELEM(drag->icon, 0, ICON_FILE_BLANK))        /* rule might not work? */
+                       return 1;
+       return 0;
+}
+
+static void text_drop_copy(wmDrag *drag, wmDropBox *drop)
+{
+       /* copy drag path to properties */
+       RNA_string_set(drop->ptr, "path", drag->path);
+}
+
+/* this region dropbox definition */
+static void text_dropboxes(void)
+{
+       ListBase *lb= WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
+       
+       WM_dropbox_add(lb, "TEXT_OT_open", text_drop_poll, text_drop_copy);
+
+}
+
+/* ************* end drop *********** */
+
+
 /****************** header region ******************/
 
 /* add handlers, stuff you only do once or on area/region changes */
@@ -413,6 +443,7 @@ void ED_spacetype_text(void)
        st->keymap= text_keymap;
        st->listener= text_listener;
        st->context= text_context;
+       st->dropboxes = text_dropboxes;
        
        /* regions: main window */
        art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
index a37ecda..326409f 100644 (file)
@@ -57,6 +57,8 @@
 
 #include "RNA_access.h"
 
+#include "UI_resources.h"
+
 #include "view3d_intern.h"     // own include
 
 /* ******************** manage regions ********************* */
@@ -416,6 +418,10 @@ static int view3d_ima_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
                if( GS(id->name)==ID_IM )
                        return 1;
        }
+       else if(drag->type==WM_DRAG_PATH){
+               if(ELEM(drag->icon, 0, ICON_FILE_IMAGE))        /* rule might not work? */
+                       return 1;
+       }
        return 0;
 }
 
@@ -435,10 +441,20 @@ static void view3d_ob_drop_copy(wmDrag *drag, wmDropBox *drop)
 static void view3d_id_drop_copy(wmDrag *drag, wmDropBox *drop)
 {
        ID *id= (ID *)drag->poin;
-
+       
        RNA_string_set(drop->ptr, "name", id->name+2);
 }
 
+static void view3d_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
+{
+       ID *id= (ID *)drag->poin;
+       
+       if(id)
+               RNA_string_set(drop->ptr, "name", id->name+2);
+       if(drag->path) 
+               RNA_string_set(drop->ptr, "path", drag->path);
+}
+
 
 /* region dropbox definition */
 static void view3d_dropboxes(void)
@@ -447,7 +463,7 @@ static void view3d_dropboxes(void)
        
        WM_dropbox_add(lb, "OBJECT_OT_add_named_cursor", view3d_ob_drop_poll, view3d_ob_drop_copy);
        WM_dropbox_add(lb, "OBJECT_OT_drop_named_material", view3d_mat_drop_poll, view3d_id_drop_copy);
-       WM_dropbox_add(lb, "MESH_OT_drop_named_image", view3d_ima_drop_poll, view3d_id_drop_copy);
+       WM_dropbox_add(lb, "MESH_OT_drop_named_image", view3d_ima_drop_poll, view3d_id_path_drop_copy);
 }