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