code cleanup:
[blender.git] / source / blender / editors / transform / transform_generics.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/transform/transform_generics.c
29  *  \ingroup edtransform
30  */
31
32 #include <string.h>
33 #include <math.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLO_sys_types.h" /* for intptr_t support */
38
39 #include "DNA_anim_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_lattice_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_sequence_types.h"
44 #include "DNA_space_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_view3d_types.h"
50 #include "DNA_modifier_types.h"
51 #include "DNA_movieclip_types.h"
52 #include "DNA_mask_types.h"
53
54 #include "BLI_math.h"
55 #include "BLI_blenlib.h"
56 #include "BLI_rand.h"
57 #include "BLI_utildefines.h"
58
59 #include "RNA_access.h"
60
61 #include "BIF_gl.h"
62 #include "BIF_glutil.h"
63
64 #include "BKE_animsys.h"
65 #include "BKE_action.h"
66 #include "BKE_armature.h"
67 #include "BKE_curve.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_displist.h"
70 #include "BKE_fcurve.h"
71 #include "BKE_lattice.h"
72 #include "BKE_mesh.h"
73 #include "BKE_nla.h"
74 #include "BKE_context.h"
75 #include "BKE_sequencer.h"
76 #include "BKE_tessmesh.h"
77 #include "BKE_tracking.h"
78 #include "BKE_mask.h"
79
80 #include "ED_anim_api.h"
81 #include "ED_armature.h"
82 #include "ED_image.h"
83 #include "ED_keyframing.h"
84 #include "ED_markers.h"
85 #include "ED_mesh.h"
86 #include "ED_particle.h"
87 #include "ED_screen_types.h"
88 #include "ED_space_api.h"
89 #include "ED_uvedit.h"
90 #include "ED_view3d.h"
91 #include "ED_curve.h" /* for curve_editnurbs */
92 #include "ED_clip.h"
93 #include "ED_screen.h"
94
95 #include "WM_types.h"
96 #include "WM_api.h"
97
98 #include "UI_resources.h"
99
100 #include "transform.h"
101
102 /* ************************** Functions *************************** */
103
104 void getViewVector(TransInfo *t, float coord[3], float vec[3])
105 {
106         if (t->persp != RV3D_ORTHO) {
107                 float p1[4], p2[4];
108                 
109                 copy_v3_v3(p1, coord);
110                 p1[3] = 1.0f;
111                 copy_v3_v3(p2, p1);
112                 p2[3] = 1.0f;
113                 mul_m4_v4(t->viewmat, p2);
114                 
115                 p2[0] = 2.0f * p2[0];
116                 p2[1] = 2.0f * p2[1];
117                 p2[2] = 2.0f * p2[2];
118                 
119                 mul_m4_v4(t->viewinv, p2);
120                 
121                 sub_v3_v3v3(vec, p1, p2);
122         }
123         else {
124                 copy_v3_v3(vec, t->viewinv[2]);
125         }
126         normalize_v3(vec);
127 }
128
129 /* ************************** GENERICS **************************** */
130
131 static void clipMirrorModifier(TransInfo *t, Object *ob)
132 {
133         ModifierData *md = ob->modifiers.first;
134         float tolerance[3] = {0.0f, 0.0f, 0.0f};
135         int axis = 0;
136         
137         for (; md; md = md->next) {
138                 if ((md->type == eModifierType_Mirror) && (md->mode & eModifierMode_Realtime)) {
139                         MirrorModifierData *mmd = (MirrorModifierData *) md;
140                         
141                         if (mmd->flag & MOD_MIR_CLIPPING) {
142                                 axis = 0;
143                                 if (mmd->flag & MOD_MIR_AXIS_X) {
144                                         axis |= 1;
145                                         tolerance[0] = mmd->tolerance;
146                                 }
147                                 if (mmd->flag & MOD_MIR_AXIS_Y) {
148                                         axis |= 2;
149                                         tolerance[1] = mmd->tolerance;
150                                 }
151                                 if (mmd->flag & MOD_MIR_AXIS_Z) {
152                                         axis |= 4;
153                                         tolerance[2] = mmd->tolerance;
154                                 }
155                                 if (axis) {
156                                         float mtx[4][4], imtx[4][4];
157                                         int i;
158                                         TransData *td = t->data;
159                                         
160                                         if (mmd->mirror_ob) {
161                                                 float obinv[4][4];
162                                                 
163                                                 invert_m4_m4(obinv, mmd->mirror_ob->obmat);
164                                                 mult_m4_m4m4(mtx, obinv, ob->obmat);
165                                                 invert_m4_m4(imtx, mtx);
166                                         }
167                                         
168                                         for (i = 0; i < t->total; i++, td++) {
169                                                 int clip;
170                                                 float loc[3], iloc[3];
171                                                 
172                                                 if (td->flag & TD_NOACTION)
173                                                         break;
174                                                 if (td->loc == NULL)
175                                                         break;
176                                                 
177                                                 if (td->flag & TD_SKIP)
178                                                         continue;
179                                                 
180                                                 copy_v3_v3(loc,  td->loc);
181                                                 copy_v3_v3(iloc, td->iloc);
182                                                 
183                                                 if (mmd->mirror_ob) {
184                                                         mul_m4_v3(mtx, loc);
185                                                         mul_m4_v3(mtx, iloc);
186                                                 }
187                                                 
188                                                 clip = 0;
189                                                 if (axis & 1) {
190                                                         if (fabsf(iloc[0]) <= tolerance[0] ||
191                                                             loc[0] * iloc[0] < 0.0f)
192                                                         {
193                                                                 loc[0] = 0.0f;
194                                                                 clip = 1;
195                                                         }
196                                                 }
197                                                 
198                                                 if (axis & 2) {
199                                                         if (fabsf(iloc[1]) <= tolerance[1] ||
200                                                             loc[1] * iloc[1] < 0.0f)
201                                                         {
202                                                                 loc[1] = 0.0f;
203                                                                 clip = 1;
204                                                         }
205                                                 }
206                                                 if (axis & 4) {
207                                                         if (fabsf(iloc[2]) <= tolerance[2] ||
208                                                             loc[2] * iloc[2] < 0.0f)
209                                                         {
210                                                                 loc[2] = 0.0f;
211                                                                 clip = 1;
212                                                         }
213                                                 }
214                                                 if (clip) {
215                                                         if (mmd->mirror_ob) {
216                                                                 mul_m4_v3(imtx, loc);
217                                                         }
218                                                         copy_v3_v3(td->loc, loc);
219                                                 }
220                                         }
221                                 }
222                                 
223                         }
224                 }
225         }
226 }
227
228 /* assumes obedit set to mesh object */
229 static void editbmesh_apply_to_mirror(TransInfo *t)
230 {
231         TransData *td = t->data;
232         BMVert *eve;
233         int i;
234         
235         for (i = 0; i < t->total; i++, td++) {
236                 if (td->flag & TD_NOACTION)
237                         break;
238                 if (td->loc == NULL)
239                         break;
240                 if (td->flag & TD_SKIP)
241                         continue;
242                 
243                 eve = td->extra;
244                 if (eve) {
245                         eve->co[0] = -td->loc[0];
246                         eve->co[1] = td->loc[1];
247                         eve->co[2] = td->loc[2];
248                 }
249                 
250                 if (td->flag & TD_MIRROR_EDGE) {
251                         td->loc[0] = 0;
252                 }
253         }
254 }
255
256 /* for the realtime animation recording feature, handle overlapping data */
257 static void animrecord_check_state(Scene *scene, ID *id, wmTimer *animtimer)
258 {
259         ScreenAnimData *sad = (animtimer) ? animtimer->customdata : NULL;
260         
261         /* sanity checks */
262         if (ELEM3(NULL, scene, id, sad))
263                 return;
264         
265         /* check if we need a new strip if:
266          *  - if animtimer is running
267          *      - we're not only keying for available channels
268          *      - the option to add new actions for each round is not enabled
269          */
270         if (IS_AUTOKEY_FLAG(scene, INSERTAVAIL) == 0 && (scene->toolsettings->autokey_flag & ANIMRECORD_FLAG_WITHNLA)) {
271                 /* if playback has just looped around, we need to add a new NLA track+strip to allow a clean pass to occur */
272                 if ((sad) && (sad->flag & ANIMPLAY_FLAG_JUMPED)) {
273                         AnimData *adt = BKE_animdata_from_id(id);
274                         
275                         /* perform push-down manually with some differences 
276                          * NOTE: BKE_nla_action_pushdown() sync warning...
277                          */
278                         if ((adt->action) && !(adt->flag & ADT_NLA_EDIT_ON)) {
279                                 float astart, aend;
280                                 
281                                 /* only push down if action is more than 1-2 frames long */
282                                 calc_action_range(adt->action, &astart, &aend, 1);
283                                 if (aend > astart + 2.0f) {
284                                         NlaStrip *strip = add_nlastrip_to_stack(adt, adt->action);
285                                         
286                                         /* clear reference to action now that we've pushed it onto the stack */
287                                         adt->action->id.us--;
288                                         adt->action = NULL;
289                                         
290                                         /* adjust blending + extend so that they will behave correctly */
291                                         strip->extendmode = NLASTRIP_EXTEND_NOTHING;
292                                         strip->flag &= ~(NLASTRIP_FLAG_AUTO_BLENDS | NLASTRIP_FLAG_SELECT | NLASTRIP_FLAG_ACTIVE);
293                                         
294                                         /* also, adjust the AnimData's action extend mode to be on 
295                                          * 'nothing' so that previous result still play 
296                                          */
297                                         adt->act_extendmode = NLASTRIP_EXTEND_NOTHING;
298                                 }
299                         }
300                 }
301         }
302 }
303
304 static int fcu_test_selected(FCurve *fcu)
305 {
306         BezTriple *bezt = fcu->bezt;
307         unsigned int i;
308
309         if (bezt == NULL) /* ignore baked */
310                 return 0;
311
312         for (i = 0; i < fcu->totvert; i++, bezt++) {
313                 if (BEZSELECTED(bezt)) return 1;
314         }
315
316         return 0;
317 }
318
319 /* helper for recalcData() - for Action Editor transforms */
320 static void recalcData_actedit(TransInfo *t)
321 {
322         Scene *scene = t->scene;
323         SpaceAction *saction = (SpaceAction *)t->sa->spacedata.first;
324         
325         bAnimContext ac = {NULL};
326         ListBase anim_data = {NULL, NULL};
327         bAnimListElem *ale;
328         int filter;
329         
330         /* initialize relevant anim-context 'context' data from TransInfo data */
331         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
332         ac.scene = t->scene;
333         ac.obact = OBACT;
334         ac.sa = t->sa;
335         ac.ar = t->ar;
336         ac.sl = (t->sa) ? t->sa->spacedata.first : NULL;
337         ac.spacetype = (t->sa) ? t->sa->spacetype : 0;
338         ac.regiontype = (t->ar) ? t->ar->regiontype : 0;
339         
340         ANIM_animdata_context_getdata(&ac);
341         
342         /* perform flush */
343         if (ELEM(ac.datatype, ANIMCONT_GPENCIL, ANIMCONT_MASK)) {
344                 /* flush transform values back to actual coordinates */
345                 flushTransIntFrameActionData(t);
346         }
347         else {
348                 /* get animdata blocks visible in editor, assuming that these will be the ones where things changed */
349                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_ANIMDATA);
350                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
351                 
352                 /* just tag these animdata-blocks to recalc, assuming that some data there changed 
353                  * BUT only do this if realtime updates are enabled
354                  */
355                 if ((saction->flag & SACTION_NOREALTIMEUPDATES) == 0) {
356                         for (ale = anim_data.first; ale; ale = ale->next) {
357                                 /* set refresh tags for objects using this animation */
358                                 ANIM_list_elem_update(t->scene, ale);
359                         }
360                 }
361                 
362                 /* now free temp channels */
363                 BLI_freelistN(&anim_data);
364         }
365 }
366 /* helper for recalcData() - for Graph Editor transforms */
367 static void recalcData_graphedit(TransInfo *t)
368 {
369         SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
370         Scene *scene;
371         
372         ListBase anim_data = {NULL, NULL};
373         bAnimContext ac = {NULL};
374         int filter;
375         
376         bAnimListElem *ale;
377         int dosort = 0;
378         
379         
380         /* initialize relevant anim-context 'context' data from TransInfo data */
381         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
382         scene = ac.scene = t->scene;
383         ac.obact = OBACT;
384         ac.sa = t->sa;
385         ac.ar = t->ar;
386         ac.sl = (t->sa) ? t->sa->spacedata.first : NULL;
387         ac.spacetype = (t->sa) ? t->sa->spacetype : 0;
388         ac.regiontype = (t->ar) ? t->ar->regiontype : 0;
389         
390         ANIM_animdata_context_getdata(&ac);
391         
392         /* do the flush first */
393         flushTransGraphData(t);
394         
395         /* get curves to check if a re-sort is needed */
396         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVE_VISIBLE);
397         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
398         
399         /* now test if there is a need to re-sort */
400         for (ale = anim_data.first; ale; ale = ale->next) {
401                 FCurve *fcu = (FCurve *)ale->key_data;
402                 
403                 /* ignore unselected fcurves */
404                 if (!fcu_test_selected(fcu))
405                         continue;
406                 
407                 // fixme: only do this for selected verts...
408                 ANIM_unit_mapping_apply_fcurve(ac.scene, ale->id, ale->key_data, ANIM_UNITCONV_ONLYSEL | ANIM_UNITCONV_SELVERTS | ANIM_UNITCONV_RESTORE);
409                 
410                 
411                 /* watch it: if the time is wrong: do not correct handles yet */
412                 if (test_time_fcurve(fcu))
413                         dosort++;
414                 else
415                         calchandles_fcurve(fcu);
416                 
417                 /* set refresh tags for objects using this animation,
418                  * BUT only if realtime updates are enabled  
419                  */
420                 if ((sipo->flag & SIPO_NOREALTIMEUPDATES) == 0)
421                         ANIM_list_elem_update(t->scene, ale);
422         }
423         
424         /* do resort and other updates? */
425         if (dosort) remake_graph_transdata(t, &anim_data);
426         
427         /* now free temp channels */
428         BLI_freelistN(&anim_data);
429 }
430
431 /* helper for recalcData() - for NLA Editor transforms */
432 static void recalcData_nla(TransInfo *t)
433 {
434         TransDataNla *tdn = (TransDataNla *)t->customData;
435         SpaceNla *snla = (SpaceNla *)t->sa->spacedata.first;
436         Scene *scene = t->scene;
437         double secf = FPS;
438         int i;
439         
440         /* for each strip we've got, perform some additional validation of the values that got set before
441          * using RNA to set the value (which does some special operations when setting these values to make
442          * sure that everything works ok)
443          */
444         for (i = 0; i < t->total; i++, tdn++) {
445                 NlaStrip *strip = tdn->strip;
446                 PointerRNA strip_ptr;
447                 short pExceeded, nExceeded, iter;
448                 int delta_y1, delta_y2;
449                 
450                 /* if this tdn has no handles, that means it is just a dummy that should be skipped */
451                 if (tdn->handle == 0)
452                         continue;
453                 
454                 /* set refresh tags for objects using this animation,
455                  * BUT only if realtime updates are enabled  
456                  */
457                 if ((snla->flag & SNLA_NOREALTIMEUPDATES) == 0)
458                         ANIM_id_update(t->scene, tdn->id);
459                 
460                 /* if canceling transform, just write the values without validating, then move on */
461                 if (t->state == TRANS_CANCEL) {
462                         /* clear the values by directly overwriting the originals, but also need to restore
463                          * endpoints of neighboring transition-strips
464                          */
465                         
466                         /* start */
467                         strip->start = tdn->h1[0];
468                         
469                         if ((strip->prev) && (strip->prev->type == NLASTRIP_TYPE_TRANSITION))
470                                 strip->prev->end = tdn->h1[0];
471                         
472                         /* end */
473                         strip->end = tdn->h2[0];
474                         
475                         if ((strip->next) && (strip->next->type == NLASTRIP_TYPE_TRANSITION))
476                                 strip->next->start = tdn->h2[0];
477                         
478                         /* flush transforms to child strips (since this should be a meta) */
479                         BKE_nlameta_flush_transforms(strip);
480                         
481                         /* restore to original track (if needed) */
482                         if (tdn->oldTrack != tdn->nlt) {
483                                 /* just append to end of list for now, since strips get sorted in special_aftertrans_update() */
484                                 BLI_remlink(&tdn->nlt->strips, strip);
485                                 BLI_addtail(&tdn->oldTrack->strips, strip);
486                         }
487                         
488                         continue;
489                 }
490                 
491                 /* firstly, check if the proposed transform locations would overlap with any neighboring strips
492                  * (barring transitions) which are absolute barriers since they are not being moved
493                  *
494                  * this is done as a iterative procedure (done 5 times max for now)
495                  */
496                 for (iter = 0; iter < 5; iter++) {
497                         pExceeded = ((strip->prev) && (strip->prev->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h1[0] < strip->prev->end));
498                         nExceeded = ((strip->next) && (strip->next->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h2[0] > strip->next->start));
499                         
500                         if ((pExceeded && nExceeded) || (iter == 4) ) {
501                                 /* both endpoints exceeded (or iteration ping-pong'd meaning that we need a compromise)
502                                  *      - simply crop strip to fit within the bounds of the strips bounding it
503                                  *      - if there were no neighbors, clear the transforms (make it default to the strip's current values)
504                                  */
505                                 if (strip->prev && strip->next) {
506                                         tdn->h1[0] = strip->prev->end;
507                                         tdn->h2[0] = strip->next->start;
508                                 }
509                                 else {
510                                         tdn->h1[0] = strip->start;
511                                         tdn->h2[0] = strip->end;
512                                 }
513                         }
514                         else if (nExceeded) {
515                                 /* move backwards */
516                                 float offset = tdn->h2[0] - strip->next->start;
517                                 
518                                 tdn->h1[0] -= offset;
519                                 tdn->h2[0] -= offset;
520                         }
521                         else if (pExceeded) {
522                                 /* more forwards */
523                                 float offset = strip->prev->end - tdn->h1[0];
524                                 
525                                 tdn->h1[0] += offset;
526                                 tdn->h2[0] += offset;
527                         }
528                         else /* all is fine and well */
529                                 break;
530                 }
531                 
532                 /* handle auto-snapping */
533                 switch (snla->autosnap) {
534                         case SACTSNAP_FRAME: /* snap to nearest frame/time  */
535                                 if (snla->flag & SNLA_DRAWTIME) {
536                                         tdn->h1[0] = (float)(floor(((double)tdn->h1[0] / secf) + 0.5) * secf);
537                                         tdn->h2[0] = (float)(floor(((double)tdn->h2[0] / secf) + 0.5) * secf);
538                                 }
539                                 else {
540                                         tdn->h1[0] = floorf(tdn->h1[0] + 0.5f);
541                                         tdn->h2[0] = floorf(tdn->h2[0] + 0.5f);
542                                 }
543                                 break;
544                         
545                         case SACTSNAP_MARKER: /* snap to nearest marker */
546                                 tdn->h1[0] = (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h1[0]);
547                                 tdn->h2[0] = (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h2[0]);
548                                 break;
549                 }
550                 
551                 /* use RNA to write the values... */
552                 // TODO: do we need to write in 2 passes to make sure that no truncation goes on?
553                 RNA_pointer_create(NULL, &RNA_NlaStrip, strip, &strip_ptr);
554                 
555                 RNA_float_set(&strip_ptr, "frame_start", tdn->h1[0]);
556                 RNA_float_set(&strip_ptr, "frame_end", tdn->h2[0]);
557                 
558                 /* flush transforms to child strips (since this should be a meta) */
559                 BKE_nlameta_flush_transforms(strip);
560                 
561                 
562                 /* now, check if we need to try and move track
563                  *      - we need to calculate both, as only one may have been altered by transform if only 1 handle moved
564                  */
565                 delta_y1 = ((int)tdn->h1[1] / NLACHANNEL_STEP(snla) - tdn->trackIndex);
566                 delta_y2 = ((int)tdn->h2[1] / NLACHANNEL_STEP(snla) - tdn->trackIndex);
567                 
568                 if (delta_y1 || delta_y2) {
569                         NlaTrack *track;
570                         int delta = (delta_y2) ? delta_y2 : delta_y1;
571                         int n;
572                         
573                         /* move in the requested direction, checking at each layer if there's space for strip to pass through,
574                          * stopping on the last track available or that we're able to fit in
575                          */
576                         if (delta > 0) {
577                                 for (track = tdn->nlt->next, n = 0; (track) && (n < delta); track = track->next, n++) {
578                                         /* check if space in this track for the strip */
579                                         if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
580                                                 /* move strip to this track */
581                                                 BLI_remlink(&tdn->nlt->strips, strip);
582                                                 BKE_nlatrack_add_strip(track, strip);
583                                                 
584                                                 tdn->nlt = track;
585                                                 tdn->trackIndex++;
586                                         }
587                                         else /* can't move any further */
588                                                 break;
589                                 }
590                         }
591                         else {
592                                 /* make delta 'positive' before using it, since we now know to go backwards */
593                                 delta = -delta;
594                                 
595                                 for (track = tdn->nlt->prev, n = 0; (track) && (n < delta); track = track->prev, n++) {
596                                         /* check if space in this track for the strip */
597                                         if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
598                                                 /* move strip to this track */
599                                                 BLI_remlink(&tdn->nlt->strips, strip);
600                                                 BKE_nlatrack_add_strip(track, strip);
601                                                 
602                                                 tdn->nlt = track;
603                                                 tdn->trackIndex--;
604                                         }
605                                         else /* can't move any further */
606                                                 break;
607                                 }
608                         }
609                 }
610         }
611 }
612
613 static void recalcData_mask_common(TransInfo *t)
614 {
615         Mask *mask = CTX_data_edit_mask(t->context);
616
617         flushTransMasking(t);
618
619         DAG_id_tag_update(&mask->id, 0);
620 }
621
622 /* helper for recalcData() - for Image Editor transforms */
623 static void recalcData_image(TransInfo *t)
624 {
625         if (t->options & CTX_MASK) {
626                 recalcData_mask_common(t);
627         }
628         else if (t->obedit && t->obedit->type == OB_MESH) {
629                 SpaceImage *sima = t->sa->spacedata.first;
630                 
631                 flushTransUVs(t);
632                 if (sima->flag & SI_LIVE_UNWRAP)
633                         ED_uvedit_live_unwrap_re_solve();
634                 
635                 DAG_id_tag_update(t->obedit->data, 0);
636         }
637 }
638
639 /* helper for recalcData() - for Movie Clip transforms */
640 static void recalcData_spaceclip(TransInfo *t)
641 {
642         SpaceClip *sc = t->sa->spacedata.first;
643
644         if (ED_space_clip_check_show_trackedit(sc)) {
645                 MovieClip *clip = ED_space_clip_get_clip(sc);
646                 ListBase *tracksbase = BKE_tracking_get_active_tracks(&clip->tracking);
647                 MovieTrackingTrack *track;
648                 int framenr = ED_space_clip_get_clip_frame_number(sc);
649
650                 flushTransTracking(t);
651
652                 track = tracksbase->first;
653                 while (track) {
654                         if (TRACK_VIEW_SELECTED(sc, track) && (track->flag & TRACK_LOCKED) == 0) {
655                                 MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenr);
656
657                                 if (t->mode == TFM_TRANSLATION) {
658                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
659                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_POS);
660                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_SEARCH))
661                                                 BKE_tracking_marker_clamp(marker, CLAMP_SEARCH_POS);
662                                 }
663                                 else if (t->mode == TFM_RESIZE) {
664                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
665                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_DIM);
666                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_SEARCH))
667                                                 BKE_tracking_marker_clamp(marker, CLAMP_SEARCH_DIM);
668                                 }
669                                 else if (t->mode == TFM_ROTATION) {
670                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
671                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_POS);
672                                 }
673                         }
674
675                         track = track->next;
676                 }
677
678                 DAG_id_tag_update(&clip->id, 0);
679         }
680         else if (t->options & CTX_MASK) {
681                 recalcData_mask_common(t);
682         }
683 }
684
685 /* helper for recalcData() - for 3d-view transforms */
686 static void recalcData_view3d(TransInfo *t)
687 {
688         Base *base = t->scene->basact;
689         
690         if (t->obedit) {
691                 if (ELEM(t->obedit->type, OB_CURVE, OB_SURF)) {
692                         Curve *cu = t->obedit->data;
693                         ListBase *nurbs = BKE_curve_editNurbs_get(cu);
694                         Nurb *nu = nurbs->first;
695                         
696                         if (t->state != TRANS_CANCEL) {
697                                 clipMirrorModifier(t, t->obedit);
698                                 applyProject(t);
699                         }
700                         
701                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
702                                 
703                         if (t->state == TRANS_CANCEL) {
704                                 while (nu) {
705                                         BKE_nurb_handles_calc(nu); /* Cant do testhandlesNurb here, it messes up the h1 and h2 flags */
706                                         nu = nu->next;
707                                 }
708                         } 
709                         else {
710                                 /* Normal updating */
711                                 while (nu) {
712                                         BKE_nurb_test2D(nu);
713                                         BKE_nurb_handles_calc(nu);
714                                         nu = nu->next;
715                                 }
716                         }
717                 }
718                 else if (t->obedit->type == OB_LATTICE) {
719                         Lattice *la = t->obedit->data;
720                         
721                         if (t->state != TRANS_CANCEL) {
722                                 applyProject(t);
723                         }
724                         
725                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
726                         
727                         if (la->editlatt->latt->flag & LT_OUTSIDE) outside_lattice(la->editlatt->latt);
728                 }
729                 else if (t->obedit->type == OB_MESH) {
730                         BMEditMesh *em = BMEdit_FromObject(t->obedit);
731                         /* mirror modifier clipping? */
732                         if (t->state != TRANS_CANCEL) {
733                                 /* apply clipping after so we never project past the clip plane [#25423] */
734                                 applyProject(t);
735                                 clipMirrorModifier(t, t->obedit);
736                         }
737                         if ((t->options & CTX_NO_MIRROR) == 0 && (t->flag & T_MIRROR))
738                                 editbmesh_apply_to_mirror(t);
739                                 
740                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
741                         
742                         EDBM_mesh_normals_update(em);
743                         BMEdit_RecalcTessellation(em);
744                 }
745                 else if (t->obedit->type == OB_ARMATURE) { /* no recalc flag, does pose */
746                         bArmature *arm = t->obedit->data;
747                         ListBase *edbo = arm->edbo;
748                         EditBone *ebo;
749                         TransData *td = t->data;
750                         int i;
751                         
752                         if (t->state != TRANS_CANCEL) {
753                                 applyProject(t);
754                         }
755                         
756                         /* Ensure all bones are correctly adjusted */
757                         for (ebo = edbo->first; ebo; ebo = ebo->next) {
758                                 
759                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent) {
760                                         /* If this bone has a parent tip that has been moved */
761                                         if (ebo->parent->flag & BONE_TIPSEL) {
762                                                 copy_v3_v3(ebo->head, ebo->parent->tail);
763                                                 if (t->mode == TFM_BONE_ENVELOPE) ebo->rad_head = ebo->parent->rad_tail;
764                                         }
765                                         /* If this bone has a parent tip that has NOT been moved */
766                                         else {
767                                                 copy_v3_v3(ebo->parent->tail, ebo->head);
768                                                 if (t->mode == TFM_BONE_ENVELOPE) ebo->parent->rad_tail = ebo->rad_head;
769                                         }
770                                 }
771                                 
772                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
773                                 ebo->length = len_v3v3(ebo->head, ebo->tail);
774                                 if (ebo->oldlength == 0.0f) {
775                                         ebo->rad_head = 0.25f * ebo->length;
776                                         ebo->rad_tail = 0.10f * ebo->length;
777                                         ebo->dist = 0.25f * ebo->length;
778                                         if (ebo->parent) {
779                                                 if (ebo->rad_head > ebo->parent->rad_tail)
780                                                         ebo->rad_head = ebo->parent->rad_tail;
781                                         }
782                                 }
783                                 else if (t->mode != TFM_BONE_ENVELOPE) {
784                                         /* if bones change length, lets do that for the deform distance as well */
785                                         ebo->dist *= ebo->length / ebo->oldlength;
786                                         ebo->rad_head *= ebo->length / ebo->oldlength;
787                                         ebo->rad_tail *= ebo->length / ebo->oldlength;
788                                         ebo->oldlength = ebo->length;
789                                 }
790                         }
791                         
792                         
793                         if (t->mode != TFM_BONE_ROLL) {
794                                 /* fix roll */
795                                 for (i = 0; i < t->total; i++, td++) {
796                                         if (td->extra) {
797                                                 float vec[3], up_axis[3];
798                                                 float qrot[4];
799                                                 
800                                                 ebo = td->extra;
801                                                 copy_v3_v3(up_axis, td->axismtx[2]);
802                                                 
803                                                 if (t->mode != TFM_ROTATION) {
804                                                         sub_v3_v3v3(vec, ebo->tail, ebo->head);
805                                                         normalize_v3(vec);
806                                                         rotation_between_vecs_to_quat(qrot, td->axismtx[1], vec);
807                                                         mul_qt_v3(qrot, up_axis);
808                                                 }
809                                                 else {
810                                                         mul_m3_v3(t->mat, up_axis);
811                                                 }
812                                                 
813                                                 ebo->roll = ED_rollBoneToVector(ebo, up_axis, FALSE);
814                                         }
815                                 }
816                         }
817                         
818                         if (arm->flag & ARM_MIRROR_EDIT)
819                                 transform_armature_mirror_update(t->obedit);
820                         
821                 }
822                 else {
823                         if (t->state != TRANS_CANCEL) {
824                                 applyProject(t);
825                         }
826                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
827                 }
828         }
829         else if ((t->flag & T_POSE) && t->poseobj) {
830                 Object *ob = t->poseobj;
831                 bArmature *arm = ob->data;
832                 
833                 /* if animtimer is running, and the object already has animation data,
834                  * check if the auto-record feature means that we should record 'samples'
835                  * (i.e. uneditable animation values)
836                  *
837                  * context is needed for keying set poll() functions.
838                  */
839                 // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
840                 if ((t->animtimer) && (t->context) && IS_AUTOKEY_ON(t->scene)) {
841                         int targetless_ik = (t->flag & T_AUTOIK); // XXX this currently doesn't work, since flags aren't set yet!
842                         
843                         animrecord_check_state(t->scene, &ob->id, t->animtimer);
844                         autokeyframe_pose_cb_func(t->context, t->scene, (View3D *)t->view, ob, t->mode, targetless_ik);
845                 }
846                 
847                 /* old optimize trick... this enforces to bypass the depgraph */
848                 if (!(arm->flag & ARM_DELAYDEFORM)) {
849                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);  /* sets recalc flags */
850                 }
851                 else
852                         BKE_pose_where_is(t->scene, ob);
853         }
854         else if (base && (base->object->mode & OB_MODE_PARTICLE_EDIT) && PE_get_current(t->scene, base->object)) {
855                 if (t->state != TRANS_CANCEL) {
856                         applyProject(t);
857                 }
858                 flushTransParticles(t);
859         }
860         else {
861                 int i;
862                 
863                 if (t->state != TRANS_CANCEL) {
864                         applyProject(t);
865                 }
866                 
867                 for (i = 0; i < t->total; i++) {
868                         TransData *td = t->data + i;
869                         Object *ob = td->ob;
870                         
871                         if (td->flag & TD_NOACTION)
872                                 break;
873                         
874                         if (td->flag & TD_SKIP)
875                                 continue;
876                         
877                         /* if animtimer is running, and the object already has animation data,
878                          * check if the auto-record feature means that we should record 'samples'
879                          * (i.e. uneditable animation values)
880                          */
881                         // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
882                         if ((t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
883                                 animrecord_check_state(t->scene, &ob->id, t->animtimer);
884                                 autokeyframe_ob_cb_func(t->context, t->scene, (View3D *)t->view, ob, t->mode);
885                         }
886                         
887                         /* sets recalc flags fully, instead of flushing existing ones 
888                          * otherwise proxies don't function correctly
889                          */
890                         DAG_id_tag_update(&ob->id, OB_RECALC_OB);
891                 }
892         }
893 }
894
895 /* helper for recalcData() - for sequencer transforms */
896 static void recalcData_sequencer(TransInfo *t)
897 {
898         Editing *ed = BKE_sequencer_editing_get(t->scene, FALSE);
899         Sequence *seq;
900
901         SEQ_BEGIN(ed, seq)
902         {
903                 if (seq->flag & SELECT) {
904                         BKE_sequence_invalidate_deendent(t->scene, seq);
905                 }
906         }
907         SEQ_END
908
909         BKE_sequencer_preprocessed_cache_cleanup();
910
911         flushTransSeq(t);
912 }
913
914 /* called for updating while transform acts, once per redraw */
915 void recalcData(TransInfo *t)
916 {
917         if (t->spacetype == SPACE_NODE) {
918                 flushTransNodes(t);
919         }
920         else if (t->spacetype == SPACE_SEQ) {
921                 recalcData_sequencer(t);
922         }
923         else if (t->spacetype == SPACE_ACTION) {
924                 recalcData_actedit(t);
925         }
926         else if (t->spacetype == SPACE_IPO) {
927                 recalcData_graphedit(t);
928         }
929         else if (t->spacetype == SPACE_NLA) {
930                 recalcData_nla(t);
931         }
932         else if (t->spacetype == SPACE_IMAGE) {
933                 recalcData_image(t);
934         }
935         else if (t->spacetype == SPACE_VIEW3D) {
936                 recalcData_view3d(t);
937         }
938         else if (t->spacetype == SPACE_CLIP) {
939                 recalcData_spaceclip(t);
940         }
941
942         if (t->options & CTX_MASK) {
943
944         }
945 }
946
947 void drawLine(TransInfo *t, const float center[3], const float dir[3], char axis, short options)
948 {
949         float v1[3], v2[3], v3[3];
950         unsigned char col[3], col2[3];
951
952         if (t->spacetype == SPACE_VIEW3D) {
953                 View3D *v3d = t->view;
954                 
955                 glPushMatrix();
956                 
957                 //if (t->obedit) glLoadMatrixf(t->obedit->obmat);       // sets opengl viewing
958                 
959                 
960                 copy_v3_v3(v3, dir);
961                 mul_v3_fl(v3, v3d->far);
962                 
963                 sub_v3_v3v3(v2, center, v3);
964                 add_v3_v3v3(v1, center, v3);
965                 
966                 if (options & DRAWLIGHT) {
967                         col[0] = col[1] = col[2] = 220;
968                 }
969                 else {
970                         UI_GetThemeColor3ubv(TH_GRID, col);
971                 }
972                 UI_make_axis_color(col, col2, axis);
973                 glColor3ubv(col2);
974                 
975                 setlinestyle(0);
976                 glBegin(GL_LINE_STRIP);
977                 glVertex3fv(v1);
978                 glVertex3fv(v2);
979                 glEnd();
980                 
981                 glPopMatrix();
982         }
983 }
984
985 void resetTransRestrictions(TransInfo *t)
986 {
987         t->flag &= ~T_ALL_RESTRICTIONS;
988 }
989
990 /* the *op can be NULL */
991 int initTransInfo(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event)
992 {
993         Scene *sce = CTX_data_scene(C);
994         ToolSettings *ts = CTX_data_tool_settings(C);
995         ARegion *ar = CTX_wm_region(C);
996         ScrArea *sa = CTX_wm_area(C);
997         Object *obedit = CTX_data_edit_object(C);
998         
999         /* moving: is shown in drawobject() (transform color) */
1000 //  TRANSFORM_FIX_ME
1001 //      if (obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
1002 //      else if (G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
1003 //      else G.moving= G_TRANSFORM_OBJ;
1004         
1005         t->scene = sce;
1006         t->sa = sa;
1007         t->ar = ar;
1008         t->obedit = obedit;
1009         t->settings = ts;
1010         
1011         t->data = NULL;
1012         t->ext = NULL;
1013         
1014         t->helpline = HLP_NONE;
1015         
1016         t->flag = 0;
1017         
1018         t->redraw = 1; /* redraw first time */
1019         
1020         if (event) {
1021                 copy_v2_v2_int(t->imval, event->mval);
1022                 t->event_type = event->type;
1023         }
1024         else {
1025                 t->imval[0] = 0;
1026                 t->imval[1] = 0;
1027         }
1028         
1029         t->con.imval[0] = t->imval[0];
1030         t->con.imval[1] = t->imval[1];
1031         
1032         t->mval[0] = t->imval[0];
1033         t->mval[1] = t->imval[1];
1034         
1035         t->transform        = NULL;
1036         t->handleEvent      = NULL;
1037         
1038         t->total            = 0;
1039         
1040         t->val = 0.0f;
1041
1042         zero_v3(t->vec);
1043         zero_v3(t->center);
1044
1045         unit_m3(t->mat);
1046         
1047         /* if there's an event, we're modal */
1048         if (event) {
1049                 t->flag |= T_MODAL;
1050         }
1051
1052         /* Crease needs edge flag */
1053         if (ELEM(t->mode, TFM_CREASE, TFM_BWEIGHT)) {
1054                 t->options |= CTX_EDGE;
1055         }
1056
1057
1058         /* Assign the space type, some exceptions for running in different mode */
1059         if (sa == NULL) {
1060                 /* background mode */
1061                 t->spacetype = SPACE_EMPTY;
1062         }
1063         else if ((ar == NULL) && (sa->spacetype == SPACE_VIEW3D)) {
1064                 /* running in the text editor */
1065                 t->spacetype = SPACE_EMPTY;
1066         }
1067         else {
1068                 /* normal operation */
1069                 t->spacetype = sa->spacetype;
1070         }
1071
1072
1073         if (t->spacetype == SPACE_VIEW3D) {
1074                 View3D *v3d = sa->spacedata.first;
1075                 bScreen *animscreen = ED_screen_animation_playing(CTX_wm_manager(C));
1076                 
1077                 t->view = v3d;
1078                 t->animtimer = (animscreen) ? animscreen->animtimer : NULL;
1079                 
1080                 /* turn manipulator off during transform */
1081                 // FIXME: but don't do this when USING the manipulator...
1082                 if (t->flag & T_MODAL) {
1083                         t->twtype = v3d->twtype;
1084                         v3d->twtype = 0;
1085                 }
1086
1087                 if (v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
1088                 t->around = v3d->around;
1089                 
1090                 if (op && (RNA_struct_find_property(op->ptr, "constraint_orientation") &&
1091                            RNA_struct_property_is_set(op->ptr, "constraint_orientation")))
1092                 {
1093                         t->current_orientation = RNA_enum_get(op->ptr, "constraint_orientation");
1094
1095                         if (t->current_orientation >= V3D_MANIP_CUSTOM + BIF_countTransformOrientation(C)) {
1096                                 t->current_orientation = V3D_MANIP_GLOBAL;
1097                         }
1098                 }
1099                 else {
1100                         t->current_orientation = v3d->twmode;
1101                 }
1102
1103                 /* exceptional case */
1104                 if (t->around == V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
1105                         if (ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL)) {
1106                                 t->options |= CTX_NO_PET;
1107                         }
1108                 }
1109
1110                 /* initialize UV transform from */
1111                 if (op && RNA_struct_find_property(op->ptr, "correct_uv")) {
1112                         if (RNA_struct_property_is_set(op->ptr, "correct_uv")) {
1113                                 if (RNA_boolean_get(op->ptr, "correct_uv")) {
1114                                         t->settings->uvcalc_flag |= UVCALC_TRANSFORM_CORRECT;
1115                                 }
1116                                 else {
1117                                         t->settings->uvcalc_flag &= ~UVCALC_TRANSFORM_CORRECT;
1118                                 }
1119                         }
1120                         else {
1121                                 RNA_boolean_set(op->ptr, "correct_uv", t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT);
1122                         }
1123                 }
1124
1125         }
1126         else if (t->spacetype == SPACE_IMAGE) {
1127                 SpaceImage *sima = sa->spacedata.first;
1128                 // XXX for now, get View2D from the active region
1129                 t->view = &ar->v2d;
1130                 t->around = sima->around;
1131
1132                 if (ED_space_image_show_uvedit(sima, t->obedit)) {
1133                         /* UV transform */
1134                 }
1135                 else if (sima->mode == SI_MODE_MASK) {
1136                         t->options |= CTX_MASK;
1137                 }
1138                 else {
1139                         BLI_assert(0);
1140                 }
1141         }
1142         else if (t->spacetype == SPACE_NODE) {
1143                 // XXX for now, get View2D from the active region
1144                 t->view = &ar->v2d;
1145                 t->around = V3D_CENTER;
1146         }
1147         else if (t->spacetype == SPACE_IPO) {
1148                 SpaceIpo *sipo = sa->spacedata.first;
1149                 t->view = &ar->v2d;
1150                 t->around = sipo->around;
1151         }
1152         else if (t->spacetype == SPACE_CLIP) {
1153                 SpaceClip *sclip = sa->spacedata.first;
1154                 t->view = &ar->v2d;
1155                 t->around = sclip->around;
1156
1157                 if (ED_space_clip_check_show_trackedit(sclip))
1158                         t->options |= CTX_MOVIECLIP;
1159                 else if (ED_space_clip_check_show_maskedit(sclip))
1160                         t->options |= CTX_MASK;
1161         }
1162         else {
1163                 if (ar) {
1164                         // XXX for now, get View2D  from the active region
1165                         t->view = &ar->v2d;
1166                         // XXX for now, the center point is the midpoint of the data
1167                 }
1168                 else {
1169                         t->view = NULL;
1170                 }
1171                 t->around = V3D_CENTER;
1172         }
1173         
1174         if (op && RNA_struct_property_is_set(op->ptr, "release_confirm")) {
1175                 if (RNA_boolean_get(op->ptr, "release_confirm")) {
1176                         t->flag |= T_RELEASE_CONFIRM;
1177                 }
1178         }
1179         else {
1180                 if (U.flag & USER_RELEASECONFIRM) {
1181                         t->flag |= T_RELEASE_CONFIRM;
1182                 }
1183         }
1184
1185         if (op && (RNA_struct_find_property(op->ptr, "mirror") &&
1186                    RNA_struct_property_is_set(op->ptr, "mirror")))
1187         {
1188                 if (RNA_boolean_get(op->ptr, "mirror")) {
1189                         t->flag |= T_MIRROR;
1190                         t->mirror = 1;
1191                 }
1192         }
1193         // Need stuff to take it from edit mesh or whatnot here
1194         else if (t->spacetype == SPACE_VIEW3D) {
1195                 if (t->obedit && t->obedit->type == OB_MESH && (((Mesh *)t->obedit->data)->editflag & ME_EDIT_MIRROR_X)) {
1196                         t->flag |= T_MIRROR;
1197                         t->mirror = 1;
1198                 }
1199         }
1200         
1201         /* setting PET flag only if property exist in operator. Otherwise, assume it's not supported */
1202         if (op && RNA_struct_find_property(op->ptr, "proportional")) {
1203                 if (RNA_struct_property_is_set(op->ptr, "proportional")) {
1204                         switch (RNA_enum_get(op->ptr, "proportional")) {
1205                                 case PROP_EDIT_CONNECTED:
1206                                         t->flag |= T_PROP_CONNECTED;
1207                                 case PROP_EDIT_ON:
1208                                         t->flag |= T_PROP_EDIT;
1209                                         break;
1210                         }
1211                 }
1212                 else {
1213                         /* use settings from scene only if modal */
1214                         if (t->flag & T_MODAL) {
1215                                 if ((t->options & CTX_NO_PET) == 0) {
1216                                         if (t->obedit && ts->proportional != PROP_EDIT_OFF) {
1217                                                 t->flag |= T_PROP_EDIT;
1218
1219                                                 if (ts->proportional == PROP_EDIT_CONNECTED) {
1220                                                         t->flag |= T_PROP_CONNECTED;
1221                                                 }
1222                                         }
1223                                         else if (t->options & CTX_MASK) {
1224                                                 if (ts->proportional_mask) {
1225                                                         t->flag |= T_PROP_EDIT;
1226
1227                                                         if (ts->proportional == PROP_EDIT_CONNECTED) {
1228                                                                 t->flag |= T_PROP_CONNECTED;
1229                                                         }
1230                                                 }
1231                                         }
1232                                         else if (t->obedit == NULL && ts->proportional_objects) {
1233                                                 t->flag |= T_PROP_EDIT;
1234                                         }
1235                                 }
1236                         }
1237                 }
1238                 
1239                 if (op && (RNA_struct_find_property(op->ptr, "proportional_size") &&
1240                            RNA_struct_property_is_set(op->ptr, "proportional_size")))
1241                 {
1242                         t->prop_size = RNA_float_get(op->ptr, "proportional_size");
1243                 }
1244                 else {
1245                         t->prop_size = ts->proportional_size;
1246                 }
1247                 
1248                 
1249                 /* TRANSFORM_FIX_ME rna restrictions */
1250                 if (t->prop_size <= 0.00001f) {
1251                         printf("Proportional size (%f) under 0.00001, reseting to 1!\n", t->prop_size);
1252                         t->prop_size = 1.0f;
1253                 }
1254                 
1255                 if (op && (RNA_struct_find_property(op->ptr, "proportional_edit_falloff") &&
1256                            RNA_struct_property_is_set(op->ptr, "proportional_edit_falloff")))
1257                 {
1258                         t->prop_mode = RNA_enum_get(op->ptr, "proportional_edit_falloff");
1259                 }
1260                 else {
1261                         t->prop_mode = ts->prop_mode;
1262                 }
1263         }
1264         else { /* add not pet option to context when not available */
1265                 t->options |= CTX_NO_PET;
1266         }
1267         
1268         // Mirror is not supported with PET, turn it off.
1269         if (t->flag & T_PROP_EDIT) {
1270                 t->flag &= ~T_MIRROR;
1271         }
1272
1273         setTransformViewMatrices(t);
1274         initNumInput(&t->num);
1275         
1276         return 1;
1277 }
1278
1279 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1280 void postTrans(bContext *C, TransInfo *t)
1281 {
1282         TransData *td;
1283         
1284         if (t->draw_handle_view)
1285                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_view);
1286         if (t->draw_handle_apply)
1287                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_apply);
1288         if (t->draw_handle_pixel)
1289                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_pixel);
1290         if (t->draw_handle_cursor)
1291                 WM_paint_cursor_end(CTX_wm_manager(C), t->draw_handle_cursor);
1292
1293         if (t->customFree) {
1294                 /* Can take over freeing t->data and data2d etc... */
1295                 t->customFree(t);
1296         }
1297         else if ((t->customData != NULL) && (t->flag & T_FREE_CUSTOMDATA)) {
1298                 MEM_freeN(t->customData);
1299         }
1300
1301         /* postTrans can be called when nothing is selected, so data is NULL already */
1302         if (t->data) {
1303                 int a;
1304                 
1305                 /* free data malloced per trans-data */
1306                 for (a = 0, td = t->data; a < t->total; a++, td++) {
1307                         if (td->flag & TD_BEZTRIPLE) 
1308                                 MEM_freeN(td->hdata);
1309                 }
1310                 MEM_freeN(t->data);
1311         }
1312         
1313         BLI_freelistN(&t->tsnap.points);
1314
1315         if (t->ext) MEM_freeN(t->ext);
1316         if (t->data2d) {
1317                 MEM_freeN(t->data2d);
1318                 t->data2d = NULL;
1319         }
1320         
1321         if (t->spacetype == SPACE_IMAGE) {
1322                 if (t->options & CTX_MASK) {
1323                         /* pass */
1324                 }
1325                 else {
1326                         SpaceImage *sima = t->sa->spacedata.first;
1327                         if (sima->flag & SI_LIVE_UNWRAP)
1328                                 ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1329                 }
1330         }
1331         else if (t->spacetype == SPACE_VIEW3D) {
1332                 View3D *v3d = t->sa->spacedata.first;
1333                 /* restore manipulator */
1334                 if (t->flag & T_MODAL) {
1335                         v3d->twtype = t->twtype;
1336                 }
1337         }
1338         
1339         if (t->mouse.data) {
1340                 MEM_freeN(t->mouse.data);
1341         }
1342 }
1343
1344 void applyTransObjects(TransInfo *t)
1345 {
1346         TransData *td;
1347         
1348         for (td = t->data; td < t->data + t->total; td++) {
1349                 copy_v3_v3(td->iloc, td->loc);
1350                 if (td->ext->rot) {
1351                         copy_v3_v3(td->ext->irot, td->ext->rot);
1352                 }
1353                 if (td->ext->size) {
1354                         copy_v3_v3(td->ext->isize, td->ext->size);
1355                 }
1356         }
1357         recalcData(t);
1358 }
1359
1360 static void restoreElement(TransData *td)
1361 {
1362         /* TransData for crease has no loc */
1363         if (td->loc) {
1364                 copy_v3_v3(td->loc, td->iloc);
1365         }
1366         if (td->val) {
1367                 *td->val = td->ival;
1368         }
1369
1370         if (td->ext && (td->flag & TD_NO_EXT) == 0) {
1371                 if (td->ext->rot) {
1372                         copy_v3_v3(td->ext->rot, td->ext->irot);
1373                 }
1374                 if (td->ext->rotAngle) {
1375                         *td->ext->rotAngle = td->ext->irotAngle;
1376                 }
1377                 if (td->ext->rotAxis) {
1378                         copy_v3_v3(td->ext->rotAxis, td->ext->irotAxis);
1379                 }
1380                 /* XXX, drotAngle & drotAxis not used yet */
1381                 if (td->ext->size) {
1382                         copy_v3_v3(td->ext->size, td->ext->isize);
1383                 }
1384                 if (td->ext->quat) {
1385                         copy_qt_qt(td->ext->quat, td->ext->iquat);
1386                 }
1387         }
1388         
1389         if (td->flag & TD_BEZTRIPLE) {
1390                 *(td->hdata->h1) = td->hdata->ih1;
1391                 *(td->hdata->h2) = td->hdata->ih2;
1392         }
1393 }
1394
1395 void restoreTransObjects(TransInfo *t)
1396 {
1397         TransData *td;
1398         TransData2D *td2d;
1399
1400         for (td = t->data; td < t->data + t->total; td++) {
1401                 restoreElement(td);
1402         }
1403         
1404         for (td2d = t->data2d; t->data2d && td2d < t->data2d + t->total; td2d++) {
1405                 if (td2d->h1) {
1406                         td2d->h1[0] = td2d->ih1[0];
1407                         td2d->h1[1] = td2d->ih1[1];
1408                 }
1409                 if (td2d->h2) {
1410                         td2d->h2[0] = td2d->ih2[0];
1411                         td2d->h2[1] = td2d->ih2[1];
1412                 }
1413         }
1414
1415         unit_m3(t->mat);
1416         
1417         recalcData(t);
1418 }
1419
1420 void calculateCenter2D(TransInfo *t)
1421 {
1422         if (t->flag & (T_EDIT | T_POSE)) {
1423                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1424                 float vec[3];
1425                 
1426                 copy_v3_v3(vec, t->center);
1427                 mul_m4_v3(ob->obmat, vec);
1428                 projectIntView(t, vec, t->center2d);
1429         }
1430         else {
1431                 projectIntView(t, t->center, t->center2d);
1432         }
1433 }
1434
1435 void calculateCenterCursor(TransInfo *t)
1436 {
1437         float *cursor;
1438         
1439         cursor = give_cursor(t->scene, t->view);
1440         copy_v3_v3(t->center, cursor);
1441         
1442         /* If edit or pose mode, move cursor in local space */
1443         if (t->flag & (T_EDIT | T_POSE)) {
1444                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1445                 float mat[3][3], imat[3][3];
1446                 
1447                 sub_v3_v3v3(t->center, t->center, ob->obmat[3]);
1448                 copy_m3_m4(mat, ob->obmat);
1449                 invert_m3_m3(imat, mat);
1450                 mul_m3_v3(imat, t->center);
1451         }
1452         
1453         calculateCenter2D(t);
1454 }
1455
1456 void calculateCenterCursor2D(TransInfo *t)
1457 {
1458         float aspx = 1.0, aspy = 1.0;
1459         float *cursor = NULL;
1460         
1461         if (t->spacetype == SPACE_IMAGE) {
1462                 SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
1463                 /* only space supported right now but may change */
1464                 if (t->options & CTX_MASK) {
1465                         ED_space_image_get_aspect(sima, &aspx, &aspy);
1466                 }
1467                 else {
1468                         ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
1469                 }
1470                 cursor = sima->cursor;
1471         }
1472         
1473         if (cursor) {
1474                 if (t->options & CTX_MASK) {
1475                         float co[2];
1476                         float frame_size[2];
1477                         SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
1478                         ED_space_image_get_size_fl(sima, frame_size);
1479
1480                         BKE_mask_coord_from_frame(co, cursor, frame_size);
1481
1482                         ED_space_image_get_aspect(sima, &aspx, &aspy);
1483
1484                         t->center[0] = co[0] * aspx;
1485                         t->center[1] = co[1] * aspy;
1486                 }
1487                 else {
1488                         t->center[0] = cursor[0] * aspx;
1489                         t->center[1] = cursor[1] * aspy;
1490                 }
1491         }
1492         
1493         calculateCenter2D(t);
1494 }
1495
1496 static void calculateCenterCursorGraph2D(TransInfo *t)
1497 {
1498         SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
1499         Scene *scene = t->scene;
1500         
1501         /* cursor is combination of current frame, and graph-editor cursor value */
1502         t->center[0] = (float)(scene->r.cfra);
1503         t->center[1] = sipo->cursorVal;
1504         
1505         calculateCenter2D(t);
1506 }
1507
1508 void calculateCenterMedian(TransInfo *t)
1509 {
1510         float partial[3] = {0.0f, 0.0f, 0.0f};
1511         int total = 0;
1512         int i;
1513         
1514         for (i = 0; i < t->total; i++) {
1515                 if (t->data[i].flag & TD_SELECTED) {
1516                         if (!(t->data[i].flag & TD_NOCENTER)) {
1517                                 add_v3_v3(partial, t->data[i].center);
1518                                 total++;
1519                         }
1520                 }
1521                 else {
1522                         /*
1523                          * All the selected elements are at the head of the array
1524                          * which means we can stop when it finds unselected data
1525                          */
1526                         break;
1527                 }
1528         }
1529         if (i)
1530                 mul_v3_fl(partial, 1.0f / total);
1531         copy_v3_v3(t->center, partial);
1532         
1533         calculateCenter2D(t);
1534 }
1535
1536 void calculateCenterBound(TransInfo *t)
1537 {
1538         float max[3];
1539         float min[3];
1540         int i;
1541         for (i = 0; i < t->total; i++) {
1542                 if (i) {
1543                         if (t->data[i].flag & TD_SELECTED) {
1544                                 if (!(t->data[i].flag & TD_NOCENTER))
1545                                         minmax_v3v3_v3(min, max, t->data[i].center);
1546                         }
1547                         else {
1548                                 /*
1549                                  * All the selected elements are at the head of the array
1550                                  * which means we can stop when it finds unselected data
1551                                  */
1552                                 break;
1553                         }
1554                 }
1555                 else {
1556                         copy_v3_v3(max, t->data[i].center);
1557                         copy_v3_v3(min, t->data[i].center);
1558                 }
1559         }
1560         add_v3_v3v3(t->center, min, max);
1561         mul_v3_fl(t->center, 0.5);
1562         
1563         calculateCenter2D(t);
1564 }
1565
1566 void calculateCenter(TransInfo *t)
1567 {
1568         switch (t->around) {
1569                 case V3D_CENTER:
1570                         calculateCenterBound(t);
1571                         break;
1572                 case V3D_CENTROID:
1573                         calculateCenterMedian(t);
1574                         break;
1575                 case V3D_CURSOR:
1576                         if (t->spacetype == SPACE_IMAGE)
1577                                 calculateCenterCursor2D(t);
1578                         else if (t->spacetype == SPACE_IPO)
1579                                 calculateCenterCursorGraph2D(t);
1580                         else
1581                                 calculateCenterCursor(t);
1582                         break;
1583                 case V3D_LOCAL:
1584                         /* Individual element center uses median center for helpline and such */
1585                         calculateCenterMedian(t);
1586                         break;
1587                 case V3D_ACTIVE:
1588                 {
1589                         /* set median, and if if if... do object center */
1590                 
1591                         /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1592
1593                         if (t->obedit) {
1594                                 if (t->obedit && t->obedit->type == OB_MESH) {
1595                                         BMEditSelection ese;
1596                                         BMEditMesh *em = BMEdit_FromObject(t->obedit);
1597
1598                                         if (BM_select_history_active_get(em->bm, &ese)) {
1599                                                 BM_editselection_center(&ese, t->center);
1600                                                 calculateCenter2D(t);
1601                                                 break;
1602                                         }
1603                                 }
1604                                 else if (ELEM(t->obedit->type, OB_CURVE, OB_SURF)) {
1605                                         float center[3];
1606                                         Curve *cu = (Curve *)t->obedit->data;
1607
1608                                         if (ED_curve_actSelection(cu, center)) {
1609                                                 copy_v3_v3(t->center, center);
1610                                                 calculateCenter2D(t);
1611                                                 break;
1612                                         }
1613                                 }
1614                         } /* END EDIT MODE ACTIVE ELEMENT */
1615
1616                         calculateCenterMedian(t);
1617                         if ((t->flag & (T_EDIT | T_POSE)) == 0) {
1618                                 Scene *scene = t->scene;
1619                                 Object *ob = OBACT;
1620                                 if (ob) {
1621                                         copy_v3_v3(t->center, ob->obmat[3]);
1622                                         projectIntView(t, t->center, t->center2d);
1623                                 }
1624                         }
1625                 
1626                 }
1627         }
1628         
1629         /* setting constraint center */
1630         copy_v3_v3(t->con.center, t->center);
1631         if (t->flag & (T_EDIT | T_POSE)) {
1632                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1633                 mul_m4_v3(ob->obmat, t->con.center);
1634         }
1635         
1636         /* for panning from cameraview */
1637         if (t->flag & T_OBJECT) {
1638                 if (t->spacetype == SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW) {
1639                         View3D *v3d = t->view;
1640                         Scene *scene = t->scene;
1641                         RegionView3D *rv3d = t->ar->regiondata;
1642                         
1643                         if (v3d->camera == OBACT && rv3d->persp == RV3D_CAMOB) {
1644                                 float axis[3];
1645                                 /* persinv is nasty, use viewinv instead, always right */
1646                                 copy_v3_v3(axis, t->viewinv[2]);
1647                                 normalize_v3(axis);
1648                                 
1649                                 /* 6.0 = 6 grid units */
1650                                 axis[0] = t->center[0] - 6.0f * axis[0];
1651                                 axis[1] = t->center[1] - 6.0f * axis[1];
1652                                 axis[2] = t->center[2] - 6.0f * axis[2];
1653                                 
1654                                 projectIntView(t, axis, t->center2d);
1655                                 
1656                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1657                                 if (t->mode == TFM_TRANSLATION) {
1658                                         copy_v3_v3(t->center, axis);
1659                                         copy_v3_v3(t->con.center, t->center);
1660                                 }
1661                         }
1662                 }
1663         }
1664         
1665         if (t->spacetype == SPACE_VIEW3D) {
1666                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1667                 if (t->flag & (T_EDIT | T_POSE)) {
1668                         Object *ob = t->obedit ? t->obedit : t->poseobj;
1669                         float vec[3];
1670                         
1671                         copy_v3_v3(vec, t->center);
1672                         mul_m4_v3(ob->obmat, vec);
1673                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1674                 }
1675                 else {
1676                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1677                 }
1678         }
1679 }
1680
1681 void calculatePropRatio(TransInfo *t)
1682 {
1683         TransData *td = t->data;
1684         int i;
1685         float dist;
1686         short connected = t->flag & T_PROP_CONNECTED;
1687
1688         if (t->flag & T_PROP_EDIT) {
1689                 for (i = 0; i < t->total; i++, td++) {
1690                         if (td->flag & TD_SELECTED) {
1691                                 td->factor = 1.0f;
1692                         }
1693                         else if (t->flag & T_MIRROR && td->loc[0] * t->mirror < -0.00001f) {
1694                                 td->flag |= TD_SKIP;
1695                                 td->factor = 0.0f;
1696                                 restoreElement(td);
1697                         }
1698                         else if ((connected && (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size)) ||
1699                                  (connected == 0 && td->rdist > t->prop_size))
1700                         {
1701                                 /*
1702                                  * The elements are sorted according to their dist member in the array,
1703                                  * that means we can stop when it finds one element outside of the propsize.
1704                                  */
1705                                 td->flag |= TD_NOACTION;
1706                                 td->factor = 0.0f;
1707                                 restoreElement(td);
1708                         }
1709                         else {
1710                                 /* Use rdist for falloff calculations, it is the real distance */
1711                                 td->flag &= ~TD_NOACTION;
1712                                 
1713                                 if (connected)
1714                                         dist = (t->prop_size - td->dist) / t->prop_size;
1715                                 else
1716                                         dist = (t->prop_size - td->rdist) / t->prop_size;
1717
1718                                 /*
1719                                  * Clamp to positive numbers.
1720                                  * Certain corner cases with connectivity and individual centers
1721                                  * can give values of rdist larger than propsize.
1722                                  */
1723                                 if (dist < 0.0f)
1724                                         dist = 0.0f;
1725                                 
1726                                 switch (t->prop_mode) {
1727                                         case PROP_SHARP:
1728                                                 td->factor = dist * dist;
1729                                                 break;
1730                                         case PROP_SMOOTH:
1731                                                 td->factor = 3.0f * dist * dist - 2.0f * dist * dist * dist;
1732                                                 break;
1733                                         case PROP_ROOT:
1734                                                 td->factor = (float)sqrt(dist);
1735                                                 break;
1736                                         case PROP_LIN:
1737                                                 td->factor = dist;
1738                                                 break;
1739                                         case PROP_CONST:
1740                                                 td->factor = 1.0f;
1741                                                 break;
1742                                         case PROP_SPHERE:
1743                                                 td->factor = (float)sqrt(2 * dist - dist * dist);
1744                                                 break;
1745                                         case PROP_RANDOM:
1746                                                 BLI_srand(BLI_rand()); /* random seed */
1747                                                 td->factor = BLI_frand() * dist;
1748                                                 break;
1749                                         default:
1750                                                 td->factor = 1;
1751                                 }
1752                         }
1753                 }
1754                 switch (t->prop_mode) {
1755                         case PROP_SHARP:
1756                                 strcpy(t->proptext, "(Sharp)");
1757                                 break;
1758                         case PROP_SMOOTH:
1759                                 strcpy(t->proptext, "(Smooth)");
1760                                 break;
1761                         case PROP_ROOT:
1762                                 strcpy(t->proptext, "(Root)");
1763                                 break;
1764                         case PROP_LIN:
1765                                 strcpy(t->proptext, "(Linear)");
1766                                 break;
1767                         case PROP_CONST:
1768                                 strcpy(t->proptext, "(Constant)");
1769                                 break;
1770                         case PROP_SPHERE:
1771                                 strcpy(t->proptext, "(Sphere)");
1772                                 break;
1773                         case PROP_RANDOM:
1774                                 strcpy(t->proptext, "(Random)");
1775                                 break;
1776                         default:
1777                                 t->proptext[0] = '\0';
1778                 }
1779         }
1780         else {
1781                 for (i = 0; i < t->total; i++, td++) {
1782                         td->factor = 1.0;
1783                 }
1784                 t->proptext[0] = '\0';
1785         }
1786 }