Bugfix for a) Fluid ipos gone after save-load b) changing viscosity causes fluidsim...
[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
108 /* gets the appropriate icon for the given blocktype */
109 int geticon_ipo_blocktype(short blocktype)
110 {
111         switch (blocktype) {
112                 case ID_OB:
113                         return ICON_OBJECT;
114                 case ID_PO:
115                         return ICON_POSE_HLT;
116                 case ID_KE:
117                         return ICON_EDIT;
118                 case ID_MA:
119                         return ICON_MATERIAL;
120                 case ID_WO:
121                         return ICON_WORLD;
122                 case ID_CU:
123                         return ICON_CURVE;
124                 case ID_CA:
125                         return ICON_CAMERA;
126                 case ID_LA:
127                         return ICON_LAMP;
128                 case ID_TE:
129                         return ICON_TEXTURE;
130                 case ID_CO:
131                         return ICON_CONSTRAINT;
132                 case ID_FLUIDSIM:
133                         return ICON_WORLD; // uggh
134                 default:
135                         return 0; // what about blank icon?
136         }
137 }
138
139 /* get name of ipo-curve
140  *      - icu should be valid pointer
141  *      - ob is only needed for a shapekey-related hack
142  */
143 char *getname_ipocurve(IpoCurve *icu, Object *ob)
144 {
145         switch (icu->blocktype) {
146                 case ID_OB: 
147                         return getname_ob_ei(icu->adrcode, 0); /* dummy 2nd arg */ 
148                 case ID_PO:
149                         return getname_ac_ei(icu->adrcode);
150                 case ID_KE:
151                         {
152                                 static char name[32];
153                                 Key *key= ob_get_key(ob);
154                                 KeyBlock *kb= key_get_keyblock(key, icu->adrcode);
155                                 
156                                 /* only return name if it has been set, otherwise use 
157                                  * default method using static string (Key #)
158                                  */
159                                 if ((kb) && (kb->name[0] != '\0'))
160                                         return kb->name; /* return keyblock's name  */
161                                 
162                                 /* in case keyblock is not named or no key/keyblock was found */
163                                 sprintf(name, "Key %d", icu->adrcode);
164                                 return name;
165                         }
166                         
167                 default: /* fixme - add all of the other types! */
168                         return NULL;
169         }
170 }
171
172 char *getname_ac_ei(int nr) 
173 {
174         switch(nr) {
175                 case AC_LOC_X:
176                 case AC_LOC_Y:
177                 case AC_LOC_Z:
178                         return ac_ic_names[nr-1];
179                 case AC_SIZE_X:
180                 case AC_SIZE_Y:
181                 case AC_SIZE_Z:
182                         return ac_ic_names[nr-10];
183                 case AC_QUAT_X:
184                 case AC_QUAT_Y:
185                 case AC_QUAT_Z:
186                 case AC_QUAT_W:
187                         return ac_ic_names[nr-19];
188                 default:
189                         return ic_name_empty[0]; /* empty */
190         }
191 }
192
193 char *getname_co_ei(int nr)
194 {
195         switch(nr){
196                 case CO_ENFORCE:
197                 case CO_HEADTAIL:
198                         return co_ic_names[nr-1];
199         }
200         return ic_name_empty[0];
201 }
202
203 char *getname_ob_ei(int nr, int colipo)
204 {
205         if(nr>=OB_LOC_X && nr <= OB_PD_FMAXD) return ob_ic_names[nr-1];
206         
207         return ic_name_empty[0];
208 }
209
210 char *getname_tex_ei(int nr)
211 {
212         if(nr>=TE_NSIZE && nr<=TE_CONTRA) return tex_ic_names[nr-1];
213         
214         return ic_name_empty[0];
215 }
216
217 char *getname_mtex_ei(int nr)
218 {
219         if(nr>=MAP_OFS_X && nr<=MAP_DISP) return mtex_ic_names[nr-1];
220         
221         return ic_name_empty[0];
222 }
223
224 char *getname_mat_ei(int nr)
225 {
226         if(nr>=MA_MAP1) return getname_mtex_ei((nr & (MA_MAP1-1)));
227         else {
228                 if(nr>=MA_COL_R && nr<=MA_ADD) return ma_ic_names[nr-1];
229         }
230         return ic_name_empty[0];
231 }
232
233 char *getname_world_ei(int nr)
234 {
235         if(nr>=MA_MAP1) return getname_mtex_ei((nr & (MA_MAP1-1)));
236         else {
237                 if(nr>=WO_HOR_R && nr<=WO_STARSIZE) return wo_ic_names[nr-1];
238         }
239         return ic_name_empty[0];
240 }
241
242 char *getname_seq_ei(int nr)
243 {
244         if(nr == SEQ_FAC1) return seq_ic_names[nr-1];
245         return ic_name_empty[0];
246 }
247
248 char *getname_cu_ei(int nr)
249 {
250         if(nr==CU_SPEED) return cu_ic_names[nr-1];
251         return ic_name_empty[0];
252 }
253
254 char *getname_la_ei(int nr)
255 {
256         if(nr>=MA_MAP1) return getname_mtex_ei((nr & (MA_MAP1-1)));
257         else {
258                 if(nr>=LA_ENERGY && nr<=LA_HALOINT) return la_ic_names[nr-1];
259         }
260         return ic_name_empty[0];
261 }
262
263 char *getname_cam_ei(int nr)
264 {
265         if(nr>=CAM_LENS && nr<=CAM_SHIFT_Y) return cam_ic_names[nr-1];
266         return ic_name_empty[0];
267 }
268
269 char *getname_snd_ei(int nr)
270 {
271         if(nr>=SND_VOLUME && nr<=SND_ATTEN) return snd_ic_names[nr-1];
272         return ic_name_empty[0];
273 }
274
275 char *getname_fluidsim_ei(int nr)
276 {
277         if(nr <= FLUIDSIM_TOTIPO) return fluidsim_ic_names[nr-1];
278         return ic_name_empty[0];
279 }
280 char *getname_part_ei(int nr)
281 {
282         if(nr <= PART_TOTIPO) return part_ic_names[nr-1];
283         return ic_name_empty[0];
284 }
285
286
287 void boundbox_ipocurve(IpoCurve *icu, int selectedonly)
288 {
289         BezTriple *bezt;
290         float vec[3]={0.0,0.0,0.0};
291         float min[3], max[3];
292         int a;
293         
294         if(icu->totvert) {
295                 INIT_MINMAX(min, max);
296                 
297                 if(icu->bezt ) {
298                         a= icu->totvert;
299                         bezt= icu->bezt;
300                         while(a--) {
301                                 if(icu->vartype & IPO_BITS) {
302                                         if((bezt->f2 & SELECT) || !selectedonly) {
303                                                 vec[0]= bezt->vec[1][0];
304                                                 vec[1]= 0.0;
305                                                 DO_MINMAX(vec, min, max);
306                                                 
307                                                 vec[1]= 16.0;
308                                                 DO_MINMAX(vec, min, max);
309                                         }
310                                 }
311                                 else {
312                                         if((bezt->f1 & SELECT) || !selectedonly) {
313                                                 if(icu->ipo==IPO_BEZ && a!=icu->totvert-1)
314                                                         DO_MINMAX(bezt->vec[0], min, max);
315                                         }
316                                         if((bezt->f2 & SELECT) || !selectedonly)
317                                                 DO_MINMAX(bezt->vec[1], min, max);
318                                         if((bezt->f3 & SELECT) || !selectedonly) {
319                                                 if(icu->ipo==IPO_BEZ && a!=0)
320                                                         DO_MINMAX(bezt->vec[2], min, max);
321                                         }
322                                 }
323                                 
324                                 bezt++;
325                         }
326                 }
327                 if(min[0]==max[0]) max[0]= (float)(min[0]+1.0);
328                 if(min[1]==max[1]) max[1]= (float)(min[1]+0.1);
329                 
330                 icu->totrct.xmin= min[0];
331                 icu->totrct.ymin= min[1];
332                 icu->totrct.xmax= max[0];
333                 icu->totrct.ymax= max[1];
334         }
335         else {
336                 icu->totrct.xmin= icu->totrct.ymin= 0.0;
337                 icu->totrct.xmax= EFRA;
338                 icu->totrct.ymax= 1.0;
339         }
340 }
341
342 void boundbox_ipo(Ipo *ipo, rctf *bb, int selectedonly)
343 {
344         IpoCurve *icu;
345         int first= 1;
346         
347         icu= ipo->curve.first;
348         while(icu) {
349                 
350                 boundbox_ipocurve(icu, selectedonly);
351                 
352                 if(first) {
353                         *bb= icu->totrct;
354                         first= 0;
355                 }
356                 else BLI_union_rctf(bb, &(icu->totrct));
357                 
358                 icu= icu->next;
359         }
360 }
361
362
363 unsigned int ipo_rainbow(int cur, int tot)
364 {
365         float dfac, fac, sat;
366         
367         dfac= (float)(1.0/( (float)tot+1.0));
368         
369         /* this calculation makes 2 or 4 different cycles of rainbow colors */
370         if(cur< tot/2) fac= (float)(cur*2.0f*dfac);
371         else fac= (float)((cur-tot/2)*2.0f*dfac +dfac);
372         if(tot > 32) fac= fac*1.95f;
373         if(fac>1.0f) fac-= 1.0f;
374         
375         if(fac>0.5f && fac<0.8f) sat= 0.4f;
376         else sat= 0.5f;
377         
378         return hsv_to_cpack(fac, sat, 1.0f);
379 }               
380
381 /* exported to python, hrms... (ton) */
382 int texchannel_to_adrcode(int channel)
383 {
384         switch(channel) {
385                 case 0: return MA_MAP1;
386                 case 1: return MA_MAP2; 
387                 case 2: return MA_MAP3; 
388                 case 3: return MA_MAP4; 
389                 case 4: return MA_MAP5; 
390                 case 5: return MA_MAP6; 
391                 case 6: return MA_MAP7; 
392                 case 7: return MA_MAP8; 
393                 case 8: return MA_MAP9; 
394                 case 9: return MA_MAP10; 
395                 default: return 0;
396         }
397 }
398
399
400
401