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