18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 07:07:05 +01:00
Files
maszyna/application/editoruimodel.cpp
T
maj00r 07b902d034 feat(editor): a window to see and edit the components of an EU07M model
Selecting a scenery model loaded from an .eu07m file opens the "EU07M
model" window: the tree of the nodes with their transforms and the
components aimed at each of them, then the components of the model.

- The fields of SOCK, JOIN, CHNL (with its bindings), LGHT, LSLT, EMIT,
  COLL and MREF are edited; the other components are shown by size.
- The clip flags are edited, and a time slider holds the instance at a
  pose (time stands still in the editor); TAnimModel::play_clip takes
  the second to start from.
- Channel sliders try the channels on the instance without changing
  the file.
- "Save the model" validates the model and writes the file through a
  temporary one; the family store is kept. "Read it again" drops the
  changes.

The geometry, the mapping and the materials aren't edited. The Polish
texts of the window are added to lang/editor_pl.po.
2026-11-29 15:18:35 +01:00

527 lines
19 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "application/editoruimodel.h"
#include "model/AnimModel.h"
#include "model/Model3d.h"
#include "model/eu07m/eu07m_dump.h"
#include "model/eu07m/eu07m_io.h"
#include "model/eu07m/eu07m_store.h"
#include "scene/scenenode.h"
#include "utilities/Globals.h"
#include "utilities/translation.h"
#include "utilities/utilities.h"
#include <array>
#include <cstring>
#include <filesystem>
#include <format>
#include <fstream>
namespace
{
namespace payload = eu07m::payload;
// the nodes of a model the panel shows open at once
constexpr std::size_t small_model{32};
// the name of a component type, its four characters
std::string fourcc_text(std::uint32_t const Type)
{
std::string result(4, ' ');
for (std::size_t idx = 0; idx < 4; ++idx)
{
result[idx] = static_cast<char>((Type >> (8 * idx)) & 0xFF);
}
return result;
}
// a string of the model edited by its text; an empty text is no string
bool edit_string(char const *Label, eu07m::model &Model, std::uint32_t &Id)
{
std::array<char, 128> buffer{};
auto const text{Model.string(Id)};
std::memcpy(buffer.data(), text.data(), std::min(text.size(), buffer.size() - 1));
ImGui::InputText(Label, buffer.data(), buffer.size());
if (false == ImGui::IsItemDeactivatedAfterEdit())
{
return false;
}
std::string_view const edited{buffer.data()};
Id = edited.empty() ? eu07m::no_index : Model.intern(edited);
return true;
}
// an enumeration of the format picked from the names of its values
template <typename Enum_> bool edit_enum(char const *Label, Enum_ &Value, std::initializer_list<char const *> Names)
{
std::vector<char const *> const names{Names};
auto index{static_cast<int>(Value)};
if (false == ImGui::Combo(Label, &index, names.data(), static_cast<int>(names.size())))
{
return false;
}
Value = static_cast<Enum_>(index);
return true;
}
bool edit_flag(char const *Label, std::uint8_t &Flags, std::uint8_t const Bit)
{
auto set{(Flags & Bit) != 0};
if (false == ImGui::Checkbox(Label, &set))
{
return false;
}
Flags = static_cast<std::uint8_t>(set ? (Flags | Bit) : (Flags & ~Bit));
return true;
}
// a rotation as a quaternion x y z w, kept of unit length
bool edit_rotation(char const *Label, std::array<float, 4> &Rotation)
{
if (false == ImGui::DragFloat4(Label, Rotation.data(), 0.01f, -1.f, 1.f, "%.3f"))
{
return false;
}
auto const length{std::sqrt(Rotation[0] * Rotation[0] + Rotation[1] * Rotation[1] + Rotation[2] * Rotation[2] + Rotation[3] * Rotation[3])};
if (length > 0.f)
{
for (auto &component : Rotation)
{
component /= length;
}
}
return true;
}
// the payload of the component at Offset of its data, edited by Edit; written back when it changed
template <typename Payload_, typename Edit_> bool edit_payload(eu07m::model &Model, std::uint32_t const Index, std::size_t const Offset, Edit_ &&Edit)
{
auto const &record{Model.components[Index]};
auto const at{std::size_t{record.data_offset} + Offset};
if ((Offset + sizeof(Payload_) > record.data_size) || (at + sizeof(Payload_) > Model.component_data.size()))
{
ImGui::TextDisabled("%s", STR_C("The data of the component is cut short"));
return false;
}
Payload_ value;
std::memcpy(&value, Model.component_data.data() + at, sizeof(value));
if (false == Edit(value))
{
return false;
}
std::memcpy(Model.component_data.data() + at, &value, sizeof(value));
return true;
}
bool edit_socket(eu07m::model &Model, payload::socket &Socket)
{
auto changed{edit_string(STR_C("Role"), Model, Socket.role)};
changed |= edit_enum(STR_C("End of the vehicle"), Socket.end, {STR_C("none"), STR_C("front"), STR_C("rear")});
changed |= ImGui::InputScalar(STR_C("Index"), ImGuiDataType_U16, &Socket.index);
changed |= ImGui::DragFloat3(STR_C("Position"), Socket.position.data(), 0.001f, 0.f, 0.f, "%.4f");
changed |= edit_rotation(STR_C("Rotation"), Socket.rotation);
return changed;
}
bool edit_joint(eu07m::model &Model, payload::joint &Joint)
{
auto changed{edit_enum(STR_C("Kind"), Joint.kind, {STR_C("fixed"), STR_C("hinge"), STR_C("ball"), STR_C("slider"), STR_C("screw")})};
auto limited{Joint.limited != 0};
if (ImGui::Checkbox(STR_C("Limited"), &limited))
{
Joint.limited = limited ? 1 : 0;
changed = true;
}
changed |= edit_string(STR_C("Role"), Model, Joint.role);
changed |= ImGui::DragFloat3(STR_C("Axis"), Joint.axis.data(), 0.01f, -1.f, 1.f, "%.3f");
changed |= ImGui::DragFloat(STR_C("Minimum"), &Joint.minimum, 0.01f);
changed |= ImGui::DragFloat(STR_C("Maximum"), &Joint.maximum, 0.01f);
changed |= ImGui::DragFloat(STR_C("Rest"), &Joint.rest, 0.01f);
if (Joint.kind == payload::joint_kind::screw)
{
changed |= ImGui::DragFloat(STR_C("Pitch, m a turn"), &Joint.pitch, 0.0005f, 0.f, 1.f, "%.4f");
}
return changed;
}
bool edit_channel(eu07m::model &Model, payload::channel &Channel)
{
auto changed{edit_string(STR_C("Name"), Model, Channel.name)};
changed |= ImGui::DragFloat(STR_C("Minimum"), &Channel.minimum, 0.01f);
changed |= ImGui::DragFloat(STR_C("Maximum"), &Channel.maximum, 0.01f);
changed |= ImGui::DragFloat(STR_C("Initial value"), &Channel.initial, 0.01f);
return changed;
}
bool edit_binding(payload::channel_binding &Binding)
{
auto changed{edit_enum(STR_C("Bound to"), Binding.kind, {STR_C("joint"), STR_C("clip"), STR_C("variants"), STR_C("light slot")})};
auto target{Binding.target == eu07m::no_index ? -1 : static_cast<int>(Binding.target)};
if (ImGui::InputInt(STR_C("Target (component or clip, -1 none)"), &target))
{
Binding.target = target < 0 ? eu07m::no_index : static_cast<std::uint32_t>(target);
changed = true;
}
changed |= ImGui::DragFloat(STR_C("Scale"), &Binding.scale, 0.01f);
changed |= ImGui::DragFloat(STR_C("Offset"), &Binding.offset, 0.01f);
return changed;
}
bool edit_light(payload::light &Light)
{
auto changed{edit_enum(STR_C("Kind"), Light.kind, {STR_C("point"), STR_C("spot")})};
changed |= edit_flag(STR_C("Lights itself"), Light.flags, payload::light_flags::shade_self);
changed |= edit_flag(STR_C("No shadows"), Light.flags, payload::light_flags::no_shadows);
changed |= edit_flag(STR_C("Soft falloff"), Light.flags, payload::light_flags::soft_falloff);
changed |= edit_flag(STR_C("Light only"), Light.flags, payload::light_flags::light_only);
changed |= ImGui::ColorEdit4(STR_C("Colour"), Light.color.data());
changed |= ImGui::DragFloat(STR_C("Intensity"), &Light.hotspot_power, 0.01f, 0.f, 100.f);
changed |= ImGui::DragFloat(STR_C("Radius"), &Light.radius, 0.1f, 0.f, 10000.f);
changed |= ImGui::DragFloat(STR_C("Cosine of the cone"), &Light.cos_falloff, 0.005f, -1.f, 1.f);
changed |= ImGui::DragFloat(STR_C("Cosine of the hotspot"), &Light.cos_hotspot, 0.005f, -1.f, 1.f);
return changed;
}
bool edit_light_slot(payload::light_slot &Slot)
{
auto changed{ImGui::InputScalar(STR_C("Slot"), ImGuiDataType_U16, &Slot.slot)};
changed |= ImGui::DragFloat(STR_C("Initial state"), &Slot.initial, 0.01f, 0.f, 10.f);
changed |= ImGui::ColorEdit3(STR_C("Colour (negative: of the sources)"), Slot.color.data(), ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR);
return changed;
}
bool edit_emitter(payload::emitter &Emitter)
{
auto changed{edit_enum(STR_C("Kind"), Emitter.kind, {STR_C("smoke"), STR_C("steam"), STR_C("sparks")})};
changed |= ImGui::DragFloat3(STR_C("Direction"), Emitter.direction.data(), 0.01f, -1.f, 1.f, "%.3f");
return changed;
}
bool edit_collision(eu07m::model &Model, payload::collision &Collision)
{
auto changed{edit_enum(STR_C("Shape"), Collision.kind, {STR_C("box"), STR_C("sphere"), STR_C("capsule")})};
changed |= edit_string(STR_C("Role"), Model, Collision.role);
changed |= ImGui::DragFloat3(STR_C("Centre"), Collision.centre.data(), 0.001f, 0.f, 0.f, "%.4f");
changed |= edit_rotation(STR_C("Rotation"), Collision.rotation);
changed |= ImGui::DragFloat3(STR_C("Half extents"), Collision.size.data(), 0.001f, 0.f, 1000.f, "%.4f");
return changed;
}
bool edit_reference(eu07m::model &Model, payload::model_reference &Reference)
{
auto changed{edit_string(STR_C("Model path"), Model, Reference.path)};
changed |= edit_string(STR_C("Skin"), Model, Reference.skin);
return changed;
}
// the components the panel edits field by field; the others are shown by their size
bool edit_component(eu07m::model &Model, std::uint32_t const Index)
{
namespace component = eu07m::component;
auto const type{Model.components[Index].type};
if (type == component::socket)
return edit_payload<payload::socket>(Model, Index, 0, [&Model](payload::socket &Value) { return edit_socket(Model, Value); });
if (type == component::joint)
return edit_payload<payload::joint>(Model, Index, 0, [&Model](payload::joint &Value) { return edit_joint(Model, Value); });
if (type == component::light)
return edit_payload<payload::light>(Model, Index, 0, [](payload::light &Value) { return edit_light(Value); });
if (type == component::light_slot)
return edit_payload<payload::light_slot>(Model, Index, 0, [](payload::light_slot &Value) { return edit_light_slot(Value); });
if (type == component::emitter)
return edit_payload<payload::emitter>(Model, Index, 0, [](payload::emitter &Value) { return edit_emitter(Value); });
if (type == component::collision)
return edit_payload<payload::collision>(Model, Index, 0, [&Model](payload::collision &Value) { return edit_collision(Model, Value); });
if (type == component::model_reference)
return edit_payload<payload::model_reference>(Model, Index, 0, [&Model](payload::model_reference &Value) { return edit_reference(Model, Value); });
if (type != component::channel)
{
ImGui::TextDisabled("%s", std::vformat(STR("{} bytes, not edited here"), std::make_format_args(Model.components[Index].data_size)).c_str());
return false;
}
payload::channel channel;
auto changed{edit_payload<payload::channel>(Model, Index, 0, [&Model, &channel](payload::channel &Value) {
auto const result{edit_channel(Model, Value)};
channel = Value;
return result;
})};
for (std::uint32_t binding = 0; binding < channel.binding_count; ++binding)
{
ImGui::PushID(static_cast<int>(binding));
ImGui::SeparatorText(std::vformat(STR("Binding {}"), std::make_format_args(binding)).c_str());
changed |= edit_payload<payload::channel_binding>(Model, Index, sizeof(payload::channel) + binding * sizeof(payload::channel_binding), [](payload::channel_binding &Value) { return edit_binding(Value); });
ImGui::PopID();
}
return changed;
}
} // namespace
bool eu07m_model_panel::load(TAnimModel &Instance)
{
m_instance = &Instance;
m_path.clear();
m_model = eu07m::model{};
m_dirty = false;
m_message.clear();
m_open = true;
if ((Instance.pModel == nullptr) || (false == Global.modelformat_eu07m))
{
return false;
}
auto const path{FileCased(Instance.pModel->NameGet() + ".eu07m")};
if (path.empty())
{
return false;
}
std::ifstream file{path, std::ios::binary | std::ios::ate};
std::vector<std::byte> data(file.good() ? static_cast<std::size_t>(file.tellg()) : 0);
file.seekg(0);
file.read(reinterpret_cast<char *>(data.data()), static_cast<std::streamsize>(data.size()));
eu07m::read_options options;
options.blocks = eu07m::store_source(eu07m::family_store(path));
if (auto const result{eu07m::read(data, m_model, options)}; false == result.ok)
{
m_message = result.error;
return false;
}
m_path = path;
m_cliptimes.assign(m_model.clips.size(), 0.f);
m_channels.clear();
for (auto const &channel : Instance.pModel->channels())
{
m_channels.push_back(channel.initial);
}
return true;
}
void eu07m_model_panel::render(scene::basic_node *Node)
{
auto *instance{dynamic_cast<TAnimModel *>(Node)};
if (instance == nullptr)
{
m_instance = nullptr;
return;
}
if (instance != m_instance)
{
load(*instance);
}
if (m_path.empty() || (false == m_open))
{
return;
}
// beside the inspector, over the right of the view, and opaque: the settings of the interface may make the windows see-through
auto const *viewport{ImGui::GetMainViewport()};
ImGui::SetNextWindowPos(ImVec2(viewport->WorkPos.x + viewport->WorkSize.x - 900.f, viewport->WorkPos.y + 120.f), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(440, 560), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowBgAlpha(0.97f);
if (ImGui::Begin((std::string(STR_C("EU07M model")) + "###eu07mmodel").c_str(), &m_open, ImGuiWindowFlags_NoFocusOnAppearing))
{
render_file();
if (ImGui::CollapsingHeader(STR_C("Nodes and their components"), ImGuiTreeNodeFlags_DefaultOpen))
{
for (std::uint32_t idx = 0; idx < m_model.nodes.size(); idx = m_model.nodes[idx].subtree_end)
{
render_node(idx);
}
}
render_model_components();
render_clips(*instance);
render_channels(*instance);
}
ImGui::End();
}
void eu07m_model_panel::render_file()
{
ImGui::TextWrapped("%s", m_path.c_str());
auto const nodes{m_model.nodes.size()};
auto const components{m_model.components.size()};
auto const clips{m_model.clips.size()};
auto const vertices{m_model.vertex_count()};
ImGui::TextDisabled("%s", std::vformat(STR("{} nodes, {} components, {} clips, {} vertices"), std::make_format_args(nodes, components, clips, vertices)).c_str());
ImGui::BeginDisabled(false == m_dirty);
if (ImGui::Button(STR_C("Save the model")))
{
save();
}
ImGui::EndDisabled();
ImGui::SameLine();
if (ImGui::Button(STR_C("Read it again")) && (m_instance != nullptr))
{
load(*const_cast<TAnimModel *>(m_instance));
}
if (false == m_message.empty())
{
ImGui::TextWrapped("%s", m_message.c_str());
}
}
void eu07m_model_panel::render_node(std::uint32_t const Index)
{
auto const &node{m_model.nodes[Index]};
auto const name{m_model.string(node.name)};
// the components aimed at the node, whether its range lists them or not
std::vector<std::uint32_t> components;
for (std::uint32_t idx = 0; idx < m_model.components.size(); ++idx)
{
if ((m_model.components[idx].kind == eu07m::target_kind::node) && (m_model.components[idx].target == Index))
{
components.push_back(idx);
}
}
auto const leaf{(node.subtree_end == Index + 1) && components.empty()};
auto label{name.empty() ? std::string{STR_C("(no name)")} : std::string{name}};
if (false == components.empty())
{
auto const count{components.size()};
label += std::vformat(STR(" [{} components]"), std::make_format_args(count));
}
label += "##node" + std::to_string(Index);
// a small model shows its whole tree at once
auto const open{m_model.nodes.size() <= small_model ? ImGuiTreeNodeFlags_DefaultOpen : ImGuiTreeNodeFlags_None};
if (false == ImGui::TreeNodeEx(label.c_str(), leaf ? ImGuiTreeNodeFlags_Leaf : open))
{
return;
}
ImGui::TextDisabled("t %.3f %.3f %.3f r %.3f %.3f %.3f %.3f s %.3f %.3f %.3f", node.translation[0], node.translation[1], node.translation[2], node.rotation[0], node.rotation[1],
node.rotation[2], node.rotation[3], node.scale[0], node.scale[1], node.scale[2]);
for (auto const component : components)
{
render_component(component);
}
for (auto child = Index + 1; child < node.subtree_end; child = m_model.nodes[child].subtree_end)
{
render_node(child);
}
ImGui::TreePop();
}
void eu07m_model_panel::render_component(std::uint32_t const Index)
{
auto const label{std::format("{} [{}]##component{}", fourcc_text(m_model.components[Index].type), Index, Index)};
auto const open{m_model.components.size() <= small_model / 4 ? ImGuiTreeNodeFlags_DefaultOpen : ImGuiTreeNodeFlags_None};
if (false == ImGui::TreeNodeEx(label.c_str(), open))
{
return;
}
ImGui::PushID(static_cast<int>(Index));
if (edit_component(m_model, Index))
{
m_dirty = true;
}
ImGui::PopID();
ImGui::TreePop();
}
void eu07m_model_panel::render_model_components()
{
auto const count{std::ranges::count(m_model.components, eu07m::target_kind::model, &eu07m::component_record::kind)};
if (count == 0)
{
return;
}
if (ImGui::CollapsingHeader(STR_C("Components of the model"), m_model.nodes.size() <= small_model ? ImGuiTreeNodeFlags_DefaultOpen : ImGuiTreeNodeFlags_None))
{
for (std::uint32_t idx = 0; idx < m_model.components.size(); ++idx)
{
if (m_model.components[idx].kind == eu07m::target_kind::model)
{
render_component(idx);
}
}
}
}
void eu07m_model_panel::render_clips(TAnimModel &Instance)
{
if (m_model.clips.empty() || (false == ImGui::CollapsingHeader(STR_C("Animation clips"))))
{
return;
}
for (std::size_t idx = 0; idx < m_model.clips.size(); ++idx)
{
auto &clip{m_model.clips[idx]};
ImGui::PushID(static_cast<int>(idx));
auto const name{std::string{m_model.string(clip.name)}};
ImGui::SeparatorText(name.c_str());
auto looped{(clip.flags & eu07m::clip_flags::loop) != 0};
auto autoplay{(clip.flags & eu07m::clip_flags::autoplay) != 0};
if (ImGui::Checkbox(STR_C("Looped"), &looped) | ImGui::Checkbox(STR_C("Plays on its own"), &autoplay))
{
clip.flags = (looped ? eu07m::clip_flags::loop : 0u) | (autoplay ? eu07m::clip_flags::autoplay : 0u);
m_dirty = true;
}
// the editor stops the time: the clip is held at the time picked
if (ImGui::SliderFloat(STR_C("Time, s"), &m_cliptimes[idx], 0.f, std::max(clip.duration, 0.001f), "%.2f"))
{
Instance.play_clip(name, 1.f, m_cliptimes[idx]);
}
ImGui::PopID();
}
if (ImGui::Button(STR_C("Stop the clips")))
{
Instance.stop_clips();
}
}
void eu07m_model_panel::render_channels(TAnimModel &Instance)
{
if ((Instance.pModel == nullptr) || Instance.pModel->channels().empty() || (false == ImGui::CollapsingHeader(STR_C("Channels"))))
{
return;
}
auto const &channels{Instance.pModel->channels()};
m_channels.resize(channels.size(), 0.f);
for (std::size_t idx = 0; idx < channels.size(); ++idx)
{
auto const &channel{channels[idx]};
ImGui::PushID(static_cast<int>(idx));
if (ImGui::SliderFloat(channel.name.c_str(), &m_channels[idx], std::min(channel.minimum, channel.maximum), std::max(channel.minimum, channel.maximum), "%.3f"))
{
Instance.set_channel(channel.name, m_channels[idx]);
}
ImGui::PopID();
}
ImGui::TextDisabled("%s", STR_C("The channels and the clips are tried on the instance; the model as the file had it when it was loaded"));
}
void eu07m_model_panel::save()
{
if (auto const faults{eu07m::validate(m_model, 1)}; false == faults.empty())
{
m_message = std::vformat(STR("Not saved: {}"), std::make_format_args(faults.front()));
return;
}
auto const data{eu07m::write(m_model)};
auto const temporary{m_path + ".tmp"};
{
std::ofstream file{temporary, std::ios::binary};
file.write(reinterpret_cast<char const *>(data.data()), static_cast<std::streamsize>(data.size()));
if (false == file.good())
{
m_message = STR("Not saved: the file can't be written");
return;
}
}
std::error_code error;
std::filesystem::rename(temporary, m_path, error);
if (error)
{
m_message = STR("Not saved: the file can't be written");
return;
}
m_dirty = false;
m_message = STR("Saved. The game uses the change once it loads the model again");
}