spatial.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*************************************************************************/
  2. /* spatial.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef SPATIAL_H
  30. #define SPATIAL_H
  31. #include "scene/main/node.h"
  32. #include "scene/main/scene_main_loop.h"
  33. /**
  34. @author Juan Linietsky <reduzio@gmail.com>
  35. */
  36. class SpatialGizmo : public Reference {
  37. OBJ_TYPE(SpatialGizmo,Reference);
  38. public:
  39. virtual void create()=0;
  40. virtual void transform()=0;
  41. virtual void clear()=0;
  42. virtual void redraw()=0;
  43. virtual void free()=0;
  44. SpatialGizmo();
  45. };
  46. class Spatial : public Node {
  47. OBJ_TYPE( Spatial, Node );
  48. OBJ_CATEGORY("3D");
  49. enum TransformDirty {
  50. DIRTY_NONE=0,
  51. DIRTY_VECTORS=1,
  52. DIRTY_LOCAL=2,
  53. DIRTY_GLOBAL=4
  54. };
  55. mutable SelfList<Node> xform_change;
  56. struct Data {
  57. mutable Transform global_transform;
  58. mutable Transform local_transform;
  59. mutable Vector3 rotation;
  60. mutable Vector3 scale;
  61. mutable int dirty;
  62. Viewport *viewport;
  63. bool toplevel_active;
  64. bool toplevel;
  65. bool inside_world;
  66. int children_lock;
  67. Spatial *parent;
  68. List<Spatial*> children;
  69. List<Spatial*>::Element *C;
  70. bool ignore_notification;
  71. bool notify_local_transform;
  72. bool visible;
  73. #ifdef TOOLS_ENABLED
  74. Ref<SpatialGizmo> gizmo;
  75. bool gizmo_disabled;
  76. bool gizmo_dirty;
  77. Transform import_transform;
  78. #endif
  79. } data;
  80. #ifdef TOOLS_ENABLED
  81. void _update_gizmo();
  82. #endif
  83. void _notify_dirty();
  84. void _propagate_transform_changed(Spatial *p_origin);
  85. // Deprecated, should be removed in a future version.
  86. void _set_rotation_deg(const Vector3& p_euler_deg);
  87. Vector3 _get_rotation_deg() const;
  88. void _propagate_visibility_changed();
  89. protected:
  90. _FORCE_INLINE_ void set_ignore_transform_notification(bool p_ignore) { data.ignore_notification=p_ignore; }
  91. _FORCE_INLINE_ void _update_local_transform() const;
  92. void _notification(int p_what);
  93. static void _bind_methods();
  94. void _set_visible_(bool p_visible);
  95. bool _is_visible_() const;
  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_deg(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_deg() 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_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal);
  136. void set_notify_local_transform(bool p_enable);
  137. bool is_local_transform_notification_enabled() const;
  138. void orthonormalize();
  139. void set_identity();
  140. void show();
  141. void hide();
  142. bool is_visible() const;
  143. bool is_hidden() const;
  144. void set_hidden(bool p_hidden);
  145. #ifdef TOOLS_ENABLED
  146. void set_import_transform(const Transform& p_transform) ;
  147. Transform get_import_transform() const;
  148. #endif
  149. Spatial();
  150. ~Spatial();
  151. };
  152. #endif