Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_node / node_ops.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation, Nathan Letwory
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_node/node_ops.c
28  *  \ingroup spnode
29  */
30
31
32 #include "DNA_node_types.h"
33
34 #include "BLI_utildefines.h"
35
36 #include "BKE_context.h"
37
38 #include "ED_node.h"  /* own include */
39 #include "ED_screen.h"
40 #include "ED_transform.h"
41
42 #include "RNA_access.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "node_intern.h"  /* own include */
48
49 void node_operatortypes(void)
50 {
51         WM_operatortype_append(NODE_OT_properties);
52         WM_operatortype_append(NODE_OT_toolbar);
53
54         WM_operatortype_append(NODE_OT_select);
55         WM_operatortype_append(NODE_OT_select_all);
56         WM_operatortype_append(NODE_OT_select_linked_to);
57         WM_operatortype_append(NODE_OT_select_linked_from);
58         WM_operatortype_append(NODE_OT_select_border);
59         WM_operatortype_append(NODE_OT_select_circle);
60         WM_operatortype_append(NODE_OT_select_lasso);
61         WM_operatortype_append(NODE_OT_select_grouped);
62         WM_operatortype_append(NODE_OT_select_same_type_step);
63
64         WM_operatortype_append(NODE_OT_find_node);
65
66         WM_operatortype_append(NODE_OT_view_all);
67         WM_operatortype_append(NODE_OT_view_selected);
68
69         WM_operatortype_append(NODE_OT_mute_toggle);
70         WM_operatortype_append(NODE_OT_hide_toggle);
71         WM_operatortype_append(NODE_OT_preview_toggle);
72         WM_operatortype_append(NODE_OT_options_toggle);
73         WM_operatortype_append(NODE_OT_hide_socket_toggle);
74         WM_operatortype_append(NODE_OT_node_copy_color);
75
76         WM_operatortype_append(NODE_OT_duplicate);
77         WM_operatortype_append(NODE_OT_delete);
78         WM_operatortype_append(NODE_OT_delete_reconnect);
79         WM_operatortype_append(NODE_OT_resize);
80
81         WM_operatortype_append(NODE_OT_link);
82         WM_operatortype_append(NODE_OT_link_make);
83         WM_operatortype_append(NODE_OT_links_cut);
84         WM_operatortype_append(NODE_OT_links_detach);
85         WM_operatortype_append(NODE_OT_add_reroute);
86
87         WM_operatortype_append(NODE_OT_group_make);
88         WM_operatortype_append(NODE_OT_group_insert);
89         WM_operatortype_append(NODE_OT_group_ungroup);
90         WM_operatortype_append(NODE_OT_group_separate);
91         WM_operatortype_append(NODE_OT_group_edit);
92
93         WM_operatortype_append(NODE_OT_link_viewer);
94
95         WM_operatortype_append(NODE_OT_insert_offset);
96
97         WM_operatortype_append(NODE_OT_read_viewlayers);
98         WM_operatortype_append(NODE_OT_render_changed);
99
100         WM_operatortype_append(NODE_OT_backimage_move);
101         WM_operatortype_append(NODE_OT_backimage_zoom);
102         WM_operatortype_append(NODE_OT_backimage_fit);
103         WM_operatortype_append(NODE_OT_backimage_sample);
104
105         WM_operatortype_append(NODE_OT_add_file);
106         WM_operatortype_append(NODE_OT_add_mask);
107
108         WM_operatortype_append(NODE_OT_new_node_tree);
109
110         WM_operatortype_append(NODE_OT_output_file_add_socket);
111         WM_operatortype_append(NODE_OT_output_file_remove_active_socket);
112         WM_operatortype_append(NODE_OT_output_file_move_active_socket);
113
114         WM_operatortype_append(NODE_OT_parent_set);
115         WM_operatortype_append(NODE_OT_join);
116         WM_operatortype_append(NODE_OT_attach);
117         WM_operatortype_append(NODE_OT_detach);
118
119         WM_operatortype_append(NODE_OT_clipboard_copy);
120         WM_operatortype_append(NODE_OT_clipboard_paste);
121
122         WM_operatortype_append(NODE_OT_shader_script_update);
123
124         WM_operatortype_append(NODE_OT_viewer_border);
125         WM_operatortype_append(NODE_OT_clear_viewer_border);
126
127         WM_operatortype_append(NODE_OT_switch_view_update);
128
129         WM_operatortype_append(NODE_OT_tree_socket_add);
130         WM_operatortype_append(NODE_OT_tree_socket_remove);
131         WM_operatortype_append(NODE_OT_tree_socket_move);
132 }
133
134 void ED_operatormacros_node(void)
135 {
136         wmOperatorType *ot;
137         wmOperatorTypeMacro *mot;
138
139         ot = WM_operatortype_append_macro("NODE_OT_select_link_viewer", "Link Viewer",
140                                           "Select node and link it to a viewer node",
141                                           OPTYPE_UNDO);
142         WM_operatortype_macro_define(ot, "NODE_OT_select");
143         WM_operatortype_macro_define(ot, "NODE_OT_link_viewer");
144
145         ot = WM_operatortype_append_macro("NODE_OT_translate_attach", "Move and Attach",
146                                           "Move nodes and attach to frame",
147                                           OPTYPE_UNDO | OPTYPE_REGISTER);
148         mot = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
149         RNA_boolean_set(mot->ptr, "release_confirm", true);
150         WM_operatortype_macro_define(ot, "NODE_OT_attach");
151         WM_operatortype_macro_define(ot, "NODE_OT_insert_offset");
152
153         /* NODE_OT_translate_attach with remove_on_canel set to true */
154         ot = WM_operatortype_append_macro("NODE_OT_translate_attach_remove_on_cancel", "Move and Attach",
155                                           "Move nodes and attach to frame",
156                                           OPTYPE_UNDO | OPTYPE_REGISTER);
157         mot = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
158         RNA_boolean_set(mot->ptr, "release_confirm", true);
159         RNA_boolean_set(mot->ptr, "remove_on_cancel", true);
160         WM_operatortype_macro_define(ot, "NODE_OT_attach");
161         WM_operatortype_macro_define(ot, "NODE_OT_insert_offset");
162
163         /* Note: Currently not in a default keymap or menu due to messy keymaps
164          * and tricky invoke functionality.
165          * Kept around in case users want to make own shortcuts.
166          */
167         ot = WM_operatortype_append_macro("NODE_OT_detach_translate_attach", "Detach and Move",
168                                           "Detach nodes, move and attach to frame",
169                                           OPTYPE_UNDO | OPTYPE_REGISTER);
170         WM_operatortype_macro_define(ot, "NODE_OT_detach");
171         mot = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
172         RNA_boolean_set(mot->ptr, "release_confirm", true);
173         WM_operatortype_macro_define(ot, "NODE_OT_attach");
174
175         ot = WM_operatortype_append_macro("NODE_OT_duplicate_move", "Duplicate",
176                                           "Duplicate selected nodes and move them",
177                                           OPTYPE_UNDO | OPTYPE_REGISTER);
178         WM_operatortype_macro_define(ot, "NODE_OT_duplicate");
179         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
180
181         /* modified operator call for duplicating with input links */
182         ot = WM_operatortype_append_macro("NODE_OT_duplicate_move_keep_inputs", "Duplicate",
183                                           "Duplicate selected nodes keeping input links and move them",
184                                           OPTYPE_UNDO | OPTYPE_REGISTER);
185         mot = WM_operatortype_macro_define(ot, "NODE_OT_duplicate");
186         RNA_boolean_set(mot->ptr, "keep_inputs", true);
187         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
188
189         ot = WM_operatortype_append_macro("NODE_OT_move_detach_links", "Detach", "Move a node to detach links",
190                                           OPTYPE_UNDO | OPTYPE_REGISTER);
191         WM_operatortype_macro_define(ot, "NODE_OT_links_detach");
192         WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
193         WM_operatortype_macro_define(ot, "NODE_OT_insert_offset");
194
195         ot = WM_operatortype_append_macro("NODE_OT_move_detach_links_release", "Detach", "Move a node to detach links",
196                                           OPTYPE_UNDO | OPTYPE_REGISTER);
197         WM_operatortype_macro_define(ot, "NODE_OT_links_detach");
198         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
199 }
200
201 /* helper function for repetitive select operator keymap */
202 static void node_select_keymap(wmKeyMap *keymap, int extend)
203 {
204         /* modifier combinations */
205         const int mod_single[] = { 0, KM_CTRL, KM_ALT, KM_CTRL | KM_ALT,
206                                    -1 /* terminator */
207         };
208         const int mod_extend[] = { KM_SHIFT, KM_SHIFT | KM_CTRL,
209                                    KM_SHIFT | KM_ALT, KM_SHIFT | KM_CTRL | KM_ALT,
210                                    -1 /* terminator */
211         };
212         const int *mod = (extend ? mod_extend : mod_single);
213         wmKeyMapItem *kmi;
214         int i;
215
216         for (i = 0; mod[i] >= 0; ++i) {
217                 kmi = WM_keymap_add_item(keymap, "NODE_OT_select", ACTIONMOUSE, KM_PRESS, mod[i], 0);
218                 RNA_boolean_set(kmi->ptr, "extend", extend);
219                 kmi = WM_keymap_add_item(keymap, "NODE_OT_select", SELECTMOUSE, KM_PRESS, mod[i], 0);
220                 RNA_boolean_set(kmi->ptr, "extend", extend);
221         }
222 }
223
224 void node_keymap(struct wmKeyConfig *keyconf)
225 {
226         wmKeyMap *keymap;
227         wmKeyMapItem *kmi;
228
229         /* Entire Editor only ----------------- */
230         keymap = WM_keymap_find(keyconf, "Node Generic", SPACE_NODE, 0);
231
232         WM_keymap_add_item(keymap, "NODE_OT_properties", NKEY, KM_PRESS, 0, 0);
233         WM_keymap_add_item(keymap, "NODE_OT_toolbar", TKEY, KM_PRESS, 0, 0);
234
235         /* Main Region only ----------------- */
236         keymap = WM_keymap_find(keyconf, "Node Editor", SPACE_NODE, 0);
237
238         /* mouse select in nodes used to be both keys, but perhaps this should be reduced?
239          * NOTE: mouse-clicks on left-mouse will fall through to allow transform-tweak, but also link/resize
240          * NOTE 2: socket select is part of the node select operator, to handle overlapping cases
241          * NOTE 3: select op is registered for various combinations of modifier key, so the specialized
242          *         grab operators (unlink, attach, etc.) can work easily on single nodes.
243          */
244         node_select_keymap(keymap, false);
245         node_select_keymap(keymap, true);
246
247         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_border", EVT_TWEAK_S, KM_ANY, 0, 0);
248         RNA_boolean_set(kmi->ptr, "tweak", true);
249
250         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
251         RNA_boolean_set(kmi->ptr, "deselect", false);
252         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
253         RNA_boolean_set(kmi->ptr, "deselect", true);
254
255         WM_keymap_add_item(keymap, "NODE_OT_select_circle", CKEY, KM_PRESS, 0, 0);
256
257         /* each of these falls through if not handled... */
258         kmi = WM_keymap_add_item(keymap, "NODE_OT_link", LEFTMOUSE, KM_PRESS, 0, 0);
259         RNA_boolean_set(kmi->ptr, "detach", false);
260         kmi = WM_keymap_add_item(keymap, "NODE_OT_link", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
261         RNA_boolean_set(kmi->ptr, "detach", true);
262
263         WM_keymap_add_item(keymap, "NODE_OT_resize", LEFTMOUSE, KM_PRESS, 0, 0);
264
265         WM_keymap_add_item(keymap, "NODE_OT_add_reroute", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
266         WM_keymap_add_item(keymap, "NODE_OT_links_cut", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
267         WM_keymap_add_item(keymap, "NODE_OT_select_link_viewer", LEFTMOUSE, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
268
269         WM_keymap_add_item(keymap, "NODE_OT_backimage_move", MIDDLEMOUSE, KM_PRESS, KM_ALT, 0);
270         kmi = WM_keymap_add_item(keymap, "NODE_OT_backimage_zoom", VKEY, KM_PRESS, 0, 0);
271         RNA_float_set(kmi->ptr, "factor", 0.83333f);
272         kmi = WM_keymap_add_item(keymap, "NODE_OT_backimage_zoom", VKEY, KM_PRESS, KM_ALT, 0);
273         RNA_float_set(kmi->ptr, "factor", 1.2f);
274         WM_keymap_add_item(keymap, "NODE_OT_backimage_fit", HOMEKEY, KM_PRESS, KM_ALT, 0);
275         WM_keymap_add_item(keymap, "NODE_OT_backimage_sample", ACTIONMOUSE, KM_PRESS, KM_ALT, 0);
276
277         kmi = WM_keymap_add_item(keymap, "NODE_OT_link_make", FKEY, KM_PRESS, 0, 0);
278         RNA_boolean_set(kmi->ptr, "replace", false);
279         kmi = WM_keymap_add_item(keymap, "NODE_OT_link_make", FKEY, KM_PRESS, KM_SHIFT, 0);
280         RNA_boolean_set(kmi->ptr, "replace", true);
281
282         WM_keymap_add_menu(keymap, "NODE_MT_add", AKEY, KM_PRESS, KM_SHIFT, 0);
283         WM_keymap_add_item(keymap, "NODE_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
284         /* modified operator call for duplicating with input links */
285         WM_keymap_add_item(keymap, "NODE_OT_duplicate_move_keep_inputs", DKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
286
287         WM_keymap_add_item(keymap, "NODE_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
288         WM_keymap_add_item(keymap, "NODE_OT_detach", PKEY, KM_PRESS, KM_ALT, 0);
289         WM_keymap_add_item(keymap, "NODE_OT_join", JKEY, KM_PRESS, KM_CTRL, 0);
290
291         WM_keymap_add_item(keymap, "NODE_OT_hide_toggle", HKEY, KM_PRESS, 0, 0);
292         WM_keymap_add_item(keymap, "NODE_OT_mute_toggle", MKEY, KM_PRESS, 0, 0);
293         WM_keymap_add_item(keymap, "NODE_OT_preview_toggle", HKEY, KM_PRESS, KM_SHIFT, 0);
294         WM_keymap_add_item(keymap, "NODE_OT_hide_socket_toggle", HKEY, KM_PRESS, KM_CTRL, 0);
295
296         WM_keymap_add_item(keymap, "NODE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
297 #ifdef WITH_INPUT_NDOF
298         WM_keymap_add_item(keymap, "NODE_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0);
299 #endif
300         WM_keymap_add_item(keymap, "NODE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
301
302         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_border", BKEY, KM_PRESS, 0, 0);
303         RNA_boolean_set(kmi->ptr, "tweak", false);
304
305         WM_keymap_add_item(keymap, "NODE_OT_delete", XKEY, KM_PRESS, 0, 0);
306         WM_keymap_add_item(keymap, "NODE_OT_delete", DELKEY, KM_PRESS, 0, 0);
307         WM_keymap_add_item(keymap, "NODE_OT_delete_reconnect", XKEY, KM_PRESS, KM_CTRL, 0);
308
309         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_all", AKEY, KM_PRESS, 0, 0);
310         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
311         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
312         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
313
314         WM_keymap_add_item(keymap, "NODE_OT_select_linked_to", LKEY, KM_PRESS, KM_SHIFT, 0);
315         WM_keymap_add_item(keymap, "NODE_OT_select_linked_from", LKEY, KM_PRESS, 0, 0);
316         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
317         RNA_boolean_set(kmi->ptr, "extend", false);
318         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_grouped", GKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
319         RNA_boolean_set(kmi->ptr, "extend", true);
320
321         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_same_type_step", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
322         RNA_boolean_set(kmi->ptr, "prev", false);
323         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_same_type_step", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
324         RNA_boolean_set(kmi->ptr, "prev", true);
325
326         WM_keymap_add_item(keymap, "NODE_OT_find_node", FKEY, KM_PRESS, KM_CTRL, 0);
327
328         /* node group operators */
329         WM_keymap_add_item(keymap, "NODE_OT_group_make", GKEY, KM_PRESS, KM_CTRL, 0);
330         WM_keymap_add_item(keymap, "NODE_OT_group_ungroup", GKEY, KM_PRESS, KM_ALT, 0);
331         WM_keymap_add_item(keymap, "NODE_OT_group_separate", PKEY, KM_PRESS, 0, 0);
332         kmi = WM_keymap_add_item(keymap, "NODE_OT_group_edit", TABKEY, KM_PRESS, 0, 0);
333         RNA_boolean_set(kmi->ptr, "exit", false);
334         kmi = WM_keymap_add_item(keymap, "NODE_OT_group_edit", TABKEY, KM_PRESS, KM_CTRL, 0);
335         RNA_boolean_set(kmi->ptr, "exit", true);
336
337         WM_keymap_add_item(keymap, "NODE_OT_read_viewlayers", RKEY, KM_PRESS, KM_CTRL, 0);
338         WM_keymap_add_item(keymap, "NODE_OT_render_changed", ZKEY, KM_PRESS, 0, 0);
339
340         WM_keymap_add_item(keymap, "NODE_OT_clipboard_copy", CKEY, KM_PRESS, KM_CTRL, 0);
341         WM_keymap_add_item(keymap, "NODE_OT_clipboard_paste", VKEY, KM_PRESS, KM_CTRL, 0);
342 #ifdef __APPLE__
343         WM_keymap_add_item(keymap, "NODE_OT_clipboard_copy", CKEY, KM_PRESS, KM_OSKEY, 0);
344         WM_keymap_add_item(keymap, "NODE_OT_clipboard_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
345 #endif
346         WM_keymap_add_item(keymap, "NODE_OT_viewer_border", BKEY, KM_PRESS, KM_CTRL, 0);
347         WM_keymap_add_item(keymap, "NODE_OT_clear_viewer_border", BKEY, KM_PRESS, KM_ALT | KM_CTRL, 0);
348
349         transform_keymap_for_space(keyconf, keymap, SPACE_NODE);
350 }