spatial_editor_plugin.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 EditorSpatialGizmoPlugin;
  44. class EditorSpatialGizmo : public SpatialGizmo {
  45. GDCLASS(EditorSpatialGizmo, 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. struct Instance {
  52. RID instance;
  53. Ref<ArrayMesh> mesh;
  54. RID skeleton;
  55. bool billboard;
  56. bool unscaled;
  57. bool can_intersect;
  58. bool extra_margin;
  59. Instance() {
  60. billboard = false;
  61. unscaled = false;
  62. can_intersect = false;
  63. extra_margin = false;
  64. }
  65. void create_instance(Spatial *p_base, bool p_hidden = false);
  66. };
  67. Vector<Vector3> collision_segments;
  68. Ref<TriangleMesh> collision_mesh;
  69. struct Handle {
  70. Vector3 pos;
  71. bool billboard;
  72. };
  73. Vector<Vector3> handles;
  74. Vector<Vector3> secondary_handles;
  75. float selectable_icon_size;
  76. bool billboard_handle;
  77. bool valid;
  78. bool hidden;
  79. Spatial *base;
  80. Vector<Instance> instances;
  81. Spatial *spatial_node;
  82. EditorSpatialGizmoPlugin *gizmo_plugin;
  83. void _set_spatial_node(Node *p_node) { set_spatial_node(Object::cast_to<Spatial>(p_node)); }
  84. protected:
  85. static void _bind_methods();
  86. public:
  87. void add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material, bool p_billboard = false);
  88. void add_mesh(const Ref<ArrayMesh> &p_mesh, bool p_billboard = false, const RID &p_skeleton = RID());
  89. void add_collision_segments(const Vector<Vector3> &p_lines);
  90. void add_collision_triangles(const Ref<TriangleMesh> &p_tmesh);
  91. void add_unscaled_billboard(const Ref<Material> &p_material, float p_scale = 1);
  92. void add_handles(const Vector<Vector3> &p_handles, const Ref<Material> &p_material, bool p_billboard = false, bool p_secondary = false);
  93. void add_solid_box(Ref<Material> &p_material, Vector3 p_size, Vector3 p_position = Vector3());
  94. virtual String get_handle_name(int p_idx) const;
  95. virtual Variant get_handle_value(int p_idx);
  96. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  97. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  98. void set_spatial_node(Spatial *p_node);
  99. Spatial *get_spatial_node() const { return spatial_node; }
  100. Vector3 get_handle_pos(int p_idx) const;
  101. bool intersect_frustum(const Camera *p_camera, const Vector<Plane> &p_frustum);
  102. bool intersect_ray(Camera *p_camera, const Point2 &p_point, Vector3 &r_pos, Vector3 &r_normal, int *r_gizmo_handle = NULL, bool p_sec_first = false);
  103. virtual void clear();
  104. virtual void create();
  105. virtual void transform();
  106. virtual void redraw();
  107. virtual void free();
  108. virtual bool is_editable() const;
  109. void set_hidden(bool p_hidden);
  110. void set_plugin(EditorSpatialGizmoPlugin *p_gizmo);
  111. EditorSpatialGizmo();
  112. ~EditorSpatialGizmo();
  113. };
  114. class SpatialEditorViewport : public Control {
  115. GDCLASS(SpatialEditorViewport, Control);
  116. friend class SpatialEditor;
  117. enum {
  118. VIEW_TOP,
  119. VIEW_BOTTOM,
  120. VIEW_LEFT,
  121. VIEW_RIGHT,
  122. VIEW_FRONT,
  123. VIEW_REAR,
  124. VIEW_CENTER_TO_ORIGIN,
  125. VIEW_CENTER_TO_SELECTION,
  126. VIEW_ALIGN_SELECTION_WITH_VIEW,
  127. VIEW_PERSPECTIVE,
  128. VIEW_ENVIRONMENT,
  129. VIEW_ORTHOGONAL,
  130. VIEW_HALF_RESOLUTION,
  131. VIEW_AUDIO_LISTENER,
  132. VIEW_AUDIO_DOPPLER,
  133. VIEW_GIZMOS,
  134. VIEW_INFORMATION,
  135. VIEW_FPS,
  136. VIEW_DISPLAY_NORMAL,
  137. VIEW_DISPLAY_WIREFRAME,
  138. VIEW_DISPLAY_OVERDRAW,
  139. VIEW_DISPLAY_SHADELESS,
  140. VIEW_LOCK_ROTATION,
  141. VIEW_CINEMATIC_PREVIEW
  142. };
  143. public:
  144. enum {
  145. GIZMO_BASE_LAYER = 27,
  146. GIZMO_EDIT_LAYER = 26,
  147. GIZMO_GRID_LAYER = 25
  148. };
  149. private:
  150. int index;
  151. String name;
  152. void _menu_option(int p_option);
  153. Spatial *preview_node;
  154. AABB *preview_bounds;
  155. Vector<String> selected_files;
  156. AcceptDialog *accept;
  157. Node *target_node;
  158. Point2 drop_pos;
  159. EditorNode *editor;
  160. EditorData *editor_data;
  161. EditorSelection *editor_selection;
  162. UndoRedo *undo_redo;
  163. CheckBox *preview_camera;
  164. ViewportContainer *viewport_container;
  165. MenuButton *view_menu;
  166. Control *surface;
  167. Viewport *viewport;
  168. Camera *camera;
  169. bool transforming;
  170. bool orthogonal;
  171. bool lock_rotation;
  172. float gizmo_scale;
  173. bool freelook_active;
  174. real_t freelook_speed;
  175. Label *info_label;
  176. Label *fps_label;
  177. Label *cinema_label;
  178. Label *locked_label;
  179. struct _RayResult {
  180. Spatial *item;
  181. float depth;
  182. int handle;
  183. _FORCE_INLINE_ bool operator<(const _RayResult &p_rr) const { return depth < p_rr.depth; }
  184. };
  185. void _update_name();
  186. void _compute_edit(const Point2 &p_point);
  187. void _clear_selected();
  188. void _select_clicked(bool p_append, bool p_single);
  189. void _select(Node *p_node, bool p_append, bool p_single);
  190. ObjectID _select_ray(const Point2 &p_pos, bool p_append, bool &r_includes_current, int *r_gizmo_handle = NULL, bool p_alt_select = false);
  191. void _find_items_at_pos(const Point2 &p_pos, bool &r_includes_current, Vector<_RayResult> &results, bool p_alt_select = false);
  192. Vector3 _get_ray_pos(const Vector2 &p_pos) const;
  193. Vector3 _get_ray(const Vector2 &p_pos) const;
  194. Point2 _point_to_screen(const Vector3 &p_point);
  195. Transform _get_camera_transform() const;
  196. int get_selected_count() const;
  197. Vector3 _get_camera_position() const;
  198. Vector3 _get_camera_normal() const;
  199. Vector3 _get_screen_to_space(const Vector3 &p_vector3);
  200. void _select_region();
  201. bool _gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only = false);
  202. void _nav_pan(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
  203. void _nav_zoom(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
  204. void _nav_orbit(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
  205. void _nav_look(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
  206. float get_znear() const;
  207. float get_zfar() const;
  208. float get_fov() const;
  209. ObjectID clicked;
  210. Vector<_RayResult> selection_results;
  211. bool clicked_includes_current;
  212. bool clicked_wants_append;
  213. PopupMenu *selection_menu;
  214. enum NavigationScheme {
  215. NAVIGATION_GODOT,
  216. NAVIGATION_MAYA,
  217. NAVIGATION_MODO,
  218. };
  219. enum NavigationZoomStyle {
  220. NAVIGATION_ZOOM_VERTICAL,
  221. NAVIGATION_ZOOM_HORIZONTAL
  222. };
  223. enum NavigationMode {
  224. NAVIGATION_NONE,
  225. NAVIGATION_PAN,
  226. NAVIGATION_ZOOM,
  227. NAVIGATION_ORBIT,
  228. NAVIGATION_LOOK
  229. };
  230. enum TransformMode {
  231. TRANSFORM_NONE,
  232. TRANSFORM_ROTATE,
  233. TRANSFORM_TRANSLATE,
  234. TRANSFORM_SCALE
  235. };
  236. enum TransformPlane {
  237. TRANSFORM_VIEW,
  238. TRANSFORM_X_AXIS,
  239. TRANSFORM_Y_AXIS,
  240. TRANSFORM_Z_AXIS,
  241. TRANSFORM_YZ,
  242. TRANSFORM_XZ,
  243. TRANSFORM_XY,
  244. };
  245. struct EditData {
  246. TransformMode mode;
  247. TransformPlane plane;
  248. Transform original;
  249. Vector3 click_ray;
  250. Vector3 click_ray_pos;
  251. Vector3 center;
  252. Vector3 orig_gizmo_pos;
  253. int edited_gizmo;
  254. Point2 mouse_pos;
  255. bool snap;
  256. Ref<EditorSpatialGizmo> gizmo;
  257. int gizmo_handle;
  258. Variant gizmo_initial_value;
  259. Vector3 gizmo_initial_pos;
  260. } _edit;
  261. struct Cursor {
  262. Vector3 pos;
  263. float x_rot, y_rot, distance;
  264. Vector3 eye_pos; // Used in freelook mode
  265. bool region_select;
  266. Point2 region_begin, region_end;
  267. Cursor() {
  268. x_rot = y_rot = 0.5;
  269. distance = 4;
  270. region_select = false;
  271. }
  272. };
  273. // Viewport camera supports movement smoothing,
  274. // so one cursor is the real cursor, while the other can be an interpolated version.
  275. Cursor cursor; // Immediate cursor
  276. Cursor camera_cursor; // That one may be interpolated (don't modify this one except for smoothing purposes)
  277. void scale_cursor_distance(real_t scale);
  278. void set_freelook_active(bool active_now);
  279. void scale_freelook_speed(real_t scale);
  280. real_t zoom_indicator_delay;
  281. RID move_gizmo_instance[3], move_plane_gizmo_instance[3], rotate_gizmo_instance[3], scale_gizmo_instance[3], scale_plane_gizmo_instance[3];
  282. String last_message;
  283. String message;
  284. float message_time;
  285. void set_message(String p_message, float p_time = 5);
  286. //
  287. void _update_camera(float p_interp_delta);
  288. Transform to_camera_transform(const Cursor &p_cursor) const;
  289. void _draw();
  290. void _surface_mouse_enter();
  291. void _surface_mouse_exit();
  292. void _surface_focus_enter();
  293. void _surface_focus_exit();
  294. void _sinput(const Ref<InputEvent> &p_event);
  295. void _update_freelook(real_t delta);
  296. SpatialEditor *spatial_editor;
  297. Camera *previewing;
  298. Camera *preview;
  299. bool previewing_cinema;
  300. void _preview_exited_scene();
  301. void _toggle_camera_preview(bool);
  302. void _toggle_cinema_preview(bool);
  303. void _init_gizmo_instance(int p_idx);
  304. void _finish_gizmo_instances();
  305. void _selection_result_pressed(int);
  306. void _selection_menu_hide();
  307. void _list_select(Ref<InputEventMouseButton> b);
  308. Point2i _get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const;
  309. Vector3 _get_instance_position(const Point2 &p_pos) const;
  310. static AABB _calculate_spatial_bounds(const Spatial *p_parent, const AABB p_bounds);
  311. void _create_preview(const Vector<String> &files) const;
  312. void _remove_preview();
  313. bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node);
  314. bool _create_instance(Node *parent, String &path, const Point2 &p_point);
  315. void _perform_drop_data();
  316. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  317. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  318. protected:
  319. void _notification(int p_what);
  320. static void _bind_methods();
  321. public:
  322. void update_surface() { surface->update(); }
  323. void update_transform_gizmo_view();
  324. void set_can_preview(Camera *p_preview);
  325. void set_state(const Dictionary &p_state);
  326. Dictionary get_state() const;
  327. void reset();
  328. bool is_freelook_active() const { return freelook_active; }
  329. void focus_selection();
  330. void assign_pending_data_pointers(
  331. Spatial *p_preview_node,
  332. AABB *p_preview_bounds,
  333. AcceptDialog *p_accept);
  334. Viewport *get_viewport_node() { return viewport; }
  335. Camera *get_camera() { return camera; } // return the default camera object.
  336. SpatialEditorViewport(SpatialEditor *p_spatial_editor, EditorNode *p_editor, int p_index);
  337. };
  338. class SpatialEditorSelectedItem : public Object {
  339. GDCLASS(SpatialEditorSelectedItem, Object);
  340. public:
  341. AABB aabb;
  342. Transform original; // original location when moving
  343. Transform original_local;
  344. Transform last_xform; // last transform
  345. Spatial *sp;
  346. RID sbox_instance;
  347. SpatialEditorSelectedItem() { sp = NULL; }
  348. ~SpatialEditorSelectedItem();
  349. };
  350. class SpatialEditorViewportContainer : public Container {
  351. GDCLASS(SpatialEditorViewportContainer, Container)
  352. public:
  353. enum View {
  354. VIEW_USE_1_VIEWPORT,
  355. VIEW_USE_2_VIEWPORTS,
  356. VIEW_USE_2_VIEWPORTS_ALT,
  357. VIEW_USE_3_VIEWPORTS,
  358. VIEW_USE_3_VIEWPORTS_ALT,
  359. VIEW_USE_4_VIEWPORTS,
  360. };
  361. private:
  362. View view;
  363. bool mouseover;
  364. float ratio_h;
  365. float ratio_v;
  366. bool hovering_v;
  367. bool hovering_h;
  368. bool dragging_v;
  369. bool dragging_h;
  370. Vector2 drag_begin_pos;
  371. Vector2 drag_begin_ratio;
  372. void _gui_input(const Ref<InputEvent> &p_event);
  373. protected:
  374. void _notification(int p_what);
  375. static void _bind_methods();
  376. public:
  377. void set_view(View p_view);
  378. View get_view();
  379. SpatialEditorViewportContainer();
  380. };
  381. class SpatialEditor : public VBoxContainer {
  382. GDCLASS(SpatialEditor, VBoxContainer);
  383. public:
  384. static const unsigned int VIEWPORTS_COUNT = 4;
  385. enum ToolMode {
  386. TOOL_MODE_SELECT,
  387. TOOL_MODE_MOVE,
  388. TOOL_MODE_ROTATE,
  389. TOOL_MODE_SCALE,
  390. TOOL_MODE_LIST_SELECT,
  391. TOOL_LOCK_SELECTED,
  392. TOOL_UNLOCK_SELECTED,
  393. TOOL_MAX
  394. };
  395. enum ToolOptions {
  396. TOOL_OPT_LOCAL_COORDS,
  397. TOOL_OPT_USE_SNAP,
  398. TOOL_OPT_MAX
  399. };
  400. private:
  401. EditorNode *editor;
  402. EditorSelection *editor_selection;
  403. SpatialEditorViewportContainer *viewport_base;
  404. SpatialEditorViewport *viewports[VIEWPORTS_COUNT];
  405. VSplitContainer *shader_split;
  406. HSplitContainer *palette_split;
  407. /////
  408. ToolMode tool_mode;
  409. bool orthogonal;
  410. VisualServer::ScenarioDebugMode scenario_debug;
  411. RID origin;
  412. RID origin_instance;
  413. bool origin_enabled;
  414. RID grid[3];
  415. RID grid_instance[3];
  416. bool grid_visible[3]; //currently visible
  417. bool grid_enable[3]; //should be always visible if true
  418. bool grid_enabled;
  419. Ref<ArrayMesh> move_gizmo[3], move_plane_gizmo[3], rotate_gizmo[3], scale_gizmo[3], scale_plane_gizmo[3];
  420. Ref<SpatialMaterial> gizmo_color[3];
  421. Ref<SpatialMaterial> plane_gizmo_color[3];
  422. Ref<SpatialMaterial> gizmo_hl;
  423. int over_gizmo_handle;
  424. Ref<ArrayMesh> selection_box;
  425. RID indicators;
  426. RID indicators_instance;
  427. RID cursor_mesh;
  428. RID cursor_instance;
  429. Ref<SpatialMaterial> indicator_mat;
  430. Ref<SpatialMaterial> cursor_material;
  431. // Scene drag and drop support
  432. Spatial *preview_node;
  433. AABB preview_bounds;
  434. struct Gizmo {
  435. bool visible;
  436. float scale;
  437. Transform transform;
  438. } gizmo;
  439. enum MenuOption {
  440. MENU_TOOL_SELECT,
  441. MENU_TOOL_MOVE,
  442. MENU_TOOL_ROTATE,
  443. MENU_TOOL_SCALE,
  444. MENU_TOOL_LIST_SELECT,
  445. MENU_TOOL_LOCAL_COORDS,
  446. MENU_TOOL_USE_SNAP,
  447. MENU_TRANSFORM_CONFIGURE_SNAP,
  448. MENU_TRANSFORM_DIALOG,
  449. MENU_VIEW_USE_1_VIEWPORT,
  450. MENU_VIEW_USE_2_VIEWPORTS,
  451. MENU_VIEW_USE_2_VIEWPORTS_ALT,
  452. MENU_VIEW_USE_3_VIEWPORTS,
  453. MENU_VIEW_USE_3_VIEWPORTS_ALT,
  454. MENU_VIEW_USE_4_VIEWPORTS,
  455. MENU_VIEW_ORIGIN,
  456. MENU_VIEW_GRID,
  457. MENU_VIEW_GIZMOS_3D_ICONS,
  458. MENU_VIEW_CAMERA_SETTINGS,
  459. MENU_LOCK_SELECTED,
  460. MENU_UNLOCK_SELECTED,
  461. MENU_SNAP_TO_FLOOR
  462. };
  463. Button *tool_button[TOOL_MAX];
  464. Button *tool_option_button[TOOL_OPT_MAX];
  465. MenuButton *transform_menu;
  466. PopupMenu *gizmos_menu;
  467. MenuButton *view_menu;
  468. ToolButton *lock_button;
  469. ToolButton *unlock_button;
  470. AcceptDialog *accept;
  471. ConfirmationDialog *snap_dialog;
  472. ConfirmationDialog *xform_dialog;
  473. ConfirmationDialog *settings_dialog;
  474. bool snap_enabled;
  475. bool snap_key_enabled;
  476. LineEdit *snap_translate;
  477. LineEdit *snap_rotate;
  478. LineEdit *snap_scale;
  479. PanelContainer *menu_panel;
  480. LineEdit *xform_translate[3];
  481. LineEdit *xform_rotate[3];
  482. LineEdit *xform_scale[3];
  483. OptionButton *xform_type;
  484. VBoxContainer *settings_vbc;
  485. SpinBox *settings_fov;
  486. SpinBox *settings_znear;
  487. SpinBox *settings_zfar;
  488. void _xform_dialog_action();
  489. void _menu_item_pressed(int p_option);
  490. void _menu_item_toggled(bool pressed, int p_option);
  491. void _menu_gizmo_toggled(int p_option);
  492. HBoxContainer *hbc_menu;
  493. void _generate_selection_box();
  494. UndoRedo *undo_redo;
  495. void _instance_scene();
  496. void _init_indicators();
  497. void _update_gizmos_menu();
  498. void _init_grid();
  499. void _finish_indicators();
  500. void _finish_grid();
  501. void _toggle_maximize_view(Object *p_viewport);
  502. Node *custom_camera;
  503. Object *_get_editor_data(Object *p_what);
  504. Ref<Environment> viewport_environment;
  505. Spatial *selected;
  506. void _request_gizmo(Object *p_obj);
  507. static SpatialEditor *singleton;
  508. void _node_removed(Node *p_node);
  509. Vector<Ref<EditorSpatialGizmoPlugin> > gizmo_plugins;
  510. void _register_all_gizmos();
  511. SpatialEditor();
  512. bool is_any_freelook_active() const;
  513. void _refresh_menu_icons();
  514. protected:
  515. void _notification(int p_what);
  516. //void _gui_input(InputEvent p_event);
  517. void _unhandled_key_input(Ref<InputEvent> p_event);
  518. static void _bind_methods();
  519. public:
  520. static SpatialEditor *get_singleton() { return singleton; }
  521. void snap_cursor_to_plane(const Plane &p_plane);
  522. Vector3 snap_point(Vector3 p_target, Vector3 p_start = Vector3(0, 0, 0)) const;
  523. float get_znear() const { return settings_znear->get_value(); }
  524. float get_zfar() const { return settings_zfar->get_value(); }
  525. float get_fov() const { return settings_fov->get_value(); }
  526. Transform get_gizmo_transform() const { return gizmo.transform; }
  527. bool is_gizmo_visible() const { return gizmo.visible; }
  528. ToolMode get_tool_mode() const { return tool_mode; }
  529. bool are_local_coords_enabled() const { return tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->is_pressed(); }
  530. bool is_snap_enabled() const { return snap_enabled ^ snap_key_enabled; }
  531. float get_translate_snap() const { return snap_translate->get_text().to_double(); }
  532. float get_rotate_snap() const { return snap_rotate->get_text().to_double(); }
  533. float get_scale_snap() const { return snap_scale->get_text().to_double(); }
  534. Ref<ArrayMesh> get_move_gizmo(int idx) const { return move_gizmo[idx]; }
  535. Ref<ArrayMesh> get_move_plane_gizmo(int idx) const { return move_plane_gizmo[idx]; }
  536. Ref<ArrayMesh> get_rotate_gizmo(int idx) const { return rotate_gizmo[idx]; }
  537. Ref<ArrayMesh> get_scale_gizmo(int idx) const { return scale_gizmo[idx]; }
  538. Ref<ArrayMesh> get_scale_plane_gizmo(int idx) const { return scale_plane_gizmo[idx]; }
  539. void update_transform_gizmo();
  540. void update_all_gizmos(Node *p_node = NULL);
  541. void snap_selected_nodes_to_floor();
  542. void select_gizmo_highlight_axis(int p_axis);
  543. void set_custom_camera(Node *p_camera) { custom_camera = p_camera; }
  544. void set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }
  545. Dictionary get_state() const;
  546. void set_state(const Dictionary &p_state);
  547. Ref<Environment> get_viewport_environment() { return viewport_environment; }
  548. UndoRedo *get_undo_redo() { return undo_redo; }
  549. void add_control_to_menu_panel(Control *p_control);
  550. void remove_control_from_menu_panel(Control *p_control);
  551. VSplitContainer *get_shader_split();
  552. HSplitContainer *get_palette_split();
  553. Spatial *get_selected() { return selected; }
  554. int get_over_gizmo_handle() const { return over_gizmo_handle; }
  555. void set_over_gizmo_handle(int idx) { over_gizmo_handle = idx; }
  556. void set_can_preview(Camera *p_preview);
  557. SpatialEditorViewport *get_editor_viewport(int p_idx) {
  558. ERR_FAIL_INDEX_V(p_idx, 4, NULL);
  559. return viewports[p_idx];
  560. }
  561. void add_gizmo_plugin(Ref<EditorSpatialGizmoPlugin> p_plugin);
  562. void remove_gizmo_plugin(Ref<EditorSpatialGizmoPlugin> p_plugin);
  563. void edit(Spatial *p_spatial);
  564. void clear();
  565. SpatialEditor(EditorNode *p_editor);
  566. ~SpatialEditor();
  567. };
  568. class SpatialEditorPlugin : public EditorPlugin {
  569. GDCLASS(SpatialEditorPlugin, EditorPlugin);
  570. SpatialEditor *spatial_editor;
  571. EditorNode *editor;
  572. protected:
  573. static void _bind_methods();
  574. public:
  575. void snap_cursor_to_plane(const Plane &p_plane);
  576. SpatialEditor *get_spatial_editor() { return spatial_editor; }
  577. virtual String get_name() const { return "3D"; }
  578. bool has_main_screen() const { return true; }
  579. virtual void make_visible(bool p_visible);
  580. virtual void edit(Object *p_object);
  581. virtual bool handles(Object *p_object) const;
  582. virtual Dictionary get_state() const;
  583. virtual void set_state(const Dictionary &p_state);
  584. virtual void clear() { spatial_editor->clear(); }
  585. virtual void edited_scene_changed();
  586. SpatialEditorPlugin(EditorNode *p_node);
  587. ~SpatialEditorPlugin();
  588. };
  589. class EditorSpatialGizmoPlugin : public Resource {
  590. GDCLASS(EditorSpatialGizmoPlugin, Resource);
  591. public:
  592. static const int VISIBLE = 0;
  593. static const int HIDDEN = 1;
  594. static const int ON_TOP = 2;
  595. private:
  596. int current_state;
  597. List<EditorSpatialGizmo *> current_gizmos;
  598. HashMap<String, Vector<Ref<SpatialMaterial> > > materials;
  599. protected:
  600. static void _bind_methods();
  601. virtual bool has_gizmo(Spatial *p_spatial);
  602. virtual Ref<EditorSpatialGizmo> create_gizmo(Spatial *p_spatial);
  603. public:
  604. void create_material(const String &p_name, const Color &p_color, bool p_billboard = false, bool p_on_top = false, bool p_use_vertex_color = false);
  605. void create_icon_material(const String &p_name, const Ref<Texture> &p_texture, bool p_on_top = false, const Color &p_albedo = Color(1, 1, 1, 1));
  606. void create_handle_material(const String &p_name, bool p_billboard = false);
  607. void add_material(const String &p_name, Ref<SpatialMaterial> p_material);
  608. Ref<SpatialMaterial> get_material(const String &p_name, const Ref<EditorSpatialGizmo> &p_gizmo = Ref<EditorSpatialGizmo>());
  609. virtual String get_name() const;
  610. virtual bool can_be_hidden() const;
  611. virtual bool is_selectable_when_hidden() const;
  612. virtual void redraw(EditorSpatialGizmo *p_gizmo);
  613. virtual String get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const;
  614. virtual Variant get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const;
  615. virtual void set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point);
  616. virtual void commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel = false);
  617. virtual bool is_gizmo_handle_highlighted(const EditorSpatialGizmo *p_gizmo, int p_idx) const;
  618. Ref<EditorSpatialGizmo> get_gizmo(Spatial *p_spatial);
  619. void set_state(int p_state);
  620. void unregister_gizmo(EditorSpatialGizmo *p_gizmo);
  621. EditorSpatialGizmoPlugin();
  622. virtual ~EditorSpatialGizmoPlugin();
  623. };
  624. #endif