75e7ee701a2354bdfc8aa0440dc39d91a58d3d59
[blender.git] / source / blender / editors / interface / interface_anim.c
1 /** \file blender/editors/interface/interface_anim.c
2  *  \ingroup edinterface
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9
10 #include "DNA_anim_types.h"
11 #include "DNA_scene_types.h"
12 #include "DNA_screen_types.h"
13
14 #include "BLI_listbase.h"
15 #include "BLI_string.h"
16
17 #include "BKE_context.h"
18 #include "BKE_fcurve.h"
19
20
21 #include "ED_keyframing.h"
22
23 #include "UI_interface.h"
24
25 #include "WM_api.h"
26 #include "WM_types.h"
27
28 #include "interface_intern.h"
29
30 static FCurve *ui_but_get_fcurve(uiBut *but, bAction **action, int *driven)
31 {
32         return rna_get_fcurve(&but->rnapoin, but->rnaprop, but->rnaindex, action, driven);
33 }
34
35 void ui_but_anim_flag(uiBut *but, float cfra)
36 {
37         FCurve *fcu;
38         int driven;
39
40         but->flag &= ~(UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN);
41
42         fcu= ui_but_get_fcurve(but, NULL, &driven);
43
44         if(fcu) {
45                 if(!driven) {
46                         but->flag |= UI_BUT_ANIMATED;
47                         
48                         if(fcurve_frame_has_keyframe(fcu, cfra, 0))
49                                 but->flag |= UI_BUT_ANIMATED_KEY;
50                 }
51                 else {
52                         but->flag |= UI_BUT_DRIVEN;
53                 }
54         }
55 }
56
57 int ui_but_anim_expression_get(uiBut *but, char *str, int maxlen)
58 {
59         FCurve *fcu;
60         ChannelDriver *driver;
61         int driven;
62
63         fcu= ui_but_get_fcurve(but, NULL, &driven);
64
65         if(fcu && driven) {
66                 driver= fcu->driver;
67
68                 if(driver && driver->type == DRIVER_TYPE_PYTHON) {
69                         BLI_strncpy(str, driver->expression, maxlen);
70                         return 1;
71                 }
72         }
73
74         return 0;
75 }
76
77 int ui_but_anim_expression_set(uiBut *but, const char *str)
78 {
79         FCurve *fcu;
80         ChannelDriver *driver;
81         int driven;
82
83         fcu= ui_but_get_fcurve(but, NULL, &driven);
84
85         if(fcu && driven) {
86                 driver= fcu->driver;
87
88                 if(driver && driver->type == DRIVER_TYPE_PYTHON) {
89                         BLI_strncpy(driver->expression, str, sizeof(driver->expression));
90                         driver->flag |= DRIVER_FLAG_RECOMPILE;
91                         WM_event_add_notifier(but->block->evil_C, NC_ANIMATION|ND_KEYFRAME, NULL);
92                         return 1;
93                 }
94         }
95
96         return 0;
97 }
98
99 void ui_but_anim_autokey(bContext *C, uiBut *but, Scene *scene, float cfra)
100 {
101         ID *id;
102         bAction *action;
103         FCurve *fcu;
104         int driven;
105
106         fcu= ui_but_get_fcurve(but, &action, &driven);
107
108         if(fcu && !driven) {
109                 id= but->rnapoin.id.data;
110                 
111                 // TODO: this should probably respect the keyingset only option for anim
112                 if(autokeyframe_cfra_can_key(scene, id)) {
113                         ReportList *reports = CTX_wm_reports(C);
114                         short flag = ANIM_get_keyframing_flags(scene, 1);
115                         
116                         fcu->flag &= ~FCURVE_SELECTED;
117                         insert_keyframe(reports, id, action, ((fcu->grp)?(fcu->grp->name):(NULL)), fcu->rna_path, fcu->array_index, cfra, flag);
118                         WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_EDITED, NULL);
119                 }
120         }
121 }
122
123 void ui_but_anim_insert_keyframe(bContext *C)
124 {
125         /* this operator calls uiContextActiveProperty */
126         WM_operator_name_call(C, "ANIM_OT_keyframe_insert_button", WM_OP_INVOKE_DEFAULT, NULL);
127 }
128
129 void ui_but_anim_delete_keyframe(bContext *C)
130 {
131         /* this operator calls uiContextActiveProperty */
132         WM_operator_name_call(C, "ANIM_OT_keyframe_delete_button", WM_OP_INVOKE_DEFAULT, NULL);
133 }
134
135 void ui_but_anim_add_driver(bContext *C)
136 {
137         /* this operator calls uiContextActiveProperty */
138         WM_operator_name_call(C, "ANIM_OT_driver_button_add", WM_OP_INVOKE_DEFAULT, NULL);
139 }
140
141 void ui_but_anim_remove_driver(bContext *C)
142 {
143         /* this operator calls uiContextActiveProperty */
144         WM_operator_name_call(C, "ANIM_OT_driver_button_remove", WM_OP_INVOKE_DEFAULT, NULL);
145 }
146
147 void ui_but_anim_copy_driver(bContext *C)
148 {
149         /* this operator calls uiContextActiveProperty */
150         WM_operator_name_call(C, "ANIM_OT_copy_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
151 }
152
153 void ui_but_anim_paste_driver(bContext *C)
154 {
155         /* this operator calls uiContextActiveProperty */
156         WM_operator_name_call(C, "ANIM_OT_paste_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
157 }
158
159 void ui_but_anim_add_keyingset(bContext *C)
160 {
161         /* this operator calls uiContextActiveProperty */
162         WM_operator_name_call(C, "ANIM_OT_keyingset_button_add", WM_OP_INVOKE_DEFAULT, NULL);
163 }
164
165 void ui_but_anim_remove_keyingset(bContext *C)
166 {
167         /* this operator calls uiContextActiveProperty */
168         WM_operator_name_call(C, "ANIM_OT_keyingset_button_remove", WM_OP_INVOKE_DEFAULT, NULL);
169 }