node_3d_editor_gizmos.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**************************************************************************/
  2. /* node_3d_editor_gizmos.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 NODE_3D_EDITOR_GIZMOS_H
  31. #define NODE_3D_EDITOR_GIZMOS_H
  32. #include "core/templates/hash_map.h"
  33. #include "core/templates/local_vector.h"
  34. #include "scene/3d/camera_3d.h"
  35. #include "scene/3d/node_3d.h"
  36. #include "scene/3d/skeleton_3d.h"
  37. class Timer;
  38. class EditorNode3DGizmoPlugin;
  39. class EditorNode3DGizmo : public Node3DGizmo {
  40. GDCLASS(EditorNode3DGizmo, Node3DGizmo);
  41. struct Instance {
  42. RID instance;
  43. Ref<Mesh> mesh;
  44. Ref<Material> material;
  45. Ref<SkinReference> skin_reference;
  46. bool extra_margin = false;
  47. Transform3D xform;
  48. void create_instance(Node3D *p_base, bool p_hidden = false);
  49. };
  50. bool selected;
  51. Vector<Vector3> collision_segments;
  52. Ref<TriangleMesh> collision_mesh;
  53. Vector<Vector3> handles;
  54. Vector<int> handle_ids;
  55. Vector<Vector3> secondary_handles;
  56. Vector<int> secondary_handle_ids;
  57. real_t selectable_icon_size;
  58. bool billboard_handle;
  59. bool valid;
  60. bool hidden;
  61. Vector<Instance> instances;
  62. Node3D *spatial_node = nullptr;
  63. void _set_node_3d(Node *p_node) { set_node_3d(Object::cast_to<Node3D>(p_node)); }
  64. protected:
  65. static void _bind_methods();
  66. EditorNode3DGizmoPlugin *gizmo_plugin = nullptr;
  67. GDVIRTUAL0(_redraw)
  68. GDVIRTUAL2RC(String, _get_handle_name, int, bool)
  69. GDVIRTUAL2RC(bool, _is_handle_highlighted, int, bool)
  70. GDVIRTUAL2RC(Variant, _get_handle_value, int, bool)
  71. GDVIRTUAL2(_begin_handle_action, int, bool)
  72. GDVIRTUAL4(_set_handle, int, bool, const Camera3D *, Vector2)
  73. GDVIRTUAL4(_commit_handle, int, bool, Variant, bool)
  74. GDVIRTUAL2RC(int, _subgizmos_intersect_ray, const Camera3D *, Vector2)
  75. GDVIRTUAL2RC(Vector<int>, _subgizmos_intersect_frustum, const Camera3D *, TypedArray<Plane>)
  76. GDVIRTUAL1RC(Transform3D, _get_subgizmo_transform, int)
  77. GDVIRTUAL2(_set_subgizmo_transform, int, Transform3D)
  78. GDVIRTUAL3(_commit_subgizmos, Vector<int>, TypedArray<Transform3D>, bool)
  79. public:
  80. 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));
  81. 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));
  82. void add_mesh(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material = Ref<Material>(), const Transform3D &p_xform = Transform3D(), const Ref<SkinReference> &p_skin_reference = Ref<SkinReference>());
  83. void add_collision_segments(const Vector<Vector3> &p_lines);
  84. void add_collision_triangles(const Ref<TriangleMesh> &p_tmesh);
  85. void add_unscaled_billboard(const Ref<Material> &p_material, real_t p_scale = 1, const Color &p_modulate = Color(1, 1, 1));
  86. void add_handles(const Vector<Vector3> &p_handles, const Ref<Material> &p_material, const Vector<int> &p_ids = Vector<int>(), bool p_billboard = false, bool p_secondary = false);
  87. void add_solid_box(const Ref<Material> &p_material, Vector3 p_size, Vector3 p_position = Vector3(), const Transform3D &p_xform = Transform3D());
  88. virtual bool is_handle_highlighted(int p_id, bool p_secondary) const;
  89. virtual String get_handle_name(int p_id, bool p_secondary) const;
  90. virtual Variant get_handle_value(int p_id, bool p_secondary) const;
  91. virtual void begin_handle_action(int p_id, bool p_secondary);
  92. virtual void set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point);
  93. virtual void commit_handle(int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel = false);
  94. virtual int subgizmos_intersect_ray(Camera3D *p_camera, const Vector2 &p_point) const;
  95. virtual Vector<int> subgizmos_intersect_frustum(const Camera3D *p_camera, const Vector<Plane> &p_frustum) const;
  96. virtual Transform3D get_subgizmo_transform(int p_id) const;
  97. virtual void set_subgizmo_transform(int p_id, Transform3D p_transform);
  98. virtual void commit_subgizmos(const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel = false);
  99. void set_selected(bool p_selected) { selected = p_selected; }
  100. bool is_selected() const { return selected; }
  101. void set_node_3d(Node3D *p_node);
  102. Node3D *get_node_3d() const { return spatial_node; }
  103. Ref<EditorNode3DGizmoPlugin> get_plugin() const { return gizmo_plugin; }
  104. bool intersect_frustum(const Camera3D *p_camera, const Vector<Plane> &p_frustum);
  105. void handles_intersect_ray(Camera3D *p_camera, const Vector2 &p_point, bool p_shift_pressed, int &r_id, bool &r_secondary);
  106. bool intersect_ray(Camera3D *p_camera, const Point2 &p_point, Vector3 &r_pos, Vector3 &r_normal);
  107. bool is_subgizmo_selected(int p_id) const;
  108. Vector<int> get_subgizmo_selection() const;
  109. virtual void clear() override;
  110. virtual void create() override;
  111. virtual void transform() override;
  112. virtual void redraw() override;
  113. virtual void free() override;
  114. virtual bool is_editable() const;
  115. void set_hidden(bool p_hidden);
  116. void set_plugin(EditorNode3DGizmoPlugin *p_plugin);
  117. EditorNode3DGizmo();
  118. ~EditorNode3DGizmo();
  119. };
  120. class EditorNode3DGizmoPlugin : public Resource {
  121. GDCLASS(EditorNode3DGizmoPlugin, Resource);
  122. public:
  123. static const int VISIBLE = 0;
  124. static const int HIDDEN = 1;
  125. static const int ON_TOP = 2;
  126. protected:
  127. int current_state;
  128. List<EditorNode3DGizmo *> current_gizmos;
  129. HashMap<String, Vector<Ref<StandardMaterial3D>>> materials;
  130. static void _bind_methods();
  131. virtual bool has_gizmo(Node3D *p_spatial);
  132. virtual Ref<EditorNode3DGizmo> create_gizmo(Node3D *p_spatial);
  133. GDVIRTUAL1RC(bool, _has_gizmo, Node3D *)
  134. GDVIRTUAL1RC(Ref<EditorNode3DGizmo>, _create_gizmo, Node3D *)
  135. GDVIRTUAL0RC(String, _get_gizmo_name)
  136. GDVIRTUAL0RC(int, _get_priority)
  137. GDVIRTUAL0RC(bool, _can_be_hidden)
  138. GDVIRTUAL0RC(bool, _is_selectable_when_hidden)
  139. GDVIRTUAL1(_redraw, Ref<EditorNode3DGizmo>)
  140. GDVIRTUAL3RC(String, _get_handle_name, Ref<EditorNode3DGizmo>, int, bool)
  141. GDVIRTUAL3RC(bool, _is_handle_highlighted, Ref<EditorNode3DGizmo>, int, bool)
  142. GDVIRTUAL3RC(Variant, _get_handle_value, Ref<EditorNode3DGizmo>, int, bool)
  143. GDVIRTUAL3(_begin_handle_action, Ref<EditorNode3DGizmo>, int, bool)
  144. GDVIRTUAL5(_set_handle, Ref<EditorNode3DGizmo>, int, bool, const Camera3D *, Vector2)
  145. GDVIRTUAL5(_commit_handle, Ref<EditorNode3DGizmo>, int, bool, Variant, bool)
  146. GDVIRTUAL3RC(int, _subgizmos_intersect_ray, Ref<EditorNode3DGizmo>, const Camera3D *, Vector2)
  147. GDVIRTUAL3RC(Vector<int>, _subgizmos_intersect_frustum, Ref<EditorNode3DGizmo>, const Camera3D *, TypedArray<Plane>)
  148. GDVIRTUAL2RC(Transform3D, _get_subgizmo_transform, Ref<EditorNode3DGizmo>, int)
  149. GDVIRTUAL3(_set_subgizmo_transform, Ref<EditorNode3DGizmo>, int, Transform3D)
  150. GDVIRTUAL4(_commit_subgizmos, Ref<EditorNode3DGizmo>, Vector<int>, TypedArray<Transform3D>, bool)
  151. public:
  152. 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);
  153. void create_icon_material(const String &p_name, const Ref<Texture2D> &p_texture, bool p_on_top = false, const Color &p_albedo = Color(1, 1, 1, 1));
  154. void create_handle_material(const String &p_name, bool p_billboard = false, const Ref<Texture2D> &p_texture = nullptr);
  155. void add_material(const String &p_name, Ref<StandardMaterial3D> p_material);
  156. Ref<StandardMaterial3D> get_material(const String &p_name, const Ref<EditorNode3DGizmo> &p_gizmo = Ref<EditorNode3DGizmo>());
  157. virtual String get_gizmo_name() const;
  158. virtual int get_priority() const;
  159. virtual bool can_be_hidden() const;
  160. virtual bool is_selectable_when_hidden() const;
  161. virtual void redraw(EditorNode3DGizmo *p_gizmo);
  162. virtual bool is_handle_highlighted(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const;
  163. virtual String get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const;
  164. virtual Variant get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const;
  165. virtual void begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary);
  166. virtual void set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point);
  167. virtual void commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel = false);
  168. virtual int subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const;
  169. virtual Vector<int> subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const;
  170. virtual Transform3D get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const;
  171. virtual void set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform);
  172. virtual void commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel = false);
  173. Ref<EditorNode3DGizmo> get_gizmo(Node3D *p_spatial);
  174. void set_state(int p_state);
  175. int get_state() const;
  176. void unregister_gizmo(EditorNode3DGizmo *p_gizmo);
  177. EditorNode3DGizmoPlugin();
  178. virtual ~EditorNode3DGizmoPlugin();
  179. };
  180. #endif // NODE_3D_EDITOR_GIZMOS_H