From d82d4b90e4fe3b7676eee8e0d2cec5f96ac97253 Mon Sep 17 00:00:00 2001 From: Cory Petkovsek <632766+TokisanGames@users.noreply.github.com> Date: Fri, 2 Aug 2024 17:41:03 +0700 Subject: [PATCH] Add shortcuts for MapType --- src/terrain_3d.cpp | 16 ++++++++-------- src/terrain_3d_editor.cpp | 34 +++++++++++++++++----------------- src/terrain_3d_storage.cpp | 2 +- src/terrain_3d_storage.h | 5 +++++ src/terrain_3d_util.cpp | 2 +- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/terrain_3d.cpp b/src/terrain_3d.cpp index 631085ac..d3f5df18 100644 --- a/src/terrain_3d.cpp +++ b/src/terrain_3d.cpp @@ -414,22 +414,22 @@ void Terrain3D::_update_collision() { Ref map, map_x, map_z, map_xz; Ref cmap, cmap_x, cmap_z, cmap_xz; - map = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, i); - cmap = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, i); + map = _storage->get_map_region(TYPE_HEIGHT, i); + cmap = _storage->get_map_region(TYPE_CONTROL, i); int region_id = _storage->get_region_id(Vector3(global_pos.x + region_size, 0.f, global_pos.z) * _mesh_vertex_spacing); if (region_id >= 0) { - map_x = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region_id); - cmap_x = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region_id); + map_x = _storage->get_map_region(TYPE_HEIGHT, region_id); + cmap_x = _storage->get_map_region(TYPE_CONTROL, region_id); } region_id = _storage->get_region_id(Vector3(global_pos.x, 0.f, global_pos.z + region_size) * _mesh_vertex_spacing); if (region_id >= 0) { - map_z = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region_id); - cmap_z = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region_id); + map_z = _storage->get_map_region(TYPE_HEIGHT, region_id); + cmap_z = _storage->get_map_region(TYPE_CONTROL, region_id); } region_id = _storage->get_region_id(Vector3(global_pos.x + region_size, 0.f, global_pos.z + region_size) * _mesh_vertex_spacing); if (region_id >= 0) { - map_xz = _storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region_id); - cmap_xz = _storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region_id); + map_xz = _storage->get_map_region(TYPE_HEIGHT, region_id); + cmap_xz = _storage->get_map_region(TYPE_CONTROL, region_id); } for (int z = 0; z < shape_size; z++) { diff --git a/src/terrain_3d_editor.cpp b/src/terrain_3d_editor.cpp index 1e3c08da..57268fb9 100644 --- a/src/terrain_3d_editor.cpp +++ b/src/terrain_3d_editor.cpp @@ -39,7 +39,7 @@ void Terrain3DEditor::_operate_region(const Vector3 &p_global_position) { } else { if (has_region) { int region_id = _terrain->get_storage()->get_region_id(p_global_position); - Ref height_map = _terrain->get_storage()->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region_id); + Ref height_map = _terrain->get_storage()->get_map_region(TYPE_HEIGHT, region_id); height_range = Util::get_min_max(height_map); _terrain->get_storage()->remove_region(p_global_position); @@ -74,11 +74,11 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_ } } - Terrain3DStorage::MapType map_type; + MapType map_type; switch (_tool) { case HEIGHT: case INSTANCER: - map_type = Terrain3DStorage::TYPE_HEIGHT; + map_type = TYPE_HEIGHT; break; case TEXTURE: case AUTOSHADER: @@ -86,11 +86,11 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_ case NAVIGATION: case ANGLE: case SCALE: - map_type = Terrain3DStorage::TYPE_CONTROL; + map_type = TYPE_CONTROL; break; case COLOR: case ROUGHNESS: - map_type = Terrain3DStorage::TYPE_COLOR; + map_type = TYPE_COLOR; break; default: LOG(ERROR, "Invalid tool selected"); @@ -213,7 +213,7 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_ Color src = map->get_pixelv(map_pixel_position); Color dest = src; - if (map_type == Terrain3DStorage::TYPE_HEIGHT) { + if (map_type == TYPE_HEIGHT) { real_t srcf = src.r; real_t destf = dest.r; @@ -300,7 +300,7 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_ edited_position.y = destf; edited_area = edited_area.expand(edited_position); - } else if (map_type == Terrain3DStorage::TYPE_CONTROL) { + } else if (map_type == TYPE_CONTROL) { // Get bit field from pixel uint32_t base_id = get_base(src.r); uint32_t overlay_id = get_overlay(src.r); @@ -424,7 +424,7 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_ // Write back to pixel in FORMAT_RF. Must be a 32-bit float dest = Color(as_float(bits), 0.f, 0.f, 1.f); - } else if (map_type == Terrain3DStorage::TYPE_COLOR) { + } else if (map_type == TYPE_COLOR) { switch (_tool) { case COLOR: dest = src.lerp((_operation == ADD) ? color : COLOR_WHITE, brush_alpha * strength); @@ -466,9 +466,9 @@ Dictionary Terrain3DEditor::_get_undo_data() const { LOG(DEBUG, "Storing region locations"); data["region_locations"] = _terrain->get_storage()->get_region_locations().duplicate(); if (_operation == SUBTRACT) { - data["height_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_HEIGHT); - data["control_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_CONTROL); - data["color_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_COLOR); + data["height_map"] = _terrain->get_storage()->get_maps_copy(TYPE_HEIGHT); + data["control_map"] = _terrain->get_storage()->get_maps_copy(TYPE_CONTROL); + data["color_map"] = _terrain->get_storage()->get_maps_copy(TYPE_COLOR); data["height_range"] = _terrain->get_storage()->get_height_range(); data["edited_area"] = _terrain->get_storage()->get_edited_area(); } @@ -477,7 +477,7 @@ Dictionary Terrain3DEditor::_get_undo_data() const { case HEIGHT: LOG(DEBUG, "Storing height maps and range"); data["region_locations"] = _terrain->get_storage()->get_region_locations().duplicate(); - data["height_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_HEIGHT); + data["height_map"] = _terrain->get_storage()->get_maps_copy(TYPE_HEIGHT); data["height_range"] = _terrain->get_storage()->get_height_range(); data["edited_area"] = _terrain->get_storage()->get_edited_area(); break; @@ -490,13 +490,13 @@ Dictionary Terrain3DEditor::_get_undo_data() const { case AUTOSHADER: case NAVIGATION: LOG(DEBUG, "Storing control maps"); - data["control_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_CONTROL); + data["control_map"] = _terrain->get_storage()->get_maps_copy(TYPE_CONTROL); break; case COLOR: case ROUGHNESS: LOG(DEBUG, "Storing color maps"); - data["color_map"] = _terrain->get_storage()->get_maps_copy(Terrain3DStorage::TYPE_COLOR); + data["color_map"] = _terrain->get_storage()->get_maps_copy(TYPE_COLOR); break; case INSTANCER: @@ -551,11 +551,11 @@ void Terrain3DEditor::_apply_undo(const Dictionary &p_set) { if (key == "region_offsets") { _terrain->get_storage()->set_region_locations(p_set[key]); } else if (key == "height_map") { - _terrain->get_storage()->set_maps(Terrain3DStorage::TYPE_HEIGHT, p_set[key]); + _terrain->get_storage()->set_maps(TYPE_HEIGHT, p_set[key]); } else if (key == "control_map") { - _terrain->get_storage()->set_maps(Terrain3DStorage::TYPE_CONTROL, p_set[key]); + _terrain->get_storage()->set_maps(TYPE_CONTROL, p_set[key]); } else if (key == "color_map") { - _terrain->get_storage()->set_maps(Terrain3DStorage::TYPE_COLOR, p_set[key]); + _terrain->get_storage()->set_maps(TYPE_COLOR, p_set[key]); } else if (key == "height_range") { _terrain->get_storage()->set_height_range(p_set[key]); } else if (key == "edited_area") { diff --git a/src/terrain_3d_storage.cpp b/src/terrain_3d_storage.cpp index 101608ca..8e98850c 100644 --- a/src/terrain_3d_storage.cpp +++ b/src/terrain_3d_storage.cpp @@ -690,7 +690,7 @@ void Terrain3DStorage::save() { if (_save_16_bit) { LOG(DEBUG, "16-bit save requested, converting heightmaps"); TypedArray original_maps; - original_maps = get_maps_copy(Terrain3DStorage::MapType::TYPE_HEIGHT); + original_maps = get_maps_copy(TYPE_HEIGHT); for (int i = 0; i < _height_maps.size(); i++) { Ref img = _height_maps[i]; img->convert(Image::FORMAT_RH); diff --git a/src/terrain_3d_storage.h b/src/terrain_3d_storage.h index 6a5e525e..4c45835e 100644 --- a/src/terrain_3d_storage.h +++ b/src/terrain_3d_storage.h @@ -197,7 +197,12 @@ class Terrain3DStorage : public Resource { static void _bind_methods(); }; +typedef Terrain3DStorage::MapType MapType; VARIANT_ENUM_CAST(Terrain3DStorage::MapType); +constexpr Terrain3DStorage::MapType TYPE_HEIGHT = Terrain3DStorage::MapType::TYPE_HEIGHT; +constexpr Terrain3DStorage::MapType TYPE_CONTROL = Terrain3DStorage::MapType::TYPE_CONTROL; +constexpr Terrain3DStorage::MapType TYPE_COLOR = Terrain3DStorage::MapType::TYPE_COLOR; +constexpr Terrain3DStorage::MapType TYPE_MAX = Terrain3DStorage::MapType::TYPE_MAX; VARIANT_ENUM_CAST(Terrain3DStorage::RegionSize); VARIANT_ENUM_CAST(Terrain3DStorage::HeightFilter); diff --git a/src/terrain_3d_util.cpp b/src/terrain_3d_util.cpp index 620b3fad..85d5599a 100644 --- a/src/terrain_3d_util.cpp +++ b/src/terrain_3d_util.cpp @@ -233,7 +233,7 @@ Ref Terrain3DUtil::load_image(const String &p_file_name, const int p_cach LOG(DEBUG, "Total file size is: ", fsize, " calculated width: ", fwidth, " dimensions: ", r16_size); file->seek(0); } - img = Image::create(r16_size.x, r16_size.y, false, Terrain3DStorage::FORMAT[Terrain3DStorage::TYPE_HEIGHT]); + img = Image::create(r16_size.x, r16_size.y, false, Terrain3DStorage::FORMAT[TYPE_HEIGHT]); for (int y = 0; y < r16_size.y; y++) { for (int x = 0; x < r16_size.x; x++) { real_t h = real_t(file->get_16()) / 65535.0f;