#include "DNA_object_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h" // Temporary, for snapping to other unselected meshes
+#include "DNA_node_types.h"
#include "DNA_space_types.h"
#include "DNA_screen_types.h"
#include "DNA_view3d_types.h"
#include "ED_armature.h"
#include "ED_image.h"
#include "ED_mesh.h"
+#include "ED_node.h"
#include "ED_uvedit.h"
#include "ED_view3d.h"
/****************** IMPLEMENTATIONS *********************/
+static int snapNodeTest(View2D *v2d, bNode *node, SnapMode mode);
+static NodeBorder snapNodeBorder(int snap_node_mode);
+
#if 0
int BIF_snappingSupported(Object *obedit)
{
void drawSnapping(const struct bContext *C, TransInfo *t)
{
- if (validSnap(t) && activeSnap(t)) {
-
- unsigned char col[4], selectedCol[4], activeCol[4];
- UI_GetThemeColor3ubv(TH_TRANSFORM, col);
- col[3] = 128;
-
- UI_GetThemeColor3ubv(TH_SELECT, selectedCol);
- selectedCol[3] = 128;
-
- UI_GetThemeColor3ubv(TH_ACTIVE, activeCol);
- activeCol[3] = 192;
-
- if (t->spacetype == SPACE_VIEW3D) {
+ unsigned char col[4], selectedCol[4], activeCol[4];
+
+ if (!activeSnap(t))
+ return;
+
+ UI_GetThemeColor3ubv(TH_TRANSFORM, col);
+ col[3] = 128;
+
+ UI_GetThemeColor3ubv(TH_SELECT, selectedCol);
+ selectedCol[3] = 128;
+
+ UI_GetThemeColor3ubv(TH_ACTIVE, activeCol);
+ activeCol[3] = 192;
+
+ if (t->spacetype == SPACE_VIEW3D) {
+ if (validSnap(t)) {
TransSnapPoint *p;
View3D *v3d = CTX_wm_view3d(C);
RegionView3D *rv3d = CTX_wm_region_view3d(C);
float size;
glDisable(GL_DEPTH_TEST);
-
+
size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
-
+
invert_m4_m4(imat, rv3d->viewmat);
-
+
for (p = t->tsnap.points.first; p; p = p->next) {
if (p == t->tsnap.selectedPoint) {
glColor4ubv(selectedCol);
else {
glColor4ubv(col);
}
-
+
drawcircball(GL_LINE_LOOP, p->co, ED_view3d_pixel_size(rv3d, p->co) * size * 0.75f, imat);
}
-
+
if (t->tsnap.status & POINT_INIT) {
glColor4ubv(activeCol);
-
+
drawcircball(GL_LINE_LOOP, t->tsnap.snapPoint, ED_view3d_pixel_size(rv3d, t->tsnap.snapPoint) * size, imat);
}
/* draw normal if needed */
if (usingSnappingNormal(t) && validSnappingNormal(t)) {
glColor4ubv(activeCol);
-
+
glBegin(GL_LINES);
glVertex3f(t->tsnap.snapPoint[0], t->tsnap.snapPoint[1], t->tsnap.snapPoint[2]);
glVertex3f(t->tsnap.snapPoint[0] + t->tsnap.snapNormal[0],
if (v3d->zbuf)
glEnable(GL_DEPTH_TEST);
}
- else if (t->spacetype == SPACE_IMAGE) {
+ }
+ else if (t->spacetype == SPACE_IMAGE) {
+ if (validSnap(t)) {
/* This will not draw, and Im nor sure why - campbell */
#if 0
float xuser_asp, yuser_asp;
glTranslatef(-t->tsnap.snapPoint[0], -t->tsnap.snapPoint[1], 0.0f);
setlinestyle(0);
#endif
+ }
+ }
+ else if (t->spacetype == SPACE_NODE) {
+ if (validSnap(t)) {
+ ARegion *ar = CTX_wm_region(C);
+ TransSnapPoint *p;
+ float size;
+
+ size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
+
+ glEnable(GL_BLEND);
+ for (p = t->tsnap.points.first; p; p = p->next) {
+ if (p == t->tsnap.selectedPoint) {
+ glColor4ubv(selectedCol);
+ }
+ else {
+ glColor4ubv(col);
+ }
+
+ drawnodesnap(&ar->v2d, p->co, size, 0);
+ }
+
+ if (t->tsnap.status & POINT_INIT) {
+ glColor4ubv(activeCol);
+
+ drawnodesnap(&ar->v2d, t->tsnap.snapPoint, size, t->tsnap.snapNodeBorder);
+ }
+
+ glDisable(GL_BLEND);
}
}
}
t->tsnap.snapNormal[0] = 0;
t->tsnap.snapNormal[1] = 0;
t->tsnap.snapNormal[2] = 0;
+
+ t->tsnap.snapNodeBorder = 0;
}
int usingSnappingNormal(TransInfo *t)
Object *obedit = t->obedit;
Scene *scene = t->scene;
- /* force project off when not supported */
- if (ts->snap_mode != SCE_SNAP_MODE_FACE) {
+ if (t->spacetype == SPACE_NODE) {
+ /* force project off when not supported */
t->tsnap.project = 0;
+
+ t->tsnap.mode = ts->snap_node_mode;
+ }
+ else {
+ /* force project off when not supported */
+ if (ts->snap_mode != SCE_SNAP_MODE_FACE) {
+ t->tsnap.project = 0;
+ }
+
+ t->tsnap.mode = ts->snap_mode;
}
-
- t->tsnap.mode = ts->snap_mode;
if ((t->spacetype == SPACE_VIEW3D || t->spacetype == SPACE_IMAGE) && /* Only 3D view or UV */
(t->flag & T_CAMERA) == 0) /* Not with camera selected in camera view */
t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
}
}
+ else if (t->spacetype == SPACE_NODE)
+ {
+ setSnappingCallback(t);
+
+ if (t->tsnap.applySnap != NULL)
+ {
+ t->tsnap.modeSelect = SNAP_NOT_SELECTED;
+ }
+ else {
+ /* Grid if snap is not possible */
+ t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
+ }
+ }
else {
/* Always grid outside of 3D view */
t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
{
float point[3];
getSnapPoint(t, point);
- sub_v3_v3v3(vec, point, t->tsnap.snapTarget);
+
+ if (t->spacetype == SPACE_NODE) {
+ char border = t->tsnap.snapNodeBorder;
+ if (border & (NODE_LEFT | NODE_RIGHT))
+ vec[0] = point[0] - t->tsnap.snapTarget[0];
+ if (border & (NODE_BOTTOM | NODE_TOP))
+ vec[1] = point[1] - t->tsnap.snapTarget[1];
+ }
+ else {
+ sub_v3_v3v3(vec, point, t->tsnap.snapTarget);
+ }
}
static void ApplySnapRotation(TransInfo *t, float *value)
t->tsnap.status &= ~POINT_INIT;
}
}
+ else if (t->spacetype == SPACE_NODE) {
+ float loc[2];
+ int dist = SNAP_MIN_DISTANCE; // Use a user defined value here
+ char node_border;
+
+ if (snapNodesTransform(t, t->mval, &dist, loc, &node_border, t->tsnap.modeSelect)) {
+ copy_v2_v2(t->tsnap.snapPoint, loc);
+ t->tsnap.snapNodeBorder = node_border;
+
+ t->tsnap.status |= POINT_INIT;
+ }
+ else {
+ t->tsnap.status &= ~POINT_INIT;
+ }
+ }
}
/********************** TARGET **************************/
+static void TargetSnapOffset(TransInfo *t, TransData *td)
+{
+ if (t->spacetype == SPACE_NODE && td != NULL) {
+ bNode *node = td->extra;
+ char border = t->tsnap.snapNodeBorder;
+ float width = node->totr.xmax - node->totr.xmin;
+ float height = node->totr.ymax - node->totr.ymin;
+
+ if (border & NODE_LEFT)
+ t->tsnap.snapTarget[0] -= 0.5f * width;
+ if (border & NODE_RIGHT)
+ t->tsnap.snapTarget[0] += 0.5f * width;
+ if (border & NODE_BOTTOM)
+ t->tsnap.snapTarget[1] -= 0.5f * height;
+ if (border & NODE_TOP)
+ t->tsnap.snapTarget[1] += 0.5f * height;
+ }
+}
+
static void TargetSnapCenter(TransInfo *t)
{
/* Only need to calculate once */
if ((t->tsnap.status & TARGET_INIT) == 0) {
copy_v3_v3(t->tsnap.snapTarget, t->center);
+
if (t->flag & (T_EDIT | T_POSE)) {
Object *ob = t->obedit ? t->obedit : t->poseobj;
mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
}
+ TargetSnapOffset(t, NULL);
+
t->tsnap.status |= TARGET_INIT;
}
}
if (active_td) {
copy_v3_v3(t->tsnap.snapTarget, active_td->center);
-
+
if (t->flag & (T_EDIT | T_POSE)) {
Object *ob = t->obedit ? t->obedit : t->poseobj;
mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
}
+ TargetSnapOffset(t, active_td);
+
t->tsnap.status |= TARGET_INIT;
}
/* No active, default to median */
mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
}
+ TargetSnapOffset(t, NULL);
+
t->tsnap.status |= TARGET_INIT;
}
}
}
}
+ TargetSnapOffset(t, closest);
+
t->tsnap.status |= TARGET_INIT;
}
}
return peelObjects(CTX_data_scene(C), v3d, CTX_wm_region(C), CTX_data_edit_object(C), depth_peels, mval, mode);
}
+/******************** NODES ***********************************/
+
+static int snapNodeTest(View2D *v2d, bNode *node, SnapMode mode)
+{
+ /* node is use for snapping only if a) snap mode matches and b) node is inside the view */
+ return ((mode == SNAP_NOT_SELECTED && !(node->flag & NODE_SELECT)) ||
+ (mode == SNAP_ALL && !(node->flag & NODE_ACTIVE))) &&
+ (node->totr.xmin < v2d->cur.xmax && node->totr.xmax > v2d->cur.xmin &&
+ node->totr.ymin < v2d->cur.ymax && node->totr.ymax > v2d->cur.ymin);
+}
+
+static NodeBorder snapNodeBorder(int snap_node_mode)
+{
+ switch (snap_node_mode) {
+ case SCE_SNAP_MODE_NODE_X:
+ return NODE_LEFT | NODE_RIGHT;
+ case SCE_SNAP_MODE_NODE_Y:
+ return NODE_TOP | NODE_BOTTOM;
+ case SCE_SNAP_MODE_NODE_XY:
+ return NODE_LEFT | NODE_RIGHT | NODE_TOP | NODE_BOTTOM;
+ }
+ return 0;
+}
+
+static int snapNode(ToolSettings *ts, SpaceNode *UNUSED(snode), ARegion *ar, bNode *node, const int mval[2],
+ float r_loc[2], int *r_dist, char *r_node_border)
+{
+ View2D *v2d = &ar->v2d;
+ NodeBorder border = snapNodeBorder(ts->snap_node_mode);
+ int retval = 0;
+ rcti totr;
+ int new_dist;
+
+ UI_view2d_to_region_no_clip(v2d, node->totr.xmin, node->totr.ymin, &totr.xmin, &totr.ymin);
+ UI_view2d_to_region_no_clip(v2d, node->totr.xmax, node->totr.ymax, &totr.xmax, &totr.ymax);
+
+ if (border & NODE_LEFT) {
+ new_dist = abs(totr.xmin - mval[0]);
+ if (new_dist < *r_dist) {
+ UI_view2d_region_to_view(v2d, totr.xmin, mval[1], &r_loc[0], &r_loc[1]);
+ *r_dist = new_dist;
+ *r_node_border = NODE_LEFT;
+ retval = 1;
+ }
+ }
+
+ if (border & NODE_RIGHT) {
+ new_dist = abs(totr.xmax - mval[0]);
+ if (new_dist < *r_dist) {
+ UI_view2d_region_to_view(v2d, totr.xmax, mval[1], &r_loc[0], &r_loc[1]);
+ *r_dist = new_dist;
+ *r_node_border = NODE_RIGHT;
+ retval = 1;
+ }
+ }
+
+ if (border & NODE_BOTTOM) {
+ new_dist = abs(totr.ymin - mval[1]);
+ if (new_dist < *r_dist) {
+ UI_view2d_region_to_view(v2d, mval[0], totr.ymin, &r_loc[0], &r_loc[1]);
+ *r_dist = new_dist;
+ *r_node_border = NODE_BOTTOM;
+ retval = 1;
+ }
+ }
+
+ if (border & NODE_TOP) {
+ new_dist = abs(totr.ymax - mval[1]);
+ if (new_dist < *r_dist) {
+ UI_view2d_region_to_view(v2d, mval[0], totr.ymax, &r_loc[0], &r_loc[1]);
+ *r_dist = new_dist;
+ *r_node_border = NODE_TOP;
+ retval = 1;
+ }
+ }
+
+ return retval;
+}
+
+static int snapNodes(ToolSettings *ts, SpaceNode *snode, ARegion *ar, const int mval[2],
+ int *r_dist, float r_loc[2], char *r_node_border, SnapMode mode)
+{
+ bNodeTree *ntree = snode->edittree;
+ bNode *node;
+ int retval = 0;
+
+ *r_node_border = 0;
+
+ for (node = ntree->nodes.first; node; node = node->next) {
+ if (snapNodeTest(&ar->v2d, node, mode))
+ retval |= snapNode(ts, snode, ar, node, mval, r_loc, r_dist, r_node_border);
+ }
+
+ return retval;
+}
+
+int snapNodesTransform(TransInfo *t, const int mval[2], int *r_dist, float r_loc[2], char *r_node_border, SnapMode mode)
+{
+ return snapNodes(t->settings, t->sa->spacedata.first, t->ar, mval, r_dist, r_loc, r_node_border, mode);
+}
+
+int snapNodesContext(bContext *C, const int mval[2], int *r_dist, float r_loc[2], char *r_node_border, SnapMode mode)
+{
+ Scene *scene = CTX_data_scene(C);
+ return snapNodes(scene->toolsettings, CTX_wm_space_node(C), CTX_wm_region(C), mval, r_dist, r_loc, r_node_border, mode);
+}
+
/*================================================================*/
static void applyGrid(TransInfo *t, float *val, int max_index, float fac[3], GearsType action);
{0, NULL, 0, NULL, NULL}
};
+EnumPropertyItem snap_node_element_items[] = {
+ {SCE_SNAP_MODE_INCREMENT, "INCREMENT", ICON_SNAP_INCREMENT, "Increment", "Snap to increments of grid"},
+ {SCE_SNAP_MODE_NODE_X, "NODE_X", ICON_SNAP_EDGE, "Node X", "Snap to left/right node border"},
+ {SCE_SNAP_MODE_NODE_Y, "NODE_Y", ICON_SNAP_EDGE, "Node Y", "Snap to top/bottom node border"},
+ {SCE_SNAP_MODE_NODE_XY, "NODE_XY", ICON_SNAP_EDGE, "Node X / Y", "Snap to any node border"},
+ {0, NULL, 0, NULL, NULL}
+};
+
/* workaround for duplice enums,
* have each enum line as a defne then conditionally set it or not
RNA_def_property_enum_items(prop, snap_element_items);
RNA_def_property_ui_text(prop, "Snap Element", "Type of element to snap to");
RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, NULL); /* header redraw */
+
+ /* node editor uses own set of snap modes */
+ prop = RNA_def_property(srna, "snap_node_element", PROP_ENUM, PROP_NONE);
+ RNA_def_property_enum_sdna(prop, NULL, "snap_node_mode");
+ RNA_def_property_enum_items(prop, snap_node_element_items);
+ RNA_def_property_ui_text(prop, "Snap Node Element", "Type of element to snap to");
+ RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, NULL); /* header redraw */
prop = RNA_def_property(srna, "snap_target", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "snap_target");
RNA_def_property_ui_text(prop, "Project to Self", "Snap onto itself (editmode)");
RNA_def_property_ui_icon(prop, ICON_ORTHO, 0);
RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, NULL); /* header redraw */
-
+
/* Grease Pencil */
prop = RNA_def_property(srna, "use_grease_pencil_sessions", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "gpencil_flags", GP_TOOL_FLAG_PAINTSESSIONS_ON);