keymap editor

New unique ID per keymap item (unique inside their keymap) for default and configuration keymaps.
This allows restoring a single user defined kmi to its previous (default or config) values instead of having to restore the whole keymap.
The restore item button is disabled for kmi added by the users (they don't have an ID).

Also fixes a bug in the rna function for add keymap item (parameter order was incorrect, messing adding back saved configurations).
This commit is contained in:
Martin Poirier 2009-12-17 03:32:33 +00:00
parent c3b978828c
commit fd18f55510
6 changed files with 101 additions and 4 deletions

@ -1365,6 +1365,7 @@ class USERPREF_PT_input(bpy.types.Panel):
else: else:
row.label() row.label()
row.operator("wm.keyitem_restore", text="", icon='BACK')
row.operator("wm.keyitem_remove", text="", icon='X') row.operator("wm.keyitem_remove", text="", icon='X')
# Expanded, additional event settings # Expanded, additional event settings
@ -1666,7 +1667,25 @@ class WM_OT_keymap_restore(bpy.types.Operator):
return ('FINISHED',) return ('FINISHED',)
class WM_OT_keyitem_restore(bpy.types.Operator):
"Restore key map item."
bl_idname = "wm.keyitem_restore"
bl_label = "Restore Key Map Item"
def poll(self, context):
kmi = context.keyitem
km = context.keymap
return km and kmi and kmi.id != 0
def execute(self, context):
wm = context.manager
kmi = context.keyitem
km = context.keymap
km.restore_item_to_default(kmi)
return ('FINISHED',)
class WM_OT_keyitem_add(bpy.types.Operator): class WM_OT_keyitem_add(bpy.types.Operator):
"Add key map item." "Add key map item."
bl_idname = "wm.keyitem_add" bl_idname = "wm.keyitem_add"
@ -1699,4 +1718,4 @@ bpy.ops.add(WM_OT_keymap_edit)
bpy.ops.add(WM_OT_keymap_restore) bpy.ops.add(WM_OT_keymap_restore)
bpy.ops.add(WM_OT_keyitem_add) bpy.ops.add(WM_OT_keyitem_add)
bpy.ops.add(WM_OT_keyitem_remove) bpy.ops.add(WM_OT_keyitem_remove)
bpy.ops.add(WM_OT_keyitem_restore)

@ -262,7 +262,9 @@ typedef struct wmKeyMapItem {
short flag; short flag;
/* runtime */ /* runtime */
short maptype, pad[2]; /* keymap editor */ short maptype; /* keymap editor */
short id; /* unique identifier */
short pad;
struct PointerRNA *ptr; /* rna pointer to access properties */ struct PointerRNA *ptr; /* rna pointer to access properties */
} wmKeyMapItem; } wmKeyMapItem;
@ -281,7 +283,7 @@ typedef struct wmKeyMap {
short regionid; /* see above */ short regionid; /* see above */
short flag; /* general flags */ short flag; /* general flags */
short pad; short kmi_id; /* last kmi id */
/* runtime */ /* runtime */
int (*poll)(struct bContext *); /* verify if enabled in the current context */ int (*poll)(struct bContext *); /* verify if enabled in the current context */

@ -944,6 +944,11 @@ static void rna_def_keyconfig(BlenderRNA *brna)
RNA_def_property_enum_funcs(prop, NULL, NULL, "rna_KeyMapItem_value_itemf"); RNA_def_property_enum_funcs(prop, NULL, NULL, "rna_KeyMapItem_value_itemf");
RNA_def_property_ui_text(prop, "Value", ""); RNA_def_property_ui_text(prop, "Value", "");
prop= RNA_def_property(srna, "id", PROP_INT, PROP_NONE);
RNA_def_property_int_sdna(prop, NULL, "id");
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
RNA_def_property_ui_text(prop, "id", "ID of the item.");
prop= RNA_def_property(srna, "any", PROP_BOOLEAN, PROP_NONE); prop= RNA_def_property(srna, "any", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_funcs(prop, "rna_KeyMapItem_any_getf", "rna_KeyMapItem_any_setf"); RNA_def_property_boolean_funcs(prop, "rna_KeyMapItem_any_getf", "rna_KeyMapItem_any_setf");
RNA_def_property_ui_text(prop, "Any", "Any modifier keys pressed."); RNA_def_property_ui_text(prop, "Any", "Any modifier keys pressed.");

@ -113,7 +113,12 @@ static wmKeyMapItem *rna_KeyMap_add_modal_item(wmKeyMap *km, bContext *C, Report
return WM_modalkeymap_add_item(km, type, value, modifier, keymodifier, propvalue); return WM_modalkeymap_add_item(km, type, value, modifier, keymodifier, propvalue);
} }
static wmKeyMapItem *rna_KeyMap_add_item(wmKeyMap *km, ReportList *reports, char *idname, int type, int any, int value, int shift, int ctrl, int alt, int oskey, int keymodifier) static void rna_keymap_restore_item_to_default(wmKeyMap *km, bContext *C, wmKeyMapItem *kmi)
{
WM_keymap_restore_item_to_default(C, km, kmi);
}
static wmKeyMapItem *rna_KeyMap_add_item(wmKeyMap *km, ReportList *reports, char *idname, int type, int value, int any, int shift, int ctrl, int alt, int oskey, int keymodifier)
{ {
// wmWindowManager *wm = CTX_wm_manager(C); // wmWindowManager *wm = CTX_wm_manager(C);
int modifier= 0; int modifier= 0;
@ -276,6 +281,11 @@ void RNA_api_keymap(StructRNA *srna)
RNA_def_function_return(func, parm); RNA_def_function_return(func, parm);
func= RNA_def_function(srna, "restore_to_default", "WM_keymap_restore_to_default"); func= RNA_def_function(srna, "restore_to_default", "WM_keymap_restore_to_default");
func= RNA_def_function(srna, "restore_item_to_default", "rna_keymap_restore_item_to_default");
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
parm= RNA_def_pointer(func, "item", "KeyMapItem", "Item", "");
RNA_def_property_flag(parm, PROP_REQUIRED);
} }
#endif #endif

@ -114,6 +114,7 @@ int WM_keymap_user_init(struct wmWindowManager *wm, struct wmKeyMap *keymap);
wmKeyMap *WM_keymap_copy_to_user(struct wmKeyMap *keymap); wmKeyMap *WM_keymap_copy_to_user(struct wmKeyMap *keymap);
void WM_keymap_restore_to_default(struct wmKeyMap *keymap); void WM_keymap_restore_to_default(struct wmKeyMap *keymap);
void WM_keymap_properties_reset(struct wmKeyMapItem *kmi); void WM_keymap_properties_reset(struct wmKeyMapItem *kmi);
void WM_keymap_restore_item_to_default(struct bContext *C, struct wmKeyMap *keymap, struct wmKeyMapItem *kmi);
wmKeyMap *WM_modalkeymap_add(struct wmKeyConfig *keyconf, char *idname, struct EnumPropertyItem *items); wmKeyMap *WM_modalkeymap_add(struct wmKeyConfig *keyconf, char *idname, struct EnumPropertyItem *items);
wmKeyMap *WM_modalkeymap_get(struct wmKeyConfig *keyconf, char *idname); wmKeyMap *WM_modalkeymap_get(struct wmKeyConfig *keyconf, char *idname);

@ -201,6 +201,12 @@ wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, char *idname, int type, int v
keymap_event_set(kmi, type, val, modifier, keymodifier); keymap_event_set(kmi, type, val, modifier, keymodifier);
keymap_properties_set(kmi); keymap_properties_set(kmi);
if ((keymap->flag & KEYMAP_USER) == 0) {
keymap->kmi_id++;
kmi->id = keymap->kmi_id;
}
return kmi; return kmi;
} }
@ -291,6 +297,11 @@ wmKeyMapItem *WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modif
keymap_event_set(kmi, type, val, modifier, keymodifier); keymap_event_set(kmi, type, val, modifier, keymodifier);
if ((km->flag & KEYMAP_USER) == 0) {
km->kmi_id++;
kmi->id = km->kmi_id;
}
return kmi; return kmi;
} }
@ -545,6 +556,55 @@ wmKeyMap *WM_keymap_copy_to_user(wmKeyMap *keymap)
return usermap; return usermap;
} }
void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapItem *kmi)
{
wmWindowManager *wm = CTX_wm_manager(C);
wmKeyConfig *keyconf;
wmKeyMap *km = NULL;
/* look in user key config */
keyconf= wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
if(keyconf) {
km= WM_keymap_list_find(&keyconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
}
if (!km) {
/* or from default */
km= WM_keymap_list_find(&wm->defaultconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
}
if (km) {
wmKeyMapItem *orig;
for (orig = km->items.first; orig; orig = orig->next) {
if (orig->id == kmi->id)
break;
}
if (orig) {
if(strcmp(orig->idname, kmi->idname) != 0) {
BLI_strncpy(kmi->idname, orig->idname, sizeof(kmi->idname));
WM_keymap_properties_reset(kmi);
}
kmi->properties= IDP_CopyProperty(orig->properties);
kmi->ptr->data= kmi->properties;
kmi->propvalue = orig->propvalue;
kmi->type = orig->type;
kmi->val = orig->val;
kmi->shift = orig->shift;
kmi->ctrl = orig->ctrl;
kmi->alt = orig->alt;
kmi->oskey = orig->oskey;
kmi->keymodifier = orig->keymodifier;
kmi->maptype = orig->maptype;
}
}
}
void WM_keymap_restore_to_default(wmKeyMap *keymap) void WM_keymap_restore_to_default(wmKeyMap *keymap)
{ {
wmKeyMap *usermap; wmKeyMap *usermap;