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