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