Skip to content

Commit

Permalink
Expose GridMapEditorPlugin to scripts and add methods to manipulate t…
Browse files Browse the repository at this point in the history
…he selection and selected palette item
  • Loading branch information
badsectoracula committed Nov 24, 2024
1 parent 0c45ace commit 6589c81
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
62 changes: 62 additions & 0 deletions modules/gridmap/doc_classes/GridMapEditorPlugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GridMapEditorPlugin" inherits="EditorPlugin" keywords="tilemap" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Editor for [GridMap] nodes.
</brief_description>
<description>
GridMapEditorPlugin provides access to the [GridMap] editor functionality.
</description>
<tutorials>
</tutorials>
<methods>
<method name="get_current_grid_map">
<return type="GridMap" />
<description>
Returns the [GridMap] currently edited by the grid map editor.
</description>
</method>
<method name="set_selection">
<return type="void" />
<param index="0" name="active" type="bool" />
<param index="1" name="begin" type="Vector3i" />
<param index="2" name="end" type="Vector3i" />
<description>
Set a rectangular selection from [param begin] to [param end].
Use [code]false[/code] for the [param active] parameter to disable the selection.
</description>
</method>
<method name="get_selection">
<return type="AABB" />
<description>
Returns the cell coordinate bounds of the current selection. Use [method is_selection_active] to check if there is an active selection.
</description>
</method>
<method name="is_selection_active">
<return type="bool" />
<description>
Returns [code]true[/code] if the selection is currently active.
</description>
</method>
<method name="get_selected_cells">
<return type="Array" />
<description>
Returns an array of [Vector3i]s with the selected cells' coordinates.
</description>
</method>
<method name="set_selected_palette_item">
<return type="int" />
<param index="0" name="item" type="int" />
<description>
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.
</description>
</method>
<method name="get_selected_palette_item">
<return type="int" />
<description>
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.
</description>
</method>
</methods>
</class>
86 changes: 86 additions & 0 deletions modules/gridmap/editor/grid_map_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<GridMap>(p_object));
}
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions modules/gridmap/editor/grid_map_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<InputEvent> &p_event) override { return grid_map_editor->forward_spatial_input_event(p_camera, p_event); }
virtual String get_name() const override { return "GridMap"; }
Expand All @@ -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();
};
Expand Down
1 change: 1 addition & 0 deletions modules/gridmap/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GridMapEditorPlugin>();
}
#endif
Expand Down

0 comments on commit 6589c81

Please sign in to comment.