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