diff --git a/modules/gridmap/doc_classes/GridMapEditorPlugin.xml b/modules/gridmap/doc_classes/GridMapEditorPlugin.xml new file mode 100644 index 000000000000..a5eb949fd026 --- /dev/null +++ b/modules/gridmap/doc_classes/GridMapEditorPlugin.xml @@ -0,0 +1,62 @@ + + + + Editor for [GridMap] nodes. + + + GridMapEditorPlugin provides access to the [GridMap] editor functionality. + + + + + + + + Returns the [GridMap] currently edited by the grid map editor. + + + + + + + + + Set a rectangular selection from [param begin] to [param end]. + Use [code]false[/code] for the [param active] parameter to disable the selection. + + + + + + Returns the cell coordinate bounds of the current selection. Use [method is_selection_active] to check if there is an active selection. + + + + + + Returns [code]true[/code] if the selection is currently active. + + + + + + Returns an array of [Vector3i]s with the selected cells' coordinates. + + + + + + + Selects the [MeshLibrary] item with the given index in the grid map editor's palette. If a negative index is given, no item will be selected. If a value greater than the last index is given, the last item will be selected. + Note: the indices may not be in the same order as appear in the editor's interface. + + + + + + Returns the index of the selected [MeshLibrary] item in the grid map editor's palette or [code]-1[/code] if no item is selected. + Note: the indices may not be in the same order as appear in the editor's interface. + + + + diff --git a/modules/gridmap/editor/grid_map_editor_plugin.cpp b/modules/gridmap/editor/grid_map_editor_plugin.cpp index 0d522a0562d2..b885105ff99e 100644 --- a/modules/gridmap/editor/grid_map_editor_plugin.cpp +++ b/modules/gridmap/editor/grid_map_editor_plugin.cpp @@ -343,6 +343,40 @@ void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const } } +AABB GridMapEditor::_get_selection() const { + AABB ret; + if (selection.active) { + ret.position = selection.begin; + ret.size = selection.end - selection.begin; + } else { + ret.position.zero(); + ret.size.zero(); + } +} + +bool GridMapEditor::_is_selection_active() const { + return selection.active; +} + +Array GridMapEditor::_get_selected_cells() const { + Array ret; + if (selection.active) { + for (int i = selection.begin.x; i <= selection.end.x; i++) { + for (int j = selection.begin.y; j <= selection.end.y; j++) { + for (int k = selection.begin.z; k <= selection.end.z; k++) { + Vector3i selected = Vector3i(i, j, k); + int itm = node->get_cell_item(selected); + if (itm == GridMap::INVALID_CELL_ITEM) { + continue; + } + ret.append(selected); + } + } + } + } + return ret; +} + bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click) { if (!spatial_editor) { return false; @@ -1713,6 +1747,16 @@ GridMapEditor::~GridMapEditor() { } } +void GridMapEditorPlugin::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_current_grid_map"), &GridMapEditorPlugin::get_current_grid_map); + ClassDB::bind_method(D_METHOD("set_selection", "active", "begin", "end"), &GridMapEditorPlugin::set_selection); + ClassDB::bind_method(D_METHOD("get_selection"), &GridMapEditorPlugin::get_selection); + ClassDB::bind_method(D_METHOD("is_selection_active"), &GridMapEditorPlugin::is_selection_active); + ClassDB::bind_method(D_METHOD("get_selected_cells"), &GridMapEditorPlugin::get_selected_cells); + ClassDB::bind_method(D_METHOD("set_selected_palette_item", "item"), &GridMapEditorPlugin::set_selected_palette_item); + ClassDB::bind_method(D_METHOD("get_selected_palette_item"), &GridMapEditorPlugin::get_selected_palette_item); +} + void GridMapEditorPlugin::edit(Object *p_object) { grid_map_editor->edit(Object::cast_to(p_object)); } @@ -1741,6 +1785,48 @@ void GridMapEditorPlugin::make_visible(bool p_visible) { } } +GridMap *GridMapEditorPlugin::get_current_grid_map() const { + return grid_map_editor->node; +} + +void GridMapEditorPlugin::set_selection(bool p_active, const Vector3i &p_begin, const Vector3i &p_end) { + grid_map_editor->_set_selection(p_active, p_begin, p_end); +} + +AABB GridMapEditorPlugin::get_selection() const { + return grid_map_editor->_get_selection(); +} + +bool GridMapEditorPlugin::is_selection_active() const { + return grid_map_editor->_is_selection_active(); +} + +Array GridMapEditorPlugin::get_selected_cells() const { + return grid_map_editor->_get_selected_cells(); +} + +void GridMapEditorPlugin::set_selected_palette_item(int p_item) const { + if (grid_map_editor->node && grid_map_editor->node->get_mesh_library().is_valid()) { + if (p_item < -1) { + p_item = -1; + } else if (p_item >= grid_map_editor->node->get_mesh_library()->get_item_list().size()) { + p_item = grid_map_editor->node->get_mesh_library()->get_item_list().size() - 1; + } + if (p_item != grid_map_editor->selected_palette) { + grid_map_editor->selected_palette = p_item; + grid_map_editor->update_palette(); + } + } +} + +int GridMapEditorPlugin::get_selected_palette_item() const { + if (grid_map_editor->selected_palette >= 0 && grid_map_editor->node && grid_map_editor->node->get_mesh_library().is_valid()) { + return grid_map_editor->selected_palette; + } else { + return -1; + } +} + GridMapEditorPlugin::GridMapEditorPlugin() { grid_map_editor = memnew(GridMapEditor); grid_map_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL); diff --git a/modules/gridmap/editor/grid_map_editor_plugin.h b/modules/gridmap/editor/grid_map_editor_plugin.h index 2d43a5c83058..abc5c81eab35 100644 --- a/modules/gridmap/editor/grid_map_editor_plugin.h +++ b/modules/gridmap/editor/grid_map_editor_plugin.h @@ -243,6 +243,9 @@ class GridMapEditor : public VBoxContainer { void _update_selection_transform(); void _validate_selection(); void _set_selection(bool p_active, const Vector3 &p_begin = Vector3(), const Vector3 &p_end = Vector3()); + AABB _get_selection() const; + bool _is_selection_active() const; + Array _get_selected_cells() const; void _floor_changed(float p_value); void _floor_mouse_exited(); @@ -272,6 +275,9 @@ class GridMapEditorPlugin : public EditorPlugin { GridMapEditor *grid_map_editor = nullptr; Button *panel_button = nullptr; +protected: + static void _bind_methods(); + public: virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref &p_event) override { return grid_map_editor->forward_spatial_input_event(p_camera, p_event); } virtual String get_name() const override { return "GridMap"; } @@ -280,6 +286,14 @@ class GridMapEditorPlugin : public EditorPlugin { virtual bool handles(Object *p_object) const override; virtual void make_visible(bool p_visible) override; + GridMap *get_current_grid_map() const; + void set_selection(bool p_active, const Vector3i &p_begin = Vector3(), const Vector3i &p_end = Vector3()); + AABB get_selection() const; + bool is_selection_active() const; + Array get_selected_cells() const; + void set_selected_palette_item(int p_item) const; + int get_selected_palette_item() const; + GridMapEditorPlugin(); ~GridMapEditorPlugin(); }; diff --git a/modules/gridmap/register_types.cpp b/modules/gridmap/register_types.cpp index 76d24310e913..e293dc72cee8 100644 --- a/modules/gridmap/register_types.cpp +++ b/modules/gridmap/register_types.cpp @@ -46,6 +46,7 @@ void initialize_gridmap_module(ModuleInitializationLevel p_level) { } #ifdef TOOLS_ENABLED if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) { + GDREGISTER_CLASS(GridMapEditorPlugin); EditorPlugins::add_by_type(); } #endif