2.5: Couple of small fun features
[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         if(ar) {
169                 for(block=ar->uiblocks.first; block; block=block->next) {
170                         for(but=block->buttons.first; but; but= but->next) {
171                                 if(but->active && but->rnapoin.id.data) {
172                                         *ptr= but->rnapoin;
173                                         *prop= but->rnaprop;
174                                         *index= but->rnaindex;
175                                         return;
176                                 }
177                         }
178                 }
179         }
180 }
181
182 void ui_but_anim_insert_keyframe(bContext *C)
183 {
184         /* this operator calls uiAnimContextProperty above */
185         WM_operator_name_call(C, "ANIM_OT_insert_keyframe_button", WM_OP_INVOKE_DEFAULT, NULL);
186 }
187
188 void ui_but_anim_delete_keyframe(bContext *C)
189 {
190         /* this operator calls uiAnimContextProperty above */
191         WM_operator_name_call(C, "ANIM_OT_delete_keyframe_button", WM_OP_INVOKE_DEFAULT, NULL);
192 }
193
194 void ui_but_anim_add_driver(bContext *C)
195 {
196         /* this operator calls uiAnimContextProperty above */
197         WM_operator_name_call(C, "ANIM_OT_add_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
198 }
199
200 void ui_but_anim_remove_driver(bContext *C)
201 {
202         /* this operator calls uiAnimContextProperty above */
203         WM_operator_name_call(C, "ANIM_OT_remove_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
204 }
205
206 void ui_but_anim_menu(bContext *C, uiBut *but)
207 {
208         uiPopupMenu *pup;
209         uiLayout *layout;
210         int length;
211
212         if(but->rnapoin.data && but->rnaprop) {
213                 pup= uiPupMenuBegin(C, RNA_property_ui_name(but->rnaprop), 0);
214                 layout= uiPupMenuLayout(pup);
215                 
216                 length= RNA_property_array_length(but->rnaprop);
217                 
218                 if(but->flag & UI_BUT_ANIMATED_KEY) {
219                         if(length) {
220                                 uiItemBooleanO(layout, "Replace Keyframes", 0, "ANIM_OT_insert_keyframe_button", "all", 1);
221                                 uiItemBooleanO(layout, "Replace Single Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
222                                 uiItemBooleanO(layout, "Delete Keyframes", 0, "ANIM_OT_delete_keyframe_button", "all", 1);
223                                 uiItemBooleanO(layout, "Delete Single Keyframe", 0, "ANIM_OT_delete_keyframe_button", "all", 0);
224                         }
225                         else {
226                                 uiItemBooleanO(layout, "Replace Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
227                                 uiItemBooleanO(layout, "Delete Keyframe", 0, "ANIM_OT_delete_keyframe_button", "all", 0);
228                         }
229                 }
230                 else if(but->flag & UI_BUT_DRIVEN);
231                 else if(RNA_property_animateable(&but->rnapoin, but->rnaprop)) {
232                         if(length) {
233                                 uiItemBooleanO(layout, "Insert Keyframes", 0, "ANIM_OT_insert_keyframe_button", "all", 1);
234                                 uiItemBooleanO(layout, "Insert Single Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
235                         }
236                         else 
237                                 uiItemBooleanO(layout, "Insert Keyframe", 0, "ANIM_OT_insert_keyframe_button", "all", 0);
238                 }
239                 
240                 if(but->flag & UI_BUT_DRIVEN) {
241                         uiItemS(layout);
242                         
243                         if(length) {
244                                 uiItemBooleanO(layout, "Delete Drivers", 0, "ANIM_OT_remove_driver_button", "all", 1);
245                                 uiItemBooleanO(layout, "Delete Single Driver", 0, "ANIM_OT_remove_driver_button", "all", 0);
246                         }
247                         else
248                                 uiItemBooleanO(layout, "Delete Driver", 0, "ANIM_OT_remove_driver_button", "all", 0);
249                 }
250                 else if(but->flag & UI_BUT_ANIMATED_KEY);
251                 else if(RNA_property_animateable(&but->rnapoin, but->rnaprop)) {
252                         uiItemS(layout);
253                         
254                         if(length) {
255                                 uiItemBooleanO(layout, "Add Drivers", 0, "ANIM_OT_add_driver_button", "all", 1);
256                                 uiItemBooleanO(layout, "Add Single Driver", 0, "ANIM_OT_add_driver_button", "all", 0);
257                         }
258                         else
259                                 uiItemBooleanO(layout, "Add Driver", 0, "ANIM_OT_add_driver_button", "all", 0);
260                 }
261
262                 uiPupMenuEnd(C, pup);
263         }
264 }
265