Skip to content

Commit 7567bb4

Browse files
committed
Added functionalities to fix invalid cells and clear all cells in GridMap
1 parent 445a518 commit 7567bb4

File tree

5 files changed

+61
-9
lines changed

5 files changed

+61
-9
lines changed

modules/gridmap/doc_classes/GridMap.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
Clears all baked meshes. See [method make_baked_meshes].
2929
</description>
3030
</method>
31+
<method name="fix_invalid_cells">
32+
<return type="void" />
33+
<description>
34+
Clears cells that do not exist in the grid map.
35+
</description>
36+
</method>
3137
<method name="get_bake_mesh_instance">
3238
<return type="RID" />
3339
<param index="0" name="idx" type="int" />

modules/gridmap/editor/grid_map_editor_plugin.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ void GridMapEditor::_menu_option(int p_option) {
185185
_fill_selection();
186186

187187
} break;
188+
case MENU_OPTION_CLEAR_ALL_CELLS: {
189+
_clear_all_cells();
190+
} break;
191+
case MENU_OPTION_FIX_INVALID_CELLS: {
192+
_fix_invalid_cells();
193+
} break;
188194
case MENU_OPTION_GRIDMAP_SETTINGS: {
189195
settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE);
190196
} break;
@@ -502,6 +508,24 @@ void GridMapEditor::_fill_selection() {
502508
undo_redo->commit_action();
503509
}
504510

511+
void GridMapEditor::_clear_all_cells() {
512+
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
513+
undo_redo->create_action(TTR("Clear All Cells"));
514+
undo_redo->add_undo_method(node, "set", "data", node->get("data"));
515+
node->clear();
516+
undo_redo->add_do_method(node, "set", "data", node->get("data"));
517+
undo_redo->commit_action();
518+
}
519+
520+
void GridMapEditor::_fix_invalid_cells() {
521+
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
522+
undo_redo->create_action(TTR("Fix Invalid Cells"));
523+
undo_redo->add_undo_method(node, "set", "data", node->get("data"));
524+
node->fix_invalid_cells();
525+
undo_redo->add_do_method(node, "set", "data", node->get("data"));
526+
undo_redo->commit_action();
527+
}
528+
505529
void GridMapEditor::_clear_clipboard_data() {
506530
for (const ClipboardItem &E : clipboard_items) {
507531
if (E.instance.is_null()) {
@@ -1323,6 +1347,9 @@ GridMapEditor::GridMapEditor() {
13231347
options->get_popup()->add_check_shortcut(ED_GET_SHORTCUT("grid_map/keep_selected"), MENU_OPTION_PASTE_SELECTS);
13241348
options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS), true);
13251349
options->get_popup()->add_separator();
1350+
options->get_popup()->add_item(TTR("Clear All Cells"), MENU_OPTION_CLEAR_ALL_CELLS);
1351+
options->get_popup()->add_item(TTR("Fix Invalid Cells"), MENU_OPTION_FIX_INVALID_CELLS);
1352+
options->get_popup()->add_separator();
13261353
options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS);
13271354

13281355
settings_dialog = memnew(ConfirmationDialog);

modules/gridmap/editor/grid_map_editor_plugin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ class GridMapEditor : public VBoxContainer {
197197
MENU_OPTION_SELECTION_CUT,
198198
MENU_OPTION_SELECTION_CLEAR,
199199
MENU_OPTION_SELECTION_FILL,
200+
MENU_OPTION_CLEAR_ALL_CELLS,
201+
MENU_OPTION_FIX_INVALID_CELLS,
200202
MENU_OPTION_GRIDMAP_SETTINGS
201203

202204
};
@@ -247,6 +249,8 @@ class GridMapEditor : public VBoxContainer {
247249

248250
void _delete_selection();
249251
void _fill_selection();
252+
void _clear_all_cells();
253+
void _fix_invalid_cells();
250254

251255
bool do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click);
252256

modules/gridmap/grid_map.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,19 @@ RID GridMap::get_navigation_map() const {
280280
#endif // NAVIGATION_3D_DISABLED
281281

282282
void GridMap::set_mesh_library(const Ref<MeshLibrary> &p_mesh_library) {
283-
if (mesh_library.is_valid()) {
284-
mesh_library->disconnect_changed(callable_mp(this, &GridMap::_recreate_octant_data));
285-
}
286-
mesh_library = p_mesh_library;
287-
if (mesh_library.is_valid()) {
288-
mesh_library->connect_changed(callable_mp(this, &GridMap::_recreate_octant_data));
289-
}
283+
if (mesh_library != p_mesh_library) {
284+
if (mesh_library.is_valid()) {
285+
mesh_library->disconnect_changed(callable_mp(this, &GridMap::_recreate_octant_data));
286+
}
290287

291-
_recreate_octant_data();
292-
emit_signal(CoreStringName(changed));
288+
mesh_library = p_mesh_library;
289+
if (mesh_library.is_valid()) {
290+
mesh_library->connect_changed(callable_mp(this, &GridMap::_recreate_octant_data));
291+
}
292+
293+
_recreate_octant_data();
294+
emit_signal(CoreStringName(changed));
295+
}
293296
}
294297

295298
Ref<MeshLibrary> GridMap::get_mesh_library() const {
@@ -1130,6 +1133,16 @@ void GridMap::clear() {
11301133
clear_baked_meshes();
11311134
}
11321135

1136+
void GridMap::fix_invalid_cells() {
1137+
ERR_FAIL_COND_MSG(mesh_library.is_null(), "Cannot fix invalid cells if MeshLibrary is not open.");
1138+
1139+
for (HashMap<IndexKey, Cell, IndexKey>::Iterator I = cell_map.begin(); I; ++I) {
1140+
if (!mesh_library.is_valid() || !mesh_library->has_item(I->value.item)) {
1141+
set_cell_item(I->key, INVALID_CELL_ITEM);
1142+
}
1143+
}
1144+
}
1145+
11331146
#ifndef DISABLE_DEPRECATED
11341147
void GridMap::resource_changed(const Ref<Resource> &p_res) {
11351148
}
@@ -1222,6 +1235,7 @@ void GridMap::_bind_methods() {
12221235
ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
12231236

12241237
ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
1238+
ClassDB::bind_method(D_METHOD("fix_invalid_cells"), &GridMap::fix_invalid_cells);
12251239

12261240
ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
12271241
ClassDB::bind_method(D_METHOD("get_used_cells_by_item", "item"), &GridMap::get_used_cells_by_item);

modules/gridmap/grid_map.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ class GridMap : public Node3D {
307307
void make_baked_meshes(bool p_gen_lightmap_uv = false, float p_lightmap_uv_texel_size = 0.1);
308308

309309
void clear();
310+
void fix_invalid_cells();
310311

311312
Array get_bake_meshes();
312313
RID get_bake_mesh_instance(int p_idx);

0 commit comments

Comments
 (0)