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