Assorted tweaks for animation editors:
[blender-staging.git] / source / blender / editors / space_action / action_header.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_anim_types.h"
33 #include "DNA_action_types.h"
34 #include "DNA_key_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_space_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_windowmanager_types.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_blenlib.h"
44
45 #include "BKE_animsys.h"
46 #include "BKE_action.h"
47 #include "BKE_context.h"
48 #include "BKE_screen.h"
49
50 #include "ED_anim_api.h"
51 #include "ED_screen.h"
52 #include "ED_transform.h"
53 #include "ED_types.h"
54 #include "ED_util.h"
55
56 #include "RNA_access.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "BIF_gl.h"
62 #include "BIF_glutil.h"
63
64 #include "UI_interface.h"
65 #include "UI_resources.h"
66 #include "UI_view2d.h"
67
68 #include "action_intern.h"
69
70 enum {
71         B_REDR= 1,
72         B_MODECHANGE,
73 } eActHeader_Events;
74
75 /* ********************************************************* */
76 /* Menu Defines... */
77
78 static void act_viewmenu(bContext *C, uiLayout *layout, void *arg_unused)
79 {
80         bScreen *sc= CTX_wm_screen(C);
81         ScrArea *sa= CTX_wm_area(C);
82         SpaceAction *sact= CTX_wm_space_action(C);
83         PointerRNA spaceptr;
84         
85         /* retrieve state */
86         RNA_pointer_create(&sc->id, &RNA_SpaceDopeSheetEditor, sact, &spaceptr);
87         
88         /* create menu */
89         //uiItemO(layout, NULL, ICON_MENU_PANEL, "ACT_OT_properties");
90         
91         //uiItemS(layout);
92         
93         uiItemR(layout, NULL, 0, &spaceptr, "show_cframe_indicator", 0);
94         uiItemR(layout, NULL, 0, &spaceptr, "show_sliders", 0);
95         uiItemR(layout, NULL, 0, &spaceptr, "automerge_keyframes", 0);
96         
97         if (sact->flag & SACTION_DRAWTIME)
98                 uiItemO(layout, "Show Frames", 0, "ANIM_OT_time_toggle");
99         else
100                 uiItemO(layout, "Show Seconds", 0, "ANIM_OT_time_toggle");
101         
102         uiItemS(layout);
103         
104         uiItemO(layout, NULL, 0, "ANIM_OT_previewrange_set");
105         uiItemO(layout, NULL, 0, "ANIM_OT_previewrange_clear");
106         
107         uiItemO(layout, NULL, 0, "ACT_OT_previewrange_set");
108         
109         uiItemS(layout);
110         
111         uiItemO(layout, NULL, 0, "ACT_OT_frame_jump");
112         
113         uiItemO(layout, NULL, 0, "ACT_OT_view_all");
114         
115         if (sa->full) 
116                 uiItemO(layout, NULL, 0, "SCREEN_OT_screen_full_area"); // "Tile Window", Ctrl UpArrow
117         else 
118                 uiItemO(layout, NULL, 0, "SCREEN_OT_screen_full_area"); // "Maximize Window", Ctrl DownArrow
119 }
120
121 static void act_selectmenu(bContext *C, uiLayout *layout, void *arg_unused)
122 {
123         uiItemO(layout, NULL, 0, "ACT_OT_select_all_toggle");
124         uiItemBooleanO(layout, "Invert All", 0, "ACT_OT_select_all_toggle", "invert", 1);
125         
126         uiItemS(layout);
127         
128         uiItemO(layout, NULL, 0, "ACT_OT_select_border");
129         uiItemBooleanO(layout, "Border Axis Range", 0, "ACT_OT_select_border", "axis_range", 1);
130         
131         uiItemS(layout);
132         
133         uiItemEnumO(layout, "Columns on Selected Keys", 0, "ACT_OT_select_column", "mode", ACTKEYS_COLUMNSEL_KEYS);
134         uiItemEnumO(layout, "Column on Current Frame", 0, "ACT_OT_select_column", "mode", ACTKEYS_COLUMNSEL_CFRA);
135         
136         uiItemEnumO(layout, "Columns on Selected Markers", 0, "ACT_OT_select_column", "mode", ACTKEYS_COLUMNSEL_MARKERS_COLUMN);
137         uiItemEnumO(layout, "Between Selected Markers", 0, "ACT_OT_select_column", "mode", ACTKEYS_COLUMNSEL_MARKERS_BETWEEN);
138 }
139
140 static void act_channelmenu(bContext *C, uiLayout *layout, void *arg_unused)
141 {
142         uiItemO(layout, NULL, 0, "ANIM_OT_channels_setting_toggle");
143         uiItemO(layout, NULL, 0, "ANIM_OT_channels_setting_enable");
144         uiItemO(layout, NULL, 0, "ANIM_OT_channels_setting_disable");
145         
146         uiItemS(layout);
147         
148         uiItemO(layout, NULL, 0, "ANIM_OT_channels_editable_toggle");
149         
150         uiItemS(layout);
151         
152         uiItemO(layout, NULL, 0, "ANIM_OT_channels_expand");
153         uiItemO(layout, NULL, 0, "ANIM_OT_channels_collapse");
154 }
155
156 static void act_gplayermenu(bContext *C, uiLayout *layout, void *arg_unused)
157 {
158         //uiItemMenuF(layout, "Transform", 0, nla_edit_transformmenu, NULL, NULL);
159         //uiItemS(layout);
160         //uiItemO(layout, NULL, 0, "NLAEDIT_OT_duplicate");
161 }
162
163 static void act_edit_transformmenu(bContext *C, uiLayout *layout, void *arg_unused)
164 {
165         uiItemEnumO(layout, "Grab/Move", 0, "TFM_OT_transform", "mode", TFM_TIME_TRANSLATE);
166         uiItemEnumO(layout, "Extend", 0, "TFM_OT_transform", "mode", TFM_TIME_EXTEND);
167         uiItemEnumO(layout, "Scale", 0, "TFM_OT_transform", "mode", TFM_TIME_SCALE);
168 }
169
170 static void act_edit_snapmenu(bContext *C, uiLayout *layout, void *arg_unused)
171 {
172         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT); 
173         uiItemEnumO(layout, NULL, 0, "ACT_OT_snap", "type", ACTKEYS_SNAP_CFRA);
174         uiItemEnumO(layout, NULL, 0, "ACT_OT_snap", "type", ACTKEYS_SNAP_NEAREST_FRAME);
175         uiItemEnumO(layout, NULL, 0, "ACT_OT_snap", "type", ACTKEYS_SNAP_NEAREST_SECOND);
176         uiItemEnumO(layout, NULL, 0, "ACT_OT_snap", "type", ACTKEYS_SNAP_NEAREST_MARKER);
177 }
178
179 static void act_edit_mirrormenu(bContext *C, uiLayout *layout, void *arg_unused)
180 {
181         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
182         uiItemEnumO(layout, NULL, 0, "ACT_OT_mirror", "type", ACTKEYS_MIRROR_CFRA);
183         uiItemEnumO(layout, NULL, 0, "ACT_OT_mirror", "type", ACTKEYS_MIRROR_YAXIS);
184         uiItemEnumO(layout, NULL, 0, "ACT_OT_mirror", "type", ACTKEYS_MIRROR_XAXIS);
185         uiItemEnumO(layout, NULL, 0, "ACT_OT_mirror", "type", ACTKEYS_MIRROR_MARKER);
186 }
187
188 static void act_edit_keytypesmenu(bContext *C, uiLayout *layout, void *arg_unused)
189 {
190         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
191         uiItemEnumO(layout, NULL, 0, "ACT_OT_keyframe_type", "type", BEZT_KEYTYPE_KEYFRAME);
192         uiItemEnumO(layout, NULL, 0, "ACT_OT_keyframe_type", "type", BEZT_KEYTYPE_BREAKDOWN);
193         uiItemEnumO(layout, NULL, 0, "ACT_OT_keyframe_type", "type", BEZT_KEYTYPE_EXTREME);
194 }
195
196 static void act_edit_handlesmenu(bContext *C, uiLayout *layout, void *arg_unused)
197 {
198         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
199         uiItemEnumO(layout, NULL, 0, "ACT_OT_handle_type", "type", HD_FREE);
200         uiItemEnumO(layout, NULL, 0, "ACT_OT_handle_type", "type", HD_AUTO);
201         uiItemEnumO(layout, NULL, 0, "ACT_OT_handle_type", "type", HD_VECT);
202         uiItemEnumO(layout, NULL, 0, "ACT_OT_handle_type", "type", HD_ALIGN);
203         uiItemEnumO(layout, NULL, 0, "ACT_OT_handle_type", "type", HD_AUTO_ANIM); // xxx?
204 }
205
206 static void act_edit_ipomenu(bContext *C, uiLayout *layout, void *arg_unused)
207 {
208         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
209         uiItemEnumO(layout, NULL, 0, "ACT_OT_interpolation_type", "type", BEZT_IPO_CONST);
210         uiItemEnumO(layout, NULL, 0, "ACT_OT_interpolation_type", "type", BEZT_IPO_LIN);
211         uiItemEnumO(layout, NULL, 0, "ACT_OT_interpolation_type", "type", BEZT_IPO_BEZ);
212 }
213
214 static void act_edit_expomenu(bContext *C, uiLayout *layout, void *arg_unused)
215 {
216         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
217         uiItemEnumO(layout, NULL, 0, "ACT_OT_extrapolation_type", "type", FCURVE_EXTRAPOLATE_CONSTANT);
218         uiItemEnumO(layout, NULL, 0, "ACT_OT_extrapolation_type", "type", FCURVE_EXTRAPOLATE_LINEAR);
219 }
220
221 static void act_editmenu(bContext *C, uiLayout *layout, void *arg_unused)
222 {
223         uiItemMenuF(layout, "Transform", 0, act_edit_transformmenu, NULL);
224         uiItemMenuF(layout, "Snap", 0, act_edit_snapmenu, NULL);
225         uiItemMenuF(layout, "Mirror", 0, act_edit_mirrormenu, NULL);
226         
227         uiItemS(layout);
228         
229         uiItemO(layout, NULL, 0, "ACT_OT_insert_keyframe");
230         
231         uiItemS(layout);
232         
233         uiItemO(layout, NULL, 0, "ACT_OT_duplicate");
234         uiItemO(layout, NULL, 0, "ACT_OT_delete");
235         
236         uiItemS(layout);
237         
238         uiItemMenuF(layout, "Keyframe Type", 0, act_edit_keytypesmenu, NULL);
239         uiItemMenuF(layout, "Handle Type", 0, act_edit_handlesmenu, NULL);
240         uiItemMenuF(layout, "Interpolation Mode", 0, act_edit_ipomenu, NULL);
241         uiItemMenuF(layout, "Extrapolation Mode", 0, act_edit_expomenu, NULL);
242         
243         uiItemS(layout);
244         
245         uiItemO(layout, NULL, 0, "ACT_OT_clean");
246         uiItemO(layout, NULL, 0, "ACT_OT_sample");
247         
248         uiItemS(layout);
249         
250         uiItemO(layout, NULL, 0, "ACT_OT_copy");
251         uiItemO(layout, NULL, 0, "ACT_OT_paste");
252 }
253
254 /* ************************ header area region *********************** */
255
256 static void do_action_buttons(bContext *C, void *arg, int event)
257 {
258         /* special exception for mode changing - enable custom settings? */
259         if (event == B_MODECHANGE) {
260                 SpaceAction *saction= CTX_wm_space_action(C);
261                 
262                 /* if the new mode is ShapeKeys editor, enable sliders */
263                 if (saction->mode == SACTCONT_SHAPEKEY)
264                         saction->flag |= SACTION_SLIDERS;
265         }
266         
267         ED_area_tag_refresh(CTX_wm_area(C));
268         ED_area_tag_redraw(CTX_wm_area(C));
269 }
270
271 void action_header_buttons(const bContext *C, ARegion *ar)
272 {
273         ScrArea *sa= CTX_wm_area(C);
274         SpaceAction *saction= CTX_wm_space_action(C);
275         bAnimContext ac;
276         uiBlock *block;
277         int xco, yco= 3, xmax;
278         
279         block= uiBeginBlock(C, ar, "header buttons", UI_EMBOSS);
280         uiBlockSetHandleFunc(block, do_action_buttons, NULL);
281         
282         xco= ED_area_header_standardbuttons(C, block, yco);
283         
284         uiBlockSetEmboss(block, UI_EMBOSS);
285         
286         /* get context... (also syncs data) */
287         ANIM_animdata_get_context(C, &ac);
288         
289         if ((sa->flag & HEADER_NO_PULLDOWN)==0) {
290                 
291                 xmax= GetButStringLength("View");
292                 uiDefMenuBut(block, act_viewmenu, NULL, "View", xco, yco, xmax-3, 20, "");
293                 xco+= xmax;
294                 
295                 xmax= GetButStringLength("Select");
296                 uiDefMenuBut(block, act_selectmenu, NULL, "Select", xco, yco, xmax-3, 20, "");
297                 xco+= xmax;
298                 
299                 if ( (saction->mode == SACTCONT_DOPESHEET) ||
300                          ((saction->action) && (saction->mode==SACTCONT_ACTION)) ) 
301                 {
302                         xmax= GetButStringLength("Channel");
303                         uiDefMenuBut(block, act_channelmenu, NULL, "Channel", xco, yco, xmax-3, 20, "");
304                         xco+= xmax;
305                 }
306                 else if (saction->mode==SACTCONT_GPENCIL) {
307                         xmax= GetButStringLength("Channel");
308                         uiDefMenuBut(block, act_gplayermenu, NULL, "Channel", xco, yco, xmax-3, 20, "");
309                         xco+= xmax;
310                 }
311                 
312                 //xmax= GetButStringLength("Marker");
313                 //uiDefMenuBut(block, act_markermenu, NULL, "Marker", xco, yco, xmax-3, 20, "");
314                 //xco+= xmax;
315                 
316                 if (saction->mode == SACTCONT_GPENCIL) {
317                         //xmax= GetButStringLength("Frame");
318                         //uiDefMenuBut(block, act_selectmenu, NULL, "Frame", xco, yco, xmax-3, 20, "");
319                         //xco+= xmax;
320                 }
321                 else {
322                         xmax= GetButStringLength("Key");
323                         uiDefMenuBut(block, act_editmenu, NULL, "Key", xco, yco, xmax-3, 20, "");
324                         xco+= xmax;
325                 }
326         }
327
328         uiBlockSetEmboss(block, UI_EMBOSS);
329         
330         /* MODE SELECTOR */
331         uiDefButC(block, MENU, B_MODECHANGE, 
332                         "Editor Mode %t|DopeSheet %x3|Action Editor %x0|ShapeKey Editor %x1|Grease Pencil %x2", 
333                         xco,yco,90,YIC, &saction->mode, 0, 1, 0, 0, 
334                         "Editing modes for this editor");
335         
336         
337         xco += (90 + 8);
338         
339         /* SUMMARY CHANNEL */
340         uiDefIconTextButBitI(block, TOG, ADS_FILTER_SUMMARY, B_REDR, ICON_BORDERMOVE, "Summary", xco,yco,XIC*4,YIC, &(saction->ads.filterflag), 0, 0, 0, 0, "Include DopeSheet summary row"); // TODO: needs a better icon
341         xco += (XIC*4.5);
342         
343         /*if (ac.data)*/ 
344         {
345                 /* MODE-DEPENDENT DRAWING */
346                 if (saction->mode == SACTCONT_DOPESHEET) {
347                         /* FILTERING OPTIONS */
348                         xco -= XIC; // XXX first button incurs this offset...
349                         xco= ANIM_headerUI_standard_buttons(C, &saction->ads, block, xco, yco);
350                 }
351                 else if (saction->mode == SACTCONT_ACTION) {
352                         uiLayout *layout;
353                         bScreen *sc= CTX_wm_screen(C);
354                         PointerRNA ptr;
355                         
356                         RNA_pointer_create(&sc->id, &RNA_SpaceDopeSheetEditor, saction, &ptr);
357                         
358                         layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_HEADER, xco, 20+3, 20, 1, U.uistyles.first);
359                         uiTemplateID(layout, (bContext*)C, &ptr, "action", "ACT_OT_new", NULL, NULL);
360                         uiBlockLayoutResolve(block, &xco, NULL);
361                         
362                         xco += 8;
363                 }
364                 
365                 /* draw AUTOSNAP */
366                 if (saction->mode != SACTCONT_GPENCIL) {
367                         if (saction->flag & SACTION_DRAWTIME) {
368                                 uiDefButC(block, MENU, B_REDR,
369                                                 "Auto-Snap Keyframes %t|No Snap %x0|Second Step %x1|Nearest Second %x2|Nearest Marker %x3", 
370                                                 xco,yco,90,YIC, &(saction->autosnap), 0, 1, 0, 0, 
371                                                 "Auto-snapping mode for keyframes when transforming");
372                         }
373                         else {
374                                 uiDefButC(block, MENU, B_REDR, 
375                                                 "Auto-Snap Keyframes %t|No Snap %x0|Frame Step %x1|Nearest Frame %x2|Nearest Marker %x3", 
376                                                 xco,yco,90,YIC, &(saction->autosnap), 0, 1, 0, 0, 
377                                                 "Auto-snapping mode for keyframes when transforming");
378                         }
379                         
380                         xco += (90 + 8);
381                 }
382                 
383                 /* COPY PASTE */
384                 uiBlockBeginAlign(block);
385                         uiDefIconButO(block, BUT, "ACT_OT_copy", WM_OP_INVOKE_REGION_WIN, ICON_COPYDOWN, xco,yco,XIC,YIC, "Copies the selected keyframes to the buffer.");
386                         xco += XIC;
387                         uiDefIconButO(block, BUT, "ACT_OT_paste", WM_OP_INVOKE_REGION_WIN, ICON_PASTEDOWN, xco,yco,XIC,YIC, "Pastes the keyframes from the buffer into the selected channels.");
388                 uiBlockEndAlign(block);
389                 xco += (XIC + 8);
390         }
391
392         /* always as last */
393         UI_view2d_totRect_set(&ar->v2d, xco+XIC+80, (int)(ar->v2d.tot.ymax-ar->v2d.tot.ymin));
394         
395         uiEndBlock(C, block);
396         uiDrawBlock(C, block);
397 }
398
399