Code cleanup: remove redundant matrix initialization
[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         char cmode = constraintModeToChar(t);
949         bool handled = false;
950
951         t->redraw |= handleMouseInput(t, &t->mouse, event);
952
953         /* Handle modal numinput events first, if already activated. */
954         if (((event->val == KM_PRESS) || (event->type == EVT_MODAL_MAP)) &&
955             hasNumInput(&t->num) && handleNumInput(t->context, &(t->num), event))
956         {
957                 t->redraw |= TREDRAW_HARD;
958                 handled = true;
959         }
960         else if (event->type == MOUSEMOVE) {
961                 if (t->modifiers & MOD_CONSTRAINT_SELECT)
962                         t->con.mode |= CON_SELECT;
963
964                 copy_v2_v2_int(t->mval, event->mval);
965
966                 // t->redraw |= TREDRAW_SOFT; /* Use this for soft redraw. Might cause flicker in object mode */
967                 t->redraw |= TREDRAW_HARD;
968
969                 if (t->state == TRANS_STARTING) {
970                         t->state = TRANS_RUNNING;
971                 }
972
973                 applyMouseInput(t, &t->mouse, t->mval, t->values);
974
975                 // Snapping mouse move events
976                 t->redraw |= handleSnapping(t, event);
977                 handled = true;
978         }
979         /* handle modal keymap first */
980         else if (event->type == EVT_MODAL_MAP) {
981                 switch (event->val) {
982                         case TFM_MODAL_CANCEL:
983                                 t->state = TRANS_CANCEL;
984                                 handled = true;
985                                 break;
986                         case TFM_MODAL_CONFIRM:
987                                 t->state = TRANS_CONFIRM;
988                                 handled = true;
989                                 break;
990                         case TFM_MODAL_TRANSLATE:
991                                 /* only switch when... */
992                                 if (ELEM5(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_EDGE_SLIDE, TFM_VERT_SLIDE)) {
993                                         resetTransModal(t);
994                                         resetTransRestrictions(t);
995                                         restoreTransObjects(t);
996                                         initTranslation(t);
997                                         initSnapping(t, NULL); // need to reinit after mode change
998                                         t->redraw |= TREDRAW_HARD;
999                                         WM_event_add_mousemove(t->context);
1000                                         handled = true;
1001                                 }
1002                                 else if (t->mode == TFM_SEQ_SLIDE) {
1003                                         t->flag ^= T_ALT_TRANSFORM;
1004                                         t->redraw |= TREDRAW_HARD;
1005                                         handled = true;
1006                                 }
1007                                 else {
1008                                         if (t->obedit && t->obedit->type == OB_MESH) {
1009                                                 if ((t->mode == TFM_TRANSLATION) && (t->spacetype == SPACE_VIEW3D)) {
1010                                                         resetTransModal(t);
1011                                                         resetTransRestrictions(t);
1012                                                         restoreTransObjects(t);
1013
1014                                                         /* first try edge slide */
1015                                                         initEdgeSlide(t);
1016                                                         /* if that fails, do vertex slide */
1017                                                         if (t->state == TRANS_CANCEL) {
1018                                                                 t->state = TRANS_STARTING;
1019                                                                 initVertSlide(t);
1020                                                         }
1021                                                         /* vert slide can fail on unconnected vertices (rare but possible) */
1022                                                         if (t->state == TRANS_CANCEL) {
1023                                                                 t->state = TRANS_STARTING;
1024                                                                 resetTransRestrictions(t);
1025                                                                 restoreTransObjects(t);
1026                                                                 initTranslation(t);
1027                                                         }
1028                                                         initSnapping(t, NULL); // need to reinit after mode change
1029                                                         t->redraw |= TREDRAW_HARD;
1030                                                         handled = true;
1031                                                         WM_event_add_mousemove(t->context);
1032                                                 }
1033                                         }
1034                                         else if (t->options & (CTX_MOVIECLIP | CTX_MASK)) {
1035                                                 if (t->mode == TFM_TRANSLATION) {
1036                                                         restoreTransObjects(t);
1037
1038                                                         t->flag ^= T_ALT_TRANSFORM;
1039                                                         t->redraw |= TREDRAW_HARD;
1040                                                         handled = true;
1041                                                 }
1042                                         }
1043                                 }
1044                                 break;
1045                         case TFM_MODAL_ROTATE:
1046                                 /* only switch when... */
1047                                 if (!(t->options & CTX_TEXTURE) && !(t->options & (CTX_MOVIECLIP | CTX_MASK))) {
1048                                         if (ELEM6(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION, TFM_EDGE_SLIDE, TFM_VERT_SLIDE)) {
1049                                                 resetTransModal(t);
1050                                                 resetTransRestrictions(t);
1051                                                 
1052                                                 if (t->mode == TFM_ROTATION) {
1053                                                         restoreTransObjects(t);
1054                                                         initTrackball(t);
1055                                                 }
1056                                                 else {
1057                                                         restoreTransObjects(t);
1058                                                         initRotation(t);
1059                                                 }
1060                                                 initSnapping(t, NULL); // need to reinit after mode change
1061                                                 t->redraw |= TREDRAW_HARD;
1062                                                 handled = true;
1063                                         }
1064                                 }
1065                                 break;
1066                         case TFM_MODAL_RESIZE:
1067                                 /* only switch when... */
1068                                 if (ELEM5(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL, TFM_EDGE_SLIDE, TFM_VERT_SLIDE)) {
1069
1070                                         /* Scale isn't normally very useful after extrude along normals, see T39756 */
1071                                         if ((t->con.mode & CON_APPLY) && (t->con.orientation == V3D_MANIP_NORMAL)) {
1072                                                 stopConstraint(t);
1073                                         }
1074
1075                                         resetTransModal(t);
1076                                         resetTransRestrictions(t);
1077                                         restoreTransObjects(t);
1078                                         initResize(t);
1079                                         initSnapping(t, NULL); // need to reinit after mode change
1080                                         t->redraw |= TREDRAW_HARD;
1081                                         handled = true;
1082                                 }
1083                                 else if (t->mode == TFM_SHRINKFATTEN) {
1084                                         t->flag ^= T_ALT_TRANSFORM;
1085                                         t->redraw |= TREDRAW_HARD;
1086                                         handled = true;
1087                                 }
1088                                 else if (t->mode == TFM_RESIZE) {
1089                                         if (t->options & CTX_MOVIECLIP) {
1090                                                 restoreTransObjects(t);
1091
1092                                                 t->flag ^= T_ALT_TRANSFORM;
1093                                                 t->redraw |= TREDRAW_HARD;
1094                                                 handled = true;
1095                                         }
1096                                 }
1097                                 break;
1098                                 
1099                         case TFM_MODAL_SNAP_INV_ON:
1100                                 t->modifiers |= MOD_SNAP_INVERT;
1101                                 t->redraw |= TREDRAW_HARD;
1102                                 handled = true;
1103                                 break;
1104                         case TFM_MODAL_SNAP_INV_OFF:
1105                                 t->modifiers &= ~MOD_SNAP_INVERT;
1106                                 t->redraw |= TREDRAW_HARD;
1107                                 handled = true;
1108                                 break;
1109                         case TFM_MODAL_SNAP_TOGGLE:
1110                                 t->modifiers ^= MOD_SNAP;
1111                                 t->redraw |= TREDRAW_HARD;
1112                                 handled = true;
1113                                 break;
1114                         case TFM_MODAL_AXIS_X:
1115                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1116                                         if (cmode == 'X') {
1117                                                 stopConstraint(t);
1118                                         }
1119                                         else {
1120                                                 if (t->flag & T_2D_EDIT) {
1121                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS0), IFACE_("along X"));
1122                                                 }
1123                                                 else {
1124                                                         setUserConstraint(t, t->current_orientation, (CON_AXIS0), IFACE_("along %s X"));
1125                                                 }
1126                                         }
1127                                         t->redraw |= TREDRAW_HARD;
1128                                         handled = true;
1129                                 }
1130                                 break;
1131                         case TFM_MODAL_AXIS_Y:
1132                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1133                                         if (cmode == 'Y') {
1134                                                 stopConstraint(t);
1135                                         }
1136                                         else {
1137                                                 if (t->flag & T_2D_EDIT) {
1138                                                         setUserConstraint(t, V3D_MANIP_GLOBAL, (CON_AXIS1), IFACE_("along Y"));
1139                                                 }
1140                                                 else {
1141                                                         setUserConstraint(t, t->current_orientation, (CON_AXIS1), IFACE_("along %s Y"));
1142                                                 }
1143                                         }
1144                                         t->redraw |= TREDRAW_HARD;
1145                                         handled = true;
1146                                 }
1147                                 break;
1148                         case TFM_MODAL_AXIS_Z:
1149                                 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1150                                         if (cmode == 'Z') {
1151                                                 stopConstraint(t);
1152                                         }
1153                                         else {
1154                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS2), IFACE_("along %s Z"));
1155                                         }
1156                                         t->redraw |= TREDRAW_HARD;
1157                                         handled = true;
1158                                 }
1159                                 break;
1160                         case TFM_MODAL_PLANE_X:
1161                                 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1162                                         if (cmode == 'X') {
1163                                                 stopConstraint(t);
1164                                         }
1165                                         else {
1166                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS1 | CON_AXIS2), IFACE_("locking %s X"));
1167                                         }
1168                                         t->redraw |= TREDRAW_HARD;
1169                                         handled = true;
1170                                 }
1171                                 break;
1172                         case TFM_MODAL_PLANE_Y:
1173                                 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1174                                         if (cmode == 'Y') {
1175                                                 stopConstraint(t);
1176                                         }
1177                                         else {
1178                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0 | CON_AXIS2), IFACE_("locking %s Y"));
1179                                         }
1180                                         t->redraw |= TREDRAW_HARD;
1181                                         handled = true;
1182                                 }
1183                                 break;
1184                         case TFM_MODAL_PLANE_Z:
1185                                 if ((t->flag & (T_NO_CONSTRAINT | T_2D_EDIT)) == 0) {
1186                                         if (cmode == 'Z') {
1187                                                 stopConstraint(t);
1188                                         }
1189                                         else {
1190                                                 setUserConstraint(t, t->current_orientation, (CON_AXIS0 | CON_AXIS1), IFACE_("locking %s Z"));
1191                                         }
1192                                         t->redraw |= TREDRAW_HARD;
1193                                         handled = true;
1194                                 }
1195                                 break;
1196                         case TFM_MODAL_CONS_OFF:
1197                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1198                                         stopConstraint(t);
1199                                         t->redraw |= TREDRAW_HARD;
1200                                         handled = true;
1201                                 }
1202                                 break;
1203                         case TFM_MODAL_ADD_SNAP:
1204                                 addSnapPoint(t);
1205                                 t->redraw |= TREDRAW_HARD;
1206                                 handled = true;
1207                                 break;
1208                         case TFM_MODAL_REMOVE_SNAP:
1209                                 removeSnapPoint(t);
1210                                 t->redraw |= TREDRAW_HARD;
1211                                 handled = true;
1212                                 break;
1213                         case TFM_MODAL_PROPSIZE:
1214                                 /* MOUSEPAN usage... */
1215                                 if (t->flag & T_PROP_EDIT) {
1216                                         float fac = 1.0f + 0.005f *(event->y - event->prevy);
1217                                         t->prop_size *= fac;
1218                                         if (t->spacetype == SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1219                                                 t->prop_size = min_ff(t->prop_size, ((View3D *)t->view)->far);
1220                                         calculatePropRatio(t);
1221                                         t->redraw |= TREDRAW_HARD;
1222                                         handled = true;
1223                                 }
1224                                 break;
1225                         case TFM_MODAL_PROPSIZE_UP:
1226                                 if (t->flag & T_PROP_EDIT) {
1227                                         t->prop_size *= 1.1f;
1228                                         if (t->spacetype == SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1229                                                 t->prop_size = min_ff(t->prop_size, ((View3D *)t->view)->far);
1230                                         calculatePropRatio(t);
1231                                         t->redraw |= TREDRAW_HARD;
1232                                         handled = true;
1233                                 }
1234                                 break;
1235                         case TFM_MODAL_PROPSIZE_DOWN:
1236                                 if (t->flag & T_PROP_EDIT) {
1237                                         t->prop_size *= 0.90909090f;
1238                                         calculatePropRatio(t);
1239                                         t->redraw |= TREDRAW_HARD;
1240                                         handled = true;
1241                                 }
1242                                 break;
1243                         case TFM_MODAL_EDGESLIDE_UP:
1244                         case TFM_MODAL_EDGESLIDE_DOWN:
1245                                 t->redraw |= TREDRAW_HARD;
1246                                 handled = true;
1247                                 break;
1248                         case TFM_MODAL_AUTOIK_LEN_INC:
1249                                 if (t->flag & T_AUTOIK) {
1250                                         transform_autoik_update(t, 1);
1251                                         t->redraw |= TREDRAW_HARD;
1252                                         handled = true;
1253                                 }
1254                                 break;
1255                         case TFM_MODAL_AUTOIK_LEN_DEC:
1256                                 if (t->flag & T_AUTOIK) {
1257                                         transform_autoik_update(t, -1);
1258                                         t->redraw |= TREDRAW_HARD;
1259                                         handled = true;
1260                                 }
1261                                 break;
1262                         default:
1263                                 break;
1264                 }
1265         }
1266         /* else do non-mapped events */
1267         else if (event->val == KM_PRESS) {
1268                 switch (event->type) {
1269                         case RIGHTMOUSE:
1270                                 t->state = TRANS_CANCEL;
1271                                 handled = true;
1272                                 break;
1273                         /* enforce redraw of transform when modifiers are used */
1274                         case LEFTSHIFTKEY:
1275                         case RIGHTSHIFTKEY:
1276                                 t->modifiers |= MOD_CONSTRAINT_PLANE;
1277                                 t->redraw |= TREDRAW_HARD;
1278                                 handled = true;
1279                                 break;
1280
1281                         case SPACEKEY:
1282                                 t->state = TRANS_CONFIRM;
1283                                 handled = true;
1284                                 break;
1285
1286                         case MIDDLEMOUSE:
1287                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1288                                         /* exception for switching to dolly, or trackball, in camera view */
1289                                         if (t->flag & T_CAMERA) {
1290                                                 if (t->mode == TFM_TRANSLATION)
1291                                                         setLocalConstraint(t, (CON_AXIS2), IFACE_("along local Z"));
1292                                                 else if (t->mode == TFM_ROTATION) {
1293                                                         restoreTransObjects(t);
1294                                                         initTrackball(t);
1295                                                 }
1296                                         }
1297                                         else {
1298                                                 t->modifiers |= MOD_CONSTRAINT_SELECT;
1299                                                 if (t->con.mode & CON_APPLY) {
1300                                                         stopConstraint(t);
1301                                                 }
1302                                                 else {
1303                                                         if (event->shift) {
1304                                                                 initSelectConstraint(t, t->spacemtx);
1305                                                         }
1306                                                         else {
1307                                                                 /* bit hackish... but it prevents mmb select to print the orientation from menu */
1308                                                                 float mati[3][3];
1309                                                                 strcpy(t->spacename, "global");
1310                                                                 unit_m3(mati);
1311                                                                 initSelectConstraint(t, mati);
1312                                                         }
1313                                                         postSelectConstraint(t);
1314                                                 }
1315                                         }
1316                                         t->redraw |= TREDRAW_HARD;
1317                                         handled = true;
1318                                 }
1319                                 break;
1320                         case ESCKEY:
1321                                 t->state = TRANS_CANCEL;
1322                                 handled = true;
1323                                 break;
1324                         case PADENTER:
1325                         case RETKEY:
1326                                 t->state = TRANS_CONFIRM;
1327                                 handled = true;
1328                                 break;
1329                         case GKEY:
1330                                 /* only switch when... */
1331                                 if (ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL) ) {
1332                                         resetTransModal(t);
1333                                         resetTransRestrictions(t);
1334                                         restoreTransObjects(t);
1335                                         initTranslation(t);
1336                                         initSnapping(t, NULL); // need to reinit after mode change
1337                                         t->redraw |= TREDRAW_HARD;
1338                                         handled = true;
1339                                 }
1340                                 break;
1341                         case SKEY:
1342                                 /* only switch when... */
1343                                 if (ELEM3(t->mode, TFM_ROTATION, TFM_TRANSLATION, TFM_TRACKBALL) ) {
1344                                         resetTransModal(t);
1345                                         resetTransRestrictions(t);
1346                                         restoreTransObjects(t);
1347                                         initResize(t);
1348                                         initSnapping(t, NULL); // need to reinit after mode change
1349                                         t->redraw |= TREDRAW_HARD;
1350                                         handled = true;
1351                                 }
1352                                 break;
1353                         case RKEY:
1354                                 /* only switch when... */
1355                                 if (!(t->options & CTX_TEXTURE)) {
1356                                         if (ELEM4(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL, TFM_TRANSLATION) ) {
1357                                                 resetTransModal(t);
1358                                                 resetTransRestrictions(t);
1359
1360                                                 if (t->mode == TFM_ROTATION) {
1361                                                         restoreTransObjects(t);
1362                                                         initTrackball(t);
1363                                                 }
1364                                                 else {
1365                                                         restoreTransObjects(t);
1366                                                         initRotation(t);
1367                                                 }
1368                                                 initSnapping(t, NULL); // need to reinit after mode change
1369                                                 t->redraw |= TREDRAW_HARD;
1370                                                 handled = true;
1371                                         }
1372                                 }
1373                                 break;
1374                         case CKEY:
1375                                 if (event->alt) {
1376                                         if (!(t->options & CTX_NO_PET)) {
1377                                                 t->flag ^= T_PROP_CONNECTED;
1378                                                 sort_trans_data_dist(t);
1379                                                 calculatePropRatio(t);
1380                                                 t->redraw = TREDRAW_HARD;
1381                                                 handled = true;
1382                                         }
1383                                 }
1384                                 else {
1385                                         if (!(t->flag & T_NO_CONSTRAINT)) {
1386                                                 stopConstraint(t);
1387                                                 t->redraw |= TREDRAW_HARD;
1388                                                 handled = true;
1389                                         }
1390                                 }
1391                                 break;
1392                         case XKEY:
1393                         case YKEY:
1394                         case ZKEY:
1395                                 if (!(t->flag & T_NO_CONSTRAINT)) {
1396                                         transform_event_xyz_constraint(t, event->type, cmode);
1397                                         handled = true;
1398                                 }
1399                                 break;
1400                         case OKEY:
1401                                 if (t->flag & T_PROP_EDIT && event->shift) {
1402                                         t->prop_mode = (t->prop_mode + 1) % PROP_MODE_MAX;
1403                                         calculatePropRatio(t);
1404                                         t->redraw |= TREDRAW_HARD;
1405                                         handled = true;
1406                                 }
1407                                 break;
1408                         case PADPLUSKEY:
1409                                 if (event->alt && t->flag & T_PROP_EDIT) {
1410                                         t->prop_size *= 1.1f;
1411                                         if (t->spacetype == SPACE_VIEW3D && t->persp != RV3D_ORTHO)
1412                                                 t->prop_size = min_ff(t->prop_size, ((View3D *)t->view)->far);
1413                                         calculatePropRatio(t);
1414                                         t->redraw = TREDRAW_HARD;
1415                                         handled = true;
1416                                 }
1417                                 break;
1418                         case PAGEUPKEY:
1419                         case WHEELDOWNMOUSE:
1420                                 if (t->flag & T_AUTOIK) {
1421                                         transform_autoik_update(t, 1);
1422                                 }
1423                                 else {
1424                                         view_editmove(event->type);
1425                                 }
1426                                 t->redraw = TREDRAW_HARD;
1427                                 handled = true;
1428                                 break;
1429                         case PADMINUS:
1430                                 if (event->alt && t->flag & T_PROP_EDIT) {
1431                                         t->prop_size *= 0.90909090f;
1432                                         calculatePropRatio(t);
1433                                         t->redraw = TREDRAW_HARD;
1434                                         handled = true;
1435                                 }
1436                                 break;
1437                         case PAGEDOWNKEY:
1438                         case WHEELUPMOUSE:
1439                                 if (t->flag & T_AUTOIK) {
1440                                         transform_autoik_update(t, -1);
1441                                 }
1442                                 else {
1443                                         view_editmove(event->type);
1444                                 }
1445                                 t->redraw = TREDRAW_HARD;
1446                                 handled = true;
1447                                 break;
1448                         case LEFTALTKEY:
1449                         case RIGHTALTKEY:
1450                                 if (ELEM(t->spacetype, SPACE_SEQ, SPACE_VIEW3D)) {
1451                                         t->flag |= T_ALT_TRANSFORM;
1452                                         t->redraw |= TREDRAW_HARD;
1453                                         handled = true;
1454                                 }
1455                                 break;
1456                         default:
1457                                 break;
1458                 }
1459
1460                 /* Snapping key events */
1461                 t->redraw |= handleSnapping(t, event);
1462         }
1463         else if (event->val == KM_RELEASE) {
1464                 switch (event->type) {
1465                         case LEFTSHIFTKEY:
1466                         case RIGHTSHIFTKEY:
1467                                 t->modifiers &= ~MOD_CONSTRAINT_PLANE;
1468                                 t->redraw |= TREDRAW_HARD;
1469                                 handled = true;
1470                                 break;
1471
1472                         case MIDDLEMOUSE:
1473                                 if ((t->flag & T_NO_CONSTRAINT) == 0) {
1474                                         t->modifiers &= ~MOD_CONSTRAINT_SELECT;
1475                                         postSelectConstraint(t);
1476                                         t->redraw |= TREDRAW_HARD;
1477                                         handled = true;
1478                                 }
1479                                 break;
1480                         case LEFTALTKEY:
1481                         case RIGHTALTKEY:
1482                                 if (ELEM(t->spacetype, SPACE_SEQ, SPACE_VIEW3D)) {
1483                                         t->flag &= ~T_ALT_TRANSFORM;
1484                                         t->redraw |= TREDRAW_HARD;
1485                                         handled = true;
1486                                 }
1487                                 break;
1488                         default:
1489                                 break;
1490                 }
1491
1492                 /* confirm transform if launch key is released after mouse move */
1493                 if (t->flag & T_RELEASE_CONFIRM) {
1494                         /* XXX Keyrepeat bug in Xorg messes this up, will test when fixed */
1495                         if (event->type == t->launch_event && (t->launch_event == LEFTMOUSE || t->launch_event == RIGHTMOUSE)) {
1496                                 t->state = TRANS_CONFIRM;
1497                         }
1498                 }
1499         }
1500
1501         /* Per transform event, if present */
1502         if (t->handleEvent &&
1503             (!handled ||
1504              /* Needed for vertex slide, see [#38756] */
1505              (event->type == MOUSEMOVE)))
1506         {
1507                 t->redraw |= t->handleEvent(t, event);
1508         }
1509
1510         /* Try to init modal numinput now, if possible. */
1511         if (!(handled || t->redraw) && ((event->val == KM_PRESS) || (event->type == EVT_MODAL_MAP)) &&
1512             handleNumInput(t->context, &(t->num), event))
1513         {
1514                 t->redraw |= TREDRAW_HARD;
1515                 handled = true;
1516         }
1517
1518         if (handled || t->redraw) {
1519                 return 0;
1520         }
1521         else {
1522                 return OPERATOR_PASS_THROUGH;
1523         }
1524 }
1525
1526 bool calculateTransformCenter(bContext *C, int centerMode, float cent3d[3], float cent2d[2])
1527 {
1528         TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
1529         bool success;
1530
1531         t->state = TRANS_RUNNING;
1532
1533         /* avoid calculating PET */
1534         t->options = CTX_NO_PET;
1535
1536         t->mode = TFM_DUMMY;
1537
1538         initTransInfo(C, t, NULL, NULL);
1539
1540         /* avoid doing connectivity lookups (when V3D_LOCAL is set) */
1541         t->around = V3D_CENTER;
1542
1543         createTransData(C, t);              // make TransData structs from selection
1544
1545         t->around = centerMode;             // override userdefined mode
1546
1547         if (t->total == 0) {
1548                 success = false;
1549         }
1550         else {
1551                 success = true;
1552
1553                 calculateCenter(t);
1554
1555                 if (cent2d) {
1556                         copy_v2_v2(cent2d, t->center2d);
1557                 }
1558
1559                 if (cent3d) {
1560                         // Copy center from constraint center. Transform center can be local
1561                         copy_v3_v3(cent3d, t->con.center);
1562                 }
1563         }
1564
1565
1566         /* aftertrans does insert keyframes, and clears base flags; doesn't read transdata */
1567         special_aftertrans_update(C, t);
1568
1569         postTrans(C, t);
1570
1571         MEM_freeN(t);
1572
1573         return success;
1574 }
1575
1576 typedef enum {
1577         UP,
1578         DOWN,
1579         LEFT,
1580         RIGHT
1581 } ArrowDirection;
1582 static void drawArrow(ArrowDirection d, short offset, short length, short size)
1583 {
1584         switch (d) {
1585                 case LEFT:
1586                         offset = -offset;
1587                         length = -length;
1588                         size = -size;
1589                         /* fall-through */
1590                 case RIGHT:
1591                         glBegin(GL_LINES);
1592                         glVertex2s(offset, 0);
1593                         glVertex2s(offset + length, 0);
1594                         glVertex2s(offset + length, 0);
1595                         glVertex2s(offset + length - size, -size);
1596                         glVertex2s(offset + length, 0);
1597                         glVertex2s(offset + length - size,  size);
1598                         glEnd();
1599                         break;
1600
1601                 case DOWN:
1602                         offset = -offset;
1603                         length = -length;
1604                         size = -size;
1605                         /* fall-through */
1606                 case UP:
1607                         glBegin(GL_LINES);
1608                         glVertex2s(0, offset);
1609                         glVertex2s(0, offset + length);
1610                         glVertex2s(0, offset + length);
1611                         glVertex2s(-size, offset + length - size);
1612                         glVertex2s(0, offset + length);
1613                         glVertex2s(size, offset + length - size);
1614                         glEnd();
1615                         break;
1616         }
1617 }
1618
1619 static void drawArrowHead(ArrowDirection d, short size)
1620 {
1621         switch (d) {
1622                 case LEFT:
1623                         size = -size;
1624                         /* fall-through */
1625                 case RIGHT:
1626                         glBegin(GL_LINES);
1627                         glVertex2s(0, 0);
1628                         glVertex2s(-size, -size);
1629                         glVertex2s(0, 0);
1630                         glVertex2s(-size,  size);
1631                         glEnd();
1632                         break;
1633
1634                 case DOWN:
1635                         size = -size;
1636                         /* fall-through */
1637                 case UP:
1638                         glBegin(GL_LINES);
1639                         glVertex2s(0, 0);
1640                         glVertex2s(-size, -size);
1641                         glVertex2s(0, 0);
1642                         glVertex2s(size, -size);
1643                         glEnd();
1644                         break;
1645         }
1646 }
1647
1648 static void drawArc(float size, float angle_start, float angle_end, int segments)
1649 {
1650         float delta = (angle_end - angle_start) / segments;
1651         float angle;
1652         int a;
1653
1654         glBegin(GL_LINE_STRIP);
1655
1656         for (angle = angle_start, a = 0; a < segments; angle += delta, a++) {
1657                 glVertex2f(cosf(angle) * size, sinf(angle) * size);
1658         }
1659         glVertex2f(cosf(angle_end) * size, sinf(angle_end) * size);
1660
1661         glEnd();
1662 }
1663
1664 static int helpline_poll(bContext *C)
1665 {
1666         ARegion *ar = CTX_wm_region(C);
1667         
1668         if (ar && ar->regiontype == RGN_TYPE_WINDOW)
1669                 return 1;
1670         return 0;
1671 }
1672
1673 static void drawHelpline(bContext *UNUSED(C), int x, int y, void *customdata)
1674 {
1675         TransInfo *t = (TransInfo *)customdata;
1676
1677         if (t->helpline != HLP_NONE && !(t->flag & T_USES_MANIPULATOR)) {
1678                 float vecrot[3], cent[2];
1679                 int mval[2];
1680
1681                 mval[0] = x;
1682                 mval[1] = y;
1683
1684                 copy_v3_v3(vecrot, t->center);
1685                 if (t->flag & T_EDIT) {
1686                         Object *ob = t->obedit;
1687                         if (ob) mul_m4_v3(ob->obmat, vecrot);
1688                 }
1689                 else if (t->flag & T_POSE) {
1690                         Object *ob = t->poseobj;
1691                         if (ob) mul_m4_v3(ob->obmat, vecrot);
1692                 }
1693
1694                 projectFloatViewEx(t, vecrot, cent, V3D_PROJ_TEST_CLIP_ZERO);
1695
1696                 glPushMatrix();
1697
1698                 switch (t->helpline) {
1699                         case HLP_SPRING:
1700                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1701
1702                                 setlinestyle(3);
1703                                 glBegin(GL_LINE_STRIP);
1704                                 glVertex2iv(t->mval);
1705                                 glVertex2fv(cent);
1706                                 glEnd();
1707
1708                                 glTranslatef(mval[0], mval[1], 0);
1709                                 glRotatef(-RAD2DEGF(atan2f(cent[0] - t->mval[0], cent[1] - t->mval[1])), 0, 0, 1);
1710
1711                                 setlinestyle(0);
1712                                 glLineWidth(3.0);
1713                                 drawArrow(UP, 5, 10, 5);
1714                                 drawArrow(DOWN, 5, 10, 5);
1715                                 glLineWidth(1.0);
1716                                 break;
1717                         case HLP_HARROW:
1718                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1719
1720                                 glTranslatef(mval[0], mval[1], 0);
1721
1722                                 glLineWidth(3.0);
1723                                 drawArrow(RIGHT, 5, 10, 5);
1724                                 drawArrow(LEFT, 5, 10, 5);
1725                                 glLineWidth(1.0);
1726                                 break;
1727                         case HLP_VARROW:
1728                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1729
1730                                 glTranslatef(mval[0], mval[1], 0);
1731
1732                                 glLineWidth(3.0);
1733                                 drawArrow(UP, 5, 10, 5);
1734                                 drawArrow(DOWN, 5, 10, 5);
1735                                 glLineWidth(1.0);
1736                                 break;
1737                         case HLP_ANGLE:
1738                         {
1739                                 float dx = t->mval[0] - cent[0], dy = t->mval[1] - cent[1];
1740                                 float angle = atan2f(dy, dx);
1741                                 float dist = sqrtf(dx * dx + dy * dy);
1742                                 float delta_angle = min_ff(15.0f / dist, (float)M_PI / 4.0f);
1743                                 float spacing_angle = min_ff(5.0f / dist, (float)M_PI / 12.0f);
1744                                 UI_ThemeColor(TH_VIEW_OVERLAY);
1745
1746                                 setlinestyle(3);
1747                                 glBegin(GL_LINE_STRIP);
1748                                 glVertex2iv(t->mval);
1749                                 glVertex2fv(cent);
1750                                 glEnd();
1751
1752                                 glTranslatef(cent[0] - t->mval[0] + mval[0], cent[1] - t->mval[1] + mval[1], 0);
1753
1754                                 setlinestyle(0);
1755                                 glLineWidth(3.0);
1756                                 drawArc(dist, angle - delta_angle, angle - spacing_angle, 10);
1757                                 drawArc(dist, angle + spacing_angle, angle + delta_angle, 10);
1758
1759                                 glPushMatrix();
1760
1761                                 glTranslatef(cosf(angle - delta_angle) * dist, sinf(angle - delta_angle) * dist, 0);
1762                                 glRotatef(RAD2DEGF(angle - delta_angle), 0, 0, 1);
1763
1764                                 drawArrowHead(DOWN, 5);
1765
1766                                 glPopMatrix();
1767
1768                                 glTranslatef(cosf(angle + delta_angle) * dist, sinf(angle + delta_angle) * dist, 0);
1769                                 glRotatef(RAD2DEGF(angle + delta_angle), 0, 0, 1);
1770
1771                                 drawArrowHead(UP, 5);
1772
1773                                 glLineWidth(1.0);
1774                                 break;
1775                         }
1776                         case HLP_TRACKBALL:
1777                         {
1778                                 unsigned char col[3], col2[3];
1779                                 UI_GetThemeColor3ubv(TH_GRID, col);
1780
1781                                 glTranslatef(mval[0], mval[1], 0);
1782
1783                                 glLineWidth(3.0);
1784
1785                                 UI_make_axis_color(col, col2, 'X');
1786                                 glColor3ubv((GLubyte *)col2);
1787
1788                                 drawArrow(RIGHT, 5, 10, 5);
1789                                 drawArrow(LEFT, 5, 10, 5);
1790
1791                                 UI_make_axis_color(col, col2, 'Y');
1792                                 glColor3ubv((GLubyte *)col2);
1793
1794                                 drawArrow(UP, 5, 10, 5);
1795                                 drawArrow(DOWN, 5, 10, 5);
1796                                 glLineWidth(1.0);
1797                                 break;
1798                         }
1799                 }
1800
1801                 glPopMatrix();
1802         }
1803 }
1804
1805 static void drawTransformView(const struct bContext *C, ARegion *UNUSED(ar), void *arg)
1806 {
1807         TransInfo *t = arg;
1808
1809         drawConstraint(t);
1810         drawPropCircle(C, t);
1811         drawSnapping(C, t);
1812
1813         /* edge slide, vert slide */
1814         drawEdgeSlide(C, t);
1815         drawVertSlide(C, t);
1816 }
1817
1818 /* just draw a little warning message in the top-right corner of the viewport to warn that autokeying is enabled */
1819 static void drawAutoKeyWarning(TransInfo *UNUSED(t), ARegion *ar)
1820 {
1821         rcti rect;
1822         const char *printable = IFACE_("Auto Keying On");
1823         float      printable_size[2];
1824         int xco, yco;
1825
1826         ED_region_visible_rect(ar, &rect);
1827         
1828         BLF_width_and_height_default(printable, BLF_DRAW_STR_DUMMY_MAX, &printable_size[0], &printable_size[1]);
1829         
1830         xco = rect.xmax - (int)printable_size[0] - 10;
1831         yco = rect.ymax - (int)printable_size[1] - 10;
1832         
1833         /* warning text (to clarify meaning of overlays)
1834          * - original color was red to match the icon, but that clashes badly with a less nasty border
1835          */
1836         UI_ThemeColorShade(TH_TEXT_HI, -50);
1837 #ifdef WITH_INTERNATIONAL
1838         BLF_draw_default(xco, ar->winy - 17, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1839 #else
1840         BLF_draw_default_ascii(xco, ar->winy - 17, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1841 #endif
1842         
1843         /* autokey recording icon... */
1844         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1845         glEnable(GL_BLEND);
1846         
1847         xco -= (ICON_DEFAULT_WIDTH + 2);
1848         UI_icon_draw(xco, yco, ICON_REC);
1849         
1850         glDisable(GL_BLEND);
1851 }
1852
1853 static void drawTransformPixel(const struct bContext *UNUSED(C), ARegion *ar, void *arg)
1854 {       
1855         TransInfo *t = arg;
1856         Scene *scene = t->scene;
1857         Object *ob = OBACT;
1858         
1859         /* draw autokeyframing hint in the corner 
1860          * - only draw if enabled (advanced users may be distracted/annoyed), 
1861          *   for objects that will be autokeyframed (no point ohterwise),
1862          *   AND only for the active region (as showing all is too overwhelming)
1863          */
1864         if ((U.autokey_flag & AUTOKEY_FLAG_NOWARNING) == 0) {
1865                 if (ar == t->ar) {
1866                         if (t->flag & (T_OBJECT | T_POSE)) {
1867                                 if (ob && autokeyframe_cfra_can_key(scene, &ob->id)) {
1868                                         drawAutoKeyWarning(t, ar);
1869                                 }
1870                         }
1871                 }
1872         }
1873 }
1874
1875 void saveTransform(bContext *C, TransInfo *t, wmOperator *op)
1876 {
1877         ToolSettings *ts = CTX_data_tool_settings(C);
1878         int constraint_axis[3] = {0, 0, 0};
1879         int proportional = 0;
1880         PropertyRNA *prop;
1881
1882         // Save back mode in case we're in the generic operator
1883         if ((prop = RNA_struct_find_property(op->ptr, "mode"))) {
1884                 RNA_property_enum_set(op->ptr, prop, t->mode);
1885         }
1886
1887         if ((prop = RNA_struct_find_property(op->ptr, "value"))) {
1888                 const float *values = (t->flag & T_AUTOVALUES) ? t->auto_values : t->values;
1889                 if (RNA_property_array_check(prop)) {
1890                         RNA_property_float_set_array(op->ptr, prop, values);
1891                 }
1892                 else {
1893                         RNA_property_float_set(op->ptr, prop, values[0]);
1894                 }
1895         }
1896
1897         /* convert flag to enum */
1898         switch (t->flag & T_PROP_EDIT_ALL) {
1899                 case T_PROP_EDIT:
1900                         proportional = PROP_EDIT_ON;
1901                         break;
1902                 case (T_PROP_EDIT | T_PROP_CONNECTED):
1903                         proportional = PROP_EDIT_CONNECTED;
1904                         break;
1905                 case (T_PROP_EDIT | T_PROP_PROJECTED):
1906                         proportional = PROP_EDIT_PROJECTED;
1907                         break;
1908                 default:
1909                         proportional = PROP_EDIT_OFF;
1910                         break;
1911         }
1912
1913         // If modal, save settings back in scene if not set as operator argument
1914         if (t->flag & T_MODAL) {
1915                 /* save settings if not set in operator */
1916
1917                 /* skip saving proportional edit if it was not actually used */
1918                 if (!(t->options & CTX_NO_PET)) {
1919                         if ((prop = RNA_struct_find_property(op->ptr, "proportional")) &&
1920                             !RNA_property_is_set(op->ptr, prop))
1921                         {
1922                                 if (t->obedit)
1923                                         ts->proportional = proportional;
1924                                 else if (t->options & CTX_MASK)
1925                                         ts->proportional_mask = (proportional != PROP_EDIT_OFF);
1926                                 else
1927                                         ts->proportional_objects = (proportional != PROP_EDIT_OFF);
1928                         }
1929
1930                         if ((prop = RNA_struct_find_property(op->ptr, "proportional_size")) &&
1931                             !RNA_property_is_set(op->ptr, prop))
1932                         {
1933                                 ts->proportional_size = t->prop_size;
1934                         }
1935
1936                         if ((prop = RNA_struct_find_property(op->ptr, "proportional_edit_falloff")) &&
1937                             !RNA_property_is_set(op->ptr, prop))
1938                         {
1939                                 ts->prop_mode = t->prop_mode;
1940                         }
1941                 }
1942                 
1943                 /* do we check for parameter? */
1944                 if (t->modifiers & MOD_SNAP) {
1945                         ts->snap_flag |= SCE_SNAP;
1946                 }
1947                 else {
1948                         ts->snap_flag &= ~SCE_SNAP;
1949                 }
1950
1951                 if (t->spacetype == SPACE_VIEW3D) {
1952                         if ((prop = RNA_struct_find_property(op->ptr, "constraint_orientation")) &&
1953                             !RNA_property_is_set(op->ptr, prop))
1954                         {
1955                                 View3D *v3d = t->view;
1956
1957                                 v3d->twmode = t->current_orientation;
1958                         }
1959                 }
1960         }
1961         
1962         if (RNA_struct_find_property(op->ptr, "proportional")) {
1963                 RNA_enum_set(op->ptr, "proportional", proportional);
1964                 RNA_enum_set(op->ptr, "proportional_edit_falloff", t->prop_mode);
1965                 RNA_float_set(op->ptr, "proportional_size", t->prop_size);
1966         }
1967
1968         if ((prop = RNA_struct_find_property(op->ptr, "axis"))) {
1969                 RNA_property_float_set_array(op->ptr, prop, t->axis);
1970         }
1971
1972         if ((prop = RNA_struct_find_property(op->ptr, "mirror"))) {
1973                 RNA_property_boolean_set(op->ptr, prop, t->flag & T_MIRROR);
1974         }
1975
1976         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis"))) {
1977                 /* constraint orientation can be global, event if user selects something else
1978                  * so use the orientation in the constraint if set
1979                  * */
1980                 if (t->con.mode & CON_APPLY) {
1981                         RNA_enum_set(op->ptr, "constraint_orientation", t->con.orientation);
1982                 }
1983                 else {
1984                         RNA_enum_set(op->ptr, "constraint_orientation", t->current_orientation);
1985                 }
1986
1987                 if (t->con.mode & CON_APPLY) {
1988                         if (t->con.mode & CON_AXIS0) {
1989                                 constraint_axis[0] = 1;
1990                         }
1991                         if (t->con.mode & CON_AXIS1) {
1992                                 constraint_axis[1] = 1;
1993                         }
1994                         if (t->con.mode & CON_AXIS2) {
1995                                 constraint_axis[2] = 1;
1996                         }
1997                 }
1998
1999                 RNA_property_boolean_set_array(op->ptr, prop, constraint_axis);
2000         }
2001 }
2002
2003 /* note: caller needs to free 't' on a 0 return */
2004 bool initTransform(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *event, int mode)
2005 {
2006         int options = 0;
2007         PropertyRNA *prop;
2008
2009         t->context = C;
2010
2011         /* added initialize, for external calls to set stuff in TransInfo, like undo string */
2012
2013         t->state = TRANS_STARTING;
2014
2015         if ((prop = RNA_struct_find_property(op->ptr, "texture_space")) && RNA_property_is_set(op->ptr, prop)) {
2016                 if (RNA_property_boolean_get(op->ptr, prop)) {
2017                         options |= CTX_TEXTURE;
2018                 }
2019         }
2020
2021         t->options = options;
2022
2023         t->mode = mode;
2024
2025         t->launch_event = event ? event->type : -1;
2026
2027         if (t->launch_event == EVT_TWEAK_R) {
2028                 t->launch_event = RIGHTMOUSE;
2029         }
2030         else if (t->launch_event == EVT_TWEAK_L) {
2031                 t->launch_event = LEFTMOUSE;
2032         }
2033
2034         // XXX Remove this when wm_operator_call_internal doesn't use window->eventstate (which can have type = 0)
2035         // For manipulator only, so assume LEFTMOUSE
2036         if (t->launch_event == 0) {
2037                 t->launch_event = LEFTMOUSE;
2038         }
2039
2040         initTransInfo(C, t, op, event);
2041
2042         if (t->spacetype == SPACE_VIEW3D) {
2043                 //calc_manipulator_stats(curarea);
2044                 initTransformOrientation(C, t);
2045
2046                 t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);
2047                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2048                 t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2049                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2050         }
2051         else if (t->spacetype == SPACE_IMAGE) {
2052                 unit_m3(t->spacemtx);
2053                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2054                 //t->draw_handle_pixel = ED_region_draw_cb_activate(t->ar->type, drawTransformPixel, t, REGION_DRAW_POST_PIXEL);
2055                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2056         }
2057         else if (t->spacetype == SPACE_CLIP) {
2058                 unit_m3(t->spacemtx);
2059                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2060                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2061         }
2062         else if (t->spacetype == SPACE_NODE) {
2063                 unit_m3(t->spacemtx);
2064                 /*t->draw_handle_apply = ED_region_draw_cb_activate(t->ar->type, drawTransformApply, t, REGION_DRAW_PRE_VIEW);*/
2065                 t->draw_handle_view = ED_region_draw_cb_activate(t->ar->type, drawTransformView, t, REGION_DRAW_POST_VIEW);
2066                 t->draw_handle_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), helpline_poll, drawHelpline, t);
2067         }
2068         else
2069                 unit_m3(t->spacemtx);
2070
2071         createTransData(C, t);          // make TransData structs from selection
2072
2073         if (t->total == 0) {
2074                 postTrans(C, t);
2075                 return 0;
2076         }
2077
2078         if (event) {
2079                 /* keymap for shortcut header prints */
2080                 t->keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
2081
2082                 /* Stupid code to have Ctrl-Click on manipulator work ok
2083                  *
2084                  * do this only for translation/rotation/resize due to only this
2085                  * moded are available from manipulator and doing such check could
2086                  * lead to keymap conflicts for other modes (see #31584)
2087                  */
2088                 if (ELEM3(mode, TFM_TRANSLATION, TFM_ROTATION, TFM_RESIZE)) {
2089                         wmKeyMapItem *kmi;
2090
2091                         for (kmi = t->keymap->items.first; kmi; kmi = kmi->next) {
2092                                 if (kmi->propvalue == TFM_MODAL_SNAP_INV_ON && kmi->val == KM_PRESS) {
2093                                         if ((ELEM(kmi->type, LEFTCTRLKEY, RIGHTCTRLKEY) &&   event->ctrl)  ||
2094                                             (ELEM(kmi->type, LEFTSHIFTKEY, RIGHTSHIFTKEY) && event->shift) ||
2095                                             (ELEM(kmi->type, LEFTALTKEY, RIGHTALTKEY) &&     event->alt)   ||
2096                                             ((kmi->type == OSKEY) &&                         event->oskey) )
2097                                         {
2098                                                 t->modifiers |= MOD_SNAP_INVERT;
2099                                         }
2100                                         break;
2101                                 }
2102                         }
2103                 }
2104         }
2105
2106         initSnapping(t, op); // Initialize snapping data AFTER mode flags
2107
2108         /* EVIL! posemode code can switch translation to rotate when 1 bone is selected. will be removed (ton) */
2109         /* EVIL2: we gave as argument also texture space context bit... was cleared */
2110         /* EVIL3: extend mode for animation editors also switches modes... but is best way to avoid duplicate code */
2111         mode = t->mode;
2112
2113         calculatePropRatio(t);
2114         calculateCenter(t);
2115
2116         initMouseInput(t, &t->mouse, t->center2d, t->imval);
2117
2118         switch (mode) {
2119                 case TFM_TRANSLATION:
2120                         initTranslation(t);
2121                         break;
2122                 case TFM_ROTATION:
2123                         initRotation(t);
2124                         break;
2125                 case TFM_RESIZE:
2126                         initResize(t);
2127                         break;
2128                 case TFM_SKIN_RESIZE:
2129                         initSkinResize(t);
2130                         break;
2131                 case TFM_TOSPHERE:
2132                         initToSphere(t);
2133                         break;
2134                 case TFM_SHEAR:
2135                         initShear(t);
2136                         break;
2137                 case TFM_BEND:
2138                         initBend(t);
2139                         break;
2140                 case TFM_SHRINKFATTEN:
2141                         initShrinkFatten(t);
2142                         break;
2143                 case TFM_TILT:
2144                         initTilt(t);
2145                         break;
2146                 case TFM_CURVE_SHRINKFATTEN:
2147                         initCurveShrinkFatten(t);
2148                         break;
2149                 case TFM_MASK_SHRINKFATTEN:
2150                         initMaskShrinkFatten(t);
2151                         break;
2152                 case TFM_TRACKBALL:
2153                         initTrackball(t);
2154                         break;
2155                 case TFM_PUSHPULL:
2156                         initPushPull(t);
2157                         break;
2158                 case TFM_CREASE:
2159                         initCrease(t);
2160                         break;
2161                 case TFM_BONESIZE:
2162                 {   /* used for both B-Bone width (bonesize) as for deform-dist (envelope) */
2163                         bArmature *arm = t->poseobj->data;
2164                         if (arm->drawtype == ARM_ENVELOPE)
2165                                 initBoneEnvelope(t);
2166                         else
2167                                 initBoneSize(t);
2168                         break;
2169                 }
2170                 case TFM_BONE_ENVELOPE:
2171                         initBoneEnvelope(t);
2172                         break;
2173                 case TFM_EDGE_SLIDE:
2174                         initEdgeSlide(t);
2175                         break;
2176                 case TFM_VERT_SLIDE:
2177                         initVertSlide(t);
2178                         break;
2179                 case TFM_BONE_ROLL:
2180                         initBoneRoll(t);
2181                         break;
2182                 case TFM_TIME_TRANSLATE:
2183                         initTimeTranslate(t);
2184                         break;
2185                 case TFM_TIME_SLIDE:
2186                         initTimeSlide(t);
2187                         break;
2188                 case TFM_TIME_SCALE:
2189                         initTimeScale(t);
2190                         break;
2191                 case TFM_TIME_DUPLICATE:
2192                         /* same as TFM_TIME_EXTEND, but we need the mode info for later
2193                          * so that duplicate-culling will work properly
2194                          */
2195                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2196                                 initTranslation(t);
2197                         else
2198                                 initTimeTranslate(t);
2199                         t->mode = mode;
2200                         break;
2201                 case TFM_TIME_EXTEND:
2202                         /* now that transdata has been made, do like for TFM_TIME_TRANSLATE (for most Animation
2203                          * Editors because they have only 1D transforms for time values) or TFM_TRANSLATION
2204                          * (for Graph/NLA Editors only since they uses 'standard' transforms to get 2D movement)
2205                          * depending on which editor this was called from
2206                          */
2207                         if (ELEM(t->spacetype, SPACE_IPO, SPACE_NLA))
2208                                 initTranslation(t);
2209                         else
2210                                 initTimeTranslate(t);
2211                         break;
2212                 case TFM_BAKE_TIME:
2213                         initBakeTime(t);
2214                         break;
2215                 case TFM_MIRROR:
2216                         initMirror(t);
2217                         break;
2218                 case TFM_BWEIGHT:
2219                         initBevelWeight(t);
2220                         break;
2221                 case TFM_ALIGN:
2222                         initAlign(t);
2223                         break;
2224                 case TFM_SEQ_SLIDE:
2225                         initSeqSlide(t);
2226                         break;
2227         }
2228
2229         if (t->state == TRANS_CANCEL) {
2230                 postTrans(C, t);
2231                 return 0;
2232         }
2233
2234
2235         /* overwrite initial values if operator supplied a non-null vector */
2236         if ((prop = RNA_struct_find_property(op->ptr, "value")) && RNA_property_is_set(op->ptr, prop)) {
2237                 float values[4] = {0}; /* in case value isn't length 4, avoid uninitialized memory  */
2238
2239                 if (RNA_property_array_check(prop)) {
2240                         RNA_float_get_array(op->ptr, "value", values);
2241                 }
2242                 else {
2243                         values[0] = RNA_float_get(op->ptr, "value");
2244                 }
2245
2246                 copy_v4_v4(t->values, values);
2247                 copy_v4_v4(t->auto_values, values);
2248                 t->flag |= T_AUTOVALUES;
2249         }
2250
2251         /* Transformation axis from operator */
2252         if ((prop = RNA_struct_find_property(op->ptr, "axis")) && RNA_property_is_set(op->ptr, prop)) {
2253                 RNA_property_float_get_array(op->ptr, prop, t->axis);
2254                 normalize_v3(t->axis);
2255                 copy_v3_v3(t->axis_orig, t->axis);
2256         }
2257
2258         /* Constraint init from operator */
2259         if ((prop = RNA_struct_find_property(op->ptr, "constraint_axis")) && RNA_property_is_set(op->ptr, prop)) {
2260                 int constraint_axis[3];
2261
2262                 RNA_property_boolean_get_array(op->ptr, prop, constraint_axis);
2263
2264                 if (constraint_axis[0] || constraint_axis[1] || constraint_axis[2]) {
2265                         t->con.mode |= CON_APPLY;
2266
2267                         if (constraint_axis[0]) {
2268                                 t->con.mode |= CON_AXIS0;
2269                         }
2270                         if (constraint_axis[1]) {
2271                                 t->con.mode |= CON_AXIS1;
2272                         }
2273                         if (constraint_axis[2]) {
2274                                 t->con.mode |= CON_AXIS2;
2275                         }
2276
2277                         setUserConstraint(t, t->current_orientation, t->con.mode, "%s");
2278                 }
2279         }
2280
2281         t->context = NULL;
2282
2283         return 1;
2284 }
2285
2286 void transformApply(bContext *C, TransInfo *t)
2287 {
2288         t->context = C;
2289
2290         if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT))) {
2291                 selectConstraint(t);
2292                 if (t->transform) {
2293                         t->transform(t, t->mval);  // calls recalcData()
2294                         viewRedrawForce(C, t);
2295                 }
2296                 t->redraw = TREDRAW_NOTHING;
2297         }
2298         else if (t->redraw & TREDRAW_SOFT) {
2299                 viewRedrawForce(C, t);
2300         }
2301
2302         /* If auto confirm is on, break after one pass */
2303         if (t->options & CTX_AUTOCONFIRM) {
2304                 t->state = TRANS_CONFIRM;
2305         }
2306
2307         t->context = NULL;
2308 }
2309
2310 static void drawTransformApply(const bContext *C, ARegion *UNUSED(ar), void *arg)
2311 {
2312         TransInfo *t = arg;
2313
2314         if (t->redraw & TREDRAW_SOFT) {
2315                 t->redraw |= TREDRAW_HARD;
2316                 transformApply((bContext *)C, t);
2317         }
2318 }
2319
2320 int transformEnd(bContext *C, TransInfo *t)
2321 {
2322         int exit_code = OPERATOR_RUNNING_MODAL;
2323
2324         t->context = C;
2325
2326         if (t->state != TRANS_STARTING && t->state != TRANS_RUNNING) {
2327                 /* handle restoring objects */
2328                 if (t->state == TRANS_CANCEL) {
2329                         /* exception, edge slide transformed UVs too */
2330                         if (t->mode == TFM_EDGE_SLIDE)
2331                                 doEdgeSlide(t, 0.0f);
2332                         
2333                         exit_code = OPERATOR_CANCELLED;
2334                         restoreTransObjects(t); // calls recalcData()
2335                 }
2336                 else {
2337                         exit_code = OPERATOR_FINISHED;
2338                 }
2339
2340                 /* aftertrans does insert keyframes, and clears base flags; doesn't read transdata */
2341                 special_aftertrans_update(C, t);
2342
2343                 /* free data */
2344                 postTrans(C, t);
2345
2346                 /* send events out for redraws */
2347                 viewRedrawPost(C, t);
2348
2349                 viewRedrawForce(C, t);
2350         }
2351
2352         t->context = NULL;
2353
2354         return exit_code;
2355 }
2356
2357 /* ************************** TRANSFORM LOCKS **************************** */
2358
2359 static void protectedTransBits(short protectflag, float vec[3])
2360 {
2361         if (protectflag & OB_LOCK_LOCX)
2362                 vec[0] = 0.0f;
2363         if (protectflag & OB_LOCK_LOCY)
2364                 vec[1] = 0.0f;
2365         if (protectflag & OB_LOCK_LOCZ)
2366                 vec[2] = 0.0f;
2367 }
2368
2369 static void protectedSizeBits(short protectflag, float size[3])
2370 {
2371         if (protectflag & OB_LOCK_SCALEX)
2372                 size[0] = 1.0f;
2373         if (protectflag & OB_LOCK_SCALEY)
2374                 size[1] = 1.0f;
2375         if (protectflag & OB_LOCK_SCALEZ)
2376                 size[2] = 1.0f;
2377 }
2378
2379 static void protectedRotateBits(short protectflag, float eul[3], const float oldeul[3])
2380 {
2381         if (protectflag & OB_LOCK_ROTX)
2382                 eul[0] = oldeul[0];
2383         if (protectflag & OB_LOCK_ROTY)
2384                 eul[1] = oldeul[1];
2385         if (protectflag & OB_LOCK_ROTZ)
2386                 eul[2] = oldeul[2];
2387 }
2388
2389
2390 /* this function only does the delta rotation */
2391 /* axis-angle is usually internally stored as quats... */
2392 static void protectedAxisAngleBits(short protectflag, float axis[3], float *angle, float oldAxis[3], float oldAngle)
2393 {
2394         /* check that protection flags are set */
2395         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2396                 return;
2397         
2398         if (protectflag & OB_LOCK_ROT4D) {
2399                 /* axis-angle getting limited as 4D entities that they are... */
2400                 if (protectflag & OB_LOCK_ROTW)
2401                         *angle = oldAngle;
2402                 if (protectflag & OB_LOCK_ROTX)
2403                         axis[0] = oldAxis[0];
2404                 if (protectflag & OB_LOCK_ROTY)
2405                         axis[1] = oldAxis[1];
2406                 if (protectflag & OB_LOCK_ROTZ)
2407                         axis[2] = oldAxis[2];
2408         }
2409         else {
2410                 /* axis-angle get limited with euler... */
2411                 float eul[3], oldeul[3];
2412                 
2413                 axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, axis, *angle);
2414                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, oldAxis, oldAngle);
2415                 
2416                 if (protectflag & OB_LOCK_ROTX)
2417                         eul[0] = oldeul[0];
2418                 if (protectflag & OB_LOCK_ROTY)
2419                         eul[1] = oldeul[1];
2420                 if (protectflag & OB_LOCK_ROTZ)
2421                         eul[2] = oldeul[2];
2422                 
2423                 eulO_to_axis_angle(axis, angle, eul, EULER_ORDER_DEFAULT);
2424                 
2425                 /* when converting to axis-angle, we need a special exception for the case when there is no axis */
2426                 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
2427                         /* for now, rotate around y-axis then (so that it simply becomes the roll) */
2428                         axis[1] = 1.0f;
2429                 }
2430         }
2431 }
2432
2433 /* this function only does the delta rotation */
2434 static void protectedQuaternionBits(short protectflag, float quat[4], const float oldquat[4])
2435 {
2436         /* check that protection flags are set */
2437         if ((protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) == 0)
2438                 return;
2439         
2440         if (protectflag & OB_LOCK_ROT4D) {
2441                 /* quaternions getting limited as 4D entities that they are... */
2442                 if (protectflag & OB_LOCK_ROTW)
2443                         quat[0] = oldquat[0];
2444                 if (protectflag & OB_LOCK_ROTX)
2445                         quat[1] = oldquat[1];
2446                 if (protectflag & OB_LOCK_ROTY)
2447                         quat[2] = oldquat[2];
2448                 if (protectflag & OB_LOCK_ROTZ)
2449                         quat[3] = oldquat[3];
2450         }
2451         else {
2452                 /* quaternions get limited with euler... (compatibility mode) */
2453                 float eul[3], oldeul[3], nquat[4], noldquat[4];
2454                 float qlen;
2455
2456                 qlen = normalize_qt_qt(nquat, quat);
2457                 normalize_qt_qt(noldquat, oldquat);
2458
2459                 quat_to_eul(eul, nquat);
2460                 quat_to_eul(oldeul, noldquat);
2461
2462                 if (protectflag & OB_LOCK_ROTX)
2463                         eul[0] = oldeul[0];
2464                 if (protectflag & OB_LOCK_ROTY)
2465                         eul[1] = oldeul[1];
2466                 if (protectflag & OB_LOCK_ROTZ)
2467                         eul[2] = oldeul[2];
2468
2469                 eul_to_quat(quat, eul);
2470
2471                 /* restore original quat size */
2472                 mul_qt_fl(quat, qlen);
2473                 
2474                 /* quaternions flip w sign to accumulate rotations correctly */
2475                 if ((nquat[0] < 0.0f && quat[0] > 0.0f) ||
2476                     (nquat[0] > 0.0f && quat[0] < 0.0f))
2477                 {
2478                         mul_qt_fl(quat, -1.0f);
2479                 }
2480         }
2481 }
2482
2483 /* ******************* TRANSFORM LIMITS ********************** */
2484
2485 static void constraintTransLim(TransInfo *t, TransData *td)
2486 {
2487         if (td->con) {
2488                 bConstraintTypeInfo *ctiLoc = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_LOCLIMIT);
2489                 bConstraintTypeInfo *ctiDist = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_DISTLIMIT);
2490                 
2491                 bConstraintOb cob = {NULL};
2492                 bConstraint *con;
2493                 float ctime = (float)(t->scene->r.cfra);
2494                 
2495                 /* Make a temporary bConstraintOb for using these limit constraints
2496                  *  - they only care that cob->matrix is correctly set ;-)
2497                  *      - current space should be local
2498                  */
2499                 unit_m4(cob.matrix);
2500                 copy_v3_v3(cob.matrix[3], td->loc);
2501                 
2502                 /* Evaluate valid constraints */
2503                 for (con = td->con; con; con = con->next) {
2504                         bConstraintTypeInfo *cti = NULL;
2505                         ListBase targets = {NULL, NULL};
2506                         
2507                         /* only consider constraint if enabled */
2508                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2509                         if (con->enforce == 0.0f) continue;
2510                         
2511                         /* only use it if it's tagged for this purpose (and the right type) */
2512                         if (con->type == CONSTRAINT_TYPE_LOCLIMIT) {
2513                                 bLocLimitConstraint *data = con->data;
2514                                 
2515                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2516                                         continue;
2517                                 cti = ctiLoc;
2518                         }
2519                         else if (con->type == CONSTRAINT_TYPE_DISTLIMIT) {
2520                                 bDistLimitConstraint *data = con->data;
2521                                 
2522                                 if ((data->flag & LIMITDIST_TRANSFORM) == 0)
2523                                         continue;
2524                                 cti = ctiDist;
2525                         }
2526                         
2527                         if (cti) {
2528                                 /* do space conversions */
2529                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2530                                         /* just multiply by td->mtx (this should be ok) */
2531                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2532                                 }
2533                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2534                                         /* skip... incompatable spacetype */
2535                                         continue;
2536                                 }
2537                                 
2538                                 /* get constraint targets if needed */
2539                                 BKE_constraint_targets_for_solving_get(con, &cob, &targets, ctime);
2540                                 
2541                                 /* do constraint */
2542                                 cti->evaluate_constraint(con, &cob, &targets);
2543                                 
2544                                 /* convert spaces again */
2545                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2546                                         /* just multiply by td->smtx (this should be ok) */
2547                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2548                                 }
2549                                 
2550                                 /* free targets list */
2551                                 BLI_freelistN(&targets);
2552                         }
2553                 }
2554                 
2555                 /* copy results from cob->matrix */
2556                 copy_v3_v3(td->loc, cob.matrix[3]);
2557         }
2558 }
2559
2560 static void constraintob_from_transdata(bConstraintOb *cob, TransData *td)
2561 {
2562         /* Make a temporary bConstraintOb for use by limit constraints
2563          *  - they only care that cob->matrix is correctly set ;-)
2564          *      - current space should be local
2565          */
2566         memset(cob, 0, sizeof(bConstraintOb));
2567         if (td->ext) {
2568                 if (td->ext->rotOrder == ROT_MODE_QUAT) {
2569                         /* quats */
2570                         /* objects and bones do normalization first too, otherwise
2571                          * we don't necessarily end up with a rotation matrix, and
2572                          * then conversion back to quat gives a different result */
2573                         float quat[4];
2574                         normalize_qt_qt(quat, td->ext->quat);
2575                         quat_to_mat4(cob->matrix, quat);
2576                 }
2577                 else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2578                         /* axis angle */
2579                         axis_angle_to_mat4(cob->matrix, &td->ext->quat[1], td->ext->quat[0]);
2580                 }
2581                 else {
2582                         /* eulers */
2583                         eulO_to_mat4(cob->matrix, td->ext->rot, td->ext->rotOrder);
2584                 }
2585         }
2586 }
2587
2588 static void constraintRotLim(TransInfo *UNUSED(t), TransData *td)
2589 {
2590         if (td->con) {
2591                 bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_ROTLIMIT);
2592                 bConstraintOb cob;
2593                 bConstraint *con;
2594                 bool do_limit = false;
2595
2596                 /* Evaluate valid constraints */
2597                 for (con = td->con; con; con = con->next) {
2598                         /* only consider constraint if enabled */
2599                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2600                         if (con->enforce == 0.0f) continue;
2601
2602                         /* we're only interested in Limit-Rotation constraints */
2603                         if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
2604                                 bRotLimitConstraint *data = con->data;
2605
2606                                 /* only use it if it's tagged for this purpose */
2607                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2608                                         continue;
2609
2610                                 /* skip incompatable spacetypes */
2611                                 if (!ELEM(con->ownspace, CONSTRAINT_SPACE_WORLD, CONSTRAINT_SPACE_LOCAL))
2612                                         continue;
2613
2614                                 /* only do conversion if necessary, to preserve quats and eulers */
2615                                 if (do_limit == false) {
2616                                         constraintob_from_transdata(&cob, td);
2617                                         do_limit = true;
2618                                 }
2619
2620                                 /* do space conversions */
2621                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2622                                         /* just multiply by td->mtx (this should be ok) */
2623                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2624                                 }
2625                                 
2626                                 /* do constraint */
2627                                 cti->evaluate_constraint(con, &cob, NULL);
2628                                 
2629                                 /* convert spaces again */
2630                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2631                                         /* just multiply by td->smtx (this should be ok) */
2632                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2633                                 }
2634                         }
2635                 }
2636                 
2637                 if (do_limit) {
2638                         /* copy results from cob->matrix */
2639                         if (td->ext->rotOrder == ROT_MODE_QUAT) {
2640                                 /* quats */
2641                                 mat4_to_quat(td->ext->quat, cob.matrix);
2642                         }
2643                         else if (td->ext->rotOrder == ROT_MODE_AXISANGLE) {
2644                                 /* axis angle */
2645                                 mat4_to_axis_angle(&td->ext->quat[1], &td->ext->quat[0], cob.matrix);
2646                         }
2647                         else {
2648                                 /* eulers */
2649                                 mat4_to_eulO(td->ext->rot, td->ext->rotOrder, cob.matrix);
2650                         }
2651                 }
2652         }
2653 }
2654
2655 static void constraintSizeLim(TransInfo *t, TransData *td)
2656 {
2657         if (td->con && td->ext) {
2658                 bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_SIZELIMIT);
2659                 bConstraintOb cob = {NULL};
2660                 bConstraint *con;
2661                 float size_sign[3], size_abs[3];
2662                 int i;
2663                 
2664                 /* Make a temporary bConstraintOb for using these limit constraints
2665                  *  - they only care that cob->matrix is correctly set ;-)
2666                  *      - current space should be local
2667                  */
2668                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2669                         /* scale val and reset size */
2670                         return; // TODO: fix this case
2671                 }
2672                 else {
2673                         /* Reset val if SINGLESIZE but using a constraint */
2674                         if (td->flag & TD_SINGLESIZE)
2675                                 return;
2676
2677                         /* separate out sign to apply back later */
2678                         for (i = 0; i < 3; i++) {
2679                                 size_sign[i] = signf(td->ext->size[i]);
2680                                 size_abs[i] = fabsf(td->ext->size[i]);
2681                         }
2682                         
2683                         size_to_mat4(cob.matrix, size_abs);
2684                 }
2685                 
2686                 /* Evaluate valid constraints */
2687                 for (con = td->con; con; con = con->next) {
2688                         /* only consider constraint if enabled */
2689                         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) continue;
2690                         if (con->enforce == 0.0f) continue;
2691                         
2692                         /* we're only interested in Limit-Scale constraints */
2693                         if (con->type == CONSTRAINT_TYPE_SIZELIMIT) {
2694                                 bSizeLimitConstraint *data = con->data;
2695                                 
2696                                 /* only use it if it's tagged for this purpose */
2697                                 if ((data->flag2 & LIMIT_TRANSFORM) == 0)
2698                                         continue;
2699                                 
2700                                 /* do space conversions */
2701                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2702                                         /* just multiply by td->mtx (this should be ok) */
2703                                         mul_m4_m3m4(cob.matrix, td->mtx, cob.matrix);
2704                                 }
2705                                 else if (con->ownspace != CONSTRAINT_SPACE_LOCAL) {
2706                                         /* skip... incompatible spacetype */
2707                                         continue;
2708                                 }
2709                                 
2710                                 /* do constraint */
2711                                 cti->evaluate_constraint(con, &cob, NULL);
2712                                 
2713                                 /* convert spaces again */
2714                                 if (con->ownspace == CONSTRAINT_SPACE_WORLD) {
2715                                         /* just multiply by td->smtx (this should be ok) */
2716                                         mul_m4_m3m4(cob.matrix, td->smtx, cob.matrix);
2717                                 }
2718                         }
2719                 }
2720
2721                 /* copy results from cob->matrix */
2722                 if ((td->flag & TD_SINGLESIZE) && !(t->con.mode & CON_APPLY)) {
2723                         /* scale val and reset size */
2724                         return; // TODO: fix this case
2725                 }
2726                 else {
2727                         /* Reset val if SINGLESIZE but using a constraint */
2728                         if (td->flag & TD_SINGLESIZE)
2729                                 return;
2730
2731                         /* extrace scale from matrix and apply back sign */
2732                         mat4_to_size(td->ext->size, cob.matrix);
2733                         mul_v3_v3(td->ext->size, size_sign);
2734                 }
2735         }
2736 }
2737
2738
2739 /* -------------------------------------------------------------------- */
2740 /* Transform (Bend) */
2741
2742 /** \name Transform Bend
2743  * \{ */
2744
2745 struct BendCustomData {
2746         float warp_sta[3];
2747         float warp_end[3];
2748
2749         float warp_nor[3];
2750         float warp_tan[3];
2751
2752         /* for applying the mouse distance */
2753         float warp_init_dist;
2754 };
2755
2756 static void initBend(TransInfo *t)
2757 {
2758         const float mval_fl[2] = {UNPACK2(t->mval)};
2759         const float *curs;
2760         float tvec[3];
2761         struct BendCustomData *data;
2762         
2763         t->mode = TFM_BEND;
2764         t->transform = Bend;
2765         t->handleEvent = handleEventBend;
2766         
2767         setInputPostFct(&t->mouse, postInputRotation);
2768         initMouseInputMode(t, &t->mouse, INPUT_ANGLE_SPRING);
2769         
2770         t->idx_max = 1;
2771         t->num.idx_max = 1;
2772         t->snap[0] = 0.0f;
2773         t->snap[1] = DEG2RAD(5.0);
2774         t->snap[2] = DEG2RAD(1.0);
2775
2776         copy_v3_fl(t->num.val_inc, t->snap[1]);
2777         t->num.unit_sys = t->scene->unit.system;
2778         t->num.unit_use_radians = (t->scene->unit.system_rotation == USER_UNIT_ROT_RADIANS);
2779         t->num.unit_type[0] = B_UNIT_ROTATION;
2780         t->num.unit_type[1] = B_UNIT_LENGTH;
2781
2782         t->flag |= T_NO_CONSTRAINT;
2783
2784         //copy_v3_v3(t->center, ED_view3d_cursor3d_get(t->scene, t->view));
2785         calculateCenterCursor(t);
2786
2787         t->val = 0.0f;
2788
2789         data = MEM_callocN(sizeof(*data), __func__);
2790
2791         curs = ED_view3d_cursor3d_get(t->scene, t->view);
2792         copy_v3_v3(data->warp_sta, curs);
2793         ED_view3d_win_to_3d(t->ar, curs, mval_fl, data->warp_end);
2794
2795         copy_v3_v3(data->warp_nor, t->viewinv[2]);
2796         if (t->flag & T_EDIT) {
2797                 sub_v3_v3(data->warp_sta, t->obedit->obmat[3]);
2798                 sub_v3_v3(data->warp_end, t->obedit->obmat[3]);
2799         }
2800         normalize_v3(data->warp_nor);
2801
2802         /* tangent */
2803         sub_v3_v3v3(tvec, data->warp_end, data->warp_sta);
2804         cross_v3_v3v3(data->warp_tan, tvec, data->warp_nor);
2805         normalize_v3(data->warp_tan);
2806
2807         data->warp_init_dist = len_v3v3(data->warp_end, data->warp_sta);
2808
2809         t->customData = data;
2810 }
2811
2812 static eRedrawFlag handleEventBend(TransInfo *UNUSED(t), const wmEvent *event)
2813 {
2814         eRedrawFlag status = TREDRAW_NOTHING;
2815         
2816         if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
2817                 status = TREDRAW_HARD;
2818         }
2819         
2820         return status;
2821 }
2822
2823 static void Bend(TransInfo *t, const int UNUSED(mval[2]))
2824 {
2825         TransData *td = t->data;
2826         float vec[3];
2827         float pivot[3];
2828         float warp_end_radius[3];
2829         int i;
2830         char str[MAX_INFO_LEN];
2831         const struct BendCustomData *data = t->customData;
2832         const bool is_clamp = (t->flag & T_ALT_TRANSFORM) == 0;
2833
2834         union {
2835                 struct { float angle, scale; };
2836                 float vector[2];
2837         } values;
2838
2839         /* amount of radians for bend */
2840         copy_v2_v2(values.vector, t->values);
2841
2842 #if 0
2843         snapGrid(t, angle_rad);
2844 #else
2845         /* hrmf, snapping radius is using 'angle' steps, need to convert to something else
2846          * this isnt essential but nicer to give reasonable snapping values for radius */
2847         if (t->tsnap.mode == SCE_SNAP_MODE_INCREMENT) {
2848                 const float radius_snap = 0.1f;
2849                 const float snap_hack = (t->snap[1] * data->warp_init_dist) / radius_snap;
2850                 values.scale *= snap_hack;
2851                 snapGridIncrement(t, values.vector);
2852                 values.scale /= snap_hack;
2853         }
2854 #endif
2855
2856         if (applyNumInput(&t->num, values.vector)) {
2857                 values.scale = values.scale / data->warp_init_dist;
2858         }
2859
2860         /* header print for NumInput */
2861         if (hasNumInput(&t->num)) {
2862                 char c[NUM_STR_REP_LEN * 2];
2863
2864                 outputNumInput(&(t->num), c);
2865           &nb