[Bf-blender-cvs] [b997a7cb515] soc-2020-custom-menus: Custom Menu : Add box and item buttons

TempoDev noreply at git.blender.org
Thu Jun 11 03:07:42 CEST 2020


Commit: b997a7cb515a2d0031b37bf2500d9c7aba2fa0bf
Author: TempoDev
Date:   Thu Jun 11 03:07:32 2020 +0200
Branches: soc-2020-custom-menus
https://developer.blender.org/rBb997a7cb515a2d0031b37bf2500d9c7aba2fa0bf

Custom Menu : Add box and item buttons

===================================================================

M	release/scripts/modules/rna_custom_menu_ui.py
M	release/scripts/startup/bl_operators/userpref.py
M	source/blender/editors/screen/screen_user_menu.c
M	source/blender/makesrna/intern/rna_userdef.c

===================================================================

diff --git a/release/scripts/modules/rna_custom_menu_ui.py b/release/scripts/modules/rna_custom_menu_ui.py
index 44f39c9a198..d688bcaf5de 100644
--- a/release/scripts/modules/rna_custom_menu_ui.py
+++ b/release/scripts/modules/rna_custom_menu_ui.py
@@ -141,8 +141,32 @@ def draw_kmi(display_keymaps, kc, km, kmi, layout, level):
 #def draw_shortcut(context, layout):
     #display_keymaps = keyconfig_merge(kc_user, kc_user)
 
+def draw_item_box(context, row):
+    prefs = context.preferences
+    cm = prefs.custom_menu
+
+    box_line = row.box()
+    box_col = box_line.column(align=True)
+
+    item_index = 0
+    active = cm.item_name_get(context=cm.cm_context_selected, index=0, spacetype=cm.cm_space_selected)
+    if active == "":
+        box_col.label(text="none")
+    while active != "":
+        box_col.label(text=active)
+        item_index = item_index + 1
+        active = cm.item_name_get(context=cm.cm_context_selected, index=item_index, spacetype=cm.cm_space_selected)
+    
+    row = row.split(factor=0.9, align=True)
+    col = row.column(align=True)
+
+    col.operator("preferences.menuitem_add", text="", icon='ADD')
+    col.operator("preferences.menuitem_remove", text="", icon='REMOVE')
+    col.operator("wm.keyconfig_preset_add", text="", icon='TRIA_UP')
+    col.operator("wm.keyconfig_preset_add", text="", icon='TRIA_DOWN').remove_active = True
+    row.separator()
+
 def draw_custom_menu(context, layout):
-    from bl_keymap_utils.io import keyconfig_merge
 
     wm = context.window_manager
     kc_user = wm.keyconfigs.user
@@ -189,22 +213,17 @@ def draw_custom_menu(context, layout):
     rowsub = row.split(factor=0.4, align=True)
 
     layout.separator()
-    rowsub = row.row(align=True)
-    rowsub.label(text="Display type :")
-    rowsub.menu("USERPREF_MT_keyconfigs", text=type_name_active)
-    rowsub = row.row(align=True)
-    rowsub.label(text="Shortcut :")
+    #rowsub = row.row(align=True)
+    #rowsub.label(text="Display type :")
+    #rowsub.menu("USERPREF_MT_keyconfigs", text=type_name_active)
+    #rowsub = row.row(align=True)
+    #rowsub.label(text="Shortcut :")
     #draw_shortcut(context, layout)
 
-    row = layout.row()
     col = layout.column()
-    rowsub = row.split(factor=0.4, align=True)
-
-    item_index = 0
-    active = cm.refresh(context=cm.cm_context_selected, index=0, spacetype=cm.cm_space_selected)
-    while active != "":
-        col.label(text=active)
-        item_index = item_index + 1
-        active = cm.refresh(context=cm.cm_context_selected, index=item_index, spacetype=cm.cm_space_selected)
+    row = layout.row()
+       
+    draw_item_box(context=context, row=row)
+    row.label(text="riht")
 
     layout.separator()
diff --git a/release/scripts/startup/bl_operators/userpref.py b/release/scripts/startup/bl_operators/userpref.py
index 521fdaa175f..a1b1d66115f 100644
--- a/release/scripts/startup/bl_operators/userpref.py
+++ b/release/scripts/startup/bl_operators/userpref.py
@@ -1155,6 +1155,43 @@ class PREFERENCES_OT_custommenu_select(Operator):
         else:
             return {'CANCELLED'}
 
+class PREFERENCES_OT_menuitem_add(Operator):
+    """Add user menu item"""
+    bl_idname = "preferences.menuitem_add"
+    bl_label = "Add User Menu Item"
+
+    def execute(self, context):
+        prefs = context.preferences
+        cm = prefs.custom_menu
+
+        
+        cm.item_add(context=cm.cm_context_selected, spacetype=cm.cm_space_selected)
+        context.preferences.is_dirty = True
+        return {'FINISHED'}
+
+
+class PREFERENCES_OT_menuitem_remove(Operator):
+    """Remove user menu item"""
+    bl_idname = "preferences.menuitem_remove"
+    bl_label = "Remove User Menu Item"
+
+    item_id: IntProperty(
+        name="Item Identifier",
+        description="Identifier of the item to remove",
+    )
+
+    @classmethod
+    def poll(cls, context):
+        return hasattr(context, "keymap")
+
+    def execute(self, context):
+        km = context.keymap
+        kmi = km.keymap_items.from_id(self.item_id)
+        km.keymap_items.remove(kmi)
+
+        context.preferences.is_dirty = True
+        return {'FINISHED'}
+
 
 classes = (
     PREFERENCES_OT_addon_disable,
@@ -1182,4 +1219,6 @@ classes = (
     PREFERENCES_OT_studiolight_copy_settings,
     PREFERENCES_OT_studiolight_show,
     PREFERENCES_OT_custommenu_select,
+    PREFERENCES_OT_menuitem_add,
+    PREFERENCES_OT_menuitem_remove,
 )
diff --git a/source/blender/editors/screen/screen_user_menu.c b/source/blender/editors/screen/screen_user_menu.c
index 733e8b694a6..177da971563 100644
--- a/source/blender/editors/screen/screen_user_menu.c
+++ b/source/blender/editors/screen/screen_user_menu.c
@@ -90,7 +90,6 @@ bUserMenu **ED_screen_user_menus_find(const bContext *C, uint *r_len)
   um_array[2] = (sl->spacetype == SPACE_VIEW3D) ?
                     BKE_blender_user_menu_find(&U.user_menus, SPACE_PROPERTIES, context_mode) :
                     NULL;
-
   *r_len = array_len;
   return um_array;
 }
diff --git a/source/blender/makesrna/intern/rna_userdef.c b/source/blender/makesrna/intern/rna_userdef.c
index 4dcba82ec5f..fb979aa73a1 100644
--- a/source/blender/makesrna/intern/rna_userdef.c
+++ b/source/blender/makesrna/intern/rna_userdef.c
@@ -1144,7 +1144,7 @@ static void rna_userdef_cm_space_selected_update(Main *bmain, Scene *scene, Poin
   USERDEF_TAG_DIRTY;
 }
 
-static const char *rna_CustomMenus_refresh(UserDef *userdef, const char *spacetype, const char *context, int index)
+static const char *rna_UserDef_usermenu_item_name_get(UserDef *userdef, const char *spacetype, const char *context, int index)
 {
   int i = 0;
   const ListBase *spacetypes = BKE_spacetypes_list();
@@ -1171,6 +1171,30 @@ static const char *rna_CustomMenus_refresh(UserDef *userdef, const char *spacety
   return "";
 }
 
+static void rna_UserDef_usermenu_item_add(UserDef *userdef, const char *spacetype, const char *context)
+{
+  int i = 0;
+  const ListBase *spacetypes = BKE_spacetypes_list();
+  SpaceType *st = NULL;
+
+  for (i = 0, st = spacetypes->first; st; st = st->next, i++) {
+    int id = st->spaceid;
+    char *space_name = st->name;
+    if (!strcmp(space_name, spacetype))
+      break;
+  }
+  if (st == NULL) return;
+
+  bUserMenu *bum = BKE_blender_user_menu_find(&userdef->user_menus, st->spaceid, context);
+  if (!bum) return;
+
+  ListBase *lb = &bum->items;
+  bUserMenuItem_Op *bumi = (bUserMenuItem_Op *)BKE_blender_user_menu_item_add(
+      lb, USER_MENU_TYPE_OPERATOR);
+  STRNCPY(bumi->item.ui_name, "new item");
+  //printf("%s\n", bumi->item.ui_name);
+}
+
 #else
 
 #  define USERDEF_TAG_DIRTY_PROPERTY_UPDATE_ENABLE \
@@ -6034,7 +6058,7 @@ static void rna_def_userdef_custom_menu(BlenderRNA *brna)
       prop, "context selected", "the context selected");
   RNA_def_property_update(prop, 0, "rna_userdef_cm_space_selected_update");
 
-  func = RNA_def_function(srna, "refresh", "rna_CustomMenus_refresh");
+  func = RNA_def_function(srna, "item_name_get", "rna_UserDef_usermenu_item_name_get");
   RNA_def_function_ui_description(func, "Refresh custom menu editor");
   parm = RNA_def_string(func,
                         "spacetype",
@@ -6053,14 +6077,31 @@ static void rna_def_userdef_custom_menu(BlenderRNA *brna)
   parm = RNA_def_int(func, "index", 0, 0, 100, "Index", "index of the item in list", 0, 100);
   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
   parm = RNA_def_string(func,
-                        "user_menu",
+                        "item_name",
                         NULL,
                         0,
                         "",
-                        "active user menu");
+                        "the item name");
   //parm = RNA_def_pointer(func, "user_menu", "bUserMenu", "", "found user menu");
   RNA_def_function_return(func, parm);
 
+  func = RNA_def_function(srna, "item_add", "rna_UserDef_usermenu_item_add");
+  RNA_def_function_ui_description(func, "add an item to a menu");
+  parm = RNA_def_string(func,
+                        "spacetype",
+                        NULL,
+                        0,
+                        "SpaceType",
+                        "space type of the menu");
+  RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
+  parm = RNA_def_string(func,
+                        "context",
+                        NULL,
+                        0,
+                        "Context",
+                        "context of the menu");
+  RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
+
 }
 
 static void rna_def_userdef_filepaths(BlenderRNA *brna)



More information about the Bf-blender-cvs mailing list