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