spatial.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*************************************************************************/
  2. /* spatial.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_H
  31. #define SPATIAL_H
  32. #include "scene/main/node.h"
  33. #include "scene/main/scene_tree.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. class SpatialGizmo : public Reference {
  38. GDCLASS(SpatialGizmo, Reference);
  39. public:
  40. virtual void create() = 0;
  41. virtual void transform() = 0;
  42. virtual void clear() = 0;
  43. virtual void redraw() = 0;
  44. virtual void free() = 0;
  45. virtual bool can_draw() const = 0;
  46. SpatialGizmo();
  47. };
  48. class Spatial : public Node {
  49. GDCLASS(Spatial, Node);
  50. OBJ_CATEGORY("3D");
  51. enum TransformDirty {
  52. DIRTY_NONE = 0,
  53. DIRTY_VECTORS = 1,
  54. DIRTY_LOCAL = 2,
  55. DIRTY_GLOBAL = 4
  56. };
  57. mutable SelfList<Node> xform_change;
  58. struct Data {
  59. mutable Transform global_transform;
  60. mutable Transform local_transform;
  61. mutable Vector3 rotation;
  62. mutable Vector3 scale;
  63. mutable int dirty;
  64. Viewport *viewport;
  65. bool toplevel_active;
  66. bool toplevel;
  67. bool inside_world;
  68. int children_lock;
  69. Spatial *parent;
  70. List<Spatial *> children;
  71. List<Spatial *>::Element *C;
  72. bool ignore_notification;
  73. bool notify_local_transform;
  74. bool notify_transform;
  75. bool visible;
  76. #ifdef TOOLS_ENABLED
  77. Ref<SpatialGizmo> gizmo;
  78. bool gizmo_disabled;
  79. bool gizmo_dirty;
  80. #endif
  81. } data;
  82. #ifdef TOOLS_ENABLED
  83. void _update_gizmo();
  84. #endif
  85. void _notify_dirty();
  86. void _propagate_transform_changed(Spatial *p_origin);
  87. // Deprecated, should be removed in a future version.
  88. void _set_rotation_deg(const Vector3 &p_euler_deg);
  89. Vector3 _get_rotation_deg() const;
  90. void _propagate_visibility_changed();
  91. protected:
  92. _FORCE_INLINE_ void set_ignore_transform_notification(bool p_ignore) { data.ignore_notification = p_ignore; }
  93. _FORCE_INLINE_ void _update_local_transform() const;
  94. void _notification(int p_what);
  95. static void _bind_methods();
  96. public:
  97. enum {
  98. NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED,
  99. NOTIFICATION_ENTER_WORLD = 41,
  100. NOTIFICATION_EXIT_WORLD = 42,
  101. NOTIFICATION_VISIBILITY_CHANGED = 43,
  102. NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 44,
  103. };
  104. Spatial *get_parent_spatial() const;
  105. Ref<World> get_world() const;
  106. void set_translation(const Vector3 &p_translation);
  107. void set_rotation(const Vector3 &p_euler_rad);
  108. void set_rotation_in_degrees(const Vector3 &p_euler_deg);
  109. void set_scale(const Vector3 &p_scale);
  110. Vector3 get_translation() const;
  111. Vector3 get_rotation() const;
  112. Vector3 get_rotation_in_degrees() const;
  113. Vector3 get_scale() const;
  114. void set_transform(const Transform &p_transform);
  115. void set_global_transform(const Transform &p_transform);
  116. Transform get_transform() const;
  117. Transform get_global_transform() const;
  118. void set_as_toplevel(bool p_enabled);
  119. bool is_set_as_toplevel() const;
  120. void set_disable_gizmo(bool p_enabled);
  121. void update_gizmo();
  122. void set_gizmo(const Ref<SpatialGizmo> &p_gizmo);
  123. Ref<SpatialGizmo> get_gizmo() const;
  124. _FORCE_INLINE_ bool is_inside_world() const { return data.inside_world; }
  125. Transform get_relative_transform(const Node *p_parent) const;
  126. void rotate(const Vector3 &p_normal, float p_radians);
  127. void rotate_x(float p_radians);
  128. void rotate_y(float p_radians);
  129. void rotate_z(float p_radians);
  130. void translate(const Vector3 &p_offset);
  131. void scale(const Vector3 &p_ratio);
  132. void global_rotate(const Vector3 &p_normal, float p_radians);
  133. void global_translate(const Vector3 &p_offset);
  134. void look_at(const Vector3 &p_target, const Vector3 &p_up_normal);
  135. void look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up_normal);
  136. Vector3 to_local(Vector3 p_global) const;
  137. Vector3 to_global(Vector3 p_local) const;
  138. void set_notify_transform(bool p_enable);
  139. bool is_transform_notification_enabled() const;
  140. void set_notify_local_transform(bool p_enable);
  141. bool is_local_transform_notification_enabled() const;
  142. void orthonormalize();
  143. void set_identity();
  144. void set_visible(bool p_visible);
  145. bool is_visible() const;
  146. void show();
  147. void hide();
  148. bool is_visible_in_tree() const;
  149. Spatial();
  150. ~Spatial();
  151. };
  152. #endif