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 "IMB_imbuf.h"
58
59 #include "BIF_gl.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63
64 #include "UI_interface.h"
65 #include "UI_resources.h"
66 #include "UI_view2d.h"
67
68 #include "RNA_access.h"
69
70
71 #include "clip_intern.h"        // own include
72
73 static void clip_scopes_tag_refresh(ScrArea *sa)
74 {
75         SpaceClip *sc= (SpaceClip *)sa->spacedata.first;
76         ARegion *ar;
77
78         /* only while proeprties are visible */
79         for (ar=sa->regionbase.first; ar; ar=ar->next) {
80                 if (ar->regiontype == RGN_TYPE_UI && ar->flag & RGN_FLAG_HIDDEN)
81                         return;
82         }
83
84         sc->scopes.ok= 0;
85 }
86
87 static void clip_stabilization_tag_refresh(ScrArea *sa)
88 {
89         SpaceClip *sc= (SpaceClip *)sa->spacedata.first;
90         MovieClip *clip= ED_space_clip(sc);
91
92         if(clip) {
93                 MovieTrackingStabilization *stab= &clip->tracking.stabilization;
94
95                 stab->ok= 0;
96         }
97 }
98
99 /* ******************** default callbacks for clip space ***************** */
100
101 static SpaceLink *clip_new(const bContext *UNUSED(C))
102 {
103         ARegion *ar;
104         SpaceClip *sc;
105
106         sc= MEM_callocN(sizeof(SpaceClip), "initclip");
107         sc->spacetype= SPACE_CLIP;
108         sc->flag= SC_SHOW_MARKER_PATTERN|SC_SHOW_TRACK_PATH|SC_SHOW_GPENCIL|SC_MANUAL_CALIBRATION;
109         sc->zoom= 1.0f;
110         sc->path_length= 20;
111         sc->scopes.track_preview_height= 120;
112
113         /* header */
114         ar= MEM_callocN(sizeof(ARegion), "header for clip");
115
116         BLI_addtail(&sc->regionbase, ar);
117         ar->regiontype= RGN_TYPE_HEADER;
118         ar->alignment= RGN_ALIGN_BOTTOM;
119
120         /* tools view */
121         ar= MEM_callocN(sizeof(ARegion), "tools for logic");
122
123         BLI_addtail(&sc->regionbase, ar);
124         ar->regiontype= RGN_TYPE_TOOLS;
125         ar->alignment= RGN_ALIGN_LEFT;
126
127         /* tool properties */
128         ar= MEM_callocN(sizeof(ARegion), "tool properties for clip");
129
130         BLI_addtail(&sc->regionbase, ar);
131         ar->regiontype= RGN_TYPE_TOOL_PROPS;
132         ar->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
133
134         /* properties view */
135         ar= MEM_callocN(sizeof(ARegion), "properties for logic");
136
137         BLI_addtail(&sc->regionbase, ar);
138         ar->regiontype= RGN_TYPE_UI;
139         ar->alignment= RGN_ALIGN_RIGHT;
140
141         /* main area */
142         ar= MEM_callocN(sizeof(ARegion), "main area for clip");
143
144         BLI_addtail(&sc->regionbase, ar);
145         ar->regiontype= RGN_TYPE_WINDOW;
146
147         return (SpaceLink *)sc;
148 }
149
150 /* not spacelink itself */
151 static void clip_free(SpaceLink *sl)
152 {
153         SpaceClip *sc= (SpaceClip*) sl;
154
155         sc->clip= NULL;
156
157         if(sc->scopes.track_preview)
158                 IMB_freeImBuf(sc->scopes.track_preview);
159 }
160
161 /* spacetype; init callback */
162 static void clip_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
163 {
164
165 }
166
167 static SpaceLink *clip_duplicate(SpaceLink *sl)
168 {
169         SpaceClip *scn= MEM_dupallocN(sl);
170
171         /* clear or remove stuff from old */
172         scn->scopes.track_preview= NULL;
173         scn->scopes.ok= 0;
174
175         return (SpaceLink *)scn;
176 }
177
178 static void clip_listener(ScrArea *sa, wmNotifier *wmn)
179 {
180         /* context changes */
181         switch(wmn->category) {
182                 case NC_SCENE:
183                         switch(wmn->data) {
184                                 case ND_FRAME:
185                                         clip_scopes_tag_refresh(sa);
186                                         /* no break! */
187
188                                 case ND_FRAME_RANGE:
189                                         ED_area_tag_refresh(sa);
190                                         ED_area_tag_redraw(sa);
191                                         break;
192                         }
193                         break;
194                 case NC_MOVIECLIP:
195                         switch(wmn->data) {
196                                 case ND_DISPLAY:
197                                 case ND_SELECT:
198                                         clip_scopes_tag_refresh(sa);
199                                         ED_area_tag_redraw(sa);
200                                         break;
201                         }
202                         switch(wmn->action) {
203                                 case NA_REMOVED:
204                                 case NA_EDITED:
205                                 case NA_EVALUATED:
206                                         clip_stabilization_tag_refresh(sa);
207                                         /* no break! */
208
209                                 case NA_SELECTED:
210                                         clip_scopes_tag_refresh(sa);
211                                         ED_area_tag_redraw(sa);
212                                         break;
213                         }
214                         break;
215                 case NC_GEOM:
216                         switch(wmn->data) {
217                                 case ND_SELECT:
218                                         clip_scopes_tag_refresh(sa);
219                                         ED_area_tag_redraw(sa);
220                                         break;
221                         }
222                         break;
223                  case NC_SCREEN:
224                         if(wmn->data==ND_ANIMPLAY) {
225                                 ED_area_tag_redraw(sa);
226                         }
227                         break;
228                 case NC_SPACE:
229                         if(wmn->data==ND_SPACE_CLIP) {
230                                 clip_scopes_tag_refresh(sa);
231                                 clip_stabilization_tag_refresh(sa);
232                                 ED_area_tag_redraw(sa);
233                         }
234                         break;
235         }
236 }
237
238 static void clip_operatortypes(void)
239 {
240         /* ** clip_ops.c ** */
241         WM_operatortype_append(CLIP_OT_open);
242         WM_operatortype_append(CLIP_OT_reload);
243         // WM_operatortype_append(CLIP_OT_unlink);
244         WM_operatortype_append(CLIP_OT_view_pan);
245         WM_operatortype_append(CLIP_OT_view_zoom);
246         WM_operatortype_append(CLIP_OT_view_zoom_in);
247         WM_operatortype_append(CLIP_OT_view_zoom_out);
248         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
249         WM_operatortype_append(CLIP_OT_view_all);
250         WM_operatortype_append(CLIP_OT_view_selected);
251         WM_operatortype_append(CLIP_OT_change_frame);
252         WM_operatortype_append(CLIP_OT_rebuild_proxy);
253         WM_operatortype_append(CLIP_OT_mode_set);
254
255         /* ** clip_toolbar.c ** */
256         WM_operatortype_append(CLIP_OT_tools);
257         WM_operatortype_append(CLIP_OT_properties);
258
259         /* ** tracking_ops.c ** */
260
261         /* navigation */
262         WM_operatortype_append(CLIP_OT_frame_jump);
263
264         /* foorage */
265         WM_operatortype_append(CLIP_OT_set_center_principal);
266
267         /* selection */
268         WM_operatortype_append(CLIP_OT_select);
269         WM_operatortype_append(CLIP_OT_select_all);
270         WM_operatortype_append(CLIP_OT_select_border);
271         WM_operatortype_append(CLIP_OT_select_circle);
272         WM_operatortype_append(CLIP_OT_select_grouped);
273
274         /* markers */
275         WM_operatortype_append(CLIP_OT_add_marker);
276         WM_operatortype_append(CLIP_OT_slide_marker);
277         WM_operatortype_append(CLIP_OT_delete_track);
278         WM_operatortype_append(CLIP_OT_delete_marker);
279
280         /* track */
281         WM_operatortype_append(CLIP_OT_track_markers);
282
283         /* solving */
284         WM_operatortype_append(CLIP_OT_solve_camera);
285         WM_operatortype_append(CLIP_OT_clear_solution);
286
287         WM_operatortype_append(CLIP_OT_disable_markers);
288         WM_operatortype_append(CLIP_OT_hide_tracks);
289         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
290         WM_operatortype_append(CLIP_OT_lock_tracks);
291
292         /* orientation */
293         WM_operatortype_append(CLIP_OT_set_origin);
294         WM_operatortype_append(CLIP_OT_set_floor);
295         WM_operatortype_append(CLIP_OT_set_axis);
296         WM_operatortype_append(CLIP_OT_set_scale);
297
298         /* detect */
299         WM_operatortype_append(CLIP_OT_detect_features);
300
301         /* stabilization */
302         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
303         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
304         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
305         WM_operatortype_append(CLIP_OT_stabilize_2d_set_rotation);
306
307         /* clean-up */
308         WM_operatortype_append(CLIP_OT_clear_track_path);
309         WM_operatortype_append(CLIP_OT_join_tracks);
310         WM_operatortype_append(CLIP_OT_track_copy_color);
311
312         WM_operatortype_append(CLIP_OT_clean_tracks);
313 }
314
315 static void clip_keymap(struct wmKeyConfig *keyconf)
316 {
317         wmKeyMap *keymap;
318         wmKeyMapItem *kmi;
319
320         /* ******** Global hotkeys avalaible for all regions ******** */
321
322         keymap= WM_keymap_find(keyconf, "Clip", SPACE_CLIP, 0);
323
324         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
325
326         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
327         WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
328
329         /* 2d tracking */
330         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
331         RNA_boolean_set(kmi->ptr, "backwards", 1);
332         WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
333         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
334         RNA_boolean_set(kmi->ptr, "sequence", 1);
335         kmi= WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
336         RNA_boolean_set(kmi->ptr, "backwards", 1);
337         RNA_boolean_set(kmi->ptr, "sequence", 1);
338
339         /* mode */
340         kmi= WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
341         RNA_enum_set(kmi->ptr, "mode", SC_MODE_RECONSTRUCTION);
342         RNA_boolean_set(kmi->ptr, "toggle", 1);
343
344         kmi= WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, KM_CTRL, 0);
345         RNA_enum_set(kmi->ptr, "mode", SC_MODE_DISTORTION);
346         RNA_boolean_set(kmi->ptr, "toggle", 1);
347
348         /* ******** Hotkeys avalaible for main region only ******** */
349
350         keymap= WM_keymap_find(keyconf, "Clip Editor", SPACE_CLIP, 0);
351
352         /* ** View/navigation ** */
353
354         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
355         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
356         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
357
358         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
359         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
360         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
361         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
362         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
363         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
364
365         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
366         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
367         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
368         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
369         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
370         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
371         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
372
373         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
374         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
375
376         /* jump to special frame */
377         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
378         RNA_enum_set(kmi->ptr, "position", 0);
379
380         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
381         RNA_enum_set(kmi->ptr, "position", 1);
382
383         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
384         RNA_enum_set(kmi->ptr, "position", 2);
385
386         kmi= WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
387         RNA_enum_set(kmi->ptr, "position", 3);
388
389         /* "timeline" */
390         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
391
392         /* selection */
393         WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
394         RNA_boolean_set(WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", 1);
395         WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
396         RNA_enum_set(WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "action", SEL_INVERT);
397         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
398         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
399         WM_keymap_add_menu(keymap, "CLIP_MT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
400
401         /* marker */
402         WM_keymap_add_item(keymap, "CLIP_OT_add_marker_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
403
404         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
405         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", XKEY, KM_PRESS, KM_SHIFT, 0);
406
407         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
408
409         kmi= WM_keymap_add_item(keymap, "CLIP_OT_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
410         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
411
412         /* tracks */
413         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
414         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
415
416         kmi= WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_CTRL, 0);
417         RNA_enum_set(kmi->ptr, "action", 0);    /* lock */
418
419         kmi= WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_ALT, 0);
420         RNA_enum_set(kmi->ptr, "action", 1);    /* unlock */
421
422         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
423
424         kmi= WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
425         RNA_boolean_set(kmi->ptr, "unselected", 1);
426
427         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
428
429         /* clean-up */
430         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
431         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
432         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
433         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
434         kmi= WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT|KM_SHIFT, 0);
435         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
436
437         WM_keymap_add_item(keymap, "CLIP_OT_join_tracks", JKEY, KM_PRESS, KM_CTRL, 0);
438
439         /* menus */
440         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
441
442         /* display */
443         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
444         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
445
446         kmi= WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0);
447         RNA_string_set(kmi->ptr, "data_path", "space_data.use_mute_footage");
448
449         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
450 }
451
452 const char *clip_context_dir[]= {"edit_movieclip", NULL};
453
454 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
455 {
456         SpaceClip *sc= CTX_wm_space_clip(C);
457
458         if(CTX_data_dir(member)) {
459                 CTX_data_dir_set(result, clip_context_dir);
460                 return 1;
461         }
462         else if(CTX_data_equals(member, "edit_movieclip")) {
463                 CTX_data_id_pointer_set(result, &sc->clip->id);
464                 return 1;
465         }
466
467         return 0;
468 }
469
470 static void clip_refresh(const bContext *C, ScrArea *UNUSED(sa))
471 {
472         SpaceClip *sc= CTX_wm_space_clip(C);
473
474         BKE_movieclip_user_set_frame(&sc->user, CTX_data_scene(C)->r.cfra);
475 }
476
477 /********************* main region ********************/
478
479 /* sets up the fields of the View2D from zoom and offset */
480 static void movieclip_main_area_set_view2d(SpaceClip *sc, ARegion *ar)
481 {
482         MovieClip *clip= ED_space_clip(sc);
483         float x1, y1, w, h;
484         int width, height, winx, winy;
485
486         ED_space_clip_size(sc, &width, &height);
487
488         w= width;
489         h= height;
490
491         if(clip)
492                 h*= clip->aspy/clip->aspx/clip->tracking.camera.pixel_aspect;
493
494         winx= ar->winrct.xmax - ar->winrct.xmin + 1;
495         winy= ar->winrct.ymax - ar->winrct.ymin + 1;
496
497         ar->v2d.tot.xmin= 0;
498         ar->v2d.tot.ymin= 0;
499         ar->v2d.tot.xmax= w;
500         ar->v2d.tot.ymax= h;
501
502         ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
503         ar->v2d.mask.xmax= winx;
504         ar->v2d.mask.ymax= winy;
505
506         /* which part of the image space do we see? */
507         x1= ar->winrct.xmin+(winx-sc->zoom*w)/2.0f;
508         y1= ar->winrct.ymin+(winy-sc->zoom*h)/2.0f;
509
510         x1-= sc->zoom*sc->xof;
511         y1-= sc->zoom*sc->yof;
512
513         /* relative display right */
514         ar->v2d.cur.xmin= ((ar->winrct.xmin - (float)x1)/sc->zoom);
515         ar->v2d.cur.xmax= ar->v2d.cur.xmin + ((float)winx/sc->zoom);
516
517         /* relative display left */
518         ar->v2d.cur.ymin= ((ar->winrct.ymin-(float)y1)/sc->zoom);
519         ar->v2d.cur.ymax= ar->v2d.cur.ymin + ((float)winy/sc->zoom);
520
521         /* normalize 0.0..1.0 */
522         ar->v2d.cur.xmin /= w;
523         ar->v2d.cur.xmax /= w;
524         ar->v2d.cur.ymin /= h;
525         ar->v2d.cur.ymax /= h;
526 }
527
528 /* add handlers, stuff you only do once or on area/region changes */
529 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
530 {
531         wmKeyMap *keymap;
532
533         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
534
535         /* own keymap */
536         keymap= WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
537         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
538
539         keymap= WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
540         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
541 }
542
543 static void clip_main_area_draw(const bContext *C, ARegion *ar)
544 {
545         /* draw entirely, view changes should be handled here */
546         SpaceClip *sc= CTX_wm_space_clip(C);
547         Scene *scene= CTX_data_scene(C);
548         MovieClip *clip= ED_space_clip(sc);
549
550         /* if trcking is in progress, we should sunchronize framenr from clipuser
551            so latest tracked frame would be shown */
552         if(clip && clip->tracking_context)
553                 BKE_tracking_sync_user(&sc->user, clip->tracking_context);
554
555         if(sc->flag&SC_LOCK_SELECTION) {
556                 ImBuf *tmpibuf= NULL;
557
558                 if(clip && clip->tracking.stabilization.flag&TRACKING_2D_STABILIZATION) {
559                         tmpibuf= ED_space_clip_acquire_stable_buffer(sc, NULL, NULL, NULL);
560                 }
561
562                 ED_clip_view_selection(sc, ar, 0);
563
564                 if(tmpibuf)
565                         IMB_freeImBuf(tmpibuf);
566         }
567
568         /* clear and setup matrix */
569         UI_ThemeClearColor(TH_BACK);
570         glClear(GL_COLOR_BUFFER_BIT);
571
572         /* data... */
573         movieclip_main_area_set_view2d(sc, ar);
574
575         draw_clip_main(sc, ar, scene);
576
577         /* Grease Pencil */
578         draw_clip_grease_pencil((bContext *)C, 1);
579
580         /* reset view matrix */
581         UI_view2d_view_restore(C);
582
583         /* draw Grease Pencil - screen space only */
584         draw_clip_grease_pencil((bContext *)C, 0);
585 }
586
587 static void clip_main_area_listener(ARegion *ar, wmNotifier *wmn)
588 {
589         /* context changes */
590         switch(wmn->category) {
591                 case NC_SCREEN:
592                         if (wmn->data==ND_GPENCIL)
593                                 ED_region_tag_redraw(ar);
594                 break;
595         }
596 }
597
598 /****************** header region ******************/
599
600 /* add handlers, stuff you only do once or on area/region changes */
601 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
602 {
603         ED_region_header_init(ar);
604 }
605
606 static void clip_header_area_draw(const bContext *C, ARegion *ar)
607 {
608         ED_region_header(C, ar);
609 }
610
611 /****************** tools region ******************/
612
613 /* add handlers, stuff you only do once or on area/region changes */
614 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
615 {
616         ED_region_panels_init(wm, ar);
617 }
618
619 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
620 {
621         ED_region_panels(C, ar, 1, NULL, -1);
622 }
623
624 /****************** tool properties region ******************/
625
626 static void clip_props_area_listener(ARegion *ar, wmNotifier *wmn)
627 {
628         /* context changes */
629         switch(wmn->category) {
630                 case NC_WM:
631                         if(wmn->data == ND_HISTORY)
632                                 ED_region_tag_redraw(ar);
633                         break;
634                 case NC_SCENE:
635                         if(wmn->data == ND_MODE)
636                                 ED_region_tag_redraw(ar);
637                         break;
638                 case NC_SPACE:
639                         if(wmn->data == ND_SPACE_CLIP)
640                                 ED_region_tag_redraw(ar);
641                         break;
642                 case NC_SCREEN:
643                         if(wmn->data == ND_GPENCIL)
644                                 ED_region_tag_redraw(ar);
645                         break;
646         }
647 }
648
649 /****************** properties region ******************/
650
651 /* add handlers, stuff you only do once or on area/region changes */
652 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
653 {
654         wmKeyMap *keymap;
655
656         ED_region_panels_init(wm, ar);
657
658         keymap= WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
659         WM_event_add_keymap_handler(&ar->handlers, keymap);
660 }
661
662 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
663 {
664         SpaceClip *sc= CTX_wm_space_clip(C);
665
666         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
667
668         ED_region_panels(C, ar, 1, NULL, -1);
669 }
670
671 static void clip_properties_area_listener(ARegion *ar, wmNotifier *wmn)
672 {
673         /* context changes */
674         switch(wmn->category) {
675                 case NC_SCREEN:
676                         if (wmn->data==ND_GPENCIL)
677                                 ED_region_tag_redraw(ar);
678                         break;
679                 case NC_BRUSH:
680                         if(wmn->action==NA_EDITED)
681                                 ED_region_tag_redraw(ar);
682                         break;
683         }
684 }
685
686 /********************* registration ********************/
687
688 /* only called once, from space/spacetypes.c */
689 void ED_spacetype_clip(void)
690 {
691         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype clip");
692         ARegionType *art;
693
694         st->spaceid= SPACE_CLIP;
695         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
696
697         st->new= clip_new;
698         st->free= clip_free;
699         st->init= clip_init;
700         st->duplicate= clip_duplicate;
701         st->operatortypes= clip_operatortypes;
702         st->keymap= clip_keymap;
703         st->listener= clip_listener;
704         st->context= clip_context;
705         st->refresh= clip_refresh;
706
707         /* regions: main window */
708         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
709         art->regionid= RGN_TYPE_WINDOW;
710         art->init= clip_main_area_init;
711         art->draw= clip_main_area_draw;
712         art->listener= clip_main_area_listener;
713         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_GPENCIL;
714
715         BLI_addhead(&st->regiontypes, art);
716
717         /* regions: properties */
718         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
719         art->regionid= RGN_TYPE_UI;
720         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
721         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
722         art->init= clip_properties_area_init;
723         art->draw= clip_properties_area_draw;
724         art->listener= clip_properties_area_listener;
725         BLI_addhead(&st->regiontypes, art);
726         ED_clip_buttons_register(art);
727
728         /* regions: tools */
729         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
730         art->regionid= RGN_TYPE_TOOLS;
731         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
732         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
733         art->listener= clip_props_area_listener;
734         art->init= clip_tools_area_init;
735         art->draw= clip_tools_area_draw;
736
737         BLI_addhead(&st->regiontypes, art);
738
739         /* tool properties */
740         art= MEM_callocN(sizeof(ARegionType), "spacetype clip tool properties region");
741         art->regionid = RGN_TYPE_TOOL_PROPS;
742         art->prefsizex= 0;
743         art->prefsizey= 120;
744         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI;
745         art->listener= clip_props_area_listener;
746         art->init= clip_tools_area_init;
747         art->draw= clip_tools_area_draw;
748         ED_clip_tool_props_register(art);
749
750         BLI_addhead(&st->regiontypes, art);
751
752         /* regions: header */
753         art= MEM_callocN(sizeof(ARegionType), "spacetype clip region");
754         art->regionid= RGN_TYPE_HEADER;
755         art->prefsizey= HEADERY;
756         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
757
758         art->init= clip_header_area_init;
759         art->draw= clip_header_area_draw;
760
761         BLI_addhead(&st->regiontypes, art);
762
763         BKE_spacetype_register(st);
764 }