2.5 - Groundwork for Adding/Removing Drivers
[blender.git] / source / blender / editors / interface / interface_anim.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "MEM_guardedalloc.h"
7
8 #include "DNA_anim_types.h"
9 #include "DNA_scene_types.h"
10 #include "DNA_screen_types.h"
11
12 #include "BLI_listbase.h"
13
14 #include "BKE_animsys.h"
15 #include "BKE_context.h"
16 #include "BKE_fcurve.h"
17
18 #include "RNA_access.h"
19 #include "RNA_types.h"
20
21 #include "UI_interface.h"
22
23 #include "WM_api.h"
24 #include "WM_types.h"
25
26 #include "interface_intern.h"
27
28 void ui_but_anim_flag(uiBut *but, float cfra)
29 {
30         but->flag &= ~(UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN);
31
32         if(but->rnaprop && but->rnapoin.id.data) {
33                 AnimData *adt= BKE_animdata_from_id(but->rnapoin.id.data);
34                 FCurve *fcu;
35                 char *path;
36
37                 if (adt) {
38                         if ((adt->action && adt->action->curves.first) || (adt->drivers.first)) {
39                                 /* XXX this function call can become a performance bottleneck */
40                                 path= RNA_path_from_ID_to_property(&but->rnapoin, but->rnaprop);
41                                 
42                                 if (path) {
43                                         /* animation takes priority over drivers */
44                                         if (adt->action && adt->action->curves.first) {
45                                                 fcu= list_find_fcurve(&adt->action->curves, path, but->rnaindex);
46                                                 
47                                                 if (fcu) {
48                                                         but->flag |= UI_BUT_ANIMATED;
49                                                         
50                                                         if (on_keyframe_fcurve(fcu, cfra))
51                                                                 but->flag |= UI_BUT_ANIMATED_KEY;
52                                                 }
53                                         }
54                                         
55                                         /* if not animated, check if driven */
56                                         if ((but->flag & UI_BUT_ANIMATED)==0 && (adt->drivers.first)) {
57                                                 fcu= list_find_fcurve(&adt->drivers, path, but->rnaindex);
58                                                 
59                                                 if (fcu)
60                                                         but->flag |= UI_BUT_DRIVEN;
61                                         }
62                                         
63                                         MEM_freeN(path);
64                                 }
65                         }
66                 }
67         }
68 }
69
70 void uiAnimContextProperty(const bContext *C, struct PointerRNA *ptr, struct PropertyRNA **prop, int *index)
71 {
72         ARegion *ar= CTX_wm_region(C);
73         uiBlock *block;
74         uiBut *but;
75
76         if(ar) {
77                 for(block=ar->uiblocks.first; block; block=block->next) {
78                         for(but=block->buttons.first; but; but= but->next) {
79                                 if(but->active && but->rnapoin.id.data) {
80                                         *ptr= but->rnapoin;
81                                         *prop= but->rnaprop;
82                                         *index= but->rnaindex;
83                                         return;
84                                 }
85                         }
86                 }
87         }
88 }
89
90 void ui_but_anim_insert_keyframe(bContext *C)
91 {
92         /* this operator calls uiAnimContextProperty above */
93         WM_operator_name_call(C, "ANIM_OT_insert_keyframe_button", WM_OP_INVOKE_DEFAULT, NULL);
94 }
95
96 void ui_but_anim_delete_keyframe(bContext *C)
97 {
98         /* this operator calls uiAnimContextProperty above */
99         WM_operator_name_call(C, "ANIM_OT_delete_keyframe_button", WM_OP_INVOKE_DEFAULT, NULL);
100 }
101
102 void ui_but_anim_add_driver(bContext *C)
103 {
104         /* this operator calls uiAnimContextProperty above */
105         WM_operator_name_call(C, "ANIM_OT_add_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
106 }
107
108 void ui_but_anim_remove_driver(bContext *C)
109 {
110         /* this operator calls uiAnimContextProperty above */
111         WM_operator_name_call(C, "ANIM_OT_remove_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
112 }
113
114 // TODO: refine the logic for adding/removing drivers...
115 void ui_but_anim_menu(bContext *C, uiBut *but)
116 {
117         uiMenuItem *head;
118         int length;
119
120         if(but->rnapoin.data && but->rnaprop) {
121                 head= uiPupMenuBegin(RNA_property_ui_name(&but->rnapoin, but->rnaprop), 0);
122
123                 length= RNA_property_array_length(&but->rnapoin, but->rnaprop);
124
125                 if(but->flag & UI_BUT_ANIMATED_KEY) {
126                         if(length) {
127                                 uiMenuItemBooleanO(head, "Delete Keyframes", 0, "ANIM_OT_delete_keyframe_button", "all", 1);
128                                 uiMenuItemBooleanO(head, "Delete Single Keyframe", 0, "ANIM_OT_delete_keyframe_button", "all", 0);
129                                 
130                                 uiMenuItemBooleanO(head, "Remove Driver", 0, "ANIM_OT_remove_driver_button", "all", 1);
131                                 uiMenuItemBooleanO(head, "Remove Single Driver", 0, "ANIM_OT_remove_driver_button", "all", 0);
132                         }
133                         else {
134                                 uiMenuItemBooleanO(head, "Delete Keyframe", 0, "ANIM_OT_delete_keyframe_button", "all", 0);
135                                 
136                                 uiMenuItemBooleanO(head, "Remove Driver", 0, "ANIM_OT_remove_driver_button", "all", 0);
137                         }
138                 }
139                 else if(RNA_property_animateable(&but->rnapoin, but->rnaprop)) {
140                         if(length) {
141                                 uiMenuItemBooleanO(head, "Insert Keyframes", 0, "ANIM_OT_insert_keyframe_button", "all", 1);
142                                 uiMenuItemBooleanO(head, "Insert Single Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
143                                 
144                                 uiMenuItemBooleanO(head, "Add Driver", 0, "ANIM_OT_add_driver_button", "all", 1);
145                                 uiMenuItemBooleanO(head, "Add Single Driver", 0, "ANIM_OT_add_driver_button", "all", 0);
146                         }
147                         else {
148                                 uiMenuItemBooleanO(head, "Insert Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
149                                 
150                                 uiMenuItemBooleanO(head, "Add Driver", 0, "ANIM_OT_add_driver_button", "all", 0);
151                         }
152                 }
153
154                 uiPupMenuEnd(C, head);
155         }
156 }
157