Camera tracking: tracks copy/paste operator
[blender.git] / source / blender / editors / space_clip / space_clip.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) 2011 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_clip/space_clip.c
29  *  \ingroup spclip
30  */
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_scene_types.h"
36 #include "DNA_movieclip_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_math.h"
43
44 #include "BKE_main.h"
45 #include "BKE_context.h"
46 #include "BKE_screen.h"
47 #include "BKE_movieclip.h"
48 #include "BKE_tracking.h"
49
50 #include "IMB_imbuf_types.h"
51
52 #include "ED_screen.h"
53 #include "ED_clip.h"
54 #include "ED_transform.h"
55
56 #include "IMB_imbuf.h"
57
58 #include "BIF_gl.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "UI_interface.h"
64 #include "UI_resources.h"
65 #include "UI_view2d.h"
66
67 #include "RNA_access.h"
68
69
70 #include "clip_intern.h"        // own include
71
72 static void init_preview_region(const bContext *C, ARegion *ar)
73 {
74         Scene *scene= CTX_data_scene(C);
75
76         ar->regiontype= RGN_TYPE_PREVIEW;
77         ar->alignment= RGN_ALIGN_TOP;
78         ar->flag|= RGN_FLAG_HIDDEN;
79
80         ar->v2d.tot.xmin= 0.0f;
81         ar->v2d.tot.ymin= (float)scene->r.sfra - 10.0f;
82         ar->v2d.tot.xmax= (float)scene->r.efra;
83         ar->v2d.tot.ymax= 10.0f;
84
85         ar->v2d.cur= ar->v2d.tot;
86
87         ar->v2d.min[0]= FLT_MIN;
88         ar->v2d.min[1]= FLT_MIN;
89
90         ar->v2d.max[0]= MAXFRAMEF;
91         ar->v2d.max[1]= FLT_MAX;
92
93         ar->v2d.scroll= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
94         ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
95
96         ar->v2d.keeptot= 0;
97 }
98
99 static ARegion *clip_has_preview_region(const bContext *C, ScrArea *sa)
100 {
101         ARegion *ar, *arnew;
102
103         ar= BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
104         if(ar)
105                 return ar;
106
107         /* add subdiv level; after header */
108         ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
109
110         /* is error! */
111         if(ar==NULL)
112                 return NULL;
113
114         arnew= MEM_callocN(sizeof(ARegion), "clip preview region");
115
116         BLI_insertlinkbefore(&sa->regionbase, ar, arnew);
117         init_preview_region(C, arnew);
118
119         return arnew;
120 }
121
122 static void clip_scopes_tag_refresh(ScrArea *sa)
123 {
124         SpaceClip *sc= (SpaceClip *)sa->spacedata.first;
125         ARegion *ar;
126
127         if(sc->mode!=SC_MODE_TRACKING)
128                 return;
129
130         /* only while proeprties are visible */
131         for (ar=sa->regionbase.first; ar; ar=ar->next) {
132                 if (ar->regiontype == RGN_TYPE_UI && ar->flag & RGN_FLAG_HIDDEN)
133                         return;
134         }
135
136         sc->scopes.ok= 0;
137 }
138
139 static void clip_stabilization_tag_refresh(ScrArea *sa)
140 {
141         SpaceClip *sc= (SpaceClip *)sa->spacedata.first;
142         MovieClip *clip= ED_space_clip(sc);
143
144         if(clip) {
145                 MovieTrackingStabilization *stab= &clip->tracking.stabilization;
146
147                 stab->ok= 0;
148         }
149 }
150
151 /* ******************** default callbacks for clip space ***************** */
152
153 static SpaceLink *clip_new(const bContext *C)
154 {
155         ARegion *ar;
156         SpaceClip *sc;
157
158         sc= MEM_callocN(sizeof(SpaceClip), "initclip");
159         sc->spacetype= SPACE_CLIP;
160         sc->flag= SC_SHOW_MARKER_PATTERN|SC_SHOW_TRACK_PATH|SC_SHOW_GPENCIL|SC_MANUAL_CALIBRATION|SC_SHOW_GRAPH_TRACKS|SC_SHOW_GRAPH_FRAMES;
161         sc->zoom= 1.0f;
162         sc->path_length= 20;
163         sc->scopes.track_preview_height= 120;
164
165         /* header */
166         ar= MEM_callocN(sizeof(ARegion), "header for clip");
167
168         BLI_addtail(&sc->regionbase, ar);
169         ar->regiontype= RGN_TYPE_HEADER;
170         ar->alignment= RGN_ALIGN_BOTTOM;
171
172         /* tools view */
173         ar= MEM_callocN(sizeof(ARegion), "tools for clip");
174
175         BLI_addtail(&sc->regionbase, ar);
176         ar->regiontype= RGN_TYPE_TOOLS;
177         ar->alignment= RGN_ALIGN_LEFT;
178
179         /* tool properties */
180         ar= MEM_callocN(sizeof(ARegion), "tool properties for clip");
181
182         BLI_addtail(&sc->regionbase, ar);
183         ar->regiontype= RGN_TYPE_TOOL_PROPS;
184         ar->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
185
186         /* properties view */
187         ar= MEM_callocN(sizeof(ARegion), "properties for clip");
188
189         BLI_addtail(&sc->regionbase, ar);
190         ar->regiontype= RGN_TYPE_UI;
191         ar->alignment= RGN_ALIGN_RIGHT;
192
193         /* preview view */
194         ar= MEM_callocN(sizeof(ARegion), "preview for clip");
195
196         BLI_addtail(&sc->regionbase, ar);
197         init_preview_region(C, ar);
198
199         /* main area */
200         ar= MEM_callocN(sizeof(ARegion), "main area for clip");
201
202         BLI_addtail(&sc->regionbase, ar);
203         ar->regiontype= RGN_TYPE_WINDOW;
204
205         return (SpaceLink *)sc;
206 }
207
208 /* not spacelink itself */
209 static void clip_free(SpaceLink *sl)
210 {
211         SpaceClip *sc= (SpaceClip*) sl;
212
213         sc->clip= NULL;
214
215         if(sc->scopes.track_preview)
216                 IMB_freeImBuf(sc->scopes.track_preview);
217 }
218
219 /* spacetype; init callback */
220 static void clip_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
221 {
222
223 }
224
225 static SpaceLink *clip_duplicate(SpaceLink *sl)
226 {
227         SpaceClip *scn= MEM_dupallocN(sl);
228
229         /* clear or remove stuff from old */
230         scn->scopes.track_preview= NULL;
231         scn->scopes.ok= 0;
232
233         return (SpaceLink *)scn;
234 }
235
236 static void clip_listener(ScrArea *sa, wmNotifier *wmn)
237 {
238         /* context changes */
239         switch(wmn->category) {
240                 case NC_SCENE:
241                         switch(wmn->data) {
242                                 case ND_FRAME:
243                                         clip_scopes_tag_refresh(sa);
244                                         /* no break! */
245
246                                 case ND_FRAME_RANGE:
247                                         ED_area_tag_refresh(sa);
248                                         ED_area_tag_redraw(sa);
249                                         break;
250                         }
251                         break;
252                 case NC_MOVIECLIP:
253                         switch(wmn->data) {
254                                 case ND_DISPLAY:
255                                 case ND_SELECT:
256                                         clip_scopes_tag_refresh(sa);
257                                         ED_area_tag_redraw(sa);
258                                         break;
259                         }
260                         switch(wmn->action) {
261                                 case NA_REMOVED:
262                                 case NA_EDITED:
263                                 case NA_EVALUATED:
264                                         clip_stabilization_tag_refresh(sa);
265                                         /* no break! */
266
267                                 case NA_SELECTED:
268                                         clip_scopes_tag_refresh(sa);
269                                         ED_area_tag_redraw(sa);
270                                         break;
271                         }
272                         break;
273                 case NC_GEOM:
274                         switch(wmn->data) {
275                                 case ND_SELECT:
276                                         clip_scopes_tag_refresh(sa);
277                                         ED_area_tag_redraw(sa);
278                                         break;
279                         }
280                         break;
281                  case NC_SCREEN:
282                         if(wmn->data==ND_ANIMPLAY) {
283                                 ED_area_tag_redraw(sa);
284                         }
285                         break;
286                 case NC_SPACE:
287                         if(wmn->data==ND_SPACE_CLIP) {
288                                 clip_scopes_tag_refresh(sa);
289                                 clip_stabilization_tag_refresh(sa);
290                                 ED_area_tag_redraw(sa);
291                         }
292                         break;
293         }
294 }
295
296 static void clip_operatortypes(void)
297 {
298         /* ** clip_ops.c ** */
299         WM_operatortype_append(CLIP_OT_open);
300         WM_operatortype_append(CLIP_OT_reload);
301         WM_operatortype_append(CLIP_OT_view_pan);
302         WM_operatortype_append(CLIP_OT_view_zoom);
303         WM_operatortype_append(CLIP_OT_view_zoom_in);
304         WM_operatortype_append(CLIP_OT_view_zoom_out);
305         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
306         WM_operatortype_append(CLIP_OT_view_all);
307         WM_operatortype_append(CLIP_OT_view_selected);
308         WM_operatortype_append(CLIP_OT_change_frame);
309         WM_operatortype_append(CLIP_OT_rebuild_proxy);
310         WM_operatortype_append(CLIP_OT_mode_set);
311
312         /* ** clip_toolbar.c ** */
313         WM_operatortype_append(CLIP_OT_tools);
314         WM_operatortype_append(CLIP_OT_properties);
315
316         /* ** tracking_ops.c ** */
317
318         /* navigation */
319         WM_operatortype_append(CLIP_OT_frame_jump);
320
321         /* foorage */
322         WM_operatortype_append(CLIP_OT_set_center_principal);
323
324         /* selection */
325         WM_operatortype_append(CLIP_OT_select);
326         WM_operatortype_append(CLIP_OT_select_all);
327         WM_operatortype_append(CLIP_OT_select_border);
328         WM_operatortype_append(CLIP_OT_select_circle);
329         WM_operatortype_append(CLIP_OT_select_grouped);
330
331         /* markers */
332         WM_operatortype_append(CLIP_OT_add_marker);
333         WM_operatortype_append(CLIP_OT_slide_marker);
334         WM_operatortype_append(CLIP_OT_delete_track);
335         WM_operatortype_append(CLIP_OT_delete_marker);
336
337         /* track */
338         WM_operatortype_append(CLIP_OT_track_markers);
339
340         /* solving */
341         WM_operatortype_append(CLIP_OT_solve_camera);
342         WM_operatortype_append(CLIP_OT_clear_solution);
343
344         WM_operatortype_append(CLIP_OT_disable_markers);
345         WM_operatortype_append(CLIP_OT_hide_tracks);
346         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
347         WM_operatortype_append(CLIP_OT_lock_tracks);
348
349         /* orientation */
350         WM_operatortype_append(CLIP_OT_set_origin);
351         WM_operatortype_append(CLIP_OT_set_floor);
352         WM_operatortype_append(CLIP_OT_set_axis);
353         WM_operatortype_append(CLIP_OT_set_scale);
354         WM_operatortype_append(CLIP_OT_set_solution_scale);
355
356         /* detect */
357         WM_operatortype_append(CLIP_OT_detect_features);
358
359         /* stabilization */
360         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
361         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
362         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
363         WM_operatortype_append(CLIP_OT_stabilize_2d_set_rotation);
364
365         /* clean-up */
366         WM_operatortype_append(CLIP_OT_clear_track_path);
367         WM_operatortype_append(CLIP_OT_join_tracks);
368         WM_operatortype_append(CLIP_OT_track_copy_color);
369
370         WM_operatortype_append(CLIP_OT_clean_tracks);
371
372         /* graph editing */
373         WM_operatortype_append(CLIP_OT_graph_select);
374         WM_operatortype_append(CLIP_OT_graph_delete_curve);
375         WM_operatortype_append(CLIP_OT_graph_delete_knot);
376
377         /* object tracking */
378         WM_operatortype_append(CLIP_OT_tracking_object_new);
379         WM_operatortype_append(CLIP_OT_tracking_object_remove);
380
381         /* clipboard */
382         WM_operatortype_append(CLIP_OT_copy_tracks);
383         WM_operatortype_append(CLIP_OT_paste_tracks);
384 }
385
386 static void clip_keymap(struct wmKeyConfig *keyconf)
387 {
388         wmKeyMap *keymap;
389         wmKeyMapItem *kmi;
390
391         /* ******** Global hotkeys avalaible for all regions ******** */
392
393         keymap= WM_keymap_find(keyconf, "Clip", SPACE_CLIP, 0);
394
395         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
396
397         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
398         WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
399
400         /* 2d tracking */
401         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
402         RNA_boolean_set(kmi->ptr, "backwards", 1);
403         WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
404         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
405         RNA_boolean_set(kmi->ptr, "sequence", 1);
406         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
407         RNA_boolean_set(kmi->ptr, "backwards", 1);
408         RNA_boolean_set(kmi->ptr, "sequence", 1);
409
410         /* mode */
411         kmi= WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
412         RNA_enum_set(kmi->ptr, "mode", SC_MODE_RECONSTRUCTION);
413         RNA_boolean_set(kmi->ptr, "toggle", 1);
414
415         kmi= WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, KM_CTRL, 0);
416         RNA_enum_set(kmi->ptr, "mode", SC_MODE_DISTORTION);
417         RNA_boolean_set(kmi->ptr, "toggle", 1);
418
419         /* ******** Hotkeys avalaible for main region only ******** */
420
421         keymap= WM_keymap_find(keyconf, "Clip Editor", SPACE_CLIP, 0);
422
423         /* ** View/navigation ** */
424
425         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
426         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
427         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
428
429         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
430         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
431         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
432         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
433         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
434         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
435
436         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
437         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
438         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
439         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
440         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
441         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
442         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
443
444         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
445         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
446
447         /* jump to special frame */
448         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
449         RNA_enum_set(kmi->ptr, "position", 0);
450
451         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
452         RNA_enum_set(kmi->ptr, "position", 1);
453
454         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
455         RNA_enum_set(kmi->ptr, "position", 2);
456
457         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
458         RNA_enum_set(kmi->ptr, "position", 3);
459
460         /* "timeline" */
461         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
462
463         /* selection */
464         WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
465         RNA_boolean_set(WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", 1);
466         WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
467         RNA_enum_set(WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "action", SEL_INVERT);
468         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
469         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
470         WM_keymap_add_menu(keymap, "CLIP_MT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
471
472         /* marker */
473         WM_keymap_add_item(keymap, "CLIP_OT_add_marker_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
474
475         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
476         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", XKEY, KM_PRESS, KM_SHIFT, 0);
477
478         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
479
480         kmi= WM_keymap_add_item(keymap, "CLIP_OT_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
481         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
482
483         /* tracks */
484         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
485         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
486
487         kmi= WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_CTRL, 0);
488         RNA_enum_set(kmi->ptr, "action", 0);    /* lock */
489
490         kmi= WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_ALT, 0);
491         RNA_enum_set(kmi->ptr, "action", 1);    /* unlock */
492
493         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
494
495         kmi= WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
496         RNA_boolean_set(kmi->ptr, "unselected", 1);
497
498         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
499
500         /* clean-up */
501         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
502         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
503         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
504         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
505         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
506         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
507
508         WM_keymap_add_item(keymap, "CLIP_OT_join_tracks", JKEY, KM_PRESS, KM_CTRL, 0);
509
510         /* menus */
511         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
512
513         /* display */
514         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
515         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
516
517         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0);
518         RNA_string_set(kmi->ptr, "data_path", "space_data.use_mute_footage");
519
520         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
521
522         /* ******** Hotkeys avalaible for preview region only ******** */
523
524         keymap= WM_keymap_find(keyconf, "Clip Graph Editor", SPACE_CLIP, 0);
525
526         /* "timeline" */
527         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", ACTIONMOUSE, KM_PRESS, 0, 0);
528
529         /* selection */
530         WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, 0, 0);
531         RNA_boolean_set(WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", 1);
532
533         /* delete */
534         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", DELKEY, KM_PRESS, 0, 0);
535         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", XKEY, KM_PRESS, 0, 0);
536
537         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", DELKEY, KM_PRESS, KM_SHIFT, 0);
538         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", XKEY, KM_PRESS, KM_SHIFT, 0);
539 }
540
541 const char *clip_context_dir[]= {"edit_movieclip", NULL};
542
543 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
544 {
545         SpaceClip *sc= CTX_wm_space_clip(C);
546
547         if(CTX_data_dir(member)) {
548                 CTX_data_dir_set(result, clip_context_dir);
549                 return 1;
550         }
551         else if(CTX_data_equals(member, "edit_movieclip")) {
552                 CTX_data_id_pointer_set(result, &sc->clip->id);
553                 return 1;
554         }
555
556         return 0;
557 }
558
559 static void clip_refresh(const bContext *C, ScrArea *sa)
560 {
561         wmWindowManager *wm= CTX_wm_manager(C);
562         wmWindow *window= CTX_wm_window(C);
563         SpaceClip *sc= (SpaceClip *)sa->spacedata.first;
564         ARegion *ar_main= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
565         ARegion *ar_preview= clip_has_preview_region(C, sa);
566         int view_changed= 0;
567
568         switch (sc->view) {
569                 case SC_VIEW_CLIP:
570                         if (ar_preview && !(ar_preview->flag & RGN_FLAG_HIDDEN)) {
571                                 ar_preview->flag |= RGN_FLAG_HIDDEN;
572                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
573                                 WM_event_remove_handlers((bContext*)C, &ar_preview->handlers);
574                                 view_changed= 1;
575                         }
576                         if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
577                                 ar_main->alignment= RGN_ALIGN_NONE;
578                                 view_changed= 1;
579                         }
580                         if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
581                                 ar_preview->alignment= RGN_ALIGN_NONE;
582                                 view_changed= 1;
583                         }
584                         break;
585                 case SC_VIEW_GRAPH:
586                         if (ar_preview && (ar_preview->flag & RGN_FLAG_HIDDEN)) {
587                                 ar_preview->flag &= ~RGN_FLAG_HIDDEN;
588                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
589                                 ar_preview->v2d.cur = ar_preview->v2d.tot;
590                                 view_changed= 1;
591                         }
592                         if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
593                                 ar_main->alignment= RGN_ALIGN_NONE;
594                                 view_changed= 1;
595                         }
596                         if (ar_preview && ar_preview->alignment != RGN_ALIGN_TOP) {
597                                 ar_preview->alignment= RGN_ALIGN_TOP;
598                                 view_changed= 1;
599                         }
600                         break;
601         }
602
603         if(view_changed) {
604                 ED_area_initialize(wm, window, sa);
605                 ED_area_tag_redraw(sa);
606         }
607
608         BKE_movieclip_user_set_frame(&sc->user, CTX_data_scene(C)->r.cfra);
609 }
610
611 /********************* main region ********************/
612
613 /* sets up the fields of the View2D from zoom and offset */
614 static void movieclip_main_area_set_view2d(SpaceClip *sc, ARegion *ar)
615 {
616         MovieClip *clip= ED_space_clip(sc);
617         float x1, y1, w, h;
618         int width, height, winx, winy;
619
620         ED_space_clip_size(sc, &width, &height);
621
622         w= width;
623         h= height;
624
625         if(clip)
626                 h*= clip->aspy/clip->aspx/clip->tracking.camera.pixel_aspect;
627
628         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
629         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
630
631         ar->v2d.tot.xmin= 0;
632         ar->v2d.tot.ymin= 0;
633         ar->v2d.tot.xmax= w;
634         ar->v2d.tot.ymax= h;
635
636         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
637         ar->v2d.mask.xmax= winx;
638         ar->v2d.mask.ymax= winy;
639
640         /* which part of the image space do we see? */
641         x1= ar->winrct.xmin+(winx-sc->zoom*w)/2.0f;
642         y1= ar->winrct.ymin+(winy-sc->zoom*h)/2.0f;
643
644         x1-= sc->zoom*sc->xof;
645         y1-= sc->zoom*sc->yof;
646
647         /* relative display right */
648         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sc->zoom);
649         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sc->zoom);
650
651         /* relative display left */
652         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sc->zoom);
653         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sc->zoom);
654
655         /* normalize 0.0..1.0 */
656         ar->v2d.cur.xmin /= w;
657         ar->v2d.cur.xmax /= w;
658         ar->v2d.cur.ymin /= h;
659         ar->v2d.cur.ymax /= h;
660 }
661
662 /* add handlers, stuff you only do once or on area/region changes */
663 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
664 {
665         wmKeyMap *keymap;
666
667         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
668
669         /* own keymap */
670         keymap= WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
671         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
672
673         keymap= WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
674         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
675 }
676
677 static void clip_main_area_draw(const bContext *C, ARegion *ar)
678 {
679         /* draw entirely, view changes should be handled here */
680         SpaceClip *sc= CTX_wm_space_clip(C);
681         Scene *scene= CTX_data_scene(C);
682         MovieClip *clip= ED_space_clip(sc);
683
684         /* if trcking is in progress, we should sunchronize framenr from clipuser
685            so latest tracked frame would be shown */
686         if(clip && clip->tracking_context)
687                 BKE_tracking_sync_user(&sc->user, clip->tracking_context);
688
689         if(sc->flag&SC_LOCK_SELECTION) {
690                 ImBuf *tmpibuf= NULL;
691
692                 if(clip && clip->tracking.stabilization.flag&TRACKING_2D_STABILIZATION) {
693                         tmpibuf= ED_space_clip_get_stable_buffer(sc, NULL, NULL, NULL);
694                 }
695
696                 if(ED_clip_view_selection(sc, ar, 0)) {
697                         sc->xof+= sc->xlockof;
698                         sc->yof+= sc->ylockof;
699                 }
700
701                 if(tmpibuf)
702                         IMB_freeImBuf(tmpibuf);
703         }
704
705         /* clear and setup matrix */
706         UI_ThemeClearColor(TH_BACK);
707         glClear(GL_COLOR_BUFFER_BIT);
708
709         /* data... */
710         movieclip_main_area_set_view2d(sc, ar);
711
712         clip_draw_main(sc, ar, scene);
713
714         /* Grease Pencil */
715         clip_draw_grease_pencil((bContext *)C, 1);
716
717         /* reset view matrix */
718         UI_view2d_view_restore(C);
719
720         /* draw Grease Pencil - screen space only */
721         clip_draw_grease_pencil((bContext *)C, 0);
722 }
723
724 static void clip_main_area_listener(ARegion *ar, wmNotifier *wmn)
725 {
726         /* context changes */
727         switch(wmn->category) {
728                 case NC_SCREEN:
729                         if (wmn->data==ND_GPENCIL)
730                                 ED_region_tag_redraw(ar);
731                 break;
732         }
733 }
734
735 /****************** preview region ******************/
736
737 static void clip_preview_area_init(wmWindowManager *wm, ARegion *ar)
738 {
739         wmKeyMap *keymap;
740
741         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
742
743         /* own keymap */
744         keymap= WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
745         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
746
747         keymap= WM_keymap_find(wm->defaultconf, "Clip Graph Editor", SPACE_CLIP, 0);
748         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
749 }
750
751 static void clip_preview_area_draw(const bContext *C, ARegion *ar)
752 {
753         View2D *v2d= &ar->v2d;
754         View2DScrollers *scrollers;
755         SpaceClip *sc= CTX_wm_space_clip(C);
756         Scene *scene= CTX_data_scene(C);
757         short unitx= V2D_UNIT_FRAMESCALE, unity= V2D_UNIT_VALUES;
758
759         /* clear and setup matrix */
760         UI_ThemeClearColor(TH_BACK);
761         glClear(GL_COLOR_BUFFER_BIT);
762
763         UI_view2d_view_ortho(v2d);
764
765         /* data... */
766         clip_draw_graph(sc, ar, scene);
767
768         /* reset view matrix */
769         UI_view2d_view_restore(C);
770
771         /* scrollers */
772         scrollers= UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
773         UI_view2d_scrollers_draw(C, v2d, scrollers);
774         UI_view2d_scrollers_free(scrollers);
775 }
776
777 static void clip_preview_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
778 {
779 }
780
781 /****************** header region ******************/
782
783 /* add handlers, stuff you only do once or on area/region changes */
784 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
785 {
786         ED_region_header_init(ar);
787 }
788
789 static void clip_header_area_draw(const bContext *C, ARegion *ar)
790 {
791         ED_region_header(C, ar);
792 }
793
794 /****************** tools region ******************/
795
796 /* add handlers, stuff you only do once or on area/region changes */
797 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
798 {
799         ED_region_panels_init(wm, ar);
800 }
801
802 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
803 {
804         ED_region_panels(C, ar, 1, NULL, -1);
805 }
806
807 /****************** tool properties region ******************/
808
809 static void clip_props_area_listener(ARegion *ar, wmNotifier *wmn)
810 {
811         /* context changes */
812         switch(wmn->category) {
813                 case NC_WM:
814                         if(wmn->data == ND_HISTORY)
815                                 ED_region_tag_redraw(ar);
816                         break;
817                 case NC_SCENE:
818                         if(wmn->data == ND_MODE)
819                                 ED_region_tag_redraw(ar);
820                         break;
821                 case NC_SPACE:
822                         if(wmn->data == ND_SPACE_CLIP)
823                                 ED_region_tag_redraw(ar);
824                         break;
825                 case NC_SCREEN:
826                         if(wmn->data == ND_GPENCIL)
827                                 ED_region_tag_redraw(ar);
828                         break;
829         }
830 }
831
832 /****************** properties region ******************/
833
834 /* add handlers, stuff you only do once or on area/region changes */
835 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
836 {
837         wmKeyMap *keymap;
838
839         ED_region_panels_init(wm, ar);
840
841         keymap= WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
842         WM_event_add_keymap_handler(&ar->handlers, keymap);
843 }
844
845 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
846 {
847         SpaceClip *sc= CTX_wm_space_clip(C);
848
849         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
850
851         ED_region_panels(C, ar, 1, NULL, -1);
852 }
853
854 static void clip_properties_area_listener(ARegion *ar, wmNotifier *wmn)
855 {
856         /* context changes */
857         switch(wmn->category) {
858                 case NC_SCREEN:
859                         if (wmn->data==ND_GPENCIL)
860                                 ED_region_tag_redraw(ar);
861                         break;
862                 case NC_BRUSH:
863                         if(wmn->action==NA_EDITED)
864                                 ED_region_tag_redraw(ar);
865                         break;
866         }
867 }
868
869 /********************* registration ********************/
870
871 /* only called once, from space/spacetypes.c */
872 void ED_spacetype_clip(void)
873 {
874         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype clip");
875         ARegionType *art;
876
877         st->spaceid= SPACE_CLIP;
878         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
879
880         st->new= clip_new;
881         st->free= clip_free;
882         st->init= clip_init;
883         st->duplicate= clip_duplicate;
884         st->operatortypes= clip_operatortypes;
885         st->keymap= clip_keymap;
886         st->listener= clip_listener;
887         st->context= clip_context;
888         st->refresh= clip_refresh;
889
890         /* regions: main window */
891         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
892         art->regionid= RGN_TYPE_WINDOW;
893         art->init= clip_main_area_init;
894         art->draw= clip_main_area_draw;
895         art->listener= clip_main_area_listener;
896         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_GPENCIL;
897
898         BLI_addhead(&st->regiontypes, art);
899
900         /* preview */
901         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region preview");
902         art->regionid = RGN_TYPE_PREVIEW;
903         art->prefsizey = 240;
904         art->init= clip_preview_area_init;
905         art->draw= clip_preview_area_draw;
906         art->listener= clip_preview_area_listener;
907         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
908
909         BLI_addhead(&st->regiontypes, art);
910
911         /* regions: properties */
912         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
913         art->regionid= RGN_TYPE_UI;
914         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
915         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
916         art->init= clip_properties_area_init;
917         art->draw= clip_properties_area_draw;
918         art->listener= clip_properties_area_listener;
919         BLI_addhead(&st->regiontypes, art);
920         ED_clip_buttons_register(art);
921
922         /* regions: tools */
923         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
924         art->regionid= RGN_TYPE_TOOLS;
925         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
926         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
927         art->listener= clip_props_area_listener;
928         art->init= clip_tools_area_init;
929         art->draw= clip_tools_area_draw;
930
931         BLI_addhead(&st->regiontypes, art);
932
933         /* tool properties */
934         art= MEM_callocN(sizeof(ARegionType), "spacetype clip tool properties region");
935         art->regionid = RGN_TYPE_TOOL_PROPS;
936         art->prefsizex= 0;
937         art->prefsizey= 120;
938         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
939         art->listener= clip_props_area_listener;
940         art->init= clip_tools_area_init;
941         art->draw= clip_tools_area_draw;
942         ED_clip_tool_props_register(art);
943
944         BLI_addhead(&st->regiontypes, art);
945
946         /* regions: header */
947         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
948         art->regionid= RGN_TYPE_HEADER;
949         art->prefsizey= HEADERY;
950         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
951
952         art->init= clip_header_area_init;
953         art->draw= clip_header_area_draw;
954
955         BLI_addhead(&st->regiontypes, art);
956
957         BKE_spacetype_register(st);
958 }