Skip to content

Commit f477c51

Browse files
simpuidtygyh
authored andcommitted
Added functionalities to fix invalid cells and clear all cells in GridMap
Changes made: * Added condition to `GridMap::set_mesh_library` to update only if new library is different from old one * Added `GridMap::fix_invalid_cells` taking inspiration from `TileMap::fix_invalide_tiles` * Added menu entry to clear cells in `GridMapEditor` * Added menu entry to fix tiles in `GridMapEditor`
1 parent 1753893 commit f477c51

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

modules/gridmap/editor/grid_map_editor_plugin.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,20 @@ void GridMapEditor::_menu_option(int p_option) {
238238
_fill_selection();
239239

240240
} break;
241+
case MENU_OPTION_CLEAR_ALL: {
242+
undo_redo->create_action(TTR("Clear All"));
243+
undo_redo->add_undo_method(node, "set", "data", node->get("data"));
244+
node->clear();
245+
undo_redo->add_do_method(node, "set", "data", node->get("data"));
246+
undo_redo->commit_action();
247+
} break;
248+
case MENU_OPTION_FIX_INVALID_CELLS: {
249+
undo_redo->create_action(TTR("Fix Invalid Cells"));
250+
undo_redo->add_undo_method(node, "set", "data", node->get("data"));
251+
node->fix_invalid_cells();
252+
undo_redo->add_do_method(node, "set", "data", node->get("data"));
253+
undo_redo->commit_action();
254+
} break;
241255
case MENU_OPTION_GRIDMAP_SETTINGS: {
242256
settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE);
243257
} break;
@@ -1339,6 +1353,8 @@ GridMapEditor::GridMapEditor() {
13391353
ED_SHORTCUT("grid_map/edit_z_axis", TTRC("Edit Z Axis"), KeyModifierMask::SHIFT + Key::C, true);
13401354
ED_SHORTCUT("grid_map/keep_selected", TTRC("Keep Selection"));
13411355
ED_SHORTCUT("grid_map/clear_rotation", TTRC("Clear Rotation"));
1356+
ED_SHORTCUT("grid_map/clear_all", TTRC("Clear All"));
1357+
ED_SHORTCUT("grid_map/fix_invalid_cells", TTRC("Fix Invalid Cells"));
13421358

13431359
options = memnew(MenuButton);
13441360
options->set_theme_type_variation(SceneStringName(FlatButton));
@@ -1353,6 +1369,10 @@ GridMapEditor::GridMapEditor() {
13531369
options->get_popup()->add_check_shortcut(ED_GET_SHORTCUT("grid_map/keep_selected"), MENU_OPTION_PASTE_SELECTS);
13541370
options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS), true);
13551371
options->get_popup()->add_separator();
1372+
options->get_popup()->add_shortcut(ED_SHORTCUT("grid_map/clear_all"), MENU_OPTION_CLEAR_ALL);
1373+
options->get_popup()->add_separator();
1374+
options->get_popup()->add_item(ED_SHORTCUT("grid_map/fix_invalid_cells"), MENU_OPTION_FIX_INVALID_CELLS);
1375+
options->get_popup()->add_separator();
13561376
options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS);
13571377

13581378
settings_dialog = memnew(ConfirmationDialog);

modules/gridmap/editor/grid_map_editor_plugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ class GridMapEditor : public VBoxContainer {
202202
MENU_OPTION_SELECTION_CUT,
203203
MENU_OPTION_SELECTION_CLEAR,
204204
MENU_OPTION_SELECTION_FILL,
205+
MENU_OPTION_CLEAR_ALL,
206+
MENU_OPTION_FIX_INVALID_CELLS,
205207
MENU_OPTION_GRIDMAP_SETTINGS
206208

207209
};

modules/gridmap/grid_map.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,22 @@ RID GridMap::get_navigation_map() const {
267267
}
268268

269269
void GridMap::set_mesh_library(const Ref<MeshLibrary> &p_mesh_library) {
270-
if (mesh_library.is_valid()) {
271-
mesh_library->disconnect_changed(callable_mp(this, &GridMap::_recreate_octant_data));
270+
if (mesh_library != p_mesh_library) {
271+
if (mesh_library.is_valid()) {
272+
mesh_library->disconnect_changed(callable_mp(this, &GridMap::_recreate_octant_data));
273+
}
274+
mesh_library = p_mesh_library;
275+
if (mesh_library.is_valid()) {
276+
mesh_library->connect_changed(callable_mp(this, &GridMap::_recreate_octant_data));
277+
}
278+
}
279+
280+
if (!mesh_library.is_null()) {
281+
mesh_library->unregister_owner(this);
272282
}
273283
mesh_library = p_mesh_library;
274-
if (mesh_library.is_valid()) {
275-
mesh_library->connect_changed(callable_mp(this, &GridMap::_recreate_octant_data));
284+
if (!mesh_library.is_null()) {
285+
mesh_library->register_owner(this);
276286
}
277287

278288
_recreate_octant_data();
@@ -1130,6 +1140,8 @@ void GridMap::_bind_methods() {
11301140
ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
11311141
ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
11321142

1143+
ClassDB::bind_method(D_METHOD("fix_invalid_cells"), &GridMap::fix_invalid_cells);
1144+
11331145
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
11341146
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material");
11351147
ADD_GROUP("Cell", "cell_");
@@ -1342,6 +1354,16 @@ RID GridMap::get_bake_mesh_instance(int p_idx) {
13421354
return baked_meshes[p_idx].instance;
13431355
}
13441356

1357+
void GridMap::fix_invalid_cells() {
1358+
ERR_FAIL_COND_MSG(mesh_library.is_null(), "Cannot fix invalid cells if MeshLibrary is not open.");
1359+
1360+
for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) {
1361+
if (!mesh_library.is_valid() || !mesh_library->has_item(E->value().item)) {
1362+
set_cell_item(E->key().x, E->key().y, E->key().z, INVALID_CELL_ITEM);
1363+
}
1364+
}
1365+
}
1366+
13451367
GridMap::GridMap() {
13461368
set_notify_transform(true);
13471369
#ifdef DEBUG_ENABLED

modules/gridmap/grid_map.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ class GridMap : public Node3D {
302302
Array get_bake_meshes();
303303
RID get_bake_mesh_instance(int p_idx);
304304

305+
void fix_invalid_cells();
306+
305307
private:
306308
static Callable _navmesh_source_geometry_parsing_callback;
307309
static RID _navmesh_source_geometry_parser;

0 commit comments

Comments
 (0)