Animato RNA wrapping:
[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;
69                 case ID_PO:
70                         return ICON_POSE_HLT;
71                 case ID_KE:
72                         return ICON_SHAPEKEY;
73                 case ID_MA:
74                         return ICON_MATERIAL;
75                 case ID_WO:
76                         return ICON_WORLD;
77                 case ID_CU:
78                         return ICON_CURVE;
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 struct-name + property name + array index:
124                          *      i.e. Bone1.Location.X, or Object.Location.X
125                          */
126                         
127                         /* for structname, we use a custom name if one is available */
128                                 // xxx we might want an icon from types?
129                                 // xxx it is hard to differentiate between object and bone channels then, if ob + bone motion occur together...
130                         nameprop= RNA_struct_name_property(&ptr);
131                         if (nameprop) {
132                                 /* this gets a string which will need to be freed */
133                                 structname= RNA_property_string_get_alloc(&ptr, nameprop, NULL, 0);
134                         }
135                         else
136                                 structname= (char *)RNA_struct_ui_name(&ptr);
137                         
138                         /* Property Name is straightforward */
139                         propname= (char *)RNA_property_ui_name(&ptr, prop);
140                         
141                         /* Array Index - only if applicable */
142                         if (RNA_property_array_length(&ptr, prop)) {
143                                         // XXX the format of these is not final... we don't know how this will go yet
144                                 static char *vectoritem[4]= {".X", ".Y", ".Z", ".W"};
145                                 static char *quatitem[4]= {".W", ".X", ".Y", ".Z"};
146                                 static char *coloritem[4]= {".R", ".G", ".B", ".A"};
147                                 
148                                 int tot= RNA_property_array_length(&ptr, prop);
149                                 int propsubtype= RNA_property_subtype(&ptr, prop);
150                                 
151                                 /* get string to use for array index */
152                                 if ((tot == 4) && (propsubtype == PROP_ROTATION))
153                                         arrayname= quatitem[fcu->array_index];
154                                 else if ( (tot <= 4) && ((propsubtype == PROP_VECTOR) || (propsubtype == PROP_ROTATION)) )
155                                         arrayname= vectoritem[fcu->array_index];
156                                 else if ((tot <= 4) && (propsubtype == PROP_COLOR))
157                                         arrayname= coloritem[fcu->array_index];
158                                 else {
159                                         /* we need to write the index to a temp buffer (in py syntax), as it is a number... */
160                                         sprintf(arrayindbuf, "[%d]", fcu->array_index);
161                                         arrayname= &arrayindbuf[0];
162                                 }
163                         }
164                         else {
165                                 /* no array index */
166                                 arrayname= "";
167                         }
168                         
169                         /* putting this all together into the buffer */
170                                 // XXX we need to check for invalid names...
171                         BLI_snprintf(name, 128, "%s.%s%s", structname, propname, arrayname); 
172                         
173                         /* free temp name if nameprop is set */
174                         if (nameprop)
175                                 MEM_freeN(structname);
176                 }
177                 else {
178                         /* invalid path */
179                         BLI_snprintf(name, 128, "\"%s[%d]\"", fcu->rna_path, fcu->array_index);
180                 }
181         }
182 }
183
184 /* ------------------------------- Color Codes for F-Curve Channels ---------------------------- */
185
186 unsigned int ipo_rainbow(int cur, int tot)
187 {
188         float dfac, fac, sat;
189         
190         dfac= (float)(1.0/( (float)tot+1.0));
191         
192         /* this calculation makes 2 or 4 different cycles of rainbow colors */
193         if(cur< tot/2) fac= (float)(cur*2.0f*dfac);
194         else fac= (float)((cur-tot/2)*2.0f*dfac +dfac);
195         if(tot > 32) fac= fac*1.95f;
196         if(fac>1.0f) fac-= 1.0f;
197         
198         if(fac>0.5f && fac<0.8f) sat= 0.5f;
199         else sat= 0.6f;
200         
201         return hsv_to_cpack(fac, sat, 1.0f);
202 }