| 1 | /**************************************************************************/ |
| 2 | /* navigation_region_2d_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 "navigation_region_2d_editor_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_node.h" |
| 34 | #include "editor/editor_undo_redo_manager.h" |
| 35 | #include "editor/settings/editor_settings.h" |
| 36 | #include "scene/2d/navigation/navigation_region_2d.h" |
| 37 | #include "scene/gui/dialogs.h" |
| 38 | #include "scene/main/timer.h" |
| 39 | |
| 40 | Ref<NavigationPolygon> NavigationRegion2DEditor::_ensure_navpoly() const { |
| 41 | Ref<NavigationPolygon> navpoly = node->get_navigation_polygon(); |
| 42 | if (navpoly.is_null()) { |
| 43 | navpoly.instantiate(); |
| 44 | node->set_navigation_polygon(navpoly); |
| 45 | } |
| 46 | return navpoly; |
| 47 | } |
| 48 | |
| 49 | Node2D *NavigationRegion2DEditor::_get_node() const { |
| 50 | return node; |
| 51 | } |
| 52 | |
| 53 | void NavigationRegion2DEditor::_set_node(Node *p_polygon) { |
| 54 | node = Object::cast_to<NavigationRegion2D>(p_polygon); |
| 55 | if (node) { |
| 56 | Ref<NavigationPolygon> navpoly = node->get_navigation_polygon(); |
| 57 | if (navpoly.is_valid() && navpoly->get_outline_count() > 0 && navpoly->get_polygon_count() == 0) { |
| 58 | // We have outlines drawn / added by the user but no polygons were created for this navmesh yet so let's bake once immediately. |
| 59 | _rebake_timer_timeout(); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | int NavigationRegion2DEditor::_get_polygon_count() const { |
| 65 | Ref<NavigationPolygon> navpoly = node->get_navigation_polygon(); |
| 66 | if (navpoly.is_valid()) { |
| 67 | return navpoly->get_outline_count(); |
| 68 | } else { |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | Variant NavigationRegion2DEditor::_get_polygon(int p_idx) const { |
| 74 | Ref<NavigationPolygon> navpoly = node->get_navigation_polygon(); |
| 75 | if (navpoly.is_valid()) { |
| 76 | return navpoly->get_outline(p_idx); |
| 77 | } else { |
| 78 | return Variant(Vector<Vector2>()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void NavigationRegion2DEditor::_set_polygon(int p_idx, const Variant &p_polygon) const { |
| 83 | Ref<NavigationPolygon> navpoly = _ensure_navpoly(); |
| 84 | navpoly->set_outline(p_idx, p_polygon); |
| 85 | |
| 86 | if (rebake_timer && _rebake_timer_delay >= 0.0) { |
| 87 | rebake_timer->start(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void NavigationRegion2DEditor::_action_add_polygon(const Variant &p_polygon) { |
| 92 | Ref<NavigationPolygon> navpoly = _ensure_navpoly(); |
| 93 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 94 | undo_redo->add_do_method(navpoly.ptr(), "add_outline", p_polygon); |
| 95 | undo_redo->add_undo_method(navpoly.ptr(), "remove_outline", navpoly->get_outline_count()); |
| 96 | |
| 97 | if (rebake_timer && _rebake_timer_delay >= 0.0) { |
| 98 | rebake_timer->start(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void NavigationRegion2DEditor::_action_remove_polygon(int p_idx) { |
| 103 | Ref<NavigationPolygon> navpoly = _ensure_navpoly(); |
| 104 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 105 | undo_redo->add_do_method(navpoly.ptr(), "remove_outline", p_idx); |
| 106 | undo_redo->add_undo_method(navpoly.ptr(), "add_outline_at_index", navpoly->get_outline(p_idx), p_idx); |
| 107 | |
| 108 | if (rebake_timer && _rebake_timer_delay >= 0.0) { |
| 109 | rebake_timer->start(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void NavigationRegion2DEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) { |
| 114 | Ref<NavigationPolygon> navpoly = _ensure_navpoly(); |
| 115 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 116 | undo_redo->add_do_method(navpoly.ptr(), "set_outline", p_idx, p_polygon); |
| 117 | undo_redo->add_undo_method(navpoly.ptr(), "set_outline", p_idx, p_previous); |
| 118 | |
| 119 | if (rebake_timer && _rebake_timer_delay >= 0.0) { |
| 120 | rebake_timer->start(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | bool NavigationRegion2DEditor::_has_resource() const { |
| 125 | return node && node->get_navigation_polygon().is_valid(); |
| 126 | } |
| 127 | |
| 128 | void NavigationRegion2DEditor::_create_resource() { |
| 129 | if (!node) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 134 | undo_redo->create_action(TTR("Create Navigation Polygon")); |
| 135 | undo_redo->add_do_method(node, "set_navigation_polygon", Ref<NavigationPolygon>(memnew(NavigationPolygon))); |
| 136 | undo_redo->add_undo_method(node, "set_navigation_polygon", Variant(Ref<RefCounted>())); |
| 137 | undo_redo->commit_action(); |
| 138 | |
| 139 | _menu_option(MODE_CREATE); |
| 140 | } |
| 141 | |
| 142 | NavigationRegion2DEditor::NavigationRegion2DEditor() { |
| 143 | bake_hbox = memnew(HBoxContainer); |
| 144 | add_child(bake_hbox); |
| 145 | |
| 146 | button_bake = memnew(Button); |
| 147 | button_bake->set_flat(true); |
| 148 | bake_hbox->add_child(button_bake); |
| 149 | button_bake->set_toggle_mode(true); |
| 150 | button_bake->set_text(TTR("Bake NavigationPolygon")); |
| 151 | button_bake->set_tooltip_text(TTR("Bakes the NavigationPolygon by first parsing the scene for source geometry and then creating the navigation polygon vertices and polygons.")); |
| 152 | button_bake->connect(SceneStringName(pressed), callable_mp(this, &NavigationRegion2DEditor::_bake_pressed)); |
| 153 | |
| 154 | button_reset = memnew(Button); |
| 155 | button_reset->set_flat(true); |
| 156 | bake_hbox->add_child(button_reset); |
| 157 | button_reset->set_text(TTR("Clear NavigationPolygon")); |
| 158 | button_reset->set_tooltip_text(TTR("Clears the internal NavigationPolygon outlines, vertices and polygons.")); |
| 159 | button_reset->connect(SceneStringName(pressed), callable_mp(this, &NavigationRegion2DEditor::_clear_pressed)); |
| 160 | |
| 161 | bake_info = memnew(Label); |
| 162 | bake_info->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 163 | bake_hbox->add_child(bake_info); |
| 164 | |
| 165 | rebake_timer = memnew(Timer); |
| 166 | add_child(rebake_timer); |
| 167 | rebake_timer->set_one_shot(true); |
| 168 | _rebake_timer_delay = EDITOR_GET("editors/polygon_editor/auto_bake_delay"); |
| 169 | if (_rebake_timer_delay >= 0.0) { |
| 170 | rebake_timer->set_wait_time(_rebake_timer_delay); |
| 171 | } |
| 172 | rebake_timer->connect("timeout", callable_mp(this, &NavigationRegion2DEditor::_rebake_timer_timeout)); |
| 173 | |
| 174 | err_dialog = memnew(AcceptDialog); |
| 175 | add_child(err_dialog); |
| 176 | node = nullptr; |
| 177 | } |
| 178 | |
| 179 | void NavigationRegion2DEditor::_notification(int p_what) { |
| 180 | switch (p_what) { |
| 181 | case NOTIFICATION_ENTER_TREE: { |
| 182 | button_bake->set_button_icon(get_editor_theme_icon(SNAME("Bake"))); |
| 183 | button_reset->set_button_icon(get_editor_theme_icon(SNAME("Reload"))); |
| 184 | } break; |
| 185 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 186 | if (rebake_timer) { |
| 187 | _rebake_timer_delay = EDITOR_GET("editors/polygon_editor/auto_bake_delay"); |
| 188 | if (_rebake_timer_delay >= 0.0) { |
| 189 | rebake_timer->set_wait_time(_rebake_timer_delay); |
| 190 | } |
| 191 | } |
| 192 | } break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void NavigationRegion2DEditor::_bake_pressed() { |
| 197 | if (rebake_timer) { |
| 198 | rebake_timer->stop(); |
| 199 | } |
| 200 | button_bake->set_pressed(false); |
| 201 | |
| 202 | ERR_FAIL_NULL(node); |
| 203 | Ref<NavigationPolygon> navigation_polygon = node->get_navigation_polygon(); |
| 204 | if (navigation_polygon.is_null()) { |
| 205 | err_dialog->set_text(TTR("A NavigationPolygon resource must be set or created for this node to work.")); |
| 206 | err_dialog->popup_centered(); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | node->bake_navigation_polygon(true); |
| 211 | |
| 212 | node->queue_redraw(); |
| 213 | } |
| 214 | |
| 215 | void NavigationRegion2DEditor::_clear_pressed() { |
| 216 | if (rebake_timer) { |
| 217 | rebake_timer->stop(); |
| 218 | } |
| 219 | if (node) { |
| 220 | if (node->get_navigation_polygon().is_valid()) { |
| 221 | node->get_navigation_polygon()->clear(); |
| 222 | // Needed to update all the region internals. |
| 223 | node->set_navigation_polygon(node->get_navigation_polygon()); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | button_bake->set_pressed(false); |
| 228 | bake_info->set_text(""); |
| 229 | |
| 230 | if (node) { |
| 231 | node->queue_redraw(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void NavigationRegion2DEditor::_update_polygon_editing_state() { |
| 236 | if (!_get_node()) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | if (node != nullptr && node->get_navigation_polygon().is_valid()) { |
| 241 | bake_hbox->show(); |
| 242 | } else { |
| 243 | bake_hbox->hide(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void NavigationRegion2DEditor::_rebake_timer_timeout() { |
| 248 | if (!node) { |
| 249 | return; |
| 250 | } |
| 251 | Ref<NavigationPolygon> navigation_polygon = node->get_navigation_polygon(); |
| 252 | if (navigation_polygon.is_null()) { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | node->bake_navigation_polygon(true); |
| 257 | node->queue_redraw(); |
| 258 | } |
| 259 | |
| 260 | NavigationRegion2DEditorPlugin::NavigationRegion2DEditorPlugin() : |
| 261 | AbstractPolygon2DEditorPlugin(memnew(NavigationRegion2DEditor), "NavigationRegion2D") { |
| 262 | } |