navigation_polygon_editor_plugin.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "navigation_polygon_editor_plugin.h"
  31. Ref<NavigationPolygon> NavigationPolygonEditor::_ensure_navpoly() const {
  32. Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
  33. if (!navpoly.is_valid()) {
  34. navpoly = Ref<NavigationPolygon>(memnew(NavigationPolygon));
  35. node->set_navigation_polygon(navpoly);
  36. }
  37. return navpoly;
  38. }
  39. Node2D *NavigationPolygonEditor::_get_node() const {
  40. return node;
  41. }
  42. void NavigationPolygonEditor::_set_node(Node *p_polygon) {
  43. node = Object::cast_to<NavigationPolygonInstance>(p_polygon);
  44. }
  45. int NavigationPolygonEditor::_get_polygon_count() const {
  46. Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
  47. if (navpoly.is_valid()) {
  48. return navpoly->get_outline_count();
  49. } else {
  50. return 0;
  51. }
  52. }
  53. Variant NavigationPolygonEditor::_get_polygon(int p_idx) const {
  54. Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
  55. if (navpoly.is_valid()) {
  56. return navpoly->get_outline(p_idx);
  57. } else {
  58. return Variant(Vector<Vector2>());
  59. }
  60. }
  61. void NavigationPolygonEditor::_set_polygon(int p_idx, const Variant &p_polygon) const {
  62. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  63. navpoly->set_outline(p_idx, p_polygon);
  64. navpoly->make_polygons_from_outlines();
  65. }
  66. void NavigationPolygonEditor::_action_add_polygon(const Variant &p_polygon) {
  67. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  68. undo_redo->add_do_method(navpoly.ptr(), "add_outline", p_polygon);
  69. undo_redo->add_undo_method(navpoly.ptr(), "remove_outline", navpoly->get_outline_count());
  70. undo_redo->add_do_method(navpoly.ptr(), "make_polygons_from_outlines");
  71. undo_redo->add_undo_method(navpoly.ptr(), "make_polygons_from_outlines");
  72. }
  73. void NavigationPolygonEditor::_action_remove_polygon(int p_idx) {
  74. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  75. undo_redo->add_do_method(navpoly.ptr(), "remove_outline", p_idx);
  76. undo_redo->add_undo_method(navpoly.ptr(), "add_outline_at_index", navpoly->get_outline(p_idx), p_idx);
  77. undo_redo->add_do_method(navpoly.ptr(), "make_polygons_from_outlines");
  78. undo_redo->add_undo_method(navpoly.ptr(), "make_polygons_from_outlines");
  79. }
  80. void NavigationPolygonEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) {
  81. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  82. undo_redo->add_do_method(navpoly.ptr(), "set_outline", p_idx, p_polygon);
  83. undo_redo->add_undo_method(navpoly.ptr(), "set_outline", p_idx, p_previous);
  84. undo_redo->add_do_method(navpoly.ptr(), "make_polygons_from_outlines");
  85. undo_redo->add_undo_method(navpoly.ptr(), "make_polygons_from_outlines");
  86. }
  87. bool NavigationPolygonEditor::_has_resource() const {
  88. return node && node->get_navigation_polygon().is_valid();
  89. }
  90. void NavigationPolygonEditor::_create_resource() {
  91. if (!node) {
  92. return;
  93. }
  94. undo_redo->create_action(TTR("Create Navigation Polygon"));
  95. undo_redo->add_do_method(node, "set_navigation_polygon", Ref<NavigationPolygon>(memnew(NavigationPolygon)));
  96. undo_redo->add_undo_method(node, "set_navigation_polygon", Variant(REF()));
  97. undo_redo->commit_action();
  98. _menu_option(MODE_CREATE);
  99. }
  100. NavigationPolygonEditor::NavigationPolygonEditor(EditorNode *p_editor) :
  101. AbstractPolygon2DEditor(p_editor) {
  102. node = nullptr;
  103. }
  104. NavigationPolygonEditorPlugin::NavigationPolygonEditorPlugin(EditorNode *p_node) :
  105. AbstractPolygon2DEditorPlugin(p_node, memnew(NavigationPolygonEditor(p_node)), "NavigationPolygonInstance") {
  106. }