This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenkernel/intern/property.c

322 lines
7.1 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(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, 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;
}
/* used by BKE_bproperty_unique() only */
static bProperty *bproperty_get(bProperty *first, bProperty *self, const char *name)
{
bProperty *p;
2012-05-06 17:22:54 +00:00
for (p = first; p; p = p->next) {
if (p != self && (strcmp(p->name, name) == 0))
return p;
}
return NULL;
}
void BKE_bproperty_unique(bProperty *first, bProperty *prop, int force)
{
bProperty *p;
/* set the first if its not set */
2012-05-06 17:22:54 +00:00
if (first == NULL) {
first = prop;
while (first->prev) {
2012-05-06 17:22:54 +00:00
first = first->prev;
}
}
if (force) {
/* change other names to make them unique */
while ((p = bproperty_get(first, prop, prop->name))) {
BKE_bproperty_unique(first, p, 0);
}
}
else {
/* change our own name until its unique */
if (bproperty_get(first, prop, prop->name)) {
/* there is a collision */
char new_name[sizeof(prop->name)];
char base_name[sizeof(prop->name)];
char num[sizeof(prop->name)];
2012-05-06 17:22:54 +00:00
int i = 0;
/* strip numbers */
BLI_strncpy(base_name, prop->name, sizeof(base_name));
2012-05-06 17:22:54 +00:00
for (i = strlen(base_name) - 1; (i >= 0 && isdigit(base_name[i])); i--) {
base_name[i] = '\0';
}
2012-05-06 17:22:54 +00:00
i = 0;
do { /* ensure we have enough chars for the new number in the name */
const size_t num_len = BLI_snprintf(num, sizeof(num), "%d", i++);
BLI_snprintf(new_name, sizeof(prop->name),
"%.*s%s", (int)(sizeof(prop->name) - num_len), base_name, num);
} while (bproperty_get(first, prop, new_name));
BLI_strncpy(prop->name, new_name, sizeof(prop->name));
}
}
}
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) {
BKE_bproperty_free(prop);
BLI_remlink(&ob->prop, 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