4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
33 #include "ED_transform.h"
35 /* ************************** Types ***************************** */
48 struct wmOperatorType;
55 typedef struct NDofInput {
62 typedef struct NumInput {
65 short flag; /* Different flags to indicate different behaviors */
66 char inv[3]; /* If the value is inverted or not */
67 float val[3]; /* Direct value of the input */
68 int ctrl[3]; /* Control to indicate what to do with the numbers that are typed */
72 The ctrl value has different meaning:
73 0 : No value has been typed
75 otherwise, |value| - 1 is where the cursor is located after the period
76 Positive : number is positive
77 Negative : number is negative
80 typedef struct TransSnap {
86 float snapPoint[3]; /* snapping from this point */
87 float snapTarget[3]; /* to this point */
90 float dist; // Distance from snapPoint to snapTarget
92 void (*applySnap)(struct TransInfo *, float *);
93 void (*calcSnap)(struct TransInfo *, float *);
94 void (*targetSnap)(struct TransInfo *);
95 float (*distance)(struct TransInfo *, float p1[3], float p2[3]); // Get the transform distance between two points (used by Closest snap)
98 typedef struct TransCon {
99 char text[50]; /* Description of the Constraint for header_print */
100 float mtx[3][3]; /* Matrix of the Constraint space */
101 float imtx[3][3]; /* Inverse Matrix of the Constraint space */
102 float pmtx[3][3]; /* Projection Constraint Matrix (same as imtx with some axis == 0) */
103 float center[3]; /* transformation center to define where to draw the view widget
104 ALWAYS in global space. Unlike the transformation center */
105 short imval[2]; /* initial mouse value for visual calculation */
106 /* the one in TransInfo is not garanty to stay the same (Rotates change it) */
107 int mode; /* Mode flags of the Constraint */
108 void (*drawExtra)(struct TransInfo *);
109 /* For constraints that needs to draw differently from the other
110 uses this instead of the generic draw function */
111 void (*applyVec)(struct TransInfo *, struct TransData *, float *, float *, float *);
112 /* Apply function pointer for linear vectorial transformation */
113 /* The last three parameters are pointers to the in/out/printable vectors */
114 void (*applySize)(struct TransInfo *, struct TransData *, float [3][3]);
115 /* Apply function pointer for size transformation */
116 void (*applyRot)(struct TransInfo *, struct TransData *, float [3], float *);
117 /* Apply function pointer for rotation transformation */
120 typedef struct TransDataIpokey {
121 int flag; /* which keys */
122 float *locx, *locy, *locz; /* channel pointers */
123 float *rotx, *roty, *rotz;
124 float *quatx, *quaty, *quatz, *quatw;
125 float *sizex, *sizey, *sizez;
126 float oldloc[9]; /* storage old values */
132 typedef struct TransDataExtension {
133 float drot[3]; /* Initial object drot */
134 float dsize[3]; /* Initial object dsize */
135 float *rot; /* Rotation of the data to transform (Faculative) */
136 float irot[3]; /* Initial rotation */
137 float *quat; /* Rotation quaternion of the data to transform (Faculative) */
138 float iquat[4]; /* Initial rotation quaternion */
139 float *size; /* Size of the data to transform (Faculative) */
140 float isize[3]; /* Initial size */
141 float obmat[4][4]; /* Object matrix */
142 } TransDataExtension;
144 typedef struct TransData2D {
145 float loc[3]; /* Location of data used to transform (x,y,0) */
146 float *loc2d; /* Pointer to real 2d location of data */
149 /* we need to store 2 handles for each transdata incase the other handle wasnt selected */
150 typedef struct TransDataCurveHandleFlags {
153 } TransDataCurveHandleFlags;
155 /* for sequencer transform */
156 typedef struct TransDataSeq {
157 struct Sequence *seq;
158 int flag; /* a copy of seq->flag that may be modified for nested strips */
159 short start_offset; /* use this so we can have transform data at the strips start, but apply correctly to the start frame */
160 short sel_flag; /* one of SELECT, SEQ_LEFTSEL and SEQ_RIGHTSEL */
164 /* for NLA transform (stored in td->extra pointer) */
165 typedef struct TransDataNla {
166 ID *id; /* ID-block NLA-data is attached to */
168 struct NlaTrack *oldTrack; /* Original NLA-Track that the strip belongs to */
169 struct NlaTrack *nlt; /* Current NLA-Track that the strip belongs to */
171 struct NlaStrip *strip; /* NLA-strip this data represents */
173 /* dummy values for transform to write in - must have 3 elements... */
174 float h1[3]; /* start handle */
175 float h2[3]; /* end handle */
177 int trackIndex; /* index of track that strip is currently in */
178 int handle; /* handle-index: 0 for dummy entry, -1 for start, 1 for end, 2 for both ends */
181 typedef struct TransData {
182 float dist; /* Distance needed to affect element (for Proportionnal Editing) */
183 float rdist; /* Distance to the nearest element (for Proportionnal Editing) */
184 float factor; /* Factor of the transformation (for Proportionnal Editing) */
185 float *loc; /* Location of the data to transform */
186 float iloc[3]; /* Initial location */
187 float *val; /* Value pointer for special transforms */
188 float ival; /* Old value*/
189 float center[3]; /* Individual data center */
190 float mtx[3][3]; /* Transformation matrix from data space to global space */
191 float smtx[3][3]; /* Transformation matrix from global space to data space */
192 float axismtx[3][3];/* Axis orientation matrix of the data */
194 struct bConstraint *con; /* for objects/bones, the first constraint in its constraint stack */
195 TransDataExtension *ext; /* for objects, poses. 1 single malloc per TransInfo! */
196 TransDataIpokey *tdi; /* for objects, ipo keys. per transdata a malloc */
197 TransDataCurveHandleFlags *hdata; /* for curves, stores handle flags for modification/cancel */
198 void *extra; /* extra data (mirrored element pointer, in editmode mesh to EditVert) (editbone for roll fixing) (...) */
199 short flag; /* Various flags */
200 short protectflag; /* If set, copy of Object or PoseChannel protection */
203 typedef struct MouseInput {
204 void (*apply)(struct TransInfo *, struct MouseInput *, short [2], float [3]);
206 short imval[2]; /* initial mouse position */
208 short precision_mval[2]; /* mouse position when precision key was pressed */
213 typedef struct TransInfo {
214 int mode; /* current mode */
215 int flag; /* generic flags for special behaviors */
216 int modifiers; /* special modifiers, by function, not key */
217 short state; /* current state (running, canceled,...)*/
218 int options; /* current context/options for transform */
219 float val; /* init value for some transformations (and rotation angle) */
220 float fac; /* factor for distance based transform */
221 int (*transform)(struct TransInfo *, short *);
222 /* transform function pointer */
223 int (*handleEvent)(struct TransInfo *, struct wmEvent *);
224 /* event handler function pointer RETURN 1 if redraw is needed */
225 int total; /* total number of transformed data */
226 TransData *data; /* transformed data (array) */
227 TransDataExtension *ext; /* transformed data extension (array) */
228 TransData2D *data2d; /* transformed data for 2d (array) */
229 TransCon con; /* transformed constraint */
231 NumInput num; /* numerical input */
232 NDofInput ndof; /* ndof input */
233 MouseInput mouse; /* mouse input */
234 char redraw; /* redraw flag */
235 float prop_size; /* proportional circle radius */
236 char proptext[20]; /* proportional falloff text */
237 float center[3]; /* center of transformation */
238 int center2d[2]; /* center in screen coordinates */
239 short imval[2]; /* initial mouse position */
240 short event_type; /* event->type used to invoke transform */
241 short idx_max; /* maximum index on the input vector */
242 float snap[3]; /* Snapping Gears */
243 char frame_side; /* Mouse side of the cfra, 'L', 'R' or 'B' */
245 float viewmat[4][4]; /* copy from G.vd, prevents feedback, */
246 float viewinv[4][4]; /* and to make sure we don't have to */
247 float persmat[4][4]; /* access G.vd from other space types */
251 char spacetype; /* spacetype where transforming is */
252 char helpline; /* helpline modes (not to be confused with hotline) */
254 float vec[3]; /* translation, to show for widget */
255 float mat[3][3]; /* rot/rescale, to show for widget */
257 char *undostr; /* if set, uses this string for undo */
258 float spacemtx[3][3]; /* orientation matrix of the current space */
259 char spacename[32]; /* name of the current space */
261 struct Object *poseobj; /* if t->flag & T_POSE, this denotes pose object */
263 void *customData; /* Per Transform custom data */
265 /*************** NEW STUFF *********************/
267 short current_orientation;
272 float auto_values[4];
277 struct ToolSettings *settings;
278 struct wmTimer *animtimer;
279 short mval[2]; /* current mouse position */
280 struct Object *obedit;
285 /* ******************** Macros & Prototypes *********************** */
288 #define NUM_NULL_ONE 2
289 #define NUM_NO_NEGATIVE 4
290 #define NUM_NO_ZERO 8
291 #define NUM_NO_FRACTION 16
292 #define NUM_AFFECT_ALL 32
294 /* NDOFINPUT FLAGS */
297 /* transinfo->state */
298 #define TRANS_RUNNING 0
299 #define TRANS_CONFIRM 1
300 #define TRANS_CANCEL 2
302 /* transinfo->flag */
303 #define T_OBJECT (1 << 0)
304 #define T_EDIT (1 << 1)
305 #define T_POSE (1 << 2)
306 #define T_TEXTURE (1 << 3)
307 #define T_CAMERA (1 << 4)
308 // trans on points, having no rotation/scale
309 #define T_POINTS (1 << 6)
310 // for manipulator exceptions, like scaling using center point, drawing help lines
311 #define T_USES_MANIPULATOR (1 << 7)
313 /* restrictions flags */
314 #define T_ALL_RESTRICTIONS ((1 << 8)|(1 << 9)|(1 << 10))
315 #define T_NO_CONSTRAINT (1 << 8)
316 #define T_NULL_ONE (1 << 9)
317 #define T_NO_ZERO (1 << 10)
319 #define T_PROP_EDIT (1 << 11)
320 #define T_PROP_CONNECTED (1 << 12)
322 #define T_V3D_ALIGN (1 << 14)
323 /* for 2d views like uv or ipo */
324 #define T_2D_EDIT (1 << 15)
325 #define T_CLIP_UV (1 << 16)
327 #define T_FREE_CUSTOMDATA (1 << 17)
329 #define T_AUTOIK (1 << 18)
331 #define T_MIRROR (1 << 19)
333 #define T_AUTOVALUES (1 << 20)
335 /* to specificy if we save back settings at the end */
336 #define T_MODAL (1 << 21)
338 /* TransInfo->modifiers */
339 #define MOD_CONSTRAINT_SELECT 0x01
340 #define MOD_PRECISION 0x02
341 #define MOD_SNAP_GEARS 0x04
342 #define MOD_CONSTRAINT_PLANE 0x08
345 /* ******************************************************************************** */
347 /* transinfo->helpline */
353 #define HLP_TRACKBALL 5
355 /* transinfo->con->mode */
360 #define CON_SELECT 16
361 #define CON_NOFLIP 32 /* does not reorient vector to face viewport when on */
364 /* transdata->flag */
365 #define TD_SELECTED 1
366 #define TD_ACTIVE (1 << 1)
367 #define TD_NOACTION (1 << 2)
368 #define TD_USEQUAT (1 << 3)
369 #define TD_NOTCONNECTED (1 << 4)
370 #define TD_SINGLESIZE (1 << 5) /* used for scaling of MetaElem->rad */
371 #define TD_TIMEONLY (1 << 8)
372 #define TD_NOCENTER (1 << 9)
373 #define TD_NO_EXT (1 << 10) /* ext abused for particle key timing */
374 #define TD_SKIP (1 << 11) /* don't transform this data */
375 #define TD_BEZTRIPLE (1 << 12) /* if this is a bez triple, we need to restore the handles, if this is set transdata->misc.hdata needs freeing */
376 #define TD_NO_LOC (1 << 13) /* when this is set, don't apply translation changes to this element */
377 #define TD_NOTIMESNAP (1 << 14) /* for Graph Editor autosnap, indicates that point should not undergo autosnapping */
378 #define TD_INTVALUES (1 << 15) /* for Graph Editor - curves that can only have int-values need their keyframes tagged with this */
380 /* transsnap->status */
382 #define SNAP_FORCED 2
383 #define TARGET_INIT 4
386 /* transsnap->modePoint */
390 /* transsnap->modeTarget */
391 #define SNAP_CLOSEST 0
392 #define SNAP_CENTER 1
393 #define SNAP_MEDIAN 2
394 #define SNAP_ACTIVE 3
397 void TFM_OT_transform(struct wmOperatorType *ot);
399 int initTransform(struct bContext *C, struct TransInfo *t, struct wmOperator *op, struct wmEvent *event, int mode);
400 void saveTransform(struct bContext *C, struct TransInfo *t, struct wmOperator *op);
401 void transformEvent(TransInfo *t, struct wmEvent *event);
402 void transformApply(struct bContext *C, TransInfo *t);
403 int transformEnd(struct bContext *C, TransInfo *t);
405 void setTransformViewMatrices(TransInfo *t);
406 void convertViewVec(TransInfo *t, float *vec, short dx, short dy);
407 void projectIntView(TransInfo *t, float *vec, int *adr);
408 void projectFloatView(TransInfo *t, float *vec, float *adr);
410 void applyAspectRatio(TransInfo *t, float *vec);
411 void removeAspectRatio(TransInfo *t, float *vec);
413 void initWarp(TransInfo *t);
414 int handleEventWarp(TransInfo *t, struct wmEvent *event);
415 int Warp(TransInfo *t, short mval[2]);
417 void initShear(TransInfo *t);
418 int handleEventShear(TransInfo *t, struct wmEvent *event);
419 int Shear(TransInfo *t, short mval[2]);
421 void initResize(TransInfo *t);
422 int Resize(TransInfo *t, short mval[2]);
424 void initTranslation(TransInfo *t);
425 int Translation(TransInfo *t, short mval[2]);
427 void initToSphere(TransInfo *t);
428 int ToSphere(TransInfo *t, short mval[2]);
430 void initRotation(TransInfo *t);
431 int Rotation(TransInfo *t, short mval[2]);
433 void initShrinkFatten(TransInfo *t);
434 int ShrinkFatten(TransInfo *t, short mval[2]);
436 void initTilt(TransInfo *t);
437 int Tilt(TransInfo *t, short mval[2]);
439 void initCurveShrinkFatten(TransInfo *t);
440 int CurveShrinkFatten(TransInfo *t, short mval[2]);
442 void initTrackball(TransInfo *t);
443 int Trackball(TransInfo *t, short mval[2]);
445 void initPushPull(TransInfo *t);
446 int PushPull(TransInfo *t, short mval[2]);
448 void initBevel(TransInfo *t);
449 int handleEventBevel(TransInfo *t, struct wmEvent *event);
450 int Bevel(TransInfo *t, short mval[2]);
452 void initBevelWeight(TransInfo *t);
453 int BevelWeight(TransInfo *t, short mval[2]);
455 void initCrease(TransInfo *t);
456 int Crease(TransInfo *t, short mval[2]);
458 void initBoneSize(TransInfo *t);
459 int BoneSize(TransInfo *t, short mval[2]);
461 void initBoneEnvelope(TransInfo *t);
462 int BoneEnvelope(TransInfo *t, short mval[2]);
464 void initBoneRoll(TransInfo *t);
465 int BoneRoll(TransInfo *t, short mval[2]);
467 void initTimeTranslate(TransInfo *t);
468 int TimeTranslate(TransInfo *t, short mval[2]);
470 void initTimeSlide(TransInfo *t);
471 int TimeSlide(TransInfo *t, short mval[2]);
473 void initTimeScale(TransInfo *t);
474 int TimeScale(TransInfo *t, short mval[2]);
476 void initBakeTime(TransInfo *t);
477 int BakeTime(TransInfo *t, short mval[2]);
479 void initMirror(TransInfo *t);
480 int Mirror(TransInfo *t, short mval[2]);
482 void initAlign(TransInfo *t);
483 int Align(TransInfo *t, short mval[2]);
486 void drawPropCircle(const struct bContext *C, TransInfo *t);
488 /*********************** transform_conversions.c ********** */
491 void flushTransGPactionData(TransInfo *t);
492 void flushTransGraphData(TransInfo *t);
493 void remake_graph_transdata(TransInfo *t, struct ListBase *anim_data);
494 void flushTransUVs(TransInfo *t);
495 void flushTransParticles(TransInfo *t);
496 int clipUVTransform(TransInfo *t, float *vec, int resize);
497 void flushTransNodes(TransInfo *t);
498 void flushTransSeq(TransInfo *t);
500 /*********************** exported from transform_manipulator.c ********** */
501 int calc_manipulator_stats(const struct bContext *C);
502 float get_drawsize(struct ARegion *ar, float *co);
504 /*********************** TransData Creation and General Handling *********** */
505 void createTransData(struct bContext *C, TransInfo *t);
506 void sort_trans_data_dist(TransInfo *t);
507 void add_tdi_poin(float *poin, float *old, float delta);
508 void special_aftertrans_update(TransInfo *t);
510 void transform_autoik_update(TransInfo *t, short mode);
512 int count_set_pose_transflags(int *out_mode, short around, struct Object *ob);
514 /* auto-keying stuff used by special_aftertrans_update */
515 void autokeyframe_ob_cb_func(struct Scene *scene, struct View3D *v3d, struct Object *ob, int tmode);
516 void autokeyframe_pose_cb_func(struct Scene *scene, struct View3D *v3d, struct Object *ob, int tmode, short targetless_ik);
518 /*********************** Constraints *****************************/
520 void drawConstraint(const struct bContext *C, TransInfo *t);
522 void getConstraintMatrix(TransInfo *t);
523 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[]);
524 void setLocalConstraint(TransInfo *t, int mode, const char text[]);
525 void setUserConstraint(TransInfo *t, int mode, const char text[]);
527 void constraintNumInput(TransInfo *t, float vec[3]);
529 void getConstraintMatrix(TransInfo *t);
530 int isLockConstraint(TransInfo *t);
531 int getConstraintSpaceDimension(TransInfo *t);
532 char constraintModeToChar(TransInfo *t);
534 void startConstraint(TransInfo *t);
535 void stopConstraint(TransInfo *t);
537 void initSelectConstraint(TransInfo *t, float mtx[3][3]);
538 void selectConstraint(TransInfo *t);
539 void postSelectConstraint(TransInfo *t);
541 void setNearestAxis(TransInfo *t);
543 /*********************** Snapping ********************************/
551 void snapGrid(TransInfo *t, float *val);
552 void snapGridAction(TransInfo *t, float *val, GearsType action);
554 void initSnapping(struct TransInfo *t, struct wmOperator *op);
555 void applySnapping(TransInfo *t, float *vec);
556 void resetSnapping(TransInfo *t);
557 int handleSnapping(TransInfo *t, struct wmEvent *event);
558 void drawSnapping(const struct bContext *C, TransInfo *t);
559 int usingSnappingNormal(TransInfo *t);
560 int validSnappingNormal(TransInfo *t);
562 /********************** Mouse Input ******************************/
571 INPUT_HORIZONTAL_RATIO,
572 INPUT_HORIZONTAL_ABSOLUTE,
573 INPUT_VERTICAL_RATIO,
574 INPUT_VERTICAL_ABSOLUTE
577 void initMouseInput(TransInfo *t, MouseInput *mi, int center[2], short mval[2]);
578 void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode);
579 int handleMouseInput(struct TransInfo *t, struct MouseInput *mi, struct wmEvent *event);
580 void applyMouseInput(struct TransInfo *t, struct MouseInput *mi, short mval[2], float output[3]);
582 /*********************** Generics ********************************/
584 int initTransInfo(struct bContext *C, TransInfo *t, struct wmOperator *op, struct wmEvent *event);
585 void postTrans (TransInfo *t);
586 void resetTransRestrictions(TransInfo *t);
588 void drawLine(TransInfo *t, float *center, float *dir, char axis, short options);
590 TransDataCurveHandleFlags *initTransDataCurveHandes(TransData *td, struct BezTriple *bezt);
592 /* DRAWLINE options flags */
597 void applyTransObjects(TransInfo *t);
598 void restoreTransObjects(TransInfo *t);
599 void restoreTransNodes(TransInfo *t);
600 void recalcData(TransInfo *t);
602 void calculateCenter(TransInfo *t);
603 void calculateCenter2D(TransInfo *t);
604 void calculateCenterBound(TransInfo *t);
605 void calculateCenterMedian(TransInfo *t);
606 void calculateCenterCursor(TransInfo *t);
608 void calculateCenterCursor2D(TransInfo *t);
609 void calculatePropRatio(TransInfo *t);
611 void getViewVector(TransInfo *t, float coord[3], float vec[3]);
613 /*********************** NumInput ********************************/
615 void initNumInput(NumInput *n);
616 void outputNumInput(NumInput *n, char *str);
617 short hasNumInput(NumInput *n);
618 void applyNumInput(NumInput *n, float *vec);
619 char handleNumInput(NumInput *n, struct wmEvent *event);
621 /*********************** NDofInput ********************************/
623 void initNDofInput(NDofInput *n);
624 int hasNDofInput(NDofInput *n);
625 void applyNDofInput(NDofInput *n, float *vec);
626 int handleNDofInput(NDofInput *n, struct wmEvent *event);
628 /* handleNDofInput return values */
629 #define NDOF_REFRESH 1
630 #define NDOF_NOMOVE 2
631 #define NDOF_CONFIRM 3
632 #define NDOF_CANCEL 4
635 /*********************** Transform Orientations ******************************/
637 void initTransformOrientation(struct bContext *C, TransInfo *t);
639 int manageObjectSpace(struct bContext *C, int confirm, int set);
640 int manageMeshSpace(struct bContext *C, int confirm, int set);
641 int manageBoneSpace(struct bContext *C, int confirm, int set);
643 /* Those two fill in mat and return non-zero on success */
644 int createSpaceNormal(float mat[3][3], float normal[3]);
645 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3]);
647 int addMatrixSpace(struct bContext *C, float mat[3][3], char name[]);
648 int addObjectSpace(struct bContext *C, struct Object *ob);
649 void applyTransformOrientation(const struct bContext *C, TransInfo *t);
652 #define ORIENTATION_NONE 0
653 #define ORIENTATION_NORMAL 1
654 #define ORIENTATION_VERT 2
655 #define ORIENTATION_EDGE 3
656 #define ORIENTATION_FACE 4
658 int getTransformOrientation(const struct bContext *C, float normal[3], float plane[3], int activeOnly);
659 int createSpaceNormal(float mat[3][3], float normal[3]);
660 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3]);