diff --git a/application/editormode.cpp b/application/editormode.cpp index a63604f60..8f6316371 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -16,6 +16,8 @@ http://mozilla.org/MPL/2.0/. #include #include #include "application/editormode.h" +#include "vehicle/DynObj.h" +#include "vehicle/Train.h" #include "application/editormodeshared.h" #include "application/editorgestures.h" #include "scene/scenenodegroups.h" @@ -566,6 +568,7 @@ editor_mode::editor_mode() { ui()->set_workspace_bank(editor_ui::workspace::models, [this]() { ui()->model_panel().render_bank(); }, STR("Model")); ui()->set_edit_model([this]() { choose_work_area(work_area::models); }); ui()->model_panel().place_model = [this](std::string const &File) { place_model_file(File); }; + ui()->model_panel().enter_cab = [this](TDynamicObject &Vehicle) { return enter_cab(Vehicle); }; ui()->model_panel().place_vehicle = [this]() { // the Trainset tool of the tracks: a click on a track, the vehicle picked, Place choose_work_area(work_area::tracks); @@ -1744,6 +1747,26 @@ bool editor_mode::update() update_road_tool(); draw_road_overlay(); // the roads and the terrain are fields of work, their tools are drawn in the tool options window in place of the edit mode ones + // the cab of the vehicle driven is drawn while it's edited in the field of work of the models, as from the seat of the driver + { + auto const &panel{ui()->model_panel()}; + auto const cabview{m_models_open && panel.shows_cab() && simulation::Train != nullptr && simulation::Train->Dynamic() == panel.vehicle()}; + if (cabview != m_cabview) + { + // the view from the cab has the vehicle as the owner of the camera (the sounds and the drops on the glass go by it), the + // free view none + m_cabview = cabview; + FreeFlyModeFlag = false == cabview; + if (false == cabview) + Camera.m_owner = nullptr; + else if (Camera.m_owner != panel.vehicle()) + { + Camera.m_owner = panel.vehicle(); + Camera.m_owneroffset = simulation::Train->pMechSittingPosition; + } + Global.pCamera = Camera; + } + } ui()->set_workspace([this]() { if (m_models_open) return editor_ui::workspace::models; @@ -5826,6 +5849,36 @@ void editor_mode::choose_edit_mode(nodebank_panel::edit_mode const Mode) ui()->set_mode(Mode); } +bool editor_mode::enter_cab(TDynamicObject &Vehicle) +{ + // the train of the vehicle, made as the simulation makes it when someone gets in: it loads the cab + auto *train{simulation::Trains.find(Vehicle.name())}; + if (train == nullptr) + { + train = new TTrain(); + if (false == train->Init(&Vehicle)) + { + delete train; + ui()->set_status(format(STR_C("The cab of %s couldn't be loaded, the log says why"), Vehicle.name().c_str()), true); + return false; + } + simulation::Trains.insert(train); + } + simulation::Train = train; + if ((Vehicle.mdKabina == nullptr) || (Vehicle.mdKabina == Vehicle.mdModel)) + return false; + // in the seat of the driver, looking ahead from the cab taken + auto const side{Vehicle.MoverParameters->CabOccupied < 0 ? -1.0 : 1.0}; + auto const look{Vehicle.VectorFront() * side}; + Camera.Pos = Vehicle.GetWorldPosition(train->pMechSittingPosition); + Camera.Angle = glm::vec3(0.0f, static_cast(std::atan2(-look.x, -look.z)), 0.0f); + Camera.m_owner = &Vehicle; + Camera.m_owneroffset = train->pMechSittingPosition; + Global.pCamera = Camera; + ui()->set_status(format(STR_C("In the cab of %s; the editor draws it while the cab is edited"), Vehicle.name().c_str())); + return true; +} + void editor_mode::place_model_file(std::string const &File) { // on the ground some way ahead of the camera, as far as Insert would put it by a click there @@ -7549,6 +7602,9 @@ void editor_mode::on_scroll(double const Xoffset, double const Yoffset) void editor_mode::update_camera(double const Deltatime) { Camera.Update(); + // in the cab the camera moves with the vehicle, by its offset in it, as the camera of the driver does + if (m_cabview && Camera.m_owner != nullptr) + Camera.Pos = Camera.m_owner->GetWorldPosition(Camera.m_owneroffset); if (Global.EditorOrtho) { Camera.Angle.x = -M_PI_2; @@ -7642,6 +7698,7 @@ void editor_mode::ride_enter() void editor_mode::enter() { m_statebackup = {Global.pCamera, FreeFlyModeFlag, Global.ControlPicking}; + m_cabview = false; // the view from outside to start with, the cab is drawn again once it's edited // the editor changes the tracks, their sleepers are worked out as they're needed track_sleepers::lay_in_background(false); // the editor may remove the tracks and the vehicles the recording refers to: it ends here diff --git a/application/editormode.h b/application/editormode.h index 744fafe98..d87249484 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -73,6 +73,7 @@ class TAnimModel; // a tool of a field of work of the editor, a button in the toolbar and over the 3d view struct editor_tool_button; class TTrack; +class TDynamicObject; struct catenary_editor; // application/editormodecatenary.h @@ -873,6 +874,8 @@ class editor_mode : public application_mode, private editor_track::observer, pri bool m_terrain_open{false}; // the terrain is the field of work, chosen in the toolbar bool m_models_open{false}; // the models are the field of work, chosen in the toolbar void place_model_file(std::string const &File); // a model of the folder of the models put in the scenery and selected + bool enter_cab(TDynamicObject &Vehicle); // the cab of the vehicle loaded as by getting in, the camera in the seat of the driver + bool m_cabview{false}; // the cab of the vehicle driven is drawn, as from the seat of the driver float m_terrain_spacing{2.0f}; // point spacing of the chunks made and of the spacing tool float m_terrain_baseheight{0.0f}; // height of chunks made where there's no ground int m_terrain_add_count{4}; // chunks along a side added around the camera diff --git a/application/editormodeselftestscene.cpp b/application/editormodeselftestscene.cpp index 70f79fa1b..8f2d754ec 100644 --- a/application/editormodeselftestscene.cpp +++ b/application/editormodeselftestscene.cpp @@ -401,6 +401,16 @@ editor_mode::selftest_result editor_mode::selftest_scene(std::string const &comm WriteLog("SELFTEST modelplace " + file + (m_node != nullptr ? "" : ": not placed")); return selftest_result::handled; } + if (command == "modelvehicle") + { + // a vehicle of the name, its body or its cab ("cab") + std::string name; + std::string part; + words >> name >> part; + auto const picked{ui()->model_panel().pick_vehicle(name, part == "cab" ? 1 : 0)}; + WriteLog("SELFTEST modelvehicle " + name + " " + part + (picked ? "" : ": no such vehicle")); + return selftest_result::handled; + } if (command == "modelimport") { auto const imported{ui()->model_panel().import_model()}; diff --git a/application/editoruimodel.cpp b/application/editoruimodel.cpp index 06368faa0..ee16d2d53 100644 --- a/application/editoruimodel.cpp +++ b/application/editoruimodel.cpp @@ -1047,14 +1047,7 @@ void eu07m_model_panel::render_bank() ImGui::TextUnformatted(name.empty() ? STR_C("(no name)") : name.c_str()); if (m_target.vehicle != nullptr) { - ImGui::BeginDisabled(m_target.vehicle->mdKabina == nullptr); - std::array const parts{STR_C("Body"), STR_C("Cab")}; - if (auto const clicked{choice_buttons("##part", static_cast(m_target.part), parts)}) - { - // the other part is shown in the next frame - m_part = static_cast(*clicked); - } - ImGui::EndDisabled(); + render_vehicle_part(); } ImGui::TextDisabled("%s", m_path.empty() ? m_source->NameGet().c_str() : std::filesystem::path{m_path}.filename().string().c_str()); } @@ -1115,6 +1108,56 @@ void eu07m_model_panel::render_bank() ImGui::EndChild(); } +void eu07m_model_panel::render_vehicle_part() +{ + auto *vehicle{m_target.vehicle}; + auto const cab{(vehicle->mdKabina != nullptr) && (vehicle->mdKabina != vehicle->mdModel)}; + if (false == cab) + { + // the cab is loaded only by getting in: the button does what the game does then + if (ImGui::Button(STR_C("Get in the cab"))) + { + open_cab(); + } + if (ImGui::IsItemHovered()) + { + ImGui::SetTooltip("%s", STR_C("Loads the cab of the vehicle as the game does when you get in, and puts the camera in the seat of the driver")); + } + return; + } + std::array const parts{STR_C("Body"), STR_C("Cab")}; + if (auto const clicked{choice_buttons("##part", static_cast(m_target.part), parts)}) + { + // the other part is shown in the next frame; from the cab the camera goes out to look at the body + m_part = static_cast(*clicked); + if ((m_part == 1) && (enter_cab != nullptr)) + { + enter_cab(*vehicle); + } + else if (m_part == 0) + { + fly_to_vehicle(*vehicle); + } + } +} + +bool eu07m_model_panel::open_cab() +{ + auto *vehicle{m_target.vehicle != nullptr ? m_target.vehicle : m_pickedvehicle}; + if ((vehicle == nullptr) || (enter_cab == nullptr)) + { + return false; + } + m_pickedvehicle = vehicle; + if (false == enter_cab(*vehicle)) + { + m_message = STR("The vehicle has no cab of its own to get in"); + return false; + } + m_part = 1; + return true; +} + void eu07m_model_panel::render_insert() { if ((place_model == nullptr) || (false == ImGui::TreeNode(STR_C("Put a model in the scenery")))) @@ -1727,6 +1770,25 @@ bool eu07m_model_panel::editable(scene::basic_node const *Node) const return Global.modelformat_eu07m && (instance != nullptr) && (instance->pModel != nullptr) && (false == FileCased(instance->pModel->NameGet() + ".eu07m").empty()); } +bool eu07m_model_panel::pick_vehicle(std::string_view const Name, int const Part) +{ + auto const &vehicles{simulation::Vehicles.sequence()}; + auto const wanted_name{lowered(Name)}; + auto const found{std::ranges::find_if(vehicles, [&wanted_name](TDynamicObject const *Vehicle) { return lowered(Vehicle->name()) == wanted_name; })}; + if (found == vehicles.end()) + { + return false; + } + m_pickedvehicle = *found; + m_part = Part; + if (Part == 1) + { + return (enter_cab != nullptr) && enter_cab(**found); + } + fly_to_vehicle(**found); + return true; +} + bool eu07m_model_panel::select_node(std::string_view const Name) { auto const nodes{eu07m::find_nodes(m_model, Name)}; diff --git a/application/editoruimodel.h b/application/editoruimodel.h index 094d89fa2..abeb98f11 100644 --- a/application/editoruimodel.h +++ b/application/editoruimodel.h @@ -84,6 +84,12 @@ class eu07m_model_panel std::function place_model; // takes the editor to the tool which puts a vehicle on a track std::function place_vehicle; + // loads the cab of the vehicle the way the game does when someone gets in, and puts the camera in the seat of the driver; + // false when the vehicle has no cab of its own + std::function enter_cab; + // the cab of the vehicle shown is edited: the editor draws it, the camera may be in it + bool shows_cab() const { return m_active && (m_target.vehicle != nullptr) && (m_target.part == 1); } + TDynamicObject *vehicle() const { return m_target.vehicle; } // the model as a whole is moved with the gizmo of the editor, in place of its part picked bool moves_whole() const { return m_whole && (m_target.instance != nullptr); } void set_whole(bool Whole) { m_whole = Whole; } @@ -97,6 +103,10 @@ class eu07m_model_panel bool add_test_light(bool Spot); // the model shown, without a file of the format, given one made of it as the game has it; false when it can't be bool import_model(); + // the vehicle of the name picked, its body (0) or its cab (1), as a click in the list picks it (the self-test of the editor) + bool pick_vehicle(std::string_view Name, int Part); + // the cab of the vehicle shown, loaded when it isn't yet, as the button does + bool open_cab(); private: enum class gizmo : int @@ -143,6 +153,8 @@ class eu07m_model_panel void open(target const &Target); bool load(TModel3d &Model); void render_unsaved_popup(); + // the body or the cab of the vehicle, or the button which gets in its cab + void render_vehicle_part(); // the models of the folder of the models to put in the scenery void render_insert(); void render_header(); diff --git a/docs/eu07m-format.md b/docs/eu07m-format.md index 63735653f..e647b5188 100644 --- a/docs/eu07m-format.md +++ b/docs/eu07m-format.md @@ -298,6 +298,12 @@ the vehicle driven (its body or its cab), so a vehicle can be driven, the editor from the cab again after F11. "Edit this model (F7)" in the inspector of a scenery model loaded from an `.eu07m` file opens it there. A model without an `.eu07m` file, or one which can't be read, says so and isn't edited. +The cab of a vehicle is loaded only by getting in. When it isn't loaded yet, "Get in the cab" does what the simulation does then: it +makes the train of the vehicle (`TTrain::Init`, which loads the cab, its controls and its sounds) and makes it the vehicle driven, +and puts the camera in the seat of the driver. While the cab is edited the editor draws it as from the seat of the driver +(`FreeFlyModeFlag` off, the vehicle the owner of the camera, which moves in the cab with the vehicle); with the body picked, or +in another field of work, the view is free again. + - **A model without a file of the format.** "Import to EU07M" writes `.eu07m` beside its `.e3d` (or its `.t3d`), made of the model as the game loaded it, as `eu07 -eu07m convert` makes it (`eu07m::import_model`); for a vehicle, its body or its cab, that is what the game writes to the `.e3d` it makes of a `.t3d`. From then on the game loads the model from it, in every diff --git a/lang/editor_pl.po b/lang/editor_pl.po index 845bbc18d..f63b96a21 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -17057,3 +17057,18 @@ msgstr "Cały model" msgid "The gizmo moves or turns the whole model in the scenery, not its part; the change is one of the scenery, saved with it" msgstr "Uchwyt przesuwa albo obraca cały model w scenerii, a nie jego część; to zmiana scenerii, zapisywana razem z nią" + +msgid "Get in the cab" +msgstr "Wejdź do kabiny" + +msgid "Loads the cab of the vehicle as the game does when you get in, and puts the camera in the seat of the driver" +msgstr "Wczytuje kabinę pojazdu tak jak gra, gdy do niego wsiadasz, i stawia kamerę na miejscu maszynisty" + +msgid "The vehicle has no cab of its own to get in" +msgstr "Pojazd nie ma własnej kabiny, do której można wejść" + +msgid "The cab of %s couldn't be loaded, the log says why" +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ę"