mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 22:55:16 +01:00
* Enhance Windows build matrix with AVX2 variants * fix(lang): the two Polish lines of the download area written with \n, so they match the tooltip and the hint * fix(files): data named in another case is found on Linux, folders included The parser and the loaders lowercase every name they read, while the data made on Windows has files and folders named in any case (scenery/Braniewo, textures/siec_trakcyjna_ph/.../STS_*.tga, sounds/sip/Gorki.wav). FileCased matched the file name only, so a folder in another case wasn't found. - FileCased walks every part of the path on Linux, the folder listings kept by their lowercase and read again once the folder changed; Windows as it was - FileExists finds the file in any case on Linux, and the variant with names and extensions gives back the name as it's on the disk, so textures, materials, sounds, station announcements and scripts open that name - models, sound captions, timetables and nodebank previews open the file as it's named on the disk; the texture format is told by the extension in any case * fix(files): the banks keep the names asked for, only the opening takes the case of the disk The textures, materials and sounds are kept by the names they're asked for. Given back as named on the disk, a file in another case missed the bank and was loaded again on every request. - FileExists with names and extensions finds the file in any case but gives back the name asked for; FileOnDisk gives the file to open, used where textures, sounds and scripts are opened - FileExists of a single name is a plain check again, the places checking it and opening the same name stay as they were - the texture format is told by the extension asked for again - a folder changed shortly before it was read is read again for a name missing from it, the time of a change being as coarse as the clock of the file system; a listing cut short isn't kept * fix(files): a folder changed in the future isn't read again on every name missing from it * perf(files): a file missing costs a single check, as before Measured in a case sensitive folder (3000 textures, every 20th in mixed case, looked up the way Texture.cpp does), the walk over every part of the path made a texture lookup 4-5 times slower than master: each name tried and missing checked every folder of its path. - the folders found are kept by the name they're given with, the missing ones for a second, so a missing file checks its folder alone - a folder named as it is on the disk is taken without its listing - the time a folder was changed is looked at once a second at most The lookup is now as fast as master's (about 180 us per texture on NTFS in both) while finding the files named in another case too. * fix(files): terrain files named in another case are found on Linux The terrain directive names .txtf/.btf files in lowercase, as the parser gives every token. The name is now taken as it is on disk, from the text file first, so the binary file is made next to the text and the editor's references and layers keep one name. * fix(files): models named in another case are found on Linux The manager of the models looked for the file with FileExists of a single name, a plain check again since the banks keep the names asked for, so a model named in another case (models/przejazdy/plyty3_L, dynamic/pkp/st44_v2/main/2M62-0685-A) was reported missing before TModel3d::LoadFromFile() could open it as it's named on the disk. find_on_disk() finds the file in any case and gives back the name asked for, the key of the bank. * refactor(files): named functions instead of the lambdas of the folder cache (Sonar S2259, S3608, S6004) Sonar took the folder captured by the lambda reading it for a reference to null, failing the quality gate on reliability. The folder is read and the path kept by functions in the anonymous namespace, the age of the listing declared in its if. * fix: the simulator opens more than 512 files at once, and a shader it can't open says why With "physicslog yes" every driver keeps its .dat open, and a large scenery (Tomaszewo: some 500 vehicles, cars of the roads included) took all 512 streams the MSVC runtime gives: the next fopen failed and the simulator stopped at "cannot read shader: shaders/map.vert" on opening the map. The limit is raised to 8192 at the start (the soft limit of open files up to the hard one on Linux), and a shader file that doesn't open is told as such, with the system's reason, rather than as one that can't be read. * feat(blackbox): the black box on its own, writing the tapes of the user's vehicles at the end "blackbox yes" in eu07.ini records the black box with the physics logs on or off, "blackbox no" never; with no entry it goes with "physicslog yes" as before. With the physics logs off, the recorder keeps the line of the physics log of the driver the user is with, every 0.05 s, and writes it to physicslog/<vehicle>.dat when the recording closes: the speed recorder's tape of the vehicles the user was in, without every vehicle keeping a log open and written twenty times a second. The line and the header of the log are made by one function of the driver, which its own log writes too, and the file is opened the same way, so the two are the same to the byte: the readers of these logs (the old Hasler tape viewer, the starter) read them as before. * feat(physicslog): the logs written every two seconds by a thread of their own, no file kept open; the black box's tapes as it goes "physicslog yes" kept the .dat of every vehicle with a driver open for the whole run and flushed it twenty times a second from the simulation's thread: some 580 open files on Tomaszewo. The lines are kept now and a thread of their own writes them every two seconds, each file opened only for the moment it is written to. The files, their lines and their text mode stay as they were. - The black box, with the physics logs off, writes the tapes of the user's vehicles through the same writer as it goes, not at the end. A log begins when the user is first in the vehicle and goes on while the user looks around outside, until another vehicle is taken; back in it, the log goes on after the gap. The vehicle's own driver keeps the log only if it is the primary one, as a passive one's clock stands still; otherwise the driver of its train does. - What is kept is written when the simulator closes, after the black box, at the end of a test run, and when the test watchdog ends a run. A crash loses the last two seconds. - On Tomaszewo with the black box alone, the one tape grows every two seconds. With "physicslog yes", the 581 logs grow and the simulator holds as many handles as without them; writing them takes the thread about 1.9 s, which the closing waits for once. * feat(blackbox): the cab record holds the speed of the wheels, apart from the vehicle's when they slip The speed of the cab record is the vehicle's. The speedometer, and a speed recorder's tape with it, shows the speed of the wheels as they turn, which runs away from it while they slip and drops to 0 while they are locked in a skid. It is added at the end of the record (194 bytes), where the readers skip what they don't know; the .dat tapes have it already, in their second column. * fix(ci): build OpenAL Soft with the PipeWire, PulseAudio and ALSA backends, so the Linux game has sound The Linux job builds OpenAL Soft 1.24.2 from source, but the runner had none of the headers of its audio backends, and OpenAL Soft's CMake leaves out every backend it can't find. The bundled libopenal.so.1 had only OSS, Null, Wave and Loopback. Modern distributions and the Steam Linux Runtime have no /dev/dsp, so the game logged "Failed to obtain audio device" and played nothing, with any Steam Linux Runtime and without one. - install libasound2-dev, libpulse-dev and libpipewire-0.3-dev before building OpenAL Soft - configure it with ALSOFT_REQUIRE_PIPEWIRE/PULSEAUDIO/ALSA, so a missing header fails the build instead of shipping a library without sound The backends are loaded with dlopen at run time (ALSOFT_DLOPEN is on by default), so the bundled library gains no new dependency. Tested by putting an OpenAL Soft 1.24.2 with these backends next to the game from the Steam depot: under Steam Linux Runtime 4.0 the sound works. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(editor): the certificates of https looked for where the distribution keeps them The Linux game bundles the libcurl of Ubuntu, which knows only Ubuntu's /etc/ssl/certs/ca-certificates.crt. Fedora, RHEL and openSUSE keep the certificates elsewhere, so every download of the editor (orthophoto, terrain model, topo map) failed there with a certificate error. The file is taken from SSL_CERT_FILE or CURL_CA_BUNDLE when set, else the first of the files the distributions keep, and passed as CURLOPT_CAINFO. With none found libcurl keeps its own. * fix(editor): an https proxy checked with the same certificates CURLOPT_PROXY_CAINFO gets the file found for CURLOPT_CAINFO, so a download through an https:// proxy on Fedora or RHEL doesn't fail on the proxy's certificate. * fix(ci): the no-AVX2 Windows build has its own concurrency group and its own public download After master was merged the two Windows variants shared one concurrency group with cancel-in-progress, so one cancelled the other, and the publish job looked for an artifact name the matrix no longer produces. Each variant now has its own group, the publish job downloads both, and the release carries maszyna-windows-x64.zip (AVX2, the old name, so the links already handed out still work) and maszyna-windows-x64-noavx2.zip. The AVX2 variant passes -DENABLE_AVX2=ON explicitly instead of an empty flag. * build: a build without AVX2 has _noavx2 at the end of its executable name With ENABLE_AVX2=OFF the executable is eu07-x64_<date>_<hash>_noavx2.exe (eu07-x64_noavx2.exe without ADD_VERSION_SUFFIX), so a starter can tell the two builds apart by the name alone and pick this one on a CPU without AVX2. The AVX2 build keeps its name. * ci: Linux is built without AVX2 too, published as maszyna-linux-x64-noavx2.tar.gz The Linux job becomes the same two-variant matrix as Windows (ENABLE_AVX2=ON/OFF), each variant with a concurrency group of its own. The publish job downloads both Linux artifacts, restores the executable bits in both and releases maszyna-linux-x64.tar.gz (AVX2, the old name) next to maszyna-linux-x64-noavx2.tar.gz. The no-AVX2 executable is eu07_<date>_<hash>_noavx2. * fix(renderer): the legacy renderer clears the arrays the scene left before it draws the ui The legacy renderer drew the ui with the client arrays of the last scene draw still enabled: the texture coordinates of the normal map unit and the normals, pointing into the vertex buffer of whatever was drawn last, 48 bytes a vertex. The ImGui backend sets up the vertex, colour and texture coordinates of the active unit only, so the driver read the stale arrays with the indices of the ui. Once the ui had more vertices than that buffer, the NVIDIA driver read past its end and the game crashed; a crash dump of the editor (an area selected, a track being laid) shows the read of vertex 15113 at a 48-byte stride in ImGui_ImplOpenGL2_RenderDrawData. The arrays are now disabled on every texture unit the geometry uses, with the normal and colour arrays, inside the glPushClientAttrib already there, so the scene gets its state back after the ui. The default and the experimental renderers don't go through this code. * fix(app): a renderer this build doesn't have starts the full one, not the legacy one The starter writes "gfxrenderer vulkan" for the Vulkan renderer of the builds that have it. This exe doesn't know the word, and every word other than default/experimental fell to the legacy renderer: a player who picked Vulkan got the slowest renderer, the one whose ui crashes on NVIDIA (the arrays left by the scene). "experimental" in a build without the NVRHI renderer ended with "no renderer found!" and no game at all. After the settings are read, a gfxrenderer word this build has no renderer for becomes "default", with a line in the log saying so. The words it knows stay as they were: full/default, legacy, simple, null, the older starters' simpleshader and off (legacy as before), and experimental where it is built. * fix(settings): the gfxrenderer word read in any case, as the starter reads it A hand-written "gfxrenderer Legacy" started the legacy renderer only because every unknown word did; with unknown words going to the full renderer it would have changed. The word is lowercased when read, as StarterNG's RenderEngineOf does, so Legacy, Simple or Full mean what they say. * fix(physicslog): a log whose file can't be written keeps its lines and header, and the writer is stopped once on every way out A failed write put its lines back, ahead of those since, so a file held by another program for a while gets its header and every line once it can be written; up to 4 MB kept per log. Stopping is joined once, and not from the writer's own thread. main stops the logs after an error it caught too, and the test watchdog flushes with a time limit, so a disk that doesn't answer doesn't keep the run from ending. * fix(blackbox): the wheels of the powered vehicle, as the speedometer has them, and the logs described as the drivers' they are In a unit driven from an unpowered car, the speed of the wheels was of the car the user is in, whose wheels never slip; now of the motor car, signed as the occupied vehicle's speed. The doc tells of a log per driver, and of the two seconds of real time a crash loses. * fix(physicslog): failed logs kept within 64 MB in all, cut outside the lock, never written twice; the writer knows itself when stopped The lines kept while files can't be written are cut to their share of 64 MB before the lock the simulation waits on. A log written in part is cut back to its length, so the next try doesn't write its start twice. A stop from the writer itself is told by its id even after another thread took it to join, and the watchdog's flush waits at most its time, the writing on a thread of its own. * feat(physicslog): the logs of the trains only, physicslog.trains "physicslog.trains yes" in eu07.ini writes the .dat log of each driver who drives rail vehicles, not of the passengers nor of the road vehicles that make "physicslog yes" hundreds of files on a big scenery: the tape of every train. A driver made primary later begins its log then, and isn't counted while it doesn't log. The black box goes with it when the ini says nothing of its own, and doesn't write a second log of a driver writing its own. The doc tells the doors' sides are the vehicle's, the driver's swapped in the rear cab. * fix(physicslog): a train's log begun at its first line, so passive crews get no file Every crew is primary in the constructor and the drivers but one of a train with crews in more vehicles are made passive only before it starts, so a log begun there left files with only the header. With physicslog on all logs begin at once as before; one of a train waits for update_logs. * fix(editor): the texture pickers let go of the images scrolled past, the memory no longer runs out The picker of the textures of a track offers every texture of the textures folder, and each tile that came into sight read its texture full size, into the graphics card and the memory, and kept it there for good. Scrolling through the list for a trackbed texture took about 2.5 MB a tile: some 900 tiles made 3.5 GB, the whole folder 28 GB, and the game went down once the memory ran out. An image out of sight for 3 seconds is now taken out of both again (texture_manager::unload, Unload_Texture of the renderers) and read anew when it's shown once more, a few a frame as before. Only a texture the scene has never drawn is let go; those of the scene stay to the resource sweep. The texture browser lets go of its images on Refresh too. The picker no longer lists textures/previews: the 4100 previews of the node bank are images of models, not textures of anything. Checked on td.scn by a scripted run: 8000 images of the picker read one batch a few frames after another held the game at 2-3 GB, where it went to 28 GB and fell before; images shown again after they were let go come back; a trackbed texture picked from the folder is drawn on the track after its image was let go. * fix(physicslog): the writer's destructor throws nothing, its wait a plain condition variable woken to stop; a driver of no vehicle controls nothing * fix(editor): the images of the materials let go of with all their textures, the browser's Refresh a frame later An image of a material read the material with all its textures, the normal and specular maps and the variants, and only the first one was let go of: those stayed in the memory for every material scrolled past. They are let go of together now (Unload_Material). texture_manager::unload also takes the textures read and not made yet, waiting for a worker still reading one, and deletes the gl texture itself: release() would read it back from the card with gfx.resource.move only for it to be dropped. The Refresh of the texture browser let go of the images in the middle of the frame, while panels drawn before it could still refer to them; it now marks them, and they go at the start of the next frame. Checked on td.scn by a scripted run: 8040 images of the picker read a batch every few frames end at 2.0 GB, against 2.3-2.6 GB before this and 1.1 GB at the start; a trackbed material picked from the folder is drawn on the track after its image was let go. * fix(physicslog): every stop asked wakes the writer, the one from itself too; no psyche for a driver of no vehicle * fix(editor): the image of a material with no texture in its first slot is let go of too Its normal and specular maps were read with it and stayed in the memory: the trim passed over every image with no texture of its own, the material along with it. * fix(physicslog): nothing that goes wrong in the writing ends the simulator; a name in 1250 makes a path on a Windows set to UTF-8 The writer's thread called flush() with nothing around it, so an exception leaving write() ended the process with std::terminate, with no message. One that happens: the file of a log was made a std::filesystem::path of the vehicle's name, which MSVC converts in the code page of the system and throws for bytes not of it; on a Windows with "Use Unicode UTF-8 for worldwide language support" a vehicle named in 1250, as the sceneries have them (0xB3 for ł), isn't UTF-8. - the file of a log is made of the name with MultiByteToWideChar without MB_ERR_INVALID_CHARS: the same file as before for every name of the code page, U+FFFD for the bytes not of it instead of a throw. Elsewhere the bytes are the name of the file as they were - a log whose writing throws for any other reason (memory) is kept for the next try as one whose file can't be written, and the other logs are written still - the thread, flush_for() and stop() write through flush_safely(), which lets nothing out: what was taken for a writing failing as a whole is lost and told in the log, the next writing goes on - the messages name the file by the bytes of the name, not by path::string(), which converts back Tests: a log which can't be written doesn't keep the others from theirs; a vehicle named in 1250 has its log, read back by physicslog_writer::file(). Reported in the review of #385. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(blackbox): the tapes go on after the editor, with the physics logs off The tapes of the user's vehicles were written by update() after the check for the writer of the recording, which close() drops on entering the editor (F11). With "physicslog no", the speed recorder's tape ended at F11 while the drivers' own logs of "physicslog yes" went on. The tapes are files of their own: they are kept from the moment the recording was to be made, the file made or not, closed or not, until the simulator closes, after the gap of the time in the editor. Only the recording itself stops at F11, as before. Reported in the review of #385. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(physicslog): what no file name may have on Windows is replaced in the name of a log, on every system A spawned vehicle is named <spawn>:<number>, and its log went to physicslog/<spawn>:<number>.dat. On NTFS that isn't even an error: it is the stream <number>.dat of the file <spawn>, which nothing lists, so the log was written and no one could find it. On Linux the file was there, under a name Windows can't open. The name of the file of a log has < > : " / \ | ? * and the control characters replaced by _, on every system, so the logs are named alike everywhere (spawn_ezt:12 is spawn_ezt_12.dat), and a _ before a name Windows keeps for a device (con, nul, com1..., with an extension too). The bytes of 1250 stay. The black box tells the log it begins by the same name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(renderer): a material's textures let go of in one place, for every backend Unload_Material walked the textures of a material and of its variants the same way in both opengl renderers, and Sonar counted its declarations, set into the long run of overrides the two headers share, as duplicated: 5.2% of the new code, 3% allowed. The walk is gfx_renderer's now, over IMaterial::GetAllTextures and the backend's Unload_Texture. Materials that don't tell their textures, the Vulkan renderer's, give none, and are left alone as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(physicslog): road vehicles keep no log, the lines are made into text by the writer, and a slow disk gets them less often On a big scenery "physicslog yes" wrote a log for every driver, the hundreds of cars of the roads too: Tomaszewo gives over a thousand. A player with an i7-3770 and a hard disk had 1 fps there and 90 fps paused; with the physics log off the frame rate came back. The disk was busy with the logs all the time, and the simulation formatted 26 values a line, twenty lines a second, for each of them. - "physicslog yes" writes the logs of all drivers but those of road vehicles. Nobody reads those, and the starter's tapes are of rail vehicles. The black box writes the tape of a car the user drives, as it does with the physics logs off. On Tomaszewo a 120 s test run gives 20 logs instead of 579. - The simulation hands the writer the values of a line; the writer's thread makes them into text, with the same stream as before. The logs of a fixed-seed run of Tomaszewo come out byte for byte the same as before. - The writer's thread does its disk work at background priority (Windows THREAD_MODE_BACKGROUND_BEGIN, Linux the lowest best-effort I/O priority), so the simulation's own reading doesn't wait behind the logs. - A writing that takes over a third of the 2 s interval makes the next wait three times as long as it took: the disk is busy with the logs a quarter of the time at most. The log of the simulator says so once. - A log is written with one write, the header of a log begun with its lines, and a log added to is opened at its end rather than asked for its length first. What changes for the data: with "physicslog yes" no .dat files of road vehicles appear in physicslog/. Nothing in the SVN needs to change; the comment of physicslog in the shipped eu07.ini ("for all vehicles manned by AI or a human") is now a little wide of it. * fix(physicslog): the wait after a slow writing is ten seconds at most, a log whose lines can't be made into text is kept - The wait after a slow writing grew with what it left to the next one, with no end once the formatting and the bytes took a quarter of the time: it is five intervals at most now, so a crash loses ten seconds at most. - The lines given as values are made into text within the try of each log, aside and added at once: out of memory there keeps that log for the next try and the others are written, as the comment said. - The Windows background mode is described as it is: very low I/O priority, the memory pages let go of first. - The black box doc says a car the user drives has no log with the black box off. * fix(physicslog): the closing waits for the logs a second at most, the rest is dropped stop() gives the writing one second (a constructor parameter, ClosingWait). The writer thread's batch under way and the final writing check the time before each log and drop the logs past it, telling how many in the log. The final writing runs on a thread of its own, left behind when it isn't done in time, so a disk that doesn't answer in the middle of a file doesn't hold the closing either; the simulator ends with _Exit, and a writer object that goes waits for those writings in its destructor. At the closing the writer thread's disk priority is put back to normal, so that second goes to the logs. A flush() after the stop is not cut short. * fix(physicslog): a second stop keeps the time of a closing under way, and a stop lets out no exception The simulator stops the logs twice on a normal exit (Application.exit() and main). A stop while an earlier closing is still writing now keeps that closing's time instead of giving it another second, and doesn't tell it again. The thread of the closing is begun in a noexcept helper: with no memory or no thread to be had the rest is written on the calling thread, the logs past the time dropped still, so stop() doesn't throw on the way out of an out-of-memory error. * fix(physicslog): a closing whose thread is begun isn't written again by the stop when its log line fails std::format of the timeout line can throw (out of memory) after the closing's thread was started; close_aside returned false then, and stop() wrote on the calling thread too, waiting on the files the closing holds with no time limit. A thread begun counts now. * fix(physicslog): a failed log keeps its values in order, and the user's log goes first at the closing A log whose file couldn't be written kept the values the writing failed to make into text (out of memory) only until the next line came: keeping it put the values given since in their place. They're made into text again now, and the values given since go after them, added at once. Values which still can't be made into text are dropped when text was given after them, as the order can't be kept otherwise. What a writing needs before it takes the lines (the names put first, the order) is had before it takes them, so without the memory for it the lines are left for the next writing. The closing, which waits a second for the writing, dropped the logs past it by the order of their names: the user's train could lose its last seconds while a log nobody reads was written. Now: - the logs put first (put_first) are written before the others at each writing: the one of the driver the black box follows, of the vehicle the user is in (the black box goes with the physics logs unless "blackbox no"), and those of the drivers of the tested trains (test.vehicle); - a stop ends a writing of the thread under way at the log at hand, and the thread begins none after it. The logs it hasn't come to are left to the closing's writing, which has them with the lines given since, those put first first. Before, the thread went on through them and the closing's writing, coming after, had no time left for the newest lines. They go back as they were taken: their values are made into text by the closing's writing, not on the way to it unless text was given since, and they don't count toward the 64 MB kept of the logs whose file can't be written. The closing drops the logs past its time as before, the user's too: none is begun once the time is over, only the one at hand may still be written while the process ends. The thread no longer raises its disk priority at a closing, as it no longer writes there. --------- Co-authored-by: maj00r <maj00r@host> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Hirek193 <polomarket73@gmail.com>
2184 lines
91 KiB
C++
2184 lines
91 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/.
|
|
*/
|
|
|
|
// properties of the selected tracks in the inspector: one sheet for one track and for many, a value shared by all of
|
|
// them shown as it is and values which differ as "different", a value typed or chosen going to all of them in one
|
|
// step of the undo; the properties copied from one track and pasted on others; the tracks like the selected ones
|
|
// selected in the whole scenery
|
|
|
|
#include "stdafx.h"
|
|
#include "application/editormode.h"
|
|
#include "application/editoruilayer.h"
|
|
#include "application/editormodeshared.h"
|
|
#include "editor/editorFormat.hpp"
|
|
#include "editor/editorGeometry.hpp"
|
|
#include "editor/editorTrackPresets.hpp"
|
|
|
|
#include "rendering/renderer.h"
|
|
#include "world/Track.h"
|
|
#include "utilities/Globals.h"
|
|
#include "world/TrackSleepers.h"
|
|
#include "scene/scenelayers.h"
|
|
#include "simulation/simulation.h"
|
|
#include "utilities/Logs.h"
|
|
#include "utilities/translation.h"
|
|
#include "utilities/utilities.h"
|
|
|
|
#include "imgui/imgui.h"
|
|
#include "imgui/imgui_internal.h"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <limits>
|
|
#include <map>
|
|
#include <set>
|
|
#include <sstream>
|
|
|
|
namespace
|
|
{
|
|
|
|
using geometry::bezier;
|
|
|
|
int const kRailCategory{1};
|
|
std::size_t const kListedValues{8}; // in the tooltip of a property whose values differ
|
|
std::size_t const kLargeSelection{300}; // above it the values are gathered again only every few frames
|
|
double const kOutcomeSeconds{10.0}; // the line under a row telling how a value went
|
|
|
|
bool rail(TTrack const &Track)
|
|
{
|
|
return (Track.iCategoryFlag & 15) == kRailCategory && false == Track.m_editorremoved && Track.visible();
|
|
}
|
|
|
|
// a number as short as it can be written: no trailing zeros
|
|
std::string decimal(double const Value, int const Digits)
|
|
{
|
|
auto text{format("%.*f", Digits, Value)};
|
|
if (text.find('.') != std::string::npos)
|
|
{
|
|
while (text.back() == '0')
|
|
text.pop_back();
|
|
if (text.back() == '.')
|
|
text.pop_back();
|
|
}
|
|
return text == "-0" ? std::string{"0"} : text;
|
|
}
|
|
|
|
bool parse_number(std::string Text, double &Value)
|
|
{
|
|
std::replace(Text.begin(), Text.end(), ',', '.');
|
|
auto const *start{Text.c_str()};
|
|
char *end{nullptr};
|
|
Value = std::strtod(start, &end);
|
|
if (end == start)
|
|
return false;
|
|
while (*end == ' ')
|
|
++end;
|
|
return *end == '\0' && std::isfinite(Value);
|
|
}
|
|
|
|
std::vector<std::string> words_of(std::string const &Text)
|
|
{
|
|
std::istringstream stream{Text};
|
|
std::vector<std::string> words;
|
|
for (std::string word; stream >> word;)
|
|
words.push_back(word);
|
|
return words;
|
|
}
|
|
|
|
material_handle bed_of(TTrack const &Track)
|
|
{
|
|
if (Track.eType == tt_Switch)
|
|
return Track.SwitchExtension ? Track.SwitchExtension->m_material3 : null_handle;
|
|
return Track.m_material2;
|
|
}
|
|
|
|
// image of the first texture of a material, 0 while there's none; reading a texture takes a moment, a few of them go in a frame
|
|
std::uint64_t material_image(material_handle const Material)
|
|
{
|
|
static int frame{-1};
|
|
static int loads{0};
|
|
if (Material == null_handle)
|
|
return 0;
|
|
auto const *data{GfxRenderer->Material(Material)};
|
|
if (data == nullptr)
|
|
return 0;
|
|
auto const texture{data->GetTexture(0)};
|
|
if (texture == null_handle)
|
|
return 0;
|
|
auto &image{GfxRenderer->Texture(texture)};
|
|
if (false == image.get_is_ready())
|
|
{
|
|
if (frame != ImGui::GetFrameCount())
|
|
{
|
|
frame = ImGui::GetFrameCount();
|
|
loads = 0;
|
|
}
|
|
if (loads >= 3)
|
|
return 0;
|
|
++loads;
|
|
image.create();
|
|
}
|
|
return image.get_is_ready() ? static_cast<std::uint64_t>(image.get_id()) : 0;
|
|
}
|
|
|
|
std::string lowered(std::string Text)
|
|
{
|
|
std::transform(Text.begin(), Text.end(), Text.begin(), [](unsigned char const Character) { return static_cast<char>(std::tolower(Character)); });
|
|
return Text;
|
|
}
|
|
|
|
bool texture_extension(std::string const &Extension)
|
|
{
|
|
return Extension == ".dds" || Extension == ".tga" || Extension == ".png" || Extension == ".bmp" || Extension == ".jpg" || Extension == ".ktx" || Extension == ".tex";
|
|
}
|
|
|
|
// a name in two lines under a tile: as much as fits, then the rest; the end, where the names differ, is kept when the rest is too long
|
|
std::pair<std::string, std::string> two_lines(std::string const &Text, float const Width)
|
|
{
|
|
if (ImGui::CalcTextSize(Text.c_str()).x <= Width)
|
|
return {Text, {}};
|
|
std::size_t split{0};
|
|
while (split < Text.size() && ImGui::CalcTextSize(Text.c_str(), Text.c_str() + split + 1).x <= Width)
|
|
++split;
|
|
while (split > 0 && (static_cast<unsigned char>(Text[split]) & 0xC0) == 0x80)
|
|
--split;
|
|
auto rest{Text.substr(split)};
|
|
if (ImGui::CalcTextSize(rest.c_str()).x > Width)
|
|
{
|
|
while (rest.size() > 1 && ImGui::CalcTextSize(("..." + rest).c_str()).x > Width)
|
|
{
|
|
rest.erase(0, 1);
|
|
while (false == rest.empty() && (static_cast<unsigned char>(rest.front()) & 0xC0) == 0x80)
|
|
rest.erase(0, 1);
|
|
}
|
|
rest = "..." + rest;
|
|
}
|
|
return {Text.substr(0, split), rest};
|
|
}
|
|
|
|
// the width of each of Count buttons in a row across the panel
|
|
float button_width(int const Count)
|
|
{
|
|
auto const spacing{ImGui::GetStyle().ItemSpacing.x};
|
|
return std::max(1.0f, (ImGui::GetContentRegionAvail().x - spacing * static_cast<float>(Count - 1)) / static_cast<float>(Count));
|
|
}
|
|
|
|
enum class field
|
|
{
|
|
number,
|
|
text,
|
|
choice,
|
|
check,
|
|
texture
|
|
};
|
|
|
|
// parts of the sheet, in their order
|
|
enum section : int
|
|
{
|
|
traffic,
|
|
look,
|
|
look_bed,
|
|
look_sleepers,
|
|
ride
|
|
};
|
|
|
|
// what can be pasted, each by itself
|
|
enum group : int
|
|
{
|
|
speed,
|
|
surroundings,
|
|
overhead_line,
|
|
circuits,
|
|
rails,
|
|
trackbed,
|
|
trackbed_shape,
|
|
sleepers,
|
|
ride_sound,
|
|
frogs,
|
|
sleeper_placement, // spacing and shift of the sleepers and of the trackbed under them, apart from their models
|
|
group_count
|
|
};
|
|
|
|
char const *const kGroupLabels[group_count] = {"Speed", "Environment", "Overhead line", "Track circuits", "Rails", "Trackbed", "Shape of the trackbed", "Sleepers", "Ride and sound", "Frog and check rails", "Sleeper placement"};
|
|
// the groups as the presets keep them in their file, by names which don't change with the order of the groups
|
|
char const *const kGroupKeys[group_count] = {"speed", "environment", "overhead", "circuits", "rails", "trackbed", "bedshape", "sleepers", "ride", "frogs", "sleeperplacement"};
|
|
|
|
struct track_property
|
|
{
|
|
char const *key{""}; // names it in the self-tests and in the copied properties
|
|
field kind{field::number};
|
|
int part{section::traffic};
|
|
int group{group::speed};
|
|
bool rebuild{false}; // the geometry of the track is made again after a change
|
|
bool late{false}; // pasted after the others, it depends on them
|
|
bool more{false};
|
|
int texture{-1}; // which list of the textures on the tracks: 0 rails, 1 trackbed
|
|
char const *label{""};
|
|
char const *tooltip{""};
|
|
char const *empty{nullptr}; // hint of an empty field
|
|
std::vector<char const *> choices;
|
|
std::function<bool(TTrack const &)> applies;
|
|
bool off{false}; // the feature is turned off in eu07.ini: the field is shown greyed and nothing changes it
|
|
std::function<std::string(TTrack const &)> get;
|
|
// false: the text doesn't fit the track or changes nothing, the track stays as it was
|
|
std::function<bool(TTrack &, std::string const &)> set;
|
|
};
|
|
|
|
track_property &add(std::vector<track_property> &List, char const *Key, field const Kind, int const Part, int const Group, char const *Label, char const *Tooltip)
|
|
{
|
|
auto &property{List.emplace_back()};
|
|
property.key = Key;
|
|
property.kind = Kind;
|
|
property.part = Part;
|
|
property.group = Group;
|
|
property.label = Label;
|
|
property.tooltip = Tooltip;
|
|
property.applies = [](TTrack const &Track) { return Track.m_visible; };
|
|
return property;
|
|
}
|
|
|
|
// a number kept in a member of the track, Least the lowest value it takes
|
|
void add_float(std::vector<track_property> &List, char const *Key, int const Part, int const Group, char const *Label, char const *Tooltip, float TTrack::*Member, int const Digits, float const Least, bool const Rebuild)
|
|
{
|
|
auto &property{add(List, Key, field::number, Part, Group, Label, Tooltip)};
|
|
property.rebuild = Rebuild;
|
|
property.get = [Member, Digits](TTrack const &Track) { return decimal(Track.*Member, Digits); };
|
|
property.set = [Member, Digits, Least](TTrack &Track, std::string const &Text) {
|
|
double value;
|
|
if (false == parse_number(Text, value))
|
|
return false;
|
|
auto const clamped{std::max(Least, static_cast<float>(value))};
|
|
if (decimal(clamped, Digits) == decimal(Track.*Member, Digits))
|
|
return false;
|
|
Track.*Member = clamped;
|
|
return true;
|
|
};
|
|
}
|
|
|
|
std::vector<track_property> make_properties()
|
|
{
|
|
std::vector<track_property> list;
|
|
auto const any = [](TTrack const &) { return true; };
|
|
float const lowest{std::numeric_limits<float>::lowest()};
|
|
|
|
// traffic
|
|
{
|
|
auto &property{add(list, "speed", field::number, section::traffic, group::speed, "Speed (km/h)", "Speed limit of the track for the AI drivers and the timetable.\nEmpty or -1: no limit of its own")};
|
|
property.applies = any;
|
|
property.empty = "no limit";
|
|
property.get = [](TTrack const &Track) {
|
|
auto const velocity{editor_track::velocity(Track)};
|
|
return velocity < 0.0 ? std::string{} : decimal(velocity, 1);
|
|
};
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double velocity{-1.0};
|
|
if (false == words_of(Text).empty() && false == parse_number(Text, velocity))
|
|
return false;
|
|
if (velocity < 0.0)
|
|
velocity = -1.0;
|
|
if (std::abs(editor_track::velocity(Track) - velocity) < 1e-6)
|
|
return false;
|
|
editor_track::velocity(Track, velocity);
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "environment", field::choice, section::traffic, group::surroundings, "Environment", "Surroundings of the track, they affect the sound of the passing vehicles")};
|
|
property.applies = any;
|
|
property.choices = {"flat", "mountains", "canyon", "tunnel", "bridge", "bank"};
|
|
property.get = [](TTrack const &Track) { return std::to_string(std::clamp(static_cast<int>(Track.eEnvironment), 0, 5)); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double index;
|
|
if (false == parse_number(Text, index) || index < 0.0 || index > 5.0 || static_cast<int>(index) == static_cast<int>(Track.eEnvironment))
|
|
return false;
|
|
Track.eEnvironment = static_cast<TEnvironmentType>(static_cast<int>(index));
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "overhead", field::choice, section::traffic, group::overhead_line, "Overhead line", "State of the overhead line over the track: the trains coast through a section with no current,\nor lower the pantographs and keep to the speed limit given")};
|
|
property.applies = any;
|
|
property.more = true;
|
|
property.choices = {"Normal", "No current, coasting", "Pantographs lowered, speed limit"};
|
|
property.get = [](TTrack const &Track) { return std::string{Track.fOverhead < 0.f ? "0" : Track.fOverhead == 0.f ? "1" : "2"}; };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double index;
|
|
if (false == parse_number(Text, index) || index < 0.0 || index > 2.0)
|
|
return false;
|
|
auto const mode{static_cast<int>(index)};
|
|
auto const overhead{mode == 0 ? -1.f : mode == 1 ? 0.f : Track.fOverhead > 0.f ? Track.fOverhead : 40.f};
|
|
if (overhead == Track.fOverhead)
|
|
return false;
|
|
editor_track::overhead(Track, overhead);
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "overheadspeed", field::number, section::traffic, group::overhead_line, "Speed with the pantographs lowered (km/h)", "Speed limit of the trains with the pantographs lowered")};
|
|
property.more = true;
|
|
property.applies = [](TTrack const &Track) { return Track.fOverhead > 0.f; };
|
|
property.get = [](TTrack const &Track) { return decimal(Track.fOverhead, 0); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double limit;
|
|
if (false == parse_number(Text, limit))
|
|
return false;
|
|
auto const overhead{std::max(1.f, static_cast<float>(limit))};
|
|
if (overhead == Track.fOverhead)
|
|
return false;
|
|
editor_track::overhead(Track, overhead);
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "circuits", field::text, section::traffic, group::circuits, "Track circuits", "Names of the track circuits (isolated) the track belongs to, separated by spaces.\nA name which doesn't exist yet makes a new circuit. Tracks with vehicles on them keep theirs")};
|
|
property.applies = any;
|
|
property.more = true;
|
|
property.empty = "no circuits";
|
|
property.get = [](TTrack const &Track) {
|
|
std::string text;
|
|
for (auto const *isolated : Track.Isolated)
|
|
text += (text.empty() ? "" : " ") + isolated->asName;
|
|
return text;
|
|
};
|
|
property.set = [get = property.get](TTrack &Track, std::string const &Text) {
|
|
auto const names{words_of(Text)};
|
|
std::string joined;
|
|
for (auto const &name : names)
|
|
joined += (joined.empty() ? "" : " ") + name;
|
|
if (false == Track.Dynamics.empty() || joined == get(Track))
|
|
return false;
|
|
editor_track::isolated(Track, names);
|
|
return true;
|
|
};
|
|
}
|
|
|
|
// look
|
|
{
|
|
auto &property{add(list, "rails", field::texture, section::look, group::rails, "Rails", "Texture of the rails; of both paths of a switch, those of its path 2 can then be set apart.\nA material dragged from the texture browser can be dropped here")};
|
|
property.rebuild = true;
|
|
property.texture = 0;
|
|
property.empty = "No texture";
|
|
property.get = [](TTrack const &Track) { return editor_track::material_name(Track.m_material1); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
auto const material{editor_track::fetch_material(Text)};
|
|
if (material == Track.m_material1 && (Track.eType != tt_Switch || material == Track.m_material2))
|
|
return false;
|
|
Track.m_material1 = material;
|
|
if (Track.eType == tt_Switch)
|
|
Track.m_material2 = material;
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
// a switch keeps the rails of its path 2 in its second texture, its trackbed is m_material3
|
|
auto &property{add(list, "rails2", field::texture, section::look, group::rails, "Rails of path 2",
|
|
"Texture of the rails of path 2 of the switch, from p3 to p4, usually the diverging track.\nPath 1, from p1 to p2, keeps the texture of the rails above.\nA material dragged from the texture browser can be dropped here")};
|
|
property.rebuild = true;
|
|
property.late = true; // after the rails, which set both paths
|
|
property.texture = 0;
|
|
property.empty = "No texture";
|
|
property.applies = [](TTrack const &Track) { return Track.m_visible && Track.eType == tt_Switch; };
|
|
property.get = [](TTrack const &Track) { return editor_track::material_name(Track.m_material2); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
auto const material{editor_track::fetch_material(Text)};
|
|
if (Track.eType != tt_Switch || material == Track.m_material2)
|
|
return false;
|
|
Track.m_material2 = material;
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
// the profiles of the rails the game has (models/tory/railprofile_*), the first: none, the default shape
|
|
static char const *const profiles[] = {"", "s60", "uic60", "s49", "s42", "s39", "s30pn", "s30din", "s24", "s20", "s18", "s14", "s10", "r-65", "ri60n", "lk1"};
|
|
auto &property{add(list, "railprofile", field::choice, section::look, group::rails, "Rail profile",
|
|
"Shape of the rails, the railprofile of the track. It also picks the switch templates of the rail: S60 60E1, S49 49E1.\nNo profile: the default shape, and the templates of all the rails are offered")};
|
|
property.rebuild = true;
|
|
property.more = true;
|
|
property.applies = [](TTrack const &Track) { return Track.m_visible && (Track.iCategoryFlag & 15) == 1; };
|
|
property.choices = {"No profile", "S60 (60E1)", "UIC60", "S49 (49E1)", "S42", "S39", "S30 PN", "S30 DIN", "S24", "S20", "S18", "S14", "S10", "R65", "Ri60N", "LK1"};
|
|
property.get = [](TTrack const &Track) {
|
|
auto const at{std::find_if(std::begin(profiles), std::end(profiles), [&](char const *Name) { return Track.m_profile1.first == Name; })};
|
|
return at != std::end(profiles) ? std::to_string(at - std::begin(profiles)) : Track.m_profile1.first;
|
|
};
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double index;
|
|
if (false == parse_number(Text, index) || index < 0.0 || index >= static_cast<double>(std::size(profiles)))
|
|
return false;
|
|
std::string const name{profiles[static_cast<int>(index)]};
|
|
if (name == Track.m_profile1.first)
|
|
return false;
|
|
Track.rail_profile(name);
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "trackbed", field::texture, section::look, group::trackbed, "Trackbed", "Texture of the trackbed (ballast) under the rails.\nA material dragged from the texture browser can be dropped here")};
|
|
property.rebuild = true;
|
|
property.texture = 1;
|
|
property.empty = "No texture";
|
|
property.applies = [](TTrack const &Track) { return Track.m_visible && (Track.eType != tt_Switch || Track.SwitchExtension != nullptr); };
|
|
property.get = [](TTrack const &Track) { return editor_track::material_name(bed_of(Track)); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
auto const material{editor_track::fetch_material(Text)};
|
|
if (material == bed_of(Track))
|
|
return false;
|
|
if (Track.eType == tt_Switch)
|
|
Track.SwitchExtension->m_material3 = material;
|
|
else
|
|
Track.m_material2 = material;
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "frogs", field::check, section::look, group::frogs, "Frog and check rails made from the rails", "Cuts the rails of the switch at its frog and lays wing rails and check rails.\nOff for a switch whose include has its own model of them")};
|
|
property.rebuild = true;
|
|
property.applies = [](TTrack const &Track) { return Track.eType == tt_Switch && Track.SwitchExtension != nullptr; };
|
|
property.off = false == Global.Frogs;
|
|
property.get = [](TTrack const &Track) { return std::string{Track.SwitchExtension->m_frogs ? "1" : "0"}; };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
auto const frogs{Text == "1"};
|
|
if (frogs == Track.SwitchExtension->m_frogs)
|
|
return false;
|
|
Track.SwitchExtension->m_frogs = frogs;
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "movablenose", field::check, section::look, group::frogs, "Movable nose", "The nose of the frog moves with the blades over to the wing rail of the track set, leaving no gap; the frog has no check rails")};
|
|
property.rebuild = true;
|
|
property.applies = [](TTrack const &Track) { return Track.eType == tt_Switch && Track.SwitchExtension != nullptr; };
|
|
property.off = false == Global.Frogs;
|
|
property.get = [](TTrack const &Track) { return std::string{Track.SwitchExtension->m_movablenose ? "1" : "0"}; };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
auto const movable{Text == "1"};
|
|
if (movable == Track.SwitchExtension->m_movablenose)
|
|
return false;
|
|
Track.SwitchExtension->m_movablenose = movable;
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto const blades = [&](char const *Key, char const *Label, char const *Tooltip, double TSwitchExtension::*Member) {
|
|
auto &property{add(list, Key, field::number, section::look, group::frogs, Label, Tooltip)};
|
|
property.rebuild = true;
|
|
property.applies = [](TTrack const &Track) { return Track.eType == tt_Switch && Track.SwitchExtension != nullptr; };
|
|
property.get = [Member](TTrack const &Track) { return decimal(( *Track.SwitchExtension ).*Member, 3); };
|
|
property.set = [Member](TTrack &Track, std::string const &Text) {
|
|
double value;
|
|
if (false == parse_number(Text, value))
|
|
return false;
|
|
value = std::max(0.0, value);
|
|
if (decimal(value, 3) == decimal(( *Track.SwitchExtension ).*Member, 3))
|
|
return false;
|
|
( *Track.SwitchExtension ).*Member = value;
|
|
return true;
|
|
};
|
|
};
|
|
blades("bladeheel", "Blade heel (m)", "Where the moving blades end, from the start of the switch, as the template of the switch gives it.\n0: as long as the flangeway of 58 mm behind them needs", &TSwitchExtension::m_bladeheel);
|
|
blades("bladethrow", "Blade throw (m)", "How far the tips of the blades move over between the two paths.\n0: the usual 0.160 m", &TSwitchExtension::m_bladethrow);
|
|
}
|
|
add_float(list, "texlength", section::look_bed, group::trackbed_shape, "Texture length (m)", "Length after which the texture of the rails and the trackbed repeats", &TTrack::fTexLength, 2, 0.01f, true);
|
|
{
|
|
auto &property{add(list, "bedheight", field::number, section::look_bed, group::trackbed_shape, "Trackbed height (m)", "Height of the trackbed edge below the rail top")};
|
|
property.rebuild = true;
|
|
property.get = [](TTrack const &Track) { return decimal(editor_track::texture_height(Track), 3); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double height;
|
|
if (false == parse_number(Text, height) || decimal(height, 3) == decimal(editor_track::texture_height(Track), 3))
|
|
return false;
|
|
editor_track::texture_height(Track, static_cast<float>(height));
|
|
return true;
|
|
};
|
|
}
|
|
add_float(list, "bedwidth", section::look_bed, group::trackbed_shape, "Trackbed width (m)", "Width of the trackbed outside of the rail", &TTrack::fTexWidth, 3, lowest, true);
|
|
add_float(list, "bedslope", section::look_bed, group::trackbed_shape, "Trackbed slope (m)", "Horizontal reach of the slope at the outer edge of the trackbed", &TTrack::fTexSlope, 3, lowest, true);
|
|
{
|
|
auto &property{add(list, "sleepermodel", field::text, section::look_sleepers, group::sleepers, "Sleeper model", "Path of the .e3d model, as in the scenery file")};
|
|
property.rebuild = true;
|
|
property.empty = "no model";
|
|
property.get = [](TTrack const &Track) { return Track.m_sleeper_model_name; };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
auto const words{words_of(Text)};
|
|
auto const model{words.empty() ? std::string{} : words.front()};
|
|
if (model == Track.m_sleeper_model_name)
|
|
return false;
|
|
Track.m_sleeper_model_name = model;
|
|
if (model.empty())
|
|
Track.m_sleeper_enabled = false;
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "sleepers", field::check, section::look_sleepers, group::sleepers, "Sleeper models", "Repeats the model along the track, the sleepers in place of the drawn trackbed.\nOnly on the tracks with a sleeper model")};
|
|
property.rebuild = true;
|
|
property.late = true;
|
|
property.get = [](TTrack const &Track) { return std::string{Track.m_sleeper_enabled ? "1" : "0"}; };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
auto const enabled{Text == "1"};
|
|
if (enabled == Track.m_sleeper_enabled || (enabled && Track.m_sleeper_model_name.empty()))
|
|
return false;
|
|
Track.m_sleeper_enabled = enabled;
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "sleeperskin", field::text, section::look_sleepers, group::sleepers, "Sleeper skin", "Replaceable skin of the model, empty: the one of the model")};
|
|
property.rebuild = true;
|
|
property.empty = "of the model";
|
|
property.get = [](TTrack const &Track) { return Track.m_sleeper_skin_name; };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
auto const words{words_of(Text)};
|
|
auto skin{words.empty() ? std::string{} : words.front()};
|
|
if (skin == "none")
|
|
skin.clear();
|
|
if (skin == Track.m_sleeper_skin_name)
|
|
return false;
|
|
Track.m_sleeper_skin_name = skin;
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
// the models of sleepers and timbers of the scenery data, chosen by the name; the first choice leaves the one of eu07.ini
|
|
static std::vector<std::string> sleepermodels;
|
|
static std::vector<std::string> timbermodels;
|
|
if (sleepermodels.empty())
|
|
{
|
|
for (auto const &model : track_sleepers::pack_models())
|
|
(model.timber ? timbermodels : sleepermodels).push_back(model.name);
|
|
}
|
|
auto const choices = [](std::vector<std::string> const &Models) {
|
|
std::vector<char const *> result{"(as in eu07.ini)"};
|
|
for (auto const &model : Models)
|
|
result.push_back(model.c_str());
|
|
return result;
|
|
};
|
|
auto const index_of = [](std::vector<std::string> const &Models, std::string const &Name) {
|
|
auto const found{std::find(Models.begin(), Models.end(), Name)};
|
|
return found == Models.end() ? 0 : static_cast<int>(found - Models.begin()) + 1;
|
|
};
|
|
{
|
|
auto &property{add(list, "sleeperpack", field::choice, section::look_sleepers, group::sleepers, "Sleeper model from the pack", "Model of the sleepers of the track running alone, of the models of sleepers in the scenery data, laid as 3D sleepers\nin place of the one set with sleepers3dplain in eu07.ini. (as in eu07.ini): the one set there")};
|
|
property.rebuild = true;
|
|
property.choices = choices(sleepermodels);
|
|
property.get = [index_of](TTrack const &Track) { return std::to_string(Track.m_sleeper_enabled ? index_of(sleepermodels, Track.m_sleeper_model_name) : 0); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double index;
|
|
if (false == parse_number(Text, index) || index < 0.0 || index > static_cast<double>(sleepermodels.size()))
|
|
return false;
|
|
auto const model{index < 1.0 ? std::string{} : sleepermodels[static_cast<std::size_t>(index) - 1]};
|
|
if (model == (Track.m_sleeper_enabled ? Track.m_sleeper_model_name : std::string{}))
|
|
return false;
|
|
Track.m_sleeper_model_name = model;
|
|
Track.m_sleeper_enabled = (false == model.empty());
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "timbermodel", field::choice, section::look_sleepers, group::sleepers, "Timber model", "Model of the timbers under the switch or the crossing the track is part of, of the models of timbers in the scenery data,\nin place of the one set with sleepers3d in eu07.ini. It's put together from its submodels begin, main and end to the length of each timber")};
|
|
property.rebuild = true;
|
|
property.choices = choices(timbermodels);
|
|
property.get = [index_of](TTrack const &Track) { return std::to_string(index_of(timbermodels, Track.m_timber_model_name)); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double index;
|
|
if (false == parse_number(Text, index) || index < 0.0 || index > static_cast<double>(timbermodels.size()))
|
|
return false;
|
|
auto const model{index < 1.0 ? std::string{} : timbermodels[static_cast<std::size_t>(index) - 1]};
|
|
if (model == Track.m_timber_model_name)
|
|
return false;
|
|
Track.m_timber_model_name = model;
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "timberskin", field::text, section::look_sleepers, group::sleepers, "Timber skin", "Replaceable skin of the timber model, empty: the one of the model")};
|
|
property.rebuild = true;
|
|
property.empty = "of the model";
|
|
property.get = [](TTrack const &Track) { return Track.m_timber_skin_name; };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
auto const words{words_of(Text)};
|
|
auto skin{words.empty() ? std::string{} : words.front()};
|
|
if (skin == "none")
|
|
skin.clear();
|
|
if (skin == Track.m_timber_skin_name)
|
|
return false;
|
|
Track.m_timber_skin_name = skin;
|
|
return true;
|
|
};
|
|
}
|
|
}
|
|
add_float(list, "sleeperspacing", section::look_sleepers, group::sleeper_placement, "Sleeper spacing (m)", "Distance between the sleepers along the track", &TTrack::m_sleeper_frequency, 3, 0.1f, true);
|
|
char const *const offsets[][3] = {{"sleeperside", "Sleeper shift sideways (m)", "Shift of each model sideways, to the right of the track"},
|
|
{"sleeperalong", "Sleeper shift along (m)", "Shift of each model along the track"},
|
|
{"sleeperup", "Sleeper shift up (m)", "Shift of each model up"}};
|
|
for (int axis = 0; axis < 3; ++axis)
|
|
{
|
|
auto &property{add(list, offsets[axis][0], field::number, section::look_sleepers, group::sleeper_placement, offsets[axis][1], offsets[axis][2])};
|
|
property.rebuild = true;
|
|
property.get = [axis](TTrack const &Track) { return decimal(Track.m_sleeper_offset[axis], 3); };
|
|
property.set = [axis](TTrack &Track, std::string const &Text) {
|
|
double offset;
|
|
if (false == parse_number(Text, offset) || decimal(offset, 3) == decimal(Track.m_sleeper_offset[axis], 3))
|
|
return false;
|
|
Track.m_sleeper_offset[axis] = static_cast<float>(offset);
|
|
return true;
|
|
};
|
|
}
|
|
add_float(list, "bedshift", section::look_sleepers, group::sleeper_placement, "Trackbed shift (m)", "Vertical shift of the drawn trackbed under the sleepers, negative lowers it", &TTrack::m_sleeper_ballast_z, 3, lowest, true);
|
|
|
|
// ride and sound
|
|
add_float(list, "friction", section::ride, group::ride_sound, "Friction", "Friction coefficient of the rails, 0.15 for the dry rails; lower values make the wheels slip", &TTrack::fFriction, 3, 0.f, false);
|
|
list.back().applies = any;
|
|
// the rails dip at the joints of an uneven track, every sound distance
|
|
add_float(list, "sounddistance", section::ride, group::ride_sound, "Sound distance (m)", "Distance between the rail joints, sets the rhythm of the wheel clatter; -1: the default", &TTrack::fSoundDistance, 1, -1.f, true);
|
|
list.back().applies = any;
|
|
{
|
|
auto const railway = [](TTrack const &Track) { return (Track.eType == tt_Normal || Track.eType == tt_Switch) && (Track.iCategoryFlag & 15) == 1; };
|
|
auto &property{add(list, "uneven", field::number, section::ride, group::ride_sound, "Unevenness (mm)",
|
|
"Peak deviation of a rail on a 10 m chord. Id-14 limits: 6 mm at 160 km/h, 10 at 120, 18 at 80, 35 at 40.\nWith a sound distance set, the rails also dip at the joints. Joined switches use the lowest of their values")};
|
|
property.applies = railway;
|
|
property.rebuild = true;
|
|
property.get = [](TTrack const &Track) { return decimal(Track.m_uneven.value, 1); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double value;
|
|
if (false == parse_number(Text, value))
|
|
return false;
|
|
auto const clamped{std::clamp(static_cast<float>(value), 0.f, track_uneven::most)};
|
|
if (decimal(clamped, 1) == decimal(Track.m_uneven.value, 1))
|
|
return false;
|
|
Track.m_uneven.value = clamped;
|
|
return true;
|
|
};
|
|
// a component set apart from the unevenness, empty or negative: the unevenness
|
|
auto const component = [&](char const *Key, char const *Label, char const *Tooltip, float track_uneven::attribute::*Member, std::function<bool(TTrack const &)> Applies) {
|
|
auto &property{add(list, Key, field::number, section::ride, group::ride_sound, Label, Tooltip)};
|
|
property.applies = std::move(Applies);
|
|
property.rebuild = true;
|
|
property.empty = "as the unevenness";
|
|
property.get = [Member](TTrack const &Track) { return Track.m_uneven.*Member < 0.f ? std::string{} : decimal(Track.m_uneven.*Member, 1); };
|
|
property.set = [Member](TTrack &Track, std::string const &Text) {
|
|
double value{-1.0};
|
|
if (false == words_of(Text).empty() && false == parse_number(Text, value))
|
|
return false;
|
|
auto const clamped{value < 0.0 ? -1.f : std::min(static_cast<float>(value), track_uneven::most)};
|
|
if (decimal(clamped, 1) == decimal(Track.m_uneven.*Member, 1))
|
|
return false;
|
|
Track.m_uneven.*Member = clamped;
|
|
return true;
|
|
};
|
|
};
|
|
auto const plain = [railway](TTrack const &Track) { return railway(Track) && Track.eType == tt_Normal; };
|
|
auto const turnout = [railway](TTrack const &Track) { return railway(Track) && Track.eType == tt_Switch; };
|
|
char const *const apart{"Set apart from the unevenness, mm. Empty: the unevenness"};
|
|
component("unevenup", "Up and down (mm)", apart, &track_uneven::attribute::up, railway);
|
|
component("unevenside", "Sideways (mm)", apart, &track_uneven::attribute::side, railway);
|
|
component("unevencross", "Cross level (mm)", apart, &track_uneven::attribute::cross, plain);
|
|
component("unevenbranch", "Diverging path (mm)", apart, &track_uneven::attribute::branch, turnout);
|
|
component("unevenbranchup", "Diverging, up and down (mm)", apart, &track_uneven::attribute::branchup, turnout);
|
|
component("unevenbranchside", "Diverging, sideways (mm)", apart, &track_uneven::attribute::branchside, turnout);
|
|
component("jointdip", "Dip at rail joints (mm)", "Depth of the dip at each rail joint, every sound distance. Works with unevenness at 0. Empty: 0.4 of the unevenness",
|
|
&track_uneven::attribute::dip, plain);
|
|
}
|
|
{
|
|
auto &property{add(list, "quality", field::number, section::ride, group::ride_sound, "Quality", "Quality of the track, 20 is the nominal one; lower values give a rougher ride")};
|
|
property.applies = any;
|
|
property.get = [](TTrack const &Track) { return std::to_string(Track.iQualityFlag); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double quality;
|
|
if (false == parse_number(Text, quality) || static_cast<int>(quality) == Track.iQualityFlag)
|
|
return false;
|
|
Track.iQualityFlag = static_cast<int>(quality);
|
|
return true;
|
|
};
|
|
}
|
|
{
|
|
auto &property{add(list, "damage", field::number, section::ride, group::ride_sound, "Damage flags", "0: track in order; any value adds jolts, 128: the vehicles derail on the track")};
|
|
property.applies = any;
|
|
property.get = [](TTrack const &Track) { return std::to_string(Track.iDamageFlag); };
|
|
property.set = [](TTrack &Track, std::string const &Text) {
|
|
double damage;
|
|
if (false == parse_number(Text, damage) || static_cast<int>(damage) == Track.iDamageFlag)
|
|
return false;
|
|
editor_track::damage(Track, static_cast<int>(damage));
|
|
return true;
|
|
};
|
|
}
|
|
add_float(list, "gauge", section::ride, group::ride_sound, "Gauge (m)", "Gauge of the track, 1.435 m for the standard gauge", &TTrack::fTrackWidth, 3, 0.01f, true);
|
|
list.back().applies = any;
|
|
return list;
|
|
}
|
|
|
|
std::vector<track_property> const &properties()
|
|
{
|
|
static std::vector<track_property> const list{make_properties()};
|
|
return list;
|
|
}
|
|
|
|
using value_counts = std::vector<std::pair<std::string, int>>;
|
|
|
|
// values of the property over the tracks it applies to, each with the number of the tracks having it, the most common first
|
|
value_counts values_of(track_property const &Property, std::vector<TTrack *> const &Tracks)
|
|
{
|
|
std::map<std::string, int> counted;
|
|
for (auto const *track : Tracks)
|
|
if (track != nullptr && false == track->m_editorremoved && Property.applies(*track))
|
|
++counted[Property.get(*track)];
|
|
value_counts result(counted.begin(), counted.end());
|
|
std::stable_sort(result.begin(), result.end(), [](auto const &Left, auto const &Right) { return Left.second > Right.second; });
|
|
return result;
|
|
}
|
|
|
|
// a value as the user reads it
|
|
std::string shown_value(track_property const &Property, std::string const &Value)
|
|
{
|
|
switch (Property.kind)
|
|
{
|
|
case field::choice:
|
|
{
|
|
auto const index{std::atoi(Value.c_str())};
|
|
return index >= 0 && index < static_cast<int>(Property.choices.size()) ? std::string{STR_C(Property.choices[index])} : Value;
|
|
}
|
|
case field::check: return Value == "1" ? STR_C("yes") : STR_C("no");
|
|
default: return Value.empty() || Value == "none" ? std::string{STR_C(Property.empty != nullptr ? Property.empty : "(none)")} : Value;
|
|
}
|
|
}
|
|
|
|
std::string tracks_count(std::size_t const Count)
|
|
{
|
|
return format(STR_P("%d track", "%d tracks", static_cast<long long>(Count)), static_cast<int>(Count));
|
|
}
|
|
|
|
std::size_t tracks_signature(std::vector<TTrack *> const &Tracks)
|
|
{
|
|
std::size_t signature{Tracks.size()};
|
|
for (auto const *track : Tracks)
|
|
signature = signature * 31 + reinterpret_cast<std::uintptr_t>(track);
|
|
return signature;
|
|
}
|
|
|
|
// the tooltip of a row: what the property is, and the values of the tracks when they differ
|
|
void row_tooltip(track_property const &Property, value_counts const &Values, std::size_t const Applies, std::size_t const Selected)
|
|
{
|
|
// not over a field being typed into, it would cover the rows under it
|
|
if (false == ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort) || ImGui::IsItemActive())
|
|
return;
|
|
ImGui::BeginTooltip();
|
|
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 30.0f);
|
|
ImGui::TextUnformatted(STR_C(Property.tooltip));
|
|
if (Applies < Selected)
|
|
ImGui::TextColored(ImVec4(1.0f, 0.75f, 0.3f, 1.0f), STR_C("Applies to %d of the %d selected tracks, the others don't have it"), static_cast<int>(Applies), static_cast<int>(Selected));
|
|
if (Values.size() > 1)
|
|
{
|
|
ImGui::Separator();
|
|
ImGui::TextUnformatted(STR_C("The tracks differ:"));
|
|
for (std::size_t i = 0; i < Values.size() && i < kListedValues; ++i)
|
|
ImGui::BulletText("%s — %s", shown_value(Property, Values[i].first).c_str(), tracks_count(static_cast<std::size_t>(Values[i].second)).c_str());
|
|
if (Values.size() > kListedValues)
|
|
ImGui::BulletText(STR_C("and %d more values"), static_cast<int>(Values.size() - kListedValues));
|
|
ImGui::TextDisabled("%s", STR_C("A value given here goes to all of them"));
|
|
}
|
|
ImGui::PopTextWrapPos();
|
|
ImGui::EndTooltip();
|
|
}
|
|
|
|
// the values of the properties of the track, as they're copied and kept by the presets
|
|
std::vector<std::pair<std::string, std::string>> values_of_track(TTrack const &Track)
|
|
{
|
|
std::vector<std::pair<std::string, std::string>> values;
|
|
for (auto const &property : properties())
|
|
if (false == property.off && property.applies(Track))
|
|
values.emplace_back(property.key, property.get(Track));
|
|
return values;
|
|
}
|
|
|
|
// the groups some of the values are of
|
|
std::array<bool, group_count> groups_in(std::vector<std::pair<std::string, std::string>> const &Values)
|
|
{
|
|
std::array<bool, group_count> groups{};
|
|
for (auto const &[key, value] : Values)
|
|
for (auto const &property : properties())
|
|
if (key == property.key)
|
|
groups[property.group] = true;
|
|
return groups;
|
|
}
|
|
|
|
// the groups the preset overwrites, from the names it keeps them by
|
|
std::array<bool, group_count> groups_of(editor_track_preset const &Preset)
|
|
{
|
|
std::array<bool, group_count> groups{};
|
|
for (auto const &key : Preset.groups)
|
|
for (int group = 0; group < group_count; ++group)
|
|
if (key == kGroupKeys[group])
|
|
groups[group] = true;
|
|
return groups;
|
|
}
|
|
|
|
std::vector<std::string> group_keys(std::array<bool, group_count> const &Groups)
|
|
{
|
|
std::vector<std::string> keys;
|
|
for (int group = 0; group < group_count; ++group)
|
|
if (Groups[group])
|
|
keys.emplace_back(kGroupKeys[group]);
|
|
return keys;
|
|
}
|
|
|
|
// what the preset puts on the tracks, for its tooltip
|
|
std::string preset_summary(editor_track_preset const &Preset)
|
|
{
|
|
auto const groups{groups_of(Preset)};
|
|
auto const available{groups_in(Preset.values)};
|
|
std::string overwrites;
|
|
for (int group = 0; group < group_count; ++group)
|
|
if (groups[group] && available[group])
|
|
overwrites += (overwrites.empty() ? "" : ", ") + std::string{STR_C(kGroupLabels[group])};
|
|
auto text{overwrites.empty() ? std::string{STR_C("Overwrites nothing: choose what it overwrites in the inspector")} : format(STR_C("Overwrites: %s"), overwrites.c_str())};
|
|
if (false == Preset.from.empty())
|
|
text += "\n" + format(STR_C("Taken from %s"), Preset.from.c_str());
|
|
return text;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::vector<TTrack *> editor_mode::selected_tracks()
|
|
{
|
|
track_set_prune();
|
|
if (m_track_set.size() >= 2)
|
|
return m_track_set;
|
|
if (auto *track{selected_track()}; track != nullptr && false == track->m_editorremoved)
|
|
return {track};
|
|
return {};
|
|
}
|
|
|
|
void editor_mode::select_tracks(std::vector<TTrack *> const &Tracks)
|
|
{
|
|
// the tracks may be the set itself
|
|
std::vector<TTrack *> tracks;
|
|
for (auto *track : Tracks)
|
|
if (track != nullptr && false == track->m_editorremoved && std::find(tracks.begin(), tracks.end(), track) == tracks.end())
|
|
tracks.push_back(track);
|
|
m_track_set.clear();
|
|
if (tracks.size() >= 2)
|
|
m_track_set = tracks;
|
|
m_node = tracks.empty() ? nullptr : tracks.back();
|
|
m_track_point = {};
|
|
ui()->set_node(m_node);
|
|
}
|
|
|
|
std::size_t editor_mode::tracks_apply(std::vector<TTrack *> const &Tracks, std::function<bool(TTrack &)> const &Change, bool const Rebuild, char const *What)
|
|
{
|
|
std::vector<std::pair<TTrack *, editor_track::state>> states;
|
|
std::vector<TTrack *> changed;
|
|
std::size_t locked{0};
|
|
for (auto *track : Tracks)
|
|
{
|
|
if (track == nullptr || track->m_editorremoved)
|
|
continue;
|
|
if (false == scene::Layers.editable(track))
|
|
{
|
|
++locked;
|
|
continue;
|
|
}
|
|
auto const before{editor_track::capture(*track)};
|
|
if (false == Change(*track))
|
|
continue;
|
|
states.emplace_back(track, before);
|
|
changed.push_back(track);
|
|
}
|
|
if (false == changed.empty())
|
|
{
|
|
push_track_snapshot(std::move(states));
|
|
for (auto *track : changed)
|
|
{
|
|
if (Rebuild)
|
|
editor_track::commit_parameters(*track);
|
|
else
|
|
track->mark_dirty();
|
|
}
|
|
++m_track_values.generation;
|
|
}
|
|
if (Tracks.size() > 1 || locked > 0)
|
|
{
|
|
auto status{changed.empty() ? format(STR_C("%s: nothing to change"), STR_C(What)) : format(STR_P("%s: changed on %d track, Ctrl+Z takes it back", "%s: changed on %d tracks, Ctrl+Z takes it back", static_cast<long long>(changed.size())), STR_C(What), static_cast<int>(changed.size()))};
|
|
if (locked > 0)
|
|
status += format(STR_C(", %zu in the files which can't be changed are left as they are"), locked);
|
|
ui()->set_status(status, locked > 0);
|
|
}
|
|
return changed.size();
|
|
}
|
|
|
|
int editor_mode::track_property_index(std::string const &Key) const
|
|
{
|
|
auto const &list{properties()};
|
|
for (std::size_t i = 0; i < list.size(); ++i)
|
|
if (Key == list[i].key)
|
|
return static_cast<int>(i);
|
|
return -1;
|
|
}
|
|
|
|
std::size_t editor_mode::track_property_apply(int const Property, std::vector<TTrack *> const &Tracks, std::string const &Text)
|
|
{
|
|
auto const &property{properties()[Property]};
|
|
std::string const key{property.key};
|
|
std::vector<TTrack *> targets;
|
|
std::size_t without{0};
|
|
for (auto *track : Tracks)
|
|
{
|
|
if (track == nullptr || track->m_editorremoved)
|
|
continue;
|
|
if (false == property.off && property.applies(*track))
|
|
targets.push_back(track);
|
|
else
|
|
++without;
|
|
}
|
|
auto const tell = [&](std::string const &Outcome, bool const Refused) {
|
|
m_track_outcome = {Property, Outcome, Refused, tracks_signature(Tracks), m_history.size(), ImGui::GetTime()};
|
|
ui()->set_status(format("%s: %s", STR_C(property.label), Outcome.c_str()), Refused);
|
|
};
|
|
// only the speed takes an empty field, for no limit
|
|
if (double number; property.kind == field::number && false == parse_number(Text, number) && (property.empty == nullptr || false == words_of(Text).empty()))
|
|
{
|
|
tell(format(STR_C("\"%s\" isn't a number"), Text.c_str()), true);
|
|
return 0;
|
|
}
|
|
std::size_t same{0};
|
|
std::size_t occupied{0};
|
|
std::size_t nomodel{0};
|
|
auto const changed{tracks_apply(targets, [&](TTrack &Track) {
|
|
if (property.set(Track, Text))
|
|
return true;
|
|
if (key == "circuits" && false == Track.Dynamics.empty() && property.get(Track) != Text)
|
|
++occupied;
|
|
else if (key == "sleepers" && Text == "1" && Track.m_sleeper_model_name.empty())
|
|
++nomodel;
|
|
else
|
|
++same;
|
|
return false;
|
|
}, property.rebuild, property.label)};
|
|
auto const locked{targets.size() - changed - same - occupied - nomodel};
|
|
|
|
auto outcome{changed > 0 ? format(STR_P("Set on %d track, Ctrl+Z takes it back", "Set on %d tracks, Ctrl+Z takes it back", static_cast<long long>(changed)), static_cast<int>(changed)) : std::string{STR_C("Nothing changed")}};
|
|
auto const also = [&](std::size_t const Count, char const *Format) {
|
|
if (Count > 0)
|
|
outcome += "; " + format(Format, static_cast<int>(Count));
|
|
};
|
|
also(same, STR_P("%d track has it already", "%d tracks have it already", static_cast<long long>(same)));
|
|
also(without, STR_P("%d track doesn't have it", "%d tracks don't have it", static_cast<long long>(without)));
|
|
also(occupied, STR_P("%d track has vehicles on it", "%d tracks have vehicles on them", static_cast<long long>(occupied)));
|
|
also(nomodel, STR_P("%d track has no sleeper model", "%d tracks have no sleeper model", static_cast<long long>(nomodel)));
|
|
if (locked > 0)
|
|
{
|
|
std::string reason;
|
|
for (auto const *track : targets)
|
|
if (false == scene::Layers.editable(track, &reason))
|
|
break;
|
|
outcome += "; " + format(STR_P("%d track can't be changed: %s", "%d tracks can't be changed: %s", static_cast<long long>(locked)), static_cast<int>(locked), reason.c_str());
|
|
}
|
|
tell(outcome, locked > 0 || occupied > 0 || nomodel > 0);
|
|
return changed;
|
|
}
|
|
|
|
void editor_mode::track_field_finish()
|
|
{
|
|
// what was typed goes on the tracks the field was started on
|
|
auto &edit{m_track_field};
|
|
if (edit.property < 0)
|
|
return;
|
|
auto const property{edit.property};
|
|
auto const tracks{edit.tracks};
|
|
std::string const typed{edit.text.data()};
|
|
auto const initial{edit.initial};
|
|
edit = {};
|
|
if (typed != initial)
|
|
track_property_apply(property, tracks, typed);
|
|
}
|
|
|
|
void editor_mode::track_field_settle()
|
|
{
|
|
// a field left without being drawn again, the selection gone or folded away: what was typed is taken as it is
|
|
auto const &edit{m_track_field};
|
|
if (edit.property < 0 || edit.frame + 1 >= ImGui::GetFrameCount())
|
|
return;
|
|
track_field_finish();
|
|
}
|
|
|
|
void editor_mode::track_textures_scan()
|
|
{
|
|
for (int kind = 0; kind < 2; ++kind)
|
|
{
|
|
std::map<material_handle, int> counted;
|
|
for (auto const *track : simulation::Paths.sequence())
|
|
if (track != nullptr && rail(*track) && track->m_visible && (kind == 0 || track->eType != tt_Switch || track->SwitchExtension))
|
|
++counted[kind == 0 ? track->m_material1 : bed_of(*track)];
|
|
auto &list{m_track_textures[kind]};
|
|
list.clear();
|
|
for (auto const &[material, count] : counted)
|
|
if (material != null_handle)
|
|
list.push_back({material, editor_track::material_name(material), count, format(STR_C("In this scenery: %s"), tracks_count(static_cast<std::size_t>(count)).c_str())});
|
|
std::stable_sort(list.begin(), list.end(), [](auto const &Left, auto const &Right) { return Left.count > Right.count; });
|
|
}
|
|
}
|
|
|
|
void editor_mode::textures_index()
|
|
{
|
|
if (m_texture_indexed)
|
|
return;
|
|
m_texture_indexed = true;
|
|
std::map<std::string, bool> found; // the name, whether there's a .mat of it
|
|
std::filesystem::path const root{paths::textures};
|
|
auto const options{std::filesystem::directory_options::follow_directory_symlink | std::filesystem::directory_options::skip_permission_denied};
|
|
std::error_code error;
|
|
for (std::filesystem::recursive_directory_iterator entry{root, options, error}, end; false == static_cast<bool>(error) && entry != end; entry.increment(error))
|
|
{
|
|
std::error_code ignored;
|
|
try
|
|
{
|
|
if (entry->is_directory(ignored))
|
|
{
|
|
// the normal maps go with their textures, not on a track by themselves; the previews of the node bank, thousands of
|
|
// them, are images of models and not textures of anything
|
|
auto const folder{lowered(entry->path().filename().string())};
|
|
if (folder == "normals" || (folder == "previews" && entry.depth() == 0))
|
|
entry.disable_recursion_pending();
|
|
continue;
|
|
}
|
|
auto const extension{lowered(entry->path().extension().string())};
|
|
auto const material{extension == ".mat"};
|
|
if (false == material && false == texture_extension(extension))
|
|
continue;
|
|
auto path{entry->path().lexically_relative(root)};
|
|
path.replace_extension();
|
|
auto const name{path.generic_string()};
|
|
// names with spaces can't be put in a scenery file
|
|
if (name.find(' ') != std::string::npos)
|
|
continue;
|
|
auto &known{found[name]};
|
|
known = known || material;
|
|
}
|
|
catch (std::exception const &)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
m_texture_index.clear();
|
|
m_texture_index.reserve(found.size());
|
|
for (auto const &[name, material] : found)
|
|
m_texture_index.push_back({name, lowered(name), material});
|
|
}
|
|
|
|
std::uint64_t editor_mode::track_texture_image(int const Kind, std::string const &Name)
|
|
{
|
|
return texture_image(m_track_textures[Kind], Name);
|
|
}
|
|
|
|
std::uint64_t editor_mode::texture_image(std::vector<texture_use> const &Used, std::string const &Name)
|
|
{
|
|
for (auto const &use : Used)
|
|
if (use.material != null_handle && use.name == Name)
|
|
return material_image(use.material);
|
|
auto const &index{m_texture_index};
|
|
auto const found{std::lower_bound(index.begin(), index.end(), Name, [](texture_index_entry const &Entry, std::string const &Wanted) { return Entry.name < Wanted; })};
|
|
texture_browser::item item;
|
|
item.name = Name;
|
|
item.material = found == index.end() || found->name != Name || found->material;
|
|
return texture_thumbnail(item);
|
|
}
|
|
|
|
bool editor_mode::track_texture_picker(int const Kind, std::string const &Current, bool const Mixed, std::string &Chosen)
|
|
{
|
|
return texture_picker({STR_C("On the tracks of this scenery:"),
|
|
[this, Kind]() {
|
|
track_textures_scan();
|
|
return m_track_textures[Kind];
|
|
},
|
|
STR_C("No texture"), "none"},
|
|
Current, Mixed, Chosen);
|
|
}
|
|
|
|
bool editor_mode::texture_picker(texture_offer const &Offer, std::string const &Current, bool const Mixed, std::string &Chosen)
|
|
{
|
|
auto &filter{m_texture_filter};
|
|
if (ImGui::IsWindowAppearing())
|
|
{
|
|
m_texture_used = Offer.used();
|
|
textures_index();
|
|
filter[0] = '\0';
|
|
m_texture_hovered.clear();
|
|
ImGui::SetKeyboardFocusHere();
|
|
}
|
|
ImGui::SetNextItemWidth(-FLT_MIN);
|
|
auto const entered{ImGui::InputTextWithHint("##filter", STR_C("find a texture"), filter.data(), filter.size(), ImGuiInputTextFlags_EnterReturnsTrue)};
|
|
auto const words{words_of(lowered(filter.data()))};
|
|
auto const matches = [&](std::string const &Lowered) {
|
|
return std::all_of(words.begin(), words.end(), [&](std::string const &Word) { return Lowered.find(Word) != std::string::npos; });
|
|
};
|
|
// those in use first, then every other one of the folder
|
|
std::vector<texture_use const *> used;
|
|
std::set<std::string> usednames;
|
|
for (auto const &use : m_texture_used)
|
|
{
|
|
auto const name{lowered(use.name)};
|
|
usednames.insert(name);
|
|
if (matches(name))
|
|
used.push_back(&use);
|
|
}
|
|
std::vector<texture_index_entry const *> files;
|
|
for (auto const &entry : m_texture_index)
|
|
if (usednames.count(entry.lowered) == 0 && matches(entry.lowered))
|
|
files.push_back(&entry);
|
|
std::string const first{false == used.empty() ? used.front()->name : false == files.empty() ? files.front()->name : std::string{}};
|
|
if (entered && false == words.empty() && false == first.empty())
|
|
{
|
|
Chosen = first;
|
|
return true;
|
|
}
|
|
|
|
// large: the tile under the mouse, or what Enter would choose, or the current texture
|
|
auto const current{lowered(Current)};
|
|
auto const shown{false == m_texture_hovered.empty() ? m_texture_hovered : false == words.empty() ? first : Mixed ? std::string{} : Current};
|
|
m_texture_hovered.clear();
|
|
auto const big{std::round(ImGui::GetFontSize() * 10.0f)};
|
|
auto const corner{ImGui::GetCursorScreenPos()};
|
|
if (auto const image{shown.empty() || shown == "none" ? 0 : texture_image(m_texture_used, shown)}; image != 0)
|
|
ImGui::Image(static_cast<ImTextureID>(image), ImVec2(big, big), ImVec2(0, 1), ImVec2(1, 0));
|
|
else
|
|
{
|
|
ImGui::Dummy(ImVec2(big, big));
|
|
ImGui::GetWindowDrawList()->AddRect(corner, ImVec2(corner.x + big, corner.y + big), ImGui::GetColorU32(ImGuiCol_Border));
|
|
}
|
|
ImGui::SameLine();
|
|
ImGui::BeginGroup();
|
|
ImGui::PushTextWrapPos(0.0f);
|
|
if (shown.empty() && false == words.empty())
|
|
ImGui::TextDisabled("%s", STR_C("No texture of this name"));
|
|
else if (shown.empty() && Mixed)
|
|
ImGui::TextDisabled("%s", STR_C("The selected tracks have different ones"));
|
|
else if (shown.empty() || shown == "none")
|
|
ImGui::TextUnformatted(Offer.none != nullptr ? Offer.none : STR_C("No texture"));
|
|
else
|
|
{
|
|
ImGui::TextUnformatted(shown.c_str());
|
|
auto const use{std::find_if(m_texture_used.begin(), m_texture_used.end(), [&](texture_use const &Use) { return Use.name == shown; })};
|
|
auto const isset{false == Mixed && lowered(shown) == current};
|
|
if (use != m_texture_used.end())
|
|
ImGui::TextDisabled("%s", use->note.c_str());
|
|
else if (false == isset)
|
|
ImGui::TextDisabled("%s", STR_C("From the textures folder, not used here yet"));
|
|
if (isset)
|
|
ImGui::TextDisabled("%s", STR_C("Set now"));
|
|
if (false == words.empty() && shown == first)
|
|
ImGui::TextColored(ImVec4(0.55f, 0.8f, 1.0f, 1.0f), "%s", STR_C("Enter chooses this one"));
|
|
}
|
|
ImGui::PopTextWrapPos();
|
|
ImGui::EndGroup();
|
|
|
|
// the tiles, a click chooses
|
|
auto const tile{std::round(ImGui::GetFontSize() * 6.0f)};
|
|
auto const label{ImGui::GetTextLineHeight() * 2.0f};
|
|
auto const spacing{ImGui::GetStyle().ItemSpacing};
|
|
bool picked{false};
|
|
ImGui::BeginChild("##tiles", ImVec2(0.0f, std::round((tile + label + spacing.y) * 4.5f)), ImGuiChildFlags_Borders);
|
|
auto const columns{std::max(1, static_cast<int>((ImGui::GetContentRegionAvail().x + spacing.x) / (tile + spacing.x)))};
|
|
int column{0};
|
|
auto const draw_tile = [&](std::string const &Name, int const Count) {
|
|
if (column++ % columns != 0)
|
|
ImGui::SameLine();
|
|
ImGui::PushID(Name.c_str());
|
|
auto const low{ImGui::GetCursorScreenPos()};
|
|
if (ImGui::Selectable("##tile", false == Mixed && lowered(Name) == current, ImGuiSelectableFlags_None, ImVec2(tile, tile + label)))
|
|
{
|
|
Chosen = Name;
|
|
picked = true;
|
|
}
|
|
if (ImGui::IsItemHovered())
|
|
m_texture_hovered = Name;
|
|
if (ImGui::IsItemVisible())
|
|
{
|
|
auto *drawlist{ImGui::GetWindowDrawList()};
|
|
if (auto const image{texture_image(m_texture_used, Name)}; image != 0)
|
|
drawlist->AddImage(static_cast<ImTextureID>(image), low, ImVec2(low.x + tile, low.y + tile), ImVec2(0, 1), ImVec2(1, 0));
|
|
else
|
|
drawlist->AddRect(low, ImVec2(low.x + tile, low.y + tile), ImGui::GetColorU32(ImGuiCol_Border));
|
|
auto const slash{Name.find_last_of('/')};
|
|
auto const [line1, line2]{two_lines(slash == std::string::npos ? Name : Name.substr(slash + 1), tile)};
|
|
drawlist->AddText(ImVec2(low.x, low.y + tile), ImGui::GetColorU32(ImGuiCol_Text), line1.c_str());
|
|
drawlist->AddText(ImVec2(low.x, low.y + tile + ImGui::GetTextLineHeight()), ImGui::GetColorU32(ImGuiCol_Text), line2.c_str());
|
|
if (Count > 0)
|
|
{
|
|
// how many tracks of the scenery have it
|
|
auto const badge{format("%d", Count)};
|
|
auto const size{ImGui::CalcTextSize(badge.c_str())};
|
|
drawlist->AddRectFilled(low, ImVec2(low.x + size.x + 6.0f, low.y + size.y + 2.0f), IM_COL32(0, 0, 0, 190), 3.0f);
|
|
drawlist->AddText(ImVec2(low.x + 3.0f, low.y + 1.0f), IM_COL32(255, 220, 120, 255), badge.c_str());
|
|
}
|
|
}
|
|
ImGui::PopID();
|
|
};
|
|
if (false == used.empty())
|
|
{
|
|
ImGui::TextDisabled("%s", Offer.title);
|
|
for (auto const *use : used)
|
|
draw_tile(use->name, use->count);
|
|
}
|
|
if (false == files.empty())
|
|
{
|
|
ImGui::TextDisabled("%s", STR_C("In the textures folder:"));
|
|
// thousands of them: only the rows in sight are drawn
|
|
ImGuiListClipper clipper;
|
|
clipper.Begin((static_cast<int>(files.size()) + columns - 1) / columns, tile + label + spacing.y);
|
|
while (clipper.Step())
|
|
for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; ++row)
|
|
{
|
|
column = 0;
|
|
for (int i = row * columns; i < (row + 1) * columns && i < static_cast<int>(files.size()); ++i)
|
|
draw_tile(files[i]->name, 0);
|
|
}
|
|
}
|
|
if (used.empty() && files.empty())
|
|
ImGui::TextDisabled("%s", STR_C("No texture of this name"));
|
|
ImGui::EndChild();
|
|
if (Offer.none != nullptr && ImGui::Selectable(Offer.none, false == Mixed && (Current == Offer.none_name || Current == "none")))
|
|
{
|
|
Chosen = Offer.none_name;
|
|
picked = true;
|
|
}
|
|
return picked;
|
|
}
|
|
|
|
bool editor_mode::texture_field(char const *Label, float const Width, std::string &Value, texture_offer const &Offer)
|
|
{
|
|
bool changed{false};
|
|
auto const none{Value.empty() || Value == "none"};
|
|
auto const side{ImGui::GetFrameHeight()};
|
|
auto const corner{ImGui::GetCursorScreenPos()};
|
|
if (auto const image{none ? 0 : texture_image({}, Value)}; image != 0)
|
|
{
|
|
ImGui::Image(static_cast<ImTextureID>(image), ImVec2(side, side), ImVec2(0, 1), ImVec2(1, 0));
|
|
if (ImGui::IsItemHovered())
|
|
{
|
|
auto const big{std::round(ImGui::GetFontSize() * 14.0f)};
|
|
ImGui::BeginTooltip();
|
|
ImGui::Image(static_cast<ImTextureID>(image), ImVec2(big, big), ImVec2(0, 1), ImVec2(1, 0));
|
|
ImGui::TextUnformatted(Value.c_str());
|
|
ImGui::EndTooltip();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ImGui::Dummy(ImVec2(side, side));
|
|
ImGui::GetWindowDrawList()->AddRect(corner, ImVec2(corner.x + side, corner.y + side), ImGui::GetColorU32(ImGuiCol_Border));
|
|
}
|
|
ImGui::SameLine(0.0f, 4.0f);
|
|
ImGui::SetNextItemWidth(Width);
|
|
ImGui::SetNextWindowSizeConstraints(ImVec2(ImGui::GetFontSize() * 36.0f, 0.0f), ImVec2(FLT_MAX, FLT_MAX));
|
|
auto const slash{Value.find_last_of('/')};
|
|
auto const preview{none ? std::string{Offer.none != nullptr ? Offer.none : ""} : slash == std::string::npos ? Value : Value.substr(slash + 1)};
|
|
if (ImGui::BeginCombo(Label, preview.c_str(), ImGuiComboFlags_HeightLargest))
|
|
{
|
|
if (std::string chosen; texture_picker(Offer, Value, false, chosen))
|
|
{
|
|
changed = chosen != Value;
|
|
Value = chosen;
|
|
ImGui::CloseCurrentPopup();
|
|
}
|
|
ImGui::EndCombo();
|
|
}
|
|
if (std::string dropped; material_drop(dropped))
|
|
{
|
|
changed = changed || dropped != Value;
|
|
Value = dropped;
|
|
}
|
|
return changed;
|
|
}
|
|
|
|
bool editor_mode::texture_field(char const *Label, float const Width, char *Buffer, std::size_t const Size, texture_offer const &Offer)
|
|
{
|
|
std::string value{Buffer};
|
|
if (false == texture_field(Label, Width, value, Offer))
|
|
return false;
|
|
std::snprintf(Buffer, Size, "%s", value.c_str());
|
|
return true;
|
|
}
|
|
|
|
void editor_mode::render_track_properties(std::vector<TTrack *> const &Tracks)
|
|
{
|
|
auto const &list{properties()};
|
|
auto &cache{m_track_values};
|
|
// the values are gathered again when the selection or the tracks change, for a large selection not on every frame
|
|
auto const signature{tracks_signature(Tracks)};
|
|
auto const stale{cache.signature != signature || cache.generation_seen != cache.generation || cache.history != m_history.size() || Tracks.size() <= kLargeSelection || ImGui::GetFrameCount() - cache.frame > 15};
|
|
if (stale)
|
|
{
|
|
cache.signature = signature;
|
|
cache.generation_seen = cache.generation;
|
|
cache.history = m_history.size();
|
|
cache.frame = ImGui::GetFrameCount();
|
|
cache.values.assign(list.size(), {});
|
|
cache.applies.assign(list.size(), 0);
|
|
for (std::size_t i = 0; i < list.size(); ++i)
|
|
{
|
|
auto values{values_of(list[i], Tracks)};
|
|
std::size_t applies{0};
|
|
for (auto const &value : values)
|
|
applies += static_cast<std::size_t>(value.second);
|
|
cache.applies[i] = applies;
|
|
cache.values[i] = std::move(values);
|
|
}
|
|
cache.unsaved = static_cast<std::size_t>(std::count_if(Tracks.begin(), Tracks.end(), [](TTrack const *Track) { return Track != nullptr && Track->dirty(); }));
|
|
}
|
|
|
|
// the lines under the rows come and go with no mouse button held: rows moved under a click would give it to another row
|
|
if (false == ImGui::IsAnyMouseDown())
|
|
{
|
|
m_track_notes.clear();
|
|
auto const &edit{m_track_field};
|
|
auto const typing{edit.property >= 0 && edit.initial != edit.text.data()};
|
|
if (typing)
|
|
{
|
|
auto const count{cache.applies[edit.property]};
|
|
m_track_notes.push_back({edit.property, format(STR_P("Enter or leaving the field sets it on %d track, Esc gives up", "Enter or leaving the field sets it on %d tracks, Esc gives up", static_cast<long long>(count)), static_cast<int>(count)), track_note::kind::typing});
|
|
}
|
|
auto const &outcome{m_track_outcome};
|
|
if (outcome.property >= 0 && (false == typing || outcome.property != edit.property) && outcome.signature == signature && outcome.history == m_history.size() && ImGui::GetTime() - outcome.time < kOutcomeSeconds)
|
|
m_track_notes.push_back({outcome.property, outcome.text, outcome.refused ? track_note::kind::refused : track_note::kind::done});
|
|
}
|
|
// the tooltip of a row would cover the line under it
|
|
auto const row_help = [&](std::size_t const Index) {
|
|
if (std::none_of(m_track_notes.begin(), m_track_notes.end(), [&](track_note const &Note) { return Note.property == static_cast<int>(Index); }))
|
|
row_tooltip(list[Index], cache.values[Index], cache.applies[Index], Tracks.size());
|
|
};
|
|
|
|
auto const row_label = [&](std::size_t const Index) {
|
|
auto const &property{list[Index]};
|
|
ImGui::TableNextRow();
|
|
ImGui::TableSetColumnIndex(0);
|
|
ImGui::AlignTextToFramePadding();
|
|
auto const partial{cache.applies[Index] < Tracks.size()};
|
|
auto const mixed{cache.values[Index].size() > 1};
|
|
ImGui::PushTextWrapPos(0.0f);
|
|
if (mixed)
|
|
ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.45f, 1.0f), "%s", STR_C(property.label));
|
|
else
|
|
ImGui::TextUnformatted(STR_C(property.label));
|
|
ImGui::PopTextWrapPos();
|
|
if (partial)
|
|
{
|
|
ImGui::SameLine(0.0f, 4.0f);
|
|
ImGui::TextDisabled("(%d)", static_cast<int>(cache.applies[Index]));
|
|
}
|
|
row_help(Index);
|
|
ImGui::TableSetColumnIndex(1);
|
|
ImGui::SetNextItemWidth(-FLT_MIN);
|
|
};
|
|
|
|
auto const text_row = [&](std::size_t const Index) {
|
|
auto const &property{list[Index]};
|
|
auto const &values{cache.values[Index]};
|
|
auto &edit{m_track_field};
|
|
auto const editing{edit.property == static_cast<int>(Index)};
|
|
std::array<char, 256> shown{};
|
|
if (values.size() == 1)
|
|
std::snprintf(shown.data(), shown.size(), "%s", values.front().first == "none" ? "" : values.front().first.c_str());
|
|
auto *buffer{editing ? edit.text.data() : shown.data()};
|
|
auto const hint{values.size() > 1 ? format(STR_C("different (%d)"), static_cast<int>(values.size())) : property.empty != nullptr ? std::string{STR_C(property.empty)} : std::string{}};
|
|
// a number clicked is typed anew, the digits wouldn't add up to what's meant going after the old ones
|
|
ImGui::InputTextWithHint("##value", hint.c_str(), buffer, shown.size(), property.kind == field::number ? ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_AutoSelectAll : 0);
|
|
if (ImGui::IsItemActivated())
|
|
{
|
|
// the field left for this one, lower down, is drawn after it: it would find its place here taken and what was typed lost
|
|
std::string const text{buffer};
|
|
track_field_finish();
|
|
edit.property = static_cast<int>(Index);
|
|
edit.tracks = Tracks;
|
|
edit.initial = text;
|
|
std::snprintf(edit.text.data(), edit.text.size(), "%s", text.c_str());
|
|
}
|
|
if (edit.property == static_cast<int>(Index))
|
|
edit.frame = ImGui::GetFrameCount();
|
|
row_help(Index);
|
|
if (ImGui::IsItemDeactivated() && edit.property == static_cast<int>(Index))
|
|
track_field_finish();
|
|
};
|
|
|
|
auto const choice_row = [&](std::size_t const Index) {
|
|
auto const &property{list[Index]};
|
|
auto const &values{cache.values[Index]};
|
|
auto const current{values.size() == 1 ? std::atoi(values.front().first.c_str()) : -1};
|
|
auto const preview{current >= 0 && current < static_cast<int>(property.choices.size()) ? std::string{STR_C(property.choices[current])} : format(STR_C("different (%d)"), static_cast<int>(values.size()))};
|
|
if (ImGui::BeginCombo("##value", preview.c_str()))
|
|
{
|
|
for (int i = 0; i < static_cast<int>(property.choices.size()); ++i)
|
|
if (ImGui::Selectable(STR_C(property.choices[i]), i == current))
|
|
track_property_apply(static_cast<int>(Index), Tracks, std::to_string(i));
|
|
ImGui::EndCombo();
|
|
}
|
|
row_help(Index);
|
|
};
|
|
|
|
auto const check_row = [&](std::size_t const Index) {
|
|
auto const &property{list[Index]};
|
|
auto const &values{cache.values[Index]};
|
|
auto const mixed{values.size() > 1};
|
|
bool value{false == mixed && values.front().first == "1"};
|
|
if (mixed)
|
|
ImGui::PushItemFlag(ImGuiItemFlags_MixedValue, true);
|
|
if (ImGui::Checkbox("##value", &value))
|
|
track_property_apply(static_cast<int>(Index), Tracks, value ? "1" : "0");
|
|
if (mixed)
|
|
ImGui::PopItemFlag();
|
|
row_help(Index);
|
|
};
|
|
|
|
auto const texture_row = [&](std::size_t const Index) {
|
|
auto const &property{list[Index]};
|
|
auto const &values{cache.values[Index]};
|
|
auto const mixed{values.size() > 1};
|
|
auto const common{mixed ? std::string{} : values.front().first};
|
|
auto const preview{mixed ? format(STR_C("different (%d)"), static_cast<int>(values.size())) : common == "none" ? std::string{STR_C(property.empty)} : common};
|
|
auto const side{ImGui::GetFrameHeight()};
|
|
if (false == mixed)
|
|
{
|
|
if (auto const image{material_image(editor_track::fetch_material(common))}; image != 0)
|
|
{
|
|
ImGui::Image(static_cast<ImTextureID>(image), ImVec2(side, side), ImVec2(0, 1), ImVec2(1, 0));
|
|
if (ImGui::IsItemHovered())
|
|
{
|
|
auto const big{std::round(ImGui::GetFontSize() * 14.0f)};
|
|
ImGui::BeginTooltip();
|
|
ImGui::Image(static_cast<ImTextureID>(image), ImVec2(big, big), ImVec2(0, 1), ImVec2(1, 0));
|
|
ImGui::TextUnformatted(common.c_str());
|
|
ImGui::EndTooltip();
|
|
}
|
|
ImGui::SameLine(0.0f, 4.0f);
|
|
}
|
|
}
|
|
ImGui::SetNextItemWidth(-FLT_MIN);
|
|
ImGui::SetNextWindowSizeConstraints(ImVec2(ImGui::GetFontSize() * 36.0f, 0.0f), ImVec2(FLT_MAX, FLT_MAX));
|
|
if (ImGui::BeginCombo("##value", preview.c_str(), ImGuiComboFlags_HeightLargest))
|
|
{
|
|
if (std::string chosen; track_texture_picker(property.texture, common, mixed, chosen))
|
|
{
|
|
track_property_apply(static_cast<int>(Index), Tracks, chosen);
|
|
ImGui::CloseCurrentPopup();
|
|
}
|
|
ImGui::EndCombo();
|
|
}
|
|
row_help(Index);
|
|
if (std::string dropped; material_drop(dropped))
|
|
track_property_apply(static_cast<int>(Index), Tracks, dropped);
|
|
};
|
|
|
|
auto const any_in = [&](int const Part, bool const More) {
|
|
for (std::size_t i = 0; i < list.size(); ++i)
|
|
if (list[i].part == Part && list[i].more == More && cache.applies[i] > 0)
|
|
return true;
|
|
return false;
|
|
};
|
|
auto const rows = [&](int const Part, bool const More) {
|
|
if (false == any_in(Part, More))
|
|
return;
|
|
if (false == ImGui::BeginTable(format("##trackprops%d%d", Part, More ? 1 : 0).c_str(), 2, ImGuiTableFlags_SizingStretchProp))
|
|
return;
|
|
ImGui::TableSetupColumn("label", ImGuiTableColumnFlags_WidthStretch, 0.45f);
|
|
ImGui::TableSetupColumn("value", ImGuiTableColumnFlags_WidthStretch, 0.55f);
|
|
for (std::size_t i = 0; i < list.size(); ++i)
|
|
{
|
|
if (list[i].part != Part || list[i].more != More || cache.applies[i] == 0)
|
|
continue;
|
|
ImGui::PushID(list[i].key);
|
|
ImGui::BeginDisabled(list[i].off);
|
|
row_label(i);
|
|
switch (list[i].kind)
|
|
{
|
|
case field::choice: choice_row(i); break;
|
|
case field::check: check_row(i); break;
|
|
case field::texture: texture_row(i); break;
|
|
default: text_row(i); break;
|
|
}
|
|
ImGui::EndDisabled();
|
|
if (list[i].off && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
|
|
ImGui::SetTooltip("%s", STR_C("Turned on with frogs yes in eu07.ini"));
|
|
ImGui::PopID();
|
|
for (auto const ¬e : m_track_notes)
|
|
if (note.property == static_cast<int>(i))
|
|
{
|
|
ImGui::TableNextRow();
|
|
ImGui::TableSetColumnIndex(1);
|
|
ImGui::PushTextWrapPos(0.0f);
|
|
ImGui::TextColored(note.type == track_note::kind::typing ? ImVec4(0.55f, 0.8f, 1.0f, 1.0f) : note.type == track_note::kind::done ? ImVec4(0.45f, 0.9f, 0.45f, 1.0f) : ImVec4(1.0f, 0.75f, 0.3f, 1.0f), "%s", note.text.c_str());
|
|
ImGui::PopTextWrapPos();
|
|
}
|
|
}
|
|
ImGui::EndTable();
|
|
};
|
|
auto const more = [&](char const *Id, int const Part) {
|
|
if (false == any_in(Part, true))
|
|
return;
|
|
ImGui::PushID(Id);
|
|
if (ImGui::TreeNode(STR_C("More settings")))
|
|
{
|
|
rows(Part, true);
|
|
ImGui::TreePop();
|
|
}
|
|
ImGui::PopID();
|
|
};
|
|
|
|
// whether what's set here is in the file yet; the same height either way, the rows don't move
|
|
if (cache.unsaved > 0)
|
|
{
|
|
ImGui::TextColored(ImVec4(1.0f, 0.75f, 0.3f, 1.0f), STR_P("Not saved to the file: %d track", "Not saved to the file: %d tracks", static_cast<long long>(cache.unsaved)), static_cast<int>(cache.unsaved));
|
|
ImGui::SameLine();
|
|
if (ImGui::SmallButton(format("%s##tracksheet", STR_C("Save")).c_str()))
|
|
save();
|
|
item_tooltip("Saves the scenery, as Ctrl+S does");
|
|
}
|
|
else
|
|
ImGui::TextDisabled("%s", STR_C("Saved in the file"));
|
|
|
|
if (ImGui::CollapsingHeader(STR_C("Traffic"), ImGuiTreeNodeFlags_DefaultOpen))
|
|
{
|
|
rows(section::traffic, false);
|
|
more("traffic", section::traffic);
|
|
}
|
|
if ((any_in(section::look, false) || any_in(section::look, true) || any_in(section::look_bed, false) || any_in(section::look_sleepers, false)) && ImGui::CollapsingHeader(STR_C("Look"), ImGuiTreeNodeFlags_DefaultOpen))
|
|
{
|
|
rows(section::look, false);
|
|
more("look", section::look);
|
|
if (any_in(section::look_bed, false) && ImGui::TreeNode(STR_C("Shape of the trackbed")))
|
|
{
|
|
rows(section::look_bed, false);
|
|
ImGui::TreePop();
|
|
}
|
|
if (any_in(section::look_sleepers, false) && ImGui::TreeNode(STR_C("Sleeper models")))
|
|
{
|
|
rows(section::look_sleepers, false);
|
|
ImGui::TreePop();
|
|
}
|
|
}
|
|
if (ImGui::CollapsingHeader(STR_C("Ride and sound")))
|
|
rows(section::ride, false);
|
|
}
|
|
|
|
void editor_mode::copy_track_style(TTrack const &Track)
|
|
{
|
|
auto &style{m_track_style};
|
|
style.from = name_of(Track);
|
|
style.values = values_of_track(Track);
|
|
style.picking = false;
|
|
style.reveal = true;
|
|
ui()->set_status(format(STR_C("Taken from %s: the inspector lists what it changes on the selected tracks, Ctrl+Shift+V sets what's ticked"), style.from.c_str()), false);
|
|
}
|
|
|
|
bool editor_mode::track_style_takes(std::string const &Key) const
|
|
{
|
|
auto const &style{m_track_style};
|
|
if (auto const tick{style.ticks.find(Key)}; tick != style.ticks.end())
|
|
return tick->second;
|
|
auto const index{track_property_index(Key)};
|
|
return index >= 0 && style.groups[properties()[index].group];
|
|
}
|
|
|
|
std::vector<editor_mode::track_style_line> editor_mode::track_style_lines(std::vector<TTrack *> const &Tracks)
|
|
{
|
|
std::vector<track_style_line> lines;
|
|
auto const &list{properties()};
|
|
auto const &cache{m_track_values};
|
|
// the values of the selected tracks gathered for the sheet, when it was drawn for them
|
|
auto const known{cache.signature == tracks_signature(Tracks) && cache.values.size() == list.size()};
|
|
for (auto const &[key, value] : m_track_style.values)
|
|
{
|
|
auto const index{track_property_index(key)};
|
|
if (index < 0 || list[index].off)
|
|
continue;
|
|
auto const &property{list[index]};
|
|
auto const values{known ? cache.values[index] : values_of(property, Tracks)};
|
|
std::size_t differ{0};
|
|
for (auto const &[other, count] : values)
|
|
if (other != value)
|
|
differ += static_cast<std::size_t>(count);
|
|
if (differ == 0)
|
|
continue;
|
|
lines.push_back({index, values.size() == 1 ? shown_value(property, values.front().first) : format(STR_C("different (%d)"), static_cast<int>(values.size())), shown_value(property, value), differ});
|
|
}
|
|
return lines;
|
|
}
|
|
|
|
std::size_t editor_mode::paste_track_style(std::vector<TTrack *> const &Tracks)
|
|
{
|
|
auto const &style{m_track_style};
|
|
if (style.values.empty() || Tracks.empty())
|
|
return 0;
|
|
std::vector<std::pair<std::string, std::string>> ticked;
|
|
for (auto const &value : style.values)
|
|
if (track_style_takes(value.first))
|
|
ticked.push_back(value);
|
|
track_groups all;
|
|
all.fill(true);
|
|
auto const changed{apply_track_values(ticked, all, Tracks, "From another track")};
|
|
if (false == changed.has_value())
|
|
{
|
|
ui()->set_status(STR_C("Nothing is ticked to be set"), true);
|
|
return 0;
|
|
}
|
|
if (Tracks.size() == 1)
|
|
ui()->set_status(*changed > 0 ? format(STR_C("Set as on %s, Ctrl+Z takes it back"), style.from.c_str()) : std::string{STR_C("The track has these properties already")}, false);
|
|
return *changed;
|
|
}
|
|
|
|
std::optional<std::size_t> editor_mode::apply_track_values(std::vector<std::pair<std::string, std::string>> const &Values, track_groups const &Groups, std::vector<TTrack *> const &Tracks, char const *What)
|
|
{
|
|
static_assert(group_count == std::tuple_size_v<track_groups>, "a choice for each group of the pasted properties");
|
|
auto const &list{properties()};
|
|
std::vector<std::pair<track_property const *, std::string>> pasted;
|
|
bool rebuild{false};
|
|
for (auto const late : {false, true})
|
|
for (auto const &[key, value] : Values)
|
|
{
|
|
auto const index{track_property_index(key)};
|
|
if (index < 0)
|
|
continue;
|
|
auto const &property{list[index]};
|
|
if (property.off || property.late != late || false == Groups[property.group])
|
|
continue;
|
|
pasted.emplace_back(&property, value);
|
|
rebuild |= property.rebuild;
|
|
}
|
|
if (pasted.empty())
|
|
return std::nullopt;
|
|
return tracks_apply(Tracks, [&](TTrack &Track) {
|
|
bool any{false};
|
|
for (auto const &[property, value] : pasted)
|
|
if (property->applies(Track))
|
|
any |= property->set(Track, value);
|
|
return any;
|
|
}, rebuild, What);
|
|
}
|
|
|
|
std::size_t editor_mode::apply_track_preset(int const Id, std::vector<TTrack *> const &Tracks)
|
|
{
|
|
auto const *preset{EditorTrackPresets.find(Id)};
|
|
if (preset == nullptr || Tracks.empty())
|
|
return 0;
|
|
auto const name{preset->name};
|
|
auto const changed{apply_track_values(preset->values, groups_of(*preset), Tracks, "Applied a track preset")};
|
|
if (false == changed.has_value())
|
|
{
|
|
ui()->set_status(format(STR_C("The preset \"%s\" overwrites nothing: choose what it overwrites"), name.c_str()), true);
|
|
return 0;
|
|
}
|
|
if (*changed > 0)
|
|
ui()->set_status(format(STR_P("Preset \"%s\" applied on %d track, Ctrl+Z takes it back", "Preset \"%s\" applied on %d tracks, Ctrl+Z takes it back", static_cast<long long>(*changed)), name.c_str(), static_cast<int>(*changed)), false);
|
|
else
|
|
ui()->set_status(STR_C("The tracks have the properties of the preset already"), false);
|
|
return *changed;
|
|
}
|
|
|
|
void editor_mode::render_track_presets(std::vector<TTrack *> const &Tracks)
|
|
{
|
|
if (Tracks.empty() || false == ImGui::TreeNode(STR_C("Presets")))
|
|
return;
|
|
auto &panel{m_track_presets};
|
|
auto const &presets{EditorTrackPresets.presets()};
|
|
if (EditorTrackPresets.find(panel.selected) == nullptr)
|
|
panel.selected = presets.empty() ? 0 : presets.front().id;
|
|
auto const single{Tracks.size() == 1};
|
|
auto const *selected{EditorTrackPresets.find(panel.selected)};
|
|
|
|
ImGui::SetNextItemWidth(-FLT_MIN);
|
|
if (ImGui::BeginCombo("##trackpreset", selected != nullptr ? selected->name.c_str() : STR_C("No presets yet")))
|
|
{
|
|
for (auto const &preset : presets)
|
|
{
|
|
if (ImGui::Selectable(preset.name.c_str(), preset.id == panel.selected))
|
|
panel.selected = preset.id;
|
|
if (ImGui::IsItemHovered())
|
|
ImGui::SetTooltip("%s", preset_summary(preset).c_str());
|
|
}
|
|
ImGui::EndCombo();
|
|
}
|
|
if (selected != nullptr && ImGui::IsItemHovered())
|
|
ImGui::SetTooltip("%s", preset_summary(*selected).c_str());
|
|
|
|
auto const width{button_width(2)};
|
|
ImGui::BeginDisabled(selected == nullptr);
|
|
if (ImGui::Button(format(STR_P("Apply on %d track", "Apply on %d tracks", static_cast<long long>(Tracks.size())), static_cast<int>(Tracks.size())).c_str(), ImVec2(width, 0.0f)))
|
|
apply_track_preset(panel.selected, Tracks);
|
|
ImGui::EndDisabled();
|
|
item_tooltip("Puts the values of the preset on the selected tracks, of what it overwrites; Ctrl+Z takes them back.\nTools in the menu applies the presets too");
|
|
ImGui::SameLine();
|
|
ImGui::BeginDisabled(false == single);
|
|
if (ImGui::Button(STR_C("New from the track"), ImVec2(width, 0.0f)))
|
|
{
|
|
// rails, trackbed, its shape, sleepers, frogs and sleeper placement at first, the look of the track
|
|
std::array<bool, group_count> groups{};
|
|
for (auto const group : {group::rails, group::trackbed, group::trackbed_shape, group::sleepers, group::frogs, group::sleeper_placement})
|
|
groups[group] = true;
|
|
panel.selected = EditorTrackPresets.create(STR_C("New preset"), name_of(*Tracks.front()), values_of_track(*Tracks.front()), group_keys(groups));
|
|
panel.nameid = 0;
|
|
ui()->set_status(format(STR_C("Properties of %s saved as a preset: give it a name and choose what it overwrites"), name_of(*Tracks.front()).c_str()), false);
|
|
}
|
|
ImGui::EndDisabled();
|
|
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
|
|
ImGui::SetTooltip("%s", STR_C(single ? "Saves the properties of this track as a new preset" : "Select one track to save its properties as a preset"));
|
|
|
|
// pointers to the presets don't outlive a change of their list
|
|
selected = EditorTrackPresets.find(panel.selected);
|
|
if (selected == nullptr)
|
|
{
|
|
ImGui::PushTextWrapPos(0.0f);
|
|
ImGui::TextDisabled("%s", STR_C("A preset keeps the properties of a track: its rails, trackbed, sleepers... to put them on other tracks,\nwith the choice of what it overwrites"));
|
|
ImGui::PopTextWrapPos();
|
|
ImGui::TreePop();
|
|
return;
|
|
}
|
|
auto const id{selected->id};
|
|
|
|
// the name
|
|
if (panel.nameid != id)
|
|
{
|
|
std::snprintf(panel.name, sizeof(panel.name), "%s", selected->name.c_str());
|
|
panel.nameid = id;
|
|
}
|
|
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(STR_C("Rename")).x - ImGui::GetStyle().FramePadding.x * 2 - ImGui::GetStyle().ItemSpacing.x);
|
|
auto const entered{ImGui::InputText("##presetname", panel.name, IM_ARRAYSIZE(panel.name), ImGuiInputTextFlags_EnterReturnsTrue)};
|
|
ImGui::PopItemWidth();
|
|
ImGui::SameLine();
|
|
if (ImGui::Button(STR_C("Rename")) || entered)
|
|
{
|
|
EditorTrackPresets.rename(id, panel.name);
|
|
panel.nameid = 0; // reload, the name could have been adjusted to stay unique
|
|
selected = EditorTrackPresets.find(id);
|
|
}
|
|
|
|
// what it overwrites, of the groups it has values of
|
|
ImGui::TextDisabled("%s", STR_C("Overwrites:"));
|
|
// its own id scope: the group checkboxes share their labels with the sections of the properties
|
|
ImGui::PushID("presetgroups");
|
|
auto const available{groups_in(selected->values)};
|
|
auto groups{groups_of(*selected)};
|
|
auto changed{false};
|
|
int column{0};
|
|
auto const half{ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x * 0.5f};
|
|
for (int group = 0; group < group_count; ++group)
|
|
{
|
|
if (false == available[group])
|
|
continue;
|
|
if (column % 2 == 1)
|
|
ImGui::SameLine(half);
|
|
changed |= ImGui::Checkbox(STR_C(kGroupLabels[group]), &groups[group]);
|
|
++column;
|
|
}
|
|
ImGui::PopID();
|
|
if (changed)
|
|
{
|
|
// the groups it has no values of are kept as they were, for a track they're taken again from
|
|
auto kept{groups_of(*selected)};
|
|
for (int group = 0; group < group_count; ++group)
|
|
if (available[group])
|
|
kept[group] = groups[group];
|
|
EditorTrackPresets.set_groups(id, group_keys(kept));
|
|
selected = EditorTrackPresets.find(id);
|
|
}
|
|
|
|
ImGui::BeginDisabled(false == single);
|
|
if (ImGui::Button(STR_C("Update from the track"), ImVec2(width, 0.0f)))
|
|
{
|
|
EditorTrackPresets.update(id, name_of(*Tracks.front()), values_of_track(*Tracks.front()));
|
|
ui()->set_status(format(STR_C("Preset \"%s\" takes the properties of %s now"), selected->name.c_str(), name_of(*Tracks.front()).c_str()), false);
|
|
}
|
|
ImGui::EndDisabled();
|
|
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
|
|
ImGui::SetTooltip("%s", STR_C(single ? "The preset takes the properties of this track in place of the ones it had; what it overwrites stays" : "Select one track to take its properties into the preset"));
|
|
ImGui::SameLine();
|
|
if (ImGui::Button(STR_C("Delete"), ImVec2(width, 0.0f)))
|
|
ImGui::OpenPopup("##deletepreset");
|
|
if (ImGui::BeginPopup("##deletepreset"))
|
|
{
|
|
ImGui::Text(STR_C("Delete preset \"%s\"?"), selected->name.c_str());
|
|
if (ImGui::Button(STR_C("Delete##confirm")))
|
|
{
|
|
EditorTrackPresets.remove(id);
|
|
panel.selected = 0;
|
|
ImGui::CloseCurrentPopup();
|
|
}
|
|
ImGui::SameLine();
|
|
if (ImGui::Button(STR_C("Cancel")))
|
|
ImGui::CloseCurrentPopup();
|
|
ImGui::EndPopup();
|
|
}
|
|
ImGui::TreePop();
|
|
}
|
|
|
|
void editor_mode::render_track_presets_menu()
|
|
{
|
|
if (false == ImGui::BeginMenu(STR_C("Track presets")))
|
|
return;
|
|
auto const &presets{EditorTrackPresets.presets()};
|
|
if (presets.empty())
|
|
ImGui::TextDisabled("%s", STR_C("No presets: save one in the inspector of a track, From another track or a preset, Presets"));
|
|
auto const tracks{selected_tracks()};
|
|
int chosen{0};
|
|
for (auto const &preset : presets)
|
|
{
|
|
if (ImGui::MenuItem(preset.name.c_str(), nullptr, false, false == tracks.empty()))
|
|
chosen = preset.id;
|
|
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
|
|
ImGui::SetTooltip("%s", tracks.empty() ? STR_C("Select the tracks to apply the preset on first") : (format(STR_P("Applies the preset on %d selected track", "Applies the preset on %d selected tracks", static_cast<long long>(tracks.size())), static_cast<int>(tracks.size())) + "\n" + preset_summary(preset)).c_str());
|
|
}
|
|
ImGui::EndMenu();
|
|
if (chosen != 0)
|
|
apply_track_preset(chosen, tracks);
|
|
}
|
|
|
|
void editor_mode::render_track_style(std::vector<TTrack *> const &Tracks)
|
|
{
|
|
auto &style{m_track_style};
|
|
if (style.reveal)
|
|
{
|
|
ImGui::SetNextItemOpen(true);
|
|
style.reveal = false;
|
|
}
|
|
if (false == ImGui::CollapsingHeader(format("%s###trackfrom", STR_C("From another track or a preset")).c_str()))
|
|
return;
|
|
if (style.picking)
|
|
{
|
|
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.85f, 0.55f, 0.1f, 1.0f));
|
|
if (ImGui::Button(STR_C("Click a track in the view, Esc gives up"), ImVec2(-FLT_MIN, 0.0f)))
|
|
style.picking = false;
|
|
ImGui::PopStyleColor();
|
|
}
|
|
else
|
|
{
|
|
if (ImGui::Button(STR_C(style.values.empty() ? "Take from a track..." : "Take from another track..."), ImVec2(-FLT_MIN, 0.0f)))
|
|
style.picking = true;
|
|
item_tooltip("Then click a track in the view: the list under shows what it changes on the selected tracks, each with a tick");
|
|
}
|
|
|
|
if (style.values.empty())
|
|
{
|
|
ImGui::PushTextWrapPos(0.0f);
|
|
ImGui::TextDisabled("%s", STR_C("Click a track after it: the list shows what it changes on the selected tracks, one button sets it"));
|
|
ImGui::PopTextWrapPos();
|
|
}
|
|
else
|
|
{
|
|
ImGui::TextColored(ImVec4(0.55f, 0.8f, 1.0f, 1.0f), STR_C("From %s:"), style.from.c_str());
|
|
ImGui::SameLine();
|
|
if (auto const room{ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(STR_C("Forget")).x - ImGui::GetStyle().FramePadding.x * 2.0f}; room > 0.0f)
|
|
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + room);
|
|
auto forget{ImGui::SmallButton(STR_C("Forget"))};
|
|
item_tooltip("Forgets the track taken");
|
|
auto const lines{track_style_lines(Tracks)};
|
|
if (lines.empty())
|
|
{
|
|
ImGui::PushTextWrapPos(0.0f);
|
|
ImGui::TextDisabled(STR_C("The selected tracks have all of it as %s has"), style.from.c_str());
|
|
ImGui::PopTextWrapPos();
|
|
}
|
|
// a property each: the tick, then how it changes, the text a part of the tick
|
|
auto const brief = [&](int const Property, std::string const &Text) {
|
|
auto const slash{properties()[Property].kind == field::texture ? Text.find_last_of('/') : std::string::npos};
|
|
return slash == std::string::npos ? Text : Text.substr(slash + 1);
|
|
};
|
|
ImGui::PushID("taken");
|
|
int ticked{0};
|
|
for (auto const &line : lines)
|
|
{
|
|
auto const &property{properties()[line.property]};
|
|
ImGui::PushID(property.key);
|
|
auto take{track_style_takes(property.key)};
|
|
if (ImGui::Checkbox("##take", &take))
|
|
style.ticks[property.key] = take;
|
|
auto const help = [&]() {
|
|
if (false == ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort))
|
|
return;
|
|
ImGui::BeginTooltip();
|
|
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 30.0f);
|
|
ImGui::TextUnformatted(STR_C(property.tooltip));
|
|
ImGui::Separator();
|
|
ImGui::Text(STR_C("Now: %s\nOn %s: %s"), line.now.c_str(), style.from.c_str(), line.taken.c_str());
|
|
ImGui::Text(STR_P("Changes %d selected track", "Changes %d selected tracks", static_cast<long long>(line.differ)), static_cast<int>(line.differ));
|
|
ImGui::PopTextWrapPos();
|
|
ImGui::EndTooltip();
|
|
};
|
|
help();
|
|
ImGui::SameLine();
|
|
auto text{format("%s: %s -> %s", STR_C(property.label), brief(line.property, line.now).c_str(), brief(line.property, line.taken).c_str())};
|
|
if (line.differ < Tracks.size())
|
|
text += format(STR_C(" (%d of %d)"), static_cast<int>(line.differ), static_cast<int>(Tracks.size()));
|
|
ImGui::PushTextWrapPos(0.0f);
|
|
if (take)
|
|
ImGui::TextUnformatted(text.c_str());
|
|
else
|
|
ImGui::TextDisabled("%s", text.c_str());
|
|
ImGui::PopTextWrapPos();
|
|
if (ImGui::IsItemClicked())
|
|
style.ticks[property.key] = false == take;
|
|
help();
|
|
ticked += take ? 1 : 0;
|
|
ImGui::PopID();
|
|
}
|
|
ImGui::PopID();
|
|
if (false == lines.empty())
|
|
{
|
|
ImGui::BeginDisabled(ticked == 0);
|
|
if (ImGui::Button(format(STR_P("Set on %d track", "Set on %d tracks", static_cast<long long>(Tracks.size())), static_cast<int>(Tracks.size())).c_str(), ImVec2(-FLT_MIN, 0.0f)))
|
|
paste_track_style(Tracks);
|
|
ImGui::EndDisabled();
|
|
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
|
|
ImGui::SetTooltip("%s", STR_C(ticked == 0 ? "Tick what's to be set first" : "Sets what's ticked on the selected tracks, in one step of the undo; Ctrl+Shift+V does the same"));
|
|
}
|
|
if (forget)
|
|
{
|
|
style.from.clear();
|
|
style.values.clear();
|
|
}
|
|
}
|
|
ImGui::Spacing();
|
|
render_track_presets(Tracks);
|
|
}
|
|
|
|
std::vector<TTrack *> editor_mode::similar_tracks(int const Property, std::vector<TTrack *> const &Tracks) const
|
|
{
|
|
auto const &property{properties()[Property]};
|
|
std::set<std::string> wanted;
|
|
for (auto const *track : Tracks)
|
|
if (track != nullptr && property.applies(*track))
|
|
wanted.insert(property.get(*track));
|
|
std::vector<TTrack *> found;
|
|
if (wanted.empty())
|
|
return found;
|
|
for (auto *track : simulation::Paths.sequence())
|
|
if (track != nullptr && rail(*track) && property.applies(*track) && wanted.count(property.get(*track)) > 0)
|
|
found.push_back(track);
|
|
return found;
|
|
}
|
|
|
|
void editor_mode::render_track_set_header(std::vector<TTrack *> const &Tracks)
|
|
{
|
|
double length{0.0};
|
|
int switches{0};
|
|
for (auto const *track : Tracks)
|
|
{
|
|
length += track->Length();
|
|
switches += track->eType == tt_Switch ? 1 : 0;
|
|
}
|
|
ImGui::TextColored(ImVec4(1.0f, 0.65f, 0.2f, 1.0f), STR_P("%d track selected, %.1f m", "%d tracks selected, %.1f m", static_cast<long long>(Tracks.size())), static_cast<int>(Tracks.size()), length);
|
|
item_tooltip("What is set below goes to all of them, in one step of the undo");
|
|
ImGui::SameLine();
|
|
if (auto const room{ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(STR_C("Deselect")).x - ImGui::GetStyle().FramePadding.x * 2.0f}; room > 0.0f)
|
|
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + room);
|
|
if (ImGui::SmallButton(STR_C("Deselect")))
|
|
{
|
|
select_tracks({});
|
|
return;
|
|
}
|
|
item_tooltip("Clears the selection (Esc)");
|
|
if (switches > 0)
|
|
ImGui::TextDisabled(STR_P("among them %d switch", "among them %d switches", static_cast<long long>(switches)), switches);
|
|
ImGui::TextDisabled("%s", STR_C("What is set below goes to all of them"));
|
|
|
|
// the list: hovering a row shows the track in the view, a click flies to it, a double click keeps it alone
|
|
if (ImGui::TreeNode(STR_C("List of the selected tracks")))
|
|
{
|
|
auto const rows{std::min<std::size_t>(Tracks.size(), 10)};
|
|
ImGui::BeginChild("##selectedtracks", ImVec2(0.0f, ImGui::GetFrameHeightWithSpacing() * static_cast<float>(rows) + 8.0f), ImGuiChildFlags_Borders);
|
|
auto const column{ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x * 0.62f};
|
|
TTrack *removed{nullptr};
|
|
TTrack *alone{nullptr};
|
|
ImGuiListClipper clipper;
|
|
clipper.Begin(static_cast<int>(Tracks.size()));
|
|
while (clipper.Step())
|
|
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; ++i)
|
|
{
|
|
auto *track{Tracks[i]};
|
|
ImGui::PushID(i);
|
|
if (ImGui::SmallButton("x"))
|
|
removed = track;
|
|
item_tooltip("Takes the track out of the selection");
|
|
ImGui::SameLine();
|
|
if (ImGui::Selectable(name_of(*track).c_str(), false, ImGuiSelectableFlags_AllowDoubleClick))
|
|
{
|
|
if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
|
|
alone = track;
|
|
else
|
|
focus_track(*track, 0, bezier{track->m_paths.front()}.point(0.5));
|
|
}
|
|
if (ImGui::IsItemHovered())
|
|
{
|
|
m_track_list_hover = track;
|
|
m_track_list_hover_frame = ImGui::GetFrameCount();
|
|
ImGui::SetTooltip("%s", STR_C("Click: look at it, double click: select only this one"));
|
|
}
|
|
ImGui::SameLine(column);
|
|
ImGui::TextDisabled("%s, %.1f m", STR_C(track_type_name(*track)), track->Length());
|
|
ImGui::PopID();
|
|
}
|
|
ImGui::EndChild();
|
|
ImGui::TreePop();
|
|
if (removed != nullptr)
|
|
track_set_toggle(*removed);
|
|
else if (alone != nullptr)
|
|
select_tracks({alone});
|
|
}
|
|
}
|
|
|
|
void editor_mode::render_track_select_tools(std::vector<TTrack *> const &Tracks)
|
|
{
|
|
auto const single{Tracks.size() == 1};
|
|
auto const open{ImGui::CollapsingHeader(format("%s (?)###trackselect%d", STR_C("Selection"), single ? 1 : 2).c_str(), single ? 0 : ImGuiTreeNodeFlags_DefaultOpen)};
|
|
if (ImGui::IsItemHovered())
|
|
ImGui::SetTooltip("%s", STR_C("Shift+click: add or take out a track\n"
|
|
"Ctrl+click: the same\n"
|
|
"Drag on the ground: select the tracks in a box, with Shift: add them\n"
|
|
"Ctrl+Shift+click: the whole run of joined tracks\n"
|
|
"Esc: clear the selection, Del: delete the selected tracks\n"
|
|
"Ctrl+Shift+C / Ctrl+Shift+V: take the properties of a track, set them on the selected ones"));
|
|
if (false == open)
|
|
return;
|
|
auto const width{button_width(2)};
|
|
if (ImGui::Button(STR_C("+ Joined"), ImVec2(width, 0.0f)))
|
|
{
|
|
if (m_track_set.empty() && false == Tracks.empty())
|
|
m_track_set = Tracks;
|
|
track_set_grow();
|
|
select_tracks(m_track_set);
|
|
}
|
|
item_tooltip("Adds the tracks joined to the selection, by the rules of the spread in the tool options");
|
|
ImGui::SameLine();
|
|
if (ImGui::Button(STR_C("Alike..."), ImVec2(width, 0.0f)))
|
|
ImGui::OpenPopup("##similar");
|
|
item_tooltip("Selects the tracks of the whole scenery which have the same rails, trackbed, speed... as the selected ones");
|
|
if (ImGui::BeginPopup("##similar"))
|
|
{
|
|
char const *const keys[] = {"rails", "trackbed", "speed", "environment", "circuits", "overhead"};
|
|
char const *const labels[] = {"The same rails", "The same trackbed", "The same speed", "The same environment", "The same track circuits", "The same overhead line"};
|
|
if (ImGui::IsWindowAppearing())
|
|
{
|
|
m_track_similar.clear();
|
|
for (auto const *key : keys)
|
|
m_track_similar.push_back(similar_tracks(track_property_index(key), Tracks));
|
|
}
|
|
for (std::size_t i = 0; i < std::size(keys) && i < m_track_similar.size(); ++i)
|
|
{
|
|
auto const &found{m_track_similar[i]};
|
|
if (ImGui::MenuItem(STR_C(labels[i]), tracks_count(found.size()).c_str(), false, false == found.empty()))
|
|
{
|
|
select_tracks(found);
|
|
ui()->set_status(format(STR_C("%s: %s selected in the scenery"), STR_C(labels[i]), tracks_count(found.size()).c_str()), false);
|
|
}
|
|
}
|
|
ImGui::EndPopup();
|
|
}
|
|
}
|
|
|
|
editor_mode::selftest_result editor_mode::selftest_props(std::string const &Command, std::istringstream &Words)
|
|
{
|
|
if (Command == "tracksel")
|
|
{
|
|
std::vector<TTrack *> tracks;
|
|
for (std::string name; Words >> name;)
|
|
if (auto *track{simulation::Paths.find(name)}; track != nullptr)
|
|
tracks.push_back(track);
|
|
select_tracks(tracks);
|
|
WriteLog(format("SELFTEST tracksel: %zu tracks selected", selected_tracks().size()));
|
|
return selftest_result::handled;
|
|
}
|
|
if (Command == "trackprop")
|
|
{
|
|
std::string key;
|
|
Words >> key;
|
|
std::string value;
|
|
std::getline(Words, value);
|
|
value = words_of(value).empty() ? std::string{} : value.substr(value.find_first_not_of(' '));
|
|
auto const index{track_property_index(key)};
|
|
if (index < 0)
|
|
{
|
|
WriteLog("SELFTEST trackprop: no property " + key);
|
|
return selftest_result::handled;
|
|
}
|
|
auto const changed{track_property_apply(index, selected_tracks(), value)};
|
|
WriteLog(format("SELFTEST trackprop %s=\"%s\": %zu changed: %s", key.c_str(), value.c_str(), changed, m_track_outcome.text.c_str()));
|
|
return selftest_result::handled;
|
|
}
|
|
if (Command == "trackprops")
|
|
{
|
|
auto const tracks{selected_tracks()};
|
|
for (auto const &property : properties())
|
|
{
|
|
auto const values{values_of(property, tracks)};
|
|
std::string text;
|
|
for (auto const &[value, count] : values)
|
|
text += format(" [%s]x%d", value.c_str(), count);
|
|
WriteLog(format("SELFTEST trackprops %s:%s", property.key, text.c_str()));
|
|
}
|
|
for (auto const ¬e : m_track_notes)
|
|
WriteLog(format("SELFTEST trackprops note %s: %s", properties()[note.property].key, note.text.c_str()));
|
|
WriteLog(format("SELFTEST trackprops unsaved: %zu", m_track_values.unsaved));
|
|
return selftest_result::handled;
|
|
}
|
|
if (Command == "trackstyle")
|
|
{
|
|
std::string action;
|
|
Words >> action;
|
|
if (action == "copy")
|
|
{
|
|
std::string name;
|
|
Words >> name;
|
|
if (auto *track{name.empty() ? selected_track() : simulation::Paths.find(name)}; track != nullptr)
|
|
copy_track_style(*track);
|
|
WriteLog(format("SELFTEST trackstyle copy: %zu values from %s", m_track_style.values.size(), m_track_style.from.c_str()));
|
|
}
|
|
else if (action == "groups")
|
|
{
|
|
// the groups ticked, as their numbers: 0 speed ... 9 frogs, 10 sleeper placement
|
|
m_track_style.groups.fill(false);
|
|
m_track_style.ticks.clear();
|
|
for (int group; Words >> group;)
|
|
if (group >= 0 && group < group_count)
|
|
m_track_style.groups[group] = true;
|
|
}
|
|
else if (action == "tick")
|
|
{
|
|
std::string key;
|
|
int tick{1};
|
|
Words >> key >> tick;
|
|
m_track_style.ticks[key] = tick != 0;
|
|
}
|
|
else if (action == "paste")
|
|
WriteLog(format("SELFTEST trackstyle paste: %zu changed", paste_track_style(selected_tracks())));
|
|
else if (action == "panel")
|
|
m_track_style.reveal = true;
|
|
else if (action == "list")
|
|
{
|
|
auto const lines{track_style_lines(selected_tracks())};
|
|
WriteLog(format("SELFTEST trackstyle list: %zu lines from %s", lines.size(), m_track_style.from.c_str()));
|
|
for (auto const &line : lines)
|
|
{
|
|
auto const &property{properties()[line.property]};
|
|
WriteLog(format("SELFTEST trackstyle line %s [%s] %s -> %s (%zu)", property.key, track_style_takes(property.key) ? "x" : " ", line.now.c_str(), line.taken.c_str(), line.differ));
|
|
}
|
|
}
|
|
return selftest_result::handled;
|
|
}
|
|
if (Command == "tracksimilar")
|
|
{
|
|
std::string key;
|
|
Words >> key;
|
|
auto const index{track_property_index(key)};
|
|
if (index >= 0)
|
|
select_tracks(similar_tracks(index, selected_tracks()));
|
|
WriteLog(format("SELFTEST tracksimilar %s: %zu selected", key.c_str(), selected_tracks().size()));
|
|
return selftest_result::handled;
|
|
}
|
|
return selftest_result::unhandled;
|
|
}
|