initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled

This commit is contained in:
2025-09-16 20:46:46 -04:00
commit 9d30169a8d
13378 changed files with 7050105 additions and 0 deletions

View File

@@ -0,0 +1,198 @@
/**************************************************************************/
/* navigation_link_2d_editor_plugin.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "navigation_link_2d_editor_plugin.h"
#include "editor/editor_node.h"
#include "editor/editor_undo_redo_manager.h"
#include "editor/scene/canvas_item_editor_plugin.h"
#include "editor/settings/editor_settings.h"
#include "scene/main/viewport.h"
void NavigationLink2DEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
get_tree()->connect("node_removed", callable_mp(this, &NavigationLink2DEditor::_node_removed));
} break;
case NOTIFICATION_EXIT_TREE: {
get_tree()->disconnect("node_removed", callable_mp(this, &NavigationLink2DEditor::_node_removed));
} break;
}
}
void NavigationLink2DEditor::_node_removed(Node *p_node) {
if (p_node == node) {
node = nullptr;
}
}
bool NavigationLink2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
if (!node || !node->is_visible_in_tree()) {
return false;
}
Viewport *vp = node->get_viewport();
if (vp && !vp->is_visible_subviewport()) {
return false;
}
real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
if (mb->is_pressed()) {
// Start position
if (xform.xform(node->get_start_position()).distance_to(mb->get_position()) < grab_threshold) {
start_grabbed = true;
original_start_position = node->get_start_position();
return true;
} else {
start_grabbed = false;
}
// End position
if (xform.xform(node->get_end_position()).distance_to(mb->get_position()) < grab_threshold) {
end_grabbed = true;
original_end_position = node->get_end_position();
return true;
} else {
end_grabbed = false;
}
} else {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
if (start_grabbed) {
undo_redo->create_action(TTR("Set start_position"));
undo_redo->add_do_method(node, "set_start_position", node->get_start_position());
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
undo_redo->add_undo_method(node, "set_start_position", original_start_position);
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
undo_redo->commit_action();
start_grabbed = false;
return true;
}
if (end_grabbed) {
undo_redo->create_action(TTR("Set end_position"));
undo_redo->add_do_method(node, "set_end_position", node->get_end_position());
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
undo_redo->add_undo_method(node, "set_end_position", original_end_position);
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
undo_redo->commit_action();
end_grabbed = false;
return true;
}
}
}
Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid()) {
Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
point = node->get_screen_transform().affine_inverse().xform(point);
if (start_grabbed) {
node->set_start_position(point);
canvas_item_editor->update_viewport();
return true;
}
if (end_grabbed) {
node->set_end_position(point);
canvas_item_editor->update_viewport();
return true;
}
}
return false;
}
void NavigationLink2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
if (!node || !node->is_visible_in_tree()) {
return;
}
Viewport *vp = node->get_viewport();
if (vp && !vp->is_visible_subviewport()) {
return;
}
Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
Vector2 global_start_position = gt.xform(node->get_start_position());
Vector2 global_end_position = gt.xform(node->get_end_position());
// Only drawing the handles here, since the debug rendering will fill in the rest.
const Ref<Texture2D> handle = get_editor_theme_icon(SNAME("EditorHandle"));
p_overlay->draw_texture(handle, global_start_position - handle->get_size() / 2);
p_overlay->draw_texture(handle, global_end_position - handle->get_size() / 2);
}
void NavigationLink2DEditor::edit(NavigationLink2D *p_node) {
if (!canvas_item_editor) {
canvas_item_editor = CanvasItemEditor::get_singleton();
}
if (p_node) {
node = p_node;
} else {
node = nullptr;
}
canvas_item_editor->update_viewport();
}
///////////////////////
void NavigationLink2DEditorPlugin::edit(Object *p_object) {
editor->edit(Object::cast_to<NavigationLink2D>(p_object));
}
bool NavigationLink2DEditorPlugin::handles(Object *p_object) const {
return Object::cast_to<NavigationLink2D>(p_object) != nullptr;
}
void NavigationLink2DEditorPlugin::make_visible(bool p_visible) {
if (!p_visible) {
edit(nullptr);
}
}
NavigationLink2DEditorPlugin::NavigationLink2DEditorPlugin() {
editor = memnew(NavigationLink2DEditor);
EditorNode::get_singleton()->get_gui_base()->add_child(editor);
}

View File

@@ -0,0 +1,76 @@
/**************************************************************************/
/* navigation_link_2d_editor_plugin.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "editor/plugins/editor_plugin.h"
#include "scene/2d/navigation/navigation_link_2d.h"
class CanvasItemEditor;
class NavigationLink2DEditor : public Control {
GDCLASS(NavigationLink2DEditor, Control);
CanvasItemEditor *canvas_item_editor = nullptr;
NavigationLink2D *node = nullptr;
bool start_grabbed = false;
Vector2 original_start_position;
bool end_grabbed = false;
Vector2 original_end_position;
protected:
void _notification(int p_what);
void _node_removed(Node *p_node);
public:
bool forward_canvas_gui_input(const Ref<InputEvent> &p_event);
void forward_canvas_draw_over_viewport(Control *p_overlay);
void edit(NavigationLink2D *p_node);
};
class NavigationLink2DEditorPlugin : public EditorPlugin {
GDCLASS(NavigationLink2DEditorPlugin, EditorPlugin);
NavigationLink2DEditor *editor = nullptr;
public:
virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return editor->forward_canvas_gui_input(p_event); }
virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { editor->forward_canvas_draw_over_viewport(p_overlay); }
virtual String get_plugin_name() const override { return "NavigationLink2D"; }
bool has_main_screen() const override { return false; }
virtual void edit(Object *p_object) override;
virtual bool handles(Object *p_object) const override;
virtual void make_visible(bool p_visible) override;
NavigationLink2DEditorPlugin();
};

View File

@@ -0,0 +1,71 @@
/**************************************************************************/
/* navigation_obstacle_2d_editor_plugin.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "navigation_obstacle_2d_editor_plugin.h"
#include "editor/editor_undo_redo_manager.h"
Node2D *NavigationObstacle2DEditor::_get_node() const {
return node;
}
void NavigationObstacle2DEditor::_set_node(Node *p_polygon) {
node = Object::cast_to<NavigationObstacle2D>(p_polygon);
}
Variant NavigationObstacle2DEditor::_get_polygon(int p_idx) const {
return node->get_vertices();
}
void NavigationObstacle2DEditor::_set_polygon(int p_idx, const Variant &p_polygon) const {
node->set_vertices(p_polygon);
}
void NavigationObstacle2DEditor::_action_add_polygon(const Variant &p_polygon) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->add_do_method(node, "set_vertices", p_polygon);
undo_redo->add_undo_method(node, "set_vertices", node->get_vertices());
}
void NavigationObstacle2DEditor::_action_remove_polygon(int p_idx) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->add_do_method(node, "set_vertices", Variant(Vector<Vector2>()));
undo_redo->add_undo_method(node, "set_vertices", node->get_vertices());
}
void NavigationObstacle2DEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->add_do_method(node, "set_vertices", p_polygon);
undo_redo->add_undo_method(node, "set_vertices", node->get_vertices());
}
NavigationObstacle2DEditorPlugin::NavigationObstacle2DEditorPlugin() :
AbstractPolygon2DEditorPlugin(memnew(NavigationObstacle2DEditor), "NavigationObstacle2D") {
}

View File

@@ -0,0 +1,58 @@
/**************************************************************************/
/* navigation_obstacle_2d_editor_plugin.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "editor/scene/2d/abstract_polygon_2d_editor.h"
#include "scene/2d/navigation/navigation_obstacle_2d.h"
class NavigationObstacle2DEditor : public AbstractPolygon2DEditor {
GDCLASS(NavigationObstacle2DEditor, AbstractPolygon2DEditor);
NavigationObstacle2D *node = nullptr;
protected:
virtual Node2D *_get_node() const override;
virtual void _set_node(Node *p_polygon) override;
virtual Variant _get_polygon(int p_idx) const override;
virtual void _set_polygon(int p_idx, const Variant &p_polygon) const override;
virtual void _action_add_polygon(const Variant &p_polygon) override;
virtual void _action_remove_polygon(int p_idx) override;
virtual void _action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) override;
};
class NavigationObstacle2DEditorPlugin : public AbstractPolygon2DEditorPlugin {
GDCLASS(NavigationObstacle2DEditorPlugin, AbstractPolygon2DEditorPlugin);
public:
NavigationObstacle2DEditorPlugin();
};

View File

@@ -0,0 +1,261 @@
/**************************************************************************/
/* navigation_region_2d_editor_plugin.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "navigation_region_2d_editor_plugin.h"
#include "editor/editor_node.h"
#include "editor/editor_undo_redo_manager.h"
#include "editor/settings/editor_settings.h"
#include "scene/2d/navigation/navigation_region_2d.h"
#include "scene/gui/dialogs.h"
Ref<NavigationPolygon> NavigationRegion2DEditor::_ensure_navpoly() const {
Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
if (navpoly.is_null()) {
navpoly.instantiate();
node->set_navigation_polygon(navpoly);
}
return navpoly;
}
Node2D *NavigationRegion2DEditor::_get_node() const {
return node;
}
void NavigationRegion2DEditor::_set_node(Node *p_polygon) {
node = Object::cast_to<NavigationRegion2D>(p_polygon);
if (node) {
Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
if (navpoly.is_valid() && navpoly->get_outline_count() > 0 && navpoly->get_polygon_count() == 0) {
// We have outlines drawn / added by the user but no polygons were created for this navmesh yet so let's bake once immediately.
_rebake_timer_timeout();
}
}
}
int NavigationRegion2DEditor::_get_polygon_count() const {
Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
if (navpoly.is_valid()) {
return navpoly->get_outline_count();
} else {
return 0;
}
}
Variant NavigationRegion2DEditor::_get_polygon(int p_idx) const {
Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
if (navpoly.is_valid()) {
return navpoly->get_outline(p_idx);
} else {
return Variant(Vector<Vector2>());
}
}
void NavigationRegion2DEditor::_set_polygon(int p_idx, const Variant &p_polygon) const {
Ref<NavigationPolygon> navpoly = _ensure_navpoly();
navpoly->set_outline(p_idx, p_polygon);
if (rebake_timer && _rebake_timer_delay >= 0.0) {
rebake_timer->start();
}
}
void NavigationRegion2DEditor::_action_add_polygon(const Variant &p_polygon) {
Ref<NavigationPolygon> navpoly = _ensure_navpoly();
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->add_do_method(navpoly.ptr(), "add_outline", p_polygon);
undo_redo->add_undo_method(navpoly.ptr(), "remove_outline", navpoly->get_outline_count());
if (rebake_timer && _rebake_timer_delay >= 0.0) {
rebake_timer->start();
}
}
void NavigationRegion2DEditor::_action_remove_polygon(int p_idx) {
Ref<NavigationPolygon> navpoly = _ensure_navpoly();
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->add_do_method(navpoly.ptr(), "remove_outline", p_idx);
undo_redo->add_undo_method(navpoly.ptr(), "add_outline_at_index", navpoly->get_outline(p_idx), p_idx);
if (rebake_timer && _rebake_timer_delay >= 0.0) {
rebake_timer->start();
}
}
void NavigationRegion2DEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) {
Ref<NavigationPolygon> navpoly = _ensure_navpoly();
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->add_do_method(navpoly.ptr(), "set_outline", p_idx, p_polygon);
undo_redo->add_undo_method(navpoly.ptr(), "set_outline", p_idx, p_previous);
if (rebake_timer && _rebake_timer_delay >= 0.0) {
rebake_timer->start();
}
}
bool NavigationRegion2DEditor::_has_resource() const {
return node && node->get_navigation_polygon().is_valid();
}
void NavigationRegion2DEditor::_create_resource() {
if (!node) {
return;
}
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Create Navigation Polygon"));
undo_redo->add_do_method(node, "set_navigation_polygon", Ref<NavigationPolygon>(memnew(NavigationPolygon)));
undo_redo->add_undo_method(node, "set_navigation_polygon", Variant(Ref<RefCounted>()));
undo_redo->commit_action();
_menu_option(MODE_CREATE);
}
NavigationRegion2DEditor::NavigationRegion2DEditor() {
bake_hbox = memnew(HBoxContainer);
add_child(bake_hbox);
button_bake = memnew(Button);
button_bake->set_flat(true);
bake_hbox->add_child(button_bake);
button_bake->set_toggle_mode(true);
button_bake->set_text(TTR("Bake NavigationPolygon"));
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."));
button_bake->connect(SceneStringName(pressed), callable_mp(this, &NavigationRegion2DEditor::_bake_pressed));
button_reset = memnew(Button);
button_reset->set_flat(true);
bake_hbox->add_child(button_reset);
button_reset->set_text(TTR("Clear NavigationPolygon"));
button_reset->set_tooltip_text(TTR("Clears the internal NavigationPolygon outlines, vertices and polygons."));
button_reset->connect(SceneStringName(pressed), callable_mp(this, &NavigationRegion2DEditor::_clear_pressed));
bake_info = memnew(Label);
bake_info->set_focus_mode(FOCUS_ACCESSIBILITY);
bake_hbox->add_child(bake_info);
rebake_timer = memnew(Timer);
add_child(rebake_timer);
rebake_timer->set_one_shot(true);
_rebake_timer_delay = EDITOR_GET("editors/polygon_editor/auto_bake_delay");
if (_rebake_timer_delay >= 0.0) {
rebake_timer->set_wait_time(_rebake_timer_delay);
}
rebake_timer->connect("timeout", callable_mp(this, &NavigationRegion2DEditor::_rebake_timer_timeout));
err_dialog = memnew(AcceptDialog);
add_child(err_dialog);
node = nullptr;
}
void NavigationRegion2DEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
button_bake->set_button_icon(get_editor_theme_icon(SNAME("Bake")));
button_reset->set_button_icon(get_editor_theme_icon(SNAME("Reload")));
} break;
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
if (rebake_timer) {
_rebake_timer_delay = EDITOR_GET("editors/polygon_editor/auto_bake_delay");
if (_rebake_timer_delay >= 0.0) {
rebake_timer->set_wait_time(_rebake_timer_delay);
}
}
} break;
}
}
void NavigationRegion2DEditor::_bake_pressed() {
if (rebake_timer) {
rebake_timer->stop();
}
button_bake->set_pressed(false);
ERR_FAIL_NULL(node);
Ref<NavigationPolygon> navigation_polygon = node->get_navigation_polygon();
if (navigation_polygon.is_null()) {
err_dialog->set_text(TTR("A NavigationPolygon resource must be set or created for this node to work."));
err_dialog->popup_centered();
return;
}
node->bake_navigation_polygon(true);
node->queue_redraw();
}
void NavigationRegion2DEditor::_clear_pressed() {
if (rebake_timer) {
rebake_timer->stop();
}
if (node) {
if (node->get_navigation_polygon().is_valid()) {
node->get_navigation_polygon()->clear();
// Needed to update all the region internals.
node->set_navigation_polygon(node->get_navigation_polygon());
}
}
button_bake->set_pressed(false);
bake_info->set_text("");
if (node) {
node->queue_redraw();
}
}
void NavigationRegion2DEditor::_update_polygon_editing_state() {
if (!_get_node()) {
return;
}
if (node != nullptr && node->get_navigation_polygon().is_valid()) {
bake_hbox->show();
} else {
bake_hbox->hide();
}
}
void NavigationRegion2DEditor::_rebake_timer_timeout() {
if (!node) {
return;
}
Ref<NavigationPolygon> navigation_polygon = node->get_navigation_polygon();
if (navigation_polygon.is_null()) {
return;
}
node->bake_navigation_polygon(true);
node->queue_redraw();
}
NavigationRegion2DEditorPlugin::NavigationRegion2DEditorPlugin() :
AbstractPolygon2DEditorPlugin(memnew(NavigationRegion2DEditor), "NavigationRegion2D") {
}

View File

@@ -0,0 +1,94 @@
/**************************************************************************/
/* navigation_region_2d_editor_plugin.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "editor/plugins/editor_plugin.h"
#include "editor/scene/2d/abstract_polygon_2d_editor.h"
class AcceptDialog;
class HBoxContainer;
class NavigationPolygon;
class NavigationRegion2D;
class NavigationRegion2DEditor : public AbstractPolygon2DEditor {
friend class NavigationRegion2DEditorPlugin;
GDCLASS(NavigationRegion2DEditor, AbstractPolygon2DEditor);
NavigationRegion2D *node = nullptr;
Ref<NavigationPolygon> _ensure_navpoly() const;
AcceptDialog *err_dialog = nullptr;
HBoxContainer *bake_hbox = nullptr;
Button *button_bake = nullptr;
Button *button_reset = nullptr;
Label *bake_info = nullptr;
Timer *rebake_timer = nullptr;
float _rebake_timer_delay = 1.5;
void _rebake_timer_timeout();
void _bake_pressed();
void _clear_pressed();
void _update_polygon_editing_state();
protected:
void _notification(int p_what);
virtual Node2D *_get_node() const override;
virtual void _set_node(Node *p_polygon) override;
virtual int _get_polygon_count() const override;
virtual Variant _get_polygon(int p_idx) const override;
virtual void _set_polygon(int p_idx, const Variant &p_polygon) const override;
virtual void _action_add_polygon(const Variant &p_polygon) override;
virtual void _action_remove_polygon(int p_idx) override;
virtual void _action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) override;
virtual bool _has_resource() const override;
virtual void _create_resource() override;
public:
NavigationRegion2DEditor();
};
class NavigationRegion2DEditorPlugin : public AbstractPolygon2DEditorPlugin {
GDCLASS(NavigationRegion2DEditorPlugin, AbstractPolygon2DEditorPlugin);
NavigationRegion2DEditor *navigation_polygon_editor = nullptr;
public:
NavigationRegion2DEditorPlugin();
};