4108e1c8532e39164abe7bdb501e9aa304ed10e3
[blender-staging.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
20 #include "ED_keyframing.h"
21
22 #include "UI_interface.h"
23
24 #include "WM_api.h"
25 #include "WM_types.h"
26
27 #include "interface_intern.h"
28
29 static FCurve *ui_but_get_fcurve(uiBut *but, bAction **action, int *driven)
30 {
31         return rna_get_fcurve(&but->rnapoin, but->rnaprop, but->rnaindex, action, driven);
32 }
33
34 void ui_but_anim_flag(uiBut *but, float cfra)
35 {
36         FCurve *fcu;
37         int driven;
38
39         but->flag &= ~(UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN);
40
41         fcu= ui_but_get_fcurve(but, NULL, &driven);
42
43         if(fcu) {
44                 if(!driven) {
45                         but->flag |= UI_BUT_ANIMATED;
46                         
47                         if(fcurve_frame_has_keyframe(fcu, cfra, 0))
48                                 but->flag |= UI_BUT_ANIMATED_KEY;
49                 }
50                 else {
51                         but->flag |= UI_BUT_DRIVEN;
52                 }
53         }
54 }
55
56 int ui_but_anim_expression_get(uiBut *but, char *str, int maxlen)
57 {
58         FCurve *fcu;
59         ChannelDriver *driver;
60         int driven;
61
62         fcu= ui_but_get_fcurve(but, NULL, &driven);
63
64         if(fcu && driven) {
65                 driver= fcu->driver;
66
67                 if(driver && driver->type == DRIVER_TYPE_PYTHON) {
68                         BLI_strncpy(str, driver->expression, maxlen);
69                         return 1;
70                 }
71         }
72
73         return 0;
74 }
75
76 int ui_but_anim_expression_set(uiBut *but, const char *str)
77 {
78         FCurve *fcu;
79         ChannelDriver *driver;
80         int driven;
81
82         fcu= ui_but_get_fcurve(but, NULL, &driven);
83
84         if(fcu && driven) {
85                 driver= fcu->driver;
86
87                 if(driver && driver->type == DRIVER_TYPE_PYTHON) {
88                         BLI_strncpy(driver->expression, str, sizeof(driver->expression));
89                         return 1;
90                 }
91         }
92
93         return 0;
94 }
95
96 void ui_but_anim_autokey(bContext *C, uiBut *but, Scene *scene, float cfra)
97 {
98         ID *id;
99         bAction *action;
100         FCurve *fcu;
101         int driven;
102
103         fcu= ui_but_get_fcurve(but, &action, &driven);
104
105         if(fcu && !driven) {
106                 id= but->rnapoin.id.data;
107                 
108                 // TODO: this should probably respect the keyingset only option for anim
109                 if(autokeyframe_cfra_can_key(scene, id)) {
110                         short flag = ANIM_get_keyframing_flags(scene, 1);
111                         
112                         fcu->flag &= ~FCURVE_SELECTED;
113                         insert_keyframe(id, action, ((fcu->grp)?(fcu->grp->name):(NULL)), fcu->rna_path, fcu->array_index, cfra, flag);
114                         WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_EDITED, NULL);
115                 }
116         }
117 }
118
119 void uiAnimContextProperty(const bContext *C, struct PointerRNA *ptr, struct PropertyRNA **prop, int *index)
120 {
121         ARegion *ar= CTX_wm_region(C);
122         uiBlock *block;
123         uiBut *but;
124
125         memset(ptr, 0, sizeof(*ptr));
126         *prop= NULL;
127         *index= 0;
128
129         if(ar) {
130                 for(block=ar->uiblocks.first; block; block=block->next) {
131                         for(but=block->buttons.first; but; but= but->next) {
132                                 if((but->active || but->flag & UI_BUT_LAST_ACTIVE) && but->rnapoin.id.data) {
133                                         *ptr= but->rnapoin;
134                                         *prop= but->rnaprop;
135                                         *index= but->rnaindex;
136                                         return;
137                                 }
138                         }
139                 }
140         }
141 }
142
143 void ui_but_anim_insert_keyframe(bContext *C)
144 {
145         /* this operator calls uiAnimContextProperty above */
146         WM_operator_name_call(C, "ANIM_OT_keyframe_insert_button", WM_OP_INVOKE_DEFAULT, NULL);
147 }
148
149 void ui_but_anim_delete_keyframe(bContext *C)
150 {
151         /* this operator calls uiAnimContextProperty above */
152         WM_operator_name_call(C, "ANIM_OT_keyframe_delete_button", WM_OP_INVOKE_DEFAULT, NULL);
153 }
154
155 void ui_but_anim_add_driver(bContext *C)
156 {
157         /* this operator calls uiAnimContextProperty above */
158         WM_operator_name_call(C, "ANIM_OT_driver_button_add", WM_OP_INVOKE_DEFAULT, NULL);
159 }
160
161 void ui_but_anim_remove_driver(bContext *C)
162 {
163         /* this operator calls uiAnimContextProperty above */
164         WM_operator_name_call(C, "ANIM_OT_driver_button_remove", WM_OP_INVOKE_DEFAULT, NULL);
165 }
166
167 void ui_but_anim_copy_driver(bContext *C)
168 {
169         /* this operator calls uiAnimContextProperty above */
170         WM_operator_name_call(C, "ANIM_OT_copy_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
171 }
172
173 void ui_but_anim_paste_driver(bContext *C)
174 {
175         /* this operator calls uiAnimContextProperty above */
176         WM_operator_name_call(C, "ANIM_OT_paste_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
177 }
178
179 void ui_but_anim_add_keyingset(bContext *C)
180 {
181         /* this operator calls uiAnimContextProperty above */
182         WM_operator_name_call(C, "ANIM_OT_keyingset_button_add", WM_OP_INVOKE_DEFAULT, NULL);
183 }
184
185 void ui_but_anim_remove_keyingset(bContext *C)
186 {
187         /* this operator calls uiAnimContextProperty above */
188         WM_operator_name_call(C, "ANIM_OT_keyingset_button_remove", WM_OP_INVOKE_DEFAULT, NULL);
189 }