spatial.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*************************************************************************/
  2. /* spatial.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 <[email protected]>
  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. SpatialGizmo();
  46. virtual ~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. bool disable_scale;
  77. #ifdef TOOLS_ENABLED
  78. Ref<SpatialGizmo> gizmo;
  79. bool gizmo_disabled;
  80. bool gizmo_dirty;
  81. #endif
  82. } data;
  83. void _update_gizmo();
  84. void _notify_dirty();
  85. void _propagate_transform_changed(Spatial *p_origin);
  86. void _propagate_visibility_changed();
  87. protected:
  88. _FORCE_INLINE_ void set_ignore_transform_notification(bool p_ignore) { data.ignore_notification = p_ignore; }
  89. _FORCE_INLINE_ void _update_local_transform() const;
  90. void _notification(int p_what);
  91. static void _bind_methods();
  92. public:
  93. enum {
  94. NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED,
  95. NOTIFICATION_ENTER_WORLD = 41,
  96. NOTIFICATION_EXIT_WORLD = 42,
  97. NOTIFICATION_VISIBILITY_CHANGED = 43,
  98. NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 44,
  99. };
  100. Spatial *get_parent_spatial() const;
  101. Ref<World> get_world() const;
  102. void set_translation(const Vector3 &p_translation);
  103. void set_rotation(const Vector3 &p_euler_rad);
  104. void set_rotation_degrees(const Vector3 &p_euler_deg);
  105. void set_scale(const Vector3 &p_scale);
  106. Vector3 get_translation() const;
  107. Vector3 get_rotation() const;
  108. Vector3 get_rotation_degrees() const;
  109. Vector3 get_scale() const;
  110. void set_transform(const Transform &p_transform);
  111. void set_global_transform(const Transform &p_transform);
  112. Transform get_transform() const;
  113. Transform get_global_transform() const;
  114. #ifdef TOOLS_ENABLED
  115. virtual Transform get_global_gizmo_transform() const;
  116. virtual Transform get_local_gizmo_transform() const;
  117. #endif
  118. void set_as_toplevel(bool p_enabled);
  119. bool is_set_as_toplevel() const;
  120. void set_disable_scale(bool p_enabled);
  121. bool is_scale_disabled() const;
  122. void set_disable_gizmo(bool p_enabled);
  123. void update_gizmo();
  124. void set_gizmo(const Ref<SpatialGizmo> &p_gizmo);
  125. Ref<SpatialGizmo> get_gizmo() const;
  126. _FORCE_INLINE_ bool is_inside_world() const { return data.inside_world; }
  127. Transform get_relative_transform(const Node *p_parent) const;
  128. void rotate(const Vector3 &p_axis, float p_angle);
  129. void rotate_x(float p_angle);
  130. void rotate_y(float p_angle);
  131. void rotate_z(float p_angle);
  132. void translate(const Vector3 &p_offset);
  133. void scale(const Vector3 &p_ratio);
  134. void rotate_object_local(const Vector3 &p_axis, float p_angle);
  135. void scale_object_local(const Vector3 &p_scale);
  136. void translate_object_local(const Vector3 &p_offset);
  137. void global_rotate(const Vector3 &p_axis, float p_angle);
  138. void global_scale(const Vector3 &p_scale);
  139. void global_translate(const Vector3 &p_offset);
  140. void look_at(const Vector3 &p_target, const Vector3 &p_up);
  141. void look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up);
  142. Vector3 to_local(Vector3 p_global) const;
  143. Vector3 to_global(Vector3 p_local) const;
  144. void set_notify_transform(bool p_enable);
  145. bool is_transform_notification_enabled() const;
  146. void set_notify_local_transform(bool p_enable);
  147. bool is_local_transform_notification_enabled() const;
  148. void orthonormalize();
  149. void set_identity();
  150. void set_visible(bool p_visible);
  151. bool is_visible() const;
  152. void show();
  153. void hide();
  154. bool is_visible_in_tree() const;
  155. void force_update_transform();
  156. Spatial();
  157. ~Spatial();
  158. };
  159. #endif