From e936f042589eeb4c64a9b0df7cfd3b70ef49af22 Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Thu, 15 Oct 2020 21:11:31 +0200 Subject: Outliner: Use operator option to decide which item to rename The `outliner.item_rename` operator needs to decide if it should rename the active or the hovered item. Previously it checked if the event is a press event, which is a hacky way of doing this and limit how the operator can be used in the keymap. Now use a operator option to let this be controlled on the keymap level. Doesn't change any default behavior. --- .../blender/editors/space_outliner/outliner_edit.c | 61 ++++++++++++++-------- 1 file changed, 40 insertions(+), 21 deletions(-) (limited to 'source/blender/editors/space_outliner/outliner_edit.c') diff --git a/source/blender/editors/space_outliner/outliner_edit.c b/source/blender/editors/space_outliner/outliner_edit.c index 4a9aa984398..779efc3cacb 100644 --- a/source/blender/editors/space_outliner/outliner_edit.c +++ b/source/blender/editors/space_outliner/outliner_edit.c @@ -363,35 +363,48 @@ void item_rename_fn(bContext *C, do_item_rename(region, te, tselem, reports); } -static int outliner_item_rename(bContext *C, wmOperator *op, const wmEvent *event) +static TreeElement *outliner_item_rename_find_active(const SpaceOutliner *space_outliner, + ReportList *reports) { - ARegion *region = CTX_wm_region(C); - SpaceOutliner *space_outliner = CTX_wm_space_outliner(C); - float fmval[2]; + TreeElement *active_element = outliner_find_element_with_flag(&space_outliner->tree, TSE_ACTIVE); - /* Rename active element if key pressed, otherwise rename element at cursor coordinates */ - if (event->val == KM_PRESS) { - TreeElement *active_element = outliner_find_element_with_flag(&space_outliner->tree, - TSE_ACTIVE); + if (!active_element) { + BKE_report(reports, RPT_WARNING, "No active item to rename"); + return NULL; + } - if (active_element) { - do_item_rename(region, active_element, TREESTORE(active_element), op->reports); - } - else { - BKE_report(op->reports, RPT_WARNING, "No active item to rename"); - } + return active_element; +} + +static TreeElement *outliner_item_rename_find_hovered(const SpaceOutliner *space_outliner, + ARegion *region, + const wmEvent *event) +{ + float fmval[2]; + UI_view2d_region_to_view(®ion->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]); + + TreeElement *hovered = outliner_find_item_at_y(space_outliner, &space_outliner->tree, fmval[1]); + if (hovered && outliner_item_is_co_over_name(hovered, fmval[0])) { + return hovered; } - else { - UI_view2d_region_to_view(®ion->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]); - TreeElement *hovered = outliner_find_item_at_y( - space_outliner, &space_outliner->tree, fmval[1]); + return NULL; +} - if (hovered && outliner_item_is_co_over_name(hovered, fmval[0])) { - do_item_rename(region, hovered, TREESTORE(hovered), op->reports); - } +static int outliner_item_rename(bContext *C, wmOperator *op, const wmEvent *event) +{ + ARegion *region = CTX_wm_region(C); + SpaceOutliner *space_outliner = CTX_wm_space_outliner(C); + const bool use_active = RNA_boolean_get(op->ptr, "use_active"); + + TreeElement *te = use_active ? outliner_item_rename_find_active(space_outliner, op->reports) : + outliner_item_rename_find_hovered(space_outliner, region, event); + if (!te) { + return OPERATOR_CANCELLED; } + do_item_rename(region, te, TREESTORE(te), op->reports); + return OPERATOR_FINISHED; } @@ -407,6 +420,12 @@ void OUTLINER_OT_item_rename(wmOperatorType *ot) /* Flags. */ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + RNA_def_boolean(ot->srna, + "use_active", + false, + "Use Active", + "Rename the active item, rather than the one the mouse is over"); } /** \} */ -- cgit v1.2.3