doxygen: blender/editors tagged.
[blender.git] / source / blender / editors / space_graph / graph_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) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation, Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_graph/graph_buttons.c
30  *  \ingroup spgraph
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <math.h>
37 #include <float.h>
38
39 #include "DNA_anim_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_math.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_editVert.h"
48 #include "BLI_rand.h"
49 #include "BLI_utildefines.h"
50
51 #include "BKE_context.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_fcurve.h"
54 #include "BKE_main.h"
55 #include "BKE_screen.h"
56
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "RNA_access.h"
62
63 #include "ED_anim_api.h"
64 #include "ED_keyframing.h"
65 #include "ED_screen.h"
66
67 #include "UI_interface.h"
68 #include "UI_resources.h"
69
70 #include "graph_intern.h"       // own include
71
72 /* XXX */
73
74 /* temporary definition for limits of float number buttons (FLT_MAX tends to infinity with old system) */
75 #define UI_FLT_MAX      10000.0f
76
77
78 /* ******************* graph editor space & buttons ************** */
79
80 #define B_NOP           1
81 #define B_REDR          2
82
83 /* -------------- */
84
85 static void do_graph_region_buttons(bContext *UNUSED(C), void *UNUSED(arg), int event)
86 {
87         //Scene *scene= CTX_data_scene(C);
88         
89         switch(event) {
90
91         }
92         
93         /* default for now */
94         //WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
95 }
96
97 /* -------------- */
98
99 static int graph_panel_context(const bContext *C, bAnimListElem **ale, FCurve **fcu)
100 {
101         bAnimContext ac;
102         bAnimListElem *elem= NULL;
103         
104         /* for now, only draw if we could init the anim-context info (necessary for all animation-related tools) 
105          * to work correctly is able to be correctly retrieved. There's no point showing empty panels?
106          */
107         if (ANIM_animdata_get_context(C, &ac) == 0) 
108                 return 0;
109         
110         /* try to find 'active' F-Curve */
111         elem= get_active_fcurve_channel(&ac);
112         if(elem == NULL) 
113                 return 0;
114         
115         if(fcu)
116                 *fcu= (FCurve*)elem->data;
117         if(ale)
118                 *ale= elem;
119         else
120                 MEM_freeN(elem);
121         
122         return 1;
123 }
124
125 static int graph_panel_poll(const bContext *C, PanelType *UNUSED(pt))
126 {
127         return graph_panel_context(C, NULL, NULL);
128 }
129
130 /* -------------- */
131
132 /* Graph Editor View Settings */
133 static void graph_panel_view(const bContext *C, Panel *pa)
134 {
135         bScreen *sc= CTX_wm_screen(C);
136         SpaceIpo *sipo= CTX_wm_space_graph(C);
137         Scene *scene= CTX_data_scene(C);
138         PointerRNA spaceptr, sceneptr;
139         uiLayout *col, *subcol, *row;
140         
141         /* get RNA pointers for use when creating the UI elements */
142         RNA_id_pointer_create(&scene->id, &sceneptr);
143         RNA_pointer_create(&sc->id, &RNA_SpaceGraphEditor, sipo, &spaceptr);
144
145         /* 2D-Cursor */
146         col= uiLayoutColumn(pa->layout, 0);
147                 uiItemR(col, &spaceptr, "show_cursor", 0, NULL, ICON_NONE);
148                 
149                 subcol= uiLayoutColumn(col, 1);
150                 uiLayoutSetActive(subcol, RNA_boolean_get(&spaceptr, "show_cursor")); 
151                         uiItemO(subcol, "Cursor from Selection", ICON_NONE, "GRAPH_OT_frame_jump");
152                 
153                 subcol= uiLayoutColumn(col, 1);
154                 uiLayoutSetActive(subcol, RNA_boolean_get(&spaceptr, "show_cursor")); 
155                         row= uiLayoutSplit(subcol, 0.7, 1);
156                                 uiItemR(row, &sceneptr, "frame_current", 0, "Cursor X", ICON_NONE);
157                                 uiItemEnumO(row, "GRAPH_OT_snap", "To Keys", 0, "type", GRAPHKEYS_SNAP_CFRA);
158                         row= uiLayoutSplit(subcol, 0.7, 1);
159                                 uiItemR(row, &spaceptr, "cursor_position_y", 0, "Cursor Y", ICON_NONE);
160                                 uiItemEnumO(row, "GRAPH_OT_snap", "To Keys", 0, "type", GRAPHKEYS_SNAP_VALUE);
161 }
162
163 /* ******************* active F-Curve ************** */
164
165 static void graph_panel_properties(const bContext *C, Panel *pa)
166 {
167         bAnimListElem *ale;
168         FCurve *fcu;
169         PointerRNA fcu_ptr;
170         uiLayout *layout = pa->layout;
171         uiLayout *col, *row, *subrow;
172         uiBlock *block;
173         char name[256];
174         int icon = 0;
175
176         if (!graph_panel_context(C, &ale, &fcu))
177                 return;
178         
179         block= uiLayoutGetBlock(layout);
180         uiBlockSetHandleFunc(block, do_graph_region_buttons, NULL);
181         
182         /* F-Curve pointer */
183         RNA_pointer_create(ale->id, &RNA_FCurve, fcu, &fcu_ptr);
184         
185         /* user-friendly 'name' for F-Curve */
186         // TODO: only show the path if this is invalid?
187         col= uiLayoutColumn(layout, 0);
188                 icon= getname_anim_fcurve(name, ale->id, fcu);
189                 uiItemL(col, name, icon);
190                 
191         /* RNA-Path Editing - only really should be enabled when things aren't working */
192         col= uiLayoutColumn(layout, 1);
193                 uiLayoutSetEnabled(col, (fcu->flag & FCURVE_DISABLED)!=0); 
194                 uiItemR(col, &fcu_ptr, "data_path", 0, "", ICON_RNA);
195                 uiItemR(col, &fcu_ptr, "array_index", 0, NULL, ICON_NONE);
196                 
197         /* color settings */
198         col= uiLayoutColumn(layout, 1);
199                 uiItemL(col, "Display Color:", ICON_NONE);
200                 
201                 row= uiLayoutRow(col, 1);
202                         uiItemR(row, &fcu_ptr, "color_mode", 0, "", ICON_NONE);
203                         
204                         subrow= uiLayoutRow(row, 1);
205                                 uiLayoutSetEnabled(subrow, (fcu->color_mode==FCURVE_COLOR_CUSTOM));
206                                 uiItemR(subrow, &fcu_ptr, "color", 0, "", ICON_NONE);
207         
208         MEM_freeN(ale);
209 }
210
211 /* ******************* active Keyframe ************** */
212
213 /* get 'active' keyframe for panel editing */
214 static short get_active_fcurve_keyframe_edit(FCurve *fcu, BezTriple **bezt, BezTriple **prevbezt)
215 {
216         BezTriple *b;
217         int i;
218         
219         /* zero the pointers */
220         *bezt = *prevbezt = NULL;
221         
222         /* sanity checks */
223         if ((fcu->bezt == NULL) || (fcu->totvert == 0))
224                 return 0;
225                 
226         /* find first selected keyframe for now, and call it the active one 
227          *      - this is a reasonable assumption, given that whenever anyone 
228          *        wants to edit numerically, there is likely to only be 1 vert selected
229          */
230         for (i=0, b=fcu->bezt; i < fcu->totvert; i++, b++) {
231                 if (BEZSELECTED(b)) {
232                         /* found 
233                          *      - 'previous' is either the one before, of the keyframe itself (which is still fine)
234                          *              XXX: we can just make this null instead if needed
235                          */
236                         *prevbezt = (i > 0) ? b-1 : b;
237                         *bezt = b;
238                         
239                         return 1;
240                 }
241         }
242         
243         /* not found */
244         return 0;
245 }
246
247 static void graph_panel_key_properties(const bContext *C, Panel *pa)
248 {
249         bAnimListElem *ale;
250         FCurve *fcu;
251         BezTriple *bezt, *prevbezt;
252         
253         uiLayout *layout = pa->layout;
254         uiLayout *col;
255         uiBlock *block;
256
257         if (!graph_panel_context(C, &ale, &fcu))
258                 return;
259         
260         block = uiLayoutGetBlock(layout);
261         uiBlockSetHandleFunc(block, do_graph_region_buttons, NULL);
262         
263         /* only show this info if there are keyframes to edit */
264         if (get_active_fcurve_keyframe_edit(fcu, &bezt, &prevbezt)) {
265                 PointerRNA bezt_ptr;
266                 
267                 /* RNA pointer to keyframe, to allow editing */
268                 RNA_pointer_create(ale->id, &RNA_Keyframe, bezt, &bezt_ptr);
269                 
270                 /* interpolation */
271                 col= uiLayoutColumn(layout, 0);
272                         uiItemR(col, &bezt_ptr, "interpolation", 0, NULL, ICON_NONE);
273                         
274                 /* numerical coordinate editing */
275                 col= uiLayoutColumn(layout, 1);
276                         /* keyframe itself */
277                         uiItemR(col, &bezt_ptr, "co", 0, "Key", ICON_NONE);
278                         
279                         /* previous handle - only if previous was Bezier interpolation */
280                         if ((prevbezt) && (prevbezt->ipo == BEZT_IPO_BEZ))
281                                 uiItemR(col, &bezt_ptr, "handle_left", 0, NULL, ICON_NONE);
282                         
283                         /* next handle - only if current is Bezier interpolation */
284                         if (bezt->ipo == BEZT_IPO_BEZ)
285                                 uiItemR(col, &bezt_ptr, "handle_right", 0, NULL, ICON_NONE);
286         }
287         else {
288                 if ((fcu->bezt == NULL) && (fcu->modifiers.first)) {
289                         /* modifiers only - so no keyframes to be active */
290                         uiItemL(layout, "F-Curve only has F-Modifiers", ICON_NONE);
291                         uiItemL(layout, "See Modifiers panel below", ICON_INFO);
292                 }
293                 else if (fcu->fpt) {
294                         /* samples only */
295                         uiItemL(layout, "F-Curve doesn't have any keyframes as it only contains sampled points", ICON_NONE);
296                 }
297                 else
298                         uiItemL(layout, "No active keyframe on F-Curve", ICON_NONE);
299         }
300         
301         MEM_freeN(ale);
302 }
303
304 /* ******************* drivers ******************************** */
305
306 #define B_IPO_DEPCHANGE         10
307
308 static void do_graph_region_driver_buttons(bContext *C, void *UNUSED(arg), int event)
309 {
310         Main *bmain= CTX_data_main(C);
311         Scene *scene= CTX_data_scene(C);
312         
313         switch (event) {
314                 case B_IPO_DEPCHANGE:
315                 {
316                         /* rebuild depsgraph for the new deps */
317                         DAG_scene_sort(bmain, scene);
318                         
319                         /* force an update of depsgraph */
320                         DAG_ids_flush_update(bmain, 0);
321                 }
322                         break;
323         }
324         
325         /* default for now */
326         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene); // XXX could use better notifier
327 }
328
329 /* callback to remove the active driver */
330 static void driver_remove_cb (bContext *C, void *ale_v, void *UNUSED(arg))
331 {
332         bAnimListElem *ale= (bAnimListElem *)ale_v;
333         ID *id= ale->id;
334         FCurve *fcu= ale->data;
335         ReportList *reports = CTX_wm_reports(C);
336         
337         /* try to get F-Curve that driver lives on, and ID block which has this AnimData */
338         if (ELEM(NULL, id, fcu))
339                 return;
340         
341         /* call API method to remove this driver  */    
342         ANIM_remove_driver(reports, id, fcu->rna_path, fcu->array_index, 0);
343 }
344
345 /* callback to add a target variable to the active driver */
346 static void driver_add_var_cb (bContext *UNUSED(C), void *driver_v, void *UNUSED(arg))
347 {
348         ChannelDriver *driver= (ChannelDriver *)driver_v;
349         
350         /* add a new variable */
351         driver_add_new_variable(driver);
352 }
353
354 /* callback to remove target variable from active driver */
355 static void driver_delete_var_cb (bContext *UNUSED(C), void *driver_v, void *dvar_v)
356 {
357         ChannelDriver *driver= (ChannelDriver *)driver_v;
358         DriverVar *dvar= (DriverVar *)dvar_v;
359         
360         /* remove the active variable */
361         driver_free_variable(driver, dvar);
362 }
363
364 /* callback to reset the driver's flags */
365 static void driver_update_flags_cb (bContext *UNUSED(C), void *fcu_v, void *UNUSED(arg))
366 {
367         FCurve *fcu= (FCurve *)fcu_v;
368         ChannelDriver *driver= fcu->driver;
369         
370         /* clear invalid flags */
371         fcu->flag &= ~FCURVE_DISABLED; // XXX?
372         driver->flag &= ~DRIVER_FLAG_INVALID;
373 }
374
375 /* drivers panel poll */
376 static int graph_panel_drivers_poll(const bContext *C, PanelType *UNUSED(pt))
377 {
378         SpaceIpo *sipo= CTX_wm_space_graph(C);
379
380         if(sipo->mode != SIPO_MODE_DRIVERS)
381                 return 0;
382
383         return graph_panel_context(C, NULL, NULL);
384 }
385
386 /* settings for 'single property' driver variable type */
387 static void graph_panel_driverVar__singleProp(uiLayout *layout, ID *id, DriverVar *dvar)
388 {
389         DriverTarget *dtar= &dvar->targets[0];
390         PointerRNA dtar_ptr;
391         uiLayout *row, *col;
392         uiBlock *block;
393         
394         /* initialise RNA pointer to the target */
395         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
396         
397         /* Target ID */
398         row= uiLayoutRow(layout, 0);
399                 uiTemplateAnyID(row, &dtar_ptr, "id", "id_type", "Prop:");
400         
401         /* Target Property */
402         // TODO: make this less technical...
403         if (dtar->id) {
404                 PointerRNA root_ptr;
405                 
406                 /* get pointer for resolving the property selected */
407                 RNA_id_pointer_create(dtar->id, &root_ptr);
408                 
409                 col= uiLayoutColumn(layout, 1);
410                 block= uiLayoutGetBlock(col);
411                         /* rna path */
412                         uiTemplatePathBuilder(col, &dtar_ptr, "data_path", &root_ptr, "Path");
413         }
414 }
415
416 /* settings for 'rotation difference' driver variable type */
417 static void graph_panel_driverVar__rotDiff(uiLayout *layout, ID *id, DriverVar *dvar)
418 {
419         DriverTarget *dtar= &dvar->targets[0];
420         DriverTarget *dtar2= &dvar->targets[1];
421         Object *ob1 = (Object *)dtar->id;
422         Object *ob2 = (Object *)dtar2->id;
423         PointerRNA dtar_ptr, dtar2_ptr;
424         uiLayout *col;
425         
426         /* initialise RNA pointer to the target */
427         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
428         RNA_pointer_create(id, &RNA_DriverTarget, dtar2, &dtar2_ptr); 
429         
430         /* Bone 1 */
431         col= uiLayoutColumn(layout, 1);
432                 uiTemplateAnyID(col, &dtar_ptr, "id", "id_type", "Bone 1:");
433                 
434                 if (dtar->id && ob1->pose) {
435                         PointerRNA tar_ptr;
436                         
437                         RNA_pointer_create(dtar->id, &RNA_Pose, ob1->pose, &tar_ptr);
438                         uiItemPointerR(col, &dtar_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
439                 }
440         
441         col= uiLayoutColumn(layout, 1);
442                 uiTemplateAnyID(col, &dtar2_ptr, "id", "id_type", "Bone 2:");
443                 
444                 if (dtar2->id && ob2->pose) {
445                         PointerRNA tar_ptr;
446                         
447                         RNA_pointer_create(dtar2->id, &RNA_Pose, ob2->pose, &tar_ptr);
448                         uiItemPointerR(col, &dtar2_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
449                 }
450 }
451
452 /* settings for 'location difference' driver variable type */
453 static void graph_panel_driverVar__locDiff(uiLayout *layout, ID *id, DriverVar *dvar)
454 {
455         DriverTarget *dtar= &dvar->targets[0];
456         DriverTarget *dtar2= &dvar->targets[1];
457         Object *ob1 = (Object *)dtar->id;
458         Object *ob2 = (Object *)dtar2->id;
459         PointerRNA dtar_ptr, dtar2_ptr;
460         uiLayout *col;
461         
462         /* initialise RNA pointer to the target */
463         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
464         RNA_pointer_create(id, &RNA_DriverTarget, dtar2, &dtar2_ptr); 
465         
466         /* Bone 1 */
467         col= uiLayoutColumn(layout, 1);
468                 uiTemplateAnyID(col, &dtar_ptr, "id", "id_type", "Ob/Bone 1:");
469                 
470                 if (dtar->id && ob1->pose) {
471                         PointerRNA tar_ptr;
472                         
473                         RNA_pointer_create(dtar->id, &RNA_Pose, ob1->pose, &tar_ptr);
474                         uiItemPointerR(col, &dtar_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
475                 }
476                 
477                 uiItemR(col, &dtar_ptr, "use_local_space_transform", 0, NULL, ICON_NONE);
478         
479         col= uiLayoutColumn(layout, 1);
480                 uiTemplateAnyID(col, &dtar2_ptr, "id", "id_type", "Ob/Bone 2:");
481                 
482                 if (dtar2->id && ob2->pose) {
483                         PointerRNA tar_ptr;
484                         
485                         RNA_pointer_create(dtar2->id, &RNA_Pose, ob2->pose, &tar_ptr);
486                         uiItemPointerR(col, &dtar2_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
487                 }
488                 
489                 uiItemR(col, &dtar2_ptr, "use_local_space_transform", 0, NULL, ICON_NONE);
490 }
491
492 /* settings for 'transform channel' driver variable type */
493 static void graph_panel_driverVar__transChan(uiLayout *layout, ID *id, DriverVar *dvar)
494 {
495         DriverTarget *dtar= &dvar->targets[0];
496         Object *ob = (Object *)dtar->id;
497         PointerRNA dtar_ptr;
498         uiLayout *col, *row;
499         
500         /* initialise RNA pointer to the target */
501         RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); 
502         
503         /* properties */
504         col= uiLayoutColumn(layout, 1);
505                 uiTemplateAnyID(col, &dtar_ptr, "id", "id_type", "Ob/Bone:");
506                 
507                 if (dtar->id && ob->pose) {
508                         PointerRNA tar_ptr;
509                         
510                         RNA_pointer_create(dtar->id, &RNA_Pose, ob->pose, &tar_ptr);
511                         uiItemPointerR(col, &dtar_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA);
512                 }
513                 
514                 row= uiLayoutRow(layout, 1);
515                         uiItemR(row, &dtar_ptr, "transform_type", 0, "", ICON_NONE);
516                         uiItemR(row, &dtar_ptr, "use_local_space_transform", 0, NULL, ICON_NONE);
517 }
518
519 /* driver settings for active F-Curve (only for 'Drivers' mode) */
520 static void graph_panel_drivers(const bContext *C, Panel *pa)
521 {
522         bAnimListElem *ale;
523         FCurve *fcu;
524         ChannelDriver *driver;
525         DriverVar *dvar;
526         
527         PointerRNA driver_ptr;
528         uiLayout *col;
529         uiBlock *block;
530         uiBut *but;
531         
532         /* Get settings from context */
533         if (!graph_panel_context(C, &ale, &fcu))
534                 return;
535         driver= fcu->driver;
536         
537         /* set event handler for panel */
538         block= uiLayoutGetBlock(pa->layout); // xxx?
539         uiBlockSetHandleFunc(block, do_graph_region_driver_buttons, NULL);
540         
541         /* general actions - management */
542         col= uiLayoutColumn(pa->layout, 0);
543         block= uiLayoutGetBlock(col);
544                 but= uiDefBut(block, BUT, B_IPO_DEPCHANGE, "Update Dependencies", 0, 0, 10*UI_UNIT_X, 22, NULL, 0.0, 0.0, 0, 0, "Force updates of dependencies");
545                 uiButSetFunc(but, driver_update_flags_cb, fcu, NULL);
546                 
547                 but= uiDefBut(block, BUT, B_IPO_DEPCHANGE, "Remove Driver", 0, 0, 10*UI_UNIT_X, 18, NULL, 0.0, 0.0, 0, 0, "Remove this driver");
548                 uiButSetNFunc(but, driver_remove_cb, MEM_dupallocN(ale), NULL);
549                 
550         /* driver-level settings - type, expressions, and errors */
551         RNA_pointer_create(ale->id, &RNA_Driver, driver, &driver_ptr);
552         
553         col= uiLayoutColumn(pa->layout, 1);
554         block= uiLayoutGetBlock(col);
555                 uiItemR(col, &driver_ptr, "type", 0, NULL, ICON_NONE);
556                 
557                 /* show expression box if doing scripted drivers, and/or error messages when invalid drivers exist */
558                 if (driver->type == DRIVER_TYPE_PYTHON) {
559                         /* expression */
560                         uiItemR(col, &driver_ptr, "expression", 0, "Expr", ICON_NONE);
561                         
562                         /* errors? */
563                         if (driver->flag & DRIVER_FLAG_INVALID)
564                                 uiItemL(col, "ERROR: invalid Python expression", ICON_ERROR);
565                 }
566                 else {
567                         /* errors? */
568                         if (driver->flag & DRIVER_FLAG_INVALID)
569                                 uiItemL(col, "ERROR: invalid target channel(s)", ICON_ERROR);
570                 }
571                 
572         col= uiLayoutColumn(pa->layout, 1);
573                 /* debug setting */
574                 uiItemR(col, &driver_ptr, "show_debug_info", 0, NULL, ICON_NONE);
575                 
576                 /* value of driver */
577                 if (driver->flag & DRIVER_FLAG_SHOWDEBUG) {
578                         uiLayout *row= uiLayoutRow(col, 1);
579                         char valBuf[32];
580                         
581                         uiItemL(row, "Driver Value:", ICON_NONE);
582                         
583                         sprintf(valBuf, "%.3f", driver->curval);
584                         uiItemL(row, valBuf, ICON_NONE);
585                 }
586         
587         /* add driver variables */
588         col= uiLayoutColumn(pa->layout, 0);
589         block= uiLayoutGetBlock(col);
590                 but= uiDefBut(block, BUT, B_IPO_DEPCHANGE, "Add Variable", 0, 0, 10*UI_UNIT_X, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "Add a new target variable for this Driver");
591                 uiButSetFunc(but, driver_add_var_cb, driver, NULL);
592         
593         /* loop over targets, drawing them */
594         for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
595                 PointerRNA dvar_ptr;
596                 uiLayout *box, *row;
597                 
598                 /* sub-layout column for this variable's settings */
599                 col= uiLayoutColumn(pa->layout, 1);
600                 
601                 /* header panel */
602                 box= uiLayoutBox(col);
603                         /* first row context info for driver */
604                         RNA_pointer_create(ale->id, &RNA_DriverVariable, dvar, &dvar_ptr);
605                         
606                         row= uiLayoutRow(box, 0);
607                         block= uiLayoutGetBlock(row);
608                                 /* variable name */
609                                 uiItemR(row, &dvar_ptr, "name", 0, "", ICON_NONE);
610                                 
611                                 /* remove button */
612                                 uiBlockSetEmboss(block, UI_EMBOSSN);
613                                         but= uiDefIconBut(block, BUT, B_IPO_DEPCHANGE, ICON_X, 290, 0, UI_UNIT_X, UI_UNIT_Y, NULL, 0.0, 0.0, 0.0, 0.0, "Delete target variable.");
614                                         uiButSetFunc(but, driver_delete_var_cb, driver, dvar);
615                                 uiBlockSetEmboss(block, UI_EMBOSS);
616                         
617                         /* variable type */
618                         row= uiLayoutRow(box, 0);
619                                 uiItemR(row, &dvar_ptr, "type", 0, "", ICON_NONE);
620                                 
621                 /* variable type settings */
622                 box= uiLayoutBox(col);
623                         /* controls to draw depends on the type of variable */
624                         switch (dvar->type) {
625                                 case DVAR_TYPE_SINGLE_PROP:     /* single property */
626                                         graph_panel_driverVar__singleProp(box, ale->id, dvar);
627                                         break;
628                                 case DVAR_TYPE_ROT_DIFF: /* rotational difference */
629                                         graph_panel_driverVar__rotDiff(box, ale->id, dvar);
630                                         break;
631                                 case DVAR_TYPE_LOC_DIFF: /* location difference */
632                                         graph_panel_driverVar__locDiff(box, ale->id, dvar);
633                                         break;
634                                 case DVAR_TYPE_TRANSFORM_CHAN: /* transform channel */
635                                         graph_panel_driverVar__transChan(box, ale->id, dvar);
636                                         break;
637                         }
638                         
639                 /* value of variable */
640                 if (driver->flag & DRIVER_FLAG_SHOWDEBUG) {
641                         char valBuf[32];
642                         
643                         box= uiLayoutBox(col);
644                         row= uiLayoutRow(box, 1);
645                                 uiItemL(row, "Value:", ICON_NONE);
646                                 
647                                 sprintf(valBuf, "%.3f", dvar->curval);
648                                 uiItemL(row, valBuf, ICON_NONE);
649                 }
650         }
651         
652         /* cleanup */
653         MEM_freeN(ale);
654 }
655
656 /* ******************* f-modifiers ******************************** */
657 /* all the drawing code is in editors/animation/fmodifier_ui.c */
658
659 #define B_FMODIFIER_REDRAW              20
660
661 static void do_graph_region_modifier_buttons(bContext *C, void *UNUSED(arg), int event)
662 {
663         switch (event) {
664                 case B_REDR:
665                 case B_FMODIFIER_REDRAW: // XXX this should send depsgraph updates too
666                         WM_event_add_notifier(C, NC_ANIMATION, NULL); // XXX need a notifier specially for F-Modifiers
667                         break;
668         }
669 }
670
671 static void graph_panel_modifiers(const bContext *C, Panel *pa) 
672 {
673         bAnimListElem *ale;
674         FCurve *fcu;
675         FModifier *fcm;
676         uiLayout *col, *row;
677         uiBlock *block;
678         
679         if (!graph_panel_context(C, &ale, &fcu))
680                 return;
681         
682         block= uiLayoutGetBlock(pa->layout);
683         uiBlockSetHandleFunc(block, do_graph_region_modifier_buttons, NULL);
684         
685         /* 'add modifier' button at top of panel */
686         {
687                 row= uiLayoutRow(pa->layout, 0);
688                 block= uiLayoutGetBlock(row);
689                 
690                 // XXX for now, this will be a operator button which calls a 'add modifier' operator
691                 uiDefButO(block, BUT, "GRAPH_OT_fmodifier_add", WM_OP_INVOKE_REGION_WIN, "Add Modifier", 10, 0, 150, 20, "Adds a new F-Curve Modifier for the active F-Curve");
692                 
693                 /* copy/paste (as sub-row)*/
694                 row= uiLayoutRow(row, 1);
695                         uiItemO(row, "", ICON_COPYDOWN, "GRAPH_OT_fmodifier_copy");
696                         uiItemO(row, "", ICON_PASTEDOWN, "GRAPH_OT_fmodifier_paste");
697         }
698         
699         /* draw each modifier */
700         for (fcm= fcu->modifiers.first; fcm; fcm= fcm->next) {
701                 col= uiLayoutColumn(pa->layout, 1);
702                 
703                 ANIM_uiTemplate_fmodifier_draw(col, ale->id, &fcu->modifiers, fcm);
704         }
705
706         MEM_freeN(ale);
707 }
708
709 /* ******************* general ******************************** */
710
711 void graph_buttons_register(ARegionType *art)
712 {
713         PanelType *pt;
714
715         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel view");
716         strcpy(pt->idname, "GRAPH_PT_view");
717         strcpy(pt->label, "View Properties");
718         pt->draw= graph_panel_view;
719         pt->flag |= PNL_DEFAULT_CLOSED;
720         BLI_addtail(&art->paneltypes, pt);
721         
722         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel properties");
723         strcpy(pt->idname, "GRAPH_PT_properties");
724         strcpy(pt->label, "Active F-Curve");
725         pt->draw= graph_panel_properties;
726         pt->poll= graph_panel_poll;
727         BLI_addtail(&art->paneltypes, pt);
728         
729         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel properties");
730         strcpy(pt->idname, "GRAPH_PT_key_properties");
731         strcpy(pt->label, "Active Keyframe");
732         pt->draw= graph_panel_key_properties;
733         pt->poll= graph_panel_poll;
734         BLI_addtail(&art->paneltypes, pt);
735
736
737         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel drivers");
738         strcpy(pt->idname, "GRAPH_PT_drivers");
739         strcpy(pt->label, "Drivers");
740         pt->draw= graph_panel_drivers;
741         pt->poll= graph_panel_drivers_poll;
742         BLI_addtail(&art->paneltypes, pt);
743
744         pt= MEM_callocN(sizeof(PanelType), "spacetype graph panel modifiers");
745         strcpy(pt->idname, "GRAPH_PT_modifiers");
746         strcpy(pt->label, "Modifiers");
747         pt->draw= graph_panel_modifiers;
748         pt->poll= graph_panel_poll;
749         BLI_addtail(&art->paneltypes, pt);
750 }
751
752 static int graph_properties(bContext *C, wmOperator *UNUSED(op))
753 {
754         ScrArea *sa= CTX_wm_area(C);
755         ARegion *ar= graph_has_buttons_region(sa);
756         
757         if(ar)
758                 ED_region_toggle_hidden(C, ar);
759
760         return OPERATOR_FINISHED;
761 }
762
763 void GRAPH_OT_properties(wmOperatorType *ot)
764 {
765         ot->name= "Properties";
766         ot->idname= "GRAPH_OT_properties";
767         ot->description= "Toggle display properties panel";
768         
769         ot->exec= graph_properties;
770         ot->poll= ED_operator_graphedit_active;
771         
772         /* flags */
773         ot->flag= 0;
774 }