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