svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22130:22205
[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 #include "BLI_string.h"
14
15 #include "BKE_animsys.h"
16 #include "BKE_context.h"
17 #include "BKE_fcurve.h"
18
19 #include "RNA_access.h"
20 #include "RNA_types.h"
21
22 #include "ED_keyframing.h"
23
24 #include "UI_interface.h"
25
26 #include "WM_api.h"
27 #include "WM_types.h"
28
29 #include "interface_intern.h"
30
31 static FCurve *ui_but_get_fcurve(uiBut *but, bAction **action, int *driven)
32 {
33         FCurve *fcu= NULL;
34
35         *driven= 0;
36
37         /* there must be some RNA-pointer + property combo for this button */
38         if(but->rnaprop && but->rnapoin.id.data && 
39                 RNA_property_animateable(&but->rnapoin, but->rnaprop)) 
40         {
41                 AnimData *adt= BKE_animdata_from_id(but->rnapoin.id.data);
42                 char *path;
43                 
44                 if(adt) {
45                         if((adt->action && adt->action->curves.first) || (adt->drivers.first)) {
46                                 /* XXX this function call can become a performance bottleneck */
47                                 path= RNA_path_from_ID_to_property(&but->rnapoin, but->rnaprop);
48
49                                 if(path) {
50                                         /* animation takes priority over drivers */
51                                         if(adt->action && adt->action->curves.first)
52                                                 fcu= list_find_fcurve(&adt->action->curves, path, but->rnaindex);
53                                         
54                                         /* if not animated, check if driven */
55                                         if(!fcu && (adt->drivers.first)) {
56                                                 fcu= list_find_fcurve(&adt->drivers, path, but->rnaindex);
57                                                 
58                                                 if(fcu)
59                                                         *driven= 1;
60                                         }
61
62                                         if(fcu && action)
63                                                 *action= adt->action;
64
65                                         MEM_freeN(path);
66                                 }
67                         }
68                 }
69         }
70
71         return fcu;
72 }
73
74 void ui_but_anim_flag(uiBut *but, float cfra)
75 {
76         FCurve *fcu;
77         int driven;
78
79         but->flag &= ~(UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN);
80
81         fcu= ui_but_get_fcurve(but, NULL, &driven);
82
83         if(fcu) {
84                 if(!driven) {
85                         but->flag |= UI_BUT_ANIMATED;
86                         
87                         if(fcurve_frame_has_keyframe(fcu, cfra, 0))
88                                 but->flag |= UI_BUT_ANIMATED_KEY;
89                 }
90                 else {
91                         but->flag |= UI_BUT_DRIVEN;
92                 }
93         }
94 }
95
96 int ui_but_anim_expression_get(uiBut *but, char *str, int maxlen)
97 {
98         FCurve *fcu;
99         ChannelDriver *driver;
100         int driven;
101
102         fcu= ui_but_get_fcurve(but, NULL, &driven);
103
104         if(fcu && driven) {
105                 driver= fcu->driver;
106
107                 if(driver && driver->type == DRIVER_TYPE_PYTHON) {
108                         BLI_strncpy(str, driver->expression, maxlen);
109                         return 1;
110                 }
111         }
112
113         return 0;
114 }
115
116 int ui_but_anim_expression_set(uiBut *but, const char *str)
117 {
118         FCurve *fcu;
119         ChannelDriver *driver;
120         int driven;
121
122         fcu= ui_but_get_fcurve(but, NULL, &driven);
123
124         if(fcu && driven) {
125                 driver= fcu->driver;
126
127                 if(driver && driver->type == DRIVER_TYPE_PYTHON) {
128                         BLI_strncpy(driver->expression, str, sizeof(driver->expression));
129                         return 1;
130                 }
131         }
132
133         return 0;
134 }
135
136 void ui_but_anim_autokey(uiBut *but, Scene *scene, float cfra)
137 {
138         ID *id;
139         bAction *action;
140         FCurve *fcu;
141         int driven;
142
143         fcu= ui_but_get_fcurve(but, &action, &driven);
144
145         if(fcu && !driven) {
146                 id= but->rnapoin.id.data;
147                 
148                 if(autokeyframe_cfra_can_key(scene, id)) {
149                         short flag = 0;
150                         
151                         if (IS_AUTOKEY_FLAG(INSERTNEEDED))
152                                 flag |= INSERTKEY_NEEDED;
153                         if (IS_AUTOKEY_FLAG(AUTOMATKEY))
154                                 flag |= INSERTKEY_MATRIX;
155                         
156                         fcu->flag &= ~FCURVE_SELECTED;
157                         insert_keyframe(id, action, ((fcu->grp)?(fcu->grp->name):(NULL)), fcu->rna_path, fcu->array_index, cfra, flag);
158                 }
159         }
160 }
161
162 void uiAnimContextProperty(const bContext *C, struct PointerRNA *ptr, struct PropertyRNA **prop, int *index)
163 {
164         ARegion *ar= CTX_wm_region(C);
165         uiBlock *block;
166         uiBut *but;
167
168         memset(ptr, 0, sizeof(*ptr));
169         *prop= NULL;
170         *index= 0;
171
172         if(ar) {
173                 for(block=ar->uiblocks.first; block; block=block->next) {
174                         for(but=block->buttons.first; but; but= but->next) {
175                                 if(but->active && but->rnapoin.id.data) {
176                                         *ptr= but->rnapoin;
177                                         *prop= but->rnaprop;
178                                         *index= but->rnaindex;
179                                         return;
180                                 }
181                         }
182                 }
183         }
184 }
185
186 void ui_but_anim_insert_keyframe(bContext *C)
187 {
188         /* this operator calls uiAnimContextProperty above */
189         WM_operator_name_call(C, "ANIM_OT_insert_keyframe_button", WM_OP_INVOKE_DEFAULT, NULL);
190 }
191
192 void ui_but_anim_delete_keyframe(bContext *C)
193 {
194         /* this operator calls uiAnimContextProperty above */
195         WM_operator_name_call(C, "ANIM_OT_delete_keyframe_button", WM_OP_INVOKE_DEFAULT, NULL);
196 }
197
198 void ui_but_anim_add_driver(bContext *C)
199 {
200         /* this operator calls uiAnimContextProperty above */
201         WM_operator_name_call(C, "ANIM_OT_add_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
202 }
203
204 void ui_but_anim_remove_driver(bContext *C)
205 {
206         /* this operator calls uiAnimContextProperty above */
207         WM_operator_name_call(C, "ANIM_OT_remove_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
208 }
209
210 void ui_but_anim_menu(bContext *C, uiBut *but)
211 {
212         uiPopupMenu *pup;
213         uiLayout *layout;
214         int length;
215
216         if(but->rnapoin.data && but->rnaprop) {
217                 pup= uiPupMenuBegin(C, RNA_property_ui_name(but->rnaprop), 0);
218                 layout= uiPupMenuLayout(pup);
219                 
220                 length= RNA_property_array_length(but->rnaprop);
221                 
222                 if(but->flag & UI_BUT_ANIMATED_KEY) {
223                         if(length) {
224                                 uiItemBooleanO(layout, "Replace Keyframes", 0, "ANIM_OT_insert_keyframe_button", "all", 1);
225                                 uiItemBooleanO(layout, "Replace Single Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
226                                 uiItemBooleanO(layout, "Delete Keyframes", 0, "ANIM_OT_delete_keyframe_button", "all", 1);
227                                 uiItemBooleanO(layout, "Delete Single Keyframe", 0, "ANIM_OT_delete_keyframe_button", "all", 0);
228                         }
229                         else {
230                                 uiItemBooleanO(layout, "Replace Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
231                                 uiItemBooleanO(layout, "Delete Keyframe", 0, "ANIM_OT_delete_keyframe_button", "all", 0);
232                         }
233                 }
234                 else if(but->flag & UI_BUT_DRIVEN);
235                 else if(RNA_property_animateable(&but->rnapoin, but->rnaprop)) {
236                         if(length) {
237                                 uiItemBooleanO(layout, "Insert Keyframes", 0, "ANIM_OT_insert_keyframe_button", "all", 1);
238                                 uiItemBooleanO(layout, "Insert Single Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
239                         }
240                         else 
241                                 uiItemBooleanO(layout, "Insert Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
242                 }
243                 
244                 if(but->flag & UI_BUT_DRIVEN) {
245                         uiItemS(layout);
246                         
247                         if(length) {
248                                 uiItemBooleanO(layout, "Delete Drivers", 0, "ANIM_OT_remove_driver_button", "all", 1);
249                                 uiItemBooleanO(layout, "Delete Single Driver", 0, "ANIM_OT_remove_driver_button", "all", 0);
250                         }
251                         else
252                                 uiItemBooleanO(layout, "Delete Driver", 0, "ANIM_OT_remove_driver_button", "all", 0);
253                 }
254                 else if(but->flag & UI_BUT_ANIMATED_KEY);
255                 else if(RNA_property_animateable(&but->rnapoin, but->rnaprop)) {
256                         uiItemS(layout);
257                         
258                         if(length) {
259                                 uiItemBooleanO(layout, "Add Drivers", 0, "ANIM_OT_add_driver_button", "all", 1);
260                                 uiItemBooleanO(layout, "Add Single Driver", 0, "ANIM_OT_add_driver_button", "all", 0);
261                         }
262                         else
263                                 uiItemBooleanO(layout, "Add Driver", 0, "ANIM_OT_add_driver_button", "all", 0);
264                 }
265
266                 uiPupMenuEnd(C, pup);
267         }
268 }
269