svn merge -r 16060:16222 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender-staging.git] / source / blender / src / editipo_lib.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  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /* ********** General calls (minimal dependencies) for editing Ipos in Blender ************* */
29
30 #include <stdio.h>
31
32 #include "BLI_blenlib.h"
33 #include "BLI_arithb.h"
34
35 #include "DNA_curve_types.h"
36 #include "DNA_ipo_types.h"
37 #include "DNA_key_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_view3d_types.h"
42
43 #include "BKE_global.h"
44 #include "BKE_ipo.h"
45 #include "BKE_key.h"
46 #include "BKE_utildefines.h"
47
48 #include "BIF_resources.h"
49
50 #include "BSE_edit.h"
51 #include "BSE_editipo_types.h"
52 #include "BSE_editipo.h"
53 #include "BSE_drawipo.h"
54
55 #include "blendef.h"
56 #include "mydevice.h"
57
58 char *ob_ic_names[OB_TOTNAM] = { "LocX", "LocY", "LocZ", "dLocX", "dLocY", "dLocZ",
59         "RotX", "RotY", "RotZ", "dRotX", "dRotY", "dRotZ",
60         "ScaleX", "ScaleY", "ScaleZ", "dScaleX", "dScaleY", "dScaleZ",
61         "Layer", "Time", "ColR", "ColG", "ColB", "ColA",
62         "FStreng", "FFall", "RDamp", "Damping", "Perm", "FMaxD" };
63
64 char *co_ic_names[CO_TOTNAM] = { "Inf", "HeadTail" };
65 char *mtex_ic_names[TEX_TOTNAM] = { "OfsX", "OfsY", "OfsZ", "SizeX", "SizeY", "SizeZ",
66         "texR", "texG", "texB", "DefVar", "Col", "Nor", "Var",
67         "Disp" };
68 char *tex_ic_names[TE_TOTNAM] = { "NSize", "NDepth", "NType", "Turb", "Vnw1", "Vnw2",
69         "Vnw3", "Vnw4", "MinkMExp", "DistM", "ColT", "iScale",
70         "DistA", "MgType", "MgH", "Lacu", "Oct", "MgOff",
71         "MgGain", "NBase1", "NBase2", "ColR", "ColG", "ColB", "Bright", "Contras"};
72 char *ma_ic_names[MA_TOTNAM] = { "R", "G", "B", "SpecR", "SpecG", "SpecB", "MirR",
73         "MirG", "MirB", "Ref", "Alpha", "Emit", "Amb", "Spec",
74         "Hard", "SpTra", "Ior", "Mode", "HaSize", "Translu",
75         "RayMir", "FresMir", "FresMirI", "FresTra", "FresTraI",
76         "TraGlow" };
77 char *seq_ic_names[SEQ_TOTNAM] = { "Fac" };
78 char *cu_ic_names[CU_TOTNAM] = { "Speed" };
79 char *key_ic_names[KEY_TOTNAM] = { "Speed", "Key 1", "Key 2", "Key 3", "Key 4", "Key 5",
80         "Key 6", "Key 7", "Key 8", "Key 9", "Key 10",
81         "Key 11", "Key 12", "Key 13", "Key 14", "Key 15",
82         "Key 16", "Key 17", "Key 18", "Key 19", "Key 20",
83         "Key 21", "Key 22", "Key 23", "Key 24", "Key 25",
84         "Key 26", "Key 27", "Key 28", "Key 29", "Key 30",
85         "Key 31", "Key 32", "Key 33", "Key 34", "Key 35",
86         "Key 36", "Key 37", "Key 38", "Key 39", "Key 40",
87         "Key 41", "Key 42", "Key 43", "Key 44", "Key 45",
88         "Key 46", "Key 47", "Key 48", "Key 49", "Key 50",
89         "Key 51", "Key 52", "Key 53", "Key 54", "Key 55",
90         "Key 56", "Key 57", "Key 58", "Key 59", "Key 60",
91         "Key 61", "Key 62", "Key 63"};
92 char *wo_ic_names[WO_TOTNAM] = { "HorR", "HorG", "HorB", "ZenR", "ZenG", "ZenB", "Expos",
93         "Misi", "MisDi", "MisSta", "MisHi", "StarR", "StarB",
94         "StarG", "StarDi", "StarSi" };
95 char *la_ic_names[LA_TOTNAM] = { "Energ", "R", "G", "B", "Dist", "SpoSi", "SpoBl",
96         "Quad1", "Quad2", "HaInt" };
97 /* yafray: two curve names added, 'Apert' for aperture, and 'FDist' for focal distance */
98 char *cam_ic_names[CAM_TOTNAM] = { "Lens", "ClSta", "ClEnd", "Apert", "FDist", "ShiftX", "ShiftY" };
99 char *snd_ic_names[SND_TOTNAM] = { "Vol", "Pitch", "Pan", "Atten" };
100 char *ac_ic_names[AC_TOTNAM] = {"LocX", "LocY", "LocZ", "ScaleX", "ScaleY",
101         "ScaleZ", "QuatW", "QuatX", "QuatY", "QuatZ"};
102 char *ic_name_empty[1] ={ "" };
103 char *fluidsim_ic_names[FLUIDSIM_TOTNAM] = { "Fac-Visc", "Fac-Time",  "GravX","GravY","GravZ",  "VelX","VelY","VelZ", "Active", "AttrForceStr", "AttrForceRad", "VelForceStr", "VelForceRad" };
104 char *part_ic_names[PART_TOTNAM] = { "E_Freq", "E_Life", "E_Speed", "E_Angular", "E_Size",
105 "Angular", "Size", "Drag", "Brown", "Damp", "Length", "Clump",
106 "GravX", "GravY", "GravZ", "KinkAmp", "KinkFreq", "KinkShape", "BBTilt",
107 "FStreng", "FFall", "FMaxD", "F2Streng", "F2Fall", "F2MaxD"};
108
109 /* gets the appropriate icon for the given blocktype */
110 int geticon_ipo_blocktype(short blocktype)
111 {
112         switch (blocktype) {
113                 case ID_OB:
114                         return ICON_OBJECT;
115                 case ID_PO:
116                         return ICON_POSE_HLT;
117                 case ID_KE:
118                         return ICON_EDIT;
119                 case ID_MA:
120                         return ICON_MATERIAL;
121                 case ID_WO:
122                         return ICON_WORLD;
123                 case ID_CU:
124                         return ICON_CURVE;
125                 case ID_CA:
126                         return ICON_CAMERA;
127                 case ID_LA:
128                         return ICON_LAMP;
129                 case ID_TE:
130                         return ICON_TEXTURE;
131                 case ID_CO:
132                         return ICON_CONSTRAINT;
133                 case ID_FLUIDSIM:
134                         return ICON_WORLD; // uggh
135                 default:
136                         return 0; // what about blank icon?
137         }
138 }
139
140 /* get name of ipo-curve
141  *      - icu should be valid pointer
142  *      - ob is only needed for a shapekey-related hack
143  */
144 char *getname_ipocurve(IpoCurve *icu, Object *ob)
145 {
146         switch (icu->blocktype) {
147                 case ID_OB: 
148                         return getname_ob_ei(icu->adrcode, 0); /* dummy 2nd arg */ 
149                 case ID_PO:
150                         return getname_ac_ei(icu->adrcode);
151                 case ID_KE:
152                         {
153                                 static char name[32];
154                                 Key *key= ob_get_key(ob);
155                                 KeyBlock *kb= key_get_keyblock(key, icu->adrcode);
156                                 
157                                 /* only return name if it has been set, otherwise use 
158                                  * default method using static string (Key #)
159                                  */
160                                 if ((kb) && (kb->name[0] != '\0'))
161                                         return kb->name; /* return keyblock's name  */
162                                 
163                                 /* in case keyblock is not named or no key/keyblock was found */
164                                 sprintf(name, "Key %d", icu->adrcode);
165                                 return name;
166                         }
167                         
168                 default: /* fixme - add all of the other types! */
169                         return NULL;
170         }
171 }
172
173 char *getname_ac_ei(int nr) 
174 {
175         switch(nr) {
176                 case AC_LOC_X:
177                 case AC_LOC_Y:
178                 case AC_LOC_Z:
179                         return ac_ic_names[nr-1];
180                 case AC_SIZE_X:
181                 case AC_SIZE_Y:
182                 case AC_SIZE_Z:
183                         return ac_ic_names[nr-10];
184                 case AC_QUAT_X:
185                 case AC_QUAT_Y:
186                 case AC_QUAT_Z:
187                 case AC_QUAT_W:
188                         return ac_ic_names[nr-19];
189                 default:
190                         return ic_name_empty[0]; /* empty */
191         }
192 }
193
194 char *getname_co_ei(int nr)
195 {
196         switch(nr){
197                 case CO_ENFORCE:
198                 case CO_HEADTAIL:
199                         return co_ic_names[nr-1];
200         }
201         return ic_name_empty[0];
202 }
203
204 char *getname_ob_ei(int nr, int colipo)
205 {
206         if(nr>=OB_LOC_X && nr <= OB_PD_FMAXD) return ob_ic_names[nr-1];
207         
208         return ic_name_empty[0];
209 }
210
211 char *getname_tex_ei(int nr)
212 {
213         if(nr>=TE_NSIZE && nr<=TE_CONTRA) return tex_ic_names[nr-1];
214         
215         return ic_name_empty[0];
216 }
217
218 char *getname_mtex_ei(int nr)
219 {
220         if(nr>=MAP_OFS_X && nr<=MAP_DISP) return mtex_ic_names[nr-1];
221         
222         return ic_name_empty[0];
223 }
224
225 char *getname_mat_ei(int nr)
226 {
227         if(nr>=MA_MAP1) return getname_mtex_ei((nr & (MA_MAP1-1)));
228         else {
229                 if(nr>=MA_COL_R && nr<=MA_ADD) return ma_ic_names[nr-1];
230         }
231         return ic_name_empty[0];
232 }
233
234 char *getname_world_ei(int nr)
235 {
236         if(nr>=MA_MAP1) return getname_mtex_ei((nr & (MA_MAP1-1)));
237         else {
238                 if(nr>=WO_HOR_R && nr<=WO_STARSIZE) return wo_ic_names[nr-1];
239         }
240         return ic_name_empty[0];
241 }
242
243 char *getname_seq_ei(int nr)
244 {
245         if(nr == SEQ_FAC1) return seq_ic_names[nr-1];
246         return ic_name_empty[0];
247 }
248
249 char *getname_cu_ei(int nr)
250 {
251         if(nr==CU_SPEED) return cu_ic_names[nr-1];
252         return ic_name_empty[0];
253 }
254
255 char *getname_la_ei(int nr)
256 {
257         if(nr>=MA_MAP1) return getname_mtex_ei((nr & (MA_MAP1-1)));
258         else {
259                 if(nr>=LA_ENERGY && nr<=LA_HALOINT) return la_ic_names[nr-1];
260         }
261         return ic_name_empty[0];
262 }
263
264 char *getname_cam_ei(int nr)
265 {
266         if(nr>=CAM_LENS && nr<=CAM_SHIFT_Y) return cam_ic_names[nr-1];
267         return ic_name_empty[0];
268 }
269
270 char *getname_snd_ei(int nr)
271 {
272         if(nr>=SND_VOLUME && nr<=SND_ATTEN) return snd_ic_names[nr-1];
273         return ic_name_empty[0];
274 }
275
276 char *getname_fluidsim_ei(int nr)
277 {
278         if(nr <= FLUIDSIM_TOTIPO) return fluidsim_ic_names[nr-1];
279         return ic_name_empty[0];
280 }
281 char *getname_part_ei(int nr)
282 {
283         if(nr <= PART_TOTIPO) return part_ic_names[nr-1];
284         return ic_name_empty[0];
285 }
286
287
288 void boundbox_ipocurve(IpoCurve *icu, int selectedonly)
289 {
290         BezTriple *bezt;
291         float vec[3]={0.0,0.0,0.0};
292         float min[3], max[3];
293         int a;
294         
295         if(icu->totvert) {
296                 INIT_MINMAX(min, max);
297                 
298                 if(icu->bezt ) {
299                         a= icu->totvert;
300                         bezt= icu->bezt;
301                         while(a--) {
302                                 if(icu->vartype & IPO_BITS) {
303                                         if((bezt->f2 & SELECT) || !selectedonly) {
304                                                 vec[0]= bezt->vec[1][0];
305                                                 vec[1]= 0.0;
306                                                 DO_MINMAX(vec, min, max);
307                                                 
308                                                 vec[1]= 16.0;
309                                                 DO_MINMAX(vec, min, max);
310                                         }
311                                 }
312                                 else {
313                                         if((bezt->f1 & SELECT) || !selectedonly) {
314                                                 if(icu->ipo==IPO_BEZ && a!=icu->totvert-1)
315                                                         DO_MINMAX(bezt->vec[0], min, max);
316                                         }
317                                         if((bezt->f2 & SELECT) || !selectedonly)
318                                                 DO_MINMAX(bezt->vec[1], min, max);
319                                         if((bezt->f3 & SELECT) || !selectedonly) {
320                                                 if(icu->ipo==IPO_BEZ && a!=0)
321                                                         DO_MINMAX(bezt->vec[2], min, max);
322                                         }
323                                 }
324                                 
325                                 bezt++;
326                         }
327                 }
328                 if(min[0]==max[0]) max[0]= (float)(min[0]+1.0);
329                 if(min[1]==max[1]) max[1]= (float)(min[1]+0.1);
330                 
331                 icu->totrct.xmin= min[0];
332                 icu->totrct.ymin= min[1];
333                 icu->totrct.xmax= max[0];
334                 icu->totrct.ymax= max[1];
335         }
336         else {
337                 icu->totrct.xmin= icu->totrct.ymin= 0.0;
338                 icu->totrct.xmax= EFRA;
339                 icu->totrct.ymax= 1.0;
340         }
341 }
342
343 void boundbox_ipo(Ipo *ipo, rctf *bb, int selectedonly)
344 {
345         IpoCurve *icu;
346         int first= 1;
347         
348         icu= ipo->curve.first;
349         while(icu) {
350                 
351                 boundbox_ipocurve(icu, selectedonly);
352                 
353                 if(first) {
354                         *bb= icu->totrct;
355                         first= 0;
356                 }
357                 else BLI_union_rctf(bb, &(icu->totrct));
358                 
359                 icu= icu->next;
360         }
361 }
362
363
364 unsigned int ipo_rainbow(int cur, int tot)
365 {
366         float dfac, fac, sat;
367         
368         dfac= (float)(1.0/( (float)tot+1.0));
369         
370         /* this calculation makes 2 or 4 different cycles of rainbow colors */
371         if(cur< tot/2) fac= (float)(cur*2.0f*dfac);
372         else fac= (float)((cur-tot/2)*2.0f*dfac +dfac);
373         if(tot > 32) fac= fac*1.95f;
374         if(fac>1.0f) fac-= 1.0f;
375         
376         if(fac>0.5f && fac<0.8f) sat= 0.4f;
377         else sat= 0.5f;
378         
379         return hsv_to_cpack(fac, sat, 1.0f);
380 }               
381
382 /* exported to python, hrms... (ton) */
383 int texchannel_to_adrcode(int channel)
384 {
385         switch(channel) {
386                 case 0: return MA_MAP1;
387                 case 1: return MA_MAP2; 
388                 case 2: return MA_MAP3; 
389                 case 3: return MA_MAP4; 
390                 case 4: return MA_MAP5; 
391                 case 5: return MA_MAP6; 
392                 case 6: return MA_MAP7; 
393                 case 7: return MA_MAP8; 
394                 case 8: return MA_MAP9; 
395                 case 9: return MA_MAP10; 
396                 default: return 0;
397         }
398 }
399
400
401
402