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