From 5ebdbcafcb2703f14f33738f1e852e5a3e1ab33a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 16 Oct 2020 16:44:06 +0200 Subject: Animation: Snap Cursor Value operator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add operator to snap the 2D Cursor value to selected keyframes. This is doing almost the same as the "Cursor to Selected" operator, except that it doesn't affect the current frame, just the Y-coordinate (the value) of the 2D cursor. The "snap cursor" operators are added to the Key → Snap menu and to the Snap pie menu. This means that these menus are now extended in meaning, to not only mean "snap the selected keyframes to the cursor", but also for some options "snap the cursor to selected keyframes". This fixes T76596. --- source/blender/editors/space_graph/graph_edit.c | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'source/blender/editors/space_graph/graph_edit.c') diff --git a/source/blender/editors/space_graph/graph_edit.c b/source/blender/editors/space_graph/graph_edit.c index 3ec68971dea..c9b26674351 100644 --- a/source/blender/editors/space_graph/graph_edit.c +++ b/source/blender/editors/space_graph/graph_edit.c @@ -2838,6 +2838,46 @@ void GRAPH_OT_frame_jump(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } +/* snap 2D cursor value to the average value of selected keyframe */ +static int graphkeys_snap_cursor_value_exec(bContext *C, wmOperator *UNUSED(op)) +{ + bAnimContext ac; + + if (ANIM_animdata_get_context(C, &ac) == 0) { + return OPERATOR_CANCELLED; + } + + const KeyframeEditData keyframe_sum = sum_selected_keyframes(&ac); + const float sum_value = keyframe_sum.f2; + const int num_keyframes = keyframe_sum.i1; + + if (num_keyframes == 0) { + return OPERATOR_FINISHED; + } + + SpaceGraph *sipo = (SpaceGraph *)ac.sl; + sipo->cursorVal = sum_value / (float)num_keyframes; + // WM_event_add_notifier(C, NC_SCENE | ND_FRAME, ac.scene); + ED_region_tag_redraw(CTX_wm_region(C)); + + return OPERATOR_FINISHED; +} + +void GRAPH_OT_snap_cursor_value(wmOperatorType *ot) +{ + /* Identifiers. */ + ot->name = "Snap Cursor Value to Selected"; + ot->idname = "GRAPH_OT_snap_cursor_value"; + ot->description = "Place the cursor value on the average value of selected keyframes"; + + /* API callbacks. */ + ot->exec = graphkeys_snap_cursor_value_exec; + ot->poll = graphkeys_framejump_poll; + + /* Flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + /* ******************** Snap Keyframes Operator *********************** */ /* defines for snap keyframes tool */ -- cgit v1.2.3