spatial_editor_plugin.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /**************************************************************************/
  2. /* spatial_editor_plugin.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "editor/editor_scale.h"
  35. #include "scene/3d/immediate_geometry.h"
  36. #include "scene/3d/light.h"
  37. #include "scene/3d/visual_instance.h"
  38. #include "scene/gui/panel_container.h"
  39. class Camera;
  40. class SpatialEditor;
  41. class EditorSpatialGizmoPlugin;
  42. class ViewportContainer;
  43. class SpatialEditorViewport;
  44. class ViewportNavigationControl;
  45. class EditorSpatialGizmo : public SpatialGizmo {
  46. GDCLASS(EditorSpatialGizmo, SpatialGizmo);
  47. bool selected;
  48. bool instanced;
  49. public:
  50. void set_selected(bool p_selected) { selected = p_selected; }
  51. bool is_selected() const { return selected; }
  52. struct Instance {
  53. RID instance;
  54. Ref<Mesh> mesh;
  55. Ref<Material> material;
  56. Ref<SkinReference> skin_reference;
  57. RID skeleton;
  58. bool billboard;
  59. bool unscaled;
  60. bool can_intersect;
  61. bool extra_margin;
  62. Instance() {
  63. billboard = false;
  64. unscaled = false;
  65. can_intersect = false;
  66. extra_margin = false;
  67. }
  68. void create_instance(Spatial *p_base, bool p_hidden = false);
  69. };
  70. Vector<Vector3> collision_segments;
  71. Ref<TriangleMesh> collision_mesh;
  72. struct Handle {
  73. Vector3 pos;
  74. bool billboard;
  75. };
  76. Vector<Vector3> handles;
  77. Vector<Vector3> secondary_handles;
  78. float selectable_icon_size;
  79. bool billboard_handle;
  80. bool valid;
  81. bool hidden;
  82. Spatial *base;
  83. Vector<Instance> instances;
  84. Spatial *spatial_node;
  85. EditorSpatialGizmoPlugin *gizmo_plugin;
  86. void _set_spatial_node(Node *p_node) { set_spatial_node(Object::cast_to<Spatial>(p_node)); }
  87. protected:
  88. static void _bind_methods();
  89. public:
  90. void add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material, bool p_billboard = false, const Color &p_modulate = Color(1, 1, 1));
  91. void add_vertices(const Vector<Vector3> &p_vertices, const Ref<Material> &p_material, Mesh::PrimitiveType p_primitive_type, bool p_billboard = false, const Color &p_modulate = Color(1, 1, 1));
  92. void add_mesh(const Ref<Mesh> &p_mesh, bool p_billboard = false, const Ref<SkinReference> &p_skin_reference = Ref<SkinReference>(), const Ref<Material> &p_material = Ref<Material>());
  93. void add_collision_segments(const Vector<Vector3> &p_lines);
  94. void add_collision_triangles(const Ref<TriangleMesh> &p_tmesh);
  95. void add_unscaled_billboard(const Ref<Material> &p_material, float p_scale = 1, const Color &p_modulate = Color(1, 1, 1));
  96. void add_handles(const Vector<Vector3> &p_handles, const Ref<Material> &p_material, bool p_billboard = false, bool p_secondary = false);
  97. void add_solid_box(Ref<Material> &p_material, Vector3 p_size, Vector3 p_position = Vector3());
  98. virtual bool is_handle_highlighted(int p_idx) const;
  99. virtual String get_handle_name(int p_idx) const;
  100. virtual Variant get_handle_value(int p_idx);
  101. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  102. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  103. void set_spatial_node(Spatial *p_node);
  104. Spatial *get_spatial_node() const { return spatial_node; }
  105. Ref<EditorSpatialGizmoPlugin> get_plugin() const { return gizmo_plugin; }
  106. Vector3 get_handle_pos(int p_idx) const;
  107. bool intersect_frustum(const Camera *p_camera, const Vector<Plane> &p_frustum);
  108. bool intersect_ray(Camera *p_camera, const Point2 &p_point, Vector3 &r_pos, Vector3 &r_normal, int *r_gizmo_handle = nullptr, bool p_sec_first = false);
  109. virtual void clear();
  110. virtual void create();
  111. virtual void transform();
  112. virtual void redraw();
  113. virtual void free();
  114. virtual bool is_editable() const;
  115. void set_hidden(bool p_hidden);
  116. void set_plugin(EditorSpatialGizmoPlugin *p_plugin);
  117. EditorSpatialGizmo();
  118. ~EditorSpatialGizmo();
  119. };
  120. class ViewportRotationControl : public Control {
  121. GDCLASS(ViewportRotationControl, Control);
  122. struct Axis2D {
  123. Vector2 screen_point;
  124. float z_axis = -99.0;
  125. int axis = -1;
  126. };
  127. struct Axis2DCompare {
  128. _FORCE_INLINE_ bool operator()(const Axis2D &l, const Axis2D &r) const {
  129. return l.z_axis < r.z_axis;
  130. }
  131. };
  132. SpatialEditorViewport *viewport = nullptr;
  133. Vector<Color> axis_colors;
  134. Vector<int> axis_menu_options;
  135. Vector2i orbiting_mouse_start;
  136. int orbiting_index = -1;
  137. int focused_axis = -2;
  138. const float AXIS_CIRCLE_RADIUS = 8.0f * EDSCALE;
  139. protected:
  140. static void _bind_methods();
  141. void _notification(int p_what);
  142. void _gui_input(Ref<InputEvent> p_event);
  143. void _draw();
  144. void _draw_axis(const Axis2D &p_axis);
  145. void _get_sorted_axis(Vector<Axis2D> &r_axis);
  146. void _update_focus();
  147. void _on_mouse_exited();
  148. void _process_click(int p_index, Vector2 p_position, bool p_pressed);
  149. void _process_drag(Ref<InputEventWithModifiers> p_event, int p_index, Vector2 p_position, Vector2 p_relative_position);
  150. public:
  151. void set_viewport(SpatialEditorViewport *p_viewport);
  152. };
  153. class SpatialEditorViewport : public Control {
  154. GDCLASS(SpatialEditorViewport, Control);
  155. friend class SpatialEditor;
  156. friend class ViewportNavigationControl;
  157. friend class ViewportRotationControl;
  158. // These values are serialized, so if adding new values
  159. // add to the bottom to preserve compatibility between
  160. // editor versions.
  161. enum {
  162. VIEW_TOP,
  163. VIEW_BOTTOM,
  164. VIEW_LEFT,
  165. VIEW_RIGHT,
  166. VIEW_FRONT,
  167. VIEW_REAR,
  168. VIEW_CENTER_TO_ORIGIN,
  169. VIEW_CENTER_TO_SELECTION,
  170. VIEW_ALIGN_TRANSFORM_WITH_VIEW,
  171. VIEW_ALIGN_ROTATION_WITH_VIEW,
  172. VIEW_PERSPECTIVE,
  173. VIEW_ENVIRONMENT,
  174. VIEW_ORTHOGONAL,
  175. VIEW_HALF_RESOLUTION,
  176. VIEW_AUDIO_LISTENER,
  177. VIEW_AUDIO_DOPPLER,
  178. VIEW_GIZMOS,
  179. VIEW_INFORMATION,
  180. VIEW_FPS,
  181. VIEW_DISPLAY_NORMAL,
  182. VIEW_DISPLAY_WIREFRAME,
  183. VIEW_DISPLAY_OVERDRAW,
  184. VIEW_DISPLAY_SHADELESS,
  185. VIEW_LOCK_ROTATION,
  186. VIEW_CINEMATIC_PREVIEW,
  187. VIEW_AUTO_ORTHOGONAL,
  188. VIEW_PORTAL_CULLING,
  189. VIEW_SELECTED_INFO,
  190. };
  191. enum ViewType {
  192. VIEW_TYPE_USER,
  193. VIEW_TYPE_TOP,
  194. VIEW_TYPE_BOTTOM,
  195. VIEW_TYPE_LEFT,
  196. VIEW_TYPE_RIGHT,
  197. VIEW_TYPE_FRONT,
  198. VIEW_TYPE_REAR,
  199. };
  200. public:
  201. enum {
  202. GIZMO_BASE_LAYER = 27,
  203. GIZMO_EDIT_LAYER = 26,
  204. GIZMO_GRID_LAYER = 25,
  205. MISC_TOOL_LAYER = 24
  206. };
  207. enum NavigationScheme {
  208. NAVIGATION_GODOT,
  209. NAVIGATION_MAYA,
  210. NAVIGATION_MODO,
  211. };
  212. enum FreelookNavigationScheme {
  213. FREELOOK_DEFAULT,
  214. FREELOOK_PARTIALLY_AXIS_LOCKED,
  215. FREELOOK_FULLY_AXIS_LOCKED,
  216. };
  217. private:
  218. int index;
  219. bool _project_settings_change_pending;
  220. ViewType view_type;
  221. void _menu_option(int p_option);
  222. void _set_auto_orthogonal();
  223. Spatial *preview_node;
  224. AABB *preview_bounds;
  225. Vector<String> selected_files;
  226. AcceptDialog *accept;
  227. Node *target_node;
  228. Point2 drop_pos;
  229. EditorNode *editor;
  230. EditorData *editor_data;
  231. EditorSelection *editor_selection;
  232. UndoRedo *undo_redo;
  233. CheckBox *preview_camera;
  234. ViewportContainer *viewport_container;
  235. MenuButton *view_menu;
  236. Control *surface;
  237. Viewport *viewport;
  238. Camera *camera;
  239. bool transforming;
  240. bool orthogonal;
  241. bool auto_orthogonal;
  242. bool lock_rotation;
  243. float gizmo_scale;
  244. bool freelook_active;
  245. real_t freelook_speed;
  246. Vector2 previous_mouse_position;
  247. Label *info_label;
  248. Label *selected_info_label;
  249. Label *cinema_label;
  250. Label *locked_label;
  251. Label *zoom_limit_label;
  252. VBoxContainer *top_right_vbox;
  253. VBoxContainer *bottom_center_vbox;
  254. ViewportNavigationControl *position_control;
  255. ViewportNavigationControl *look_control;
  256. ViewportRotationControl *rotation_control;
  257. Gradient *frame_time_gradient;
  258. Label *fps_label;
  259. struct _RayResult {
  260. Spatial *item;
  261. float depth;
  262. int handle;
  263. _FORCE_INLINE_ bool operator<(const _RayResult &p_rr) const { return depth < p_rr.depth; }
  264. };
  265. void _update_name();
  266. void _compute_edit(const Point2 &p_point);
  267. void _clear_selected();
  268. void _select_clicked(bool p_append, bool p_single, bool p_allow_locked = false);
  269. void _select(Node *p_node, bool p_append, bool p_single);
  270. ObjectID _select_ray(const Point2 &p_pos, bool p_append, bool &r_includes_current, int *r_gizmo_handle = nullptr, bool p_alt_select = false);
  271. void _find_items_at_pos(const Point2 &p_pos, bool &r_includes_current, Vector<_RayResult> &results, bool p_alt_select = false);
  272. Vector3 _get_ray_pos(const Vector2 &p_pos) const;
  273. Vector3 _get_ray(const Vector2 &p_pos) const;
  274. Point2 _point_to_screen(const Vector3 &p_point);
  275. Transform _get_camera_transform() const;
  276. int get_selected_count() const;
  277. Vector3 _get_camera_position() const;
  278. Vector3 _get_camera_normal() const;
  279. Vector3 _get_screen_to_space(const Vector3 &p_vector3);
  280. void _select_region();
  281. bool _gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only = false);
  282. void _nav_pan(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
  283. void _nav_zoom(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
  284. void _nav_orbit(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
  285. void _nav_look(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
  286. float get_znear() const;
  287. float get_zfar() const;
  288. float get_fov() const;
  289. ObjectID clicked;
  290. Vector<_RayResult> selection_results;
  291. bool clicked_includes_current;
  292. bool clicked_wants_append;
  293. bool selection_in_progress = false;
  294. PopupMenu *selection_menu;
  295. enum NavigationZoomStyle {
  296. NAVIGATION_ZOOM_VERTICAL,
  297. NAVIGATION_ZOOM_HORIZONTAL
  298. };
  299. enum NavigationMode {
  300. NAVIGATION_NONE,
  301. NAVIGATION_PAN,
  302. NAVIGATION_ZOOM,
  303. NAVIGATION_ORBIT,
  304. NAVIGATION_LOOK,
  305. NAVIGATION_MOVE
  306. };
  307. enum TransformMode {
  308. TRANSFORM_NONE,
  309. TRANSFORM_ROTATE,
  310. TRANSFORM_TRANSLATE,
  311. TRANSFORM_SCALE
  312. };
  313. enum TransformPlane {
  314. TRANSFORM_VIEW,
  315. TRANSFORM_X_AXIS,
  316. TRANSFORM_Y_AXIS,
  317. TRANSFORM_Z_AXIS,
  318. TRANSFORM_YZ,
  319. TRANSFORM_XZ,
  320. TRANSFORM_XY,
  321. };
  322. struct EditData {
  323. TransformMode mode;
  324. TransformPlane plane;
  325. Transform original;
  326. Vector3 click_ray;
  327. Vector3 click_ray_pos;
  328. Vector3 center;
  329. Vector3 orig_gizmo_pos;
  330. int edited_gizmo;
  331. Point2 mouse_pos;
  332. Point2 original_mouse_pos;
  333. bool snap;
  334. Ref<EditorSpatialGizmo> gizmo;
  335. int gizmo_handle;
  336. Variant gizmo_initial_value;
  337. Vector3 gizmo_initial_pos;
  338. } _edit;
  339. struct Cursor {
  340. Vector3 pos;
  341. float x_rot, y_rot, distance, fov_scale;
  342. Vector3 eye_pos; // Used in freelook mode
  343. bool region_select;
  344. Point2 region_begin, region_end;
  345. Cursor() {
  346. // These rotations place the camera in +X +Y +Z, aka south east, facing north west.
  347. x_rot = 0.5;
  348. y_rot = -0.5;
  349. distance = 4;
  350. fov_scale = 1.0;
  351. region_select = false;
  352. }
  353. };
  354. // Viewport camera supports movement smoothing,
  355. // so one cursor is the real cursor, while the other can be an interpolated version.
  356. Cursor cursor; // Immediate cursor
  357. Cursor camera_cursor; // That one may be interpolated (don't modify this one except for smoothing purposes)
  358. void scale_fov(real_t p_fov_offset);
  359. void reset_fov();
  360. void scale_cursor_distance(real_t scale);
  361. void set_freelook_active(bool active_now);
  362. void scale_freelook_speed(real_t scale);
  363. real_t zoom_indicator_delay;
  364. int zoom_failed_attempts_count = 0;
  365. RID move_gizmo_instance[3], move_plane_gizmo_instance[3], rotate_gizmo_instance[4], scale_gizmo_instance[3], scale_plane_gizmo_instance[3];
  366. String last_message;
  367. String message;
  368. float message_time;
  369. void set_message(String p_message, float p_time = 5);
  370. void _view_settings_confirmed(float p_interp_delta);
  371. void _update_camera(float p_interp_delta);
  372. void _update_navigation_controls_visibility();
  373. Transform to_camera_transform(const Cursor &p_cursor) const;
  374. void _draw();
  375. void _surface_mouse_enter();
  376. void _surface_mouse_exit();
  377. void _surface_focus_enter();
  378. void _surface_focus_exit();
  379. void _sinput(const Ref<InputEvent> &p_event);
  380. void _update_freelook(real_t delta);
  381. SpatialEditor *spatial_editor;
  382. Camera *previewing;
  383. Camera *preview;
  384. bool previewing_camera;
  385. bool previewing_cinema;
  386. bool _is_node_locked(const Node *p_node);
  387. void _preview_exited_scene();
  388. void _toggle_camera_preview(bool);
  389. void _toggle_cinema_preview(bool);
  390. void _init_gizmo_instance(int p_idx);
  391. void _finish_gizmo_instances();
  392. void _selection_result_pressed(int);
  393. void _selection_menu_hide();
  394. void _list_select(Ref<InputEventMouseButton> b);
  395. Point2i _get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const;
  396. Vector3 _get_instance_position(const Point2 &p_pos) const;
  397. static AABB _calculate_spatial_bounds(const Spatial *p_parent, bool p_exclude_toplevel_transform = true);
  398. Node *_sanitize_preview_node(Node *p_node) const;
  399. void _create_preview(const Vector<String> &files) const;
  400. void _remove_preview();
  401. bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node);
  402. bool _create_instance(Node *parent, String &path, const Point2 &p_point);
  403. void _perform_drop_data();
  404. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  405. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  406. void _project_settings_changed();
  407. protected:
  408. void _notification(int p_what);
  409. static void _bind_methods();
  410. public:
  411. void update_surface() { surface->update(); }
  412. void update_transform_gizmo_view();
  413. void set_can_preview(Camera *p_preview);
  414. void set_state(const Dictionary &p_state);
  415. Dictionary get_state() const;
  416. void reset();
  417. bool is_freelook_active() const { return freelook_active; }
  418. void focus_selection();
  419. void assign_pending_data_pointers(
  420. Spatial *p_preview_node,
  421. AABB *p_preview_bounds,
  422. AcceptDialog *p_accept);
  423. Viewport *get_viewport_node() { return viewport; }
  424. Camera *get_camera() { return camera; } // return the default camera object.
  425. SpatialEditorViewport(SpatialEditor *p_spatial_editor, EditorNode *p_editor, int p_index);
  426. ~SpatialEditorViewport();
  427. };
  428. class SpatialEditorSelectedItem : public Object {
  429. GDCLASS(SpatialEditorSelectedItem, Object);
  430. public:
  431. AABB aabb;
  432. Transform original; // original location when moving
  433. Transform original_local;
  434. Transform last_xform; // last transform
  435. bool last_xform_dirty;
  436. Spatial *sp;
  437. RID sbox_instance;
  438. RID sbox_instance_offset;
  439. RID sbox_instance_xray;
  440. RID sbox_instance_xray_offset;
  441. SpatialEditorSelectedItem() {
  442. sp = nullptr;
  443. last_xform_dirty = true;
  444. }
  445. ~SpatialEditorSelectedItem();
  446. };
  447. class SpatialEditorViewportContainer : public Container {
  448. GDCLASS(SpatialEditorViewportContainer, Container);
  449. public:
  450. enum View {
  451. VIEW_USE_1_VIEWPORT,
  452. VIEW_USE_2_VIEWPORTS,
  453. VIEW_USE_2_VIEWPORTS_ALT,
  454. VIEW_USE_3_VIEWPORTS,
  455. VIEW_USE_3_VIEWPORTS_ALT,
  456. VIEW_USE_4_VIEWPORTS,
  457. };
  458. private:
  459. View view;
  460. bool mouseover;
  461. float ratio_h;
  462. float ratio_v;
  463. bool hovering_v;
  464. bool hovering_h;
  465. bool dragging_v;
  466. bool dragging_h;
  467. Vector2 drag_begin_pos;
  468. Vector2 drag_begin_ratio;
  469. void _gui_input(const Ref<InputEvent> &p_event);
  470. protected:
  471. void _notification(int p_what);
  472. static void _bind_methods();
  473. public:
  474. void set_view(View p_view);
  475. View get_view();
  476. SpatialEditorViewportContainer();
  477. };
  478. class SpatialEditor : public VBoxContainer {
  479. GDCLASS(SpatialEditor, VBoxContainer);
  480. public:
  481. static const unsigned int VIEWPORTS_COUNT = 4;
  482. enum ToolMode {
  483. TOOL_MODE_SELECT,
  484. TOOL_MODE_MOVE,
  485. TOOL_MODE_ROTATE,
  486. TOOL_MODE_SCALE,
  487. TOOL_MODE_LIST_SELECT,
  488. TOOL_LOCK_SELECTED,
  489. TOOL_UNLOCK_SELECTED,
  490. TOOL_GROUP_SELECTED,
  491. TOOL_UNGROUP_SELECTED,
  492. TOOL_CONVERT_ROOMS,
  493. TOOL_MAX
  494. };
  495. enum ToolOptions {
  496. TOOL_OPT_LOCAL_COORDS,
  497. TOOL_OPT_USE_SNAP,
  498. TOOL_OPT_OVERRIDE_CAMERA,
  499. TOOL_OPT_MAX
  500. };
  501. private:
  502. EditorNode *editor;
  503. EditorSelection *editor_selection;
  504. SpatialEditorViewportContainer *viewport_base;
  505. SpatialEditorViewport *viewports[VIEWPORTS_COUNT];
  506. VSplitContainer *shader_split;
  507. HSplitContainer *left_panel_split;
  508. HSplitContainer *right_panel_split;
  509. /////
  510. ToolMode tool_mode;
  511. VisualServer::ScenarioDebugMode scenario_debug;
  512. RID origin;
  513. RID origin_instance;
  514. bool origin_enabled;
  515. RID grid[3];
  516. RID grid_instance[3];
  517. bool grid_visible[3]; //currently visible
  518. bool grid_enable[3]; //should be always visible if true
  519. bool grid_enabled;
  520. Ref<ArrayMesh> move_gizmo[3], move_plane_gizmo[3], rotate_gizmo[4], scale_gizmo[3], scale_plane_gizmo[3];
  521. Ref<Material3D> gizmo_color[3];
  522. Ref<Material3D> plane_gizmo_color[3];
  523. Ref<ShaderMaterial> rotate_gizmo_color[3];
  524. Ref<Material3D> gizmo_color_hl[3];
  525. Ref<Material3D> plane_gizmo_color_hl[3];
  526. Ref<ShaderMaterial> rotate_gizmo_color_hl[3];
  527. int over_gizmo_handle;
  528. float snap_translate_value;
  529. float snap_rotate_value;
  530. float snap_scale_value;
  531. Ref<ArrayMesh> selection_box_xray;
  532. Ref<ArrayMesh> selection_box;
  533. RID indicators;
  534. RID indicators_instance;
  535. RID cursor_mesh;
  536. RID cursor_instance;
  537. Ref<SpatialMaterial> indicator_mat;
  538. Ref<ShaderMaterial> grid_mat[3];
  539. // Scene drag and drop support
  540. Spatial *preview_node;
  541. AABB preview_bounds;
  542. struct Gizmo {
  543. bool visible;
  544. float scale;
  545. Transform transform;
  546. } gizmo;
  547. enum MenuOption {
  548. MENU_TOOL_SELECT,
  549. MENU_TOOL_MOVE,
  550. MENU_TOOL_ROTATE,
  551. MENU_TOOL_SCALE,
  552. MENU_TOOL_LIST_SELECT,
  553. MENU_TOOL_LOCAL_COORDS,
  554. MENU_TOOL_USE_SNAP,
  555. MENU_TOOL_OVERRIDE_CAMERA,
  556. MENU_TOOL_CONVERT_ROOMS,
  557. MENU_TRANSFORM_CONFIGURE_SNAP,
  558. MENU_TRANSFORM_DIALOG,
  559. MENU_VIEW_USE_1_VIEWPORT,
  560. MENU_VIEW_USE_2_VIEWPORTS,
  561. MENU_VIEW_USE_2_VIEWPORTS_ALT,
  562. MENU_VIEW_USE_3_VIEWPORTS,
  563. MENU_VIEW_USE_3_VIEWPORTS_ALT,
  564. MENU_VIEW_USE_4_VIEWPORTS,
  565. MENU_VIEW_ORIGIN,
  566. MENU_VIEW_GRID,
  567. MENU_VIEW_PORTAL_CULLING,
  568. MENU_VIEW_OCCLUSION_CULLING,
  569. MENU_VIEW_LEVEL_OF_DETAIL,
  570. MENU_VIEW_GIZMOS_3D_ICONS,
  571. MENU_VIEW_CAMERA_SETTINGS,
  572. MENU_LOCK_SELECTED,
  573. MENU_UNLOCK_SELECTED,
  574. MENU_GROUP_SELECTED,
  575. MENU_UNGROUP_SELECTED,
  576. MENU_SNAP_TO_FLOOR
  577. };
  578. Button *tool_button[TOOL_MAX];
  579. Button *tool_option_button[TOOL_OPT_MAX];
  580. MenuButton *transform_menu;
  581. PopupMenu *gizmos_menu;
  582. MenuButton *view_menu = nullptr;
  583. AcceptDialog *accept;
  584. ConfirmationDialog *snap_dialog;
  585. ConfirmationDialog *xform_dialog;
  586. ConfirmationDialog *settings_dialog;
  587. bool snap_enabled;
  588. bool snap_key_enabled;
  589. LineEdit *snap_translate;
  590. LineEdit *snap_rotate;
  591. LineEdit *snap_scale;
  592. PanelContainer *menu_panel;
  593. LineEdit *xform_translate[3];
  594. LineEdit *xform_rotate[3];
  595. LineEdit *xform_scale[3];
  596. OptionButton *xform_type;
  597. VBoxContainer *settings_vbc;
  598. SpinBox *settings_fov;
  599. SpinBox *settings_znear;
  600. SpinBox *settings_zfar;
  601. void _snap_changed();
  602. void _snap_update();
  603. void _xform_dialog_action();
  604. void _menu_item_pressed(int p_option);
  605. void _menu_item_toggled(bool pressed, int p_option);
  606. void _menu_gizmo_toggled(int p_option);
  607. void _update_camera_override_button(bool p_game_running);
  608. void _update_camera_override_viewport(Object *p_viewport);
  609. // Used for secondary menu items which are displayed depending on the currently selected node
  610. // (such as MeshInstance's "Mesh" menu).
  611. PanelContainer *context_menu_panel = nullptr;
  612. HBoxContainer *context_menu_hbox = nullptr;
  613. void _generate_selection_boxes();
  614. UndoRedo *undo_redo;
  615. int camera_override_viewport_id;
  616. void _init_indicators();
  617. void _update_context_menu_stylebox();
  618. void _update_gizmos_menu();
  619. void _update_gizmos_menu_theme();
  620. void _init_grid();
  621. void _finish_indicators();
  622. void _finish_grid();
  623. void _toggle_maximize_view(Object *p_viewport);
  624. Node *custom_camera;
  625. Object *_get_editor_data(Object *p_what);
  626. Ref<Environment> viewport_environment;
  627. Spatial *selected;
  628. void _request_gizmo(Object *p_obj);
  629. static SpatialEditor *singleton;
  630. void _node_removed(Node *p_node);
  631. Vector<Ref<EditorSpatialGizmoPlugin>> gizmo_plugins_by_priority;
  632. Vector<Ref<EditorSpatialGizmoPlugin>> gizmo_plugins_by_name;
  633. void _register_all_gizmos();
  634. bool is_any_freelook_active() const;
  635. void _selection_changed();
  636. void _refresh_menu_icons();
  637. protected:
  638. void _notification(int p_what);
  639. //void _gui_input(InputEvent p_event);
  640. void _unhandled_key_input(Ref<InputEvent> p_event);
  641. static void _bind_methods();
  642. public:
  643. static SpatialEditor *get_singleton() { return singleton; }
  644. void snap_cursor_to_plane(const Plane &p_plane);
  645. Vector3 snap_point(Vector3 p_target, Vector3 p_start = Vector3(0, 0, 0)) const;
  646. float get_znear() const { return settings_znear->get_value(); }
  647. float get_zfar() const { return settings_zfar->get_value(); }
  648. float get_fov() const { return settings_fov->get_value(); }
  649. Transform get_gizmo_transform() const { return gizmo.transform; }
  650. bool is_gizmo_visible() const { return gizmo.visible; }
  651. ToolMode get_tool_mode() const { return tool_mode; }
  652. bool are_local_coords_enabled() const { return tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->is_pressed(); }
  653. bool is_snap_enabled() const { return snap_enabled ^ snap_key_enabled; }
  654. float get_translate_snap() const;
  655. float get_rotate_snap() const;
  656. float get_scale_snap() const;
  657. Ref<ArrayMesh> get_move_gizmo(int idx) const { return move_gizmo[idx]; }
  658. Ref<ArrayMesh> get_move_plane_gizmo(int idx) const { return move_plane_gizmo[idx]; }
  659. Ref<ArrayMesh> get_rotate_gizmo(int idx) const { return rotate_gizmo[idx]; }
  660. Ref<ArrayMesh> get_scale_gizmo(int idx) const { return scale_gizmo[idx]; }
  661. Ref<ArrayMesh> get_scale_plane_gizmo(int idx) const { return scale_plane_gizmo[idx]; }
  662. void update_grid();
  663. void update_transform_gizmo();
  664. void update_portal_tools();
  665. void show_advanced_portal_tools(bool p_show);
  666. void update_all_gizmos(Node *p_node = nullptr);
  667. void snap_selected_nodes_to_floor();
  668. void select_gizmo_highlight_axis(int p_axis);
  669. void set_custom_camera(Node *p_camera) { custom_camera = p_camera; }
  670. void set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }
  671. Dictionary get_state() const;
  672. void set_state(const Dictionary &p_state);
  673. Ref<Environment> get_viewport_environment() { return viewport_environment; }
  674. UndoRedo *get_undo_redo() { return undo_redo; }
  675. void add_control_to_menu_panel(Control *p_control);
  676. void remove_control_from_menu_panel(Control *p_control);
  677. void add_control_to_left_panel(Control *p_control);
  678. void remove_control_from_left_panel(Control *p_control);
  679. void add_control_to_right_panel(Control *p_control);
  680. void remove_control_from_right_panel(Control *p_control);
  681. void move_control_to_left_panel(Control *p_control);
  682. void move_control_to_right_panel(Control *p_control);
  683. VSplitContainer *get_shader_split();
  684. Spatial *get_selected() { return selected; }
  685. int get_over_gizmo_handle() const { return over_gizmo_handle; }
  686. void set_over_gizmo_handle(int idx) { over_gizmo_handle = idx; }
  687. // Simple way to turn off (expensive) gizmo generation
  688. // especially for temporary objects in the editor.
  689. static bool _prevent_gizmo_generation;
  690. void set_can_preview(Camera *p_preview);
  691. void set_message(String p_message, float p_time = 5);
  692. SpatialEditorViewport *get_editor_viewport(int p_idx) {
  693. ERR_FAIL_INDEX_V(p_idx, static_cast<int>(VIEWPORTS_COUNT), nullptr);
  694. return viewports[p_idx];
  695. }
  696. void add_gizmo_plugin(Ref<EditorSpatialGizmoPlugin> p_plugin);
  697. void remove_gizmo_plugin(Ref<EditorSpatialGizmoPlugin> p_plugin);
  698. void edit(Spatial *p_spatial);
  699. void clear();
  700. SpatialEditor(EditorNode *p_editor);
  701. ~SpatialEditor();
  702. };
  703. class SpatialEditorPlugin : public EditorPlugin {
  704. GDCLASS(SpatialEditorPlugin, EditorPlugin);
  705. SpatialEditor *spatial_editor;
  706. EditorNode *editor;
  707. protected:
  708. static void _bind_methods();
  709. public:
  710. void snap_cursor_to_plane(const Plane &p_plane);
  711. SpatialEditor *get_spatial_editor() { return spatial_editor; }
  712. virtual String get_name() const { return "3D"; }
  713. bool has_main_screen() const { return true; }
  714. virtual void make_visible(bool p_visible);
  715. virtual void edit(Object *p_object);
  716. virtual bool handles(Object *p_object) const;
  717. virtual Dictionary get_state() const;
  718. virtual void set_state(const Dictionary &p_state);
  719. virtual void clear() { spatial_editor->clear(); }
  720. virtual void edited_scene_changed();
  721. SpatialEditorPlugin(EditorNode *p_node);
  722. ~SpatialEditorPlugin();
  723. };
  724. class EditorSpatialGizmoPlugin : public Resource {
  725. GDCLASS(EditorSpatialGizmoPlugin, Resource);
  726. public:
  727. static const int VISIBLE = 0;
  728. static const int HIDDEN = 1;
  729. static const int ON_TOP = 2;
  730. protected:
  731. int current_state;
  732. List<EditorSpatialGizmo *> current_gizmos;
  733. HashMap<String, Vector<Ref<Material3D>>> materials;
  734. static void _bind_methods();
  735. virtual bool has_gizmo(Spatial *p_spatial);
  736. virtual Ref<EditorSpatialGizmo> create_gizmo(Spatial *p_spatial);
  737. public:
  738. 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);
  739. 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));
  740. void create_handle_material(const String &p_name, bool p_billboard = false, const Ref<Texture> &p_icon = nullptr);
  741. void add_material(const String &p_name, Ref<Material3D> p_material);
  742. Ref<Material3D> get_material(const String &p_name, const Ref<EditorSpatialGizmo> &p_gizmo = Ref<EditorSpatialGizmo>());
  743. virtual String get_name() const;
  744. virtual int get_priority() const;
  745. virtual bool can_be_hidden() const;
  746. virtual bool is_selectable_when_hidden() const;
  747. virtual void redraw(EditorSpatialGizmo *p_gizmo);
  748. virtual String get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const;
  749. virtual Variant get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const;
  750. virtual void set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point);
  751. virtual void commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel = false);
  752. virtual bool is_handle_highlighted(const EditorSpatialGizmo *p_gizmo, int p_idx) const;
  753. Ref<EditorSpatialGizmo> get_gizmo(Spatial *p_spatial);
  754. void set_state(int p_state);
  755. int get_state() const;
  756. void unregister_gizmo(EditorSpatialGizmo *p_gizmo);
  757. EditorSpatialGizmoPlugin();
  758. virtual ~EditorSpatialGizmoPlugin();
  759. };
  760. class ViewportNavigationControl : public Control {
  761. GDCLASS(ViewportNavigationControl, Control);
  762. SpatialEditorViewport *viewport = nullptr;
  763. Vector2i focused_mouse_start;
  764. Vector2 focused_pos;
  765. bool hovered = false;
  766. int focused_index = -1;
  767. SpatialEditorViewport::NavigationMode nav_mode = SpatialEditorViewport::NavigationMode::NAVIGATION_NONE;
  768. const float AXIS_CIRCLE_RADIUS = 30.0f * EDSCALE;
  769. protected:
  770. static void _bind_methods();
  771. void _notification(int p_what);
  772. void _gui_input(Ref<InputEvent> p_event);
  773. void _draw();
  774. void _on_mouse_entered();
  775. void _on_mouse_exited();
  776. void _process_click(int p_index, Vector2 p_position, bool p_pressed);
  777. void _process_drag(int p_index, Vector2 p_position, Vector2 p_relative_position);
  778. void _update_navigation();
  779. public:
  780. void set_navigation_mode(SpatialEditorViewport::NavigationMode p_nav_mode);
  781. void set_viewport(SpatialEditorViewport *p_viewport);
  782. };
  783. #endif // SPATIAL_EDITOR_PLUGIN_H