Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GridMap to use Vector3i instead of three ints #39958

Merged
merged 1 commit into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions modules/gridmap/grid_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,26 +244,26 @@ bool GridMap::get_center_z() const {
return center_z;
}

void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
void GridMap::set_cell_item(const Vector3i &p_position, int p_item, int p_rot) {
if (baked_meshes.size() && !recreating_octants) {
//if you set a cell item, baked meshes go good bye
clear_baked_meshes();
_recreate_octant_data();
}

ERR_FAIL_INDEX(ABS(p_x), 1 << 20);
ERR_FAIL_INDEX(ABS(p_y), 1 << 20);
ERR_FAIL_INDEX(ABS(p_z), 1 << 20);
ERR_FAIL_INDEX(ABS(p_position.x), 1 << 20);
ERR_FAIL_INDEX(ABS(p_position.y), 1 << 20);
ERR_FAIL_INDEX(ABS(p_position.z), 1 << 20);

IndexKey key;
key.x = p_x;
key.y = p_y;
key.z = p_z;
key.x = p_position.x;
key.y = p_position.y;
key.z = p_position.z;

OctantKey ok;
ok.x = p_x / octant_size;
ok.y = p_y / octant_size;
ok.z = p_z / octant_size;
ok.x = p_position.x / octant_size;
ok.y = p_position.y / octant_size;
ok.z = p_position.z / octant_size;

if (p_item < 0) {
//erase
Expand Down Expand Up @@ -318,52 +318,52 @@ void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
cell_map[key] = c;
}

int GridMap::get_cell_item(int p_x, int p_y, int p_z) const {
ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, INVALID_CELL_ITEM);
ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, INVALID_CELL_ITEM);
ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, INVALID_CELL_ITEM);
int GridMap::get_cell_item(const Vector3i &p_position) const {
ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, INVALID_CELL_ITEM);
ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, INVALID_CELL_ITEM);
ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, INVALID_CELL_ITEM);

IndexKey key;
key.x = p_x;
key.y = p_y;
key.z = p_z;
key.x = p_position.x;
key.y = p_position.y;
key.z = p_position.z;

if (!cell_map.has(key)) {
return INVALID_CELL_ITEM;
}
return cell_map[key].item;
}

int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const {
ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, -1);
ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, -1);
ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, -1);
int GridMap::get_cell_item_orientation(const Vector3i &p_position) const {
ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, -1);
ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, -1);
ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, -1);

IndexKey key;
key.x = p_x;
key.y = p_y;
key.z = p_z;
key.x = p_position.x;
key.y = p_position.y;
key.z = p_position.z;

if (!cell_map.has(key)) {
return -1;
}
return cell_map[key].rot;
}

Vector3 GridMap::world_to_map(const Vector3 &p_world_pos) const {
Vector3 map_pos = p_world_pos / cell_size;
map_pos.x = floor(map_pos.x);
map_pos.y = floor(map_pos.y);
map_pos.z = floor(map_pos.z);
return map_pos;
Vector3i GridMap::world_to_map(const Vector3 &p_world_position) const {
Vector3 map_position = p_world_position / cell_size;
map_position.x = floor(map_position.x);
map_position.y = floor(map_position.y);
map_position.z = floor(map_position.z);
return Vector3i(map_position);
}

Vector3 GridMap::map_to_world(int p_x, int p_y, int p_z) const {
Vector3 GridMap::map_to_world(const Vector3i &p_map_position) const {
Vector3 offset = _get_offset();
Vector3 world_pos(
p_x * cell_size.x + offset.x,
p_y * cell_size.y + offset.y,
p_z * cell_size.z + offset.z);
p_map_position.x * cell_size.x + offset.x,
p_map_position.y * cell_size.y + offset.y,
p_map_position.z * cell_size.z + offset.z);
return world_pos;
}

Expand Down Expand Up @@ -725,7 +725,7 @@ void GridMap::_recreate_octant_data() {
Map<IndexKey, Cell> cell_copy = cell_map;
_clear_internal();
for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
set_cell_item(E->key().x, E->key().y, E->key().z, E->get().item, E->get().rot);
set_cell_item(Vector3i(E->key()), E->get().item, E->get().rot);
}
recreating_octants = false;
}
Expand Down Expand Up @@ -799,12 +799,12 @@ void GridMap::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);

ClassDB::bind_method(D_METHOD("set_cell_item", "x", "y", "z", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
ClassDB::bind_method(D_METHOD("get_cell_item", "x", "y", "z"), &GridMap::get_cell_item);
ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation);
ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &GridMap::get_cell_item);
ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation);

ClassDB::bind_method(D_METHOD("world_to_map", "pos"), &GridMap::world_to_map);
ClassDB::bind_method(D_METHOD("map_to_world", "x", "y", "z"), &GridMap::map_to_world);
ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &GridMap::world_to_map);
ClassDB::bind_method(D_METHOD("map_to_world", "map_position"), &GridMap::map_to_world);

ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
Expand Down
19 changes: 14 additions & 5 deletions modules/gridmap/grid_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ class GridMap : public Node3D {
return key < p_key.key;
}

_FORCE_INLINE_ operator Vector3i() const {
return Vector3i(x, y, z);
}

IndexKey(Vector3i p_vector) {
x = (int16_t)p_vector.x;
y = (int16_t)p_vector.y;
z = (int16_t)p_vector.z;
}
IndexKey() { key = 0; }
};

Expand Down Expand Up @@ -234,12 +243,12 @@ class GridMap : public Node3D {
void set_center_z(bool p_enable);
bool get_center_z() const;

void set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot = 0);
int get_cell_item(int p_x, int p_y, int p_z) const;
int get_cell_item_orientation(int p_x, int p_y, int p_z) const;
void set_cell_item(const Vector3i &p_position, int p_item, int p_rot = 0);
int get_cell_item(const Vector3i &p_position) const;
int get_cell_item_orientation(const Vector3i &p_position) const;

Vector3 world_to_map(const Vector3 &p_world_pos) const;
Vector3 map_to_world(int p_x, int p_y, int p_z) const;
Vector3i world_to_map(const Vector3 &p_world_position) const;
Vector3 map_to_world(const Vector3i &p_map_position) const;

void set_clip(bool p_enabled, bool p_clip_above = true, int p_floor = 0, Vector3::Axis p_axis = Vector3::AXIS_X);

Expand Down
57 changes: 30 additions & 27 deletions modules/gridmap/grid_map_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,15 @@ void GridMapEditor::_update_selection_transform() {
} else {
Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
scale[edit_axis] = 1.0;
Vector3 pos = selection.begin;
pos[edit_axis] = edit_floor[edit_axis];
Vector3 position = selection.begin;
position[edit_axis] = edit_floor[edit_axis];

scale *= node->get_cell_size();
pos *= node->get_cell_size();
position *= node->get_cell_size();

Transform xf2;
xf2.basis.scale(scale);
xf2.origin = pos;
xf2.origin = position;

RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf2);
}
Expand Down Expand Up @@ -414,11 +414,11 @@ bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, b
}

if (input_action == INPUT_PASTE) {
paste_indicator.current = Vector3(cell[0], cell[1], cell[2]);
paste_indicator.current = Vector3i(cell[0], cell[1], cell[2]);
_update_paste_indicator();

} else if (input_action == INPUT_SELECT) {
selection.current = Vector3(cell[0], cell[1], cell[2]);
selection.current = Vector3i(cell[0], cell[1], cell[2]);
if (p_click) {
selection.click = selection.current;
}
Expand All @@ -427,7 +427,7 @@ bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, b

return true;
} else if (input_action == INPUT_PICK) {
int item = node->get_cell_item(cell[0], cell[1], cell[2]);
int item = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
if (item >= 0) {
selected_palette = item;
mesh_library_palette->set_current(item);
Expand All @@ -438,23 +438,23 @@ bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, b
}
if (input_action == INPUT_PAINT) {
SetItem si;
si.pos = Vector3(cell[0], cell[1], cell[2]);
si.position = Vector3i(cell[0], cell[1], cell[2]);
si.new_value = selected_palette;
si.new_orientation = cursor_rot;
si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
set_items.push_back(si);
node->set_cell_item(cell[0], cell[1], cell[2], selected_palette, cursor_rot);
node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), selected_palette, cursor_rot);
return true;
} else if (input_action == INPUT_ERASE) {
SetItem si;
si.pos = Vector3(cell[0], cell[1], cell[2]);
si.position = Vector3i(cell[0], cell[1], cell[2]);
si.new_value = -1;
si.new_orientation = 0;
si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
set_items.push_back(si);
node->set_cell_item(cell[0], cell[1], cell[2], -1);
node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), -1);
return true;
}

Expand All @@ -470,8 +470,9 @@ void GridMapEditor::_delete_selection() {
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++) {
undo_redo->add_do_method(node, "set_cell_item", i, j, k, GridMap::INVALID_CELL_ITEM);
undo_redo->add_undo_method(node, "set_cell_item", i, j, k, node->get_cell_item(i, j, k), node->get_cell_item_orientation(i, j, k));
Vector3i selected = Vector3i(i, j, k);
undo_redo->add_do_method(node, "set_cell_item", selected, GridMap::INVALID_CELL_ITEM);
undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
}
}
}
Expand All @@ -489,8 +490,9 @@ void GridMapEditor::_fill_selection() {
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++) {
undo_redo->add_do_method(node, "set_cell_item", i, j, k, selected_palette, cursor_rot);
undo_redo->add_undo_method(node, "set_cell_item", i, j, k, node->get_cell_item(i, j, k), node->get_cell_item_orientation(i, j, k));
Vector3i selected = Vector3i(i, j, k);
undo_redo->add_do_method(node, "set_cell_item", selected, selected_palette, cursor_rot);
undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
}
}
}
Expand All @@ -515,7 +517,8 @@ void GridMapEditor::_set_clipboard_data() {
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++) {
int itm = node->get_cell_item(i, j, k);
Vector3i selected = Vector3i(i, j, k);
int itm = node->get_cell_item(selected);
if (itm == GridMap::INVALID_CELL_ITEM) {
continue;
}
Expand All @@ -524,8 +527,8 @@ void GridMapEditor::_set_clipboard_data() {

ClipboardItem item;
item.cell_item = itm;
item.grid_offset = Vector3(i, j, k) - selection.begin;
item.orientation = node->get_cell_item_orientation(i, j, k);
item.grid_offset = Vector3(selected) - selection.begin;
item.orientation = node->get_cell_item_orientation(selected);
item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario());

clipboard_items.push_back(item);
Expand Down Expand Up @@ -583,14 +586,14 @@ void GridMapEditor::_do_paste() {
for (List<ClipboardItem>::Element *E = clipboard_items.front(); E; E = E->next()) {
ClipboardItem &item = E->get();

Vector3 pos = rot.xform(item.grid_offset) + paste_indicator.begin + ofs;
Vector3 position = rot.xform(item.grid_offset) + paste_indicator.begin + ofs;

Basis orm;
orm.set_orthogonal_index(item.orientation);
orm = rot * orm;

undo_redo->add_do_method(node, "set_cell_item", pos.x, pos.y, pos.z, item.cell_item, orm.get_orthogonal_index());
undo_redo->add_undo_method(node, "set_cell_item", pos.x, pos.y, pos.z, node->get_cell_item(pos.x, pos.y, pos.z), node->get_cell_item_orientation(pos.x, pos.y, pos.z));
undo_redo->add_do_method(node, "set_cell_item", position, item.cell_item, orm.get_orthogonal_index());
undo_redo->add_undo_method(node, "set_cell_item", position, node->get_cell_item(position), node->get_cell_item_orientation(position));
}

if (reselect) {
Expand Down Expand Up @@ -667,11 +670,11 @@ bool GridMapEditor::forward_spatial_input_event(Camera3D *p_camera, const Ref<In
undo_redo->create_action(TTR("GridMap Paint"));
for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
const SetItem &si = E->get();
undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation);
undo_redo->add_do_method(node, "set_cell_item", si.position, si.new_value, si.new_orientation);
}
for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
const SetItem &si = E->get();
undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation);
undo_redo->add_undo_method(node, "set_cell_item", si.position, si.old_value, si.old_orientation);
}

undo_redo->commit_action();
Expand Down
2 changes: 1 addition & 1 deletion modules/gridmap/grid_map_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class GridMapEditor : public VBoxContainer {
Label *spin_box_label;

struct SetItem {
Vector3 pos;
Vector3i position;
int new_value;
int new_orientation;
int old_value;
Expand Down