Blender  V2.59
anim_ipo_utils.c
Go to the documentation of this file.
00001 /*
00002  * $Id: anim_ipo_utils.c 35242 2011-02-27 20:29:51Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * Contributor(s): Blender Foundation, 2005. Full recode
00024  *                                 Joshua Leung
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 /* This file contains code for presenting F-Curves and other animation data
00035  * in the UI (especially for use in the Animation Editors).
00036  *
00037  * -- Joshua Leung, Dec 2008
00038  */
00039 
00040 
00041 #include "MEM_guardedalloc.h"
00042 
00043 #include "BLI_blenlib.h"
00044 #include "BLI_math.h"
00045 #include "BLI_utildefines.h"
00046 
00047 #include "DNA_anim_types.h"
00048 
00049 #include "RNA_access.h"
00050 
00051 #include "ED_anim_api.h"
00052 
00053 /* ----------------------- Getter functions ----------------------- */
00054 
00055 /* Write into "name" buffer, the name of the property (retrieved using RNA from the curve's settings),
00056  * and return the icon used for the struct that this property refers to 
00057  * WARNING: name buffer we're writing to cannot exceed 256 chars (check anim_channels_defines.c for details)
00058  */
00059 int getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
00060 {
00061         int icon = 0;
00062         
00063         /* sanity checks */
00064         if (name == NULL)
00065                 return icon;
00066         else if ELEM3(NULL, id, fcu, fcu->rna_path) {
00067                 if (fcu == NULL)
00068                         sprintf(name, "<invalid>");
00069                 else if (fcu->rna_path == NULL)
00070                         sprintf(name, "<no path>");
00071                 else /* id == NULL */
00072                         BLI_snprintf(name, 256, "%s[%d]", fcu->rna_path, fcu->array_index);
00073         }
00074         else {
00075                 PointerRNA id_ptr, ptr;
00076                 PropertyRNA *prop;
00077                 
00078                 /* get RNA pointer, and resolve the path */
00079                 RNA_id_pointer_create(id, &id_ptr);
00080                 
00081                 /* try to resolve the path */
00082                 if (RNA_path_resolve(&id_ptr, fcu->rna_path, &ptr, &prop)) {
00083                         char *structname=NULL, *propname=NULL, arrayindbuf[16];
00084                         const char *arrayname=NULL;
00085                         short free_structname = 0;
00086                         
00087                         /* For now, name will consist of 3 parts: struct-name, property name, array index
00088                          * There are several options possible:
00089                          *      1) <struct-name>.<property-name>.<array-index>
00090                          *              i.e. Bone1.Location.X, or Object.Location.X
00091                          *      2) <array-index> <property-name> (<struct name>)
00092                          *              i.e. X Location (Bone1), or X Location (Object)
00093                          *      
00094                          * Currently, option 2 is in use, to try and make it easier to quickly identify F-Curves (it does have
00095                          * problems with looking rather odd though). Option 1 is better in terms of revealing a consistent sense of 
00096                          * hierarchy though, which isn't so clear with option 2.
00097                          */
00098                         
00099                         /* for structname
00100                          *      - as base, we use a custom name from the structs if one is available 
00101                          *      - however, if we're showing subdata of bones (probably there will be other exceptions later)
00102                          *        need to include that info too since it gets confusing otherwise
00103                          */
00104                         if (strstr(fcu->rna_path, "bones") && strstr(fcu->rna_path, "constraints")) {
00105                                 /* perform string 'chopping' to get "Bone Name : Constraint Name" */
00106                                 char *pchanName= BLI_getQuotedStr(fcu->rna_path, "bones[");
00107                                 char *constName= BLI_getQuotedStr(fcu->rna_path, "constraints[");
00108                                 
00109                                 /* assemble the string to display in the UI... */
00110                                 structname= BLI_sprintfN("%s : %s", pchanName, constName);
00111                                 free_structname= 1;
00112                                 
00113                                 /* free the temp names */
00114                                 if (pchanName) MEM_freeN(pchanName);
00115                                 if (constName) MEM_freeN(constName);
00116                         }
00117                         else {
00118                                 PropertyRNA *nameprop= RNA_struct_name_property(ptr.type);
00119                                 if (nameprop) {
00120                                         /* this gets a string which will need to be freed */
00121                                         structname= RNA_property_string_get_alloc(&ptr, nameprop, NULL, 0);
00122                                         free_structname= 1;
00123                                 }
00124                                 else
00125                                         structname= (char *)RNA_struct_ui_name(ptr.type);
00126                         }
00127                         
00128                         /* Property Name is straightforward */
00129                         propname= (char *)RNA_property_ui_name(prop);
00130                         
00131                         /* Array Index - only if applicable */
00132                         if (RNA_property_array_length(&ptr, prop)) {
00133                                 char c= RNA_property_array_item_char(prop, fcu->array_index);
00134                                 
00135                                 /* we need to write the index to a temp buffer (in py syntax) */
00136                                 if (c) sprintf(arrayindbuf, "%c ", c);
00137                                 else sprintf(arrayindbuf, "[%d]", fcu->array_index);
00138                                 
00139                                 arrayname= &arrayindbuf[0];
00140                         }
00141                         else {
00142                                 /* no array index */
00143                                 arrayname= "";
00144                         }
00145                         
00146                         /* putting this all together into the buffer */
00147                         // XXX we need to check for invalid names...
00148                         BLI_snprintf(name, 256, "%s%s (%s)", arrayname, propname, structname); 
00149                         
00150                         /* free temp name if nameprop is set */
00151                         if (free_structname)
00152                                 MEM_freeN(structname);
00153                         
00154                         
00155                         /* Icon for this property's owner:
00156                          *      use the struct's icon if it is set
00157                          */
00158                         icon= RNA_struct_ui_icon(ptr.type);
00159                 }
00160                 else {
00161                         /* invalid path */
00162                         BLI_snprintf(name, 256, "\"%s[%d]\"", fcu->rna_path, fcu->array_index);
00163                         
00164                         /* icon for this should be the icon for the base ID */
00165                         // TODO: or should we just use the error icon?
00166                         icon= RNA_struct_ui_icon(id_ptr.type);
00167                         
00168                         /* tag F-Curve as disabled - as not usable path */
00169                         fcu->flag |= FCURVE_DISABLED;
00170                 }
00171         }
00172         
00173         /* return the icon that the active data had */
00174         return icon;
00175 }
00176 
00177 /* ------------------------------- Color Codes for F-Curve Channels ---------------------------- */
00178 
00179 /* step between the major distinguishable color bands of the primary colors */
00180 #define HSV_BANDWIDTH   0.3f
00181 
00182 /* used to determine the color of F-Curves with FCURVE_COLOR_AUTO_RAINBOW set */
00183 //void fcurve_rainbow (unsigned int cur, unsigned int tot, float *out)
00184 void getcolor_fcurve_rainbow (int cur, int tot, float *out)
00185 {
00186         float hue, val, sat, fac;
00187         int grouping;
00188         
00189         /* we try to divide the color into groupings of n colors,
00190          * where n is:
00191          *      3 - for 'odd' numbers of curves - there should be a majority of triplets of curves
00192          *      4 - for 'even' numbers of curves - there should be a majority of quartets of curves
00193          * so the base color is simply one of the three primary colors
00194          */
00195         grouping= (4 - (tot % 2));
00196         hue= HSV_BANDWIDTH * (float)(cur % grouping);
00197         
00198         /* 'Value' (i.e. darkness) needs to vary so that larger sets of three will be 
00199          * 'darker' (i.e. smaller value), so that they don't look that similar to previous ones.
00200          * However, only a range of 0.3 to 1.0 is really usable to avoid clashing
00201          * with some other stuff 
00202          */
00203         fac = ((float)cur / (float)tot) * 0.7f;
00204         
00205         /* the base color can get offset a bit so that the colors aren't so identical */
00206         hue += fac * HSV_BANDWIDTH; 
00207         if (hue > 1.0f) hue= fmod(hue, 1.0f);
00208         
00209         /* saturation adjustments for more visible range */
00210         if ((hue > 0.5f) && (hue < 0.8f)) sat= 0.5f;
00211         else sat= 0.6f;
00212         
00213         /* value is fixed at 1.0f, otherwise we cannot clearly see the curves... */
00214         val= 1.0f;
00215         
00216         /* finally, conver this to RGB colors */
00217         hsv_to_rgb(hue, sat, val, out, out+1, out+2); 
00218 }