195dd60b72a14be893732fe565e041300166fdcb
[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 "BKE_context.h"
35
36 #include "BLI_utildefines.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         
53         WM_operatortype_append(NODE_OT_select);
54         WM_operatortype_append(NODE_OT_select_all);
55         WM_operatortype_append(NODE_OT_select_linked_to);
56         WM_operatortype_append(NODE_OT_select_linked_from);
57         WM_operatortype_append(NODE_OT_select_border);
58         WM_operatortype_append(NODE_OT_select_same_type);
59         WM_operatortype_append(NODE_OT_select_same_type_next);
60         WM_operatortype_append(NODE_OT_select_same_type_prev);
61
62         WM_operatortype_append(NODE_OT_view_all);
63         WM_operatortype_append(NODE_OT_view_selected);
64
65         WM_operatortype_append(NODE_OT_mute_toggle);
66         WM_operatortype_append(NODE_OT_hide_toggle);
67         WM_operatortype_append(NODE_OT_preview_toggle);
68         WM_operatortype_append(NODE_OT_options_toggle);
69         WM_operatortype_append(NODE_OT_hide_socket_toggle);
70         WM_operatortype_append(NODE_OT_show_cyclic_dependencies);
71         WM_operatortype_append(NODE_OT_node_copy_color);
72         
73         WM_operatortype_append(NODE_OT_duplicate);
74         WM_operatortype_append(NODE_OT_delete);
75         WM_operatortype_append(NODE_OT_delete_reconnect);
76         WM_operatortype_append(NODE_OT_resize);
77         
78         WM_operatortype_append(NODE_OT_link);
79         WM_operatortype_append(NODE_OT_link_make);
80         WM_operatortype_append(NODE_OT_links_cut);
81         WM_operatortype_append(NODE_OT_links_detach);
82         WM_operatortype_append(NODE_OT_add_reroute);
83
84         WM_operatortype_append(NODE_OT_group_make);
85         WM_operatortype_append(NODE_OT_group_ungroup);
86         WM_operatortype_append(NODE_OT_group_separate);
87         WM_operatortype_append(NODE_OT_group_edit);
88         WM_operatortype_append(NODE_OT_group_socket_add);
89         WM_operatortype_append(NODE_OT_group_socket_remove);
90         WM_operatortype_append(NODE_OT_group_socket_move_up);
91         WM_operatortype_append(NODE_OT_group_socket_move_down);
92         
93         WM_operatortype_append(NODE_OT_link_viewer);
94         
95         WM_operatortype_append(NODE_OT_read_renderlayers);
96         WM_operatortype_append(NODE_OT_read_fullsamplelayers);
97         WM_operatortype_append(NODE_OT_render_changed);
98         
99         WM_operatortype_append(NODE_OT_backimage_move);
100         WM_operatortype_append(NODE_OT_backimage_zoom);
101         WM_operatortype_append(NODE_OT_backimage_sample);
102         
103         WM_operatortype_append(NODE_OT_add_file);
104         
105         WM_operatortype_append(NODE_OT_new_node_tree);
106         
107         WM_operatortype_append(NODE_OT_output_file_add_socket);
108         WM_operatortype_append(NODE_OT_output_file_remove_active_socket);
109         WM_operatortype_append(NODE_OT_output_file_move_active_socket);
110         
111         WM_operatortype_append(NODE_OT_parent_set);
112         WM_operatortype_append(NODE_OT_parent_clear);
113         WM_operatortype_append(NODE_OT_join);
114         WM_operatortype_append(NODE_OT_attach);
115         WM_operatortype_append(NODE_OT_detach);
116         
117         WM_operatortype_append(NODE_OT_clipboard_copy);
118         WM_operatortype_append(NODE_OT_clipboard_paste);
119 }
120
121 void ED_operatormacros_node(void)
122 {
123         wmOperatorType *ot;
124         wmOperatorTypeMacro *mot;
125         
126         ot = WM_operatortype_append_macro("NODE_OT_select_link_viewer", "Link Viewer",
127                                           "Select node and link it to a viewer node",
128                                           OPTYPE_UNDO);
129         WM_operatortype_macro_define(ot, "NODE_OT_select");
130         WM_operatortype_macro_define(ot, "NODE_OT_link_viewer");
131
132         ot = WM_operatortype_append_macro("NODE_OT_translate_attach", "Move and Attach",
133                                           "Move nodes and attach to frame",
134                                           OPTYPE_UNDO | OPTYPE_REGISTER);
135         mot = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
136         RNA_boolean_set(mot->ptr, "release_confirm", TRUE);
137         WM_operatortype_macro_define(ot, "NODE_OT_attach");
138
139         ot = WM_operatortype_append_macro("NODE_OT_detach_translate_attach", "Detach and Move",
140                                           "Detach nodes, move and attach to frame",
141                                           OPTYPE_UNDO | OPTYPE_REGISTER);
142         WM_operatortype_macro_define(ot, "NODE_OT_detach");
143         mot = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
144         RNA_boolean_set(mot->ptr, "release_confirm", TRUE);
145         WM_operatortype_macro_define(ot, "NODE_OT_attach");
146
147         ot = WM_operatortype_append_macro("NODE_OT_duplicate_move", "Duplicate",
148                                           "Duplicate selected nodes and move them",
149                                           OPTYPE_UNDO | OPTYPE_REGISTER);
150         WM_operatortype_macro_define(ot, "NODE_OT_duplicate");
151         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
152
153         /* modified operator call for duplicating with input links */
154         ot = WM_operatortype_append_macro("NODE_OT_duplicate_move_keep_inputs", "Duplicate",
155                                           "Duplicate selected nodes keeping input links and move them",
156                                           OPTYPE_UNDO | OPTYPE_REGISTER);
157         mot = WM_operatortype_macro_define(ot, "NODE_OT_duplicate");
158         RNA_boolean_set(mot->ptr, "keep_inputs", TRUE);
159         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
160
161         ot = WM_operatortype_append_macro("NODE_OT_move_detach_links", "Detach", "Move a node to detach links",
162                                           OPTYPE_UNDO | OPTYPE_REGISTER);
163         WM_operatortype_macro_define(ot, "NODE_OT_links_detach");
164         WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
165
166         ot = WM_operatortype_append_macro("NODE_OT_move_detach_links_release", "Detach", "Move a node to detach links",
167                                           OPTYPE_UNDO | OPTYPE_REGISTER);
168         WM_operatortype_macro_define(ot, "NODE_OT_links_detach");
169         mot = WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
170 }
171
172 /* helper function for repetitive select operator keymap */
173 static void node_select_keymap(wmKeyMap *keymap, int extend)
174 {
175         /* modifier combinations */
176         const int mod_single[] = { 0, KM_CTRL, KM_ALT, KM_CTRL | KM_ALT,
177                                    -1 /* terminator */
178         };
179         const int mod_extend[] = { KM_SHIFT, KM_SHIFT | KM_CTRL,
180                                    KM_SHIFT | KM_ALT, KM_SHIFT | KM_CTRL | KM_ALT,
181                                    -1 /* terminator */
182         };
183         const int *mod = (extend ? mod_extend : mod_single);
184         wmKeyMapItem *kmi;
185         int i;
186         
187         for (i = 0; mod[i] >= 0; ++i) {
188                 kmi = WM_keymap_add_item(keymap, "NODE_OT_select", ACTIONMOUSE, KM_PRESS, mod[i], 0);
189                 RNA_boolean_set(kmi->ptr, "extend", extend);
190                 kmi = WM_keymap_add_item(keymap, "NODE_OT_select", SELECTMOUSE, KM_PRESS, mod[i], 0);
191                 RNA_boolean_set(kmi->ptr, "extend", extend);
192         }
193 }
194
195 void node_keymap(struct wmKeyConfig *keyconf)
196 {
197         wmKeyMap *keymap;
198         wmKeyMapItem *kmi;
199         
200         /* Entire Editor only ----------------- */
201         keymap = WM_keymap_find(keyconf, "Node Generic", SPACE_NODE, 0);
202         
203         WM_keymap_add_item(keymap, "NODE_OT_properties", NKEY, KM_PRESS, 0, 0);
204         
205         /* Main Area only ----------------- */
206         keymap = WM_keymap_find(keyconf, "Node Editor", SPACE_NODE, 0);
207         
208         /* mouse select in nodes used to be both keys, but perhaps this should be reduced? 
209          * NOTE: mouse-clicks on left-mouse will fall through to allow transform-tweak, but also link/resize
210          * NOTE 2: socket select is part of the node select operator, to handle overlapping cases
211          * NOTE 3: select op is registered for various combinations of modifier key, so the specialized
212          *         grab operators (unlink, attach, etc.) can work easily on single nodes.
213          */
214         node_select_keymap(keymap, FALSE);
215         node_select_keymap(keymap, TRUE);
216         
217         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_border", EVT_TWEAK_S, KM_ANY, 0, 0);
218         RNA_boolean_set(kmi->ptr, "tweak", TRUE);
219         
220         /* each of these falls through if not handled... */
221         WM_keymap_add_item(keymap, "NODE_OT_link", LEFTMOUSE, KM_PRESS, 0, 0);
222         kmi = WM_keymap_add_item(keymap, "NODE_OT_link", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
223         RNA_boolean_set(kmi->ptr, "detach", TRUE);
224         WM_keymap_add_item(keymap, "NODE_OT_resize", LEFTMOUSE, KM_PRESS, 0, 0);
225         
226         WM_keymap_add_item(keymap, "NODE_OT_add_reroute", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
227         WM_keymap_add_item(keymap, "NODE_OT_links_cut", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
228         WM_keymap_add_item(keymap, "NODE_OT_select_link_viewer", LEFTMOUSE, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
229         
230         WM_keymap_add_item(keymap, "NODE_OT_backimage_move", MIDDLEMOUSE, KM_PRESS, KM_ALT, 0);
231         kmi = WM_keymap_add_item(keymap, "NODE_OT_backimage_zoom", VKEY, KM_PRESS, 0, 0);
232         RNA_float_set(kmi->ptr, "factor", 0.83333f);
233         kmi = WM_keymap_add_item(keymap, "NODE_OT_backimage_zoom", VKEY, KM_PRESS, KM_ALT, 0);
234         RNA_float_set(kmi->ptr, "factor", 1.2f);
235         WM_keymap_add_item(keymap, "NODE_OT_backimage_sample", ACTIONMOUSE, KM_PRESS, KM_ALT, 0);
236
237         kmi = WM_keymap_add_item(keymap, "NODE_OT_link_make", FKEY, KM_PRESS, 0, 0);
238         RNA_boolean_set(kmi->ptr, "replace", FALSE);
239         kmi = WM_keymap_add_item(keymap, "NODE_OT_link_make", FKEY, KM_PRESS, KM_CTRL, 0);
240         RNA_boolean_set(kmi->ptr, "replace", TRUE);
241
242         WM_keymap_add_menu(keymap, "NODE_MT_add", AKEY, KM_PRESS, KM_SHIFT, 0);
243         WM_keymap_add_item(keymap, "NODE_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
244         /* modified operator call for duplicating with input links */
245         WM_keymap_add_item(keymap, "NODE_OT_duplicate_move_keep_inputs", DKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
246         
247         WM_keymap_add_item(keymap, "NODE_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
248         WM_keymap_add_item(keymap, "NODE_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
249         WM_keymap_add_item(keymap, "NODE_OT_join", JKEY, KM_PRESS, KM_CTRL, 0);
250         
251         WM_keymap_add_item(keymap, "NODE_OT_hide_toggle", HKEY, KM_PRESS, 0, 0);
252         WM_keymap_add_item(keymap, "NODE_OT_mute_toggle", MKEY, KM_PRESS, 0, 0);
253         WM_keymap_add_item(keymap, "NODE_OT_preview_toggle", HKEY, KM_PRESS, KM_SHIFT, 0);
254         WM_keymap_add_item(keymap, "NODE_OT_hide_socket_toggle", HKEY, KM_PRESS, KM_CTRL, 0);
255         
256         WM_keymap_add_item(keymap, "NODE_OT_show_cyclic_dependencies", CKEY, KM_PRESS, 0, 0);
257         
258         WM_keymap_add_item(keymap, "NODE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
259         WM_keymap_add_item(keymap, "NODE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
260
261         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_border", BKEY, KM_PRESS, 0, 0);
262         RNA_boolean_set(kmi->ptr, "tweak", FALSE);
263
264         WM_keymap_add_item(keymap, "NODE_OT_delete", XKEY, KM_PRESS, 0, 0);
265         WM_keymap_add_item(keymap, "NODE_OT_delete", DELKEY, KM_PRESS, 0, 0);
266         WM_keymap_add_item(keymap, "NODE_OT_delete_reconnect", XKEY, KM_PRESS, KM_CTRL, 0);
267
268         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_all", AKEY, KM_PRESS, 0, 0);
269         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
270         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
271         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
272
273         WM_keymap_add_item(keymap, "NODE_OT_select_linked_to", LKEY, KM_PRESS, KM_SHIFT, 0);
274         WM_keymap_add_item(keymap, "NODE_OT_select_linked_from", LKEY, KM_PRESS, 0, 0);
275         WM_keymap_add_item(keymap, "NODE_OT_select_same_type", GKEY, KM_PRESS, KM_SHIFT, 0);
276         WM_keymap_add_item(keymap, "NODE_OT_select_same_type_next", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
277         WM_keymap_add_item(keymap, "NODE_OT_select_same_type_prev", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
278
279         WM_keymap_add_item(keymap, "NODE_OT_group_make", GKEY, KM_PRESS, KM_CTRL, 0);
280         WM_keymap_add_item(keymap, "NODE_OT_group_ungroup", GKEY, KM_PRESS, KM_ALT, 0);
281         WM_keymap_add_item(keymap, "NODE_OT_group_separate", PKEY, KM_PRESS, 0, 0);
282         WM_keymap_add_item(keymap, "NODE_OT_group_edit", TABKEY, KM_PRESS, 0, 0);
283         
284         WM_keymap_add_item(keymap, "NODE_OT_read_renderlayers", RKEY, KM_PRESS, KM_CTRL, 0);
285         WM_keymap_add_item(keymap, "NODE_OT_read_fullsamplelayers", RKEY, KM_PRESS, KM_SHIFT, 0);
286         WM_keymap_add_item(keymap, "NODE_OT_render_changed", ZKEY, KM_PRESS, 0, 0);
287         
288         WM_keymap_add_item(keymap, "NODE_OT_clipboard_copy", CKEY, KM_PRESS, KM_CTRL, 0);
289         WM_keymap_add_item(keymap, "NODE_OT_clipboard_paste", VKEY, KM_PRESS, KM_CTRL, 0);
290         
291         transform_keymap_for_space(keyconf, keymap, SPACE_NODE);
292 }