Patch #36622, by Henrik Aarnio: Fit backdrop image to the area dimensions.
[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_lasso);
60         WM_operatortype_append(NODE_OT_select_same_type);
61         WM_operatortype_append(NODE_OT_select_same_type_step);
62         
63         WM_operatortype_append(NODE_OT_find_node);
64         
65         WM_operatortype_append(NODE_OT_view_all);
66         WM_operatortype_append(NODE_OT_view_selected);
67
68         WM_operatortype_append(NODE_OT_mute_toggle);
69         WM_operatortype_append(NODE_OT_hide_toggle);
70         WM_operatortype_append(NODE_OT_preview_toggle);
71         WM_operatortype_append(NODE_OT_options_toggle);
72         WM_operatortype_append(NODE_OT_hide_socket_toggle);
73         WM_operatortype_append(NODE_OT_show_cyclic_dependencies);
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_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_fit);
102         WM_operatortype_append(NODE_OT_backimage_sample);
103         
104         WM_operatortype_append(NODE_OT_add_file);
105         WM_operatortype_append(NODE_OT_add_mask);
106         
107         WM_operatortype_append(NODE_OT_new_node_tree);
108         
109         WM_operatortype_append(NODE_OT_output_file_add_socket);
110         WM_operatortype_append(NODE_OT_output_file_remove_active_socket);
111         WM_operatortype_append(NODE_OT_output_file_move_active_socket);
112         
113         WM_operatortype_append(NODE_OT_parent_set);
114         WM_operatortype_append(NODE_OT_parent_clear);
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
126         WM_operatortype_append(NODE_OT_tree_socket_add);
127         WM_operatortype_append(NODE_OT_tree_socket_remove);
128         WM_operatortype_append(NODE_OT_tree_socket_move);
129 }
130
131 void ED_operatormacros_node(void)
132 {
133         wmOperatorType *ot;
134         wmOperatorTypeMacro *mot;
135         
136         ot = WM_operatortype_append_macro("NODE_OT_select_link_viewer", "Link Viewer",
137                                           "Select node and link it to a viewer node",
138                                           OPTYPE_UNDO);
139         WM_operatortype_macro_define(ot, "NODE_OT_select");
140         WM_operatortype_macro_define(ot, "NODE_OT_link_viewer");
141
142         ot = WM_operatortype_append_macro("NODE_OT_translate_attach", "Move and Attach",
143                                           "Move nodes and attach to frame",
144                                           OPTYPE_UNDO | OPTYPE_REGISTER);
145         mot = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
146         RNA_boolean_set(mot->ptr, "release_confirm", TRUE);
147         WM_operatortype_macro_define(ot, "NODE_OT_attach");
148
149         ot = WM_operatortype_append_macro("NODE_OT_detach_translate_attach", "Detach and Move",
150                                           "Detach nodes, move and attach to frame",
151                                           OPTYPE_UNDO | OPTYPE_REGISTER);
152         WM_operatortype_macro_define(ot, "NODE_OT_detach");
153         mot = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
154         RNA_boolean_set(mot->ptr, "release_confirm", TRUE);
155         WM_operatortype_macro_define(ot, "NODE_OT_attach");
156
157         ot = WM_operatortype_append_macro("NODE_OT_duplicate_move", "Duplicate",
158                                           "Duplicate selected nodes and move them",
159                                           OPTYPE_UNDO | OPTYPE_REGISTER);
160         WM_operatortype_macro_define(ot, "NODE_OT_duplicate");
161         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
162
163         /* modified operator call for duplicating with input links */
164         ot = WM_operatortype_append_macro("NODE_OT_duplicate_move_keep_inputs", "Duplicate",
165                                           "Duplicate selected nodes keeping input links and move them",
166                                           OPTYPE_UNDO | OPTYPE_REGISTER);
167         mot = WM_operatortype_macro_define(ot, "NODE_OT_duplicate");
168         RNA_boolean_set(mot->ptr, "keep_inputs", TRUE);
169         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
170
171         ot = WM_operatortype_append_macro("NODE_OT_move_detach_links", "Detach", "Move a node to detach links",
172                                           OPTYPE_UNDO | OPTYPE_REGISTER);
173         WM_operatortype_macro_define(ot, "NODE_OT_links_detach");
174         WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
175
176         ot = WM_operatortype_append_macro("NODE_OT_move_detach_links_release", "Detach", "Move a node to detach links",
177                                           OPTYPE_UNDO | OPTYPE_REGISTER);
178         WM_operatortype_macro_define(ot, "NODE_OT_links_detach");
179         WM_operatortype_macro_define(ot, "NODE_OT_translate_attach");
180 }
181
182 /* helper function for repetitive select operator keymap */
183 static void node_select_keymap(wmKeyMap *keymap, int extend)
184 {
185         /* modifier combinations */
186         const int mod_single[] = { 0, KM_CTRL, KM_ALT, KM_CTRL | KM_ALT,
187                                    -1 /* terminator */
188         };
189         const int mod_extend[] = { KM_SHIFT, KM_SHIFT | KM_CTRL,
190                                    KM_SHIFT | KM_ALT, KM_SHIFT | KM_CTRL | KM_ALT,
191                                    -1 /* terminator */
192         };
193         const int *mod = (extend ? mod_extend : mod_single);
194         wmKeyMapItem *kmi;
195         int i;
196         
197         for (i = 0; mod[i] >= 0; ++i) {
198                 kmi = WM_keymap_add_item(keymap, "NODE_OT_select", ACTIONMOUSE, KM_PRESS, mod[i], 0);
199                 RNA_boolean_set(kmi->ptr, "extend", extend);
200                 kmi = WM_keymap_add_item(keymap, "NODE_OT_select", SELECTMOUSE, KM_PRESS, mod[i], 0);
201                 RNA_boolean_set(kmi->ptr, "extend", extend);
202         }
203 }
204
205 void node_keymap(struct wmKeyConfig *keyconf)
206 {
207         wmKeyMap *keymap;
208         wmKeyMapItem *kmi;
209         
210         /* Entire Editor only ----------------- */
211         keymap = WM_keymap_find(keyconf, "Node Generic", SPACE_NODE, 0);
212         
213         WM_keymap_add_item(keymap, "NODE_OT_properties", NKEY, KM_PRESS, 0, 0);
214         WM_keymap_add_item(keymap, "NODE_OT_toolbar", TKEY, KM_PRESS, 0, 0);
215         
216         /* Main Area only ----------------- */
217         keymap = WM_keymap_find(keyconf, "Node Editor", SPACE_NODE, 0);
218         
219         /* mouse select in nodes used to be both keys, but perhaps this should be reduced? 
220          * NOTE: mouse-clicks on left-mouse will fall through to allow transform-tweak, but also link/resize
221          * NOTE 2: socket select is part of the node select operator, to handle overlapping cases
222          * NOTE 3: select op is registered for various combinations of modifier key, so the specialized
223          *         grab operators (unlink, attach, etc.) can work easily on single nodes.
224          */
225         node_select_keymap(keymap, FALSE);
226         node_select_keymap(keymap, TRUE);
227         
228         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_border", EVT_TWEAK_S, KM_ANY, 0, 0);
229         RNA_boolean_set(kmi->ptr, "tweak", TRUE);
230         
231         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
232         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
233         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
234         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
235
236         /* each of these falls through if not handled... */
237         kmi = WM_keymap_add_item(keymap, "NODE_OT_link", LEFTMOUSE, KM_PRESS, 0, 0);
238         RNA_boolean_set(kmi->ptr, "detach", FALSE);
239         kmi = WM_keymap_add_item(keymap, "NODE_OT_link", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
240         RNA_boolean_set(kmi->ptr, "detach", TRUE);
241         
242         WM_keymap_add_item(keymap, "NODE_OT_resize", LEFTMOUSE, KM_PRESS, 0, 0);
243         
244         WM_keymap_add_item(keymap, "NODE_OT_add_reroute", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
245         WM_keymap_add_item(keymap, "NODE_OT_links_cut", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
246         WM_keymap_add_item(keymap, "NODE_OT_select_link_viewer", LEFTMOUSE, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
247         
248         WM_keymap_add_item(keymap, "NODE_OT_backimage_move", MIDDLEMOUSE, KM_PRESS, KM_ALT, 0);
249         kmi = WM_keymap_add_item(keymap, "NODE_OT_backimage_zoom", VKEY, KM_PRESS, 0, 0);
250         RNA_float_set(kmi->ptr, "factor", 0.83333f);
251         kmi = WM_keymap_add_item(keymap, "NODE_OT_backimage_zoom", VKEY, KM_PRESS, KM_ALT, 0);
252         RNA_float_set(kmi->ptr, "factor", 1.2f);
253         WM_keymap_add_item(keymap, "NODE_OT_backimage_fit", HOMEKEY, KM_PRESS, KM_ALT, 0);
254         WM_keymap_add_item(keymap, "NODE_OT_backimage_sample", ACTIONMOUSE, KM_PRESS, KM_ALT, 0);
255
256         kmi = WM_keymap_add_item(keymap, "NODE_OT_link_make", FKEY, KM_PRESS, 0, 0);
257         RNA_boolean_set(kmi->ptr, "replace", FALSE);
258         kmi = WM_keymap_add_item(keymap, "NODE_OT_link_make", FKEY, KM_PRESS, KM_SHIFT, 0);
259         RNA_boolean_set(kmi->ptr, "replace", TRUE);
260
261         WM_keymap_add_menu(keymap, "NODE_MT_add", AKEY, KM_PRESS, KM_SHIFT, 0);
262         WM_keymap_add_item(keymap, "NODE_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
263         /* modified operator call for duplicating with input links */
264         WM_keymap_add_item(keymap, "NODE_OT_duplicate_move_keep_inputs", DKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
265         
266         WM_keymap_add_item(keymap, "NODE_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
267         WM_keymap_add_item(keymap, "NODE_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
268         WM_keymap_add_item(keymap, "NODE_OT_join", JKEY, KM_PRESS, KM_CTRL, 0);
269         
270         WM_keymap_add_item(keymap, "NODE_OT_hide_toggle", HKEY, KM_PRESS, 0, 0);
271         WM_keymap_add_item(keymap, "NODE_OT_mute_toggle", MKEY, KM_PRESS, 0, 0);
272         WM_keymap_add_item(keymap, "NODE_OT_preview_toggle", HKEY, KM_PRESS, KM_SHIFT, 0);
273         WM_keymap_add_item(keymap, "NODE_OT_hide_socket_toggle", HKEY, KM_PRESS, KM_CTRL, 0);
274         
275         WM_keymap_add_item(keymap, "NODE_OT_show_cyclic_dependencies", CKEY, KM_PRESS, 0, 0);
276         
277         WM_keymap_add_item(keymap, "NODE_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
278         WM_keymap_add_item(keymap, "NODE_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
279
280         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_border", BKEY, KM_PRESS, 0, 0);
281         RNA_boolean_set(kmi->ptr, "tweak", FALSE);
282
283         WM_keymap_add_item(keymap, "NODE_OT_delete", XKEY, KM_PRESS, 0, 0);
284         WM_keymap_add_item(keymap, "NODE_OT_delete", DELKEY, KM_PRESS, 0, 0);
285         WM_keymap_add_item(keymap, "NODE_OT_delete_reconnect", XKEY, KM_PRESS, KM_CTRL, 0);
286
287         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_all", AKEY, KM_PRESS, 0, 0);
288         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
289         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
290         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
291
292         WM_keymap_add_item(keymap, "NODE_OT_select_linked_to", LKEY, KM_PRESS, KM_SHIFT, 0);
293         WM_keymap_add_item(keymap, "NODE_OT_select_linked_from", LKEY, KM_PRESS, 0, 0);
294         WM_keymap_add_item(keymap, "NODE_OT_select_same_type", GKEY, KM_PRESS, KM_SHIFT, 0);
295
296         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_same_type_step", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
297         RNA_boolean_set(kmi->ptr, "prev", FALSE);
298         kmi = WM_keymap_add_item(keymap, "NODE_OT_select_same_type_step", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
299         RNA_boolean_set(kmi->ptr, "prev", TRUE);
300         
301         WM_keymap_add_item(keymap, "NODE_OT_find_node", FKEY, KM_PRESS, KM_CTRL, 0);
302         
303         /* node group operators */
304         WM_keymap_add_item(keymap, "NODE_OT_group_make", GKEY, KM_PRESS, KM_CTRL, 0);
305         WM_keymap_add_item(keymap, "NODE_OT_group_ungroup", GKEY, KM_PRESS, KM_ALT, 0);
306         WM_keymap_add_item(keymap, "NODE_OT_group_separate", PKEY, KM_PRESS, 0, 0);
307         kmi = WM_keymap_add_item(keymap, "NODE_OT_group_edit", TABKEY, KM_PRESS, 0, 0);
308         RNA_boolean_set(kmi->ptr, "exit", FALSE);
309         kmi = WM_keymap_add_item(keymap, "NODE_OT_group_edit", TABKEY, KM_PRESS, KM_SHIFT, 0);
310         RNA_boolean_set(kmi->ptr, "exit", TRUE);
311
312         WM_keymap_add_item(keymap, "NODE_OT_read_renderlayers", RKEY, KM_PRESS, KM_CTRL, 0);
313         WM_keymap_add_item(keymap, "NODE_OT_read_fullsamplelayers", RKEY, KM_PRESS, KM_SHIFT, 0);
314         WM_keymap_add_item(keymap, "NODE_OT_render_changed", ZKEY, KM_PRESS, 0, 0);
315         
316         WM_keymap_add_item(keymap, "NODE_OT_clipboard_copy", CKEY, KM_PRESS, KM_CTRL, 0);
317         WM_keymap_add_item(keymap, "NODE_OT_clipboard_paste", VKEY, KM_PRESS, KM_CTRL, 0);
318 #ifdef __APPLE__
319         WM_keymap_add_item(keymap, "NODE_OT_clipboard_copy", CKEY, KM_PRESS, KM_OSKEY, 0);
320         WM_keymap_add_item(keymap, "NODE_OT_clipboard_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
321 #endif
322         WM_keymap_add_item(keymap, "NODE_OT_viewer_border", BKEY, KM_PRESS, KM_CTRL, 0);
323
324         transform_keymap_for_space(keyconf, keymap, SPACE_NODE);
325 }