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