1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup spgraph
22  */
23 
24 #include <math.h>
25 #include <stdlib.h>
26 
27 #include "DNA_scene_types.h"
28 
29 #include "BLI_blenlib.h"
30 #include "BLI_math_base.h"
31 #include "BLI_utildefines.h"
32 
33 #include "BKE_context.h"
34 #include "BKE_global.h"
35 #include "BKE_main.h"
36 
37 #include "UI_view2d.h"
38 
39 #include "ED_anim_api.h"
40 #include "ED_markers.h"
41 #include "ED_object.h"
42 #include "ED_screen.h"
43 #include "ED_select_utils.h"
44 #include "ED_transform.h"
45 
46 #include "graph_intern.h"
47 
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50 
51 #include "DEG_depsgraph.h"
52 
53 #include "WM_api.h"
54 #include "WM_types.h"
55 
56 /* ************************** view-based operators **********************************/
57 /* XXX should these really be here? */
58 
59 /* Set Cursor --------------------------------------------------------------------- */
60 /* The 'cursor' in the Graph Editor consists of two parts:
61  * 1) Current Frame Indicator (as per ANIM_OT_change_frame)
62  * 2) Value Indicator (stored per Graph Editor instance)
63  */
64 
graphview_cursor_poll(bContext * C)65 static bool graphview_cursor_poll(bContext *C)
66 {
67   /* prevent changes during render */
68   if (G.is_rendering) {
69     return false;
70   }
71 
72   return ED_operator_graphedit_active(C);
73 }
74 
75 /* Set the new frame number */
graphview_cursor_apply(bContext * C,wmOperator * op)76 static void graphview_cursor_apply(bContext *C, wmOperator *op)
77 {
78   Scene *scene = CTX_data_scene(C);
79   SpaceGraph *sipo = CTX_wm_space_graph(C);
80   /* this isn't technically "frame", but it'll do... */
81   float frame = RNA_float_get(op->ptr, "frame");
82 
83   /* adjust the frame or the cursor x-value */
84   if (sipo->mode == SIPO_MODE_DRIVERS) {
85     /* adjust cursor x-value */
86     sipo->cursorTime = frame;
87   }
88   else {
89     /* adjust the frame
90      * NOTE: sync this part of the code with ANIM_OT_change_frame
91      */
92     /* 1) frame is rounded to the nearest int, since frames are ints */
93     CFRA = round_fl_to_int(frame);
94 
95     if (scene->r.flag & SCER_LOCK_FRAME_SELECTION) {
96       /* Clip to preview range
97        * NOTE: Preview range won't go into negative values,
98        *       so only clamping once should be fine.
99        */
100       CLAMP(CFRA, PSFRA, PEFRA);
101     }
102     else {
103       /* Prevent negative frames */
104       FRAMENUMBER_MIN_CLAMP(CFRA);
105     }
106 
107     SUBFRA = 0.0f;
108     DEG_id_tag_update(&scene->id, ID_RECALC_AUDIO_SEEK);
109   }
110 
111   /* set the cursor value */
112   sipo->cursorVal = RNA_float_get(op->ptr, "value");
113 
114   /* send notifiers - notifiers for frame should force an update for both vars ok... */
115   WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
116 }
117 
118 /* ... */
119 
120 /* Non-modal callback for running operator without user input */
graphview_cursor_exec(bContext * C,wmOperator * op)121 static int graphview_cursor_exec(bContext *C, wmOperator *op)
122 {
123   graphview_cursor_apply(C, op);
124   return OPERATOR_FINISHED;
125 }
126 
127 /* ... */
128 
129 /* set the operator properties from the initial event */
graphview_cursor_setprops(bContext * C,wmOperator * op,const wmEvent * event)130 static void graphview_cursor_setprops(bContext *C, wmOperator *op, const wmEvent *event)
131 {
132   ARegion *region = CTX_wm_region(C);
133   float viewx, viewy;
134 
135   /* abort if not active region (should not really be possible) */
136   if (region == NULL) {
137     return;
138   }
139 
140   /* convert from region coordinates to View2D 'tot' space */
141   UI_view2d_region_to_view(&region->v2d, event->mval[0], event->mval[1], &viewx, &viewy);
142 
143   /* store the values in the operator properties */
144   /* NOTE: we don't clamp frame here, as it might be used for the drivers cursor */
145   RNA_float_set(op->ptr, "frame", viewx);
146   RNA_float_set(op->ptr, "value", viewy);
147 }
148 
149 /* Modal Operator init */
graphview_cursor_invoke(bContext * C,wmOperator * op,const wmEvent * event)150 static int graphview_cursor_invoke(bContext *C, wmOperator *op, const wmEvent *event)
151 {
152   bScreen *screen = CTX_wm_screen(C);
153 
154   /* Change to frame that mouse is over before adding modal handler,
155    * as user could click on a single frame (jump to frame) as well as
156    * click-dragging over a range (modal scrubbing). Apply this change.
157    */
158   graphview_cursor_setprops(C, op, event);
159   graphview_cursor_apply(C, op);
160 
161   /* Signal that a scrubbing operating is starting */
162   if (screen) {
163     screen->scrubbing = true;
164   }
165 
166   /* add temp handler */
167   WM_event_add_modal_handler(C, op);
168   return OPERATOR_RUNNING_MODAL;
169 }
170 
171 /* Modal event handling of cursor changing */
graphview_cursor_modal(bContext * C,wmOperator * op,const wmEvent * event)172 static int graphview_cursor_modal(bContext *C, wmOperator *op, const wmEvent *event)
173 {
174   bScreen *screen = CTX_wm_screen(C);
175   Scene *scene = CTX_data_scene(C);
176 
177   /* execute the events */
178   switch (event->type) {
179     case EVT_ESCKEY:
180       if (screen) {
181         screen->scrubbing = false;
182       }
183 
184       WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
185       return OPERATOR_FINISHED;
186 
187     case MOUSEMOVE:
188       /* set the new values */
189       graphview_cursor_setprops(C, op, event);
190       graphview_cursor_apply(C, op);
191       break;
192 
193     case LEFTMOUSE:
194     case RIGHTMOUSE:
195     case MIDDLEMOUSE:
196       /* We check for either mouse-button to end, to work with all user keymaps. */
197       if (event->val == KM_RELEASE) {
198         if (screen) {
199           screen->scrubbing = false;
200         }
201 
202         WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
203         return OPERATOR_FINISHED;
204       }
205       break;
206   }
207 
208   return OPERATOR_RUNNING_MODAL;
209 }
210 
GRAPH_OT_cursor_set(wmOperatorType * ot)211 static void GRAPH_OT_cursor_set(wmOperatorType *ot)
212 {
213   /* identifiers */
214   ot->name = "Set Cursor";
215   ot->idname = "GRAPH_OT_cursor_set";
216   ot->description = "Interactively set the current frame and value cursor";
217 
218   /* api callbacks */
219   ot->exec = graphview_cursor_exec;
220   ot->invoke = graphview_cursor_invoke;
221   ot->modal = graphview_cursor_modal;
222   ot->poll = graphview_cursor_poll;
223 
224   /* flags */
225   ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR_X | OPTYPE_UNDO;
226 
227   /* rna */
228   RNA_def_float(ot->srna, "frame", 0, MINAFRAMEF, MAXFRAMEF, "Frame", "", MINAFRAMEF, MAXFRAMEF);
229   RNA_def_float(ot->srna, "value", 0, -FLT_MAX, FLT_MAX, "Value", "", -100.0f, 100.0f);
230 }
231 
232 /* Hide/Reveal ------------------------------------------------------------ */
233 
graphview_curves_hide_exec(bContext * C,wmOperator * op)234 static int graphview_curves_hide_exec(bContext *C, wmOperator *op)
235 {
236   bAnimContext ac;
237   ListBase anim_data = {NULL, NULL};
238   ListBase all_data = {NULL, NULL};
239   bAnimListElem *ale;
240   int filter;
241   const bool unselected = RNA_boolean_get(op->ptr, "unselected");
242 
243   /* get editor data */
244   if (ANIM_animdata_get_context(C, &ac) == 0) {
245     return OPERATOR_CANCELLED;
246   }
247 
248   /* get list of all channels that selection may need to be flushed to
249    * - hierarchy must not affect what we have access to here...
250    */
251   filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_NODUPLIS);
252   ANIM_animdata_filter(&ac, &all_data, filter, ac.data, ac.datatype);
253 
254   /* filter data
255    * - of the remaining visible curves, we want to hide the ones that are
256    *   selected/unselected (depending on "unselected" prop)
257    */
258   filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_CURVE_VISIBLE | ANIMFILTER_NODUPLIS);
259   if (unselected) {
260     filter |= ANIMFILTER_UNSEL;
261   }
262   else {
263     filter |= ANIMFILTER_SEL;
264   }
265 
266   ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
267 
268   for (ale = anim_data.first; ale; ale = ale->next) {
269     /* hack: skip object channels for now, since flushing those will always flush everything,
270      * but they are always included */
271     /* TODO: find out why this is the case, and fix that */
272     if (ale->type == ANIMTYPE_OBJECT) {
273       continue;
274     }
275 
276     /* change the hide setting, and unselect it... */
277     ANIM_channel_setting_set(&ac, ale, ACHANNEL_SETTING_VISIBLE, ACHANNEL_SETFLAG_CLEAR);
278     ANIM_channel_setting_set(&ac, ale, ACHANNEL_SETTING_SELECT, ACHANNEL_SETFLAG_CLEAR);
279 
280     /* now, also flush selection status up/down as appropriate */
281     ANIM_flush_setting_anim_channels(
282         &ac, &all_data, ale, ACHANNEL_SETTING_VISIBLE, ACHANNEL_SETFLAG_CLEAR);
283   }
284 
285   /* cleanup */
286   ANIM_animdata_freelist(&anim_data);
287   BLI_freelistN(&all_data);
288 
289   /* unhide selected */
290   if (unselected) {
291     /* turn off requirement for visible */
292     filter = ANIMFILTER_SEL | ANIMFILTER_NODUPLIS | ANIMFILTER_LIST_CHANNELS;
293 
294     /* flushing has been done */
295     ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
296 
297     for (ale = anim_data.first; ale; ale = ale->next) {
298       /* hack: skip object channels for now, since flushing those
299        * will always flush everything, but they are always included */
300 
301       /* TODO: find out why this is the case, and fix that */
302       if (ale->type == ANIMTYPE_OBJECT) {
303         continue;
304       }
305 
306       /* change the hide setting, and unselect it... */
307       ANIM_channel_setting_set(&ac, ale, ACHANNEL_SETTING_VISIBLE, ACHANNEL_SETFLAG_ADD);
308       ANIM_channel_setting_set(&ac, ale, ACHANNEL_SETTING_SELECT, ACHANNEL_SETFLAG_ADD);
309 
310       /* now, also flush selection status up/down as appropriate */
311       ANIM_flush_setting_anim_channels(
312           &ac, &anim_data, ale, ACHANNEL_SETTING_VISIBLE, ACHANNEL_SETFLAG_ADD);
313     }
314     ANIM_animdata_freelist(&anim_data);
315   }
316 
317   /* send notifier that things have changed */
318   WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
319 
320   return OPERATOR_FINISHED;
321 }
322 
GRAPH_OT_hide(wmOperatorType * ot)323 static void GRAPH_OT_hide(wmOperatorType *ot)
324 {
325   /* identifiers */
326   ot->name = "Hide Curves";
327   ot->idname = "GRAPH_OT_hide";
328   ot->description = "Hide selected curves from Graph Editor view";
329 
330   /* api callbacks */
331   ot->exec = graphview_curves_hide_exec;
332   ot->poll = ED_operator_graphedit_active;
333 
334   /* flags */
335   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
336 
337   /* props */
338   RNA_def_boolean(
339       ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected curves");
340 }
341 
342 /* ........ */
343 
graphview_curves_reveal_exec(bContext * C,wmOperator * op)344 static int graphview_curves_reveal_exec(bContext *C, wmOperator *op)
345 {
346   bAnimContext ac;
347   ListBase anim_data = {NULL, NULL};
348   ListBase all_data = {NULL, NULL};
349   bAnimListElem *ale;
350   int filter;
351   const bool select = RNA_boolean_get(op->ptr, "select");
352 
353   /* get editor data */
354   if (ANIM_animdata_get_context(C, &ac) == 0) {
355     return OPERATOR_CANCELLED;
356   }
357 
358   /* get list of all channels that selection may need to be flushed to
359    * - hierarchy must not affect what we have access to here...
360    */
361   filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_NODUPLIS);
362   ANIM_animdata_filter(&ac, &all_data, filter, ac.data, ac.datatype);
363 
364   /* filter data
365    * - just go through all visible channels, ensuring that everything is set to be curve-visible
366    */
367   filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_NODUPLIS);
368   ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
369 
370   for (ale = anim_data.first; ale; ale = ale->next) {
371     /* hack: skip object channels for now, since flushing those will always flush everything,
372      * but they are always included. */
373     /* TODO: find out why this is the case, and fix that */
374     if (ale->type == ANIMTYPE_OBJECT) {
375       continue;
376     }
377 
378     /* select if it is not visible */
379     if (ANIM_channel_setting_get(&ac, ale, ACHANNEL_SETTING_VISIBLE) == 0) {
380       ANIM_channel_setting_set(&ac,
381                                ale,
382                                ACHANNEL_SETTING_SELECT,
383                                select ? ACHANNEL_SETFLAG_ADD : ACHANNEL_SETFLAG_CLEAR);
384     }
385 
386     /* change the visibility setting */
387     ANIM_channel_setting_set(&ac, ale, ACHANNEL_SETTING_VISIBLE, ACHANNEL_SETFLAG_ADD);
388 
389     /* now, also flush selection status up/down as appropriate */
390     ANIM_flush_setting_anim_channels(&ac, &all_data, ale, ACHANNEL_SETTING_VISIBLE, true);
391   }
392 
393   /* cleanup */
394   ANIM_animdata_freelist(&anim_data);
395   BLI_freelistN(&all_data);
396 
397   /* send notifier that things have changed */
398   WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
399 
400   return OPERATOR_FINISHED;
401 }
402 
GRAPH_OT_reveal(wmOperatorType * ot)403 static void GRAPH_OT_reveal(wmOperatorType *ot)
404 {
405   /* identifiers */
406   ot->name = "Reveal Curves";
407   ot->idname = "GRAPH_OT_reveal";
408   ot->description = "Make previously hidden curves visible again in Graph Editor view";
409 
410   /* api callbacks */
411   ot->exec = graphview_curves_reveal_exec;
412   ot->poll = ED_operator_graphedit_active;
413 
414   /* flags */
415   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
416 
417   RNA_def_boolean(ot->srna, "select", true, "Select", "");
418 }
419 
420 /* ************************** registration - operator types **********************************/
421 
graphedit_operatortypes(void)422 void graphedit_operatortypes(void)
423 {
424   /* view */
425   WM_operatortype_append(GRAPH_OT_cursor_set);
426 
427   WM_operatortype_append(GRAPH_OT_previewrange_set);
428   WM_operatortype_append(GRAPH_OT_view_all);
429   WM_operatortype_append(GRAPH_OT_view_selected);
430   WM_operatortype_append(GRAPH_OT_view_frame);
431 
432   WM_operatortype_append(GRAPH_OT_ghost_curves_create);
433   WM_operatortype_append(GRAPH_OT_ghost_curves_clear);
434 
435   WM_operatortype_append(GRAPH_OT_hide);
436   WM_operatortype_append(GRAPH_OT_reveal);
437 
438   /* keyframes */
439   /* selection */
440   WM_operatortype_append(GRAPH_OT_clickselect);
441   WM_operatortype_append(GRAPH_OT_select_all);
442   WM_operatortype_append(GRAPH_OT_select_box);
443   WM_operatortype_append(GRAPH_OT_select_lasso);
444   WM_operatortype_append(GRAPH_OT_select_circle);
445   WM_operatortype_append(GRAPH_OT_select_column);
446   WM_operatortype_append(GRAPH_OT_select_linked);
447   WM_operatortype_append(GRAPH_OT_select_more);
448   WM_operatortype_append(GRAPH_OT_select_less);
449   WM_operatortype_append(GRAPH_OT_select_leftright);
450 
451   /* editing */
452   WM_operatortype_append(GRAPH_OT_snap);
453   WM_operatortype_append(GRAPH_OT_mirror);
454   WM_operatortype_append(GRAPH_OT_frame_jump);
455   WM_operatortype_append(GRAPH_OT_snap_cursor_value);
456   WM_operatortype_append(GRAPH_OT_handle_type);
457   WM_operatortype_append(GRAPH_OT_interpolation_type);
458   WM_operatortype_append(GRAPH_OT_extrapolation_type);
459   WM_operatortype_append(GRAPH_OT_easing_type);
460   WM_operatortype_append(GRAPH_OT_sample);
461   WM_operatortype_append(GRAPH_OT_bake);
462   WM_operatortype_append(GRAPH_OT_sound_bake);
463   WM_operatortype_append(GRAPH_OT_smooth);
464   WM_operatortype_append(GRAPH_OT_clean);
465   WM_operatortype_append(GRAPH_OT_decimate);
466   WM_operatortype_append(GRAPH_OT_euler_filter);
467   WM_operatortype_append(GRAPH_OT_delete);
468   WM_operatortype_append(GRAPH_OT_duplicate);
469 
470   WM_operatortype_append(GRAPH_OT_copy);
471   WM_operatortype_append(GRAPH_OT_paste);
472 
473   WM_operatortype_append(GRAPH_OT_keyframe_insert);
474   WM_operatortype_append(GRAPH_OT_click_insert);
475 
476   /* F-Curve Modifiers */
477   WM_operatortype_append(GRAPH_OT_fmodifier_add);
478   WM_operatortype_append(GRAPH_OT_fmodifier_copy);
479   WM_operatortype_append(GRAPH_OT_fmodifier_paste);
480 
481   /* Drivers */
482   WM_operatortype_append(GRAPH_OT_driver_variables_copy);
483   WM_operatortype_append(GRAPH_OT_driver_variables_paste);
484   WM_operatortype_append(GRAPH_OT_driver_delete_invalid);
485 }
486 
ED_operatormacros_graph(void)487 void ED_operatormacros_graph(void)
488 {
489   wmOperatorType *ot;
490   wmOperatorTypeMacro *otmacro;
491 
492   ot = WM_operatortype_append_macro("GRAPH_OT_duplicate_move",
493                                     "Duplicate",
494                                     "Make a copy of all selected keyframes and move them",
495                                     OPTYPE_UNDO | OPTYPE_REGISTER);
496   WM_operatortype_macro_define(ot, "GRAPH_OT_duplicate");
497   otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_transform");
498   RNA_enum_set(otmacro->ptr, "mode", TFM_TIME_DUPLICATE);
499   RNA_boolean_set(otmacro->ptr, "use_proportional_edit", false);
500 }
501 
502 /* ************************** registration - keymaps **********************************/
503 
graphedit_keymap(wmKeyConfig * keyconf)504 void graphedit_keymap(wmKeyConfig *keyconf)
505 {
506   /* keymap for all regions */
507   WM_keymap_ensure(keyconf, "Graph Editor Generic", SPACE_GRAPH, 0);
508 
509   /* channels */
510   /* Channels are not directly handled by the Graph Editor module,
511    * but are inherited from the Animation module.
512    * All the relevant operations, keymaps, drawing, etc.
513    * can therefore all be found in that module instead,
514    * as these are all used for the Graph Editor too.
515    */
516 
517   /* keyframes */
518   WM_keymap_ensure(keyconf, "Graph Editor", SPACE_GRAPH, 0);
519 }
520