Merging r40612 through r40614 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_clip / clip_buttons.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2011 Blender Foundation.
21  * All rights reserved.
22  *
23  *
24  * Contributor(s): Blender Foundation,
25  *                 Sergey Sharybin
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/editors/space_clip/clip_buttons.c
31  *  \ingroup spclip
32  */
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_space_types.h"
42
43 #include "BLI_math.h"
44 #include "BLI_utildefines.h"
45 #include "BLI_listbase.h"
46
47 #include "BKE_context.h"
48 #include "BKE_depsgraph.h"
49 #include "BKE_screen.h"
50 #include "BKE_movieclip.h"
51 #include "BKE_tracking.h"
52
53 #include "ED_clip.h"
54 #include "ED_gpencil.h"
55
56 #include "UI_interface.h"
57 #include "UI_resources.h"
58
59 #include "RNA_access.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63
64 #include "clip_intern.h"        // own include
65
66 /* Panels */
67
68 void ED_clip_buttons_register(ARegionType *art)
69 {
70         PanelType *pt;
71
72         pt= MEM_callocN(sizeof(PanelType), "spacetype clip panel gpencil");
73         strcpy(pt->idname, "CLIP_PT_gpencil");
74         strcpy(pt->label, "Grease Pencil");
75         pt->draw= gpencil_panel_standard;
76         pt->flag|= PNL_DEFAULT_CLOSED;
77         BLI_addtail(&art->paneltypes, pt);
78 }
79
80 /********************* MovieClip Template ************************/
81
82 void uiTemplateMovieClip(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, int compact)
83 {
84         PropertyRNA *prop;
85         PointerRNA clipptr;
86         MovieClip *clip;
87         uiLayout *row, *split;
88         uiBlock *block;
89
90         if(!ptr->data)
91                 return;
92
93         prop= RNA_struct_find_property(ptr, propname);
94         if(!prop) {
95                 printf("uiTemplateMovieClip: property not found: %s.%s\n", RNA_struct_identifier(ptr->type), propname);
96                 return;
97         }
98
99         if(RNA_property_type(prop) != PROP_POINTER) {
100                 printf("uiTemplateMovieClip: expected pointer property for %s.%s\n", 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("uiTemplateTrack: property not found: %s.%s\n", RNA_struct_identifier(ptr->type), propname);
142                 return;
143         }
144
145         if(RNA_property_type(prop) != PROP_POINTER) {
146                 printf("uiTemplateTrack: expected pointer property for %s.%s\n", RNA_struct_identifier(ptr->type), propname);
147                 return;
148         }
149
150         scopesptr= RNA_property_pointer_get(ptr, prop);
151         scopes= (MovieClipScopes *)scopesptr.data;
152
153         rect.xmin= 0; rect.xmax= 200;
154         rect.ymin= 0; rect.ymax= 120;
155
156         block= uiLayoutAbsoluteBlock(layout);
157
158         scopes->track_preview_height= (scopes->track_preview_height<=UI_UNIT_Y)?UI_UNIT_Y:scopes->track_preview_height;
159
160         uiDefBut(block, TRACKPREVIEW, 0, "", rect.xmin, rect.ymin, rect.xmax-rect.xmin, scopes->track_preview_height, scopes, 0, 0, 0, 0, "");
161 }
162
163 /********************* Marker Template ************************/
164
165 #define B_MARKER_POS                    3
166 #define B_MARKER_OFFSET                 4
167 #define B_MARKER_PAT_DIM                5
168 #define B_MARKER_SEARCH_POS             6
169 #define B_MARKER_SEARCH_DIM             7
170 #define B_MARKER_FLAG                   8
171
172 typedef struct {
173         int compact;                                                            /* compact mode */
174
175         MovieClip *clip;
176         MovieClipUser *user;                                            /* user of clip */
177         MovieTrackingTrack *track;
178
179         int framenr;                                                            /* current frame number */
180         float marker_pos[2];                                            /* position of marker in pixel coords */
181         float track_pat[2];                                                     /* position and dimensions of marker pattern in pixel coords */
182         float track_offset[2];                                          /* offset of "parenting" point */
183         float track_search_pos[2], track_search[2];     /* position and dimensions of marker search in pixel coords */
184         int marker_flag;                                                        /* marker's flags */
185 } MarkerUpdateCb;
186
187 static void to_pixel_space(float r[2], float a[2], int width, int height)
188 {
189         copy_v2_v2(r, a);
190         r[0]*= width;
191         r[1]*= height;
192 }
193
194 static void marker_update_cb(bContext *C, void *arg_cb, void *UNUSED(arg))
195 {
196         MarkerUpdateCb *cb= (MarkerUpdateCb*) arg_cb;
197         MovieTrackingMarker *marker;
198
199         if(!cb->compact)
200                 return;
201
202         marker= BKE_tracking_ensure_marker(cb->track, cb->framenr);
203
204         marker->flag= cb->marker_flag;
205
206         WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, NULL);
207 }
208
209 static void marker_block_handler(bContext *C, void *arg_cb, int event)
210 {
211         MarkerUpdateCb *cb= (MarkerUpdateCb*) arg_cb;
212         MovieTrackingMarker *marker;
213         int width, height, ok= 0;
214
215         BKE_movieclip_acquire_size(cb->clip, cb->user, &width, &height);
216
217         marker= BKE_tracking_ensure_marker(cb->track, cb->framenr);
218
219         if(event==B_MARKER_POS) {
220                 marker->pos[0]= cb->marker_pos[0]/width;
221                 marker->pos[1]= cb->marker_pos[1]/height;
222
223                 /* to update position of "parented" objects */
224                 DAG_id_tag_update(&cb->clip->id, 0);
225                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, NULL);
226
227                 ok= 1;
228         }
229         else if(event==B_MARKER_PAT_DIM) {
230                 float dim[2], pat_dim[2];
231
232                 sub_v2_v2v2(pat_dim, cb->track->pat_max, cb->track->pat_min);
233
234                 dim[0]= cb->track_pat[0]/width;
235                 dim[1]= cb->track_pat[1]/height;
236
237                 sub_v2_v2(dim, pat_dim);
238                 mul_v2_fl(dim, 0.5f);
239
240                 cb->track->pat_min[0]-= dim[0];
241                 cb->track->pat_min[1]-= dim[1];
242
243                 cb->track->pat_max[0]+= dim[0];
244                 cb->track->pat_max[1]+= dim[1];
245
246                 BKE_tracking_clamp_track(cb->track, CLAMP_PAT_DIM);
247
248                 ok= 1;
249         }
250         else if(event==B_MARKER_SEARCH_POS) {
251                 float delta[2], side[2];
252
253                 sub_v2_v2v2(side, cb->track->search_max, cb->track->search_min);
254                 mul_v2_fl(side, 0.5f);
255
256                 delta[0]= cb->track_search_pos[0]/width;
257                 delta[1]= cb->track_search_pos[1]/height;
258
259                 sub_v2_v2v2(cb->track->search_min, delta, side);
260                 add_v2_v2v2(cb->track->search_max, delta, side);
261
262                 BKE_tracking_clamp_track(cb->track, CLAMP_SEARCH_POS);
263
264                 ok= 1;
265         }
266         else if(event==B_MARKER_SEARCH_DIM) {
267                 float dim[2], search_dim[2];
268
269                 sub_v2_v2v2(search_dim, cb->track->search_max, cb->track->search_min);
270
271                 dim[0]= cb->track_search[0]/width;
272                 dim[1]= cb->track_search[1]/height;
273
274                 sub_v2_v2(dim, search_dim);
275                 mul_v2_fl(dim, 0.5f);
276
277                 cb->track->search_min[0]-= dim[0];
278                 cb->track->search_min[1]-= dim[1];
279
280                 cb->track->search_max[0]+= dim[0];
281                 cb->track->search_max[1]+= dim[1];
282
283                 BKE_tracking_clamp_track(cb->track, CLAMP_SEARCH_DIM);
284
285                 ok= 1;
286         } else if(event==B_MARKER_FLAG) {
287                 marker->flag= cb->marker_flag;
288
289                 ok= 1;
290         } else if(event==B_MARKER_OFFSET) {
291                 float offset[2], delta[2];
292                 int i;
293
294                 offset[0]= cb->track_offset[0]/width;
295                 offset[1]= cb->track_offset[1]/height;
296
297                 sub_v2_v2v2(delta, offset, cb->track->offset);
298                 copy_v2_v2(cb->track->offset, offset);
299
300                 for(i=0; i<cb->track->markersnr; i++)
301                         sub_v2_v2(cb->track->markers[i].pos, delta);
302
303                 /* to update position of "parented" objects */
304                 DAG_id_tag_update(&cb->clip->id, 0);
305                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, NULL);
306
307                 ok= 1;
308         }
309
310         if(ok)
311                 WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, cb->clip);
312 }
313
314 void uiTemplateMarker(uiLayout *layout, PointerRNA *ptr, const char *propname, PointerRNA *userptr, PointerRNA *trackptr, int compact)
315 {
316         PropertyRNA *prop;
317         uiBlock *block;
318         uiBut *bt;
319         PointerRNA clipptr;
320         MovieClip *clip;
321         MovieClipUser *user;
322         MovieTrackingTrack *track;
323         MovieTrackingMarker *marker;
324         MarkerUpdateCb *cb;
325
326         if(!ptr->data)
327                 return;
328
329         prop= RNA_struct_find_property(ptr, propname);
330         if(!prop) {
331                 printf("uiTemplateMarker: property not found: %s.%s\n", RNA_struct_identifier(ptr->type), propname);
332                 return;
333         }
334
335         if(RNA_property_type(prop) != PROP_POINTER) {
336                 printf("uiTemplateMarker: expected pointer property for %s.%s\n", RNA_struct_identifier(ptr->type), propname);
337                 return;
338         }
339
340         clipptr= RNA_property_pointer_get(ptr, prop);
341         clip= (MovieClip *)clipptr.data;
342         user= userptr->data;
343         track= trackptr->data;
344
345         marker= BKE_tracking_get_marker(track, user->framenr);
346
347         cb= MEM_callocN(sizeof(MarkerUpdateCb), "uiTemplateMarker update_cb");
348         cb->compact= compact;
349         cb->clip= clip;
350         cb->user= user;
351         cb->track= track;
352         cb->marker_flag= marker->flag;
353         cb->framenr= user->framenr;
354
355         if(compact) {
356                 block= uiLayoutGetBlock(layout);
357
358                 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.");
359                 uiButSetNFunc(bt, marker_update_cb, cb, NULL);
360         } else {
361                 int width, height, step, digits;
362                 float pat_dim[2], pat_pos[2], search_dim[2], search_pos[2];
363                 uiLayout *col;
364
365                 BKE_movieclip_acquire_size(clip, user, &width, &height);
366
367                 if(track->flag&TRACK_LOCKED) {
368                         uiLayoutSetActive(layout, 0);
369                         block= uiLayoutAbsoluteBlock(layout);
370                         uiDefBut(block, LABEL, 0, "Track is locked", 0, 0, 300, 19, NULL, 0, 0, 0, 0, "");
371
372                         return;
373                 }
374
375                 step= 100;
376                 digits= 2;
377
378                 sub_v2_v2v2(pat_dim, track->pat_max, track->pat_min);
379                 sub_v2_v2v2(search_dim, track->search_max, track->search_min);
380
381                 add_v2_v2v2(search_pos, track->search_max, track->search_min);
382                 mul_v2_fl(search_pos, 0.5);
383
384                 add_v2_v2v2(pat_pos, track->pat_max, track->pat_min);
385                 mul_v2_fl(pat_pos, 0.5);
386
387                 to_pixel_space(cb->marker_pos, marker->pos, width, height);
388                 to_pixel_space(cb->track_pat, pat_dim, width, height);
389                 to_pixel_space(cb->track_search, search_dim, width, height);
390                 to_pixel_space(cb->track_search_pos, search_pos, width, height);
391                 to_pixel_space(cb->track_offset, track->offset, width, height);
392
393                 cb->marker_flag= marker->flag;
394
395                 block= uiLayoutAbsoluteBlock(layout);
396                 uiBlockSetHandleFunc(block, marker_block_handler, cb);
397                 uiBlockSetNFunc(block, marker_update_cb, cb, NULL);
398
399                 uiDefButBitI(block, OPTIONN, MARKER_DISABLED, B_MARKER_FLAG,  "Enabled", 10, 190, 145, 19, &cb->marker_flag,
400                         0, 0, 0, 0, "Marker is disabled for current frame.");
401
402                 col= uiLayoutColumn(layout, 1);
403                 uiLayoutSetActive(col, (cb->marker_flag&MARKER_DISABLED)==0);
404
405                 block= uiLayoutAbsoluteBlock(col);
406                 uiBlockBeginAlign(block);
407
408                 uiDefBut(block, LABEL, 0, "Position:", 0, 190, 300, 19, NULL, 0, 0, 0, 0, "");
409                 uiDefButF(block, NUM, B_MARKER_POS, "X:", 10, 171, 145, 19, &cb->marker_pos[0],
410                         -10*width, 10.0*width, step, digits, "X-position of marker at frame in screen coordinates.");
411                 uiDefButF(block, NUM, B_MARKER_POS, "Y:", 165, 171, 145, 19, &cb->marker_pos[1],
412                         -10*height, 10.0*height, step, digits, "Y-position of marker at frame in screen coordinates.");
413
414                 uiDefBut(block, LABEL, 0, "Offset:", 0, 152, 300, 19, NULL, 0, 0, 0, 0, "");
415                 uiDefButF(block, NUM, B_MARKER_OFFSET, "X:", 10, 133, 145, 19, &cb->track_offset[0],
416                         -10*width, 10.0*width, step, digits, "X-offset to parenting point.");
417                 uiDefButF(block, NUM, B_MARKER_OFFSET, "Y:", 165, 133, 145, 19, &cb->track_offset[1],
418                         -10*height, 10.0*height, step, digits, "Y-offset to parenting point.");
419
420                 uiDefBut(block, LABEL, 0, "Pattern Area:", 0, 114, 300, 19, NULL, 0, 0, 0, 0, "");
421                 uiDefButF(block, NUM, B_MARKER_PAT_DIM, "Width:", 10, 95, 300, 19, &cb->track_pat[0], 3.0f,
422                         10.0*width, step, digits, "Width of marker's pattern in screen soordinates.");
423                 uiDefButF(block, NUM, B_MARKER_PAT_DIM, "Height:", 10, 76, 300, 19, &cb->track_pat[1], 3.0f,
424                         10.0*height, step, digits, "Height of marker's pattern in screen soordinates.");
425
426                 uiDefBut(block, LABEL, 0, "Search Area:", 0, 57, 300, 19, NULL, 0, 0, 0, 0, "");
427                 uiDefButF(block, NUM, B_MARKER_SEARCH_POS, "X:", 10, 38, 145, 19, &cb->track_search_pos[0],
428                         -width, width, step, digits, "X-position of search at frame relative to marker's position");
429                 uiDefButF(block, NUM, B_MARKER_SEARCH_POS, "Y:", 165, 38, 145, 19, &cb->track_search_pos[1],
430                         -height, height, step, digits, "X-position of search at frame relative to marker's position");
431                 uiDefButF(block, NUM, B_MARKER_SEARCH_DIM, "Width:", 10, 19, 300, 19, &cb->track_search[0], 3.0f,
432                         10.0*width, step, digits, "Width of marker's search in screen soordinates.");
433                 uiDefButF(block, NUM, B_MARKER_SEARCH_DIM, "Height:", 10, 0, 300, 19, &cb->track_search[1], 3.0f,
434                         10.0*height, step, digits, "Height of marker's search in screen soordinates.");
435
436                 uiBlockEndAlign(block);
437         }
438 }