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