spatial_editor_plugin.h 22 KB

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