| 1 | /**************************************************************************/ |
| 2 | /* navigation_polygon_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_polygon_editor_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_node.h" |
| 34 | #include "editor/editor_undo_redo_manager.h" |
| 35 | |
| 36 | Ref<NavigationPolygon> NavigationPolygonEditor::_ensure_navpoly() const { |
| 37 | Ref<NavigationPolygon> navpoly = node->get_navigation_polygon(); |
| 38 | if (!navpoly.is_valid()) { |
| 39 | navpoly = Ref<NavigationPolygon>(memnew(NavigationPolygon)); |
| 40 | node->set_navigation_polygon(navpoly); |
| 41 | } |
| 42 | return navpoly; |
| 43 | } |
| 44 | |
| 45 | Node2D *NavigationPolygonEditor::_get_node() const { |
| 46 | return node; |
| 47 | } |
| 48 | |
| 49 | void NavigationPolygonEditor::_set_node(Node *p_polygon) { |
| 50 | node = Object::cast_to<NavigationRegion2D>(p_polygon); |
| 51 | } |
| 52 | |
| 53 | int NavigationPolygonEditor::_get_polygon_count() const { |
| 54 | Ref<NavigationPolygon> navpoly = node->get_navigation_polygon(); |
| 55 | if (navpoly.is_valid()) { |
| 56 | return navpoly->get_outline_count(); |
| 57 | } else { |
| 58 | return 0; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | Variant NavigationPolygonEditor::_get_polygon(int p_idx) const { |
| 63 | Ref<NavigationPolygon> navpoly = node->get_navigation_polygon(); |
| 64 | if (navpoly.is_valid()) { |
| 65 | return navpoly->get_outline(p_idx); |
| 66 | } else { |
| 67 | return Variant(Vector<Vector2>()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void NavigationPolygonEditor::_set_polygon(int p_idx, const Variant &p_polygon) const { |
| 72 | Ref<NavigationPolygon> navpoly = _ensure_navpoly(); |
| 73 | navpoly->set_outline(p_idx, p_polygon); |
| 74 | navpoly->make_polygons_from_outlines(); |
| 75 | } |
| 76 | |
| 77 | void NavigationPolygonEditor::_action_add_polygon(const Variant &p_polygon) { |
| 78 | Ref<NavigationPolygon> navpoly = _ensure_navpoly(); |
| 79 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 80 | undo_redo->add_do_method(navpoly.ptr(), "add_outline" , p_polygon); |
| 81 | undo_redo->add_undo_method(navpoly.ptr(), "remove_outline" , navpoly->get_outline_count()); |
| 82 | undo_redo->add_do_method(navpoly.ptr(), "make_polygons_from_outlines" ); |
| 83 | undo_redo->add_undo_method(navpoly.ptr(), "make_polygons_from_outlines" ); |
| 84 | } |
| 85 | |
| 86 | void NavigationPolygonEditor::_action_remove_polygon(int p_idx) { |
| 87 | Ref<NavigationPolygon> navpoly = _ensure_navpoly(); |
| 88 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 89 | undo_redo->add_do_method(navpoly.ptr(), "remove_outline" , p_idx); |
| 90 | undo_redo->add_undo_method(navpoly.ptr(), "add_outline_at_index" , navpoly->get_outline(p_idx), p_idx); |
| 91 | undo_redo->add_do_method(navpoly.ptr(), "make_polygons_from_outlines" ); |
| 92 | undo_redo->add_undo_method(navpoly.ptr(), "make_polygons_from_outlines" ); |
| 93 | } |
| 94 | |
| 95 | void NavigationPolygonEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) { |
| 96 | Ref<NavigationPolygon> navpoly = _ensure_navpoly(); |
| 97 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 98 | undo_redo->add_do_method(navpoly.ptr(), "set_outline" , p_idx, p_polygon); |
| 99 | undo_redo->add_undo_method(navpoly.ptr(), "set_outline" , p_idx, p_previous); |
| 100 | undo_redo->add_do_method(navpoly.ptr(), "make_polygons_from_outlines" ); |
| 101 | undo_redo->add_undo_method(navpoly.ptr(), "make_polygons_from_outlines" ); |
| 102 | } |
| 103 | |
| 104 | bool NavigationPolygonEditor::_has_resource() const { |
| 105 | return node && node->get_navigation_polygon().is_valid(); |
| 106 | } |
| 107 | |
| 108 | void NavigationPolygonEditor::_create_resource() { |
| 109 | if (!node) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 114 | undo_redo->create_action(TTR("Create Navigation Polygon" )); |
| 115 | undo_redo->add_do_method(node, "set_navigation_polygon" , Ref<NavigationPolygon>(memnew(NavigationPolygon))); |
| 116 | undo_redo->add_undo_method(node, "set_navigation_polygon" , Variant(Ref<RefCounted>())); |
| 117 | undo_redo->commit_action(); |
| 118 | |
| 119 | _menu_option(MODE_CREATE); |
| 120 | } |
| 121 | |
| 122 | NavigationPolygonEditor::NavigationPolygonEditor() {} |
| 123 | |
| 124 | NavigationPolygonEditorPlugin::NavigationPolygonEditorPlugin() : |
| 125 | AbstractPolygon2DEditorPlugin(memnew(NavigationPolygonEditor), "NavigationRegion2D" ) { |
| 126 | } |
| 127 | |