Merging r39634 through r39716 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                 cb->track->offset[0]= cb->track_offset[0]/width;
292                 cb->track->offset[1]= cb->track_offset[1]/height;
293
294                 /* to update position of "parented" objects */
295                 DAG_id_tag_update(&cb->clip->id, 0);
296                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, NULL);
297
298                 ok= 1;
299         }
300
301         if(ok)
302                 WM_event_add_notifier(C, NC_MOVIECLIP|NA_EDITED, cb->clip);
303 }
304
305 void uiTemplateMarker(uiLayout *layout, PointerRNA *ptr, const char *propname, PointerRNA *userptr, PointerRNA *trackptr, int compact)
306 {
307         PropertyRNA *prop;
308         uiBlock *block;
309         uiBut *bt;
310         PointerRNA clipptr;
311         MovieClip *clip;
312         MovieClipUser *user;
313         MovieTrackingTrack *track;
314         MovieTrackingMarker *marker;
315         MarkerUpdateCb *cb;
316
317         if(!ptr->data)
318                 return;
319
320         prop= RNA_struct_find_property(ptr, propname);
321         if(!prop) {
322                 printf("uiTemplateMarker: property not found: %s.%s\n", RNA_struct_identifier(ptr->type), propname);
323                 return;
324         }
325
326         if(RNA_property_type(prop) != PROP_POINTER) {
327                 printf("uiTemplateMarker: expected pointer property for %s.%s\n", RNA_struct_identifier(ptr->type), propname);
328                 return;
329         }
330
331         clipptr= RNA_property_pointer_get(ptr, prop);
332         clip= (MovieClip *)clipptr.data;
333         user= userptr->data;
334         track= trackptr->data;
335
336         marker= BKE_tracking_get_marker(track, user->framenr);
337
338         cb= MEM_callocN(sizeof(MarkerUpdateCb), "uiTemplateMarker update_cb");
339         cb->compact= compact;
340         cb->clip= clip;
341         cb->user= user;
342         cb->track= track;
343         cb->marker_flag= marker->flag;
344         cb->framenr= user->framenr;
345
346         if(compact) {
347                 block= uiLayoutGetBlock(layout);
348
349                 bt= uiDefIconButBitI(block, TOGN, MARKER_DISABLED, 0, ICON_MUTE_IPO_OFF, 0, 0, 20, 20, &cb->marker_flag, 0, 0, 1, 0, "Marker is disabled for current frame.");
350                 uiButSetNFunc(bt, marker_update_cb, cb, NULL);
351         } else {
352                 int width, height, step, digits;
353                 float pat_dim[2], pat_pos[2], search_dim[2], search_pos[2];
354                 uiLayout *col;
355
356                 BKE_movieclip_acquire_size(clip, user, &width, &height);
357
358                 if(track->flag&TRACK_LOCKED) {
359                         uiLayoutSetActive(layout, 0);
360                         block= uiLayoutAbsoluteBlock(layout);
361                         uiDefBut(block, LABEL, 0, "Track is locked", 0, 0, 300, 19, NULL, 0, 0, 0, 0, "");
362
363                         return;
364                 }
365
366                 step= 100;
367                 digits= 2;
368
369                 sub_v2_v2v2(pat_dim, track->pat_max, track->pat_min);
370                 sub_v2_v2v2(search_dim, track->search_max, track->search_min);
371
372                 add_v2_v2v2(search_pos, track->search_max, track->search_min);
373                 mul_v2_fl(search_pos, 0.5);
374
375                 add_v2_v2v2(pat_pos, track->pat_max, track->pat_min);
376                 mul_v2_fl(pat_pos, 0.5);
377
378                 to_pixel_space(cb->marker_pos, marker->pos, width, height);
379                 to_pixel_space(cb->track_pat, pat_dim, width, height);
380                 to_pixel_space(cb->track_search, search_dim, width, height);
381                 to_pixel_space(cb->track_search_pos, search_pos, width, height);
382                 to_pixel_space(cb->track_offset, track->offset, width, height);
383
384                 cb->marker_flag= marker->flag;
385
386                 block= uiLayoutAbsoluteBlock(layout);
387                 uiBlockSetHandleFunc(block, marker_block_handler, cb);
388                 uiBlockSetNFunc(block, marker_update_cb, cb, NULL);
389
390                 uiDefButBitI(block, OPTIONN, MARKER_DISABLED, B_MARKER_FLAG,  "Enabled", 10, 190, 145, 19, &cb->marker_flag,
391                         0, 0, 0, 0, "Marker is disabled for current frame.");
392
393                 col= uiLayoutColumn(layout, 1);
394                 uiLayoutSetActive(col, (cb->marker_flag&MARKER_DISABLED)==0);
395
396                 block= uiLayoutAbsoluteBlock(col);
397                 uiBlockBeginAlign(block);
398
399                 uiDefBut(block, LABEL, 0, "Position:", 0, 190, 300, 19, NULL, 0, 0, 0, 0, "");
400                 uiDefButF(block, NUM, B_MARKER_POS, "X:", 10, 171, 145, 19, &cb->marker_pos[0],
401                         -10*width, 10.0*width, step, digits, "X-position of marker at frame in screen coordinates.");
402                 uiDefButF(block, NUM, B_MARKER_POS, "Y:", 165, 171, 145, 19, &cb->marker_pos[1],
403                         -10*height, 10.0*height, step, digits, "Y-position of marker at frame in screen coordinates.");
404
405                 uiDefBut(block, LABEL, 0, "Offset:", 0, 152, 300, 19, NULL, 0, 0, 0, 0, "");
406                 uiDefButF(block, NUM, B_MARKER_OFFSET, "X:", 10, 133, 145, 19, &cb->track_offset[0],
407                         -10*width, 10.0*width, step, digits, "X-offset to parenting point.");
408                 uiDefButF(block, NUM, B_MARKER_OFFSET, "Y:", 165, 133, 145, 19, &cb->track_offset[1],
409                         -10*height, 10.0*height, step, digits, "Y-offset to parenting point.");
410
411                 uiDefBut(block, LABEL, 0, "Pattern Area:", 0, 114, 300, 19, NULL, 0, 0, 0, 0, "");
412                 uiDefButF(block, NUM, B_MARKER_PAT_DIM, "Width:", 10, 95, 300, 19, &cb->track_pat[0], 3.0f,
413                         10.0*width, step, digits, "Width of marker's pattern in screen soordinates.");
414                 uiDefButF(block, NUM, B_MARKER_PAT_DIM, "Height:", 10, 76, 300, 19, &cb->track_pat[1], 3.0f,
415                         10.0*height, step, digits, "Height of marker's pattern in screen soordinates.");
416
417                 uiDefBut(block, LABEL, 0, "Search Area:", 0, 57, 300, 19, NULL, 0, 0, 0, 0, "");
418                 uiDefButF(block, NUM, B_MARKER_SEARCH_POS, "X:", 10, 38, 145, 19, &cb->track_search_pos[0],
419                         -width, width, step, digits, "X-position of search at frame relative to marker's position");
420                 uiDefButF(block, NUM, B_MARKER_SEARCH_POS, "Y:", 165, 38, 145, 19, &cb->track_search_pos[1],
421                         -height, height, step, digits, "X-position of search at frame relative to marker's position");
422                 uiDefButF(block, NUM, B_MARKER_SEARCH_DIM, "Width:", 10, 19, 300, 19, &cb->track_search[0], 3.0f,
423                         10.0*width, step, digits, "Width of marker's search in screen soordinates.");
424                 uiDefButF(block, NUM, B_MARKER_SEARCH_DIM, "Height:", 10, 0, 300, 19, &cb->track_search[1], 3.0f,
425                         10.0*height, step, digits, "Height of marker's search in screen soordinates.");
426
427                 uiBlockEndAlign(block);
428         }
429 }