From 3ee7bf459a55ceaa02cf2ff8c8fa66a94183c166 Mon Sep 17 00:00:00 2001 From: mchlopecki Date: Fri, 4 Apr 2025 20:50:11 -0500 Subject: [PATCH] Add Bresenham Line to GridMap Drawing --- .../gridmap/editor/grid_map_editor_plugin.cpp | 81 ++++++++++++++----- 1 file changed, 61 insertions(+), 20 deletions(-) diff --git a/modules/gridmap/editor/grid_map_editor_plugin.cpp b/modules/gridmap/editor/grid_map_editor_plugin.cpp index 8a8f4dfb144..0868661e89c 100644 --- a/modules/gridmap/editor/grid_map_editor_plugin.cpp +++ b/modules/gridmap/editor/grid_map_editor_plugin.cpp @@ -30,6 +30,7 @@ #include "grid_map_editor_plugin.h" +#include "core/math/geometry_2d.h" #include "core/os/keyboard.h" #include "editor/editor_main_screen.h" #include "editor/editor_node.h" @@ -435,26 +436,66 @@ bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, b return true; } - if (input_action == INPUT_PAINT) { - SetItem si; - si.position = cursor_gridpos; - si.new_value = selected_palette; - si.new_orientation = cursor_rot; - si.old_value = node->get_cell_item(cursor_gridpos); - si.old_orientation = node->get_cell_item_orientation(cursor_gridpos); - set_items.push_back(si); - node->set_cell_item(cursor_gridpos, selected_palette, cursor_rot); - return true; - } else if (input_action == INPUT_ERASE) { - SetItem si; - si.position = cursor_gridpos; - si.new_value = -1; - si.new_orientation = 0; - si.old_value = node->get_cell_item(cursor_gridpos); - si.old_orientation = node->get_cell_item_orientation(cursor_gridpos); - set_items.push_back(si); - node->set_cell_item(cursor_gridpos, -1); - return true; + if (input_action == INPUT_PAINT || input_action == INPUT_ERASE) { + LocalVector cells; + if (!set_items.is_empty()) { + Vector3i last_si = (--set_items.end())->position; + // Manipulate Vector3i into Point2i by ignoring the edit_axis. + int i = edit_axis == 0 ? 1 : 0; + int j = edit_axis == 2 ? 1 : 2; + Point2i from_cell = Point2i(last_si[i], last_si[j]); + Point2i to_cell = Point2i(cursor_gridpos[i], cursor_gridpos[j]); + + Vector cells_2d = Geometry2D::bresenham_line(from_cell, to_cell); + + switch (edit_axis) { + case 0: + for (const Point2i &cell_2d : cells_2d) { + cells.push_back(Vector3i(edit_floor[0], cell_2d[0], cell_2d[1])); + } + break; + case 1: + for (const Point2i &cell_2d : cells_2d) { + cells.push_back(Vector3i(cell_2d[0], edit_floor[1], cell_2d[1])); + } + break; + case 2: + for (const Point2i &cell_2d : cells_2d) { + cells.push_back(Vector3i(cell_2d[0], cell_2d[1], edit_floor[2])); + } + break; + default: + break; + } + } else { + cells.push_back(cursor_gridpos); + } + + if (input_action == INPUT_PAINT) { + for (const Vector3i &cell_v : cells) { + SetItem si; + si.position = cell_v; + si.new_value = selected_palette; + si.new_orientation = cursor_rot; + si.old_value = node->get_cell_item(cell_v); + si.old_orientation = node->get_cell_item_orientation(cell_v); + set_items.push_back(si); + node->set_cell_item(cell_v, selected_palette, cursor_rot); + } + return true; + } else if (input_action == INPUT_ERASE) { + for (const Vector3i &cell_v : cells) { + SetItem si; + si.position = cell_v; + si.new_value = -1; + si.new_orientation = 0; + si.old_value = node->get_cell_item(cell_v); + si.old_orientation = node->get_cell_item_orientation(cell_v); + set_items.push_back(si); + node->set_cell_item(cell_v, -1); + } + return true; + } } return false;