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