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