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