From 82cf2aebba1c340c1c4fffde1e89fc80a4591fea Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Fri, 26 Dec 2008 20:38:52 +0000 Subject: RNA: * Added support for using pointers + collections as operator properties, but with the restriction that they must point to other type derived from ID property groups. The "add" function for these properties will allocate a new ID property group and point to that. * Added support for arrays with type IDP_GROUP in ID properties. * Fix bug getting/setting float array values. Example code for collections, note the "OperatorMousePath" type is defined in rna_wm.c and has a float[2] property named "loc". Defining the operator property: prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE); RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath); Adding values: PointerRNA itemptr; float loc[2] = {1, 1}, RNA_collection_add(op->ptr, "path", &itemptr); RNA_float_set_array(&itemptr, "loc", loc); Iterating: RNA_BEGIN(op->ptr, itemptr, "path") { float loc[2]; RNA_float_get_array(&itemptr, "loc", loc); printf("Location: %f %f\n", loc[0], loc[1]); } RNA_END; --- source/blender/blenkernel/intern/idprop.c | 68 ++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 10 deletions(-) (limited to 'source/blender/blenkernel/intern/idprop.c') diff --git a/source/blender/blenkernel/intern/idprop.c b/source/blender/blenkernel/intern/idprop.c index 542a1dff651..bd6dc7d6ed5 100644 --- a/source/blender/blenkernel/intern/idprop.c +++ b/source/blender/blenkernel/intern/idprop.c @@ -61,6 +61,34 @@ static char idp_size_table[] = { /* ----------- Array Type ----------- */ +static void idp_resize_group_array(IDProperty *prop, int newlen, void *newarr) +{ + if(prop->subtype != IDP_GROUP) + return; + + if(newlen >= prop->len) { + /* bigger */ + IDProperty **array= newarr; + IDPropertyTemplate val; + int a; + + for(a=prop->len; adata.pointer; + int a; + + for(a=newlen; alen; a++) { + IDP_FreeProperty(array[a]); + MEM_freeN(array[a]); + } + } +} + /*this function works for strings too!*/ void IDP_ResizeArray(IDProperty *prop, int newlen) { @@ -70,6 +98,7 @@ void IDP_ResizeArray(IDProperty *prop, int newlen) /*first check if the array buffer size has room*/ /*if newlen is 200 chars less then totallen, reallocate anyway*/ if (newlen <= prop->totallen && prop->totallen - newlen < 200) { + idp_resize_group_array(prop, newlen, newarr); prop->len = newlen; return; } @@ -84,11 +113,17 @@ void IDP_ResizeArray(IDProperty *prop, int newlen) */ newsize = (newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize; - newarr = MEM_callocN(idp_size_table[prop->type]*newsize, "idproperty array resized"); - /*newlen is bigger*/ - if (newlen >= prop->len) memcpy(newarr, prop->data.pointer, prop->len*idp_size_table[prop->type]); - /*newlen is smaller*/ - else memcpy(newarr, prop->data.pointer, newlen*prop->len*idp_size_table[prop->type]); + newarr = MEM_callocN(idp_size_table[prop->subtype]*newsize, "idproperty array resized"); + if (newlen >= prop->len) { + /* newlen is bigger*/ + memcpy(newarr, prop->data.pointer, prop->len*idp_size_table[prop->subtype]); + idp_resize_group_array(prop, newlen, newarr); + } + else { + /* newlen is smaller*/ + idp_resize_group_array(prop, newlen, newarr); + memcpy(newarr, prop->data.pointer, newlen*prop->len*idp_size_table[prop->subtype]); + } MEM_freeN(prop->data.pointer); prop->data.pointer = newarr; @@ -96,10 +131,12 @@ void IDP_ResizeArray(IDProperty *prop, int newlen) prop->totallen = newsize; } - void IDP_FreeArray(IDProperty *prop) +void IDP_FreeArray(IDProperty *prop) { - if (prop->data.pointer) + if (prop->data.pointer) { + idp_resize_group_array(prop, 0, NULL); MEM_freeN(prop->data.pointer); + } } @@ -120,7 +157,17 @@ IDProperty *IDP_CopyArray(IDProperty *prop) { IDProperty *newp = idp_generic_copy(prop); - if (prop->data.pointer) newp->data.pointer = MEM_dupallocN(prop->data.pointer); + if (prop->data.pointer) { + newp->data.pointer = MEM_dupallocN(prop->data.pointer); + + if(prop->type == IDP_GROUP) { + IDProperty **array= newp->data.pointer; + int a; + + for(a=0; alen; a++) + array[a]= IDP_CopyProperty(array[a]); + } + } newp->len = prop->len; newp->subtype = prop->subtype; newp->totallen = prop->totallen; @@ -381,11 +428,12 @@ IDProperty *IDP_New(int type, IDPropertyTemplate val, char *name) case IDP_ARRAY: { /*for now, we only support float and int and double arrays*/ - if (val.array.type == IDP_FLOAT || val.array.type == IDP_INT || val.array.type == IDP_DOUBLE) { + if (val.array.type == IDP_FLOAT || val.array.type == IDP_INT || val.array.type == IDP_DOUBLE || val.array.type == IDP_GROUP) { prop = MEM_callocN(sizeof(IDProperty), "IDProperty array"); - prop->len = prop->totallen = val.array.len; prop->subtype = val.array.type; prop->data.pointer = MEM_callocN(idp_size_table[val.array.type]*val.array.len, "id property array"); + idp_resize_group_array(prop, val.array.len, prop->data.pointer); + prop->len = prop->totallen = val.array.len; break; } else { return NULL; -- cgit v1.2.3