path_3d_editor_plugin.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**************************************************************************/
  2. /* path_3d_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 PATH_3D_EDITOR_PLUGIN_H
  31. #define PATH_3D_EDITOR_PLUGIN_H
  32. #include "editor/plugins/editor_plugin.h"
  33. #include "editor/plugins/node_3d_editor_gizmos.h"
  34. #include "scene/3d/camera_3d.h"
  35. #include "scene/3d/path_3d.h"
  36. class HBoxContainer;
  37. class MenuButton;
  38. class ConfirmationDialog;
  39. class Path3DGizmo : public EditorNode3DGizmo {
  40. GDCLASS(Path3DGizmo, EditorNode3DGizmo);
  41. // Map handle id to control point id and handle type.
  42. enum HandleType {
  43. HANDLE_TYPE_IN,
  44. HANDLE_TYPE_OUT,
  45. HANDLE_TYPE_TILT,
  46. };
  47. struct HandleInfo {
  48. int point_idx; // Index of control point.
  49. HandleType type; // Type of this handle.
  50. };
  51. Path3D *path = nullptr;
  52. mutable Vector3 original;
  53. mutable float orig_in_length;
  54. mutable float orig_out_length;
  55. mutable float disk_size = 0.8;
  56. // Cache information of secondary handles.
  57. Vector<HandleInfo> _secondary_handles_info;
  58. void _update_transform_gizmo();
  59. public:
  60. virtual String get_handle_name(int p_id, bool p_secondary) const override;
  61. virtual Variant get_handle_value(int p_id, bool p_secondary) const override;
  62. virtual void set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) override;
  63. virtual void commit_handle(int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel = false) override;
  64. virtual void redraw() override;
  65. Path3DGizmo(Path3D *p_path = nullptr, float p_disk_size = 0.8);
  66. };
  67. class Path3DGizmoPlugin : public EditorNode3DGizmoPlugin {
  68. GDCLASS(Path3DGizmoPlugin, EditorNode3DGizmoPlugin);
  69. float disk_size = 0.8;
  70. // Locking basis is meant to ensure a predictable behavior during translation of the curve points in "local space transform mode".
  71. // Without the locking, the gizmo/point, in "local space transform mode", wouldn't follow a straight path and would curve and twitch in an unpredictable way.
  72. HashMap<int, Basis> transformation_locked_basis;
  73. protected:
  74. Ref<EditorNode3DGizmo> create_gizmo(Node3D *p_spatial) override;
  75. public:
  76. virtual bool has_gizmo(Node3D *p_spatial) override;
  77. String get_gizmo_name() const override;
  78. virtual void redraw(EditorNode3DGizmo *p_gizmo) override;
  79. virtual int subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const override;
  80. virtual Vector<int> subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const override;
  81. virtual Transform3D get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const override;
  82. virtual void set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) override;
  83. virtual void commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel = false) override;
  84. int get_priority() const override;
  85. Path3DGizmoPlugin(float p_disk_size);
  86. };
  87. class Path3DEditorPlugin : public EditorPlugin {
  88. GDCLASS(Path3DEditorPlugin, EditorPlugin);
  89. friend class Path3DGizmo;
  90. friend class Path3DGizmoPlugin;
  91. Ref<Path3DGizmoPlugin> path_3d_gizmo_plugin;
  92. HBoxContainer *topmenu_bar = nullptr;
  93. HBoxContainer *toolbar = nullptr;
  94. Button *curve_create = nullptr;
  95. Button *curve_edit = nullptr;
  96. Button *curve_edit_curve = nullptr;
  97. Button *curve_edit_tilt = nullptr;
  98. Button *curve_del = nullptr;
  99. Button *curve_closed = nullptr;
  100. Button *curve_clear_points = nullptr;
  101. MenuButton *handle_menu = nullptr;
  102. Button *create_curve_button = nullptr;
  103. ConfirmationDialog *clear_points_dialog = nullptr;
  104. float disk_size = 0.8;
  105. enum Mode {
  106. MODE_CREATE,
  107. MODE_EDIT,
  108. MODE_EDIT_CURVE,
  109. MODE_EDIT_TILT,
  110. MODE_DELETE,
  111. ACTION_CLOSE
  112. };
  113. Path3D *path = nullptr;
  114. void _update_theme();
  115. void _update_toolbar();
  116. void _mode_changed(int p_mode);
  117. void _toggle_closed_curve();
  118. void _handle_option_pressed(int p_option);
  119. bool handle_clicked = false;
  120. bool mirror_handle_angle = true;
  121. bool mirror_handle_length = true;
  122. void _create_curve();
  123. void _confirm_clear_points();
  124. void _clear_points();
  125. void _clear_curve_points();
  126. void _restore_curve_points(const PackedVector3Array &p_points);
  127. enum HandleOption {
  128. HANDLE_OPTION_ANGLE,
  129. HANDLE_OPTION_LENGTH
  130. };
  131. protected:
  132. static void _bind_methods();
  133. public:
  134. Path3D *get_edited_path() { return path; }
  135. inline static Path3DEditorPlugin *singleton = nullptr;
  136. virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) override;
  137. virtual String get_name() const override { return "Path3D"; }
  138. bool has_main_screen() const override { return false; }
  139. virtual void edit(Object *p_object) override;
  140. virtual bool handles(Object *p_object) const override;
  141. virtual void make_visible(bool p_visible) override;
  142. bool mirror_angle_enabled() { return mirror_handle_angle; }
  143. bool mirror_length_enabled() { return mirror_handle_length; }
  144. bool is_handle_clicked() { return handle_clicked; }
  145. void set_handle_clicked(bool clicked) { handle_clicked = clicked; }
  146. Path3DEditorPlugin();
  147. };
  148. #endif // PATH_3D_EDITOR_PLUGIN_H