Gizmo: add per gizmo keymaps

Remove click-drag support for tweak gizmo,
rely on keymap events instead.

This is needed for some gizmos to use modifiers keys
without having all gizmos use all modifier keys (see: T63996).
This commit is contained in:
2019-05-28 02:14:31 +10:00
parent d83d376c02
commit 08dff7b40b
8 changed files with 69 additions and 118 deletions

View File

@@ -208,6 +208,13 @@ def _template_items_gizmo_tweak_value():
]
def _template_items_gizmo_tweak_value_click_drag():
return [
("gizmogroup.gizmo_tweak", {"type": 'LEFTMOUSE', "value": 'CLICK', "any": True}, None),
("gizmogroup.gizmo_tweak", {"type": 'EVT_TWEAK_L', "value": 'ANY', "any": True}, None),
]
def _template_items_gizmo_tweak_modal():
return [
("CANCEL", {"type": 'ESC', "value": 'PRESS', "any": True}, None),
@@ -4724,6 +4731,15 @@ def km_generic_gizmos(_params):
return keymap
def km_generic_gizmos_click_drag(_params):
keymap = (
"Generic Gizmos Click Drag",
{"space_type": 'EMPTY', "region_type": 'WINDOW'},
{"items": _template_items_gizmo_tweak_value_click_drag()},
)
return keymap
def km_generic_gizmos_tweak_modal_map(_params):
keymap = (
"Generic Gizmos Tweak Modal Map",
@@ -5838,6 +5854,7 @@ def generate_keymaps(params=None):
# Gizmos.
km_generic_gizmos(params),
km_generic_gizmos_click_drag(params),
km_generic_gizmos_tweak_modal_map(params),
km_generic_gizmos_select(params),
km_generic_gizmos_select_tweak_modal_map(params),

View File

@@ -883,20 +883,25 @@ static uiTooltipData *ui_tooltip_data_from_gizmo(bContext *C, wmGizmo *gz)
/* Operator Actions */
{
bool use_drag = gz->drag_part != -1 && gz->highlight_part != gz->drag_part;
const struct {
int part;
const char *prefix;
} gzop_actions[] = {
{
.part = gz->highlight_part,
.prefix = use_drag ? CTX_TIP_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Click") : NULL,
},
{
.part = use_drag ? gz->drag_part : -1,
.prefix = use_drag ? CTX_TIP_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Drag") : NULL,
},
#if 0
{
.part = gz->highlight_part,
.prefix = use_drag ? CTX_TIP_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Click") : NULL,
},
{
.part = use_drag ? gz->drag_part : -1,
.prefix = use_drag ? CTX_TIP_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Drag") : NULL,
},
#else
{
.part = gz->highlight_part,
.prefix = NULL,
},
#endif
};
for (int i = 0; i < ARRAY_SIZE(gzop_actions); i++) {

View File

@@ -131,7 +131,7 @@ static bool WIDGETGROUP_navigate_poll(const bContext *C, wmGizmoGroupType *UNUSE
return true;
}
static void WIDGETGROUP_navigate_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
static void WIDGETGROUP_navigate_setup(const bContext *C, wmGizmoGroup *gzgroup)
{
struct NavigateWidgetGroup *navgroup = MEM_callocN(sizeof(struct NavigateWidgetGroup), __func__);
@@ -225,14 +225,33 @@ static void WIDGETGROUP_navigate_setup(const bContext *UNUSED(C), wmGizmoGroup *
PointerRNA *ptr = WM_gizmo_operator_set(gz, part_index + 1, ot_view_axis, NULL);
RNA_enum_set(ptr, "type", mapping[part_index]);
}
}
/* When dragging an axis, use this instead. */
gz->drag_part = 0;
{
wmWindowManager *wm = CTX_wm_manager(C);
wmGizmo *gz = navgroup->gz_array[GZ_INDEX_ROTATE];
gz->keymap = WM_keymap_ensure(
wm->defaultconf, "Generic Gizmos Click Drag", SPACE_EMPTY, RGN_TYPE_WINDOW);
}
gzgroup->customdata = navgroup;
}
static void WIDGETGROUP_navigate_invoke_prepare(const bContext *UNUSED(C),
wmGizmoGroup *gzgroup,
wmGizmo *gz,
const wmEvent *event)
{
struct NavigateWidgetGroup *navgroup = gzgroup->customdata;
wmGizmo *gz_rotate = navgroup->gz_array[GZ_INDEX_ROTATE];
if (gz_rotate == gz) {
if (ISTWEAK(event->type)) {
/* When dragging an axis, use this instead. */
gz->highlight_part = 0;
}
}
}
static void WIDGETGROUP_navigate_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
{
struct NavigateWidgetGroup *navgroup = gzgroup->customdata;
@@ -326,6 +345,7 @@ void VIEW3D_GGT_navigate(wmGizmoGroupType *gzgt)
gzgt->poll = WIDGETGROUP_navigate_poll;
gzgt->setup = WIDGETGROUP_navigate_setup;
gzgt->invoke_prepare = WIDGETGROUP_navigate_invoke_prepare;
gzgt->draw_prepare = WIDGETGROUP_navigate_draw_prepare;
}

View File

@@ -182,6 +182,9 @@ struct wmGizmo {
/** Pointer back to group this gizmo is in (just for quick access). */
struct wmGizmoGroup *parent_gzgroup;
/** Optional keymap to use for this gizmo (overrides #wmGizmoGroupType.keymap) */
struct wmKeyMap *keymap;
void *py_instance;
/** Rna pointer to access properties. */
@@ -195,8 +198,6 @@ struct wmGizmo {
/** Optional ID for highlighting different parts of this gizmo.
* -1 when unset, otherwise a valid index. (Used as index to 'op_data'). */
int highlight_part;
/** For single click button gizmos, use a different part as a fallback, -1 when unused. */
int drag_part;
/** Distance to bias this gizmo above others when picking
* (in worldspace, scaled by the gizmo scale - when used). */

View File

@@ -90,8 +90,6 @@ static wmGizmo *wm_gizmo_create(const wmGizmoType *gzt, PointerRNA *properties)
unit_m4(gz->matrix_basis);
unit_m4(gz->matrix_offset);
gz->drag_part = -1;
return gz;
}

View File

@@ -59,10 +59,6 @@
# include "BPY_extern.h"
#endif
/* Allow gizmo part's to be single click only,
* dragging falls back to activating their 'drag_part' action. */
#define USE_DRAG_DETECT
/* -------------------------------------------------------------------- */
/** \name wmGizmoGroup
*
@@ -351,22 +347,6 @@ typedef struct GizmoTweakData {
int init_event; /* initial event type */
int flag; /* tweak flags */
#ifdef USE_DRAG_DETECT
/* True until the mouse is moved (only use when the operator has no modal).
* this allows some gizmos to be click-only. */
enum {
/* Don't detect dragging. */
DRAG_NOP = 0,
/* Detect dragging (wait until a drag or click is detected). */
DRAG_DETECT,
/* Drag has started, idle until there is no active modal operator.
* This is needed because finishing the modal operator also exits
* the modal gizmo state (un-grabbs the cursor).
* Ideally this workaround could be removed later. */
DRAG_IDLE,
} drag_state;
#endif
} GizmoTweakData;
static bool gizmo_tweak_start(bContext *C, wmGizmoMap *gzmap, wmGizmo *gz, const wmEvent *event)
@@ -380,6 +360,10 @@ static bool gizmo_tweak_start(bContext *C, wmGizmoMap *gzmap, wmGizmo *gz, const
static bool gizmo_tweak_start_and_finish(
bContext *C, wmGizmoMap *gzmap, wmGizmo *gz, const wmEvent *event, bool *r_is_modal)
{
if (gz->parent_gzgroup->type->invoke_prepare) {
gz->parent_gzgroup->type->invoke_prepare(C, gz->parent_gzgroup, gz, event);
}
wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, gz->highlight_part);
if (r_is_modal) {
*r_is_modal = false;
@@ -410,9 +394,6 @@ static bool gizmo_tweak_start_and_finish(
}
}
else {
if (gz->parent_gzgroup->type->invoke_prepare) {
gz->parent_gzgroup->type->invoke_prepare(C, gz->parent_gzgroup, gz, event);
}
/* Allow for 'button' gizmos, single click to run an action. */
WM_gizmo_operator_invoke(C, gz, gzop);
}
@@ -451,47 +432,6 @@ static int gizmo_tweak_modal(bContext *C, wmOperator *op, const wmEvent *event)
return (OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH);
}
#ifdef USE_DRAG_DETECT
wmGizmoMap *gzmap = mtweak->gzmap;
if (mtweak->drag_state == DRAG_DETECT) {
if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
if (len_manhattan_v2v2_int(&event->x, gzmap->gzmap_context.event_xy) >=
WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD) {
mtweak->drag_state = DRAG_IDLE;
gz->highlight_part = gz->drag_part;
}
}
else if (event->type == mtweak->init_event && event->val == KM_RELEASE) {
mtweak->drag_state = DRAG_NOP;
retval = OPERATOR_FINISHED;
}
if (mtweak->drag_state != DRAG_DETECT) {
/* Follow logic in 'gizmo_tweak_invoke' */
bool is_modal = false;
if (gizmo_tweak_start_and_finish(C, gzmap, gz, event, &is_modal)) {
if (is_modal) {
clear_modal = false;
}
}
else {
if (!gizmo_tweak_start(C, gzmap, gz, event)) {
retval = OPERATOR_FINISHED;
}
}
}
}
if (mtweak->drag_state == DRAG_IDLE) {
if (gzmap->gzmap_context.modal != NULL) {
return OPERATOR_PASS_THROUGH;
}
else {
gizmo_tweak_finish(C, op, false, false);
return OPERATOR_FINISHED;
}
}
#endif /* USE_DRAG_DETECT */
if (retval == OPERATOR_FINISHED) {
/* pass */
}
@@ -562,35 +502,13 @@ static int gizmo_tweak_invoke(bContext *C, wmOperator *op, const wmEvent *event)
return (OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH);
}
bool use_drag_fallback = false;
#ifdef USE_DRAG_DETECT
use_drag_fallback = !ELEM(gz->drag_part, -1, gz->highlight_part);
#endif
if (use_drag_fallback == false) {
if (gizmo_tweak_start_and_finish(C, gzmap, gz, event, NULL)) {
return OPERATOR_FINISHED;
}
if (gizmo_tweak_start_and_finish(C, gzmap, gz, event, NULL)) {
return OPERATOR_FINISHED;
}
bool use_drag_detect = false;
#ifdef USE_DRAG_DETECT
if (use_drag_fallback) {
wmGizmoOpElem *gzop = WM_gizmo_operator_get(gz, gz->highlight_part);
if (gzop && gzop->type) {
if (gzop->type->modal == NULL) {
use_drag_detect = true;
}
}
}
#endif
if (use_drag_detect == false) {
if (!gizmo_tweak_start(C, gzmap, gz, event)) {
/* failed to start */
return OPERATOR_PASS_THROUGH;
}
if (!gizmo_tweak_start(C, gzmap, gz, event)) {
/* failed to start */
return OPERATOR_PASS_THROUGH;
}
GizmoTweakData *mtweak = MEM_mallocN(sizeof(GizmoTweakData), __func__);
@@ -601,10 +519,6 @@ static int gizmo_tweak_invoke(bContext *C, wmOperator *op, const wmEvent *event)
mtweak->gzmap = gzmap;
mtweak->flag = 0;
#ifdef USE_DRAG_DETECT
mtweak->drag_state = use_drag_detect ? DRAG_DETECT : DRAG_NOP;
#endif
op->customdata = mtweak;
WM_event_add_modal_handler(C, op);

View File

@@ -1017,11 +1017,6 @@ void wm_gizmomap_modal_set(
WM_tooltip_clear(C, win);
/* Use even if we don't have invoke, so we can setup data before an operator runs. */
if (gz->parent_gzgroup->type->invoke_prepare) {
gz->parent_gzgroup->type->invoke_prepare(C, gz->parent_gzgroup, gz, event);
}
if (gz->type->invoke && (gz->type->modal || gz->custom_modal)) {
const int retval = gz->type->invoke(C, gz, event);
if ((retval & OPERATOR_RUNNING_MODAL) == 0) {

View File

@@ -2753,7 +2753,8 @@ static int wm_handlers_do_intern(bContext *C, wmEvent *event, ListBase *handlers
/* Handle highlight gizmo. */
if (gz != NULL) {
wmGizmoGroup *gzgroup = gz->parent_gzgroup;
wmKeyMap *keymap = WM_keymap_active(wm, gzgroup->type->keymap);
wmKeyMap *keymap = WM_keymap_active(wm,
gz->keymap ? gz->keymap : gzgroup->type->keymap);
action |= wm_handlers_do_keymap_with_gizmo_handler(
C, event, handlers, handler, gzgroup, keymap, do_debug_handler);
}