From db8ea9081f290f18d3a562bccbb6aba31fdc80e2 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 11:43:16 +0100 Subject: [PATCH] fix(editor): Fields: the edits of a field stick, the natural slopes stay when the formations are laid again, no holes far off or by a missing chunk The copy of the field the widgets edit is kept while one of them is held, so a drag ends with what it was dragged to. Laying the formations again keeps the natural shape of the slopes of each. The surfaces of the fields are made as far as the chunks are drawn, so their holes never show without them; a field over ground with no chunk is made of what has ground, and waits only for chunks which are in the files but not loaded yet. Outline it again puts the points on the ground and the cut changes that field. The holes of the fields are marked (254) and giving one back closes only its own, the holes cut by hand stay. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ --- application/editormode.h | 3 ++ application/editormodefields.cpp | 55 +++++++++++++++++++--- application/editormodenmt.cpp | 8 ++++ editor/editorTerrainStreamer.cpp | 78 ++++++++++++++++++++++++-------- editor/editorTerrainStreamer.hpp | 13 +++++- lang/editor_pl.po | 6 +++ 6 files changed, 136 insertions(+), 27 deletions(-) diff --git a/application/editormode.h b/application/editormode.h index 71cd5a394..e6a7ad0fa 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -614,6 +614,9 @@ class editor_mode : public application_mode, private editor_track::observer, pri terrain_waves m_waves; heightmap::modifier::natural_shape m_natural{0.8, 15.0, 0.15, 0.3, 0.08}; int m_patch_selected{-1}; // field chosen in the list + heightmap::surface_patch m_patch_edit; // its copy the widgets edit, kept while one of them is held + int m_patch_edited{-1}; // which field m_patch_edit is of + std::string m_patch_reoutline; // the field whose outline was put back to be drawn again: cutting out changes it void render_fields_ui(); void cut_out_field(); void render_waves_options(); diff --git a/application/editormodefields.cpp b/application/editormodefields.cpp index f5d462a82..515341f0a 100644 --- a/application/editormodefields.cpp +++ b/application/editormodefields.cpp @@ -202,6 +202,36 @@ void editor_mode::cut_out_field() for (auto const &point : m_paint_outline) patch.outline.emplace_back(point.x, point.z); auto patches{m_streamer.patches()}; + // the outline of a field put back to be drawn again: that field gets it, the rest of it as it was + auto const again{std::find_if(patches.begin(), patches.end(), [this](heightmap::surface_patch const &Other) { return Other.name == m_patch_reoutline; })}; + m_patch_reoutline.clear(); + if (again != patches.end()) + { + auto const old{*again}; + auto const index{static_cast(again - patches.begin())}; + auto outline{std::move(patch.outline)}; + patch = old; + patch.outline = std::move(outline); + terrain_history_step(); + if (m_waves.on && apply_waves(patch.outline)) + forget_ground(); + auto const name{patch.name}; + patches[index] = patch; + if (false == patch.paint) + { + m_streamer.cut_field(old.outline, false); + m_streamer.cut_field(patch.outline, true); + m_streamer.patches(std::move(patches)); + forget_ground(); + } + else + repaint_patches(std::move(patches), {index}, {old}); + terrain_history_step(); + m_patch_selected = static_cast(index); + m_paint_outline.clear(); + m_terrain_status = format(STR_C("%s outlined again, Ctrl+Z puts it back"), name.c_str()); + return; + } // a name not taken yet for (int number = static_cast(patches.size()) + 1;; ++number) { @@ -221,7 +251,7 @@ void editor_mode::cut_out_field() if (separate) { // the ground cut away under it, the field drawn over the hole with the heights the terrain has there - m_streamer.cut(outline, true); + m_streamer.cut_field(outline, true); m_streamer.patches(std::move(patches)); forget_ground(); } @@ -780,7 +810,14 @@ void editor_mode::render_patches_list() } if (m_patch_selected < 0 || m_patch_selected >= static_cast(patches.size())) return; - auto patch{patches[m_patch_selected]}; + // the copy edited is kept from frame to frame while a widget is held, so a drag ends with the values dragged to; taken + // anew from the list otherwise (another field chosen, an undo) + if (false == ImGui::IsAnyItemActive() || m_patch_edited != m_patch_selected) + { + m_patch_edit = patches[m_patch_selected]; + m_patch_edited = m_patch_selected; + } + auto &patch{m_patch_edit}; bool changed{false}; char buffer[256]; std::snprintf(buffer, sizeof(buffer), "%s", patch.material.c_str()); @@ -836,7 +873,7 @@ void editor_mode::render_patches_list() patch.noise = std::max(0.0f, patch.noise); patch.noise_scale = std::max(0.5f, patch.noise_scale); auto all{patches}; - all[m_patch_selected] = std::move(patch); + all[m_patch_selected] = patch; repaint_patches(std::move(all), {static_cast(m_patch_selected)}); return; } @@ -845,17 +882,21 @@ void editor_mode::render_patches_list() m_paint_outline.clear(); for (auto const &point : patch.outline) { + // on the ground: a field following the terrain has no heights of its own double height{0.0}; - patch.height(point.x, point.y, height); + if (false == m_streamer.height_at(point.x, point.y, height)) + patch.height(point.x, point.y, height); m_paint_outline.emplace_back(point.x, height, point.y); } + // cutting it out again changes this field, not a new one + m_patch_reoutline = patch.name; } if (ImGui::IsItemHovered()) - ImGui::SetTooltip("%s", STR_C("Puts the outline of the field back as the outline being drawn")); + ImGui::SetTooltip("%s", STR_C("Puts the outline of the field back as the outline being drawn; cut out again, it changes this field")); ImGui::SameLine(); if (ImGui::Button(STR_C("Give back to the terrain##patch"))) { - auto const gone = patch; + auto const gone = patches[m_patch_selected]; auto all{patches}; all.erase(all.begin() + m_patch_selected); m_patch_selected = -1; @@ -866,7 +907,7 @@ void editor_mode::render_patches_list() m_terrain_status = STR_C("Surface removed, the ground under it has the first material of the palette again"); return; } - m_streamer.cut(gone.outline, false); + m_streamer.cut_field(gone.outline, false); m_streamer.patches(std::move(all)); forget_ground(); m_terrain_status = STR_C("Surface removed, the hole under it closed"); diff --git a/application/editormodenmt.cpp b/application/editormodenmt.cpp index d92837e35..fa0e56f46 100644 --- a/application/editormodenmt.cpp +++ b/application/editormodenmt.cpp @@ -934,6 +934,14 @@ std::size_t editor_mode::nmt_shape_tracks(std::vector const *Only) for (auto const &name : gone) m_streamer.remove_modifier(name); } + // the natural shape of the slopes made for a formation stays with it when it's laid again + for (auto &modifier : laid) + for (auto const &old : m_streamer.modifiers()) + if (old.name == modifier.name) + { + modifier.natural = old.natural; + break; + } auto const count{laid.size()}; m_streamer.modifiers(std::move(laid)); forget_ground(); diff --git a/editor/editorTerrainStreamer.cpp b/editor/editorTerrainStreamer.cpp index 21bc2f4e6..7c24769a0 100644 --- a/editor/editorTerrainStreamer.cpp +++ b/editor/editorTerrainStreamer.cpp @@ -911,6 +911,22 @@ void terrain_streamer::collect(std::vector &Out) const Out.push_back(entry.second.get()); } +bool terrain_streamer::waits_for_ground(heightmap::surface_patch const &Patch) const +{ + glm::dvec2 low, high; + Patch.bounds(low, high); + auto const first{heightmap::chunk_at(low.x, low.y)}; + auto const last{heightmap::chunk_at(high.x, high.y)}; + for (auto z{std::min(first.second, last.second)}; z <= std::max(first.second, last.second); ++z) + for (auto x{std::min(first.first, last.first)}; x <= std::max(first.first, last.first); ++x) + { + chunk_key const key{x, z}; + if (m_chunks.find(key) == m_chunks.end() && exists(key)) + return true; + } + return false; +} + editor_terrain *terrain_streamer::terrain_at(double X, double Z) const { auto const lookup{m_chunks.find(heightmap::chunk_at(X, Z))}; @@ -1594,7 +1610,19 @@ bool terrain_streamer::cut(std::vector const &Outline, bool Cut) return cut_area(low, high, Cut, true, [&](double const Px, double const Pz) { return inside_outline(Outline, Px, Pz); }); } -bool terrain_streamer::cut_area(glm::dvec2 const &Min, glm::dvec2 const &Max, bool const Cut, bool const Load, std::function const &Inside) +bool terrain_streamer::cut_field(std::vector const &Outline, bool const Cut) +{ + if (Outline.size() < 3) + return false; + auto const [low, high]{outline_bounds(Outline)}; + auto const inside = [&](double const Px, double const Pz) { return inside_outline(Outline, Px, Pz); }; + if (cut_area(low, high, Cut, true, inside, kFieldHole) || Cut) + return true; + return cut_area(low, high, false, true, inside); // cut before the holes of the fields were marked: all of it +} + +bool terrain_streamer::cut_area(glm::dvec2 const &Min, glm::dvec2 const &Max, bool const Cut, bool const Load, std::function const &Inside, + std::uint8_t const Mark) { bool changed{false}; for (auto *chunk : chunks_in(Min, Max, Load)) @@ -1608,13 +1636,16 @@ bool terrain_streamer::cut_area(glm::dvec2 const &Min, glm::dvec2 const &Max, bo auto const low{data->corner()}; auto const xs{index_range(low.x, step, data->holes + 1, Min.x, Max.x)}; auto const zs{index_range(low.y, step, data->holes + 1, Min.y, Max.y)}; - std::uint8_t const value{Cut ? std::uint8_t{255} : std::uint8_t{0}}; + std::uint8_t const value{Cut ? Mark : std::uint8_t{0}}; + auto const marked{Mark != 255}; bool touched{false}; for (int iz = zs.first; iz <= zs.second; ++iz) for (int ix = xs.first; ix <= xs.second; ++ix) { auto &sample{data->hole_mask[static_cast(iz) * (data->holes + 1) + ix]}; - if (sample == value || false == Inside(low.x + ix * step, low.y + iz * step)) + // a marked cut leaves the holes there are as they are, a marked close closes its own only + auto const kept{marked && (Cut ? sample != 0 : sample != Mark)}; + if (sample == value || kept || false == Inside(low.x + ix * step, low.y + iz * step)) continue; sample = value; touched = true; @@ -2348,21 +2379,31 @@ std::vector patch_triangles(heightmap::surface_patch const &Patch, H for (int k = 0; k <= samples; ++k) mark(glm::mix(a, b, static_cast(k) / samples)); } - auto const vertex = [&](double const X, double const Z) { - double height{0.0}; - if (false == Height(X, Z, height)) - Missing = true; - return glm::dvec3{X, height, Z}; - }; - // a polygon in the plan, as triangles facing up + // a polygon in the plan, as triangles facing up; a triangle with a corner over no ground is left out, the rest is made auto const emit = [&](std::vector const &Polygon) { if (Polygon.size() < 3) return; auto const indices{heightmap::triangulate(Polygon)}; // the polygon is turned counterclockwise in the plan, the triangles go the other way round to face up for (std::size_t i = 0; i + 2 < indices.size(); i += 3) + { + glm::dvec3 corners[3]; + bool grounded{true}; + int k{0}; for (auto const index : {indices[i + 2], indices[i + 1], indices[i]}) - result.push_back(vertex(Polygon[index].x, Polygon[index].y)); + { + double height{0.0}; + auto const &point{Polygon[index]}; + if (false == Height(point.x, point.y, height)) + { + grounded = false; + Missing = true; + } + corners[k++] = glm::dvec3{point.x, height, point.y}; + } + if (grounded) + result.insert(result.end(), std::begin(corners), std::end(corners)); + } }; // Sutherland-Hodgman: the outline clipped to a triangle of a cell (A, B, C counterclockwise) auto const clip = [&](glm::dvec2 const &A, glm::dvec2 const &B, glm::dvec2 const &C) { @@ -2417,8 +2458,8 @@ std::vector patch_triangles(heightmap::surface_patch const &Patch, H } if (false == inside(x0 + 0.5 * step, z0 + 0.5 * step)) continue; - for (auto const &corner : {a, d, b, b, d, c}) - result.push_back(vertex(corner.x, corner.y)); + emit({a, b, d}); + emit({b, c, d}); } return result; } @@ -2537,6 +2578,9 @@ void terrain_streamer::build_water() // ground not loaded yet waits for it m_patchcentre = glm::dvec2{m_camera.x, m_camera.z}; m_patchpending = false; + // as far as the chunks are drawn, their holes with them: never a hole without the surface over it + auto const reach_of_patches{std::max(patch_reach, static_cast(Global.BaseDrawRange) * std::max(1.0, static_cast(Global.fDistanceFactor)) + + heightmap::chunk_size)}; for (auto const &patch : m_manifest.patches) { if (patch.paint) @@ -2545,7 +2589,7 @@ void terrain_streamer::build_water() glm::dvec2 boxhigh; patch.bounds(boxlow, boxhigh); auto const nearest{glm::clamp(m_patchcentre, boxlow, boxhigh)}; - if (m_hascamera && glm::distance(nearest, m_patchcentre) > patch_reach) + if (m_hascamera && glm::distance(nearest, m_patchcentre) > reach_of_patches) continue; auto const surface = [&](double const X, double const Z, double &Height) { if (false == patch.follow) @@ -2563,11 +2607,9 @@ void terrain_streamer::build_water() }; bool missing{false}; auto const triangles{patch_triangles(patch, surface, missing)}; - if (missing) - { + // made of what has the ground loaded; waited for only where the ground is still to come, not where the terrain has none + if (missing && patch.follow && waits_for_ground(patch)) m_patchpending = true; - continue; - } if (triangles.empty()) continue; glm::dvec2 low{std::numeric_limits::max()}; diff --git a/editor/editorTerrainStreamer.hpp b/editor/editorTerrainStreamer.hpp index bde2f8c10..09ff96046 100644 --- a/editor/editorTerrainStreamer.hpp +++ b/editor/editorTerrainStreamer.hpp @@ -136,6 +136,9 @@ class terrain_streamer bool cut(double X, double Z, double Radius, bool Cut); // the same within Outline (x, z); the chunks are loaded where needed bool cut(std::vector const &Outline, bool Cut); + // the hole of a field of its own surface cut (Cut true) or closed: marked as the field's, so closing it leaves the holes + // cut otherwise (the Holes tool) there as they are; a hole of a field cut before the mark is closed whole + bool cut_field(std::vector const &Outline, bool Cut); // grass: grows the kind of the grass palette Kind within Radius of (X,Z), Strength (0..1) of the way at the centre; // Erase: takes every kind of grass away there instead bool grass(double X, double Z, double Radius, double Strength, std::uint16_t Kind, bool Erase); @@ -287,7 +290,11 @@ class terrain_streamer bool paint_area(glm::dvec2 const &Min, glm::dvec2 const &Max, std::uint16_t Layer, bool Load, std::function const &Amount); // cuts (Cut true) or closes the holes at the hole samples within the rectangle where Inside says so. Load: chunks not in // memory are loaded - bool cut_area(glm::dvec2 const &Min, glm::dvec2 const &Max, bool Cut, bool Load, std::function const &Inside); + // Mark: the value of a hole sample, 255 of the holes cut by hand, kFieldHole of the fields; with Mark other than 255 only + // the samples of that mark are closed, and a hole already there isn't marked over + bool cut_area(glm::dvec2 const &Min, glm::dvec2 const &Max, bool Cut, bool Load, std::function const &Inside, + std::uint8_t Mark = 255); + static std::uint8_t constexpr kFieldHole{254}; // grows the kind Kind of the grass palette (or takes all grass away, Erase) over the grass samples within the rectangle, // Amount (0..1) of the way at each bool grass_area(glm::dvec2 const &Min, glm::dvec2 const &Max, std::uint16_t Kind, bool Erase, bool Load, std::function const &Amount); @@ -344,7 +351,9 @@ class terrain_streamer double m_cameratime{0.0}; bool m_hascamera{false}; // the pieces cut out of the terrain are made around where the camera was, within patch_reach, again when it moves patch_move away - static constexpr double patch_reach{1500.0}; + static constexpr double patch_reach{1500.0}; // at least; as far as the chunks are drawn + // a chunk under the piece is in the files but not loaded yet + bool waits_for_ground(heightmap::surface_patch const &Patch) const; static constexpr double patch_move{200.0}; glm::dvec2 m_patchcentre{1e12}; bool m_patchpending{false}; // some waited for chunks not loaded diff --git a/lang/editor_pl.po b/lang/editor_pl.po index 78e106617..d0428d25f 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -14639,3 +14639,9 @@ msgstr "Pola" msgid "LMB adds a point of the outline of a field: a surface cut out of the terrain with a material of its own, the ground under it made wavy if wanted; Shift+LMB takes the last one back" msgstr "LPM dodaje punkt obrysu pola: powierzchni wyciętej z terenu z własnym materiałem, z pofalowanym gruntem, jeśli chcesz; Shift+LPM cofa ostatni" + +msgid "%s outlined again, Ctrl+Z puts it back" +msgstr "%s obrysowane na nowo, Ctrl+Z cofa" + +msgid "Puts the outline of the field back as the outline being drawn; cut out again, it changes this field" +msgstr "Wstawia obrys pola z powrotem jako rysowany obrys; wycięcie go zmienia to pole"