navigation_polygon_editor_plugin.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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. Variant NavigationPolygonEditor::_get_polygon(int p_idx) const {
  53. Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
  54. if (navpoly.is_valid())
  55. return navpoly->get_outline(p_idx);
  56. else
  57. return Variant(Vector<Vector2>());
  58. }
  59. void NavigationPolygonEditor::_set_polygon(int p_idx, const Variant &p_polygon) const {
  60. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  61. navpoly->set_outline(p_idx, p_polygon);
  62. navpoly->make_polygons_from_outlines();
  63. }
  64. void NavigationPolygonEditor::_action_add_polygon(const Variant &p_polygon) {
  65. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  66. undo_redo->add_do_method(navpoly.ptr(), "add_outline", p_polygon);
  67. undo_redo->add_undo_method(navpoly.ptr(), "remove_outline", navpoly->get_outline_count());
  68. undo_redo->add_do_method(navpoly.ptr(), "make_polygons_from_outlines");
  69. undo_redo->add_undo_method(navpoly.ptr(), "make_polygons_from_outlines");
  70. }
  71. void NavigationPolygonEditor::_action_remove_polygon(int p_idx) {
  72. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  73. undo_redo->add_do_method(navpoly.ptr(), "remove_outline", p_idx);
  74. undo_redo->add_undo_method(navpoly.ptr(), "add_outline_at_index", navpoly->get_outline(p_idx), p_idx);
  75. undo_redo->add_do_method(navpoly.ptr(), "make_polygons_from_outlines");
  76. undo_redo->add_undo_method(navpoly.ptr(), "make_polygons_from_outlines");
  77. }
  78. void NavigationPolygonEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) {
  79. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  80. undo_redo->add_do_method(navpoly.ptr(), "set_outline", p_idx, p_polygon);
  81. undo_redo->add_undo_method(navpoly.ptr(), "set_outline", p_idx, p_previous);
  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. bool NavigationPolygonEditor::_has_resource() const {
  86. return node && node->get_navigation_polygon().is_valid();
  87. }
  88. void NavigationPolygonEditor::_create_resource() {
  89. if (!node)
  90. return;
  91. undo_redo->create_action(TTR("Create Navigation Polygon"));
  92. undo_redo->add_do_method(node, "set_navigation_polygon", Ref<NavigationPolygon>(memnew(NavigationPolygon)));
  93. undo_redo->add_undo_method(node, "set_navigation_polygon", Variant(REF()));
  94. undo_redo->commit_action();
  95. _menu_option(MODE_CREATE);
  96. }
  97. NavigationPolygonEditor::NavigationPolygonEditor(EditorNode *p_editor)
  98. : AbstractPolygon2DEditor(p_editor) {
  99. }
  100. NavigationPolygonEditorPlugin::NavigationPolygonEditorPlugin(EditorNode *p_node)
  101. : AbstractPolygon2DEditorPlugin(p_node, memnew(NavigationPolygonEditor(p_node)), "NavigationPolygonInstance") {
  102. }