Keymap: share annotation tool keymaps between spaces

Not sharing caused duplication in the keymap and
required a factory class generator.

Simplify tool & keymap definitions by sharing them.

It's highly unlikely we will ever want these to use different keys
once they're set as the active tool.
This commit is contained in:
Campbell Barton
2018-11-27 17:33:52 +11:00
parent a58f0eea4f
commit 1f8360171c
5 changed files with 165 additions and 203 deletions

View File

@@ -4943,6 +4943,62 @@ def km_popup_toolbar(params):
#
# Named are auto-generated based on the tool name and it's toolbar.
def km_generic_tool_annotate(params):
return (
"Generic Tool: Annotate",
{"region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS'},
{"properties": [("mode", 'DRAW'), ("wait_for_input", False)]}),
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS', "alt": True},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
),
},
)
def km_generic_tool_annotate_line(params):
return (
"Generic Tool: Annotate Line",
{"region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_tweak, "value": 'ANY'},
{"properties": [("mode", 'DRAW_STRAIGHT'), ("wait_for_input", False)]}),
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS', "alt": True},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
),
},
)
def km_generic_tool_annotate_polygon(params):
return (
"Generic Tool: Annotate Polygon",
{"region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS'},
{"properties": [("mode", 'DRAW_POLY'), ("wait_for_input", False)]}),
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS', "alt": True},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
),
},
)
def km_generic_tool_annotate_eraser(params):
return (
"Generic Tool: Annotate Eraser",
{"region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS'},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS', "alt": True},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
),
},
)
def km_image_editor_tool_uv_cursor(params):
return (
"Image Editor Tool: Uv, Cursor",
@@ -4994,54 +5050,6 @@ def km_image_editor_tool_uv_select_lasso(params):
)
def km_image_editor_tool_uv_annotate(params):
return (
"Image Editor Tool: Uv, Annotate",
{"space_type": 'IMAGE_EDITOR', "region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS'},
{"properties": [("mode", 'DRAW'), ("wait_for_input", False)]}),
),
},
)
def km_image_editor_tool_uv_annotate_line(params):
return (
"Image Editor Tool: Uv, Annotate Line",
{"space_type": 'IMAGE_EDITOR', "region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_tweak, "value": 'ANY'},
{"properties": [("mode", 'DRAW_STRAIGHT'), ("wait_for_input", False)]}),
),
},
)
def km_image_editor_tool_uv_annotate_polygon(params):
return (
"Image Editor Tool: Uv, Annotate Polygon",
{"space_type": 'IMAGE_EDITOR', "region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS'},
{"properties": [("mode", 'DRAW_POLY'), ("wait_for_input", False)]}),
),
},
)
def km_image_editor_tool_uv_annotate_eraser(params):
return (
"Image Editor Tool: Uv, Annotate Eraser",
{"space_type": 'IMAGE_EDITOR', "region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS'},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
),
},
)
def km_3d_view_tool_object_cursor(params):
return (
"3D View Tool: Object, Cursor",
@@ -5140,62 +5148,6 @@ def km_3d_view_tool_object_scale(params):
)
def km_3d_view_tool_object_annotate(params):
return (
"3D View Tool: Object, Annotate",
{"space_type": 'VIEW_3D', "region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS'},
{"properties": [("mode", 'DRAW'), ("wait_for_input", False)]}),
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS', "alt": True},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
),
},
)
def km_3d_view_tool_object_annotate_line(params):
return (
"3D View Tool: Object, Annotate Line",
{"space_type": 'VIEW_3D', "region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_tweak, "value": 'ANY'},
{"properties": [("mode", 'DRAW_STRAIGHT'), ("wait_for_input", False)]}),
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS', "alt": True},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
),
},
)
def km_3d_view_tool_object_annotate_polygon(params):
return (
"3D View Tool: Object, Annotate Polygon",
{"space_type": 'VIEW_3D', "region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS'},
{"properties": [("mode", 'DRAW_POLY'), ("wait_for_input", False)]}),
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS', "alt": True},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
),
},
)
def km_3d_view_tool_object_annotate_eraser(params):
return (
"3D View Tool: Object, Annotate Eraser",
{"space_type": 'VIEW_3D', "region_type": 'WINDOW'},
{"items": (
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS'},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
("gpencil.annotate", {"type": params.tool_mouse, "value": 'PRESS', "alt": True},
{"properties": [("mode", 'ERASER'), ("wait_for_input", False)]}),
),
},
)
def km_3d_view_tool_object_measure(params):
return (
"3D View Tool: Object, Measure",
@@ -5940,15 +5892,16 @@ def generate_keymaps(params=None):
km_popup_toolbar(params),
# Tool System.
km_generic_tool_annotate(params),
km_generic_tool_annotate_line(params),
km_generic_tool_annotate_polygon(params),
km_generic_tool_annotate_eraser(params),
km_image_editor_tool_uv_cursor(params),
km_image_editor_tool_uv_select(params),
km_image_editor_tool_uv_select_box(params),
km_image_editor_tool_uv_select_circle(params),
km_image_editor_tool_uv_select_lasso(params),
km_image_editor_tool_uv_annotate(params),
km_image_editor_tool_uv_annotate_line(params),
km_image_editor_tool_uv_annotate_polygon(params),
km_image_editor_tool_uv_annotate_eraser(params),
km_3d_view_tool_object_cursor(params),
km_3d_view_tool_object_select(params),
km_3d_view_tool_object_select_box(params),
@@ -5958,10 +5911,6 @@ def generate_keymaps(params=None):
km_3d_view_tool_object_move(params),
km_3d_view_tool_object_rotate(params),
km_3d_view_tool_object_scale(params),
km_3d_view_tool_object_annotate(params),
km_3d_view_tool_object_annotate_line(params),
km_3d_view_tool_object_annotate_polygon(params),
km_3d_view_tool_object_annotate_eraser(params),
km_3d_view_tool_object_measure(params),
km_3d_view_tool_pose_breakdowner(params),
km_3d_view_tool_pose_push(params),

View File

@@ -108,99 +108,90 @@ class _defs_view3d_generic:
)
def _defs_annotate_factory():
class _defs_annotate:
class _defs_annotate:
def draw_settings_common(context, layout, tool):
if type(context.gpencil_data_owner) is bpy.types.Object:
gpd = context.scene.grease_pencil
else:
gpd = context.gpencil_data
def draw_settings_common(context, layout, tool):
if type(context.gpencil_data_owner) is bpy.types.Object:
gpd = context.scene.grease_pencil
if gpd is not None:
if gpd.layers.active_note is not None:
text = gpd.layers.active_note
maxw = 25
if len(text) > maxw:
text = text[:maxw - 5] + '..' + text[-3:]
else:
gpd = context.gpencil_data
text = ""
if gpd is not None:
if gpd.layers.active_note is not None:
text = gpd.layers.active_note
maxw = 25
if len(text) > maxw:
text = text[:maxw - 5] + '..' + text[-3:]
else:
text = ""
layout.label(text="Annotation:")
gpl = context.active_gpencil_layer
if gpl is not None:
sub = layout.row(align=True)
sub.ui_units_x = 8
layout.label(text="Annotation:")
gpl = context.active_gpencil_layer
if gpl is not None:
sub = layout.row(align=True)
sub.ui_units_x = 8
sub.prop(gpl, "color", text="")
sub.popover(
panel="TOPBAR_PT_annotation_layers",
text=text,
)
sub.prop(gpl, "color", text="")
sub.popover(
panel="TOPBAR_PT_annotation_layers",
text=text,
)
tool_settings = context.tool_settings
space_type = tool.space_type
if space_type == 'VIEW_3D':
layout.separator()
tool_settings = context.tool_settings
space_type = tool.space_type
if space_type == 'VIEW_3D':
layout.separator()
row = layout.row(align=True)
row.prop(tool_settings, "annotation_stroke_placement_view3d", text="Placement")
if tool_settings.gpencil_stroke_placement_view3d == 'CURSOR':
row.prop(tool_settings.gpencil_sculpt, "lockaxis")
elif tool_settings.gpencil_stroke_placement_view3d in {'SURFACE', 'STROKE'}:
row.prop(tool_settings, "use_gpencil_stroke_endpoints")
row = layout.row(align=True)
row.prop(tool_settings, "annotation_stroke_placement_view3d", text="Placement")
if tool_settings.gpencil_stroke_placement_view3d == 'CURSOR':
row.prop(tool_settings.gpencil_sculpt, "lockaxis")
elif tool_settings.gpencil_stroke_placement_view3d in {'SURFACE', 'STROKE'}:
row.prop(tool_settings, "use_gpencil_stroke_endpoints")
@ToolDef.from_fn.with_args(draw_settings=draw_settings_common)
def scribble(*, draw_settings):
return dict(
text="Annotate",
icon="ops.gpencil.draw",
cursor='PAINT_BRUSH',
keymap="Generic Tool: Annotate",
draw_settings=draw_settings,
)
@ToolDef.from_fn.with_args(draw_settings=draw_settings_common)
def scribble(*, draw_settings):
return dict(
text="Annotate",
icon="ops.gpencil.draw",
cursor='PAINT_BRUSH',
keymap=(),
draw_settings=draw_settings,
)
@ToolDef.from_fn.with_args(draw_settings=draw_settings_common)
def line(*, draw_settings):
return dict(
text="Annotate Line",
icon="ops.gpencil.draw.line",
cursor='CROSSHAIR',
keymap="Generic Tool: Annotate Line",
draw_settings=draw_settings,
)
@ToolDef.from_fn.with_args(draw_settings=draw_settings_common)
def line(*, draw_settings):
return dict(
text="Annotate Line",
icon="ops.gpencil.draw.line",
cursor='CROSSHAIR',
keymap=(),
draw_settings=draw_settings,
)
@ToolDef.from_fn.with_args(draw_settings=draw_settings_common)
def poly(*, draw_settings):
return dict(
text="Annotate Polygon",
icon="ops.gpencil.draw.poly",
cursor='CROSSHAIR',
keymap="Generic Tool: Annotate Polygon",
draw_settings=draw_settings,
)
@ToolDef.from_fn.with_args(draw_settings=draw_settings_common)
def poly(*, draw_settings):
return dict(
text="Annotate Polygon",
icon="ops.gpencil.draw.poly",
cursor='CROSSHAIR',
keymap=(),
draw_settings=draw_settings,
)
@ToolDef.from_fn
def eraser():
def draw_settings(context, layout, tool):
# TODO: Move this setting to tool_settings
user_prefs = context.user_preferences
layout.prop(user_prefs.edit, "grease_pencil_eraser_radius", text="Radius")
return dict(
text="Annotate Eraser",
icon="ops.gpencil.draw.eraser",
cursor='CROSSHAIR', # XXX: Always show brush circle when enabled
keymap=(),
draw_settings=draw_settings,
)
return _defs_annotate
# Needed so annotation gets a keymap per space type.
_defs_annotate_image = _defs_annotate_factory()
_defs_annotate_view3d = _defs_annotate_factory()
@ToolDef.from_fn
def eraser():
def draw_settings(context, layout, tool):
# TODO: Move this setting to tool_settings
user_prefs = context.user_preferences
layout.prop(user_prefs.edit, "grease_pencil_eraser_radius", text="Radius")
return dict(
text="Annotate Eraser",
icon="ops.gpencil.draw.eraser",
cursor='CROSSHAIR', # XXX: Always show brush circle when enabled
keymap="Generic Tool: Annotate Eraser",
draw_settings=draw_settings,
)
class _defs_transform:
@@ -1216,10 +1207,10 @@ class IMAGE_PT_tools_active(ToolSelectPanelHelper, Panel):
_tools_annotate = (
(
_defs_annotate_image.scribble,
_defs_annotate_image.line,
_defs_annotate_image.poly,
_defs_annotate_image.eraser,
_defs_annotate.scribble,
_defs_annotate.line,
_defs_annotate.poly,
_defs_annotate.eraser,
),
)
@@ -1298,10 +1289,10 @@ class VIEW3D_PT_tools_active(ToolSelectPanelHelper, Panel):
_tools_annotate = (
(
_defs_annotate_view3d.scribble,
_defs_annotate_view3d.line,
_defs_annotate_view3d.poly,
_defs_annotate_view3d.eraser,
_defs_annotate.scribble,
_defs_annotate.line,
_defs_annotate.poly,
_defs_annotate.eraser,
),
_defs_view3d_generic.ruler,
)

View File

@@ -74,8 +74,10 @@ bool WM_keymap_remove_item(struct wmKeyMap *keymap, struct wmKeyMapItem *
int WM_keymap_item_to_string(wmKeyMapItem *kmi, const bool compact, char *result, const int result_len);
wmKeyMap *WM_keymap_list_find(ListBase *lb, const char *idname, int spaceid, int regionid);
wmKeyMap *WM_keymap_list_find_spaceid_or_empty(ListBase *lb, const char *idname, int spaceid, int regionid);
wmKeyMap *WM_keymap_ensure(struct wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid);
wmKeyMap *WM_keymap_find_all(const struct bContext *C, const char *idname, int spaceid, int regionid);
wmKeyMap *WM_keymap_find_all_spaceid_or_empty(const struct bContext *C, const char *idname, int spaceid, int regionid);
wmKeyMap *WM_keymap_active(struct wmWindowManager *wm, struct wmKeyMap *keymap);
bool WM_keymap_remove(struct wmKeyConfig *keyconfig, struct wmKeyMap *keymap);
bool WM_keymap_poll(struct bContext *C, struct wmKeyMap *keymap);

View File

@@ -2860,8 +2860,9 @@ static void wm_event_temp_tool_handler_apply(
if (ar->regiontype == RGN_TYPE_WINDOW) {
bToolRef_Runtime *tref_rt = sa->runtime.tool ? sa->runtime.tool->runtime : NULL;
if (tref_rt && tref_rt->keymap[0]) {
wmKeyMap *km = WM_keymap_find_all(
wmKeyMap *km = WM_keymap_find_all_spaceid_or_empty(
C, tref_rt->keymap, sa->spacetype, RGN_TYPE_WINDOW);
/* We shouldn't use keymaps from unrelated spaces. */
if (km != NULL) {
// printf("Keymap: '%s' -> '%s'\n", tref_rt->keymap, sa->runtime.tool->idname);
sneaky_handler->keymap = km;

View File

@@ -806,6 +806,18 @@ wmKeyMap *WM_keymap_list_find(ListBase *lb, const char *idname, int spaceid, int
return NULL;
}
wmKeyMap *WM_keymap_list_find_spaceid_or_empty(ListBase *lb, const char *idname, int spaceid, int regionid)
{
wmKeyMap *km;
for (km = lb->first; km; km = km->next)
if (ELEM(km->spaceid, spaceid, SPACE_EMPTY) && km->regionid == regionid)
if (STREQLEN(idname, km->idname, KMAP_MAX_NAME))
return km;
return NULL;
}
wmKeyMap *WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
{
wmKeyMap *km = WM_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
@@ -827,6 +839,13 @@ wmKeyMap *WM_keymap_find_all(const bContext *C, const char *idname, int spaceid,
return WM_keymap_list_find(&wm->userconf->keymaps, idname, spaceid, regionid);
}
wmKeyMap *WM_keymap_find_all_spaceid_or_empty(const bContext *C, const char *idname, int spaceid, int regionid)
{
wmWindowManager *wm = CTX_wm_manager(C);
return WM_keymap_list_find_spaceid_or_empty(&wm->userconf->keymaps, idname, spaceid, regionid);
}
/* ****************** modal keymaps ************ */
/* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */