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