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;
53 typedef struct NDofInput {
60 typedef struct NumInput {
63 short flag; /* Different flags to indicate different behaviors */
64 float val[3]; /* Direct value of the input */
65 int ctrl[3]; /* Control to indicate what to do with the numbers that are typed */
69 The ctrl value has different meaning:
70 0 : No value has been typed
72 otherwise, |value| - 1 is where the cursor is located after the period
73 Positive : number is positive
74 Negative : number is negative
77 typedef struct TransSnap {
85 float dist; // Distance from snapPoint to snapTarget
87 void (*applySnap)(struct TransInfo *, float *);
88 void (*calcSnap)(struct TransInfo *, float *);
89 void (*targetSnap)(struct TransInfo *);
90 float (*distance)(struct TransInfo *, float p1[3], float p2[3]); // Get the transform distance between two points (used by Closest snap)
93 typedef struct TransCon {
94 char text[50]; /* Description of the Constraint for header_print */
95 float mtx[3][3]; /* Matrix of the Constraint space */
96 float imtx[3][3]; /* Inverse Matrix of the Constraint space */
97 float pmtx[3][3]; /* Projection Constraint Matrix (same as imtx with some axis == 0) */
98 float center[3]; /* transformation center to define where to draw the view widget
99 ALWAYS in global space. Unlike the transformation center */
100 short imval[2]; /* initial mouse value for visual calculation */
101 /* the one in TransInfo is not garanty to stay the same (Rotates change it) */
102 int mode; /* Mode flags of the Constraint */
103 void (*drawExtra)(struct TransInfo *);
104 /* For constraints that needs to draw differently from the other
105 uses this instead of the generic draw function */
106 void (*applyVec)(struct TransInfo *, struct TransData *, float *, float *, float *);
107 /* Apply function pointer for linear vectorial transformation */
108 /* The last three parameters are pointers to the in/out/printable vectors */
109 void (*applySize)(struct TransInfo *, struct TransData *, float [3][3]);
110 /* Apply function pointer for size transformation */
111 void (*applyRot)(struct TransInfo *, struct TransData *, float [3], float *);
112 /* Apply function pointer for rotation transformation */
115 typedef struct TransDataIpokey {
116 int flag; /* which keys */
117 float *locx, *locy, *locz; /* channel pointers */
118 float *rotx, *roty, *rotz;
119 float *quatx, *quaty, *quatz, *quatw;
120 float *sizex, *sizey, *sizez;
121 float oldloc[9]; /* storage old values */
127 typedef struct TransDataExtension {
128 float drot[3]; /* Initial object drot */
129 float dsize[3]; /* Initial object dsize */
130 float *rot; /* Rotation of the data to transform (Faculative) */
131 float irot[3]; /* Initial rotation */
132 float *quat; /* Rotation quaternion of the data to transform (Faculative) */
133 float iquat[4]; /* Initial rotation quaternion */
134 float *size; /* Size of the data to transform (Faculative) */
135 float isize[3]; /* Initial size */
136 float obmat[4][4]; /* Object matrix */
137 } TransDataExtension;
139 typedef struct TransData2D {
140 float loc[3]; /* Location of data used to transform (x,y,0) */
141 float *loc2d; /* Pointer to real 2d location of data */
144 /* we need to store 2 handles for each transdata incase the other handle wasnt selected */
145 typedef struct TransDataCurveHandleFlags {
148 } TransDataCurveHandleFlags;
151 typedef struct TransData {
152 float dist; /* Distance needed to affect element (for Proportionnal Editing) */
153 float rdist; /* Distance to the nearest element (for Proportionnal Editing) */
154 float factor; /* Factor of the transformation (for Proportionnal Editing) */
155 float *loc; /* Location of the data to transform */
156 float iloc[3]; /* Initial location */
157 float *val; /* Value pointer for special transforms */
158 float ival; /* Old value*/
159 float center[3]; /* Individual data center */
160 float mtx[3][3]; /* Transformation matrix from data space to global space */
161 float smtx[3][3]; /* Transformation matrix from global space to data space */
162 float axismtx[3][3];/* Axis orientation matrix of the data */
164 struct bConstraint *con; /* for objects/bones, the first constraint in its constraint stack */
165 TransDataExtension *ext; /* for objects, poses. 1 single malloc per TransInfo! */
166 TransDataIpokey *tdi; /* for objects, ipo keys. per transdata a malloc */
167 TransDataCurveHandleFlags *hdata; /* for curves, stores handle flags for modification/cancel */
168 void *extra; /* extra data (mirrored element pointer, in editmode mesh to EditVert) (editbone for roll fixing) (...) */
169 short flag; /* Various flags */
170 short protectflag; /* If set, copy of Object or PoseChannel protection */
171 /*#ifdef WITH_VERSE*/
172 void *verse; /* pointer at verse data struct (VerseVert, etc.) */
176 typedef struct MouseInput {
177 void (*apply)(struct TransInfo *, struct MouseInput *, short [2], float [3]);
179 short imval[2]; /* initial mouse position */
181 short precision_mval[2]; /* mouse position when precision key was pressed */
186 typedef struct TransInfo {
187 int mode; /* current mode */
188 int flag; /* generic flags for special behaviors */
189 short state; /* current state (running, canceled,...)*/
190 int options; /* current context/options for transform */
191 float val; /* init value for some transformations (and rotation angle) */
192 float fac; /* factor for distance based transform */
193 int (*transform)(struct TransInfo *, short *);
194 /* transform function pointer */
195 int (*handleEvent)(struct TransInfo *, struct wmEvent *);
196 /* event handler function pointer RETURN 1 if redraw is needed */
197 int total; /* total number of transformed data */
198 TransData *data; /* transformed data (array) */
199 TransDataExtension *ext; /* transformed data extension (array) */
200 TransData2D *data2d; /* transformed data for 2d (array) */
201 TransCon con; /* transformed constraint */
203 NumInput num; /* numerical input */
204 NDofInput ndof; /* ndof input */
205 MouseInput mouse; /* mouse input */
206 char redraw; /* redraw flag */
207 float propsize; /* proportional circle radius */
208 char proptext[20]; /* proportional falloff text */
209 float center[3]; /* center of transformation */
210 int center2d[2]; /* center in screen coordinates */
211 short imval[2]; /* initial mouse position */
212 short idx_max; /* maximum index on the input vector */
213 float snap[3]; /* Snapping Gears */
215 float viewmat[4][4]; /* copy from G.vd, prevents feedback, */
216 float viewinv[4][4]; /* and to make sure we don't have to */
217 float persmat[4][4]; /* access G.vd from other space types */
221 char spacetype; /* spacetype where transforming is */
223 float vec[3]; /* translation, to show for widget */
224 float mat[3][3]; /* rot/rescale, to show for widget */
226 char *undostr; /* if set, uses this string for undo */
227 float spacemtx[3][3]; /* orientation matrix of the current space */
228 char spacename[32]; /* name of the current space */
230 struct Object *poseobj; /* if t->flag & T_POSE, this denotes pose object */
232 void *customData; /* Per Transform custom data */
234 /*************** NEW STUFF *********************/
241 struct wmEvent *event; /* last event, reset at the start of each transformApply and nulled at the transformEnd */
242 short mval[2]; /* current mouse position */
243 struct Object *obedit;
247 /* ******************** Macros & Prototypes *********************** */
250 #define NUM_NULL_ONE 2
251 #define NUM_NO_NEGATIVE 4
252 #define NUM_NO_ZERO 8
253 #define NUM_NO_FRACTION 16
254 #define NUM_AFFECT_ALL 32
256 /* NDOFINPUT FLAGS */
259 /* transinfo->state */
260 #define TRANS_RUNNING 0
261 #define TRANS_CONFIRM 1
262 #define TRANS_CANCEL 2
264 /* transinfo->flag */
265 #define T_OBJECT (1 << 0)
266 #define T_EDIT (1 << 1)
267 #define T_POSE (1 << 2)
268 #define T_TEXTURE (1 << 3)
269 #define T_CAMERA (1 << 4)
270 // trans on points, having no rotation/scale
271 #define T_POINTS (1 << 6)
272 // for manipulator exceptions, like scaling using center point, drawing help lines
273 #define T_USES_MANIPULATOR (1 << 7)
275 /* restrictions flags */
276 #define T_ALL_RESTRICTIONS ((1 << 8)|(1 << 9)|(1 << 10))
277 #define T_NO_CONSTRAINT (1 << 8)
278 #define T_NULL_ONE (1 << 9)
279 #define T_NO_ZERO (1 << 10)
281 #define T_PROP_EDIT (1 << 11)
282 #define T_PROP_CONNECTED (1 << 12)
284 /* if MMB is pressed or not */
285 #define T_MMB_PRESSED (1 << 13)
287 #define T_V3D_ALIGN (1 << 14)
288 /* for 2d views like uv or ipo */
289 #define T_2D_EDIT (1 << 15)
290 #define T_CLIP_UV (1 << 16)
292 #define T_FREE_CUSTOMDATA (1 << 17)
294 #define T_AUTOIK (1 << 18)
296 /* ******************************************************************************** */
298 /* transinfo->con->mode */
303 #define CON_SELECT 16
304 #define CON_NOFLIP 32 /* does not reorient vector to face viewport when on */
308 /* transdata->flag */
309 #define TD_SELECTED 1
310 #define TD_ACTIVE (1 << 1)
311 #define TD_NOACTION (1 << 2)
312 #define TD_USEQUAT (1 << 3)
313 #define TD_NOTCONNECTED (1 << 4)
314 #define TD_SINGLESIZE (1 << 5) /* used for scaling of MetaElem->rad */
316 #define TD_VERSE_OBJECT (1 << 6)
317 #define TD_VERSE_VERT (1 << 7)
319 #define TD_TIMEONLY (1 << 8)
320 #define TD_NOCENTER (1 << 9)
321 #define TD_NO_EXT (1 << 10) /* ext abused for particle key timing */
322 #define TD_SKIP (1 << 11) /* don't transform this data */
323 #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 */
324 #define TD_NO_LOC (1 << 13) /* when this is set, don't apply translation changes to this element */
326 /* transsnap->status */
328 #define TARGET_INIT 2
331 /* transsnap->modePoint */
335 /* transsnap->modeTarget */
336 #define SNAP_CLOSEST 0
337 #define SNAP_CENTER 1
338 #define SNAP_MEDIAN 2
339 #define SNAP_ACTIVE 3
342 void TFM_OT_transform(struct wmOperatorType *ot);
344 void initTransform(struct bContext *C, struct TransInfo *t, int mode, int context, struct wmEvent *event);
345 void transformEvent(TransInfo *t, struct wmEvent *event);
346 void transformApply(TransInfo *t);
347 int transformEnd(struct bContext *C, TransInfo *t);
349 void setTransformViewMatrices(TransInfo *t);
350 void convertViewVec(TransInfo *t, float *vec, short dx, short dy);
351 void projectIntView(TransInfo *t, float *vec, int *adr);
352 void projectFloatView(TransInfo *t, float *vec, float *adr);
354 void convertVecToDisplayNum(float *vec, float *num);
355 void convertDisplayNumToVec(float *num, float *vec);
357 void initWarp(TransInfo *t);
358 int handleEventWarp(TransInfo *t, struct wmEvent *event);
359 int Warp(TransInfo *t, short mval[2]);
361 void initShear(TransInfo *t);
362 int handleEventShear(TransInfo *t, struct wmEvent *event);
363 int Shear(TransInfo *t, short mval[2]);
365 void initResize(TransInfo *t);
366 int Resize(TransInfo *t, short mval[2]);
368 void initTranslation(TransInfo *t);
369 int Translation(TransInfo *t, short mval[2]);
371 void initToSphere(TransInfo *t);
372 int ToSphere(TransInfo *t, short mval[2]);
374 void initRotation(TransInfo *t);
375 int Rotation(TransInfo *t, short mval[2]);
377 void initShrinkFatten(TransInfo *t);
378 int ShrinkFatten(TransInfo *t, short mval[2]);
380 void initTilt(TransInfo *t);
381 int Tilt(TransInfo *t, short mval[2]);
383 void initCurveShrinkFatten(TransInfo *t);
384 int CurveShrinkFatten(TransInfo *t, short mval[2]);
386 void initTrackball(TransInfo *t);
387 int Trackball(TransInfo *t, short mval[2]);
389 void initPushPull(TransInfo *t);
390 int PushPull(TransInfo *t, short mval[2]);
392 void initBevel(TransInfo *t);
393 int handleEventBevel(TransInfo *t, struct wmEvent *event);
394 int Bevel(TransInfo *t, short mval[2]);
396 void initBevelWeight(TransInfo *t);
397 int BevelWeight(TransInfo *t, short mval[2]);
399 void initCrease(TransInfo *t);
400 int Crease(TransInfo *t, short mval[2]);
402 void initBoneSize(TransInfo *t);
403 int BoneSize(TransInfo *t, short mval[2]);
405 void initBoneEnvelope(TransInfo *t);
406 int BoneEnvelope(TransInfo *t, short mval[2]);
408 void initBoneRoll(TransInfo *t);
409 int BoneRoll(TransInfo *t, short mval[2]);
411 void initTimeTranslate(TransInfo *t);
412 int TimeTranslate(TransInfo *t, short mval[2]);
414 void initTimeSlide(TransInfo *t);
415 int TimeSlide(TransInfo *t, short mval[2]);
417 void initTimeScale(TransInfo *t);
418 int TimeScale(TransInfo *t, short mval[2]);
420 void initBakeTime(TransInfo *t);
421 int BakeTime(TransInfo *t, short mval[2]);
423 void initMirror(TransInfo *t);
424 int Mirror(TransInfo *t, short mval[2]);
426 void initAlign(TransInfo *t);
427 int Align(TransInfo *t, short mval[2]);
429 /*********************** transform_conversions.c ********** */
431 void flushTransGPactionData(TransInfo *t);
432 void flushTransIpoData(TransInfo *t);
433 void flushTransUVs(TransInfo *t);
434 void flushTransParticles(TransInfo *t);
435 int clipUVTransform(TransInfo *t, float *vec, int resize);
437 /*********************** exported from transform_manipulator.c ********** */
438 void draw_manipulator_ext(struct ScrArea *sa, int type, char axis, int col, float vec[3], float mat[][3]);
439 int calc_manipulator_stats(struct ScrArea *sa);
440 float get_drawsize(struct View3D *v3d, struct ScrArea *sa, float *co);
442 /*********************** TransData Creation and General Handling *********** */
443 void createTransData(struct bContext *C, TransInfo *t);
444 void sort_trans_data_dist(TransInfo *t);
445 void add_tdi_poin(float *poin, float *old, float delta);
446 void special_aftertrans_update(TransInfo *t);
448 void transform_autoik_update(TransInfo *t, short mode);
450 /* auto-keying stuff used by special_aftertrans_update */
451 short autokeyframe_cfra_can_key(struct Object *ob);
452 void autokeyframe_ob_cb_func(struct Object *ob, int tmode);
453 void autokeyframe_pose_cb_func(struct Object *ob, int tmode, short targetless_ik);
455 /*********************** Constraints *****************************/
457 void getConstraintMatrix(TransInfo *t);
458 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[]);
459 void setLocalConstraint(TransInfo *t, int mode, const char text[]);
460 void setUserConstraint(TransInfo *t, int mode, const char text[]);
462 void constraintNumInput(TransInfo *t, float vec[3]);
464 void getConstraintMatrix(TransInfo *t);
465 int isLockConstraint(TransInfo *t);
466 int getConstraintSpaceDimension(TransInfo *t);
467 char constraintModeToChar(TransInfo *t);
469 void startConstraint(TransInfo *t);
470 void stopConstraint(TransInfo *t);
472 void initSelectConstraint(TransInfo *t, float mtx[3][3]);
473 void selectConstraint(TransInfo *t);
474 void postSelectConstraint(TransInfo *t);
476 void setNearestAxis(TransInfo *t);
478 /*********************** Snapping ********************************/
486 void snapGrid(TransInfo *t, float *val);
487 void snapGridAction(TransInfo *t, float *val, GearsType action);
489 void initSnapping(struct TransInfo *t);
490 void applySnapping(TransInfo *t, float *vec);
491 void resetSnapping(TransInfo *t);
492 int handleSnapping(TransInfo *t, struct wmEvent *event);
493 void drawSnapping(TransInfo *t);
494 int usingSnappingNormal(TransInfo *t);
495 int validSnappingNormal(TransInfo *t);
497 /********************** Mouse Input ******************************/
506 INPUT_HORIZONTAL_RATIO,
507 INPUT_HORIZONTAL_ABSOLUTE,
508 INPUT_VERTICAL_RATIO,
509 INPUT_VERTICAL_ABSOLUTE
512 void initMouseInput(TransInfo *t, MouseInput *mi, int center[2], short mval[2]);
513 void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode);
514 int handleMouseInput(struct TransInfo *t, struct MouseInput *mi, struct wmEvent *event);
515 void applyMouseInput(struct TransInfo *t, struct MouseInput *mi, short mval[2], float output[3]);
517 /*********************** Generics ********************************/
519 void initTransInfo(struct bContext *C, TransInfo *t, struct wmEvent *event);
520 void postTrans (TransInfo *t);
521 void resetTransRestrictions(TransInfo *t);
523 void drawLine(float *center, float *dir, char axis, short options);
525 TransDataCurveHandleFlags *initTransDataCurveHandes(TransData *td, struct BezTriple *bezt);
527 /* DRAWLINE options flags */
532 void applyTransObjects(TransInfo *t);
533 void restoreTransObjects(TransInfo *t);
534 void recalcData(TransInfo *t);
536 void calculateCenter(TransInfo *t);
537 void calculateCenter2D(TransInfo *t);
538 void calculateCenterBound(TransInfo *t);
539 void calculateCenterMedian(TransInfo *t);
540 void calculateCenterCursor(TransInfo *t);
542 void calculateCenterCursor2D(TransInfo *t);
543 void calculatePropRatio(TransInfo *t);
545 void getViewVector(TransInfo *t, float coord[3], float vec[3]);
547 TransInfo * BIF_GetTransInfo(void);
549 /*********************** NumInput ********************************/
551 void initNumInput(NumInput *n);
552 void outputNumInput(NumInput *n, char *str);
553 short hasNumInput(NumInput *n);
554 void applyNumInput(NumInput *n, float *vec);
555 char handleNumInput(NumInput *n, struct wmEvent *event);
557 /*********************** NDofInput ********************************/
559 void initNDofInput(NDofInput *n);
560 int hasNDofInput(NDofInput *n);
561 void applyNDofInput(NDofInput *n, float *vec);
562 int handleNDofInput(NDofInput *n, struct wmEvent *event);
564 /* handleNDofInput return values */
565 #define NDOF_REFRESH 1
566 #define NDOF_NOMOVE 2
567 #define NDOF_CONFIRM 3
568 #define NDOF_CANCEL 4
571 /*********************** TransSpace ******************************/
573 int manageObjectSpace(int confirm, int set);
574 int manageMeshSpace(int confirm, int set);
575 int manageBoneSpace(int confirm, int set);
577 /* Those two fill in mat and return non-zero on success */
578 int createSpaceNormal(float mat[3][3], float normal[3]);
579 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3]);
581 int addMatrixSpace(float mat[3][3], char name[]);
582 int addObjectSpace(struct Object *ob);
583 void applyTransformOrientation(void);
586 #define ORIENTATION_NONE 0
587 #define ORIENTATION_NORMAL 1
588 #define ORIENTATION_VERT 2
589 #define ORIENTATION_EDGE 3
590 #define ORIENTATION_FACE 4
592 int getTransformOrientation(struct bContext *C, float normal[3], float plane[3], int activeOnly);
593 int createSpaceNormal(float mat[3][3], float normal[3]);
594 int createSpaceNormalTangent(float mat[3][3], float normal[3], float tangent[3]);