Camera tracking integration
[blender.git] / source / blender / editors / space_clip / space_clip.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2011 Blender Foundation.
21  * All rights reserved.
22  *
23  *
24  * Contributor(s): Blender Foundation,
25  *                 Sergey Sharybin
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/editors/space_clip/space_clip.c
31  *  \ingroup spclip
32  */
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "DNA_scene_types.h"
38 #include "DNA_movieclip_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_main.h"
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
48 #include "BKE_movieclip.h"
49 #include "BKE_tracking.h"
50
51 #include "IMB_imbuf_types.h"
52
53 #include "ED_screen.h"
54 #include "ED_clip.h"
55 #include "ED_transform.h"
56
57 #include "BIF_gl.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "UI_interface.h"
63 #include "UI_resources.h"
64 #include "UI_view2d.h"
65
66 #include "RNA_access.h"
67
68
69 #include "clip_intern.h"        // own include
70
71 /* ******************** default callbacks for clip space ***************** */
72
73 static SpaceLink *clip_new(const bContext *UNUSED(C))
74 {
75         ARegion *ar;
76         SpaceClip *sc;
77
78         sc= MEM_callocN(sizeof(SpaceClip), "initclip");
79         sc->spacetype= SPACE_CLIP;
80         sc->flag= SC_SHOW_MARKER_PATTERN|SC_SHOW_TRACK_PATH;
81         sc->zoom= 1.0f;
82         sc->path_length= 20;
83
84         /* header */
85         ar= MEM_callocN(sizeof(ARegion), "header for clip");
86
87         BLI_addtail(&sc->regionbase, ar);
88         ar->regiontype= RGN_TYPE_HEADER;
89         ar->alignment= RGN_ALIGN_BOTTOM;
90
91         /* tools view */
92         ar= MEM_callocN(sizeof(ARegion), "tools for logic");
93
94         BLI_addtail(&sc->regionbase, ar);
95         ar->regiontype= RGN_TYPE_TOOLS;
96         ar->alignment= RGN_ALIGN_LEFT;
97
98         /* properties view */
99         ar= MEM_callocN(sizeof(ARegion), "properties for logic");
100
101         BLI_addtail(&sc->regionbase, ar);
102         ar->regiontype= RGN_TYPE_UI;
103         ar->alignment= RGN_ALIGN_RIGHT;
104
105         /* main area */
106         ar= MEM_callocN(sizeof(ARegion), "main area for clip");
107
108         BLI_addtail(&sc->regionbase, ar);
109         ar->regiontype= RGN_TYPE_WINDOW;
110
111         return (SpaceLink *)sc;
112 }
113
114 /* not spacelink itself */
115 static void clip_free(SpaceLink *sl)
116 {
117         SpaceClip *sclip= (SpaceClip*) sl;
118
119         sclip->clip= NULL;
120 }
121
122 /* spacetype; init callback */
123 static void clip_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
124 {
125
126 }
127
128 static SpaceLink *clip_duplicate(SpaceLink *sl)
129 {
130         SpaceClip *sclipn= MEM_dupallocN(sl);
131
132         /* clear or remove stuff from old */
133
134         return (SpaceLink *)sclipn;
135 }
136
137 static void clip_listener(ScrArea *sa, wmNotifier *wmn)
138 {
139         SpaceClip *sc= sa->spacedata.first;
140
141         (void)sc;
142
143         /* context changes */
144         switch(wmn->category) {
145                 case NC_SCENE:
146                         switch(wmn->data) {
147                                 case ND_FRAME:
148                                 case ND_FRAME_RANGE:
149                                         ED_area_tag_refresh(sa);
150                                         ED_area_tag_redraw(sa);
151                                         break;
152                         }
153                         break;
154                 case NC_MOVIECLIP:
155                         switch(wmn->data) {
156                                 case ND_DISPLAY:
157                                 case ND_SELECT:
158                                         ED_area_tag_redraw(sa);
159                                         break;
160                         }
161                         switch(wmn->action) {
162                                 case NA_REMOVED:
163                                 case NA_SELECTED:
164                                 case NA_EDITED:
165                                 case NA_EVALUATED:
166                                         ED_area_tag_redraw(sa);
167                                         break;
168                         }
169                         break;
170                 case NC_GEOM:
171                         switch(wmn->data) {
172                                 case ND_SELECT:
173                                         ED_area_tag_redraw(sa);
174                                         break;
175                         }
176                         break;
177                 case NC_SPACE:
178                         if(wmn->data==ND_SPACE_CLIP)
179                                 ED_area_tag_redraw(sa);
180                         break;
181         }
182 }
183
184 static void clip_operatortypes(void)
185 {
186         WM_operatortype_append(CLIP_OT_open);
187         WM_operatortype_append(CLIP_OT_reload);
188         WM_operatortype_append(CLIP_OT_tools);
189          WM_operatortype_append(CLIP_OT_properties);
190         // WM_operatortype_append(CLIP_OT_unlink);
191         WM_operatortype_append(CLIP_OT_view_pan);
192         WM_operatortype_append(CLIP_OT_view_zoom);
193         WM_operatortype_append(CLIP_OT_view_zoom_in);
194         WM_operatortype_append(CLIP_OT_view_zoom_out);
195         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
196         WM_operatortype_append(CLIP_OT_view_all);
197         WM_operatortype_append(CLIP_OT_view_selected);
198
199         WM_operatortype_append(CLIP_OT_select);
200         WM_operatortype_append(CLIP_OT_select_all);
201         WM_operatortype_append(CLIP_OT_select_border);
202         WM_operatortype_append(CLIP_OT_select_circle);
203
204         WM_operatortype_append(CLIP_OT_add_marker);
205         WM_operatortype_append(CLIP_OT_delete_track);
206         WM_operatortype_append(CLIP_OT_delete_marker);
207
208         WM_operatortype_append(CLIP_OT_track_markers);
209         WM_operatortype_append(CLIP_OT_solve_camera);
210         WM_operatortype_append(CLIP_OT_clear_reconstruction);
211
212         WM_operatortype_append(CLIP_OT_disable_markers);
213         WM_operatortype_append(CLIP_OT_hide_tracks);
214         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
215
216         WM_operatortype_append(CLIP_OT_set_origin);
217         WM_operatortype_append(CLIP_OT_set_floor);
218         WM_operatortype_append(CLIP_OT_set_axis);
219
220         WM_operatortype_append(CLIP_OT_clear_track_path);
221
222         WM_operatortype_append(CLIP_OT_slide_marker);
223 }
224
225 static void clip_keymap(struct wmKeyConfig *keyconf)
226 {
227         wmKeyMap *keymap;
228         wmKeyMapItem *kmi;
229
230         keymap= WM_keymap_find(keyconf, "Clip", SPACE_CLIP, 0);
231
232         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
233
234         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
235          WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
236
237         /* ********* View/navigation ******** */
238
239         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
240         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
241         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
242
243         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
244         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
245         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
246         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
247         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
248         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
249
250         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
251         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
252         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
253         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
254         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
255         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
256         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
257
258         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
259
260         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
261
262         WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
263         RNA_boolean_set(WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", 1);
264         WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
265         RNA_enum_set(WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "action", SEL_INVERT);
266         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
267         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
268
269         WM_keymap_add_item(keymap, "CLIP_OT_add_marker", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
270
271         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
272         RNA_boolean_set(kmi->ptr, "backwards", 1);
273         WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
274         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
275         RNA_boolean_set(kmi->ptr, "sequence", 1);
276         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
277         RNA_boolean_set(kmi->ptr, "backwards", 1);
278         RNA_boolean_set(kmi->ptr, "sequence", 1);
279
280         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
281         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
282         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
283         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
284         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
285         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
286
287         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
288         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
289
290         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
291
292         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
293         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
294
295         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
296
297         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
298
299         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
300         kmi= WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
301         RNA_boolean_set(kmi->ptr, "unselected", 1);
302         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
303
304         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
305 }
306
307 const char *clip_context_dir[]= {"edit_movieclip", NULL};
308
309 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
310 {
311         SpaceClip *sc= CTX_wm_space_clip(C);
312
313         if(CTX_data_dir(member)) {
314                 CTX_data_dir_set(result, clip_context_dir);
315                 return 1;
316         }
317         else if(CTX_data_equals(member, "edit_movieclip")) {
318                 CTX_data_id_pointer_set(result, &sc->clip->id);
319                 return 1;
320         }
321
322         return 0;
323 }
324
325 static void clip_refresh(const bContext *C, ScrArea *UNUSED(sa))
326 {
327         SpaceClip *sc= CTX_wm_space_clip(C);
328
329         BKE_movieclip_user_set_frame(&sc->user, CTX_data_scene(C)->r.cfra);
330 }
331
332 /********************* main region ********************/
333
334 /* sets up the fields of the View2D from zoom and offset */
335 static void movieclip_main_area_set_view2d(SpaceClip *sc, ARegion *ar)
336 {
337         float x1, y1, w, h;
338         int width, height, winx, winy;
339
340         ED_space_clip_size(sc, &width, &height);
341
342         w= width;
343         h= height;
344
345         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
346         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
347
348         ar->v2d.tot.xmin= 0;
349         ar->v2d.tot.ymin= 0;
350         ar->v2d.tot.xmax= w;
351         ar->v2d.tot.ymax= h;
352
353         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
354         ar->v2d.mask.xmax= winx;
355         ar->v2d.mask.ymax= winy;
356
357         /* which part of the image space do we see? */
358         x1= ar->winrct.xmin+(winx-sc->zoom*w)/2.0f;
359         y1= ar->winrct.ymin+(winy-sc->zoom*h)/2.0f;
360
361         x1-= sc->zoom*sc->xof;
362         y1-= sc->zoom*sc->yof;
363
364         /* relative display right */
365         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sc->zoom);
366         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sc->zoom);
367
368         /* relative display left */
369         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sc->zoom);
370         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sc->zoom);
371
372         /* normalize 0.0..1.0 */
373         ar->v2d.cur.xmin /= w;
374         ar->v2d.cur.xmax /= w;
375         ar->v2d.cur.ymin /= h;
376         ar->v2d.cur.ymax /= h;
377 }
378
379 /* add handlers, stuff you only do once or on area/region changes */
380 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
381 {
382         wmKeyMap *keymap;
383
384         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
385
386         /* own keymap */
387         keymap= WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
388         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
389 }
390
391 static void clip_main_area_draw(const bContext *C, ARegion *ar)
392 {
393         /* draw entirely, view changes should be handled here */
394         SpaceClip *sc= CTX_wm_space_clip(C);
395         Scene *scene= CTX_data_scene(C);
396         MovieClip *clip= ED_space_clip(sc);
397
398         /* if trcking is in progress, we should sunchronize framenr from clipuser
399            so latest tracked frame would be shown */
400         if(clip && clip->tracking_context)
401                 BKE_tracking_sync_user(&sc->user, clip->tracking_context);
402
403         if(sc->flag&SC_LOCK_SELECTION)
404                 ED_clip_view_selection(sc, ar, 0);
405
406         /* clear and setup matrix */
407         UI_ThemeClearColor(TH_BACK);
408         glClear(GL_COLOR_BUFFER_BIT);
409
410         /* data... */
411         movieclip_main_area_set_view2d(sc, ar);
412         draw_clip_main(sc, ar, scene);
413
414         /* reset view matrix */
415         UI_view2d_view_restore(C);
416 }
417
418 /****************** header region ******************/
419
420 /* add handlers, stuff you only do once or on area/region changes */
421 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
422 {
423         ED_region_header_init(ar);
424 }
425
426 static void clip_header_area_draw(const bContext *C, ARegion *ar)
427 {
428         ED_region_header(C, ar);
429 }
430
431 /****************** tools region ******************/
432
433 /* add handlers, stuff you only do once or on area/region changes */
434 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
435 {
436         ED_region_panels_init(wm, ar);
437 }
438
439 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
440 {
441         ED_region_panels(C, ar, 1, NULL, -1);
442 }
443
444 /****************** properties region ******************/
445
446 /* add handlers, stuff you only do once or on area/region changes */
447 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
448 {
449         ED_region_panels_init(wm, ar);
450 }
451
452 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
453 {
454         ED_region_panels(C, ar, 1, NULL, -1);
455 }
456
457 /********************* registration ********************/
458
459 /* only called once, from space/spacetypes.c */
460 void ED_spacetype_clip(void)
461 {
462         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype clip");
463         ARegionType *art;
464
465         st->spaceid= SPACE_CLIP;
466         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
467
468         st->new= clip_new;
469         st->free= clip_free;
470         st->init= clip_init;
471         st->duplicate= clip_duplicate;
472         st->operatortypes= clip_operatortypes;
473         st->keymap= clip_keymap;
474         st->listener= clip_listener;
475         st->context= clip_context;
476         st->refresh= clip_refresh;
477
478         /* regions: main window */
479         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
480         art->regionid= RGN_TYPE_WINDOW;
481         art->init= clip_main_area_init;
482         art->draw= clip_main_area_draw;
483         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
484
485         BLI_addhead(&st->regiontypes, art);
486
487         /* regions: properties */
488         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
489         art->regionid= RGN_TYPE_UI;
490         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
491         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
492         art->init= clip_properties_area_init;
493         art->draw= clip_properties_area_draw;
494         BLI_addhead(&st->regiontypes, art);
495         ED_clip_buttons_register(art);
496
497         /* regions: tools */
498         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
499         art->regionid= RGN_TYPE_TOOLS;
500         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
501         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
502         art->init= clip_tools_area_init;
503         art->draw= clip_tools_area_draw;
504
505         BLI_addhead(&st->regiontypes, art);
506
507         /* regions: header */
508         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
509         art->regionid= RGN_TYPE_HEADER;
510         art->prefsizey= HEADERY;
511         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
512
513         art->init= clip_header_area_init;
514         art->draw= clip_header_area_draw;
515
516         BLI_addhead(&st->regiontypes, art);
517
518         BKE_spacetype_register(st);
519 }