From fc123a40d3afd699005cd90d46cf205a200a79fb Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 2 Feb 2010 23:03:56 +0000 Subject: =?UTF-8?q?patch=20[#20889]=20Support=20"unit"s=20for=20FloatPrope?= =?UTF-8?q?rty=20from=20Martin=20B=C3=BCrbaum=20(pontiac)=20(with=20own=20?= =?UTF-8?q?minor=20changes)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/blender/editors/interface/interface.c | 6 +++++ source/blender/makesrna/RNA_access.h | 1 - source/blender/makesrna/RNA_enum_types.h | 2 ++ source/blender/makesrna/intern/rna_access.c | 11 --------- source/blender/makesrna/intern/rna_rna.c | 23 ++++++++++--------- source/blender/python/intern/bpy_props.c | 34 ++++++++++++++++++---------- source/blender/python/intern/bpy_rna.c | 2 +- 7 files changed, 43 insertions(+), 36 deletions(-) (limited to 'source') diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 7d9d78f5b20..40efabda061 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -1429,6 +1429,12 @@ static double ui_get_but_scale_unit(uiBut *but, double value) if(subtype == PROP_UNIT_LENGTH) { return value * scene->unit.scale_length; } + else if(subtype == PROP_UNIT_AREA) { + return value * pow(scene->unit.scale_length, 2); + } + else if(subtype == PROP_UNIT_VOLUME) { + return value * pow(scene->unit.scale_length, 3); + } else if(subtype == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */ return FRA2TIME(value); } diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 8559f30bfeb..61197b2296e 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -649,7 +649,6 @@ void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *soft int RNA_enum_identifier(EnumPropertyItem *item, const int value, const char **identifier); int RNA_enum_bitflag_identifiers(EnumPropertyItem *item, const int value, const char **identifier); int RNA_enum_name(EnumPropertyItem *item, const int value, const char **name); -int RNA_enum_value(EnumPropertyItem *item, const char *name, int *r_value); 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); diff --git a/source/blender/makesrna/RNA_enum_types.h b/source/blender/makesrna/RNA_enum_types.h index 14b2f0c561e..3e07896d760 100644 --- a/source/blender/makesrna/RNA_enum_types.h +++ b/source/blender/makesrna/RNA_enum_types.h @@ -77,6 +77,8 @@ extern EnumPropertyItem keymap_propvalue_items[]; extern EnumPropertyItem wm_report_items[]; +extern EnumPropertyItem property_unit_items[]; + struct bContext; struct PointerRNA; EnumPropertyItem *rna_TransformOrientation_itemf(struct bContext *C, struct PointerRNA *ptr, int *free); diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index 84baec784df..9dd03ed7477 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -1034,17 +1034,6 @@ int RNA_enum_name(EnumPropertyItem *item, const int value, const char **name) return 0; } -int RNA_enum_value(EnumPropertyItem *item, const char *name, int *r_value) -{ - for (; item->identifier; item++) { - if(item->identifier[0] && strcmp(item->identifier, name)==0) { - *r_value= item->value; - return 1; - } - } - return 0; -} - int RNA_property_enum_identifier(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier) { EnumPropertyItem *item= NULL; diff --git a/source/blender/makesrna/intern/rna_rna.c b/source/blender/makesrna/intern/rna_rna.c index 7de418108ed..b185d8f8961 100644 --- a/source/blender/makesrna/intern/rna_rna.c +++ b/source/blender/makesrna/intern/rna_rna.c @@ -32,6 +32,17 @@ #include "rna_internal.h" +EnumPropertyItem property_unit_items[] = { + {PROP_UNIT_NONE, "NONE", 0, "None", ""}, + {PROP_UNIT_LENGTH, "LENGTH", 0, "Length", ""}, + {PROP_UNIT_AREA, "AREA", 0, "Area", ""}, + {PROP_UNIT_VOLUME, "VOLUME", 0, "Volume", ""}, + {PROP_UNIT_ROTATION, "ROTATION", 0, "Rotation", ""}, + {PROP_UNIT_TIME, "TIME", 0, "Time", ""}, + {PROP_UNIT_VELOCITY, "VELOCITY", 0, "Velocity", ""}, + {PROP_UNIT_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""}, + {0, NULL, 0, NULL, NULL}}; + #ifdef RNA_RUNTIME #include "BLI_ghash.h" @@ -926,16 +937,6 @@ static void rna_def_property(BlenderRNA *brna) {PROP_LAYER, "LAYER", 0, "Layer", ""}, {PROP_LAYER_MEMBER, "LAYER_MEMBERSHIP", 0, "Layer Membership", ""}, {0, NULL, 0, NULL, NULL}}; - static EnumPropertyItem unit_items[] = { - {PROP_UNIT_NONE, "NONE", 0, "None", ""}, - {PROP_UNIT_LENGTH, "LENGTH", 0, "Length", ""}, - {PROP_UNIT_AREA, "AREA", 0, "Area", ""}, - {PROP_UNIT_VOLUME, "VOLUME", 0, "Volume", ""}, - {PROP_UNIT_ROTATION, "ROTATION", 0, "Rotation", ""}, - {PROP_UNIT_TIME, "TIME", 0, "Time", ""}, - {PROP_UNIT_VELOCITY, "VELOCITY", 0, "Velocity", ""}, - {PROP_UNIT_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""}, - {0, NULL, 0, NULL, NULL}}; srna= RNA_def_struct(brna, "Property", NULL); RNA_def_struct_ui_text(srna, "Property Definition", "RNA property definition."); @@ -978,7 +979,7 @@ static void rna_def_property(BlenderRNA *brna) prop= RNA_def_property(srna, "unit", PROP_ENUM, PROP_NONE); RNA_def_property_clear_flag(prop, PROP_EDITABLE); - RNA_def_property_enum_items(prop, unit_items); + RNA_def_property_enum_items(prop, property_unit_items); RNA_def_property_enum_funcs(prop, "rna_Property_unit_get", NULL, NULL); RNA_def_property_ui_text(prop, "Unit", "Type of units for this property."); diff --git a/source/blender/python/intern/bpy_props.c b/source/blender/python/intern/bpy_props.c index 2268ca1ab6d..75b31e9f732 100644 --- a/source/blender/python/intern/bpy_props.c +++ b/source/blender/python/intern/bpy_props.c @@ -28,6 +28,7 @@ #include "RNA_access.h" #include "RNA_define.h" /* for defining our own rna */ +#include "RNA_enum_types.h" #include "MEM_guardedalloc.h" @@ -127,7 +128,7 @@ PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw) if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "BoolProperty(options={...}):")) return NULL; - if(pysubtype && RNA_enum_value(property_subtype_number_items, pysubtype, &subtype)==0) { + if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) { PyErr_Format(PyExc_TypeError, "BoolProperty(subtype='%s'): invalid subtype."); return NULL; } @@ -189,7 +190,7 @@ PyObject *BPy_BoolVectorProperty(PyObject *self, PyObject *args, PyObject *kw) if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "BoolVectorProperty(options={...}):")) return NULL; - if(pysubtype && RNA_enum_value(property_subtype_array_items, pysubtype, &subtype)==0) { + if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) { PyErr_Format(PyExc_TypeError, "BoolVectorProperty(subtype='%s'): invalid subtype."); return NULL; } @@ -258,7 +259,7 @@ PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw) if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "IntProperty(options={...}):")) return NULL; - if(pysubtype && RNA_enum_value(property_subtype_number_items, pysubtype, &subtype)==0) { + if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) { PyErr_Format(PyExc_TypeError, "IntProperty(subtype='%s'): invalid subtype."); return NULL; } @@ -321,7 +322,7 @@ PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw) if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "IntVectorProperty(options={...}):")) return NULL; - if(pysubtype && RNA_enum_value(property_subtype_array_items, pysubtype, &subtype)==0) { + if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) { PyErr_Format(PyExc_TypeError, "IntVectorProperty(subtype='%s'): invalid subtype."); return NULL; } @@ -355,14 +356,16 @@ PyObject *BPy_IntVectorProperty(PyObject *self, PyObject *args, PyObject *kw) static char BPy_FloatProperty_doc[] = -".. function:: FloatProperty(name=\"\", description=\"\", default=0.0, min=sys.float_info.min, max=sys.float_info.max, soft_min=sys.float_info.min, soft_max=sys.float_info.max, step=3, precision=2, options={'ANIMATABLE'}, subtype='NONE')\n" +".. function:: FloatProperty(name=\"\", description=\"\", default=0.0, min=sys.float_info.min, max=sys.float_info.max, soft_min=sys.float_info.min, soft_max=sys.float_info.max, step=3, precision=2, options={'ANIMATABLE'}, subtype='NONE', unit='NONE')\n" "\n" " Returns a new float property definition.\n" "\n" " :arg options: Enumerator in ['HIDDEN', 'ANIMATABLE'].\n" " :type options: set\n" " :arg subtype: Enumerator in ['UNSIGNED', 'PERCENTAGE', 'FACTOR', 'ANGLE', 'TIME', 'DISTANCE', 'NONE'].\n" -" :type subtype: string"; +" :type subtype: string\n" +" :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION'].\n" +" :type unit: string\n"; PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw) { StructRNA *srna; @@ -377,7 +380,7 @@ PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw) return NULL; /* self's type was compatible but error getting the srna */ } else if(srna) { - static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", NULL}; + static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "precision", "options", "subtype", "unit", NULL}; char *id=NULL, *name="", *description=""; float min=-FLT_MAX, max=FLT_MAX, soft_min=-FLT_MAX, soft_max=FLT_MAX, step=3, def=0.0f; int precision= 2; @@ -386,19 +389,26 @@ PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw) int opts=0; char *pysubtype= NULL; int subtype= PROP_NONE; + char *pyunit= NULL; + int unit= PROP_UNIT_NONE; - if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssffffffiO!s:FloatProperty", kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &precision, &PySet_Type, &pyopts, &pysubtype)) + if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssffffffiO!ss:FloatProperty", kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &precision, &PySet_Type, &pyopts, &pysubtype, &pyunit)) return NULL; if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "FloatProperty(options={...}):")) return NULL; - if(pysubtype && RNA_enum_value(property_subtype_number_items, pysubtype, &subtype)==0) { + if(pysubtype && RNA_enum_value_from_id(property_subtype_number_items, pysubtype, &subtype)==0) { PyErr_Format(PyExc_TypeError, "FloatProperty(subtype='%s'): invalid subtype."); return NULL; } - prop= RNA_def_property(srna, id, PROP_FLOAT, subtype); + if(pyunit && RNA_enum_value_from_id(property_unit_items, pyunit, &unit)==0) { + PyErr_Format(PyExc_TypeError, "FloatProperty(unit='%s'): invalid unit."); + return NULL; + } + + prop= RNA_def_property(srna, id, PROP_FLOAT, subtype | unit); RNA_def_property_float_default(prop, def); RNA_def_property_range(prop, min, max); RNA_def_property_ui_text(prop, name, description); @@ -456,7 +466,7 @@ PyObject *BPy_FloatVectorProperty(PyObject *self, PyObject *args, PyObject *kw) if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "FloatVectorProperty(options={...}):")) return NULL; - if(pysubtype && RNA_enum_value(property_subtype_array_items, pysubtype, &subtype)==0) { + if(pysubtype && RNA_enum_value_from_id(property_subtype_array_items, pysubtype, &subtype)==0) { PyErr_Format(PyExc_TypeError, "FloatVectorProperty(subtype='%s'): invalid subtype."); return NULL; } @@ -526,7 +536,7 @@ PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw) if(pyopts && pyrna_set_to_enum_bitfield(property_flag_items, pyopts, &opts, "StringProperty(options={...}):")) return NULL; - if(pysubtype && RNA_enum_value(property_subtype_string_items, pysubtype, &subtype)==0) { + if(pysubtype && RNA_enum_value_from_id(property_subtype_string_items, pysubtype, &subtype)==0) { PyErr_Format(PyExc_TypeError, "StringProperty(subtype='%s'): invalid subtype."); return NULL; } diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index ea445ecc595..fde805f94ee 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -442,7 +442,7 @@ int pyrna_set_to_enum_bitfield(EnumPropertyItem *items, PyObject *value, int *r_ return -1; } - if(RNA_enum_value(items, param, &ret) == 0) { + if(RNA_enum_value_from_id(items, param, &ret) == 0) { char *enum_str= BPy_enum_as_string(items); PyErr_Format(PyExc_TypeError, "%s \"%.200s\" not found in (%.200s)", error_prefix, param, enum_str); MEM_freeN(enum_str); -- cgit v1.2.3