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