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