Files
test2/source/blender/blenkernel/intern/property.c

268 lines
5.7 KiB
C
Raw Normal View History

2011-10-10 09:38:02 +00:00
/*
* ***** BEGIN GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
2002-10-12 11:37:38 +00:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
2010-02-12 13:34:04 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2002-10-12 11:37:38 +00:00
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
2011-10-10 09:38:02 +00:00
* Contributor(s): ton roosendaal
2002-10-12 11:37:38 +00:00
*
* ***** END GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*/
2011-02-27 20:40:57 +00:00
/** \file blender/blenkernel/intern/property.c
* \ingroup bke
*
* This module deals with bProperty only,
* they are used on blender objects in the game engine
* (where they get converted into C++ classes - CValue and subclasses)
2011-02-27 20:40:57 +00:00
*/
2002-10-12 11:37:38 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
2002-10-12 11:37:38 +00:00
#include <string.h>
#include <ctype.h>
2002-10-12 11:37:38 +00:00
#include "MEM_guardedalloc.h"
#include "DNA_property_types.h"
#include "DNA_object_types.h"
#include "BLI_blenlib.h"
#include "BKE_property.h"
void BKE_bproperty_free(bProperty *prop)
2002-10-12 11:37:38 +00:00
{
if (prop->poin && prop->poin != &prop->data) MEM_freeN(prop->poin);
2002-10-12 11:37:38 +00:00
MEM_freeN(prop);
}
void BKE_bproperty_free_list(ListBase *lb)
2002-10-12 11:37:38 +00:00
{
bProperty *prop;
while ((prop = BLI_pophead(lb))) {
BKE_bproperty_free(prop);
2002-10-12 11:37:38 +00:00
}
}
bProperty *BKE_bproperty_copy(const bProperty *prop)
2002-10-12 11:37:38 +00:00
{
bProperty *propn;
2012-05-06 17:22:54 +00:00
propn = MEM_dupallocN(prop);
if (prop->poin && prop->poin != &prop->data) {
2012-05-06 17:22:54 +00:00
propn->poin = MEM_dupallocN(prop->poin);
2002-10-12 11:37:38 +00:00
}
else {
propn->poin = &propn->data;
}
2002-10-12 11:37:38 +00:00
return propn;
}
void BKE_bproperty_copy_list(ListBase *lbn, const ListBase *lbo)
2002-10-12 11:37:38 +00:00
{
bProperty *prop, *propn;
BKE_bproperty_free_list(lbn); /* in case we are copying to an object with props */
2012-05-06 17:22:54 +00:00
prop = lbo->first;
while (prop) {
propn = BKE_bproperty_copy(prop);
2002-10-12 11:37:38 +00:00
BLI_addtail(lbn, propn);
2012-05-06 17:22:54 +00:00
prop = prop->next;
2002-10-12 11:37:38 +00:00
}
}
void BKE_bproperty_init(bProperty *prop)
2002-10-12 11:37:38 +00:00
{
/* also use when property changes type */
if (prop->poin && prop->poin != &prop->data) MEM_freeN(prop->poin);
2012-05-06 17:22:54 +00:00
prop->poin = NULL;
2002-10-12 11:37:38 +00:00
2012-05-06 17:22:54 +00:00
prop->data = 0;
2002-10-12 11:37:38 +00:00
switch (prop->type) {
2012-05-06 17:22:54 +00:00
case GPROP_BOOL:
case GPROP_INT:
case GPROP_FLOAT:
case GPROP_TIME:
prop->poin = &prop->data;
break;
case GPROP_STRING:
prop->poin = MEM_callocN(MAX_PROPSTRING, "property string");
break;
2002-10-12 11:37:38 +00:00
}
}
bProperty *BKE_bproperty_new(int type)
2002-10-12 11:37:38 +00:00
{
bProperty *prop;
2012-05-06 17:22:54 +00:00
prop = MEM_callocN(sizeof(bProperty), "property");
prop->type = type;
2002-10-12 11:37:38 +00:00
BKE_bproperty_init(prop);
2002-10-12 11:37:38 +00:00
strcpy(prop->name, "prop");
return prop;
}
bProperty *BKE_bproperty_object_get(Object *ob, const char *name)
2002-10-12 11:37:38 +00:00
{
return BLI_findstring(&ob->prop, name, offsetof(bProperty, name));
2002-10-12 11:37:38 +00:00
}
void BKE_bproperty_object_set(Object *ob, bProperty *propc)
{
bProperty *prop;
prop = BKE_bproperty_object_get(ob, propc->name);
if (prop) {
BLI_remlink(&ob->prop, prop);
BKE_bproperty_free(prop);
}
BLI_addtail(&ob->prop, BKE_bproperty_copy(propc));
}
2002-10-12 11:37:38 +00:00
/* negative: prop is smaller
* positive: prop is larger
*/
#if 0 /* UNUSED */
int BKE_bproperty_cmp(bProperty *prop, const char *str)
2002-10-12 11:37:38 +00:00
{
// extern int Gdfra; /* sector.c */
float fvalue, ftest;
switch (prop->type) {
2012-05-06 17:22:54 +00:00
case GPROP_BOOL:
if (BLI_strcasecmp(str, "true") == 0) {
if (prop->data == 1) return 0;
else return 1;
}
else if (BLI_strcasecmp(str, "false") == 0) {
if (prop->data == 0) return 0;
else return 1;
}
/* no break, do GPROP_int too! */
2002-10-12 11:37:38 +00:00
2012-05-06 17:22:54 +00:00
case GPROP_INT:
return prop->data - atoi(str);
case GPROP_FLOAT:
case GPROP_TIME:
2012-07-07 22:51:57 +00:00
/* WARNING: untested for GPROP_TIME
* function isn't used currently */
2012-05-06 17:22:54 +00:00
fvalue = *((float *)&prop->data);
ftest = (float)atof(str);
if (fvalue > ftest) return 1;
else if (fvalue < ftest) return -1;
return 0;
case GPROP_STRING:
return strcmp(prop->poin, str);
2002-10-12 11:37:38 +00:00
}
return 0;
}
#endif
2002-10-12 11:37:38 +00:00
void BKE_bproperty_set(bProperty *prop, const char *str)
2002-10-12 11:37:38 +00:00
{
// extern int Gdfra; /* sector.c */
switch (prop->type) {
2012-05-06 17:22:54 +00:00
case GPROP_BOOL:
if (BLI_strcasecmp(str, "true") == 0) prop->data = 1;
else if (BLI_strcasecmp(str, "false") == 0) prop->data = 0;
else prop->data = (atoi(str) != 0);
break;
case GPROP_INT:
prop->data = atoi(str);
break;
case GPROP_FLOAT:
case GPROP_TIME:
*((float *)&prop->data) = (float)atof(str);
break;
case GPROP_STRING:
strcpy(prop->poin, str); /* TODO - check size? */
break;
2002-10-12 11:37:38 +00:00
}
}
void BKE_bproperty_add(bProperty *prop, const char *str)
2002-10-12 11:37:38 +00:00
{
// extern int Gdfra; /* sector.c */
switch (prop->type) {
2012-05-06 17:22:54 +00:00
case GPROP_BOOL:
case GPROP_INT:
prop->data += atoi(str);
break;
case GPROP_FLOAT:
case GPROP_TIME:
*((float *)&prop->data) += (float)atof(str);
break;
case GPROP_STRING:
/* strcpy(prop->poin, str); */
break;
2002-10-12 11:37:38 +00:00
}
}
/* reads value of property, sets it in chars in str */
void BKE_bproperty_set_valstr(bProperty *prop, char str[MAX_PROPSTRING])
2002-10-12 11:37:38 +00:00
{
// extern int Gdfra; /* sector.c */
if (str == NULL) return;
2002-10-12 11:37:38 +00:00
switch (prop->type) {
2012-05-06 17:22:54 +00:00
case GPROP_BOOL:
case GPROP_INT:
sprintf(str, "%d", prop->data);
break;
case GPROP_FLOAT:
case GPROP_TIME:
sprintf(str, "%f", *((float *)&prop->data));
break;
case GPROP_STRING:
BLI_strncpy(str, prop->poin, MAX_PROPSTRING);
break;
2002-10-12 11:37:38 +00:00
}
}
#if 0 /* UNUSED */
2002-10-12 11:37:38 +00:00
void cp_property(bProperty *prop1, bProperty *prop2)
{
char str[128];
BKE_bproperty_set_valstr(prop2, str);
2002-10-12 11:37:38 +00:00
BKE_bproperty_set(prop1, str);
2002-10-12 11:37:38 +00:00
}
#endif