1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup edanimation
22 */
23
24 /* This file contains code for presenting F-Curves and other animation data
25 * in the UI (especially for use in the Animation Editors).
26 *
27 * -- Joshua Leung, Dec 2008
28 */
29
30 #include "MEM_guardedalloc.h"
31
32 #include "BLI_blenlib.h"
33 #include "BLI_math.h"
34 #include "BLI_utildefines.h"
35
36 #include "BLT_translation.h"
37
38 #include "DNA_anim_types.h"
39
40 #include "RNA_access.h"
41
42 #include "ED_anim_api.h"
43
44 /* ----------------------- Getter functions ----------------------- */
45
46 /**
47 * Write into "name" buffer, the name of the property
48 * (retrieved using RNA from the curve's settings),
49 * and return the icon used for the struct that this property refers to
50 *
51 * \warning name buffer we're writing to cannot exceed 256 chars
52 * (check anim_channels_defines.c for details).
53 */
getname_anim_fcurve(char * name,ID * id,FCurve * fcu)54 int getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
55 {
56 int icon = 0;
57
58 /* sanity checks */
59 if (name == NULL) {
60 return icon;
61 }
62
63 if (ELEM(NULL, id, fcu, fcu->rna_path)) {
64 if (fcu == NULL) {
65 strcpy(name, TIP_("<invalid>"));
66 }
67 else if (fcu->rna_path == NULL) {
68 strcpy(name, TIP_("<no path>"));
69 }
70 else { /* id == NULL */
71 BLI_snprintf(name, 256, "%s[%d]", fcu->rna_path, fcu->array_index);
72 }
73 }
74 else {
75 PointerRNA id_ptr, ptr;
76 PropertyRNA *prop;
77
78 /* get RNA pointer, and resolve the path */
79 RNA_id_pointer_create(id, &id_ptr);
80
81 /* try to resolve the path */
82 if (RNA_path_resolve_property(&id_ptr, fcu->rna_path, &ptr, &prop)) {
83 const char *structname = NULL, *propname = NULL;
84 char arrayindbuf[16];
85 const char *arrayname = NULL;
86 short free_structname = 0;
87
88 /* For now, name will consist of 3 parts: struct-name, property name, array index
89 * There are several options possible:
90 * 1) <struct-name>.<property-name>.<array-index>
91 * i.e. Bone1.Location.X, or Object.Location.X
92 * 2) <array-index> <property-name> (<struct name>)
93 * i.e. X Location (Bone1), or X Location (Object)
94 *
95 * Currently, option 2 is in use, to try and make it easier to quickly identify F-Curves
96 * (it does have problems with looking rather odd though).
97 * Option 1 is better in terms of revealing a consistent sense of hierarchy though,
98 * which isn't so clear with option 2.
99 */
100
101 /* For structname:
102 * - As base, we use a custom name from the structs if one is available
103 * - However, if we're showing subdata of bones
104 * (probably there will be other exceptions later).
105 * need to include that info too since it gets confusing otherwise.
106 * - If a pointer just refers to the ID-block, then don't repeat this info
107 * since this just introduces clutter.
108 */
109 if (strstr(fcu->rna_path, "bones") && strstr(fcu->rna_path, "constraints")) {
110 /* perform string 'chopping' to get "Bone Name : Constraint Name" */
111 char *pchanName = BLI_str_quoted_substrN(fcu->rna_path, "bones[");
112 char *constName = BLI_str_quoted_substrN(fcu->rna_path, "constraints[");
113
114 /* assemble the string to display in the UI... */
115 structname = BLI_sprintfN("%s : %s", pchanName, constName);
116 free_structname = 1;
117
118 /* free the temp names */
119 if (pchanName) {
120 MEM_freeN(pchanName);
121 }
122 if (constName) {
123 MEM_freeN(constName);
124 }
125 }
126 else if (ptr.data != ptr.owner_id) {
127 PropertyRNA *nameprop = RNA_struct_name_property(ptr.type);
128 if (nameprop) {
129 /* this gets a string which will need to be freed */
130 structname = RNA_property_string_get_alloc(&ptr, nameprop, NULL, 0, NULL);
131 free_structname = 1;
132 }
133 else {
134 structname = RNA_struct_ui_name(ptr.type);
135 }
136 }
137
138 /* Property Name is straightforward */
139 propname = RNA_property_ui_name(prop);
140
141 /* Array Index - only if applicable */
142 if (RNA_property_array_check(prop)) {
143 char c = RNA_property_array_item_char(prop, fcu->array_index);
144
145 /* we need to write the index to a temp buffer (in py syntax) */
146 if (c) {
147 BLI_snprintf(arrayindbuf, sizeof(arrayindbuf), "%c ", c);
148 }
149 else {
150 BLI_snprintf(arrayindbuf, sizeof(arrayindbuf), "[%d]", fcu->array_index);
151 }
152
153 arrayname = &arrayindbuf[0];
154 }
155 else {
156 /* no array index */
157 arrayname = "";
158 }
159
160 /* putting this all together into the buffer */
161 /* XXX we need to check for invalid names...
162 * XXX the name length limit needs to be passed in or as some define */
163 if (structname) {
164 BLI_snprintf(name, 256, "%s%s (%s)", arrayname, propname, structname);
165 }
166 else {
167 BLI_snprintf(name, 256, "%s%s", arrayname, propname);
168 }
169
170 /* free temp name if nameprop is set */
171 if (free_structname) {
172 MEM_freeN((void *)structname);
173 }
174
175 /* Icon for this property's owner:
176 * use the struct's icon if it is set
177 */
178 icon = RNA_struct_ui_icon(ptr.type);
179
180 /* valid path - remove the invalid tag since we now know how to use it saving
181 * users manual effort to re-enable using "Revive Disabled FCurves" T29629. */
182 fcu->flag &= ~FCURVE_DISABLED;
183 }
184 else {
185 /* invalid path */
186 BLI_snprintf(name, 256, "\"%s[%d]\"", fcu->rna_path, fcu->array_index);
187
188 /* icon for this should be the icon for the base ID */
189 /* TODO: or should we just use the error icon? */
190 icon = RNA_struct_ui_icon(id_ptr.type);
191
192 /* tag F-Curve as disabled - as not usable path */
193 fcu->flag |= FCURVE_DISABLED;
194 }
195 }
196
197 /* return the icon that the active data had */
198 return icon;
199 }
200
201 /* ------------------------------- Color Codes for F-Curve Channels ---------------------------- */
202
203 /* step between the major distinguishable color bands of the primary colors */
204 #define HSV_BANDWIDTH 0.3f
205
206 /* used to determine the color of F-Curves with FCURVE_COLOR_AUTO_RAINBOW set */
207 // void fcurve_rainbow(uint cur, uint tot, float *out)
getcolor_fcurve_rainbow(int cur,int tot,float out[3])208 void getcolor_fcurve_rainbow(int cur, int tot, float out[3])
209 {
210 float hsv[3], fac;
211 int grouping;
212
213 /* we try to divide the color into groupings of n colors,
214 * where n is:
215 * 3 - for 'odd' numbers of curves - there should be a majority of triplets of curves
216 * 4 - for 'even' numbers of curves - there should be a majority of quartets of curves
217 * so the base color is simply one of the three primary colors
218 */
219 grouping = (4 - (tot % 2));
220 hsv[0] = HSV_BANDWIDTH * (float)(cur % grouping);
221
222 /* 'Value' (i.e. darkness) needs to vary so that larger sets of three will be
223 * 'darker' (i.e. smaller value), so that they don't look that similar to previous ones.
224 * However, only a range of 0.3 to 1.0 is really usable to avoid clashing
225 * with some other stuff
226 */
227 fac = ((float)cur / (float)tot) * 0.7f;
228
229 /* the base color can get offset a bit so that the colors aren't so identical */
230 hsv[0] += fac * HSV_BANDWIDTH;
231 if (hsv[0] > 1.0f) {
232 hsv[0] = fmod(hsv[0], 1.0f);
233 }
234
235 /* saturation adjustments for more visible range */
236 hsv[1] = ((hsv[0] > 0.5f) && (hsv[0] < 0.8f)) ? 0.5f : 0.6f;
237
238 /* value is fixed at 1.0f, otherwise we cannot clearly see the curves... */
239 hsv[2] = 1.0f;
240
241 /* finally, convert this to RGB colors */
242 hsv_to_rgb_v(hsv, out);
243 }
244