2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r18677...
[blender-staging.git] / source / blender / editors / animation / anim_ipo_utils.c
1 /**
2  * $Id: 
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2005. Full recode
24  *                                 Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /* This file contains code for presenting F-Curves and other animation data
30  * in the UI (especially for use in the Animation Editors).
31  *
32  * -- Joshua Leung, Dec 2008
33  */
34
35
36 #include <stdio.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_arithb.h"
42
43 #include "DNA_anim_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_view3d_types.h"
49
50 #include "BKE_animsys.h"
51 #include "BKE_key.h"
52 #include "BKE_utildefines.h"
53
54 #include "UI_resources.h"
55 #include "ED_anim_api.h"
56
57 #include "RNA_access.h"
58 #include "RNA_types.h"
59
60 /* ----------------------- Getter functions ----------------------- */
61
62 /* gets the appropriate icon for the given blocktype */
63 // XXX some of these will be depreceated?
64 int geticon_anim_blocktype(short blocktype)
65 {
66         switch (blocktype) {
67                 case ID_OB:
68                         return ICON_OBJECT_DATA;
69                 case ID_PO:
70                         return ICON_POSE_HLT;
71                 case ID_KE:
72                         return ICON_SHAPEKEY_DATA;
73                 case ID_MA:
74                         return ICON_MATERIAL;
75                 case ID_WO:
76                         return ICON_WORLD;
77                 case ID_CU:
78                         return ICON_CURVE_DATA;
79                 case ID_CA:
80                         return ICON_CAMERA;
81                 case ID_LA:
82                         return ICON_LAMP;
83                 case ID_TE:
84                         return ICON_TEXTURE;
85                 case ID_CO:
86                         return ICON_CONSTRAINT;
87                 case ID_FLUIDSIM:
88                         return ICON_WORLD; // uggh
89                 default:
90                         return 0; // what about blank icon?
91         }
92 }
93
94 /* Write into "name" buffer, the name of the property (retrieved using RNA from the curve's settings) 
95  * WARNING: name buffer we're writing to cannot exceed 128 chars (check action_draw.c for details)
96  */
97 // TODO: have an extra var to indicate if prop was valid?
98 void getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
99 {
100         /* sanity checks */
101         if (name == NULL)
102                 return;
103         else if ELEM3(NULL, id, fcu, fcu->rna_path) {
104                 if (fcu == NULL)
105                         sprintf(name, "<invalid>");
106                 else if (fcu->rna_path == NULL)
107                         sprintf(name, "<no path>");
108                 else /* id == NULL */
109                         BLI_snprintf(name, 128, "%s[%d]", fcu->rna_path, fcu->array_index);
110         }
111         else {
112                 PointerRNA id_ptr, ptr;
113                 PropertyRNA *prop;
114                 
115                 /* get RNA pointer, and resolve the path */
116                 RNA_id_pointer_create(id, &id_ptr);
117                 
118                 /* try to resolve the path */
119                 if (RNA_path_resolve(&id_ptr, fcu->rna_path, &ptr, &prop)) {
120                         char *structname=NULL, *propname=NULL, *arrayname=NULL, arrayindbuf[16];
121                         PropertyRNA *nameprop;
122                         
123                         /* For now, name will consist of 3 parts: struct-name, property name, array index
124                          * There are several options possible:
125                          *      1) <struct-name>.<property-name>.<array-index>
126                          *              i.e. Bone1.Location.X, or Object.Location.X
127                          *      2) <array-index> <property-name> (<struct name>)
128                          *              i.e. X Location (Bone1), or X Location (Object)
129                          *      
130                          * Currently, option 2 is in use, to try and make it easier to quickly identify F-Curves (it does have
131                          * problems with looking rather odd though). Option 1 is better in terms of revealing a consistent sense of 
132                          * hierarchy though, which isn't so clear with option 2.
133                          */
134                         
135                         /* for structname, we use a custom name if one is available */
136                                 // xxx we might want an icon from types?
137                                 // xxx it is hard to differentiate between object and bone channels then, if ob + bone motion occur together...
138                         nameprop= RNA_struct_name_property(&ptr);
139                         if (nameprop) {
140                                 /* this gets a string which will need to be freed */
141                                 structname= RNA_property_string_get_alloc(&ptr, nameprop, NULL, 0);
142                         }
143                         else
144                                 structname= (char *)RNA_struct_ui_name(&ptr);
145                         
146                         /* Property Name is straightforward */
147                         propname= (char *)RNA_property_ui_name(&ptr, prop);
148                         
149                         /* Array Index - only if applicable */
150                         if (RNA_property_array_length(&ptr, prop)) {
151                                         // XXX the format of these is not final... we don't know how this will go yet
152                                         // format 1 style
153                                 //static char *vectoritem[4]= {".X", ".Y", ".Z", ".W"};
154                                 //static char *quatitem[4]= {".W", ".X", ".Y", ".Z"};
155                                 //static char *coloritem[4]= {".R", ".G", ".B", ".A"};
156                                         // format 2 style
157                                 static char *vectoritem[4]= {"X ", "Y ", "Z ", "W "};
158                                 static char *quatitem[4]= {"W ", "X ", "Y ", "Z "};
159                                 static char *coloritem[4]= {"R ", "G ", "B ", "A "};
160                                 
161                                 int tot= RNA_property_array_length(&ptr, prop);
162                                 int propsubtype= RNA_property_subtype(&ptr, prop);
163                                 
164                                 /* get string to use for array index */
165                                 if ((tot == 4) && (propsubtype == PROP_ROTATION))
166                                         arrayname= quatitem[fcu->array_index];
167                                 else if ( (tot <= 4) && ((propsubtype == PROP_VECTOR) || (propsubtype == PROP_ROTATION)) )
168                                         arrayname= vectoritem[fcu->array_index];
169                                 else if ((tot <= 4) && (propsubtype == PROP_COLOR))
170                                         arrayname= coloritem[fcu->array_index];
171                                 else {
172                                         /* we need to write the index to a temp buffer (in py syntax), as it is a number... */
173                                         sprintf(arrayindbuf, "[%d]", fcu->array_index);
174                                         arrayname= &arrayindbuf[0];
175                                 }
176                         }
177                         else {
178                                 /* no array index */
179                                 arrayname= "";
180                         }
181                         
182                         /* putting this all together into the buffer */
183                         // XXX we need to check for invalid names...
184                         //BLI_snprintf(name, 128, "%s.%s%s", structname, propname, arrayname);  // format 1
185                         BLI_snprintf(name, 128, "%s%s (%s)", arrayname, propname, structname);  // format 2
186                         
187                         /* free temp name if nameprop is set */
188                         if (nameprop)
189                                 MEM_freeN(structname);
190                 }
191                 else {
192                         /* invalid path */
193                         BLI_snprintf(name, 128, "\"%s[%d]\"", fcu->rna_path, fcu->array_index);
194                 }
195         }
196 }
197
198 /* ------------------------------- Color Codes for F-Curve Channels ---------------------------- */
199
200 /* used for FCURVE_COLOR_AUTO_RAINBOW */
201 // XXX this still doesn't work too great when there are more than 32 curves (which happens most of the time)
202 void ipo_rainbow (int cur, int tot, float *out)
203 {
204         float dfac, fac, sat;
205         
206         dfac= (float)(1.0/( (float)tot+1.0));
207         
208         /* this calculation makes 2 or 4 different cycles of rainbow colors */
209         if(cur< tot/2) fac= (float)(cur*2.0f*dfac);
210         else fac= (float)((cur-tot/2)*2.0f*dfac +dfac);
211         if(tot > 32) fac= fac*1.95f;
212         if(fac>1.0f) fac-= 1.0f;
213         
214         if(fac>0.5f && fac<0.8f) sat= 0.5f;
215         else sat= 0.6f;
216         
217         //return hsv_to_cpack(fac, sat, 1.0f);
218         hsv_to_rgb(fac, sat, 1.0f, out, out+1, out+2);
219 }