2
0

spatial_editor_plugin.h 24 KB

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