Fix #32755: Stripes in Metastrip can not be moved on other channel with mouse (grab...
[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         TransData *td;
899         int a;
900         Sequence *seq_prev = NULL;
901
902         for (a = 0, td = t->data; a < t->total; a++, td++) {
903                 TransDataSeq *tdsq = (TransDataSeq *) td->extra;
904                 Sequence *seq = tdsq->seq;
905
906                 if (seq != seq_prev) {
907                         BKE_sequence_invalidate_dependent(t->scene, seq);
908                 }
909
910                 seq_prev = seq;
911         }
912
913         BKE_sequencer_preprocessed_cache_cleanup();
914
915         flushTransSeq(t);
916 }
917
918 /* called for updating while transform acts, once per redraw */
919 void recalcData(TransInfo *t)
920 {
921         if (t->spacetype == SPACE_NODE) {
922                 flushTransNodes(t);
923         }
924         else if (t->spacetype == SPACE_SEQ) {
925                 recalcData_sequencer(t);
926         }
927         else if (t->spacetype == SPACE_ACTION) {
928                 recalcData_actedit(t);
929         }
930         else if (t->spacetype == SPACE_IPO) {
931                 recalcData_graphedit(t);
932         }
933         else if (t->spacetype == SPACE_NLA) {
934                 recalcData_nla(t);
935         }
936         else if (t->spacetype == SPACE_IMAGE) {
937                 recalcData_image(t);
938         }
939         else if (t->spacetype == SPACE_VIEW3D) {
940                 recalcData_view3d(t);
941         }
942         else if (t->spacetype == SPACE_CLIP) {
943                 recalcData_spaceclip(t);
944         }
945
946         if (t->options & CTX_MASK) {
947
948         }
949 }
950
951 void drawLine(TransInfo *t, const float center[3], const float dir[3], char axis, short options)
952 {
953         float v1[3], v2[3], v3[3];
954         unsigned char col[3], col2[3];
955
956         if (t->spacetype == SPACE_VIEW3D) {
957                 View3D *v3d = t->view;
958                 
959                 glPushMatrix();
960                 
961                 //if (t->obedit) glLoadMatrixf(t->obedit->obmat);       // sets opengl viewing
962                 
963                 
964                 copy_v3_v3(v3, dir);
965                 mul_v3_fl(v3, v3d->far);
966                 
967                 sub_v3_v3v3(v2, center, v3);
968                 add_v3_v3v3(v1, center, v3);
969                 
970                 if (options & DRAWLIGHT) {
971                         col[0] = col[1] = col[2] = 220;
972                 }
973                 else {
974                         UI_GetThemeColor3ubv(TH_GRID, col);
975                 }
976                 UI_make_axis_color(col, col2, axis);
977                 glColor3ubv(col2);
978                 
979                 setlinestyle(0);
980                 glBegin(GL_LINE_STRIP);
981                 glVertex3fv(v1);
982                 glVertex3fv(v2);
983                 glEnd();
984                 
985                 glPopMatrix();
986         }
987 }
988
989 void resetTransRestrictions(TransInfo *t)
990 {
991         t->flag &= ~T_ALL_RESTRICTIONS;
992 }
993
994 /* the *op can be NULL */
995 int initTransInfo(bContext *C, TransInfo *t, wmOperator *op, wmEvent *event)
996 {
997         Scene *sce = CTX_data_scene(C);
998         ToolSettings *ts = CTX_data_tool_settings(C);
999         ARegion *ar = CTX_wm_region(C);
1000         ScrArea *sa = CTX_wm_area(C);
1001         Object *obedit = CTX_data_edit_object(C);
1002         
1003         /* moving: is shown in drawobject() (transform color) */
1004 //  TRANSFORM_FIX_ME
1005 //      if (obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
1006 //      else if (G.f & G_PARTICLEEDIT) G.moving= G_TRANSFORM_PARTICLE;
1007 //      else G.moving= G_TRANSFORM_OBJ;
1008         
1009         t->scene = sce;
1010         t->sa = sa;
1011         t->ar = ar;
1012         t->obedit = obedit;
1013         t->settings = ts;
1014         
1015         t->data = NULL;
1016         t->ext = NULL;
1017         
1018         t->helpline = HLP_NONE;
1019         
1020         t->flag = 0;
1021         
1022         t->redraw = 1; /* redraw first time */
1023         
1024         if (event) {
1025                 copy_v2_v2_int(t->imval, event->mval);
1026                 t->event_type = event->type;
1027         }
1028         else {
1029                 t->imval[0] = 0;
1030                 t->imval[1] = 0;
1031         }
1032         
1033         t->con.imval[0] = t->imval[0];
1034         t->con.imval[1] = t->imval[1];
1035         
1036         t->mval[0] = t->imval[0];
1037         t->mval[1] = t->imval[1];
1038         
1039         t->transform        = NULL;
1040         t->handleEvent      = NULL;
1041         
1042         t->total            = 0;
1043         
1044         t->val = 0.0f;
1045
1046         zero_v3(t->vec);
1047         zero_v3(t->center);
1048
1049         unit_m3(t->mat);
1050         
1051         /* if there's an event, we're modal */
1052         if (event) {
1053                 t->flag |= T_MODAL;
1054         }
1055
1056         /* Crease needs edge flag */
1057         if (ELEM(t->mode, TFM_CREASE, TFM_BWEIGHT)) {
1058                 t->options |= CTX_EDGE;
1059         }
1060
1061
1062         /* Assign the space type, some exceptions for running in different mode */
1063         if (sa == NULL) {
1064                 /* background mode */
1065                 t->spacetype = SPACE_EMPTY;
1066         }
1067         else if ((ar == NULL) && (sa->spacetype == SPACE_VIEW3D)) {
1068                 /* running in the text editor */
1069                 t->spacetype = SPACE_EMPTY;
1070         }
1071         else {
1072                 /* normal operation */
1073                 t->spacetype = sa->spacetype;
1074         }
1075
1076
1077         if (t->spacetype == SPACE_VIEW3D) {
1078                 View3D *v3d = sa->spacedata.first;
1079                 bScreen *animscreen = ED_screen_animation_playing(CTX_wm_manager(C));
1080                 
1081                 t->view = v3d;
1082                 t->animtimer = (animscreen) ? animscreen->animtimer : NULL;
1083                 
1084                 /* turn manipulator off during transform */
1085                 // FIXME: but don't do this when USING the manipulator...
1086                 if (t->flag & T_MODAL) {
1087                         t->twtype = v3d->twtype;
1088                         v3d->twtype = 0;
1089                 }
1090
1091                 if (v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
1092                 t->around = v3d->around;
1093                 
1094                 if (op && (RNA_struct_find_property(op->ptr, "constraint_orientation") &&
1095                            RNA_struct_property_is_set(op->ptr, "constraint_orientation")))
1096                 {
1097                         t->current_orientation = RNA_enum_get(op->ptr, "constraint_orientation");
1098
1099                         if (t->current_orientation >= V3D_MANIP_CUSTOM + BIF_countTransformOrientation(C)) {
1100                                 t->current_orientation = V3D_MANIP_GLOBAL;
1101                         }
1102                 }
1103                 else {
1104                         t->current_orientation = v3d->twmode;
1105                 }
1106
1107                 /* exceptional case */
1108                 if (t->around == V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
1109                         if (ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL)) {
1110                                 t->options |= CTX_NO_PET;
1111                         }
1112                 }
1113
1114                 /* initialize UV transform from */
1115                 if (op && RNA_struct_find_property(op->ptr, "correct_uv")) {
1116                         if (RNA_struct_property_is_set(op->ptr, "correct_uv")) {
1117                                 if (RNA_boolean_get(op->ptr, "correct_uv")) {
1118                                         t->settings->uvcalc_flag |= UVCALC_TRANSFORM_CORRECT;
1119                                 }
1120                                 else {
1121                                         t->settings->uvcalc_flag &= ~UVCALC_TRANSFORM_CORRECT;
1122                                 }
1123                         }
1124                         else {
1125                                 RNA_boolean_set(op->ptr, "correct_uv", t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT);
1126                         }
1127                 }
1128
1129         }
1130         else if (t->spacetype == SPACE_IMAGE) {
1131                 SpaceImage *sima = sa->spacedata.first;
1132                 // XXX for now, get View2D from the active region
1133                 t->view = &ar->v2d;
1134                 t->around = sima->around;
1135
1136                 if (ED_space_image_show_uvedit(sima, t->obedit)) {
1137                         /* UV transform */
1138                 }
1139                 else if (sima->mode == SI_MODE_MASK) {
1140                         t->options |= CTX_MASK;
1141                 }
1142                 else {
1143                         BLI_assert(0);
1144                 }
1145         }
1146         else if (t->spacetype == SPACE_NODE) {
1147                 // XXX for now, get View2D from the active region
1148                 t->view = &ar->v2d;
1149                 t->around = V3D_CENTER;
1150         }
1151         else if (t->spacetype == SPACE_IPO) {
1152                 SpaceIpo *sipo = sa->spacedata.first;
1153                 t->view = &ar->v2d;
1154                 t->around = sipo->around;
1155         }
1156         else if (t->spacetype == SPACE_CLIP) {
1157                 SpaceClip *sclip = sa->spacedata.first;
1158                 t->view = &ar->v2d;
1159                 t->around = sclip->around;
1160
1161                 if (ED_space_clip_check_show_trackedit(sclip))
1162                         t->options |= CTX_MOVIECLIP;
1163                 else if (ED_space_clip_check_show_maskedit(sclip))
1164                         t->options |= CTX_MASK;
1165         }
1166         else {
1167                 if (ar) {
1168                         // XXX for now, get View2D  from the active region
1169                         t->view = &ar->v2d;
1170                         // XXX for now, the center point is the midpoint of the data
1171                 }
1172                 else {
1173                         t->view = NULL;
1174                 }
1175                 t->around = V3D_CENTER;
1176         }
1177         
1178         if (op && RNA_struct_property_is_set(op->ptr, "release_confirm")) {
1179                 if (RNA_boolean_get(op->ptr, "release_confirm")) {
1180                         t->flag |= T_RELEASE_CONFIRM;
1181                 }
1182         }
1183         else {
1184                 if (U.flag & USER_RELEASECONFIRM) {
1185                         t->flag |= T_RELEASE_CONFIRM;
1186                 }
1187         }
1188
1189         if (op && (RNA_struct_find_property(op->ptr, "mirror") &&
1190                    RNA_struct_property_is_set(op->ptr, "mirror")))
1191         {
1192                 if (RNA_boolean_get(op->ptr, "mirror")) {
1193                         t->flag |= T_MIRROR;
1194                         t->mirror = 1;
1195                 }
1196         }
1197         // Need stuff to take it from edit mesh or whatnot here
1198         else if (t->spacetype == SPACE_VIEW3D) {
1199                 if (t->obedit && t->obedit->type == OB_MESH && (((Mesh *)t->obedit->data)->editflag & ME_EDIT_MIRROR_X)) {
1200                         t->flag |= T_MIRROR;
1201                         t->mirror = 1;
1202                 }
1203         }
1204         
1205         /* setting PET flag only if property exist in operator. Otherwise, assume it's not supported */
1206         if (op && RNA_struct_find_property(op->ptr, "proportional")) {
1207                 if (RNA_struct_property_is_set(op->ptr, "proportional")) {
1208                         switch (RNA_enum_get(op->ptr, "proportional")) {
1209                                 case PROP_EDIT_CONNECTED:
1210                                         t->flag |= T_PROP_CONNECTED;
1211                                 case PROP_EDIT_ON:
1212                                         t->flag |= T_PROP_EDIT;
1213                                         break;
1214                         }
1215                 }
1216                 else {
1217                         /* use settings from scene only if modal */
1218                         if (t->flag & T_MODAL) {
1219                                 if ((t->options & CTX_NO_PET) == 0) {
1220                                         if (t->obedit && ts->proportional != PROP_EDIT_OFF) {
1221                                                 t->flag |= T_PROP_EDIT;
1222
1223                                                 if (ts->proportional == PROP_EDIT_CONNECTED) {
1224                                                         t->flag |= T_PROP_CONNECTED;
1225                                                 }
1226                                         }
1227                                         else if (t->options & CTX_MASK) {
1228                                                 if (ts->proportional_mask) {
1229                                                         t->flag |= T_PROP_EDIT;
1230
1231                                                         if (ts->proportional == PROP_EDIT_CONNECTED) {
1232                                                                 t->flag |= T_PROP_CONNECTED;
1233                                                         }
1234                                                 }
1235                                         }
1236                                         else if (t->obedit == NULL && ts->proportional_objects) {
1237                                                 t->flag |= T_PROP_EDIT;
1238                                         }
1239                                 }
1240                         }
1241                 }
1242                 
1243                 if (op && (RNA_struct_find_property(op->ptr, "proportional_size") &&
1244                            RNA_struct_property_is_set(op->ptr, "proportional_size")))
1245                 {
1246                         t->prop_size = RNA_float_get(op->ptr, "proportional_size");
1247                 }
1248                 else {
1249                         t->prop_size = ts->proportional_size;
1250                 }
1251                 
1252                 
1253                 /* TRANSFORM_FIX_ME rna restrictions */
1254                 if (t->prop_size <= 0.00001f) {
1255                         printf("Proportional size (%f) under 0.00001, reseting to 1!\n", t->prop_size);
1256                         t->prop_size = 1.0f;
1257                 }
1258                 
1259                 if (op && (RNA_struct_find_property(op->ptr, "proportional_edit_falloff") &&
1260                            RNA_struct_property_is_set(op->ptr, "proportional_edit_falloff")))
1261                 {
1262                         t->prop_mode = RNA_enum_get(op->ptr, "proportional_edit_falloff");
1263                 }
1264                 else {
1265                         t->prop_mode = ts->prop_mode;
1266                 }
1267         }
1268         else { /* add not pet option to context when not available */
1269                 t->options |= CTX_NO_PET;
1270         }
1271         
1272         // Mirror is not supported with PET, turn it off.
1273         if (t->flag & T_PROP_EDIT) {
1274                 t->flag &= ~T_MIRROR;
1275         }
1276
1277         setTransformViewMatrices(t);
1278         initNumInput(&t->num);
1279         
1280         return 1;
1281 }
1282
1283 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1284 void postTrans(bContext *C, TransInfo *t)
1285 {
1286         TransData *td;
1287         
1288         if (t->draw_handle_view)
1289                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_view);
1290         if (t->draw_handle_apply)
1291                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_apply);
1292         if (t->draw_handle_pixel)
1293                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_pixel);
1294         if (t->draw_handle_cursor)
1295                 WM_paint_cursor_end(CTX_wm_manager(C), t->draw_handle_cursor);
1296
1297         if (t->customFree) {
1298                 /* Can take over freeing t->data and data2d etc... */
1299                 t->customFree(t);
1300                 BLI_assert(t->customData == NULL);
1301         }
1302         else if ((t->customData != NULL) && (t->flag & T_FREE_CUSTOMDATA)) {
1303                 MEM_freeN(t->customData);
1304                 t->customData = NULL;
1305         }
1306
1307         /* postTrans can be called when nothing is selected, so data is NULL already */
1308         if (t->data) {
1309                 
1310                 /* free data malloced per trans-data */
1311                 if ((t->obedit && ELEM(t->obedit->type, OB_CURVE, OB_SURF)) ||
1312                     (t->spacetype == SPACE_IPO))
1313                 {
1314                         int a;
1315                         for (a = 0, td = t->data; a < t->total; a++, td++) {
1316                                 if (td->flag & TD_BEZTRIPLE) {
1317                                         MEM_freeN(td->hdata);
1318                                 }
1319                         }
1320                 }
1321                 MEM_freeN(t->data);
1322         }
1323         
1324         BLI_freelistN(&t->tsnap.points);
1325
1326         if (t->ext) MEM_freeN(t->ext);
1327         if (t->data2d) {
1328                 MEM_freeN(t->data2d);
1329                 t->data2d = NULL;
1330         }
1331         
1332         if (t->spacetype == SPACE_IMAGE) {
1333                 if (t->options & CTX_MASK) {
1334                         /* pass */
1335                 }
1336                 else {
1337                         SpaceImage *sima = t->sa->spacedata.first;
1338                         if (sima->flag & SI_LIVE_UNWRAP)
1339                                 ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1340                 }
1341         }
1342         else if (t->spacetype == SPACE_VIEW3D) {
1343                 View3D *v3d = t->sa->spacedata.first;
1344                 /* restore manipulator */
1345                 if (t->flag & T_MODAL) {
1346                         v3d->twtype = t->twtype;
1347                 }
1348         }
1349         
1350         if (t->mouse.data) {
1351                 MEM_freeN(t->mouse.data);
1352         }
1353 }
1354
1355 void applyTransObjects(TransInfo *t)
1356 {
1357         TransData *td;
1358         
1359         for (td = t->data; td < t->data + t->total; td++) {
1360                 copy_v3_v3(td->iloc, td->loc);
1361                 if (td->ext->rot) {
1362                         copy_v3_v3(td->ext->irot, td->ext->rot);
1363                 }
1364                 if (td->ext->size) {
1365                         copy_v3_v3(td->ext->isize, td->ext->size);
1366                 }
1367         }
1368         recalcData(t);
1369 }
1370
1371 static void restoreElement(TransData *td)
1372 {
1373         /* TransData for crease has no loc */
1374         if (td->loc) {
1375                 copy_v3_v3(td->loc, td->iloc);
1376         }
1377         if (td->val) {
1378                 *td->val = td->ival;
1379         }
1380
1381         if (td->ext && (td->flag & TD_NO_EXT) == 0) {
1382                 if (td->ext->rot) {
1383                         copy_v3_v3(td->ext->rot, td->ext->irot);
1384                 }
1385                 if (td->ext->rotAngle) {
1386                         *td->ext->rotAngle = td->ext->irotAngle;
1387                 }
1388                 if (td->ext->rotAxis) {
1389                         copy_v3_v3(td->ext->rotAxis, td->ext->irotAxis);
1390                 }
1391                 /* XXX, drotAngle & drotAxis not used yet */
1392                 if (td->ext->size) {
1393                         copy_v3_v3(td->ext->size, td->ext->isize);
1394                 }
1395                 if (td->ext->quat) {
1396                         copy_qt_qt(td->ext->quat, td->ext->iquat);
1397                 }
1398         }
1399         
1400         if (td->flag & TD_BEZTRIPLE) {
1401                 *(td->hdata->h1) = td->hdata->ih1;
1402                 *(td->hdata->h2) = td->hdata->ih2;
1403         }
1404 }
1405
1406 void restoreTransObjects(TransInfo *t)
1407 {
1408         TransData *td;
1409         TransData2D *td2d;
1410
1411         for (td = t->data; td < t->data + t->total; td++) {
1412                 restoreElement(td);
1413         }
1414         
1415         for (td2d = t->data2d; t->data2d && td2d < t->data2d + t->total; td2d++) {
1416                 if (td2d->h1) {
1417                         td2d->h1[0] = td2d->ih1[0];
1418                         td2d->h1[1] = td2d->ih1[1];
1419                 }
1420                 if (td2d->h2) {
1421                         td2d->h2[0] = td2d->ih2[0];
1422                         td2d->h2[1] = td2d->ih2[1];
1423                 }
1424         }
1425
1426         unit_m3(t->mat);
1427         
1428         recalcData(t);
1429 }
1430
1431 void calculateCenter2D(TransInfo *t)
1432 {
1433         if (t->flag & (T_EDIT | T_POSE)) {
1434                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1435                 float vec[3];
1436                 
1437                 copy_v3_v3(vec, t->center);
1438                 mul_m4_v3(ob->obmat, vec);
1439                 projectIntView(t, vec, t->center2d);
1440         }
1441         else {
1442                 projectIntView(t, t->center, t->center2d);
1443         }
1444 }
1445
1446 void calculateCenterCursor(TransInfo *t)
1447 {
1448         float *cursor;
1449         
1450         cursor = give_cursor(t->scene, t->view);
1451         copy_v3_v3(t->center, cursor);
1452         
1453         /* If edit or pose mode, move cursor in local space */
1454         if (t->flag & (T_EDIT | T_POSE)) {
1455                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1456                 float mat[3][3], imat[3][3];
1457                 
1458                 sub_v3_v3v3(t->center, t->center, ob->obmat[3]);
1459                 copy_m3_m4(mat, ob->obmat);
1460                 invert_m3_m3(imat, mat);
1461                 mul_m3_v3(imat, t->center);
1462         }
1463         
1464         calculateCenter2D(t);
1465 }
1466
1467 void calculateCenterCursor2D(TransInfo *t)
1468 {
1469         float aspx = 1.0, aspy = 1.0;
1470         float *cursor = NULL;
1471         
1472         if (t->spacetype == SPACE_IMAGE) {
1473                 SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
1474                 /* only space supported right now but may change */
1475                 if (t->options & CTX_MASK) {
1476                         ED_space_image_get_aspect(sima, &aspx, &aspy);
1477                 }
1478                 else {
1479                         ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
1480                 }
1481                 cursor = sima->cursor;
1482         }
1483         
1484         if (cursor) {
1485                 if (t->options & CTX_MASK) {
1486                         float co[2];
1487                         float frame_size[2];
1488                         SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
1489                         ED_space_image_get_size_fl(sima, frame_size);
1490
1491                         BKE_mask_coord_from_frame(co, cursor, frame_size);
1492
1493                         ED_space_image_get_aspect(sima, &aspx, &aspy);
1494
1495                         t->center[0] = co[0] * aspx;
1496                         t->center[1] = co[1] * aspy;
1497                 }
1498                 else {
1499                         t->center[0] = cursor[0] * aspx;
1500                         t->center[1] = cursor[1] * aspy;
1501                 }
1502         }
1503         
1504         calculateCenter2D(t);
1505 }
1506
1507 static void calculateCenterCursorGraph2D(TransInfo *t)
1508 {
1509         SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
1510         Scene *scene = t->scene;
1511         
1512         /* cursor is combination of current frame, and graph-editor cursor value */
1513         t->center[0] = (float)(scene->r.cfra);
1514         t->center[1] = sipo->cursorVal;
1515         
1516         calculateCenter2D(t);
1517 }
1518
1519 void calculateCenterMedian(TransInfo *t)
1520 {
1521         float partial[3] = {0.0f, 0.0f, 0.0f};
1522         int total = 0;
1523         int i;
1524         
1525         for (i = 0; i < t->total; i++) {
1526                 if (t->data[i].flag & TD_SELECTED) {
1527                         if (!(t->data[i].flag & TD_NOCENTER)) {
1528                                 add_v3_v3(partial, t->data[i].center);
1529                                 total++;
1530                         }
1531                 }
1532                 else {
1533                         /*
1534                          * All the selected elements are at the head of the array
1535                          * which means we can stop when it finds unselected data
1536                          */
1537                         break;
1538                 }
1539         }
1540         if (i)
1541                 mul_v3_fl(partial, 1.0f / total);
1542         copy_v3_v3(t->center, partial);
1543         
1544         calculateCenter2D(t);
1545 }
1546
1547 void calculateCenterBound(TransInfo *t)
1548 {
1549         float max[3];
1550         float min[3];
1551         int i;
1552         for (i = 0; i < t->total; i++) {
1553                 if (i) {
1554                         if (t->data[i].flag & TD_SELECTED) {
1555                                 if (!(t->data[i].flag & TD_NOCENTER))
1556                                         minmax_v3v3_v3(min, max, t->data[i].center);
1557                         }
1558                         else {
1559                                 /*
1560                                  * All the selected elements are at the head of the array
1561                                  * which means we can stop when it finds unselected data
1562                                  */
1563                                 break;
1564                         }
1565                 }
1566                 else {
1567                         copy_v3_v3(max, t->data[i].center);
1568                         copy_v3_v3(min, t->data[i].center);
1569                 }
1570         }
1571         add_v3_v3v3(t->center, min, max);
1572         mul_v3_fl(t->center, 0.5);
1573         
1574         calculateCenter2D(t);
1575 }
1576
1577 void calculateCenter(TransInfo *t)
1578 {
1579         switch (t->around) {
1580                 case V3D_CENTER:
1581                         calculateCenterBound(t);
1582                         break;
1583                 case V3D_CENTROID:
1584                         calculateCenterMedian(t);
1585                         break;
1586                 case V3D_CURSOR:
1587                         if (t->spacetype == SPACE_IMAGE)
1588                                 calculateCenterCursor2D(t);
1589                         else if (t->spacetype == SPACE_IPO)
1590                                 calculateCenterCursorGraph2D(t);
1591                         else
1592                                 calculateCenterCursor(t);
1593                         break;
1594                 case V3D_LOCAL:
1595                         /* Individual element center uses median center for helpline and such */
1596                         calculateCenterMedian(t);
1597                         break;
1598                 case V3D_ACTIVE:
1599                 {
1600                         /* set median, and if if if... do object center */
1601                 
1602                         /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1603
1604                         if (t->obedit) {
1605                                 if (t->obedit && t->obedit->type == OB_MESH) {
1606                                         BMEditSelection ese;
1607                                         BMEditMesh *em = BMEdit_FromObject(t->obedit);
1608
1609                                         if (BM_select_history_active_get(em->bm, &ese)) {
1610                                                 BM_editselection_center(&ese, t->center);
1611                                                 calculateCenter2D(t);
1612                                                 break;
1613                                         }
1614                                 }
1615                                 else if (ELEM(t->obedit->type, OB_CURVE, OB_SURF)) {
1616                                         float center[3];
1617                                         Curve *cu = (Curve *)t->obedit->data;
1618
1619                                         if (ED_curve_actSelection(cu, center)) {
1620                                                 copy_v3_v3(t->center, center);
1621                                                 calculateCenter2D(t);
1622                                                 break;
1623                                         }
1624                                 }
1625                         } /* END EDIT MODE ACTIVE ELEMENT */
1626
1627                         calculateCenterMedian(t);
1628                         if ((t->flag & (T_EDIT | T_POSE)) == 0) {
1629                                 Scene *scene = t->scene;
1630                                 Object *ob = OBACT;
1631                                 if (ob) {
1632                                         copy_v3_v3(t->center, ob->obmat[3]);
1633                                         projectIntView(t, t->center, t->center2d);
1634                                 }
1635                         }
1636                 
1637                 }
1638         }
1639         
1640         /* setting constraint center */
1641         copy_v3_v3(t->con.center, t->center);
1642         if (t->flag & (T_EDIT | T_POSE)) {
1643                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1644                 mul_m4_v3(ob->obmat, t->con.center);
1645         }
1646         
1647         /* for panning from cameraview */
1648         if (t->flag & T_OBJECT) {
1649                 if (t->spacetype == SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW) {
1650                         View3D *v3d = t->view;
1651                         Scene *scene = t->scene;
1652                         RegionView3D *rv3d = t->ar->regiondata;
1653                         
1654                         if (v3d->camera == OBACT && rv3d->persp == RV3D_CAMOB) {
1655                                 float axis[3];
1656                                 /* persinv is nasty, use viewinv instead, always right */
1657                                 copy_v3_v3(axis, t->viewinv[2]);
1658                                 normalize_v3(axis);
1659                                 
1660                                 /* 6.0 = 6 grid units */
1661                                 axis[0] = t->center[0] - 6.0f * axis[0];
1662                                 axis[1] = t->center[1] - 6.0f * axis[1];
1663                                 axis[2] = t->center[2] - 6.0f * axis[2];
1664                                 
1665                                 projectIntView(t, axis, t->center2d);
1666                                 
1667                                 /* rotate only needs correct 2d center, grab needs initgrabz() value */
1668                                 if (t->mode == TFM_TRANSLATION) {
1669                                         copy_v3_v3(t->center, axis);
1670                                         copy_v3_v3(t->con.center, t->center);
1671                                 }
1672                         }
1673                 }
1674         }
1675         
1676         if (t->spacetype == SPACE_VIEW3D) {
1677                 /* initgrabz() defines a factor for perspective depth correction, used in window_to_3d_delta() */
1678                 if (t->flag & (T_EDIT | T_POSE)) {
1679                         Object *ob = t->obedit ? t->obedit : t->poseobj;
1680                         float vec[3];
1681                         
1682                         copy_v3_v3(vec, t->center);
1683                         mul_m4_v3(ob->obmat, vec);
1684                         initgrabz(t->ar->regiondata, vec[0], vec[1], vec[2]);
1685                 }
1686                 else {
1687                         initgrabz(t->ar->regiondata, t->center[0], t->center[1], t->center[2]);
1688                 }
1689         }
1690 }
1691
1692 void calculatePropRatio(TransInfo *t)
1693 {
1694         TransData *td = t->data;
1695         int i;
1696         float dist;
1697         short connected = t->flag & T_PROP_CONNECTED;
1698
1699         if (t->flag & T_PROP_EDIT) {
1700                 for (i = 0; i < t->total; i++, td++) {
1701                         if (td->flag & TD_SELECTED) {
1702                                 td->factor = 1.0f;
1703                         }
1704                         else if (t->flag & T_MIRROR && td->loc[0] * t->mirror < -0.00001f) {
1705                                 td->flag |= TD_SKIP;
1706                                 td->factor = 0.0f;
1707                                 restoreElement(td);
1708                         }
1709                         else if ((connected && (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size)) ||
1710                                  (connected == 0 && td->rdist > t->prop_size))
1711                         {
1712                                 /*
1713                                  * The elements are sorted according to their dist member in the array,
1714                                  * that means we can stop when it finds one element outside of the propsize.
1715                                  */
1716                                 td->flag |= TD_NOACTION;
1717                                 td->factor = 0.0f;
1718                                 restoreElement(td);
1719                         }
1720                         else {
1721                                 /* Use rdist for falloff calculations, it is the real distance */
1722                                 td->flag &= ~TD_NOACTION;
1723                                 
1724                                 if (connected)
1725                                         dist = (t->prop_size - td->dist) / t->prop_size;
1726                                 else
1727                                         dist = (t->prop_size - td->rdist) / t->prop_size;
1728
1729                                 /*
1730                                  * Clamp to positive numbers.
1731                                  * Certain corner cases with connectivity and individual centers
1732                                  * can give values of rdist larger than propsize.
1733                                  */
1734                                 if (dist < 0.0f)
1735                                         dist = 0.0f;
1736                                 
1737                                 switch (t->prop_mode) {
1738                                         case PROP_SHARP:
1739                                                 td->factor = dist * dist;
1740                                                 break;
1741                                         case PROP_SMOOTH:
1742                                                 td->factor = 3.0f * dist * dist - 2.0f * dist * dist * dist;
1743                                                 break;
1744                                         case PROP_ROOT:
1745                                                 td->factor = (float)sqrt(dist);
1746                                                 break;
1747                                         case PROP_LIN:
1748                                                 td->factor = dist;
1749                                                 break;
1750                                         case PROP_CONST:
1751                                                 td->factor = 1.0f;
1752                                                 break;
1753                                         case PROP_SPHERE:
1754                                                 td->factor = (float)sqrt(2 * dist - dist * dist);
1755                                                 break;
1756                                         case PROP_RANDOM:
1757                                                 BLI_srand(BLI_rand()); /* random seed */
1758                                                 td->factor = BLI_frand() * dist;
1759                                                 break;
1760                                         default:
1761                                                 td->factor = 1;
1762                                 }
1763                         }
1764                 }
1765                 switch (t->prop_mode) {
1766                         case PROP_SHARP:
1767                                 strcpy(t->proptext, "(Sharp)");
1768                                 break;
1769                         case PROP_SMOOTH:
1770                                 strcpy(t->proptext, "(Smooth)");
1771                                 break;
1772                         case PROP_ROOT:
1773                                 strcpy(t->proptext, "(Root)");
1774                                 break;
1775                         case PROP_LIN:
1776                                 strcpy(t->proptext, "(Linear)");
1777                                 break;
1778                         case PROP_CONST:
1779                                 strcpy(t->proptext, "(Constant)");
1780                                 break;
1781                         case PROP_SPHERE:
1782                                 strcpy(t->proptext, "(Sphere)");
1783                                 break;
1784                         case PROP_RANDOM:
1785                                 strcpy(t->proptext, "(Random)");
1786                                 break;
1787                         default:
1788                                 t->proptext[0] = '\0';
1789                 }
1790         }
1791         else {
1792                 for (i = 0; i < t->total; i++, td++) {
1793                         td->factor = 1.0;
1794                 }
1795                 t->proptext[0] = '\0';
1796         }
1797 }