Added backdrop image color information to the node editor. When clicking on empty...
authorLukas Toenne <lukas.toenne@googlemail.com>
Mon, 31 Jan 2011 14:42:55 +0000 (14:42 +0000)
committerLukas Toenne <lukas.toenne@googlemail.com>
Mon, 31 Jan 2011 14:42:55 +0000 (14:42 +0000)
source/blender/editors/space_node/CMakeLists.txt
source/blender/editors/space_node/drawnode.c
source/blender/editors/space_node/node_edit.c
source/blender/editors/space_node/node_intern.h
source/blender/editors/space_node/node_ops.c

index 6ade219583a8dda1ea9995388c67d8754fc93ff8..c615f5cc85f35caa83f843dcc35c3528b93f30e4 100644 (file)
@@ -21,6 +21,7 @@
 
 set(INC
        ../include
+       ../../blenfont
        ../../blenkernel
        ../../blenlib
        ../../imbuf
index a17fceb4fffb51d9b2b0d237efcde5d078e8e68f..31706154dc2cc944b0a5f80ad47907e0fa79fe20 100644 (file)
@@ -55,6 +55,8 @@
 #include "BIF_gl.h"
 #include "BIF_glutil.h"
 
+#include "BLF_api.h"
+
 #include "MEM_guardedalloc.h"
 
 
@@ -1409,6 +1411,47 @@ void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode, int color_manage)
        }
 }
 
+void draw_nodespace_color_info(ARegion *ar, int channels, int x, int y, char *cp, float *fp)
+{
+       char str[256];
+       int ofs;
+       
+       ofs= sprintf(str, "X: %4d Y: %4d ", x, y);
+
+       if(channels==4) {
+               if(cp)
+                       ofs+= sprintf(str+ofs, "| R: %3d G: %3d B: %3d A: %3d ", cp[0], cp[1], cp[2], cp[3]);
+               if (fp)
+                       ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f A: %.3f ", fp[0], fp[1], fp[2], fp[3]);
+       }
+       else if(channels==1) {
+               if(cp)
+                       ofs+= sprintf(str+ofs, "| Val: %3d ", cp[0]);
+               if (fp)
+                       ofs+= sprintf(str+ofs, "| Val: %.3f ", fp[0]);
+       }
+       else if(channels==3) {
+               if(cp)
+                       ofs+= sprintf(str+ofs, "| R: %3d G: %3d B: %3d ", cp[0], cp[1], cp[2]);
+               if (fp)
+                       ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f ", fp[0], fp[1], fp[2]);
+       }
+
+       glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
+       glEnable(GL_BLEND);
+       
+       glColor4f(.0,.0,.0,.25);
+       glRecti(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 20);
+       glDisable(GL_BLEND);
+       
+       glColor3ub(255, 255, 255);
+       
+       // UI_DrawString(6, 6, str); // works ok but fixed width is nicer.
+       BLF_size(blf_mono_font, 11, 72);
+       BLF_position(blf_mono_font, 6, 6, 0);
+       BLF_draw_ascii(blf_mono_font, str, sizeof(str));
+}
+
 #if 0
 /* note: needs to be userpref or opengl profile option */
 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
index cf5822d04617d5d61f2cd6b87eb8d3414e5dc01b..60eba173e1dca22fea03293e8b47a673b1fcf696 100644 (file)
@@ -53,6 +53,7 @@
 #include "BKE_node.h"
 #include "BKE_material.h"
 #include "BKE_paint.h"
+#include "BKE_screen.h"
 #include "BKE_texture.h"
 #include "BKE_report.h"
 
@@ -62,6 +63,7 @@
 
 #include "ED_node.h"
 #include "ED_screen.h"
+#include "ED_space_api.h"
 #include "ED_render.h"
 
 #include "RNA_access.h"
@@ -73,6 +75,8 @@
 #include "UI_interface.h"
 #include "UI_view2d.h"
 
+#include "IMB_imbuf.h"
+
 #include "node_intern.h"
 
 #define SOCK_IN                1
@@ -869,6 +873,169 @@ void NODE_OT_backimage_zoom(wmOperatorType *ot)
        RNA_def_float(ot->srna, "factor", 1.2f, 0.0f, 10.0f, "Factor", "", 0.0f, 10.0f);
 }
 
+/******************** sample backdrop operator ********************/
+
+typedef struct ImageSampleInfo {
+       ARegionType *art;
+       void *draw_handle;
+       int x, y;
+       int channels;
+       int color_manage;
+
+       char col[4];
+       float colf[4];
+
+       int draw;
+} ImageSampleInfo;
+
+static void sample_draw(const bContext *UNUSED(C), ARegion *ar, void *arg_info)
+{
+       ImageSampleInfo *info= arg_info;
+
+       draw_nodespace_color_info(ar, info->channels, info->x, info->y, info->col, info->colf);
+}
+
+static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
+{
+       SpaceNode *snode= CTX_wm_space_node(C);
+       ARegion *ar= CTX_wm_region(C);
+       ImageSampleInfo *info= op->customdata;
+       void *lock;
+       Image *ima;
+       ImBuf *ibuf;
+       float fx, fy, bufx, bufy;
+       int mx, my;
+       
+       ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
+       ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
+       if(!ibuf)
+               return;
+       
+       if(!ibuf->rect) {
+               if(info->color_manage)
+                       ibuf->profile = IB_PROFILE_LINEAR_RGB;
+               else
+                       ibuf->profile = IB_PROFILE_NONE;
+               IMB_rect_from_float(ibuf);
+       }
+
+       mx= event->x - ar->winrct.xmin;
+       my= event->y - ar->winrct.ymin;
+       /* map the mouse coords to the backdrop image space */
+       bufx = ibuf->x * snode->zoom;
+       bufy = ibuf->y * snode->zoom;
+       fx = (bufx > 0.0f ? ((float)mx - 0.5f*ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
+       fy = (bufy > 0.0f ? ((float)my - 0.5f*ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
+
+       if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
+               float *fp;
+               char *cp;
+               int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
+
+               CLAMP(x, 0, ibuf->x-1);
+               CLAMP(y, 0, ibuf->y-1);
+
+               info->x= x;
+               info->y= y;
+               info->draw= 1;
+               info->channels= ibuf->channels;
+
+               if(ibuf->rect) {
+                       cp= (char *)(ibuf->rect + y*ibuf->x + x);
+
+                       info->col[0]= cp[0];
+                       info->col[1]= cp[1];
+                       info->col[2]= cp[2];
+                       info->col[3]= cp[3];
+
+                       info->colf[0]= (float)cp[0]/255.0f;
+                       info->colf[1]= (float)cp[1]/255.0f;
+                       info->colf[2]= (float)cp[2]/255.0f;
+                       info->colf[3]= (float)cp[3]/255.0f;
+               }
+               if(ibuf->rect_float) {
+                       fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
+
+                       info->colf[0]= fp[0];
+                       info->colf[1]= fp[1];
+                       info->colf[2]= fp[2];
+                       info->colf[3]= fp[3];
+               }
+       }
+       else
+               info->draw= 0;
+
+       BKE_image_release_ibuf(ima, lock);
+       
+       ED_area_tag_redraw(CTX_wm_area(C));
+}
+
+static void sample_exit(bContext *C, wmOperator *op)
+{
+       ImageSampleInfo *info= op->customdata;
+
+       ED_region_draw_cb_exit(info->art, info->draw_handle);
+       ED_area_tag_redraw(CTX_wm_area(C));
+       MEM_freeN(info);
+}
+
+static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
+{
+       SpaceNode *snode= CTX_wm_space_node(C);
+       ARegion *ar= CTX_wm_region(C);
+       ImageSampleInfo *info;
+
+       if(snode->treetype!=NTREE_COMPOSIT || !(snode->flag & SNODE_BACKDRAW))
+               return OPERATOR_CANCELLED;
+       
+       info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
+       info->art= ar->type;
+       info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
+       op->customdata= info;
+
+       sample_apply(C, op, event);
+
+       WM_event_add_modal_handler(C, op);
+
+       return OPERATOR_RUNNING_MODAL;
+}
+
+static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
+{
+       switch(event->type) {
+               case LEFTMOUSE:
+               case RIGHTMOUSE: // XXX hardcoded
+                       sample_exit(C, op);
+                       return OPERATOR_CANCELLED;
+               case MOUSEMOVE:
+                       sample_apply(C, op, event);
+                       break;
+       }
+
+       return OPERATOR_RUNNING_MODAL;
+}
+
+static int sample_cancel(bContext *C, wmOperator *op)
+{
+       sample_exit(C, op);
+       return OPERATOR_CANCELLED;
+}
+
+void NODE_OT_backimage_sample(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Backimage Sample";
+       ot->idname= "NODE_OT_backimage_sample";
+       
+       /* api callbacks */
+       ot->invoke= sample_invoke;
+       ot->modal= sample_modal;
+       ot->cancel= sample_cancel;
+       ot->poll= ED_operator_node_active;
+
+       /* flags */
+       ot->flag= OPTYPE_BLOCKING;
+}
 
 /* ********************** size widget operator ******************** */
 
index cfe32846124582e1fb632148be3554b02cd073a3..66bf9310db876373140c4b3de79d68416f8daeff 100644 (file)
@@ -71,6 +71,7 @@ void node_draw_link(View2D *v2d, SpaceNode *snode, bNodeLink *link);
 void node_draw_link_bezier(View2D *v2d, SpaceNode *snode, bNodeLink *link, int th_col1, int do_shaded, int th_col2, int do_triple, int th_col3 );
 int node_link_bezier_points(View2D *v2d, SpaceNode *snode, bNodeLink *link, float coord_array[][2], int resol);
 void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode, int color_manage);
+void draw_nodespace_color_info(ARegion *ar, int channels, int x, int y, char *cp, float *fp);
 
 void node_buts_group(struct uiLayout *layout, struct bContext *C, struct PointerRNA *ptr);
 
@@ -114,8 +115,10 @@ void NODE_OT_show_cyclic_dependencies(struct wmOperatorType *ot);
 void NODE_OT_link_viewer(struct wmOperatorType *ot);
 void NODE_OT_read_fullsamplelayers(struct wmOperatorType *ot);
 void NODE_OT_read_renderlayers(struct wmOperatorType *ot);
+
 void NODE_OT_backimage_move(struct wmOperatorType *ot);
 void NODE_OT_backimage_zoom(struct wmOperatorType *ot);
+void NODE_OT_backimage_sample(wmOperatorType *ot);
 
 void NODE_OT_add_file(struct wmOperatorType *ot);
 
index 46cd8515f231e19128a9844f450026082544a790..aa29f3e3d14a6408be85db1fadb2d4c1ab5fb6cd 100644 (file)
@@ -81,6 +81,7 @@ void node_operatortypes(void)
        
        WM_operatortype_append(NODE_OT_backimage_move);
        WM_operatortype_append(NODE_OT_backimage_zoom);
+       WM_operatortype_append(NODE_OT_backimage_sample);
        
        WM_operatortype_append(NODE_OT_add_file);
 }
@@ -136,7 +137,7 @@ void node_keymap(struct wmKeyConfig *keyconf)
                RNA_float_set(kmi->ptr, "factor", 0.83333f);
        kmi= WM_keymap_add_item(keymap, "NODE_OT_backimage_zoom", VKEY, KM_PRESS, KM_ALT, 0);
                RNA_float_set(kmi->ptr, "factor", 1.2f);
-       
+       WM_keymap_add_item(keymap, "NODE_OT_backimage_sample", ACTIONMOUSE, KM_PRESS, 0, 0);
        
        WM_keymap_add_item(keymap, "NODE_OT_link_make", FKEY, KM_PRESS, 0, 0);
        RNA_boolean_set(WM_keymap_add_item(keymap, "NODE_OT_link_make", FKEY, KM_PRESS, KM_CTRL, 0)->ptr, "replace", 1);