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