quiet some compiler warnings & fix possible (but unlikely) crash.
[blender.git] / source / blender / editors / interface / interface_anim.c
1 /*
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s):
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/interface/interface_anim.c
26  *  \ingroup edinterface
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33
34 #include "DNA_anim_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37
38 #include "BLI_listbase.h"
39 #include "BLI_string.h"
40
41 #include "BKE_context.h"
42 #include "BKE_fcurve.h"
43
44
45 #include "ED_keyframing.h"
46
47 #include "UI_interface.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "interface_intern.h"
53
54 static FCurve *ui_but_get_fcurve(uiBut *but, bAction **action, int *driven)
55 {
56         return rna_get_fcurve(&but->rnapoin, but->rnaprop, but->rnaindex, action, driven);
57 }
58
59 void ui_but_anim_flag(uiBut *but, float cfra)
60 {
61         FCurve *fcu;
62         int driven;
63
64         but->flag &= ~(UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN);
65
66         fcu= ui_but_get_fcurve(but, NULL, &driven);
67
68         if(fcu) {
69                 if(!driven) {
70                         but->flag |= UI_BUT_ANIMATED;
71                         
72                         if(fcurve_frame_has_keyframe(fcu, cfra, 0))
73                                 but->flag |= UI_BUT_ANIMATED_KEY;
74                 }
75                 else {
76                         but->flag |= UI_BUT_DRIVEN;
77                 }
78         }
79 }
80
81 int ui_but_anim_expression_get(uiBut *but, char *str, int maxlen)
82 {
83         FCurve *fcu;
84         ChannelDriver *driver;
85         int driven;
86
87         fcu= ui_but_get_fcurve(but, NULL, &driven);
88
89         if(fcu && driven) {
90                 driver= fcu->driver;
91
92                 if(driver && driver->type == DRIVER_TYPE_PYTHON) {
93                         BLI_strncpy(str, driver->expression, maxlen);
94                         return 1;
95                 }
96         }
97
98         return 0;
99 }
100
101 int ui_but_anim_expression_set(uiBut *but, const char *str)
102 {
103         FCurve *fcu;
104         ChannelDriver *driver;
105         int driven;
106
107         fcu= ui_but_get_fcurve(but, NULL, &driven);
108
109         if(fcu && driven) {
110                 driver= fcu->driver;
111
112                 if(driver && driver->type == DRIVER_TYPE_PYTHON) {
113                         BLI_strncpy(driver->expression, str, sizeof(driver->expression));
114                         driver->flag |= DRIVER_FLAG_RECOMPILE;
115                         WM_event_add_notifier(but->block->evil_C, NC_ANIMATION|ND_KEYFRAME, NULL);
116                         return 1;
117                 }
118         }
119
120         return 0;
121 }
122
123 void ui_but_anim_autokey(bContext *C, uiBut *but, Scene *scene, float cfra)
124 {
125         ID *id;
126         bAction *action;
127         FCurve *fcu;
128         int driven;
129
130         fcu= ui_but_get_fcurve(but, &action, &driven);
131
132         if(fcu && !driven) {
133                 id= but->rnapoin.id.data;
134                 
135                 // TODO: this should probably respect the keyingset only option for anim
136                 if(autokeyframe_cfra_can_key(scene, id)) {
137                         ReportList *reports = CTX_wm_reports(C);
138                         short flag = ANIM_get_keyframing_flags(scene, 1);
139                         
140                         fcu->flag &= ~FCURVE_SELECTED;
141                         insert_keyframe(reports, id, action, ((fcu->grp)?(fcu->grp->name):(NULL)), fcu->rna_path, fcu->array_index, cfra, flag);
142                         WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_EDITED, NULL);
143                 }
144         }
145 }
146
147 void ui_but_anim_insert_keyframe(bContext *C)
148 {
149         /* this operator calls uiContextActiveProperty */
150         WM_operator_name_call(C, "ANIM_OT_keyframe_insert_button", WM_OP_INVOKE_DEFAULT, NULL);
151 }
152
153 void ui_but_anim_delete_keyframe(bContext *C)
154 {
155         /* this operator calls uiContextActiveProperty */
156         WM_operator_name_call(C, "ANIM_OT_keyframe_delete_button", WM_OP_INVOKE_DEFAULT, NULL);
157 }
158
159 void ui_but_anim_add_driver(bContext *C)
160 {
161         /* this operator calls uiContextActiveProperty */
162         WM_operator_name_call(C, "ANIM_OT_driver_button_add", WM_OP_INVOKE_DEFAULT, NULL);
163 }
164
165 void ui_but_anim_remove_driver(bContext *C)
166 {
167         /* this operator calls uiContextActiveProperty */
168         WM_operator_name_call(C, "ANIM_OT_driver_button_remove", WM_OP_INVOKE_DEFAULT, NULL);
169 }
170
171 void ui_but_anim_copy_driver(bContext *C)
172 {
173         /* this operator calls uiContextActiveProperty */
174         WM_operator_name_call(C, "ANIM_OT_copy_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
175 }
176
177 void ui_but_anim_paste_driver(bContext *C)
178 {
179         /* this operator calls uiContextActiveProperty */
180         WM_operator_name_call(C, "ANIM_OT_paste_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
181 }
182
183 void ui_but_anim_add_keyingset(bContext *C)
184 {
185         /* this operator calls uiContextActiveProperty */
186         WM_operator_name_call(C, "ANIM_OT_keyingset_button_add", WM_OP_INVOKE_DEFAULT, NULL);
187 }
188
189 void ui_but_anim_remove_keyingset(bContext *C)
190 {
191         /* this operator calls uiContextActiveProperty */
192         WM_operator_name_call(C, "ANIM_OT_keyingset_button_remove", WM_OP_INVOKE_DEFAULT, NULL);
193 }