f2c44e64d432e87cb5eec0af6c14b3ddf04b2ba5
[blender-staging.git] / source / blender / editors / transform / transform.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.c
29  *  \ingroup edtransform
30  */
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36 #include <float.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_anim_types.h"
41 #include "DNA_armature_types.h"
42 #include "DNA_constraint_types.h"
43 #include "DNA_mask_types.h"
44 #include "DNA_movieclip_types.h"
45 #include "DNA_scene_types.h"  /* PET modes */
46
47 #include "BLI_alloca.h"
48 #include "BLI_utildefines.h"
49 #include "BLI_math.h"
50 #include "BLI_rect.h"
51 #include "BLI_listbase.h"
52 #include "BLI_string.h"
53 #include "BLI_ghash.h"
54 #include "BLI_memarena.h"
55
56 #include "BKE_nla.h"
57 #include "BKE_editmesh.h"
58 #include "BKE_editmesh_bvh.h"
59 #include "BKE_context.h"
60 #include "BKE_constraint.h"
61 #include "BKE_particle.h"
62 #include "BKE_unit.h"
63 #include "BKE_mask.h"
64 #include "BKE_report.h"
65
66 #include "BIF_gl.h"
67 #include "BIF_glutil.h"
68
69 #include "ED_image.h"
70 #include "ED_keyframing.h"
71 #include "ED_screen.h"
72 #include "ED_space_api.h"
73 #include "ED_markers.h"
74 #include "ED_view3d.h"
75 #include "ED_mesh.h"
76 #include "ED_clip.h"
77 #include "ED_node.h"
78
79 #include "WM_types.h"
80 #include "WM_api.h"
81
82 #include "UI_view2d.h"
83 #include "UI_interface_icons.h"
84 #include "UI_resources.h"
85
86 #include "RNA_access.h"
87
88 #include "BLF_api.h"
89 #include "BLF_translation.h"
90
91 #include "transform.h"
92
93 /* Disabling, since when you type you know what you are doing, and being able to set it to zero is handy. */
94 // #define USE_NUM_NO_ZERO
95
96 #define MAX_INFO_LEN 256
97
98 static void drawTransformApply(const struct bContext *C, ARegion *ar, void *arg);
99 static void doEdgeSlide(TransInfo *t, float perc);
100 static void doVertSlide(TransInfo *t, float perc);
101
102 static void drawEdgeSlide(TransInfo *t);
103 static void drawVertSlide(TransInfo *t);
104 static void len_v3_ensure(float v[3], const float length);
105 static void postInputRotation(TransInfo *t, float values[3]);
106
107 static void ElementRotation(TransInfo *t, TransData *td, float mat[3][3], short around);
108
109
110 /* Transform Callbacks */
111 static void initBend(TransInfo *t);
112 static eRedrawFlag handleEventBend(TransInfo *t, const struct wmEvent *event);
113 static void Bend(TransInfo *t, const int mval[2]);
114
115 static void initShear(TransInfo *t);
116 static eRedrawFlag handleEventShear(TransInfo *t, const struct wmEvent *event);
117 static void applyShear(TransInfo *t, const int mval[2]);
118
119 static void initResize(TransInfo *t);
120 static void applyResize(TransInfo *t, const int mval[2]);
121
122 static void initSkinResize(TransInfo *t);
123 static void applySkinResize(TransInfo *t, const int mval[2]);
124
125 static void initTranslation(TransInfo *t);
126 static void applyTranslation(TransInfo *t, const int mval[2]);
127
128 static void initToSphere(TransInfo *t);
129 static void applyToSphere(TransInfo *t, const int mval[2]);
130
131 static void initRotation(TransInfo *t);
132 static void applyRotation(TransInfo *t, const int mval[2]);
133
134 static void initShrinkFatten(TransInfo *t);
135 static void applyShrinkFatten(TransInfo *t, const int mval[2]);
136
137 static void initTilt(TransInfo *t);
138 static void applyTilt(TransInfo *t, const int mval[2]);
139
140 static void initCurveShrinkFatten(TransInfo *t);
141 static void applyCurveShrinkFatten(TransInfo *t, const int mval[2]);
142
143 static void initMaskShrinkFatten(TransInfo *t);
144 static void applyMaskShrinkFatten(TransInfo *t, const int mval[2]);
145
146 static void initGPShrinkFatten(TransInfo *t);
147 static void applyGPShrinkFatten(TransInfo *t, const int mval[2]);
148
149 static void initTrackball(TransInfo *t);
150 static void applyTrackball(TransInfo *t, const int mval[2]);
151
152 static void initPushPull(TransInfo *t);
153 static void applyPushPull(TransInfo *t, const int mval[2]);
154
155 static void initBevelWeight(TransInfo *t);
156 static void applyBevelWeight(TransInfo *t, const int mval[2]);
157
158 static void initCrease(TransInfo *t);
159 static void applyCrease(TransInfo *t, const int mval[2]);
160
161 static void initBoneSize(TransInfo *t);
162 static void applyBoneSize(TransInfo *t, const int mval[2]);
163
164 static void initBoneEnvelope(TransInfo *t);
165 static void applyBoneEnvelope(TransInfo *t, const int mval[2]);
166
167 static void initBoneRoll(TransInfo *t);
168 static void applyBoneRoll(TransInfo *t, const int mval[2]);
169
170 static void initEdgeSlide(TransInfo *t);
171 static eRedrawFlag handleEventEdgeSlide(TransInfo *t, const struct wmEvent *event);
172 static void applyEdgeSlide(TransInfo *t, const int mval[2]);
173
174 static void initVertSlide(TransInfo *t);
175 static eRedrawFlag handleEventVertSlide(TransInfo *t, const struct wmEvent *event);
176 static void applyVertSlide(TransInfo *t, const int mval[2]);
177
178 static void initTimeTranslate(TransInfo *t);
179 static void applyTimeTranslate(TransInfo *t, const int mval[2]);
180
181 static void initTimeSlide(TransInfo *t);
182 static void applyTimeSlide(TransInfo *t, const int mval[2]);
183
184 static void initTimeScale(TransInfo *t);
185 static void applyTimeScale(TransInfo *t, const int mval[2]);
186
187 static void initBakeTime(TransInfo *t);
188 static void applyBakeTime(TransInfo *t, const int mval[2]);
189
190 static void initMirror(TransInfo *t);
191 static void applyMirror(TransInfo *t, const int mval[2]);
192
193 static void initAlign(TransInfo *t);
194 static void applyAlign(TransInfo *t, const int mval[2]);
195
196 static void initSeqSlide(TransInfo *t);
197 static void applySeqSlide(TransInfo *t, const int mval[2]);
198 /* end transform callbacks */
199
200
201 static bool transdata_check_local_center(TransInfo *t, short around)
202 {
203         return ((around == V3D_LOCAL) && (
204                     (t->flag & (T_OBJECT | T_POSE)) ||
205                     (t->obedit && ELEM(t->obedit->type, OB_MESH, OB_CURVE, OB_MBALL, OB_ARMATURE)) ||
206                     (t->spacetype == SPACE_IPO) ||
207                     (t->options & (CTX_MOVIECLIP | CTX_MASK | CTX_PAINT_CURVE)))
208                 );
209 }
210
211 bool transdata_check_local_islands(TransInfo *t, short around)
212 {
213         return ((around == V3D_LOCAL) && (
214                 (t->obedit && ELEM(t->obedit->type, OB_MESH))));
215 }
216
217 /* ************************** SPACE DEPENDANT CODE **************************** */
218
219 void setTransformViewMatrices(TransInfo *t)
220 {
221         if (t->spacetype == SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW) {
222                 RegionView3D *rv3d = t->ar->regiondata;
223
224                 copy_m4_m4(t->viewmat, rv3d->viewmat);
225                 copy_m4_m4(t->viewinv, rv3d->viewinv);
226                 copy_m4_m4(t->persmat, rv3d->persmat);
227                 copy_m4_m4(t->persinv, rv3d->persinv);
228                 t->persp = rv3d->persp;
229         }
230         else {
231                 unit_m4(t->viewmat);
232                 unit_m4(t->viewinv);
233                 unit_m4(t->persmat);
234                 unit_m4(t->persinv);
235                 t->persp = RV3D_ORTHO;
236         }
237
238         calculateCenter2D(t);
239 }
240
241 static void convertViewVec2D(View2D *v2d, float r_vec[3], int dx, int dy)
242 {
243         float divx, divy;
244         
245         divx = BLI_rcti_size_x(&v2d->mask);
246         divy = BLI_rcti_size_y(&v2d->mask);
247
248         r_vec[0] = BLI_rctf_size_x(&v2d->cur) * dx / divx;
249         r_vec[1] = BLI_rctf_size_y(&v2d->cur) * dy / divy;
250         r_vec[2] = 0.0f;
251 }
252
253 static void convertViewVec2D_mask(View2D *v2d, float r_vec[3], int dx, int dy)
254 {
255         float divx, divy;
256         float mulx, muly;
257
258         divx = BLI_rcti_size_x(&v2d->mask);
259         divy = BLI_rcti_size_y(&v2d->mask);
260
261         mulx = BLI_rctf_size_x(&v2d->cur);
262         muly = BLI_rctf_size_y(&v2d->cur);
263
264         /* difference with convertViewVec2D */
265         /* clamp w/h, mask only */
266         if (mulx / divx < muly / divy) {
267                 divy = divx;
268                 muly = mulx;
269         }
270         else {
271                 divx = divy;
272                 mulx = muly;
273         }
274         /* end difference */
275
276         r_vec[0] = mulx * dx / divx;
277         r_vec[1] = muly * dy / divy;
278         r_vec[2] = 0.0f;
279 }
280
281 void convertViewVec(TransInfo *t, float r_vec[3], int dx, int dy)
282 {
283         if ((t->spacetype == SPACE_VIEW3D) && (t->ar->regiontype == RGN_TYPE_WINDOW)) {
284                 if (t->options & CTX_PAINT_CURVE) {
285                         r_vec[0] = dx;
286                         r_vec[1] = dy;
287                 }
288                 else {
289                         const float mval_f[2] = {(float)dx, (float)dy};
290                         ED_view3d_win_to_delta(t->ar, mval_f, r_vec, t->zfac);
291                 }
292         }
293         else if (t->spacetype == SPACE_IMAGE) {
294                 float aspx, aspy;
295
296                 if (t->options & CTX_MASK) {
297                         convertViewVec2D_mask(t->view, r_vec, dx, dy);
298                         ED_space_image_get_aspect(t->sa->spacedata.first, &aspx, &aspy);
299                 }
300                 else if (t->options & CTX_PAINT_CURVE) {
301                         r_vec[0] = dx;
302                         r_vec[1] = dy;
303
304                         aspx = aspy = 1.0;
305                 }
306                 else {
307                         convertViewVec2D(t->view, r_vec, dx, dy);
308                         ED_space_image_get_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
309                 }
310
311                 r_vec[0] *= aspx;
312                 r_vec[1] *= aspy;
313         }
314         else if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)) {
315                 convertViewVec2D(t->view, r_vec, dx, dy);
316         }
317         else if (ELEM(t->spacetype, SPACE_NODE, SPACE_SEQ)) {
318                 convertViewVec2D(&t->ar->v2d, r_vec, dx, dy);
319         }
320         else if (t->spacetype == SPACE_CLIP) {
321                 float aspx, aspy;
322
323                 if (t->options & CTX_MASK) {
324                         convertViewVec2D_mask(t->view, r_vec, dx, dy);
325                 }
326                 else {
327                         convertViewVec2D(t->view, r_vec, dx, dy);
328                 }
329
330                 if (t->options & CTX_MOVIECLIP) {
331                         ED_space_clip_get_aspect_dimension_aware(t->sa->spacedata.first, &aspx, &aspy);
332                 }
333                 else if (t->options & CTX_MASK) {
334                         /* TODO - NOT WORKING, this isnt so bad since its only display aspect */
335                         ED_space_clip_get_aspect(t->sa->spacedata.first, &aspx, &aspy);
336                 }
337                 else {
338                         /* should never happen, quiet warnings */
339                         BLI_assert(0);
340                         aspx = aspy = 1.0f;
341                 }
342
343                 r_vec[0] *= aspx;
344                 r_vec[1] *= aspy;
345         }
346         else {
347                 printf("%s: called in an invalid context\n", __func__);
348                 zero_v3(r_vec);
349         }
350 }
351
352 void projectIntViewEx(TransInfo *t, const float vec[3], int adr[2], const eV3DProjTest flag)
353 {
354         if (t->spacetype == SPACE_VIEW3D) {
355                 if (t->ar->regiontype == RGN_TYPE_WINDOW) {
356                         if (ED_view3d_project_int_global(t->ar, vec, adr, flag) != V3D_PROJ_RET_OK) {
357                                 adr[0] = (int)2140000000.0f;  /* this is what was done in 2.64, perhaps we can be smarter? */
358                                 adr[1] = (int)2140000000.0f;
359                         }
360                 }
361         }
362         else if (t->spacetype == SPACE_IMAGE) {
363                 SpaceImage *sima = t->sa->spacedata.first;
364
365                 if (t->options & CTX_MASK) {
366                         float aspx, aspy;
367                         float v[2];
368
369                         ED_space_image_get_aspect(sima, &aspx, &aspy);
370
371                         copy_v2_v2(v, vec);
372
373                         v[0] = v[0] / aspx;
374                         v[1] = v[1] / aspy;
375
376                         BKE_mask_coord_to_image(sima->image, &sima->iuser, v, v);
377
378                         ED_image_point_pos__reverse(sima, t->ar, v, v);
379
380                         adr[0] = v[0];
381                         adr[1] = v[1];
382                 }
383                 else if (t->options & CTX_PAINT_CURVE) {
384                         adr[0] = vec[0];
385                         adr[1] = vec[1];
386                 }
387                 else {
388                         float aspx, aspy, v[2];
389
390                         ED_space_image_get_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
391                         v[0] = vec[0] / aspx;
392                         v[1] = vec[1] / aspy;
393
394                         UI_view2d_view_to_region(t->view, v[0], v[1], &adr[0], &adr[1]);
395                 }
396         }
397         else if (t->spacetype == SPACE_ACTION) {
398                 int out[2] = {0, 0};
399 #if 0
400                 SpaceAction *sact = t->sa->spacedata.first;
401
402                 if (sact->flag & SACTION_DRAWTIME) {
403                         //vec[0] = vec[0]/((t->scene->r.frs_sec / t->scene->r.frs_sec_base));
404                         /* same as below */
405                         UI_view2d_view_to_region((View2D *)t->view, vec[0], vec[1], &out[0], &out[1]);
406                 }
407                 else
408 #endif
409                 {
410                         UI_view2d_view_to_region((View2D *)t->view, vec[0], vec[1], &out[0], &out[1]);
411                 }
412
413                 adr[0] = out[0];
414                 adr[1] = out[1];
415         }
416         else if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA)) {
417                 int out[2] = {0, 0};
418
419                 UI_view2d_view_to_region((View2D *)t->view, vec[0], vec[1], &out[0], &out[1]);
420                 adr[0] = out[0];
421                 adr[1] = out[1];
422         }
423         else if (t->spacetype == SPACE_SEQ) { /* XXX not tested yet, but should work */
424                 int out[2] = {0, 0};
425
426                 UI_view2d_view_to_region((View2D *)t->view, vec[0], vec[1], &out[0], &out[1]);
427                 adr[0] = out[0];
428                 adr[1] = out[1];
429         }
430         else if (t->spacetype == SPACE_CLIP) {
431                 SpaceClip *sc = t->sa->spacedata.first;
432
433                 if (t->options & CTX_MASK) {
434                         MovieClip *clip = ED_space_clip_get_clip(sc);
435
436                         if (clip) {
437                                 float aspx, aspy;
438                                 float v[2];
439
440                                 ED_space_clip_get_aspect(sc, &aspx, &aspy);
441
442                                 copy_v2_v2(v, vec);
443
444                                 v[0] = v[0] / aspx;
445                                 v[1] = v[1] / aspy;
446
447                                 BKE_mask_coord_to_movieclip(sc->clip, &sc->user, v, v);
448
449                                 ED_clip_point_stable_pos__reverse(sc, t->ar, v, v);
450
451                                 adr[0] = v[0];
452                                 adr[1] = v[1];
453                         }
454                         else {
455                                 adr[0] = 0;
456                                 adr[1] = 0;
457                         }
458                 }
459                 else if (t->options & CTX_MOVIECLIP) {
460                         float v[2], aspx, aspy;
461
462                         copy_v2_v2(v, vec);
463                         ED_space_clip_get_aspect_dimension_aware(t->sa->spacedata.first, &aspx, &aspy);
464
465                         v[0] /= aspx;
466                         v[1] /= aspy;
467
468                         UI_view2d_view_to_region(t->view, v[0], v[1], &adr[0], &adr[1]);
469                 }
470                 else {
471                         BLI_assert(0);
472                 }
473         }
474         else if (t->spacetype == SPACE_NODE) {
475                 UI_view2d_view_to_region((View2D *)t->view, vec[0], vec[1], &adr[0], &adr[1]);
476         }
477 }
478 void projectIntView(TransInfo *t, const float vec[3], int adr[2])
479 {
480         projectIntViewEx(t, vec, adr, V3D_PROJ_TEST_NOP);
481 }
482
483 void projectFloatViewEx(TransInfo *t, const float vec[3], float adr[2], const eV3DProjTest flag)
484 {
485         switch (t->spacetype) {
486                 case SPACE_VIEW3D:
487                 {
488                         if (t->options & CTX_PAINT_CURVE) {
489                                 adr[0] = vec[0];
490                                 adr[1] = vec[1];
491                         }
492                         else if (t->ar->regiontype == RGN_TYPE_WINDOW) {
493                                 /* allow points behind the view [#33643] */
494                                 if (ED_view3d_project_float_global(t->ar, vec, adr, flag) != V3D_PROJ_RET_OK) {
495                                         /* XXX, 2.64 and prior did this, weak! */
496                                         adr[0] = t->ar->winx / 2.0f;
497                                         adr[1] = t->ar->winy / 2.0f;
498                                 }
499                                 return;
500                         }
501                         break;
502                 }
503                 default:
504                 {
505                         int a[2] = {0, 0};
506                         projectIntView(t, vec, a);
507                         adr[0] = a[0];
508                         adr[1] = a[1];
509                         break;
510                 }
511         }
512 }
513 void projectFloatView(TransInfo *t, const float vec[3], float adr[2])
514 {
515         projectFloatViewEx(t, vec, adr, V3D_PROJ_TEST_NOP);
516 }
517
518 void applyAspectRatio(TransInfo *t, float vec[2])
519 {
520         if ((t->spacetype == SPACE_IMAGE) && (t->mode == TFM_TRANSLATION) && !(t->options & CTX_PAINT_CURVE)) {
521                 SpaceImage *sima = t->sa->spacedata.first;
522                 float aspx, aspy;
523
524                 if ((sima->flag & SI_COORDFLOATS) == 0) {
525                         int width, height;
526                         ED_space_image_get_size(sima, &width, &height);
527
528                         vec[0] *= width;
529                         vec[1] *= height;
530                 }
531
532                 ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
533                 vec[0] /= aspx;
534                 vec[1] /= aspy;
535         }
536         else if ((t->spacetype == SPACE_CLIP) && (t->mode == TFM_TRANSLATION)) {
537                 if (t->options & (CTX_MOVIECLIP | CTX_MASK)) {
538                         SpaceClip *sc = t->sa->spacedata.first;
539                         float aspx, aspy;
540
541
542                         if (t->options & CTX_MOVIECLIP) {
543                                 ED_space_clip_get_aspect_dimension_aware(sc, &aspx, &aspy);
544
545                                 vec[0] /= aspx;
546                                 vec[1] /= aspy;
547                         }
548                         else if (t->options & CTX_MASK) {
549                                 ED_space_clip_get_aspect(sc, &aspx, &aspy);
550
551                                 vec[0] /= aspx;
552                                 vec[1] /= aspy;
553                         }
554                 }
555         }
556 }
557
558 void removeAspectRatio(TransInfo *t, float vec[2])
559 {
560         if ((t->spacetype == SPACE_IMAGE) && (t->mode == TFM_TRANSLATION)) {
561                 SpaceImage *sima = t->sa->spacedata.first;
562                 float aspx, aspy;
563
564                 if ((sima->flag & SI_COORDFLOATS) == 0) {
565                         int width, height;
566                         ED_space_image_get_size(sima, &width, &height);
567
568                         vec[0] /= width;
569                         vec[1] /= height;
570                 }
571
572                 ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
573                 vec[0] *= aspx;
574                 vec[1] *= aspy;
575         }
576         else if ((t->spacetype == SPACE_CLIP) && (t->mode == TFM_TRANSLATION)) {
577                 if (t->options & (CTX_MOVIECLIP | CTX_MASK)) {
578                         SpaceClip *sc = t->sa->spacedata.first;
579                         float aspx = 1.0f, aspy = 1.0f;
580
581                         if (t->options & CTX_MOVIECLIP) {
582                                 ED_space_clip_get_aspect_dimension_aware(sc, &aspx, &aspy);
583                         }
584                         else if (t->options & CTX_MASK) {
585                                 ED_space_clip_get_aspect(sc, &aspx, &aspy);
586                         }
587
588                         vec[0] *= aspx;
589                         vec[1] *= aspy;
590                 }
591         }
592 }
593
594 static void viewRedrawForce(const bContext *C, TransInfo *t)
595 {
596         if (t->options & CTX_GPENCIL_STROKES) {
597                 WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL);
598         }
599         else if (t->spacetype == SPACE_VIEW3D) {
600                 if (t->options & CTX_PAINT_CURVE) {
601                         wmWindow *window = CTX_wm_window(C);
602                         WM_paint_cursor_tag_redraw(window, t->ar);
603                 }
604                 else {
605                         /* Do we need more refined tags? */
606                         if (t->flag & T_POSE)
607                                 WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
608                         else
609                                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
610
611                         /* for realtime animation record - send notifiers recognised by animation editors */
612                         // XXX: is this notifier a lame duck?
613                         if ((t->animtimer) && IS_AUTOKEY_ON(t->scene))
614                                 WM_event_add_notifier(C, NC_OBJECT | ND_KEYS, NULL);
615
616                 }
617         }
618         else if (t->spacetype == SPACE_ACTION) {
619                 //SpaceAction *saction = (SpaceAction *)t->sa->spacedata.first;
620                 WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
621         }
622         else if (t->spacetype == SPACE_IPO) {
623                 //SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
624                 WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
625         }
626         else if (t->spacetype == SPACE_NLA) {
627                 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
628         }
629         else if (t->spacetype == SPACE_NODE) {
630                 //ED_area_tag_redraw(t->sa);
631                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_NODE_VIEW, NULL);
632         }
633         else if (t->spacetype == SPACE_SEQ) {
634                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, NULL);
635         }
636         else if (t->spacetype == SPACE_IMAGE) {
637                 if (t->options & CTX_MASK) {
638                         Mask *mask = CTX_data_edit_mask(C);
639
640                         WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
641                 }
642                 else if (t->options & CTX_PAINT_CURVE) {
643                         wmWindow *window = CTX_wm_window(C);
644                         WM_paint_cursor_tag_redraw(window, t->ar);
645                 }
646                 else {
647                         // XXX how to deal with lock?
648                         SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
649                         if (sima->lock) WM_event_add_notifier(C, NC_GEOM | ND_DATA, t->obedit->data);
650                         else ED_area_tag_redraw(t->sa);
651                 }
652         }
653         else if (t->spacetype == SPACE_CLIP) {
654                 SpaceClip *sc = (SpaceClip *)t->sa->spacedata.first;
655
656                 if (ED_space_clip_check_show_trackedit(sc)) {
657                         MovieClip *clip = ED_space_clip_get_clip(sc);
658
659                         /* objects could be parented to tracking data, so send this for viewport refresh */
660                         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
661
662                         WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
663                 }
664                 else if (ED_space_clip_check_show_maskedit(sc)) {
665                         Mask *mask = CTX_data_edit_mask(C);
666
667                         WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
668                 }
669         }
670 }
671
672 static void viewRedrawPost(bContext *C, TransInfo *t)
673 {
674         ED_area_headerprint(t->sa, NULL);
675         
676         if (t->spacetype == SPACE_VIEW3D) {
677                 /* if autokeying is enabled, send notifiers that keyframes were added */
678                 if (IS_AUTOKEY_ON(t->scene))
679                         WM_main_add_notifier(NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
680
681                 /* redraw UV editor */
682                 if (ELEM(t->mode, TFM_VERT_SLIDE, TFM_EDGE_SLIDE) &&
683                     (t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT))
684                 {
685                         WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
686                 }
687                 
688                 /* XXX temp, first hack to get auto-render in compositor work (ton) */
689                 WM_event_add_notifier(C, NC_SCENE | ND_TRANSFORM_DONE, CTX_data_scene(C));
690
691         }
692         
693 #if 0 // TRANSFORM_FIX_ME
694         if (t->spacetype == SPACE_VIEW3D) {
695                 allqueue(REDRAWBUTSOBJECT, 0);
696                 allqueue(REDRAWVIEW3D, 0);
697         }
698         else if (t->spacetype == SPACE_IMAGE) {
699                 allqueue(REDRAWIMAGE, 0);
700                 allqueue(REDRAWVIEW3D, 0);
701         }
702         else if (ELEM(t->spacetype, SPACE_ACTION, SPACE_NLA, SPACE_IPO)) {
703                 allqueue(REDRAWVIEW3D, 0);
704                 allqueue(REDRAWACTION, 0);
705                 allqueue(REDRAWNLA, 0);
706                 allqueue(REDRAWIPO, 0);
707                 allqueue(REDRAWTIME, 0);
708                 allqueue(REDRAWBUTSOBJECT, 0);
709         }
710
711         scrarea_queue_headredraw(curarea);
712 #endif
713 }
714
715 /* ************************** TRANSFORMATIONS **************************** */
716
717 static void view_editmove(unsigned short UNUSED(event))
718 {
719 #if 0 // TRANSFORM_FIX_ME
720         int refresh = 0;
721         /* Regular:   Zoom in */
722         /* Shift:     Scroll up */
723         /* Ctrl:      Scroll right */
724         /* Alt-Shift: Rotate up */
725         /* Alt-Ctrl:  Rotate right */
726
727         /* only work in 3D window for now
728          * In the end, will have to send to event to a 2D window handler instead
729          */
730         if (Trans.flag & T_2D_EDIT)
731                 return;
732
733         switch (event) {
734                 case WHEELUPMOUSE:
735                         if (G.qual & LR_SHIFTKEY) {
736                                 if (G.qual & LR_ALTKEY) {
737                                         G.qual &= ~LR_SHIFTKEY;
738                                         persptoetsen(PAD2);
739                                         G.qual |= LR_SHIFTKEY;
740                                 }
741                                 else {
742                                         persptoetsen(PAD2);
743                                 }
744                         }
745                         else if (G.qual & LR_CTRLKEY) {
746                                 if (G.qual & LR_ALTKEY) {
747                                         G.qual &= ~LR_CTRLKEY;
748                                         persptoetsen(PAD4);
749                                         G.qual |= LR_CTRLKEY;
750                                 }
751                                 else {
752                                         persptoetsen(PAD4);
753                                 }
754                         }
755                         else if (U.uiflag & USER_WHEELZOOMDIR)
756                                 persptoetsen(PADMINUS);
757                         else
758                                 persptoetsen(PADPLUSKEY);
759
760                         refresh = 1;
761                         break;
762                 case WHEELDOWNMOUSE:
763                         if (G.qual & LR_SHIFTKEY) {
764                                 if (G.qual & LR_ALTKEY) {
765                                         G.qual &= ~LR_SHIFTKEY;
766                                         persptoetsen(PAD8);
767                                         G.qual |= LR_SHIFTKEY;
768                                 }
769                                 else {
770                                         persptoetsen(PAD8);
771                                 }
772                         }
773                         else if (G.qual & LR_CTRLKEY) {
774                                 if (G.qual & LR_ALTKEY) {
775                                         G.qual &= ~LR_CTRLKEY;
776                                         persptoetsen(PAD6);
777                                         G.qual |= LR_CTRLKEY;
778                                 }
779                                 else {
780                                         persptoetsen(PAD6);
781                                 }
782                         }
783                         else if (U.uiflag & USER_WHEELZOOMDIR)
784                                 persptoetsen(PADPLUSKEY);
785                         else
786                                 persptoetsen(PADMINUS);
787
788                         refresh = 1;
789                         break;
790         }
791
792         if (refresh)
793                 setTransformViewMatrices(&Trans);
794 #endif
795 }
796
797 /* ************************************************* */
798
799 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
800 enum {
801         TFM_MODAL_CANCEL         = 1,
802         TFM_MODAL_CONFIRM        = 2,
803         TFM_MODAL_TRANSLATE      = 3,
804         TFM_MODAL_ROTATE         = 4,
805         TFM_MODAL_RESIZE         = 5,
806         TFM_MODAL_SNAP_INV_ON    = 6,
807         TFM_MODAL_SNAP_INV_OFF   = 7,
808         TFM_MODAL_SNAP_TOGGLE    = 8,
809         TFM_MODAL_AXIS_X         = 9,
810         TFM_MODAL_AXIS_Y         = 10,
811         TFM_MODAL_AXIS_Z         = 11,
812         TFM_MODAL_PLANE_X        = 12,
813         TFM_MODAL_PLANE_Y        = 13,
814         TFM_MODAL_PLANE_Z        = 14,
815         TFM_MODAL_CONS_OFF       = 15,
816         TFM_MODAL_ADD_SNAP       = 16,
817         TFM_MODAL_REMOVE_SNAP    = 17,
818
819 /*      18 and 19 used by numinput, defined in transform.h */
820
821         TFM_MODAL_PROPSIZE_UP    = 20,
822         TFM_MODAL_PROPSIZE_DOWN  = 21,
823         TFM_MODAL_AUTOIK_LEN_INC = 22,
824         TFM_MODAL_AUTOIK_LEN_DEC = 23,
825
826         TFM_MODAL_EDGESLIDE_UP   = 24,
827         TFM_MODAL_EDGESLIDE_DOWN = 25,
828
829 /* for analog input, like trackpad */
830         TFM_MODAL_PROPSIZE       = 26,
831 };
832
833 /* called in transform_ops.c, on each regeneration of keymaps */
834 wmKeyMap *transform_modal_keymap(wmKeyConfig *keyconf)
835 {
836         static EnumPropertyItem modal_items[] = {
837                 {TFM_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
838                 {TFM_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
839                 {TFM_MODAL_TRANSLATE, "TRANSLATE", 0, "Translate", ""},
840                 {TFM_MODAL_ROTATE, "ROTATE", 0, "Rotate", ""},
841                 {TFM_MODAL_RESIZE, "RESIZE", 0, "Resize", ""},
842                 {TFM_MODAL_SNAP_INV_ON, "SNAP_INV_ON", 0, "Invert Snap On", ""},
843                 {TFM_MODAL_SNAP_INV_OFF, "SNAP_INV_OFF", 0, "Invert Snap Off", ""},
844                 {TFM_MODAL_SNAP_TOGGLE, "SNAP_TOGGLE", 0, "Snap Toggle", ""},
845                 {TFM_MODAL_AXIS_X, "AXIS_X", 0, "Orientation X axis", ""},
846                 {TFM_MODAL_AXIS_Y, "AXIS_Y", 0, "Orientation Y axis", ""},
847                 {TFM_MODAL_AXIS_Z, "AXIS_Z", 0, "Orientation Z axis", ""},
848                 {TFM_MODAL_PLANE_X, "PLANE_X", 0, "Orientation X plane", ""},
849                 {TFM_MODAL_PLANE_Y, "PLANE_Y", 0, "Orientation Y plane", ""},
850                 {TFM_MODAL_PLANE_Z, "PLANE_Z", 0, "Orientation Z plane", ""},
851                 {TFM_MODAL_CONS_OFF, "CONS_OFF", 0, "Remove Constraints", ""},
852                 {TFM_MODAL_ADD_SNAP, "ADD_SNAP", 0, "Add Snap Point", ""},
853                 {TFM_MODAL_REMOVE_SNAP, "REMOVE_SNAP", 0, "Remove Last Snap Point", ""},
854                 {NUM_MODAL_INCREMENT_UP, "INCREMENT_UP", 0, "Numinput Increment Up", ""},
855                 {NUM_MODAL_INCREMENT_DOWN, "INCREMENT_DOWN", 0, "Numinput Increment Down", ""},
856                 {TFM_MODAL_PROPSIZE_UP, "PROPORTIONAL_SIZE_UP", 0, "Increase Proportional Influence", ""},
857                 {TFM_MODAL_PROPSIZE_DOWN, "PROPORTIONAL_SIZE_DOWN", 0, "Decrease Proportional Influence", ""},
858                 {TFM_MODAL_AUTOIK_LEN_INC, "AUTOIK_CHAIN_LEN_UP", 0, "Increase Max AutoIK Chain Length", ""},
859                 {TFM_MODAL_AUTOIK_LEN_DEC, "AUTOIK_CHAIN_LEN_DOWN", 0, "Decrease Max AutoIK Chain Length", ""},
860                 {TFM_MODAL_EDGESLIDE_UP, "EDGESLIDE_EDGE_NEXT", 0, "Select next Edge Slide Edge", ""},
861                 {TFM_MODAL_EDGESLIDE_DOWN, "EDGESLIDE_PREV_NEXT", 0, "Select previous Edge Slide Edge", ""},
862                 {TFM_MODAL_PROPSIZE, "PROPORTIONAL_SIZE", 0, "Adjust Proportional Influence", ""},
863                 {0, NULL, 0, NULL, NULL}
864         };
865         
866         wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "Transform Modal Map");
867         
868         /* this function is called for each spacetype, only needs to add map once */
869         if (keymap && keymap->modal_items) return NULL;
870         
871         keymap = WM_modalkeymap_add(keyconf, "Transform Modal Map", modal_items);
872         
873         /* items for modal map */
874         WM_modalkeymap_add_item(keymap, ESCKEY,    KM_PRESS, KM_ANY, 0, TFM_MODAL_CANCEL);
875         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
876         WM_modalkeymap_add_item(keymap, RETKEY,    KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
877         WM_modalkeymap_add_item(keymap, PADENTER,  KM_PRESS, KM_ANY, 0, TFM_MODAL_CONFIRM);
878
879         WM_modalkeymap_add_item(keymap, GKEY, KM_PRESS, 0, 0, TFM_MODAL_TRANSLATE);
880         WM_modalkeymap_add_item(keymap, RKEY, KM_PRESS, 0, 0, TFM_MODAL_ROTATE);
881         WM_modalkeymap_add_item(keymap, SKEY, KM_PRESS, 0, 0, TFM_MODAL_RESIZE);
882         
883         WM_modalkeymap_add_item(keymap, TABKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_SNAP_TOGGLE);
884
885         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_SNAP_INV_ON);
886         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, TFM_MODAL_SNAP_INV_OFF);
887
888         WM_modalkeymap_add_item(keymap, RIGHTCTRLKEY, KM_PRESS, KM_ANY, 0, TFM_MODAL_SNAP_INV_ON);
889         WM_modalkeymap_add_item(keymap, RIGHTCTRLKEY, KM_RELEASE, KM_ANY, 0, TFM_MODAL_SNAP_INV_OFF);
890         
891         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, 0, 0, TFM_MODAL_ADD_SNAP);
892         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, KM_ALT, 0, TFM_MODAL_REMOVE_SNAP);
893
894         WM_modalkeymap_add_item(keymap, PAGEUPKEY, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_UP);
895         WM_modalkeymap_add_item(keymap, PAGEDOWNKEY, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_DOWN);
896         WM_modalkeymap_add_item(keymap, PAGEUPKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_PROPSIZE_UP);
897         WM_modalkeymap_add_item(keymap, PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_PROPSIZE_DOWN);
898         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_UP);
899         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, 0, 0, TFM_MODAL_PROPSIZE_DOWN);
900         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_PROPSIZE_UP);
901         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_PROPSIZE_DOWN);
902         WM_modalkeymap_add_item(keymap, MOUSEPAN, 0, 0, 0, TFM_MODAL_PROPSIZE);
903
904         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_ALT, 0, TFM_MODAL_EDGESLIDE_UP);
905         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_ALT, 0, TFM_MODAL_EDGESLIDE_DOWN);
906         
907         WM_modalkeymap_add_item(keymap, PAGEUPKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_INC);
908         WM_modalkeymap_add_item(keymap, PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_DEC);
909         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_INC);
910         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_SHIFT, 0, TFM_MODAL_AUTOIK_LEN_DEC);
911         
912         return keymap;
913 }
914
915 static void transform_event_xyz_constraint(TransInfo *t, short key_type, char cmode)
916 {
917         if (!(t->flag & T_NO_CONSTRAINT)) {
918                 int constraint_axis, constraint_plane;
919                 const bool edit_2d = (t->flag & T_2D_EDIT) != 0;
920                 const char *msg1 = "", *msg2 = "", *msg3 = "";
921                 char axis;
922         
923                 /* Initialize */
924                 switch (key_type) {
925                         case XKEY:
926                                 msg1 = IFACE_("along X");
927                                 msg2 = IFACE_("along %s X");
928                                 msg3 = IFACE_("locking %s X");
929                                 axis = 'X';
930                                 constraint_axis = CON_AXIS0;
931                                 break;
932                         case YKEY:
933                                 msg1 = IFACE_("along Y");
934                                 msg2 = IFACE_("along %s Y");
935                                 msg3 = IFACE_("locking %s Y");
936                                 axis = 'Y';
937                                 constraint_axis = CON_AXIS1;
938                                 break;
939                         case ZKEY:
940                                 msg1 = IFACE_("along Z");
941                                 msg2 = IFACE_("along %s Z");
942                                 msg3 = IFACE_("locking %s Z");
943                                 axis = 'Z';
944                                 constraint_axis = CON_AXIS2;
945                                 break;
946                         default:
947                                 /* Invalid key */
948                                 return;
949                 }
950                 constraint_plane = ((CON_AXIS0 | CON_AXIS1 | CON_AXIS2) & (~constraint_axis));
951
952                 if (edit_2d && (key_type != ZKEY)) {
953                         if (cmode == axis) {
954                                 stopConstraint(t);
955                         }
956                         else {
957                                 setUserConstraint(t, V3D_MANIP_GLOBAL, constraint_axis, msg1);
958                         }
959                 }
960                 else if (!edit_2d) {
961                         if (cmode == axis) {
962                                 if (t->con.orientation != V3D_MANIP_GLOBAL) {
963                                         stopConstraint(t);
964                                 }
965                                 else {
966                                         short orientation = (t->current_orientation != V3D_MANIP_GLOBAL ?
967                                                              t->current_orientation : V3D_MANIP_LOCAL);
968                                         if (!(t->modifiers & MOD_CONSTRAINT_PLANE))
969                                                 setUserConstraint(t, orientation, constraint_axis, msg2);
970                                         else if (t->modifiers & MOD_CONSTRAINT_PLANE)
971                                                 setUserConstraint(t, orientation, constraint_plane, msg3);
972                                 }
973                         }
974                         else {
975                                 if (!(t->modifiers & MOD_CONSTRAINT_PLANE))
976                                         setUserConstraint(t, V3D_MANIP_GLOBAL, constraint_axis, msg2);
977                                 else if (t->modifiers & MOD_CONSTRAINT_PLANE)
978                                         setUserConstraint(t, V3D_MANIP_GLOBAL, constraint_plane, msg3);
979                         }
980                 }
981                 t->redraw |= TREDRAW_HARD;
982         }
983 }
984
985 int transformEvent(TransInfo *t, const wmEvent *event)
986 {
987         char cmode = constraintModeToChar(t);
988         bool handled = false;
989         const int modifiers_prev = t->modifiers;
990
991         t->redraw |= handleMouseInput(t, &t->mouse, event);
992
993         /* Handle modal numinput events first, if already activated. */
994         if (((event->val == KM_PRESS) || (event->type == EVT_MODAL_MAP)) &&
995             hasNumInput(&t->num) && handleNumInput(t->context, &(t->num), event))
996         {
997                 t->redraw |= TREDRAW_HARD;
998                 handled = true;
999         }
1000         else if (event->type == MOUSEMOVE) {
1001                 if (t->modifiers & MOD_CONSTRAINT_SELECT)
1002                         t->con.mode |= CON_SELECT;
1003
1004                 copy_v2_v2_int(t->mval, event->mval);
1005
1006                 // t->redraw |= TREDRAW_SOFT; /* Use this for soft redraw. Might cause flicker in object mode */
1007                 t->redraw |= TREDRAW_HARD;
1008
1009                 if (t->state == TRANS_STARTING) {
1010                         t->state = TRANS_RUNNING;
1011                 }
1012
1013                 applyMouseInput(t, &t->mouse, t->mval, t->values);
1014
1015                 // Snapping mouse move events
1016                 t->redraw |= handleSnapping(t, event);
1017                 handled = true;
1018         }
1019         /* handle modal keymap first */
1020         else if (event->type == EVT_MODAL_MAP) {
1021                 switch (event->val) {
1022                         case TFM_MODAL_CANCEL:
1023                                 t->state = TRANS_CANCEL;
1024                                 handled = true;
1025                                 break;
1026                         case TFM_MODAL_CONFIRM:
1027                                 t->state = TRANS_CONFIRM;
1028                                 handled = true;
1029                                 break;
1030                         case TFM_MODAL_TRANSLATE:
1031                                 /* only switch when... */
1032                                 if (ELEM(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_EDGE_SLIDE, TFM_VERT_SLIDE)) {
1033                                         resetTransModal(t);
1034                                         resetTransRestrictions(t);
1035                                         restoreTransObjects(t);
1036                                         initTranslation(t);
1037                                         initSnapping(t, NULL); // need to reinit after mode change
1038                                         t->redraw |= TREDRAW_HARD;
1039                                         WM_event_add_mousemove(t->context);
1040                                         handled = true;
1041                                 }
1042                                 else if (t->mode == TFM_SEQ_SLIDE) {
1043                                         t->flag ^= T_ALT_TRANSFORM;
1044                                         t->redraw |= TREDRAW_HARD;
1045                                         handled = true;
1046                                 }
1047                                 else {
1048                                         if (t->obedit && t->obedit->type == OB_MESH) {
1049                                                 if ((t->mode == TFM_TRANSLATION) && (t->spacetype == SPACE_VIEW3D)) {
1050                                                         resetTransModal(t);
1051                                                         resetTransRestrictions(t);
1052                                                         restoreTransObjects(t);
1053
1054                                                         /* first try edge slide */
1055                                                         initEdgeSlide(t);
1056                                                         /* if that fails, do vertex slide */
1057                                                         if (t->state == TRANS_CANCEL) {
1058                                                                 t->state = TRANS_STARTING;
1059                                                                 initVertSlide(t);
1060                                                         }
1061                                                         /* vert slide can fail on unconnected vertices (rare but possible) */
1062                                                         if (t->state == TRANS_CANCEL) {
1063                                                                 t->state = TRANS_STARTING;
1064                                                                 resetTransRestrictions(t);
1065                                                                 restoreTransObjects(t);
1066                                                                 initTranslation(t);
1067                                                         }
1068                                                         initSnapping(t, NULL); // need to reinit after mode change
1069                                                         t->redraw |= TREDRAW_HARD;
1070                                                         handled = true;
1071                                                         WM_event_add_mousemove(t->context);
1072                                                 }
1073                                         }
1074                                         else if (t->options & (CTX_MOVIECLIP | CTX_MASK)) {
1075                                                 if (t->mode == TFM_TRANSLATION) {
1076                                                         restoreTransObjects(t);
1077
1078                                                         t->flag ^= T_ALT_TRANSFORM;
1079                                                         t->redraw |= TREDRAW_HARD;
1080                                                         handled = true;
1081                                                 }
1082                                         }
1083                                 }
1084                                 break;
1085                         case TFM_MODAL_ROTATE:
1086                                 /* only switch when... */
1087                                 if (!(t->options & CTX_TEXTURE) && !(t->options & (CTX_MOVIECLIP | CTX_MASK))) {
1088                                         if (ELEM(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION, TFM_EDGE_SLIDE, TFM_VERT_SLIDE)) {
1089                                                 resetTransModal(t);
1090                                                 resetTransRestrictions(t);
1091                                                 
1092                                                 if (t->mode == TFM_ROTATION) {
1093                                                         restoreTransObjects(t);
1094                                                         initTrackball(t);
1095                                                 }
1096                                                 else {
1097                                                         restoreTransObjects(t);
1098                                                         initRotation(t);
1099                                                 }
1100                                                 initSnapping(t, NULL); // need to reinit after mode change
1101                                                 t->redraw |= TREDRAW_HARD;
1102                                                 handled = true;
1103                                         }
1104                                 }
1105                                 break;
1106                         case TFM_MODAL_RESIZE:
1107                                 /* only switch when... */
1108                                 if (ELEM(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL, TFM_EDGE_SLIDE, TFM_VERT_SLIDE)) {
1109
1110                                         /* Scale isn't normally very useful after extrude along normals, see T39756 */
1111                                         if ((t->con.mode & CON_APPLY) && (t->con.orientation == V3D_MANIP_NORMAL)) {
1112                                                 stopConstraint(t);
1113                                         }
1114
1115                                         resetTransModal(t);
1116                                         resetTransRestrictions(t);
1117                                         restoreTransObjects(t);
1118                                         initResize(t);
1119                                         initSnapping(t, NULL); // need to reinit after mode change
1120                                         t->redraw |= TREDRAW_HARD;
1121                                         handled = true;
1122                                 }
1123                                 else if (t->mode == TFM_SHRINKFATTEN) {
1124                                         t->flag ^= T_ALT_TRANSFORM;
1125                                         t->redraw |= TREDRAW_HARD;
1126                                         handled = true;
1127                                 }
1128                                 else if (t->mode == TFM_RESIZE) {
1129                                         if (t->options & CTX_MOVIECLIP) {
1130                                                 restoreTransObjects(t);
1131
1132                                                 t->flag ^= T_ALT_TRANSFORM;
1133                                                 t->redraw |= TREDRAW_HARD;
1134                                                 handled = true;
1135                                         }
1136                                 }
1137                                 break;
1138                                 
1139                         case TFM_MODAL_SNAP_INV_ON:
1140                                 t->modifiers |= MOD_SNAP_INVERT;
1141                                 t->redraw |= TREDRAW_HARD;
1142                                 handled = true;
1143                                 break;
1144                         case TFM_MODAL_SNAP_INV_OFF:
1145                                 t->modifiers &= ~MOD_SNAP_INVERT;
1146                                 t->redraw |= TREDRAW_HARD;
1147                                 handled = true;
1148                                 break;
1149                         case TFM_MODAL_SNAP_TOGGLE:
1150                                 t->modifiers ^= MOD_SNAP;
1151                                 t->redraw |= TREDRAW_HARD;
1152                                 handled = true;
1153                                 break;
1154                         case TFM_MODAL_AXIS_X:
1155                                 if (!(t->flag & T_NO_CONSTRAINT)) {
1156                                         transform_event_xyz_constraint(t, XKEY, cmode);
1157                                         t->redraw |= TREDRAW_HARD;
1158                                         handled = true;
1159                                 }
1160                                 break;
1161                         case TFM_MODAL_AXIS_Y:
1162                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1163                                         transform_event_xyz_constraint(t, YKEY, cmode);
1164                                         t->redraw |= TREDRAW_HARD;
1165                                         handled = true;
1166                                 }
1167                                 break;
1168                         case TFM_MODAL_AXIS_Z:
1169                                 if ((t->flag & (T_NO_CONSTRAINT)) == 0) {
1170                                         transform_event_xyz_constraint(t, ZKEY, cmode);
1171                                         t->redraw |= TREDRAW_HARD;
1172                                         handled = true;
1173                                 }
1174                                 break;
1175                         case TFM_MODAL_PLANE_X:
1176                                 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1177                                         if (cmode == 'X') {
1178                                                 stopConstraint(t);
1179                                         }
1180                                         else {
1181                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS1 | CON_AXIS2), IFACE_("locking %s X"));
1182                                         }
1183                                         t->redraw |= TREDRAW_HARD;
1184                                         handled = true;
1185                                 }
1186                                 break;
1187                         case TFM_MODAL_PLANE_Y:
1188                                 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1189                                         if (cmode == 'Y') {
1190                                                 stopConstraint(t);
1191                                         }
1192                                         else {
1193                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0 | CON_AXIS2), IFACE_("locking %s Y"));
1194                                         }
1195                                         t->redraw |= TREDRAW_HARD;
1196                                         handled = true;
1197                                 }
1198                                 break;
1199                         case TFM_MODAL_PLANE_Z:
1200                                 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1201                                         if (cmode == 'Z') {
1202                                                 stopConstraint(t);
1203                                         }
1204                                         else {
1205                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0 | CON_AXIS1), IFACE_("locking %s Z"));
1206                                         }
1207                                         t->redraw |= TREDRAW_HARD;
1208                                         handled = true;
1209                                 }
1210                                 break;
1211                         case TFM_MODAL_CONS_OFF:
1212                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1213                                         stopConstraint(t);
1214                                         t->redraw |= TREDRAW_HARD;
1215                                         handled = true;
1216                                 }
1217                                 break;
1218                         case TFM_MODAL_ADD_SNAP:
1219                                 addSnapPoint(t);
1220                                 t->redraw |= TREDRAW_HARD;
1221                                 handled = true;
1222                                 break;
1223                         case TFM_MODAL_REMOVE_SNAP:
1224                                 removeSnapPoint(t);
1225                                 t->redraw |= TREDRAW_HARD;
1226                                 handled = true;
1227                                 break;
1228                         case TFM_MODAL_PROPSIZE:
1229                                 /* MOUSEPAN usage... */
1230                                 if (t->flag & T_PROP_EDIT) {
1231                                         float fac = 1.0f + 0.005f *(event->y - event->prevy);
1232                                         t->prop_size *= fac;
1233                                         if (t->spacetype == SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1234                                                 t->prop_size = min_ff(t->prop_size, ((View3D *)t->view)->far);
1235                                         calculatePropRatio(t);
1236                                         t->redraw |= TREDRAW_HARD;
1237                                         handled = true;
1238                                 }
1239                                 break;
1240                         case TFM_MODAL_PROPSIZE_UP:
1241                                 if (t->flag & T_PROP_EDIT) {
1242                                         t->prop_size *= (t->modifiers & MOD_PRECISION) ? 1.01f : 1.1f;
1243                                         if (t->spacetype == SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1244                                                 t->prop_size = min_ff(t->prop_size, ((View3D *)t->view)->far);
1245                                         calculatePropRatio(t);
1246                                         t->redraw |= TREDRAW_HARD;
1247                                         handled = true;
1248                                 }
1249                                 break;
1250                         case TFM_MODAL_PROPSIZE_DOWN:
1251                                 if (t->flag & T_PROP_EDIT) {
1252                                         t->prop_size /= (t->modifiers & MOD_PRECISION) ? 1.01f : 1.1f;
1253                                         calculatePropRatio(t);
1254                                         t->redraw |= TREDRAW_HARD;
1255                                         handled = true;
1256                                 }
1257                                 break;
1258                         case TFM_MODAL_AUTOIK_LEN_INC:
1259                                 if (t->flag & T_AUTOIK) {
1260                                         transform_autoik_update(t, 1);
1261                                         t->redraw |= TREDRAW_HARD;
1262                                         handled = true;
1263                                 }
1264                                 break;
1265                         case TFM_MODAL_AUTOIK_LEN_DEC:
1266                                 if (t->flag & T_AUTOIK) {
1267                                         transform_autoik_update(t, -1);
1268                                         t->redraw |= TREDRAW_HARD;
1269                                         handled = true;
1270                                 }
1271                                 break;
1272                         /* Those two are only handled in transform's own handler, see T44634! */
1273                         case TFM_MODAL_EDGESLIDE_UP:
1274                         case TFM_MODAL_EDGESLIDE_DOWN:
1275                         default:
1276                                 break;
1277                 }
1278         }
1279         /* else do non-mapped events */
1280         else if (event->val == KM_PRESS) {
1281                 switch (event->type) {
1282                         case RIGHTMOUSE:
1283                                 t->state = TRANS_CANCEL;
1284                                 handled = true;
1285                                 break;
1286                         /* enforce redraw of transform when modifiers are used */
1287                         case LEFTSHIFTKEY:
1288                         case RIGHTSHIFTKEY:
1289                                 t->modifiers |= MOD_CONSTRAINT_PLANE;
1290                                 t->redraw |= TREDRAW_HARD;
1291                                 handled = true;
1292                                 break;
1293
1294                         case SPACEKEY:
1295                                 t->state = TRANS_CONFIRM;
1296                                 handled = true;
1297                                 break;
1298
1299                         case MIDDLEMOUSE:
1300                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1301                                         /* exception for switching to dolly, or trackball, in camera view */
1302                                         if (t->flag & T_CAMERA) {
1303                                                 if (t->mode == TFM_TRANSLATION)
1304                                                         setLocalConstraint(t, (CON_AXIS2), IFACE_("along local Z"));
1305                                                 else if (t->mode == TFM_ROTATION) {
1306                                                         restoreTransObjects(t);
1307                                                         initTrackball(t);
1308                                                 }
1309                                         }
1310                                         else {
1311                                                 t->modifiers |= MOD_CONSTRAINT_SELECT;
1312                                                 if (t->con.mode & CON_APPLY) {
1313                                                         stopConstraint(t);
1314                                                 }
1315                                                 else {
1316                                                         if (event->shift) {
1317                                                                 initSelectConstraint(t, t->spacemtx);
1318                                                         }
1319                                                         else {
1320                                                                 /* bit hackish... but it prevents mmb select to print the orientation from menu */
1321                                                                 float mati[3][3];
1322                                                                 strcpy(t->spacename, "global");
1323                                                                 unit_m3(mati);
1324                                                                 initSelectConstraint(t, mati);
1325                                                         }
1326                                                         postSelectConstraint(t);
1327                                                 }
1328                                         }
1329                                         t->redraw |= TREDRAW_HARD;
1330                                         handled = true;
1331                                 }
1332                                 break;
1333                         case ESCKEY:
1334                                 t->state = TRANS_CANCEL;
1335                                 handled = true;
1336                                 break;
1337                         case PADENTER:
1338                         case RETKEY:
1339                                 t->state = TRANS_CONFIRM;
1340                                 handled = true;
1341                                 break;
1342                         case GKEY:
1343                                 /* only switch when... */
1344                                 if (ELEM(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL)) {
1345                                         resetTransModal(t);
1346                                         resetTransRestrictions(t);
1347                                         restoreTransObjects(t);
1348                                         initTranslation(t);
1349                                         initSnapping(t, NULL); // need to reinit after mode change
1350                                         t->redraw |= TREDRAW_HARD;
1351                                         handled = true;
1352                                 }
1353                                 break;
1354                         case SKEY:
1355                                 /* only switch when... */
1356                                 if (ELEM(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL)) {
1357                                         resetTransModal(t);
1358                                         resetTransRestrictions(t);
1359                                         restoreTransObjects(t);
1360                                         initResize(t);
1361                                         initSnapping(t, NULL); // need to reinit after mode change
1362                                         t->redraw |= TREDRAW_HARD;
1363                                         handled = true;
1364                                 }
1365                                 break;
1366                         case RKEY:
1367                                 /* only switch when... */
1368                                 if (!(t->options & CTX_TEXTURE)) {
1369                                         if (ELEM(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION)) {
1370                                                 resetTransModal(t);
1371                                                 resetTransRestrictions(t);
1372
1373                                                 if (t->mode == TFM_ROTATION) {
1374                                                         restoreTransObjects(t);
1375                                                         initTrackball(t);
1376                                                 }
1377                                                 else {
1378                                                         restoreTransObjects(t);
1379                                                         initRotation(t);
1380                                                 }
1381                                                 initSnapping(t, NULL); // need to reinit after mode change
1382                                                 t->redraw |= TREDRAW_HARD;
1383                                                 handled = true;
1384                                         }
1385                                 }
1386                                 break;
1387                         case CKEY:
1388                                 if (event->alt) {
1389                                         if (!(t->options & CTX_NO_PET)) {
1390                                                 t->flag ^= T_PROP_CONNECTED;
1391                                                 sort_trans_data_dist(t);
1392                                                 calculatePropRatio(t);
1393                                                 t->redraw = TREDRAW_HARD;
1394                                                 handled = true;
1395                                         }
1396                                 }
1397                                 else {
1398                                         if (!(t->flag & T_NO_CONSTRAINT)) {
1399                                                 stopConstraint(t);
1400                                                 t->redraw |= TREDRAW_HARD;
1401                                                 handled = true;
1402                                         }
1403                                 }
1404                                 break;
1405                         case XKEY:
1406                         case YKEY:
1407                         case ZKEY:
1408                                 if (!(t->flag & T_NO_CONSTRAINT)) {
1409                                         transform_event_xyz_constraint(t, event->type, cmode);
1410                                         handled = true;
1411                                 }
1412                                 break;
1413                         case OKEY:
1414                                 if (t->flag & T_PROP_EDIT && event->shift) {
1415                                         t->prop_mode = (t->prop_mode + 1) % PROP_MODE_MAX;
1416                                         calculatePropRatio(t);
1417                                         t->redraw |= TREDRAW_HARD;
1418                                         handled = true;
1419                                 }
1420                                 break;
1421                         case PADPLUSKEY:
1422                                 if (event->alt && t->flag & T_PROP_EDIT) {
1423                                         t->prop_size *= (t->modifiers & MOD_PRECISION) ? 1.01f : 1.1f;
1424                                         if (t->spacetype == SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1425                                                 t->prop_size = min_ff(t->prop_size, ((View3D *)t->view)->far);
1426                                         calculatePropRatio(t);
1427                                         t->redraw = TREDRAW_HARD;
1428                                         handled = true;
1429                                 }
1430                                 break;
1431                         case PAGEUPKEY:
1432                         case WHEELDOWNMOUSE:
1433                                 if (t->flag & T_AUTOIK) {
1434                                         transform_autoik_update(t, 1);
1435                                 }
1436                                 else {
1437                                         view_editmove(event->type);
1438                                 }
1439                                 t->redraw = TREDRAW_HARD;
1440                                 handled = true;
1441                                 break;
1442                         case PADMINUS:
1443                                 if (event->alt && t->flag & T_PROP_EDIT) {
1444                                         t->prop_size /= (t->modifiers & MOD_PRECISION) ? 1.01f : 1.1f;
1445                                         calculatePropRatio(t);
1446                                         t->redraw = TREDRAW_HARD;
1447                                         handled = true;
1448                                 }
1449                                 break;
1450                         case PAGEDOWNKEY:
1451                         case WHEELUPMOUSE:
1452                                 if (t->flag & T_AUTOIK) {
1453                                         transform_autoik_update(t, -1);
1454                                 }
1455                                 else {
1456                                         view_editmove(event->type);
1457                                 }
1458                                 t->redraw = TREDRAW_HARD;
1459                                 handled = true;
1460                                 break;
1461                         case LEFTALTKEY:
1462                         case RIGHTALTKEY:
1463                                 if (ELEM(t->spacetype, SPACE_SEQ, SPACE_VIEW3D)) {
1464                                         t->flag |= T_ALT_TRANSFORM;
1465                                         t->redraw |= TREDRAW_HARD;
1466                                         handled = true;
1467                                 }
1468                                 break;
1469                         default:
1470                                 break;
1471                 }
1472
1473                 /* Snapping key events */
1474                 t->redraw |= handleSnapping(t, event);
1475         }
1476         else if (event->val == KM_RELEASE) {
1477                 switch (event->type) {
1478                         case LEFTSHIFTKEY:
1479                         case RIGHTSHIFTKEY:
1480                                 t->modifiers &= ~MOD_CONSTRAINT_PLANE;
1481                                 t->redraw |= TREDRAW_HARD;
1482                                 handled = true;
1483                                 break;
1484
1485                         case MIDDLEMOUSE:
1486                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1487                                         t->modifiers &= ~MOD_CONSTRAINT_SELECT;
1488                                         postSelectConstraint(t);
1489                                         t->redraw |= TREDRAW_HARD;
1490                                         handled = true;
1491                                 }
1492                                 break;
1493                         case LEFTALTKEY:
1494                         case RIGHTALTKEY:
1495                                 if (ELEM(t->spacetype, SPACE_SEQ, SPACE_VIEW3D)) {
1496                                         t->flag &= ~T_ALT_TRANSFORM;
1497                                         t->redraw |= TREDRAW_HARD;
1498                                         handled = true;
1499                                 }
1500                                 break;
1501                         default:
1502                                 break;
1503                 }
1504
1505                 /* confirm transform if launch key is released after mouse move */
1506                 if (t->flag & T_RELEASE_CONFIRM) {
1507                         /* XXX Keyrepeat bug in Xorg messes this up, will test when fixed */
1508                         if (event->type == t->launch_event && (t->launch_event == LEFTMOUSE || t->launch_event == RIGHTMOUSE)) {
1509                                 t->state = TRANS_CONFIRM;
1510                         }
1511                 }
1512         }
1513
1514         /* if we change snap options, get the unsnapped values back */
1515         if ((t->modifiers   & (MOD_SNAP | MOD_SNAP_INVERT)) !=
1516             (modifiers_prev & (MOD_SNAP | MOD_SNAP_INVERT)))
1517         {
1518                 applyMouseInput(t, &t->mouse, t->mval, t->values);
1519         }
1520
1521         /* Per transform event, if present */
1522         if (t->handleEvent &&
1523             (!handled ||
1524              /* Needed for vertex slide, see [#38756] */
1525              (event->type == MOUSEMOVE)))
1526         {
1527                 t->redraw |= t->handleEvent(t, event);
1528         }
1529
1530         /* Try to init modal numinput now, if possible. */
1531         if (!(handled || t->redraw) && ((event->val == KM_PRESS) || (event->type == EVT_MODAL_MAP)) &&
1532             handleNumInput(t->context, &(t->num), event))
1533         {
1534                 t->redraw |= TREDRAW_HARD;
1535                 handled = true;
1536         }
1537
1538         if (handled || t->redraw) {
1539                 return 0;
1540         }
1541         else {
1542                 return OPERATOR_PASS_THROUGH;
1543         }
1544 }
1545
1546 bool calculateTransformCenter(bContext *C, int centerMode, float cent3d[3], float cent2d[2])
1547 {
1548         TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
1549         bool success;
1550
1551         t->state = TRANS_RUNNING;
1552
1553         /* avoid calculating PET */
1554         t->options = CTX_NO_PET;
1555
1556         t->mode = TFM_DUMMY;
1557
1558         initTransInfo(C, t, NULL, NULL);
1559
1560         /* avoid doing connectivity lookups (when V3D_LOCAL is set) */
1561         t->around = V3D_CENTER;
1562
1563         createTransData(C, t);              // make TransData structs from selection
1564
1565         t->around = centerMode;             // override userdefined mode
1566
1567         if (t->total == 0) {
1568                 success = false;
1569         }
1570         else {
1571                 success = true;
1572
1573                 calculateCenter(t);
1574
1575                 if (cent2d) {
1576                         copy_v2_v2(cent2d, t->center2d);
1577                 }
1578
1579                 if (cent3d) {
1580                         // Copy center from constraint center. Transform center can be local
1581                         copy_v3_v3(cent3d, t->con.center);
1582                 }
1583         }
1584
1585
1586         /* aftertrans does insert keyframes, and clears base flags; doesn't read transdata */
1587         special_aftertrans_update(C, t);
1588
1589         postTrans(C, t);
1590
1591         MEM_freeN(t);
1592
1593         return success;
1594 }
1595
1596 typedef enum {
1597         UP,
1598         DOWN,
1599         LEFT,
1600         RIGHT
1601 } ArrowDirection;
1602 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1603 {
1604         switch (d) {
1605                 case LEFT:
1606                         offset = -offset;
1607                         length = -length;
1608                         size = -size;
1609                         /* fall-through */
1610                 case RIGHT:
1611                         glBegin(GL_LINES);
1612                         glVertex2s(offset, 0);
1613                         glVertex2s(offset + length, 0);
1614                         glVertex2s(offset + length, 0);
1615                         glVertex2s(offset + length - size, -size);
1616                         glVertex2s(offset + length, 0);
1617                         glVertex2s(offset + length - size,  size);
1618                         glEnd();
1619                         break;
1620
1621                 case DOWN:
1622                         offset = -offset;
1623                         length = -length;
1624                         size = -size;
1625                         /* fall-through */
1626                 case UP:
1627                         glBegin(GL_LINES);
1628                         glVertex2s(0, offset);
1629                         glVertex2s(0, offset + length);
1630                         glVertex2s(0, offset + length);
1631                         glVertex2s(-size, offset + length - size);
1632                         glVertex2s(0, offset + length);
1633                         glVertex2s(size, offset + length - size);
1634                         glEnd();
1635                         break;
1636         }
1637 }
1638
1639 static void drawArrowHead(ArrowDirection d, short size)
1640 {
1641         switch (d) {
1642                 case LEFT:
1643                         size = -size;
1644                         /* fall-through */
1645                 case RIGHT:
1646                         glBegin(GL_LINES);
1647                         glVertex2s(0, 0);
1648                         glVertex2s(-size, -size);
1649                         glVertex2s(0, 0);
1650                         glVertex2s(-size,  size);
1651                         glEnd();
1652                         break;
1653
1654                 case DOWN:
1655                         size = -size;
1656                         /* fall-through */
1657                 case UP:
1658                         glBegin(GL_LINES);
1659                         glVertex2s(0, 0);
1660                         glVertex2s(-size, -size);
1661                         glVertex2s(0, 0);
1662                         glVertex2s(size, -size);
1663                         glEnd();
1664                         break;
1665         }
1666 }
1667
1668 static void drawArc(float size, float angle_start, float angle_end, int segments)
1669 {
1670         float delta = (angle_end - angle_start) / segments;
1671         float angle;
1672         int a;
1673
1674         glBegin(GL_LINE_STRIP);
1675
1676         for (angle = angle_start, a = 0; a < segments; angle += delta, a++) {
1677                 glVertex2f(cosf(angle) * size, sinf(angle) * size);
1678         }
1679         glVertex2f(cosf(angle_end) * size, sinf(angle_end) * size);
1680
1681         glEnd();
1682 }
1683
1684 static int helpline_poll(bContext *C)
1685 {
1686         ARegion *ar = CTX_wm_region(C);
1687         
1688         if (ar && ar->regiontype == RGN_TYPE_WINDOW)
1689                 return 1;
1690         return 0;
1691 }
1692
1693 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1694 {
1695         TransInfo *t = (TransInfo *)customdata;
1696
1697         if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR)) {
1698                 float vecrot[3], cent[2];
1699                 int mval[2];
1700
1701                 mval[0] = x;
1702                 mval[1] = y;
1703
1704                 copy_v3_v3(vecrot, t->center);
1705                 if (t->flag & T_EDIT) {
1706                         Object *ob = t->obedit;
1707                         if (ob) mul_m4_v3(ob->obmat, vecrot);
1708                 }
1709                 else if (t->flag & T_POSE) {
1710                         Object *ob = t->poseobj;
1711                         if (ob) mul_m4_v3(ob->obmat, vecrot);
1712                 }
1713
1714                 projectFloatViewEx(t, vecrot, cent, V3D_PROJ_TEST_CLIP_ZERO);
1715
1716                 glPushMatrix();
1717
1718                 switch (t->helpline) {
1719                         case HLP_SPRING:
1720                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1721
1722                                 setlinestyle(3);
1723                                 glBegin(GL_LINE_STRIP);
1724                                 glVertex2iv(t->mval);
1725                                 glVertex2fv(cent);
1726                                 glEnd();
1727
1728                                 glTranslatef(mval[0], mval[1], 0);
1729                                 glRotatef(-RAD2DEGF(atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1])), 0, 0, 1);
1730
1731                                 setlinestyle(0);
1732                                 glLineWidth(3.0);
1733                                 drawArrow(UP, 5, 10, 5);
1734                                 drawArrow(DOWN, 5, 10, 5);
1735                                 glLineWidth(1.0);
1736                                 break;
1737                         case HLP_HARROW:
1738                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1739
1740                                 glTranslatef(mval[0], mval[1], 0);
1741
1742                                 glLineWidth(3.0);
1743                                 drawArrow(RIGHT, 5, 10, 5);
1744                                 drawArrow(LEFT, 5, 10, 5);
1745                                 glLineWidth(1.0);
1746                                 break;
1747                         case HLP_VARROW:
1748                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1749
1750                                 glTranslatef(mval[0], mval[1], 0);
1751
1752                                 glLineWidth(3.0);
1753                                 drawArrow(UP, 5, 10, 5);
1754                                 drawArrow(DOWN, 5, 10, 5);
1755                                 glLineWidth(1.0);
1756                                 break;
1757                         case HLP_ANGLE:
1758                         {
1759                                 float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1760                                 float angle = atan2f(dy, dx);
1761                                 float dist = hypotf(dx, dy);
1762                                 float delta_angle = min_ff(15.0f / dist, (float)M_PI / 4.0f);
1763                                 float spacing_angle = min_ff(5.0f / dist, (float)M_PI / 12.0f);
1764                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1765
1766                                 setlinestyle(3);
1767                                 glBegin(GL_LINE_STRIP);
1768                                 glVertex2iv(t->mval);
1769                                 glVertex2fv(cent);
1770                                 glEnd();
1771
1772                                 glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1773
1774                                 setlinestyle(0);
1775                                 glLineWidth(3.0);
1776                                 drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1777                                 drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1778
1779                                 glPushMatrix();
1780
1781                                 glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1782                                 glRotatef(RAD2DEGF(angle - delta_angle), 0, 0, 1);
1783
1784                                 drawArrowHead(DOWN, 5);
1785
1786                                 glPopMatrix();
1787
1788                                 glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1789                                 glRotatef(RAD2DEGF(angle + delta_angle), 0, 0, 1);
1790
1791                                 drawArrowHead(UP, 5);
1792
1793                                 glLineWidth(1.0);
1794                                 break;
1795                         }
1796                         case HLP_TRACKBALL:
1797                         {
1798                                 unsigned char col[3], col2[3];
1799                                 UI_GetThemeColor3ubv(TH_GRID, col);
1800
1801                                 glTranslatef(mval[0], mval[1], 0);
1802
1803                                 glLineWidth(3.0);
1804
1805                                 UI_make_axis_color(col, col2, 'X');
1806                                 glColor3ubv((GLubyte *)col2);
1807
1808                                 drawArrow(RIGHT, 5, 10, 5);
1809                                 drawArrow(LEFT, 5, 10, 5);
1810
1811                                 UI_make_axis_color(col, col2, 'Y');
1812                                 glColor3ubv((GLubyte *)col2);
1813
1814                                 drawArrow(UP, 5, 10, 5);
1815                                 drawArrow(DOWN, 5, 10, 5);
1816                                 glLineWidth(1.0);
1817                                 break;
1818                         }
1819                 }
1820
1821                 glPopMatrix();
1822         }
1823 }
1824
1825 static void drawTransformView(const struct bContext *C, ARegion *UNUSED(ar), void *arg)
1826 {
1827         TransInfo *t = arg;
1828
1829         drawConstraint(t);
1830         drawPropCircle(C, t);
1831         drawSnapping(C, t);
1832
1833         /* edge slide, vert slide */
1834         drawEdgeSlide(t);
1835         drawVertSlide(t);
1836 }
1837
1838 /* just draw a little warning message in the top-right corner of the viewport to warn that autokeying is enabled */
1839 static void drawAutoKeyWarning(TransInfo *UNUSED(t), ARegion *ar)
1840 {
1841         rcti rect;
1842         const char *printable = IFACE_("Auto Keying On");
1843         float      printable_size[2];
1844         int xco, yco;
1845
1846         ED_region_visible_rect(ar, &rect);
1847         
1848         BLF_width_and_height_default(printable, BLF_DRAW_STR_DUMMY_MAX, &printable_size[0], &printable_size[1]);
1849         
1850         xco = (rect.xmax - U.widget_unit) - (int)printable_size[0];
1851         yco = (rect.ymax - U.widget_unit);
1852         
1853         /* warning text (to clarify meaning of overlays)
1854          * - original color was red to match the icon, but that clashes badly with a less nasty border
1855          */
1856         UI_ThemeColorShade(TH_TEXT_HI, -50);
1857 #ifdef WITH_INTERNATIONAL
1858         BLF_draw_default(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1859 #else
1860         BLF_draw_default_ascii(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1861 #endif
1862         
1863         /* autokey recording icon... */
1864         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1865         glEnable(GL_BLEND);
1866         
1867         xco -= U.widget_unit;
1868         yco -= (int)printable_size[1] / 2;
1869
1870         UI_icon_draw(xco, yco, ICON_REC);
1871         
1872         glDisable(GL_BLEND);
1873 }
1874
1875 static void drawTransformPixel(const struct bContext *UNUSED(C), ARegion *ar, void *arg)
1876 {       
1877         TransInfo *t = arg;
1878         Scene *scene = t->scene;
1879         Object *ob = OBACT;
1880         
1881         /* draw autokeyframing hint in the corner 
1882          * - only draw if enabled (advanced users may be distracted/annoyed), 
1883          *   for objects that will be autokeyframed (no point ohterwise),
1884          *   AND only for the active region (as showing all is too overwhelming)
1885          */
1886         if ((U.autokey_flag & AUTOKEY_FLAG_NOWARNING) == 0) {
1887                 if (ar == t->ar) {
1888                         if (t->flag & (T_OBJECT | T_POSE)) {
1889                                 if (ob && autokeyframe_cfra_can_key(scene, &ob->id)) {
1890                                         drawAutoKeyWarning(t, ar);
1891                                 }
1892                         }
1893                 }
1894         }
1895 }
1896
1897 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1898 {
1899         ToolSettings *ts = CTX_data_tool_settings(C);
1900         int constraint_axis[3] = {0, 0, 0};
1901         int proportional = 0;
1902         PropertyRNA *prop;
1903
1904         // Save back mode in case we're in the generic operator
1905         if ((prop = RNA_struct_find_property(op->ptr, "mode"))) {
1906                 RNA_property_enum_set(op->ptr, prop, t->mode);
1907         }
1908
1909         if ((prop = RNA_struct_find_property(op->ptr, "value"))) {
1910                 const float *values = (t->flag & T_AUTOVALUES) ? t->auto_values : t->values;
1911                 if (RNA_property_array_check(prop)) {
1912                         RNA_property_float_set_array(op->ptr, prop, values);
1913                 }
1914                 else {
1915                         RNA_property_float_set(op->ptr, prop, values[0]);
1916                 }
1917         }
1918
1919         /* convert flag to enum */
1920         switch (t->flag & T_PROP_EDIT_ALL) {
1921                 case T_PROP_EDIT:
1922                         proportional = PROP_EDIT_ON;
1923                         break;
1924                 case (T_PROP_EDIT | T_PROP_CONNECTED):
1925                         proportional = PROP_EDIT_CONNECTED;
1926                         break;
1927                 case (T_PROP_EDIT | T_PROP_PROJECTED):
1928                         proportional = PROP_EDIT_PROJECTED;
1929                         break;
1930                 default:
1931                         proportional = PROP_EDIT_OFF;
1932                         break;
1933         }
1934
1935         // If modal, save settings back in scene if not set as operator argument
1936         if (t->flag & T_MODAL) {
1937                 /* save settings if not set in operator */
1938
1939                 /* skip saving proportional edit if it was not actually used */
1940                 if (!(t->options & CTX_NO_PET)) {
1941                         if ((prop = RNA_struct_find_property(op->ptr, "proportional")) &&
1942                             !RNA_property_is_set(op->ptr, prop))
1943                         {
1944                                 if (t->spacetype == SPACE_IPO)
1945                                         ts->proportional_fcurve = proportional;
1946                                 else if (t->spacetype == SPACE_ACTION)
1947                                         ts->proportional_action = proportional;
1948                                 else if (t->obedit)
1949                                         ts->proportional = proportional;
1950                                 else if (t->options & CTX_MASK)
1951                                         ts->proportional_mask = (proportional != PROP_EDIT_OFF);
1952                                 else
1953                                         ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1954                         }
1955
1956                         if ((prop = RNA_struct_find_property(op->ptr, "proportional_size")) &&
1957                             !RNA_property_is_set(op->ptr, prop))
1958                         {
1959                                 ts->proportional_size = t->prop_size;
1960                         }
1961
1962                         if ((prop = RNA_struct_find_property(op->ptr, "proportional_edit_falloff")) &&
1963                             !RNA_property_is_set(op->ptr, prop))
1964                         {
1965                                 ts->prop_mode = t->prop_mode;
1966                         }
1967                 }
1968                 
1969                 /* do we check for parameter? */
1970                 if (t->modifiers & MOD_SNAP) {
1971                         ts->snap_flag |= SCE_SNAP;
1972                 }
1973                 else {
1974                         ts->snap_flag &= ~SCE_SNAP;
1975                 }
1976
1977                 if (t->spacetype == SPACE_VIEW3D) {
1978                         if ((prop = RNA_struct_find_property(op->ptr, "constraint_orientation")) &&
1979                             !RNA_property_is_set(op->ptr, prop))
1980                         {
1981                                 View3D *v3d = t->view;
1982
1983                                 v3d->twmode = t->current_orientation;
1984                         }
1985                 }
1986         }
1987         
1988         if ((prop = RNA_struct_find_property(op->ptr, "proportional"))) {
1989                 RNA_property_enum_set(op->ptr, prop, proportional);
1990                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1991                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1992         }
1993
1994         if ((prop = RNA_struct_find_property(op->ptr, "axis"))) {
1995                 RNA_property_float_set_array(op->ptr, prop, t->axis);
1996         }
1997
1998         if ((prop = RNA_struct_find_property(op->ptr, "mirror"))) {
1999                 RNA_property_boolean_set(op->ptr, prop, (t->flag & T_MIRROR) != 0);
2000         }
2001
2002         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis"))) {
2003                 /* constraint orientation can be global, event if user selects something else
2004                  * so use the orientation in the constraint if set
2005                  * */
2006                 if (t->con.mode & CON_APPLY) {
2007                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
2008                 }
2009                 else {
2010                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
2011                 }
2012
2013                 if (t->con.mode & CON_APPLY) {
2014                         if (t->con.mode & CON_AXIS0) {
2015                                 constraint_axis[0] = 1;
2016                         }
2017                         if (t->con.mode & CON_AXIS1) {
2018                                 constraint_axis[1] = 1;
2019                         }
2020                         if (t->con.mode & CON_AXIS2) {
2021                                 constraint_axis[2] = 1;
2022                         }
2023                 }
2024
2025                 RNA_property_boolean_set_array(op->ptr, prop, constraint_axis);
2026         }
2027
2028         {
2029                 const char *prop_id = NULL;
2030                 if (t->mode == TFM_SHRINKFATTEN) {
2031                         prop_id = "use_even_offset";
2032                 }
2033
2034                 if (prop_id && (prop = RNA_struct_find_property(op->ptr, prop_id))) {
2035
2036                         RNA_property_boolean_set(op->ptr, prop, (t->flag & T_ALT_TRANSFORM) != 0);
2037                 }
2038         }
2039 }
2040
2041 /* note: caller needs to free 't' on a 0 return */
2042 bool initTransform(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *event, int mode)
2043 {
2044         int options = 0;
2045         PropertyRNA *prop;
2046
2047         t->context = C;
2048
2049         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
2050
2051         t->state = TRANS_STARTING;
2052
2053         if ((prop = RNA_struct_find_property(op->ptr, "texture_space")) && RNA_property_is_set(op->ptr, prop)) {
2054                 if (RNA_property_boolean_get(op->ptr, prop)) {
2055                         options |= CTX_TEXTURE;
2056                 }
2057         }
2058         
2059         if ((prop = RNA_struct_find_property(op->ptr, "gpencil_strokes")) && RNA_property_is_set(op->ptr, prop)) {
2060                 if (RNA_property_boolean_get(op->ptr, prop)) {
2061                         options |= CTX_GPENCIL_STROKES;
2062                 }
2063         }
2064
2065         t->options = options;
2066
2067         t->mode = mode;
2068
2069         t->launch_event = event ? event->type : -1;
2070
2071         if (t->launch_event == EVT_TWEAK_R) {
2072                 t->launch_event = RIGHTMOUSE;
2073         }
2074         else if (t->launch_event == EVT_TWEAK_L) {
2075                 t->launch_event = LEFTMOUSE;
2076         }
2077
2078         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
2079         // For manipulator only, so assume LEFTMOUSE
2080         if (t->launch_event == 0) {
2081                 t->launch_event = LEFTMOUSE;
2082         }
2083
2084         initTransInfo(C, t, op, event);
2085
2086         if (t->spacetype == SPACE_VIEW3D) {
2087                 initTransformOrientation(C, t);
2088
2089                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
2090                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2091                 t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2092                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2093         }
2094         else if (t->spacetype == SPACE_IMAGE) {
2095                 unit_m3(t->spacemtx);
2096                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2097                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2098                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2099         }
2100         else if (t->spacetype == SPACE_CLIP) {
2101                 unit_m3(t->spacemtx);
2102                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2103                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2104         }
2105         else if (t->spacetype == SPACE_NODE) {
2106                 unit_m3(t->spacemtx);
2107                 /*t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);*/
2108                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2109                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2110         }
2111         else if (t->spacetype == SPACE_IPO) {
2112                 unit_m3(t->spacemtx);
2113                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2114                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2115                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2116         }
2117         else if (t->spacetype == SPACE_ACTION) {
2118                 unit_m3(t->spacemtx);
2119                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2120                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2121                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2122         }
2123         else
2124                 unit_m3(t->spacemtx);
2125
2126         createTransData(C, t);          // make TransData structs from selection
2127
2128         if (t->total == 0) {
2129                 postTrans(C, t);
2130                 return 0;
2131         }
2132
2133         if (event) {
2134                 /* keymap for shortcut header prints */
2135                 t->keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
2136
2137                 /* Stupid code to have Ctrl-Click on manipulator work ok
2138                  *
2139                  * do this only for translation/rotation/resize due to only this
2140                  * moded are available from manipulator and doing such check could
2141                  * lead to keymap conflicts for other modes (see #31584)
2142                  */
2143                 if (ELEM(mode, TFM_TRANSLATION, TFM_ROTATION, TFM_RESIZE)) {
2144                         wmKeyMapItem *kmi;
2145
2146                         for (kmi = t->keymap->items.first; kmi; kmi = kmi->next) {
2147                                 if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS) {
2148                                         if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) &&   event->ctrl)  ||
2149                                             (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
2150                                             (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) &&     event->alt)   ||
2151                                             ((kmi->type == OSKEY) &&                         event->oskey) )
2152                                         {
2153                                                 t->modifiers |= MOD_SNAP_INVERT;
2154                                         }
2155                                         break;
2156                                 }
2157                         }
2158                 }
2159         }
2160
2161         initSnapping(t, op); // Initialize snapping data AFTER mode flags
2162
2163         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
2164         /* EVIL2: we gave as argument also texture space context bit... was cleared */
2165         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
2166         mode = t->mode;
2167
2168         calculatePropRatio(t);
2169         calculateCenter(t);
2170
2171         initMouseInput(t, &t->mouse, t->center2d, t->imval);
2172
2173         switch (mode) {
2174                 case TFM_TRANSLATION:
2175                         initTranslation(t);
2176                         break;
2177                 case TFM_ROTATION:
2178                         initRotation(t);
2179                         break;
2180                 case TFM_RESIZE:
2181                         initResize(t);
2182                         break;
2183                 case TFM_SKIN_RESIZE:
2184                         initSkinResize(t);
2185                         break;
2186                 case TFM_TOSPHERE:
2187                         initToSphere(t);
2188                         break;
2189                 case TFM_SHEAR:
2190                         initShear(t);
2191                         break;
2192                 case TFM_BEND:
2193                         initBend(t);
2194                         break;
2195                 case TFM_SHRINKFATTEN:
2196                         initShrinkFatten(t);
2197                         break;
2198                 case TFM_TILT:
2199                         initTilt(t);
2200                         break;
2201                 case TFM_CURVE_SHRINKFATTEN:
2202                         initCurveShrinkFatten(t);
2203                         break;
2204                 case TFM_MASK_SHRINKFATTEN:
2205                         initMaskShrinkFatten(t);
2206                         break;
2207                 case TFM_GPENCIL_SHRINKFATTEN:
2208                         initGPShrinkFatten(t);
2209                         break;
2210                 case TFM_TRACKBALL:
2211                         initTrackball(t);
2212                         break;
2213                 case TFM_PUSHPULL:
2214                         initPushPull(t);
2215                         break;
2216                 case TFM_CREASE:
2217                         initCrease(t);
2218                         break;
2219                 case TFM_BONESIZE:
2220                 {   /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
2221                         bArmature *arm = t->poseobj->data;
2222                         if (arm->drawtype == ARM_ENVELOPE)
2223                                 initBoneEnvelope(t);
2224                         else
2225                                 initBoneSize(t);
2226                         break;
2227                 }
2228                 case TFM_BONE_ENVELOPE:
2229                         initBoneEnvelope(t);
2230                         break;
2231                 case TFM_EDGE_SLIDE:
2232                         initEdgeSlide(t);
2233                         break;
2234                 case TFM_VERT_SLIDE:
2235                         initVertSlide(t);
2236                         break;
2237                 case TFM_BONE_ROLL:
2238                         initBoneRoll(t);
2239                         break;
2240                 case TFM_TIME_TRANSLATE:
2241                         initTimeTranslate(t);
2242                         break;
2243                 case TFM_TIME_SLIDE:
2244                         initTimeSlide(t);
2245                         break;
2246                 case TFM_TIME_SCALE:
2247                         initTimeScale(t);
2248                         break;
2249                 case TFM_TIME_DUPLICATE:
2250                         /* same as TFM_TIME_EXTEND, but we need the mode info for later
2251                          * so that duplicate-culling will work properly
2252                          */
2253                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2254                                 initTranslation(t);
2255                         else
2256                                 initTimeTranslate(t);
2257                         t->mode = mode;
2258                         break;
2259                 case TFM_TIME_EXTEND:
2260                         /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
2261                          * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
2262                          * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
2263                          * depending on which editor this was called from
2264                          */
2265                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2266                                 initTranslation(t);
2267                         else
2268                                 initTimeTranslate(t);
2269                         break;
2270                 case TFM_BAKE_TIME:
2271                         initBakeTime(t);
2272                         break;
2273                 case TFM_MIRROR:
2274                         initMirror(t);
2275                         break;
2276                 case TFM_BWEIGHT:
2277                         initBevelWeight(t);
2278                         break;
2279                 case TFM_ALIGN:
2280                         initAlign(t);
2281                         break;
2282                 case TFM_SEQ_SLIDE:
2283                         initSeqSlide(t);
2284                         break;
2285         }
2286
2287         if (t->state == TRANS_CANCEL) {
2288                 postTrans(C, t);
2289                 return 0;
2290         }
2291
2292
2293         /* overwrite initial values if operator supplied a non-null vector */
2294         if ((prop = RNA_struct_find_property(op->ptr, "value")) && RNA_property_is_set(op->ptr, prop)) {
2295                 float values[4] = {0}; /* in case value isn't length 4, avoid uninitialized memory  */
2296
2297                 if (RNA_property_array_check(prop)) {
2298                         RNA_float_get_array(op->ptr, "value", values);
2299                 }
2300                 else {
2301                         values[0] = RNA_float_get(op->ptr, "value");
2302                 }
2303
2304                 copy_v4_v4(t->values, values);
2305                 copy_v4_v4(t->auto_values, values);
2306                 t->flag |= T_AUTOVALUES;
2307         }
2308
2309         /* Transformation axis from operator */
2310         if ((prop = RNA_struct_find_property(op->ptr, "axis")) && RNA_property_is_set(op->ptr, prop)) {
2311                 RNA_property_float_get_array(op->ptr, prop, t->axis);
2312                 normalize_v3(t->axis);
2313                 copy_v3_v3(t->axis_orig, t->axis);
2314         }
2315
2316         /* Constraint init from operator */
2317         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis")) && RNA_property_is_set(op->ptr, prop)) {
2318                 int constraint_axis[3];
2319
2320                 RNA_property_boolean_get_array(op->ptr, prop, constraint_axis);
2321
2322                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2]) {
2323                         t->con.mode |= CON_APPLY;
2324
2325                         if (constraint_axis[0]) {
2326                                 t->con.mode |= CON_AXIS0;
2327                         }
2328                         if (constraint_axis[1]) {
2329                                 t->con.mode |= CON_AXIS1;
2330                         }
2331                         if (constraint_axis[2]) {
2332                                 t->con.mode |= CON_AXIS2;
2333                         }
2334
2335                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
2336                 }
2337         }
2338
2339         t->context = NULL;
2340
2341         return 1;
2342 }
2343
2344 void transformApply(bContext *C, TransInfo *t)
2345 {
2346         t->context = C;
2347
2348         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT))) {
2349                 selectConstraint(t);
2350                 if (t->transform) {
2351                         t->transform(t, t->mval);  // calls recalcData()
2352                         viewRedrawForce(C, t);
2353                 }
2354                 t->redraw = TREDRAW_NOTHING;
2355         }
2356         else if (t->redraw & TREDRAW_SOFT) {
2357                 viewRedrawForce(C, t);
2358         }
2359
2360         /* If auto confirm is on, break after one pass */
2361         if (t->options & CTX_AUTOCONFIRM) {
2362                 t->state = TRANS_CONFIRM;
2363         }
2364
2365         t->context = NULL;
2366 }
2367
2368 static void drawTransformApply(const bContext *C, ARegion *UNUSED(ar), void *arg)
2369 {
2370         TransInfo *t = arg;
2371
2372         if (t->redraw & TREDRAW_SOFT) {
2373                 t->redraw |= TREDRAW_HARD;
2374                 transformApply((bContext *)C, t);
2375         }
2376 }
2377
2378 int transformEnd(bContext *C, TransInfo *t)
2379 {
2380         int exit_code = OPERATOR_RUNNING_MODAL;
2381
2382         t->context = C;
2383
2384         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING) {
2385                 /* handle restoring objects */
2386                 if (t->state == TRANS_CANCEL) {
2387                         /* exception, edge slide transformed UVs too */
2388                         if (t->mode == TFM_EDGE_SLIDE) {
2389                                 doEdgeSlide(t, 0.0f);
2390                         }
2391                         else if (t->mode == TFM_VERT_SLIDE) {
2392                                 doVertSlide(t, 0.0f);
2393                         }
2394                         
2395                         exit_code = OPERATOR_CANCELLED;
2396                         restoreTransObjects(t); // calls recalcData()
2397                 }
2398                 else {
2399                         exit_code = OPERATOR_FINISHED;
2400                 }
2401
2402                 /* aftertrans does insert keyframes, and clears base flags; doesn't read transdata */
2403                 special_aftertrans_update(C, t);
2404
2405                 /* free data */
2406                 postTrans(C, t);
2407
2408                 /* send events out for redraws */
2409                 viewRedrawPost(C, t);
2410
2411                 viewRedrawForce(C, t);
2412         }
2413
2414         t->context = NULL;
2415
2416         return exit_code;
2417 }
2418
2419 /* ************************** TRANSFORM LOCKS **************************** */
2420
2421 static void protectedTransBits(short protectflag, float vec[3])
2422 {
2423         if (protectflag & OB_LOCK_LOCX)
2424                 vec[0] = 0.0f;
2425         if (protectflag & OB_LOCK_LOCY)
2426                 vec[1] = 0.0f;
2427         if (protectflag & OB_LOCK_LOCZ)
2428                 vec[2] = 0.0f;
2429 }
2430
2431 static void protectedSizeBits(short protectflag, float size[3])
2432 {
2433         if (protectflag & OB_LOCK_SCALEX)
2434                 size[0] = 1.0f;
2435         if (protectflag & OB_LOCK_SCALEY)
2436                 size[1] = 1.0f;
2437         if (protectflag & OB_LOCK_SCALEZ)
2438                 size[2] = 1.0f;
2439 }
2440
2441 static void protectedRotateBits(short protectflag, float eul[3], const float oldeul[3])
2442 {
2443         if (protectflag & OB_LOCK_ROTX)
2444                 eul[0] = oldeul[0];
2445         if (protectflag & OB_LOCK_ROTY)
2446                 eul[1] = oldeul[1];
2447         if (protectflag & OB_LOCK_ROTZ)
2448                 eul[2] = oldeul[2];
2449 }
2450
2451
2452 /* this function only does the delta rotation */
2453 /* axis-angle is usually internally stored as quats... */
2454 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
2455 {
2456         /* check that protection flags are set */
2457         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2458                 return;
2459         
2460         if (protectflag & OB_LOCK_ROT4D) {
2461                 /* axis-angle getting limited as 4D entities that they are... */
2462                 if (protectflag & OB_LOCK_ROTW)
2463                         *angle = oldAngle;
2464                 if (protectflag & OB_LOCK_ROTX)
2465                         axis[0] = oldAxis[0];
2466                 if (protectflag & OB_LOCK_ROTY)
2467                         axis[1] = oldAxis[1];
2468                 if (protectflag & OB_LOCK_ROTZ)
2469                         axis[2] = oldAxis[2];
2470         }
2471         else {
2472                 /* axis-angle get limited with euler... */
2473                 float eul[3], oldeul[3];
2474                 
2475                 axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, axis, *angle);
2476                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, oldAxis, oldAngle);
2477                 
2478                 if (protectflag & OB_LOCK_ROTX)
2479                         eul[0] = oldeul[0];
2480                 if (protectflag & OB_LOCK_ROTY)
2481                         eul[1] = oldeul[1];
2482                 if (protectflag & OB_LOCK_ROTZ)
2483                         eul[2] = oldeul[2];
2484                 
2485                 eulO_to_axis_angle(axis, angle, eul, EULER_ORDER_DEFAULT);
2486                 
2487                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
2488                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
2489                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
2490                         axis[1] = 1.0f;
2491                 }
2492         }
2493 }
2494
2495 /* this function only does the delta rotation */
2496 static void protectedQuaternionBits(short protectflag, float quat[4], const float oldquat[4])
2497 {
2498         /* check that protection flags are set */
2499         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2500                 return;
2501         
2502         if (protectflag & OB_LOCK_ROT4D) {
2503                 /* quaternions getting limited as 4D entities that they are... */
2504                 if (protectflag & OB_LOCK_ROTW)
2505                         quat[0] = oldquat[0];
2506                 if (protectflag & OB_LOCK_ROTX)
2507                         quat[1] = oldquat[1];
2508                 if (protectflag & OB_LOCK_ROTY)
2509                         quat[2] = oldquat[2];
2510                 if (protectflag & OB_LOCK_ROTZ)
2511                         quat[3] = oldquat[3];
2512         }
2513         else {
2514                 /* quaternions get limited with euler... (compatibility mode) */
2515                 float eul[3], oldeul[3], nquat[4], noldquat[4];
2516                 float qlen;
2517
2518                 qlen = normalize_qt_qt(nquat, quat);
2519                 normalize_qt_qt(noldquat, oldquat);
2520
2521                 quat_to_eul(eul, nquat);
2522                 quat_to_eul(oldeul, noldquat);
2523
2524                 if (protectflag & OB_LOCK_ROTX)
2525                         eul[0] = oldeul[0];
2526                 if (protectflag & OB_LOCK_ROTY)
2527                         eul[1] = oldeul[1];
2528                 if (protectflag & OB_LOCK_ROTZ)
2529                         eul[2] = oldeul[2];
2530
2531                 eul_to_quat(quat, eul);
2532
2533                 /* restore original quat size */
2534                 mul_qt_fl(quat, qlen);
2535                 
2536                 /* quaternions flip w sign to accumulate rotations correctly */
2537                 if ((nquat[0] < 0.0f && quat[0] > 0.0f) ||
2538                     (nquat[0] > 0.0f && quat[0] < 0.0f))
2539                 {
2540                         mul_qt_fl(quat, -1.0f);
2541                 }
2542         }
2543 }
2544
2545 /* ******************* TRANSFORM LIMITS ********************** */
2546
2547 static void constraintTransLim(TransInfo *t, TransData *td)
2548 {
2549         if (td->con) {
2550                 const bConstraintTypeInfo *ctiLoc = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_LOCLIMIT);
2551                 const bConstraintTypeInfo *ctiDist = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_DISTLIMIT);
2552                 
2553                 bConstraintOb cob = {NULL};
2554                 bConstraint *con;
2555                 float ctime = (float)(t->scene->r.cfra);
2556                 
2557                 /* Make a temporary bConstraintOb for using these limit constraints
2558                  *  - they only care that cob->matrix is correctly set ;-)
2559                  *      - current space should be local
2560                  */
2561                 unit_m4(cob.matrix);
2562                 copy_v3_v3(cob.matrix[3], td->loc);
2563                 
2564                 /* Evaluate valid constraints */
2565                 for (con = td->con; con; con = con->next) {
2566                         const bConstraintTypeInfo *cti = NULL;
2567                         ListBase targets = {NULL, NULL};
2568                         
2569                         /* only consider constraint if enabled */
2570                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2571                         if (con->enforce == 0.0f) continue;
2572                         
2573                         /* only use it if it's tagged for this purpose (and the right type) */
2574                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2575                                 bLocLimitConstraint *data = con->data;
2576                                 
2577                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2578                                         continue;
2579                                 cti = ctiLoc;
2580                         }
2581                         else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
2582                                 bDistLimitConstraint *data = con->data;
2583                                 
2584                                 if ((data->flag & LIMITDIST_TRANSFORM) == 0)
2585                                         continue;
2586                                 cti = ctiDist;
2587                         }
2588                         
2589                         if (cti) {
2590                                 /* do space conversions */
2591                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2592                                         /* just multiply by td->mtx (this should be ok) */
2593                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2594                                 }
2595                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2596                                         /* skip... incompatable spacetype */
2597                                         continue;
2598                                 }
2599                                 
2600                                 /* get constraint targets if needed */
2601                                 BKE_constraint_targets_for_solving_get(con, &cob, &targets, ctime);
2602                                 
2603                                 /* do constraint */
2604                                 cti->evaluate_constraint(con, &cob, &targets);
2605                                 
2606                                 /* convert spaces again */
2607                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2608                                         /* just multiply by td->smtx (this should be ok) */
2609                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2610                                 }
2611                                 
2612                                 /* free targets list */
2613                                 BLI_freelistN(&targets);
2614                         }
2615                 }
2616                 
2617                 /* copy results from cob->matrix */
2618                 copy_v3_v3(td->loc, cob.matrix[3]);
2619         }
2620 }
2621
2622 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2623 {
2624         /* Make a temporary bConstraintOb for use by limit constraints
2625          *  - they only care that cob->matrix is correctly set ;-)
2626          *      - current space should be local
2627          */
2628         memset(cob, 0, sizeof(bConstraintOb));
2629         if (td->ext) {
2630                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2631                         /* quats */
2632                         /* objects and bones do normalization first too, otherwise
2633                          * we don't necessarily end up with a rotation matrix, and
2634                          * then conversion back to quat gives a different result */
2635                         float quat[4];
2636                         normalize_qt_qt(quat, td->ext->quat);
2637                         quat_to_mat4(cob->matrix, quat);
2638                 }
2639                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2640                         /* axis angle */
2641                         axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2642                 }
2643                 else {
2644                         /* eulers */
2645                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2646                 }
2647         }
2648 }
2649
2650 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2651 {
2652         if (td->con) {
2653                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_ROTLIMIT);
2654                 bConstraintOb cob;
2655                 bConstraint *con;
2656                 bool do_limit = false;
2657
2658                 /* Evaluate valid constraints */
2659                 for (con = td->con; con; con = con->next) {
2660                         /* only consider constraint if enabled */
2661                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2662                         if (con->enforce == 0.0f) continue;
2663
2664                         /* we're only interested in Limit-Rotation constraints */
2665                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2666                                 bRotLimitConstraint *data = con->data;
2667
2668                                 /* only use it if it's tagged for this purpose */
2669                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2670                                         continue;
2671
2672                                 /* skip incompatable spacetypes */
2673                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2674                                         continue;
2675
2676                                 /* only do conversion if necessary, to preserve quats and eulers */
2677                                 if (do_limit == false) {
2678                                         constraintob_from_transdata(&cob, td);
2679                                         do_limit = true;
2680                                 }
2681
2682                                 /* do space conversions */
2683                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2684                                         /* just multiply by td->mtx (this should be ok) */
2685                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2686                                 }
2687                                 
2688                                 /* do constraint */
2689                                 cti->evaluate_constraint(con, &cob, NULL);
2690                                 
2691                                 /* convert spaces again */
2692                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2693                                         /* just multiply by td->smtx (this should be ok) */
2694                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2695                                 }
2696                         }
2697                 }
2698                 
2699                 if (do_limit) {
2700                         /* copy results from cob->matrix */
2701                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2702                                 /* quats */
2703                                 mat4_to_quat(td->ext->quat, cob.matrix);
2704                         }
2705                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2706                                 /* axis angle */
2707                                 mat4_to_axis_angle(&td->ext->quat[1], &td->ext->quat[0], cob.matrix);
2708                         }
2709                         else {
2710                                 /* eulers */
2711                                 mat4_to_eulO(td->ext->rot, td->ext->rotOrder, cob.matrix);
2712                         }
2713                 }
2714         }
2715 }
2716
2717 static void constraintSizeLim(TransInfo *t, TransData *td)
2718 {
2719         if (td->con && td->ext) {
2720                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_SIZELIMIT);
2721                 bConstraintOb cob = {NULL};
2722                 bConstraint *con;
2723                 float size_sign[3], size_abs[3];
2724                 int i;
2725                 
2726                 /* Make a temporary bConstraintOb for using these limit constraints
2727                  *  - they only care that cob->matrix is correctly set ;-)
2728                  *      - current space should be local
2729                  */
2730                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2731                         /* scale val and reset size */
2732                         return; // TODO: fix this case
2733                 }
2734                 else {
2735                         /* Reset val if SINGLESIZE but using a constraint */
2736                         if (td->flag & TD_SINGLESIZE)
2737                                 return;
2738
2739                         /* separate out sign to apply back later */
2740                         for (i = 0; i < 3; i++) {
2741                                 size_sign[i] = signf(td->ext->size[i]);
2742                                 size_abs[i] = fabsf(td->ext->size[i]);
2743                         }
2744                         
2745                         size_to_mat4(cob.matrix, size_abs);
2746                 }
2747                 
2748                 /* Evaluate valid constraints */
2749                 for (con = td->con; con; con = con->next) {
2750                         /* only consider constraint if enabled */
2751                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2752                         if (con->enforce == 0.0f) continue;
2753                         
2754                         /* we're only interested in Limit-Scale constraints */
2755                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2756                                 bSizeLimitConstraint *data = con->data;
2757                                 
2758                                 /* only use it if it's tagged for this purpose */
2759                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2760                                         continue;
2761                                 
2762                                 /* do space conversions */
2763                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2764                                         /* just multiply by td->mtx (this should be ok) */
2765                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2766                                 }
2767                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2768                                         /* skip... incompatible spacetype */
2769                                         continue;
2770                                 }
2771                                 
2772                                 /* do constraint */
2773                                 cti->evaluate_constraint(con, &cob, NULL);
2774                                 
2775                                 /* convert spaces again */
2776                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2777                                         /* just multiply by td->smtx (this should be ok) */
2778                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2779                                 }
2780                         }
2781                 }
2782