occluder_instance_3d_editor_plugin.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*************************************************************************/
  2. /* occluder_instance_3d_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "occluder_instance_3d_editor_plugin.h"
  31. #include "editor/editor_file_dialog.h"
  32. #include "editor/editor_node.h"
  33. void OccluderInstance3DEditorPlugin::_bake_select_file(const String &p_file) {
  34. if (occluder_instance) {
  35. OccluderInstance3D::BakeError err;
  36. if (get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root() == occluder_instance) {
  37. err = occluder_instance->bake_scene(occluder_instance, p_file);
  38. } else {
  39. err = occluder_instance->bake_scene(occluder_instance->get_parent(), p_file);
  40. }
  41. switch (err) {
  42. case OccluderInstance3D::BAKE_ERROR_NO_SAVE_PATH: {
  43. String scene_path = occluder_instance->get_scene_file_path();
  44. if (scene_path.is_empty()) {
  45. scene_path = occluder_instance->get_owner()->get_scene_file_path();
  46. }
  47. if (scene_path.is_empty()) {
  48. EditorNode::get_singleton()->show_warning(TTR("Can't determine a save path for the occluder.\nSave your scene and try again."));
  49. break;
  50. }
  51. scene_path = scene_path.get_basename() + ".occ";
  52. file_dialog->set_current_path(scene_path);
  53. file_dialog->popup_file_dialog();
  54. } break;
  55. case OccluderInstance3D::BAKE_ERROR_NO_MESHES: {
  56. EditorNode::get_singleton()->show_warning(TTR("No meshes to bake.\nMake sure there is at least one MeshInstance3D node in the scene whose visual layers are part of the OccluderInstance3D's Bake Mask property."));
  57. break;
  58. }
  59. case OccluderInstance3D::BAKE_ERROR_CANT_SAVE: {
  60. EditorNode::get_singleton()->show_warning(TTR("Could not save the new occluder at the specified path:") + " " + p_file);
  61. break;
  62. }
  63. default: {
  64. }
  65. }
  66. }
  67. }
  68. void OccluderInstance3DEditorPlugin::_bake() {
  69. _bake_select_file("");
  70. }
  71. void OccluderInstance3DEditorPlugin::edit(Object *p_object) {
  72. OccluderInstance3D *s = Object::cast_to<OccluderInstance3D>(p_object);
  73. if (!s) {
  74. return;
  75. }
  76. occluder_instance = s;
  77. }
  78. bool OccluderInstance3DEditorPlugin::handles(Object *p_object) const {
  79. return p_object->is_class("OccluderInstance3D");
  80. }
  81. void OccluderInstance3DEditorPlugin::make_visible(bool p_visible) {
  82. if (p_visible) {
  83. bake->show();
  84. } else {
  85. bake->hide();
  86. }
  87. }
  88. void OccluderInstance3DEditorPlugin::_bind_methods() {
  89. ClassDB::bind_method("_bake", &OccluderInstance3DEditorPlugin::_bake);
  90. }
  91. OccluderInstance3DEditorPlugin::OccluderInstance3DEditorPlugin() {
  92. bake = memnew(Button);
  93. bake->set_flat(true);
  94. bake->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Bake"), SNAME("EditorIcons")));
  95. bake->set_text(TTR("Bake Occluders"));
  96. bake->hide();
  97. bake->connect("pressed", Callable(this, "_bake"));
  98. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);
  99. occluder_instance = nullptr;
  100. file_dialog = memnew(EditorFileDialog);
  101. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  102. file_dialog->add_filter("*.occ", "Occluder3D");
  103. file_dialog->set_title(TTR("Select occluder bake file:"));
  104. file_dialog->connect("file_selected", callable_mp(this, &OccluderInstance3DEditorPlugin::_bake_select_file));
  105. bake->add_child(file_dialog);
  106. }
  107. OccluderInstance3DEditorPlugin::~OccluderInstance3DEditorPlugin() {
  108. }