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 "BIF_transform.h"
35 /* ************************** Types ***************************** */
48 struct wmOperatorType;
54 typedef struct NDofInput {
61 typedef struct NumInput {
64 short flag; /* Different flags to indicate different behaviors */
65 char inv[3]; /* If the value is inverted or not */
66 float val[3]; /* Direct value of the input */
67 int ctrl[3]; /* Control to indicate what to do with the numbers that are typed */
71 The ctrl value has different meaning:
72 0 : No value has been typed
74 otherwise, |value| - 1 is where the cursor is located after the period
75 Positive : number is positive
76 Negative : number is negative
79 typedef struct TransSnap {
88 float dist; // Distance from snapPoint to snapTarget
90 void (*applySnap)(struct TransInfo *, float *);
91 void (*calcSnap)(struct TransInfo *, float *);
92 void (*targetSnap)(struct TransInfo *);
93 float (*distance)(struct TransInfo *, float p1[3], float p2[3]); // Get the transform distance between two points (used by Closest snap)
96 typedef struct TransCon {
97 char text[50]; /* Description of the Constraint for header_print */
98 float mtx[3][3]; /* Matrix of the Constraint space */
99 float imtx[3][3]; /* Inverse Matrix of the Constraint space */
100 float pmtx[3][3]; /* Projection Constraint Matrix (same as imtx with some axis == 0) */
101 float center[3]; /* transformation center to define where to draw the view widget
102 ALWAYS in global space. Unlike the transformation center */
103 short imval[2]; /* initial mouse value for visual calculation */
104 /* the one in TransInfo is not garanty to stay the same (Rotates change it) */
105 int mode; /* Mode flags of the Constraint */
106 void (*drawExtra)(struct TransInfo *);
107 /* For constraints that needs to draw differently from the other
108 uses this instead of the generic draw function */
109 void (*applyVec)(struct TransInfo *, struct TransData *, float *, float *, float *);
110 /* Apply function pointer for linear vectorial transformation */
111 /* The last three parameters are pointers to the in/out/printable vectors */
112 void (*applySize)(struct TransInfo *, struct TransData *, float [3][3]);
113 /* Apply function pointer for size transformation */
114 void (*applyRot)(struct TransInfo *, struct TransData *, float [3], float *);
115 /* Apply function pointer for rotation transformation */
118 typedef struct TransDataIpokey {
119 int flag; /* which keys */
120 float *locx, *locy, *locz; /* channel pointers */
121 float *rotx, *roty, *rotz;
122 float *quatx, *quaty, *quatz, *quatw;
123 float *sizex, *sizey, *sizez;
124 float oldloc[9]; /* storage old values */
130 typedef struct TransDataExtension {
131 float drot[3]; /* Initial object drot */
132 float dsize[3]; /* Initial object dsize */
133 float *rot; /* Rotation of the data to transform (Faculative) */
134 float irot[3]; /* Initial rotation */
135 float *quat; /* Rotation quaternion of the data to transform (Faculative) */
136 float iquat[4]; /* Initial rotation quaternion */
137 float *size; /* Size of the data to transform (Faculative) */
138 float isize[3]; /* Initial size */
139 float obmat[4][4]; /* Object matrix */
140 } TransDataExtension;
142 typedef struct TransData2D {
143 float loc[3]; /* Location of data used to transform (x,y,0) */
144 float *loc2d; /* Pointer to real 2d location of data */
147 /* we need to store 2 handles for each transdata incase the other handle wasnt selected */
148 typedef struct TransDataCurveHandleFlags {
151 } TransDataCurveHandleFlags;
153 /* for sequencer transform */
154 typedef struct TransDataSeq {
155 struct Sequence *seq;
156 int flag; /* a copy of seq->flag that may be modified for nested strips */
157 short start_offset; /* use this so we can have transform data at the strips start, but apply correctly to the start frame */
158 short sel_flag; /* one of SELECT, SEQ_LEFTSEL and SEQ_RIGHTSEL */
162 typedef struct TransData {
163 float dist; /* Distance needed to affect element (for Proportionnal Editing) */
164 float rdist; /* Distance to the nearest element (for Proportionnal Editing) */
165 float factor; /* Factor of the transformation (for Proportionnal Editing) */
166 float *loc; /* Location of the data to transform */
167 float iloc[3]; /* Initial location */
168 float *val; /* Value pointer for special transforms */
169 float ival; /* Old value*/
170 float center[3]; /* Individual data center */
171 float mtx[3][3]; /* Transformation matrix from data space to global space */
172 float smtx[3][3]; /* Transformation matrix from global space to data space */
173 float axismtx[3][3];/* Axis orientation matrix of the data */
175 struct bConstraint *con; /* for objects/bones, the first constraint in its constraint stack */
176 TransDataExtension *ext; /* for objects, poses. 1 single malloc per TransInfo! */
177 TransDataIpokey *tdi; /* for objects, ipo keys. per transdata a malloc */
178 TransDataCurveHandleFlags *hdata; /* for curves, stores handle flags for modification/cancel */
179 void *extra; /* extra data (mirrored element pointer, in editmode mesh to EditVert) (editbone for roll fixing) (...) */
180 short flag; /* Various flags */
181 short protectflag; /* If set, copy of Object or PoseChannel protection */
184 typedef struct MouseInput {
185 void (*apply)(struct TransInfo *, struct MouseInput *, short [2], float [3]);
187 short imval[2]; /* initial mouse position */
189 short precision_mval[2]; /* mouse position when precision key was pressed */
194 typedef struct TransInfo {
195 int mode; /* current mode */
196 int flag; /* generic flags for special behaviors */
197 int modifiers; /* special modifiers, by function, not key */
198 short state; /* current state (running, canceled,...)*/
199 int options; /* current context/options for transform */
200 float val; /* init value for some transformations (and rotation angle) */
201 float fac; /* factor for distance based transform */
202 int (*transform)(struct TransInfo *, short *);
203 /* transform function pointer */
204 int (*handleEvent)(struct TransInfo *, struct wmEvent *);
205 /* event handler function pointer RETURN 1 if redraw is needed */
206 int total; /* total number of transformed data */
207 TransData *data; /* transformed data (array) */
208 TransDataExtension *ext; /* transformed data extension (array) */
209 TransData2D *data2d; /* transformed data for 2d (array) */
210 TransCon con; /* transformed constraint */
212 NumInput num; /* numerical input */
213 NDofInput ndof; /* ndof input */
214 MouseInput mouse; /* mouse input */
215 char redraw; /* redraw flag */
216 float propsize; /* proportional circle radius */
217 char proptext[20]; /* proportional falloff text */
218 float center[3]; /* center of transformation */
219 int center2d[2]; /* center in screen coordinates */
220 short imval[2]; /* initial mouse position */
221 short idx_max; /* maximum index on the input vector */
222 float snap[3]; /* Snapping Gears */
223 char frame_side; /* Mouse side of the cfra, 'L', 'R' or 'B' */
225 float viewmat[4][4]; /* copy from G.vd, prevents feedback, */
226 float viewinv[4][4]; /* and to make sure we don't have to */
227 float persmat[4][4]; /* access G.vd from other space types */
231 char spacetype; /* spacetype where transforming is */
233 float vec[3]; /* translation, to show for widget */
234 float mat[3][3]; /* rot/rescale, to show for widget */
236 char *undostr; /* if set, uses this string for undo */
237 float spacemtx[3][3]; /* orientation matrix of the current space */
238 char spacename[32]; /* name of the current space */
240 struct Object *poseobj; /* if t->flag & T_POSE, this denotes pose object */
242 void *customData; /* Per Transform custom data */
244 /*************** NEW STUFF *********************/
251 short mval[2]; /* current mouse position */
252 struct Object *obedit;
257 /* ******************** Macros & Prototypes *********************** */
260 #define NUM_NULL_ONE 2
261 #define NUM_NO_NEGATIVE 4
262 #define NUM_NO_ZERO 8
263 #define NUM_NO_FRACTION 16
264 #define NUM_AFFECT_ALL 32
266 /* NDOFINPUT FLAGS */
269 /* transinfo->state */
270 #define TRANS_RUNNING 0
271 #define TRANS_CONFIRM 1
272 #define TRANS_CANCEL 2
274 /* transinfo->flag */
275 #define T_OBJECT (1 << 0)
276 #define T_EDIT (1 << 1)
277 #define T_POSE (1 << 2)
278 #define T_TEXTURE (1 << 3)
279 #define T_CAMERA (1 << 4)
280 // trans on points, having no rotation/scale
281 #define T_POINTS (1 << 6)
282 // for manipulator exceptions, like scaling using center point, drawing help lines
283 #define T_USES_MANIPULATOR (1 << 7)
285 /* restrictions flags */
286 #define T_ALL_RESTRICTIONS ((1 << 8)|(1 << 9)|(1 << 10))
287 #define T_NO_CONSTRAINT (1 << 8)
288 #define T_NULL_ONE (1 << 9)
289 #define T_NO_ZERO (1 << 10)
291 #define T_PROP_EDIT (1 << 11)
292 #define T_PROP_CONNECTED (1 << 12)
294 #define T_V3D_ALIGN (1 << 14)
295 /* for 2d views like uv or ipo */
296 #define T_2D_EDIT (1 << 15)
297 #define T_CLIP_UV (1 << 16)
299 #define T_FREE_CUSTOMDATA (1 << 17)
301 #define T_AUTOIK (1 << 18)
303 /* TransInfo->modifiers */
304 #define MOD_CONSTRAINT_SELECT 0x01
305 #define MOD_PRECISION 0x02
306 #define MOD_SNAP_GEARS 0x04
307 #define MOD_CONSTRAINT_PLANE 0x08
310 /* ******************************************************************************** */
312 /* transinfo->con->mode */
317 #define CON_SELECT 16
318 #define CON_NOFLIP 32 /* does not reorient vector to face viewport when on */
321 /* transdata->flag */
322 #define TD_SELECTED 1
323 #define TD_ACTIVE (1 << 1)
324 #define TD_NOACTION (1 << 2)
325 #define TD_USEQUAT (1 << 3)
326 #define TD_NOTCONNECTED (1 << 4)
327 #define TD_SINGLESIZE (1 << 5) /* used for scaling of MetaElem->rad */
328 #define TD_TIMEONLY (1 << 8)
329 #define TD_NOCENTER (1 << 9)
330 #define TD_NO_EXT (1 << 10) /* ext abused for particle key timing */
331 #define TD_SKIP (1 << 11) /* don't transform this data */
332 #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 */
333 #define TD_NO_LOC (1 << 13) /* when this is set, don't apply translation changes to this element */
335 /* transsnap->status */
337 #define TARGET_INIT 2
340 /* transsnap->modePoint */
344 /* transsnap->modeTarget */
345 #define SNAP_CLOSEST 0
346 #define SNAP_CENTER 1
347 #define SNAP_MEDIAN 2
348 #define SNAP_ACTIVE 3
351 void TFM_OT_transform(struct wmOperatorType *ot);
353 void initTransform(struct bContext *C, struct TransInfo *t, struct wmOperator *op, struct wmEvent *event);
354 void saveTransform(struct bContext *C, struct TransInfo *t, struct wmOperator *op);
355 void transformEvent(TransInfo *t, struct wmEvent *event);
356 void transformApply(struct bContext *C, TransInfo *t);
357 int transformEnd(struct bContext *C, TransInfo *t);
359 void setTransformViewMatrices(TransInfo *t);
360 void convertViewVec(TransInfo *t, float *vec, short dx, short dy);
361 void projectIntView(TransInfo *t, float *vec, int *adr);
362 void projectFloatView(TransInfo *t, float *vec, float *adr);
364 void applyAspectRatio(TransInfo *t, float *vec);
365 void removeAspectRatio(TransInfo *t, float *vec);
367 void initWarp(TransInfo *t);
368 int handleEventWarp(TransInfo *t, struct wmEvent *event);
369 int Warp(TransInfo *t, short mval[2]);
371 void initShear(TransInfo *t);
372 int handleEventShear(TransInfo *t, struct wmEvent *event);
373 int Shear(TransInfo *t, short mval[2]);
375 void initResize(TransInfo *t);
376 int Resize(TransInfo *t, short mval[2]);
378 void initTranslation(TransInfo *t);
379 int Translation(TransInfo *t, short mval[2]);
381 void initToSphere(TransInfo *t);
382 int ToSphere(TransInfo *t, short mval[2]);
384 void initRotation(TransInfo *t);
385 int Rotation(TransInfo *t, short mval[2]);
387 void initShrinkFatten(TransInfo *t);
388 int ShrinkFatten(TransInfo *t, short mval[2]);
390 void initTilt(TransInfo *t);
391 int Tilt(TransInfo *t, short mval[2]);
393 void initCurveShrinkFatten(TransInfo *t);
394 int CurveShrinkFatten(TransInfo *t, short mval[2]);
396 void initTrackball(TransInfo *t);
397 int Trackball(TransInfo *t, short mval[2]);
399 void initPushPull(TransInfo *t);
400 int PushPull(TransInfo *t, short mval[2]);
402 void initBevel(TransInfo *t);
403 int handleEventBevel(TransInfo *t, struct wmEvent *event);
404 int Bevel(TransInfo *t, short mval[2]);
406 void initBevelWeight(TransInfo *t);
407 int BevelWeight(TransInfo *t, short mval[2]);
409 void initCrease(TransInfo *t);
410 int Crease(TransInfo *t, short mval[2]);
412 void initBoneSize(TransInfo *t);
413 int BoneSize(TransInfo *t, short mval[2]);
415 void initBoneEnvelope(TransInfo *t);
416 int BoneEnvelope(TransInfo *t, short mval[2]);
418 void initBoneRoll(TransInfo *t);
419 int BoneRoll(TransInfo *t, short mval[2]);
421 void initTimeTranslate(TransInfo *t);
422 int TimeTranslate(TransInfo *t, short mval[2]);
424 void initTimeSlide(TransInfo *t);
425 int TimeSlide(TransInfo *t, short mval[2]);
427 void initTimeScale(TransInfo *t);
428 int TimeScale(TransInfo *t, short mval[2]);
430 void initBakeTime(TransInfo *t);
431 int BakeTime(TransInfo *t, short mval[2]);
433 void initMirror(TransInfo *t);
434 int Mirror(TransInfo *t, short mval[2]);
436 void initAlign(TransInfo *t);
437 int Align(TransInfo *t, short mval[2]);
440 void drawPropCircle(TransInfo *t);
442 /*********************** transform_conversions.c ********** */
444 void flushTransGPactionData(TransInfo *t);
445 void flushTransIpoData(TransInfo *t);
446 void flushTransUVs(TransInfo *t);
447 void flushTransParticles(TransInfo *t);
448 int clipUVTransform(TransInfo *t, float *vec, int resize);
449 void flushTransNodes(TransInfo *t);
450 void flushTransSeq(TransInfo *t);
452 /*********************** exported from transform_manipulator.c ********** */
453 void draw_manipulator_ext(struct ScrArea *sa, int type, char axis, int col, float vec[3], float mat[][3]);
454 int calc_manipulator_stats(struct ScrArea *sa);
455 float get_drawsize(struct ARegion *ar, float *co);
457 /*********************** TransData Creation and General Handling *********** */
458 void createTransData(struct bContext *C, TransInfo *t);
459 void sort_trans_data_dist(TransInfo *t);
460 void add_tdi_poin(float *poin, float *old, float delta);
461 void special_aftertrans_update(TransInfo *t);
463 void transform_autoik_update(TransInfo *t, short mode);
465 /* auto-keying stuff used by special_aftertrans_update */
466 short autokeyframe_cfra_can_key(struct Object *ob);
467 void autokeyframe_ob_cb_func(struct Object *ob, int tmode);
468 void autokeyframe_pose_cb_func(struct Object *ob, int tmode, short targetless_ik);
470 /*********************** Constraints *****************************/
472 void drawConstraint(TransInfo *t);
474 void getConstraintMatrix(TransInfo *t);
475 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[]);
476 void setLocalConstraint(TransInfo *t, int mode, const char text[]);
477 void setUserConstraint(TransInfo *t, int mode, const char text[]);
479 void constraintNumInput(TransInfo *t, float vec[3]);
481 void getConstraintMatrix(TransInfo *t);
482 int isLockConstraint(TransInfo *t);
483 int getConstraintSpaceDimension(TransInfo *t);
484 char constraintModeToChar(TransInfo *t);
486 void startConstraint(TransInfo *t);
487 void stopConstraint(TransInfo *t);
489 void initSelectConstraint(TransInfo *t, float mtx[3][3]);
490 void selectConstraint(TransInfo *t);
491 void postSelectConstraint(TransInfo *t);
493 void setNearestAxis(TransInfo *t);
495 /*********************** Snapping ********************************/
503 void snapGrid(TransInfo *t, float *val);
504 void snapGridAction(TransInfo *t, float *val, GearsType action);
506 void initSnapping(struct TransInfo *t);
507 void applySnapping(TransInfo *t, float *vec);
508 void resetSnapping(TransInfo *t);
509 int handleSnapping(TransInfo *t, struct wmEvent *event);
510 void drawSnapping(TransInfo *t);
511 int usingSnappingNormal(TransInfo *t);
512 int validSnappingNormal(TransInfo *t);
514 /********************** Mouse Input ******************************/
523 INPUT_HORIZONTAL_RATIO,
524 INPUT_HORIZONTAL_ABSOLUTE,
525 INPUT_VERTICAL_RATIO,
526 INPUT_VERTICAL_ABSOLUTE
529 void initMouseInput(TransInfo *t, MouseInput *mi, int center[2], short mval[2]);
530 void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode);
531 int handleMouseInput(struct TransInfo *t, struct MouseInput *mi, struct wmEvent *event);
532 void applyMouseInput(struct TransInfo *t, struct MouseInput *mi, short mval[2], float output[3]);
534 /*********************** Generics ********************************/
536 void initTransInfo(struct bContext *C, TransInfo *t, struct wmEvent *event);
537 void postTrans (TransInfo *t);
538 void resetTransRestrictions(TransInfo *t);
540 void drawLine(TransInfo *t, float *center, float *dir, char axis, short options);
542 TransDataCurveHandleFlags *initTransDataCurveHandes(TransData *td, struct BezTriple *bezt);
544 /* DRAWLINE options flags */
549 void applyTransObjects(TransInfo *t);
550 void restoreTransObjects(TransInfo *t);
551 void restoreTransNodes(TransInfo *t);
552 void recalcData(TransInfo *t);
554 void calculateCenter(TransInfo *t);
555 void calculateCenter2D(TransInfo *t);
556 void calculateCenterBound(TransInfo *t);
557 void calculateCenterMedian(TransInfo *t);
558 void calculateCenterCursor(TransInfo *t);
560 void calculateCenterCursor2D(TransInfo *t);
561 void calculatePropRatio(TransInfo *t);
563 void getViewVector(TransInfo *t, float coord[3], float vec[3]);
565 TransInfo * BIF_GetTransInfo(void);
567 /*********************** NumInput ********************************/
569 void initNumInput(NumInput *n);
570 void outputNumInput(NumInput *n, char *str);
571 short hasNumInput(NumInput *n);
572 void applyNumInput(NumInput *n, float *vec);
573 char handleNumInput(NumInput *n, struct wmEvent *event);
575 /*********************** NDofInput ********************************/
577 void initNDofInput(NDofInput *n);
578 int hasNDofInput(NDofInput *n);
579 void applyNDofInput(NDofInput *n, float *vec);
580 int handleNDofInput(NDofInput *n, struct wmEvent *event);
582 /* handleNDofInput return values */
583 #define NDOF_REFRESH 1
584 #define NDOF_NOMOVE 2
585 #define NDOF_CONFIRM 3
586 #define NDOF_CANCEL 4
589 /*********************** Transform Orientations ******************************/
591 void initTransformOrientation(struct bContext *C, TransInfo *t);
593 int manageObjectSpace(struct bContext *C, int confirm, int set);
594 int manageMeshSpace(struct bContext *C, int confirm, int set);
595 int manageBoneSpace(struct bContext *C, int confirm, int set);
597 /* Those two fill in mat and return non-zero on success */
598 int createSpaceNormal(float mat[3][3], float normal[3]);
599 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3]);
601 int addMatrixSpace(struct bContext *C, float mat[3][3], char name[]);
602 int addObjectSpace(struct bContext *C, struct Object *ob);
603 void applyTransformOrientation(struct bContext *C, TransInfo *t);
606 #define ORIENTATION_NONE 0
607 #define ORIENTATION_NORMAL 1
608 #define ORIENTATION_VERT 2
609 #define ORIENTATION_EDGE 3
610 #define ORIENTATION_FACE 4
612 int getTransformOrientation(struct bContext *C, float normal[3], float plane[3], int activeOnly);
613 int createSpaceNormal(float mat[3][3], float normal[3]);
614 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3]);