Merging r38562 through r38614 from trunkinto soc-2011-tomato
[blender.git] / source / blender / editors / transform / transform_conversions.c
index 0a5e290643af3f46806efc46905ca185ebeaade9..c87fe4c14ab1bde475a4915a0e53b5d97c3cd1d6 100644 (file)
@@ -53,6 +53,7 @@
 #include "DNA_scene_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_gpencil_types.h"
+#include "DNA_movieclip_types.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -76,6 +77,8 @@
 #include "BKE_bmesh.h"
 #include "BKE_scene.h"
 #include "BKE_report.h"
+#include "BKE_tracking.h"
+#include "BKE_movieclip.h"
 
 
 #include "ED_anim_api.h"
@@ -90,6 +93,7 @@
 #include "ED_node.h"
 #include "ED_types.h"
 #include "ED_uvedit.h"
+#include "ED_clip.h"
 #include "ED_curve.h" /* for ED_curve_editnurbs */
 #include "ED_util.h"  /* for crazyspace correction */
 
@@ -4770,6 +4774,9 @@ void special_aftertrans_update(bContext *C, TransInfo *t)
                ED_node_link_intersect_test(t->sa, 0);
                
        }
+       else if (t->spacetype == SPACE_CLIP) {
+               /* pass */
+       }
        else if (t->spacetype == SPACE_ACTION) {
                SpaceAction *saction= (SpaceAction *)t->sa->spacedata.first;
                bAnimContext ac;
@@ -5216,6 +5223,147 @@ static void createTransNodeData(bContext *C, TransInfo *t)
        CTX_DATA_END
 }
 
+/* *** CLIP EDITOR *** */
+
+static void markerToTransDataInit(TransData *td, TransData2D *td2d, float *loc, float *rel)
+{
+       td2d->loc[0] = loc[0]; /* hold original location */
+       td2d->loc[1] = loc[1];
+       td2d->loc[2] = 0.0f;
+
+       if(rel) {       /* XXX: could it be nicer? */
+               td2d->loc[0]+= rel[0];
+               td2d->loc[1]+= rel[1];
+
+               td->extra= rel;
+       }
+
+       td2d->loc2d = loc; /* current location */
+
+       td->flag = 0;
+       td->loc = td2d->loc;
+       VECCOPY(td->center, td->loc);
+       VECCOPY(td->iloc, td->loc);
+
+       memset(td->axismtx, 0, sizeof(td->axismtx));
+       td->axismtx[2][2] = 1.0f;
+
+       td->ext= NULL; td->val= NULL;
+
+       td->flag |= TD_SELECTED;
+       td->dist= 0.0;
+
+       unit_m3(td->mtx);
+       unit_m3(td->smtx);
+}
+
+static void trackToTransData(SpaceClip *sc, TransData *td, TransData2D *td2d, MovieTrackingTrack *track)
+{
+       MovieTrackingMarker *marker= BKE_tracking_ensure_marker(track, sc->user.framenr);
+
+       track->transflag= marker->flag;
+
+       marker->flag&= ~(MARKER_DISABLED|MARKER_TRACKED);
+
+       if(track->flag&SELECT)
+               markerToTransDataInit(td++, td2d++, marker->pos, NULL);
+
+       if(track->pat_flag&SELECT) {
+               markerToTransDataInit(td++, td2d++, track->pat_min, marker->pos);
+               markerToTransDataInit(td++, td2d++, track->pat_max, marker->pos);
+       }
+
+       if(track->search_flag&SELECT) {
+               markerToTransDataInit(td++, td2d++, track->search_min, marker->pos);
+               markerToTransDataInit(td++, td2d++, track->search_max, marker->pos);
+       }
+}
+
+static void createTransTrackingData(bContext *C, TransInfo *t)
+{
+       TransData *td;
+       TransData2D *td2d;
+       SpaceClip *sc = CTX_wm_space_clip(C);
+       MovieClip *clip = ED_space_clip(sc);
+       MovieTrackingTrack *track;
+       MovieTrackingMarker *marker;
+       int framenr = sc->user.framenr;
+
+       if(clip && !BKE_movieclip_has_frame(clip, &sc->user)) {
+               t->total = 0;
+               return;
+       }
+
+       /* count */
+       t->total = 0;
+
+       track = clip->tracking.tracks.first;
+       while(track) {
+               if(TRACK_SELECTED(track) && (track->flag&TRACK_LOCKED)==0) {
+                       marker= BKE_tracking_get_marker(track, framenr);
+
+                       if(marker) {
+                               if(track->flag&SELECT) t->total++;
+                               if(track->pat_flag&SELECT) t->total+= 2;
+                               if(track->search_flag&SELECT) t->total+= 2;
+                       }
+               }
+
+               track = track->next;
+       }
+
+       td = t->data = MEM_callocN(t->total*sizeof(TransData), "TransTracking TransData");
+       td2d = t->data2d = MEM_callocN(t->total*sizeof(TransData2D), "TransTracking TransData2D");
+
+       /* create actual data */
+       track = clip->tracking.tracks.first;
+       while(track) {
+               if(TRACK_SELECTED(track) && (track->flag&TRACK_LOCKED)==0) {
+                       marker= BKE_tracking_get_marker(track, framenr);
+
+                       if(marker) {
+                               trackToTransData(sc, td, td2d, track);
+
+                               if((marker->flag&MARKER_DISABLED)==0) {
+                                       if(track->flag&SELECT) {td++; td2d++;}
+                                       if(track->pat_flag&SELECT) {td+= 2; td2d+= 2;}
+                               }
+
+                               if(track->search_flag&SELECT) {
+                                       td+= 2;
+                                       td2d+= 2;
+
+                                       if(marker->flag&MARKER_DISABLED) {
+                                               td+= 3;
+                                               td2d+= 3;
+                                       }
+                               };
+                       }
+               }
+
+               track = track->next;
+       }
+}
+
+void flushTransTracking(TransInfo *t)
+{
+       TransData *td;
+       TransData2D *td2d;
+       int a;
+
+       /* flush to 2d vector from internally used 3d vector */
+       for(a=0, td= t->data, td2d= t->data2d; a<t->total; a++, td2d++, td++) {
+               if(t->flag&T_RELATIVE_POSITION && !td->extra)
+                       continue;
+
+               td2d->loc2d[0] = td2d->loc[0];
+               td2d->loc2d[1] = td2d->loc[1];
+
+               if(td->extra)
+                       sub_v2_v2(td2d->loc2d, td->extra);
+       }
+}
+
 void createTransData(bContext *C, TransInfo *t)
 {
        Scene *scene = t->scene;
@@ -5281,6 +5429,10 @@ void createTransData(bContext *C, TransInfo *t)
                        sort_trans_data_dist(t);
                }
        }
+       else if (t->spacetype == SPACE_CLIP) {
+               t->flag |= T_POINTS|T_2D_EDIT;
+               createTransTrackingData(C, t);
+       }
        else if (t->obedit) {
                t->ext = NULL;
                if (t->obedit->type == OB_MESH) {