diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-12-07 03:16:57 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-12-07 03:16:57 +0300 |
commit | 750764f411d18b5f57cac3857cf201d4e9425521 (patch) | |
tree | e0de3ecab9cefe266080f9962a848e32c6e58c0f /source | |
parent | 85773c72303962f03c2a2d62bb26e042da8dd4b7 (diff) |
rna flag PROP_ENUM_FLAG which makes rna props a tuple of enums when converted into a PyObject
only used by wm.invoke_props_popup() currently
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/makesrna/RNA_access.h | 2 | ||||
-rw-r--r-- | source/blender/makesrna/RNA_enum_types.h | 1 | ||||
-rw-r--r-- | source/blender/makesrna/RNA_types.h | 5 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_access.c | 28 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_wm.c | 7 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_wm_api.c | 6 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 112 |
7 files changed, 109 insertions, 52 deletions
diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 959109f7bbb..263183cadd8 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -633,11 +633,13 @@ void RNA_property_float_range(PointerRNA *ptr, PropertyRNA *prop, float *hardmin void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision); int RNA_enum_identifier(EnumPropertyItem *item, const int value, const char **identifier); +int RNA_enum_bitflag_identifierss(EnumPropertyItem *item, const int value, const char **identifier); int RNA_enum_name(EnumPropertyItem *item, const int value, const char **name); void RNA_property_enum_items(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, EnumPropertyItem **item, int *totitem, int *free); int RNA_property_enum_value(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *value); int RNA_property_enum_identifier(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier); +int RNA_property_enum_bitflag_identifiers(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier); StructRNA *RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop); diff --git a/source/blender/makesrna/RNA_enum_types.h b/source/blender/makesrna/RNA_enum_types.h index b33dbe6f20d..479397a2180 100644 --- a/source/blender/makesrna/RNA_enum_types.h +++ b/source/blender/makesrna/RNA_enum_types.h @@ -60,6 +60,7 @@ extern EnumPropertyItem nla_mode_blend_items[]; extern EnumPropertyItem event_value_items[]; extern EnumPropertyItem event_type_items[]; +extern EnumPropertyItem operator_return_items[]; extern EnumPropertyItem brush_sculpt_tool_items[]; diff --git a/source/blender/makesrna/RNA_types.h b/source/blender/makesrna/RNA_types.h index 27dd1069c1c..8743bb39d57 100644 --- a/source/blender/makesrna/RNA_types.h +++ b/source/blender/makesrna/RNA_types.h @@ -87,6 +87,8 @@ typedef enum PropertyUnit { #define RNA_SUBTYPE_UNIT(subtype) (subtype & 0x00FF0000) #define RNA_SUBTYPE_UNIT_VALUE(subtype) (subtype>>16) +#define RNA_ENUM_BITFLAG_SIZE 32 + /* also update rna_property_subtypename when you change this */ typedef enum PropertySubType { PROP_NONE = 0, @@ -161,6 +163,9 @@ typedef enum PropertyFlag { PROP_ID_SELF_CHECK = 1<<20, PROP_NEVER_NULL = 1<<18, + /* flag contains multiple enums */ + PROP_ENUM_FLAG = 1<<21, + /* internal flags */ PROP_BUILTIN = 1<<7, PROP_EXPORT = 1<<8, diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index 5ee811d4c46..360f43428d1 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -972,6 +972,18 @@ int RNA_enum_identifier(EnumPropertyItem *item, const int value, const char **id return 0; } +int RNA_enum_bitflag_identifiers(EnumPropertyItem *item, const int value, const char **identifier) +{ + int index= 0; + for (; item->identifier; item++) { + if(item->identifier[0] && item->value & value) { + identifier[index++] = item->identifier; + } + } + identifier[index]= NULL; + return index; +} + int RNA_enum_name(EnumPropertyItem *item, const int value, const char **name) { for (; item->identifier; item++) { @@ -999,6 +1011,22 @@ int RNA_property_enum_identifier(bContext *C, PointerRNA *ptr, PropertyRNA *prop return 0; } +int RNA_property_enum_bitflag_identifiers(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier) +{ + EnumPropertyItem *item= NULL; + int result, free; + + RNA_property_enum_items(C, ptr, prop, &item, NULL, &free); + if(item) { + result= RNA_enum_bitflag_identifiers(item, value, identifier); + if(free) + MEM_freeN(item); + + return result; + } + return 0; +} + const char *RNA_property_ui_name(PropertyRNA *prop) { return rna_ensure_property_name(prop); diff --git a/source/blender/makesrna/intern/rna_wm.c b/source/blender/makesrna/intern/rna_wm.c index 35f2a31b7ef..2e4c0690031 100644 --- a/source/blender/makesrna/intern/rna_wm.c +++ b/source/blender/makesrna/intern/rna_wm.c @@ -243,6 +243,13 @@ EnumPropertyItem keymap_modifiers_items[] = { {2, "SECOND", 0, "Second", ""}, {0, NULL, 0, NULL, NULL}}; +EnumPropertyItem operator_return_items[] = { + {OPERATOR_RUNNING_MODAL, "RUNNING_MODAL", 0, "Running Modal", ""}, + {OPERATOR_CANCELLED, "CANCELLED", 0, "Cancelled", ""}, + {OPERATOR_FINISHED, "FINISHED", 0, "Finished", ""}, + {OPERATOR_PASS_THROUGH, "PASS_THROUGH", 0, "Pass Through", ""}, // used as a flag + {0, NULL, 0, NULL, NULL}}; + #define KMI_TYPE_KEYBOARD 0 #define KMI_TYPE_MOUSE 1 #define KMI_TYPE_TWEAK 2 diff --git a/source/blender/makesrna/intern/rna_wm_api.c b/source/blender/makesrna/intern/rna_wm_api.c index 50b0e37b7cd..ce26072e91b 100644 --- a/source/blender/makesrna/intern/rna_wm_api.c +++ b/source/blender/makesrna/intern/rna_wm_api.c @@ -146,7 +146,11 @@ void RNA_api_wm(StructRNA *srna) RNA_def_property_flag(parm, PROP_REQUIRED); parm= RNA_def_pointer(func, "event", "Event", "", "Event."); RNA_def_property_flag(parm, PROP_REQUIRED); - RNA_def_function_return(func, RNA_def_int(func, "mode", 0, 0, INT_MAX, "Mode", "", 0, INT_MAX)); // XXX, should be an enum/flag thingo + + parm= RNA_def_enum(func, "result", operator_return_items, 0, "result", ""); // better name? + RNA_def_property_flag(parm, PROP_ENUM_FLAG); + RNA_def_function_return(func, parm); + /* invoke functions, for use with python */ func= RNA_def_function(srna, "invoke_popup", "WM_operator_ui_popup"); diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 9370ec91d37..9136a166365 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -379,41 +379,31 @@ static int pyrna_string_to_enum(PyObject *item, PointerRNA *ptr, PropertyRNA *pr return 1; } -PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) +static PyObject *pyrna_enum_to_py(PointerRNA *ptr, PropertyRNA *prop, int val) { - PyObject *ret; - int type = RNA_property_type(prop); + PyObject *ret= NULL; - if (RNA_property_array_check(ptr, prop)) { - return pyrna_py_from_array(ptr, prop); - } - - /* see if we can coorce into a python type - PropertyType */ - switch (type) { - case PROP_BOOLEAN: - ret = PyBool_FromLong( RNA_property_boolean_get(ptr, prop) ); - break; - case PROP_INT: - ret = PyLong_FromSsize_t( (Py_ssize_t)RNA_property_int_get(ptr, prop) ); - break; - case PROP_FLOAT: - ret = PyFloat_FromDouble( RNA_property_float_get(ptr, prop) ); - break; - case PROP_STRING: - { - char *buf; - buf = RNA_property_string_get_alloc(ptr, prop, NULL, -1); - ret = PyUnicode_FromString( buf ); - MEM_freeN(buf); - break; + if(RNA_property_flag(prop) & PROP_ENUM_FLAG) { + const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1]; + int index; + + if ((index=RNA_property_enum_bitflag_identifiers(BPy_GetContext(), ptr, prop, val, identifier))) { + ret= PyTuple_New(index); + index= 0; + + while(identifier[index]) { + PyTuple_SET_ITEM(ret, index, PyUnicode_FromString(identifier[index])); + index++; + } + } + else { + ret= PyTuple_New(0); + } } - case PROP_ENUM: - { + else { const char *identifier; - int val = RNA_property_enum_get(ptr, prop); - if (RNA_property_enum_identifier(BPy_GetContext(), ptr, prop, val, &identifier)) { - ret = PyUnicode_FromString( identifier ); + ret = PyUnicode_FromString(identifier); } else { EnumPropertyItem *item; int free= FALSE; @@ -422,11 +412,11 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) * right values, python code should not generate error for that */ RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free); if(item && item->identifier) { - ret = PyUnicode_FromString( item->identifier ); + ret= PyUnicode_FromString(item->identifier); } else { - char *ptr_name= RNA_struct_name_get_alloc(ptr, NULL, FALSE); - + char *ptr_name= RNA_struct_name_get_alloc(ptr, NULL, FALSE); + /* prefer not fail silently incase of api errors, maybe disable it later */ printf("RNA Warning: Current value \"%d\" matches no enum in '%s', '%s', '%s'\n", val, RNA_struct_identifier(ptr->type), ptr_name, RNA_property_identifier(prop)); @@ -436,8 +426,8 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) PyErr_Warn(PyExc_RuntimeWarning, error_str); #endif - if(ptr_name) - MEM_freeN(ptr_name); + if(ptr_name) + MEM_freeN(ptr_name); ret = PyUnicode_FromString( "" ); } @@ -448,7 +438,42 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) /*PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val); ret = NULL;*/ } + } + + return ret; +} + +PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop) +{ + PyObject *ret; + int type = RNA_property_type(prop); + if (RNA_property_array_check(ptr, prop)) { + return pyrna_py_from_array(ptr, prop); + } + + /* see if we can coorce into a python type - PropertyType */ + switch (type) { + case PROP_BOOLEAN: + ret = PyBool_FromLong( RNA_property_boolean_get(ptr, prop) ); + break; + case PROP_INT: + ret = PyLong_FromSsize_t( (Py_ssize_t)RNA_property_int_get(ptr, prop) ); + break; + case PROP_FLOAT: + ret = PyFloat_FromDouble( RNA_property_float_get(ptr, prop) ); + break; + case PROP_STRING: + { + char *buf; + buf = RNA_property_string_get_alloc(ptr, prop, NULL, -1); + ret = PyUnicode_FromString( buf ); + MEM_freeN(buf); + break; + } + case PROP_ENUM: + { + ret= pyrna_enum_to_py(ptr, prop, RNA_property_enum_get(ptr, prop)); break; } case PROP_POINTER: @@ -2422,22 +2447,7 @@ PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data) } case PROP_ENUM: { - const char *identifier; - int val = *(int*)data; - - if (RNA_property_enum_identifier(BPy_GetContext(), ptr, prop, val, &identifier)) { - ret = PyUnicode_FromString( identifier ); - } else { - /* prefer not fail silently incase of api errors, maybe disable it later */ - char error_str[128]; - sprintf(error_str, "RNA Warning: Current value \"%d\" matches no enum", val); - PyErr_Warn(PyExc_RuntimeWarning, error_str); - - ret = PyUnicode_FromString( "" ); - /*PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val); - ret = NULL;*/ - } - + ret= pyrna_enum_to_py(ptr, prop, *(int*)data); break; } case PROP_POINTER: |