17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 02:49:19 +02:00

feat(editor): edit parameters of a placed include

This commit is contained in:
2026-10-03 00:37:17 +02:00
parent 968594c3e9
commit ced26d171f
16 changed files with 603 additions and 41 deletions

View File

@@ -675,10 +675,8 @@ void editor_mode::undo_last()
if (snap.instance != 0)
{
// include of a scenery template was placed or removed; either is taken back by flipping its removal
toggle_include(snap.instance);
restore_include(snap);
g_redo.push_back(std::move(snap));
m_instance = 0;
return;
}
@@ -766,9 +764,8 @@ void editor_mode::redo_last()
if (snap.instance != 0)
{
toggle_include(snap.instance);
restore_include(snap);
m_history.push_back(std::move(snap));
m_instance = 0;
return;
}
@@ -926,8 +923,27 @@ bool editor_mode::update()
}
if (m_instance != 0 && false == scene::Layers.removable(m_instance))
{
m_instance = 0;
select_include(0);
}
if (m_include.changed)
{
// a parameter of the selected include was changed in the node properties
m_include.changed = false;
apply_include();
}
if (false == m_include.active && false == m_gizmo_using)
{
m_include_gesture = false;
}
// models replaced after changes of includes are destroyed once the renderer can't have them among its pick candidates
for (auto &retired : m_retired)
{
if (--retired.second <= 0)
{
simulation::Instances.purge(retired.first);
}
}
m_retired.erase(std::remove_if(m_retired.begin(), m_retired.end(), [](std::pair<TAnimModel *, int> const &Retired) { return Retired.second <= 0; }), m_retired.end());
simulation::Region->update_sounds();
audio::renderer.update(Global.iPause ? 0.0 : deltarealtime);
@@ -1615,6 +1631,97 @@ void editor_mode::toggle_include(scene::instance_handle const Instance)
ui()->set_status("Include of \"" + *included.file + "\" " + (included.removed ? "removed." : "restored."));
}
void editor_mode::restore_include(EditorSnapshot &Snapshot)
{
if (Snapshot.serialized.empty())
{
// the include was placed or removed; either is taken back by flipping its removal
toggle_include(Snapshot.instance);
select_include(0);
return;
}
// parameters of the include were changed. the snapshot holds the directive to go back to, and leaves with the current one
auto current = scene::Layers.directive(Snapshot.instance);
if (current.empty() || scene::Layers.instance(Snapshot.instance).dead)
{
ui()->set_status("Include of \"" + Snapshot.node_name + "\" is gone from the scenery, the change can't be taken back.", true);
return;
}
set_include_directive(Snapshot.instance, Snapshot.serialized);
Snapshot.serialized = std::move(current);
select_include(scene::Layers.removable(Snapshot.instance) ? Snapshot.instance : 0);
}
void editor_mode::select_include(scene::instance_handle const Instance)
{
m_instance = (scene::Layers.tracked(Instance) ? Instance : 0);
m_include = include_selection{};
m_include_gesture = false;
if (m_instance == 0)
{
ui()->set_include(nullptr);
return;
}
auto const &included = scene::Layers.instance(m_instance);
m_include.instance = m_instance;
m_include.target = *included.file;
// NOTE: a template without a usable description is still presented, with its parameters unnamed
std::string error;
editor_includes::load(*included.file, m_include.info, error);
if (false == editor_includes::parse_directive(scene::Layers.directive(m_instance), m_include.target, m_include.values))
{
m_include.values.clear();
m_include.issue = "The directive of the include can't be read from the scenery file, its parameters can't be changed.";
}
// with a rotation or a scale in effect the parameters don't translate to a location in the scene in a single way,
// see place_include()
m_include.placement = (included.context.rotation == glm::vec3{0.f} && included.context.scale == glm::vec3{1.f});
ui()->set_include(&m_include);
}
void editor_mode::set_include_directive(scene::instance_handle const Instance, std::string const &Directive)
{
if (false == scene::Layers.modify(Instance, Directive))
return;
std::vector<TAnimModel *> retired;
simulation::State.rebuild_include(Instance, retired);
// the pick of the renderer is resolved a few frames after it's requested, and can still deliver one of these
for (auto *model : retired)
m_retired.emplace_back(model, 120);
}
void editor_mode::apply_include()
{
if (m_instance == 0 || false == m_include.issue.empty())
return;
for (auto const &value : m_include.values)
{
if (value == "end" || value.find_first_of("\"\r\n") != std::string::npos)
{
// either would cut the directive short
ui()->set_status("A parameter of an include can't be \"end\", or have a quotation mark in it.", true);
select_include(m_instance); // back to the values of the directive
return;
}
}
auto before = scene::Layers.directive(m_instance);
auto const directive = editor_includes::compose_directive(m_include.target, m_include.values);
if (directive == before)
return;
if (false == m_include_gesture)
{
// a single undo step for the whole drag
EditorSnapshot snap;
snap.instance = m_instance;
snap.node_name = *scene::Layers.instance(m_instance).file;
snap.serialized = std::move(before);
m_history.push_back(std::move(snap));
g_redo.clear();
m_include_gesture = true;
}
set_include_directive(m_instance, directive);
}
void editor_mode::place_include(std::string const &File, int const RotationMode, float const FixedRotation)
{
if (scene::Layers.empty())
@@ -1670,6 +1777,7 @@ void editor_mode::place_include(std::string const &File, int const RotationMode,
snap.node_name = File;
m_history.push_back(std::move(snap));
g_redo.clear();
select_include(instance);
ui()->set_status(
"\"" + info.name + "\" placed in layer \"" + scene::Layers.layer(layer).name + "\": " + std::to_string(preview.first) + " model(s) shown"
@@ -2097,7 +2205,15 @@ void editor_mode::render_gizmo_options()
if (m_gizmo_snap < 0.0f)
m_gizmo_snap = 0.0f;
}
if (!m_node)
if (m_instance != 0)
{
auto const located = editor_includes::parameter_with_role(m_include.info, "pos.x") != 0 || editor_includes::parameter_with_role(m_include.info, "pos.y") != 0 ||
editor_includes::parameter_with_role(m_include.info, "pos.z") != 0;
ImGui::TextDisabled("%s", false == located ? "Include selected, no position parameters described" :
false == m_include.placement ? "Include selected, no gizmo under rotate or scale" :
"Include selected, axes as its template allows");
}
else if (!m_node)
ImGui::TextDisabled("No node selected");
}
@@ -2110,7 +2226,7 @@ void editor_mode::render_gizmo()
return;
}
if (!m_node)
if (!m_node && m_instance == 0)
{
m_gizmo_using = false;
return;
@@ -2134,6 +2250,12 @@ void editor_mode::render_gizmo()
float const aspect = io.DisplaySize.y > 0.0f ? io.DisplaySize.x / io.DisplaySize.y : 1.0f;
glm::mat4 const projection = glm::perspective(fovy, aspect, 0.1f, 10000.0f);
if (!m_node)
{
render_include_gizmo(view, projection, camerapos);
return;
}
// rotation/scale are only meaningful for instanced models; other node types translate only
TAnimModel *model = dynamic_cast<TAnimModel *>(m_node);
@@ -2211,6 +2333,103 @@ void editor_mode::render_gizmo()
}
}
void editor_mode::render_include_gizmo(glm::mat4 const &View, glm::mat4 const &Projection, glm::dvec3 const &Camerapos)
{
m_gizmo_using = false;
if (false == m_include.placement || false == m_include.issue.empty())
return;
// parameters of the template the gizmo can drive, as indices of their values; -1 where the template has none
static char const *const positionroles[] = {"pos.x", "pos.y", "pos.z"};
static char const *const rotationroles[] = {"rot.x", "rot.y", "rot.z"};
auto const numeric_parameter = [this](char const *Role, double &Value) {
auto const index = editor_includes::parameter_with_role(m_include.info, Role) - 1;
if (index < 0 || index >= static_cast<int>(m_include.values.size()))
return -1;
auto const &text = m_include.values[index];
char *end = nullptr;
auto const number = std::strtod(text.c_str(), &end);
if (text.empty() || *end != '\0')
return -1;
Value = number;
return index;
};
int position[3], rotation[3];
double location[3] = {0.0, 0.0, 0.0}, angles[3] = {0.0, 0.0, 0.0};
auto positions = 0, rotations = 0;
for (auto axis = 0; axis < 3; ++axis)
{
position[axis] = numeric_parameter(positionroles[axis], location[axis]);
rotation[axis] = numeric_parameter(rotationroles[axis], angles[axis]);
positions += (position[axis] >= 0 ? 1 : 0);
rotations += (rotation[axis] >= 0 ? 1 : 0);
}
if (positions == 0)
return; // nothing tells where the include is
// only the axes the template has parameters for can be manipulated
auto operation = 0;
for (auto axis = 0; axis < 3; ++axis)
{
if (m_gizmo_op == gizmo_operation::translate && position[axis] >= 0)
operation |= (ImGuizmo::TRANSLATE_X << axis);
else if (m_gizmo_op == gizmo_operation::rotate && rotation[axis] >= 0)
operation |= (ImGuizmo::ROTATE_X << axis);
}
if (operation == 0)
return;
auto const translating = (m_gizmo_op == gizmo_operation::translate);
// the parameters are relative to the origin in effect at the directive
auto const &offset = scene::Layers.instance(m_instance).context.offset;
glm::vec3 const relativepos = glm::vec3(offset + glm::dvec3{location[0], location[1], location[2]} - Camerapos);
float const translation[3] = {relativepos.x, relativepos.y, relativepos.z};
float const rotationangles[3] = {static_cast<float>(angles[0]), static_cast<float>(angles[1]), static_cast<float>(angles[2])};
float const scale[3] = {1.0f, 1.0f, 1.0f};
glm::mat4 matrix(1.0f);
ImGuizmo::RecomposeMatrixFromComponents(translation, rotationangles, scale, glm::value_ptr(matrix));
// moving along the axes of the include would need all three of them
ImGuizmo::MODE const mode = (m_gizmo_local && (false == translating || positions == 3)) ? ImGuizmo::LOCAL : ImGuizmo::WORLD;
glm::vec3 const snapvalue(translating ? m_gizmo_snap : 5.0f);
float const *snap = Global.ctrlState && snapvalue.x > 0.0f ? glm::value_ptr(snapvalue) : nullptr;
ImGuizmo::Manipulate(glm::value_ptr(View), glm::value_ptr(Projection), static_cast<ImGuizmo::OPERATION>(operation), mode, glm::value_ptr(matrix), nullptr, snap);
if (false == ImGuizmo::IsUsing())
return;
m_gizmo_using = true;
float newtranslation[3], newrotation[3], newscale[3];
ImGuizmo::DecomposeMatrixToComponents(glm::value_ptr(matrix), newtranslation, newrotation, newscale);
if (translating)
{
glm::dvec3 const newlocation = Camerapos + glm::dvec3{newtranslation[0], newtranslation[1], newtranslation[2]} - offset;
for (auto axis = 0; axis < 3; ++axis)
{
if (position[axis] >= 0)
m_include.values[position[axis]] = editor_includes::number(newlocation[axis]);
}
}
else
{
if (rotations == 1)
{
// the decomposition spreads a turn of more than a quarter over all three angles;
// with a single axis to turn around the angle is taken straight from the matrix
newrotation[0] = glm::degrees(std::atan2(matrix[1][2], matrix[1][1]));
newrotation[1] = glm::degrees(std::atan2(-matrix[0][2], matrix[0][0]));
newrotation[2] = glm::degrees(std::atan2(matrix[0][1], matrix[0][0]));
}
for (auto axis = 0; axis < 3; ++axis)
{
if (rotation[axis] >= 0)
m_include.values[rotation[axis]] = editor_includes::number(clamp_circular(newrotation[axis]));
}
}
apply_include();
}
void editor_mode::update_camera(double const Deltatime)
{
Camera.Update();
@@ -2294,7 +2513,7 @@ void editor_mode::exit()
// drop selection so a stale/dangling node pointer isn't used on the next editor session
m_node = nullptr;
m_instance = 0;
select_include(0);
m_gizmo_using = false;
ui()->set_node(nullptr);
@@ -2400,7 +2619,7 @@ void editor_mode::on_key(int const Key, int const Scancode, int const Action, in
m_history.push_back(std::move(snap));
g_redo.clear();
ui()->set_status("Include of \"" + *scene::Layers.instance(m_instance).file + "\" removed, Ctrl+Z brings it back. Its directive is erased when the scenery is saved.");
m_instance = 0;
select_include(0);
break;
}
if (is_press(Action))
@@ -2526,7 +2745,7 @@ void editor_mode::on_mouse_button(int const Button, int const Action, int const
return;
m_node = nullptr;
m_instance = 0;
select_include(0);
// in a scenery opened for editing selection has its limits. nodes of locked layers are off limits.
// nodes defined by a scenery template can't be rewritten one by one on save, so a click on one
@@ -2536,8 +2755,8 @@ void editor_mode::on_mouse_button(int const Button, int const Action, int const
std::string reason;
if (node->from_template() && scene::Layers.removable(node->m_instance, &reason))
{
m_instance = node->m_instance;
ui()->set_status("Include of \"" + *scene::Layers.instance(m_instance).file + "\" selected. Del removes it from the scenery.");
select_include(node->m_instance);
ui()->set_status("Include of \"" + *scene::Layers.instance(m_instance).file + "\" selected. Its parameters are in the node properties, Del removes it from the scenery.");
node = nullptr;
}
else if (node->from_template() || false == scene::Layers.editable(node, &reason))

View File

@@ -129,6 +129,9 @@ class editor_mode : public application_mode
scene::basic_editor m_editor;
scene::basic_node *m_node{nullptr}; // currently selected scene node
scene::instance_handle m_instance{0}; // currently selected include of a scenery template; these are handled as a whole
include_selection m_include; // parameters of the selected include, changed with the gizmo and in the node properties
bool m_include_gesture{false}; // a change of the selected include is under way, and has its undo snapshot already
std::vector<std::pair<TAnimModel *, int>> m_retired; // models replaced after changes of includes, with the frames left until they're destroyed
bool m_takesnapshot{true}; // helper, hints whether snapshot of selected node(s) should be taken before modification
bool m_dragging = false;
glm::dvec3 oldPos; // world position of the last brush placement
@@ -173,6 +176,16 @@ class editor_mode : public application_mode
void place_include(std::string const &File, int RotationMode, float FixedRotation);
// undo and redo of placing or removing an include: brings removed include back, or removes it
void toggle_include(scene::instance_handle Instance);
// makes specified include the selected one, 0 clears the selection
void select_include(scene::instance_handle Instance);
// writes parameter values of the selected include to its directive, and shows the outcome in the scene
void apply_include();
// replaces directive of specified include, and the models shown for it
void set_include_directive(scene::instance_handle Instance, std::string const &Directive);
// undo and redo of an include snapshot
void restore_include(EditorSnapshot &Snapshot);
// gizmo for the selected include, limited to what the parameters of its template can express
void render_include_gizmo(glm::mat4 const &View, glm::mat4 const &Projection, glm::dvec3 const &Camerapos);
// exports the scenery in legacy (text) format, with layers hidden in the editor left out of the picture
void export_scenery();
// raises/lowers terrain under the cursor while the left mouse button is held in sculpt mode

View File

@@ -164,6 +164,11 @@ void editor_ui::set_node(scene::basic_node *Node)
m_node = Node;
}
void editor_ui::set_include(include_selection *Include)
{
m_itempropertiespanel.include(Include);
}
void editor_ui::add_node_template(const std::string &desc)
{
m_nodebankpanel.add_template(desc);

View File

@@ -29,6 +29,8 @@ class editor_ui : public ui_layer
// updates state of UI elements
void update() override;
void set_node(scene::basic_node *Node);
// include of a scenery template to show in the node properties in place of a node; nullptr if there's none
void set_include(include_selection *Include);
void add_node_template(const std::string &desc);
float rot_val();
bool rot_from_last();

View File

@@ -312,6 +312,11 @@ void itemproperties_panel::render()
void itemproperties_panel::render_body()
{
if (m_include != nullptr)
{
render_include();
return;
}
// header section
for (auto const &line : text_lines)
{
@@ -1187,6 +1192,74 @@ ImVec4 const status_errorcolor{1.0f, 0.45f, 0.4f, 1.0f};
} // namespace
void itemproperties_panel::render_include()
{
auto &include{*m_include};
include.active = false;
ImGui::Text("include: %s", Bezogonkow(include.target).c_str());
if (false == include.info.name.empty())
{
ImGui::TextDisabled("%s", include.info.name.c_str());
}
if (false == include.issue.empty())
{
ImGui::PushTextWrapPos(0.0f);
ImGui::TextColored(status_errorcolor, "%s", Bezogonkow(include.issue).c_str());
ImGui::PopTextWrapPos();
return;
}
if (include.values.empty())
{
ImGui::TextDisabled("The template takes no parameters.");
return;
}
ImGui::PushItemWidth(ImGui::GetFontSize() * 14.0f);
for (std::size_t idx = 0; idx < include.values.size(); ++idx)
{
auto const id{static_cast<int>(idx + 1)};
auto const lookup{std::find_if(std::begin(include.info.parameters), std::end(include.info.parameters), [=](include_parameter const &Parameter) { return Parameter.id == id; })};
auto const described{lookup != std::end(include.info.parameters)};
auto const role{described ? lookup->role : std::string{"free"}};
// the label from the description, or what the description tells about the parameter
auto const label{described && false == lookup->label.empty() ? lookup->label : role != "free" ? role : "(p" + std::to_string(id) + ")"};
auto &value{include.values[idx]};
ImGui::PushID(id);
char *end{nullptr};
auto number{std::strtod(value.c_str(), &end)};
if ((role.starts_with("pos.") || role.starts_with("rot.") || role == "number") && false == value.empty() && *end == '\0')
{
if (ImGui::DragScalar(label.c_str(), ImGuiDataType_Double, &number, role.starts_with("pos.") ? 0.05f : 0.5f, nullptr, nullptr, "%.3f"))
{
value = editor_includes::number(number);
include.changed = true;
}
}
else
{
// applied once the field is left, so the template isn't processed with a half-typed value
input_text(label.c_str(), value);
if (ImGui::IsItemDeactivatedAfterEdit())
{
include.changed = true;
}
}
include.active |= ImGui::IsItemActive();
if (ImGui::IsItemHovered())
{
ImGui::SetTooltip("(p%d), %s", id, role.c_str());
}
ImGui::PopID();
}
ImGui::PopItemWidth();
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_TextDisabled));
ImGui::TextWrapped("Models of the template follow the changes at once, the rest of it once the scenery is saved and loaded again.");
ImGui::PopStyleColor();
}
void layers_panel::report(std::string const &Status, bool const Error)
{
status = Status;

View File

@@ -29,6 +29,20 @@ struct item_properties {
changeable<glm::vec3> rotation {};
};
*/
// include of a scenery template selected in the editor, with the values its directive passes to the template.
// owned by the editor mode; the node properties let the user change the values
struct include_selection
{
scene::instance_handle instance{0};
std::string target; // name of the template, the way the directive spells it
include_info info; // description of the template
std::vector<std::string> values; // value of each parameter in turn
bool placement{false}; // the position and rotation parameters can be driven by the gizmo
std::string issue; // why the values can't be changed, if they can't
bool changed{false}; // set by the UI when a value was changed, cleared by the editor mode once the change is applied
bool active{false}; // the UI is in the middle of changing a value
};
class itemproperties_panel : public ui_panel
{
@@ -39,6 +53,11 @@ class itemproperties_panel : public ui_panel
// position/rotation/scale of TAnimModel nodes in place. Other node types
// are still treated as read-only.
void update(scene::basic_node *Node);
// binds include of a scenery template to the panel, shown in place of a node; nullptr unbinds
void include(include_selection *Include)
{
m_include = Include;
}
void render() override;
// panel content without the window, also drawn inside the toolset window
void render_body();
@@ -50,9 +69,12 @@ class itemproperties_panel : public ui_panel
// renders DragFloat3/DragScalarN widgets for position, rotation and scale
// of the currently bound TAnimModel; no-op for other node subclasses.
void render_transform_editor();
// renders widgets for the parameters of the bound include
void render_include();
// members
scene::basic_node *m_node{nullptr}; // scene node bound to the panel
include_selection *m_include{nullptr}; // include of a scenery template bound to the panel
scene::group_handle m_grouphandle{null_handle}; // scene group bound to the panel
std::string m_groupprefix;
std::vector<text_line> m_grouplines;