Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_view3d / view3d_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
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_ops.c
28  *  \ingroup spview3d
29  */
30
31
32 #include <stdlib.h>
33 #include <math.h>
34
35
36 #include "DNA_collection_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_view3d_types.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_utildefines.h"
45
46 #include "BKE_appdir.h"
47 #include "BKE_blender_copybuffer.h"
48 #include "BKE_collection.h"
49 #include "BKE_context.h"
50 #include "BKE_main.h"
51 #include "BKE_report.h"
52
53 #include "RNA_access.h"
54 #include "RNA_define.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "ED_screen.h"
60 #include "ED_select_utils.h"
61 #include "ED_transform.h"
62
63 #include "view3d_intern.h"
64
65 #ifdef WIN32
66 #  include "BLI_math_base.h" /* M_PI */
67 #endif
68
69 /* ************************** copy paste ***************************** */
70
71 static int view3d_copybuffer_exec(bContext *C, wmOperator *op)
72 {
73         Main *bmain = CTX_data_main(C);
74         char str[FILE_MAX];
75
76         BKE_copybuffer_begin(bmain);
77
78         /* context, selection, could be generalized */
79         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
80         {
81                 BKE_copybuffer_tag_ID(&ob->id);
82         }
83         CTX_DATA_END;
84
85         for (Collection *collection = bmain->collection.first; collection; collection = collection->id.next) {
86                 for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
87                         Object *object = cob->ob;
88
89                         if (object && (object->id.tag & LIB_TAG_DOIT)) {
90                                 BKE_copybuffer_tag_ID(&collection->id);
91                                 /* don't expand out to all other objects */
92                                 collection->id.tag &= ~LIB_TAG_NEED_EXPAND;
93                                 break;
94                         }
95                 }
96         }
97
98         BLI_make_file_string("/", str, BKE_tempdir_base(), "copybuffer.blend");
99         BKE_copybuffer_save(bmain, str, op->reports);
100
101         BKE_report(op->reports, RPT_INFO, "Copied selected objects to buffer");
102
103         return OPERATOR_FINISHED;
104 }
105
106 static void VIEW3D_OT_copybuffer(wmOperatorType *ot)
107 {
108
109         /* identifiers */
110         ot->name = "Copy Selection to Buffer";
111         ot->idname = "VIEW3D_OT_copybuffer";
112         ot->description = "Selected objects are saved in a temp file";
113
114         /* api callbacks */
115         ot->exec = view3d_copybuffer_exec;
116         ot->poll = ED_operator_scene;
117 }
118
119 static int view3d_pastebuffer_exec(bContext *C, wmOperator *op)
120 {
121         char str[FILE_MAX];
122         short flag = 0;
123
124         if (RNA_boolean_get(op->ptr, "autoselect"))
125                 flag |= FILE_AUTOSELECT;
126         if (RNA_boolean_get(op->ptr, "active_collection"))
127                 flag |= FILE_ACTIVE_COLLECTION;
128
129         BLI_make_file_string("/", str, BKE_tempdir_base(), "copybuffer.blend");
130         if (BKE_copybuffer_paste(C, str, flag, op->reports)) {
131                 WM_event_add_notifier(C, NC_WINDOW, NULL);
132
133                 BKE_report(op->reports, RPT_INFO, "Objects pasted from buffer");
134
135                 return OPERATOR_FINISHED;
136         }
137
138         BKE_report(op->reports, RPT_INFO, "No buffer to paste from");
139
140         return OPERATOR_CANCELLED;
141 }
142
143 static void VIEW3D_OT_pastebuffer(wmOperatorType *ot)
144 {
145
146         /* identifiers */
147         ot->name = "Paste Selection from Buffer";
148         ot->idname = "VIEW3D_OT_pastebuffer";
149         ot->description = "Contents of copy buffer gets pasted";
150
151         /* api callbacks */
152         ot->exec = view3d_pastebuffer_exec;
153         ot->poll = ED_operator_scene_editable;
154
155         /* flags */
156         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
157
158         RNA_def_boolean(ot->srna, "autoselect", true, "Select", "Select pasted objects");
159         RNA_def_boolean(ot->srna, "active_collection", true, "Active Collection", "Put pasted objects on the active collection");
160 }
161
162 /* ************************** registration **********************************/
163
164 void view3d_operatortypes(void)
165 {
166         WM_operatortype_append(VIEW3D_OT_rotate);
167         WM_operatortype_append(VIEW3D_OT_move);
168         WM_operatortype_append(VIEW3D_OT_zoom);
169         WM_operatortype_append(VIEW3D_OT_zoom_camera_1_to_1);
170         WM_operatortype_append(VIEW3D_OT_dolly);
171 #ifdef WITH_INPUT_NDOF
172         WM_operatortype_append(VIEW3D_OT_ndof_orbit_zoom);
173         WM_operatortype_append(VIEW3D_OT_ndof_orbit);
174         WM_operatortype_append(VIEW3D_OT_ndof_pan);
175         WM_operatortype_append(VIEW3D_OT_ndof_all);
176 #endif /* WITH_INPUT_NDOF */
177         WM_operatortype_append(VIEW3D_OT_view_all);
178         WM_operatortype_append(VIEW3D_OT_view_axis);
179         WM_operatortype_append(VIEW3D_OT_view_camera);
180         WM_operatortype_append(VIEW3D_OT_view_orbit);
181         WM_operatortype_append(VIEW3D_OT_view_roll);
182         WM_operatortype_append(VIEW3D_OT_view_pan);
183         WM_operatortype_append(VIEW3D_OT_view_persportho);
184         WM_operatortype_append(VIEW3D_OT_background_image_add);
185         WM_operatortype_append(VIEW3D_OT_background_image_remove);
186         WM_operatortype_append(VIEW3D_OT_view_selected);
187         WM_operatortype_append(VIEW3D_OT_view_lock_clear);
188         WM_operatortype_append(VIEW3D_OT_view_lock_to_active);
189         WM_operatortype_append(VIEW3D_OT_view_center_cursor);
190         WM_operatortype_append(VIEW3D_OT_view_center_pick);
191         WM_operatortype_append(VIEW3D_OT_view_center_camera);
192         WM_operatortype_append(VIEW3D_OT_view_center_lock);
193         WM_operatortype_append(VIEW3D_OT_select);
194         WM_operatortype_append(VIEW3D_OT_select_box);
195         // WM_operatortype_append(VIEW3D_OT_clip_border);
196         WM_operatortype_append(VIEW3D_OT_select_circle);
197         WM_operatortype_append(VIEW3D_OT_smoothview);
198         WM_operatortype_append(VIEW3D_OT_render_border);
199         WM_operatortype_append(VIEW3D_OT_clear_render_border);
200         WM_operatortype_append(VIEW3D_OT_zoom_border);
201         WM_operatortype_append(VIEW3D_OT_cursor3d);
202         WM_operatortype_append(VIEW3D_OT_select_lasso);
203         WM_operatortype_append(VIEW3D_OT_select_menu);
204         WM_operatortype_append(VIEW3D_OT_camera_to_view);
205         WM_operatortype_append(VIEW3D_OT_camera_to_view_selected);
206         WM_operatortype_append(VIEW3D_OT_object_as_camera);
207         WM_operatortype_append(VIEW3D_OT_localview);
208         WM_operatortype_append(VIEW3D_OT_localview_remove_from);
209         WM_operatortype_append(VIEW3D_OT_fly);
210         WM_operatortype_append(VIEW3D_OT_walk);
211         WM_operatortype_append(VIEW3D_OT_navigate);
212         WM_operatortype_append(VIEW3D_OT_copybuffer);
213         WM_operatortype_append(VIEW3D_OT_pastebuffer);
214
215         WM_operatortype_append(VIEW3D_OT_properties);
216         WM_operatortype_append(VIEW3D_OT_object_mode_pie_or_toggle);
217         WM_operatortype_append(VIEW3D_OT_toolshelf);
218
219         WM_operatortype_append(VIEW3D_OT_snap_selected_to_grid);
220         WM_operatortype_append(VIEW3D_OT_snap_selected_to_cursor);
221         WM_operatortype_append(VIEW3D_OT_snap_selected_to_active);
222         WM_operatortype_append(VIEW3D_OT_snap_cursor_to_grid);
223         WM_operatortype_append(VIEW3D_OT_snap_cursor_to_center);
224         WM_operatortype_append(VIEW3D_OT_snap_cursor_to_selected);
225         WM_operatortype_append(VIEW3D_OT_snap_cursor_to_active);
226
227         WM_operatortype_append(VIEW3D_OT_toggle_shading);
228         WM_operatortype_append(VIEW3D_OT_toggle_xray);
229         WM_operatortype_append(VIEW3D_OT_toggle_matcap_flip);
230
231         WM_operatortype_append(VIEW3D_OT_ruler_add);
232
233         transform_operatortypes();
234 }
235
236 void view3d_keymap(wmKeyConfig *keyconf)
237 {
238         WM_keymap_ensure(keyconf, "3D View Generic", SPACE_VIEW3D, 0);
239
240         /* only for region 3D window */
241         WM_keymap_ensure(keyconf, "3D View", SPACE_VIEW3D, 0);
242
243         fly_modal_keymap(keyconf);
244         walk_modal_keymap(keyconf);
245         viewrotate_modal_keymap(keyconf);
246         viewmove_modal_keymap(keyconf);
247         viewzoom_modal_keymap(keyconf);
248         viewdolly_modal_keymap(keyconf);
249 }