spatial.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*************************************************************************/
  2. /* spatial.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. #include "spatial.h"
  30. #include "scene/main/viewport.h"
  31. #include "message_queue.h"
  32. #include "scene/scene_string_names.h"
  33. /*
  34. possible algorithms:
  35. Algorithm 1: (current)
  36. definition of invalidation: global is invalid
  37. 1) If a node sets a LOCAL, it produces an invalidation of everything above
  38. a) If above is invalid, don't keep invalidating upwards
  39. 2) If a node sets a GLOBAL, it is converted to LOCAL (and forces validation of everything pending below)
  40. drawback: setting/reading globals is useful and used very very often, and using affine inverses is slow
  41. ---
  42. Algorithm 2: (no longer current)
  43. definition of invalidation: NONE dirty, LOCAL dirty, GLOBAL dirty
  44. 1) If a node sets a LOCAL, it must climb the tree and set it as GLOBAL dirty
  45. a) marking GLOBALs as dirty up all the tree must be done always
  46. 2) If a node sets a GLOBAL, it marks local as dirty, and that's all?
  47. //is clearing the dirty state correct in this case?
  48. drawback: setting a local down the tree forces many tree walks often
  49. --
  50. future: no idea
  51. */
  52. SpatialGizmo::SpatialGizmo() {
  53. }
  54. void Spatial::_notify_dirty() {
  55. if (!data.ignore_notification && !xform_change.in_list()) {
  56. get_scene()->xform_change_list.add(&xform_change);
  57. }
  58. }
  59. void Spatial::_update_local_transform() const {
  60. data.local_transform.basis.set_euler(data.rotation);
  61. data.local_transform.basis.scale(data.scale);
  62. data.dirty&=~DIRTY_LOCAL;
  63. }
  64. void Spatial::_propagate_transform_changed(Spatial *p_origin) {
  65. if (!is_inside_scene()) {
  66. return;
  67. }
  68. // if (data.dirty&DIRTY_GLOBAL)
  69. // return; //already dirty
  70. data.children_lock++;
  71. for (List<Spatial*>::Element *E=data.children.front();E;E=E->next()) {
  72. if (E->get()->data.toplevel_active)
  73. continue; //don't propagate to a toplevel
  74. E->get()->_propagate_transform_changed(p_origin);
  75. }
  76. if (!data.ignore_notification && !xform_change.in_list()) {
  77. get_scene()->xform_change_list.add(&xform_change);
  78. }
  79. data.dirty|=DIRTY_GLOBAL;
  80. data.children_lock--;
  81. }
  82. void Spatial::_notification(int p_what) {
  83. switch(p_what) {
  84. case NOTIFICATION_ENTER_SCENE: {
  85. Node *p = get_parent();
  86. if (p)
  87. data.parent=p->cast_to<Spatial>();
  88. if (data.parent)
  89. data.C=data.parent->data.children.push_back(this);
  90. else
  91. data.C=NULL;
  92. if (data.toplevel && !get_scene()->is_editor_hint()) {
  93. if (data.parent) {
  94. data.local_transform = data.parent->get_global_transform() * get_transform();
  95. data.dirty=DIRTY_VECTORS; //global is always dirty upon entering a scene
  96. }
  97. data.toplevel_active=true;
  98. }
  99. data.dirty|=DIRTY_GLOBAL; //global is always dirty upon entering a scene
  100. _notify_dirty();
  101. notification(NOTIFICATION_ENTER_WORLD);
  102. } break;
  103. case NOTIFICATION_EXIT_SCENE: {
  104. notification(NOTIFICATION_EXIT_WORLD,true);
  105. if (xform_change.in_list())
  106. get_scene()->xform_change_list.remove(&xform_change);
  107. if (data.C)
  108. data.parent->data.children.erase(data.C);
  109. data.parent=NULL;
  110. data.C=NULL;
  111. data.toplevel_active=false;
  112. } break;
  113. case NOTIFICATION_ENTER_WORLD: {
  114. data.inside_world=true;
  115. data.viewport=NULL;
  116. Node *parent = get_parent();
  117. while(parent && !data.viewport) {
  118. data.viewport=parent->cast_to<Viewport>();
  119. parent=parent->get_parent();
  120. }
  121. ERR_FAIL_COND(!data.viewport);
  122. if (get_script_instance()) {
  123. Variant::CallError err;
  124. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_enter_world,NULL,0);
  125. }
  126. #ifdef TOOLS_ENABLED
  127. if (get_scene()->is_editor_hint()) {
  128. // get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,SceneStringNames::get_singleton()->_spatial_editor_group,SceneStringNames::get_singleton()->_request_gizmo,this);
  129. get_scene()->call_group(0,SceneStringNames::get_singleton()->_spatial_editor_group,SceneStringNames::get_singleton()->_request_gizmo,this);
  130. if (!data.gizmo_disabled) {
  131. if (data.gizmo.is_valid())
  132. data.gizmo->create();
  133. }
  134. }
  135. #endif
  136. } break;
  137. case NOTIFICATION_EXIT_WORLD: {
  138. #ifdef TOOLS_ENABLED
  139. if (data.gizmo.is_valid()) {
  140. data.gizmo->free();
  141. }
  142. #endif
  143. if (get_script_instance()) {
  144. Variant::CallError err;
  145. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_exit_world,NULL,0);
  146. }
  147. data.viewport=NULL;
  148. data.inside_world=false;
  149. } break;
  150. case NOTIFICATION_TRANSFORM_CHANGED: {
  151. #ifdef TOOLS_ENABLED
  152. if (data.gizmo.is_valid()) {
  153. data.gizmo->transform();
  154. }
  155. #endif
  156. } break;
  157. default: {}
  158. }
  159. }
  160. void Spatial::set_transform(const Transform& p_transform) {
  161. data.local_transform=p_transform;
  162. data.dirty|=DIRTY_VECTORS;
  163. _change_notify("transform/translation");
  164. _change_notify("transform/rotation");
  165. _change_notify("transform/scale");
  166. _propagate_transform_changed(this);
  167. }
  168. void Spatial::set_global_transform(const Transform& p_transform) {
  169. Transform xform =
  170. (data.parent && !data.toplevel_active) ?
  171. data.parent->get_global_transform().affine_inverse() * p_transform :
  172. p_transform;
  173. set_transform(xform);
  174. }
  175. Transform Spatial::get_transform() const {
  176. if (data.dirty & DIRTY_LOCAL) {
  177. _update_local_transform();
  178. }
  179. return data.local_transform;
  180. }
  181. Transform Spatial::get_global_transform() const {
  182. ERR_FAIL_COND_V(!is_inside_scene(), Transform());
  183. if (data.dirty & DIRTY_GLOBAL) {
  184. if (data.dirty & DIRTY_LOCAL) {
  185. _update_local_transform();
  186. }
  187. if (data.parent && !data.toplevel_active) {
  188. data.global_transform=data.parent->get_global_transform() * data.local_transform;
  189. } else {
  190. data.global_transform=data.local_transform;
  191. }
  192. data.dirty&=~DIRTY_GLOBAL;
  193. }
  194. return data.global_transform;
  195. }
  196. #if 0
  197. void Spatial::add_child_notify(Node *p_child) {
  198. /*
  199. Spatial *s=p_child->cast_to<Spatial>();
  200. if (!s)
  201. return;
  202. ERR_FAIL_COND(data.children_lock>0);
  203. s->data.dirty=DIRTY_GLOBAL; // don't allow global transform to be valid
  204. s->data.parent=this;
  205. data.children.push_back(s);
  206. s->data.C=data.children.back();
  207. */
  208. }
  209. void Spatial::remove_child_notify(Node *p_child) {
  210. /*
  211. Spatial *s=p_child->cast_to<Spatial>();
  212. if (!s)
  213. return;
  214. ERR_FAIL_COND(data.children_lock>0);
  215. if (s->data.C)
  216. data.children.erase(s->data.C);
  217. s->data.parent=NULL;
  218. s->data.C=NULL;
  219. */
  220. }
  221. #endif
  222. Spatial *Spatial::get_parent_spatial() const {
  223. return data.parent;
  224. }
  225. Transform Spatial::get_relative_transform(const Node *p_parent) const {
  226. if (p_parent==this)
  227. return Transform();
  228. ERR_FAIL_COND_V(!data.parent,Transform());
  229. if (p_parent==data.parent)
  230. return get_transform();
  231. else
  232. return data.parent->get_relative_transform(p_parent) * get_transform();
  233. }
  234. void Spatial::set_translation(const Vector3& p_translation) {
  235. data.local_transform.origin=p_translation;
  236. _propagate_transform_changed(this);
  237. }
  238. void Spatial::set_rotation(const Vector3& p_euler){
  239. if (data.dirty&DIRTY_VECTORS) {
  240. data.scale=data.local_transform.basis.get_scale();
  241. data.dirty&=~DIRTY_VECTORS;
  242. }
  243. data.rotation=p_euler;
  244. data.dirty|=DIRTY_LOCAL;
  245. _propagate_transform_changed(this);
  246. }
  247. void Spatial::set_scale(const Vector3& p_scale){
  248. if (data.dirty&DIRTY_VECTORS) {
  249. data.rotation=data.local_transform.basis.get_euler();
  250. data.dirty&=~DIRTY_VECTORS;
  251. }
  252. data.scale=p_scale;
  253. data.dirty|=DIRTY_LOCAL;
  254. _propagate_transform_changed(this);
  255. }
  256. Vector3 Spatial::get_translation() const{
  257. return data.local_transform.origin;
  258. }
  259. Vector3 Spatial::get_rotation() const{
  260. if (data.dirty&DIRTY_VECTORS) {
  261. data.scale=data.local_transform.basis.get_scale();
  262. data.rotation=data.local_transform.basis.get_euler();
  263. data.dirty&=~DIRTY_VECTORS;
  264. }
  265. return data.rotation;
  266. }
  267. Vector3 Spatial::get_scale() const{
  268. if (data.dirty&DIRTY_VECTORS) {
  269. data.scale=data.local_transform.basis.get_scale();
  270. data.rotation=data.local_transform.basis.get_euler();
  271. data.dirty&=~DIRTY_VECTORS;
  272. }
  273. return data.scale;
  274. }
  275. void Spatial::update_gizmo() {
  276. #ifdef TOOLS_ENABLED
  277. if (!is_inside_world())
  278. return;
  279. if (!data.gizmo.is_valid())
  280. return;
  281. if (data.gizmo_dirty)
  282. return;
  283. data.gizmo_dirty=true;
  284. MessageQueue::get_singleton()->push_call(this,"_update_gizmo");
  285. #endif
  286. }
  287. void Spatial::set_gizmo(const Ref<SpatialGizmo>& p_gizmo) {
  288. #ifdef TOOLS_ENABLED
  289. if (data.gizmo_disabled)
  290. return;
  291. if (data.gizmo.is_valid() && is_inside_world())
  292. data.gizmo->free();
  293. data.gizmo=p_gizmo;
  294. if (data.gizmo.is_valid() && is_inside_world()) {
  295. data.gizmo->create();
  296. data.gizmo->redraw();
  297. data.gizmo->transform();
  298. }
  299. #endif
  300. }
  301. Ref<SpatialGizmo> Spatial::get_gizmo() const {
  302. #ifdef TOOLS_ENABLED
  303. return data.gizmo;
  304. #else
  305. return Ref<SpatialGizmo>();
  306. #endif
  307. }
  308. #ifdef TOOLS_ENABLED
  309. void Spatial::_update_gizmo() {
  310. data.gizmo_dirty=false;
  311. if (data.gizmo.is_valid())
  312. data.gizmo->redraw();
  313. }
  314. void Spatial::set_disable_gizmo(bool p_enabled) {
  315. data.gizmo_disabled=p_enabled;
  316. if (!p_enabled && data.gizmo.is_valid())
  317. data.gizmo=Ref<SpatialGizmo>();
  318. }
  319. #endif
  320. void Spatial::set_as_toplevel(bool p_enabled) {
  321. if (data.toplevel==p_enabled)
  322. return;
  323. if (is_inside_scene() && !get_scene()->is_editor_hint()) {
  324. if (p_enabled)
  325. set_transform(get_global_transform());
  326. else if (data.parent)
  327. set_transform(data.parent->get_global_transform().affine_inverse() * get_global_transform());
  328. data.toplevel=p_enabled;
  329. data.toplevel_active=p_enabled;
  330. } else {
  331. data.toplevel=p_enabled;
  332. }
  333. }
  334. bool Spatial::is_set_as_toplevel() const{
  335. return data.toplevel;
  336. }
  337. void Spatial::_set_rotation_deg(const Vector3& p_deg) {
  338. set_rotation(p_deg * Math_PI / 180.0);
  339. }
  340. Vector3 Spatial::_get_rotation_deg() const {
  341. return get_rotation() * 180.0 / Math_PI;
  342. }
  343. Ref<World> Spatial::get_world() const {
  344. ERR_FAIL_COND_V(!is_inside_world(),Ref<World>());
  345. return data.viewport->find_world();
  346. }
  347. #ifdef TOOLS_ENABLED
  348. void Spatial::set_import_transform(const Transform& p_transform) {
  349. data.import_transform=p_transform;
  350. }
  351. Transform Spatial::get_import_transform() const {
  352. return data.import_transform;
  353. }
  354. #endif
  355. void Spatial::_propagate_visibility_changed() {
  356. notification(NOTIFICATION_VISIBILITY_CHANGED);
  357. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  358. _change_notify("visibility/visible");
  359. for (List<Spatial*>::Element*E=data.children.front();E;E=E->next()) {
  360. Spatial *c=E->get();
  361. if (!c || !c->data.visible)
  362. continue;
  363. c->_propagate_visibility_changed();
  364. }
  365. }
  366. void Spatial::show() {
  367. if (data.visible)
  368. return;
  369. data.visible=true;
  370. if (!is_inside_scene())
  371. return;
  372. if (!data.parent || is_visible()) {
  373. _propagate_visibility_changed();
  374. }
  375. }
  376. void Spatial::hide(){
  377. if (!data.visible)
  378. return;
  379. bool was_visible = is_visible();
  380. data.visible=false;
  381. if (!data.parent || was_visible) {
  382. _propagate_visibility_changed();
  383. }
  384. }
  385. bool Spatial::is_visible() const{
  386. const Spatial *s=this;
  387. while(s) {
  388. if (!s->data.visible) {
  389. return false;
  390. }
  391. s=s->data.parent;
  392. }
  393. return true;
  394. }
  395. bool Spatial::is_hidden() const{
  396. return !data.visible;
  397. }
  398. void Spatial::_set_visible_(bool p_visible) {
  399. if (p_visible)
  400. show();
  401. else
  402. hide();
  403. }
  404. bool Spatial::_is_visible_() const {
  405. return !is_hidden();
  406. }
  407. void Spatial::_bind_methods() {
  408. ObjectTypeDB::bind_method(_MD("set_transform","local"), &Spatial::set_transform);
  409. ObjectTypeDB::bind_method(_MD("get_transform"), &Spatial::get_transform);
  410. ObjectTypeDB::bind_method(_MD("set_translation","translation"), &Spatial::set_translation);
  411. ObjectTypeDB::bind_method(_MD("get_translation"), &Spatial::get_translation);
  412. ObjectTypeDB::bind_method(_MD("set_rotation","rotation"), &Spatial::set_rotation);
  413. ObjectTypeDB::bind_method(_MD("get_rotation"), &Spatial::get_rotation);
  414. ObjectTypeDB::bind_method(_MD("set_scale","scale"), &Spatial::set_scale);
  415. ObjectTypeDB::bind_method(_MD("get_scale"), &Spatial::get_scale);
  416. ObjectTypeDB::bind_method(_MD("set_global_transform","global"), &Spatial::set_global_transform);
  417. ObjectTypeDB::bind_method(_MD("get_global_transform"), &Spatial::get_global_transform);
  418. ObjectTypeDB::bind_method(_MD("get_parent_spatial"), &Spatial::get_parent_spatial);
  419. ObjectTypeDB::bind_method(_MD("set_ignore_transform_notification","enabled"), &Spatial::set_ignore_transform_notification);
  420. ObjectTypeDB::bind_method(_MD("set_as_toplevel","enable"), &Spatial::set_as_toplevel);
  421. ObjectTypeDB::bind_method(_MD("is_set_as_toplevel"), &Spatial::is_set_as_toplevel);
  422. ObjectTypeDB::bind_method(_MD("_set_rotation_deg","rotation_deg"), &Spatial::_set_rotation_deg);
  423. ObjectTypeDB::bind_method(_MD("_get_rotation_deg"), &Spatial::_get_rotation_deg);
  424. ObjectTypeDB::bind_method(_MD("get_world:World"), &Spatial::get_world);
  425. #ifdef TOOLS_ENABLED
  426. ObjectTypeDB::bind_method(_MD("_update_gizmo"), &Spatial::_update_gizmo);
  427. ObjectTypeDB::bind_method(_MD("_set_import_transform"), &Spatial::set_import_transform);
  428. ObjectTypeDB::bind_method(_MD("_get_import_transform"), &Spatial::get_import_transform);
  429. ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"_import_transform",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_import_transform"),_SCS("_get_import_transform"));
  430. #endif
  431. ObjectTypeDB::bind_method(_MD("update_gizmo"), &Spatial::update_gizmo);
  432. ObjectTypeDB::bind_method(_MD("set_gizmo","gizmo:SpatialGizmo"), &Spatial::set_gizmo);
  433. ObjectTypeDB::bind_method(_MD("get_gizmo:SpatialGizmo"), &Spatial::get_gizmo);
  434. ObjectTypeDB::bind_method(_MD("show"), &Spatial::show);
  435. ObjectTypeDB::bind_method(_MD("hide"), &Spatial::hide);
  436. ObjectTypeDB::bind_method(_MD("is_visible"), &Spatial::is_visible);
  437. ObjectTypeDB::bind_method(_MD("is_hidden"), &Spatial::is_hidden);
  438. ObjectTypeDB::bind_method(_MD("_set_visible_"), &Spatial::_set_visible_);
  439. ObjectTypeDB::bind_method(_MD("_is_visible_"), &Spatial::_is_visible_);
  440. BIND_CONSTANT( NOTIFICATION_TRANSFORM_CHANGED );
  441. BIND_CONSTANT( NOTIFICATION_ENTER_WORLD );
  442. BIND_CONSTANT( NOTIFICATION_EXIT_WORLD );
  443. BIND_CONSTANT( NOTIFICATION_VISIBILITY_CHANGED );
  444. //ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"transform/global",PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR ), _SCS("set_global_transform"), _SCS("get_global_transform") );
  445. ADD_PROPERTYNZ( PropertyInfo(Variant::TRANSFORM,"transform/local",PROPERTY_HINT_NONE,""), _SCS("set_transform"), _SCS("get_transform") );
  446. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"transform/translation",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR), _SCS("set_translation"), _SCS("get_translation") );
  447. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"transform/rotation",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR), _SCS("_set_rotation_deg"), _SCS("_get_rotation_deg") );
  448. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"transform/rotation_rad",PROPERTY_HINT_NONE,"",0), _SCS("set_rotation"), _SCS("get_rotation") );
  449. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"transform/scale",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR), _SCS("set_scale"), _SCS("get_scale") );
  450. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"visibility/visible"), _SCS("_set_visible_"), _SCS("_is_visible_") );
  451. //ADD_PROPERTY( PropertyInfo(Variant::TRANSFORM,"transform/local"), _SCS("set_transform"), _SCS("get_transform") );
  452. ADD_SIGNAL( MethodInfo("visibility_changed" ) );
  453. }
  454. Spatial::Spatial() : xform_change(this)
  455. {
  456. data.dirty=DIRTY_NONE;
  457. data.children_lock=0;
  458. data.ignore_notification=false;
  459. data.toplevel=false;
  460. data.toplevel_active=false;
  461. data.scale=Vector3(1,1,1);
  462. data.viewport=NULL;
  463. data.inside_world=false;
  464. data.visible=true;
  465. #ifdef TOOLS_ENABLED
  466. data.gizmo_disabled=false;
  467. data.gizmo_dirty=false;
  468. #endif
  469. data.parent=NULL;
  470. data.C=NULL;
  471. }
  472. Spatial::~Spatial() {
  473. }