- replace rna context.mode_string with an enum context.mode, that way its self docume...
authorCampbell Barton <ideasman42@gmail.com>
Sun, 16 Aug 2009 03:40:00 +0000 (03:40 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 16 Aug 2009 03:40:00 +0000 (03:40 +0000)
- view3d use select menus use the context.mode enum value.

- if selectmenu in dir(bpy.types) # creates a list of 1400+ strings, does a lookup and throws them away, better avoid this for redrawing.

release/ui/space_view3d.py
source/blender/blenkernel/BKE_context.h
source/blender/blenkernel/intern/context.c
source/blender/makesrna/intern/rna_context.c

index 89e34fc1dc3dadc0a3ed1cb09333135c07e6baa1..796c04dafbe25613bef38717fb6eb5b8991a06f4 100644 (file)
@@ -10,7 +10,7 @@ class VIEW3D_HT_header(bpy.types.Header):
                layout = self.layout
                
                view = context.space_data
-               mode_string = context.mode_string
+               mode_string = context.mode
 
                layout.template_header()
                
@@ -21,11 +21,10 @@ class VIEW3D_HT_header(bpy.types.Header):
                        row.itemM("VIEW3D_MT_view")
                        
                        # Select Menu
-                       selectmenu = "VIEW3D_MT_select_%s" % mode_string
-                       if selectmenu in dir(bpy.types):
-                               row.itemM(selectmenu)
+                       if mode_string not in ('EDIT_TEXT', ):
+                               row.itemM("VIEW3D_MT_select_%s" % mode_string)
                        
-                       if mode_string == 'objectmode':
+                       if mode_string == 'OBJECT':
                                row.itemM("VIEW3D_MT_object")
 
                layout.template_header_3D()
@@ -109,9 +108,9 @@ class VIEW3D_MT_view_navigation(bpy.types.Menu):
                layout.item_floatO("view3d.zoom", "delta", 1.0, text="Zoom In")
                layout.item_floatO("view3d.zoom", "delta", -1.0, text="Zoom Out")
 
-# ********** Select menus **********
+# ********** Select menus, suffix from context.mode **********
 
-class VIEW3D_MT_select_objectmode(bpy.types.Menu):
+class VIEW3D_MT_select_OBJECT(bpy.types.Menu):
        __space_type__ = "VIEW_3D"
        __label__ = "Select"
 
@@ -127,9 +126,9 @@ class VIEW3D_MT_select_objectmode(bpy.types.Menu):
                layout.itemO("object.select_random", text="Random")
                layout.itemO("object.select_by_layer", text="Select All by Layer")
                layout.item_enumO("object.select_by_type", "type", "", text="Select All by Type")
-               layout.item_enumO("object.select_grouped", "type", "", text="Select Grouped")
+               layout.itemO("object.select_grouped", text="Select Grouped")
 
-class VIEW3D_MT_select_posemode(bpy.types.Menu):
+class VIEW3D_MT_select_POSE(bpy.types.Menu):
        __space_type__ = "VIEW_3D"
        __label__ = "Select"
 
@@ -146,14 +145,14 @@ class VIEW3D_MT_select_posemode(bpy.types.Menu):
                
                layout.itemS()
                
-               layout.item_enumO("pose.select_hierarchy", "direction", "PARENT")
-               layout.item_enumO("pose.select_hierarchy", "direction", "CHILD")
+               layout.item_enumO("pose.select_hierarchy", "direction", 'PARENT')
+               layout.item_enumO("pose.select_hierarchy", "direction", 'CHILD')
                
                layout.itemS()
                
                layout.view3d_select_posemenu()
 
-class VIEW3D_MT_select_particlemode(bpy.types.Menu):
+class VIEW3D_MT_select_PARTICLE(bpy.types.Menu):
        __space_type__ = "VIEW_3D"
        __label__ = "Select"
 
@@ -175,7 +174,7 @@ class VIEW3D_MT_select_particlemode(bpy.types.Menu):
                layout.itemO("particle.select_more")
                layout.itemO("particle.select_less")
 
-class VIEW3D_MT_select_mesh_edit(bpy.types.Menu):
+class VIEW3D_MT_select_EDIT_MESH(bpy.types.Menu):
        __space_type__ = "VIEW_3D"
        __label__ = "Select"
 
@@ -197,9 +196,9 @@ class VIEW3D_MT_select_mesh_edit(bpy.types.Menu):
 
                layout.itemS()
 
-               layout.item_enumO("mesh.select_by_number_vertices", "type", "TRIANGLES", text="Triangles")
-               layout.item_enumO("mesh.select_by_number_vertices", "type", "QUADS", text="Quads")
-               layout.item_enumO("mesh.select_by_number_vertices", "type", "OTHER", text="Loose Verts/Edges")
+               layout.item_enumO("mesh.select_by_number_vertices", "type", 'TRIANGLES', text="Triangles")
+               layout.item_enumO("mesh.select_by_number_vertices", "type", 'QUADS', text="Quads")
+               layout.item_enumO("mesh.select_by_number_vertices", "type", 'OTHER', text="Loose Verts/Edges")
                layout.itemO("mesh.select_similar", text="Similar...")
 
                layout.itemS()
@@ -219,7 +218,7 @@ class VIEW3D_MT_select_mesh_edit(bpy.types.Menu):
                layout.itemO("mesh.loop_to_region")
                layout.itemO("mesh.region_to_loop")
 
-class VIEW3D_MT_select_curve_edit(bpy.types.Menu):
+class VIEW3D_MT_select_EDIT_CURVE(bpy.types.Menu):
        __space_type__ = "VIEW_3D"
        __label__ = "Select"
 
@@ -248,7 +247,7 @@ class VIEW3D_MT_select_curve_edit(bpy.types.Menu):
                layout.itemO("curve.select_more")
                layout.itemO("curve.select_less")
 
-class VIEW3D_MT_select_surface_edit(bpy.types.Menu):
+class VIEW3D_MT_select_EDIT_SURFACE(bpy.types.Menu):
        __space_type__ = "VIEW_3D"
        __label__ = "Select"
 
@@ -274,7 +273,7 @@ class VIEW3D_MT_select_surface_edit(bpy.types.Menu):
                layout.itemO("curve.select_more")
                layout.itemO("curve.select_less")
 
-class VIEW3D_MT_select_mball_edit(bpy.types.Menu):
+class VIEW3D_MT_select_EDIT_METABALL(bpy.types.Menu):
        __space_type__ = "VIEW_3D"
        __label__ = "Select"
 
@@ -292,7 +291,7 @@ class VIEW3D_MT_select_mball_edit(bpy.types.Menu):
                
                layout.itemL(text="Random")
 
-class VIEW3D_MT_select_lattice_edit(bpy.types.Menu):
+class VIEW3D_MT_select_EDIT_LATTICE(bpy.types.Menu):
        __space_type__ = "VIEW_3D"
        __label__ = "Select"
 
@@ -305,7 +304,7 @@ class VIEW3D_MT_select_lattice_edit(bpy.types.Menu):
                
                layout.itemO("lattice.select_all_toggle", text="Select/Deselect All")
 
-class VIEW3D_MT_select_armature_edit(bpy.types.Menu):
+class VIEW3D_MT_select_EDIT_ARMATURE(bpy.types.Menu):
        __space_type__ = "VIEW_3D"
        __label__ = "Select"
 
@@ -321,14 +320,14 @@ class VIEW3D_MT_select_armature_edit(bpy.types.Menu):
 
                layout.itemS()
                
-               layout.item_enumO("armature.select_hierarchy", "direction", "PARENT")
-               layout.item_enumO("armature.select_hierarchy", "direction", "CHILD")
+               layout.item_enumO("armature.select_hierarchy", "direction", 'PARENT')
+               layout.item_enumO("armature.select_hierarchy", "direction", 'CHILD')
                
                layout.itemS()
                
                layout.view3d_select_armaturemenu()
 
-class VIEW3D_MT_select_facesel(bpy.types.Menu):
+class VIEW3D_MT_select_FACE(bpy.types.Menu):# XXX no matching enum
        __space_type__ = "VIEW_3D"
        __label__ = "Select"
 
@@ -570,16 +569,16 @@ bpy.types.register(VIEW3D_HT_header) # Header
 bpy.types.register(VIEW3D_MT_view) #View Menus
 bpy.types.register(VIEW3D_MT_view_navigation)
 
-bpy.types.register(VIEW3D_MT_select_objectmode) # Select Menus
-bpy.types.register(VIEW3D_MT_select_posemode)
-bpy.types.register(VIEW3D_MT_select_particlemode)
-bpy.types.register(VIEW3D_MT_select_mesh_edit)
-bpy.types.register(VIEW3D_MT_select_curve_edit)
-bpy.types.register(VIEW3D_MT_select_surface_edit)
-bpy.types.register(VIEW3D_MT_select_mball_edit)
-bpy.types.register(VIEW3D_MT_select_lattice_edit)
-bpy.types.register(VIEW3D_MT_select_armature_edit)
-bpy.types.register(VIEW3D_MT_select_facesel)
+bpy.types.register(VIEW3D_MT_select_OBJECT) # Select Menus
+bpy.types.register(VIEW3D_MT_select_POSE)
+bpy.types.register(VIEW3D_MT_select_PARTICLE)
+bpy.types.register(VIEW3D_MT_select_EDIT_MESH)
+bpy.types.register(VIEW3D_MT_select_EDIT_CURVE)
+bpy.types.register(VIEW3D_MT_select_EDIT_SURFACE)
+bpy.types.register(VIEW3D_MT_select_EDIT_METABALL)
+bpy.types.register(VIEW3D_MT_select_EDIT_LATTICE)
+bpy.types.register(VIEW3D_MT_select_EDIT_ARMATURE)
+bpy.types.register(VIEW3D_MT_select_FACE) # XXX todo
 
 bpy.types.register(VIEW3D_MT_object) # Object Menu
 bpy.types.register(VIEW3D_MT_object_clear)
index 81dea204dd0b9b3f3d2046a757597075e62dd253..035b7e2e5b9423f62323ff3425d9f169bb59bd9f 100644 (file)
@@ -86,6 +86,25 @@ typedef struct bContextStore {
        int used;
 } bContextStore;
 
+/* for the conrtext's rna mode enum
+ * keep aligned with data_mode_strings in context.c */
+enum {
+       CTX_MODE_EDIT_MESH = 0,
+       CTX_MODE_EDIT_CURVE,
+       CTX_MODE_EDIT_SURFACE,
+       CTX_MODE_EDIT_TEXT,
+       CTX_MODE_EDIT_ARMATURE,
+       CTX_MODE_EDIT_METABALL,
+       CTX_MODE_EDIT_LATTICE,
+       CTX_MODE_POSE,
+       CTX_MODE_SCULPT,
+       CTX_MODE_PAINT_WEIGHT,
+       CTX_MODE_PAINT_VERTEX,
+       CTX_MODE_PAINT_TEXTURE,
+       CTX_MODE_PARTICLE,
+       CTX_MODE_OBJECT
+};
+
 /* Context */
 
 bContext *CTX_create(void);
@@ -192,6 +211,7 @@ struct Scene *CTX_data_scene(const bContext *C);
 struct ToolSettings *CTX_data_tool_settings(const bContext *C);
 
 char *CTX_data_mode_string(const bContext *C);
+int CTX_data_mode_enum(const bContext *C);
 
 void CTX_data_main_set(bContext *C, struct Main *bmain);
 void CTX_data_scene_set(bContext *C, struct Scene *bmain);
index 9ffd3aa704fc5287c52bbe2aafd30916a240929a..c38b994849aabe179030cc1d660fd507d3be966c 100644 (file)
@@ -661,42 +661,67 @@ Scene *CTX_data_scene(const bContext *C)
                return C->data.scene;
 }
 
-char *CTX_data_mode_string(const bContext *C)
+int CTX_data_mode_enum(const bContext *C)
 {
        Object *obedit= CTX_data_edit_object(C);
 
        if(obedit) {
                switch(obedit->type) {
                        case OB_MESH:
-                               return "mesh_edit";
+                               return CTX_MODE_EDIT_MESH;
                        case OB_CURVE:
-                               return "curve_edit";
+                               return CTX_MODE_EDIT_CURVE;
                        case OB_SURF:
-                               return "surface_edit";
+                               return CTX_MODE_EDIT_SURFACE;
                        case OB_FONT:
-                               return "text_edit";
+                               return CTX_MODE_EDIT_TEXT;
                        case OB_ARMATURE:
-                               return "armature_edit";
+                               return CTX_MODE_EDIT_ARMATURE;
                        case OB_MBALL:
-                               return "mball_edit";
+                               return CTX_MODE_EDIT_METABALL;
                        case OB_LATTICE:
-                               return "lattice_edit";
+                               return CTX_MODE_EDIT_LATTICE;
                }
        }
        else {
                Object *ob = CTX_data_active_object(C);
-               
+
                if(ob) {
-                       if(ob->mode & OB_MODE_POSE) return "posemode";
-                       else if(ob->mode & OB_MODE_SCULPT)  return "sculpt_mode";
-                       else if(ob->mode & OB_MODE_WEIGHT_PAINT) return "weightpaint";
-                       else if(ob->mode & OB_MODE_VERTEX_PAINT) return "vertexpaint";
-                       else if(ob->mode & OB_MODE_TEXTURE_PAINT) return "texturepaint";
-                       else if(ob->mode & OB_MODE_PARTICLE_EDIT) return "particlemode";
+                       if(ob->mode & OB_MODE_POSE) return CTX_MODE_POSE;
+                       else if(ob->mode & OB_MODE_SCULPT)  return CTX_MODE_SCULPT;
+                       else if(ob->mode & OB_MODE_WEIGHT_PAINT) return CTX_MODE_PAINT_WEIGHT;
+                       else if(ob->mode & OB_MODE_VERTEX_PAINT) return CTX_MODE_PAINT_VERTEX;
+                       else if(ob->mode & OB_MODE_TEXTURE_PAINT) return CTX_MODE_PAINT_TEXTURE;
+                       else if(ob->mode & OB_MODE_PARTICLE_EDIT) return CTX_MODE_PARTICLE;
                }
        }
-       
-       return "objectmode";
+
+       return CTX_MODE_OBJECT;
+}
+
+
+/* would prefer if we can use the enum version below over this one - Campbell */
+/* must be aligned with above enum  */
+static char *data_mode_strings[] = {
+       "mesh_edit",
+       "curve_edit",
+       "surface_edit",
+       "text_edit",
+       "armature_edit",
+       "mball_edit",
+       "lattice_edit",
+       "posemode",
+       "sculpt_mode",
+       "weightpaint",
+       "vertexpaint",
+       "texturepaint",
+       "particlemode",
+       "objectmode",
+       0
+};
+char *CTX_data_mode_string(const bContext *C)
+{
+       return data_mode_strings[CTX_data_mode_enum(C)];
 }
 
 void CTX_data_scene_set(bContext *C, Scene *scene)
index 5d45a3513d134e007216ebbf3c4291537fd769cb..7239fc1ff3d6737b55e7f83600ae3b8876db4b28 100644 (file)
 #include "RNA_define.h"
 #include "RNA_types.h"
 
-#ifdef RNA_RUNTIME
-
 #include "BKE_context.h"
 
+#ifdef RNA_RUNTIME
+
 static PointerRNA rna_Context_manager_get(PointerRNA *ptr)
 {
        bContext *C= (bContext*)ptr->data;
@@ -110,16 +110,10 @@ static PointerRNA rna_Context_user_preferences_get(PointerRNA *ptr)
        return newptr;
 }
 
-static void rna_Context_mode_string_get(PointerRNA *ptr, char *value)
+static int rna_Context_mode_get(PointerRNA *ptr)
 {
        bContext *C= (bContext*)ptr->data;
-       strcpy(value, CTX_data_mode_string(C));
-}
-
-static int rna_Context_mode_string_length(PointerRNA *ptr)
-{
-       bContext *C= (bContext*)ptr->data;
-       return strlen(CTX_data_mode_string(C));
+       return CTX_data_mode_enum(C);
 }
 
 #else
@@ -129,6 +123,23 @@ void RNA_def_context(BlenderRNA *brna)
        StructRNA *srna;
        PropertyRNA *prop;
 
+       static EnumPropertyItem mode_items[] = {
+               {CTX_MODE_EDIT_MESH, "EDIT_MESH", 0, "Mesh Edit", ""},
+               {CTX_MODE_EDIT_CURVE, "EDIT_CURVE", 0, "Curve Edit", ""},
+               {CTX_MODE_EDIT_SURFACE, "EDIT_SURFACE", 0, "Surface Edit", ""},
+               {CTX_MODE_EDIT_TEXT, "EDIT_TEXT", 0, "Edit Edit", ""},
+               {CTX_MODE_EDIT_ARMATURE, "EDIT_ARMATURE", 0, "Armature Edit", ""}, // PARSKEL reuse will give issues
+               {CTX_MODE_EDIT_METABALL, "EDIT_METABALL", 0, "Metaball Edit", ""},
+               {CTX_MODE_EDIT_LATTICE, "EDIT_LATTICE", 0, "Lattice Edit", ""},
+               {CTX_MODE_POSE, "POSE", 0, "Pose ", ""},
+               {CTX_MODE_SCULPT, "SCULPT", 0, "Sculpt", ""},
+               {CTX_MODE_PAINT_WEIGHT, "PAINT_WEIGHT", 0, "Weight Paint", ""},
+               {CTX_MODE_PAINT_VERTEX, "PAINT_VERTEX", 0, "Vertex Paint", ""},
+               {CTX_MODE_PAINT_TEXTURE, "PAINT_TEXTURE", 0, "Texture Paint", ""},
+               {CTX_MODE_PARTICLE, "PARTICLE", 0, "Particle", ""},
+               {CTX_MODE_OBJECT, "OBJECT", 0, "Object", ""},
+               {0, NULL, 0, NULL, NULL}};
+
        srna= RNA_def_struct(brna, "Context", NULL);
        RNA_def_struct_ui_text(srna, "Context", "Current windowmanager and data context.");
        RNA_def_struct_sdna(srna, "bContext");
@@ -190,9 +201,10 @@ void RNA_def_context(BlenderRNA *brna)
        RNA_def_property_struct_type(prop, "UserPreferences");
        RNA_def_property_pointer_funcs(prop, "rna_Context_user_preferences_get", NULL, NULL);
 
-       prop= RNA_def_property(srna, "mode_string", PROP_STRING, PROP_NONE);
+       prop= RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE);
+       RNA_def_property_enum_items(prop, mode_items);
        RNA_def_property_clear_flag(prop, PROP_EDITABLE);
-       RNA_def_property_string_funcs(prop, "rna_Context_mode_string_get", "rna_Context_mode_string_length", NULL);
+       RNA_def_property_enum_funcs(prop, "rna_Context_mode_get", NULL, NULL);
 }
 
 #endif