occluder_instance_3d_editor_plugin.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_node.h"
  32. #include "editor/editor_string_names.h"
  33. #include "editor/gui/editor_file_dialog.h"
  34. void OccluderInstance3DEditorPlugin::_bake_select_file(const String &p_file) {
  35. if (occluder_instance) {
  36. OccluderInstance3D::BakeError err;
  37. if (get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root() == occluder_instance) {
  38. err = occluder_instance->bake_scene(occluder_instance, p_file);
  39. } else {
  40. err = occluder_instance->bake_scene(occluder_instance->get_parent(), p_file);
  41. }
  42. switch (err) {
  43. case OccluderInstance3D::BAKE_ERROR_NO_SAVE_PATH: {
  44. String scene_path = occluder_instance->get_scene_file_path();
  45. if (scene_path.is_empty()) {
  46. scene_path = occluder_instance->get_owner()->get_scene_file_path();
  47. }
  48. if (scene_path.is_empty()) {
  49. EditorNode::get_singleton()->show_warning(TTR("Can't determine a save path for the occluder.\nSave your scene and try again."));
  50. break;
  51. }
  52. scene_path = scene_path.get_basename() + ".occ";
  53. file_dialog->set_current_path(scene_path);
  54. file_dialog->popup_file_dialog();
  55. } break;
  56. case OccluderInstance3D::BAKE_ERROR_NO_MESHES: {
  57. 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."));
  58. break;
  59. }
  60. case OccluderInstance3D::BAKE_ERROR_CANT_SAVE: {
  61. EditorNode::get_singleton()->show_warning(TTR("Could not save the new occluder at the specified path:") + " " + p_file);
  62. break;
  63. }
  64. default: {
  65. }
  66. }
  67. }
  68. }
  69. void OccluderInstance3DEditorPlugin::_bake() {
  70. _bake_select_file("");
  71. }
  72. void OccluderInstance3DEditorPlugin::edit(Object *p_object) {
  73. OccluderInstance3D *s = Object::cast_to<OccluderInstance3D>(p_object);
  74. if (!s) {
  75. return;
  76. }
  77. occluder_instance = s;
  78. }
  79. bool OccluderInstance3DEditorPlugin::handles(Object *p_object) const {
  80. return p_object->is_class("OccluderInstance3D");
  81. }
  82. void OccluderInstance3DEditorPlugin::make_visible(bool p_visible) {
  83. if (p_visible) {
  84. bake->show();
  85. } else {
  86. bake->hide();
  87. }
  88. }
  89. void OccluderInstance3DEditorPlugin::_bind_methods() {
  90. ClassDB::bind_method("_bake", &OccluderInstance3DEditorPlugin::_bake);
  91. }
  92. OccluderInstance3DEditorPlugin::OccluderInstance3DEditorPlugin() {
  93. bake = memnew(Button);
  94. bake->set_theme_type_variation(SceneStringName(FlatButton));
  95. bake->set_button_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
  96. bake->set_text(TTR("Bake Occluders"));
  97. bake->hide();
  98. bake->connect(SceneStringName(pressed), Callable(this, "_bake"));
  99. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, bake);
  100. occluder_instance = nullptr;
  101. file_dialog = memnew(EditorFileDialog);
  102. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  103. file_dialog->add_filter("*.occ", "Occluder3D");
  104. file_dialog->set_title(TTR("Select occluder bake file:"));
  105. file_dialog->connect("file_selected", callable_mp(this, &OccluderInstance3DEditorPlugin::_bake_select_file));
  106. bake->add_child(file_dialog);
  107. }
  108. OccluderInstance3DEditorPlugin::~OccluderInstance3DEditorPlugin() {
  109. }