Planar tracking support for motion tracking
[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_mask_types.h"
37 #include "DNA_movieclip_types.h"
38 #include "DNA_view3d_types.h"   /* for pivot point */
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_math.h"
45
46 #include "BKE_main.h"
47 #include "BKE_context.h"
48 #include "BKE_screen.h"
49 #include "BKE_movieclip.h"
50 #include "BKE_tracking.h"
51
52 #include "IMB_imbuf_types.h"
53
54 #include "ED_mask.h"
55 #include "ED_space_api.h"
56 #include "ED_screen.h"
57 #include "ED_clip.h"
58 #include "ED_transform.h"
59
60 #include "IMB_imbuf.h"
61
62 #include "BIF_gl.h"
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66
67 #include "UI_interface.h"
68 #include "UI_resources.h"
69 #include "UI_view2d.h"
70
71 #include "RNA_access.h"
72
73
74 #include "clip_intern.h"        // own include
75
76 static void init_preview_region(const bContext *C, ARegion *ar)
77 {
78         Scene *scene = CTX_data_scene(C);
79         ScrArea *sa = CTX_wm_area(C);
80         SpaceClip *sc = CTX_wm_space_clip(C);
81
82         ar->regiontype = RGN_TYPE_PREVIEW;
83         ar->alignment = RGN_ALIGN_TOP;
84         ar->flag |= RGN_FLAG_HIDDEN;
85
86         if (sc->view == SC_VIEW_DOPESHEET) {
87                 ar->v2d.tot.xmin = -10.0f;
88                 ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
89                 ar->v2d.tot.xmax = (float)(sa->winx);
90                 ar->v2d.tot.ymax = 0.0f;
91
92                 ar->v2d.cur = ar->v2d.tot;
93
94                 ar->v2d.min[0] = 0.0f;
95                 ar->v2d.min[1] = 0.0f;
96
97                 ar->v2d.max[0] = MAXFRAMEF;
98                 ar->v2d.max[1] = FLT_MAX;
99
100                 ar->v2d.minzoom = 0.01f;
101                 ar->v2d.maxzoom = 50;
102                 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
103                 ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
104                 ar->v2d.keepzoom = V2D_LOCKZOOM_Y;
105                 ar->v2d.keepofs = V2D_KEEPOFS_Y;
106                 ar->v2d.align = V2D_ALIGN_NO_POS_Y;
107                 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
108         }
109         else {
110                 ar->v2d.tot.xmin = 0.0f;
111                 ar->v2d.tot.ymin = -10.0f;
112                 ar->v2d.tot.xmax = (float)scene->r.efra;
113                 ar->v2d.tot.ymax = 10.0f;
114
115                 ar->v2d.cur = ar->v2d.tot;
116
117                 ar->v2d.min[0] = FLT_MIN;
118                 ar->v2d.min[1] = FLT_MIN;
119
120                 ar->v2d.max[0] = MAXFRAMEF;
121                 ar->v2d.max[1] = FLT_MAX;
122
123                 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
124                 ar->v2d.scroll |= (V2D_SCROLL_LEFT | V2D_SCROLL_SCALE_VERTICAL);
125
126                 ar->v2d.minzoom = 0.0f;
127                 ar->v2d.maxzoom = 0.0f;
128                 ar->v2d.keepzoom = 0;
129                 ar->v2d.keepofs = 0;
130                 ar->v2d.align = 0;
131                 ar->v2d.flag = 0;
132
133                 ar->v2d.keeptot = 0;
134         }
135 }
136
137 static void reinit_preview_region(const bContext *C, ARegion *ar)
138 {
139         SpaceClip *sc = CTX_wm_space_clip(C);
140
141         if (sc->view == SC_VIEW_DOPESHEET) {
142                 if ((ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL) == 0)
143                         init_preview_region(C, ar);
144         }
145         else {
146                 if (ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL)
147                         init_preview_region(C, ar);
148         }
149 }
150
151 static ARegion *ED_clip_has_preview_region(const bContext *C, ScrArea *sa)
152 {
153         ARegion *ar, *arnew;
154
155         ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
156         if (ar)
157                 return ar;
158
159         /* add subdiv level; after header */
160         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
161
162         /* is error! */
163         if (ar == NULL)
164                 return NULL;
165
166         arnew = MEM_callocN(sizeof(ARegion), "clip preview region");
167
168         BLI_insertlinkbefore(&sa->regionbase, ar, arnew);
169         init_preview_region(C, arnew);
170
171         return arnew;
172 }
173
174 static ARegion *ED_clip_has_channels_region(ScrArea *sa)
175 {
176         ARegion *ar, *arnew;
177
178         ar = BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
179         if (ar)
180                 return ar;
181
182         /* add subdiv level; after header */
183         ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
184
185         /* is error! */
186         if (ar == NULL)
187                 return NULL;
188
189         arnew = MEM_callocN(sizeof(ARegion), "clip channels region");
190
191         BLI_insertlinkbefore(&sa->regionbase, ar, arnew);
192         arnew->regiontype = RGN_TYPE_CHANNELS;
193         arnew->alignment = RGN_ALIGN_LEFT;
194
195         arnew->v2d.scroll = V2D_SCROLL_BOTTOM;
196         arnew->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
197
198         return arnew;
199 }
200
201 static void clip_scopes_tag_refresh(ScrArea *sa)
202 {
203         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
204         ARegion *ar;
205
206         if (sc->mode != SC_MODE_TRACKING)
207                 return;
208
209         /* only while proeprties are visible */
210         for (ar = sa->regionbase.first; ar; ar = ar->next) {
211                 if (ar->regiontype == RGN_TYPE_UI && ar->flag & RGN_FLAG_HIDDEN)
212                         return;
213         }
214
215         sc->scopes.ok = FALSE;
216 }
217
218 static void clip_stabilization_tag_refresh(ScrArea *sa)
219 {
220         SpaceClip *sc = (SpaceClip *) sa->spacedata.first;
221         MovieClip *clip = ED_space_clip(sc);
222
223         if (clip) {
224                 MovieTrackingStabilization *stab = &clip->tracking.stabilization;
225
226                 stab->ok = FALSE;
227         }
228 }
229
230 /* ******************** default callbacks for clip space ***************** */
231
232 static SpaceLink *clip_new(const bContext *C)
233 {
234         ARegion *ar;
235         SpaceClip *sc;
236
237         sc = MEM_callocN(sizeof(SpaceClip), "initclip");
238         sc->spacetype = SPACE_CLIP;
239         sc->flag = SC_SHOW_MARKER_PATTERN | SC_SHOW_TRACK_PATH | SC_MANUAL_CALIBRATION |
240                    SC_SHOW_GRAPH_TRACKS | SC_SHOW_GRAPH_FRAMES;
241         sc->zoom = 1.0f;
242         sc->path_length = 20;
243         sc->scopes.track_preview_height = 120;
244         sc->around = V3D_LOCAL;
245
246         /* header */
247         ar = MEM_callocN(sizeof(ARegion), "header for clip");
248
249         BLI_addtail(&sc->regionbase, ar);
250         ar->regiontype = RGN_TYPE_HEADER;
251         ar->alignment = RGN_ALIGN_BOTTOM;
252
253         /* tools view */
254         ar = MEM_callocN(sizeof(ARegion), "tools for clip");
255
256         BLI_addtail(&sc->regionbase, ar);
257         ar->regiontype = RGN_TYPE_TOOLS;
258         ar->alignment = RGN_ALIGN_LEFT;
259
260         /* tool properties */
261         ar = MEM_callocN(sizeof(ARegion), "tool properties for clip");
262
263         BLI_addtail(&sc->regionbase, ar);
264         ar->regiontype = RGN_TYPE_TOOL_PROPS;
265         ar->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
266
267         /* properties view */
268         ar = MEM_callocN(sizeof(ARegion), "properties for clip");
269
270         BLI_addtail(&sc->regionbase, ar);
271         ar->regiontype = RGN_TYPE_UI;
272         ar->alignment = RGN_ALIGN_RIGHT;
273
274         /* channels view */
275         ar = MEM_callocN(sizeof(ARegion), "channels for clip");
276
277         BLI_addtail(&sc->regionbase, ar);
278         ar->regiontype = RGN_TYPE_CHANNELS;
279         ar->alignment = RGN_ALIGN_LEFT;
280
281         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
282         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
283
284         /* preview view */
285         ar = MEM_callocN(sizeof(ARegion), "preview for clip");
286
287         BLI_addtail(&sc->regionbase, ar);
288         init_preview_region(C, ar);
289
290         /* main area */
291         ar = MEM_callocN(sizeof(ARegion), "main area for clip");
292
293         BLI_addtail(&sc->regionbase, ar);
294         ar->regiontype = RGN_TYPE_WINDOW;
295
296         return (SpaceLink *) sc;
297 }
298
299 /* not spacelink itself */
300 static void clip_free(SpaceLink *sl)
301 {
302         SpaceClip *sc = (SpaceClip*) sl;
303
304         sc->clip = NULL;
305
306         if (sc->scopes.track_preview)
307                 IMB_freeImBuf(sc->scopes.track_preview);
308
309         if (sc->scopes.track_search)
310                 IMB_freeImBuf(sc->scopes.track_search);
311
312         ED_space_clip_free_texture_buffer(sc);
313 }
314
315 /* spacetype; init callback */
316 static void clip_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
317 {
318         ListBase *lb = WM_dropboxmap_find("Clip", SPACE_CLIP, 0);
319
320         /* add drop boxes */
321         WM_event_add_dropbox_handler(&sa->handlers, lb);
322 }
323
324 static SpaceLink *clip_duplicate(SpaceLink *sl)
325 {
326         SpaceClip *scn = MEM_dupallocN(sl);
327
328         /* clear or remove stuff from old */
329         scn->scopes.track_search = NULL;
330         scn->scopes.track_preview = NULL;
331         scn->scopes.ok = FALSE;
332         scn->draw_context = NULL;
333
334         return (SpaceLink *)scn;
335 }
336
337 static void clip_listener(ScrArea *sa, wmNotifier *wmn)
338 {
339         /* context changes */
340         switch (wmn->category) {
341                 case NC_SCENE:
342                         switch (wmn->data) {
343                                 case ND_FRAME:
344                                         clip_scopes_tag_refresh(sa);
345                                         /* no break! */
346
347                                 case ND_FRAME_RANGE:
348                                         ED_area_tag_redraw(sa);
349                                         break;
350                         }
351                         break;
352                 case NC_MOVIECLIP:
353                         switch (wmn->data) {
354                                 case ND_DISPLAY:
355                                 case ND_SELECT:
356                                         clip_scopes_tag_refresh(sa);
357                                         ED_area_tag_redraw(sa);
358                                         break;
359                         }
360                         switch (wmn->action) {
361                                 case NA_REMOVED:
362                                 case NA_EDITED:
363                                 case NA_EVALUATED:
364                                         clip_stabilization_tag_refresh(sa);
365                                         /* no break! */
366
367                                 case NA_SELECTED:
368                                         clip_scopes_tag_refresh(sa);
369                                         ED_area_tag_redraw(sa);
370                                         break;
371                         }
372                         break;
373                 case NC_MASK:
374                         switch (wmn->data) {
375                                 case ND_SELECT:
376                                 case ND_DATA:
377                                 case ND_DRAW:
378                                         ED_area_tag_redraw(sa);
379                                         break;
380                         }
381                         switch (wmn->action) {
382                                 case NA_SELECTED:
383                                         clip_scopes_tag_refresh(sa);
384                                         ED_area_tag_redraw(sa);
385                                         break;
386                                 case NA_EDITED:
387                                         ED_area_tag_redraw(sa);
388                                         break;
389                         }
390                         break;
391                 case NC_GEOM:
392                         switch (wmn->data) {
393                                 case ND_SELECT:
394                                         clip_scopes_tag_refresh(sa);
395                                         ED_area_tag_redraw(sa);
396                                         break;
397                         }
398                         break;
399                 case NC_SCREEN:
400                          switch (wmn->data) {
401                                 case ND_ANIMPLAY:
402                                 case ND_GPENCIL:
403                                         ED_area_tag_redraw(sa);
404                                         break;
405                         }
406                         break;
407                 case NC_SPACE:
408                         if (wmn->data == ND_SPACE_CLIP) {
409                                 clip_scopes_tag_refresh(sa);
410                                 clip_stabilization_tag_refresh(sa);
411                                 ED_area_tag_redraw(sa);
412                         }
413                         break;
414         }
415 }
416
417 static void clip_operatortypes(void)
418 {
419         /* ** clip_ops.c ** */
420         WM_operatortype_append(CLIP_OT_open);
421         WM_operatortype_append(CLIP_OT_reload);
422         WM_operatortype_append(CLIP_OT_view_pan);
423         WM_operatortype_append(CLIP_OT_view_zoom);
424         WM_operatortype_append(CLIP_OT_view_zoom_in);
425         WM_operatortype_append(CLIP_OT_view_zoom_out);
426         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
427         WM_operatortype_append(CLIP_OT_view_all);
428         WM_operatortype_append(CLIP_OT_view_selected);
429         WM_operatortype_append(CLIP_OT_change_frame);
430         WM_operatortype_append(CLIP_OT_rebuild_proxy);
431         WM_operatortype_append(CLIP_OT_mode_set);
432
433         /* ** clip_toolbar.c ** */
434         WM_operatortype_append(CLIP_OT_tools);
435         WM_operatortype_append(CLIP_OT_properties);
436
437         /* ** tracking_ops.c ** */
438
439         /* navigation */
440         WM_operatortype_append(CLIP_OT_frame_jump);
441
442         /* foorage */
443         WM_operatortype_append(CLIP_OT_set_center_principal);
444
445         /* selection */
446         WM_operatortype_append(CLIP_OT_select);
447         WM_operatortype_append(CLIP_OT_select_all);
448         WM_operatortype_append(CLIP_OT_select_border);
449         WM_operatortype_append(CLIP_OT_select_lasso);
450         WM_operatortype_append(CLIP_OT_select_circle);
451         WM_operatortype_append(CLIP_OT_select_grouped);
452
453         /* markers */
454         WM_operatortype_append(CLIP_OT_add_marker);
455         WM_operatortype_append(CLIP_OT_slide_marker);
456         WM_operatortype_append(CLIP_OT_delete_track);
457         WM_operatortype_append(CLIP_OT_delete_marker);
458
459         /* track */
460         WM_operatortype_append(CLIP_OT_track_markers);
461
462         /* solving */
463         WM_operatortype_append(CLIP_OT_solve_camera);
464         WM_operatortype_append(CLIP_OT_clear_solution);
465
466         WM_operatortype_append(CLIP_OT_disable_markers);
467         WM_operatortype_append(CLIP_OT_hide_tracks);
468         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
469         WM_operatortype_append(CLIP_OT_lock_tracks);
470
471         /* orientation */
472         WM_operatortype_append(CLIP_OT_set_origin);
473         WM_operatortype_append(CLIP_OT_set_plane);
474         WM_operatortype_append(CLIP_OT_set_axis);
475         WM_operatortype_append(CLIP_OT_set_scale);
476         WM_operatortype_append(CLIP_OT_set_solution_scale);
477
478         /* detect */
479         WM_operatortype_append(CLIP_OT_detect_features);
480
481         /* stabilization */
482         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
483         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
484         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
485         WM_operatortype_append(CLIP_OT_stabilize_2d_set_rotation);
486
487         /* clean-up */
488         WM_operatortype_append(CLIP_OT_clear_track_path);
489         WM_operatortype_append(CLIP_OT_join_tracks);
490         WM_operatortype_append(CLIP_OT_track_copy_color);
491
492         WM_operatortype_append(CLIP_OT_clean_tracks);
493
494         /* object tracking */
495         WM_operatortype_append(CLIP_OT_tracking_object_new);
496         WM_operatortype_append(CLIP_OT_tracking_object_remove);
497
498         /* clipboard */
499         WM_operatortype_append(CLIP_OT_copy_tracks);
500         WM_operatortype_append(CLIP_OT_paste_tracks);
501
502         /* ** clip_graph_ops.c  ** */
503
504         /* graph editing */
505
506         /* selection */
507         WM_operatortype_append(CLIP_OT_graph_select);
508         WM_operatortype_append(CLIP_OT_graph_select_border);
509         WM_operatortype_append(CLIP_OT_graph_select_all_markers);
510
511         WM_operatortype_append(CLIP_OT_graph_delete_curve);
512         WM_operatortype_append(CLIP_OT_graph_delete_knot);
513         WM_operatortype_append(CLIP_OT_graph_view_all);
514         WM_operatortype_append(CLIP_OT_graph_center_current_frame);
515
516         WM_operatortype_append(CLIP_OT_graph_disable_markers);
517
518         /* ** clip_dopesheet_ops.c  ** */
519
520         WM_operatortype_append(CLIP_OT_dopesheet_select_channel);
521 }
522
523 static void clip_keymap(struct wmKeyConfig *keyconf)
524 {
525         wmKeyMap *keymap;
526         wmKeyMapItem *kmi;
527
528         /* ******** Global hotkeys avalaible for all regions ******** */
529
530         keymap = WM_keymap_find(keyconf, "Clip", SPACE_CLIP, 0);
531
532         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
533
534         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
535         WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
536
537         /* 2d tracking */
538         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
539         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
540         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
541         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
542         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
543         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
544         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
545         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
546         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
547         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
548         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
549         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
550
551         /* mode */
552         kmi = WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
553         RNA_enum_set(kmi->ptr, "mode", SC_MODE_RECONSTRUCTION);
554         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
555
556         kmi = WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, KM_CTRL, 0);
557         RNA_enum_set(kmi->ptr, "mode", SC_MODE_DISTORTION);
558         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
559
560         WM_keymap_add_item(keymap, "CLIP_OT_solve_camera", SKEY, KM_PRESS, KM_SHIFT, 0);
561
562         /* ******** Hotkeys avalaible for main region only ******** */
563
564         keymap = WM_keymap_find(keyconf, "Clip Editor", SPACE_CLIP, 0);
565 //      keymap->poll = ED_space_clip_tracking_poll;
566         /* ** View/navigation ** */
567
568         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
569         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
570         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
571
572         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
573         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
574         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
575         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
576         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
577         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
578
579         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
580         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
581         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
582         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
583         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
584         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
585         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
586
587         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
588
589         kmi = WM_keymap_add_item(keymap, "CLIP_OT_view_all", FKEY, KM_PRESS, 0, 0);
590         RNA_boolean_set(kmi->ptr, "fit_view", TRUE);
591
592         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
593
594         /* jump to special frame */
595         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
596         RNA_enum_set(kmi->ptr, "position", 0);
597
598         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
599         RNA_enum_set(kmi->ptr, "position", 1);
600
601         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
602         RNA_enum_set(kmi->ptr, "position", 2);
603
604         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
605         RNA_enum_set(kmi->ptr, "position", 3);
606
607         /* "timeline" */
608         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
609
610         /* selection */
611         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
612         RNA_boolean_set(kmi->ptr, "extend", FALSE);
613         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
614         RNA_boolean_set(kmi->ptr, "extend", TRUE);
615         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
616         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
617         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
618         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
619         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
620         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
621         WM_keymap_add_menu(keymap, "CLIP_MT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
622
623         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
624         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
625         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
626         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
627
628         /* marker */
629         WM_keymap_add_item(keymap, "CLIP_OT_add_marker_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
630
631         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
632         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", XKEY, KM_PRESS, KM_SHIFT, 0);
633
634         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
635
636         kmi = WM_keymap_add_item(keymap, "CLIP_OT_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
637         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
638
639         /* tracks */
640         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
641         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
642
643         kmi = WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_CTRL, 0);
644         RNA_enum_set(kmi->ptr, "action", 0);    /* lock */
645
646         kmi = WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_ALT, 0);
647         RNA_enum_set(kmi->ptr, "action", 1);    /* unlock */
648
649         kmi = WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
650         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
651
652         kmi = WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
653         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
654
655         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
656
657         /* clean-up */
658         WM_keymap_add_item(keymap, "CLIP_OT_join_tracks", JKEY, KM_PRESS, KM_CTRL, 0);
659
660         /* menus */
661         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
662
663         /* display */
664         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
665         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
666
667         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", DKEY, KM_PRESS, KM_ALT, 0);
668         RNA_string_set(kmi->ptr, "data_path", "space_data.show_disabled");
669
670         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_ALT, 0);
671         RNA_string_set(kmi->ptr, "data_path", "space_data.show_marker_search");
672
673         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0);
674         RNA_string_set(kmi->ptr, "data_path", "space_data.use_mute_footage");
675
676         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
677
678         /* clean-up */
679         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
680         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
681         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
682         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
683         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
684         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
685         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
686         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
687         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
688
689         /* ******** Hotkeys avalaible for preview region only ******** */
690
691         keymap = WM_keymap_find(keyconf, "Clip Graph Editor", SPACE_CLIP, 0);
692
693         /* "timeline" */
694         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", ACTIONMOUSE, KM_PRESS, 0, 0);
695
696         /* selection */
697         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, 0, 0);
698         RNA_boolean_set(kmi->ptr, "extend", FALSE);
699         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
700         RNA_boolean_set(kmi->ptr, "extend", TRUE);
701
702         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select_all_markers", AKEY, KM_PRESS, 0, 0);
703         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
704         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select_all_markers", IKEY, KM_PRESS, KM_CTRL, 0);
705         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
706
707         WM_keymap_add_item(keymap, "CLIP_OT_graph_select_border", BKEY, KM_PRESS, 0, 0);
708
709         /* delete */
710         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", DELKEY, KM_PRESS, 0, 0);
711         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", XKEY, KM_PRESS, 0, 0);
712
713         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", DELKEY, KM_PRESS, KM_SHIFT, 0);
714         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", XKEY, KM_PRESS, KM_SHIFT, 0);
715
716         /* view */
717         WM_keymap_add_item(keymap, "CLIP_OT_graph_view_all", HOMEKEY, KM_PRESS, 0, 0);
718         WM_keymap_add_item(keymap, "CLIP_OT_graph_center_current_frame", PADPERIOD, KM_PRESS, 0, 0);
719
720         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
721         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_time_cursor");
722
723         /* clean-up */
724         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
725         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
726         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
727         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
728         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
729         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
730         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
731         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
732         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
733
734         /* tracks */
735         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
736         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
737
738         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
739
740         /* ******** Hotkeys avalaible for channels region only ******** */
741
742         keymap = WM_keymap_find(keyconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
743
744         kmi = WM_keymap_add_item(keymap, "CLIP_OT_dopesheet_select_channel", ACTIONMOUSE, KM_PRESS, 0, 0);
745         RNA_boolean_set(kmi->ptr, "extend", TRUE);      /* toggle */
746 }
747
748 const char *clip_context_dir[] = {"edit_movieclip", "edit_mask", NULL};
749
750 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
751 {
752         SpaceClip *sc = CTX_wm_space_clip(C);
753
754         if (CTX_data_dir(member)) {
755                 CTX_data_dir_set(result, clip_context_dir);
756
757                 return TRUE;
758         }
759         else if (CTX_data_equals(member, "edit_movieclip")) {
760                 if (sc->clip)
761                         CTX_data_id_pointer_set(result, &sc->clip->id);
762                 return TRUE;
763         }
764         else if (CTX_data_equals(member, "edit_mask")) {
765                 if (sc->mask)
766                         CTX_data_id_pointer_set(result, &sc->mask->id);
767                 return TRUE;
768         }
769
770         return FALSE;
771 }
772
773 /* dropboxes */
774 static int clip_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
775 {
776         if (drag->type == WM_DRAG_PATH)
777                 if (ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
778                         return TRUE;
779
780         return FALSE;
781 }
782
783 static void clip_drop_copy(wmDrag *drag, wmDropBox *drop)
784 {
785         /* copy drag path to properties */
786         RNA_string_set(drop->ptr, "filepath", drag->path);
787 }
788
789 /* area+region dropbox definition */
790 static void clip_dropboxes(void)
791 {
792         ListBase *lb = WM_dropboxmap_find("Clip", SPACE_CLIP, 0);
793
794         WM_dropbox_add(lb, "CLIP_OT_open", clip_drop_poll, clip_drop_copy);
795 }
796
797 static void clip_refresh(const bContext *C, ScrArea *sa)
798 {
799         wmWindowManager *wm = CTX_wm_manager(C);
800         wmWindow *window = CTX_wm_window(C);
801         Scene *scene = CTX_data_scene(C);
802         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
803         ARegion *ar_main = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
804         ARegion *ar_tools = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
805         ARegion *ar_tool_props = BKE_area_find_region_type(sa, RGN_TYPE_TOOL_PROPS);
806         ARegion *ar_preview = ED_clip_has_preview_region(C, sa);
807         ARegion *ar_properties = ED_clip_has_properties_region(sa);
808         ARegion *ar_channels = ED_clip_has_channels_region(sa);
809         int main_visible = FALSE, preview_visible = FALSE, tools_visible = FALSE;
810         int tool_props_visible = FALSE, properties_visible = FALSE, channels_visible = FALSE;
811         int view_changed = FALSE;
812
813         switch (sc->view) {
814                 case SC_VIEW_CLIP:
815                         main_visible = TRUE;
816                         preview_visible = FALSE;
817                         tools_visible = TRUE;
818                         tool_props_visible = TRUE;
819                         properties_visible = TRUE;
820                         channels_visible = FALSE;
821                         break;
822                 case SC_VIEW_GRAPH:
823                         main_visible = FALSE;
824                         preview_visible = TRUE;
825                         tools_visible = FALSE;
826                         tool_props_visible = FALSE;
827                         properties_visible = FALSE;
828                         channels_visible = FALSE;
829
830                         reinit_preview_region(C, ar_preview);
831                         break;
832                 case SC_VIEW_DOPESHEET:
833                         main_visible = FALSE;
834                         preview_visible = TRUE;
835                         tools_visible = FALSE;
836                         tool_props_visible = FALSE;
837                         properties_visible = FALSE;
838                         channels_visible = TRUE;
839
840                         reinit_preview_region(C, ar_preview);
841                         break;
842         }
843
844         if (main_visible) {
845                 if (ar_main && (ar_main->flag & RGN_FLAG_HIDDEN)) {
846                         ar_main->flag &= ~RGN_FLAG_HIDDEN;
847                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
848                         view_changed = TRUE;
849                 }
850
851                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
852                         ar_main->alignment = RGN_ALIGN_NONE;
853                         view_changed = TRUE;
854                 }
855         }
856         else {
857                 if (ar_main && !(ar_main->flag & RGN_FLAG_HIDDEN)) {
858                         ar_main->flag |= RGN_FLAG_HIDDEN;
859                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
860                         WM_event_remove_handlers((bContext *)C, &ar_main->handlers);
861                         view_changed = TRUE;
862                 }
863                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
864                         ar_main->alignment = RGN_ALIGN_NONE;
865                         view_changed = TRUE;
866                 }
867         }
868
869         if (properties_visible) {
870                 if (ar_properties && (ar_properties->flag & RGN_FLAG_HIDDEN)) {
871                         ar_properties->flag &= ~RGN_FLAG_HIDDEN;
872                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
873                         view_changed = TRUE;
874                 }
875                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_RIGHT) {
876                         ar_properties->alignment = RGN_ALIGN_RIGHT;
877                         view_changed = TRUE;
878                 }
879         }
880         else {
881                 if (ar_properties && !(ar_properties->flag & RGN_FLAG_HIDDEN)) {
882                         ar_properties->flag |= RGN_FLAG_HIDDEN;
883                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
884                         WM_event_remove_handlers((bContext *)C, &ar_properties->handlers);
885                         view_changed = TRUE;
886                 }
887                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_NONE) {
888                         ar_properties->alignment = RGN_ALIGN_NONE;
889                         view_changed = TRUE;
890                 }
891         }
892
893         if (tools_visible) {
894                 if (ar_tools && (ar_tools->flag & RGN_FLAG_HIDDEN)) {
895                         ar_tools->flag &= ~RGN_FLAG_HIDDEN;
896                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
897                         view_changed = TRUE;
898                 }
899                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_LEFT) {
900                         ar_tools->alignment = RGN_ALIGN_LEFT;
901                         view_changed = TRUE;
902                 }
903         }
904         else {
905                 if (ar_tools && !(ar_tools->flag & RGN_FLAG_HIDDEN)) {
906                         ar_tools->flag |= RGN_FLAG_HIDDEN;
907                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
908                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
909                         view_changed = TRUE;
910                 }
911                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_NONE) {
912                         ar_tools->alignment = RGN_ALIGN_NONE;
913                         view_changed = TRUE;
914                 }
915         }
916
917         if (tool_props_visible) {
918                 if (ar_tool_props && (ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
919                         ar_tool_props->flag &= ~RGN_FLAG_HIDDEN;
920                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
921                         view_changed = TRUE;
922                 }
923                 if (ar_tool_props && (ar_tool_props->alignment != (RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV))) {
924                         ar_tool_props->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
925                         view_changed = TRUE;
926                 }
927         }
928         else {
929                 if (ar_tool_props && !(ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
930                         ar_tool_props->flag |= RGN_FLAG_HIDDEN;
931                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
932                         WM_event_remove_handlers((bContext *)C, &ar_tool_props->handlers);
933                         view_changed = TRUE;
934                 }
935                 if (ar_tool_props && ar_tool_props->alignment != RGN_ALIGN_NONE) {
936                         ar_tool_props->alignment = RGN_ALIGN_NONE;
937                         view_changed = TRUE;
938                 }
939         }
940
941         if (preview_visible) {
942                 if (ar_preview && (ar_preview->flag & RGN_FLAG_HIDDEN)) {
943                         ar_preview->flag &= ~RGN_FLAG_HIDDEN;
944                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
945                         ar_preview->v2d.cur = ar_preview->v2d.tot;
946                         view_changed = TRUE;
947                 }
948                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
949                         ar_preview->alignment = RGN_ALIGN_NONE;
950                         view_changed = TRUE;
951                 }
952         }
953         else {
954                 if (ar_preview && !(ar_preview->flag & RGN_FLAG_HIDDEN)) {
955                         ar_preview->flag |= RGN_FLAG_HIDDEN;
956                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
957                         WM_event_remove_handlers((bContext *)C, &ar_preview->handlers);
958                         view_changed = TRUE;
959                 }
960                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
961                         ar_preview->alignment = RGN_ALIGN_NONE;
962                         view_changed = TRUE;
963                 }
964         }
965
966         if (channels_visible) {
967                 if (ar_channels && (ar_channels->flag & RGN_FLAG_HIDDEN)) {
968                         ar_channels->flag &= ~RGN_FLAG_HIDDEN;
969                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
970                         view_changed = TRUE;
971                 }
972                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_LEFT) {
973                         ar_channels->alignment = RGN_ALIGN_LEFT;
974                         view_changed = TRUE;
975                 }
976         }
977         else {
978                 if (ar_channels && !(ar_channels->flag & RGN_FLAG_HIDDEN)) {
979                         ar_channels->flag |= RGN_FLAG_HIDDEN;
980                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
981                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
982                         view_changed = TRUE;
983                 }
984                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_NONE) {
985                         ar_channels->alignment = RGN_ALIGN_NONE;
986                         view_changed = TRUE;
987                 }
988         }
989
990         if (view_changed) {
991                 ED_area_initialize(wm, window, sa);
992                 ED_area_tag_redraw(sa);
993         }
994
995         BKE_movieclip_user_set_frame(&sc->user, scene->r.cfra);
996 }
997
998 /********************* main region ********************/
999
1000 /* sets up the fields of the View2D from zoom and offset */
1001 static void movieclip_main_area_set_view2d(SpaceClip *sc, ARegion *ar)
1002 {
1003         MovieClip *clip = ED_space_clip(sc);
1004         float x1, y1, w, h;
1005         int width, height, winx, winy;
1006
1007         ED_space_clip_size(sc, &width, &height);
1008
1009         w = width;
1010         h = height;
1011
1012         if (clip)
1013                 h *= clip->aspy / clip->aspx / clip->tracking.camera.pixel_aspect;
1014
1015         winx = ar->winrct.xmax - ar->winrct.xmin + 1;
1016         winy = ar->winrct.ymax - ar->winrct.ymin + 1;
1017
1018         ar->v2d.tot.xmin = 0;
1019         ar->v2d.tot.ymin = 0;
1020         ar->v2d.tot.xmax = w;
1021         ar->v2d.tot.ymax = h;
1022
1023         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
1024         ar->v2d.mask.xmax = winx;
1025         ar->v2d.mask.ymax = winy;
1026
1027         /* which part of the image space do we see? */
1028         x1 = ar->winrct.xmin + (winx - sc->zoom * w) / 2.0f;
1029         y1 = ar->winrct.ymin + (winy - sc->zoom * h) / 2.0f;
1030
1031         x1 -= sc->zoom * sc->xof;
1032         y1 -= sc->zoom * sc->yof;
1033
1034         /* relative display right */
1035         ar->v2d.cur.xmin = (ar->winrct.xmin - (float)x1) / sc->zoom;
1036         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sc->zoom);
1037
1038         /* relative display left */
1039         ar->v2d.cur.ymin = (ar->winrct.ymin - (float)y1) / sc->zoom;
1040         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sc->zoom);
1041
1042         /* normalize 0.0..1.0 */
1043         ar->v2d.cur.xmin /= w;
1044         ar->v2d.cur.xmax /= w;
1045         ar->v2d.cur.ymin /= h;
1046         ar->v2d.cur.ymax /= h;
1047 }
1048
1049 /* add handlers, stuff you only do once or on area/region changes */
1050 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
1051 {
1052         wmKeyMap *keymap;
1053
1054         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
1055
1056         /* own keymap */
1057         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
1058         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1059
1060         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1061         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1062
1063         keymap = WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
1064         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1065 }
1066
1067 static void clip_main_area_draw(const bContext *C, ARegion *ar)
1068 {
1069         /* draw entirely, view changes should be handled here */
1070         SpaceClip *sc = CTX_wm_space_clip(C);
1071         Scene *scene = CTX_data_scene(C);
1072         MovieClip *clip = ED_space_clip(sc);
1073
1074         /* if tracking is in progress, we should synchronize framenr from clipuser
1075          * so latest tracked frame would be shown */
1076         if (clip && clip->tracking_context)
1077                 BKE_tracking_sync_user(&sc->user, clip->tracking_context);
1078
1079         if (sc->flag & SC_LOCK_SELECTION) {
1080                 ImBuf *tmpibuf = NULL;
1081
1082                 if (clip && clip->tracking.stabilization.flag & TRACKING_2D_STABILIZATION) {
1083                         tmpibuf = ED_space_clip_get_stable_buffer(sc, NULL, NULL, NULL);
1084                 }
1085
1086                 if (ED_clip_view_selection(sc, ar, 0)) {
1087                         sc->xof += sc->xlockof;
1088                         sc->yof += sc->ylockof;
1089                 }
1090
1091                 if (tmpibuf)
1092                         IMB_freeImBuf(tmpibuf);
1093         }
1094
1095         /* clear and setup matrix */
1096         UI_ThemeClearColor(TH_BACK);
1097         glClear(GL_COLOR_BUFFER_BIT);
1098
1099         /* data... */
1100         movieclip_main_area_set_view2d(sc, ar);
1101
1102         clip_draw_main(sc, ar, scene);
1103
1104         /* Grease Pencil */
1105         clip_draw_grease_pencil((bContext *)C, 1);
1106
1107         if (sc->mode == SC_MODE_MASKEDIT) {
1108                 int x, y;
1109                 int width, height;
1110                 float zoomx, zoomy, aspx, aspy;
1111
1112                 /* frame image */
1113                 float maxdim;
1114                 float xofs, yofs;
1115
1116                 /* find window pixel coordinates of origin */
1117                 UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x, &y);
1118
1119                 ED_space_clip_size(sc, &width, &height);
1120                 ED_space_clip_zoom(sc, ar, &zoomx, &zoomy);
1121                 ED_space_clip_aspect(sc, &aspx, &aspy);
1122
1123                 /* frame the image */
1124                 maxdim = maxf(width, height);
1125                 if (width == height) {
1126                         xofs = yofs = 0;
1127                 }
1128                 else if (width < height) {
1129                         xofs = ((height - width) / -2.0f) * zoomx;
1130                         yofs = 0.0f;
1131                 }
1132                 else { /* (width > height) */
1133                         xofs = 0.0f;
1134                         yofs = ((width - height) / -2.0f) * zoomy;
1135                 }
1136
1137                 /* apply transformation so mask editing tools will assume drawing from the origin in normalized space */
1138                 glPushMatrix();
1139                 glTranslatef(x + xofs, y + yofs, 0);
1140                 glScalef(maxdim * zoomx, maxdim * zoomy, 0);
1141                 glMultMatrixf(sc->stabmat);
1142
1143                 ED_mask_draw((bContext *)C, sc->mask_draw_flag, sc->mask_draw_type);
1144
1145                 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
1146
1147                 glPopMatrix();
1148         }
1149
1150         /* reset view matrix */
1151         UI_view2d_view_restore(C);
1152
1153         /* draw Grease Pencil - screen space only */
1154         clip_draw_grease_pencil((bContext *)C, 0);
1155 }
1156
1157 static void clip_main_area_listener(ARegion *ar, wmNotifier *wmn)
1158 {
1159         /* context changes */
1160         switch (wmn->category) {
1161                 case NC_SCREEN:
1162                         if (wmn->data == ND_GPENCIL)
1163                                 ED_region_tag_redraw(ar);
1164                 break;
1165         }
1166 }
1167
1168 /****************** preview region ******************/
1169
1170 static void clip_preview_area_init(wmWindowManager *wm, ARegion *ar)
1171 {
1172         wmKeyMap *keymap;
1173
1174         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
1175
1176         /* own keymap */
1177         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1178         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1179
1180         keymap = WM_keymap_find(wm->defaultconf, "Clip Graph Editor", SPACE_CLIP, 0);
1181         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1182 }
1183
1184 static void graph_area_draw(const bContext *C, ARegion *ar)
1185 {
1186         View2D *v2d = &ar->v2d;
1187         View2DScrollers *scrollers;
1188         SpaceClip *sc = CTX_wm_space_clip(C);
1189         Scene *scene = CTX_data_scene(C);
1190         short unitx, unity;
1191
1192         if (sc->flag & SC_LOCK_TIMECURSOR)
1193                 ED_clip_graph_center_current_frame(scene, ar);
1194
1195         /* clear and setup matrix */
1196         UI_ThemeClearColor(TH_BACK);
1197         glClear(GL_COLOR_BUFFER_BIT);
1198
1199         UI_view2d_view_ortho(v2d);
1200
1201         /* data... */
1202         clip_draw_graph(sc, ar, scene);
1203
1204         /* reset view matrix */
1205         UI_view2d_view_restore(C);
1206
1207         /* scrollers */
1208         unitx = (sc->flag & SC_SHOW_SECONDS)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1209         unity = V2D_UNIT_VALUES;
1210         scrollers = UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
1211         UI_view2d_scrollers_draw(C, v2d, scrollers);
1212         UI_view2d_scrollers_free(scrollers);
1213 }
1214
1215 static void dopesheet_area_draw(const bContext *C, ARegion *ar)
1216 {
1217         Scene *scene = CTX_data_scene(C);
1218         SpaceClip *sc = CTX_wm_space_clip(C);
1219         MovieClip *clip = ED_space_clip(sc);
1220         View2D *v2d = &ar->v2d;
1221         View2DGrid *grid;
1222         View2DScrollers *scrollers;
1223         short unit = 0;
1224
1225         if (clip)
1226                 BKE_tracking_dopesheet_update(&clip->tracking, sc->dope_sort, sc->dope_flag & SC_DOPE_SORT_INVERSE);
1227
1228         /* clear and setup matrix */
1229         UI_ThemeClearColor(TH_BACK);
1230         glClear(GL_COLOR_BUFFER_BIT);
1231
1232         UI_view2d_view_ortho(v2d);
1233
1234         /* time grid */
1235         unit = (sc->flag & SC_SHOW_SECONDS)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1236         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP,
1237                                    V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
1238         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
1239         UI_view2d_grid_free(grid);
1240
1241         /* data... */
1242         clip_draw_dopesheet_main(sc, ar, scene);
1243
1244         /* reset view matrix */
1245         UI_view2d_view_restore(C);
1246
1247         /* scrollers */
1248         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1249         UI_view2d_scrollers_draw(C, v2d, scrollers);
1250         UI_view2d_scrollers_free(scrollers);
1251 }
1252
1253 static void clip_preview_area_draw(const bContext *C, ARegion *ar)
1254 {
1255         SpaceClip *sc = CTX_wm_space_clip(C);
1256
1257         if (sc->view == SC_VIEW_GRAPH)
1258                 graph_area_draw(C, ar);
1259         else if (sc->view == SC_VIEW_DOPESHEET)
1260                 dopesheet_area_draw(C, ar);
1261 }
1262
1263 static void clip_preview_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1264 {
1265 }
1266
1267 /****************** channels region ******************/
1268
1269 static void clip_channels_area_init(wmWindowManager *wm, ARegion *ar)
1270 {
1271         wmKeyMap *keymap;
1272
1273         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
1274
1275         keymap = WM_keymap_find(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
1276         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1277 }
1278
1279 static void clip_channels_area_draw(const bContext *C, ARegion *ar)
1280 {
1281         SpaceClip *sc = CTX_wm_space_clip(C);
1282         MovieClip *clip = ED_space_clip(sc);
1283         View2D *v2d = &ar->v2d;
1284         View2DScrollers *scrollers;
1285
1286         if (clip)
1287                 BKE_tracking_dopesheet_update(&clip->tracking, sc->dope_sort, sc->dope_flag & SC_DOPE_SORT_INVERSE);
1288
1289         /* clear and setup matrix */
1290         UI_ThemeClearColor(TH_BACK);
1291         glClear(GL_COLOR_BUFFER_BIT);
1292
1293         UI_view2d_view_ortho(v2d);
1294
1295         /* data... */
1296         clip_draw_dopesheet_channels(C, ar);
1297
1298         /* reset view matrix */
1299         UI_view2d_view_restore(C);
1300
1301         /* scrollers */
1302         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1303         UI_view2d_scrollers_draw(C, v2d, scrollers);
1304         UI_view2d_scrollers_free(scrollers);
1305 }
1306
1307 static void clip_channels_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1308 {
1309 }
1310
1311 /****************** header region ******************/
1312
1313 /* add handlers, stuff you only do once or on area/region changes */
1314 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
1315 {
1316         ED_region_header_init(ar);
1317 }
1318
1319 static void clip_header_area_draw(const bContext *C, ARegion *ar)
1320 {
1321         ED_region_header(C, ar);
1322 }
1323
1324 static void clip_header_area_listener(ARegion *ar, wmNotifier *wmn)
1325 {
1326         /* context changes */
1327         switch (wmn->category) {
1328                 case NC_SCENE:
1329                         switch (wmn->data) {
1330                                 /* for proportional editmode only */
1331                                 case ND_TOOLSETTINGS:
1332                                         /* TODO - should do this when in mask mode only but no datas available */
1333                                         // if (sc->mode == SC_MODE_MASKEDIT)
1334                                         {
1335                                                 ED_region_tag_redraw(ar);
1336                                         }
1337                                         break;
1338                         }
1339                         break;
1340         }
1341 }
1342
1343
1344 /****************** tools region ******************/
1345
1346 /* add handlers, stuff you only do once or on area/region changes */
1347 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
1348 {
1349         ED_region_panels_init(wm, ar);
1350 }
1351
1352 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
1353 {
1354         ED_region_panels(C, ar, 1, NULL, -1);
1355 }
1356
1357 /****************** tool properties region ******************/
1358
1359 static void clip_props_area_listener(ARegion *ar, wmNotifier *wmn)
1360 {
1361         /* context changes */
1362         switch (wmn->category) {
1363                 case NC_WM:
1364                         if (wmn->data == ND_HISTORY)
1365                                 ED_region_tag_redraw(ar);
1366                         break;
1367                 case NC_SCENE:
1368                         if (wmn->data == ND_MODE)
1369                                 ED_region_tag_redraw(ar);
1370                         break;
1371                 case NC_SPACE:
1372                         if (wmn->data == ND_SPACE_CLIP)
1373                                 ED_region_tag_redraw(ar);
1374                         break;
1375                 case NC_SCREEN:
1376                         if (wmn->data == ND_GPENCIL)
1377                                 ED_region_tag_redraw(ar);
1378                         break;
1379         }
1380 }
1381
1382 /****************** properties region ******************/
1383
1384 /* add handlers, stuff you only do once or on area/region changes */
1385 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
1386 {
1387         wmKeyMap *keymap;
1388
1389         ED_region_panels_init(wm, ar);
1390
1391         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1392         WM_event_add_keymap_handler(&ar->handlers, keymap);
1393 }
1394
1395 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
1396 {
1397         SpaceClip *sc = CTX_wm_space_clip(C);
1398
1399         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
1400
1401         ED_region_panels(C, ar, 1, NULL, -1);
1402 }
1403
1404 static void clip_properties_area_listener(ARegion *ar, wmNotifier *wmn)
1405 {
1406         /* context changes */
1407         switch (wmn->category) {
1408                 case NC_SCREEN:
1409                         if (wmn->data == ND_GPENCIL)
1410                                 ED_region_tag_redraw(ar);
1411                         break;
1412                 case NC_BRUSH:
1413                         if (wmn->action == NA_EDITED)
1414                                 ED_region_tag_redraw(ar);
1415                         break;
1416         }
1417 }
1418
1419 /********************* registration ********************/
1420
1421 /* only called once, from space/spacetypes.c */
1422 void ED_spacetype_clip(void)
1423 {
1424         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype clip");
1425         ARegionType *art;
1426
1427         st->spaceid = SPACE_CLIP;
1428         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
1429
1430         st->new = clip_new;
1431         st->free = clip_free;
1432         st->init = clip_init;
1433         st->duplicate = clip_duplicate;
1434         st->operatortypes = clip_operatortypes;
1435         st->keymap = clip_keymap;
1436         st->listener = clip_listener;
1437         st->context = clip_context;
1438         st->dropboxes = clip_dropboxes;
1439         st->refresh = clip_refresh;
1440
1441         /* regions: main window */
1442         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1443         art->regionid = RGN_TYPE_WINDOW;
1444         art->init = clip_main_area_init;
1445         art->draw = clip_main_area_draw;
1446         art->listener = clip_main_area_listener;
1447         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_GPENCIL;
1448
1449         BLI_addhead(&st->regiontypes, art);
1450
1451         /* preview */
1452         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region preview");
1453         art->regionid = RGN_TYPE_PREVIEW;
1454         art->prefsizey = 240;
1455         art->init = clip_preview_area_init;
1456         art->draw = clip_preview_area_draw;
1457         art->listener = clip_preview_area_listener;
1458         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
1459
1460         BLI_addhead(&st->regiontypes, art);
1461
1462         /* regions: properties */
1463         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
1464         art->regionid = RGN_TYPE_UI;
1465         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1466         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1467         art->init = clip_properties_area_init;
1468         art->draw = clip_properties_area_draw;
1469         art->listener = clip_properties_area_listener;
1470         BLI_addhead(&st->regiontypes, art);
1471         ED_clip_buttons_register(art);
1472
1473         /* regions: tools */
1474         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
1475         art->regionid = RGN_TYPE_TOOLS;
1476         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1477         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1478         art->listener = clip_props_area_listener;
1479         art->init = clip_tools_area_init;
1480         art->draw = clip_tools_area_draw;
1481
1482         BLI_addhead(&st->regiontypes, art);
1483
1484         /* tool properties */
1485         art = MEM_callocN(sizeof(ARegionType), "spacetype clip tool properties region");
1486         art->regionid = RGN_TYPE_TOOL_PROPS;
1487         art->prefsizex = 0;
1488         art->prefsizey = 120;
1489         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1490         art->listener = clip_props_area_listener;
1491         art->init = clip_tools_area_init;
1492         art->draw = clip_tools_area_draw;
1493         ED_clip_tool_props_register(art);
1494
1495         BLI_addhead(&st->regiontypes, art);
1496
1497         /* regions: header */
1498         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1499         art->regionid = RGN_TYPE_HEADER;
1500         art->prefsizey = HEADERY;
1501         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
1502
1503         art->init = clip_header_area_init;
1504         art->draw = clip_header_area_draw;
1505         art->listener = clip_header_area_listener;
1506
1507         BLI_addhead(&st->regiontypes, art);
1508
1509         BKE_spacetype_register(st);
1510
1511         /* channels */
1512         art = MEM_callocN(sizeof(ARegionType), "spacetype clip channels region");
1513         art->regionid = RGN_TYPE_CHANNELS;
1514         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1515         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1516         art->listener = clip_channels_area_listener;
1517         art->init = clip_channels_area_init;
1518         art->draw = clip_channels_area_draw;
1519
1520         BLI_addhead(&st->regiontypes, art);
1521 }