Camera tracking: backport refactoring made in local branches with masking and dopeshe...
[blender.git] / source / blender / editors / space_clip / clip_buttons.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/clip_buttons.c
29  *  \ingroup spclip
30  */
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_space_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_listbase.h"
44
45 #include "BKE_context.h"
46 #include "BKE_depsgraph.h"
47 #include "BKE_screen.h"
48 #include "BKE_movieclip.h"
49 #include "BKE_tracking.h"
50
51 #include "ED_clip.h"
52 #include "ED_gpencil.h"
53
54 #include "UI_interface.h"
55 #include "UI_resources.h"
56
57 #include "RNA_access.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "clip_intern.h"        // own include
63
64 /* Panels */
65
66 static int clip_grease_pencil_panel_poll(const bContext *C, PanelType *UNUSED(pt))
67 {
68         SpaceClip *sc = CTX_wm_space_clip(C);
69
70         return TRUE;
71 }
72
73 void ED_clip_buttons_register(ARegionType *art)
74 {
75         PanelType *pt;
76
77         pt = MEM_callocN(sizeof(PanelType), "spacetype clip panel gpencil");
78         strcpy(pt->idname, "CLIP_PT_gpencil");
79         strcpy(pt->label, "Grease Pencil");
80         pt->draw = gpencil_panel_standard;
81         pt->flag |= PNL_DEFAULT_CLOSED;
82         pt->poll = clip_grease_pencil_panel_poll;
83         BLI_addtail(&art->paneltypes, pt);
84 }
85
86 /********************* MovieClip Template ************************/
87
88 void uiTemplateMovieClip(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, int compact)
89 {
90         PropertyRNA *prop;
91         PointerRNA clipptr;
92         MovieClip *clip;
93         uiLayout *row, *split;
94         uiBlock *block;
95
96         if (!ptr->data)
97                 return;
98
99         prop = RNA_struct_find_property(ptr, propname);
100         if (!prop) {
101                 printf("%s: property not found: %s.%s\n",
102                        __func__, RNA_struct_identifier(ptr->type), propname);
103                 return;
104         }
105
106         if (RNA_property_type(prop) != PROP_POINTER) {
107                 printf("%s: expected pointer property for %s.%s\n",
108                        __func__, RNA_struct_identifier(ptr->type), propname);
109                 return;
110         }
111
112         clipptr = RNA_property_pointer_get(ptr, prop);
113         clip = clipptr.data;
114
115         uiLayoutSetContextPointer(layout, "edit_movieclip", &clipptr);
116
117         if (!compact)
118                 uiTemplateID(layout, C, ptr, propname, NULL, "CLIP_OT_open", NULL);
119
120         if (clip) {
121                 row = uiLayoutRow(layout, 0);
122                 block = uiLayoutGetBlock(row);
123                 uiDefBut(block, LABEL, 0, "File Path:", 0, 19, 145, 19, NULL, 0, 0, 0, 0, "");
124
125                 row = uiLayoutRow(layout, 0);
126                 split = uiLayoutSplit(row, 0.0, 0);
127                 row = uiLayoutRow(split, 1);
128
129                 uiItemR(row, &clipptr, "filepath", 0, "", ICON_NONE);
130                 uiItemO(row, "", ICON_FILE_REFRESH, "clip.reload");
131         }
132 }
133
134 /********************* Track Template ************************/
135
136 void uiTemplateTrack(uiLayout *layout, PointerRNA *ptr, const char *propname)
137 {
138         PropertyRNA *prop;
139         PointerRNA scopesptr;
140         uiBlock *block;
141         rctf rect;
142         MovieClipScopes *scopes;
143
144         if (!ptr->data)
145                 return;
146
147         prop = RNA_struct_find_property(ptr, propname);
148         if (!prop) {
149                 printf("%s: property not found: %s.%s\n",
150                        __func__, RNA_struct_identifier(ptr->type), propname);
151                 return;
152         }
153
154         if (RNA_property_type(prop) != PROP_POINTER) {
155                 printf("%s: expected pointer property for %s.%s\n",
156                        __func__, RNA_struct_identifier(ptr->type), propname);
157                 return;
158         }
159
160         scopesptr = RNA_property_pointer_get(ptr, prop);
161         scopes = (MovieClipScopes *)scopesptr.data;
162
163         rect.xmin = 0; rect.xmax = 200;
164         rect.ymin = 0; rect.ymax = 120;
165
166         block = uiLayoutAbsoluteBlock(layout);
167
168         scopes->track_preview_height = (scopes->track_preview_height<=UI_UNIT_Y)?UI_UNIT_Y:scopes->track_preview_height;
169
170         uiDefBut(block, TRACKPREVIEW, 0, "", rect.xmin, rect.ymin, rect.xmax-rect.xmin, scopes->track_preview_height, scopes, 0, 0, 0, 0, "");
171 }
172
173 /********************* Marker Template ************************/
174
175 #define B_MARKER_POS                    3
176 #define B_MARKER_OFFSET                 4
177 #define B_MARKER_PAT_DIM                5
178 #define B_MARKER_SEARCH_POS             6
179 #define B_MARKER_SEARCH_DIM             7
180 #define B_MARKER_FLAG                   8
181
182 typedef struct {
183         int compact;                                                            /* compact mode */
184
185         MovieClip *clip;
186         MovieClipUser *user;                                            /* user of clip */
187         MovieTrackingTrack *track;
188
189         int framenr;                                                            /* current frame number */
190         float marker_pos[2];                                            /* position of marker in pixel coords */
191         float track_pat[2];                                                     /* position and dimensions of marker pattern in pixel coords */
192         float track_offset[2];                                          /* offset of "parenting" point */
193         float track_search_pos[2], track_search[2];     /* position and dimensions of marker search in pixel coords */
194         int marker_flag;                                                        /* marker's flags */
195 } MarkerUpdateCb;
196
197 static void to_pixel_space(float r[2], float a[2], int width, int height)
198 {
199         copy_v2_v2(r, a);
200         r[0] *= width;
201         r[1] *= height;
202 }
203
204 static void marker_update_cb(bContext *C, void *arg_cb, void *UNUSED(arg))
205 {
206         MarkerUpdateCb *cb = (MarkerUpdateCb*) arg_cb;
207         MovieTrackingMarker *marker;
208
209         if (!cb->compact)
210                 return;
211
212         marker = BKE_tracking_ensure_marker(cb->track, cb->framenr);
213
214         marker->flag = cb->marker_flag;
215
216         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, NULL);
217 }
218
219 static void marker_block_handler(bContext *C, void *arg_cb, int event)
220 {
221         MarkerUpdateCb *cb = (MarkerUpdateCb*) arg_cb;
222         MovieTrackingMarker *marker;
223         int width, height, ok = FALSE;
224
225         BKE_movieclip_get_size(cb->clip, cb->user, &width, &height);
226
227         marker = BKE_tracking_ensure_marker(cb->track, cb->framenr);
228
229         if (event == B_MARKER_POS) {
230                 marker->pos[0] = cb->marker_pos[0]/width;
231                 marker->pos[1] = cb->marker_pos[1]/height;
232
233                 /* to update position of "parented" objects */
234                 DAG_id_tag_update(&cb->clip->id, 0);
235                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, NULL);
236
237                 ok = TRUE;
238         }
239         else if (event == B_MARKER_PAT_DIM) {
240                 float dim[2], pat_dim[2];
241
242                 sub_v2_v2v2(pat_dim, cb->track->pat_max, cb->track->pat_min);
243
244                 dim[0] = cb->track_pat[0] / width;
245                 dim[1] = cb->track_pat[1] / height;
246
247                 sub_v2_v2(dim, pat_dim);
248                 mul_v2_fl(dim, 0.5f);
249
250                 cb->track->pat_min[0] -= dim[0];
251                 cb->track->pat_min[1] -= dim[1];
252
253                 cb->track->pat_max[0] += dim[0];
254                 cb->track->pat_max[1] += dim[1];
255
256                 BKE_tracking_clamp_track(cb->track, CLAMP_PAT_DIM);
257
258                 ok = TRUE;
259         }
260         else if (event == B_MARKER_SEARCH_POS) {
261                 float delta[2], side[2];
262
263                 sub_v2_v2v2(side, cb->track->search_max, cb->track->search_min);
264                 mul_v2_fl(side, 0.5f);
265
266                 delta[0] = cb->track_search_pos[0] / width;
267                 delta[1] = cb->track_search_pos[1] / height;
268
269                 sub_v2_v2v2(cb->track->search_min, delta, side);
270                 add_v2_v2v2(cb->track->search_max, delta, side);
271
272                 BKE_tracking_clamp_track(cb->track, CLAMP_SEARCH_POS);
273
274                 ok = TRUE;
275         }
276         else if (event == B_MARKER_SEARCH_DIM) {
277                 float dim[2], search_dim[2];
278
279                 sub_v2_v2v2(search_dim, cb->track->search_max, cb->track->search_min);
280
281                 dim[0] = cb->track_search[0]/width;
282                 dim[1] = cb->track_search[1]/height;
283
284                 sub_v2_v2(dim, search_dim);
285                 mul_v2_fl(dim, 0.5f);
286
287                 cb->track->search_min[0]-= dim[0];
288                 cb->track->search_min[1]-= dim[1];
289
290                 cb->track->search_max[0]+= dim[0];
291                 cb->track->search_max[1]+= dim[1];
292
293                 BKE_tracking_clamp_track(cb->track, CLAMP_SEARCH_DIM);
294
295                 ok = TRUE;
296         }
297         else if (event == B_MARKER_FLAG) {
298                 marker->flag = cb->marker_flag;
299
300                 ok = TRUE;
301         }
302         else if (event == B_MARKER_OFFSET) {
303                 float offset[2], delta[2];
304                 int i;
305
306                 offset[0] = cb->track_offset[0] / width;
307                 offset[1] = cb->track_offset[1] / height;
308
309                 sub_v2_v2v2(delta, offset, cb->track->offset);
310                 copy_v2_v2(cb->track->offset, offset);
311
312                 for (i = 0; i < cb->track->markersnr; i++)
313                         sub_v2_v2(cb->track->markers[i].pos, delta);
314
315                 /* to update position of "parented" objects */
316                 DAG_id_tag_update(&cb->clip->id, 0);
317                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, NULL);
318
319                 ok = TRUE;
320         }
321
322         if (ok)
323                 WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, cb->clip);
324 }
325
326 void uiTemplateMarker(uiLayout *layout, PointerRNA *ptr, const char *propname, PointerRNA *userptr, PointerRNA *trackptr, int compact)
327 {
328         PropertyRNA *prop;
329         uiBlock *block;
330         uiBut *bt;
331         PointerRNA clipptr;
332         MovieClip *clip;
333         MovieClipUser *user;
334         MovieTrackingTrack *track;
335         MovieTrackingMarker *marker;
336         MarkerUpdateCb *cb;
337         const char *tip;
338
339         if (!ptr->data)
340                 return;
341
342         prop = RNA_struct_find_property(ptr, propname);
343         if (!prop) {
344                 printf("%s: property not found: %s.%s\n",
345                        __func__, RNA_struct_identifier(ptr->type), propname);
346                 return;
347         }
348
349         if (RNA_property_type(prop) != PROP_POINTER) {
350                 printf("%s: expected pointer property for %s.%s\n",
351                        __func__, RNA_struct_identifier(ptr->type), propname);
352                 return;
353         }
354
355         clipptr = RNA_property_pointer_get(ptr, prop);
356         clip = (MovieClip *)clipptr.data;
357         user = userptr->data;
358         track = trackptr->data;
359
360         marker = BKE_tracking_get_marker(track, user->framenr);
361
362         cb = MEM_callocN(sizeof(MarkerUpdateCb), "uiTemplateMarker update_cb");
363         cb->compact = compact;
364         cb->clip = clip;
365         cb->user = user;
366         cb->track = track;
367         cb->marker_flag = marker->flag;
368         cb->framenr = user->framenr;
369
370         if (compact) {
371                 block = uiLayoutGetBlock(layout);
372
373                 if (cb->marker_flag & MARKER_DISABLED)
374                         tip= "Marker is disabled at current frame";
375                 else
376                         tip= "Marker is enabled at current frame";
377
378                 bt = uiDefIconButBitI(block, TOGN, MARKER_DISABLED, 0, ICON_RESTRICT_VIEW_OFF, 0, 0, 20, 20, &cb->marker_flag, 0, 0, 1, 0, tip);
379                 uiButSetNFunc(bt, marker_update_cb, cb, NULL);
380         }
381         else {
382                 int width, height, step, digits;
383                 float pat_dim[2], pat_pos[2], search_dim[2], search_pos[2];
384                 uiLayout *col;
385
386                 BKE_movieclip_get_size(clip, user, &width, &height);
387
388                 if (track->flag & TRACK_LOCKED) {
389                         uiLayoutSetActive(layout, 0);
390                         block = uiLayoutAbsoluteBlock(layout);
391                         uiDefBut(block, LABEL, 0, "Track is locked", 0, 0, 300, 19, NULL, 0, 0, 0, 0, "");
392
393                         return;
394                 }
395
396                 step= 100;
397                 digits = 2;
398
399                 sub_v2_v2v2(pat_dim, track->pat_max, track->pat_min);
400                 sub_v2_v2v2(search_dim, track->search_max, track->search_min);
401
402                 add_v2_v2v2(search_pos, track->search_max, track->search_min);
403                 mul_v2_fl(search_pos, 0.5);
404
405                 add_v2_v2v2(pat_pos, track->pat_max, track->pat_min);
406                 mul_v2_fl(pat_pos, 0.5);
407
408                 to_pixel_space(cb->marker_pos, marker->pos, width, height);
409                 to_pixel_space(cb->track_pat, pat_dim, width, height);
410                 to_pixel_space(cb->track_search, search_dim, width, height);
411                 to_pixel_space(cb->track_search_pos, search_pos, width, height);
412                 to_pixel_space(cb->track_offset, track->offset, width, height);
413
414                 cb->marker_flag = marker->flag;
415
416                 block= uiLayoutAbsoluteBlock(layout);
417                 uiBlockSetHandleFunc(block, marker_block_handler, cb);
418                 uiBlockSetNFunc(block, marker_update_cb, cb, NULL);
419
420                 if (cb->marker_flag & MARKER_DISABLED)
421                         tip= "Marker is disabled at current frame";
422                 else
423                         tip= "Marker is enabled at current frame";
424
425                 uiDefButBitI(block, OPTIONN, MARKER_DISABLED, B_MARKER_FLAG,  "Enabled", 10, 190, 145, 19, &cb->marker_flag,
426                         0, 0, 0, 0, tip);
427
428                 col = uiLayoutColumn(layout, 1);
429                 uiLayoutSetActive(col, (cb->marker_flag&MARKER_DISABLED)==0);
430
431                 block = uiLayoutAbsoluteBlock(col);
432                 uiBlockBeginAlign(block);
433
434                 uiDefBut(block, LABEL, 0, "Position:", 0, 190, 300, 19, NULL, 0, 0, 0, 0, "");
435                 uiDefButF(block, NUM, B_MARKER_POS, "X:", 10, 171, 145, 19, &cb->marker_pos[0],
436                         -10*width, 10.0*width, step, digits, "X-position of marker at frame in screen coordinates");
437                 uiDefButF(block, NUM, B_MARKER_POS, "Y:", 165, 171, 145, 19, &cb->marker_pos[1],
438                         -10*height, 10.0*height, step, digits, "Y-position of marker at frame in screen coordinates");
439
440                 uiDefBut(block, LABEL, 0, "Offset:", 0, 152, 300, 19, NULL, 0, 0, 0, 0, "");
441                 uiDefButF(block, NUM, B_MARKER_OFFSET, "X:", 10, 133, 145, 19, &cb->track_offset[0],
442                         -10*width, 10.0*width, step, digits, "X-offset to parenting point");
443                 uiDefButF(block, NUM, B_MARKER_OFFSET, "Y:", 165, 133, 145, 19, &cb->track_offset[1],
444                         -10*height, 10.0*height, step, digits, "Y-offset to parenting point");
445
446                 uiDefBut(block, LABEL, 0, "Pattern Area:", 0, 114, 300, 19, NULL, 0, 0, 0, 0, "");
447                 uiDefButF(block, NUM, B_MARKER_PAT_DIM, "Width:", 10, 95, 300, 19, &cb->track_pat[0], 3.0f,
448                         10.0*width, step, digits, "Width of marker's pattern in screen coordinates");
449                 uiDefButF(block, NUM, B_MARKER_PAT_DIM, "Height:", 10, 76, 300, 19, &cb->track_pat[1], 3.0f,
450                         10.0*height, step, digits, "Height of marker's pattern in screen coordinates");
451
452                 uiDefBut(block, LABEL, 0, "Search Area:", 0, 57, 300, 19, NULL, 0, 0, 0, 0, "");
453                 uiDefButF(block, NUM, B_MARKER_SEARCH_POS, "X:", 10, 38, 145, 19, &cb->track_search_pos[0],
454                         -width, width, step, digits, "X-position of search at frame relative to marker's position");
455                 uiDefButF(block, NUM, B_MARKER_SEARCH_POS, "Y:", 165, 38, 145, 19, &cb->track_search_pos[1],
456                         -height, height, step, digits, "X-position of search at frame relative to marker's position");
457                 uiDefButF(block, NUM, B_MARKER_SEARCH_DIM, "Width:", 10, 19, 300, 19, &cb->track_search[0], 3.0f,
458                         10.0*width, step, digits, "Width of marker's search in screen soordinates");
459                 uiDefButF(block, NUM, B_MARKER_SEARCH_DIM, "Height:", 10, 0, 300, 19, &cb->track_search[1], 3.0f,
460                         10.0*height, step, digits, "Height of marker's search in screen soordinates");
461
462                 uiBlockEndAlign(block);
463         }
464 }