4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation, 2005. Full recode
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/editors/animation/anim_ipo_utils.c
30 * \ingroup edanimation
34 /* This file contains code for presenting F-Curves and other animation data
35 * in the UI (especially for use in the Animation Editors).
37 * -- Joshua Leung, Dec 2008
41 #include "MEM_guardedalloc.h"
43 #include "BLI_blenlib.h"
45 #include "BLI_utildefines.h"
47 #include "DNA_anim_types.h"
49 #include "RNA_access.h"
51 #include "ED_anim_api.h"
53 /* ----------------------- Getter functions ----------------------- */
55 /* Write into "name" buffer, the name of the property (retrieved using RNA from the curve's settings),
56 * and return the icon used for the struct that this property refers to
57 * WARNING: name buffer we're writing to cannot exceed 256 chars (check anim_channels_defines.c for details)
59 int getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
66 else if ELEM3(NULL, id, fcu, fcu->rna_path) {
68 sprintf(name, "<invalid>");
69 else if (fcu->rna_path == NULL)
70 sprintf(name, "<no path>");
72 BLI_snprintf(name, 256, "%s[%d]", fcu->rna_path, fcu->array_index);
75 PointerRNA id_ptr, ptr;
78 /* get RNA pointer, and resolve the path */
79 RNA_id_pointer_create(id, &id_ptr);
81 /* try to resolve the path */
82 if (RNA_path_resolve(&id_ptr, fcu->rna_path, &ptr, &prop)) {
83 char *structname=NULL, *propname=NULL, arrayindbuf[16];
84 const char *arrayname=NULL;
85 short free_structname = 0;
87 /* For now, name will consist of 3 parts: struct-name, property name, array index
88 * There are several options possible:
89 * 1) <struct-name>.<property-name>.<array-index>
90 * i.e. Bone1.Location.X, or Object.Location.X
91 * 2) <array-index> <property-name> (<struct name>)
92 * i.e. X Location (Bone1), or X Location (Object)
94 * Currently, option 2 is in use, to try and make it easier to quickly identify F-Curves (it does have
95 * problems with looking rather odd though). Option 1 is better in terms of revealing a consistent sense of
96 * hierarchy though, which isn't so clear with option 2.
100 * - as base, we use a custom name from the structs if one is available
101 * - however, if we're showing subdata of bones (probably there will be other exceptions later)
102 * need to include that info too since it gets confusing otherwise
104 if (strstr(fcu->rna_path, "bones") && strstr(fcu->rna_path, "constraints")) {
105 /* perform string 'chopping' to get "Bone Name : Constraint Name" */
106 char *pchanName= BLI_getQuotedStr(fcu->rna_path, "bones[");
107 char *constName= BLI_getQuotedStr(fcu->rna_path, "constraints[");
109 /* assemble the string to display in the UI... */
110 structname= BLI_sprintfN("%s : %s", pchanName, constName);
113 /* free the temp names */
114 if (pchanName) MEM_freeN(pchanName);
115 if (constName) MEM_freeN(constName);
118 PropertyRNA *nameprop= RNA_struct_name_property(ptr.type);
120 /* this gets a string which will need to be freed */
121 structname= RNA_property_string_get_alloc(&ptr, nameprop, NULL, 0);
125 structname= (char *)RNA_struct_ui_name(ptr.type);
128 /* Property Name is straightforward */
129 propname= (char *)RNA_property_ui_name(prop);
131 /* Array Index - only if applicable */
132 if (RNA_property_array_length(&ptr, prop)) {
133 char c= RNA_property_array_item_char(prop, fcu->array_index);
135 /* we need to write the index to a temp buffer (in py syntax) */
136 if (c) sprintf(arrayindbuf, "%c ", c);
137 else sprintf(arrayindbuf, "[%d]", fcu->array_index);
139 arrayname= &arrayindbuf[0];
146 /* putting this all together into the buffer */
147 // XXX we need to check for invalid names...
148 BLI_snprintf(name, 256, "%s%s (%s)", arrayname, propname, structname);
150 /* free temp name if nameprop is set */
152 MEM_freeN(structname);
155 /* Icon for this property's owner:
156 * use the struct's icon if it is set
158 icon= RNA_struct_ui_icon(ptr.type);
162 BLI_snprintf(name, 256, "\"%s[%d]\"", fcu->rna_path, fcu->array_index);
164 /* icon for this should be the icon for the base ID */
165 // TODO: or should we just use the error icon?
166 icon= RNA_struct_ui_icon(id_ptr.type);
168 /* tag F-Curve as disabled - as not usable path */
169 fcu->flag |= FCURVE_DISABLED;
173 /* return the icon that the active data had */
177 /* ------------------------------- Color Codes for F-Curve Channels ---------------------------- */
179 /* step between the major distinguishable color bands of the primary colors */
180 #define HSV_BANDWIDTH 0.3f
182 /* used to determine the color of F-Curves with FCURVE_COLOR_AUTO_RAINBOW set */
183 //void fcurve_rainbow (unsigned int cur, unsigned int tot, float *out)
184 void getcolor_fcurve_rainbow (int cur, int tot, float *out)
186 float hue, val, sat, fac;
189 /* we try to divide the color into groupings of n colors,
191 * 3 - for 'odd' numbers of curves - there should be a majority of triplets of curves
192 * 4 - for 'even' numbers of curves - there should be a majority of quartets of curves
193 * so the base color is simply one of the three primary colors
195 grouping= (4 - (tot % 2));
196 hue= HSV_BANDWIDTH * (float)(cur % grouping);
198 /* 'Value' (i.e. darkness) needs to vary so that larger sets of three will be
199 * 'darker' (i.e. smaller value), so that they don't look that similar to previous ones.
200 * However, only a range of 0.3 to 1.0 is really usable to avoid clashing
201 * with some other stuff
203 fac = ((float)cur / (float)tot) * 0.7f;
205 /* the base color can get offset a bit so that the colors aren't so identical */
206 hue += fac * HSV_BANDWIDTH;
207 if (hue > 1.0f) hue= fmod(hue, 1.0f);
209 /* saturation adjustments for more visible range */
210 if ((hue > 0.5f) && (hue < 0.8f)) sat= 0.5f;
213 /* value is fixed at 1.0f, otherwise we cannot clearly see the curves... */
216 /* finally, conver this to RGB colors */
217 hsv_to_rgb(hue, sat, val, out, out+1, out+2);