UI: Add angle snapping to line gesture tools

This adds support for snapping for line gesture tool. It is implemented
in the modal keymap as Snap, which is a toggle (similar to how snapping
in the transform operator works).

Right now it snaps the angle of the line to 15 degree increments, which
is defined in code. This should be easy to expose in the UI in the future
if we need to.

Reviewed By: Severin

Differential Revision: https://developer.blender.org/D9115
This commit is contained in:
Pablo Dobarro 2020-10-05 21:05:45 +02:00
parent cf8aa20967
commit 14d56b4217
Notes: blender-bot 2023-02-14 09:36:46 +01:00
Referenced by commit 503d79cd77, Fix straightline gesture snapping not working for modal tools
5 changed files with 52 additions and 2 deletions

View File

@ -5134,6 +5134,7 @@ def km_gesture_straight_line(_params):
("BEGIN", {"type": 'LEFTMOUSE', "value": 'PRESS'}, None),
("SELECT", {"type": 'LEFTMOUSE', "value": 'RELEASE', "any": True}, None),
("MOVE", {"type": 'SPACE', "value": 'ANY', "repeat": False, "any": True}, None),
("SNAP", {"type": 'LEFT_CTRL', "value": 'ANY', "any": True, "repeat": False}, None),
])
return keymap

View File

@ -500,6 +500,9 @@ typedef struct wmGesture {
uint wait_for_input : 1;
/** Use for gestures that can be moved, like box selection */
uint move : 1;
/** For gestures that support snapping, stores if snapping is enabled using the modal keymap
* toggle. */
uint use_snap : 1;
/**
* customdata

View File

@ -884,6 +884,32 @@ int WM_gesture_straightline_active_side_invoke(bContext *C, wmOperator *op, cons
return OPERATOR_RUNNING_MODAL;
}
#define STRAIGHTLINE_SNAP_DEG 15.0f
static void wm_gesture_straightline_do_angle_snap(rcti *rect)
{
const float line_start[2] = {rect->xmin, rect->ymin};
const float line_end[2] = {rect->xmax, rect->ymax};
const float x_axis[2] = {1.0f, 0.0f};
float line_direction[2];
sub_v2_v2v2(line_direction, line_end, line_start);
const float line_length = normalize_v2(line_direction);
const float angle = angle_signed_v2v2(x_axis, line_direction);
const float angle_deg = RAD2DEG(angle) + (STRAIGHTLINE_SNAP_DEG / 2.0f);
const float angle_snapped_deg = -floorf(angle_deg / STRAIGHTLINE_SNAP_DEG) *
STRAIGHTLINE_SNAP_DEG;
const float angle_snapped = DEG2RAD(angle_snapped_deg);
float line_snapped_end[2];
rotate_v2_v2fl(line_snapped_end, x_axis, angle_snapped);
mul_v2_fl(line_snapped_end, line_length);
add_v2_v2(line_snapped_end, line_start);
rect->xmax = (int)line_snapped_end[0];
rect->ymax = (int)line_snapped_end[1];
}
/**
* This modal callback calls exec once per mouse move event while the gesture is active with the
* updated line start and end values, so it can be used for tools that have a real time preview
@ -912,6 +938,10 @@ int WM_gesture_straightline_modal(bContext *C, wmOperator *op, const wmEvent *ev
gesture_straightline_apply(C, op);
}
if (gesture->use_snap) {
wm_gesture_straightline_do_angle_snap(rect);
}
wm_gesture_tag_redraw(win);
}
else if (event->type == EVT_MODAL_MAP) {
@ -925,6 +955,10 @@ int WM_gesture_straightline_modal(bContext *C, wmOperator *op, const wmEvent *ev
wm_gesture_tag_redraw(win);
}
break;
case GESTURE_MODAL_SNAP:
/* Toggle snapping on/off. */
gesture->use_snap = !gesture->use_snap;
break;
case GESTURE_MODAL_SELECT:
if (gesture_straightline_apply(C, op)) {
gesture_modal_end(C, op);
@ -971,6 +1005,10 @@ int WM_gesture_straightline_oneshot_modal(bContext *C, wmOperator *op, const wmE
rect->ymax = event->y - gesture->winrct.ymin;
}
if (gesture->use_snap) {
wm_gesture_straightline_do_angle_snap(rect);
}
wm_gesture_tag_redraw(win);
}
else if (event->type == EVT_MODAL_MAP) {
@ -984,6 +1022,10 @@ int WM_gesture_straightline_oneshot_modal(bContext *C, wmOperator *op, const wmE
wm_gesture_tag_redraw(win);
}
break;
case GESTURE_MODAL_SNAP:
/* Toggle snapping on/off. */
gesture->use_snap = !gesture->use_snap;
break;
case GESTURE_MODAL_SELECT:
case GESTURE_MODAL_DESELECT:
case GESTURE_MODAL_IN:

View File

@ -3806,6 +3806,7 @@ static void gesture_straightline_modal_keymap(wmKeyConfig *keyconf)
{GESTURE_MODAL_SELECT, "SELECT", 0, "Select", ""},
{GESTURE_MODAL_BEGIN, "BEGIN", 0, "Begin", ""},
{GESTURE_MODAL_MOVE, "MOVE", 0, "Move", ""},
{GESTURE_MODAL_SNAP, "SNAP", 0, "Snap", ""},
{0, NULL, 0, NULL, NULL},
};
@ -3822,6 +3823,8 @@ static void gesture_straightline_modal_keymap(wmKeyConfig *keyconf)
WM_modalkeymap_assign(keymap, "IMAGE_OT_sample_line");
WM_modalkeymap_assign(keymap, "PAINT_OT_weight_gradient");
WM_modalkeymap_assign(keymap, "MESH_OT_bisect");
WM_modalkeymap_assign(keymap, "PAINT_OT_mask_line_gesture");
WM_modalkeymap_assign(keymap, "SCULPT_OT_project_line_gesture");
}
/* box_select-like modal operators */
@ -3870,8 +3873,6 @@ static void gesture_box_modal_keymap(wmKeyConfig *keyconf)
WM_modalkeymap_assign(keymap, "PAINT_OT_mask_box_gesture");
WM_modalkeymap_assign(keymap, "SCULPT_OT_face_set_box_gesture");
WM_modalkeymap_assign(keymap, "SCULPT_OT_trim_box_gesture");
WM_modalkeymap_assign(keymap, "PAINT_OT_mask_line_gesture");
WM_modalkeymap_assign(keymap, "SCULPT_OT_project_line_gesture");
WM_modalkeymap_assign(keymap, "VIEW2D_OT_zoom_border");
WM_modalkeymap_assign(keymap, "VIEW3D_OT_clip_border");
WM_modalkeymap_assign(keymap, "VIEW3D_OT_render_border");

View File

@ -493,6 +493,9 @@ enum {
/** Move selection area. */
GESTURE_MODAL_MOVE = 12,
/** Toggle to activate snapping (angle snapping for straight line). */
GESTURE_MODAL_SNAP = 13,
};
#ifdef __cplusplus