17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-01 16:05:35 +01:00

Merge remote-tracking branch 'origin/feat/switch-blades' into feat/renderers-cleanup

This commit is contained in:
2026-10-29 23:55:04 +01:00
145 changed files with 36946 additions and 3791 deletions

View File

@@ -23,7 +23,7 @@ bool ui::scenerylist_panel::on_key(int key, int action)
if (it == keyboard.keytonamemap.end())
return false;
if (Global.ctrlState && it->second == "delete")
if (Global.ctrlState && it->second == "delete" && selected_trainset != nullptr)
purge_selected_trainset();
return true;
@@ -117,7 +117,7 @@ void ui::scenerylist_panel::draw_launch_box()
{
ImGui::NextColumn();
ImGui::TextWrapped(selected_trainset->description.c_str());
ImGui::TextWrapped("%s", selected_trainset->description.c_str());
ImGui::Checkbox(STR_C("Trainset turned around"), &selected_trainset->reversed);
if (ImGui::IsItemHovered())
@@ -283,7 +283,8 @@ void ui::scenerylist_panel::draw_trainset(trainset_desc &trainset)
draw_droptarget(trainset, position++);
ImGui::SameLine(15.0f);
for (auto &dyn_desc : trainset.vehicles) {
for (std::size_t index = 0; index < trainset.vehicles.size(); ++index) {
auto &dyn_desc = trainset.vehicles[index];
deferred_image *mini = nullptr;
if (dyn_desc.skin && dyn_desc.skin->mini.get() != deferred_image::invalid_id)
@@ -345,16 +346,19 @@ void ui::scenerylist_panel::draw_droptarget(trainset_desc &trainset, int positio
if (payload) {
skin_set *ptr = *reinterpret_cast<skin_set **>(payload->Data);
std::shared_ptr<skin_set> skin;
for (auto &s : ptr->vehicle.lock()->matching_skinsets)
if (s.get() == ptr)
skin = s;
if (auto const vehicle = ptr->vehicle.lock())
for (auto &s : vehicle->matching_skinsets)
if (s.get() == ptr)
skin = s;
trainset.vehicles.emplace(trainset.vehicles.begin() + position);
dynamic_desc &desc = trainset.vehicles[position];
if (skin) {
trainset.vehicles.emplace(trainset.vehicles.begin() + position);
dynamic_desc &desc = trainset.vehicles[position];
desc.name = skin->skin + "_" + std::to_string((int)LocalRandom(0.0, 10000000.0));
desc.vehicle = skin->vehicle.lock();
desc.skin = skin;
desc.name = skin->skin + "_" + std::to_string((int)LocalRandom(0.0, 10000000.0));
desc.vehicle = skin->vehicle.lock();
desc.skin = skin;
}
}
payload = ImGui::AcceptDragDropPayload("vehicle_set");
@@ -368,7 +372,7 @@ void ui::scenerylist_panel::draw_droptarget(trainset_desc &trainset, int positio
if (moved->position < position)
offset = -1;
} else {
desc_copy.name = desc_copy.skin->skin + "_" + std::to_string((int)LocalRandom(0.0, 10000000.0));
desc_copy.name = (desc_copy.skin ? desc_copy.skin->skin : desc_copy.name) + "_" + std::to_string((int)LocalRandom(0.0, 10000000.0));
}
trainset.vehicles.insert(trainset.vehicles.begin() + position + offset, desc_copy);

View File

@@ -10,13 +10,21 @@ scenery_scanner::scenery_scanner(ui::vehicles_bank &bank)
void scenery_scanner::scan()
{
for (auto &f : std::filesystem::directory_iterator("scenery")) {
std::filesystem::path path(std::filesystem::relative(f.path(), "scenery/"));
std::error_code error;
for (std::filesystem::directory_iterator f{"scenery", std::filesystem::directory_options::skip_permission_denied, error}, end; f != end; f.increment(error)) {
std::filesystem::path path(std::filesystem::relative(f->path(), "scenery/", error));
std::string name;
try {
name = path.string();
}
catch (std::exception const &) {
continue;
}
if (*path.filename().string().begin() == '$')
if (name.empty() || path.filename().string().front() == '$')
continue;
if (path.string().ends_with(".scn"))
if (name.ends_with(".scn"))
scan_scn(path);
}

View File

@@ -11,15 +11,13 @@ http://mozilla.org/MPL/2.0/.
#include "launcher/scenery_wizard.h"
#include "application/application.h"
#include "editor/editorFormat.hpp"
#include "editor/editorGeoMap.hpp"
#include "editor/editorOrthophoto.hpp"
#include "editor/editorSettings.hpp"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/translation.h"
#include "rendering/renderer.h"
#include "stb/stb_image.h"
#include <algorithm>
#include <chrono>
#include <cmath>
@@ -32,61 +30,6 @@ http://mozilla.org/MPL/2.0/.
namespace
{
double constexpr kSemiMajor{6378137.0};
double constexpr kFlattening{1.0 / 298.257222101};
double constexpr kScale{0.9993};
double constexpr kFalseEasting{500000.0};
double constexpr kFalseNorthing{-5300000.0};
double constexpr kCentralMeridian{19.0};
glm::dvec2 const kPolandLow{140000.0, 120000.0};
glm::dvec2 const kPolandHigh{890000.0, 800000.0};
std::string const kMapService{"https://mapy.geoportal.gov.pl/wss/ext/OSM/BaseMap/service"};
double seconds()
{
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
glm::dvec2 to_puwg(double const Latitude, double const Longitude)
{
auto const e2{2.0 * kFlattening - kFlattening * kFlattening};
auto const ep2{e2 / (1.0 - e2)};
auto const phi{glm::radians(Latitude)};
auto const sine{std::sin(phi)};
auto const cosine{std::cos(phi)};
auto const n{kSemiMajor / std::sqrt(1.0 - e2 * sine * sine)};
auto const t{std::tan(phi) * std::tan(phi)};
auto const c{ep2 * cosine * cosine};
auto const a{glm::radians(Longitude - kCentralMeridian) * cosine};
auto const m{kSemiMajor * ((1.0 - e2 / 4.0 - 3.0 * e2 * e2 / 64.0 - 5.0 * e2 * e2 * e2 / 256.0) * phi - (3.0 * e2 / 8.0 + 3.0 * e2 * e2 / 32.0 + 45.0 * e2 * e2 * e2 / 1024.0) * std::sin(2.0 * phi) +
(15.0 * e2 * e2 / 256.0 + 45.0 * e2 * e2 * e2 / 1024.0) * std::sin(4.0 * phi) - (35.0 * e2 * e2 * e2 / 3072.0) * std::sin(6.0 * phi))};
auto const x{kScale * n * (a + (1.0 - t + c) * std::pow(a, 3) / 6.0 + (5.0 - 18.0 * t + t * t + 72.0 * c - 58.0 * ep2) * std::pow(a, 5) / 120.0)};
auto const y{kScale * (m + n * std::tan(phi) * (a * a / 2.0 + (5.0 - t + 9.0 * c + 4.0 * c * c) * std::pow(a, 4) / 24.0 + (61.0 - 58.0 * t + t * t + 600.0 * c - 330.0 * ep2) * std::pow(a, 6) / 720.0))};
return {kFalseEasting + x, y + kFalseNorthing};
}
glm::dvec2 to_degrees(glm::dvec2 const &Point)
{
auto const e2{2.0 * kFlattening - kFlattening * kFlattening};
auto const ep2{e2 / (1.0 - e2)};
auto const x{Point.x - kFalseEasting};
auto const m{(Point.y - kFalseNorthing) / kScale};
auto const mu{m / (kSemiMajor * (1.0 - e2 / 4.0 - 3.0 * e2 * e2 / 64.0 - 5.0 * e2 * e2 * e2 / 256.0))};
auto const e1{(1.0 - std::sqrt(1.0 - e2)) / (1.0 + std::sqrt(1.0 - e2))};
auto const phi1{mu + (3.0 * e1 / 2.0 - 27.0 * std::pow(e1, 3) / 32.0) * std::sin(2.0 * mu) + (21.0 * e1 * e1 / 16.0 - 55.0 * std::pow(e1, 4) / 32.0) * std::sin(4.0 * mu) +
(151.0 * std::pow(e1, 3) / 96.0) * std::sin(6.0 * mu) + (1097.0 * std::pow(e1, 4) / 512.0) * std::sin(8.0 * mu)};
auto const sine{std::sin(phi1)};
auto const c1{ep2 * std::cos(phi1) * std::cos(phi1)};
auto const t1{std::tan(phi1) * std::tan(phi1)};
auto const n1{kSemiMajor / std::sqrt(1.0 - e2 * sine * sine)};
auto const r1{kSemiMajor * (1.0 - e2) / std::pow(1.0 - e2 * sine * sine, 1.5)};
auto const d{x / (n1 * kScale)};
auto const phi{phi1 - (n1 * std::tan(phi1) / r1) * (d * d / 2.0 - (5.0 + 3.0 * t1 + 10.0 * c1 - 4.0 * c1 * c1 - 9.0 * ep2) * std::pow(d, 4) / 24.0 +
(61.0 + 90.0 * t1 + 298.0 * c1 + 45.0 * t1 * t1 - 252.0 * ep2 - 3.0 * c1 * c1) * std::pow(d, 6) / 720.0)};
auto const lambda{(d - (1.0 + 2.0 * t1 + c1) * std::pow(d, 3) / 6.0 + (5.0 - 2.0 * c1 + 28.0 * t1 - 3.0 * c1 * c1 + 8.0 * ep2 + 24.0 * t1 * t1) * std::pow(d, 5) / 120.0) / std::cos(phi1)};
return {glm::degrees(phi), kCentralMeridian + glm::degrees(lambda)};
}
bool valid_name(std::string const &Name)
{
return false == Name.empty() && std::all_of(Name.begin(), Name.end(), [](char const Character) { return (Character >= 'a' && Character <= 'z') || (Character >= '0' && Character <= '9') || Character == '_' || Character == '-'; });
@@ -96,22 +39,16 @@ bool valid_name(std::string const &Name)
ui::scenerywizard_panel::scenerywizard_panel() : ui_panel(STR("New scenery"), false) {}
ui::scenerywizard_panel::~scenerywizard_panel()
{
if (m_request.valid())
m_request.wait();
if ((m_texture != 0) && (GfxRenderer != nullptr) && (GfxRenderer->GetImguiRenderer() != nullptr))
GfxRenderer->GetImguiRenderer()->Release_Image(m_texture);
}
ui::scenerywizard_panel::~scenerywizard_panel() = default;
void ui::scenerywizard_panel::render_contents()
{
upload_map();
auto const available{ImGui::GetContentRegionAvail()};
auto const form{360.f * Global.ui_scale};
glm::vec2 const map{std::max(200.f, available.x - form - ImGui::GetStyle().ItemSpacing.x), std::max(200.f, available.y)};
ImGui::BeginChild("##wizardmap", ImVec2(map.x, map.y), true, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
render_map(map - glm::vec2{16.f});
if (m_map.render(map - glm::vec2{16.f}))
pick(m_map.point());
ImGui::EndChild();
ImGui::SameLine();
ImGui::BeginChild("##wizardform", ImVec2(0.f, map.y), true);
@@ -119,146 +56,10 @@ void ui::scenerywizard_panel::render_contents()
ImGui::EndChild();
}
void ui::scenerywizard_panel::render_map(glm::vec2 const &Size)
{
auto const origin{ImGui::GetCursorScreenPos()};
if (false == m_fitted)
{
m_fitted = true;
m_centre = (kPolandLow + kPolandHigh) * 0.5;
m_scale = std::max((kPolandHigh.x - kPolandLow.x) / Size.x, (kPolandHigh.y - kPolandLow.y) / Size.y);
}
ImGui::InvisibleButton("##map", ImVec2(Size.x, Size.y));
auto const hovered{ImGui::IsItemHovered()};
auto const active{ImGui::IsItemActive()};
auto const &io{ImGui::GetIO()};
glm::vec2 const mouse{io.MousePos.x, io.MousePos.y};
glm::vec2 const middle{origin.x + Size.x * 0.5f, origin.y + Size.y * 0.5f};
auto const to_world = [&](glm::vec2 const &Screen) { return glm::dvec2{m_centre.x + (Screen.x - middle.x) * m_scale, m_centre.y - (Screen.y - middle.y) * m_scale}; };
auto const to_screen = [&](glm::dvec2 const &World) { return ImVec2(middle.x + static_cast<float>((World.x - m_centre.x) / m_scale), middle.y - static_cast<float>((World.y - m_centre.y) / m_scale)); };
if (hovered && io.MouseWheel != 0.f)
{
auto const under{to_world(mouse)};
m_scale = std::clamp(m_scale * std::pow(0.8, static_cast<double>(io.MouseWheel)), 0.25, 2000.0);
m_centre = {under.x - (mouse.x - middle.x) * m_scale, under.y + (mouse.y - middle.y) * m_scale};
m_dirty = true;
m_changed = seconds();
}
if (ImGui::IsItemActivated())
{
m_pressed = mouse;
m_dragging = false;
}
if (active)
{
if (glm::length(mouse - m_pressed) > 4.f)
m_dragging = true;
if (m_dragging && (io.MouseDelta.x != 0.f || io.MouseDelta.y != 0.f))
{
m_centre.x -= io.MouseDelta.x * m_scale;
m_centre.y += io.MouseDelta.y * m_scale;
m_dirty = true;
m_changed = seconds();
}
}
if (ImGui::IsItemDeactivated() && false == m_dragging)
pick(to_world(mouse));
if (m_dirty && seconds() - m_changed > 0.25 && false == m_request.valid())
request_map(Size);
auto *drawlist{ImGui::GetWindowDrawList()};
drawlist->PushClipRect(origin, ImVec2(origin.x + Size.x, origin.y + Size.y), true);
drawlist->AddRectFilled(origin, ImVec2(origin.x + Size.x, origin.y + Size.y), IM_COL32(200, 214, 222, 255));
if (m_texture != 0)
drawlist->AddImage((ImTextureID)(static_cast<std::intptr_t>(m_texture)), to_screen({m_image_low.x, m_image_high.y}), to_screen({m_image_high.x, m_image_low.y}));
if (m_picked)
{
auto const at{to_screen(m_point)};
auto const colour{IM_COL32(220, 30, 30, 255)};
drawlist->AddCircle(at, 10.f, colour, 24, 2.5f);
drawlist->AddLine(ImVec2(at.x - 16.f, at.y), ImVec2(at.x + 16.f, at.y), colour, 2.f);
drawlist->AddLine(ImVec2(at.x, at.y - 16.f), ImVec2(at.x, at.y + 16.f), colour, 2.f);
}
std::string info;
if (hovered)
{
auto const world{to_world(mouse)};
auto const degrees{to_degrees(world)};
info = format(STR_C("E %.3f km, N %.3f km (%.5f N, %.5f E)"), world.x / 1000.0, world.y / 1000.0, degrees.x, degrees.y);
}
if (m_request.valid())
info += (info.empty() ? "" : " ") + std::string{STR_C("loading the map...")};
else if (false == m_map_error.empty())
info += (info.empty() ? "" : " ") + m_map_error;
if (false == info.empty())
{
auto const size{ImGui::CalcTextSize(info.c_str())};
drawlist->AddRectFilled(ImVec2(origin.x + 4.f, origin.y + 4.f), ImVec2(origin.x + 12.f + size.x, origin.y + 8.f + size.y), IM_COL32(0, 0, 0, 170), 4.f);
drawlist->AddText(ImVec2(origin.x + 8.f, origin.y + 6.f), IM_COL32(255, 255, 255, 240), info.c_str());
}
drawlist->PopClipRect();
}
void ui::scenerywizard_panel::request_map(glm::vec2 const &Size)
{
m_dirty = false;
auto const width{std::clamp(static_cast<int>(Size.x), 64, 2048)};
auto const height{std::clamp(static_cast<int>(Size.y), 64, 2048)};
glm::dvec2 const half{width * 0.5 * m_scale, height * 0.5 * m_scale};
auto const low{m_centre - half};
auto const high{m_centre + half};
char bbox[160];
std::snprintf(bbox, sizeof(bbox), "%.1f,%.1f,%.1f,%.1f", low.x, low.y, high.x, high.y);
auto const url{kMapService + "?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&LAYERS=osm&STYLES=&SRS=EPSG:2180&BBOX=" + bbox + "&WIDTH=" + std::to_string(width) + "&HEIGHT=" + std::to_string(height) + "&FORMAT=image/png"};
m_request = std::async(std::launch::async, [url, low, high, width, height]() {
map_result result;
result.low = low;
result.high = high;
std::vector<std::uint8_t> data;
if (false == editor_http_get(url, data, result.error))
return result;
int w{0}, h{0}, components{0};
auto *pixels{stbi_load_from_memory(data.data(), static_cast<int>(data.size()), &w, &h, &components, 4)};
if (pixels == nullptr)
{
result.error = STR("The map service sent no image");
return result;
}
result.width = w;
result.height = h;
result.pixels.assign(pixels, pixels + static_cast<std::size_t>(w) * h * 4);
stbi_image_free(pixels);
return result;
});
}
void ui::scenerywizard_panel::upload_map()
{
if (false == m_request.valid() || m_request.wait_for(std::chrono::seconds(0)) != std::future_status::ready)
return;
auto result{m_request.get()};
if (false == result.error.empty() || result.pixels.empty())
{
m_map_error = result.error.empty() ? std::string{STR("The map service sent no image")} : STR("No map: ") + result.error;
return;
}
m_map_error.clear();
// the map as the service sends it, its values taken as they are (not as srgb), with the linear filtering
imgui_renderer::image_options options;
options.srgb = false;
if (auto *renderer{GfxRenderer->GetImguiRenderer()})
m_texture = renderer->Update_Image(m_texture, result.pixels.data(), result.width, result.height, options);
m_image_low = result.low;
m_image_high = result.high;
}
void ui::scenerywizard_panel::pick(glm::dvec2 const &Point)
{
m_point = Point;
m_picked = true;
auto const degrees{to_degrees(Point)};
m_map.pick(Point);
auto const degrees{editor_geo::to_degrees(Point)};
std::snprintf(m_latlon, sizeof(m_latlon), "%.6f, %.6f", degrees.x, degrees.y);
}
@@ -297,7 +98,7 @@ void ui::scenerywizard_panel::render_form()
ImGui::InputTextWithHint("##title", STR_C("title, written in the file"), m_title, sizeof(m_title));
ImGui::Spacing();
auto const placed{m_picked || false == m_realistic};
auto const placed{m_map.picked() || false == m_realistic};
auto const ready{named && false == exists && placed};
if (false == ready)
{
@@ -317,8 +118,8 @@ void ui::scenerywizard_panel::render_form()
ImGui::TextUnformatted(STR_C("Centre of the scenery, PUWG 1992"));
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("PUWG 1992: the coordinate system of the maps of Poland, in km"));
double east{m_point.x / 1000.0};
double north{m_point.y / 1000.0};
double east{m_map.point().x / 1000.0};
double north{m_map.point().y / 1000.0};
ImGui::SetNextItemWidth(130.f);
auto changed{ImGui::InputDouble(STR_C("E, km##east"), &east, 0.0, 0.0, "%.3f", ImGuiInputTextFlags_EnterReturnsTrue)};
ImGui::SetNextItemWidth(130.f);
@@ -326,8 +127,7 @@ void ui::scenerywizard_panel::render_form()
if (changed)
{
pick({east * 1000.0, north * 1000.0});
m_centre = m_point;
m_dirty = true;
m_map.show(m_map.point(), 1000.0);
}
ImGui::SetNextItemWidth(-90.f);
auto const entered{ImGui::InputTextWithHint("##latlon", STR_C("latitude, longitude"), m_latlon, sizeof(m_latlon), ImGuiInputTextFlags_EnterReturnsTrue)};
@@ -340,10 +140,8 @@ void ui::scenerywizard_panel::render_form()
double latitude{0.0}, longitude{0.0};
if (words >> latitude >> longitude && latitude > 45.0 && latitude < 58.0 && longitude > 10.0 && longitude < 28.0)
{
pick(to_puwg(latitude, longitude));
m_centre = m_point;
m_scale = std::min(m_scale, 4.0);
m_dirty = true;
pick(editor_geo::to_puwg(latitude, longitude));
m_map.show(m_map.point());
m_status.clear();
}
else
@@ -351,9 +149,9 @@ void ui::scenerywizard_panel::render_form()
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Degrees, as given by the online maps; the map moves there"));
if (m_picked)
if (m_map.picked())
{
auto const degrees{to_degrees(m_point)};
auto const degrees{editor_geo::to_degrees(m_map.point())};
ImGui::TextDisabled(STR_C("%.5f N, %.5f E"), degrees.x, degrees.y);
}
ImGui::Spacing();
@@ -382,21 +180,12 @@ bool ui::scenerywizard_panel::create_scenery()
char date[32];
std::strftime(date, sizeof(date), "%Y-%m-%d", std::localtime(&now));
char reference[96];
std::snprintf(reference, sizeof(reference), "//$g PUWG1992 %.3f %.3f", m_point.x / 1000.0, m_point.y / 1000.0);
std::snprintf(reference, sizeof(reference), "//$g PUWG1992 %.3f %.3f", m_map.point().x / 1000.0, m_map.point().y / 1000.0);
if (m_realistic)
{
auto const &stored{EditorSettings.orthophoto()};
editor_orthophoto::config map;
map.radius = stored.radius;
map.height = stored.height;
map.opacity = stored.opacity;
map.year = stored.year;
map.hires = stored.hires;
map.drape = stored.drape;
map.lift = stored.lift;
map.in_scene = stored.in_scene;
// the orthophoto on; it's shown as the settings of the editor have it, its origin is the one of //$g
output << reference << "\r\n";
output << "//$o " << editor_orthophoto::mark(map, true, false) << "\r\n";
output << "//$o " << editor_orthophoto::mark(editor_orthophoto::config{}, true, false) << "\r\n";
}
else
std::snprintf(reference, sizeof(reference), "fictional");

View File

@@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/.
#pragma once
#include "application/uilayer.h"
#include "editor/editorGeoMap.hpp"
#include <cstdint>
#include <future>
@@ -28,40 +29,12 @@ class scenerywizard_panel : public ui_panel
void render_contents() override;
private:
struct map_result
{
glm::dvec2 low{0.0};
glm::dvec2 high{0.0};
int width{0};
int height{0};
std::vector<std::uint8_t> pixels;
std::string error;
};
void render_map(glm::vec2 const &Size);
void render_form();
void request_map(glm::vec2 const &Size);
void upload_map();
void pick(glm::dvec2 const &Point);
bool create_scenery();
glm::dvec2 m_centre{515000.0, 460000.0};
double m_scale{1000.0};
bool m_fitted{false};
double m_changed{0.0};
bool m_dirty{true};
glm::vec2 m_pressed{0.f};
bool m_dragging{false};
std::future<map_result> m_request;
std::uint64_t m_texture{0}; // imgui_renderer image
glm::dvec2 m_image_low{0.0};
glm::dvec2 m_image_high{0.0};
std::string m_map_error;
bool m_picked{false};
editor_map m_map;
bool m_realistic{true};
glm::dvec2 m_point{0.0};
char m_filename[64]{};
char m_title[128]{};
char m_latlon[64]{};

View File

@@ -4,12 +4,13 @@
void ui::vehicles_bank::scan_textures()
{
for (auto &f : std::filesystem::recursive_directory_iterator("dynamic")) {
if (f.path().filename() != "textures.txt")
std::error_code error;
for (std::filesystem::recursive_directory_iterator f{"dynamic", std::filesystem::directory_options::skip_permission_denied, error}, end; f != end; f.increment(error)) {
if (f->path().filename() != "textures.txt")
continue;
std::fstream stream(f.path(), std::ios_base::binary | std::ios_base::in);
ctx_path = std::filesystem::relative(f.path().parent_path(), "dynamic/");
std::fstream stream(f->path(), std::ios_base::binary | std::ios_base::in);
ctx_path = std::filesystem::relative(f->path().parent_path(), "dynamic/", error);
std::string line;
while (std::getline(stream, line))