2
0

spatial_editor_plugin.h 25 KB

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