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