svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r20849:20855
[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         uiPopupMenu *pup;
118         uiLayout *layout;
119         int length;
120
121         if(but->rnapoin.data && but->rnaprop) {
122                 pup= uiPupMenuBegin(C, RNA_property_ui_name(but->rnaprop), 0);
123                 layout= uiPupMenuLayout(pup);
124                 
125                 length= RNA_property_array_length(but->rnaprop);
126                 
127                 if(but->flag & UI_BUT_ANIMATED_KEY) {
128                         if(length) {
129                                 uiItemBooleanO(layout, "Replace Keyframes", 0, "ANIM_OT_insert_keyframe_button", "all", 1);
130                                 uiItemBooleanO(layout, "Replace Single Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
131                                 uiItemBooleanO(layout, "Delete Keyframes", 0, "ANIM_OT_delete_keyframe_button", "all", 1);
132                                 uiItemBooleanO(layout, "Delete Single Keyframe", 0, "ANIM_OT_delete_keyframe_button", "all", 0);
133                         }
134                         else {
135                                 uiItemBooleanO(layout, "Replace Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
136                                 uiItemBooleanO(layout, "Delete Keyframe", 0, "ANIM_OT_delete_keyframe_button", "all", 0);
137                         }
138                 }
139                 else if(RNA_property_animateable(&but->rnapoin, but->rnaprop)) {
140                         if(length) {
141                                 uiItemBooleanO(layout, "Insert Keyframes", 0, "ANIM_OT_insert_keyframe_button", "all", 1);
142                                 uiItemBooleanO(layout, "Insert Single Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
143                         }
144                         else 
145                                 uiItemBooleanO(layout, "Insert Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
146                 }
147                 
148                 if(but->flag & UI_BUT_DRIVEN) {
149                         uiItemS(layout);
150                         
151                         if(length) {
152                                 uiItemBooleanO(layout, "Remove Driver", 0, "ANIM_OT_remove_driver_button", "all", 1);
153                                 uiItemBooleanO(layout, "Remove Single Driver", 0, "ANIM_OT_remove_driver_button", "all", 0);
154                         }
155                         else
156                                 uiItemBooleanO(layout, "Remove Driver", 0, "ANIM_OT_remove_driver_button", "all", 0);
157                 }
158                 else if(RNA_property_animateable(&but->rnapoin, but->rnaprop)) {
159                         uiItemS(layout);
160                         
161                         if(length) {
162                                 uiItemBooleanO(layout, "Add Driver", 0, "ANIM_OT_add_driver_button", "all", 1);
163                                 uiItemBooleanO(layout, "Add Single Driver", 0, "ANIM_OT_add_driver_button", "all", 0);
164                         }
165                         else
166                                 uiItemBooleanO(layout, "Add Driver", 0, "ANIM_OT_add_driver_button", "all", 0);
167                 }
168
169                 uiPupMenuEnd(C, pup);
170         }
171 }
172