spatial_editor_plugin.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*************************************************************************/
  2. /* spatial_editor_plugin.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef SPATIAL_EDITOR_PLUGIN_H
  31. #define SPATIAL_EDITOR_PLUGIN_H
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_plugin.h"
  34. #include "scene/3d/immediate_geometry.h"
  35. #include "scene/3d/light.h"
  36. #include "scene/3d/visual_instance.h"
  37. #include "scene/gui/panel_container.h"
  38. /**
  39. @author Juan Linietsky <reduzio@gmail.com>
  40. */
  41. class Camera;
  42. class SpatialEditor;
  43. class SpatialEditorGizmos;
  44. class SpatialEditorGizmo : public SpatialGizmo {
  45. GDCLASS(SpatialEditorGizmo, SpatialGizmo);
  46. bool selected;
  47. bool instanced;
  48. public:
  49. void set_selected(bool p_selected) { selected = p_selected; }
  50. bool is_selected() const { return selected; }
  51. virtual String get_handle_name(int p_idx) const;
  52. virtual Variant get_handle_value(int p_idx) const;
  53. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  54. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  55. virtual bool intersect_frustum(const Camera *p_camera, const Vector<Plane> &p_frustum);
  56. virtual bool intersect_ray(const Camera *p_camera, const Point2 &p_point, Vector3 &r_pos, Vector3 &r_normal, int *r_gizmo_handle = NULL, bool p_sec_first = false);
  57. SpatialEditorGizmo();
  58. };
  59. class SpatialEditorViewport : public Control {
  60. GDCLASS(SpatialEditorViewport, Control);
  61. friend class SpatialEditor;
  62. enum {
  63. VIEW_TOP,
  64. VIEW_BOTTOM,
  65. VIEW_LEFT,
  66. VIEW_RIGHT,
  67. VIEW_FRONT,
  68. VIEW_REAR,
  69. VIEW_CENTER_TO_ORIGIN,
  70. VIEW_CENTER_TO_SELECTION,
  71. VIEW_ALIGN_SELECTION_WITH_VIEW,
  72. VIEW_PERSPECTIVE,
  73. VIEW_ENVIRONMENT,
  74. VIEW_ORTHOGONAL,
  75. VIEW_HALF_RESOLUTION,
  76. VIEW_AUDIO_LISTENER,
  77. VIEW_AUDIO_DOPPLER,
  78. VIEW_GIZMOS,
  79. VIEW_INFORMATION,
  80. VIEW_DISPLAY_NORMAL,
  81. VIEW_DISPLAY_WIREFRAME,
  82. VIEW_DISPLAY_OVERDRAW,
  83. VIEW_DISPLAY_SHADELESS,
  84. };
  85. public:
  86. enum {
  87. GIZMO_BASE_LAYER = 27,
  88. GIZMO_EDIT_LAYER = 26,
  89. GIZMO_GRID_LAYER = 25
  90. };
  91. private:
  92. int index;
  93. String name;
  94. void _menu_option(int p_option);
  95. Size2 prev_size;
  96. Spatial *preview_node;
  97. Rect3 *preview_bounds;
  98. Vector<String> selected_files;
  99. AcceptDialog *accept;
  100. Node *target_node;
  101. Point2 drop_pos;
  102. EditorNode *editor;
  103. EditorData *editor_data;
  104. EditorSelection *editor_selection;
  105. UndoRedo *undo_redo;
  106. Button *preview_camera;
  107. ViewportContainer *viewport_container;
  108. MenuButton *view_menu;
  109. Control *surface;
  110. Viewport *viewport;
  111. Camera *camera;
  112. bool transforming;
  113. bool orthogonal;
  114. float gizmo_scale;
  115. bool freelook_active;
  116. real_t freelook_speed;
  117. PanelContainer *info;
  118. Label *info_label;
  119. struct _RayResult {
  120. Spatial *item;
  121. float depth;
  122. int handle;
  123. _FORCE_INLINE_ bool operator<(const _RayResult &p_rr) const { return depth < p_rr.depth; }
  124. };
  125. void _update_name();
  126. void _compute_edit(const Point2 &p_point);
  127. void _clear_selected();
  128. void _select_clicked(bool p_append, bool p_single);
  129. void _select(Spatial *p_node, bool p_append, bool p_single);
  130. ObjectID _select_ray(const Point2 &p_pos, bool p_append, bool &r_includes_current, int *r_gizmo_handle = NULL, bool p_alt_select = false);
  131. void _find_items_at_pos(const Point2 &p_pos, bool &r_includes_current, Vector<_RayResult> &results, bool p_alt_select = false);
  132. Vector3 _get_ray_pos(const Vector2 &p_pos) const;
  133. Vector3 _get_ray(const Vector2 &p_pos) const;
  134. Point2 _point_to_screen(const Vector3 &p_point);
  135. Transform _get_camera_transform() const;
  136. int get_selected_count() const;
  137. Vector3 _get_camera_position() const;
  138. Vector3 _get_camera_normal() const;
  139. Vector3 _get_screen_to_space(const Vector3 &p_vector3);
  140. void _select_region();
  141. bool _gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only = false);
  142. float get_znear() const;
  143. float get_zfar() const;
  144. float get_fov() const;
  145. ObjectID clicked;
  146. Vector<_RayResult> selection_results;
  147. bool clicked_includes_current;
  148. bool clicked_wants_append;
  149. PopupMenu *selection_menu;
  150. enum NavigationScheme {
  151. NAVIGATION_GODOT,
  152. NAVIGATION_MAYA,
  153. NAVIGATION_MODO,
  154. };
  155. enum NavigationZoomStyle {
  156. NAVIGATION_ZOOM_VERTICAL,
  157. NAVIGATION_ZOOM_HORIZONTAL
  158. };
  159. enum NavigationMode {
  160. NAVIGATION_NONE,
  161. NAVIGATION_PAN,
  162. NAVIGATION_ZOOM,
  163. NAVIGATION_ORBIT,
  164. NAVIGATION_LOOK
  165. };
  166. enum TransformMode {
  167. TRANSFORM_NONE,
  168. TRANSFORM_ROTATE,
  169. TRANSFORM_TRANSLATE,
  170. TRANSFORM_SCALE
  171. };
  172. enum TransformPlane {
  173. TRANSFORM_VIEW,
  174. TRANSFORM_X_AXIS,
  175. TRANSFORM_Y_AXIS,
  176. TRANSFORM_Z_AXIS,
  177. TRANSFORM_YZ,
  178. TRANSFORM_XZ,
  179. TRANSFORM_XY,
  180. };
  181. struct EditData {
  182. TransformMode mode;
  183. TransformPlane plane;
  184. Transform original;
  185. Vector3 click_ray;
  186. Vector3 click_ray_pos;
  187. Vector3 center;
  188. Vector3 orig_gizmo_pos;
  189. int edited_gizmo;
  190. Point2 mouse_pos;
  191. bool snap;
  192. Ref<SpatialEditorGizmo> gizmo;
  193. int gizmo_handle;
  194. Variant gizmo_initial_value;
  195. Vector3 gizmo_initial_pos;
  196. } _edit;
  197. struct Cursor {
  198. Vector3 pos;
  199. float x_rot, y_rot, distance;
  200. Vector3 eye_pos; // Used in freelook mode
  201. bool region_select;
  202. Point2 region_begin, region_end;
  203. Cursor() {
  204. x_rot = y_rot = 0.5;
  205. distance = 4;
  206. region_select = false;
  207. }
  208. };
  209. // Viewport camera supports movement smoothing,
  210. // so one cursor is the real cursor, while the other can be an interpolated version.
  211. Cursor cursor; // Immediate cursor
  212. Cursor camera_cursor; // That one may be interpolated (don't modify this one except for smoothing purposes)
  213. void scale_cursor_distance(real_t scale);
  214. void set_freelook_active(bool active_now);
  215. void scale_freelook_speed(real_t scale);
  216. real_t zoom_indicator_delay;
  217. RID move_gizmo_instance[3], move_plane_gizmo_instance[3], rotate_gizmo_instance[3], scale_gizmo_instance[3];
  218. String last_message;
  219. String message;
  220. float message_time;
  221. void set_message(String p_message, float p_time = 5);
  222. //
  223. void _update_camera(float p_interp_delta);
  224. Transform to_camera_transform(const Cursor &p_cursor) const;
  225. void _draw();
  226. void _smouseenter();
  227. void _smouseexit();
  228. void _sinput(const Ref<InputEvent> &p_event);
  229. void _update_freelook(real_t delta);
  230. SpatialEditor *spatial_editor;
  231. Camera *previewing;
  232. Camera *preview;
  233. void _preview_exited_scene();
  234. void _toggle_camera_preview(bool);
  235. void _init_gizmo_instance(int p_idx);
  236. void _finish_gizmo_instances();
  237. void _selection_result_pressed(int);
  238. void _selection_menu_hide();
  239. void _list_select(Ref<InputEventMouseButton> b);
  240. Point2i _get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const;
  241. Vector3 _get_instance_position(const Point2 &p_pos) const;
  242. static Rect3 _calculate_spatial_bounds(const Spatial *p_parent, const Rect3 p_bounds);
  243. void _create_preview(const Vector<String> &files) const;
  244. void _remove_preview();
  245. bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node);
  246. bool _create_instance(Node *parent, String &path, const Point2 &p_point);
  247. void _perform_drop_data();
  248. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  249. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  250. protected:
  251. void _notification(int p_what);
  252. static void _bind_methods();
  253. public:
  254. void update_transform_gizmo_view();
  255. void set_can_preview(Camera *p_preview);
  256. void set_state(const Dictionary &p_state);
  257. Dictionary get_state() const;
  258. void reset();
  259. bool is_freelook_active() const { return freelook_active; }
  260. void focus_selection();
  261. void assign_pending_data_pointers(
  262. Spatial *p_preview_node,
  263. Rect3 *p_preview_bounds,
  264. AcceptDialog *p_accept);
  265. Viewport *get_viewport_node() { return viewport; }
  266. SpatialEditorViewport(SpatialEditor *p_spatial_editor, EditorNode *p_editor, int p_index);
  267. };
  268. class SpatialEditorSelectedItem : public Object {
  269. GDCLASS(SpatialEditorSelectedItem, Object);
  270. public:
  271. Rect3 aabb;
  272. Transform original; // original location when moving
  273. Transform original_local;
  274. Transform last_xform; // last transform
  275. Spatial *sp;
  276. RID sbox_instance;
  277. SpatialEditorSelectedItem() { sp = NULL; }
  278. ~SpatialEditorSelectedItem();
  279. };
  280. class SpatialEditorViewportContainer : public Container {
  281. GDCLASS(SpatialEditorViewportContainer, Container)
  282. public:
  283. enum View {
  284. VIEW_USE_1_VIEWPORT,
  285. VIEW_USE_2_VIEWPORTS,
  286. VIEW_USE_2_VIEWPORTS_ALT,
  287. VIEW_USE_3_VIEWPORTS,
  288. VIEW_USE_3_VIEWPORTS_ALT,
  289. VIEW_USE_4_VIEWPORTS,
  290. };
  291. private:
  292. View view;
  293. bool mouseover;
  294. float ratio_h;
  295. float ratio_v;
  296. bool dragging_v;
  297. bool dragging_h;
  298. Vector2 drag_begin_pos;
  299. Vector2 drag_begin_ratio;
  300. void _gui_input(const Ref<InputEvent> &p_event);
  301. protected:
  302. void _notification(int p_what);
  303. static void _bind_methods();
  304. public:
  305. void set_view(View p_view);
  306. View get_view();
  307. SpatialEditorViewportContainer();
  308. };
  309. class SpatialEditor : public VBoxContainer {
  310. GDCLASS(SpatialEditor, VBoxContainer);
  311. public:
  312. enum ToolMode {
  313. TOOL_MODE_SELECT,
  314. TOOL_MODE_MOVE,
  315. TOOL_MODE_ROTATE,
  316. TOOL_MODE_SCALE,
  317. TOOL_MODE_LIST_SELECT,
  318. TOOL_MAX
  319. };
  320. private:
  321. static const unsigned int VIEWPORTS_COUNT = 4;
  322. EditorNode *editor;
  323. EditorSelection *editor_selection;
  324. SpatialEditorViewportContainer *viewport_base;
  325. SpatialEditorViewport *viewports[VIEWPORTS_COUNT];
  326. VSplitContainer *shader_split;
  327. HSplitContainer *palette_split;
  328. /////
  329. ToolMode tool_mode;
  330. bool orthogonal;
  331. VisualServer::ScenarioDebugMode scenario_debug;
  332. RID origin;
  333. RID origin_instance;
  334. RID grid[3];
  335. RID grid_instance[3];
  336. bool grid_visible[3]; //currently visible
  337. float last_grid_snap;
  338. bool grid_enable[3]; //should be always visible if true
  339. bool grid_enabled;
  340. Ref<ArrayMesh> move_gizmo[3], move_plane_gizmo[3], rotate_gizmo[3], scale_gizmo[3];
  341. Ref<SpatialMaterial> gizmo_color[3];
  342. Ref<SpatialMaterial> plane_gizmo_color[3];
  343. Ref<SpatialMaterial> gizmo_hl;
  344. int over_gizmo_handle;
  345. Ref<ArrayMesh> selection_box;
  346. RID indicators;
  347. RID indicators_instance;
  348. RID cursor_mesh;
  349. RID cursor_instance;
  350. Ref<SpatialMaterial> indicator_mat;
  351. Ref<SpatialMaterial> cursor_material;
  352. // Scene drag and drop support
  353. Spatial *preview_node;
  354. Rect3 preview_bounds;
  355. /*
  356. struct Selected {
  357. AABB aabb;
  358. Transform original; // original location when moving
  359. Transform last_xform; // last transform
  360. Spatial *sp;
  361. RID poly_instance;
  362. };
  363. Map<uint32_t,Selected> selected;
  364. */
  365. struct Gizmo {
  366. bool visible;
  367. float scale;
  368. Transform transform;
  369. } gizmo;
  370. enum MenuOption {
  371. MENU_TOOL_SELECT,
  372. MENU_TOOL_MOVE,
  373. MENU_TOOL_ROTATE,
  374. MENU_TOOL_SCALE,
  375. MENU_TOOL_LIST_SELECT,
  376. MENU_TRANSFORM_USE_SNAP,
  377. MENU_TRANSFORM_CONFIGURE_SNAP,
  378. MENU_TRANSFORM_LOCAL_COORDS,
  379. MENU_TRANSFORM_DIALOG,
  380. MENU_VIEW_USE_1_VIEWPORT,
  381. MENU_VIEW_USE_2_VIEWPORTS,
  382. MENU_VIEW_USE_2_VIEWPORTS_ALT,
  383. MENU_VIEW_USE_3_VIEWPORTS,
  384. MENU_VIEW_USE_3_VIEWPORTS_ALT,
  385. MENU_VIEW_USE_4_VIEWPORTS,
  386. MENU_VIEW_ORIGIN,
  387. MENU_VIEW_GRID,
  388. MENU_VIEW_CAMERA_SETTINGS,
  389. };
  390. Button *tool_button[TOOL_MAX];
  391. MenuButton *transform_menu;
  392. MenuButton *view_menu;
  393. AcceptDialog *accept;
  394. ConfirmationDialog *snap_dialog;
  395. ConfirmationDialog *xform_dialog;
  396. ConfirmationDialog *settings_dialog;
  397. bool snap_enabled;
  398. LineEdit *snap_translate;
  399. LineEdit *snap_rotate;
  400. LineEdit *snap_scale;
  401. PanelContainer *menu_panel;
  402. LineEdit *xform_translate[3];
  403. LineEdit *xform_rotate[3];
  404. LineEdit *xform_scale[3];
  405. OptionButton *xform_type;
  406. VBoxContainer *settings_vbc;
  407. SpinBox *settings_fov;
  408. SpinBox *settings_znear;
  409. SpinBox *settings_zfar;
  410. void _xform_dialog_action();
  411. void _menu_item_pressed(int p_option);
  412. HBoxContainer *hbc_menu;
  413. //
  414. //
  415. void _generate_selection_box();
  416. UndoRedo *undo_redo;
  417. void _instance_scene();
  418. void _init_indicators();
  419. void _finish_indicators();
  420. void _toggle_maximize_view(Object *p_viewport);
  421. Node *custom_camera;
  422. Object *_get_editor_data(Object *p_what);
  423. Ref<Environment> viewport_environment;
  424. Spatial *selected;
  425. void _request_gizmo(Object *p_obj);
  426. static SpatialEditor *singleton;
  427. void _node_removed(Node *p_node);
  428. SpatialEditorGizmos *gizmos;
  429. SpatialEditor();
  430. bool is_any_freelook_active() const;
  431. protected:
  432. void _notification(int p_what);
  433. //void _gui_input(InputEvent p_event);
  434. void _unhandled_key_input(Ref<InputEvent> p_event);
  435. static void _bind_methods();
  436. public:
  437. static SpatialEditor *get_singleton() { return singleton; }
  438. void snap_cursor_to_plane(const Plane &p_plane);
  439. Vector3 snap_point(Vector3 p_target, Vector3 p_start = Vector3(0, 0, 0)) const;
  440. float get_znear() const { return settings_znear->get_value(); }
  441. float get_zfar() const { return settings_zfar->get_value(); }
  442. float get_fov() const { return settings_fov->get_value(); }
  443. Transform get_gizmo_transform() const { return gizmo.transform; }
  444. bool is_gizmo_visible() const { return gizmo.visible; }
  445. ToolMode get_tool_mode() const { return tool_mode; }
  446. bool is_snap_enabled() const { return snap_enabled; }
  447. float get_translate_snap() const { return snap_translate->get_text().to_double(); }
  448. float get_rotate_snap() const { return snap_rotate->get_text().to_double(); }
  449. float get_scale_snap() const { return snap_scale->get_text().to_double(); }
  450. bool are_local_coords_enabled() const { return transform_menu->get_popup()->is_item_checked(transform_menu->get_popup()->get_item_index(SpatialEditor::MENU_TRANSFORM_LOCAL_COORDS)); }
  451. Ref<ArrayMesh> get_move_gizmo(int idx) const { return move_gizmo[idx]; }
  452. Ref<ArrayMesh> get_move_plane_gizmo(int idx) const { return move_plane_gizmo[idx]; }
  453. Ref<ArrayMesh> get_rotate_gizmo(int idx) const { return rotate_gizmo[idx]; }
  454. Ref<ArrayMesh> get_scale_gizmo(int idx) const { return scale_gizmo[idx]; }
  455. void update_transform_gizmo();
  456. void select_gizmo_highlight_axis(int p_axis);
  457. void set_custom_camera(Node *p_camera) { custom_camera = p_camera; }
  458. void set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }
  459. Dictionary get_state() const;
  460. void set_state(const Dictionary &p_state);
  461. Ref<Environment> get_viewport_environment() { return viewport_environment; }
  462. UndoRedo *get_undo_redo() { return undo_redo; }
  463. void add_control_to_menu_panel(Control *p_control);
  464. VSplitContainer *get_shader_split();
  465. HSplitContainer *get_palette_split();
  466. Spatial *get_selected() { return selected; }
  467. int get_over_gizmo_handle() const { return over_gizmo_handle; }
  468. void set_over_gizmo_handle(int idx) { over_gizmo_handle = idx; }
  469. void set_can_preview(Camera *p_preview);
  470. SpatialEditorViewport *get_editor_viewport(int p_idx) {
  471. ERR_FAIL_INDEX_V(p_idx, 4, NULL);
  472. return viewports[p_idx];
  473. }
  474. Camera *get_camera() { return NULL; }
  475. void edit(Spatial *p_spatial);
  476. void clear();
  477. SpatialEditor(EditorNode *p_editor);
  478. ~SpatialEditor();
  479. };
  480. class SpatialEditorPlugin : public EditorPlugin {
  481. GDCLASS(SpatialEditorPlugin, EditorPlugin);
  482. SpatialEditor *spatial_editor;
  483. EditorNode *editor;
  484. protected:
  485. static void _bind_methods();
  486. public:
  487. void snap_cursor_to_plane(const Plane &p_plane);
  488. SpatialEditor *get_spatial_editor() { return spatial_editor; }
  489. virtual String get_name() const { return "3D"; }
  490. bool has_main_screen() const { return true; }
  491. virtual void make_visible(bool p_visible);
  492. virtual void edit(Object *p_object);
  493. virtual bool handles(Object *p_object) const;
  494. virtual Dictionary get_state() const;
  495. virtual void set_state(const Dictionary &p_state);
  496. virtual void clear() { spatial_editor->clear(); }
  497. SpatialEditorPlugin(EditorNode *p_node);
  498. ~SpatialEditorPlugin();
  499. };
  500. #endif