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