arvr_script_interface.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "arvr_script_interface.h"
  2. ARVRScriptInterface::ARVRScriptInterface() {
  3. // testing
  4. printf("Construct script interface");
  5. }
  6. ARVRScriptInterface::~ARVRScriptInterface() {
  7. if (is_initialized()) {
  8. uninitialize();
  9. };
  10. // testing
  11. printf("Destruct script interface");
  12. }
  13. StringName ARVRScriptInterface::get_name() const {
  14. if (get_script_instance() && get_script_instance()->has_method("get_name")) {
  15. return get_script_instance()->call("get_name");
  16. } else {
  17. // just return something for now
  18. return "ARVR Script interface";
  19. }
  20. }
  21. int ARVRScriptInterface::get_capabilities() const {
  22. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_capabilities")), ARVRInterface::ARVR_NONE);
  23. return get_script_instance()->call("get_capabilities");
  24. };
  25. ARVRInterface::Tracking_status ARVRScriptInterface::get_tracking_status() const {
  26. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_tracking_status")), ARVRInterface::ARVR_NOT_TRACKING);
  27. int status = get_script_instance()->call("get_tracking_status");
  28. return (ARVRInterface::Tracking_status)status;
  29. }
  30. bool ARVRScriptInterface::get_anchor_detection_is_enabled() const {
  31. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_anchor_detection_is_enabled")), false);
  32. return get_script_instance()->call("get_anchor_detection_is_enabled");
  33. };
  34. void ARVRScriptInterface::set_anchor_detection_is_enabled(bool p_enable) {
  35. ERR_FAIL_COND(!(get_script_instance() && get_script_instance()->has_method("set_anchor_detection_is_enabled")));
  36. get_script_instance()->call("set_anchor_detection_is_enabled");
  37. };
  38. bool ARVRScriptInterface::is_stereo() {
  39. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("is_stereo")), false);
  40. return get_script_instance()->call("is_stereo");
  41. }
  42. bool ARVRScriptInterface::is_initialized() {
  43. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("is_initialized")), false);
  44. return get_script_instance()->call("is_initialized");
  45. }
  46. bool ARVRScriptInterface::initialize() {
  47. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("initialize")), false);
  48. return get_script_instance()->call("initialize");
  49. }
  50. void ARVRScriptInterface::uninitialize() {
  51. ARVRServer *arvr_server = ARVRServer::get_singleton();
  52. if (arvr_server != NULL) {
  53. // Whatever happens, make sure this is no longer our primary interface
  54. arvr_server->clear_primary_interface_if(this);
  55. }
  56. ERR_FAIL_COND(!(get_script_instance() && get_script_instance()->has_method("uninitialize")));
  57. get_script_instance()->call("uninitialize");
  58. }
  59. Size2 ARVRScriptInterface::get_recommended_render_targetsize() {
  60. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_recommended_render_targetsize")), Size2());
  61. return get_script_instance()->call("get_recommended_render_targetsize");
  62. }
  63. Transform ARVRScriptInterface::get_transform_for_eye(Eyes p_eye, const Transform &p_cam_transform) {
  64. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_transform_for_eye")), Transform());
  65. return get_script_instance()->call("get_transform_for_eye", p_eye, p_cam_transform);
  66. }
  67. // Suggestion from Reduz, as we can't return a CameraMatrix, return a PoolVector with our 16 floats
  68. PoolVector<float> ARVRScriptInterface::_get_projection_for_eye(Eyes p_eye, real_t p_aspect, real_t p_z_near, real_t p_z_far) {
  69. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("_get_projection_for_eye")), PoolVector<float>());
  70. return get_script_instance()->call("_get_projection_for_eye", p_eye, p_aspect, p_z_near, p_z_far);
  71. }
  72. CameraMatrix ARVRScriptInterface::get_projection_for_eye(Eyes p_eye, real_t p_aspect, real_t p_z_near, real_t p_z_far) {
  73. CameraMatrix cm;
  74. int i = 0;
  75. int j = 0;
  76. PoolVector<float> cm_as_floats = _get_projection_for_eye(p_eye, p_aspect, p_z_near, p_z_far);
  77. for (int k = 0; k < cm_as_floats.size() && i < 4; k++) {
  78. cm.matrix[i][j] = cm_as_floats[k];
  79. j++;
  80. if (j == 4) {
  81. j = 0;
  82. i++;
  83. };
  84. };
  85. return cm;
  86. }
  87. void ARVRScriptInterface::commit_for_eye(ARVRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) {
  88. ERR_FAIL_COND(!(get_script_instance() && get_script_instance()->has_method("commit_for_eye")));
  89. get_script_instance()->call("commit_for_eye");
  90. }
  91. void ARVRScriptInterface::process() {
  92. ERR_FAIL_COND(!(get_script_instance() && get_script_instance()->has_method("process")));
  93. get_script_instance()->call("process");
  94. }
  95. void ARVRScriptInterface::_bind_methods() {
  96. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::INT, "get_capabilities"));
  97. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "is_initialized"));
  98. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "initialize"));
  99. ClassDB::add_virtual_method(get_class_static(), MethodInfo("uninitialize"));
  100. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::INT, "get_tracking_status"));
  101. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "get_anchor_detection_is_enabled"));
  102. ClassDB::add_virtual_method(get_class_static(), MethodInfo("set_anchor_detection_is_enabled", PropertyInfo(Variant::BOOL, "enabled")));
  103. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "is_stereo"));
  104. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::VECTOR2, "get_recommended_render_targetsize"));
  105. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::TRANSFORM, "get_transform_for_eye", PropertyInfo(Variant::INT, "eye"), PropertyInfo(Variant::TRANSFORM, "cam_transform")));
  106. ClassDB::add_virtual_method(get_class_static(), MethodInfo("_get_projection_for_eye"));
  107. ClassDB::add_virtual_method(get_class_static(), MethodInfo("commit_for_eye", PropertyInfo(Variant::INT, "eye"), PropertyInfo(Variant::_RID, "render_target")));
  108. ClassDB::add_virtual_method(get_class_static(), MethodInfo("process"));
  109. }