diff --git a/application/editoruimodel.cpp b/application/editoruimodel.cpp index e7204bcc5..b2f80a48b 100644 --- a/application/editoruimodel.cpp +++ b/application/editoruimodel.cpp @@ -1608,9 +1608,8 @@ void eu07m_model_panel::render_selection() render_light_settings(*light); } render_place(index); - if (light && eu07m::node_removable(m_model, index) && ImGui::Button(STR_C("Remove this light (Del)"))) + if (render_remove(index, light.has_value())) { - remove_selected(); ImGui::PopItemWidth(); return; } @@ -1618,6 +1617,22 @@ void eu07m_model_panel::render_selection() ImGui::PopItemWidth(); } +bool eu07m_model_panel::render_remove(std::uint32_t const Index, bool const Light) +{ + auto const *target{submodel(Index)}; + auto const removable{(target != nullptr) && (target->Parent != nullptr) && eu07m::node_removable(m_model, Index)}; + ImGui::Separator(); + ImGui::BeginDisabled(false == removable); + auto const clicked{ImGui::Button(Light ? STR_C("Remove this light (Del)") : STR_C("Remove this part (Del)"))}; + ImGui::EndDisabled(); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) + { + ImGui::SetTooltip("%s", removable ? STR_C("Takes it out of the model, with what it draws; Ctrl+Z brings it back") + : STR_C("This part can't be taken out: it has parts under it, or something else in the model points at it (an animation, a light slot, a skin)")); + } + return clicked && remove_selected(); +} + void eu07m_model_panel::render_light_settings(std::uint32_t const Component) { auto const &record{m_model.components[Component]}; @@ -2166,6 +2181,8 @@ void eu07m_model_panel::apply_tree(tree_change const &Change, bool const Forward m_source->LinkEditorSubmodel(*Change.submodel, *Change.parent, Change.previous); m_submodels.insert(m_submodels.begin() + Change.index, Change.submodel); m_boxes.insert(m_boxes.begin() + Change.index, node_box(m_model, Change.index)); + // the part back is picked again, as it was when it was taken out + m_pick_after = Change.index; return; } if (Change.index >= m_submodels.size()) @@ -2175,6 +2192,10 @@ void eu07m_model_panel::apply_tree(tree_change const &Change, bool const Forward m_source->UnlinkEditorSubmodel(*Change.submodel); m_submodels.erase(m_submodels.begin() + Change.index); m_boxes.erase(m_boxes.begin() + Change.index); + if (m_selected == static_cast(Change.index)) + { + m_selected = -1; + } } void eu07m_model_panel::push_change(snapshot Snapshot) @@ -2238,6 +2259,11 @@ void eu07m_model_panel::settled() { m_selected = -1; } + if (auto const part{std::exchange(m_pick_after, std::nullopt)}; part && (*part < m_model.nodes.size())) + { + select(*part); + m_scrolltoselected = true; + } } void eu07m_model_panel::add_light(std::uint32_t const Parent, glm::vec3 const &Position, glm::quat const &Rotation, bool const Spot) @@ -2353,13 +2379,14 @@ bool eu07m_model_panel::remove_selected() } auto const index{static_cast(m_selected)}; auto *target{submodel(index)}; - if ((false == light_of(index).has_value()) || (target == nullptr) || (target->Parent == nullptr) || (false == eu07m::node_removable(m_model, index))) + if ((target == nullptr) || (target->Parent == nullptr) || (false == eu07m::node_removable(m_model, index))) { return false; } auto before{take(true)}; auto *parent{target->Parent}; auto const name{part_name(index)}; + auto const light{light_of(index).has_value()}; if (false == eu07m::remove_node(m_model, index)) { return false; @@ -2371,7 +2398,8 @@ bool eu07m_model_panel::remove_selected() push_change(std::move(before)); m_before = take(false); m_selected = -1; - m_message = std::vformat(STR("Removed the light {}. Ctrl+Z brings it back"), std::make_format_args(name)); + m_message = light ? std::vformat(STR("Removed the light {}. Ctrl+Z brings it back"), std::make_format_args(name)) + : std::vformat(STR("Removed {}. Ctrl+Z brings it back"), std::make_format_args(name)); return true; } diff --git a/application/editoruimodel.h b/application/editoruimodel.h index 5f4a4b545..4aa4e2c68 100644 --- a/application/editoruimodel.h +++ b/application/editoruimodel.h @@ -64,7 +64,7 @@ class eu07m_model_panel // the last change of the model taken back, or made again; false when there's none bool undo(); bool redo(); - // the part picked taken out, when it's a light source that can be (Del) + // the part picked taken out with what it draws, when it can be (Del) bool remove_selected(); // a scenery model has a file of the format to edit bool editable(scene::basic_node const *Node) const; @@ -172,6 +172,8 @@ class eu07m_model_panel void render_channels(TAnimModel &Instance); void render_light_settings(std::uint32_t Component); void render_place(std::uint32_t Index); + // the button which takes the part out, greyed with the reason when it can't be; true when it was taken out + bool render_remove(std::uint32_t Index, bool Light); void render_advanced(std::uint32_t Index); void render_component(std::uint32_t Index); // over the 3d view: the frame of the part picked, the marks of the light sources and the gizmo @@ -234,6 +236,7 @@ class eu07m_model_panel std::vector m_redo; snapshot m_before; // the model when no press or drag was going on bool m_changing{false}; // the press or the drag going on has changed the model, its change is kept + std::optional m_pick_after; // the part put back by the change taken back or made again, to be picked std::string m_filter; // of the parts in the list of all of them bool m_importable{false}; // the model shown has no file of the format, one can be made of it bool m_whole{false}; diff --git a/docs/eu07m-format.md b/docs/eu07m-format.md index e43c028ad..6a89e9d7b 100644 --- a/docs/eu07m-format.md +++ b/docs/eu07m-format.md @@ -335,8 +335,11 @@ in another field of work, the view is free again. - **The settings of a part.** A light: a bulb or a spot, its colour (with a few common ones), its brightness, its glowing spot, the width of the beam and its bright middle (in degrees from its axis), and whether it casts shadows, has a soft edge and lights its own model. The place: the gizmo, and under "Exact values" the position, the rotation (in degrees) and the scale, unless a - matrix of `XFRM` places the node; and the distances it is seen from and up to. A light which nothing else points at is taken out - with "Remove this light" or Del (`eu07m::remove_node`). "Advanced" holds the fields of the common components (`SOCK`, `JOIN`, + matrix of `XFRM` places the node; and the distances it is seen from and up to. A part without parts under it, which nothing else + points at (an animation, a light slot, a skin), is taken out with "Remove this part" ("Remove this light" for a light) or Del, + with what it draws: its components, its transform and its own mesh, whose vertices and indices leave the streams where no other + primitive shares them; the primitives and the references to the geometry store after them move back (`eu07m::remove_node`). So + the flat glowing patch under a lamp post of E3D (`placek`) is taken out and a real light source put in the lamp instead. "Advanced" holds the fields of the common components (`SOCK`, `JOIN`, `CHNL` with its bindings, `LGHT`, `LSLT`, `EMIT`, `COLL`, `MREF`; the others are shown by their size, strings are edited by their text) and "Add a light to this part". - **Changes.** A change shows at once on the model the game has (on every instance of it), when that model is the one of the file. diff --git a/lang/editor_pl.po b/lang/editor_pl.po index f63b96a21..fed6a38c5 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -17072,3 +17072,15 @@ msgstr "Nie udało się wczytać kabiny %s, powód jest w logu" msgid "In the cab of %s; the editor draws it while the cab is edited" msgstr "W kabinie %s; edytor rysuje ją, dopóki edytujesz kabinę" + +msgid "Remove this part (Del)" +msgstr "Usuń tę część (Del)" + +msgid "Takes it out of the model, with what it draws; Ctrl+Z brings it back" +msgstr "Usuwa ją z modelu razem z tym, co rysuje; Ctrl+Z ją przywraca" + +msgid "This part can't be taken out: it has parts under it, or something else in the model points at it (an animation, a light slot, a skin)" +msgstr "Tej części nie da się usunąć: ma pod sobą inne części albo coś innego w modelu na nią wskazuje (animacja, slot świateł, skóra)" + +msgid "Removed {}. Ctrl+Z brings it back" +msgstr "Usunięto {}. Ctrl+Z ją przywróci" diff --git a/model/eu07m/eu07m_build.cpp b/model/eu07m/eu07m_build.cpp index 880cebc0e..07a6e8010 100644 --- a/model/eu07m/eu07m_build.cpp +++ b/model/eu07m/eu07m_build.cpp @@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "model/eu07m/eu07m_build.h" +#include "model/eu07m/eu07m_store.h" #include #include @@ -19,6 +20,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include namespace eu07m { @@ -485,14 +487,34 @@ void shift_subtrees(model &Model, std::int32_t const Parent, std::uint32_t const } } -// something other than its own components points at the node: the geometry, a transform, a skin, a clip, the data of a component +// the mesh of the node, which no other node draws; none when it has none or shares it +std::optional own_mesh(model const &Model, std::uint32_t const Node) { + auto const mesh{Model.nodes[Node].mesh}; + if ((mesh < 0) || (static_cast(mesh) >= Model.meshes.size())) + return std::nullopt; + if (std::ranges::count(Model.nodes, mesh, &node_record::mesh) != 1) + return std::nullopt; + return static_cast(mesh); +} + +// the primitive is one of the mesh +bool in_mesh(model const &Model, std::optional const Mesh, std::uint32_t const Primitive) { + if (false == Mesh.has_value()) + return false; + auto const &mesh{Model.meshes[*Mesh]}; + return (Primitive >= mesh.primitive_first) && (std::size_t{Primitive} < std::size_t{mesh.primitive_first} + mesh.primitive_count); +} + +// something other than what goes with it points at the node: a primitive of another mesh, a skin, a clip, the data of a component, +// a vertex group of its mesh. its own transform, the primitives of its own mesh and its own components go with it bool node_referenced(model &Model, std::uint32_t const Node) { auto found{false}; auto const check = [Node, &found](std::uint32_t const &Index) { found |= (Index == Node); }; - for (auto const &transform : Model.transforms) - check(transform.node); - for (auto const &primitive : Model.primitives) - check(primitive.node); + auto const mesh{own_mesh(Model, Node)}; + for (std::uint32_t idx = 0; idx < Model.primitives.size(); ++idx) { + if (false == in_mesh(Model, mesh, idx)) + check(Model.primitives[idx].node); + } for (auto const &joint : Model.skin_joints) check(joint.node); for (auto const &track : Model.clip_tracks) @@ -500,6 +522,8 @@ bool node_referenced(model &Model, std::uint32_t const Node) { for (auto const &record : Model.components) visit_payload_nodes(Model, record, check); found |= std::ranges::any_of(Model.skins, [Node](skin_record const &Skin) { return Skin.root == static_cast(Node); }); + if (mesh) + found |= std::ranges::any_of(Model.vertex_groups, [&mesh](vertex_group_record const &Group) { return Group.mesh == *mesh; }); return found; } @@ -546,6 +570,77 @@ std::vector components_of(model const &Model, std::uint32_t const return components; } +// the range of each vertex stream the model has, Count elements from First, taken out +void cut_vertices(model &Model, std::size_t const First, std::size_t const Count) { + auto const vertices{Model.vertex_count()}; + auto const cut = [vertices, First, Count](auto &Stream) { + if ((Stream.size() == vertices) && (First + Count <= Stream.size())) + Stream.erase(Stream.begin() + static_cast(First), Stream.begin() + static_cast(First + Count)); + }; + // the positions last: the others are checked against their count + cut(Model.vertex_attributes); + cut(Model.weights); + cut(Model.weights_wide); + cut(Model.vertex_userdata); + cut(Model.bindings); + cut(Model.positions); +} + +// the vertices and the indices of the primitive taken out of the streams, when they are its own, and the other primitives after them +// moved back over them; shared ones stay +void cut_geometry(model &Model, std::uint32_t const Primitive) { + if (false == shareable(Model, Primitive)) + return; + auto const removed{Model.primitives[Primitive]}; + auto const vertex_end{std::size_t{removed.base_vertex} + removed.vertex_count}; + cut_vertices(Model, removed.base_vertex, removed.vertex_count); + auto const indexed{(removed.flags & primitive_flags::indexed) != 0}; + auto const wide{(removed.flags & primitive_flags::index32) != 0}; + auto const index_end{std::size_t{removed.first_index} + removed.index_count}; + if (indexed && wide && (index_end <= Model.indices32.size())) + Model.indices32.erase(Model.indices32.begin() + removed.first_index, Model.indices32.begin() + static_cast(index_end)); + else if (indexed && (false == wide) && (index_end <= Model.indices16.size())) + Model.indices16.erase(Model.indices16.begin() + removed.first_index, Model.indices16.begin() + static_cast(index_end)); + for (std::uint32_t idx = 0; idx < Model.primitives.size(); ++idx) { + auto &primitive{Model.primitives[idx]}; + if (idx == Primitive) + continue; + if (primitive.base_vertex >= vertex_end) + primitive.base_vertex -= removed.vertex_count; + auto const same_indices{indexed && ((primitive.flags & primitive_flags::indexed) != 0) && (((primitive.flags & primitive_flags::index32) != 0) == wide)}; + if (same_indices && (primitive.first_index >= index_end)) + primitive.first_index -= removed.index_count; + } +} + +// the primitive taken out with its geometry; the meshes and the references to the store after it close up +void erase_primitive(model &Model, std::uint32_t const Primitive) { + cut_geometry(Model, Primitive); + std::erase_if(Model.geometry_references, [Primitive](geometry_reference_record const &Reference) { return Reference.primitive == Primitive; }); + for (auto &reference : Model.geometry_references) { + if (reference.primitive > Primitive) + --reference.primitive; + } + Model.primitives.erase(Model.primitives.begin() + Primitive); + for (auto &mesh : Model.meshes) + close_range(mesh.primitive_first, mesh.primitive_count, Primitive); +} + +// the mesh taken out with its primitives; the meshes after it move back by one +void erase_mesh(model &Model, std::uint32_t const Mesh) { + while (Model.meshes[Mesh].primitive_count > 0) + erase_primitive(Model, Model.meshes[Mesh].primitive_first + Model.meshes[Mesh].primitive_count - 1); + Model.meshes.erase(Model.meshes.begin() + Mesh); + for (auto &node : Model.nodes) { + if (node.mesh > static_cast(Mesh)) + --node.mesh; + } + for (auto &group : Model.vertex_groups) { + if (group.mesh > Mesh) + --group.mesh; + } +} + } // namespace std::uint32_t insert_node(model &Model, std::int32_t const Parent, node_record Node) { @@ -568,7 +663,10 @@ std::uint32_t insert_node(model &Model, std::int32_t const Parent, node_record N } bool node_removable(model &Model, std::uint32_t const Node) { - if ((Node >= Model.nodes.size()) || (Model.nodes[Node].subtree_end != Node + 1) || (Model.nodes[Node].mesh >= 0) || node_referenced(Model, Node)) + if ((Node >= Model.nodes.size()) || (Model.nodes[Node].subtree_end != Node + 1) || node_referenced(Model, Node)) + return false; + // geometry which another node draws too stays, and with it the node + if ((Model.nodes[Node].mesh >= 0) && (false == own_mesh(Model, Node).has_value())) return false; auto const components{components_of(Model, Node)}; return std::ranges::none_of(components, [&Model](std::uint32_t const Component) { return component_referenced(Model, Component); }); @@ -581,6 +679,11 @@ bool remove_node(model &Model, std::uint32_t const Node) { auto const components{components_of(Model, Node)}; for (auto component{components.rbegin()}; component != components.rend(); ++component) erase_component(Model, *component); + // its own transform and its geometry go with it + std::erase_if(Model.transforms, [Node](transform_record const &Transform) { return Transform.node == Node; }); + if (auto const mesh{own_mesh(Model, Node)}) + erase_mesh(Model, *mesh); + Model.nodes[Node].mesh = -1; visit_node_references(Model, node_shift{Node, true}); for (auto &skin : Model.skins) { if (skin.root > static_cast(Node)) diff --git a/model/eu07m/eu07m_build.h b/model/eu07m/eu07m_build.h index c141fd01d..ab7139302 100644 --- a/model/eu07m/eu07m_build.h +++ b/model/eu07m/eu07m_build.h @@ -57,12 +57,14 @@ void compute_bounds(model &Model); // the subtrees, the transforms, the primitives, the skins, the tracks of the clips, the targets of the components and the nodes // in their data. the index of the names and the bounds, where the model has them, are built again. returns the index of the node std::uint32_t insert_node(model &Model, std::int32_t Parent, node_record Node); -// the node can be taken out: a leaf without geometry which nothing points at but its own components, and those components aren't -// pointed at by the data of others (the bindings of a channel, the light sources of a light slot) +// the node can be taken out: a leaf which nothing points at but what goes with it (its components, its transform, its own mesh), whose +// mesh no other node draws and has no vertex groups, and whose components aren't pointed at by the data of others (the bindings of a +// channel, the light sources of a light slot) bool node_removable(model &Model, std::uint32_t Node); -// takes out the node, which node_removable() allows, with its components; each reference to the nodes and the components after -// them moves back by one, the data of the components taken out stays unreferenced. the index of the names and the bounds, where -// the model has them, are built again. false, and nothing changed, when the node can't be taken out +// takes out the node, which node_removable() allows, with its components, its transform and its mesh with the primitives; their +// vertices and indices, where no other primitive shares them, leave the streams. each reference to the nodes, the meshes, the +// primitives, the components and the vertices after them moves back; the data of the components taken out stays unreferenced. the +// index of the names and the bounds, where the model has them, are built again. false, and nothing changed, when it can't be bool remove_node(model &Model, std::uint32_t Node); // appends a component of the type aimed at the target, with its data; returns its index. a node or a vertex group without // components lists it as its range diff --git a/tests/unit/test_eu07m_edit.cpp b/tests/unit/test_eu07m_edit.cpp index 9017dfcc1..aa83aff1b 100644 --- a/tests/unit/test_eu07m_edit.cpp +++ b/tests/unit/test_eu07m_edit.cpp @@ -157,3 +157,79 @@ TEST_CASE("eu07m: a node with children, or one something points at, isn't taken CHECK(listed_node(model, 0, 1) == 2); CHECK(eu07m::validate(model).empty()); } + +namespace { + +// root (0) { decal (1), lamp (2) }: the decal and the lamp have a triangle of their own each, the lamp's after the decal's in the +// streams; a vertex has a position and its attributes +eu07m::model geometry_model() { + eu07m::model model; + auto const node = [&model](std::string_view const Name, std::int32_t const Parent, std::uint32_t const End, std::int32_t const Mesh) { + eu07m::node_record record; + record.name = model.intern(Name); + record.parent = Parent; + record.subtree_end = End; + record.mesh = Mesh; + model.nodes.push_back(record); + }; + node("root", -1, 3, -1); + node("decal", 0, 2, 0); + node("lamp", 0, 3, 1); + for (std::uint32_t mesh = 0; mesh < 2; ++mesh) { + eu07m::mesh_record record; + record.primitive_first = mesh; + record.primitive_count = 1; + model.meshes.push_back(record); + eu07m::primitive_record primitive; + primitive.first_index = mesh * 3; + primitive.index_count = 3; + primitive.base_vertex = mesh * 3; + primitive.vertex_count = 3; + primitive.node = mesh + 1; + primitive.flags = static_cast(eu07m::topology::triangles) | eu07m::primitive_flags::indexed | eu07m::primitive_flags::opaque; + model.primitives.push_back(primitive); + for (std::uint16_t vertex = 0; vertex < 3; ++vertex) { + model.positions.push_back({static_cast(mesh), static_cast(vertex), 0.f}); + model.vertex_attributes.emplace_back(); + model.indices16.push_back(vertex); + } + } + eu07m::build_name_index(model); + return model; +} + +} // namespace + +TEST_CASE("eu07m: a part taken out takes its geometry, and the parts after it move into its place") { + auto model{geometry_model()}; + REQUIRE(eu07m::validate(model).empty()); + REQUIRE(eu07m::node_removable(model, 1)); + REQUIRE(eu07m::remove_node(model, 1)); + REQUIRE(model.nodes.size() == 2); + CHECK(model.string(model.nodes[1].name) == "lamp"); + CHECK(model.nodes[0].subtree_end == 2); + REQUIRE(model.meshes.size() == 1); + CHECK(model.nodes[1].mesh == 0); + REQUIRE(model.primitives.size() == 1); + CHECK(model.meshes[0].primitive_first == 0); + CHECK(model.primitives[0].base_vertex == 0); + CHECK(model.primitives[0].first_index == 0); + CHECK(model.primitives[0].node == 1); + // the lamp's vertices are the ones left + REQUIRE(model.positions.size() == 3); + CHECK(model.positions[0][0] == 1.f); + CHECK(model.vertex_attributes.size() == 3); + CHECK(model.indices16.size() == 3); + CHECK(eu07m::validate(model).empty()); + eu07m::model read; + REQUIRE(eu07m::read(eu07m::write(model), read).ok); + CHECK(read.primitives.size() == 1); +} + +TEST_CASE("eu07m: a part whose mesh another part draws too isn't taken out") { + auto model{geometry_model()}; + model.nodes[2].mesh = 0; + CHECK_FALSE(eu07m::node_removable(model, 1)); + CHECK_FALSE(eu07m::remove_node(model, 1)); + CHECK(model.primitives.size() == 2); +}