18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-28 23:13:23 +01:00
Files
maszyna/application/editormode.cpp
jerrrrycho e76ab8b5b3 Revert #408 and merge master into staging (unblocks #442) (#443)
* 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>

* AI: shunting brakes for a stop point again; test mode: no false SPAD for a train set across a signal (#409)

* fix(ai): shunting AI brakes for a stop point again, the braking curve doesn't plan from a brake table left at zero

The braking curve outside the final approach plans from the consist brake table since ce87aa0e (#187, merged
with #264) whenever the brake is pneumatic. That table is worked out only for train running (Obey_train, Bank);
in shunting it stays at zero, the planned full braking falls to its floor of 0.01 m/s2, and the demand comes out
at a few hundredths of a m/s2, above the shunting brake threshold of -0.2. A shunting engine then rolled on
without braking until the final approach, too late, and ran through the signal it was to stop at (seed
480873929 of l053_poranek: ep07p-2003 at sandomierz_g4_sem_info, sm42-1096, et22-654).

With no worked out table the curve is again the constant deceleration over the distance left, as it was before
ce87aa0e and as #187 meant to keep it for shunting. Train running is unchanged.

The curve moves out of TController into vehicle/brakingcurve.{h,cpp} so that it is unit tested:
tests/unit/test_braking_curve.cpp checks a shunting engine braking to a stop point with the zeroed table, and
that a train with a worked out table gets the same demand as before.

* test(ai): pin the train braking-curve demand to the results of the code before the move

A brake table that grips harder as the train slows and a gradient, so that a wrong gravity sign, table index or a dropped term in the shaped demand shows; the expected values were worked out with the code removed from TController::adjust_desired_speed_for_braking_curve.

* fix(test): a train set across a signal doesn't pass it at danger the moment the AI first looks at it

The speed table takes in events up to a train length behind the head, so a signal the train stands across at the start of a scenario is found already behind the head, becomes elapsed on its first update, and the test mode reported it as passed at danger (calkowo_lotos: st43-189 at mac_daleko3_sem_info, manewrowo3: st43-100 at suw_szopa_a02_sem_info, both at 13.6 s with the train at a stand, the signal 20 m behind the head). A point now counts as passed only when the head of the train had it ahead once; the blackbox record of passed signals follows the same rule.

* fix(test): a signal found up to 5 m behind the head still counts as seen ahead

The distance to an event is found in steps of a metre from the position of its memory cell, so a signal just ahead of the head (a train stopped short of it when the autopilot takes over, or set right at it at the start) could come out slightly behind it, and passing it would not be reported. The trains set across a signal that the previous commit is about stood 20 m past it.

Still not reported: a signal crossed while the speed table is being traced anew in motion (a Timetable: command to a running train, the autopilot taking over in motion), as it is found behind the head when the trace is made.

* docs(test): say what the seen-ahead tolerance gives up

A train set less than 5 m past a signal at stop is still reported to pass it at danger: standing that close it can't be told from a train stopped just short of the signal, which the tolerance is there for.

---------

Co-authored-by: maj00r <maj00r@host>

* fix(camera): the roll of the cab shake tilts the view about the line of sight again (#414)

623c4f82 (#97, in 26.05.1) changed the order of the camera rotations from
Z, X, Y to X, Y, Z. The order matters only with a roll, which the cab
shake gives (Angle.z, set in drivermode.cpp), or a camera entry of the
scenery with a roll. Applied last, in the
space of the cab, the roll nodded the view up and down when looking out
of a side window instead of tilting it. The roll is multiplied in first
again, as in 26.05.

Co-authored-by: maj00r <maj00r@host>

* fix(particles): the smoke of a cold diesel keeps the colour of its template (#413)

bcb9e644 (#109) turned a comparison into an assignment: the smoke of a
vehicle in air at 5 degrees or less, or with its engine under 45 degrees,
got the colour 128,128,128. The emitter colour is on a 0-1 scale, so the
smoke turned pure white, glowing at night, and stayed so once the engine
warmed up. Up to 26.05 the line compared and did nothing; the smoke keeps
the colour of its template again, as it did then.

Co-authored-by: maj00r <maj00r@host>

* Fix stutter with fpslimit on Windows and a texture unit cache race with threaded Python screens (#399)

* fix(render): the python upload thread only marks the texture unit cache stale, the drawing thread clears it

update_from_memory() runs on the python upload thread with python.threadedupload (the default), and since
e230d7b9 it cleared opengl_texture::units there outright. That raced Bind_Material filling the same array
for its multi-bind: a unit reset between the fill and glBindTextures() made the bind of that unit fail, and
the object was drawn with the texture of the one drawn before it, for a frame, at the rate of the screens.

The thread now sets an atomic flag, and the drawing thread clears the cache when it next binds textures
(bind() and the multi-bind of Bind_Material), so a screen texture given a new gl name is still rebound.

* fix(app): fpslimit asks Windows for the 1 ms timer, the frames no longer come in turns long and short

The wait of fpslimit sleeps, and Windows wakes a process which didn't ask for a finer timer in steps of
up to ~15.6 ms. A 10 ms slot of fpslimit 100 ended ~15.6 ms later, the pacer made it up with no wait in the
next frame, and with vsync on the frames were shown for one or two refreshes in turn: visible stutter at a
steady FPS. timeBeginPeriod(1) is held from the settings to the exit, only when fpslimit is set.

* fix: the 1 ms timer also for an fpslimit set by the scenery, the stale unit cache checked with a plain load first

A config block of the scenery is parsed after the settings and can set fpslimit, so the timer period is
asked for at the first wait of the pacer too, once. bind() checks the stale flag of the unit cache with a
relaxed load before the exchange, which it does for every texture.

* fix(textures): the stale flag of the unit cache uses the default memory order (Sonar S8417)

On x86 a sequentially consistent load and exchange cost the same as the
relaxed and acq_rel ones; only the store in invalidate_unit_cache() becomes
an xchg, and it runs rarely.

---------

Co-authored-by: maj00r <maj00r@host>

* fix(camera): the cab shake taken at the frame's moment between its fixed steps, it no longer moves in steps (#403)

The shake of the cab camera (ShakeState) is stepped by update_shake at a fixed 1/50 s, while the camera read the
raw state every frame. At any frame rate other than 50 the shake moved in steps: some frames showed the same
state as the one before, others jumped one or two steps at once. Players with 144/165 Hz screens saw the cab
jump around the camera.

The vehicle now keeps the state from before the last step (ShakeStateBefore), and the camera takes the state
between the two at the share of the step the simulation time has reached (shake_between). The offset and the
velocity are blended, the angles worked out from the blended velocity. The shake itself is computed as before,
with the same fixed step and the same springs; only what the camera shows of it changes. It lags the vehicle
by a constant 1/50 s instead of 0 to 1/50 s.

Measured with the same build and the blending switched off by a temporary environment variable (not in this
commit), the EU07 on td.scn driven by the AI, frames over 30 km/h, two runs of each:
- 60 fps: frames with the shake standing still 25% -> 0%, the change in its speed frame to frame 4-5 times
  smaller for the offset, a third smaller for the angles
- ~144 fps: 65% -> 0%, 10-18 times smaller for the offset, 4 times for the angles
- no fps limit (~520 fps): 90% -> 0%

Co-authored-by: maj00r <maj00r@host>

* fix: inverted clamp bounds give the upper bound again, as the old clamp did (#412)

* fix: inverted clamp bounds give the upper bound again, as the old clamp did

The project's own clamp() of 26.05 applied the lower bound first and the
upper one last, so bounds given the wrong way round always gave the upper
one. Its calls became std::clamp (undefined for such bounds) and, where the
bounds can cross, safe_clamp, which swapped them instead. Neither gives the
old result.

- safe_clamp is the old clamp again, min(max(value, lo), hi). This restores
  26.05 at every place it is used, among them the AI's dynamic brake test
  (its demand was fAccThreshold x1.21, the swap made it x1.01), the
  generator voltage, the heavy cargo acceleration of the AI and the EP
  correction of the brake.
- The cab camera is kept inside the cab with safe_clamp. The bounds come
  from the .mmd as written and some cabs have them reversed (the B cars of
  SA134 and 21xM, ET40, 754); with std::clamp the camera jumped between
  the two bounds every frame on MSVC, as its result is fed back.

* fix: every call of the old clamp gives what it gave up to 26.05.1

d1f16411 (#99) made every call of the project's clamp() std::clamp. Where the bounds come from the data or
are computed at runtime they can cross, and then std::clamp gives another result (undefined behaviour;
MSVC gives the lower bound unless the value is above the upper one) where the old clamp gave the upper
bound. These calls use safe_clamp, the old clamp, too:

- Mover: the pantograph pressure (MaxPress), the compressor switch (CompressorListPosNo), the coupler
  distance (DmaxB, DmaxC), the engine revolutions, the eimic ramp (CtrlDelay), the eimic limit
  (eimic_max), the speed control ramp (PowerUpSpeed, PowerDownSpeed), the load (MaxLoad), the magnet of
  the security system (Dim.L);
- DynObj: the coupler distance of the buffers (DmaxB), the axle distance (Dim.L);
- Train: the brake handle positions (BrakeCtrlPosNo, the range of the handle);
- the legacy renderer: the mouse pick (the window size).

With the bounds in order nothing changes. Calls whose bounds the code keeps in order (constants,
SplineFidelity 1-4, the season table, the guarded output calibration) give the same result with
std::clamp and stay as they are.

The load offset takes its arguments in the 26.05.1 order again; fa2ed089 swapped them, which with the
old clamp differs only for a negative load.

The bump at a rail joint gives the old result again. Up to 26.05.1 its clamps had 0 in place of the
value (since 96f43e11 and 4d72d352), and the old clamp made that the headroom Vmax - (Vel + 0.32 Vmax),
negative above 68% of the top speed, capped from above only; d464628a and 03bfbb13 kept it at 0 or
above. Only damaged track has the bump: a damage flag in the .scn, a derailer set by an event, a track
made on a derailment.

The interior light of the cab in the 3.3 renderer is computed in double again, as before d1f16411.

The call added since (stepped_uc_value, 00955513) keeps what master does: bounds given the other way
round are put in order.

* fix: the alpha of a particle in the legacy renderer as up to 26.05.1, the NaN cases of the clamp tested

Up to 26.05.1 the alpha was clamp<std::uint8_t>( particle.opacity * 255, 0, 255 ): the float was
made a byte first, so the clamp did nothing and an opacity over 1 (a smoke max_opacity over 1)
wrapped round, 1.2 giving 50. The move to std::clamp made it 255. It's made a byte as then again,
through int: the byte the x86-64 compilers gave, without the undefined behaviour of that conversion
(MSVC makes the same two instructions of both). openglparticles.cpp goes with feat/gl46-migration;
its opengl33 counterpart, std::clamp( opacity, 0, 1 ), already gives what 26.05.1 gave.

The tests pin what the old clamp gave with NaN: a NaN value stays NaN, a NaN bound doesn't clamp.
The checks named after the AI braking test and the bump at a rail joint say they test the helper with
the arguments of those calls, not the calls.

* test: the includes of the clamp tests before the project's headers

<cmath> and <limits> go up, after the test headers, so the change no longer touches the lines
#416 adds its includes after. With both merged <cmath> is included twice, which is harmless.

---------

Co-authored-by: maj00r <maj00r@host>

* Python screens: the worker's image goes in place only after its fence, not a bare glFlush (#411)

* fix(python): a screen uploaded by the worker goes into a texture of its own, put in place after a fence

With python.threadedupload (the default) the worker wrote the image into the texture the drawing thread
draws from, with glTexImage2D and glGenerateMipmap from its own context, and only a glFlush after. Nothing
ordered the two contexts: the drawing one could sample the texture while its storage was being made anew,
its level 0 half written or its mipmaps not made yet, and on a change of size the worker deleted the texture
and wrote a new name into `id` with no synchronisation.

The worker now uploads each image into a new texture, sets a fence after it and flushes. The drawing thread,
once per frame in python_taskqueue::update(), waits for that fence on the gpu (glWaitSync, the cpu goes on),
deletes the old texture and puts the new one in place. The worker no longer touches the texture drawn from,
nor `id`. An image the drawing thread didn't take yet is dropped by the next one. Without sync objects the
worker waits for the upload with glFinish.

The windows of python.displaywindows with python.sharectx bound the texture once; they now bind the name of
the last image put in place, which python_rt keeps for them.

Uploads on the drawing thread (python.threadedupload no, the Lua and WebAssembly screens, the editor) are
as they were.

* fix(python): the image of the worker goes in place once its fence is signalled, the viewer binds it each frame

From the review of 8811ee5a:

- glWaitSync ordered only the context of the main window. The extra viewports and the windows of
  python.displaywindows draw in contexts of their own and could bind the new texture before the upload
  was done. commit_thread_upload() now asks the fence without waiting (glClientWaitSync, timeout 0) and
  puts the image in place only once it is signalled, so every context binding it afterwards has it whole.
  Until then the task stays in the upload queue and is asked again the next frame.
- The drawing thread chose between upload and commit by python.threadedupload, the worker by whether it
  got a context. With python.threadedupload on and no context made for the worker, the screens were no
  longer updated. The task now records that a worker uploaded it.
- The viewer of python.displaywindows told a new image by its texture name, but a deleted name comes back
  for a later texture. It binds the name of the last image each frame, read under the lock of the render
  target, and the name it starts with is read on the drawing thread when it is made.

* fix(python): the old texture deleted under the lock the viewers bind under, a fence not made waited for with glFinish

From the review of #411:

- finish_upload() deleted the old texture before it took the lock of the render target, and the
  windows of python.displaywindows read the name under that lock but bound it after letting go of it.
  A window could bind a name already deleted, or already given to the next upload. In a compatibility
  context (the legacy renderer) that makes a new empty texture: a black frame and a texture never
  deleted. The commit and the publishing of the new name now go under the lock, and the viewer binds
  under it too. A name bound before the deletion stays alive in that window's context while bound.
- A glFenceSync that returns no fence was taken for a signalled one: the image went in place at once,
  with nothing ordering the contexts. The worker now waits for its upload with glFinish then, as
  without sync objects.

* fix(python): the worker waits for the lock of the screen without the gil, no flush after a finish

From the review of 939514ea:

- The drawing thread now holds the lock of the render target for a whole upload with
  python.threadedupload off. The worker, copying the next image under that lock, waited for it holding
  the gil. It now lets the gil go around the lock and the copy: the bytes it copies stay valid, the
  object is immutable and held by the reference the worker has.
- glFlush after glFinish did nothing; the worker flushes only when it made a fence.

---------

Co-authored-by: maj00r <maj00r@host>

* fix(models): a scenery light that is off no longer shows lit for a moment the first time it comes into view (#407)

* fix(models): a scenery light that is off no longer shows lit for a moment the first time it comes into view

Since d13c27a7 (the light blinking rework) the opacity of a light changes step
by step from its last value, and the opacities start at 1. RaAnimate runs only
for the instances that are drawn, so a model with "lights 0" (a W24 LED
indicator, for one) was drawn lit when it first came into range and faded out
over 0.45 s. Before d13c27a7 the opacity of a plain on/off light was worked out
anew from a timer starting at 0, so a light that is off was off from the first
frame.

Now the first time an instance is animated its lights take their current
state at once, whether it came from the definition or from an event since.
Every later change fades as before. The light threshold modes (ls_Dark,
ls_Home) are untouched. The only other difference from before d13c27a7: a
light that is on no longer fades in the first time it is drawn.

* fix(models): the light opacity step written without an implicit double to float conversion (Sonar S5276)

The sum is still worked out in double and rounded to float once, as the
compound assignment did, so the result is the same to the bit.

---------

Co-authored-by: maj00r <maj00r@host>

* Editor: switch drives with no switch listed and marked; a deleted switch takes its drives and ballast along (#330)

* feat(track): the rail code split by concern, the blades of the switches with the key frogs shaped from their templates

The rails are laid by code of their own instead of inside Track.cpp: gauge lines (GaugeLine), knot lines (Knots.h), crossings and check rails (RailCrossings), rail materials (RailMaterials), measuring the rail profile (RailProfile), planning the cuts (RailPlan), the carving scan (RailCarve) and laying the geometry (RailLayer).

A switch with the key frogs draws its blades from the tip where their planed nose ends to the heel, bent as they're thrown, and the rail behind them from the heel on, where they were laid from the start of the switch before; on a switch of the catalogue they start at the cut tip of the tables, 1.248 m from the joint at R 190, 1.5 m at R 300 (track_features::blades). The frogs cut from the rails, the wing and the check rails are built but switched off (track_features::crossings), the rails laid whole as before. The switches without the key keep the blades they had.

A switch path can be a chain of pieces: pieces1 and pieces2 keep the parts of each path after the classic bezier, which stays as the approximation older versions read. A path with its radius given is a curve however gently it bends, a plain track a straight only where both of its control vectors lie along it. A vehicle trailing through a switch with a movable nose set the other way runs off the rails at it (TRAILED_SWITCH).

* feat(render): the shared treads of the rails and their cut faces drawn, the cut faces in the matte variants of the rail materials

Both renderers draw the treads a track shares with the rails it crosses, in the pick pass too, and a pass of their own for the faces where the rails are cut, the ends of the blades among them, in the matte variants of the rail materials, so the cut faces don't shine like the head the wheels polish.

* feat(vehicle): an axle on a switch keeps to the path it came on, and coming from the frog end trails the switch through where the wheels reach the blades

The switch thrown under an axle no longer moves it over onto the other path; it runs on along the one it entered. Coming from the frog end, where the blades are shaped or the frog has a movable nose, the switch set the other way is trailed through (rozprucie) only where the wheels get to the blades or to the nose, not as the axle enters the track; the other switches are trailed through as it enters, as before.

* fix(track): the tracks drawn straight before, which their second control vector bends now, written to the log

The simulator told a straight by the first control vector of a path alone, so a track whose second control vector bent it was drawn straight; now it's a curve. Such a track is named in the log as it loads, so a scenery made counting on the straight can be found: 25 tracks of lk357. Paths with both control vectors along them bend a centimetre or so at most and aren't named.

* feat(track): 3D sleepers and switch timbers laid where the simulator makes the ballast, switched off in the build

The sleepers of a track running alone are laid square to it, the timbers of the switches and crossings across the tracks running over them, shared by them, of the models set with sleepers3d and sleepers3dplain in eu07.ini; sleepers3dbed tells whether the ballast is laid under them too, lowered so they stick out of it. The layout of a cluster of tracks is worked out once and kept till a track of it changes, in a thread of its own while the simulation runs. timbermodel of a track, read and written back before, takes the place of the model of sleepers3d there. All of it is off whatever eu07.ini sets (track_features::sleepers), the tracks keeping their ballast and their sleeper models.

* feat(render): the 3D sleepers and switch timbers drawn in instanced batches

Render_Timbers lays the beams of the tracks in range where they're out of date and draws those of a run of tracks of a cell gathered by the model and its part or its level of detail, in as few instanced batches as they fit in; in the shadow pass only those near the camera. A track with them leaves its old sleeper model out. OpenGL 3.3 renderer only.

* fix(track): the counts of what's wrong with the sleepers kept safe from the thread laying them, and that thread stopped before what it works with goes

The counts of the beams clipped, dropped and off the plane, of the slowest and the biggest cluster, are counted by the thread working out the layouts in the background as well as by the simulation laying a cluster itself, and were plain values written by both; they're atomic now. What the thread keeps the clusters in, their version and the counts were made after the worker, as it first asked for them, and so destroyed before it at the exit while its thread could still be laying a cluster; they're made first now, the worker stopping and joining its thread before they go. The tracks go with the paths of the simulation, made before the worker.

* revert(track): a tributary and a track of an unknown type load as before

They came in with the rail code from the guards of the editor against crashing, and don't belong with the blades: a track of an unknown type loaded as a normal track of the railway could be joined to it and run on, and a tributary loaded as a river with its first path lost the second one when the editor saved the scenery. Both load as on master again; the place where the editor crashed on them is to be guarded where it is.

* fix(track): the tracks drawn straight before named in the log once, as they load, and the paths of the switches with no radius told a straight as before in one term

The log of the tracks the second control vector bends now was written each time the segments of a track were made again, as a track with its unevenness loads and as the editor changes or drags a track; it's written only the first time, as the track loads. The paths of a switch or a crossing with no radius keep the rule of master, a straight told by the first control vector alone, its two terms being the same length; they're one term now, with the comment saying so.

* fix(track): the thread laying the sleepers stopped as the application exits, and loading no models itself

The thread reads the scenery, which the application deletes as it exits well before the statics go: it's stopped and joined first thing in exit() now, the worker destroyed with the statics only where the application exits some other way, and not started again after. The models the beams are made of were loaded by the thread where it didn't find them, through the renderer, out of the thread of the renderer, into a map read by the simulation meanwhile: they're found under a lock now and loaded only by the threads other than it, again for all the tracks whenever a track changed since, before a layout is asked of it. With the 3D sleepers off in the build their settings aren't exported, and a track changed doesn't look for the tracks around to lay their beams again.

* feat(track): the 3D sleepers turned on with sleepers3d in eu07.ini, no longer switched off in the build

track_features::sleepers is gone: sleepers3d <model> in eu07.ini lays them, none (the default) lays nothing and the
tracks keep their ballast, as before. The sleepers3d settings are written back with the others.

The editor changes the tracks, so while it's open their sleepers are worked out on the main thread as they're needed,
not in the background.

* fix(render): only the part of a timber model drawn, not what else the model holds

A timber model of parts may hold more than its begin, main and end, as the kits with the fastenings of the rails do. Those
were drawn with each part of each timber, in the middle of the track; they aren't laid, so they're skipped now.

* fix(track): the 3D sleepers laid only for a renderer which draws them

Only the OpenGL 3.3 renderer draws the beams. With another one a track set to them lost its ballast to them, lowered or
not laid at all with sleepers3dbed no, and nothing was drawn in its place. Draws_Timbers() of the renderer tells now,
and where it's false the tracks keep their ballast, as with sleepers3d none.

* fix(render): the submodels of a timber model left as they were after its parts are drawn

They were all made visible after a part was drawn, the ones the model hides itself too, like Light_On, while the same
model may stand in the scenery. The submodels of the top level besides the parts are listed as the model is loaded,
hidden while a part is drawn and given back their own visibility after. The parts have to be at the top level of the
model for that; a model with them deeper is logged and drawn whole.

Render_Timbers no longer looks the model up under the lock for every run of tracks, the name set is enough there.

* fix(render): a vertex whose faces give its texture no direction gets a tangent across its normal, not a NaN one

The cut ends of the rails have one spot of the texture on all their corners, so their tangent came out of normalize() as NaN. The normal-mapped shaders then shaded them with a NaN normal: about half of the vertices of the cuts and frogs, and the end faces of the rails.

* feat(track): the frogs cut from the rails turned on with frogs in eu07.ini, no longer switched off in the build

frogs yes cuts the rails of the plain tracks and of the switches with the key frogs where they cross other rails, into frogs, wing rails and check rails. It's off by default: the rails are laid whole as before, the blades of the switches with the key are shaped still.

* refactor(editor): the track mode file split into the files of its tools, the code as it was

A pure move, so that the commits after it show what changes in each tool rather than a 4800-line file rewritten.
The member functions of editor_mode go to the file the editor of this branch has them in: route, straights, lay,
switchplace, switchdrive, switchtool, trackgizmo, and the rest stays in editormodetrack.cpp. The helpers of its
anonymous namespace go to editormodetrackshared.h, as they were but inline, in namespace track_mode, which the files
use. No line of code changes; the files add only their license, includes and the using line.

* feat(editor): the groundwork the tools of the editor share

- The tool interface (editortool.h), the gestures of the mouse (editorgestures.h) and what the modes share
  (editormodeshared.h); the projection, viewport, selection and texture picking taking them up.
- Scripted input (scriptedinput): the input of a selftest script fed to the editor as if typed and clicked, the real
  mouse and keyboard ignored meanwhile; EU07_EDITOR_SELFTEST_HIDDEN draws into a window never shown nor focused.
- The ui layers and panels of the editor, its settings, icons and the info of the includes; the layers of the scenery
  and their saving; the launcher mode.
- utilities: user_cache_path (the cache folder of the system for the user) and the helpers of the translations.
- stb_image: the gif loader takes the frame two back from the frames it decoded, not from before its buffer.
- tools/editor: the scripts making the headers of the drives, the turntables, the editor and its usage.
- Unit tests: test_area and the stubs of the editor the tests link.

Not built on its own: the tree builds again with the selftests commit, the one before the translation.

* feat(editor): the tracks laid along an alignment, to the PKP PLK norms, with their grade line and the ground

- The alignment of the laying (editorAlignment, editorAlignmentStore): straights, transition curves and arcs with the
  cant of the norms for the speed, the radius below the minimum for it a warning; the lay, route and straights tools
  build on it, the route plan reading the curves going on from the tracks adjoining.
- The grade line (editorProfile, the profile and gradeline tools): fitted to the terrain model or edited by hand, the
  tracks fitted marked in the scenery (//$f), the shared grade lines of nearly parallel tracks (//$n).
- The ground under the tracks (editorGroundFollow, trackbed): the terrain drawn to the tracks and restored, the tracks
  put onto the terrain.
- Connections between tracks (editorConnections, the connection and connection curve tools), the track presets, the
  shape of a track (editorTrackShape), digitizing tracks from the orthophoto, the properties sheet of the tracks
  (trackprops) and the track ux.
- Unit tests: alignment, ground fit, heights, with the data of two fits.

Not built on its own: the tree builds again with the selftests commit, the one before the translation.

* feat(editor): the switches placed, slid and designed from their templates

- The switch templates (editorSwitchStore, the template tool): the catalogue switches and the user's own, kept with
  the settings; the designer (editorSwitchDesign) with a drawing of the dimensions, a and b typed in with the shape
  making up the rest, a switch clicked off the orthophoto.
- The curve of a switch laid into a curve (editorSwitchCurve), placement on a straight or into a curve, slide along
  it, crossovers and double slips (switchplace, switchslide), the drives of the switches (switchdrive) and the records
  of the switches placed (records).
- Unit tests: test_switch_curve.

Not built on its own: the tree builds again with the selftests commit, the one before the translation.

* feat(editor): the tools of the infrastructure beside the tracks

The gauge, speed, joints, lineside, parallel tracks, vehicles, signals, turntables, sweep and road tools with headers
of their own; the kilometrage of the lines (editorKilometrage, the km tool); the infrastructure, roads and speeds
behind them (editorInfra, editorRoad, editorSpeed).

Not built on its own: the tree builds again with the selftests commit, the one before the translation.

* feat(editor): the terrain model of geoportal.gov.pl and the orthophoto downloaded for the editor

- editorNmt: the squares of the terrain model at 1 m (NMT GRID1, the WCS service of geoportal.gov.pl) fetched by two
  workers, kept in editor/cache/nmt/grid1 of the game folder, the heights of the scenery taken from them; the nmt
  mode loads it along the tracks or around the camera.
- editorHttp: the HTTP client of both, WinHTTP on Windows, libcurl elsewhere where it's built with it; without it the
  caches only.
- The orthophoto: its cache moved to the cache folder of the system for the user, the download area a polygon on the
  topo map (//$a) filled in the background, the 4K imagery, the tiles waiting outlined on the ground.
- editorGeoMap: the topo map of the top view; heightmapterrain and the terrain streamer grow the terrain made from the
  model around the camera.

Not built on its own: the tree builds again with the selftests commit, the one before the translation.

* feat(launcher): the scenery wizard picks the place of a new scenery on the topo map the editor shares

The wizard drops its own map download and decoding for the topo map widget of the editor (editorGeoMap). The place
clicked becomes the geographic reference of the new scenery (//$g PUWG1992), and the scenery starts with the
orthophoto of the editor on (//$o), lying under it in place.

Not built on its own: the tree builds again with the selftests commit, the one before the translation.

* feat(editor): the tools wired into the editor mode and the build

editor_mode takes the tools of the commits before: its header declares them, editormode.cpp and the track mode drive
them, and the renderers draw their overlays (opengl33 and legacy). CMakeLists lists the new sources and adds
WITH_EDITOR_SELFTESTS (off by default); the unit tests build the new test files.

Not built on its own: the tree builds again with the selftests commit, the one before the translation.

* feat(editor): the selftests of the editor split by what they test

The scripts of EU07_EDITOR_SELFTEST run the commands of editormodeselftest*.cpp: scene, track, rails, join, switch,
signal, sleepers, sweep, terrain, monkey. The code is in every build; only with WITH_EDITOR_SELFTESTS=ON does the
editor read the variables and run a script. The selftests of master now need the option too.

The tree builds again with this commit.

* feat(editor): the Polish texts of the editor, all of them in one update of editor_pl.po

* fix(editor): the marks of the editor moved off the letters the starters read, and its lines told by their words

The starters read some letters in a scenery file: the Starter the title //$n, the attachments //$f and the old scenery //$a,
StarterNG and the launcher of the game the attachments //$f and the title //$n. The editor wrote its download area,
shared grade lines and fitted tracks under them, and took every line of those marks as its own: saved, a scenery lost
its title and attachments, and the starters showed the lines of the editor as them.

- The download area is //$z, the shared grade lines //$w, the fitted tracks //$t.
- Their lines and those of //$o are told by their first words, as //$h, //$r and //$k were: //$o only as "on 0" or
  "on 1", the origin or the settings of older builds after it, so a trainset described as "on ..." isn't taken.
- The lines older builds wrote under //$a, //$n and //$f are read only by the words the editor wrote in them, or by
  its own comment line before them, under the new mark. Each save writes them again with it, also when the editor
  didn't change them; the lines of the starters stay.

* fix(editor): a square of the terrain model cut short isn't read, and the squares are kept in the user's cache

The service puts the directory of its GeoTIFF before the pixels, so a file cut short by a write or a fetch broken off
was read whole, the pixels missing coming out as heights of 0 m, for good.

- The reader (editorGeoTiff.hpp, with unit tests) wants all the strips the rows need, each within the file.
- A square is written under a name of its own and renamed.
- The squares are kept in the user's cache folder (nmt/grid1), as the orthophoto, since the game folder may not be
  writable; without one, in the game folder as before. The ones older builds kept there are moved over as each is
  asked for, a damaged one deleted.
- Capabilities that didn't come aren't kept as the fallback name for the session: the next square asks again.

* chore(editor): the http client said to serve the terrain model and the topo map too, the gif fix of stb_image marked local

- CMake names the orthophoto, the terrain model and the topo map as what goes without libcurl.
- The fix of the gif frame two back in stb_image is ours, not upstream's (v2.30 still has the old line): a comment at
  the line says so, for an update of stb_image to keep it.

* fix(editor): the lines moved off the letters of the starters in a merged layer written once, not again with each save

A merged layer's text goes into the file of its target on save, and the target is read again; the marked lines of the
merged layer were dropped only for the marks the editor had changed. The lines moved to the new letters stayed with
their old places, and the next save of the session wrote them into the target once more.

- After a save, the marked lines of every merged layer are dropped; those of its target are read again from its file.
- The lines of a dead layer aren't written again.
- The selftest layermerge merges the layer of one file into another's, as the layers panel does.

* feat(editor): the station as the signalling sees it, found from a switch of it, and its schematic

editorStation: the switches joined by plain track no longer than 1.5 km, the tracks between them and the lines leading
out, 400 m of each beyond the outermost switch; laid out as a schematic along the main track through the station. The
track circuits, the signals and their lights, the lengths of the station tracks and the lines to the next stations are
worked out from it for the signalling tool.

Nothing uses it yet: the signalling tool, wired in the next commit, does. It builds on its own.

* feat(editor): the signalling of a station in the Signalling tab, laid out from a switch of it as on the lines of PKP PLK

LMB on a switch in the Signalling tab opens its station: the schematic in a window of its own, the name, the code, the
line and the km of the station, and the steps of its signalling, each one step of undo:

- the switches numbered along the station after its code, bpd_1, the parts of a slip bpd_15a..d; the drives and the
  objects naming a switch follow it, events written by hand keep the old names;
- the tracks cut at the joints of the track circuits, Ie-4, and the circuits named, bpd_iz1, bpd_it1;
- the entry, exit and shunting signals placed and named, A, F1, Tm1, from the templates chosen;
- the signals of the lines out: the automatic block of three or four aspects, the block sections no shorter than the
  braking distance, or the semi-automatic one with a warning disc; block boundaries drawn by hand;
- the lights each signal needs for its routes, Ie-1 and Ie-4 § 8, its template fitted to them;
- the SHP track resonators the signals lack, rezonator.inc;
- the lengths of the station tracks as a table, and what the station and its lines still lack, with Repair.

The stations described are kept as //$s lines in the files of their layers.

* fix(editor): the signals of a station and of its lines not placed with no template for a kind, no track cut for them first

- Set the signals and Set the line signals cut the tracks first, and only then found no template for a kind. They
  stopped there, but the cuts stayed as a step of undo. Now they ask for the templates first, by the lookup
  signal_batch() uses (signal_template_for(), behind editor_signals::signal_templates_missing()). Without one
  nothing is cut.
- Set the signals and Set the track circuits again under Track lengths had the ids of the buttons of the steps
  above them. ImGui flagged them when hovered. Each now has an id of its own.

* fix(utilities): win1250_to_utf8 turns the polish letters into utf-8, not into themselves

utilities.cpp starts with a byte order mark, so the compiler reads it as utf-8. The game's targets don't get
/utf-8, though: set_target_properties(... COMPILE_OPTIONS ...) in the MSVC block of CMakeLists.txt replaces the
options add_compile_options gave them. So the string literals went into the program in the code page of the
system, windows-1250 on a polish one, and the map of win1250_to_utf8 turned each letter into itself.

What read windows-1250 through it got no utf-8: the names, descriptions and categories of the sceneries and the
names of the textures in the launcher, and the descriptions of the templates in the editor. The editor writes
such a description back through utf8_to_win1250, which makes a question mark of each byte that doesn't start
a utf-8 sequence, so the letters it had read unchanged didn't come back.

The utf-8 sequences are written as bytes now, the way utf8_to_win1250 has them, so they don't depend on the
options. The options stay as they are: with /utf-8 other literals of the files with a byte order mark would
change, the "±" of the table of Bezogonkow would take two bytes and move the letters after it.

The unit tests check the letters and that the text given is valid utf-8.

(cherry picked from commit 9ffed96ec1)

* fix(editor): the switches renamed only after the events by their old names are shown, the station records in windows-1250 and in the file the scenery had

Number the switches renames the switches and what the editor writes along with them, but not the events written
by hand: a switch event aiming at a switch, or <name>:event0 and the others named after it, would keep the old
name and lose its switch. Now the first click lists them in the status, up to six and how many more, and renames
nothing. A second click with the same renames goes ahead.

The name of a station is written in windows-1250, like the rest of the scenery file. A name read that isn't
valid utf-8 is taken for windows-1250; the utf-8 the earlier builds wrote is read as it is. utf8_to_win1250
moves from editorIncludeInfo.cpp to the utilities, beside win1250_to_utf8, and the unit tests check both ways.

The record of a station that had none goes beside the records of the other stations, in the file marked with
them, so a scenery saved under another name keeps them in one file. output_layer takes, after
<scenery>_<part>.scm, an included file of the part of another name, the one of the scenery it was copied from,
before it makes a new one.

The comments: the joints of the track circuits stand 4.2 m beyond the fouling points, where the axes are 3.5 m
apart, as joints() counts them; the regulation of the lengths is cited with its date and title.

* fix(editor): a scenery saved under another name keeps one file of the keys by the movable noses, not the old one beside its own

The editor writes the keys by the movable noses on each save to <scenery>_dzioby.scm and includes it. A copy of
a scenery, saved under another name, still included the file of the scenery it came from, and its save wrote a
file of its own and included it too, so each key stood twice.

Now the save leaves out of the scenery the other files of such keys it includes, told by the line the editor
writes at their start. Their files stay on the disk, for the scenery they were made for. A file made by hand,
without that line, stays included. The turntable controls had the same, fixed the same way on their own.

* fix(editor): a file of the scenery named like a part of a tool isn't taken for the one the tool made

output_layer took, after <scenery>_<part>.scm, any included file named *_<part>.scm, so that a scenery saved under
another name goes on with the file of the scenery it was copied from. A file of the scenery named alike, written by
hand, such as rejon_teren.scm, got the terrain directive, the hectometre posts or the new signals too.

The files output_layer makes start with a line of their own now, "// scenery layer created in the editor for its
<part> part", and a file of another name is taken only with that line. A layer gets it through the new parameter
of scene::node_layers::create(); the others start with the general line as before. <scenery>_<part>.scm of the
scenery itself is taken without the line, as it was. A file made for a part by an earlier build has only the
general line, so a copy saved from it gets a file of its own.

* feat(editor): a player of GIF clips for the windows of the editor

editor_clips::clip plays a GIF in an ImGui window, looped, from the time given. stb_image decodes the whole
file on a thread of its own, so the window isn't held up; until then the clip says it's loading. The frames are
kept as stb_image decoded them, and the one shown is a texture of the ImGui renderer (Create_Image), made again
only when the frame changes. A file that isn't there is told missing at once, with no thread started.

Nothing uses it yet; the tutorial of the next commit shows its clips with it.

* feat(editor): Help > Tutorial, fifteen short lessons of the editor

The lessons: looking around, laying track, curves, the radius of the curves at the bends, correcting a curve,
joining tracks, switches, switch templates, signals and posts, selecting and fixing, editing with the gizmo, the
grade line, terrain from the terrain model, sceneries made before, saving and driving.

Each lesson has:
- in eight lessons, a looping clip from lang/editor_tutorial/<lesson>.gif, played from the start on a click;
  in six others, a moving drawing (the radius at a bend, a switch template clicked, the signals, the grade
  line, the terrain, a trainset);
- the mouse buttons and keys drawn as pictures beside what they do;
- ticks that fill in as the user does it, told by the tools through editor_guide::first_step_done();
- a button that turns the tool of the lesson on;
- Back / Next and a dot per lesson, green when the lesson is done.

The window follows the work: when the tool in use changes, it turns to the first lesson of that tool not done
yet, and a page turned by hand stays until then. In a lesson of laying it warns when there's no ground under
the cursor.

Where it opens:
- by itself when an editor session starts, until "Show when the editor opens" is unticked (first_steps in the
  editor's settings);
- from the Help menu;
- from a Tutorial button beside Lay a track in the hint of an empty scenery, which isn't drawn while the
  tutorial is open;
- at its lesson, from a "?" by the radius at the bends in the lay panel.

A lesson whose clip isn't there is shown without it; the clips aren't in this commit.

editor_guide is a new role of the editor, given to the hekto-post and trainset tools. Selftest command
tutorial <page>: opens the tutorial at a lesson, from 0; -1 closes it.

* fix(editor): the lessons of the tutorial say what the editor does

The lesson texts checked one by one against the tools; the ones that told otherwise:
- Switches: a connection isn't chosen at the top of the panel, its kind comes from where the drag is let go (a
  crossover onto the other track, over a crossover the other way a double one, or scissors where they overlap).
- Switches: a ladder leads onto the parallel tracks it's dragged across, let go on the farthest, with the
  straights up to where they start; it lays no missing tracks.
- Terrain: there is no step 3. Step 1 is Location, step 2 is Load around the camera while there are no tracks
  and Tracks and terrain with its three ways once there are. The rows had the step numbers in the keys column,
  and 1, 2 and 3 switch the edit modes in the Terrain field. Now the steps show the mouse, the terrain under
  some tracks only is a row of F3 (the line the terrain panel shows), and the rows that only tell what the
  fit does have no key.
- Sceneries made before: a plain include can be changed; the row lists what can't: an include template (.inc),
  the binary terrain cache, a file included twice or with parameters, a locked or hidden layer.
- Laying: the second Esc drops the points only within 3 s.
- The radius at the bends: two transition curves from 6000 m down to 2000 m while they're long enough for the
  speed, then an arc; a label turns orange where the curve breaks the norms, not because it came out smaller;
  two transitions meeting read 2xL and R; the labels of each transition curve are on laid tracks, close up.
- Signals and posts: the W17 markers go by the selected switch, around the camera or the whole scenery, not
  along a line.
- Selecting and fixing: E lengthens the track, the control vectors bend it.
- The gizmo: at a point Q moves the point alone, W turns the whole track about it.

The tooltip of Help > Tutorial no longer speaks of clips: the tutorial goes without them. lang/editor_pl.po
has the new texts.

* fix(editor): the lessons of the tutorial in plain words

Every row, goal and warning of the tutorial is said again for someone
who has never laid a track, in English and in Polish:
- the words of the railway and of the editor are said in everyday ones,
  or explained where they come: the radius (how big the curve is), the
  transition curve (the easing into it), the cant (the outer rail
  raised), the gizmo (handles on the track), the control vectors (points
  that bend it), the grade line (how the line goes up and down), the
  terrain model (the real ground), the orthophoto (the aerial photo),
  the hectometre posts (every 100 m), W17 (how far a wagon may stand);
- what only a designer needs is left out: the 6000 and 2000 m of the
  radius ladder, the compound curve, the hump, the kinds of include that
  can't be changed;
- the buttons are named as the panels name them: Apply to the scenery,
  Template editor..., the tiles of the switch tool;
- the Polish names of three lessons say what they are about ("Uchwyty na
  torze (gizmo)", "Wzniesienia i spadki", "Prawdziwy teren"). Their
  English titles stay, the lessons are found by them;
- the warning of no ground under the cursor tells what happens to the
  track;
- two captions of the drawings say what they show (the tracks inside a
  box, posts every 100 m), and in Polish the button of the top view is
  "Widok z góry (O)", without "ortogonalny".

What each row tells of is the same as before. The msgid of the length
typed while laying stays, the window of the keys (F1) uses it.

* feat(editor): the semaphores and the warning discs chosen by what their descriptions say they show

The descriptions of the signal templates can now say what a semaphore shows and what a disc
repeats, and the editor reads it:

- signal: {arms: 2, aspects: {Sr1: ..., Sr2: ..., Sr3: ..., Sz: ...}, repeats: {stop: ...,
  vmax: ..., "40": ...}} is kept with the template, the events named for each aspect
- the palette shows a template with arms as a semaphore of its arms, with 1 arm / 2 arms and
  Sz picking the template of the same tile that has them
- the signals of SRK take a semaphore whose aspects cover what the signal needs (Sr3 where a
  speed is reduced, Sz on the main signals), the fewest arms that do it, and say which one
  does when the placed one hasn't them or has more than it needs
- the warning disc before a semaphore is one whose repeats cover stop, the highest speed and
  40 km/h when the semaphore shows Sr3; SRK writes no events of the routes, so this only
  chooses and checks the disc
- the linked signal (parameter 7) is read for the role link as well as text
- signal_headers.py keeps the keys written by hand in a description (arms, aspects, repeats,
  the description and the parameters from 6 on) when it writes the guessed ones again
- signaltemplates of the self test lists what the templates say and the heads made of them

* feat(editor): the shaped discs swapped and set by hand, the warning discs checked by what they repeat

The shaped shunting and stop discs (mk1, tzm, z1) are known to the signalling, but it doesn't
place them by itself:

- a shaped disc chosen in the palette for the shunting or the stop signals is placed by hand
  only; the signalling of a station takes the light template of the kind there, and says so
  when there is none
- the template of a shunting or a stop signal standing can be swapped for a light or a shaped
  one of another mount as well (dwarf and mast, not gantry)
- a signal whose description lists its aspects shows a button for each under its card: it sets
  the signal to that aspect in the editor, by the animations, the lights and the submodels its
  event sets off, the time standing still; nothing of it is saved. TAnimContainer::Finish takes
  an animation to its end at once, giving the submodel its angles, multi_event::branches tells
  the events set off when the condition isn't met

From the review of the descriptions:

- a shaped warning disc standing before a semaphore described is checked by the events it
  repeats: stop, the highest speed and 40 km/h of a second arm told apart; one which doesn't is
  offered the disc which does, and Fit swaps it
- the disc offered is of the mount of the disc chosen, taken from the template even when it
  isn't described
- 1 arm / 2 arms / Sz of the palette pick the template leaning as the chosen one and most like
  it by its name
- a semaphore described with its arms but not its aspects has Sr1, Sr2 and with a second arm
  Sr3
- Fit leaves a semaphore having what it needs with its arms, for the events written by hand to
  the aspects of a second one; the messages of the fitting speak of what the signals need, not
  of their lights
- signal_headers.py warns of a signal map written as a block, whose keys it can't keep

* fix(editor): an aspect shown by hand doesn't name a vehicle that may be gone

The Show button runs the events of the aspect outside the queue. run() writes
the vehicle that set the event off last into the log, and nothing clears that
pointer, so a train deleted since would have been read after it was freed. The
queue sets the activator anew each time, so it is cleared before the run.

* fix(editor): a disc not described is only offered the one that tells, a light stand-in of the mount of the shaped one

- A shaped warning disc whose template doesn't say what it repeats may tell
  all its semaphore shows as well. It is no longer marked as not fitting:
  the disc described is offered as a hint, and Fit leaves it be. A disc whose
  template isn't among the scanned, its mount not known, is offered nothing
  rather than discs of any mount.
- A shaped disc chosen for shunting or stop is stood in for by a light
  template of its own mount first, a dwarf for a dwarf.
- The tooltip of the aspect buttons says the trains read the signal as its
  events set it last, in a run started from the editor too.
- The Polish texts of the fitting speak of signals, not semaphores, since it
  takes the discs too.

* refactor: the Sonar remarks on the switch blades taken care of

- the big functions of the rails split into helpers with one job each:
  the carving, the laying and the planning of the rails, their
  crossings, the blades, the lofted spans of a segment, the profile of
  a rail measured piece by piece, the joining of the switches
- the floating-point loop counters are while loops doing the same
  additions, so the samples come out where they did
- C arrays are std::array, the two sides of the paths path_sides; the
  ends of a cut are two parts of one struct, ending and starting
- the rail laid by a course of its own, not eleven parameters
- the globals kept in functions, the nested ternaries taken apart, one
  declaration per statement, explicit captures, ranges algorithms,
  std::format, std::numbers, emplace_back, structured bindings
- the region finds the sections around a point into a list the caller
  gives, so the search for the rails around a track shares no list with
  another thread
- check_rails and create_track_rail_profile are const, the pool of the
  reused chunks can't be copied

The geometry of every chunk of lk357 and td_fog, with the frogs on and
off, comes out the same as before.

* refactor: the Sonar remarks on the 3D sleepers taken care of

- the big functions of the sleepers split into helpers with one job
  each: the clusters and the lines of timbers, the gaps under the rails,
  the models and their parts, the check of the layout, the drawing of
  the timbers in instanced batches
- the tracks near a track found into a list of their own, the region
  searched into a list the caller keeps, not the one the region shares
  with the main thread; the callbacks are templates, not std::function
- the layout of a cluster takes a span of its tracks
- the globals kept in functions, the parts of a beam int constants,
  beam_overhang no longer hidden by a field of the same name
- scoped_lock, jthread, contains, ranges algorithms, std::format,
  emplace_back, structured bindings, explicit captures, upper-case
  literal suffixes, one declaration per statement
- the uniform buffer takes a span of bytes

* fix(editor): the editor core builds with GCC again

- kCrossoverStraights is constexpr, so the connection curve can take it into
  a constant expression; GCC refused a double that was only const there.
- the track_hover alias inside editor_mode dropped: declared after
  hovered_track had already used the name, it changed what the name meant in
  the class (-Wchanges-meaning); the definitions name the shared track_hover.
- <cstring> included where the rail self-test uses std::memcpy, which MSVC
  reached through other headers.

* refactor: the second round of Sonar remarks on the switch blades

- the carve of a throwable part takes the cut before it only where there
  is one, as only that cut makes it throwable
- the splitting of a grooved profile by the floor of its groove moved out
  of the rail layer into a class of its own, which is given the profile,
  the size of the rail, the texture of the plates and the side
- the rail drawing is made by a const function
- the triangles of an ear-clipped outline go in as the arrays they are,
  the search for a corner within an ear is an any_of, the loop pruning
  thin corners keeps the count it started with, as it stops at the first
- the bytes of the blocks of a texture are taken off the file through
  its buffer, with no cast of their storage
- the log of a carved part takes the tracks around it as a span
- the joint length of a switch, a constant, isn't captured, and the
  directions at the ends of a crossroads are tested in one condition each

* refactor: the Sonar remarks on the tutorial

- the window of the tutorial in smaller pieces: the steps reached, the
  lesson followed and its clip, the clip or the sketch of a lesson, its
  keys and steps, the button under it and the buttons of the pages; the
  lessons are a table of their own, the button of each kind of lesson
  picked from a switch
- a clip whose delays add up to nothing shows no frame instead of
  dividing by them
- the frames of a clip are decoded by a function of their own, on a
  thread kept with them and joined once it's done, or as the editor
  closes, instead of a detached one
- the sleepers of the signal sketch counted by a whole number, the texts
  of the sketches made by std::format, their points kept in std::array,
  the whole numbers turned into floats where it's meant

* fix: the bugs and vulnerabilities Sonar found in the editor core

- the loops stepping along a track, a curve or the view count their
  steps by a whole number and take the station as so many steps from
  the first, instead of adding a fraction up, which drifts
- the self-test of the guard rails keeps the plan of the rails of the
  track against while it goes through them: the range of the loop was
  in a plan made for it and gone before the loop began
- the generators of the self-tests and of a unit test are seeded on
  purpose, so a run can be repeated: marked for Sonar as such
- the tool writing the descriptions of the templates takes the names
  out of its tables only where they stay in the data folder

* fix: the loops of the signalling stepping by a whole number

the places of the block signals along a line and the marks of the
chainage under the plan of a station are counted by a whole number and
taken as so many steps from the first, instead of adding a fraction up,
which drifts; Sonar took the counters of floats for bugs

* refactor: the checks of the signal heads and the signal sheet in smaller pieces

the remarks Sonar made on the signal descriptions: the warning about
a disc on the mount of a signal is worked out by a function of its own,
the fit and the aspects of a head, the swap of a template and its
aspects in the sheet of a signal are drawn by members of their own, the
walk over the events of a signal is a free function instead of a
std::function held by a lambda, and the description of an include is
read a part at a time; the names of the kinds are kept in a std::array
and looked up by the algorithms of ranges, a name is taken as a
std::string_view where it is only compared, and the locals named event,
which hid the enumerator, are renamed

* style: a space after a comma in the join self-test

* style: a space after the question mark in the terrain sketch

* style: a space after the question mark in the status of the fitting

* refactor: the last Sonar remarks on the small fixes

the opening of a scenario catches the exception the scenery throws when
it can't be read, not any runtime error; the vehicle moved between
trainsets keeps its source by a reference to const; the messages of a
track of an unknown type and of a tributary are raw string literals;
the parser opens a file by a std::filesystem::path, and the log of a
file it can't open still names its folder and its name

* refactor: the crossings of a track found without casting its const away

the radius of a node can be read off a const one: radius_() is const
across the nodes, the ones of the tracks and the models marked override,
and a const radius() gives the radius worked out already, or works it
out anew without keeping it; the helpers finding the crossings of a
track only read it and take it const, its own run of rails typed apart
from the ones of the tracks it crosses, which the crossings found name;
the triangles of a filled outline are pushed as named corners

* refactor: the paths around a track sampled without casting its const away

the sampling of the paths and the clusters of tracks laid together only
read the tracks, so they take and keep them const: the path sampled
points at a const track, the tracks near one are found from a const one
by its const radius, and a cluster holds const tracks; the shared zones
of the timbers no longer cast the const of their track away

* fix(editor): the Sonar blocker remarks on the stack, no side effects in the operands of && and ||, no local named final (S912, S1669)

Counting the leftover signals, the pieces of a station walk, the places with nothing beside a run and the joints and fouling points
logged by the self-tests no longer counts inside a condition; the last span of a straight is named tail. The counts and what they
log are the same.

* revert(vehicle): an axle on a switch follows the switch as it is set, as before the stack

The axle kept to the path it came on, so a switch thrown under a moving train no longer sent the rest of it onto the other
branch, and coming from the frog end it trailed shaped blades only where the wheels reached them. That changed the running of
every switch in every scenery, and the AI still scans its route by the switch as set. TrkFoll is as on master again, and the
trailing point of the blades it used is gone.

* revert(track): a path is told a straight by its first control vector alone again, as the sceneries are made for

A plain track was drawn bent where its second control vector was off the chord though the first lay along it, and a path with a
radius given was never straightened. Tracks of existing sceneries drawn straight before would have been drawn bent, with their
length, cant and the models along them. TTrack::straightened is the rule of master, for plain tracks and the paths of switches.

The editor keeps a bend dragged with the second control vector, or set by joining the tangents, by moving the first one 2.5 cm
off the chord to the side of the bend, past the tolerance, so the track is drawn, saved and read back bent, by older builds too;
the second one dragged back along the chord takes the first one back onto it.

* feat(track): the switches joined directly to other switches at load told in the log

A switch right after another one is joined to it as the sceneries load, where its end was left open before and vehicles came off
the track there. Each switch joined so is logged with its place and the number of ends, and the whole number once, so a scenery
can be checked for places that run differently now.

* fix(editor): a bend kept by lengthening the first control vector, not turning it

keep_bent moved the first control vector 2.5 cm sideways, which turned the
start tangent: a kink of 0.29 deg on a 15 m track, flagged by the joints
check, and against the fixed direction at a switch or a locked track.

Now the vector keeps its direction and gets 2.5 cm longer than a third of
the chord, which the simulator no longer takes for a straight. It is also
applied after an end is moved or snapped, since a new chord or a snap onto
a neighbour could bring the vector back within the tolerance. With the
second vector back on the chord, the first goes back to a third of it.

* fix(track): each switch joined directly to another one logged with both names

join_switches counted ends, while one join links both switches, and only
the first switch of a pair was named. It now returns the switches joined
to, and the log names both of each joint and gives the number of joints.

* fix(editor): a path drawn bent is kept bent through every edit, one drawn straight is left as it is

The first control vector is made longer past the 2 cm tolerance only where the path was drawn bent before the edit, or where
the edit itself sets the second vector: the end of the path moved, its first vector, its second one, a neighbour followed or
its grade taken, the controls aligned at a joint, an end snapped. A path drawn straight keeps being straight when its ends
move, and the paths of a track that the edit doesn't bend aren't changed. Moving the second vector back along the chord takes
the first one back to a third of the chord where it was made longer here.

* fix(app): a scenery that can't be opened takes the game back to the launcher whatever the file system throws

Catches std::runtime_error again, as master did: invalid_scenery_exception is one of them, and so is what the file system
throws while the scenery is opened.

* fix(track): the beams worked out in the background don't read the ends a switch is set to

The ballast a switch trackbed takes is asked of the tracks joined to its main path, as the trackbed is made at the load,
not of the ends the switch is set to now, which change as it's thrown while the thread runs. What the thread reads with no
lock against the main thread is written down where it runs, with the two things driving can change: a switch thrown and a
turntable turned.

* perf(render): the cut faces of a track drawn with nothing put on the heap, a track with none passed over at once

* chore(track): the switch drawing the cut faces in the unrolled material read only in the builds with the self-tests

* docs(track): the blades flag said to be switched on, and what frogs in eu07.ini changes of it

* fix(editor): a response over 64 MB is refused, curl kept to http and https, the address read as UTF-8

The body of a map or of a service the user typed is read into memory up to 64 MB and fails above that, with WinHTTP and
with curl. curl follows at most 10 redirects and only to http or https. WinHTTP gets the address widened from UTF-8, so a
pasted one with letters past ASCII isn't turned into garbage.

* fix(editor): only the orthophoto an older editor kept is deleted from the settings folder, not all its cache

The geoportal folder of the old cache is removed, and the cache folder itself only where nothing else is left in it.

* fix(editor): the square metre of the outlined area written as its UTF-8 bytes, shown right where /utf-8 doesn't reach

* docs(track): why the velocity read for a switch is put back after its segments are made

* fix(track): the blade tips and the open ends of a track take the dark of the rail, not the colour of a worn tread

No wheel runs over them, so their cut faces aren't drawn as worn: the beige the matte material gives a worn face is left
to the cut faces of the frogs.

* fix(editor): a neighbour followed or given its grade is bent only where its second vector really changed

follow() and follow_grade() write the second vector of a track joined at its end back as it was when the joint didn't
turn or keep its grade; a track the simulator draws straight with that vector off the chord was made bent then, though
the edit never touched it. Its first vector is lengthened now only where the path was drawn bent or the second vector
took another direction or grade.

* fix(track): the open ends of a track laid with cuts take the dark of the rail too

The rails a frog or a crossing cuts are laid piece by piece; the caps of their pieces at the open ends of the track were
drawn worn with the cuts of the frogs. They aren't worn now, as the open ends of the other tracks.

* fix(editor): a neighbour drawn straight is bent only where the edit took its second vector off the chord

follow_grade() sets the height of the second vector anew on every graded joint, a fraction of a millimetre off the height
the scenery has, so a change of the vector bent a track whose second vector stood off the chord sideways already, though
the simulator draws it straight. follow() and follow_grade() keep a path drawn straight bent now only where its second
vector lay along the chord before the edit and stands off it after; a path drawn bent is kept bent as before. How far the
vector stands off the chord is measured in one place, keep_bent() uses it too.

* docs(editor): the comment of keep_bent above keep_bent again, not above the measure of the chord

* Editor: signal reads kept by the signal, level crossing discs, signals out of the gauge, indicators, the signalling tools split (#307)

* fix(editor): the read of a signal stays by it as its track is split, on a switch and when it is moved, and a level crossing disc is placed

The trains find a signal by its read, a passive event in a list of the track, and measure the way to the cell it reads along three
tracks further at most. The editor let the read drift away from the signal, or put it where trains of another path read it.

- A track split in two: a read the trains scan for (a passive event) goes to the piece by the point it reads, whatever its list;
  before, those of the runs towards the end stayed on the first piece. Five splits left the reads of A, B, C and J on the first
  piece while the signals stood on the sixth, out of the reach of the trains. The reads of signals placed since the scenery was
  opened, not bound to an event yet, go to the piece by their signal the same way.
- A signal moved off its track jumped back to that track's end once the track was changed: the track had taken it up as its
  object and measured it again along itself. A moved signal is let go by the track it stood by, and an object of a single point
  moved off the side of its track no longer follows it.
- A switch reads its events to the trains of both its paths. A signal past the frog of a path, where the axes are a gauge apart,
  is passed by the trains of that path only, so its read goes to the track joined to the far end of the path, in the list of
  the trains running from the switch onto it or the other way. Before the frog it stays on the switch. Moving the signal across
  the frog takes the read between the two.
- A signal by a switch on the side of its other path stood on that path, in the way of its trains (0.45 m from its axis); it
  is refused there, placed or moved, with the reason.
- The kind crossing of the descriptions (tsosp, tsospn) is known: the level crossing disc is listed in the palette and placed
  with its read.

Checked in the editor on a hidden desktop: five splits of a track with bound and fresh reads, a signal moved to another track
and the old one raised, signals placed before and past the frog of a switch on both paths, turned and moved across the frog,
saved and opened again with the reads bound, the tracks raised, shifted and bent with the signals following. Unit tests pass.

* fix(editor): a split keeps reads far off the path and bound objects up to 30 m by their pieces

A read the trains scan for moves to the piece by the point it reads only from event1 and event2, only
when it isn't ignored and only when that point is within 50 m of one of the pieces; a read at the
station building stays in its list. An object bound by hand keeps its binding as its path moves up to
30 m from it, an adopted one up to 12 m as before.

* feat(editor): the tracks that read a signal are drawn and can be picked, flipped or dropped

The selected signal shows each read of its _sem_info as a thick band over the reading track, arrows
in the way the read is scanned, a line from the foot of the signal and a plate with the list and the
track; the hovered one shows them thinner and placing or moving a signal shows where it will be read.
The panel lists the reads: each can be turned the other way or removed, "Pick the path" takes a
click on a track (with Shift one read more or less) and "By the path it stands by" puts back the
read the signal gets when it is placed. Each change is one step of the undo.

The self-test gets signalread, signalreadflip, signalreaddrop and signalreaddefault, and the probe
lists every read of a signal.

* fix(editor): a moved or turned signal keeps the reads picked by hand, and a read set again isn't an undo step

Moving a signal, taking it across the track or turning it put back only the read by the track it
stands by and dropped the others: those stay on their tracks now, on the other list when the signal
is turned. Setting the reads the signal has already says so and adds no step to the undo. Picking a
reading track disarms the template, so a click after Esc doesn't place a signal, and the signals
are listed again after any change of the history, also once it is full.

* feat(editor): a signal is placed out of the gauge of the tracks around it

As a signal is placed, moved, taken across or turned, its models are measured against the gauge
chosen in the Gauge window along every path near it. One that enters it is taken sideways, away
from the axis of the track whose gauge it enters, as deep as it went in and 3 cm more, in the same
step of the undo; between two tracks too close for it, it stays where it was put and the status
tells which gauge it enters and how deep. A gantry is only measured.

The panel of the selected signal tells when it stands in the gauge, with a button to move it out,
and the view marks the deepest place. The gauge tool measures any models against the paths around
them for it. The self-test places a signal at a given offset and its probe tells the gauge.

* fix(editor): a moved signal leaves only the read of the track it stood by, and the gauge is checked on its level

The reads a moved signal keeps were all but the first one listed, which is the first track of the
scenery having it, not the one the signal stood by: a read picked by hand could go and the track
it left could keep it. It is the read of the track it stood by that goes with it now, and turned,
eventall1 and eventall2 swap as well. A track more than 2 m above or below the foot doesn't push
the signal aside, the status tells the real distance it went, the gauge is measured again when the
signal follows a dragged track, and the marker in the view shows only what is up to date.

* feat(editor): indicators put on a signal from the panel, they move and go with it

The indicators of scenery/pkp/wskazniki described as "category: indicator"
are offered under the selected signal, grouped by their sign. One is put
in as an include named after the signal, on the srk layer:

- a lit one (place: head) under the lowest lamp of the signal, 0.73 m
  below it and 5 cm ahead of the mast, each next one 0.45 m lower; a dwarf
  signal is refused, it has no mast for one;
- a plate (place: foot) at the foot of the signal, its model has its place
  by the mast built in.

The panel lists the indicators standing by the signal with a Remove
button. They are found as the includes whose first parameter is the
signal's name, within 1 m of it in plan. When the signal is flipped,
turned or moved, they keep their place in its frame, in the same undo
step; deleting the signal deletes them too. If the signal and the
indicator both define an event, the status says so, since the scenery
keeps only one of them.

The self-test gets signalindicator and signalindicatordrop, and its
signalprobe gives each indicator's height and the height of the lowest
lamp.

* feat(tools): indicator descriptions and the indicators taken out of the signal templates

indicator_headers.py writes //$e descriptions into the LED indicators of
scenery/pkp/wskazniki (W19, W20, W21, W24, W26) with the events each
shows. It also makes the indicators that were so far only built into the
signal templates:

- W1 of the warning discs, new, old and with the light strip;
- W1 and W18 (new and old) of the block signals;
- the I, II and III plates of the repeaters.

The plates fixed flat to the mast are moved 4 cm ahead. Their back is
5 cm from the mast's axis, while the mast is 6.1 cm thick and its clamp
under them 8.3 cm, so the mast went through the plate, in the templates
too.

* fix(editor): a signal turned or moved by the gizmo is read and kept out of the gauge, its indicators go with it everywhere

The gizmo of an include only rewrote the numbers of a signal. Its read stayed on the event it had, even when the signal was turned
to the trains of the other direction. Its indicators stayed behind, its height could be dragged off the ground, and the gauge
wasn't checked. Now:

- the gizmo moves a signal in the plan only, at the height it has;
- while it is dragged, its indicators keep their place by it;
- once it is let go, the drag is made again as a move of the signal tool, one step of the history: the read goes to the event of
  the trains it faces on the path it stands by, and it is taken out of the gauge;
- dropped away from every track, it goes back where it stood.

The indicators of a signal are found in the scenery now, not in the list the signal tool keeps up only while it is open:

- deleted from another tab, the signal takes them with it;
- a signal renamed in the node properties gives its new name to them in the same step of the history;
- in the station and line batches, a renamed signal renames them, and a removed one removes them;
- the binding of objects to tracks puts them on the path reading their signal, not on the one nearest to them, and a signal moved
  by hand lets go of them too.

Also:

- a signal on a gantry takes no indicator from the list;
- an indicator a signal has already isn't put on it twice;
- the signals and their indicators are found in one pass over the includes, the indicators by the name of their signal;
- a step of the history with tracks is always one of their edit, so they are taken back with the includes.

The self-test gets signalgizmo, which turns and moves a signal as the gizmo does.

* fix(tools): indicator_headers.py keeps the files it replaces, writes beside the data on request

- An indicator it made, found changed, is put into a tar in editor_backup before it's written again.
- --sidecar writes the descriptions of the lit indicators into editor/templates, as the other header scripts do, the files of
  the game left alone.
- The indicators it makes are copies: the signal templates keep their own, and the docstring and the files say so.

* fix(editor): a signal moved by the gizmo by another track is read by it, the templates known without the signal tool

- At the end of a gizmo move the nearest track takes the signal when it is nearer than its own by more than 0.5 m; a track it
  was dropped onto, within 1.2 m of its axis, doesn't, so a signal only turned between two tracks or dropped onto one in a
  switch keeps its own.
- The signal templates and indicators are looked for when a signal is renamed or its gizmo shown, so its indicators follow
  without the signal tool opened first.
- A drop refused puts the gizmo and the node properties back where the signal stands.
- The indicators follow only once the gizmo made its step, and the ones not changed are left alone: grabbed and let go it
  changes nothing.

* fix(editor): a signal dropped by the gizmo onto a track keeps its own, the gizmo doesn't wait for the template scan

- When the nearest path to the drop is within 1.2 m of its axis, the signal keeps its own track, instead of only that path
  being left out: dropped onto a middle track it no longer goes to the track beyond it.
- The gizmo starts the signal template scan without waiting for it, so a rescan with a signal selected doesn't stop the
  editor; renaming a signal still waits.

* feat(editor): the signalling split into Signals, Station, Station signals and Line block

The one Signalling tool held the signals placed by hand, the whole wizard of a station and its checks; each is now a tool of its
own in the toolbar:
- Signals (I): the palette, a signal placed by a track, dragged along it, selected; a click on a switch no longer opens a station.
- Station: LMB on a switch lays the station out; its name, code, line, km, speed and braking distance, the numbers of the
  switches, the track circuits, the number of the selected track, the list of the switches.
- Station signals: the signals of the whole station placed at once, the template of each of their kinds (main, shunt) chosen in
  a table, and below what the signalling lacks: the repairs, the SHP resonators, the track lengths, the signal heads.
- Line block: the block of each line, the block boundaries drawn by hand, the line signals, the templates of the block signals
  and warning discs.
The station tools share the open station and its schematic; Station signals and Line block show what the Station tool set for
it, the line and the km in red where the line block needs them. The template table replaces the palette in the station
tools, so choosing a template there no longer arms placing. Two icons drawn for the new tools, in editor_icons.py too; the
png is drawn by the same shapes, Pillow wasn't at hand. The tracktab self-test takes a tool by its name, and srkopen shows the
Station tool.

* feat(editor): a signal shows its distance from the track axis and to the switch ahead, and goes to 50, 100 m... from it

- editor/editorTrackTrace: the track followed the way a train runs, shared by the W17 markers and the signals. The fouling
  point of a switch (where the axes of its tracks are 3.5 m apart) moved here…

* fix: a failed start says why and ends with an error code; a small crash dump by default (#425)

* fix: a failed start says why in the log and ends with an error code

When the window could not be made, the log only said "Bad init: failed to
create glfw window", without the reason glfw knows (no OpenGL driver, no
pixel format that fits, no EGL). Sometimes it did not even say that: the
log thread writes every 50 ms, and main() ended the process with
std::_Exit(0) without stopping it, so a start that failed within that time
left a log that ends at "// startup".

- glfw's errors go to the log and errors.txt through glfwSetErrorCallback,
  set before glfwInit(); each distinct message once.
- after a failed start (or an exception caught in main) the log thread is
  stopped and waited for, as the test runs already do, so nothing logged
  last is lost.
- a failed start ends with exit code 1 instead of 0, so the starter can
  tell it from a closing. A start that worked ends as before.

* fix: glfw's errors told during the start only, a closed crash dialog is no failed start

After review of the previous commit:

- the glfw error callback is taken off at the end of init(). Once the
  simulator runs, glfw would only repeat what the platform can't do
  (glfwSetCursorPos on Wayland, on every mouse-look) or what a scenery
  asks for in vain (launcher key codes below GLFW_KEY_SPACE, checked on
  every update), which would put new lines in errors.txt of existing
  sceneries and cost a format and a lock per call.
- closing the crash-report window ends the simulator as before, but
  init() now returns 1 for it (above 0: closed by the player), and only a
  result below 0 gives exit code 1.
- a start that throws from init() (bad_alloc, or a runtime_error on
  Windows) ends with exit code 1 too; an exception after a start that
  worked ends with 0 as before.

* feat: a small crash dump by default, the full one with "crashdump.full yes"

The dump written by the crash handler (Windows, WITHDUMPGEN) held all the
memory of the simulator (MiniDumpWithFullMemory), so it was as large as
the memory the simulator took: gigabytes on a large scenery, written to
the game folder at the moment of the crash, on machines with 8 GB of RAM
too.

By default it now holds the stacks with the memory they point to, the
threads, the modules, the handles, the memory map and the modules' data
segments (MiniDumpWithDataSegs in place of MiniDumpWithFullMemory, the
rest of the flags as before). The globals, Global among them, stay
readable. "crashdump.full yes" in eu07.ini writes the full dump as before.

Measured on td.scn with a ghostview camera (1.2 GB committed): the small
dump 44 MB in 2 s, the full one 1.6 GB in 19 s.

The key is new and optional: eu07.ini files without it read as before,
and the setting is listed in the log at start.

* fix: the small crash dump keeps the process and thread blocks

MiniDumpWithProcessThreadData adds the PEB and the TEBs, a few KB: the
command line (the scenery and the vehicle), the environment and the last
error of each thread, which the small dump otherwise lacks, since the heap
behind Global's longer strings is not in it.

* fix: an exception that ends the simulator says what it was in the log

After review of #425:

- the exceptions main() catches are logged ("Critical error: ..."): on
  Windows the runtime_error that only showed a message box, and on the
  other systems any std::exception, which used to end the simulator
  through std::terminate with no word in the log. On Windows the others
  are still left to CrashHandler, whose dump shows where they were
  thrown.
- every exception caught there ends with exit code 1, in the start or
  later, as a crash; it used to be 0 when caught, the abort code when not.
- closing the crash-report window logs "Crash report window closed,
  closing" instead of the error "Failed to run crash gui!", which stays
  for a result below 0.

* feat: the stack of a crash on Linux in the log and on the standard error

On Linux no dump is written (the crash handler is Windows only, crashpad
is off), so a crash left a log that just stopped.

A handler of SIGSEGV, SIGBUS, SIGFPE, SIGILL and SIGABRT (installed at
the start of main) now writes, with write() only:

  Crash: signal 11 (SIGSEGV) at address 0x...
  Version: EU07 (cmake), <hash> (...)
  Stack (addr2line -f -C -e <the eu07 file of this version> <offset>):
  <backtrace_symbols_fd>

to the end of the log file (its whole path, set when the log opens it)
and to the standard error, which StarterNG shows first. The Linux builds
carry their symbols (GENERATE_PDB adds -g, nothing strips them), so
addr2line on the eu07 file of the same version gives the files and lines.

Then the signal ends the process as before (SA_RESETHAND, raise): the
same exit status, and a core dump where the system makes one. The
handler runs on an alternate stack, so an overflow of the main thread's
stack is reported too; backtrace() is warmed up at install so it doesn't
allocate in the handler, and alarm(5) ends a handler that hangs (a crash
in the allocator or the loader). WAMR is built without its hardware bound
checks, so it installs no SIGSEGV handler of its own.

Elsewhere crash_signals does nothing. A unit test (Linux only) crashes a
child process and checks the report on its standard error and in the log,
and that the child still ended by SIGSEGV.

* fix: the crash report can't be cut by a second crash signal or end with SIGALRM

After review:
- the five crash signals are blocked while a report is written, so a
  second one waits rather than starting a second report in its middle
  (a fault that comes while blocked still ends the process).
- a report that hangs past alarm(5) ends the process by the signal of
  the crash, not by SIGALRM: a SIGALRM handler, set only in the crash
  handler, raises the crash signal and unblocks it.
- the child of the unit test calls alarm(10) first, so a child stuck
  before the crash can't hang the build.

---------

Co-authored-by: maj00r <maj00r@host>

* feat(track): unevenness waves shaped by the track spectra of FRA and ORE B176 (#426)

* feat(track): unevenness waves shaped by the track spectra of FRA and ORE B176

The waves were 5-42 m sideways and 8-83 m vertically, with the long ones
cut down, so a short wavelength carried a large share and the ride and
the rails looked busy. They are now 7-90 m sideways and 8-110 m
vertically, about the bands D1-D3 of EN 13848, each wave standing for a
band of the spectrum S ~ Wc^2 / (W^2 (W^2 + Wc^2)), Wc = 0.8245 rad/m,
the shape the FRA and the ORE B176 (German) spectra share: the longer
a wave, the higher it is.

The "uneven" value keeps its meaning, the peak deviation of a rail on a
10 m chord as PLK Id-14 measures it, so the values in sceneries stay as
they are. Since the chord sees the long waves less, the amplitudes are
calibrated again, and apart for the vertical and the sideways waves:
stray_per_mm becomes stray_up_per_mm 0.00229 and stray_side_per_mm
0.0031, and cross_share 0.35. A track at "uneven 18" measures 18 mm
vertically, 17 sideways, 15 of twist, +-10 of gauge, 21 of cross level
and J 5.7 against the Id-14 limits for 80 km/h. A rail now goes up to
about 5 cm off its design over the long waves, so the bound of the test
goes from 40 to 60 mm; the short waves the cab shakes on are smaller,
the sideways bend under the axles about 30% less.

Every scenery with unevenness looks and rides differently after this.
No release has unevenness yet. The grading tool follows the vertical
waves; on Baltyk the pantograph keeps within 0.6 cm of the wire up to
the product 1150 of the vertical value and the speed (925 before).

* test(track): bound the sideways offset of an uneven track off its design

The longer waves move the track about 4.5 cm sideways off its design at
"uneven 18" (2 cm before), and nothing checked or stated it. The Id-14
test now bounds the peak sideways offset of the middle of the track
below 5 cm, next to the vertical one, so a later change of the shares
can't double it unnoticed. The doc gives the figure, and names platform
edges and level crossing panels among what a rail moves away from.

---------

Co-authored-by: maj00r <maj00r@host>

* feat(editor): ~ shows or hides the orthophoto, Ctrl+~ downloads what the camera sees first; the speed of the camera in the View menu (#312)

* feat(editor): ~ shows or hides the orthophoto, Ctrl+~ downloads what the camera sees first; the speed of the camera in the View menu

~ (the key left of 1) turns the orthophoto on and off. Ctrl+~ finds what of the ground the camera sees (rays through a grid
over the screen down to the level of the ground under the camera, or as far as 2.5 km where they don't come down to it) and
puts those tiles at the front of the queue of the downloads, the ones which failed tried again at once; the status line
tells how many. Tab is not used, the laying and the switches have it.

View > Camera speed: the speed of the free camera of the editor (x0.05 to x10, 1 as it was), how many times faster Ctrl makes
it (1 to 30, 30 as it was), and in the top view as fast as the view is zoomed, so the map crosses the screen at the same pace
zoomed in or out. Kept in the settings of the editor; the camera of the cab and of the free fly outside the editor unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* fix(editor): Ctrl+~ in the top view, over a scenery with no terrain under the camera and with the orthophoto off; "As it was" without the speed going with the zoom

In the top view the box is the view itself, its corners on the ground. The ground under the camera is the terrain, else the
scenery, else the rays go as far as the reach. The tile of the camera is taken anew by hurry(), it isn't followed while the
layer is off. The top view is as fast zoomed in as out by default, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* fix(editor): the corners of the top view on the ground put where the camera is (the view matrix is camera-relative)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* i18n(editor): Polish for the orthophoto keys and the camera speed, on top of the master's own translations

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* fix(editor): Ctrl+~ asks only for the tiles update() would ask for; the tooltip of "Track colours" back in its place

After the reviews of #312:
- hurry() skips the tiles outside the download area set and marks the ones outside the area the service covers
  as empty, as update() does, so Ctrl+~ no longer queues downloads update() drops at once (and the workers took
  anyway, into the disk cache, against the area set), nor requests the service can't answer.
- A blank tile hurried takes the standard image first, as in update(); first_level() and outside_area() are
  shared by both loops, and the body of the loop of hurry() is hurry_tile().
- orthophoto_hurry() takes the level of the terrain only when it's below the camera, as the branch of the
  scenery already did: a camera under the terrain (a tunnel, under the heightmap) no longer flips the box of
  the view behind it.
- The "Camera speed" menu goes after the tooltip of "Track colours", which it had taken.
- #include <set> once in editorOrthophoto.hpp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QRgQ1K1Qu7TCsa74zsNURd

---------

Co-authored-by: Claude <noreply@anthropic.com>

* fix: whole-number bounds of Random() give a real number again (#416)

* fix: whole-number bounds of Random() give a real number again

ee3af0bf (#99, in 26.05.2) renamed RandomInt to an overload,
Random(int, int). The calls with whole-number bounds that expect a real
number in between took the overload and draw whole numbers since:

- wheel flats given with P0 happen to 1 vehicle in 101 (before: none),
  the random part of a flat is a whole number of millimetres;
- the chance of truck hunting is h/101 instead of h/100;
- the wind starts with a whole azimuth, speed and rates of change, and
  its direction changes in whole degrees per second;
- the tachometer below 5 km/h jumps to whole numbers.

The whole-number draw is RandomInt again, keeping the shared engine and
the swap of inverted bounds; the loading screen's trivia use it. Those
calls take the real Random() again, as up to 26.05.1.

* RandomInt: the comment says what it draws and why it has a name of its own

The comment put the bug on the overloading. It was in the calls that pass
whole-number bounds and expect a real number; the separate name keeps
them on Random(double, double).

* fix: RandomInt draws from the local engine, as up to 26.05.1 from one of its own

From the review of #416: ee3af0bf moved RandomInt from an engine of its own
(std::mt19937 seeded by std::random_device) to Global.random_engine, the
engine of the run's seed, and only the name was taken back here. Its one
caller draws the trivia of the loading screen, in init_modes, before a
network client gets the seed of the session in server_hello: the server's
engine was then one draw ahead of the client's, and with a fixed
randomseed the trivia was the same on every start. It is drawn from
Global.local_random_engine now, the engine of what one machine shows,
seeded from a true random source in init_modes before the loader is made.

The test of the same seed checks the shared engine with Random() and the
local one with RandomInt(); a new test checks that RandomInt() doesn't move
the engine of the run's seed.

---------

Co-authored-by: maj00r <maj00r@host>

* fix(editor): the orthophoto only on the chunks of the heightmap terrain in memory, and laid onto the ground again once it comes (#417)

* fix(editor): the orthophoto only on the chunks of the heightmap terrain in memory, and laid onto the ground again once it comes

Right after the editor started, the tiles of the orthophoto came from the disk cache at once and were fitted to the ground before
the chunks of the heightmap terrain had come (on IRT the orthophoto was on at 24.5 s, the first chunk came at 24.5 s, the last one
at 85 s). A tile with no ground under it, and none beside, stayed flat at the height of the layer (0 m, some 230 m under IRT);
one partly over the ground had its holes filled from the points beside. Neither was fitted again when the ground came: only the
guessed tiles were, by their middle and corners alone, and a tile with no chunk under it at all never got any. The overlay,
drawn over everything, then slid with the camera (WASD, turning) instead of lying on the ground. Turning the orthophoto off and on
hid it: by then the terrain was in and the tiles took their heights from it or from the edges of the tiles beside.

- With the heightmap terrain (the terrain streamer active), a tile of the orthophoto, of the WMS layers and of Ctrl+~ is asked
  for and shown only where its chunks are in memory (terrain_streamer::ground_under): it waits while one is still to come, and
  where no chunk is nor will be there's no imagery at all. A scenery without the heightmap terrain is as before.
- A tile remembers the points of its grid the ground didn't answer and is fitted again once the ground comes under any of them
  (every second point and the edges), not only the guessed tiles.

No asset changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(editor): the orthophoto waits only for the chunks that will come at this distance, and lies on the other ground or the guess elsewhere

After the reviews of maj00r and jerrrrycho on #417:

- A tile is no longer dropped where no chunk of the heightmap terrain is nor will be. There it lies on whatever ground there is (terrain models) or on the NMT guess, as on master; a scenery with a few chunks, or with chunks removed, keeps its imagery around them.

- A chunk counts as still to come only while it's being loaded or lies within the circle the streamer walks around the camera. The corners of the imagery in the 3D view, beyond that circle, no longer wait for chunks that won't load there: they lie on the guess and are sampled again when the camera comes near and the chunk loads.

- terrain_streamer::ground_under() (true / false / empty) became ground_coming() (bool), the cover source of the orthophoto the coming source, and on_ground() waits_for_ground(), which only holds back a new tile.

- In the scene (in_scene), the points a steep triangle leaves without a height but the heightmap terrain answers aren't kept as unanswered, so the tile isn't sampled again on every chunk that comes. The holes are counted from the heights again, as on master.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* fix(editor): the closing doesn't wait for a geoportal service that doesn't answer (#432)

* fix(editor): the closing doesn't wait for a geoportal service that doesn't answer

When geoportal.gov.pl took the connection and never answered, the
editor's closing stood still for tens of seconds and was killed: the
WinHTTP client was synchronous and looked at the stop only between the
reads of a body, so a request waiting for the answer held the closing
for the timeouts (up to 20 s for the answer, a few tries a square),
and the map of the place-picker downloaded with no stop at all, its
std::async future waited for to the end.

On Windows the client now uses WinHTTP asynchronously: each step is
waited for while the stop is looked at every 50 ms, and a stop closes
the request at once. The timeouts, the user agent, the size limit, the
errors and the body of a response with an error status stay as they
were. The map of the place-picker is given up when it goes. libcurl
looked at the stop during the whole transfer already.

The unit tests run the client against a local server that never
answers (given up within 0.4 s of the stop), one with a body of
100 kB and one with an error status.

* fix(editor): the capabilities of a WMS service being added don't hold the closing either

The reading of the layers of a WMS service the user adds ran on a
std::async future of the editor with no stop: closing the editor while
such a service didn't answer waited for its three tries. The reading
is given up when the editor goes, as the other downloads now are, and
editor_http_get, left with no callers, goes.

The unit test's silent server no longer waits for the client to close
its connection, and the requests to the local server are skipped when
the environment names a proxy, which couldn't reach it.

* fix(editor): the stop of the WMS reading outlives the future waiting for it

The flag was declared after the future of the reading, so it was
destroyed first while the future's destructor still waited for the
task reading it.

---------

Co-authored-by: maj00r <maj00r@host>

* revert(environment): the turbidity of the sky as it was before #227 (#410)

#227 (842b7688) raised the turbidity of the sky dome for most cloud covers
(3 -> 4 for a clear sky, 5 -> 6 at 0.41-0.60, one more from 0.61 up to 0.90).
In the Preetham model a higher turbidity darkens the sky a lot once the sun is
below the horizon: with a cover of 0.47 the zenith is about a third darker
1.4 degrees below it and half as bright 3 degrees below, the stars come out
right after the sunset and the ambient light taken from the sky dome drops
with it. Players report the dusk as too dark since the update.

The values are put back as they were before #227, in the turbidity_of()
that #323 made of the old chain of conditionals. A clear sky gets the
turbidity 3 again, which #227 said breaks the model at night.

Co-authored-by: maj00r <maj00r@host>

* Revert "fix(physicslog): no road-vehicle logs, formatting on the writer, a slow disk and the closing kept short (#408)"

This reverts commit d6c3593520.

---------

Co-authored-by: maj00r <73280030+maj00r@users.noreply.github.com>
Co-authored-by: maj00r <maj00r@host>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Hirek193 <polomarket73@gmail.com>
Co-authored-by: pol102 <mindak.m@icloud.com>
2026-11-22 09:55:09 +01:00

8237 lines
361 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "world/TrackSleepers.h"
#include <filesystem>
#include <format>
#include <iterator>
#include <fstream>
#include <regex>
#include <optional>
#include "application/editormode.h"
#include "application/editormodeshared.h"
#include "application/editorgestures.h"
#include "scene/scenenodegroups.h"
#include "application/editoruilayer.h"
#ifndef _WIN32
#include <spawn.h>
#include <unistd.h>
extern char **environ;
#endif
#include "application/editorprojection.h"
#include "application/application.h"
#include "editor/editorKilometrage.hpp"
#include "editor/editorSettings.hpp"
#include "editor/editorModelSets.hpp"
#include "editor/editorTrackPresets.hpp"
#include "editor/editorIncludeInfo.hpp"
#include "editor/editorGroundMesh.hpp"
#include "editor/editorFormat.hpp"
#include "editor/editorAlignmentStore.hpp"
#include "editor/editorSwitchStore.hpp"
#include "editor/editorGeometry.hpp"
#include "editor/editorIcons.hpp"
#include "utilities/translation.h"
#include "utilities/Globals.h"
#include "utilities/glmHelpers.h"
#include "simulation/simulation.h"
#include "simulation/simulationtime.h"
#include "simulation/simulationenvironment.h"
#include "simulation/blackboxrecorder.h"
#include "utilities/Timer.h"
#include "Console.h"
#include "rendering/renderer.h"
#include "model/AnimModel.h"
#include "model/Model3d.h"
#include "world/Track.h"
#include "utilities/Float3d.h"
#include "scene/scene.h"
#include "scene/scenelayers.h"
#include "scene/sceneterrain.h"
#include "utilities/parser.h"
#include "utilities/utilities.h"
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h"
#include "imgui/ImGuizmo.h"
#include "utilities/Logs.h"
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <array>
#include <chrono>
#include <cmath>
#include <functional>
#include <limits>
#include <vector>
// Static member initialization
TCamera editor_mode::Camera;
bool editor_mode::m_focus_active = false;
bool editor_mode::m_change_history = false;
bool editor_mode::m_settings_open = false;
namespace
{
using vec3 = glm::vec3;
using dvec2 = glm::dvec2;
// the steps of a frame of the editor timed: a frame whose update takes longer than kSlow tells in the log which of them took
// the time, at most twice a second
class frame_steps
{
public:
void step(char const *Name)
{
auto const now{std::chrono::steady_clock::now()};
m_steps.emplace_back(Name, std::chrono::duration<double, std::milli>(now - m_last).count());
m_last = now;
}
~frame_steps()
{
constexpr double kSlow{50.0}; // ms
auto const total{std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - m_start).count()};
static auto told{std::chrono::steady_clock::time_point{}};
if (total < kSlow || m_start - told < std::chrono::milliseconds{500})
return;
told = m_start;
std::string text{"Editor: slow frame, update " + std::to_string(static_cast<int>(total)) + " ms:"};
for (auto const &[name, time] : m_steps)
if (time >= 3.0)
text += " " + std::string{name} + " " + std::to_string(static_cast<int>(time));
WriteLog(text, logtype::generic);
}
private:
std::chrono::steady_clock::time_point const m_start{std::chrono::steady_clock::now()};
std::chrono::steady_clock::time_point m_last{m_start};
std::vector<std::pair<char const *, double>> m_steps;
};
inline bool is_release(int state)
{
return state == GLFW_RELEASE;
}
inline bool is_press(int state)
{
return state == GLFW_PRESS;
}
// true when the most recent left button press landed in the viewport rather than on an ImGui window.
// valid once ImGui has processed the press (i.e. from mode update onwards), so it's also used to
// discard deferred pick callbacks of a press that leaked past the application's UI filter
inline bool viewport_click()
{
return false == ImGui::GetIO().MouseDownOwned[0];
}
// tests whether the vertical line through (Px,Pz) passes over triangle abc; if so returns the
// surface height at that point through OutY. used by the "snap to ground" (END) feature.
inline bool triangle_height_at(glm::dvec3 const &a, glm::dvec3 const &b, glm::dvec3 const &c,
double const Px, double const Pz, double &OutY)
{
double const ux = b.x - a.x, uz = b.z - a.z;
double const vx = c.x - a.x, vz = c.z - a.z;
double const wx = Px - a.x, wz = Pz - a.z;
double const den = ux * vz - vx * uz;
if (std::abs(den) < 1e-9)
return false; // degenerate or vertical triangle, no defined height
double const s = (wx * vz - vx * wz) / den;
double const t = (ux * wz - wx * uz) / den;
if (s < 0.0 || t < 0.0 || s + t > 1.0)
return false;
OutY = a.y + s * (b.y - a.y) + t * (c.y - a.y);
return true;
}
using world_triangle = std::array<glm::dvec3, 3>;
// walks a model's submodel tree (mirroring the renderer's transform chain) and appends every
// mesh triangle, in world space, to Out. siblings are iterated to avoid deep recursion.
void gather_submodel_triangles(TSubModel *Submodel, glm::dmat4 const &M, std::vector<world_triangle> &Out)
{
for (TSubModel *sub = Submodel; sub != nullptr; sub = sub->Next)
{
glm::dmat4 mlocal = M;
if (sub->iFlags & 0xC000 && sub->GetMatrix() != nullptr)
mlocal = M * glm::dmat4(glm::make_mat4(sub->GetMatrix()->readArray()));
if (sub->eType < TP_ROTATOR) // a drawable mesh, not a rotator/light/etc.
{
auto const handle = sub->m_geometry.handle;
if (handle.bank != 0 || handle.chunk != 0)
{
auto const &verts = GfxRenderer->Vertices(handle);
auto const &indices = GfxRenderer->Indices(handle);
auto const to_world = [&](gfx::basic_vertex const &v) {
return glm::dvec3(mlocal * glm::dvec4(glm::dvec3(v.position), 1.0));
};
if (false == indices.empty())
{
for (std::size_t i = 0; i + 2 < indices.size(); i += 3)
Out.push_back({to_world(verts[indices[i]]), to_world(verts[indices[i + 1]]), to_world(verts[indices[i + 2]])});
}
else
{
for (std::size_t i = 0; i + 2 < verts.size(); i += 3)
Out.push_back({to_world(verts[i]), to_world(verts[i + 1]), to_world(verts[i + 2])});
}
}
}
if (sub->Child != nullptr)
gather_submodel_triangles(sub->Child, mlocal, Out); // children inherit this matrix
}
}
// world transform of a model instance, matching the renderer: translate * rotateY * rotateX * rotateZ * scale
glm::dmat4 instance_matrix(TAnimModel const &Model)
{
glm::dmat4 rootm(1.0);
rootm = glm::translate(rootm, Model.location());
glm::vec3 const angles = Model.Angles();
if (angles.y != 0.0f) rootm = glm::rotate(rootm, glm::radians(static_cast<double>(angles.y)), glm::dvec3(0.0, 1.0, 0.0));
if (angles.x != 0.0f) rootm = glm::rotate(rootm, glm::radians(static_cast<double>(angles.x)), glm::dvec3(1.0, 0.0, 0.0));
if (angles.z != 0.0f) rootm = glm::rotate(rootm, glm::radians(static_cast<double>(angles.z)), glm::dvec3(0.0, 0.0, 1.0));
return glm::scale(rootm, glm::dvec3(Model.Scale()));
}
// even-odd test of a point against a polygon outline projected onto the XZ plane
bool point_in_polygon_xz(std::vector<glm::dvec3> const &Polygon, double const X, double const Z)
{
bool inside = false;
for (std::size_t i = 0, j = Polygon.size() - 1; i < Polygon.size(); j = i++)
{
auto const &a = Polygon[i];
auto const &b = Polygon[j];
if ((a.z > Z) != (b.z > Z) && X < (b.x - a.x) * (Z - a.z) / (b.z - a.z) + a.x)
inside = !inside;
}
return inside;
}
// area of a polygon outline projected onto the XZ plane (shoelace formula)
double polygon_area_xz(std::vector<glm::dvec3> const &Polygon)
{
if (Polygon.size() < 3)
return 0.0;
double sum = 0.0;
for (std::size_t i = 0, j = Polygon.size() - 1; i < Polygon.size(); j = i++)
sum += Polygon[j].x * Polygon[i].z - Polygon[i].x * Polygon[j].z;
return std::abs(sum) * 0.5;
}
// world triangles bucketed on a uniform XZ grid, for many ground height queries over one area
class triangle_grid
{
public:
triangle_grid(std::vector<world_triangle> Triangles, glm::dvec2 const &Min, glm::dvec2 const &Max) : m_triangles(std::move(Triangles)), m_min(Min)
{
glm::dvec2 const extent = glm::max(Max - Min, glm::dvec2(1.0));
if (false == std::isfinite(extent.x) || false == std::isfinite(extent.y))
{
m_triangles.clear();
m_cells.resize(1);
return;
}
// ~8 m cells, coarser for big areas so large terrain triangles don't flood the grid
m_cellsize = std::max(8.0, std::max(extent.x, extent.y) / 128.0);
m_columns = static_cast<int>(std::ceil(extent.x / m_cellsize)) + 1;
m_rows = static_cast<int>(std::ceil(extent.y / m_cellsize)) + 1;
m_cells.resize(static_cast<std::size_t>(m_columns) * m_rows);
for (std::uint32_t idx = 0; idx < m_triangles.size(); ++idx)
{
auto const &t = m_triangles[idx];
int const c0 = column(std::min({t[0].x, t[1].x, t[2].x}));
int const c1 = column(std::max({t[0].x, t[1].x, t[2].x}));
int const r0 = row(std::min({t[0].z, t[1].z, t[2].z}));
int const r1 = row(std::max({t[0].z, t[1].z, t[2].z}));
for (int r = r0; r <= r1; ++r)
for (int c = c0; c <= c1; ++c)
m_cells[static_cast<std::size_t>(r) * m_columns + c].push_back(idx);
}
}
// highest surface at (X,Z) that isn't above Ceiling. returns: true if there is one, and which triangle it is in Index if asked
bool height_at(double const X, double const Z, double const Ceiling, double &OutY, std::uint32_t *Index = nullptr) const
{
bool found = false;
for (auto const idx : m_cells[static_cast<std::size_t>(row(Z)) * m_columns + column(X)])
{
auto const &t = m_triangles[idx];
double y;
if (triangle_height_at(t[0], t[1], t[2], X, Z, y) && y <= Ceiling && (!found || y > OutY))
{
OutY = y;
found = true;
if (Index != nullptr)
*Index = idx;
}
}
return found;
}
private:
int column(double const X) const
{
double const cell = std::floor((X - m_min.x) / m_cellsize);
return cell > 0.0 ? static_cast<int>(std::min(cell, static_cast<double>(m_columns - 1))) : 0;
}
int row(double const Z) const
{
double const cell = std::floor((Z - m_min.y) / m_cellsize);
return cell > 0.0 ? static_cast<int>(std::min(cell, static_cast<double>(m_rows - 1))) : 0;
}
std::vector<world_triangle> m_triangles;
std::vector<std::vector<std::uint32_t>> m_cells;
glm::dvec2 m_min;
double m_cellsize{8.0};
int m_columns{1};
int m_rows{1};
};
// meshes of the shapes and the models asked about as ground. the ground is asked about a tile at a time, by the orthophoto
// and by the road tools, and without these every question meant going through every triangle of every terrain model in reach
struct ground_record
{
ground_mesh mesh;
// what the mesh was made of, to tell when it has to be made again
std::size_t vertices{0};
std::size_t indices{0};
glm::dvec3 origin{0.0};
TModel3d const *model{nullptr};
glm::dmat4 matrix{0.0};
std::uint64_t used{0}; // number of the last question it served
};
std::unordered_map<std::uint64_t, ground_record> ground_shapes; // by the geometry of the shape
std::unordered_map<TAnimModel const *, ground_record> ground_models;
std::uint64_t ground_question{0};
std::size_t ground_held{0}; // triangles in the meshes
std::size_t constexpr ground_limit = 8000000; // triangles kept; some 400 MB with the grids
std::size_t constexpr ground_small = 256; // triangles; anything smaller is quicker to go through than to look up
// lets go of the meshes left alone the longest, once there's too much of them
void trim_ground_meshes()
{
if (ground_held <= ground_limit)
return;
std::vector<std::uint64_t> ages;
for (auto const &record : ground_shapes)
ages.emplace_back(record.second.used);
for (auto const &record : ground_models)
ages.emplace_back(record.second.used);
std::sort(ages.begin(), ages.end());
// the older half goes, except for what the current question needed
auto const threshold = std::min(ages[ages.size() / 2], ground_question - 1);
for (auto record = ground_shapes.begin(); record != ground_shapes.end();)
{
if (record->second.used <= threshold)
{
ground_held -= record->second.mesh.size();
record = ground_shapes.erase(record);
}
else
++record;
}
for (auto record = ground_models.begin(); record != ground_models.end();)
{
if (record->second.used <= threshold)
{
ground_held -= record->second.mesh.size();
record = ground_models.erase(record);
}
else
++record;
}
}
// forgets the meshes of the shapes, whose geometry can be changed in place (terrain being sculpted)
void forget_ground_shapes()
{
for (auto const &record : ground_shapes)
ground_held -= record.second.mesh.size();
ground_shapes.clear();
}
// appends triangles of a shape node which overlap specified XZ rectangle
void gather_shape_triangles(scene::shape_node const &Shape, glm::dvec2 const &Min, glm::dvec2 const &Max, std::vector<world_triangle> &Out, bool const Skiproads = false)
{
auto const &data = Shape.data();
if (editor_orthophoto::owns(data.geometry))
return; // the orthophoto laid over the ground isn't ground itself
if (Skiproads && owned_shapes::owns(data.geometry))
return; // neither are the roads, for the ones which ask where to put a road
if (data.area.radius >= 0.0f)
{
double const r = data.area.radius;
if (data.area.center.x + r < Min.x || data.area.center.x - r > Max.x || data.area.center.z + r < Min.y || data.area.center.z - r > Max.y)
return;
}
auto const add = [&](glm::dvec3 const &a, glm::dvec3 const &b, glm::dvec3 const &c) {
if (std::max({a.x, b.x, c.x}) < Min.x || std::min({a.x, b.x, c.x}) > Max.x || std::max({a.z, b.z, c.z}) < Min.y || std::min({a.z, b.z, c.z}) > Max.y)
return;
Out.push_back({a, b, c});
};
if (false == data.vertices.empty())
{
for (std::size_t i = 0; i + 2 < data.vertices.size(); i += 3)
add(data.vertices[i].position, data.vertices[i + 1].position, data.vertices[i + 2].position);
return;
}
// once the shape is uploaded its world-space source is released; the renderer keeps an origin-relative copy
if (data.geometry.bank == 0 && data.geometry.chunk == 0)
return;
auto const &verts = GfxRenderer->Vertices(data.geometry);
auto const &indices = GfxRenderer->Indices(data.geometry);
auto const count = (indices.empty() ? verts.size() : indices.size()) / 3;
if (count >= ground_small)
{
// a large shape is sorted once, and asked for what's in the rectangle from then on
auto &record = ground_shapes[(static_cast<std::uint64_t>(data.geometry.bank) << 32) | data.geometry.chunk];
if (record.mesh.size() == 0 || record.vertices != verts.size() || record.indices != indices.size() || record.origin != data.origin)
{
std::vector<ground_mesh::triangle> triangles;
triangles.reserve(count);
if (false == indices.empty())
{
for (std::size_t i = 0; i + 2 < indices.size(); i += 3)
triangles.push_back({verts[indices[i]].position, verts[indices[i + 1]].position, verts[indices[i + 2]].position});
}
else
{
for (std::size_t i = 0; i + 2 < verts.size(); i += 3)
triangles.push_back({verts[i].position, verts[i + 1].position, verts[i + 2].position});
}
ground_held -= record.mesh.size();
record.mesh.assign(std::move(triangles), data.origin);
ground_held += record.mesh.size();
record.vertices = verts.size();
record.indices = indices.size();
record.origin = data.origin;
}
record.used = ground_question;
record.mesh.collect(Min, Max, Out);
return;
}
auto const to_world = [&](gfx::basic_vertex const &v) { return data.origin + glm::dvec3(v.position); };
if (false == indices.empty())
{
for (std::size_t i = 0; i + 2 < indices.size(); i += 3)
add(to_world(verts[indices[i]]), to_world(verts[indices[i + 1]]), to_world(verts[indices[i + 2]]));
}
else
{
for (std::size_t i = 0; i + 2 < verts.size(); i += 3)
add(to_world(verts[i]), to_world(verts[i + 1]), to_world(verts[i + 2]));
}
}
// ground triangles overlapping an XZ rectangle (Min/Max are x,z). shapes hold the (legacy) terrain,
// model instances at least ModelRadius large (if ModelsAsGround) the terrain tiles
// Heightmap: the heightmap terrain is given as triangles too (for the ones which can ask the terrain for heights, it's a waste)
void gather_ground_triangles(glm::dvec2 const &Min, glm::dvec2 const &Max, bool const ModelsAsGround, float const ModelRadius, std::vector<world_triangle> &Out, bool const Skiproads = false,
bool const Heightmap = true)
{
auto const started = std::chrono::steady_clock::now();
++ground_question;
glm::dvec2 const center = (Min + Max) * 0.5;
float const radius = static_cast<float>(glm::length(Max - Min) * 0.5);
auto const sections = simulation::Region->sections(glm::dvec3(center.x, 0.0, center.y), radius); // copy, the result is a scratchpad
for (auto *section : sections)
{
for (auto const &shape : section->m_shapes)
gather_shape_triangles(shape, Min, Max, Out, Skiproads);
for (auto &cell : section->m_cells)
{
double const r = cell.m_area.radius;
if (cell.m_area.center.x + r < Min.x || cell.m_area.center.x - r > Max.x || cell.m_area.center.z + r < Min.y || cell.m_area.center.z - r > Max.y)
continue;
for (auto const &shape : cell.m_shapesopaque)
gather_shape_triangles(shape, Min, Max, Out);
if (false == ModelsAsGround)
continue;
for (auto *instance : cell.m_instancesopaque)
{
if (instance == nullptr || instance->Model() == nullptr || instance->radius() < ModelRadius)
continue;
// going through the triangles of a model which doesn't reach the rectangle is all cost and no gain
double const reach = instance->radius();
if (instance->location().x + reach < Min.x || instance->location().x - reach > Max.x || instance->location().z + reach < Min.y || instance->location().z - reach > Max.y)
continue;
// the model is gone through once, and asked for what's in the rectangle from then on, for as long as it stays put
auto const matrix = instance_matrix(*instance);
auto &record = ground_models[instance];
if (record.model != instance->Model() || record.matrix != matrix)
{
std::vector<world_triangle> modeltriangles;
gather_submodel_triangles(instance->Model()->Root, matrix, modeltriangles);
glm::dvec3 const origin = instance->location();
std::vector<ground_mesh::triangle> triangles;
triangles.reserve(modeltriangles.size());
for (auto const &t : modeltriangles)
triangles.push_back({glm::vec3(t[0] - origin), glm::vec3(t[1] - origin), glm::vec3(t[2] - origin)});
ground_held -= record.mesh.size();
record.mesh.assign(std::move(triangles), origin);
ground_held += record.mesh.size();
// a model which isn't loaded yet has nothing to show for itself, and is looked at again the next time
record.model = (record.mesh.size() > 0 ? instance->Model() : nullptr);
record.matrix = matrix;
}
record.used = ground_question;
record.mesh.collect(Min, Max, Out);
}
}
}
// the heightmap terrain answers for itself, with its full grid whatever is drawn of it
if (Heightmap)
EditorTerrain.gather_triangles(Min, Max, Out);
trim_ground_meshes();
// going through the scenery is the one thing here which can take long enough to be felt; when it does it's worth knowing
auto const elapsed = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - started).count();
if (elapsed > 250.0)
WriteLog("editor: gathering the ground took " + std::to_string(static_cast<int>(elapsed)) + " ms, for " + std::to_string(Out.size()) + " triangles; " + std::to_string(ground_held) + " are kept for the next time");
}
// ground kept for the road tools, which ask about the same places frame after frame. it's gathered a tile at a time:
// gathering is what takes time, so a place asked about for the first time costs a single tile a frame instead of a stall
struct ground_tile
{
std::unique_ptr<triangle_grid> grid;
double gathered{0.0}; // when, on the clock of the user interface
int used{0}; // frame it was last asked for
};
double constexpr ground_tile_size = 256.0;
double constexpr ground_tile_lifetime = 30.0; // older tiles are gathered again when asked for, to catch up with the scenery
std::size_t constexpr ground_tile_limit = 96;
std::map<std::pair<int, int>, ground_tile> ground_tiles;
int ground_tile_frame{-1}; // frame the last tile was gathered in without being told to
// scenery file name without path and extension
std::string scenery_stem()
{
std::string scenery = Global.SceneryFile;
auto const slash = scenery.find_last_of("/\\");
if (slash != std::string::npos)
scenery = scenery.substr(slash + 1);
auto const dot = scenery.find_last_of('.');
if (dot != std::string::npos)
scenery = scenery.substr(0, dot);
return scenery.empty() ? "default" : scenery;
}
}
bool editor_mode::editormode_input::init()
{
return mouse.init() && keyboard.init();
}
void editor_mode::editormode_input::poll()
{
keyboard.poll();
}
editor_mode::editor_mode() {
alignment_store::reset();
switch_store::reset();
kilometrage::reset();
m_userinterface = std::make_shared<editor_ui>();
// the area fill settings live in the node bank window, in the tab of the fill mode
ui()->set_fill_options([this]() { render_area_fill(); });
ui()->set_gizmo_options([this]() { render_gizmo_options(); });
ui()->set_toolbar_options([this]() { render_toolbar(); });
ui()->set_workspace_bar([this]() { render_workspaces(); });
ui()->set_track_options([this]() { render_track_tool_options(); }, [this]() { render_track_selection(); });
ui()->set_workspace_options(editor_ui::workspace::roads, [this]() { render_road_tool_options(); }, [this]() { render_road_selection(); });
ui()->set_workspace_options(editor_ui::workspace::terrain, [this]() { render_terrain_tool_options(); });
ui()->set_workspace_bank(editor_ui::workspace::terrain, [this]() { render_texture_browser(); }, STR("Textures"));
// a node picked in the scene window is selected as if clicked in the view; a double click flies the camera to it
ui()->set_scene_select([this](scene::basic_node *Node, bool const Focus) {
if (Focus)
start_focus(Node);
std::string reason;
if (Node->from_template() || false == scene::Layers.editable(Node, &reason))
{
ui()->set_status(format(STR_C("\"%s\" can't be edited: %s"), name_of(*Node).c_str(), reason.c_str()), true);
return;
}
m_track_window_open = false;
ui()->set_mode(nodebank_panel::MODIFY);
m_node = Node;
ui()->set_node(m_node);
});
ui()->set_array_options([this]() { render_array(); });
ui()->set_bend_options([this]() { render_bend(); });
ui()->set_file_actions([this]() { save(); }, [this]() { export_scenery(); });
ui()->set_new_scenery([this]() { m_newscenery_asked = true; });
ui()->set_open_scenery([this]() { m_openscenery_asked = true; });
ui()->set_edit_menu([this]() { render_edit_menu(); });
ui()->set_tools_menu([this]() { render_track_presets_menu(); });
ui()->set_menu_options([this]() {
render_object_menu();
render_view_menu();
render_map_menu();
render_help_menu();
});
m_roadtool.settings.normalize();
road_select(nullptr);
ground_sources(m_orthophoto);
ui()->set_base_layers([this]() { render_base_layers(); });
editor_track::observe(this);
}
void editor_mode::ground_sources(editor_orthophoto &Layer)
{
// the imagery is fitted onto the same ground as the area fill uses, terrain tile models included
// the roads built in the editor aren't ground: the imagery goes under them
Layer.ground_source([](glm::dvec2 const &Min, glm::dvec2 const &Max, bool const Heightmap, std::vector<world_triangle> &Out) {
gather_ground_triangles(Min, Max, true, 50.0f, Out, true, Heightmap);
});
Layer.height_source([](double const X, double const Z, double &Height) { return EditorTerrain.height_at(X, Z, Height); });
// with the heightmap terrain, a new tile waits while a chunk under it is still to come; where none is nor will be, or the walk of
// the chunks doesn't reach, it lies on the other ground or on the NMT guess as before. A scenery without it as before
Layer.coming_source([](glm::dvec2 const &Min, glm::dvec2 const &Max) { return EditorTerrain.active() && EditorTerrain.ground_coming(Min, Max); });
// the imagery often comes before the terrain made from the model: it lies where the terrain will be
Layer.guess_source([this](double const X, double const Z, double &Height) {
if (nmt_ground(X, Z, Height))
return true;
// asked for, the tile goes onto it once it comes
if (m_nmt.placed())
m_nmt.request({X - 1.0, Z - 1.0}, {X + 1.0, Z + 1.0});
return false;
});
}
editor_mode::~editor_mode()
{
editor_track::observe(nullptr);
// the capabilities of a WMS service being read are given up rather than waited for to their timeouts
m_wms_adding.closing = true;
}
std::vector<double> editor_mode::ground_heights(std::vector<glm::dvec3> const &Points, bool const Fresh, std::vector<char> *Found) const
{
std::vector<double> heights;
heights.reserve(Points.size());
if (Found != nullptr)
Found->assign(Points.size(), 0);
for (auto const &point : Points)
heights.push_back(point.y);
if (Points.empty() || simulation::Region == nullptr)
return heights;
double const now = ImGui::GetTime();
int const frame = ImGui::GetFrameCount();
// surfaces much higher than the point are something it's under, rather than the ground
double constexpr height_tolerance = 50.0;
auto const terrains = active_terrains();
for (std::size_t i = 0; i < Points.size(); ++i)
{
auto const &p = Points[i];
if (false == std::isfinite(p.x) || false == std::isfinite(p.y) || false == std::isfinite(p.z))
continue;
std::pair<int, int> const key{static_cast<int>(std::floor(p.x / ground_tile_size)), static_cast<int>(std::floor(p.z / ground_tile_size))};
auto &tile = ground_tiles[key];
bool const missing = (tile.grid == nullptr);
bool const stale = missing || now < tile.gathered || now - tile.gathered > ground_tile_lifetime;
// told to wait the caller gets every tile it needs, otherwise what's there, with one tile gathered each frame
if (stale && (Fresh ? missing : ground_tile_frame != frame))
{
glm::dvec2 const tilemin{key.first * ground_tile_size, key.second * ground_tile_size};
glm::dvec2 const tilemax{tilemin + glm::dvec2{ground_tile_size}};
std::vector<world_triangle> triangles;
// the same ground as the area fill uses, terrain tile models included
gather_ground_triangles(tilemin, tilemax, true, 50.0f, triangles, true);
tile.grid = std::make_unique<triangle_grid>(std::move(triangles), tilemin, tilemax);
tile.gathered = now;
if (false == Fresh)
ground_tile_frame = frame;
}
tile.used = frame;
double const ceiling = p.y + height_tolerance;
double y = 0.0;
bool found = (tile.grid != nullptr) && tile.grid->height_at(p.x, p.z, ceiling, y);
for (editor_terrain *terrain : terrains)
{
if (!terrain->contains(p.x, p.z))
continue;
double const h = terrain->height_at(p.x, p.z);
if (h <= ceiling && (!found || h > y))
{
y = h;
found = true;
}
}
if (found)
heights[i] = y;
if (Found != nullptr)
(*Found)[i] = found ? 1 : 0;
}
// the tiles left alone the longest go once there are too many
while (ground_tiles.size() > ground_tile_limit)
{
auto oldest = ground_tiles.begin();
for (auto tile = ground_tiles.begin(); tile != ground_tiles.end(); ++tile)
if (tile->second.used < oldest->second.used)
oldest = tile;
if (oldest->second.used == frame)
break;
ground_tiles.erase(oldest);
}
return heights;
}
editor_ui *editor_mode::ui() const
{
return static_cast<editor_ui *>(m_userinterface.get());
}
bool editor_mode::init()
{
EditorSettings.load();
Global.EditorOrthoTracks = EditorSettings.top_tracks();
// the terrain model as the user left it; the settings are read only here, after the mode was made
auto const &model{EditorSettings.terrain_model()};
m_nmt_state.grow = EditorSettings.terrain_while_laying();
m_nmt_state.band = std::clamp(model.band, 50.0, 3000.0);
// the formation and the slopes are always made along the tracks: the switch of them is gone, a setting left off by it isn't kept
m_nmt_state.follow = model.follow;
m_terrain_add_count = std::clamp(model.around, 1, 8);
EditorModelSets.load();
EditorTrackPresets.load();
Camera.Init({0, 15, 0}, {glm::radians(-30.0), glm::radians(180.0), 0}, nullptr);
return m_input.init();
}
void editor_mode::apply_rotation_for_new_node(scene::basic_node *node, int rotation_mode, float fixed_rotation_value)
{
if (!node)
return;
if (rotation_mode == functions_panel::RANDOM)
{
const vec3 rotation{0.0f, LocalRandom(0.0, 360.0), 0.0f};
m_editor.rotate(node, rotation, 1);
}
else if (rotation_mode == functions_panel::FIXED)
{
const vec3 rotation{0.0f, fixed_rotation_value, 0.0f};
m_editor.rotate(node, rotation, 0);
}
}
void editor_mode::start_focus(scene::basic_node *node, double duration)
{
if (!node)
return;
glm::dvec3 const center = node->location();
// distance that frames the object's bounding sphere within the vertical FOV, with some margin
double const radius = std::max(1.0, static_cast<double>(node->radius()));
double const fovy = glm::radians(static_cast<double>(Global.FieldOfView) / std::max(0.01, static_cast<double>(Global.ZoomFactor)));
double distance = radius / std::tan(fovy * 0.5) * 1.6;
distance = std::clamp(distance, radius * 1.5, static_cast<double>(kMaxPlacementDistance));
// keep the camera on the side it currently views from, so the move turns toward the object
// rather than flying around it; fall back to a pleasant 3/4 direction when sitting on top of it
glm::dvec3 dir = Camera.Pos - center;
double const len = glm::length(dir);
dir = len > 1e-3 ? dir / len : glm::normalize(glm::dvec3(1.0, 0.5, 1.0));
// looking from there straight at the object
fly_to(center, -dir, distance, duration);
}
void editor_mode::fly_to(glm::dvec3 const &Target, glm::dvec3 const &Look, double const Distance, double const Duration)
{
// the camera flown off somewhere leaves the track it rode along
m_ride.leave();
m_focus_start_pos = Camera.Pos;
m_focus_start_angle = Camera.Angle;
m_focus_target_pos = Target - Look * Distance;
m_focus_target_angle = glm::vec3(static_cast<float>(std::asin(std::clamp(Look.y, -1.0, 1.0))), static_cast<float>(std::atan2(-Look.x, -Look.z)), 0.0f);
m_focus_active = true;
m_focus_time = 0.0;
m_focus_duration = Duration;
}
void editor_mode::snap_to_ground(scene::basic_node *node, bool const Alone, bool const Joined)
{
if (!node || !simulation::Region)
return;
glm::dvec3 const origin = node->location();
if (!simulation::Region->point_inside(origin))
return;
// the same ground the models are put on when they're inserted: the terrain shapes, the terrain tile models and the terrain
// patches of the editor, under the node, or over it when it went under the ground. the shapes alone and the spheres of the
// models around, which were asked about before, missed the ground of the sceneries made of terrain tile models
glm::dvec3 const target = placement_on_ground(origin);
if (std::abs(target.y - origin.y) < 1e-4)
return;
push_snapshot(node, EditorSnapshot::Action::Move);
if (false == m_history.empty())
m_history.back().joined = Joined;
if (auto *model = dynamic_cast<TAnimModel *>(node); Alone && model != nullptr && node->group() > 1)
{
// the model of a group goes down by itself, the rest of the group stays
simulation::Region->erase(model);
model->location(target);
simulation::Region->insert(model);
model->mark_dirty();
return;
}
m_editor.translate(node, target, true); // true == apply the computed Y (free vertical move)
}
void editor_mode::handle_brush_mouse_hold(int Action, int Button)
{
auto const mode = ui()->mode();
auto const rotation_mode = ui()->rot_mode();
auto const fixed_rotation_value = ui()->rot_val();
if(mode != nodebank_panel::BRUSH)
return;
GfxRenderer->Pick_Node_Callback(
[this, mode, rotation_mode, fixed_rotation_value, Action, Button](scene::basic_node * /*node*/) {
// picks are resolved a few frames later; drop the ones queued before the button went up,
// or for a press that turned out to belong to the UI
if (!mouseHold || !viewport_click())
return;
// spacing is measured in world space: the mouse offset is camera-relative and the camera may move mid-stroke
glm::dvec3 const newPos = Camera.Pos + clamp_mouse_offset_to_max(GfxRenderer->Mouse_Position());
if (m_brush_has_last && glm::distance(newPos, oldPos) < ui()->getSpacing())
return;
// picked only when a placement is due, so a random pick from a set isn't drawn every tick
const std::string *src = ui()->get_active_node_template();
if (!src || src->starts_with(editor_includes::directive_mark)) // scenery templates are placed one at a time
return;
// NOTE: no name for what the brush puts in, as with thousands of nodes the names take their share of the time
// the scenery needs to load. a node which needs one, to be referred to by an event, gets it in the node properties
TAnimModel *cloned = simulation::State.create_model(*src, std::string{}, placement_on_ground(newPos));
oldPos = newPos;
m_brush_has_last = true;
if (!cloned)
return;
apply_rotation_for_new_node(cloned, rotation_mode, fixed_rotation_value);
std::string as_text;
cloned->export_as_text(as_text);
push_snapshot(cloned, EditorSnapshot::Action::Add, as_text);
m_node = cloned;
ui()->set_node(m_node);
});
}
void editor_mode::add_to_hierarchy(scene::basic_node *node)
{
if (!node) return;
scene::Hierarchy[node->uuid.to_string()] = node;
}
void editor_mode::remove_from_hierarchy(scene::basic_node *node)
{
if (!node) return;
auto it = scene::Hierarchy.find(node->uuid.to_string());
if (it != scene::Hierarchy.end())
scene::Hierarchy.erase(it);
}
scene::basic_node* editor_mode::find_in_hierarchy(const std::string &uuid_str)
{
if (uuid_str.empty()) return nullptr;
auto it = scene::Hierarchy.find(uuid_str);
return it != scene::Hierarchy.end() ? it->second : nullptr;
}
scene::basic_node* editor_mode::find_node_by_any(scene::basic_node *node_ptr, const std::string &uuid_str, const std::string &name)
{
if (node_ptr) return node_ptr;
if (!uuid_str.empty()) {
auto *node = find_in_hierarchy(uuid_str);
if (node) return node;
}
if (!name.empty()) {
return simulation::Instances.find(name);
}
return nullptr;
}
void editor_mode::push_snapshot(scene::basic_node *node, EditorSnapshot::Action Action, std::string const &Serialized)
{
if (!node)
return;
if(m_max_history_size >= 0 && (int)m_history.size() >= m_max_history_size)
{
m_history.erase(m_history.begin(), m_history.begin() + ((int)m_history.size() - m_max_history_size + 1));
}
EditorSnapshot snap;
snap.action = Action;
snap.node_name = node->name();
snap.position = node->location();
snap.uuid = node->uuid;
snap.layer = node->layer();
if (auto *model = dynamic_cast<TAnimModel *>(node))
{
snap.rotation = model->Angles();
snap.scale = model->Scale();
}
else
{
snap.rotation = glm::vec3(0.0f);
snap.scale = glm::vec3(1.0f);
}
if (Action == EditorSnapshot::Action::Delete || Action == EditorSnapshot::Action::Add)
{
if (!Serialized.empty())
snap.serialized = Serialized;
else
node->export_as_text(snap.serialized);
}
snap.node_ptr = node;
m_history.push_back(std::move(snap));
g_redo.clear();
}
glm::dvec3 editor_mode::clamp_mouse_offset_to_max(const glm::dvec3 &offset)
{
double len = glm::length(offset);
if (len <= static_cast<double>(kMaxPlacementDistance) || len <= 1e-6)
return offset;
return glm::normalize(offset) * static_cast<double>(kMaxPlacementDistance);
}
void editor_mode::nullify_history_pointers(scene::basic_node *node)
{
if (!node)
return;
if (ui()->node() == node)
ui()->set_node(nullptr);
infra_forget(node);
for (auto *tool : m_tools)
tool->forget(node);
auto const followed = [node](infra::object_state const &State) { return State.saved.node == node; };
for (auto &s : m_history)
{
if (s.node_ptr == node)
s.node_ptr = nullptr;
for (auto &copy : s.copies)
{
if (copy.model == node)
copy.model = nullptr;
}
s.infra.erase(std::remove_if(s.infra.begin(), s.infra.end(), followed), s.infra.end());
}
for (auto &s : g_redo)
{
if (s.node_ptr == node)
s.node_ptr = nullptr;
s.infra.erase(std::remove_if(s.infra.begin(), s.infra.end(), followed), s.infra.end());
}
// deleted nodes also drop out of the "undo last fill" set
m_fill_last.erase(std::remove(m_fill_last.begin(), m_fill_last.end(), node), m_fill_last.end());
// and out of the selection and their group
m_selection.erase(std::remove(m_selection.begin(), m_selection.end(), node), m_selection.end());
scene::Groups.remove(node);
// an array no longer follows its settings once the model it was made of is gone
if (m_array.base == node)
m_array.base = nullptr;
}
void editor_mode::terrain_history_step()
{
if (false == m_streamer.active())
return;
// the terrain loaded from the model is taken back as a whole
if (nmt_busy() && false == m_history_busy)
return;
if (m_streamer.recording())
{
if (terrain_brush() && mouseHold && false == m_history_busy)
return; // the stroke goes on
auto change{m_streamer.end_change()};
if (false == change.empty())
{
trim_history();
EditorSnapshot snap;
snap.action = EditorSnapshot::Action::Terrain;
snap.node_name = STR("terrain");
if (false == change.chunks.empty())
{
auto const centre{heightmap::chunk_corner(change.chunks.begin()->first) + glm::dvec2{heightmap::chunk_size * 0.5}};
snap.position = {centre.x, 0.0, centre.y};
}
snap.terrain = std::make_shared<terrain_streamer::change const>(std::move(change));
m_history.push_back(std::move(snap));
g_redo.clear();
}
}
m_streamer.begin_change();
}
void editor_mode::restore_terrain(EditorSnapshot &Snapshot)
{
if (Snapshot.terrain == nullptr)
return;
auto opposite{m_streamer.restore(*Snapshot.terrain)};
Snapshot.terrain = std::make_shared<terrain_streamer::change const>(std::move(opposite));
forget_ground();
m_water_selected = -1;
m_water_material_of = -1;
}
void editor_mode::undo_last()
{
first_step_done(first_step::undo);
// the warning by the cursor told of the step taken back now: "Ctrl+Z takes it back" is done
ui()->drop_refusal();
// a change of the terrain made a moment ago is a step of its own before it's taken back
m_history_busy = true;
terrain_history_step();
m_history_busy = false;
// the changes made together are taken back together, and their models are the selection again
std::vector<scene::basic_node *> touched;
while (false == m_history.empty())
{
auto const joined{m_history.back().joined};
undo_one();
if (m_node != nullptr && std::find(touched.begin(), touched.end(), m_node) == touched.end())
touched.push_back(m_node);
if (false == joined)
break;
}
if (touched.size() > 1)
{
// the one with the gizmo was recorded first, so it's taken back last
m_node = touched.back();
m_selection.assign(touched.begin(), touched.end() - 1);
ui()->set_node(m_node);
}
}
std::string editor_mode::refused(std::string Text)
{
ui()->set_status(Text, true);
return Text;
}
void editor_mode::drop_step(std::size_t const Step)
{
if (Step == 0 || m_history.size() != Step)
return;
auto const redo{g_redo.size()};
undo_one();
if (g_redo.size() > redo)
g_redo.erase(g_redo.begin() + redo, g_redo.end());
}
void editor_mode::undo_one()
{
if (m_history.empty())
return;
m_nmt_state.restoring = true;
struct restored
{
bool &flag;
~restored() { flag = false; }
} const restoring{m_nmt_state.restoring};
EditorSnapshot snap = m_history.back();
m_history.pop_back();
if (snap.action == EditorSnapshot::Action::Terrain)
{
restore_terrain(snap);
g_redo.push_back(std::move(snap));
return;
}
if (snap.action == EditorSnapshot::Action::Kilometrage)
{
restore_kilometrage(snap);
g_redo.push_back(std::move(snap));
return;
}
if (snap.action == EditorSnapshot::Action::Bend)
{
restore_bend(snap, true);
g_redo.push_back(std::move(snap));
return;
}
if (false == snap.sweeps.empty() || false == snap.sweeps_toggled.empty())
{
restore_sweeps(snap);
g_redo.push_back(std::move(snap));
return;
}
if (false == snap.instances.empty() || false == snap.directives.empty())
{
restore_includes(snap);
if (snap.action != EditorSnapshot::Action::TrackEdit)
{
g_redo.push_back(std::move(snap));
return;
}
}
if (snap.instance != 0)
{
restore_include(snap);
g_redo.push_back(std::move(snap));
return;
}
if (snap.action == EditorSnapshot::Action::TrackEdit)
{
restore_track_snapshot(snap, g_redo, true);
return;
}
if (snap.action == EditorSnapshot::Action::RoadEdit)
{
restore_road_snapshot(snap, g_redo, true);
return;
}
if (snap.action == EditorSnapshot::Action::Array)
{
restore_array_snapshot(std::move(snap), g_redo, true);
return;
}
if (snap.action == EditorSnapshot::Action::Delete)
{
// undo delete -> recreate model
EditorSnapshot redoSnap;
redoSnap.action = EditorSnapshot::Action::Delete;
redoSnap.node_name = snap.node_name;
redoSnap.serialized = snap.serialized;
redoSnap.position = snap.position;
redoSnap.node_ptr = nullptr;
redoSnap.uuid = snap.uuid;
redoSnap.layer = snap.layer;
redoSnap.joined = snap.joined;
g_redo.push_back(std::move(redoSnap));
TAnimModel *created = simulation::State.create_model(snap.serialized, snap.node_name, snap.position);
if (created)
{
scene::Layers.move(created, snap.layer); // back to the layer it was deleted from, rather than the active one
created->location(snap.position);
created->Angles(snap.rotation);
m_node = created;
m_node->uuid = snap.uuid; // restore original UUID for better tracking (not strictly necessary)
add_to_hierarchy(created);
// a node without a name can be found for the redo only this way
g_redo.back().node_ptr = created;
ui()->set_node(m_node);
}
return;
}
scene::basic_node *target = find_node_by_any(snap.node_ptr, snap.uuid.to_string(), snap.node_name);
if (!target)
return;
EditorSnapshot current;
current.action = snap.action;
current.joined = snap.joined;
current.node_name = snap.node_name;
current.node_ptr = target;
current.layer = target->layer();
current.position = target->location();
if (auto *model = dynamic_cast<TAnimModel *>(target))
{
current.rotation = model->Angles();
current.scale = model->Scale();
}
else
current.rotation = glm::vec3(0.0f);
g_redo.push_back(std::move(current));
if (snap.action == EditorSnapshot::Action::Add)
{
// undo add -> delete the instance
if (auto *model = dynamic_cast<TAnimModel *>(target))
{
erase_model(model);
m_node = nullptr;
ui()->set_node(nullptr);
}
return;
}
target->location(snap.position);
if (auto *model = dynamic_cast<TAnimModel *>(target))
{
glm::vec3 cur = model->Angles();
glm::vec3 delta = snap.rotation - cur;
m_editor.rotate(target, delta, 0);
model->Scale(snap.scale);
}
m_node = target;
ui()->set_node(m_node);
}
void editor_mode::redo_last()
{
// a change of the terrain made since the undo goes to the history, and the redo steps are dropped with it
m_history_busy = true;
terrain_history_step();
m_history_busy = false;
// the first of the changes made together is on the top, the others follow it
std::vector<scene::basic_node *> touched;
do
{
redo_one();
if (m_node != nullptr && std::find(touched.begin(), touched.end(), m_node) == touched.end())
touched.push_back(m_node);
} while (false == g_redo.empty() && g_redo.back().joined);
if (touched.size() > 1)
{
m_node = touched.front();
m_selection.assign(touched.begin() + 1, touched.end());
ui()->set_node(m_node);
}
}
void editor_mode::redo_one()
{
if (g_redo.empty())
return;
m_nmt_state.restoring = true;
struct restored
{
bool &flag;
~restored() { flag = false; }
} const restoring{m_nmt_state.restoring};
EditorSnapshot snap = g_redo.back();
g_redo.pop_back();
if (snap.action == EditorSnapshot::Action::Terrain)
{
restore_terrain(snap);
m_history.push_back(std::move(snap));
return;
}
if (snap.action == EditorSnapshot::Action::Kilometrage)
{
restore_kilometrage(snap);
m_history.push_back(std::move(snap));
return;
}
if (snap.action == EditorSnapshot::Action::Bend)
{
restore_bend(snap, false);
m_history.push_back(std::move(snap));
return;
}
if (false == snap.sweeps.empty() || false == snap.sweeps_toggled.empty())
{
restore_sweeps(snap);
m_history.push_back(std::move(snap));
return;
}
if (false == snap.instances.empty() || false == snap.directives.empty())
{
restore_includes(snap);
if (snap.action != EditorSnapshot::Action::TrackEdit)
{
m_history.push_back(std::move(snap));
return;
}
}
if (snap.instance != 0)
{
restore_include(snap);
m_history.push_back(std::move(snap));
return;
}
if (snap.action == EditorSnapshot::Action::TrackEdit)
{
restore_track_snapshot(snap, m_history, false);
return;
}
if (snap.action == EditorSnapshot::Action::RoadEdit)
{
restore_road_snapshot(snap, m_history, false);
return;
}
if (snap.action == EditorSnapshot::Action::Array)
{
restore_array_snapshot(std::move(snap), m_history, false);
return;
}
// handle delete redo (re-delete) separately
if (snap.action == EditorSnapshot::Action::Delete)
{
EditorSnapshot hist;
hist.action = snap.action;
hist.node_name = snap.node_name;
hist.serialized = snap.serialized;
hist.position = snap.position;
hist.uuid = snap.uuid;
hist.layer = snap.layer;
hist.joined = snap.joined;
m_history.push_back(std::move(hist));
// NOTE: located the way other steps do it, as the name alone doesn't lead to a node which has none
scene::basic_node *target = find_node_by_any(snap.node_ptr, snap.uuid.to_string(), snap.node_name);
if (target)
{
if (auto *model = dynamic_cast<TAnimModel *>(target))
{
erase_model(model);
m_node = nullptr;
ui()->set_node(nullptr);
}
}
return;
}
scene::basic_node *target = find_node_by_any(snap.node_ptr, snap.uuid.to_string(), snap.node_name);
EditorSnapshot hist;
hist.action = snap.action;
hist.joined = snap.joined;
hist.node_name = snap.node_name;
hist.node_ptr = target;
if (target)
{
hist.position = target->location();
if (auto *model = dynamic_cast<TAnimModel *>(target))
{
hist.rotation = model->Angles();
hist.scale = model->Scale();
}
hist.uuid = snap.uuid;
}
m_history.push_back(std::move(hist));
if (snap.action == EditorSnapshot::Action::Add)
{
TAnimModel *created = simulation::State.create_model(snap.serialized, snap.node_name, snap.position);
if (created)
{
scene::Layers.move(created, snap.layer);
created->location(snap.position);
created->Angles(snap.rotation);
created->Scale(snap.scale);
m_node = created;
m_node->uuid = snap.uuid;
ui()->set_node(m_node);
if (!m_history.empty())
m_history.back().node_ptr = created;
}
return;
}
if (!target)
return;
// apply redo position
target->location(snap.position);
if (auto *model = dynamic_cast<TAnimModel *>(target))
{
glm::vec3 cur = model->Angles();
glm::vec3 delta = snap.rotation - cur;
m_editor.rotate(target, delta, 0);
model->Scale(snap.scale);
}
m_node = target;
ui()->set_node(m_node);
}
bool editor_mode::update()
{
frame_steps steps;
Global.editor_tracks = (current_work_area() == work_area::tracks);
selftest_step();
steps.step("selftest");
thumbnails_trim();
// the drives of the switches go to the tips of their blades once, as the templates of the scenery are known
if (false == m_drives_checked)
{
EditorIncludes.start();
if (EditorIncludes.scanned())
{
m_drives_checked = true;
if (auto const moved{drives_to_blade_tips()}; moved > 0)
{
auto const text{format(STR_P("The drive of %zu switch is moved to the tips of its blades", "The drives of %zu switches are moved to the tips of their blades", static_cast<long long>(moved)), moved)};
ui()->set_status(text, false);
WriteLog("Editor: " + text, logtype::generic);
}
// the drives left without their switches are told once, as the scenery is opened
if (auto const stray{stray_drives().size()}; stray > 0)
{
auto const text{format(STR_P("%zu switch drive has no switch of its name: it's listed in the switch tool and marked red in the view",
"%zu switch drives have no switch of their names: they're listed in the switch tool and marked red in the view", static_cast<long long>(stray)),
stray)};
ui()->set_notice(text);
WriteLog("Editor: " + text, logtype::generic);
}
}
}
// after the tool has told its own status
if (false == m_orphan_note.empty())
{
ui()->set_notice(m_orphan_note);
WriteLog("Editor: " + m_orphan_note);
m_orphan_note.clear();
}
if (m_vehicle.take_leave())
{
Application.pop_mode();
return true;
}
steps.step("start");
Timer::UpdateTimers(true);
move_thrown_switches();
simulation::State.update_clocks();
simulation::Environment.update();
// bindings are matched with the objects by their locations, before anything gets moved
if (false == m_bindings_loaded && simulation::is_ready)
infra_load();
// the templates are looked for in the background from the start, for the tools to have them at hand
if (simulation::is_ready)
{
EditorIncludes.start();
EditorIncludes.poll();
}
auto const deltarealtime = Timer::GetDeltaRenderTime();
// the whole frame before, drawing included: one slow where its update wasn't took the time drawing
if (deltarealtime > 0.15 && simulation::is_ready)
WriteLog("Editor: slow frame before, " + std::to_string(static_cast<int>(deltarealtime * 1000.0)) + " ms in all", logtype::generic);
// reconcile camera fly-mode with the real right-button state. ImGui is always fed the button
// events (even when it captures the mouse), so io.MouseDown[1] is authoritative; if a release
// was swallowed by an ImGui window while flying, force the editor out of fly-mode here so the
// camera doesn't get stuck spinning with a hidden cursor.
if (!ImGui::GetIO().MouseDown[1] && m_input.mouse.button(GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
{
m_input.mouse.button(GLFW_MOUSE_BUTTON_RIGHT, GLFW_RELEASE);
Application.set_cursor(GLFW_CURSOR_NORMAL);
}
if (m_ortho_pan && !ImGui::GetIO().MouseDown[2])
ortho_pan_stop();
// same for the left button (brush / sculpt / click placement). the UI filter in the application
// uses io.WantCaptureMouse from the previous frame, so a press on a panel the cursor has just
// moved onto can leak into the viewport, and its release is then swallowed by the UI; the brush
// would keep painting with the button up. by now ImGui has processed this frame's input, so
// MouseDown is the real button state and MouseDownOwned tells whether the press hit the UI.
// (pick callbacks queued by such a press are discarded separately, see viewport_click())
if (mouseHold && (!ImGui::GetIO().MouseDown[0] || !viewport_click()))
{
mouseHold = false;
m_dragging = false;
m_input.mouse.button(GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE);
}
terrain_history_step();
steps.step("simulation");
// fixed step render time routines (50 Hz)
fTime50Hz += deltarealtime; // accumulate even when paused to keep frame reads stable
while (fTime50Hz >= 1.0 / 50.0)
{
#ifdef _WIN32
Console::Update();
#endif
m_userinterface->update();
if (mouseHold)
{
// process continuous brush placement
if(ui()->mode() == nodebank_panel::BRUSH)
handle_brush_mouse_hold(GLFW_REPEAT, GLFW_MOUSE_BUTTON_LEFT);
}
// decelerate camera velocity with thresholding
Camera.Velocity *= 0.65f;
if (std::abs(Camera.Velocity.x) < 0.01)
Camera.Velocity.x = 0.0;
if (std::abs(Camera.Velocity.y) < 0.01)
Camera.Velocity.y = 0.0;
if (std::abs(Camera.Velocity.z) < 0.01)
Camera.Velocity.z = 0.0;
fTime50Hz -= 1.0 / 50.0;
}
steps.step("ui");
// variable step routines
update_camera(deltarealtime);
steps.step("camera");
// drop the selection if its layer was hidden or locked in the meantime
if (m_node != nullptr && false == scene::Layers.editable(m_node))
{
m_node = nullptr;
m_dragging = false;
ui()->set_node(nullptr);
}
if (m_instance != 0 && false == scene::Layers.removable(m_instance))
{
select_include(0);
}
if (m_include.changed)
{
// a parameter of the selected include was changed in the node properties
m_include.changed = false;
apply_include();
}
if (false == m_include.active && false == m_gizmo_using)
{
m_include_gesture = false;
}
// the gizmo let go of a signal while it wasn't drawn
if (false == m_gizmo_using)
signal_gizmo_end();
// models replaced after changes of includes are destroyed once the renderer can't have them among its pick candidates
for (auto &retired : m_retired)
{
if (--retired.second <= 0)
{
simulation::Instances.purge(retired.first);
}
}
m_retired.erase(std::remove_if(m_retired.begin(), m_retired.end(), [](std::pair<TAnimModel *, int> const &Retired) { return Retired.second <= 0; }), m_retired.end());
simulation::Region->update_sounds();
audio::renderer.update(Global.iPause ? 0.0 : deltarealtime);
GfxRenderer->Update(deltarealtime);
steps.step("renderer");
simulation::is_ready = true;
// note: the streamer is advanced centrally in the application main loop (so it runs in every
// mode), using the published Global.pCamera; nothing to do here
// the brush of the terrain works while the left mouse button is held
if (terrain_brush() && mouseHold)
handle_terrain_brush(deltarealtime);
else
m_terrain_stroke = false;
if (m_terrain_open)
draw_terrain_overlay();
// --- geoportal orthophoto: streamed around the camera, drawn beneath the other overlays ---
// chunks of the terrain come from the files and from the model: the imagery lying at guessed heights goes onto them
// and so do the squares of the model it lies on meanwhile
auto const resident{m_streamer.resident()};
auto const waiting{m_nmt.waiting()};
if (resident > m_ortho_resident || waiting < m_ortho_waiting)
{
m_orthophoto.ground_arrived();
for (auto &layer : m_wms)
layer->ground_arrived();
}
m_ortho_resident = resident;
m_ortho_waiting = waiting;
steps.step("terrain");
m_orthophoto.update(Camera.Pos);
steps.step("orthophoto");
update_wms_layers();
steps.step("wms");
nmt_update();
keep_nmt_settings();
steps.step("nmt");
// what's drawn over the 3d view stays in it, the docks around it are see-through a little
auto *background{ImGui::GetBackgroundDrawList(ImGui::GetMainViewport())};
background->PushClipRect(ImVec2(editor_ui::view_min().x, editor_ui::view_min().y), ImVec2(editor_ui::view_max().x, editor_ui::view_max().y), true);
draw_orthophoto();
draw_nmt_pending();
area_update();
draw_download_area();
steps.step("imagery");
// --- ImGuizmo: in-viewport transform gizmo for the selected node ---
render_gizmo();
// the tools, the gizmo modes and the axes over the 3d view
render_viewport_overlays();
m_ride.draw_hud();
render_track_inspector();
steps.step("inspector");
if (ui()->mode() == nodebank_panel::TRACK)
{
draw_track_lines();
draw_track_labels();
draw_track_legend();
}
else if (Global.EditorOrtho && Global.EditorOrthoTracks > 1)
draw_track_lines();
if (selected_track())
draw_track_overlay();
if (route_active())
draw_route_overlay();
if (ui()->mode() == nodebank_panel::TRACK && m_track_tab == track_tab::straights)
draw_straights_overlay();
steps.step("track lines");
if (ui()->mode() == nodebank_panel::TRACK)
{
if (m_track_box.active && false == ImGui::GetIO().MouseDown[0])
finish_track_box();
update_track_hover();
steps.step("hover");
point_drag_update();
switch_slide_update();
draw_switch_slide();
if (m_track_tab == track_tab::turntable)
draw_turntable_overlay();
if (m_track_tab == track_tab::signals)
{
signal_refresh();
signal_move_update();
draw_signal_overlay();
}
m_parallel.draw_overlay();
if (m_track_tab == track_tab::lineside)
{
m_hekto.draw_overlay();
m_fouling.draw_overlay();
m_vehicle.draw_overlay();
}
render_track_context();
steps.step("context");
update_build_tools();
steps.step("laying");
tape_update();
update_track_intent();
steps.step("intent");
draw_track_hover();
draw_track_set();
draw_track_spread();
draw_build_overlay();
steps.step("laying drawn");
draw_blade_tips();
draw_stray_drives();
draw_track_intent();
draw_tape();
steps.step("track drawn");
m_speed.update();
steps.step("speed");
m_joints.update();
steps.step("joints");
if (m_nmt_state.grade_lines_dirty)
{
m_nmt_state.grade_lines_dirty = false;
grade_lines_store();
}
m_nmt_state.grade_line_replaced.clear();
m_srk.update();
kilometrage::follow(m_track_edits);
m_km.update();
steps.step("srk km");
draw_track_hints();
draw_empty_scenery_hint();
draw_profile_overlay();
draw_grade_line_pick();
draw_infra_overlay();
m_speed.draw_overlay();
m_joints.draw_overlay();
m_srk.draw_overlay();
m_km.draw_overlay();
steps.step("overlays");
}
else
{
draw_sweep_overlay();
m_sweep.open = false;
bend_drag_update();
draw_bend_overlay();
}
m_gauge.update();
m_gauge.render_windows();
m_minimap.render_windows();
// roads: trajectories of the lanes, and the tools of the road window
update_road_tool();
draw_road_overlay();
// the roads and the terrain are fields of work, their tools are drawn in the tool options window in place of the edit mode ones
ui()->set_workspace(m_terrain_open ? editor_ui::workspace::terrain : m_roadtool.window ? editor_ui::workspace::roads : editor_ui::workspace::none);
steps.step("roads");
// --- array: the one made last is kept in line with its settings ---
update_array();
// --- area fill: outline overlay while the mode is active (its settings are drawn in the node bank window) ---
if (ui()->mode() == nodebank_panel::FILL)
draw_area_fill_outline();
background->PopClipRect();
draw_ortho_compass();
render_new_scenery_popup();
render_open_scenery_popup();
render_shortcuts_window();
render_first_steps();
render_nmt_prompt();
render_nmt_banner();
render_nmt_buried();
render_empty_scenery_hint();
render_outside_area_hint();
render_switch_designer();
render_environment_window();
draw_selection_overlay();
render_context_menu();
steps.step("windows");
// --- ImGui: Editor Settings & History windows ---
if(m_settings_open)
render_settings();
if(!m_change_history) return true;
render_change_history();
return true;
}
void editor_mode::render_settings()
{
ImGui::Begin((std::string(STR_C("Editor Settings")) + "###editorsettings").c_str(), &m_settings_open);
if (ImGui::BeginTabBar("##editorsettings"))
{
if (ImGui::BeginTabItem(STR_C("General")))
{
ImGui::TextUnformatted(STR_C("Camera movement"));
const char *schemes[] = {STR_C("WSAD (new)"), STR_C("Arrows (legacy)")};
int current = EditorSettings.movement() == editorSettings::movement_scheme::legacy ? 1 : 0;
if (ImGui::Combo("##movement_scheme", &current, schemes, IM_ARRAYSIZE(schemes)))
{
EditorSettings.movement(current == 1 ? editorSettings::movement_scheme::legacy
: editorSettings::movement_scheme::wsad);
m_input.keyboard.apply_scheme();
EditorSettings.save();
}
ImGui::Separator();
ImGui::Checkbox(STR_C("Transform gizmo (ImGuizmo)"), &m_gizmo_enabled);
ImGui::Separator();
ImGui::TextUnformatted(STR_C("Colours of the editor"));
const char *palettes[] = {STR_C("Green, as the rest of the simulator"), STR_C("Amber, of the editor")};
int palette = std::clamp(EditorSettings.palette(), 0, 1);
if (ImGui::Combo("##palette", &palette, palettes, IM_ARRAYSIZE(palettes)))
{
EditorSettings.palette(palette);
EditorSettings.save();
}
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
ImGui::End();
}
namespace
{
// point spacings offered for the chunks, as the user reads them
char const *spacing_label(float const Spacing)
{
return Spacing < 0.75f ? "0.5 m" : Spacing < 1.5f ? "1 m" : Spacing < 3.f ? "2 m" : "5 m";
}
bool spacing_combo(char const *Label, float &Spacing)
{
bool changed = false;
ImGui::SetNextItemWidth(120.0f);
if (ImGui::BeginCombo(Label, spacing_label(Spacing)))
{
for (auto const spacing : heightmap::spacings)
if (ImGui::Selectable(spacing_label(spacing), spacing == Spacing))
{
Spacing = spacing;
changed = true;
}
ImGui::EndCombo();
}
return changed;
}
} // namespace
void editor_mode::render_terrain_ui()
{
if (false == m_streamer.active())
{
ImGui::TextUnformatted(STR_C("The scenery has no terrain yet"));
// the terrain model makes the terrain itself
if (ImGui::CollapsingHeader(STR_C("Terrain model (NMT)"), ImGuiTreeNodeFlags_DefaultOpen))
render_nmt_ui();
ImGui::Spacing();
ImGui::TextDisabled("%s", STR_C("Or without the real ground:"));
if (ImGui::Button(STR_C("Make an empty, flat terrain")))
ensure_terrain();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Kept in the folder terrain/<name of the scenery>/ and written there with the scenery (Ctrl+S)"));
return;
}
// the real terrain first, open: the tracks put onto it from there
if (nmt_busy())
ImGui::SetNextItemOpen(true);
if (ImGui::CollapsingHeader(STR_C("Terrain model (NMT)"), ImGuiTreeNodeFlags_DefaultOpen))
render_nmt_ui();
// the tools are chosen in the toolbar, here are their settings
if (ImGui::CollapsingHeader(STR_C("Chunks"), m_terrain_tool == terrain_tool::chunks ? ImGuiTreeNodeFlags_DefaultOpen : 0))
{
if (spacing_combo(STR_C("Point spacing"), m_terrain_spacing))
m_streamer.default_spacing(m_terrain_spacing);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Spacing of the grid points of the chunks made.\n"
"A chunk is 250 x 250 m; finer grids take more memory and time to draw."));
ImGui::SetNextItemWidth(120.0f);
ImGui::InputFloat(STR_C("Base height (m)"), &m_terrain_baseheight, 0.0f, 0.0f, "%.2f");
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Height of the chunks made where there's no ground under them"));
ImGui::SetNextItemWidth(120.0f);
ImGui::InputInt(STR_C("Chunks along a side"), &m_terrain_add_count);
m_terrain_add_count = std::clamp(m_terrain_add_count, 1, 8);
ImGui::SameLine();
if (ImGui::Button(STR_C("Add around the camera")))
add_chunks_around(m_terrain_add_count);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Makes the chunks missing around the camera, shaped as the ground under them"));
render_ground_conversion();
if (dynamic_cast<TAnimModel *>(m_node) != nullptr)
{
if (ImGui::Button(STR_C("Capture the selected model as terrain")))
capture_terrain();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The chunks under the model take its shape, and the model is removed"));
}
}
if (ImGui::CollapsingHeader(STR_C("Chunk resolution"), m_terrain_tool == terrain_tool::spacing ? ImGuiTreeNodeFlags_DefaultOpen : 0))
{
// chunks removed since are no longer selected
for (auto it = m_terrain_selection.begin(); it != m_terrain_selection.end();)
it = m_streamer.exists(*it) ? std::next(it) : m_terrain_selection.erase(it);
if (m_terrain_selection.empty())
ImGui::TextWrapped("%s", STR_C("No chunk selected. With the Point spacing tool LMB selects the clicked chunk, Ctrl+LMB adds one or takes it away."));
else
{
bool coarser = false;
std::size_t points = 0, after = 0;
int const cells = heightmap::cells_for(m_terrain_convert_spacing);
ImGui::BeginChild("##chunkselection", ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * std::min<float>(6.0f, static_cast<float>(m_terrain_selection.size()) + 0.5f)), true);
for (auto const &key : m_terrain_selection)
{
auto const centre = heightmap::chunk_corner(key) + glm::dvec2{heightmap::chunk_size * 0.5};
auto const *chunk = m_streamer.terrain_at(centre.x, centre.y);
if (chunk == nullptr)
{
ImGui::TextDisabled(STR_C("%d,%d: not loaded"), key.first, key.second);
continue;
}
auto const &data = chunk->data();
coarser |= cells < data.cells;
points += static_cast<std::size_t>(data.side()) * data.side();
after += static_cast<std::size_t>(cells + 1) * (cells + 1);
auto const paint = data.paint > 0 ? format(STR_C(", paint every %.2f m"), heightmap::chunk_size / data.paint) : std::string(STR_C(", one material"));
auto const touchups = data.adjust.empty() ? std::string() : std::string(STR_C(", touch-ups"));
ImGui::Text("%d,%d: %s%s%s", key.first, key.second, spacing_label(static_cast<float>(data.spacing())), paint.c_str(), touchups.c_str());
}
ImGui::EndChild();
spacing_combo(STR_C("Convert to"), m_terrain_convert_spacing);
ImGui::SameLine();
if (ImGui::Button(STR_C("Convert")))
convert_selected_chunks();
ImGui::SameLine();
if (ImGui::Button(STR_C("Clear the selection")))
m_terrain_selection.clear();
if (ImGui::Button(STR_C("Remove the selected (Del)")))
remove_selected_chunks();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Takes the selected chunks out of the terrain of the scenery, and out of its files on save; Ctrl+Z brings them back"));
ImGui::Checkbox(STR_C("Paint every 0.5 m with 0.5 m spacing"), &m_terrain_convert_paint);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Chunks converted to 0.5 m get their paint samples every 0.5 m as well (every metre otherwise),\n"
"so the materials can be painted as finely as the ground is shaped"));
if (points > 0)
ImGui::TextDisabled(STR_C("Grid points: %zu -> %zu"), points, after);
if (coarser)
ImGui::TextWrapped("%s", STR_C("A coarser grid can't hold the shapes smaller than its spacing: they're averaged out. "
"The edges of the chunks keep their heights, so the neighbours still meet them. Ctrl+Z takes it back."));
}
}
if (terrain_brush() && ImGui::CollapsingHeader(STR_C("Brush"), ImGuiTreeNodeFlags_DefaultOpen))
{
ImGui::SetNextItemWidth(120.0f);
ImGui::DragFloat(STR_C("Radius (m)"), &m_terrain_brush_radius, 0.25f, 0.5f, 500.0f, "%.1f");
m_terrain_brush_radius = std::max(0.5f, m_terrain_brush_radius);
ImGui::SetNextItemWidth(120.0f);
ImGui::DragFloat(STR_C("Strength"), &m_terrain_brush_strength, 0.05f, 0.05f, 50.0f, "%.2f");
m_terrain_brush_strength = std::max(0.05f, m_terrain_brush_strength);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Sculpting: metres per second at the middle of the brush; smoothing, levelling, painting and restoring: how fast they work"));
if (m_terrain_tool == terrain_tool::sculpt || m_terrain_tool == terrain_tool::smooth || m_terrain_tool == terrain_tool::level)
{
auto touchup = m_streamer.touch_up();
if (ImGui::Checkbox(STR_C("Touch up over the tracks"), &touchup))
m_streamer.touch_up(touchup);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("On: the brush shapes the terrain as it's seen, also where the tracks (modifiers) shape it;\n"
"the changes there are kept as touch-ups over the modifiers, and stay when the track changes.\n"
"The Restore tool takes them back.\n"
"Off: the brush shapes only the ground under the modifiers, the tracks keep their formation and slopes."));
}
if (m_terrain_tool == terrain_tool::level)
{
ImGui::SetNextItemWidth(120.0f);
ImGui::DragFloat(STR_C("Target height (m)"), &m_terrain_target, 0.05f, -1000.0f, 5000.0f, "%.2f");
ImGui::SameLine();
if (ImGui::Button(m_terrain_picking ? STR_C("Click in the view...") : STR_C("Pick")))
m_terrain_picking = false == m_terrain_picking;
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The next click takes the height of whatever is under the cursor: terrain, a track, a model.\nCtrl+LMB does the same at any time"));
char const *modes[]{STR_C("Raise and lower"), STR_C("Only raise"), STR_C("Only lower")};
ImGui::SetNextItemWidth(160.0f);
ImGui::Combo("##levelmode", &m_terrain_level_mode, modes, IM_ARRAYSIZE(modes));
ImGui::Checkbox(STR_C("Target where the stroke starts"), &m_terrain_target_from_stroke);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Each stroke takes the height of the terrain under the cursor where it starts as its target,\nand drags it along: the ground stroked over is levelled with that point"));
}
}
if (ImGui::CollapsingHeader(STR_C("Materials"), m_terrain_tool == terrain_tool::paint ? ImGuiTreeNodeFlags_DefaultOpen : 0))
{
auto const &layers{m_streamer.manifest().layers};
m_terrain_layer = std::clamp(m_terrain_layer, 0, static_cast<int>(layers.size()) - 1);
for (std::size_t i = 0; i < layers.size(); ++i)
{
auto label{layers[i].size > 0.f ? format("%zu: %s (%.1f m)", i, layers[i].material.c_str(), layers[i].size) : format("%zu: %s", i, layers[i].material.c_str())};
if (layers[i].rotation != 0.f)
label += format(STR_C(", turned %.0f°"), layers[i].rotation);
if (ImGui::Selectable(label.c_str(), static_cast<int>(i) == m_terrain_layer))
m_terrain_layer = static_cast<int>(i);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("New chunks are covered with the first material. A texture dragged from the browser onto an entry of the palette takes its place"));
// a material dropped on an entry takes its place, on every chunk painted with it
std::string dropped;
if (material_drop(dropped))
m_streamer.layer_material(static_cast<std::uint16_t>(i), dropped);
}
if (false == layers.empty())
{
// the values are kept while dragged and given to the palette once the dragging ends;
// read back from the palette every frame, the release would hand it the old value
if (false == m_terrain_layer_size_dragged)
m_terrain_layer_size = layers[m_terrain_layer].size;
ImGui::SetNextItemWidth(120.0f);
ImGui::DragFloat(STR_C("Repeats every (m)"), &m_terrain_layer_size, 0.1f, 0.0f, 200.0f, "%.1f");
m_terrain_layer_size_dragged = ImGui::IsItemActive();
if (ImGui::IsItemDeactivatedAfterEdit())
m_streamer.layer_size(static_cast<std::uint16_t>(m_terrain_layer), m_terrain_layer_size);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Metres the textures of the chosen material repeat at; 0: as the material gives it, or 8 m"));
if (false == m_terrain_layer_rotation_dragged)
m_terrain_layer_rotation = layers[m_terrain_layer].rotation;
ImGui::SetNextItemWidth(120.0f);
ImGui::SliderFloat(STR_C("Turned by (deg)"), &m_terrain_layer_rotation, -180.0f, 180.0f, "%.0f");
m_terrain_layer_rotation_dragged = ImGui::IsItemActive();
if (ImGui::IsItemDeactivatedAfterEdit())
m_streamer.layer_rotation(static_cast<std::uint16_t>(m_terrain_layer), m_terrain_layer_rotation);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Angle the textures of the chosen material are turned by, on every chunk painted with it.\n"
"Ctrl+click to type it. To have the material turned in one place only, fill it there with an angle of its own."));
}
texture_field(STR_C("Material"), 160.0f, m_terrain_material, IM_ARRAYSIZE(m_terrain_material), terrain_texture_offer(nullptr));
ImGui::SameLine();
if (ImGui::Button(STR_C("Add to the palette")) && m_terrain_material[0] != '\0')
m_terrain_layer = m_streamer.layer(m_terrain_material, m_terrain_material_size);
ImGui::TextDisabled("%s", STR_C("A chunk holds up to 8 materials"));
}
if (ImGui::CollapsingHeader(STR_C("Fill"), m_terrain_tool == terrain_tool::fill ? ImGuiTreeNodeFlags_DefaultOpen : 0))
{
auto const &layers{m_streamer.manifest().layers};
if (false == layers.empty())
{
auto const &chosen{layers[std::clamp(m_terrain_layer, 0, static_cast<int>(layers.size()) - 1)]};
ImGui::Text(STR_C("Material: %s (chosen in the palette above)"), chosen.material.c_str());
}
ImGui::Text(STR_C("Outline: %zu points"), m_paint_outline.size());
ImGui::SetNextItemWidth(120.0f);
ImGui::DragFloat(STR_C("Edge blur (m)"), &m_paint_feather, 0.1f, 0.0f, 100.0f, m_paint_feather > 0.0f ? "%.1f" : STR_C("sharp"));
m_paint_feather = std::max(0.0f, m_paint_feather);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Metres across the outline the material blends in over, half of them inside the area.\n"
"0: a sharp edge, as sharp as the paint samples go: every metre, or every 0.5 m in chunks converted to 0.5 m with fine paint"));
ImGui::SetNextItemWidth(120.0f);
ImGui::SliderFloat(STR_C("Texture turned by (deg)"), &m_paint_rotation, -180.0f, 180.0f, "%.0f");
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The area gets the material turned by this angle: an entry of the palette with the material at that angle\n"
"is used, or added. Ctrl+click to type it"));
ImGui::BeginDisabled(m_paint_outline.size() < 3 || layers.empty());
if (ImGui::Button(STR_C("Fill the area")))
fill_terrain_area();
ImGui::EndDisabled();
ImGui::SameLine();
if (ImGui::Button(STR_C("Clear the outline##fill")))
m_paint_outline.clear();
}
if (ImGui::CollapsingHeader(STR_C("Water"), m_terrain_tool == terrain_tool::water ? ImGuiTreeNodeFlags_DefaultOpen : 0))
{
if (m_terrain_tool == terrain_tool::water)
{
ImGui::Text(STR_C("Outline: %zu points"), m_water_points.size());
ImGui::SetNextItemWidth(120.0f);
if (ImGui::DragFloat(STR_C("Level (m)"), &m_water_level, 0.01f, -1000.0f, 5000.0f, "%.2f"))
m_water_level_edited = true;
texture_field(STR_C("Material##water"), 160.0f, m_water_material, IM_ARRAYSIZE(m_water_material), terrain_texture_offer(STR_C("The default water material")));
if (ImGui::IsItemHovered())
ImGui::SetTooltip(STR_C("The default one: %s, the water material of the terrain"), m_streamer.manifest().water_material.c_str());
ImGui::BeginDisabled(m_water_points.size() < 3);
if (ImGui::Button(STR_C("Make the water")))
{
auto water{m_streamer.water()};
heightmap::water_body body;
body.name = format("water %zu", water.size() + 1);
body.level = m_water_level;
body.material = m_water_material;
for (auto const &point : m_water_points)
body.outline.emplace_back(point.x, point.z);
water.push_back(std::move(body));
m_streamer.water(std::move(water));
m_water_selected = static_cast<int>(m_streamer.water().size()) - 1;
m_water_points.clear();
m_water_level_edited = false;
}
ImGui::EndDisabled();
ImGui::SameLine();
if (ImGui::Button(STR_C("Clear the outline")))
{
m_water_points.clear();
m_water_level_edited = false;
}
}
auto const &water{m_streamer.water()};
std::optional<std::size_t> removed;
std::optional<std::pair<std::size_t, std::string>> dropped;
for (std::size_t i = 0; i < water.size(); ++i)
{
ImGui::PushID(static_cast<int>(i));
auto const material = water[i].material.empty() ? std::string(STR_C("default material")) : water[i].material;
if (ImGui::Selectable(format(STR_C("%s, %.2f m, %zu points, %s"), water[i].name.c_str(), water[i].level, water[i].outline.size(), material.c_str()).c_str(), static_cast<int>(i) == m_water_selected))
m_water_selected = static_cast<int>(i);
// a texture dropped on an entry becomes the material of that water
std::string name;
if (material_drop(name))
dropped = std::make_pair(i, name);
ImGui::PopID();
}
if (dropped)
{
auto changed{water};
changed[dropped->first].material = dropped->second;
m_streamer.water(std::move(changed));
m_water_selected = static_cast<int>(dropped->first);
m_water_material_of = -1;
}
if (m_water_selected >= 0 && m_water_selected < static_cast<int>(water.size()))
{
// the level is changed in the water once the dragging ends
if (false == m_water_level_dragged)
m_water_selected_level = static_cast<float>(water[m_water_selected].level);
ImGui::SetNextItemWidth(120.0f);
ImGui::DragFloat(STR_C("Level of the chosen (m)"), &m_water_selected_level, 0.01f, -1000.0f, 5000.0f, "%.2f");
m_water_level_dragged = ImGui::IsItemActive();
if (ImGui::IsItemDeactivatedAfterEdit())
{
auto changed{water};
changed[m_water_selected].level = m_water_selected_level;
m_streamer.water(std::move(changed));
}
// the material and the repeat of its texture, changed once the field is left (or a texture dropped on it)
if (m_water_material_of != m_water_selected)
{
std::snprintf(m_water_selected_material, sizeof(m_water_selected_material), "%s", water[m_water_selected].material.c_str());
m_water_selected_size = water[m_water_selected].size;
m_water_material_of = m_water_selected;
}
bool retexture = false;
retexture |= texture_field(STR_C("Material of the chosen"), 160.0f, m_water_selected_material, IM_ARRAYSIZE(m_water_selected_material), terrain_texture_offer(STR_C("The default water material")));
if (ImGui::IsItemHovered())
ImGui::SetTooltip(STR_C("Chosen from the textures, or dragged from the texture browser.\nThe default one: %s, the water material of the terrain"), m_streamer.manifest().water_material.c_str());
ImGui::SetNextItemWidth(120.0f);
ImGui::DragFloat(STR_C("Texture repeats every (m)##water"), &m_water_selected_size, 0.1f, 0.0f, 500.0f, "%.1f");
m_water_selected_size = std::max(0.0f, m_water_selected_size);
retexture |= ImGui::IsItemDeactivatedAfterEdit();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Metres the texture of the water repeats at; 0: as the material gives it, or 8 m"));
if (retexture)
{
auto changed{water};
changed[m_water_selected].material = m_water_selected_material;
changed[m_water_selected].size = m_water_selected_size;
m_streamer.water(std::move(changed));
}
if (ImGui::Button(STR_C("Remove the chosen water")))
removed = static_cast<std::size_t>(m_water_selected);
}
if (removed)
{
auto changed{water};
changed.erase(changed.begin() + *removed);
m_streamer.water(std::move(changed));
m_water_selected = -1;
m_water_material_of = -1;
}
// the material of the bodies of water which don't name their own
auto defaultmaterial{m_streamer.manifest().water_material};
auto const redefault{texture_field(STR_C("Default water material"), 160.0f, defaultmaterial, terrain_texture_offer(nullptr))};
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Material of the bodies of water which don't name their own"));
if (redefault)
m_streamer.water_material(defaultmaterial);
}
if (ImGui::CollapsingHeader(STR_C("Modifiers")))
{
auto const &modifiers{m_streamer.modifiers()};
if (modifiers.empty())
ImGui::TextWrapped("%s", STR_C("None. The vertical profile of the tracks makes them: the terrain follows the formation and the slopes of the route."));
std::optional<std::string> removed, baked;
for (auto const &modifier : modifiers)
{
ImGui::PushID(modifier.name.c_str());
ImGui::TextUnformatted(format(STR_C("%s, %zu points"), modifier.name.c_str(), modifier.points.size()).c_str());
ImGui::SameLine();
if (ImGui::SmallButton(STR_C("Bake")))
baked = modifier.name;
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The ground keeps the shape the modifier gives it, and the modifier is removed:\n"
"from then on the terrain there is shaped by hand like anywhere else, and no longer follows the track"));
ImGui::SameLine();
if (ImGui::SmallButton(STR_C("Remove")))
removed = modifier.name;
ImGui::PopID();
}
if (auto const touchups = m_streamer.touched_up(); touchups > 0)
ImGui::TextDisabled(STR_C("Touch-ups over the modifiers: %zu points (of the chunks in memory)"), touchups);
if (baked)
{
m_streamer.bake_modifier(*baked);
ground_tiles.clear();
forget_ground_shapes();
}
if (removed)
{
m_streamer.remove_modifier(*removed);
ground_tiles.clear();
forget_ground_shapes();
}
}
auto const files{format(STR_C("Folder: %s"), m_streamer.directory().c_str()) + "\n" + format(STR_C("Chunks in memory: %zu%s"), m_streamer.resident(), m_streamer.modified() ? STR_C(", changes not saved yet") : "")};
auto const drawing{ImGui::CollapsingHeader(STR_C("Drawing and files"))};
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", files.c_str());
if (drawing)
{
ImGui::TextDisabled("%s", files.c_str());
auto detail{m_streamer.detail()};
ImGui::SetNextItemWidth(160.0f);
if (ImGui::SliderFloat(STR_C("Detail"), &detail, 30.0f, 400.0f, "%.0f"))
m_streamer.detail(detail);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("How far the full grid of the chunks reaches: metres of distance per metre of the drawn grid. Further away the grid is coarser"));
bool compress{m_streamer.manifest().compress};
if (ImGui::Checkbox(STR_C("Compress the files (zstd)"), &compress))
m_streamer.compress(compress);
ImGui::TextDisabled("%s", STR_C("The terrain is written with the scenery (Ctrl+S)."));
}
if (false == m_terrain_status.empty())
ImGui::TextWrapped("%s", m_terrain_status.c_str());
}
void editor_mode::draw_terrain_overlay()
{
if (false == m_streamer.active())
return;
screen_projection const projection;
auto *drawlist = ImGui::GetBackgroundDrawList();
// the cursor where a click would put the point: the ground along the ray through the cursor in this frame
glm::dvec3 cursor{Camera.Pos};
bool const hovering = false == ImGui::GetIO().WantCaptureMouse && cursor_on_ground(cursor);
auto const ground = [&](double const X, double const Z, double const Fallback) {
double height;
return m_streamer.height_at(X, Z, height) ? height : Fallback;
};
auto const polyline = [&](std::vector<glm::dvec3> const &Points, ImU32 const Color, bool const Closed, float const Thickness) {
auto const count = Points.size();
for (std::size_t i = 0; i + (Closed ? 0 : 1) < count; ++i)
{
ImVec2 from, to;
if (projection.project(Points[i], from) && projection.project(Points[(i + 1) % count], to))
drawlist->AddLine(from, to, Color, Thickness);
}
};
// the brush aimed at the ground itself along the ray through the cursor
glm::dvec3 brushcentre = cursor;
bool const onground = false == ImGui::GetIO().WantCaptureMouse && terrain_cursor(brushcentre);
if (terrain_brush() && onground)
{
auto const cursor = brushcentre;
std::vector<glm::dvec3> circle;
for (int i = 0; i < 64; ++i)
{
auto const angle = i * 6.283185307179586 / 64.0;
auto const x = cursor.x + std::cos(angle) * m_terrain_brush_radius;
auto const z = cursor.z + std::sin(angle) * m_terrain_brush_radius;
circle.emplace_back(x, ground(x, z, cursor.y) + 0.1, z);
}
auto const color = m_terrain_tool == terrain_tool::paint ? IM_COL32(120, 230, 120, 220)
: m_terrain_tool == terrain_tool::smooth ? IM_COL32(120, 200, 255, 220)
: m_terrain_tool == terrain_tool::restore ? IM_COL32(230, 120, 230, 220)
: IM_COL32(255, 180, 80, 220);
polyline(circle, color, true, 2.0f);
if (m_terrain_tool == terrain_tool::level)
{
// the target: the rim of the brush at its height, joined with the ground, and the height by the cursor
auto const target = static_cast<double>(m_terrain_target);
std::vector<glm::dvec3> level;
for (auto const &point : circle)
level.emplace_back(point.x, target, point.z);
ImU32 const levelcolor = IM_COL32(255, 230, 90, 230);
polyline(level, levelcolor, true, 1.5f);
for (std::size_t i = 0; i < circle.size(); i += circle.size() / 8)
{
ImVec2 from, to;
if (projection.project(circle[i], from) && projection.project(level[i], to))
drawlist->AddLine(from, to, IM_COL32(255, 230, 90, 120), 1.0f);
}
ImVec2 label;
if (projection.project(glm::dvec3{cursor.x, target, cursor.z}, label))
{
double ground{cursor.y};
m_streamer.height_at(cursor.x, cursor.z, ground);
auto const text = format("%.2f m (%+.2f)", target, target - ground);
drawlist->AddText(ImVec2(label.x + 12.0f, label.y - 8.0f), levelcolor, text.c_str());
}
}
}
// outline of a chunk, laid over the ground
auto const chunk_outline = [&](heightmap::chunk_key const &Key, double const Fallback, ImU32 const Color, float const Thickness) {
auto const low = heightmap::chunk_corner(Key);
std::vector<glm::dvec3> square;
auto const edge = [&](glm::dvec2 const &From, glm::dvec2 const &To) {
for (int i = 0; i < 25; ++i)
{
auto const point = From + (To - From) * (i / 25.0);
square.emplace_back(point.x, ground(point.x, point.y, Fallback) + 0.2, point.y);
}
};
auto const size = heightmap::chunk_size;
edge(low, low + glm::dvec2{size, 0.0});
edge(low + glm::dvec2{size, 0.0}, low + glm::dvec2{size});
edge(low + glm::dvec2{size}, low + glm::dvec2{0.0, size});
edge(low + glm::dvec2{0.0, size}, low);
polyline(square, Color, true, Thickness);
};
if (m_terrain_tool == terrain_tool::spacing)
for (auto const &key : m_terrain_selection)
chunk_outline(key, cursor.y, IM_COL32(90, 220, 255, 230), 3.0f);
if ((m_terrain_tool == terrain_tool::chunks || m_terrain_tool == terrain_tool::spacing) && hovering)
{
auto const key = heightmap::chunk_at(cursor.x, cursor.z);
auto const exists = m_streamer.exists(key);
chunk_outline(key, cursor.y, exists ? IM_COL32(255, 255, 255, 200) : IM_COL32(255, 230, 90, 200), 2.0f);
ImVec2 label;
if (exists && projection.project(cursor, label))
{
auto const *chunk = m_streamer.terrain_at(cursor.x, cursor.z);
if (chunk != nullptr)
{
auto const &data = chunk->data();
auto text = std::string(spacing_label(static_cast<float>(data.spacing())));
if (m_terrain_tool == terrain_tool::spacing)
{
if (data.paint > 0)
text += format(STR_C(", paint every %.2f m"), heightmap::chunk_size / data.paint);
if (m_terrain_selection.count(key) != 0)
text += STR(" (selected)");
}
drawlist->AddText(ImVec2(label.x + 14.0f, label.y - 14.0f), IM_COL32(255, 255, 255, 230), text.c_str());
}
}
}
// the bodies of water, and the one being outlined
auto const &water = m_streamer.water();
for (std::size_t i = 0; i < water.size(); ++i)
{
std::vector<glm::dvec3> outline;
for (auto const &point : water[i].outline)
outline.emplace_back(point.x, water[i].level, point.y);
polyline(outline, static_cast<int>(i) == m_water_selected ? IM_COL32(120, 200, 255, 255) : IM_COL32(60, 120, 220, 160), true, static_cast<int>(i) == m_water_selected ? 2.5f : 1.5f);
}
if (m_terrain_tool == terrain_tool::fill && false == m_paint_outline.empty())
{
// laid over the ground, the edges followed every few metres
std::vector<glm::dvec3> outline;
auto const closed = m_paint_outline.size() >= 3;
for (std::size_t i = 0; i + (closed ? 0 : 1) < m_paint_outline.size(); ++i)
{
auto const &from = m_paint_outline[i];
auto const &to = m_paint_outline[(i + 1) % m_paint_outline.size()];
auto const steps = std::clamp(static_cast<int>(glm::distance(glm::dvec2{from.x, from.z}, glm::dvec2{to.x, to.z}) / 4.0), 1, 200);
for (int k = 0; k < steps; ++k)
{
auto const point = from + (to - from) * (static_cast<double>(k) / steps);
outline.emplace_back(point.x, ground(point.x, point.z, point.y) + 0.15, point.z);
}
}
if (false == closed)
outline.push_back(m_paint_outline.back() + glm::dvec3{0.0, 0.15, 0.0});
polyline(outline, IM_COL32(250, 200, 90, 255), false, 2.0f);
for (auto const &point : m_paint_outline)
{
ImVec2 dot;
if (projection.project(point + glm::dvec3{0.0, 0.15, 0.0}, dot))
drawlist->AddCircleFilled(dot, 4.0f, IM_COL32(250, 200, 90, 255));
}
}
if (m_terrain_tool == terrain_tool::water && false == m_water_points.empty())
{
std::vector<glm::dvec3> outline;
for (auto const &point : m_water_points)
outline.emplace_back(point.x, m_water_level, point.z);
polyline(outline, IM_COL32(120, 220, 255, 255), m_water_points.size() >= 3, 2.0f);
for (auto const &point : outline)
{
ImVec2 dot;
if (projection.project(point, dot))
drawlist->AddCircleFilled(dot, 4.0f, IM_COL32(120, 220, 255, 255));
}
}
}
void editor_mode::load_orthophoto_settings()
{
std::string const scenery = scenery_stem();
if (scenery == m_orthophoto_scenery)
return;
m_orthophoto_scenery = scenery;
// a scenery of its own: nobody chose for it yet, unless the user or the scenery keeps the orthophoto on or off
m_orthophoto_chosen = false;
// the height of the terrain model against the tracks of the scenery, as it was fitted or typed in last
m_nmt_state.offset = 0.0;
m_nmt_state.fitted = false;
std::as_const(EditorSettings).terrain_model_offset(scenery, m_nmt_state.offset, m_nmt_state.fitted);
auto const &stored = EditorSettings.orthophoto();
editor_orthophoto::config config = m_orthophoto.settings();
config.radius = stored.radius;
config.height = stored.height;
config.opacity = stored.opacity;
config.year = stored.year;
config.hires = stored.hires;
config.drape = stored.drape;
config.lift = stored.lift;
config.in_scene = stored.in_scene;
config.north = 0.0;
config.east = 0.0;
m_georeference_read = false;
read_georeference();
if (false == std::as_const(EditorSettings).orthophoto_origin(scenery, config.north, config.east) && m_georeference)
{
config.north = m_georeference_origin.x;
config.east = m_georeference_origin.y;
}
auto enabled{m_orthophoto.enabled()};
m_orthophoto_mark.clear();
if (auto const marked{scene::Layers.empty() ? std::vector<std::string>{} : scene::Layers.marked(scene::layer_handle{1}, "//$o")}; false == marked.empty() && editor_orthophoto::read_mark(marked.front(), config, enabled))
{
m_orthophoto_mark = marked.front();
m_orthophoto.enabled(enabled);
m_orthophoto_chosen = true;
WriteLog(format("Editor: the map settings of the scenery, orthophoto %s", enabled ? "on" : "off"));
}
// the user's choice stands over the scenery's; turned on only where the scenery lies on the map, a fictional one has nothing to
// show, and there it's left to the terrain model once the scenery is placed
if (auto const on{stored.on}; on.has_value() && (false == *on || config.north != 0.0 || config.east != 0.0))
{
m_orthophoto.enabled(*on);
m_orthophoto_chosen = true;
WriteLog(format("Editor: the orthophoto %s, as the user left it", *on ? "on" : "off"));
}
m_orthophoto.settings(config);
m_orthophoto_origin_edit = {config.north, config.east};
// the area the imagery and the terrain model are downloaded in, none in a scenery which has none
m_orthophoto_area_mark = scene::Layers.empty() ? std::vector<std::string>{} : scene::Layers.marked(scene::layer_handle{1}, "//$z");
m_orthophoto.area(editor_orthophoto::read_area(m_orthophoto_area_mark));
if (false == m_orthophoto.area().empty())
WriteLog(format("Editor: the download area of the scenery, %zu points, %d tiles of the orthophoto", m_orthophoto.area().size(), m_orthophoto.stats().area_tiles));
}
void editor_mode::save_orthophoto_settings()
{
editor_orthophoto::config const &config = m_orthophoto.settings();
auto &stored = EditorSettings.orthophoto();
stored.radius = config.radius;
stored.height = config.height;
stored.opacity = config.opacity;
stored.year = config.year;
stored.hires = config.hires;
stored.drape = config.drape;
stored.lift = config.lift;
stored.in_scene = config.in_scene;
if (config.north != 0.0 || config.east != 0.0)
EditorSettings.orthophoto_origin(m_orthophoto_scenery, config.north, config.east);
EditorSettings.save();
}
void editor_mode::choose_orthophoto(bool const On)
{
m_orthophoto.enabled(On);
m_orthophoto_chosen = true;
EditorSettings.orthophoto().on = On;
EditorSettings.save();
}
void editor_mode::keep_nmt_settings()
{
// not while a number is being typed in, which changes it at every key
if (ImGui::IsAnyItemActive())
return;
auto const &state{m_nmt_state};
auto &stored{EditorSettings.terrain_model()};
auto changed{stored.band != state.band || stored.around != m_terrain_add_count || stored.shape != state.shape || stored.follow != state.follow};
stored.band = state.band;
stored.around = m_terrain_add_count;
stored.shape = state.shape;
stored.follow = state.follow;
// the offset of the model is the scenery's, fitted to its tracks or typed in
if (false == m_orthophoto_scenery.empty())
{
double offset{0.0};
bool fitted{false};
std::as_const(EditorSettings).terrain_model_offset(m_orthophoto_scenery, offset, fitted);
if (offset != state.offset || fitted != state.fitted)
{
EditorSettings.terrain_model_offset(m_orthophoto_scenery, state.offset, state.fitted);
changed = true;
}
}
if (changed)
EditorSettings.save();
}
void editor_mode::render_orthophoto_ui()
{
load_orthophoto_settings();
editor_orthophoto::config config = m_orthophoto.settings();
bool changed = false; // apply to the layer
bool persist = false; // and store in the editor settings
ImGui::TextUnformatted(STR_C("Scenery origin (0,0,0) in PUWG 1992 / EPSG:2180"));
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("PUWG 1992 (EPSG:2180): the coordinate system of the maps of Poland, in metres; the numbers as on geoportal.gov.pl"));
// geodetic convention, as displayed by geoportal.gov.pl: X grows north, Y grows east.
// applied once editing ends, so half-typed numbers don't trigger downloads
ImGui::SetNextItemWidth(160.0f);
ImGui::InputDouble(STR_C("X (northing, m)"), &m_orthophoto_origin_edit.x, 0.0, 0.0, "%.2f");
bool const northedited = ImGui::IsItemDeactivatedAfterEdit();
ImGui::SetNextItemWidth(160.0f);
ImGui::InputDouble(STR_C("Y (easting, m)"), &m_orthophoto_origin_edit.y, 0.0, 0.0, "%.2f");
bool const eastedited = ImGui::IsItemDeactivatedAfterEdit();
if (northedited || eastedited)
{
config.north = m_orthophoto_origin_edit.x;
config.east = m_orthophoto_origin_edit.y;
changed = true;
persist = true;
}
if (m_georeference)
{
ImGui::TextDisabled("%s", m_georeference_line.c_str());
ImGui::TextDisabled(STR_C("in the scenery file: Y (easting) %.0f m, X (northing) %.0f m"), m_georeference_origin.y, m_georeference_origin.x);
if (config.north != m_georeference_origin.x || config.east != m_georeference_origin.y)
{
ImGui::SameLine();
if (ImGui::SmallButton(STR_C("Use it")))
{
m_orthophoto_origin_edit.x = m_georeference_origin.x;
config.north = m_orthophoto_origin_edit.x;
m_orthophoto_origin_edit.y = m_georeference_origin.y;
config.east = m_orthophoto_origin_edit.y;
changed = true;
persist = true;
}
}
}
if (config.north == 0.0 && config.east == 0.0)
ImGui::TextColored(ImVec4(1.0f, 0.75f, 0.2f, 1.0f), STR_C("Enter the origin of this scenery first"));
else
ImGui::TextDisabled(STR_C("Camera at X %.1f Y %.1f"), config.north + Camera.Pos.z, config.east - Camera.Pos.x);
ImGui::Separator();
ImGui::SetNextItemWidth(160.0f);
changed |= ImGui::SliderInt(STR_C("Distance (tiles)"), &config.radius, 0, editor_orthophoto::max_radius);
persist |= ImGui::IsItemDeactivatedAfterEdit();
ImGui::SameLine();
int const span = static_cast<int>((2 * config.radius + 1) * editor_orthophoto::tile_size);
ImGui::TextDisabled("%d x %d m", span, span);
// ImGui 1.73 has no public disabled state yet, the internal item flag does the job
auto const begin_disabled = [](bool Disabled) {
if (!Disabled)
return;
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
};
auto const end_disabled = [](bool Disabled) {
if (!Disabled)
return;
ImGui::PopStyleVar();
ImGui::PopItemFlag();
};
auto const stats = m_orthophoto.stats();
if (stats.failed > 0 || stats.area_failed > 0)
{
ImGui::Text(STR_C("Tiles: %d shown, %d loading, %d failed"), stats.resident, stats.loading, stats.failed);
ImGui::SameLine();
if (ImGui::Button(STR_C("Retry")))
m_orthophoto.retry_failed();
}
ImGui::Separator();
render_download_area_ui(stats);
ImGui::Separator();
if (ImGui::TreeNode(STR_C("More settings")))
{
// one choice of three, what it means under it: drawn over the view (laid on the ground, where there's none at the height), a
// surface of the scene on the ground, or a flat sheet in the scene; flat over the view says nothing more than on the ground
enum class look
{
overlay,
ground,
sheet
};
auto const current{false == config.in_scene ? look::overlay : config.drape ? look::ground : look::sheet};
auto const choice = [&](char const *Label, look const Look) {
// the Better Renderer doesn't pick up geometry added in the editor: in the scene the photos wouldn't show at all
auto const disabled{Global.NvRenderer && Look != look::overlay && current != Look};
begin_disabled(disabled);
if (ImGui::RadioButton(Label, current == Look) && current != Look)
{
config.in_scene = Look != look::overlay;
config.drape = Look != look::sheet;
changed = true;
persist = true;
}
end_disabled(disabled);
};
auto const explain = [](char const *Text) {
ImGui::Indent();
ImGui::PushTextWrapPos(ImGui::GetCursorPosX() + 360.0f);
ImGui::TextDisabled("%s", Text);
ImGui::PopTextWrapPos();
ImGui::Unindent();
};
ImGui::TextUnformatted(STR_C("Show the photos"));
choice(STR_C("Over the view"), look::overlay);
choice(STR_C("On the terrain, in the scene"), look::ground);
choice(STR_C("A flat sheet in the scene"), look::sheet);
switch (current)
{
case look::overlay:
explain(STR_C("The photos are drawn over the whole view, over the tracks and models as well: lower the opacity to see "
"what's under them. The quickest way to compare the scenery with the map."));
break;
case look::ground:
explain(STR_C("The photos lie on the terrain as its surface: tracks and models standing on them cover them, the scene lights "
"them, and fully opaque the mouse places objects on them as on the ground."));
break;
case look::sheet:
explain(STR_C("One flat sheet at the height below, in the scene: for a scenery with no terrain yet. Where there is "
"terrain, it cuts through the sheet."));
break;
}
if (Global.NvRenderer && current != look::overlay)
ImGui::TextColored(ImVec4(1.0f, 0.75f, 0.2f, 1.0f), "%s", STR_C("The Better Renderer doesn't show them in the scene: pick \"Over the view\""));
if (config.drape)
{
if (ImGui::Button(STR_C("Refit")))
{
// the ground was edited: what's kept of it has to go as well
ground_tiles.clear();
forget_ground_shapes();
m_orthophoto.refit();
for (auto &layer : m_wms)
layer->refit();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Samples the ground again, after it was edited"));
// the lift only matters where the scene can cover the photos
if (config.in_scene)
{
ImGui::SetNextItemWidth(160.0f);
changed |= ImGui::DragFloat(STR_C("Lift above ground (m)"), &config.lift, 0.01f, 0.0f, 50.0f, "%.2f");
persist |= ImGui::IsItemDeactivatedAfterEdit();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Raises the photos above the ground.\n"
"Whatever is lower than this, the rails and the sleepers too, ends up under them: keep it small."));
}
}
ImGui::SetNextItemWidth(160.0f);
changed |= ImGui::DragFloat(config.drape ? STR_C("Height where there's no ground (m)") : STR_C("Height (m)"), &config.height, 0.1f, -1000.0f, 3000.0f, "%.2f");
persist |= ImGui::IsItemDeactivatedAfterEdit();
ImGui::Spacing();
ImGui::SetNextItemWidth(160.0f);
changed |= ImGui::SliderFloat(STR_C("Opacity"), &config.opacity, 0.0f, 1.0f, "%.2f");
persist |= ImGui::IsItemDeactivatedAfterEdit();
if (config.in_scene && ImGui::IsItemHovered())
ImGui::SetTooltip(STR_C("In the scene the opacity is part of the textures: the tiles are read again from the cache\n"
"shortly after the slider stops, which takes a moment."));
// newest imagery, or the newest imagery taken up to the end of the selected year
int const thisyear = static_cast<int>(std::chrono::year_month_day{std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now())}.year());
std::string const yearlabel = config.year ? std::to_string(config.year) : std::string(STR_C("Newest"));
ImGui::SetNextItemWidth(160.0f);
if (ImGui::BeginCombo(STR_C("Photo year"), yearlabel.c_str()))
{
if (ImGui::Selectable(STR_C("Newest"), config.year == 0))
{
config.year = 0;
changed = true;
persist = true;
}
for (int year = thisyear; year >= editor_orthophoto::oldest_year; --year)
{
if (ImGui::Selectable(std::to_string(year).c_str(), config.year == year))
{
config.year = year;
changed = true;
persist = true;
}
}
ImGui::EndCombo();
}
if (ImGui::Checkbox(STR_C("4K tiles where available"), &config.hires))
{
changed = true;
persist = true;
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip(STR_C("4096 px tiles (~6 cm/px) from the high-resolution orthophoto, for the tiles around the camera.\n"
"Areas it doesn't cover use the standard imagery. Each 4K tile takes ~85 MB of video memory."));
ImGui::Separator();
if (stats.failed == 0)
ImGui::Text(STR_C("Tiles: %d shown, %d loading, %d failed"), stats.resident, stats.loading, stats.failed);
if (!editor_orthophoto::can_download())
ImGui::TextDisabled(STR_C("This build has no HTTP client, only cached tiles are shown"));
ImGui::TextDisabled(STR_C("Cache: %s"), editor_orthophoto::cache_directory().c_str());
if (ImGui::Button(STR_C("Clear cache")))
m_orthophoto.clear_cache();
if (ImGui::IsItemHovered())
ImGui::SetTooltip(STR_C("Deletes downloaded tiles from disk. They are fetched again around the camera."));
ImGui::TreePop();
}
if (changed)
m_orthophoto.settings(config);
if (persist)
save_orthophoto_settings();
ImGui::TextDisabled(STR_C("Imagery: GUGiK, geoportal.gov.pl"));
}
void editor_mode::draw_orthophoto()
{
if (false == m_orthophoto.enabled() && std::none_of(m_wms.begin(), m_wms.end(), [](auto const &Layer) { return Layer->enabled(); }))
return;
// the camera was just published to the renderer (update_camera), so build the view from it rather than
// from the renderer's previous frame; camera-relative rotation, same projection as the other overlays
glm::dmat4 viewmatrix{1.0};
Camera.SetMatrix(viewmatrix);
glm::mat4 const view = glm::mat4(glm::mat3(viewmatrix));
ImGuiIO const &io = ImGui::GetIO();
float const aspect = io.DisplaySize.y > 0.0f ? io.DisplaySize.x / io.DisplaySize.y : 1.0f;
auto const projection{editor_mode::projection_matrix(aspect) * view};
m_orthophoto.draw(projection, Camera.Pos, io.DisplaySize.x, io.DisplaySize.y);
// the WMS layers over it, the top one of the list last
for (auto layer{m_wms.rbegin()}; layer != m_wms.rend(); ++layer)
(*layer)->draw(projection, Camera.Pos, io.DisplaySize.x, io.DisplaySize.y);
}
void editor_mode::draw_ortho_compass() const
{
if (false == Global.EditorOrtho)
return;
screen_projection const projection;
ImVec2 north_from, north_to, east_from, east_to;
glm::dvec3 const eye{Camera.Pos};
if (false == projection.project(eye + glm::dvec3{0.0, 0.0, -80.0}, north_from) || false == projection.project(eye + glm::dvec3{0.0, 0.0, 80.0}, north_to) ||
false == projection.project(eye + glm::dvec3{80.0, 0.0, 0.0}, east_from) || false == projection.project(eye + glm::dvec3{-80.0, 0.0, 0.0}, east_to))
return;
auto const dir = [](ImVec2 const From, ImVec2 const To) {
ImVec2 const d{To.x - From.x, To.y - From.y};
auto const length{std::sqrt(d.x * d.x + d.y * d.y)};
return length > 1e-3f ? ImVec2{d.x / length, d.y / length} : ImVec2{0.0f, -1.0f};
};
auto const north{dir(north_from, north_to)};
auto const east{dir(east_from, east_to)};
ImVec2 const south{-north.x, -north.y};
ImVec2 const west{-east.x, -east.y};
ImGuiIO const &io = ImGui::GetIO();
float const radius{36.0f};
float const margin{16.0f};
// in the corner of the view, where the buttons of the toolbar and the hints along its bottom aren't
ImVec2 const center{editor_ui::view_max().x - margin - radius - 14.0f, editor_ui::view_max().y - margin - radius - 54.0f};
auto *drawlist{ImGui::GetForegroundDrawList()};
drawlist->AddCircleFilled(center, radius + 14.0f, IM_COL32(0, 0, 0, 180), 48);
drawlist->AddCircle(center, radius - 6.0f, IM_COL32(255, 255, 255, 50), 48, 1.2f);
drawlist->AddCircleFilled(center, 3.0f, IM_COL32(255, 255, 255, 200), 12);
auto const at = [&](ImVec2 const Unit, float const Length) { return ImVec2{center.x + Unit.x * Length, center.y + Unit.y * Length}; };
ImVec2 const side{-north.y * 5.0f, north.x * 5.0f};
drawlist->AddTriangleFilled(at(north, radius - 14.0f), ImVec2{center.x + side.x, center.y + side.y}, ImVec2{center.x - side.x, center.y - side.y}, IM_COL32(230, 70, 55, 255));
drawlist->AddTriangleFilled(at(south, radius - 18.0f), ImVec2{center.x + side.x, center.y + side.y}, ImVec2{center.x - side.x, center.y - side.y}, IM_COL32(210, 210, 210, 210));
auto const letter = [&](char const *Text, ImVec2 const Unit, ImU32 const Color) {
auto const size{ImGui::CalcTextSize(Text)};
auto const p{at(Unit, radius + 2.0f)};
drawlist->AddText(ImVec2(p.x - size.x * 0.5f, p.y - size.y * 0.5f), Color, Text);
};
letter("N", north, IM_COL32(255, 90, 70, 255));
letter("E", east, IM_COL32(230, 230, 230, 230));
letter("S", south, IM_COL32(190, 190, 190, 180));
letter("W", west, IM_COL32(230, 230, 230, 230));
}
void editor_mode::forget_ground()
{
ground_tiles.clear();
forget_ground_shapes();
}
void editor_mode::forget_ground_in(glm::dvec2 const &Min, glm::dvec2 const &Max)
{
for (auto tile{ground_tiles.begin()}; tile != ground_tiles.end();)
{
glm::dvec2 const low{tile->first.first * ground_tile_size, tile->first.second * ground_tile_size};
if (low.x <= Max.x && low.x + ground_tile_size >= Min.x && low.y <= Max.y && low.y + ground_tile_size >= Min.y)
tile = ground_tiles.erase(tile);
else
++tile;
}
}
editor_terrain *editor_mode::terrain_at(double X, double Z)
{
return m_streamer.terrain_at(X, Z);
}
std::vector<editor_terrain *> editor_mode::active_terrains() const
{
std::vector<editor_terrain *> out;
out.reserve(m_streamer.resident());
m_streamer.collect(out);
return out;
}
bool editor_mode::ensure_terrain()
{
if (m_streamer.active())
return true;
auto const name = scenery_stem();
if (name.empty())
return false;
m_streamer.open(name);
m_terrain_spacing = m_streamer.manifest().spacing;
return m_streamer.active();
}
bool editor_mode::add_chunk_from_ground(heightmap::chunk_key const &Key, float const Spacing, std::uint16_t const Layer)
{
if (m_streamer.exists(Key))
return false;
auto const low = heightmap::chunk_corner(Key);
auto const high = low + glm::dvec2{heightmap::chunk_size};
// the ground under the chunk, the edges of the neighbouring chunks included, so the new one meets them
std::vector<world_triangle> triangles;
gather_ground_triangles(low - glm::dvec2{1.0}, high + glm::dvec2{1.0}, true, 50.0f, triangles, true);
triangle_grid const ground(std::move(triangles), low - glm::dvec2{1.0}, high + glm::dvec2{1.0});
auto const cells = heightmap::cells_for(Spacing);
auto const side = static_cast<std::size_t>(cells) + 1;
auto const step = heightmap::chunk_size / cells;
std::vector<double> heights(side * side, 0.0);
std::vector<char> found(side * side, 0);
bool any = false;
for (std::size_t iz = 0; iz < side; ++iz)
for (std::size_t ix = 0; ix < side; ++ix)
{
auto const index = iz * side + ix;
if (ground.height_at(low.x + ix * step, low.y + iz * step, std::numeric_limits<double>::max(), heights[index]))
found[index] = 1, any = true;
}
// past the edge of the ground the chunk is carried on level with the nearest point of it
if (any)
fill_from_nearest(heights, found, side);
else
std::fill(heights.begin(), heights.end(), m_terrain_baseheight);
auto const added = m_streamer.add_chunk(Key.first, Key.second, Spacing, [&](double const X, double const Z) {
auto const ix = static_cast<std::size_t>(std::clamp<long>(std::lround((X - low.x) / step), 0, cells));
auto const iz = static_cast<std::size_t>(std::clamp<long>(std::lround((Z - low.y) / step), 0, cells));
return static_cast<float>(heights[iz * side + ix]);
}, Layer);
if (added)
{
ground_tiles.clear();
forget_ground_shapes();
}
return added;
}
void editor_mode::add_chunks_around(int const Count)
{
if (false == ensure_terrain())
return;
auto const centre = heightmap::chunk_at(Camera.Pos.x, Camera.Pos.z);
auto const first = -(Count - 1) / 2;
int made = 0;
for (int dz = 0; dz < Count; ++dz)
for (int dx = 0; dx < Count; ++dx)
if (add_chunk_from_ground({centre.first + first + dx, centre.second + first + dz}, m_terrain_spacing))
++made;
m_terrain_status = format(STR_C("%d chunks made"), made);
}
void editor_mode::handle_terrain_click(bool const Shift)
{
glm::dvec3 world;
if (false == cursor_on_ground(world))
return; // nothing under the cursor
switch (m_terrain_tool)
{
case terrain_tool::chunks:
{
if (false == ensure_terrain())
return;
auto const key = heightmap::chunk_at(world.x, world.z);
if (Shift)
{
if (m_streamer.exists(key))
{
m_streamer.remove_chunk(key.first, key.second);
ground_tiles.clear();
forget_ground_shapes();
m_terrain_status = format(STR_C("Chunk %d,%d removed"), key.first, key.second);
}
return;
}
// a click on a chunk adds the neighbour by the nearest edge, a click outside fills the clicked place
auto target = key;
if (m_streamer.exists(key))
{
auto const low = heightmap::chunk_corner(key);
double const west = world.x - low.x, east = heightmap::chunk_size - west, south = world.z - low.y, north = heightmap::chunk_size - south;
double const nearest = std::min({west, east, south, north});
if (nearest == west)
--target.first;
else if (nearest == east)
++target.first;
else if (nearest == south)
--target.second;
else
++target.second;
}
m_terrain_status = add_chunk_from_ground(target, m_terrain_spacing) ? format(STR_C("Chunk %d,%d made"), target.first, target.second) : std::string(STR_C("There's a chunk there already"));
break;
}
case terrain_tool::spacing:
{
// a click selects the chunk, Ctrl adds it to the selection or takes it away
auto const key = heightmap::chunk_at(world.x, world.z);
if (false == m_streamer.exists(key))
{
if (false == Global.ctrlState)
m_terrain_selection.clear();
return;
}
if (false == Global.ctrlState)
m_terrain_selection = {key};
else if (m_terrain_selection.erase(key) == 0)
m_terrain_selection.insert(key);
m_terrain_status = format(STR_C("Chunks selected: %zu"), m_terrain_selection.size());
break;
}
case terrain_tool::fill:
{
if (Shift)
{
if (false == m_paint_outline.empty())
m_paint_outline.pop_back();
return;
}
m_paint_outline.push_back(placement_on_ground(world));
break;
}
case terrain_tool::water:
{
if (Shift)
{
if (false == m_water_points.empty())
m_water_points.pop_back();
return;
}
auto const point = placement_on_ground(world);
// the water reaches the lowest point of its shore, unless given by hand
if (false == m_water_level_edited)
m_water_level = m_water_points.empty() ? static_cast<float>(point.y) : std::min(m_water_level, static_cast<float>(point.y));
m_water_points.push_back(point);
break;
}
default:
break;
}
}
namespace
{
// takes the shapes of specified terrain files and materials out of the scene. returns: the count of the shapes
std::size_t drop_terrain_shapes(std::set<std::uint16_t> const &Files, std::set<material_handle> const &Materials)
{
std::size_t dropped = 0;
auto const dropping = [&](scene::shape_node const &Shape) {
auto const &data = Shape.data();
bool const drop = Files.count(data.terrainfile) != 0 && Materials.count(data.material) != 0;
dropped += drop ? 1 : 0;
return drop;
};
for (std::size_t i = 0; i < sq(scene::EU07_REGIONSIDESECTIONCOUNT); ++i)
{
auto *section = simulation::Region->get_section(i);
if (section == nullptr)
continue;
section->m_shapes.erase(std::remove_if(section->m_shapes.begin(), section->m_shapes.end(), dropping), section->m_shapes.end());
for (auto &cell : section->m_cells)
{
cell.m_shapesopaque.erase(std::remove_if(cell.m_shapesopaque.begin(), cell.m_shapesopaque.end(), dropping), cell.m_shapesopaque.end());
cell.m_shapestranslucent.erase(std::remove_if(cell.m_shapestranslucent.begin(), cell.m_shapestranslucent.end(), dropping), cell.m_shapestranslucent.end());
}
}
return dropped;
}
} // namespace
void editor_mode::load_ground_files()
{
auto &conversion{m_ground_conversion};
conversion.files.clear();
conversion.triangles.clear();
conversion.materials.clear();
conversion.names.clear();
conversion.chosen.clear();
std::map<std::string, std::uint16_t> lookup;
auto const &references{scene::terrain_file::references()};
for (std::size_t index = 0; index < references.size(); ++index)
{
ground_conversion::source source;
source.file = references[index].file;
source.reference = static_cast<std::uint16_t>(index + 1);
source.first = conversion.triangles.size();
source.good = scene::terrain_file::read_triangles(
references[index],
[&](std::string const &Material, std::array<glm::dvec3, 3> const &Triangle) {
auto found{lookup.find(Material)};
if (found == lookup.end())
{
found = lookup.emplace(Material, static_cast<std::uint16_t>(conversion.names.size())).first;
conversion.names.push_back(Material);
}
conversion.triangles.push_back(Triangle);
conversion.materials.push_back(found->second);
},
&source.message, &source.text);
if (false == source.good)
{
// what was read before the failure doesn't count
conversion.triangles.resize(source.first);
conversion.materials.resize(source.first);
source.chosen = false;
}
source.count = conversion.triangles.size() - source.first;
conversion.files.push_back(std::move(source));
}
// the terrain files hold the ground, all of it is taken unless told otherwise
for (std::size_t index = 0; index < conversion.names.size(); ++index)
conversion.chosen.insert(index);
}
std::string editor_mode::convert_ground()
{
auto &conversion{m_ground_conversion};
if (simulation::Region == nullptr || conversion.chosen.empty())
return {};
if (false == ensure_terrain())
return STR_C("The heightmap terrain can't be opened");
double const size = heightmap::chunk_size;
int const cells = heightmap::cells_for(m_terrain_spacing);
double const cellsize = size / cells;
// the chosen triangles of the chosen files
std::vector<world_triangle> triangles;
std::vector<std::uint16_t> materials;
std::set<std::uint16_t> files;
for (auto const &source : conversion.files)
{
if (false == source.good || false == source.chosen)
continue;
files.insert(source.reference);
for (auto index = source.first; index < source.first + source.count; ++index)
{
if (conversion.chosen.count(conversion.materials[index]) == 0)
continue;
triangles.push_back(conversion.triangles[index]);
materials.push_back(conversion.materials[index]);
}
}
if (triangles.empty())
return STR_C("The chosen files and materials have no triangles");
// the triangles reaching each chunk
std::map<heightmap::chunk_key, std::vector<std::uint32_t>> buckets;
for (std::uint32_t i = 0; i < triangles.size(); ++i)
{
auto const &t = triangles[i];
// a triangle which only touches the edge of a chunk gives it nothing but the edge, which the chunk on the other side has
auto const margin{1e-3};
auto const low{heightmap::chunk_at(std::min({t[0].x, t[1].x, t[2].x}) + margin, std::min({t[0].z, t[1].z, t[2].z}) + margin)};
auto const high{heightmap::chunk_at(std::max({t[0].x, t[1].x, t[2].x}) - margin, std::max({t[0].z, t[1].z, t[2].z}) - margin)};
for (int x = low.first; x <= high.first; ++x)
for (int z = low.second; z <= high.second; ++z)
buckets[{x, z}].push_back(i);
}
// palette entries of the materials, added as they're needed
std::map<std::uint16_t, std::uint16_t> palette;
auto const layer_of = [&](std::uint16_t const Material) {
auto const found{palette.find(Material)};
if (found != palette.end())
return found->second;
auto const layer{m_streamer.layer(conversion.names[Material])};
palette.emplace(Material, layer);
return layer;
};
std::size_t made = 0, skipped = 0;
std::size_t const side = static_cast<std::size_t>(cells) + 1;
auto const samples{heightmap::default_paint_samples};
std::size_t const paintside = static_cast<std::size_t>(samples) + 1;
double const paintstep = size / samples;
for (auto const &bucket : buckets)
{
auto const &key = bucket.first;
if (m_streamer.exists(key))
{
++skipped;
continue;
}
auto const low{heightmap::chunk_corner(key)};
std::vector<world_triangle> local;
std::vector<std::uint16_t> localmaterials;
for (auto const i : bucket.second)
{
local.push_back(triangles[i]);
localmaterials.push_back(materials[i]);
}
triangle_grid const ground(std::move(local), low - glm::dvec2{1.0}, low + glm::dvec2{size + 1.0});
auto const ceiling{std::numeric_limits<double>::max()};
// heights of the grid points; where there's no ground they're taken from the nearest point which has some
std::vector<double> heights(side * side, 0.0);
std::vector<char> found(side * side, 0);
bool any = false;
for (std::size_t iz = 0; iz < side; ++iz)
for (std::size_t ix = 0; ix < side; ++ix)
{
auto const index = iz * side + ix;
if (ground.height_at(low.x + ix * cellsize, low.y + iz * cellsize, ceiling, heights[index]))
found[index] = 1, any = true;
}
if (false == any)
continue;
fill_from_nearest(heights, found, side);
// the material on top at each paint sample
std::vector<std::uint16_t> paint(paintside * paintside, 0);
std::vector<char> painted(paintside * paintside, 0);
std::map<std::uint16_t, std::size_t> counts;
for (std::size_t iz = 0; iz < paintside; ++iz)
for (std::size_t ix = 0; ix < paintside; ++ix)
{
double height;
std::uint32_t triangle;
if (false == ground.height_at(low.x + ix * paintstep, low.y + iz * paintstep, ceiling, height, &triangle))
continue;
auto const index = iz * paintside + ix;
paint[index] = localmaterials[triangle];
painted[index] = 1;
++counts[paint[index]];
}
if (counts.empty())
{
// the points of the grid hit the ground, the samples in between didn't; the nearest triangle covers the chunk
counts[localmaterials.front()] = 1;
std::fill(paint.begin(), paint.end(), localmaterials.front());
}
else
fill_from_nearest(paint, painted, paintside);
// a chunk holds up to 8 materials, the ones covering the most; the rest is given to the one covering the most
std::vector<std::pair<std::size_t, std::uint16_t>> ranking;
for (auto const &count : counts)
ranking.emplace_back(count.second, count.first);
std::sort(ranking.begin(), ranking.end(), [](auto const &A, auto const &B) { return A.first > B.first; });
if (ranking.size() > heightmap::max_chunk_layers)
ranking.resize(heightmap::max_chunk_layers);
auto data{heightmap::chunk_data::make_flat(key, m_terrain_spacing, 0.f)};
for (std::size_t index = 0; index < data->heights.size(); ++index)
data->heights[index] = static_cast<float>(heights[index]);
std::map<std::uint16_t, std::size_t> slots;
for (std::size_t slot = 0; slot < ranking.size(); ++slot)
{
data->layers[slot] = layer_of(ranking[slot].second);
slots.emplace(ranking[slot].second, slot);
}
if (ranking.size() > 1)
{
auto const plane{paintside * paintside};
data->paint = samples;
data->weights.assign(plane * ranking.size(), 0);
for (std::size_t index = 0; index < plane; ++index)
{
auto const slot{slots.find(paint[index])};
data->weights[(slot != slots.end() ? slot->second : 0) * plane + index] = 255;
}
}
if (m_streamer.add_chunk(std::move(data)))
++made;
}
forget_ground();
// the converted ground goes from the scene, and from the text files on save
std::set<material_handle> handles;
for (auto const material : conversion.chosen)
if (material < conversion.names.size())
handles.insert(GfxRenderer->Fetch_Material(conversion.names[material]));
auto const dropped = drop_terrain_shapes(files, handles);
std::set<scene::layer_handle> layers;
std::size_t binary = 0;
for (auto const &source : conversion.files)
{
if (false == source.good || false == source.chosen)
continue;
if (false == source.text)
{
++binary;
continue;
}
auto const textfile{source.file + ".txtf"};
for (std::size_t layer = 1; layer <= scene::Layers.size(); ++layer)
{
auto name{scene::Layers.layer(static_cast<scene::layer_handle>(layer)).name};
replace_slashes(name);
if (name.ends_with(textfile))
layers.insert(static_cast<scene::layer_handle>(layer));
}
}
auto const erased = scene::Layers.erase_shapes(handles, &layers);
auto summary = format(STR_C("Ground converted: %zu chunks of heightmap terrain made, %zu shapes taken out of the scene, %zu triangle nodes go from the terrain files on save"), made, dropped, erased.first);
if (skipped > 0)
summary += format(STR_C(", %zu chunks kept as there was terrain already"), skipped);
if (erased.second > 0)
summary += format(STR_C(", %zu nodes stay as their files can't be rewritten"), erased.second);
if (binary > 0)
summary += format(STR_C(". %zu of the files are binary (.btf) without their text: their triangles come back with the next load, until the files are taken out of the terrain directive"), binary);
WriteLog("Editor: " + summary, logtype::generic);
return summary;
}
bool editor_mode::save()
{
if (scene::Layers.empty())
{
// without the sources tracked during the load there's no telling where the changes belong
ui()->set_status(STR("The scenery wasn't opened for editing. Start the simulator with: -edit <scenery file>"), true);
return false;
}
first_step_done(first_step::save);
// the heightmap terrain is kept in files of its own, the scenery only needs the directive which loads it, in a layer of its own
std::vector<std::string> rootstatements;
std::map<scene::layer_handle, std::vector<std::string>> statements;
if (m_streamer.active())
{
std::string error;
if (false == m_streamer.save(&error))
{
ui()->set_status(format(STR_C("The terrain couldn't be saved: %s"), error.c_str()), true);
return false;
}
if (false == scene::Layers.terrain_directive())
{
auto const directive{"heightmap_terrain " + m_streamer.name() + " endheightmap_terrain"};
if (auto const layer{output_layer("teren")}; layer != null_handle)
statements[layer].push_back(directive);
else
rootstatements.push_back(directive);
}
}
if (m_profile.changed)
profile_store();
switch_store::tidy();
switch_store::settle();
infra_store();
m_srk.store();
if (false == m_saved_camera.has_value() || glm::distance(m_saved_camera->first, glm::dvec3{Camera.Pos}) > 0.5 || glm::distance(m_saved_camera->second, glm::vec3{Camera.Angle}) > 0.01f)
{
m_saved_camera = std::make_pair(glm::dvec3{Camera.Pos}, glm::vec3{Camera.Angle});
scene::Layers.mark(scene::layer_handle{1}, "//$c", {format("%.3f %.3f %.3f %.5f %.5f", Camera.Pos.x, Camera.Pos.y, Camera.Pos.z, Camera.Angle.x, Camera.Angle.y)});
}
if (auto const map{editor_orthophoto::mark(m_orthophoto.settings(), m_orthophoto.enabled(), m_orthophoto.settings().north != 0.0 || m_orthophoto.settings().east != 0.0)}; false == m_orthophoto_scenery.empty() && map != m_orthophoto_mark && (m_orthophoto.enabled() || false == m_orthophoto_mark.empty()))
{
m_orthophoto_mark = map;
scene::Layers.mark(scene::layer_handle{1}, "//$o", {map});
}
if (auto const area{editor_orthophoto::area_lines(m_orthophoto.area())}; false == m_orthophoto_scenery.empty() && area != m_orthophoto_area_mark)
{
m_orthophoto_area_mark = area;
scene::Layers.mark(scene::layer_handle{1}, "//$z", area);
}
auto const terrainadded{false == rootstatements.empty() || false == statements.empty()};
auto const controls{turntable_controls(rootstatements)};
auto const noses{nose_controls(rootstatements)};
std::vector<std::string> trailing;
auto const placed{m_vehicle.trainset_lines(trailing)};
if (placed == 0)
starter_trainset(trailing);
auto const result = scene::Layers.save(rootstatements, trailing, statements);
if (result.success && controls)
m_turntable_included = true;
if (result.success && noses)
m_nose_included = true;
if (result.success && placed > 0)
m_vehicle.trainsets_written();
if (result.success && placed == 0 && trailing.size() == 3)
{
std::istringstream header{trailing.front()};
std::string keyword;
std::string timetable;
std::string track;
double offset{0.0};
header >> keyword >> timetable >> track >> offset;
if (auto *path{simulation::Paths.find(track)}; path != nullptr && simulation::Vehicles.find("wmb10-6457") == nullptr)
simulation::State.insert_trainset("wmb10-6457", path, offset, trailing[1] + "\n");
}
if (result.success && terrainadded)
{
scene::Layers.terrain_directive(true);
}
ui()->set_status(result.message, false == result.success);
WriteLog("Editor: " + result.message, logtype::generic);
return result.success;
}
void editor_mode::toggle_include(scene::instance_handle const Instance)
{
if (false == scene::Layers.tracked(Instance))
return;
auto const &included = scene::Layers.instance(Instance);
if (included.dead)
{
// its directive is gone from the scenery file, and the place it was at with it
ui()->set_status(format(STR_C("Include of \"%s\" was removed from the saved scenery, this can't be taken back"), included.file->c_str()), true);
return;
}
scene::Layers.removed(Instance, false == included.removed);
drives_changed();
ui()->set_status(format(included.removed ? STR_C("Include of \"%s\" removed") : STR_C("Include of \"%s\" restored"), included.file->c_str()));
}
void editor_mode::restore_include(EditorSnapshot &Snapshot)
{
if (Snapshot.serialized.empty())
{
// the include was placed or removed; either is taken back by flipping its removal
toggle_include(Snapshot.instance);
select_include(0);
return;
}
// parameters of the include were changed. the snapshot holds the directive to go back to, and leaves with the current one
auto current = scene::Layers.directive(Snapshot.instance);
if (current.empty() || scene::Layers.instance(Snapshot.instance).dead)
{
ui()->set_status(format(STR_C("Include of \"%s\" is gone from the scenery, the change can't be taken back"), Snapshot.node_name.c_str()), true);
return;
}
set_include_directive(Snapshot.instance, Snapshot.serialized);
Snapshot.serialized = std::move(current);
select_include(scene::Layers.removable(Snapshot.instance) ? Snapshot.instance : 0);
}
void editor_mode::restore_includes(EditorSnapshot &Snapshot)
{
std::size_t gone{0};
for (auto const instance : Snapshot.instances)
{
if (false == scene::Layers.tracked(instance) || scene::Layers.instance(instance).dead)
{
++gone;
continue;
}
scene::Layers.removed(instance, false == scene::Layers.instance(instance).removed);
}
drives_changed();
for (auto &entry : Snapshot.directives)
{
auto current = scene::Layers.directive(entry.first);
if (current.empty() || scene::Layers.instance(entry.first).dead)
{
++gone;
continue;
}
set_include_directive(entry.first, entry.second);
entry.second = std::move(current);
}
select_include(0);
ui()->set_status(gone > 0 ? format(STR_C("Includes gone from the saved scenery, they can't be taken back: %zu"), gone) : format(STR_C("Includes restored: %zu"), Snapshot.instances.size() + Snapshot.directives.size()), gone > 0);
}
void editor_mode::select_include(scene::instance_handle const Instance)
{
m_instance = (scene::Layers.tracked(Instance) ? Instance : 0);
m_include = include_selection{};
m_include_gesture = false;
if (m_instance == 0)
{
ui()->set_include(nullptr);
return;
}
auto const &included = scene::Layers.instance(m_instance);
m_include.instance = m_instance;
m_include.target = *included.file;
// NOTE: a template without a usable description is still presented, with its parameters unnamed
std::string error;
editor_includes::load(*included.file, m_include.info, error);
if (false == editor_includes::parse_directive(scene::Layers.directive(m_instance), m_include.target, m_include.values))
{
m_include.values.clear();
m_include.issue = STR_C("The directive of the include can't be read from the scenery file, its parameters can't be changed");
}
// with a rotation or a scale in effect the parameters don't translate to a location in the scene in a single way,
// see place_include()
m_include.placement = (included.context.rotation == glm::vec3{0.f} && included.context.scale == glm::vec3{1.f});
ui()->set_include(&m_include);
}
void editor_mode::set_include_directive(scene::instance_handle const Instance, std::string const &Directive)
{
drives_changed();
std::string file;
std::vector<std::string> values;
if (scene::Layers.tracked(Instance) && editor_includes::parse_directive(Directive, file, values) && scene::Layers.instance(Instance).file != nullptr &&
editor_includes::normalized(file) == editor_includes::normalized(*scene::Layers.instance(Instance).file))
file.clear();
if (false == scene::Layers.modify(Instance, Directive, file))
return;
std::vector<TAnimModel *> retired;
simulation::State.rebuild_include(Instance, retired);
// the pick of the renderer is resolved a few frames after it's requested, and can still deliver one of these
for (auto *model : retired)
{
nullify_history_pointers(model);
m_retired.emplace_back(model, 120);
}
}
void editor_mode::apply_include()
{
if (m_instance == 0 || false == m_include.issue.empty())
return;
for (auto const &value : m_include.values)
{
if (value == "end" || value.find_first_of("\"\r\n") != std::string::npos)
{
// either would cut the directive short
ui()->set_status(STR_C("A parameter of an include can't be \"end\", or have a quotation mark in it"), true);
select_include(m_instance); // back to the values of the directive
return;
}
}
auto before = scene::Layers.directive(m_instance);
if (false == m_include.info.signal_kind.empty()
&& std::ranges::any_of(m_include.info.parameters, [](auto const &Parameter) { return Parameter.id == 6 && Parameter.role == "texture"; }))
{
// the drawn plate of a signal follows its name; a plate picked from the textures stays, and so does a drawn one given
// another name by hand. While the name is cleared or one the plate can't carry, the plate keeps the last name, and
// follows again once a usable name is typed. No plate, or the texture named like the signal the editor gave before the
// plates were drawn, become the drawn one. The look, the index and the m of the plate go with it
std::string file;
std::vector<std::string> previous;
if (editor_includes::parse_directive(before, file, previous) && previous.size() >= 6 && m_include.values.size() >= 6)
{
auto const look{signal_plate_style(previous[5])};
auto const carried = [&](std::string const &Name) { return Name != "none" && signal_plate_drawn(signal_plate(Name, look)); };
if (previous[0] != m_include.values[0] && m_include.values[5] == previous[5] && carried(m_include.values[0])
&& (previous[5] == signal_plate(previous[0], look) || (false == carried(previous[0]) && signal_plate_drawn(previous[5])) || signal_plate_blank(previous[0], previous[5])))
m_include.values[5] = signal_plate(m_include.values[0], look);
}
}
auto const directive = editor_includes::compose_directive(m_include.target, m_include.values);
if (directive == before)
return;
std::string read_before;
std::string read_after;
// a signal renamed: its indicators are named after it
std::string name_before;
glm::dvec3 standing{0.0};
// the signal templates known, though no signal tool looked for them yet
if (m_include.info.category == "signal")
signal_scan(true);
if (false == m_include.info.signal_read.empty() || signal_template_of(m_include.target) != nullptr)
{
std::string file;
std::vector<std::string> previous;
if (editor_includes::parse_directive(before, file, previous))
{
if (false == m_include.info.signal_read.empty())
{
read_before = editor_includes::substitute(m_include.info.signal_read, previous);
read_after = editor_includes::substitute(m_include.info.signal_read, m_include.values);
}
if (previous.size() >= 4 && false == m_include.values.empty() && previous.front() != m_include.values.front())
{
name_before = previous.front();
standing = scene::Layers.instance(m_instance).context.offset + glm::dvec3{std::strtod(previous[1].c_str(), nullptr), std::strtod(previous[2].c_str(), nullptr), std::strtod(previous[3].c_str(), nullptr)};
}
}
}
if (false == m_include_gesture)
{
// a single undo step for the whole drag
EditorSnapshot snap;
snap.instance = m_instance;
snap.node_name = *scene::Layers.instance(m_instance).file;
snap.serialized = std::move(before);
if (read_before != read_after)
signal_renamed(read_before, read_after, snap);
m_history.push_back(std::move(snap));
g_redo.clear();
m_include_gesture = true;
}
else if (read_before != read_after && false == m_history.empty())
signal_renamed(read_before, read_after, m_history.back());
set_include_directive(m_instance, directive);
if (false == name_before.empty() && false == m_history.empty())
{
std::vector<std::pair<scene::instance_handle, std::string>> carried;
signal_indicators_renamed(name_before, m_include.values.front(), standing, carried);
history_add_directives(m_history.back(), carried);
}
}
void editor_mode::place_include(std::string const &File, int const RotationMode, float const FixedRotation)
{
if (scene::Layers.empty())
{
// the directive has to go to a scenery file, and without the sources tracked there's no telling which
ui()->set_status(STR_C("Scenery templates can be placed only in a scenery opened for editing (-edit)"), true);
WriteLog("Editor: scenery templates can be placed only in a scenery opened for editing (-edit)", logtype::generic);
return;
}
include_info info;
std::string issue;
auto const parameters = editor_includes::parameter_count(File);
if (false == editor_includes::load(File, info, issue) || false == editor_includes::complete(info, parameters, &issue))
{
ui()->set_status(format(STR_C("Template \"%s\" can't be placed: %s"), File.c_str(), issue.c_str()), true);
return;
}
// the directive is added to the file of the active layer, and has to make sense with the placement in effect where it goes
auto const layer = scene::Layers.resolve(scene::Layers.active());
if (std::string reason; false == scene::Layers.accepts(layer, &reason))
{
ui()->set_status(format(STR_C("Template \"%s\" can't be placed: %s"), File.c_str(), (reason.empty() ? std::string{STR_C("there's no active layer to put it in")} : format(STR_C("the active layer can't take it, %s"), reason.c_str())).c_str()), true);
return;
}
auto const context = scene::Layers.layer(layer).context_insert();
if (context.rotation != glm::vec3{0.f} || context.scale != glm::vec3{1.f})
{
// a template can pass its parameters to an origin directive, which isn't affected by these, or straight to
// the nodes, which are. there's no single set of parameter values which puts both at the cursor
ui()->set_status(format(STR_C("Template \"%s\" can't be placed: the file of the active layer would receive it with a rotation or a scale in effect. Pick another layer"), File.c_str()), true);
return;
}
glm::dvec3 const location = placement_on_ground(Camera.Pos + clamp_mouse_offset_to_max(GfxRenderer->Mouse_Position()));
std::optional<float> yaw;
if (RotationMode == functions_panel::RANDOM)
yaw = static_cast<float>(LocalRandom(0.0, 360.0));
else if (RotationMode == functions_panel::FIXED)
yaw = FixedRotation;
auto const directive = editor_includes::directive(File, info, parameters, context.to_local(location), yaw);
auto const instance = scene::Layers.place(layer, File, directive);
if (instance == 0)
{
ui()->set_status(format(STR_C("Template \"%s\" can't be placed in the active layer"), File.c_str()), true);
return;
}
// show the models of the template right away. the rest of it comes into play when the saved scenery is loaded
auto const preview = simulation::State.preview_include(directive, context, layer, instance);
EditorSnapshot snap;
snap.instance = instance;
snap.node_name = File;
m_history.push_back(std::move(snap));
g_redo.clear();
select_include(instance);
ui()->set_status(format(STR_C("\"%s\" placed in layer \"%s\", models shown: %d"), info.name.c_str(), scene::Layers.layer(layer).name.c_str(), static_cast<int>(preview.first)) +
(preview.second > 0 ? format(STR_C(", other statements of the template, working once the scenery is saved and loaded again: %d"), static_cast<int>(preview.second)) : std::string{}));
}
void editor_mode::export_scenery()
{
// hiding a layer clears the visibility flag of its nodes, and the flag is a part of exported node data.
// show the hidden layers for the duration of the export, so the files get the visibility defined in the scenery
auto const hiddenlayers = scene::Layers.hidden();
for (auto const layer : hiddenlayers)
scene::Layers.visible(layer, true);
simulation::State.export_as_text(Global.SceneryFile);
for (auto const layer : hiddenlayers)
scene::Layers.visible(layer, false);
}
void editor_mode::handle_terrain_brush(double Deltatime)
{
// the ground itself along the ray through the cursor in this frame
glm::dvec3 world;
if (false == terrain_cursor(world))
return;
// only when the cursor is over the terrain
if (terrain_at(world.x, world.z) == nullptr)
return;
double const rate = m_terrain_brush_strength * Deltatime;
bool shaped = false;
switch (m_terrain_tool)
{
case terrain_tool::sculpt:
shaped = m_streamer.sculpt(world.x, world.z, m_terrain_brush_radius, Global.shiftState ? -rate : rate);
break;
case terrain_tool::smooth:
shaped = m_streamer.smooth(world.x, world.z, m_terrain_brush_radius, std::min(1.0, rate * 0.5));
break;
case terrain_tool::level:
{
if (false == m_terrain_stroke && m_terrain_target_from_stroke)
{
double height;
if (m_streamer.height_at(world.x, world.z, height))
m_terrain_target = static_cast<float>(height);
}
m_terrain_stroke = true;
shaped = m_streamer.level(world.x, world.z, m_terrain_brush_radius, m_terrain_target, std::min(1.0, rate * 0.5), m_terrain_level_mode);
break;
}
case terrain_tool::paint:
m_streamer.paint(world.x, world.z, m_terrain_brush_radius, std::min(1.0, rate * 0.25), Global.shiftState ? std::uint16_t{0} : static_cast<std::uint16_t>(m_terrain_layer));
break;
case terrain_tool::restore:
shaped = m_streamer.restore_stitching(world.x, world.z, m_terrain_brush_radius, std::min(1.0, rate * 0.5));
break;
default:
break;
}
if (shaped)
{
// what the road tools know about the ground is no longer true
ground_tiles.clear();
forget_ground_shapes();
}
}
bool editor_mode::cursor_on_ground(glm::dvec3 &World)
{
if (m_streamer.active() && terrain_cursor(World))
return true;
// no terrain under the ray: the depth read back under the cursor, as before, where it hit anything
if (false == GfxRenderer->Mouse_Hit())
return false;
World = Camera.Pos + GfxRenderer->Mouse_Position();
return true;
}
bool editor_mode::terrain_cursor(glm::dvec3 &World) const
{
// along the ray through the cursor as it is in this frame: the depth read back lags a frame behind
ImGuiIO const &io = ImGui::GetIO();
if (io.DisplaySize.x <= 0.0f || io.DisplaySize.y <= 0.0f)
return false;
auto const inverse = glm::inverse(editor_mode::projection_matrix(io.DisplaySize.x / io.DisplaySize.y) * GfxRenderer->Camera_View_Matrix());
glm::vec2 const ndc{io.MousePos.x / io.DisplaySize.x * 2.0f - 1.0f, 1.0f - io.MousePos.y / io.DisplaySize.y * 2.0f};
// two points of the ray, whatever the depth range of the projection
auto const point = [&](float const Depth) {
auto const clip = inverse * glm::vec4(ndc, Depth, 1.0f);
return glm::dvec3{glm::vec3{clip} / clip.w};
};
auto const direction = point(0.5f) - point(0.0f);
if (glm::length(direction) < 1e-9)
return false;
// the top view sees from 10 km behind the plane of the camera (the near plane of its projection): the ray starts there,
// so the ground higher than the camera is met too
auto const back{Global.EditorOrtho ? 10000.0 : 0.0};
auto const along{glm::normalize(direction)};
return m_streamer.raycast(GfxRenderer->Camera_Position() + point(0.0f) - along * back, along, 5000.0 + back, World);
}
void editor_mode::fill_terrain_area()
{
auto const &layers = m_streamer.manifest().layers;
if (m_paint_outline.size() < 3 || layers.empty())
return;
auto const chosen = layers[std::clamp(m_terrain_layer, 0, static_cast<int>(layers.size()) - 1)];
// the material turned by its own angle is another entry of the palette
auto const rotation = static_cast<float>(std::remainder(static_cast<double>(m_paint_rotation), 360.0));
auto const layer = chosen.rotation == rotation ? static_cast<std::uint16_t>(std::clamp(m_terrain_layer, 0, static_cast<int>(layers.size()) - 1))
: m_streamer.layer(chosen.material, chosen.size, rotation);
std::vector<glm::dvec2> outline;
for (auto const &point : m_paint_outline)
outline.emplace_back(point.x, point.z);
auto const filled = m_streamer.fill(outline, m_paint_feather, layer);
m_terrain_status = filled ? format(STR_C("Area filled with %s"), chosen.material.c_str()) : std::string(STR_C("Nothing filled: no chunk under the outline, or no room for another material"));
if (filled)
m_paint_outline.clear();
}
void editor_mode::remove_selected_chunks()
{
// one step of the history, as the terrain changed in a frame goes
std::size_t removed{0};
for (auto const &key : m_terrain_selection)
if (m_streamer.exists(key))
{
m_streamer.remove_chunk(key.first, key.second);
++removed;
}
m_terrain_selection.clear();
if (removed > 0)
{
ground_tiles.clear();
forget_ground_shapes();
}
m_terrain_status = format(STR_P("%zu chunk removed, Ctrl+Z brings it back", "%zu chunks removed, Ctrl+Z brings them back", removed), removed);
}
void editor_mode::convert_selected_chunks()
{
// with 0.5 m spacing the paint can be as fine, the other spacings keep a sample per metre
auto const paint = m_terrain_convert_paint ? (m_terrain_convert_spacing < 0.75f ? heightmap::default_paint_samples * 2 : heightmap::default_paint_samples) : 0;
std::size_t converted = 0;
for (auto const &key : m_terrain_selection)
if (m_streamer.spacing(key.first, key.second, m_terrain_convert_spacing, paint))
++converted;
if (converted > 0)
{
ground_tiles.clear();
forget_ground_shapes();
}
m_terrain_status = format(STR_C("Chunks converted to %s: %zu of %zu"), spacing_label(m_terrain_convert_spacing), converted, m_terrain_selection.size());
}
bool editor_mode::pick_terrain_target()
{
glm::dvec3 world;
if (false == cursor_on_ground(world))
return false; // nothing under the cursor
m_terrain_target = static_cast<float>(world.y);
m_terrain_status = format(STR_C("Target height: %.2f m"), m_terrain_target);
return true;
}
void editor_mode::capture_terrain()
{
TAnimModel *model = dynamic_cast<TAnimModel *>(m_node);
if (model == nullptr || model->pModel == nullptr)
{
WriteLog("Editor: select a model instance to capture as terrain", logtype::generic);
return;
}
if (false == ensure_terrain())
return;
std::vector<world_triangle> tris;
gather_submodel_triangles(model->pModel->Root, instance_matrix(*model), tris);
if (tris.empty())
{
WriteLog("Editor: selected model has no readable geometry to capture", logtype::generic);
return;
}
// horizontal bounds of the captured geometry
glm::dvec2 lo(std::numeric_limits<double>::max());
glm::dvec2 hi(-std::numeric_limits<double>::max());
for (auto const &t : tris)
for (auto const &p : t)
{
lo = glm::min(lo, glm::dvec2(p.x, p.z));
hi = glm::max(hi, glm::dvec2(p.x, p.z));
}
triangle_grid const surface(std::move(tris), lo - glm::dvec2(1.0), hi + glm::dvec2(1.0));
// the chunks under the model are made where missing, then take its shape wherever it has any
auto const first = heightmap::chunk_at(lo.x, lo.y);
auto const last = heightmap::chunk_at(hi.x, hi.y);
for (int cz = first.second; cz <= last.second; ++cz)
for (int cx = first.first; cx <= last.first; ++cx)
add_chunk_from_ground({cx, cz}, m_terrain_spacing);
m_streamer.reshape(lo, hi, [&](double X, double Z, float &Height) {
double y;
if (false == surface.height_at(X, Z, std::numeric_limits<double>::max(), y))
return false;
Height = static_cast<float>(y);
return true;
});
ground_tiles.clear();
forget_ground_shapes();
// remove the original instance (recorded as a deletion so it can be undone)
std::string as_text;
model->export_as_text(as_text);
push_snapshot(model, EditorSnapshot::Action::Delete, as_text);
m_node = nullptr;
m_dragging = false;
ui()->set_node(nullptr);
erase_model(model);
}
// the ground under a point the cursor landed on. the orthophoto laid over the ground catches the cursor like any
// other surface, so the point can hang over the ground by as much as the orthophoto is lifted, or lie on the flat sheet
// the orthophoto makes where it's not fitted to the ground. what's put in the scenery is to stand on the ground itself
glm::dvec3 editor_mode::placement_on_ground(glm::dvec3 Location)
{
return ground_at(Location);
}
glm::dvec3 editor_mode::ground_at(glm::dvec3 Location) const
{
double constexpr reach = 2.0; // the ground is asked about this far around the point
double constexpr slack = 0.5; // a surface this far over the point still is what the cursor landed on
double constexpr height_tolerance = 50.0; // with nothing there or lower, how far up the ground is looked for
float constexpr terrain_model_radius = 50.0f; // the same models count as ground as for the area fill and the orthophoto
if (false == std::isfinite(Location.x) || false == std::isfinite(Location.y) || false == std::isfinite(Location.z))
return Location;
glm::dvec2 const bmin{Location.x - reach, Location.z - reach};
glm::dvec2 const bmax{Location.x + reach, Location.z + reach};
std::vector<world_triangle> triangles;
gather_ground_triangles(bmin, bmax, true, terrain_model_radius, triangles);
triangle_grid const ground(std::move(triangles), bmin, bmax);
auto const terrains = active_terrains();
for (double const ceiling : {Location.y + slack, Location.y + height_tolerance})
{
double y = -std::numeric_limits<double>::max();
bool found = ground.height_at(Location.x, Location.z, ceiling, y);
for (editor_terrain *terrain : terrains)
{
if (!terrain->contains(Location.x, Location.z))
continue;
double const h = terrain->height_at(Location.x, Location.z);
if (h <= ceiling && (!found || h > y))
{
y = h;
found = true;
}
}
if (found)
{
Location.y = y;
break;
}
}
// with no ground found the point stays where the cursor landed
return Location;
}
// puts every instance of the model the selected node shows on the ground under it
void editor_mode::drop_model_instances()
{
auto *selected = dynamic_cast<TAnimModel *>(m_node);
if (selected == nullptr || selected->Model() == nullptr)
{
ui()->set_status(STR("Select a model in the scene first"), true);
return;
}
if (selected->radius() >= 50.0f)
{
// it'd be put on top of itself
ui()->set_status(format(STR_C("\"%s\" is large enough to count as ground itself, so there's nothing to put it on"), selected->Model()->NameGet().c_str()), true);
return;
}
std::vector<TAnimModel *> instances;
std::vector<glm::dvec3> points;
int locked = 0;
for (auto *instance : simulation::Instances.sequence())
{
if (instance == nullptr || instance->Model() != selected->Model())
continue;
if (instance->from_template() || false == scene::Layers.editable(instance))
{
// placed by a template, or on a layer which is hidden or locked
++locked;
continue;
}
instances.push_back(instance);
points.push_back(instance->location());
}
// the ground the road tools use: terrain shapes, large models and the terrain made in the editor, without the roads and the orthophoto.
// a point with nothing under it comes back with the height it has
auto const heights = ground_heights(points, true);
int moved = 0;
for (std::size_t idx = 0; idx < instances.size(); ++idx)
{
if (std::abs(heights[idx] - points[idx].y) < 0.001)
continue;
auto *instance = instances[idx];
// one step of the history for each instance
push_snapshot(instance, EditorSnapshot::Action::Move);
auto location = points[idx];
location.y = heights[idx];
simulation::Region->erase(instance);
instance->location(location);
simulation::Region->insert(instance);
instance->mark_dirty();
++moved;
}
std::string status = format(STR_C("%d of %d copies of \"%s\" put on the ground"), moved, static_cast<int>(instances.size()), selected->Model()->NameGet().c_str());
if (moved > 0)
status += STR_C("; Ctrl+Z takes them back one at a time");
if (locked > 0)
status += format(STR_C(". Left alone, placed by a template or on a layer which can't be edited: %d"), locked);
ui()->set_status(status);
}
void editor_mode::read_georeference()
{
if (m_georeference_read)
return;
m_georeference_read = true;
m_georeference = false;
m_georeference_line.clear();
// the name of the scenery is kept in lower case, the file may have capitals in its name
std::filesystem::path file{Global.asCurrentSceneryPath + Global.SceneryFile};
std::error_code error;
if (false == std::filesystem::exists(file, error))
{
for (std::filesystem::directory_iterator entry{Global.asCurrentSceneryPath, error}, end; entry != end; entry.increment(error))
{
std::string name;
try
{
name = entry->path().filename().string();
}
catch (std::exception const &)
{
continue;
}
std::transform(name.begin(), name.end(), name.begin(), [](unsigned char const Character) { return static_cast<char>(std::tolower(Character)); });
if (name == Global.SceneryFile)
{
file = entry->path();
break;
}
}
}
std::ifstream stream(file);
std::string line;
for (int count = 0; count < 200 && std::getline(stream, line); ++count)
{
auto const mark = line.find("//$g");
if (mark == std::string::npos)
continue;
// Rainsted writes the origin of the scenery as: //$g <system> <easting> <northing>, in kilometres
std::istringstream words(line.substr(mark + 4));
std::string system;
double east{0.0};
double north{0.0};
if (false == static_cast<bool>(words >> system >> east >> north) || system.find("1992") == std::string::npos)
continue;
auto const scale = (east < 10000.0 && north < 10000.0) ? 1000.0 : 1.0;
m_georeference_origin = {north * scale, east * scale};
m_georeference_line = line.substr(mark);
if (false == m_georeference_line.empty() && m_georeference_line.back() == '\r')
m_georeference_line.pop_back();
m_georeference = true;
break;
}
}
void editor_mode::render_map_menu()
{
if (false == ImGui::BeginMenu(STR_C("Map")))
return;
if (ImGui::MenuItem(STR_C("Minimap"), "Ctrl+M", m_minimap.is_open()))
m_minimap.toggle();
ImGui::Separator();
bool enabled = m_orthophoto.enabled();
if (ImGui::MenuItem(STR_C("Orthophoto"), nullptr, enabled))
choose_orthophoto(false == enabled);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Aerial imagery of geoportal.gov.pl under the scenery, laid out by the origin of the scenery"));
// the settings among the base layers in the layers window
if (ImGui::MenuItem(STR_C("Orthophoto settings..."), nullptr, orthophoto_settings_open()))
{
ui()->show_layers();
m_orthophoto_window = true;
}
ImGui::EndMenu();
}
bool editor_mode::orthophoto_settings_open() const
{
// drawn open in the layers window in this frame or in the last one
return ImGui::GetCurrentContext() != nullptr && ImGui::GetFrameCount() - m_orthophoto_frame <= 1;
}
void editor_mode::render_base_layers()
{
auto &stored{EditorSettings.wms_layers()};
bool save{false};
// the WMS layers of the user's, the top one first, each drawn over the ones under it
int moved{-1};
int removed{-1};
for (int i = 0; i < static_cast<int>(stored.size()); ++i)
{
auto &entry{stored[static_cast<std::size_t>(i)]};
ImGui::PushID(i + 1);
save |= ImGui::Checkbox("##visible", &entry.visible);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Show or hide the layer"));
ImGui::SameLine();
if (ImGui::TreeNode("##wms", "%s", entry.title.empty() ? entry.layers.c_str() : entry.title.c_str()))
{
ImGui::PushTextWrapPos(0.0f);
ImGui::TextDisabled("%s", entry.url.c_str());
ImGui::TextDisabled(STR_C("Layers: %s"), entry.layers.c_str());
ImGui::PopTextWrapPos();
ImGui::SetNextItemWidth(160.0f);
ImGui::SliderFloat(STR_C("Opacity"), &entry.opacity, 0.0f, 1.0f, "%.2f");
save |= ImGui::IsItemDeactivatedAfterEdit();
if (false == entry.terms.empty())
{
ImGui::TextUnformatted(STR_C("Terms of the service:"));
ImGui::PushTextWrapPos(0.0f);
ImGui::TextDisabled("%s", entry.terms.c_str());
ImGui::PopTextWrapPos();
}
if (i > 0)
{
if (ImGui::SmallButton(STR_C("Up")))
moved = i - 1;
ImGui::SameLine();
}
if (i + 1 < static_cast<int>(stored.size()))
{
if (ImGui::SmallButton(STR_C("Down")))
moved = i;
ImGui::SameLine();
}
if (ImGui::SmallButton(STR_C("Remove")))
removed = i;
ImGui::TreePop();
}
ImGui::PopID();
}
// a layer swapped with the one under it, or one taken away; the ones of the view go along
if (moved >= 0 && moved + 1 < static_cast<int>(stored.size()))
{
std::swap(stored[static_cast<std::size_t>(moved)], stored[static_cast<std::size_t>(moved) + 1]);
if (moved + 1 < static_cast<int>(m_wms.size()))
std::swap(m_wms[static_cast<std::size_t>(moved)], m_wms[static_cast<std::size_t>(moved) + 1]);
save = true;
}
if (removed >= 0)
{
stored.erase(stored.begin() + removed);
if (removed < static_cast<int>(m_wms.size()))
{
m_wms[static_cast<std::size_t>(removed)]->enabled(false);
m_wms[static_cast<std::size_t>(removed)]->detach_scene();
m_wms.erase(m_wms.begin() + removed);
}
save = true;
}
if (save)
EditorSettings.save();
// the orthophoto at the bottom, its settings opened here from the map menu too
ImGui::PushID("orthophoto");
bool enabled{m_orthophoto.enabled()};
if (ImGui::Checkbox("##visible", &enabled))
choose_orthophoto(enabled);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Aerial imagery of geoportal.gov.pl under the scenery, laid out by the origin of the scenery"));
ImGui::SameLine();
if (m_orthophoto_window)
{
ImGui::SetNextItemOpen(true);
m_orthophoto_window = false;
}
if (ImGui::TreeNode("##settings", "%s", STR_C("Orthophoto (geoportal.gov.pl)")))
{
m_orthophoto_frame = ImGui::GetFrameCount();
render_orthophoto_ui();
ImGui::TreePop();
}
ImGui::PopID();
// the terrain of the model under the tracks laid, as the terrain panel has it too
if (ImGui::Checkbox(STR_C("Terrain of the model right away while laying"), &m_nmt_state.grow))
{
EditorSettings.terrain_while_laying(m_nmt_state.grow);
EditorSettings.save();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Where the terrain of the scenery ends, the terrain model is made under the tracks as they're laid, around the cursor from the first point placed; "
"the chunks there are stay as they are. Off, it's made only when asked for"));
render_wms_adding();
}
void editor_mode::render_wms_adding()
{
auto &adding{m_wms_adding};
ImGui::PushID("wmsadding");
if (false == ImGui::TreeNode("##add", "%s", STR_C("Add a WMS layer")))
{
ImGui::PopID();
return;
}
ImGui::PushTextWrapPos(0.0f);
ImGui::TextDisabled("%s", STR_C("The address of a WMS service drawing in PUWG 1992 (EPSG:2180). Its tiles are kept in memory only and asked for only around the camera; "
"what the service allows is up to you, its terms are shown below"));
ImGui::PopTextWrapPos();
ImGui::SetNextItemWidth(-1.0f);
auto const entered{ImGui::InputTextWithHint("##url", "https://...", adding.url.data(), adding.url.size(), ImGuiInputTextFlags_EnterReturnsTrue)};
std::string const url{adding.url.data()};
if ((ImGui::Button(STR_C("Read the layers")) || entered) && false == adding.reading.valid() && false == url.empty())
{
adding.info.reset();
adding.ticked.clear();
adding.service = url;
adding.reading = std::async(std::launch::async, [url, closing = &adding.closing]() { return editor_orthophoto::describe(url, [closing]() { return closing->load(); }); });
}
if (adding.reading.valid())
{
if (adding.reading.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
adding.info = adding.reading.get();
adding.ticked.assign(adding.info->layers.size(), 0);
}
else
ImGui::TextDisabled("%s", STR_C("Reading the capabilities of the service..."));
}
if (adding.info.has_value())
{
auto const &info{*adding.info};
ImGui::PushTextWrapPos(0.0f);
if (false == info.error.empty())
ImGui::TextColored(ImVec4(1.0f, 0.45f, 0.4f, 1.0f), STR_C("The service can't be read: %s"), info.error.c_str());
if (false == info.title.empty())
ImGui::TextUnformatted(info.title.c_str());
if (false == info.terms.empty())
{
ImGui::TextUnformatted(STR_C("Terms of the service:"));
ImGui::TextDisabled("%s", info.terms.c_str());
}
if (false == info.puwg && false == info.layers.empty())
ImGui::TextColored(ImVec4(1.0f, 0.75f, 0.2f, 1.0f), "%s", STR_C("The service doesn't draw in PUWG 1992 (EPSG:2180), the editor can't lay it out"));
ImGui::PopTextWrapPos();
if (false == info.layers.empty())
{
ImGui::SetNextItemWidth(-1.0f);
ImGui::InputTextWithHint("##filter", STR_C("Filter the layers"), adding.filter.data(), adding.filter.size());
auto const filter{ToLower(adding.filter.data())};
ImGui::BeginChild("##layers", ImVec2(0.0f, std::min(200.0f, ImGui::GetFrameHeightWithSpacing() * static_cast<float>(info.layers.size()) + 4.0f)), true);
for (std::size_t i = 0; i < info.layers.size(); ++i)
{
auto const &[name, title]{info.layers[i]};
auto const label{title.empty() || title == name ? name : title + " (" + name + ")"};
if (false == filter.empty() && ToLower(label).find(filter) == std::string::npos)
continue;
bool ticked{adding.ticked[i] != 0};
ImGui::PushID(static_cast<int>(i));
if (ImGui::Checkbox(label.c_str(), &ticked))
adding.ticked[i] = ticked ? 1 : 0;
ImGui::PopID();
}
ImGui::EndChild();
std::string layers;
for (std::size_t i = 0; i < info.layers.size(); ++i)
if (adding.ticked[i] != 0)
layers += (layers.empty() ? "" : ",") + info.layers[i].first;
auto const ready{info.puwg && false == layers.empty()};
if (false == ready)
{
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
}
auto const add{ImGui::Button(STR_C("Add")) && ready};
if (false == ready)
{
ImGui::PopStyleVar();
ImGui::PopItemFlag();
}
if (add)
{
editorSettings::wms_layer entry;
entry.url = adding.service;
entry.layers = layers;
entry.title = info.title.empty() ? layers : info.title;
entry.terms = info.terms;
// on top of the others
EditorSettings.wms_layers().insert(EditorSettings.wms_layers().begin(), entry);
EditorSettings.save();
adding.info.reset();
adding.ticked.clear();
adding.url.fill('\0');
adding.filter.fill('\0');
}
}
}
ImGui::TreePop();
ImGui::PopID();
}
void editor_mode::update_wms_layers()
{
auto const &stored{EditorSettings.wms_layers()};
// a layer added or removed: the ones of the view made again, as the settings have them
auto same{m_wms.size() == stored.size()};
for (std::size_t i = 0; same && i < stored.size(); ++i)
same = m_wms[i]->settings().service == stored[i].url && m_wms[i]->settings().layers == stored[i].layers;
if (false == same)
{
for (auto &layer : m_wms)
{
layer->enabled(false);
layer->detach_scene();
}
m_wms.clear();
for (auto const &entry : stored)
{
auto layer{std::make_unique<editor_orthophoto>()};
auto config{layer->settings()};
config.service = entry.url;
config.layers = entry.layers;
config.hires = false;
config.year = 0;
config.in_scene = false;
layer->settings(config);
ground_sources(*layer);
m_wms.push_back(std::move(layer));
}
}
// laid out by the origin of the scenery and onto the ground as the orthophoto is, around the camera as far as it
load_orthophoto_settings();
auto const &base{m_orthophoto.settings()};
auto const placed{base.north != 0.0 || base.east != 0.0};
for (std::size_t i = 0; i < m_wms.size(); ++i)
{
auto &layer{*m_wms[i]};
auto config{layer.settings()};
if (config.north != base.north || config.east != base.east || config.radius != base.radius || config.drape != base.drape || config.lift != base.lift || config.height != base.height ||
config.opacity != stored[i].opacity)
{
config.north = base.north;
config.east = base.east;
config.radius = base.radius;
config.drape = base.drape;
config.lift = base.lift;
config.height = base.height;
config.opacity = stored[i].opacity;
layer.settings(config);
}
layer.enabled(stored[i].visible && placed);
layer.update(Camera.Pos);
}
}
void editor_mode::render_view_menu()
{
if (false == ImGui::BeginMenu(STR_C("View")))
return;
if (ImGui::MenuItem(STR_C("Top view, orthographic"), "O", Global.EditorOrtho))
toggle_ortho();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The view straight down without perspective, as on a map; again for the camera as it was"));
if (ImGui::BeginMenu(STR_C("Tracks in the top view")))
{
render_top_tracks_choice(false);
ImGui::EndMenu();
}
if (ImGui::BeginMenu(STR_C("Track colours")))
{
char const *const names[]{"Plain", "By the radius", "By the speed", "By the grade"};
for (int i = 0; i < 4; ++i)
if (ImGui::MenuItem(STR_C(names[i]), nullptr, static_cast<int>(m_track_colouring) == i))
m_track_colouring = static_cast<track_colouring>(i);
ImGui::EndMenu();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The tracks of the track mode coloured by their radius, speed or grade, with the legend in the corner"));
if (ImGui::BeginMenu(STR_C("Camera speed")))
{
render_camera_settings();
ImGui::EndMenu();
}
ImGui::MenuItem(STR_C("Track labels in the top view"), nullptr, &m_track_labels);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Zoomed in from above, the curves get their radius and length and the switches their name and type, as on a plan; where they'd crowd, some are left out"));
ImGui::MenuItem(STR_C("Show lanes"), nullptr, &m_roadtool.lanes);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The lanes of the roads drawn as the paths the vehicles take"));
if (ImGui::MenuItem(STR_C("Cursor drawn into the picture (streams)"), nullptr, m_draw_cursor))
{
m_draw_cursor = !m_draw_cursor;
ui_layer::draw_cursor(m_draw_cursor);
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The pointer drawn by the editor into the frame instead of the one of the system: seen by those watching a stream\n"
"or a recording of the window (Discord, OBS, the capture of the system), which leave the system pointer out"));
if (ImGui::MenuItem(STR_C("Structure gauge"), nullptr, m_gauge.is_open()))
m_gauge.toggle();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Checks which models enter the structure gauge of the tracks and the clearance over the roads (skrajnia budowli)"));
ImGui::Separator();
ImGui::MenuItem(STR_C("Weather and time"), nullptr, &m_environment_open);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Time of day, day of the year, visibility, clouds and air temperature, as the scenery is seen in the editor"));
ImGui::EndMenu();
}
void editor_mode::render_environment_window()
{
if (false == m_environment_open)
return;
ImGui::SetNextWindowSize(ImVec2(420.0f * Global.ui_scale, 0.0f), ImGuiCond_FirstUseEver);
if (ImGui::Begin((std::string(STR_C("Weather and time")) + "###environment").c_str(), &m_environment_open, ImGuiWindowFlags_NoCollapse))
{
// what the setdatetime, setweather and settemperature commands do in the driver mode; the editor doesn't take commands
auto const set_date_time = [](int const Yearday, int const Minute) {
simulation::Time.set_time(Yearday, Minute);
auto const weather{Global.Weather};
simulation::Environment.compute_season(Yearday);
if (weather != Global.Weather)
{
// HACK: force re-calculation of precipitation, as the command does
Global.Overcast = std::clamp(Global.Overcast - 0.0001f, 0.0f, 2.0f);
}
simulation::Environment.update_moon();
};
// the names of the period, season and weather end with a colon, being keys of other lookups
auto const name = [](std::string Text) {
if (false == Text.empty() && Text.back() == ':')
Text.pop_back();
return Text;
};
auto const &time{simulation::Time.data()};
auto minute{time.wHour * 60 + time.wMinute};
auto const clock{format("%02d:%02d (%s)", time.wHour, time.wMinute, name(Global.Period).c_str())};
if (ImGui::SliderInt((std::string(STR_C("Time of day")) + "###timeofday").c_str(), &minute, 0, 1439, clock.c_str()))
set_date_time(static_cast<int>(std::round(Global.fMoveLight)), std::clamp(minute, 0, 1439));
auto yearday{Global.fMoveLight};
auto const day{format("%.0f (%s)", Global.fMoveLight, name(Global.Season).c_str())};
if (ImGui::SliderFloat((std::string(STR_C("Day of the year")) + "###dayofyear").c_str(), &yearday, 0.0f, 364.0f, day.c_str()))
set_date_time(static_cast<int>(std::round(std::clamp(yearday, 0.0f, 365.0f))), time.wHour * 60 + time.wMinute);
ImGui::Separator();
auto visibility{std::log(Global.fFogEnd)};
auto const fog{format("%.0f m", Global.fFogEnd)};
if (ImGui::SliderFloat((std::string(STR_C("Visibility")) + "###visibility").c_str(), &visibility, std::log(10.0f), std::log(50000.0f), fog.c_str()))
{
Global.fFogEnd = std::clamp(std::exp(visibility), 10.0f, 50000.0f);
simulation::Environment.compute_weather();
}
auto overcast{Global.Overcast};
auto const clouds{format("%.2f (%s)", Global.Overcast, name(Global.Weather).c_str())};
if (ImGui::SliderFloat((std::string(STR_C("Clouds")) + "###overcast").c_str(), &overcast, 0.0f, 2.0f, clouds.c_str()))
{
Global.Overcast = std::clamp(overcast, 0.0f, 2.0f);
simulation::Environment.compute_weather();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Up to 1 the sky clouds over, past it rain or snow falls, by the air temperature"));
auto temperature{Global.AirTemperature};
auto const air{format("%.1f °C", Global.AirTemperature)};
if (ImGui::SliderFloat((std::string(STR_C("Air temperature")) + "###airtemperature").c_str(), &temperature, -35.0f, 40.0f, air.c_str()))
{
Global.AirTemperature = std::clamp(temperature, -35.0f, 40.0f);
simulation::Environment.compute_weather();
}
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_TextDisabled));
ImGui::TextWrapped("%s", STR_C("Seen in the editor only; the scenery keeps its own time and weather"));
ImGui::PopStyleColor();
}
ImGui::End();
}
void editor_mode::render_new_scenery_popup()
{
if (m_newscenery_asked)
{
ImGui::OpenPopup(STR_C("New scenery##restart"));
m_newscenery_asked = false;
}
if (false == ImGui::BeginPopupModal(STR_C("New scenery##restart"), nullptr, ImGuiWindowFlags_AlwaysAutoResize))
return;
ImGui::TextUnformatted(STR_C("The editor starts again, with the wizard of a new scenery."));
auto const editsession{false == scene::Layers.empty()};
if (false == m_history.empty())
ImGui::TextColored(ImVec4(1.0f, 0.7f, 0.3f, 1.0f), "%s", STR_C("What isn't saved of this scenery is lost."));
if (editsession && ImGui::Button(STR_C("Save and start the wizard")))
{
if (save() && false == restart_for_new_scenery())
ui()->set_status(STR_C("The editor couldn't be started again"), true);
ImGui::CloseCurrentPopup();
}
if (editsession)
ImGui::SameLine();
if (ImGui::Button(STR_C("Start the wizard")))
{
if (false == restart_for_new_scenery())
ui()->set_status(STR_C("The editor couldn't be started again"), true);
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button(STR_C("Cancel")))
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
bool editor_mode::restart_for_new_scenery()
{
return restart_editor(std::string{});
}
bool editor_mode::restart_editor(std::string const &Scenery)
{
#ifdef _WIN32
_putenv("EU07_EDITOR_SELFTEST=");
wchar_t path[MAX_PATH];
if (GetModuleFileNameW(nullptr, path, MAX_PATH) == 0)
return false;
std::wstring command{L"\"" + std::wstring{path} + L"\" -edit"};
if (false == Scenery.empty())
command += L" \"" + std::filesystem::path(Scenery).wstring() + L"\"";
STARTUPINFOW startup{};
startup.cb = sizeof(startup);
PROCESS_INFORMATION process{};
if (false == CreateProcessW(nullptr, command.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &startup, &process))
return false;
CloseHandle(process.hThread);
CloseHandle(process.hProcess);
#else
unsetenv("EU07_EDITOR_SELFTEST");
char path[4096];
auto const size{readlink("/proc/self/exe", path, sizeof(path) - 1)};
if (size <= 0)
return false;
path[size] = '\0';
char edit[]{"-edit"};
std::string scenery{Scenery};
char *arguments[]{path, edit, scenery.empty() ? nullptr : scenery.data(), nullptr};
posix_spawnattr_t attributes;
posix_spawnattr_init(&attributes);
posix_spawnattr_setflags(&attributes, POSIX_SPAWN_SETSID);
pid_t child;
auto const result{posix_spawn(&child, path, nullptr, &attributes, arguments, environ)};
posix_spawnattr_destroy(&attributes);
if (result != 0)
return false;
#endif
WriteLog(Scenery.empty() ? std::string{"Editor: started again for a new scenery"} : "Editor: started again with scenery " + Scenery);
Application.queue_quit(true);
return true;
}
void editor_mode::render_open_scenery_popup()
{
auto const lower = [](std::string Text) {
std::transform(Text.begin(), Text.end(), Text.begin(), [](unsigned char const Character) { return static_cast<char>(std::tolower(Character)); });
return Text;
};
if (m_openscenery_asked)
{
m_openscenery_asked = false;
m_openscenery_list.clear();
m_openscenery_choice.clear();
std::error_code error;
for (std::filesystem::directory_iterator entry{Global.asCurrentSceneryPath, error}, end; entry != end; entry.increment(error))
{
std::string name;
try
{
name = entry->path().filename().string();
}
catch (std::exception const &)
{
continue;
}
std::error_code status;
if (entry->is_regular_file(status) && name.size() > 4 && lower(name.substr(name.size() - 4)) == ".scn" && name.front() != '$')
m_openscenery_list.emplace_back(name);
}
std::sort(m_openscenery_list.begin(), m_openscenery_list.end(), [&](std::string const &A, std::string const &B) { return lower(A) < lower(B); });
ImGui::OpenPopup(STR_C("Open scenery##restart"));
}
if (false == ImGui::BeginPopupModal(STR_C("Open scenery##restart"), nullptr, ImGuiWindowFlags_AlwaysAutoResize))
return;
ImGui::TextUnformatted(STR_C("The editor starts again with the scenery chosen below."));
ImGui::SetNextItemWidth(360.0f);
if (ImGui::IsWindowAppearing())
ImGui::SetKeyboardFocusHere();
ImGui::InputTextWithHint("##opensearch", STR_C("search"), m_openscenery_search, sizeof(m_openscenery_search));
std::string const text{lower(m_openscenery_search)};
ImGui::BeginChild("##openlist", ImVec2(360.0f, ImGui::GetTextLineHeightWithSpacing() * 14.0f), true);
for (auto const &name : m_openscenery_list)
{
if (false == text.empty() && lower(name).find(text) == std::string::npos)
continue;
if (ImGui::Selectable(name.c_str(), name == m_openscenery_choice, ImGuiSelectableFlags_AllowDoubleClick))
{
m_openscenery_choice = name;
if (ImGui::IsMouseDoubleClicked(0) && false == restart_editor(name))
ui()->set_status(STR_C("The editor couldn't be started again"), true);
}
}
ImGui::EndChild();
auto const editsession{false == scene::Layers.empty()};
if (false == m_history.empty())
ImGui::TextColored(ImVec4(1.0f, 0.7f, 0.3f, 1.0f), "%s", STR_C("What isn't saved of this scenery is lost."));
auto const chosen{false == m_openscenery_choice.empty()};
if (chosen && editsession && ImGui::Button(STR_C("Save and open")))
{
if (save() && false == restart_editor(m_openscenery_choice))
ui()->set_status(STR_C("The editor couldn't be started again"), true);
ImGui::CloseCurrentPopup();
}
if (chosen && editsession)
ImGui::SameLine();
if (chosen && ImGui::Button(STR_C("Open")))
{
if (false == restart_editor(m_openscenery_choice))
ui()->set_status(STR_C("The editor couldn't be started again"), true);
ImGui::CloseCurrentPopup();
}
if (chosen)
ImGui::SameLine();
if (ImGui::Button(STR_C("Cancel")))
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
void editor_mode::terrain_workspace(bool const Open)
{
m_terrain_open = Open;
if (false == Open)
m_terrain_tool = terrain_tool::none;
else if (m_streamer.active())
m_terrain_spacing = m_streamer.manifest().spacing;
}
editor_mode::texture_offer editor_mode::terrain_texture_offer(char const *None)
{
return {STR_C("In the palette and on the water of this terrain:"),
[this]() {
std::vector<texture_use> list;
auto const add = [&](std::string const &Name, char const *Note) {
if (Name.empty() || Name == "none" || std::any_of(list.begin(), list.end(), [&](texture_use const &Use) { return Use.name == Name; }))
return;
list.push_back({null_handle, Name, 0, Note});
};
for (auto const &layer : m_streamer.manifest().layers)
add(layer.material, STR_C("In the palette of the terrain"));
add(m_streamer.manifest().water_material, STR_C("The default water material"));
for (auto const &body : m_streamer.water())
add(body.material, STR_C("On a body of water"));
return list;
},
None, ""};
}
void editor_mode::render_terrain_tool_options()
{
// the orthophoto has its settings among the base layers in the layers window, see render_base_layers()
render_terrain_ui();
}
void editor_mode::render_object_menu()
{
if (ImGui::BeginMenu(STR_C("Objects")))
{
auto const *selected = dynamic_cast<TAnimModel const *>(m_node);
if (ImGui::MenuItem(STR_C("Put all instances of the selected model on the ground"), nullptr, false, selected != nullptr && selected->Model() != nullptr))
drop_model_instances();
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
ImGui::SetTooltip("%s", STR_C("Select a model in the scene. Every instance of the same model file is moved straight up or down\n"
"to the ground under it: terrain, large models and terrain made in the editor."));
ImGui::EndMenu();
}
}
void editor_mode::add_area_fill_point()
{
// the ground along the ray through the cursor in this frame
glm::dvec3 world;
if (false == cursor_on_ground(world))
return; // no ground under the cursor
// the outline gives the fill the height to look for the ground at, and the height of what it finds no ground for
m_fill_points.push_back(placement_on_ground(world));
}
void editor_mode::undo_last_area_fill()
{
// nullify_history_pointers() removes deleted nodes from m_fill_last, so work on a copy
auto const created = std::move(m_fill_last);
m_fill_last.clear();
for (TAnimModel *model : created)
{
if (model == nullptr)
continue;
if (m_node == model)
{
m_node = nullptr;
ui()->set_node(nullptr);
}
erase_model(model);
}
m_fill_status = format(STR_C("Removed %zu objects of the last fill"), created.size());
}
void editor_mode::run_area_fill()
{
// limits keeping a single click from freezing the editor for too long
std::size_t constexpr max_objects = 20000;
// large model instances (e.g. terrain tiles) are treated as ground, smaller ones (houses, trees) are not
float constexpr terrain_model_radius = 50.0f;
// ground is searched up to this far above the height interpolated from the outline points,
// so hills inside the outline are found but bridges or roofs high above it are not
double constexpr height_tolerance = 50.0;
if (m_fill_points.size() < 3)
{
m_fill_status = STR("Outline needs at least 3 points");
return;
}
// model package
std::vector<std::string> package;
if (m_fill_source.kind == model_set_ref::source::manual)
package = m_fill_custom;
else
for (auto const *entry : ui()->nodebank().set_entries(m_fill_source))
package.push_back(*entry);
package.erase(std::remove_if(package.begin(), package.end(), [](std::string const &Template) { return Template.empty(); }), package.end());
if (package.empty())
{
m_fill_status = STR("Selected model package is empty");
return;
}
// area bounds
glm::dvec2 bmin(std::numeric_limits<double>::max());
glm::dvec2 bmax(-std::numeric_limits<double>::max());
for (auto const &p : m_fill_points)
{
bmin = glm::min(bmin, glm::dvec2(p.x, p.z));
bmax = glm::max(bmax, glm::dvec2(p.x, p.z));
}
double const area = polygon_area_xz(m_fill_points);
auto const target = static_cast<std::size_t>(std::min<double>(std::round(area / 10000.0 * std::max(0.0f, m_fill_density)), max_objects));
if (target == 0)
{
m_fill_status = STR("Density too low for this area, nothing to place");
return;
}
// scatter points: random samples inside the outline, rejecting the ones closer than the minimal spacing
// to an already accepted point (checked on a hashed grid with cell size == spacing)
double const spacing = std::max(0.0f, m_fill_min_spacing);
std::unordered_map<std::int64_t, std::vector<std::size_t>> occupancy;
auto const cell_key = [](std::int64_t const X, std::int64_t const Z) { return (X << 32) ^ (Z & 0xffffffff); };
std::vector<glm::dvec2> points;
points.reserve(target);
for (std::size_t attempt = 0; attempt < target * 30 && points.size() < target; ++attempt)
{
glm::dvec2 const p(LocalRandom(bmin.x, bmax.x), LocalRandom(bmin.y, bmax.y));
if (false == point_in_polygon_xz(m_fill_points, p.x, p.y))
continue;
if (spacing > 0.0)
{
auto const cx = static_cast<std::int64_t>(std::floor(p.x / spacing));
auto const cz = static_cast<std::int64_t>(std::floor(p.y / spacing));
bool free = true;
for (std::int64_t dz = -1; dz <= 1 && free; ++dz)
for (std::int64_t dx = -1; dx <= 1 && free; ++dx)
{
auto const lookup = occupancy.find(cell_key(cx + dx, cz + dz));
if (lookup == occupancy.end())
continue;
for (auto const idx : lookup->second)
if (glm::distance(points[idx], p) < spacing)
{
free = false;
break;
}
}
if (!free)
continue;
occupancy[cell_key(cx, cz)].push_back(points.size());
}
points.push_back(p);
}
// ground geometry within the area
std::vector<world_triangle> triangles;
gather_ground_triangles(bmin, bmax, m_fill_models_as_ground, terrain_model_radius, triangles);
triangle_grid const ground(std::move(triangles), bmin, bmax);
auto const terrains = active_terrains();
auto const rotation_mode = m_fill_random_rotation ? functions_panel::RANDOM : ui()->rot_mode();
auto const fixed_rotation_value = ui()->rot_val();
float const scalemin = std::max(0.01f, std::min(m_fill_scale_min, m_fill_scale_max));
float const scalemax = std::max(0.01f, std::max(m_fill_scale_min, m_fill_scale_max));
std::vector<TAnimModel *> created;
created.reserve(points.size());
std::size_t unsupported = 0;
for (auto const &p : points)
{
// reference height: the outline points, weighted by inverse squared distance
double weights = 0.0, reference = 0.0;
for (auto const &v : m_fill_points)
{
double const w = 1.0 / std::max(1e-6, glm::length2(glm::dvec2(v.x, v.z) - p));
weights += w;
reference += w * v.y;
}
reference /= weights;
double const ceiling = reference + height_tolerance;
double y = -std::numeric_limits<double>::max();
bool found = ground.height_at(p.x, p.y, ceiling, y);
for (editor_terrain *terrain : terrains)
{
if (!terrain->contains(p.x, p.y))
continue;
double const h = terrain->height_at(p.x, p.y);
if (h <= ceiling && (!found || h > y))
{
y = h;
found = true;
}
}
if (!found)
{
y = reference;
++unsupported;
}
glm::dvec3 const location(p.x, y, p.y);
if (!simulation::Region->point_inside(location))
continue;
auto const &src = package[package.size() > 1 ? std::min(package.size() - 1, static_cast<std::size_t>(LocalRandom(0.0, static_cast<double>(package.size())))) : 0];
// NOTE: no names here either, same as with the brush
TAnimModel *cloned = simulation::State.create_model(src, std::string{}, location);
if (!cloned)
continue;
apply_rotation_for_new_node(cloned, rotation_mode, fixed_rotation_value);
if (scalemin != 1.0f || scalemax != 1.0f)
cloned->Scale(static_cast<float>(LocalRandom(scalemin, scalemax)));
created.push_back(cloned);
}
// a new fill replaces the "last fill" undo set; the previous objects stay in the scene
m_fill_last = std::move(created);
m_fill_status = format(STR_C("Placed %zu of %zu objects"), m_fill_last.size(), static_cast<std::size_t>(target));
if (points.size() < target)
m_fill_status += STR_C(" (limited by min. spacing)");
if (unsupported > 0)
m_fill_status += format(STR_C(", without ground under them (height from the outline): %zu"), unsupported);
WriteLog("Editor: area fill - " + m_fill_status, logtype::generic);
}
void editor_mode::draw_area_fill_outline() const
{
if (m_fill_points.empty())
return;
// same camera-relative view and clean perspective the transform gizmo uses
ImGuiIO const &io = ImGui::GetIO();
glm::mat4 const view = GfxRenderer->Camera_View_Matrix();
glm::dvec3 const camerapos = GfxRenderer->Camera_Position();
float const aspect = io.DisplaySize.y > 0.0f ? io.DisplaySize.x / io.DisplaySize.y : 1.0f;
glm::mat4 const viewprojection = editor_mode::projection_matrix(aspect) * view;
auto const to_clip = [&](glm::dvec3 const &Point) { return viewprojection * glm::vec4(glm::vec3(Point - camerapos), 1.0f); };
auto const to_screen = [&](glm::vec4 const &Clip) {
return ImVec2((Clip.x / Clip.w * 0.5f + 0.5f) * io.DisplaySize.x, (0.5f - Clip.y / Clip.w * 0.5f) * io.DisplaySize.y);
};
float constexpr nearw = 0.1f;
ImDrawList *drawlist = ImGui::GetBackgroundDrawList(ImGui::GetMainViewport());
ImU32 const edgecolor = IM_COL32(255, 200, 40, 230);
ImU32 const closingcolor = IM_COL32(255, 200, 40, 110);
std::size_t const count = m_fill_points.size();
for (std::size_t i = 0; i < count; ++i)
{
bool const closing = (i + 1 == count);
if (closing && count < 3)
break;
glm::vec4 a = to_clip(m_fill_points[i]);
glm::vec4 b = to_clip(m_fill_points[(i + 1) % count]);
// clip the edge against the near plane, so points behind the camera don't flip across the screen
if (a.w < nearw && b.w < nearw)
continue;
if (a.w < nearw)
a = glm::mix(a, b, (nearw - a.w) / (b.w - a.w));
else if (b.w < nearw)
b = glm::mix(b, a, (nearw - b.w) / (a.w - b.w));
drawlist->AddLine(to_screen(a), to_screen(b), closing ? closingcolor : edgecolor, 2.0f);
}
for (std::size_t i = 0; i < count; ++i)
{
glm::vec4 const c = to_clip(m_fill_points[i]);
if (c.w < nearw)
continue;
drawlist->AddCircleFilled(to_screen(c), i == 0 ? 6.0f : 4.0f, i == 0 ? IM_COL32(255, 90, 40, 255) : edgecolor);
}
}
void editor_mode::render_area_fill()
{
ImGui::TextDisabled(STR_C("LMB: add outline point Backspace: remove last point"));
double const area = polygon_area_xz(m_fill_points);
// the square sign written as its utf-8 bytes: the game is built without /utf-8 where it has the debug symbols
ImGui::Text(STR_C("Points: %zu Area: %.0f m\xC2\xB2 (%.2f ha)"), m_fill_points.size(), area, area / 10000.0);
if (ImGui::Button(STR_C("Remove last point")) && !m_fill_points.empty())
m_fill_points.pop_back();
ImGui::SameLine();
if (ImGui::Button(STR_C("Clear outline")))
m_fill_points.clear();
ImGui::Separator();
// model package: a hand-assembled list, a user-defined set or a node bank group
auto &bank = ui()->nodebank();
bank.set_combo(STR_C("Model set"), m_fill_source, STR_C("Custom list"));
if (m_fill_source.kind == model_set_ref::source::manual)
bank.manual_list("fillset", m_fill_custom, m_fill_custom_idx);
else
ImGui::TextDisabled(STR_C("%zu templates in set"), bank.set_entries(m_fill_source).size());
ImGui::Separator();
ImGui::SetNextItemWidth(120.0f);
ImGui::InputFloat(STR_C("Density (objects/ha)"), &m_fill_density, 10.0f, 100.0f, "%.1f");
m_fill_density = std::max(0.0f, m_fill_density);
ImGui::PushID("areafill");
if (ImGui::TreeNode(STR_C("More settings")))
{
ImGui::SetNextItemWidth(120.0f);
ImGui::InputFloat(STR_C("Min. spacing (m)"), &m_fill_min_spacing, 0.5f, 2.0f, "%.1f");
m_fill_min_spacing = std::max(0.0f, m_fill_min_spacing);
ImGui::SetNextItemWidth(200.0f);
ImGui::DragFloatRange2(STR_C("Scale"), &m_fill_scale_min, &m_fill_scale_max, 0.01f, 0.1f, 5.0f, "min %.2f", "max %.2f");
ImGui::Checkbox(STR_C("Random rotation"), &m_fill_random_rotation);
if (!m_fill_random_rotation)
ui()->render_rotation_controls();
ImGui::Checkbox(STR_C("Large models count as ground (terrain tiles)"), &m_fill_models_as_ground);
ImGui::TreePop();
}
ImGui::PopID();
ImGui::Text(STR_C("Estimated objects: %.0f"), std::round(area / 10000.0 * m_fill_density));
if (ImGui::Button(STR_C("Fill area")))
run_area_fill();
ImGui::SameLine();
std::string const undolabel = format(STR_C("Undo last fill (%zu)"), m_fill_last.size());
if (ImGui::Button(undolabel.c_str()) && !m_fill_last.empty())
undo_last_area_fill();
if (!m_fill_status.empty())
ImGui::TextUnformatted(m_fill_status.c_str());
}
glm::vec3 editor_mode::array_size(TAnimModel const &Base)
{
auto *model = Base.Model();
if (model == nullptr)
return glm::vec3{0.0f};
if (m_array.sized == model)
return m_array.size;
// the triangles the other tools take for the ground, left where the model has them
std::vector<world_triangle> triangles;
gather_submodel_triangles(model->Root, glm::dmat4{1.0}, triangles);
if (triangles.empty())
return glm::vec3{0.0f}; // not loaded yet, or nothing to it; it's asked about again the next time
glm::dvec3 low{std::numeric_limits<double>::max()};
glm::dvec3 high{-std::numeric_limits<double>::max()};
for (auto const &triangle : triangles)
{
for (auto const &vertex : triangle)
{
low = glm::min(low, vertex);
high = glm::max(high, vertex);
}
}
m_array.sized = model;
m_array.size = glm::vec3{high - low};
return m_array.size;
}
glm::dvec3 editor_mode::array_offset(TAnimModel const &Base)
{
auto const &settings = m_array.settings;
glm::dvec3 offset{0.0};
if (settings.relative)
offset += glm::dvec3{settings.relative_offset * array_size(Base) * Base.Scale()};
if (settings.constant)
offset += glm::dvec3{settings.constant_offset};
return offset;
}
int editor_mode::array_count(double const Step) const
{
auto const &settings = m_array.settings;
if (settings.fit == 0)
return std::clamp(settings.count, 1, array_tool::limit);
if (Step < array_tool::minimal_step)
return 1;
// the model, and a copy for each whole offset the length takes
return static_cast<int>(std::min<double>(array_tool::limit, std::floor(std::max(0.0f, settings.length) / Step + 1e-6) + 1.0));
}
std::vector<std::pair<glm::dvec3, glm::vec3>> editor_mode::array_placements(TAnimModel &Base)
{
// a model this large counts as ground itself, and would be put on top of its own copies
float constexpr terrain_model_radius = 50.0f;
std::vector<std::pair<glm::dvec3, glm::vec3>> places;
auto const &settings = m_array.settings;
auto const offset = array_offset(Base);
auto const count = array_count(glm::length(offset));
if (count < 2 || glm::length(offset) < array_tool::minimal_step)
return places;
// the offset goes along the axes of the model, which is turned the way the renderer does it: around y, then x, then z
auto angles = Base.Angles();
glm::dmat4 turned{1.0};
turned = glm::rotate(turned, glm::radians(static_cast<double>(angles.y)), glm::dvec3{0.0, 1.0, 0.0});
turned = glm::rotate(turned, glm::radians(static_cast<double>(angles.x)), glm::dvec3{1.0, 0.0, 0.0});
turned = glm::rotate(turned, glm::radians(static_cast<double>(angles.z)), glm::dvec3{0.0, 0.0, 1.0});
auto step = glm::dvec3{turned * glm::dvec4{offset, 0.0}};
auto location = Base.location();
places.reserve(count - 1);
for (int idx = 1; idx < count; ++idx)
{
// each copy is turned against the one before it, and leads to the next one the way it faces
location += step;
if (false == simulation::Region->point_inside(location))
break; // the row ends at the edge of the region, nodes past it are left out of the scene
if (settings.turn != 0.0f)
{
step = glm::rotateY(step, glm::radians(static_cast<double>(settings.turn)));
angles.y = clamp_circular(angles.y + settings.turn, 360.0f);
}
places.emplace_back(location, angles);
}
if (settings.ground && Base.radius() < terrain_model_radius)
{
// the ground the road tools use. a copy with nothing under it stays at the height the offset gave it
std::vector<glm::dvec3> points;
points.reserve(places.size() + 1);
points.push_back(Base.location());
for (auto const &place : places)
points.push_back(place.first);
std::vector<char> found;
auto const heights = ground_heights(points, true, &found);
if (found[0])
{
auto const clearance = points[0].y - heights[0];
for (std::size_t idx = 0; idx < places.size(); ++idx)
{
if (found[idx + 1])
places[idx].first.y = heights[idx + 1] + clearance;
}
}
}
return places;
}
bool editor_mode::array_live() const
{
// the model the array was made of is still selected, and the array is the last thing done
return m_array.base != nullptr && m_array.base == m_node && false == m_history.empty() && m_history.size() == m_array.step &&
m_history.back().action == EditorSnapshot::Action::Array;
}
void editor_mode::make_array()
{
auto *base = dynamic_cast<TAnimModel *>(m_node);
if (base == nullptr || false == scene::Layers.editable(base))
return;
auto const step = glm::length(array_offset(*base));
if (step < array_tool::minimal_step)
{
m_array.status = STR("The offsets come to nothing, the copies would sit in the model");
return;
}
if (array_count(step) < 2)
{
m_array.status = (m_array.settings.fit == 0 ? STR("An array of one is the model itself, nothing to make") : STR("The length takes no copy, nothing to make"));
return;
}
// a single undo step for the whole array, whatever is done to it while it follows the settings
trim_history();
EditorSnapshot snap;
snap.action = EditorSnapshot::Action::Array;
snap.node_name = base->name();
snap.position = base->location();
snap.layer = base->layer();
m_history.push_back(std::move(snap));
g_redo.clear();
m_array.base = base;
m_array.step = m_history.size();
shape_array();
}
void editor_mode::shape_array()
{
auto *base = m_array.base;
auto &copies = m_history.back().copies;
auto const places = array_placements(*base);
// the copies there's no place for anymore go
while (copies.size() > places.size())
{
auto *model = copies.back().model;
copies.pop_back();
if (model == nullptr)
continue;
erase_model(model);
}
std::string definition;
for (std::size_t idx = 0; idx < places.size(); ++idx)
{
TAnimModel *model = nullptr;
if (idx < copies.size())
{
model = copies[idx].model;
m_editor.translate(model, places[idx].first, true); // true: the height is set as well
}
else
{
if (definition.empty())
base->export_as_text(definition);
// NOTE: no names for the copies, same as with the brush
model = simulation::State.create_model(definition, std::string{}, places[idx].first);
if (model == nullptr)
break;
// the copies go where the model is, rather than to the active layer
scene::Layers.move(model, base->layer());
EditorSnapshot::array_copy copy;
copy.model = model;
copy.uuid = model->uuid;
copies.push_back(std::move(copy));
}
model->Angles(places[idx].second);
model->Scale(base->Scale());
}
m_array.applied = m_array.settings;
m_array.location = base->location();
m_array.angles = base->Angles();
m_array.scale = base->Scale();
if (copies.size() < places.size())
m_array.status = format(STR_C("Only %zu of %zu copies could be made"), copies.size(), places.size());
else if (copies.empty())
m_array.status = STR("No copies with these settings");
else
m_array.status = format(STR_C("The model and %zu copies; Ctrl+Z takes the array back"), copies.size());
}
void editor_mode::update_array()
{
if (m_array.base == nullptr)
return;
if (false == array_live())
{
// once left, the copies are models like any other
m_array.base = nullptr;
return;
}
auto const *base = m_array.base;
if (m_array.settings == m_array.applied && base->location() == m_array.location && base->Angles() == m_array.angles && base->Scale() == m_array.scale)
return;
shape_array();
}
void editor_mode::restore_array_snapshot(EditorSnapshot Snapshot, std::vector<EditorSnapshot> &Opposite, bool const Undo)
{
// either way the array is done with following the settings
m_array.base = nullptr;
for (auto &copy : Snapshot.copies)
{
if (Undo)
{
// NOTE: a copy which was deleted and brought back is another model by now, found by its uuid
auto *model = dynamic_cast<TAnimModel *>(find_node_by_any(copy.model, copy.uuid.to_string(), std::string{}));
copy.model = nullptr;
copy.serialized.clear();
if (model == nullptr)
continue; // deleted since, and stays that way
// the copy is brought back the way it is now, with whatever was done to it since the array was made
model->export_as_text(copy.serialized);
copy.name = model->name();
copy.position = model->location();
copy.rotation = model->Angles();
copy.scale = model->Scale();
if (m_node == model)
{
m_node = nullptr;
m_dragging = false;
ui()->set_node(nullptr);
}
erase_model(model);
}
else
{
if (copy.serialized.empty())
continue;
auto *model = simulation::State.create_model(copy.serialized, copy.name, copy.position);
if (model == nullptr)
continue;
scene::Layers.move(model, Snapshot.layer);
model->Angles(copy.rotation);
model->Scale(copy.scale);
model->uuid = copy.uuid;
add_to_hierarchy(model);
copy.model = model;
}
}
Opposite.push_back(std::move(Snapshot));
}
void editor_mode::render_array()
{
auto &settings = m_array.settings;
auto *selected = dynamic_cast<TAnimModel *>(m_node);
if (selected != nullptr && false == scene::Layers.editable(selected))
selected = nullptr;
ImGui::PushID("array");
ImGui::TextDisabled(STR_C("Copies of the selected model, set out in a row"));
ImGui::RadioButton(STR_C("Fixed count"), &settings.fit, 0);
ImGui::SameLine();
ImGui::RadioButton(STR_C("Fit length"), &settings.fit, 1);
ImGui::SetNextItemWidth(120.0f);
if (settings.fit == 0)
{
ImGui::InputInt(STR_C("Count"), &settings.count);
settings.count = std::clamp(settings.count, 1, array_tool::limit);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Number of the models in the array, the selected one included"));
}
else
{
ImGui::InputFloat(STR_C("Length (m)"), &settings.length, 1.0f, 10.0f, "%.2f");
settings.length = std::max(0.0f, settings.length);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("As many copies are made as fit in this length, measured along the row"));
}
ImGui::SetNextItemWidth(120.0f);
ImGui::DragFloat(STR_C("Turn per copy (deg)"), &settings.turn, 0.1f, -180.0f, 180.0f, "%.2f");
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Each copy is turned by this much around the vertical axis against the one before it,\n"
"and the row bends with them into an arc"));
if (ImGui::TreeNode(STR_C("More settings")))
{
ImGui::Checkbox(STR_C("Relative offset"), &settings.relative);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Distance between the copies as a part of the size of the model, along its axes: x, y (up), z.\n"
"1 along an axis puts the copies end to end"));
if (settings.relative)
{
ImGui::SetNextItemWidth(200.0f);
ImGui::DragFloat3(STR_C("x size"), &settings.relative_offset.x, 0.01f, 0.0f, 0.0f, "%.3f");
}
ImGui::Checkbox(STR_C("Constant offset"), &settings.constant);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Distance between the copies in metres, along the axes of the model: x, y (up), z.\n"
"With both offsets on the copies are set apart by their sum"));
if (settings.constant)
{
ImGui::SetNextItemWidth(200.0f);
ImGui::DragFloat3("m", &settings.constant_offset.x, 0.05f, 0.0f, 0.0f, "%.3f");
}
ImGui::Checkbox(STR_C("Follow the ground"), &settings.ground);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The copies are put as high over the ground as the model is, instead of level with it.\n"
"Not for the models large enough to count as ground themselves"));
ImGui::TreePop();
}
if (selected != nullptr)
{
auto const size = array_size(*selected) * selected->Scale();
auto const step = glm::length(array_offset(*selected));
ImGui::TextDisabled(STR_C("Model %.2f x %.2f x %.2f m, %d in the array, %.2f m apart"), size.x, size.y, size.z, array_count(step), step);
}
if (array_live())
{
// NOTE: kept away from the place of the other button, a click too many on which would make the array twice
ImGui::TextDisabled(STR_C("The array follows the settings"));
ImGui::SameLine();
if (ImGui::Button(STR_C("Done")))
m_array.base = nullptr;
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The copies stay the way they are. Selecting or changing anything else does the same"));
}
else if (selected != nullptr)
{
if (ImGui::Button(STR_C("Make array")))
make_array();
}
else
{
ImGui::TextDisabled(STR_C("Select a model to make an array of"));
}
if (false == m_array.status.empty())
ImGui::TextUnformatted(m_array.status.c_str());
ImGui::PopID();
}
void editor_mode::choose_edit_mode(nodebank_panel::edit_mode const Mode)
{
// an edit mode is the field of work instead of the tracks, roads or terrain
m_track_window_open = false;
m_roadtool.window = false;
terrain_workspace(false);
if (Mode != nodebank_panel::MODIFY)
m_selection.clear();
ui()->set_mode(Mode);
}
editor_mode::work_area editor_mode::current_work_area() const
{
// the terrain is shown over the tracks, see the tool options window
if (m_terrain_open)
return work_area::terrain;
if (m_track_window_open)
return work_area::tracks;
if (m_roadtool.window)
return work_area::roads;
return work_area::surroundings;
}
void editor_mode::choose_work_area(work_area const Area)
{
switch (Area)
{
case work_area::surroundings:
// the edit mode used last
choose_edit_mode(ui()->nodebank().mode);
break;
case work_area::tracks:
terrain_workspace(false);
show_track_tab(m_track_tab);
break;
case work_area::roads:
m_track_window_open = false;
terrain_workspace(false);
m_roadtool.window = true;
break;
case work_area::terrain:
m_track_window_open = false;
m_roadtool.window = false;
terrain_workspace(true);
break;
}
}
void editor_mode::render_workspaces()
{
struct area
{
char const *label;
work_area which;
editor_icons::icon picture;
char const *tooltip;
};
area const areas[] = {
{STR_C("Surroundings"), work_area::surroundings, editor_icons::icon::work_surroundings, STR_C("Models around the line: select, insert, brush, area fill, copy to the node bank")},
{STR_C("Tracks"), work_area::tracks, editor_icons::icon::work_tracks, STR_C("Tracks and switches, signals, kilometrage and posts along the track, checks of the line")},
{STR_C("Roads"), work_area::roads, editor_icons::icon::work_roads, STR_C("Roads and junctions, level crossings, the points where the traffic comes and goes")},
{STR_C("Terrain"), work_area::terrain, editor_icons::icon::work_terrain, STR_C("Heightmap terrain: chunks, sculpting, painting, water, orthophoto")},
};
char const *const keys[] = {"F2", "F3", "F4", "F5"};
auto const current{current_work_area()};
auto const &style{ImGui::GetStyle()};
auto *list{ImGui::GetWindowDrawList()};
// bigger and bold, with the font made for it if there's one, or drawn twice a pixel apart
auto *font{ui_layer::font_bold != nullptr ? ui_layer::font_bold : ImGui::GetFont()};
auto const fontsize{ui_layer::font_bold != nullptr ? ui_layer::font_bold->FontSize : ImGui::GetFontSize() * 1.25f};
auto const height{ImGui::GetWindowHeight()};
auto const padding{style.FramePadding.x * 3.0f};
// the icon of the field of work before its name
auto const iconside{editor_icons::size()};
auto const spacing{style.ItemInnerSpacing.x * 1.5f};
render_toolbar_caption(STR_C("Work modes"));
for (auto const &entry : areas)
{
auto const text{font->CalcTextSizeA(fontsize, FLT_MAX, 0.0f, entry.label)};
ImVec2 const size{text.x + iconside + spacing + padding * 2.0f, height};
auto const position{ImGui::GetCursorScreenPos()};
ImGui::PushID(static_cast<int>(entry.which));
if (ImGui::InvisibleButton("##area", size))
choose_work_area(entry.which);
ImGui::PopID();
auto const hovered{ImGui::IsItemHovered()};
auto const chosen{entry.which == current};
ImVec2 const end{position.x + size.x, position.y + size.y};
if (chosen || hovered)
list->AddRectFilled(position, end, ImGui::GetColorU32(chosen ? ImGuiCol_Header : ImGuiCol_HeaderHovered));
if (chosen)
list->AddRectFilled(ImVec2(position.x, end.y - 3.0f), end, ImGui::GetColorU32(ImGuiCol_CheckMark));
ImVec2 const textposition{position.x + padding + iconside + spacing, position.y + (height - text.y) * 0.5f};
auto const color{ImGui::GetColorU32(chosen ? ImGuiCol_Text : ImGuiCol_TextDisabled)};
editor_icons::draw(list, entry.picture, ImVec2(position.x + padding + iconside * 0.5f, position.y + height * 0.5f), color);
list->AddText(font, fontsize, textposition, color, entry.label);
if (ui_layer::font_bold == nullptr)
list->AddText(font, fontsize, ImVec2(textposition.x + 1.0f, textposition.y), color, entry.label);
if (hovered)
ImGui::SetTooltip("%s [%s]", entry.tooltip, keys[static_cast<int>(entry.which)]);
ImGui::SameLine(0.0f, style.ItemSpacing.x);
}
}
void editor_mode::render_toolbar()
{
// the modes of the gizmo and the top view are over the 3d view, see render_viewport_overlays()
render_toolbar_tools();
}
void editor_mode::render_edit_menu()
{
if (false == ImGui::BeginMenu(STR_C("Edit")))
return;
if (ImGui::MenuItem(STR_C("Undo"), "Ctrl+Z", false, false == m_history.empty()))
undo_last();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Takes the last change back"));
if (ImGui::MenuItem(STR_C("Redo"), "Ctrl+Y", false, false == g_redo.empty()))
redo_last();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Makes the change taken back again"));
ImGui::Separator();
auto const selected{m_node != nullptr};
if (ImGui::MenuItem(STR_C("Delete"), STR_C("Del"), false, selected || m_instance != 0))
delete_selected();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Deletes what's selected: a model, tracks, a road piece or point, a signal, an include of a scenery template"));
auto const models{dynamic_cast<TAnimModel *>(m_node) != nullptr && ui()->mode() != nodebank_panel::TRACK};
if (ImGui::MenuItem(STR_C("Copy"), "Ctrl+C", false, models))
copy_selection();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Copies the selected models with their groups"));
if (ImGui::MenuItem(STR_C("Paste"), "Ctrl+V", false, false == m_clipboard.empty()))
paste_clipboard(cursor_ground());
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Puts the copied models at the cursor, around it as they were around the selected one; Ctrl+RMB in the view pastes them where it was clicked"));
ImGui::Separator();
if (ImGui::MenuItem(STR_C("Group together"), "Ctrl+G", false, models && selected_nodes().size() > 1))
group_selection();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The selected models move together from now on; a click on one selects them all. Shift+LMB adds a model to the selection"));
// Delete above may have just dropped the node, selected is from before it
if (ImGui::MenuItem(STR_C("Ungroup"), "Ctrl+Shift+G", false, m_node != nullptr && m_node->group() > 1))
ungroup_selection();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The models of the group move each by itself again"));
if (ImGui::MenuItem(STR_C("Drop to the ground"), "End", false, selected))
drop_selection_to_ground();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Puts each selected model, also each model of their groups, down onto the terrain or the object under it"));
if (ImGui::MenuItem(STR_C("Fly to the selected"), "F", false, selected))
start_focus(m_node, 0.6);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The camera flies to the selected model and looks at it"));
if (ImGui::MenuItem(STR_C("Find a track by its name"), "Ctrl+F"))
{
terrain_workspace(false);
show_track_tab(m_track_window_open ? m_track_tab : track_tab::path);
m_track_search.focus = true;
}
if (ImGui::MenuItem(STR_C("Bend the selected along the track"), "B", false, ui()->mode() != nodebank_panel::TRACK))
bend_shortcut();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Bends the selected model, e.g. a platform edge or a fence, along the nearest track; the settings are in the inspector"));
ImGui::EndMenu();
}
void editor_mode::render_help_menu()
{
if (false == ImGui::BeginMenu(STR_C("Help")))
return;
ImGui::MenuItem(STR_C("Keyboard shortcuts"), "F1", &m_shortcuts_open);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The keys of the editor, for each field of work"));
ImGui::MenuItem(STR_C("Tutorial"), nullptr, &m_first_steps_open);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Short lessons: the camera, laying track, curves, the radius at the bends, correcting a curve, joining, switches, switch templates, signals and posts, a whole line step by step, fixing, the gizmo, the grade line, terrain, older sceneries, saving and driving"));
ImGui::EndMenu();
}
void editor_mode::first_step_done(first_step const Step)
{
m_first_steps_done[static_cast<std::size_t>(Step)] = true;
}
void editor_mode::open_kilometrage()
{
choose_work_area(work_area::tracks);
show_track_tab(track_tab::kilometrage);
}
namespace
{
void tutorial_input(char const *Keys, char const *Text, float const Column)
{
auto &list{*ImGui::GetWindowDrawList()};
auto const at{ImGui::GetCursorScreenPos()};
auto const height{std::round(ImGui::GetTextLineHeight() * 1.45f)};
auto const gap{std::round(height * 0.2f)};
float x{0.0f};
std::istringstream words{Keys};
for (std::string word; words >> word;)
{
ImVec2 const place{at.x + x, at.y};
if (word == "+")
{
auto const size{ImGui::CalcTextSize("+")};
list.AddText(ImVec2(place.x, std::round(at.y + (height - size.y) * 0.5f)), ImGui::GetColorU32(ImGuiCol_TextDisabled), "+");
x += size.x + gap;
}
else if (word == "LMB" || word == "RMB" || word == "MMB" || word == "wheel" || word == "move")
x += gesture_mouse(list, place, height, word) + gap;
else
x += gesture_key(list, place, height, word) + gap;
}
ImGui::Dummy(ImVec2(std::max(x, Column), height));
ImGui::SameLine();
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + std::round((height - ImGui::GetTextLineHeight()) * 0.5f));
ImGui::TextWrapped("%s", STR_C(Text));
}
// a window that grew past the bottom or the right of the screen is moved back onto it
void keep_on_screen(char const *Name)
{
auto const *window{ImGui::FindWindowByName(Name)};
if (window == nullptr)
return;
auto const display{ImGui::GetIO().DisplaySize};
ImVec2 position{window->Pos};
position.x = std::max(0.0f, std::min(position.x, display.x - window->Size.x));
position.y = std::max(20.0f, std::min(position.y, display.y - window->Size.y));
if (position.x != window->Pos.x || position.y != window->Pos.y)
ImGui::SetNextWindowPos(position);
}
// the strip of the profile: the ground, the track as it lies, the grade line over them with a point of it dragged up and down,
// the grades on both sides of it following
void tutorial_profile_sketch(ImDrawList &List, ImVec2 const Low, ImVec2 const High, double const Time)
{
auto const width{High.x - Low.x};
auto const height{High.y - Low.y};
auto const x_of = [&](float const Share) { return Low.x + width * (0.06f + 0.88f * Share); };
auto const y_of = [&](float const Level) { return Low.y + height * (0.82f - 0.6f * Level); };
std::vector<ImVec2> ground;
for (int i = 0; i <= 60; ++i)
{
auto const share{i / 60.0f};
ground.emplace_back(x_of(share), y_of(0.25f + 0.35f * std::sin(share * 5.2f + 0.6f) * 0.5f + 0.18f * share));
}
auto const flags{List.Flags};
List.Flags &= ~ImDrawListFlags_AntiAliasedFill;
for (std::size_t i = 0; i + 1 < ground.size(); ++i)
List.AddQuadFilled(ground[i], ground[i + 1], ImVec2(ground[i + 1].x, High.y), ImVec2(ground[i].x, High.y), IM_COL32(110, 85, 55, 70));
List.Flags = flags;
List.AddPolyline(ground.data(), static_cast<int>(ground.size()), IM_COL32(170, 130, 80, 220), 0, 1.5f);
auto const lift{static_cast<float>(0.5 + 0.5 * std::sin(Time * 1.6))};
std::array const points{ImVec2(x_of(0.0f), y_of(0.32f)), ImVec2(x_of(0.38f), y_of(0.42f)), ImVec2(x_of(0.66f), y_of(0.35f + 0.4f * lift)), ImVec2(x_of(1.0f), y_of(0.55f))};
List.AddPolyline(points.data(), static_cast<int>(points.size()), IM_COL32(170, 170, 170, 160), 0, 1.0f);
List.AddPolyline(points.data(), static_cast<int>(points.size()), IM_COL32(90, 220, 110, 255), 0, 2.5f);
for (std::size_t i = 0; i < points.size(); ++i)
List.AddCircleFilled(points[i], i == 2 ? 6.0f : 4.5f, i == 2 ? kGestureLit : IM_COL32(250, 210, 80, 255));
List.AddCircle(points[2], 9.0f, kGestureLit, 0, 1.5f);
for (std::size_t i = 0; i + 1 < points.size(); ++i)
{
auto const grade{-(points[i + 1].y - points[i].y) / (points[i + 1].x - points[i].x) * 40.0f};
auto const text{std::format("{:+.1f}", std::abs(grade) < 0.05f ? 0.0f : grade)};
ImVec2 const middle{(points[i].x + points[i + 1].x) * 0.5f, (points[i].y + points[i + 1].y) * 0.5f - ImGui::GetTextLineHeight() - 2.0f};
List.AddText(ImVec2(middle.x - ImGui::CalcTextSize(text.c_str()).x * 0.5f, middle.y), i == 0 ? IM_COL32(200, 200, 200, 255) : IM_COL32(255, 255, 255, 255), text.c_str());
}
gesture_mouse(List, ImVec2(points[2].x + 12.0f, points[2].y + 10.0f), height * 0.18f, "LMB");
}
// the points of a switch clicked one after another: its start, the ends of its two tracks, the way on, the points along the diverging track
void tutorial_template_sketch(ImDrawList &List, ImVec2 const Low, ImVec2 const High, double const Time)
{
auto const width{High.x - Low.x};
auto const height{High.y - Low.y};
auto const at = [&](float const X, float const Y) { return ImVec2(Low.x + width * X, Low.y + height * Y); };
auto const curve = [&](float const Share) { return at(0.08f + 0.70f * Share, 0.72f - 0.42f * Share * Share); };
auto const shown{static_cast<int>(std::fmod(static_cast<float>(Time), 10.0f))};
std::vector<ImVec2> line;
for (int i = 0; i <= 24; ++i)
line.push_back(curve(static_cast<float>(i) / 24.0f));
List.AddLine(at(0.08f, 0.72f), at(0.78f, 0.72f), IM_COL32(150, 150, 150, 130), 3.0f);
List.AddPolyline(line.data(), static_cast<int>(line.size()), IM_COL32(150, 150, 150, 130), ImDrawFlags_None, 3.0f);
std::array<ImVec2, 8> const points{at(0.08f, 0.72f), at(0.78f, 0.72f), curve(1.0f), at(0.94f, 0.14f), curve(0.28f), curve(0.5f), curve(0.72f), curve(0.88f)};
if (shown >= 1)
List.AddLine(points[0], points[1], IM_COL32(255, 255, 255, 230), 1.5f);
if (shown >= 3)
List.AddLine(points[2], points[3], IM_COL32(90, 200, 255, 230), 1.5f);
for (int i = 0; i < static_cast<int>(points.size()) && i <= shown; ++i)
{
List.AddCircleFilled(points[i], 4.0f, i < 4 ? IM_COL32(255, 225, 60, 255) : IM_COL32(120, 255, 120, 255));
if (i < 4)
List.AddText(ImVec2(points[i].x + 6.0f, points[i].y - 18.0f), IM_COL32(255, 255, 255, 255), std::to_string(i + 1).c_str());
}
if (shown >= 8)
List.AddPolyline(line.data(), static_cast<int>(line.size()), IM_COL32(255, 90, 200, 255), ImDrawFlags_None, 2.0f);
}
// the radius of the curve at a bend: the two straights clicked stay, the curve between them grows from sharp to gentle and back
void tutorial_corner_sketch(ImDrawList &List, ImVec2 const Low, ImVec2 const High, double const Time)
{
auto const width{High.x - Low.x};
auto const height{High.y - Low.y};
glm::vec2 const from{Low.x + width * 0.04f, Low.y + height * 0.80f};
glm::vec2 const vertex{Low.x + width * 0.50f, Low.y + height * 0.18f};
glm::vec2 const to{Low.x + width * 0.96f, Low.y + height * 0.80f};
auto const point = [](glm::vec2 const Point) { return ImVec2(Point.x, Point.y); };
List.AddLine(point(from), point(vertex), IM_COL32(150, 150, 150, 130), 1.0f);
List.AddLine(point(vertex), point(to), IM_COL32(150, 150, 150, 130), 1.0f);
List.AddCircleFilled(point(vertex), 4.0f, IM_COL32(255, 255, 255, 255));
// there and back in 8 s, a moment at each end
auto const cycle{std::fmod(static_cast<float>(Time), 8.0f) / 8.0f};
auto const swing{std::clamp(1.3f - std::abs(cycle * 2.0f - 1.0f) * 1.6f, 0.0f, 1.0f)};
auto const share{swing * swing * (3.0f - 2.0f * swing)};
auto const in{glm::normalize(from - vertex)};
auto const out{glm::normalize(to - vertex)};
auto const tangent{glm::mix(0.12f, 0.92f, share) * glm::length(from - vertex)};
auto const half{std::acos(glm::dot(in, out)) * 0.5f};
auto const centre{vertex + glm::normalize(in + out) * (tangent / std::cos(half))};
auto const start{vertex + in * tangent};
auto const end{vertex + out * tangent};
auto const begin{std::atan2(start.y - centre.y, start.x - centre.x)};
auto sweep{std::atan2(end.y - centre.y, end.x - centre.x) - begin};
while (sweep > glm::pi<float>())
sweep -= glm::two_pi<float>();
while (sweep < -glm::pi<float>())
sweep += glm::two_pi<float>();
auto const radius{glm::length(start - centre)};
std::vector line{point(from)};
for (int i = 0; i <= 32; ++i)
{
auto const angle{begin + sweep * static_cast<float>(i) / 32.0f};
line.push_back(ImVec2(centre.x + radius * std::cos(angle), centre.y + radius * std::sin(angle)));
}
line.push_back(point(to));
List.AddPolyline(line.data(), static_cast<int>(line.size()), IM_COL32(230, 190, 90, 255), ImDrawFlags_None, 3.0f);
auto const label{format(STR_C("R %.0f m: %s"), glm::mix(300.0, 6000.0, static_cast<double>(share)), share < 0.5f ? STR_C("a sharp curve, a slow train") : STR_C("a gentle curve, a fast train"))};
List.AddText(ImVec2(Low.x + (width - ImGui::CalcTextSize(label.c_str()).x) * 0.5f, High.y - ImGui::GetTextLineHeight() - 4.0f), IM_COL32(235, 235, 235, 255), label.c_str());
}
// the route of the grade line picked on the map: a click lights up the whole line through the track, Shift and a box only the
// tracks between the outermost ones in it; beside it the strip with a point of the line dragged
void tutorial_route_sketch(ImDrawList &List, ImVec2 const Low, ImVec2 const High, double const Time)
{
auto const split{std::round(Low.x + (High.x - Low.x) * 0.48f)};
tutorial_profile_sketch(List, ImVec2(split + 6.0f, Low.y), High, Time);
List.AddLine(ImVec2(split + 2.0f, Low.y + 6.0f), ImVec2(split + 2.0f, High.y - 6.0f), ImGui::GetColorU32(ImGuiCol_Border), 1.0f);
auto const width{split - Low.x};
auto const height{High.y - Low.y};
auto const at = [&](float const X, float const Y) { return ImVec2(Low.x + width * X, Low.y + height * Y); };
auto const mix = [](ImVec2 const A, ImVec2 const B, float const T) { return ImVec2(A.x + (B.x - A.x) * T, A.y + (B.y - A.y) * T); };
auto const ease = [](float const T) { auto const t{std::clamp(T, 0.0f, 1.0f)}; return t * t * (3.0f - 2.0f * t); };
// the joints of the tracks of the line, and a siding going off it at a switch
std::array const joints{at(0.04f, 0.72f), at(0.18f, 0.64f), at(0.32f, 0.54f), at(0.46f, 0.48f), at(0.60f, 0.46f), at(0.74f, 0.42f), at(0.88f, 0.32f), at(0.98f, 0.24f)};
int constexpr tracks{7};
List.AddBezierQuadratic(joints[3], at(0.58f, 0.52f), at(0.80f, 0.66f), IM_COL32(130, 130, 130, 200), 2.0f);
auto const cycle{static_cast<float>(std::fmod(Time, 8.0))};
bool const boxing{cycle >= 4.0f};
int first{0};
int last{tracks - 1};
bool lit{false};
ImVec2 cursor;
if (false == boxing)
{
auto const target{mix(joints[4], joints[5], 0.5f)};
cursor = mix(at(0.75f, 0.92f), target, ease(cycle / 1.0f));
lit = cycle > 1.3f;
}
else
{
auto const corner{at(0.29f, 0.28f)};
auto const drag{ease((cycle - 4.6f) / 1.5f)};
auto const other{mix(corner, at(0.72f, 0.70f), drag)};
cursor = cycle < 4.6f ? mix(at(0.75f, 0.92f), corner, ease((cycle - 4.0f) / 0.6f)) : other;
if (cycle >= 4.6f && cycle < 6.6f)
{
List.AddRectFilled(corner, other, IM_COL32(90, 175, 255, 40));
List.AddRect(corner, other, kGestureLit, 0.0f, 0, 1.5f);
}
lit = cycle >= 6.2f;
first = 2;
last = 4;
gesture_key(List, ImVec2(Low.x + 6.0f, Low.y + 6.0f), std::round(ImGui::GetTextLineHeight() * 1.3f), "Shift");
}
for (int i = 0; i < tracks; ++i)
{
auto const on{lit && i >= first && i <= last};
List.AddLine(joints[i], joints[i + 1], on ? IM_COL32(90, 220, 110, 255) : IM_COL32(170, 170, 170, 220), on ? 4.0f : 2.0f);
}
for (auto const &joint : joints)
List.AddCircleFilled(joint, 2.5f, IM_COL32(220, 220, 220, 255));
gesture_mouse(List, ImVec2(cursor.x + 2.0f, cursor.y + 2.0f), std::round(height * 0.16f), "LMB");
auto const caption{boxing ? STR_C("Shift + a box: between the outermost tracks") : STR_C("LMB on a track: its whole line")};
List.AddText(ImVec2(Low.x + 6.0f, High.y - ImGui::GetTextLineHeight() - 4.0f), IM_COL32(230, 230, 230, 230), caption);
}
// a signal put by a track: pressed beside it and dragged along, the arrow tells the way of the trains which see it; then the
// hectometre posts of the line stand up one after the other
void tutorial_signal_sketch(ImDrawList &List, ImVec2 const Low, ImVec2 const High, double const Time)
{
auto const width{High.x - Low.x};
auto const height{High.y - Low.y};
auto const at = [&](float const X, float const Y) { return ImVec2(Low.x + width * X, Low.y + height * Y); };
auto const ease = [](float const T) { auto const t{std::clamp(T, 0.0f, 1.0f)}; return t * t * (3.0f - 2.0f * t); };
auto const cycle{static_cast<float>(std::fmod(Time, 8.0))};
// the track seen from above: sleepers and the two rails
auto const middle{at(0.0f, 0.5f).y};
auto const gauge{height * 0.07f};
for (int sleeper = 0; sleeper < 40; ++sleeper)
{
auto const x{at(0.02f + 0.025f * static_cast<float>(sleeper), 0.0f).x};
List.AddLine(ImVec2(x, middle - gauge * 1.6f), ImVec2(x, middle + gauge * 1.6f), IM_COL32(110, 90, 70, 255), 3.0f);
}
List.AddLine(ImVec2(Low.x, middle - gauge), ImVec2(High.x, middle - gauge), IM_COL32(200, 200, 205, 255), 2.0f);
List.AddLine(ImVec2(Low.x, middle + gauge), ImVec2(High.x, middle + gauge), IM_COL32(200, 200, 205, 255), 2.0f);
auto const line{ImGui::GetTextLineHeight()};
if (cycle < 4.0f)
{
// pressed beside the track, dragged along it
auto const press{at(0.32f, 0.67f)};
auto const drag{ease((cycle - 1.0f) / 1.4f)};
ImVec2 const cursor{cycle < 1.0f ? ImVec2(press.x + (1.0f - ease(cycle)) * width * 0.3f, press.y + (1.0f - ease(cycle)) * height * 0.2f) : ImVec2(press.x + width * 0.3f * drag, press.y)};
if (cycle >= 1.0f)
{
ImVec2 const tip{cursor.x, press.y};
List.AddLine(press, tip, IM_COL32(255, 210, 60, 255), 3.0f);
if (drag > 0.2f)
List.AddTriangleFilled(ImVec2(tip.x + 8.0f, tip.y), ImVec2(tip.x - 4.0f, tip.y - 7.0f), ImVec2(tip.x - 4.0f, tip.y + 7.0f), IM_COL32(255, 210, 60, 255));
}
if (cycle >= 2.6f)
{
// the signal by the track: the mast and the head with its lights
auto const foot{at(0.32f, 0.98f)};
List.AddLine(foot, ImVec2(foot.x, foot.y - height * 0.06f), IM_COL32(200, 200, 200, 255), 3.0f);
ImVec2 const head_low{foot.x - height * 0.04f, foot.y - height * 0.27f};
ImVec2 const head_high{foot.x + height * 0.04f, foot.y - height * 0.06f};
List.AddRectFilled(head_low, head_high, IM_COL32(25, 25, 25, 255), height * 0.03f);
List.AddRect(head_low, head_high, IM_COL32(150, 150, 150, 255), height * 0.03f);
std::array const lights{IM_COL32(230, 60, 50, 255), IM_COL32(70, 210, 90, 255), IM_COL32(240, 240, 240, 255)};
for (std::size_t i = 0; i < lights.size(); ++i)
List.AddCircleFilled(ImVec2(foot.x, head_low.y + height * (0.04f + 0.065f * static_cast<float>(i))), height * 0.025f, lights[i]);
List.AddText(ImVec2(foot.x - height * 0.06f - ImGui::CalcTextSize("S1").x, foot.y - line), IM_COL32(255, 255, 255, 230), "S1");
}
gesture_mouse(List, ImVec2(cursor.x + 2.0f, cursor.y + 2.0f), std::round(height * 0.16f), "LMB");
List.AddText(ImVec2(Low.x + 6.0f, Low.y + 4.0f), IM_COL32(230, 230, 230, 230), STR_C("LMB by the track, drag along: the way the trains run"));
return;
}
// the hectometre posts of the line, one after the other
auto const shown{static_cast<int>((cycle - 4.3f) / 0.35f) + 1};
for (int i = 0; i < std::min(shown, 9); ++i)
{
auto const x{at(0.07f + 0.105f * static_cast<float>(i), 0.0f).x};
auto const foot{middle - gauge * 3.2f};
List.AddRectFilled(ImVec2(x - 3.0f, foot - height * 0.14f), ImVec2(x + 3.0f, foot), IM_COL32(240, 240, 240, 255), 1.0f);
auto const text{std::format("12.{}", i + 1)};
auto const size{ImGui::CalcTextSize(text.c_str())};
List.AddText(ImVec2(std::round(x - size.x * 0.5f), foot - height * 0.14f - size.y - 1.0f), IM_COL32(255, 220, 120, 255), text.c_str());
}
List.AddText(ImVec2(Low.x + 6.0f, High.y - line - 4.0f), IM_COL32(230, 230, 230, 230), STR_C("B: posts every 100 m along the whole line at once"));
}
// a vehicle seen from the side, drawn simply: the body with its windows on two bogies
void tutorial_vehicle(ImDrawList &List, ImVec2 const Low, ImVec2 const High, ImU32 const Body, bool const Locomotive)
{
auto const width{High.x - Low.x};
auto const height{High.y - Low.y};
auto const wheel{height * 0.14f};
ImVec2 const body_high{High.x, High.y - wheel * 1.6f};
List.AddRectFilled(Low, body_high, Body, height * 0.12f);
auto const windows{Locomotive ? 2 : 5};
for (int i = 0; i < windows; ++i)
{
// a locomotive has a window at each end, a coach a row of them
auto window_x{Low.x + width * (0.08f + 0.18f * static_cast<float>(i))};
if (Locomotive)
window_x = (i == 0 ? Low.x + width * 0.06f : High.x - width * 0.2f);
auto const size{Locomotive ? width * 0.14f : width * 0.12f};
List.AddRectFilled(ImVec2(window_x, Low.y + height * 0.15f), ImVec2(window_x + size, Low.y + height * 0.42f), IM_COL32(30, 40, 50, 255), 2.0f);
}
for (float const share : {0.22f, 0.78f})
{
ImVec2 const middle{Low.x + width * share, High.y - wheel};
List.AddCircleFilled(ImVec2(middle.x - wheel * 1.1f, middle.y), wheel, IM_COL32(40, 40, 40, 255));
List.AddCircleFilled(ImVec2(middle.x + wheel * 1.1f, middle.y), wheel, IM_COL32(40, 40, 40, 255));
}
}
// the trainset tool: a double click on the list puts a vehicle into the train, the train grows on the track, Place and drive
// and it runs off
void tutorial_trainset_sketch(ImDrawList &List, ImVec2 const Low, ImVec2 const High, double const Time)
{
auto const width{High.x - Low.x};
auto const height{High.y - Low.y};
auto const at = [&](float const X, float const Y) { return ImVec2(Low.x + width * X, Low.y + height * Y); };
auto const cycle{static_cast<float>(std::fmod(Time, 9.0))};
std::array const colours{IM_COL32(200, 60, 50, 255), IM_COL32(60, 120, 70, 255), IM_COL32(70, 100, 160, 255)};
auto const line{ImGui::GetTextLineHeight()};
// the list: the side view of each vehicle and its name
for (std::size_t i = 0; i < colours.size(); ++i)
{
auto const slot{static_cast<float>(i)};
auto const top{0.08f + 0.28f * slot};
// lit while it's double clicked
if (cycle > 0.8f + 1.4f * slot && cycle < 1.2f + 1.4f * slot)
List.AddRectFilled(at(0.02f, top - 0.02f), at(0.40f, top + 0.24f), IM_COL32(90, 175, 255, 70), 3.0f);
tutorial_vehicle(List, at(0.03f, top), at(0.16f, top + 0.2f), colours[i], i == 0);
List.AddRectFilled(at(0.19f, top + 0.03f), at(0.36f, top + 0.03f + line / height * 0.6f), IM_COL32(220, 220, 220, 200), 2.0f);
List.AddRectFilled(at(0.19f, top + 0.12f), at(0.31f, top + 0.12f + line / height * 0.5f), IM_COL32(150, 150, 150, 160), 2.0f);
}
// the cursor goes down the list double clicking, then to the button
auto const row{std::min(2, static_cast<int>(std::max(0.0f, cycle - 0.4f) / 1.4f))};
ImVec2 cursor{at(0.30f, 0.16f + 0.28f * static_cast<float>(row))};
auto const added{std::clamp(static_cast<int>((cycle - 0.8f) / 1.4f) + 1, 0, 3)};
auto const button_low{at(0.55f, 0.08f)};
auto const button_high{at(0.95f, 0.30f)};
if (cycle > 4.6f)
cursor = ImVec2((button_low.x + button_high.x) * 0.5f, (button_low.y + button_high.y) * 0.5f);
bool const pressed{cycle > 5.2f && cycle < 5.6f};
List.AddRectFilled(button_low, button_high, pressed ? IM_COL32(60, 170, 75, 255) : IM_COL32(50, 140, 64, 255), 4.0f);
auto const label{STR_C("Place and drive")};
auto const size{ImGui::CalcTextSize(label)};
List.AddText(ImVec2(std::round((button_low.x + button_high.x - size.x) * 0.5f), std::round((button_low.y + button_high.y - size.y) * 0.5f)), IM_COL32(255, 255, 255, 255), label);
// the track and the train on it, the head on the left; after the button it runs off
auto const rail{at(0.0f, 0.86f).y};
List.AddLine(ImVec2(at(0.44f, 0.0f).x, rail), ImVec2(High.x, rail), IM_COL32(170, 170, 170, 255), 2.0f);
auto const run{cycle > 5.6f ? (cycle - 5.6f) * (cycle - 5.6f) * width * 0.06f : 0.0f};
auto const car{width * 0.15f};
List.PushClipRect(ImVec2(at(0.44f, 0.0f).x, Low.y), High, true);
for (std::size_t i = 0; i < static_cast<std::size_t>(added); ++i)
{
auto const front{at(0.46f, 0.0f).x + (car + 3.0f) * static_cast<float>(i) - run};
tutorial_vehicle(List, ImVec2(front, rail - height * 0.24f), ImVec2(front + car, rail), colours[i], i == 0);
}
List.PopClipRect();
gesture_mouse(List, ImVec2(cursor.x + 2.0f, cursor.y + 2.0f), std::round(height * 0.16f), "LMB");
if (cycle < 4.6f)
List.AddText(ImVec2(cursor.x + height * 0.16f, cursor.y), IM_COL32(255, 220, 120, 255), "x2");
}
// the terrain model under the tracks: the track laid level sinks onto the ground along, the formation and the slopes under it
void tutorial_terrain_sketch(ImDrawList &List, ImVec2 const Low, ImVec2 const High, double const Time)
{
auto const width{High.x - Low.x};
auto const height{High.y - Low.y};
auto const phase{static_cast<float>(std::fmod(Time * 0.3, 1.4))};
auto const settle{std::clamp(phase / 0.8f, 0.0f, 1.0f)};
auto const eased{settle * settle * (3.0f - 2.0f * settle)};
auto const ground_at = [&](float const Share) { return 0.45f + 0.22f * std::sin(Share * 4.0f) + 0.08f * std::sin(Share * 13.0f); };
auto const smooth_at = [&](float const Share) { return 0.45f + 0.18f * std::sin(Share * 4.0f); };
auto const x_of = [&](float const Share) { return Low.x + width * Share; };
auto const y_of = [&](float const Level) { return Low.y + height * (0.95f - 0.75f * Level); };
int constexpr steps{80};
std::vector<ImVec2> ground;
std::vector<ImVec2> track;
for (int i = 0; i <= steps; ++i)
{
auto const share{static_cast<float>(i) / steps};
ground.emplace_back(x_of(share), y_of(ground_at(share)));
track.emplace_back(x_of(share), y_of(0.55f + (smooth_at(share) + 0.04f - 0.55f) * eased));
}
auto const flags{List.Flags};
List.Flags &= ~ImDrawListFlags_AntiAliasedFill;
for (int i = 0; i < steps; ++i)
{
std::array const quad{ground[i], ground[i + 1], ImVec2(ground[i + 1].x, High.y), ImVec2(ground[i].x, High.y)};
List.AddConvexPolyFilled(quad.data(), static_cast<int>(quad.size()), IM_COL32(95, 120, 70, 120));
}
if (eased > 0.99f)
for (int i = 0; i < steps; ++i)
{
auto const fill{track[i].y < ground[i].y};
List.AddQuadFilled(track[i], track[i + 1], ground[i + 1], ground[i], fill ? IM_COL32(190, 150, 90, 150) : IM_COL32(60, 45, 30, 150));
}
List.Flags = flags;
List.AddPolyline(ground.data(), static_cast<int>(ground.size()), IM_COL32(150, 190, 110, 255), 0, 1.5f);
List.AddPolyline(track.data(), static_cast<int>(track.size()), IM_COL32(230, 230, 230, 255), 0, 3.0f);
auto const label{eased > 0.99f ? STR_C("embankment and cutting") : STR_C("the terrain model")};
List.AddText(ImVec2(Low.x + 6.0f, Low.y + 4.0f), IM_COL32(220, 220, 220, 200), label);
}
} // namespace
// a lesson of the tutorial: its clip or sketch, the mouse and the keys it teaches, the steps to try and the button under it
struct editor_mode::tutorial_lesson
{
// what the button under a lesson does: opens the tool the lesson is about, or switches the view
enum class trial { reading, top, lay, turnout, select, profile, terrain, plan, trainset, signals, station };
struct input
{
char const *keys;
char const *text;
};
struct goal
{
first_step step;
char const *text;
};
char const *title;
char const *clip;
std::vector<input> inputs;
std::vector<goal> goals;
trial action;
void (*sketch)(ImDrawList &, ImVec2, ImVec2, double){nullptr};
};
std::vector<editor_mode::tutorial_lesson> const &editor_mode::tutorial_lessons()
{
using trial = tutorial_lesson::trial;
static std::vector<tutorial_lesson> const lessons{
{"Looking around",
"camera",
{{"RMB + move", "Hold the right mouse button and move the mouse to look around"},
{"RMB + W S A D", "Keep holding the right button and press W S A D to fly around; E takes you up, Q down"},
{"O", "Switches between the view from above, like a map, and the normal 3D view"},
{"wheel", "Zooms in and out at the mouse pointer"},
{"MMB + move", "Press the wheel down and drag to move the view, from above and in 3D"},
{"Y", "Rides the camera along the track under the mouse pointer, as from a train; Y again gets off. The tools keep working on the way"},
{"RMB + W S A D", "While riding: W and S drive, Shift faster; A and D jump to the next track on the left or right; Z picks the way at the next switch"},
{"F", "Flies to what you have selected"}},
{{first_step::look, "Look around with the right mouse button held"}, {first_step::top, "Press O: from above to 3D, or back"}, {first_step::ride, "Ride along a track with Y"}},
trial::top},
{"Laying track",
"lay",
{{"F3", "Opens Tracks, the part of the editor where track is laid"},
{"L", "Turns laying on"},
{"LMB", "Click the ground where the track should start, or the end of a track to carry it on"},
{"LMB", "Click again wherever the track should turn; the curves are made for you. Until you press Enter it is only a line, not a track"},
{"Enter", "Lays the track along the line. To carry on, click its end"},
{"Backspace", "Removes the last point you clicked"},
{"Esc", "Throws away the points not laid yet, but only when pressed twice within 3 seconds: the first press just warns. With no points left, Esc stops laying"}},
{{first_step::tracks, "Open Tracks (F3)"}, {first_step::lay, "Turn laying on (L)"}, {first_step::laid, "Click a few points and press Enter"}},
trial::lay},
{"Curves",
"curves",
{{"LMB", "Every place where the line turns gets a curve, as big as fits. An orange label means the curve breaks the rules for the speed set in the panel"},
{"Tab", "To copy a curve from the aerial photo: press Tab, then click points along the middle of the curve; it ends at the last one"},
{"Alt", "Hold it while clicking: the direction jumps in steps of 15 degrees, or to the angle of a switch to a straight track nearby"},
{"0 1 2 3", "After the first point: type a number and press Enter, and the next straight piece is that many metres long"},
{"wheel", "The wheel alone zooms, as everywhere"}},
{{first_step::bend, "Lay a track that turns"}, {first_step::trace, "Press Tab to copy a curve from the photo"}},
trial::lay},
{"Radius of the curves at the bends",
nullptr,
{{"LMB", "Where the line turns, two straight pieces meet at a corner no train could take, so the editor rounds it off with a curve"},
{"R", "The radius tells how big the curve is, in metres: the bigger it is, the gentler the curve and the faster a train can go round it"},
{"R", "The editor picks the biggest curve that fits between your points. A slight turn gets only a gentle easing in and out; a sharper one gets a real curve, eased in and out, with the outer rail raised, as the railway rules say"},
{"LMB", "Points too close together leave little room, so the curve comes out smaller. When it no longer meets the rules, its label turns orange: click the points further apart"},
{"R L h", "The label of a curve: R is the radius, L the length of the easing into it, h how much higher the outer rail is, in millimetres. A slight turn with no real curve reads 2xL"}},
{{first_step::bend, "Lay a track that turns and read the label of its curve"}},
trial::lay,
tutorial_corner_sketch},
{"Correcting a curve",
"plan",
{{"G", "Turns Plan on. Click a curve to see how it is built; its corner point and its size can be dragged in the view"},
{"LMB + move", "The strip in the panel shows how much the track turns along the way. Drag a flat part of it up or down to change how sharp that curve is"},
{"LMB + move", "Drag a sloping part sideways: the easing into the curve gets longer or shorter"},
{"wheel", "Over the strip, the wheel changes it in small steps"},
{"LMB", "Apply to the scenery: the tracks are laid again as the plan shows"}},
{{first_step::plan, "Open the plan of a curve"}, {first_step::plan_band, "Change the curve on the strip"}, {first_step::plan_apply, "Apply the change"}},
trial::plan},
{"Joining tracks",
"join",
{{"LMB + move", "Press on the loose end of a track (a pink ring while laying) and drag: a new track comes out of it"},
{"LMB", "Let go on another loose end: a curve joins the two tracks"},
{"LMB", "While laying, a click on a loose end joins your track to it exactly, with no gap"},
{"Ctrl + wheel", "Over the other end: makes the joining curve bigger or smaller"},
{"Shift + wheel", "Over the other end: tries another way of joining"}},
{{first_step::join, "Join two loose ends"}, {first_step::wheel, "Change the size of the joining curve with Ctrl+wheel"}},
trial::lay},
{"Switches",
"switch",
{{"T", "Turns Switch on. The tiles at the top choose what you put in: a switch, a slip switch or a crossing"},
{"LMB", "Click the track the switch should go into"},
{"LMB + move", "Press on it and drag: along the track sets which way the switch points, sideways which side it branches off to; let go to lay it"},
{"LMB + move", "Drag from a straight track onto the track beside it: two switches joining them (a crossover). Over a crossover going the other way you get a double or a scissors one"},
{"LMB + move", "Drag from a straight across several tracks running alongside and let go on the farthest: a row of switches leading onto each of them (a switch ladder)"},
{"Esc", "Back to selecting"}},
{{first_step::turnout, "Put a switch into a track"}},
trial::turnout},
{"Switch templates",
nullptr,
{{"T", "In Switch, the Template editor... button: ready switch shapes and your own. Point at a number to see on the drawing what it means"},
{"LMB", "To copy a switch from the aerial photo: Click off the view..., then click where the switch starts, the end of its straight track, the end of its branching track and one point further along that branch"},
{"LMB", "Then at least 3 points along the branching track: its shape and size follow them"},
{"Backspace", "Removes the last point; Esc stops clicking"},
{"O", "The view from above; the aerial photo is turned on in the Layers panel"},
{"LMB", "Make the template: the new switch lies on the photo where it was clicked and changes as its numbers change"}},
{{first_step::template_open, "Open the template editor"}, {first_step::template_clicked, "Make a template by clicking in the view"}},
trial::turnout,
tutorial_template_sketch},
{"Signals and posts",
nullptr,
{{"I", "Turns Signals on. Click a kind of signal in its list; it shows the signals this scenery has"},
{"LMB", "Click beside a track: the signal stands on that side"},
{"LMB + move", "Press beside the track and drag along it: the signal faces the trains going the way you dragged"},
{"LMB + move", "Drag a signal that already stands: it slides along its track"},
{"X", "Turns Kilometrage on. Click a track where you know the km, by a post or a station: the posts every 100 m and the block signals are numbered after it"},
{"B", "Things beside the track: posts every 100 m along a whole line at once, the W17 signs by the switches (how far a wagon may stand) and trains. The posts take their numbers from the km set with X, or from the km you type in their panel for the place you clicked"}},
{{first_step::signal, "Put up a signal"}, {first_step::posts, "Put up the posts along a line"}},
trial::signals,
tutorial_signal_sketch},
{"A whole line, step by step",
nullptr,
{{"1", "Lay the tracks of the line (L), and the switches (T) where the stations are"},
{"2", "Kilometrage (X): click a track where you know the km, by a post or at a station. One point is enough: the posts, the stations and the block signals count from it"},
{"3", "Station (under More): click a switch of the station, then give its name, its code and the number of the line"},
{"4", "Station signals (under More): Set the signals puts up the entry, exit and shunting signals of the whole station at once"},
{"5", "Line block (under More): the block signals on the line out of each station, once the stations are set"},
{"6", "Posts (B): click the line, then Place the posts, every 100 m along all of it"},
{"7", "Save with Ctrl+S, put a train on the track and drive: the last lesson"}},
{},
trial::station},
{"Selecting and fixing",
"edit",
{{"Esc", "Turns selecting on"},
{"LMB", "Click a track to select it; its ends show"},
{"LMB + move", "Drag an end: where tracks meet, the ones beside follow; a loose end pulls out a new track, or with Ctrl moves by itself"},
{"RMB", "Click a track: a menu of what can be done with it"},
{"Del", "Deletes what is selected"},
{"Ctrl + Z", "Undoes the last change; Ctrl+Y brings it back"},
{"Q W E", "Handles on the track to move, turn, lengthen or bend it: the next lesson"}},
{{first_step::select, "Select a track"}, {first_step::undo, "Undo a change"}},
trial::select},
{"Editing with the gizmo",
"gizmo",
{{"Q", "Shows arrows on the selected track: drag one to move the track"},
{"W", "Shows rings: drag one to turn the track, to tilt it up or down, or to raise one rail; the tracks joined to it follow"},
{"E", "Shows handles that make the track longer at its loose end; a curve stays as sharp as it was"},
{"LMB + move", "With the handles out there are also points that bend the track: drag one and the track bends after it"},
{"LMB", "Click a point of the track to put the handles there: Q then moves that point only, W turns the whole track around it"},
{"Ctrl", "Hold it while dragging to move in even steps"},
{"Esc", "Lets go of the point; the arrow button above the view hides the handles again"}},
{{first_step::gizmo, "Show the handles with Q, W or E"}, {first_step::vector, "Bend a track by one of its points, or move it with the handles"}},
trial::select},
{"The grade line",
nullptr,
{{"P", "Turns Profile on. Click a track: below the view you see how its whole line goes up and down"},
{"Shift + LMB", "A click adds another track; a box drawn from empty ground takes the tracks inside it. The route runs between the outermost of them, through the switches"},
{"LMB + move", "Drag a point of the line: the slopes on both sides of it change with it"},
{"LMB LMB", "Double-click the line to add a point"},
{"Del", "Removes the chosen point"},
{"LMB", "Tracks running side by side share one line: change it, press Apply to the scenery, and the tracks beside it (blue) change too"},
{"LMB", "To give the route open in Profile a line of its own, press Take off the common grade line"},
{"Ctrl + wheel", "Over the chart: stretches it up and down, so the slopes are easier to see"},
{"RMB + move", "Over the chart: moves along the line"}},
{{first_step::profile, "Open the profile of a track"}, {first_step::profile_point, "Move or add a point"}, {first_step::profile_apply, "Apply it: the tracks take the new heights"}},
trial::profile,
tutorial_route_sketch},
{"Terrain from the terrain model",
nullptr,
{{"L", "In a new, empty scenery the first L asks whether you want the real ground of the place or a flat plane"},
{"F5", "Opens Terrain; the real ground (the terrain model) is in its panel"},
{"LMB", "Step 1, Location: show where the scenery is, with a click on the map or by typing the place"},
{"LMB", "Step 2, before there are tracks: Load around the camera brings the ground of that spot from geoportal.gov.pl"},
{"LMB", "Step 2, once there are tracks: Tracks onto the terrain puts the tracks not fitted yet onto the real ground; Terrain under the tracks brings the ground up or down to them; The terrain of the model only loads the ground along them and leaves the tracks alone"},
{"F3", "The terrain to some of the tracks only: select them (F3), Terrain in the inspector"},
{"LMB", "A new track partly under the ground: the message above the view has Fit the ground to the track; one click builds the bank or the cutting along it"},
{"", "A line you shaped by hand in Profile stays as it is, even when all the tracks are fitted anew, unless you tick that the ones edited by hand go too"},
{"", "Tracks close side by side (under 5.1 m apart) get one common line of heights; a track crossing others where the ground is low on both sides goes under them"},
{"Ctrl + Z", "Takes the changes back"}},
{{first_step::terrain, "Open Terrain (F5)"}, {first_step::nmt, "Load the real ground"}},
trial::terrain,
tutorial_terrain_sketch},
{"Sceneries made before",
nullptr,
{{"LMB", "Some tracks can't be changed, for example ones put in from a ready .inc file or lying on a locked layer: the Inspector says why"},
{"T", "Switches laid earlier keep their shape; a template picked for one in the Inspector gives it the ready shape"},
{"P", "Heights saved by an older editor are read as they were saved"},
{"Ctrl + S", "Saving writes only what was changed; the rest of the files stays as it was"}},
{},
trial::reading},
{"Saving and driving",
nullptr,
{{"Ctrl + S", "Saves the scenery"},
{"LMB", "The trainset tool (the button below): click the track where the train should stand"},
{"LMB LMB", "Double-click a vehicle in the list to add it to the train; click a vehicle in the train to move it, turn it round or take it out"},
{"LMB", "Place and drive: the train stands on the track and you sit in the cab"}},
{{first_step::save, "Save the scenery"}, {first_step::drive, "Drive on the new track"}},
trial::trainset,
tutorial_trainset_sketch},
};
return lessons;
}
bool editor_mode::tutorial_done(tutorial_lesson const &Lesson) const
{
return false == Lesson.goals.empty() && std::ranges::all_of(Lesson.goals, [this](tutorial_lesson::goal const &Goal) { return m_first_steps_done[static_cast<std::size_t>(Goal.step)]; });
}
void editor_mode::note_first_steps()
{
if (ui()->mode() == nodebank_panel::TRACK)
first_step_done(first_step::tracks);
if (m_lay.active)
first_step_done(first_step::lay);
if (ui()->mode() == nodebank_panel::TRACK && m_track_tab == track_tab::path && selected_track() != nullptr)
first_step_done(first_step::select);
if (current_work_area() == work_area::terrain)
first_step_done(first_step::terrain);
if (ui()->mode() == nodebank_panel::TRACK && m_track_tab == track_tab::route && false == m_route.chain.tracks.empty())
first_step_done(first_step::plan);
if (ui()->mode() == nodebank_panel::TRACK && m_track_gizmo_on)
first_step_done(first_step::gizmo);
if (m_track_gizmo_using || (m_point_drag.moved && m_track_point.valid() && false == editor_track::is_end(m_track_point.kind)))
first_step_done(first_step::vector);
if (m_ride.active())
first_step_done(first_step::ride);
}
char const *editor_mode::tutorial_of_tool()
{
auto const &lessons{tutorial_lessons()};
// the first of the lessons named whose steps aren't all reached yet
auto const undone = [&](std::initializer_list<char const *> const Titles) {
for (auto const *title : Titles)
if (auto const found{std::ranges::find_if(lessons, [title](tutorial_lesson const &Lesson) { return std::string_view{title} == Lesson.title; })};
found != lessons.end() && false == tutorial_done(*found))
return title;
return "";
};
if (m_lay.active)
return undone({"Laying track", "Radius of the curves at the bends", "Curves", "Joining tracks"});
if (current_work_area() == work_area::terrain)
return undone({"Terrain from the terrain model"});
if (ui()->mode() != nodebank_panel::TRACK)
return "";
switch (m_track_tab)
{
case track_tab::turnout:
return undone({m_switch_designer.open ? "Switch templates" : "Switches"});
case track_tab::profile:
return undone({"The grade line"});
case track_tab::route:
case track_tab::straights:
return undone({"Correcting a curve"});
case track_tab::signals:
case track_tab::kilometrage:
return undone({"Signals and posts"});
case track_tab::station:
case track_tab::station_signals:
case track_tab::line_block:
return undone({"A whole line, step by step"});
default:
return "";
}
}
void editor_mode::follow_tutorial()
{
auto const &lessons{tutorial_lessons()};
auto const open_lesson = [&](std::string_view const Title) {
if (auto const found{std::ranges::find_if(lessons, [Title](tutorial_lesson const &Lesson) { return Title == Lesson.title; })}; found != lessons.end())
m_tutorial_page = static_cast<int>(found - lessons.begin());
};
// the lesson of what's being done now, the next one once its steps are reached; opened when that changes, so a page turned by
// hand stays
if (auto const *followed{tutorial_of_tool()}; m_tutorial_followed != followed)
{
m_tutorial_followed = followed;
open_lesson(followed);
}
if (m_tutorial_goto != nullptr)
{
open_lesson(m_tutorial_goto);
m_tutorial_goto = nullptr;
}
m_tutorial_page = std::clamp(m_tutorial_page, 0, static_cast<int>(lessons.size()) - 1);
auto const &lesson{lessons[m_tutorial_page]};
if (lesson.clip == nullptr)
m_tutorial_clip.close();
else if (auto const path{std::string{"lang/editor_tutorial/"} + lesson.clip + ".gif"}; path != m_tutorial_clip.path())
{
m_tutorial_clip.open(path);
m_tutorial_since = ImGui::GetTime();
}
}
void editor_mode::render_first_steps()
{
if (false == m_first_steps_open)
{
if (false == m_tutorial_clip.path().empty())
m_tutorial_clip.close();
return;
}
note_first_steps();
follow_tutorial();
auto const &lessons{tutorial_lessons()};
auto const &lesson{lessons[m_tutorial_page]};
auto const scale{Global.ui_scale};
ImGui::SetNextWindowPos(ImVec2(ImGui::GetIO().DisplaySize.x * 0.5f, ImGui::GetIO().DisplaySize.y * 0.5f), ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f));
ImGui::SetNextWindowBgAlpha(1.0f);
ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, 0.0f), ImVec2(FLT_MAX, ImGui::GetIO().DisplaySize.y - 60.0f));
keep_on_screen("###tutorial");
if (ImGui::Begin((std::string(STR_C("Tutorial")) + "###tutorial").c_str(), &m_first_steps_open, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize))
{
// floating it's as wide as the clips; docked it takes the width of its dock, the texts wrapped to it
auto const width{ImGui::IsWindowDocked() ? std::max(160.0f * scale, ImGui::GetContentRegionAvail().x) : 520.0f * scale};
ImGui::PushTextWrapPos(ImGui::GetCursorPosX() + width);
ImGui::TextDisabled(STR_C("Lesson %d of %d"), m_tutorial_page + 1, static_cast<int>(lessons.size()));
ImGui::SameLine();
ImGui::TextColored(ImVec4(0.55f, 0.8f, 1.0f, 1.0f), "%s", STR_C(lesson.title));
render_tutorial_media(lesson, width);
render_tutorial_goals(lesson);
render_tutorial_trial(lesson);
ImGui::PopTextWrapPos();
ImGui::Separator();
render_tutorial_pages();
}
ImGui::End();
}
void editor_mode::render_tutorial_media(tutorial_lesson const &Lesson, float const Width)
{
if (Lesson.clip != nullptr && false == m_tutorial_clip.missing())
{
// the room of a clip still loading, as the clips are recorded: 520 x 325
auto const height{Width * 325.0f / 520.0f};
if (auto const texture{m_tutorial_clip.texture(ImGui::GetTime() - m_tutorial_since)}; texture != 0)
{
auto const shown{Width * static_cast<float>(m_tutorial_clip.height()) / static_cast<float>(std::max(1, m_tutorial_clip.width()))};
ImGui::Image(static_cast<ImTextureID>(texture), ImVec2(Width, shown));
if (ImGui::IsItemClicked())
m_tutorial_since = ImGui::GetTime();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Click: play it from the start"));
}
else
{
auto const at{ImGui::GetCursorScreenPos()};
ImGui::GetWindowDrawList()->AddRectFilled(at, ImVec2(at.x + Width, at.y + height), ImGui::GetColorU32(ImGuiCol_FrameBg));
ImGui::Dummy(ImVec2(Width, height));
}
}
if (Lesson.sketch != nullptr)
{
auto const at{ImGui::GetCursorScreenPos()};
ImVec2 const end{at.x + Width, at.y + std::round(Width * 0.36f)};
auto *list{ImGui::GetWindowDrawList()};
list->AddRectFilled(at, end, ImGui::GetColorU32(ImGuiCol_FrameBg), 4.0f);
list->PushClipRect(at, end, true);
Lesson.sketch(*list, at, end, ImGui::GetTime());
list->PopClipRect();
ImGui::Dummy(ImVec2(Width, end.y - at.y));
}
}
void editor_mode::render_tutorial_goals(tutorial_lesson const &Lesson)
{
ImGui::SeparatorText(STR_C("Mouse and keys"));
for (auto const &entry : Lesson.inputs)
tutorial_input(entry.keys, entry.text, 150.0f * Global.ui_scale);
if (false == Lesson.goals.empty())
ImGui::SeparatorText(STR_C("Try it"));
for (auto const &entry : Lesson.goals)
{
bool const reached{m_first_steps_done[static_cast<std::size_t>(entry.step)]};
ImGui::TextColored(reached ? ImVec4(0.4f, 0.9f, 0.4f, 1.0f) : ImGui::GetStyleColorVec4(ImGuiCol_Text), "%s", reached ? "[x]" : "[ ]");
ImGui::SameLine();
if (reached)
ImGui::TextDisabled("%s", STR_C(entry.text));
else
ImGui::TextWrapped("%s", STR_C(entry.text));
}
if (Lesson.action == tutorial_lesson::trial::lay && ui()->mode() == nodebank_panel::TRACK && false == GfxRenderer->Mouse_Hit())
ImGui::TextColored(ImVec4(1.0f, 0.7f, 0.2f, 1.0f), "%s", STR_C("There is no ground under the mouse: the track will lie flat at height 0. Real ground: F5"));
}
void editor_mode::render_tutorial_trial(tutorial_lesson const &Lesson)
{
using trial = tutorial_lesson::trial;
if (Lesson.action == trial::top)
{
if (ImGui::Button(Global.EditorOrtho ? STR_C("3D view (O)") : STR_C("Top view, orthographic (O)")))
toggle_ortho();
return;
}
if (Lesson.action == trial::terrain)
{
ImGui::BeginDisabled(current_work_area() == work_area::terrain);
if (ImGui::Button(STR_C("Terrain (F5)")))
choose_work_area(work_area::terrain);
ImGui::EndDisabled();
return;
}
// a tool of Tracks: the tab its button opens, and the one beside it which counts as that tool open as well
struct tool_button
{
char const *label;
track_tab tab;
track_tab also;
};
tool_button button{};
switch (Lesson.action)
{
case trial::lay:
button = {STR_C("Lay track (L)"), track_tab::lay, track_tab::lay};
break;
case trial::turnout:
button = {STR_C("The switch tool (T)"), track_tab::turnout, track_tab::turnout};
break;
case trial::select:
button = {STR_C("The select tool (Esc)"), track_tab::path, track_tab::path};
break;
case trial::profile:
button = {STR_C("The profile tool (P)"), track_tab::profile, track_tab::profile};
break;
case trial::signals:
button = {STR_C("The signals tool (I)"), track_tab::signals, track_tab::signals};
break;
case trial::station:
button = {STR_C("The station tool"), track_tab::station, track_tab::station_signals};
break;
case trial::plan:
button = {STR_C("The plan tool (G)"), track_tab::straights, track_tab::route};
break;
case trial::trainset:
button = {STR_C("The trainset tool"), track_tab::lineside, track_tab::lineside};
break;
default:
return;
}
// the trainset tool is open only with its part of the tab expanded
auto const in_use{ui()->mode() == nodebank_panel::TRACK && (m_track_tab == button.tab || m_track_tab == button.also) && (Lesson.action != trial::trainset || m_vehicle.tab().open)};
ImGui::BeginDisabled(in_use);
if (ImGui::Button(button.label))
{
choose_work_area(work_area::tracks);
show_track_tab(button.tab);
if (Lesson.action == trial::trainset)
m_vehicle.tab().expand = true;
}
ImGui::EndDisabled();
}
void editor_mode::render_tutorial_pages()
{
auto const &lessons{tutorial_lessons()};
auto const count{static_cast<int>(lessons.size())};
ImGui::BeginDisabled(m_tutorial_page == 0);
if (ImGui::Button(STR_C("< Back")))
--m_tutorial_page;
ImGui::EndDisabled();
ImGui::SameLine();
auto const dot{ImGui::GetFrameHeight()};
for (int index = 0; index < count; ++index)
{
ImGui::PushID(index);
auto const at{ImGui::GetCursorScreenPos()};
if (ImGui::InvisibleButton("##lesson", ImVec2(dot * 0.8f, dot)))
m_tutorial_page = index;
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C(lessons[index].title));
ImVec2 const centre{at.x + dot * 0.4f, at.y + dot * 0.5f};
auto const colour{tutorial_done(lessons[index]) ? IM_COL32(100, 230, 100, 255) : ImGui::GetColorU32(ImGuiCol_TextDisabled)};
if (index == m_tutorial_page)
ImGui::GetWindowDrawList()->AddCircle(centre, dot * 0.3f, kGestureLit, 0, 2.0f);
ImGui::GetWindowDrawList()->AddCircleFilled(centre, dot * 0.17f, colour);
ImGui::PopID();
ImGui::SameLine(0.0f, 0.0f);
}
ImGui::SameLine();
if (m_tutorial_page + 1 < count)
{
if (ImGui::Button(STR_C("Next >")))
++m_tutorial_page;
}
else if (ImGui::Button(STR_C("Close")))
m_first_steps_open = false;
bool show{EditorSettings.first_steps()};
if (ImGui::Checkbox(STR_C("Show when the editor opens"), &show))
{
EditorSettings.first_steps(show);
EditorSettings.save();
}
}
void editor_mode::render_shortcuts_window()
{
if (false == m_shortcuts_open)
return;
struct shortcut
{
char const *keys;
char const *action;
};
struct group
{
char const *title;
std::vector<shortcut> shortcuts;
};
static std::vector<group> const groups{
{"General",
{{"F1", "This list of shortcuts"},
{"Ctrl+S", "Save the scenery"},
{"Ctrl+Z / Ctrl+Y", "Undo / redo"},
{"Ctrl+Space", "Unpin all docks for the most of the view, or pin them back"},
{"F9", "Log"},
{"PrtScr", "Screenshot"},
{"Ctrl+Shift+F11", "Export scenery dump"},
{"F11", "Leave the editor"},
{"F10", "Quit the simulator"}}},
{"Fields of work", {{"F2", "Surroundings"}, {"F3", "Tracks"}, {"F4", "Roads"}, {"F5", "Terrain"}}},
{"Camera",
{{"RMB held, mouse", "Look around"},
{"RMB held, W S A D", "Fly forwards, back, left, right (arrows in the legacy scheme)"},
{"RMB held, E / Q", "Fly up / down (Page Up / Page Down in the legacy scheme)"},
{"F", "Fly to the selected"},
{"O", "Top view, orthographic"}}},
{"Ride along the track",
{{"Y", "Ride along the track under the cursor, the tools working on; Y again leaves"},
{"RMB held, W / S", "Drive; Shift faster, Ctrl+W / Ctrl+Down much faster"},
{"RMB held, A / D", "Next track on the left / right"},
{"Z", "The way at the next switch (it isn't thrown)"},
{"RMB held, E / Q; wheel", "Higher / lower"},
{"Ctrl+M", "Minimap, stations by their W4"}}},
{"Gizmo",
{{"Q / W / E", "Translate / rotate / scale; in the tracks they bring the hidden gizmo out"},
{"R", "Local space"},
{"Ctrl held while dragging", "Snap to the step set in the gizmo settings"},
{"Tool options, Gizmo, Off", "Hides the gizmo, so nothing moves by chance; in the tracks it is hidden until Q, W or E"}}},
{"Surroundings",
{{"1 2 3 4 5", "Select, insert, brush, area fill, copy to bank"},
{"LMB", "Select a model, or place one in the insert and brush modes"},
{"Shift+LMB", "Add a model to the selection, or take it out"},
{"Ctrl+RMB", "Menu of the selection at the cursor"},
{"Ctrl+C / Ctrl+V", "Copy the selected models / paste them at the cursor"},
{"Ctrl+G / Ctrl+Shift+G", "Group the selected models / take their group apart"},
{"End", "Drop the selected onto the ground, each model of a group by itself"},
{"Del", "Delete the selected"},
{"B", "Bend the selected along the track"},
{"Backspace", "Area fill: take the last point of the outline back"}}},
{"Tracks",
{{"Esc", "Select tool; cancels what's going on first"},
{"L / T / U", "Lay track / switch / turntable"},
{"H", "Throw the selected switch over to its other path, in the editor only"},
{"G (C)", "Plan: a straight, its ends and the middle; a curve, its vertex and radius"},
{"I / X / B", "Signals / kilometrage / posts along the track"},
{"P / V / J", "Profile / speed / joints"},
{"Ctrl+F", "Find a track by its name"},
{"Shift+LMB", "Add a track to the selection, drag: a box"},
{"RMB on a track", "What can be done with it"},
{"K", "Split the selected track at the cursor, a break of the grade at the height of the ground"},
{"L, LMB, LMB...", "Lay track: each click a bend of the straights, eased with a large curve; Enter lays them; a click on a free end goes on from it"},
{"Tab while laying", "The points clicked next go along the middle of a curve seen on the photo, which ends at the last one; Tab again: back to the bends"},
{"Drag from a free end", "Lead a track out of it: a straight, an arc and a straight where a curve is traced (Tab); onto another free end: join the two"},
{"Ctrl+wheel", "Dragging from a free end: radius"},
{"Shift+wheel", "Dragging over another free end: another way to join"},
{"0-9 while laying", "Type the length of the next straight, Enter puts the point"},
{"Alt held while laying", "The direction in steps of 15 degrees, or 1:9, 1:6.6... to a straight track; the first click with Alt goes onto the axis of the track"},
{"N", "Parallel track: LMB on a track, then the side and the spacing"},
{"Shift+LMB on a track in L", "The Parallel tool (N) with it"},
{"Enter / Backspace", "Laying track: lay the points clicked / take back the last point"},
{"Esc twice while laying", "Drop the points clicked and not laid yet"},
{"Del", "Delete the selected tracks"}}},
{"Plan of a curve",
{{"G, LMB on a curve", "Its design: the vertex and the radius to drag in the view, the curvature in the panel"},
{"Drag a plateau of the curvature band", "The radius of that arc"},
{"Drag a ramp sideways", "The length of the transition curve"},
{"Wheel over the band", "The radius by 5%, a transition by 5 m"},
{"Apply to the scenery", "Lay the paths by the design; Ctrl+Z takes it back"}}},
{"Trainset",
{{"B, Trainset, LMB on a track", "Where the train stands"},
{"LMB LMB on the list", "Adds the vehicle to the train"},
{"LMB on a vehicle of the train", "Moves it, turns it around or takes it out"},
{"Place and drive", "Puts the train on the track and gives the cab at once"}}},
{"Vertical profile",
{{"P, LMB on a track", "The grade line of its whole line, in the strip under the view"},
{"Shift+LMB box", "The route between the outermost of the tracks, through the switches"},
{"LMB + move on a point", "Change the grade before and after it"},
{"LMB LMB on the line", "Add a point"},
{"Del", "Remove the chosen point"},
{"Wheel", "Zoom along the line"},
{"Ctrl+wheel", "Stretch the heights"},
{"RMB + move", "Move the view along the line"},
{"Apply in the scenery", "Write the heights into the tracks; Ctrl+Z takes them back"}}},
{"Roads",
{{"Esc", "Finish the road being built"},
{"Ctrl held", "Build: straight ahead"},
{"Shift+LMB", "Select: add a point, or take it out"},
{"K", "Split the road at the cursor, or make a point there"},
{"Del", "Delete the selected"}}},
{"Terrain", {{"LMB", "Sculpt: raise; smooth: even out; chunks: add one"}, {"Shift+LMB", "Sculpt: lower; chunks: delete one"}}},
};
ImGui::SetNextWindowSize(ImVec2(620.0f * Global.ui_scale, 560.0f * Global.ui_scale), ImGuiCond_FirstUseEver);
keep_on_screen("###shortcuts");
// it floats over the view, read better on a solid background
ImGui::SetNextWindowBgAlpha(0.97f);
if (ImGui::Begin((std::string(STR_C("Keyboard shortcuts")) + "###shortcuts").c_str(), &m_shortcuts_open, ImGuiWindowFlags_NoCollapse))
{
static char filter[64]{};
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x * 0.6f);
ImGui::InputTextWithHint("##shortcutfilter", STR_C("Find: a key or what it does"), filter, sizeof(filter));
ImGui::SameLine();
if (ImGui::Button(STR_C("Tutorial")))
m_first_steps_open = true;
auto const lower = [](std::string Text) {
std::transform(Text.begin(), Text.end(), Text.begin(), [](unsigned char const Character) { return static_cast<char>(std::tolower(Character)); });
return Text;
};
auto const wanted{lower(filter)};
auto const matches = [&](shortcut const &Item) { return wanted.empty() || lower(STR_C(Item.keys)).find(wanted) != std::string::npos || lower(STR_C(Item.action)).find(wanted) != std::string::npos; };
auto const picture{std::round(ImGui::GetTextLineHeight() * 1.15f)};
for (auto const &entry : groups)
{
if (std::none_of(entry.shortcuts.begin(), entry.shortcuts.end(), matches))
continue;
ImGui::SeparatorText(STR_C(entry.title));
if (false == ImGui::BeginTable(entry.title, 2, ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp))
continue;
ImGui::TableSetupColumn("##keys", ImGuiTableColumnFlags_WidthFixed, 250.0f * Global.ui_scale);
ImGui::TableSetupColumn("##action", ImGuiTableColumnFlags_WidthStretch, 1.0f);
for (auto const &item : entry.shortcuts)
{
if (false == matches(item))
continue;
ImGui::TableNextColumn();
auto const words{gesture_picture(item.keys)};
if (false == words.empty())
{
auto const at{ImGui::GetCursorScreenPos()};
auto const drawn{gesture_draw(*ImGui::GetWindowDrawList(), at, picture, words, ImGui::GetColorU32(ImGuiCol_Text))};
ImGui::Dummy(ImVec2(drawn, picture));
ImGui::SameLine();
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + std::round((picture - ImGui::GetTextLineHeight()) * 0.5f));
}
if (words.empty() || false == gesture_keys_only(item.keys))
ImGui::TextColored(ImGui::GetStyleColorVec4(ImGuiCol_CheckMark), "%s", STR_C(item.keys));
else
ImGui::NewLine();
ImGui::TableNextColumn();
if (false == words.empty())
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + std::round((picture - ImGui::GetTextLineHeight()) * 0.5f));
ImGui::TextWrapped("%s", STR_C(item.action));
}
ImGui::EndTable();
}
}
ImGui::End();
}
bool editor_mode::gizmo_shown() const
{
return ui()->mode() == nodebank_panel::TRACK ? m_track_gizmo_on : m_gizmo_enabled;
}
void editor_mode::render_gizmo_options()
{
if (ImGui::Button(Global.EditorOrtho ? STR_C("3D view (O)") : STR_C("Top view, orthographic (O)")))
toggle_ortho();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", Global.EditorOrtho ? STR_C("Wheel zooms. Hold the wheel and drag to pan.") : STR_C("Top-down orthographic view"));
if (Global.EditorOrtho)
{
ImGui::SameLine();
ImGui::SetNextItemWidth(120.0f);
ImGui::SliderFloat(STR_C("Extent (m), wheel / MMB pan"), &Global.EditorOrthoExtent, 5.0f, 50000.0f, "%.0f", ImGuiSliderFlags_Logarithmic);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Wheel zooms. Hold the wheel and drag to pan."));
ImGui::SameLine();
render_top_tracks_choice(true);
}
if (ui()->mode() == nodebank_panel::TRACK && m_track_tab != track_tab::path && m_track_tab != track_tab::turnout && m_track_tab != track_tab::turntable && m_track_tab != track_tab::signals &&
m_track_tab != track_tab::lineside)
return;
auto &enabled{ui()->mode() == nodebank_panel::TRACK ? m_track_gizmo_on : m_gizmo_enabled};
int op{enabled ? static_cast<int>(m_gizmo_op) : -1};
if (segmented("gizmo", op,
{{-1, STR_C("Off"), "The gizmo hidden: the selected things won't move by chance"},
{static_cast<int>(gizmo_operation::translate), STR_C("Translate (Q)"), "Arrows which move the selected thing"},
{static_cast<int>(gizmo_operation::rotate), STR_C("Rotate (W)"), "Rings which turn the selected thing"},
{static_cast<int>(gizmo_operation::scale), STR_C("Scale (E)"), "Handles which scale the selected thing; a track is lengthened"}}))
{
enabled = op >= 0;
if (enabled)
m_gizmo_op = static_cast<gizmo_operation>(op);
}
if (!enabled)
return;
if (terrain_brush() || terrain_clicking())
{
ImGui::TextDisabled(STR_C("Suspended while editing terrain"));
return;
}
if (m_gizmo_op != gizmo_operation::scale) // ImGuizmo always scales in local space
ImGui::Checkbox(STR_C("Local space (R)"), &m_gizmo_local);
if (m_gizmo_op == gizmo_operation::translate)
{
ImGui::SetNextItemWidth(120.0f);
ImGui::InputFloat(STR_C("Snap (hold Ctrl)"), &m_gizmo_snap);
if (m_gizmo_snap < 0.0f)
m_gizmo_snap = 0.0f;
}
if (m_instance != 0)
{
auto const located = editor_includes::parameter_with_role(m_include.info, "pos.x") != 0 || editor_includes::parameter_with_role(m_include.info, "pos.y") != 0 ||
editor_includes::parameter_with_role(m_include.info, "pos.z") != 0;
ImGui::TextDisabled("%s", false == located ? STR_C("Include selected, no position parameters described") :
false == m_include.placement ? STR_C("Include selected, no gizmo under rotate or scale") :
STR_C("Include selected, axes as its template allows"));
}
else if (!m_node && ui()->mode() != nodebank_panel::TRACK)
ImGui::TextDisabled(STR_C("No node selected"));
}
void editor_mode::render_gizmo()
{
if (straights_active())
{
render_straight_gizmo();
return;
}
if (route_active())
{
render_route_gizmo();
return;
}
// the transform gizmo is suppressed while editing terrain, so the brush/chunk tool owns the mouse
if (!gizmo_shown() || terrain_brush() || terrain_clicking())
{
m_gizmo_using = false;
m_track_gizmo_using = false;
return;
}
if (ui()->mode() == nodebank_panel::TRACK && m_track_tab == track_tab::path && m_track_set.size() >= 2)
{
render_track_set_gizmo();
return;
}
if (!m_node && m_instance == 0)
{
m_gizmo_using = false;
return;
}
if (selected_track())
{
// in the track modes the whole path is moved only while selecting, or the switch in its mode
if (ui()->mode() != nodebank_panel::TRACK || (m_track_tab == track_tab::path && false == m_point_drag.active) || (m_track_tab == track_tab::turnout && selected_track()->eType == tt_Switch))
render_track_gizmo();
else
m_track_gizmo_using = false;
return;
}
ImGuizmo::BeginFrame();
ImGuizmo::SetOrthographic(Global.EditorOrtho);
ImGuiIO const &io = ImGui::GetIO();
ImGuizmo::SetRect(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y);
// the view matrix comes from the most recent color pass and is camera-relative
// (rotation only), so the gizmo is positioned relative to the camera as well.
glm::mat4 const view = GfxRenderer->Camera_View_Matrix();
glm::dvec3 const camerapos = GfxRenderer->Camera_Position();
// the engine's own projection bakes in reverse-Z (and screen orientation), which ImGuizmo
// doesn't expect; rebuild a clean, standard perspective that matches the rendered view.
// for the main viewport the engine uses a symmetric frustum with this exact fov/aspect.
float const aspect = io.DisplaySize.y > 0.0f ? io.DisplaySize.x / io.DisplaySize.y : 1.0f;
glm::mat4 const projection = editor_mode::projection_matrix(aspect);
if (!m_node)
{
render_include_gizmo(view, projection, camerapos);
return;
}
// rotation/scale are only meaningful for instanced models; other node types translate only
TAnimModel *model = dynamic_cast<TAnimModel *>(m_node);
glm::vec3 const relativepos = glm::vec3(m_node->location() - camerapos);
glm::vec3 const angles = model ? model->Angles() : glm::vec3(0.0f);
glm::vec3 const scalevec = model ? model->Scale() : glm::vec3(1.0f);
// build the gizmo model matrix from the node's current translation + rotation + scale
float const translation[3] = {relativepos.x, relativepos.y, relativepos.z};
float const rotation[3] = {angles.x, angles.y, angles.z};
float const scale[3] = {scalevec.x, scalevec.y, scalevec.z};
glm::mat4 matrix(1.0f);
ImGuizmo::RecomposeMatrixFromComponents(translation, rotation, scale, glm::value_ptr(matrix));
// map the editor's transform mode onto ImGuizmo; fall back to translate for non-models
ImGuizmo::OPERATION operation = ImGuizmo::TRANSLATE;
EditorSnapshot::Action action = EditorSnapshot::Action::Move;
if (model && m_gizmo_op == gizmo_operation::rotate)
{
operation = ImGuizmo::ROTATE;
action = EditorSnapshot::Action::Rotate;
}
else if (model && m_gizmo_op == gizmo_operation::scale)
{
operation = ImGuizmo::SCALE;
action = EditorSnapshot::Action::Scale;
}
ImGuizmo::MODE const mode = m_gizmo_local ? ImGuizmo::LOCAL : ImGuizmo::WORLD;
// optional snapping while Ctrl is held (metres / degrees / scale factor depending on mode)
glm::vec3 snapvalue(0.0f);
if (operation == ImGuizmo::TRANSLATE)
snapvalue = glm::vec3(m_gizmo_snap);
else if (operation == ImGuizmo::ROTATE)
snapvalue = glm::vec3(5.0f);
else
snapvalue = glm::vec3(0.1f);
float const *snap = Global.ctrlState && snapvalue.x > 0.0f ? glm::value_ptr(snapvalue) : nullptr;
// the change of this frame alone: a turn is taken from it, not from the angles the whole matrix decomposes into,
// which can't tell a turn of more than 90 degrees about the vertical axis (it comes out as 180 - angle with the
// other two axes flipped, and the model turned back and forth while dragged one way)
glm::mat4 delta(1.0f);
ImGuizmo::Manipulate(glm::value_ptr(view), glm::value_ptr(projection),
operation, mode, glm::value_ptr(matrix), glm::value_ptr(delta), snap);
if (ImGuizmo::IsUsing())
{
// record a single undo snapshot at the start of the drag
if (!m_gizmo_using)
{
// the models selected along with it and their groups go into the same step of the history
selection_snapshots(action);
m_gizmo_using = true;
}
float newtranslation[3], newrotation[3], newscale[3];
ImGuizmo::DecomposeMatrixToComponents(glm::value_ptr(matrix), newtranslation, newrotation, newscale);
if (operation == ImGuizmo::ROTATE && model)
{
// the turn of this frame, small, so its angles decompose with no ambiguity
float deltatranslation[3], deltarotation[3], deltascale[3];
ImGuizmo::DecomposeMatrixToComponents(glm::value_ptr(delta), deltatranslation, deltarotation, deltascale);
auto const wrapped = [](float Angle) { return Angle - 360.0f * std::floor((Angle + 180.0f) / 360.0f); };
glm::vec3 const turned{wrapped(deltarotation[0]), wrapped(deltarotation[1]), wrapped(deltarotation[2])};
m_editor.rotate(model, turned, 0.0f);
selection_follow(glm::dvec3(0.0), turned, model->location());
}
else if (operation == ImGuizmo::SCALE && model)
{
model->Scale(glm::vec3(newscale[0], newscale[1], newscale[2]));
}
else
{
glm::dvec3 const newworldpos = camerapos + glm::dvec3(newtranslation[0], newtranslation[1], newtranslation[2]);
// pass Snaptoground == true so the gizmo's Y component is applied (free 3D move)
auto const before{m_node->location()};
m_editor.translate(m_node, newworldpos, true);
selection_follow(m_node->location() - before, glm::vec3(0.0f), m_node->location());
}
}
else
{
m_gizmo_using = false;
}
}
void editor_mode::render_include_gizmo(glm::mat4 const &View, glm::mat4 const &Projection, glm::dvec3 const &Camerapos)
{
m_gizmo_using = false;
if (false == m_include.placement || false == m_include.issue.empty())
return;
// parameters of the template the gizmo can drive, as indices of their values; -1 where the template has none
static char const *const positionroles[] = {"pos.x", "pos.y", "pos.z"};
static char const *const rotationroles[] = {"rot.x", "rot.y", "rot.z"};
auto const numeric_parameter = [this](char const *Role, double &Value) {
auto const index = editor_includes::parameter_with_role(m_include.info, Role) - 1;
if (index < 0 || index >= static_cast<int>(m_include.values.size()))
return -1;
auto const &text = m_include.values[index];
char *end = nullptr;
auto const number = std::strtod(text.c_str(), &end);
if (text.empty() || *end != '\0')
return -1;
Value = number;
return index;
};
int position[3], rotation[3];
double location[3] = {0.0, 0.0, 0.0}, angles[3] = {0.0, 0.0, 0.0};
auto positions = 0, rotations = 0;
for (auto axis = 0; axis < 3; ++axis)
{
position[axis] = numeric_parameter(positionroles[axis], location[axis]);
rotation[axis] = numeric_parameter(rotationroles[axis], angles[axis]);
positions += (position[axis] >= 0 ? 1 : 0);
rotations += (rotation[axis] >= 0 ? 1 : 0);
}
if (positions == 0)
return; // nothing tells where the include is
// only the axes the template has parameters for can be manipulated. a signal stands on the ground by its track: it is moved in
// the plan, at the height it has
// not waited for: a scan started again while a signal is selected would stop the editor for its whole length
if (m_include.info.category == "signal")
signal_scan(false);
auto const signal{signal_template_of(m_include.target) != nullptr};
auto operation = 0;
for (auto axis = 0; axis < 3; ++axis)
{
if (m_gizmo_op == gizmo_operation::translate && position[axis] >= 0 && false == (signal && axis == 1))
operation |= (ImGuizmo::TRANSLATE_X << axis);
else if (m_gizmo_op == gizmo_operation::rotate && rotation[axis] >= 0)
operation |= (ImGuizmo::ROTATE_X << axis);
}
if (operation == 0)
return;
auto const translating = (m_gizmo_op == gizmo_operation::translate);
// the parameters are relative to the origin in effect at the directive
auto const &offset = scene::Layers.instance(m_instance).context.offset;
glm::vec3 const relativepos = glm::vec3(offset + glm::dvec3{location[0], location[1], location[2]} - Camerapos);
float const translation[3] = {relativepos.x, relativepos.y, relativepos.z};
float const rotationangles[3] = {static_cast<float>(angles[0]), static_cast<float>(angles[1]), static_cast<float>(angles[2])};
float const scale[3] = {1.0f, 1.0f, 1.0f};
glm::mat4 matrix(1.0f);
ImGuizmo::RecomposeMatrixFromComponents(translation, rotationangles, scale, glm::value_ptr(matrix));
// moving along the axes of the include would need all three of them
ImGuizmo::MODE const mode = (m_gizmo_local && (false == translating || positions == 3)) ? ImGuizmo::LOCAL : ImGuizmo::WORLD;
glm::vec3 const snapvalue(translating ? m_gizmo_snap : 5.0f);
float const *snap = Global.ctrlState && snapvalue.x > 0.0f ? glm::value_ptr(snapvalue) : nullptr;
// in the signal tool a press on the mast drags the signal along its track: the gizmo, whose up axis runs along the
// mast, gives way over a signal and while one is dragged
auto const yield{ui()->mode() == nodebank_panel::TRACK && m_track_tab == track_tab::signals && false == ImGuizmo::IsUsing() && (m_signal.moving || m_signal.placing || signal_hit() >= 0)};
ImGuizmo::Enable(false == yield);
ImGuizmo::Manipulate(glm::value_ptr(View), glm::value_ptr(Projection), static_cast<ImGuizmo::OPERATION>(operation), mode, glm::value_ptr(matrix), nullptr, snap);
ImGuizmo::Enable(true);
if (false == ImGuizmo::IsUsing())
{
signal_gizmo_end();
return;
}
m_gizmo_using = true;
signal_gizmo_begin();
float newtranslation[3], newrotation[3], newscale[3];
ImGuizmo::DecomposeMatrixToComponents(glm::value_ptr(matrix), newtranslation, newrotation, newscale);
if (translating)
{
glm::dvec3 const newlocation = Camerapos + glm::dvec3{newtranslation[0], newtranslation[1], newtranslation[2]} - offset;
for (auto axis = 0; axis < 3; ++axis)
{
if (position[axis] >= 0)
m_include.values[position[axis]] = editor_includes::number(newlocation[axis]);
}
}
else
{
if (rotations == 1)
{
// the decomposition spreads a turn of more than a quarter over all three angles;
// with a single axis to turn around the angle is taken straight from the matrix
newrotation[0] = glm::degrees(std::atan2(matrix[1][2], matrix[1][1]));
newrotation[1] = glm::degrees(std::atan2(-matrix[0][2], matrix[0][0]));
newrotation[2] = glm::degrees(std::atan2(matrix[0][1], matrix[0][0]));
}
for (auto axis = 0; axis < 3; ++axis)
{
if (rotation[axis] >= 0)
m_include.values[rotation[axis]] = editor_includes::number(clamp_circular(newrotation[axis]));
}
}
apply_include();
signal_gizmo_step();
}
glm::mat4 editor_mode::projection_matrix(float const Aspect)
{
if (Global.EditorOrtho)
{
auto const height{Global.EditorOrthoExtent};
return glm::ortho(-height * Aspect, height * Aspect, -height, height, -10000.0f, 10000.0f);
}
return glm::perspective(glm::radians(Global.FieldOfView / Global.ZoomFactor), Aspect, 0.1f, 10000.0f);
}
void editor_mode::toggle_ortho()
{
// the view from above isn't one from the track, the ride ends
m_ride.leave();
Global.EditorOrtho = !Global.EditorOrtho;
// either way: a scenery made in the wizard opens seen from above already
first_step_done(first_step::top);
if (Global.EditorOrtho)
{
m_ortho_pitch = Camera.Angle.x;
Camera.Angle.x = -M_PI_2;
Camera.Angle.z = 0.0;
}
else
{
Camera.Angle.x = m_ortho_pitch;
ortho_pan_stop();
camera_above_ground();
}
}
void editor_mode::ortho_pan_to(double const Horizontal, double const Vertical)
{
auto const &io{ImGui::GetIO()};
if (io.DisplaySize.y <= 0.0f)
return;
auto const dx{Horizontal - m_ortho_pan_cursor.x};
auto const dy{Vertical - m_ortho_pan_cursor.y};
m_ortho_pan_cursor = {Horizontal, Vertical};
auto const scale{Global.EditorOrtho ? 2.0 * static_cast<double>(Global.EditorOrthoExtent) / static_cast<double>(io.DisplaySize.y) : m_pan_metres};
// looked at from the side, a pixel up the screen is more of the ground than one across
auto const along{Global.EditorOrtho ? 1.0 : 1.0 / std::max(0.25, std::abs(std::sin(static_cast<double>(Camera.Angle.x))))};
auto const world{RotateY(glm::dvec3{dx * scale, 0.0, dy * scale * along}, static_cast<double>(Camera.Angle.y))};
m_ride.leave();
Camera.Pos.x -= world.x;
Camera.Pos.z -= world.z;
Camera.Velocity = {};
m_focus_active = false;
Global.pCamera = Camera;
}
void editor_mode::ortho_pan_stop()
{
m_ortho_pan = false;
}
void editor_mode::on_scroll(double const Xoffset, double const Yoffset)
{
if (false == ImGui::GetIO().WantCaptureMouse && track_wheel(Yoffset != 0.0 ? Yoffset : Xoffset))
return;
// riding along the track the wheel the tools left raises and lowers the camera over the rails
if (m_ride.active())
{
if (false == ImGui::GetIO().WantCaptureMouse && Yoffset != 0.0)
m_ride.wheel(std::clamp(Yoffset, -2.0, 2.0));
return;
}
// a swipe on a touchpad goes on after the cursor left the window, with no place for the ray under it
if (ImGui::GetIO().WantCaptureMouse || Yoffset == 0.0 || false == ImGui::IsMousePosValid())
return;
// a fast swipe on a touchpad can come as one big step
auto const notches{std::clamp(Yoffset, -2.0, 2.0)};
if (false == Global.EditorOrtho)
{
// in 3D the camera flies along the ray under the cursor, by a share of the distance to what it points at;
// a touchpad sends many small steps, together they come to the same
screen_projection const projection;
glm::dvec3 origin;
glm::dvec3 direction;
projection.ray(ImGui::GetIO().MousePos, origin, direction);
// with nothing under the cursor, the ground under the camera stands for it: the level 0 can lie far below the terrain
std::vector<char> found;
auto const below{ground_heights({Camera.Pos}, false, &found)};
auto const ground{found[0] != 0 ? below[0] : 0.0};
auto const height{std::max(2.0, std::abs(Camera.Pos.y - ground))};
glm::dvec3 level;
auto distance{std::min(GfxRenderer->Mouse_Hit() ? glm::length(GfxRenderer->Mouse_Position()) : projection.on_level(ImGui::GetIO().MousePos, ground, level) ? glm::distance(level, origin) : 2.0 * height, 20000.0)};
// backing away from a point on the horizon would throw the camera kilometres up
if (notches < 0.0)
distance = std::min(distance, std::max(100.0, 4.0 * height));
auto step{distance * (1.0 - std::pow(0.85, notches))};
// no nearer than 2 m to the point, not through it
if (notches > 0.0)
step = std::max(0.0, std::min(step, distance - 2.0));
// a point under the cursor which isn't a number, as one on a broken path, moves the camera nowhere
if (false == std::isfinite(step) || glm::any(glm::isnan(direction)) || glm::any(glm::isinf(direction)))
return;
Camera.Pos += direction * step;
Global.pCamera = Camera;
return;
}
auto const before{Global.EditorOrthoExtent};
auto const after{std::clamp(before * static_cast<float>(std::pow(0.85, notches)), 5.0f, 50000.0f)};
screen_projection const projection;
glm::dvec3 origin;
glm::dvec3 direction;
projection.ray(ImGui::GetIO().MousePos, origin, direction);
auto const scale{static_cast<double>(after) / static_cast<double>(before)};
Camera.Pos.x = origin.x + (Camera.Pos.x - origin.x) * scale;
Camera.Pos.z = origin.z + (Camera.Pos.z - origin.z) * scale;
Global.EditorOrthoExtent = after;
Global.pCamera = Camera;
}
void editor_mode::update_camera(double const Deltatime)
{
Camera.Update();
if (Global.EditorOrtho)
{
Camera.Angle.x = -M_PI_2;
Camera.Angle.z = 0.0;
}
// riding along the track the camera goes where the track takes it, a framing doesn't fly it off
if (m_ride.active())
{
m_focus_active = false;
m_ride.advance(Camera, Deltatime, m_input.mouse.button(GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
}
// focus animation runs after Camera.Update() so it overrides any residual velocity/rotation;
// it smoothly drives both position and orientation toward the framed object
if (m_focus_active)
{
m_focus_time += Deltatime;
double t = m_focus_duration > 0.0 ? m_focus_time / m_focus_duration : 1.0;
if (t >= 1.0)
t = 1.0;
// smoothstep easing
float const s = static_cast<float>(t * t * (3.0 - 2.0 * t));
Camera.Pos = glm::mix(m_focus_start_pos, m_focus_target_pos, static_cast<double>(s));
// interpolate angles, taking the shortest path around the yaw wrap-around
constexpr float TWO_PI = 6.283185307179586f;
float const dyaw = std::remainder(m_focus_target_angle.y - m_focus_start_angle.y, TWO_PI);
Camera.Angle.x = m_focus_start_angle.x + (m_focus_target_angle.x - m_focus_start_angle.x) * s;
Camera.Angle.y = m_focus_start_angle.y + dyaw * s;
Camera.Angle.z = m_focus_start_angle.z + (m_focus_target_angle.z - m_focus_start_angle.z) * s;
// suppress any residual fly velocity so it doesn't fight the animation
Camera.Velocity = glm::dvec3(0.0);
if (t >= 1.0)
m_focus_active = false;
}
// reset window state (will be set again if UI requires it)
Global.CabWindowOpen = false;
// a camera at no finite place, as a zoom or a framing aimed at a broken path could put it, draws nothing sane and can
// stall the graphics card: it goes back to where it last was
auto const number = [](auto const &Vector) { return false == glm::any(glm::isnan(Vector)) && false == glm::any(glm::isinf(Vector)); };
if (number(Camera.Pos) && number(Camera.Angle))
{
m_camera_finite_pos = Camera.Pos;
m_camera_finite_angle = Camera.Angle;
m_camera_finite = true;
}
else if (m_camera_finite)
{
WriteLog("Editor: the camera went to no finite place, back where it was", logtype::generic);
Camera.Pos = m_camera_finite_pos;
Camera.Angle = m_camera_finite_angle;
Camera.Velocity = glm::dvec3(0.0);
m_focus_active = false;
}
// publish camera back to global copy
Global.pCamera = Camera;
}
glm::dvec3 editor_mode::camera_look() const
{
return {-std::sin(Camera.Angle.y), 0.0, -std::cos(Camera.Angle.y)};
}
void editor_mode::ride_enter()
{
// the track under the cursor, else the one nearest the ground under it, else the one nearest the camera
auto under{track_under_cursor(nullptr, 30.0f)};
auto *track{under.track};
auto path{under.path};
auto point{under.point};
if (track == nullptr && ImGui::IsMousePosValid())
{
point = std::as_const(*this).cursor_ground();
track = kilometrage::track_near(point, 30.0, path);
}
if (track == nullptr)
{
point = Camera.Pos;
track = kilometrage::track_near(point, 200.0, path);
}
if (false == m_ride.enter(track, path, point, camera_look(), true))
ui()->set_status(STR_C("No track nearby"), true);
}
void editor_mode::enter()
{
m_statebackup = {Global.pCamera, FreeFlyModeFlag, Global.ControlPicking};
// the editor changes the tracks, their sleepers are worked out as they're needed
track_sleepers::lay_in_background(false);
// the editor may remove the tracks and the vehicles the recording refers to: it ends here
simulation::Blackbox.close();
// the cursor drawn into the picture is the editor's: back on when it comes back, if it was chosen
ui_layer::draw_cursor(m_draw_cursor);
apply_camera_settings();
Global.EditorCameraOn = true;
if (simulation::Region != nullptr)
{
// the editor works with geometry of the whole scenery, including the parts of binary terrain which weren't needed so far
simulation::Region->load_terrain();
}
Camera = Global.pCamera;
if (!FreeFlyModeFlag)
{
auto const *vehicle = Camera.m_owner;
if (vehicle)
{
const int cab = vehicle->MoverParameters->CabOccupied == 0 ? 1 : vehicle->MoverParameters->CabOccupied;
const glm::dvec3 left = vehicle->VectorLeft() * (double)cab;
Camera.Pos = glm::dvec3(Camera.Pos.x, vehicle->GetPosition().y, Camera.Pos.z) + left * vehicle->GetWidth() + glm::dvec3(1.25f * left.x, 1.6f, 1.25f * left.z);
Camera.m_owner = nullptr;
Camera.LookAt = vehicle->GetPosition();
Camera.RaLook(); // single camera reposition
FreeFlyModeFlag = true;
}
}
if (Global.editor_session && false == m_opened_at_track)
{
m_opened_at_track = true;
auto const saved{scene::Layers.empty() ? std::vector<std::string>{} : scene::Layers.marked(scene::layer_handle{1}, "//$c")};
if (false == saved.empty())
{
std::istringstream values(saved.front());
glm::dvec3 position{0.0};
glm::vec3 angle{0.0f};
if (values >> position.x >> position.y >> position.z >> angle.x >> angle.y)
{
Camera.Pos = position;
Camera.Angle = angle;
Camera.m_owner = nullptr;
FreeFlyModeFlag = true;
Global.pCamera = Camera;
m_saved_camera = {position, angle};
WriteLog("Editor: camera where it was at the last save");
}
}
for (auto *track : simulation::Paths.sequence())
{
if (m_saved_camera.has_value() || track == nullptr || track->m_editorremoved || track->m_paths.empty())
continue;
auto const &path{track->m_paths.front()};
auto const start{path.points[segment_data::point::start]};
auto const end{path.points[segment_data::point::end]};
auto const centre{0.5 * (start + end)};
glm::dvec3 along{end.x - start.x, 0.0, end.z - start.z};
along = glm::length(along) > 1e-6 ? glm::normalize(along) : glm::dvec3{0.0, 0.0, 1.0};
glm::dvec3 const side{-along.z, 0.0, along.x};
Camera.Pos = centre - along * 30.0 + side * 15.0 + glm::dvec3{0.0, 20.0, 0.0};
auto const look{glm::normalize(centre - Camera.Pos)};
Camera.Angle = glm::vec3(static_cast<float>(std::asin(std::clamp(look.y, -1.0, 1.0))), static_cast<float>(std::atan2(-look.x, -look.z)), 0.0f);
Camera.m_owner = nullptr;
FreeFlyModeFlag = true;
Global.pCamera = Camera;
WriteLog("Editor: camera at the first track, " + track->name());
break;
}
if (false == m_saved_camera.has_value() && simulation::Paths.sequence().empty())
{
std::vector<char> found;
auto const ground{ground_heights({glm::dvec3{0.0, 5000.0, 0.0}}, true, &found).front()};
Camera.Pos = glm::dvec3{0.0, (found.front() != 0 ? ground : 0.0) + 60.0, 0.0};
// facing north, the top view with the north up as on a map: the yaw 0 looks along -z, to the south
Camera.Angle = glm::vec3(glm::radians(-45.0f), glm::pi<float>(), 0.0f);
Camera.m_owner = nullptr;
FreeFlyModeFlag = true;
if (false == Global.EditorOrtho)
{
Global.EditorOrthoExtent = 150.0f;
toggle_ortho();
}
Global.pCamera = Camera;
choose_work_area(work_area::tracks);
WriteLog("Editor: camera over the origin of the empty scenery");
}
m_first_steps_open = EditorSettings.first_steps();
}
Global.ControlPicking = true;
EditorModeFlag = true;
load_orthophoto_settings();
m_orthophoto.area_paused(m_area_paused);
m_nmt.prefetch_paused(m_area_paused);
Application.set_cursor(GLFW_CURSOR_NORMAL);
}
void editor_mode::exit()
{
m_ride.leave();
track_sleepers::lay_in_background(true);
// the driver's view has no menu to switch the drawn cursor off: the system one there
ui_layer::draw_cursor(false);
Global.EditorCameraOn = false;
Global.editor_tracks = false;
EditorModeFlag = false;
Global.ControlPicking = m_statebackup.picking;
FreeFlyModeFlag = m_statebackup.freefly;
Global.pCamera = m_statebackup.camera;
commit_track_drag(true);
m_track_gizmo_using = false;
m_track_drag.clear();
m_track_drag_points.clear();
m_track_snap = {};
m_track_point = {};
m_route = {};
m_route_dragging = false;
if (Global.EditorOrtho)
toggle_ortho();
g_redo.clear();
m_history.clear();
m_orphans.clear();
m_fill_last.clear();
m_orthophoto.cancel_pending();
m_orthophoto.detach_scene(); // the layer is an editor aid, keep it out of the other modes
for (auto &layer : m_wms)
{
layer->cancel_pending();
layer->detach_scene();
}
// the download of the area held while the scenery is driven, it goes on once the editor is back
m_orthophoto.area_paused(true);
m_nmt.prefetch_paused(true);
scene::Layers.show_all(); // likewise for hidden scenery layers, the other modes get the scenery as it was defined
// drop selection so a stale/dangling node pointer isn't used on the next editor session
m_node = nullptr;
select_include(0);
m_gizmo_using = false;
ui()->set_node(nullptr);
Application.set_cursor(Global.ControlPicking ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED);
if (!Global.ControlPicking)
{
Application.set_cursor_pos(0, 0);
}
}
void editor_mode::delete_model(TAnimModel *model, bool const Joined)
{
// record deletion for undo (serialize full node)
std::string as_text;
model->export_as_text(as_text);
std::string debug = "Deleting node: " + as_text + "\nSerialized data:\n";
push_snapshot(model, EditorSnapshot::Action::Delete, as_text);
if (false == m_history.empty())
m_history.back().joined = Joined;
WriteLog(debug, logtype::generic);
erase_model(model);
}
void editor_mode::erase_model(TAnimModel *model)
{
nullify_history_pointers(model);
remove_from_hierarchy(model);
if (m_node == model)
{
m_node = nullptr;
m_dragging = false;
}
simulation::State.delete_model(model);
}
void editor_mode::delete_selected()
{
// what's selected: a road piece or point, a signal, an include of a scenery template, a model, tracks
if (road_delete())
return;
if (m_instance != 0 && signal_delete(m_instance))
return;
if (m_instance != 0)
{
// include of a scenery template. what it shows goes out of sight, the directive is erased on save
scene::Layers.removed(m_instance, true);
EditorSnapshot snap;
snap.instance = m_instance;
snap.node_name = *scene::Layers.instance(m_instance).file;
m_history.push_back(std::move(snap));
g_redo.clear();
ui()->set_status(format(STR_C("Include of \"%s\" removed, Ctrl+Z brings it back. Its directive is erased when the scenery is saved"), scene::Layers.instance(m_instance).file->c_str()));
select_include(0);
return;
}
TAnimModel *model = dynamic_cast<TAnimModel *>(m_node);
if (model)
{
// the models of the selection go together, one step of the history
auto joined{false};
for (auto *node : selected_nodes())
{
if (auto *selected = dynamic_cast<TAnimModel *>(node))
{
delete_model(selected, joined);
joined = true;
}
}
m_selection.clear();
}
else if (ui()->mode() == nodebank_panel::TRACK && m_track_tab == track_tab::path && m_track_set.size() >= 2)
{
track_set_delete();
}
else if (selected_track() != nullptr)
{
delete_selected_track();
}
}
void editor_mode::on_key(int const Key, int const Scancode, int const Action, int const Mods)
{
#ifndef __unix__
Global.shiftState = Mods & GLFW_MOD_SHIFT ? true : false;
Global.ctrlState = Mods & GLFW_MOD_CONTROL ? true : false;
Global.altState = Mods & GLFW_MOD_ALT ? true : false;
#endif
bool anyModifier = Mods & (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL | GLFW_MOD_ALT);
if (Action == GLFW_RELEASE && Key >= 0 && Key <= GLFW_KEY_LAST)
{
input::keys[Key] = GLFW_RELEASE;
if (Key == GLFW_KEY_LEFT_SHIFT || Key == GLFW_KEY_RIGHT_SHIFT)
input::key_shift = false;
if (Key == GLFW_KEY_LEFT_CONTROL || Key == GLFW_KEY_RIGHT_CONTROL)
input::key_ctrl = false;
if (Key == GLFW_KEY_LEFT_ALT || Key == GLFW_KEY_RIGHT_ALT)
input::key_alt = false;
}
// first give UI a chance to handle the key
if (!anyModifier && m_userinterface->on_key(Key, Action))
return;
// riding along the track the keys drive the camera; Y starts the ride on the track under the cursor
if (false == ImGui::GetIO().WantTextInput)
{
std::string status;
auto const taken{m_ride.key(Key, Action, Mods, camera_look(), m_input.mouse.button(GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS, status)};
if (false == status.empty())
ui()->set_status(status, m_ride.active());
if (taken)
{
// the right button held to drive isn't a click for the menu of the track under the cursor
m_track_context.armed = false;
return;
}
// not with a button held: a drag going on would lose its release to the ride
if (Key == GLFW_KEY_Y && Action == GLFW_PRESS && false == anyModifier && false == m_ride.active() && m_input.mouse.button(GLFW_MOUSE_BUTTON_RIGHT) != GLFW_PRESS && m_input.mouse.button(GLFW_MOUSE_BUTTON_LEFT) != GLFW_PRESS)
{
ride_enter();
return;
}
}
if (ui()->mode() == nodebank_panel::TRACK && is_press(Action) && (Mods & (GLFW_MOD_CONTROL | GLFW_MOD_ALT)) == 0 && typed_key(Key))
return;
// gizmo transform shortcuts (Q/W/E/R) — only when the camera isn't being flown (RMB up).
// handled before the camera keyboard step because Q/W/E are also the fly-mode movement keys,
// which would otherwise consume them.
if (!anyModifier && is_press(Action)
&& m_input.mouse.button(GLFW_MOUSE_BUTTON_RIGHT) != GLFW_PRESS)
{
if (ui()->mode() == nodebank_panel::TRACK && false == m_track_gizmo_using && false == m_straights.dragging && false == m_switch.placing && track_shortcut(Key))
return;
bool handled = true;
switch (Key)
{
case GLFW_KEY_O: toggle_ortho(); break;
// in the tracks they also bring the gizmo out, hidden until then
case GLFW_KEY_Q: m_gizmo_op = gizmo_operation::translate; m_track_gizmo_on |= ui()->mode() == nodebank_panel::TRACK; break;
case GLFW_KEY_W: m_gizmo_op = gizmo_operation::rotate; m_track_gizmo_on |= ui()->mode() == nodebank_panel::TRACK; break;
case GLFW_KEY_E: m_gizmo_op = gizmo_operation::scale; m_track_gizmo_on |= ui()->mode() == nodebank_panel::TRACK; break;
case GLFW_KEY_R: m_gizmo_local = !m_gizmo_local; break;
case GLFW_KEY_B: handled = ui()->mode() != nodebank_panel::TRACK && bend_shortcut(); break;
case GLFW_KEY_F1: m_shortcuts_open = !m_shortcuts_open; break;
// the fields of work in the order of their row
case GLFW_KEY_F2: choose_work_area(work_area::surroundings); break;
case GLFW_KEY_F3: choose_work_area(work_area::tracks); break;
case GLFW_KEY_F4: choose_work_area(work_area::roads); break;
case GLFW_KEY_F5: choose_work_area(work_area::terrain); break;
// the edit modes in the order of the toolbar; in the track mode the digits typed as a value were taken already
case GLFW_KEY_1: handled = ui()->mode() != nodebank_panel::TRACK; if (handled) choose_edit_mode(nodebank_panel::MODIFY); break;
case GLFW_KEY_2: handled = ui()->mode() != nodebank_panel::TRACK; if (handled) choose_edit_mode(nodebank_panel::ADD); break;
case GLFW_KEY_3: handled = ui()->mode() != nodebank_panel::TRACK; if (handled) choose_edit_mode(nodebank_panel::BRUSH); break;
case GLFW_KEY_4: handled = ui()->mode() != nodebank_panel::TRACK; if (handled) choose_edit_mode(nodebank_panel::FILL); break;
case GLFW_KEY_5: handled = ui()->mode() != nodebank_panel::TRACK; if (handled) choose_edit_mode(nodebank_panel::COPY); break;
default: handled = false; break;
}
if (handled)
return;
}
// the minimap with the stations
if (Global.ctrlState && false == Global.shiftState && Key == GLFW_KEY_M)
{
if (is_press(Action))
m_minimap.toggle();
return;
}
// all docks unpinned and hidden at the edges, for the most of the view, or pinned back
if (Global.ctrlState && false == Global.shiftState && Key == GLFW_KEY_SPACE)
{
if (is_press(Action))
ui()->toggle_docks();
return;
}
// the selection: copy, paste at the cursor, group and ungroup. ahead of the camera keys, the letters may be bound there
if (Global.ctrlState && (Key == GLFW_KEY_C || Key == GLFW_KEY_V || Key == GLFW_KEY_G) && ui()->mode() != nodebank_panel::TRACK && false == m_roadtool.window)
{
if (is_press(Action))
{
if (Key == GLFW_KEY_C)
copy_selection();
else if (Key == GLFW_KEY_V)
paste_clipboard(cursor_ground());
else if (Global.shiftState)
ungroup_selection();
else
group_selection();
}
return;
}
// the properties of a track copied and pasted on the selected ones
if (Global.ctrlState && Global.shiftState && (Key == GLFW_KEY_C || Key == GLFW_KEY_V) && ui()->mode() == nodebank_panel::TRACK)
{
if (is_press(Action) && false == ImGui::GetIO().WantTextInput)
{
auto const tracks{selected_tracks()};
if (Key == GLFW_KEY_C)
{
// the selected track, or the one under the cursor when there are more of them
auto *from{tracks.size() == 1 ? tracks.front() : m_hover.track};
if (from != nullptr)
copy_track_style(*from);
else
ui()->set_status(STR_C("Select one track, or point at one, to take its properties"), true);
}
else if (m_track_style.values.empty())
ui()->set_status(STR_C("Take the properties of a track first: Ctrl+Shift+C"), true);
else if (tracks.empty())
ui()->set_status(STR_C("Select the tracks to set the properties on"), true);
else
paste_track_style(tracks);
}
return;
}
// save; checked ahead of the camera keys, which have S bound to moving back
if (Global.ctrlState && false == Global.shiftState && Key == GLFW_KEY_S)
{
if (is_press(Action))
save();
return;
}
if (Global.ctrlState && false == Global.shiftState && Key == GLFW_KEY_F && ui()->mode() == nodebank_panel::TRACK)
{
if (is_press(Action))
{
show_track_tab(m_track_tab);
m_track_search.focus = true;
}
return;
}
// then internal input handling
if (m_input.keyboard.key(Key, Action))
return;
if (Action == GLFW_RELEASE)
return;
// shortcuts: undo/redo. not in the middle of a drag, it would go on from the state which was undone
if (Global.ctrlState && (Key == GLFW_KEY_Z || Key == GLFW_KEY_Y) && is_press(Action) && (m_track_gizmo_using || m_track_set_using || m_straights.dragging || m_extend.active || m_switch.placing || m_point_drag.active || m_handle_drag.active || m_roadtool.lifting))
return;
if (Global.ctrlState && Key == GLFW_KEY_Z && is_press(Action))
{
undo_last();
return;
}
if (Global.ctrlState && Key == GLFW_KEY_Y && is_press(Action))
{
redo_last();
return;
}
// legacy hardcoded keyboard commands
switch (Key)
{
case GLFW_KEY_F11:
if (Action != GLFW_PRESS)
break;
if (!Global.ctrlState && !Global.shiftState)
{
Application.pop_mode();
}
else if (Global.ctrlState && Global.shiftState)
{
export_scenery();
}
break;
case GLFW_KEY_F12:
if (Global.ctrlState && Global.shiftState && is_press(Action))
{
DebugModeFlag = !DebugModeFlag;
}
break;
case GLFW_KEY_DELETE:
// the chunks of the terrain selected, where the terrain is the field of work; else what's selected in the scenery
if (is_press(Action) && m_terrain_open && false == m_terrain_selection.empty())
remove_selected_chunks();
else if (is_press(Action))
delete_selected();
break;
case GLFW_KEY_ESCAPE:
// an open list or menu takes this Esc for itself, the selection behind it stays
if (ImGui::IsPopupOpen("", ImGuiPopupFlags_AnyPopupId | ImGuiPopupFlags_AnyPopupLevel))
break;
// the switch being clicked off the view given up
if (is_press(Action) && m_switch_designer.open && m_switch_designer.picking)
{
m_switch_designer.picking = false;
break;
}
// the download area being drawn given up, the one before stays
if (is_press(Action) && m_area_drawing.active)
{
area_finish(false);
break;
}
if (is_press(Action))
{
road_cancel();
}
// the first Esc drops what is going on in the track mode, the next one goes back to the selection
if (is_press(Action) && ui()->mode() == nodebank_panel::TRACK)
{
if (m_tape.active)
m_tape = {};
else if (m_route.trace.active && route_active())
route_trace_end(false);
else if (m_lay.active && false == m_lay.points.empty())
lay_escape();
else if (m_track_style.picking)
m_track_style.picking = false;
else if (m_profile.picking)
m_profile.picking = false;
else if (track_busy())
{
cancel_track_tools();
m_straights.tool = 0;
}
else if (m_track_tab == track_tab::signals && m_signal.picking_read)
m_signal.picking_read = false;
else if (m_track_tab == track_tab::signals && m_signal.armed)
m_signal.armed = false;
else if (m_track_tab == track_tab::path && m_track_set.size() >= 2)
select_tracks({});
else if (m_track_tab != track_tab::path)
show_track_tab(track_tab::path);
else if (selected_track() != nullptr)
{
m_node = nullptr;
m_track_point = {};
ui()->set_node(nullptr);
}
}
break;
case GLFW_KEY_TAB:
// a switch dragged from a free end and bent: its diverging track the other way than the main one, or the same way
if (is_press(Action) && false == anyModifier && m_switch.placing && m_switch.free && false == ImGui::GetIO().WantTextInput)
{
m_switch.one_sided = false == m_switch.one_sided;
break;
}
// laying: the bends of straights or a curve traced, for the points clicked from now on
if (is_press(Action) && false == anyModifier && ui()->mode() == nodebank_panel::TRACK && m_lay.active && false == ImGui::GetIO().WantTextInput)
lay_switch_mode();
break;
case GLFW_KEY_GRAVE_ACCENT:
// ~ (left of 1): the orthophoto shown or hidden; with Ctrl what the camera sees downloaded before anything else
if (false == is_press(Action) || ImGui::GetIO().WantTextInput)
break;
if (Mods & GLFW_MOD_CONTROL)
{
orthophoto_hurry();
break;
}
if (false == anyModifier)
{
m_orthophoto.enabled(false == m_orthophoto.enabled());
ui()->set_status(m_orthophoto.enabled() ? STR("Orthophoto on (~)") : STR("Orthophoto off (~)"));
}
break;
case GLFW_KEY_BACKSPACE:
// the switch being clicked off the view: the last point taken back
if (m_switch_designer.open && m_switch_designer.picking)
{
if (is_press(Action) && false == m_switch_designer.clicks.empty())
{
m_switch_designer.clicks.pop_back();
switch_designer_measure();
}
break;
}
// the download area: the last point taken back
if (is_press(Action) && m_area_drawing.active)
{
if (false == m_area_drawing.points.empty())
m_area_drawing.points.pop_back();
break;
}
// area fill: remove the last outline point
if (is_press(Action) && ui()->mode() == nodebank_panel::FILL && !m_fill_points.empty())
m_fill_points.pop_back();
if (is_press(Action) && ui()->mode() == nodebank_panel::TRACK && m_lay.active && !m_lay.points.empty())
lay_back();
if (is_press(Action) && m_route.trace.active && route_active() && !m_route.trace.points.empty())
{
m_route.trace.points.pop_back();
route_trace_fit();
}
break;
case GLFW_KEY_ENTER:
case GLFW_KEY_KP_ENTER:
// the download area closed, from three points on
if (is_press(Action) && m_area_drawing.active)
{
if (m_area_drawing.points.size() >= 3)
area_finish(true);
break;
}
if (is_press(Action) && ui()->mode() == nodebank_panel::TRACK && m_lay.active)
lay_enter();
if (is_press(Action) && m_route.trace.active && route_active())
route_trace_end(true);
break;
case GLFW_KEY_K:
if (is_press(Action) && road_split())
{
break;
}
if (is_press(Action) && ui()->mode() == nodebank_panel::TRACK && selected_track() != nullptr)
{
auto const under{split_spot()};
if (under.track != nullptr)
split_track_at(*under.track, under.point);
}
break;
case GLFW_KEY_F:
if (is_press(Action) && m_node)
{
// start smooth focus camera on selected node
start_focus(m_node, 0.6);
}
break;
case GLFW_KEY_END:
if (is_press(Action) && m_node)
{
// Unreal-style "snap to floor": drop the selected node onto the surface below it.
// works against triangle geometry (shape_node terrain / opaque shapes); once a proper
// editable terrain mesh exists, dropping onto it works without further changes here.
drop_selection_to_ground();
}
break;
default:
break;
}
}
void editor_mode::on_cursor_pos(double const Horizontal, double const Vertical)
{
if (m_ortho_pan)
ortho_pan_to(Horizontal, Vertical);
// object transforms are handled by the gizmo now; here we only forward the cursor to the
// mouse input, which rotates the camera while the right mouse button is held (panning mode)
m_input.mouse.position(Horizontal, Vertical);
}
void editor_mode::on_mouse_button(int const Button, int const Action, int const Mods)
{
// UI first
if (m_userinterface->on_mouse_button(Button, Action))
{
m_input.mouse.button(Button, Action);
return;
}
// a switch being clicked off the view for the template editor takes the left button
if (m_switch_designer.open && m_switch_designer.picking && Button == GLFW_MOUSE_BUTTON_LEFT)
{
// on the terrain under the cursor, then on the level of the first point: no track or ballast in the view catches the point
if (is_press(Action))
switch_designer_click(m_switch_designer.clicks.empty() ? cursor_terrain(std::as_const(*this).cursor_ground().y) : cursor_level(m_switch_designer.clicks.front().y));
m_input.mouse.button(Button, Action);
return;
}
// the download area drawn on the map takes the left button while it's drawn
if (m_area_drawing.active && Button == GLFW_MOUSE_BUTTON_LEFT)
{
if (is_press(Action))
area_click(Global.shiftState);
else
m_area_drawing.dragged = -1;
m_input.mouse.button(Button, Action);
return;
}
// the chunk, spacing and water tools of the terrain work on a click (Shift: the other way round)
if (terrain_clicking() && Button == GLFW_MOUSE_BUTTON_LEFT)
{
if (is_press(Action))
handle_terrain_click(Global.shiftState);
m_input.mouse.button(Button, Action);
return;
}
// with a brush of the terrain the left button works the brush instead of picking nodes
if (terrain_brush() && Button == GLFW_MOUSE_BUTTON_LEFT)
{
if (m_terrain_tool == terrain_tool::level && is_press(Action) && (Global.ctrlState || m_terrain_picking))
{
// a click which picks the target height doesn't level anything
pick_terrain_target();
m_terrain_picking = false;
mouseHold = false;
m_input.mouse.button(Button, Action);
return;
}
mouseHold = is_press(Action);
m_input.mouse.button(Button, Action);
return;
}
// with the road window open the left button works its tools, whichever edit mode is on but the track one
if (m_roadtool.window && Button == GLFW_MOUSE_BUTTON_LEFT && ui()->mode() != nodebank_panel::TRACK)
{
// Alt+LMB on a point: its height (and the other selected ones') dragged up or down
if (is_press(Action) && Global.altState && false == ImGui::GetIO().WantCaptureMouse && road_lift_start())
{
m_input.mouse.button(Button, Action);
return;
}
if (false == is_press(Action) && m_roadtool.lifting)
{
road_lift_finish();
m_input.mouse.button(Button, Action);
return;
}
if (is_press(Action))
{
// NOTE: the pick is resolved a few frames later, by then it's known whether the press was meant for the UI
GfxRenderer->Pick_Node_Callback([this](scene::basic_node * /*node*/) {
if (viewport_click())
road_click();
});
}
m_input.mouse.button(Button, Action);
return;
}
if (Button == GLFW_MOUSE_BUTTON_LEFT && is_press(Action) && false == m_bend.picking && ui()->mode() != nodebank_panel::TRACK && (bend_drag_start(Mods) || bend_click()))
{
m_input.mouse.button(Button, Action);
return;
}
if (m_bend.picking && Button == GLFW_MOUSE_BUTTON_LEFT && ui()->mode() != nodebank_panel::TRACK)
{
if (is_press(Action))
bend_pick(Global.pCamera.Pos + GfxRenderer->Mouse_Position());
m_input.mouse.button(Button, Action);
return;
}
if (Button == GLFW_MOUSE_BUTTON_LEFT)
{
auto const mode = ui()->mode();
auto const rotation_mode = ui()->rot_mode();
auto const fixed_rotation_value = ui()->rot_val();
if (is_press(Action))
{
mouseHold = true;
m_brush_has_last = false; // every brush stroke starts with a placement
// in area fill mode the left button only adds outline points, it doesn't touch the selection
if (mode == nodebank_panel::FILL)
{
GfxRenderer->Pick_Node_Callback([this](scene::basic_node * /*node*/) {
if (viewport_click())
add_area_fill_point();
});
m_input.mouse.button(Button, Action);
return;
}
if (mode == nodebank_panel::TRACK)
{
track_press(track_intent_at(Mods));
m_input.mouse.button(Button, Action);
return;
}
// delegate node picking behaviour depending on current panel mode.
// NOTE: the pick is resolved a few frames later, often after the button was already released,
// so the callback mustn't depend on the button still being held
// Shift+LMB adds a model to the selection, or takes it out
auto const additive{mode == nodebank_panel::MODIFY && (Mods & GLFW_MOD_SHIFT) != 0};
GfxRenderer->Pick_Node_Callback(
[this, mode, rotation_mode, fixed_rotation_value, additive](scene::basic_node *node) {
// the press turned out to be meant for the UI
if (!viewport_click())
return;
auto *const previous{m_node};
m_node = nullptr;
select_include(0);
ui()->set_node(additive ? previous : nullptr);
// in a scenery opened for editing selection has its limits. nodes of locked layers are off limits.
// nodes defined by a scenery template can't be rewritten one by one on save, so a click on one
// selects the include of the template as a whole
if (node && mode == nodebank_panel::MODIFY)
{
std::string reason;
if (node->from_template() && scene::Layers.removable(node->m_instance, &reason))
{
select_include(node->m_instance);
ui()->set_status(format(STR_C("Include of \"%s\" selected. Its parameters are in the inspector, Del removes it from the scenery"), scene::Layers.instance(m_instance).file->c_str()));
node = nullptr;
}
else if (node->from_template() || false == scene::Layers.editable(node, &reason))
{
ui()->set_status(format(STR_C("\"%s\" can't be edited: %s"), name_of(*node).c_str(), reason.c_str()), true);
node = nullptr;
}
}
// models stay within placement distance; tracks in the track workspace have no range cap
if (node && false == (Global.editor_tracks && dynamic_cast<TTrack *>(node) != nullptr)) {
double const dist = glm::distance(node->location(), glm::dvec3{Global.pCamera.Pos});
if (dist > static_cast<double>(kMaxPlacementDistance))
{
if (additive)
m_node = previous;
return;
}
}
if (mode == nodebank_panel::MODIFY)
{
if (additive)
m_node = previous;
select_node(node, additive);
}
else if (mode == nodebank_panel::COPY)
{
if (node && typeid(*node) == typeid(TAnimModel))
{
std::string as_text;
node->export_as_text(as_text);
ui()->add_node_template(as_text);
}
m_dragging = false;
}
else if (mode == nodebank_panel::ADD)
{
const std::string *src = ui()->get_active_node_template();
if (!src)
return;
if (src->starts_with(editor_includes::directive_mark))
{
// a scenery template rather than a single node
place_include(src->substr(editor_includes::directive_mark.size()), rotation_mode, fixed_rotation_value);
return;
}
std::string name = "editor_";
glm::dvec3 mouseOffset = clamp_mouse_offset_to_max(GfxRenderer->Mouse_Position());
TAnimModel *cloned = simulation::State.create_model(*src, name, placement_on_ground(Camera.Pos + mouseOffset));
if (!cloned)
return;
std::string new_name = "editor_" + cloned->uuid.to_string();
cloned->m_name = new_name;
apply_rotation_for_new_node(cloned, rotation_mode, fixed_rotation_value);
// record addition for undo (after rotation, so a redo recreates the same thing)
std::string as_text;
cloned->export_as_text(as_text);
push_snapshot(cloned, EditorSnapshot::Action::Add, as_text);
m_node = cloned;
ui()->set_node(m_node);
}
});
m_dragging = true;
m_takesnapshot = true;
}
else
{
if (is_release(Action))
{
mouseHold = false;
finish_track_box();
point_drag_finish();
m_handle_drag = {};
sweep_release();
if (m_straights.tool_mouse)
finish_straight_gesture();
if (m_extend.active)
finish_extend();
if (m_switch.placing)
{
// a press let go with no drag places no switch: the track it took is only selected, the ground with no track there
// selects nothing
auto const &io{ImGui::GetIO()};
if (glm::length(glm::vec2{io.MousePos.x, io.MousePos.y} - m_switch.pressed_at) <= 10.0f)
{
m_switch.placing = false;
if (m_switch.free)
{
m_switch.free = false;
track_click_select(nullptr);
}
}
else
finish_switch_placement();
}
if (m_turntable.placing)
turntable_finish_placement();
if (m_signal.placing || m_signal.moving)
signal_release();
if (m_switch.slide.active)
switch_slide_finish();
}
m_dragging = false;
}
}
else if (Button == GLFW_MOUSE_BUTTON_RIGHT)
{
// Ctrl+RMB: the menu at the cursor, for the model under it or the selection; RMB alone turns the camera
if ((Mods & GLFW_MOD_CONTROL) != 0 && ui()->mode() != nodebank_panel::TRACK)
{
if (is_press(Action))
{
m_contextpoint = cursor_ground();
GfxRenderer->Pick_Node_Callback([this](scene::basic_node *node) {
if (!viewport_click())
return;
auto const selection{selected_nodes()};
if (node != nullptr && false == node->from_template() && scene::Layers.editable(node) && std::find(selection.begin(), selection.end(), node) == selection.end())
select_node(node, false);
m_contextopen = true;
});
}
return;
}
if (ui()->mode() == nodebank_panel::TRACK)
{
if (is_press(Action))
track_context_press();
else if (is_release(Action))
track_context_release();
}
// game-engine style look: hide & grab the cursor while flying, restore it on release
Application.set_cursor(is_press(Action) ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
}
else if (Button == GLFW_MOUSE_BUTTON_MIDDLE)
{
if (is_press(Action))
{
m_ortho_pan = true;
m_ortho_pan_cursor = m_input.mouse.position();
m_focus_active = false;
Camera.Velocity = {};
// in the perspective view the ground under the cursor goes along with it: metres of it to a pixel there, as far as it is
m_pan_metres = 0.0;
if (false == Global.EditorOrtho)
{
auto const &io{ImGui::GetIO()};
auto const distance{glm::length(GfxRenderer->Mouse_Position())};
if (GfxRenderer->Mouse_Hit() && io.DisplaySize.y > 0.0f && distance > 0.5 && distance < 20000.0)
m_pan_metres = 2.0 * distance * std::tan(glm::radians(Global.FieldOfView / Global.ZoomFactor) * 0.5) / static_cast<double>(io.DisplaySize.y);
else
m_ortho_pan = false; // nothing under the cursor to hold
}
}
else if (is_release(Action))
ortho_pan_stop();
}
m_input.mouse.button(Button, Action);
}
void editor_mode::render_change_history(){
ImGui::Begin((std::string(STR_C("Change History")) + "###editorhistory").c_str(), &m_change_history);
int maxsize = m_max_history_size;
if (ImGui::InputInt(STR_C("Max history size"), &maxsize))
{
m_max_history_size = std::max(1, maxsize);
if ((int)m_history.size() > m_max_history_size && m_max_history_size >= 0)
{
auto remove_count = (int)m_history.size() - m_max_history_size;
m_history.erase(m_history.begin(), m_history.begin() + remove_count);
// adjust selected index
if (m_selected_history_idx >= (int)m_history.size())
m_selected_history_idx = (int)m_history.size() - 1;
}
}
float dist = kMaxPlacementDistance;
if (ImGui::InputFloat(STR_C("Max placement distance"), &dist))
{
kMaxPlacementDistance = std::max(0.0f, dist);
}
ImGui::Separator();
ImGui::Text(STR_C("History (newest at end): %zu entries"), m_history.size());
ImGui::BeginChild("history_list", ImVec2(400, 200), true);
for (int i = 0; i < (int)m_history.size(); ++i)
{
auto &s = m_history[i];
char buf[256];
std::snprintf(buf, sizeof(buf), "%3d: %s %s pos=(%.1f,%.1f,%.1f)", i,
s.action == EditorSnapshot::Action::Add ? "ADD" :
s.action == EditorSnapshot::Action::Delete ? "DEL" :
s.action == EditorSnapshot::Action::Move ? "MOV" :
s.action == EditorSnapshot::Action::Rotate ? "ROT" :
s.action == EditorSnapshot::Action::Scale ? "SCA" :
s.action == EditorSnapshot::Action::TrackEdit ? "TRK" :
s.action == EditorSnapshot::Action::RoadEdit ? "ROAD" :
s.action == EditorSnapshot::Action::Array ? "ARR" :
s.action == EditorSnapshot::Action::Terrain ? "TER" :
s.action == EditorSnapshot::Action::Kilometrage ? "KM" : "OTH",
s.node_name.empty() ? STR_C("(no name)") : s.node_name.c_str(),
s.position.x, s.position.y, s.position.z);
if (ImGui::Selectable(buf, m_selected_history_idx == i))
m_selected_history_idx = i;
}
ImGui::EndChild();
ImGui::Separator();
if (ImGui::Button(STR_C("Clear History")))
{
m_history.clear();
g_redo.clear();
m_selected_history_idx = -1;
}
ImGui::SameLine();
ImGui::SameLine();
if (ImGui::Button(STR_C("Undo back to the selected")))
{
if (m_selected_history_idx >= 0 && m_selected_history_idx < (int)m_history.size())
{
int target = m_selected_history_idx;
int undoCount = (int)m_history.size() - 1 - target;
for (int k = 0; k < undoCount; ++k)
undo_last();
m_selected_history_idx = -1;
}
}
ImGui::End();
}
void editor_mode::on_event_poll()
{
m_selftest.input.deliver();
// game-engine style camera: WSAD/EQ only fly the camera while the right mouse button is held.
// when it's released the keyboard is free for gizmo shortcuts, and we flush a zero-movement
// command once so the camera doesn't keep coasting on the last velocity it was given.
bool const flying = m_input.mouse.button(GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS;
if (flying)
{
// riding along the track the camera only looks around, the keys drive the ride
if (false == m_ride.active())
m_input.poll();
first_step_done(first_step::look);
}
else if (m_camera_flying)
{
m_camera_relay.post(user_command::movehorizontal, 0.0, 0.0, GLFW_PRESS, 0);
m_camera_relay.post(user_command::movevertical, 0.0, 0.0, GLFW_PRESS, 0);
}
m_camera_flying = flying;
}
bool editor_mode::is_command_processor() const
{
return false;
}
bool editor_mode::focus_active()
{
return m_focus_active;
}
void editor_mode::set_focus_active(bool isActive)
{
m_focus_active = isActive;
}
void editor_mode::starter_trainset(std::vector<std::string> &Trailing)
{
static std::regex const trainset{R"((^|\n)[ \t]*trainset[ \t])", std::regex::icase};
for (std::size_t handle = 1; handle <= scene::Layers.size(); ++handle)
{
auto const &layer{scene::Layers.layer(static_cast<scene::layer_handle>(handle))};
if (layer.created)
continue;
std::ifstream input{Global.asCurrentSceneryPath + layer.name, std::ios_base::binary};
std::string const content{std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>()};
if (std::regex_search(content, trainset))
return;
}
std::error_code error;
if (false == std::filesystem::exists("dynamic/pkp/wmb10_v1/wmb10.mmd", error))
return;
for (auto const *track : simulation::Paths.sequence())
{
if (track == nullptr || track->m_editorremoved || track->eType != tt_Normal || (track->iCategoryFlag & 1) == 0 || track->name().empty() || track->m_paths.empty())
continue;
auto const length{geometry::bezier{track->m_paths.front()}.plan_length()};
if (length < 15.0)
continue;
Trailing.push_back("trainset none " + track->name() + " " + to_string(std::min(length, 0.5 * length + 5.0), 2) + " 0");
Trailing.push_back("node -1 0 wmb10-6457 dynamic pkp/wmb10_v1 wmb10-6457 wmb10 0 headdriver 0 0 enddynamic");
Trailing.push_back("endtrainset");
WriteLog("Editor: the scenery has no trainset, a WMB10 is put on " + track->name() + " to drive");
return;
}
}
void editor_mode::render_top_tracks_choice(bool const Combo)
{
static char const *const labels[]{"Whole", "Without ballast", "Rails only"};
static char const *const tooltips[]{"The tracks as in the 3D view", "The rails and the sleepers, the ground under them shows", "The rails alone, without the sleepers and the timbers of the switches, as on a plan"};
auto chosen{std::clamp(Global.EditorOrthoTracks, 0, 2)};
auto const pick = [&](int const Choice) {
Global.EditorOrthoTracks = Choice;
EditorSettings.top_tracks(Choice);
EditorSettings.save();
};
if (false == Combo)
{
for (int choice = 0; choice < 3; ++choice)
{
if (ImGui::MenuItem(STR_C(labels[choice]), nullptr, chosen == choice))
pick(choice);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C(tooltips[choice]));
}
return;
}
ImGui::SetNextItemWidth(130.0f);
if (ImGui::BeginCombo(STR_C("Tracks"), STR_C(labels[chosen])))
{
for (int choice = 0; choice < 3; ++choice)
{
if (ImGui::Selectable(STR_C(labels[choice]), chosen == choice))
pick(choice);
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C(tooltips[choice]));
}
ImGui::EndCombo();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("How the top view draws the tracks"));
if (chosen < 2)
return;
ImGui::SameLine();
auto colour{ImGui::ColorConvertU32ToFloat4(EditorSettings.top_rails())};
if (ImGui::ColorEdit3("##toprails", &colour.x, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel))
{
EditorSettings.top_rails(ImGui::ColorConvertFloat4ToU32(ImVec4(colour.x, colour.y, colour.z, 1.0f)));
EditorSettings.save();
}
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("The colour of the rails, to stand out on the imagery and the map"));
}
void editor_mode::apply_camera_settings()
{
auto const &camera{EditorSettings.camera()};
Global.EditorCameraSpeed = camera.speed;
Global.EditorCameraFast = camera.fast;
Global.EditorCameraZoomed = camera.zoomed;
}
void editor_mode::render_camera_settings()
{
auto &camera{EditorSettings.camera()};
auto changed{false};
auto release{false};
ImGui::PushItemWidth(180.0f * Global.ui_scale);
auto speed{static_cast<float>(camera.speed)};
if (ImGui::SliderFloat(STR_C("Speed of the camera"), &speed, 0.05f, 10.0f, "x %.2f", ImGuiSliderFlags_Logarithmic))
{
camera.speed = speed;
changed = true;
}
release |= ImGui::IsItemDeactivatedAfterEdit();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("How fast the arrows or WSAD move the camera, Shift a bit faster; 1 as it was"));
auto fast{static_cast<float>(camera.fast)};
if (ImGui::SliderFloat(STR_C("Ctrl speeds it up"), &fast, 1.0f, 30.0f, "x %.1f", ImGuiSliderFlags_Logarithmic))
{
camera.fast = fast;
changed = true;
}
release |= ImGui::IsItemDeactivatedAfterEdit();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("How many times faster the camera goes with Ctrl held, Ctrl+Shift too; 30 as it was"));
ImGui::PopItemWidth();
if (ImGui::Checkbox(STR_C("In the top view as fast as the view is zoomed"), &camera.zoomed))
changed = release = true;
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("Zoomed in the map moves slowly, zoomed out fast: across the screen at the same pace whatever the zoom"));
ImGui::Separator();
if (ImGui::MenuItem(STR_C("As it was")))
{
camera = {};
changed = release = true;
}
if (changed)
apply_camera_settings();
if (release)
EditorSettings.save();
}
void editor_mode::orthophoto_hurry()
{
if (false == m_orthophoto.enabled())
m_orthophoto.enabled(true);
// what the camera sees of the ground: rays through a grid over the screen to the level of the ground under the camera, or as far
// as the reach of the orthophoto along it where they don't come down to it (the sky)
ImGuiIO const &io = ImGui::GetIO();
if (io.DisplaySize.x <= 0.0f || io.DisplaySize.y <= 0.0f)
return;
auto const inverse = glm::inverse(editor_mode::projection_matrix(io.DisplaySize.x / io.DisplaySize.y) * GfxRenderer->Camera_View_Matrix());
auto const camera{GfxRenderer->Camera_Position()};
// the level of the ground under the camera: the terrain, else the scenery; with neither the rays go as far as the reach
std::optional<double> level;
// only a level below the camera: one above it (in a tunnel, or flown under the terrain) would put the hits behind it
if (double ground{0.0}; m_streamer.active() && m_streamer.height_at(camera.x, camera.z, ground) && ground < camera.y)
level = ground;
else if (std::vector<char> found; Global.EditorOrtho == false)
{
auto const heights{ground_heights({camera}, true, &found)};
if (false == found.empty() && found.front() != 0 && heights.front() < camera.y)
level = heights.front();
}
double constexpr reach{2500.0};
glm::dvec2 low{camera.x, camera.z};
glm::dvec2 high{camera.x, camera.z};
for (int j = 0; j <= 6; ++j)
for (int i = 0; i <= 6; ++i)
{
glm::vec2 const ndc{static_cast<float>(i) / 3.0f - 1.0f, static_cast<float>(j) / 3.0f - 1.0f};
auto const point = [&inverse, &ndc](float const Depth) {
auto const clip = inverse * glm::vec4(ndc, Depth, 1.0f);
return glm::dvec3{glm::vec3{clip} / clip.w};
};
if (Global.EditorOrtho)
{
// the view straight down: the corners of the screen are where it reaches on the ground
auto const corner{camera + point(0.0f)}; // the view matrix is camera-relative
low = glm::min(low, glm::dvec2{corner.x, corner.z});
high = glm::max(high, glm::dvec2{corner.x, corner.z});
continue;
}
auto direction = point(0.5f) - point(0.0f);
if (glm::length(direction) < 1e-9)
continue;
direction = glm::normalize(direction);
glm::dvec2 flat{direction.x, direction.z};
auto distance{reach};
if (level && direction.y < -1e-6)
distance = std::min(reach, (camera.y - *level) / -direction.y * glm::length(flat));
if (glm::length(flat) < 1e-6)
continue;
flat = glm::normalize(flat) * distance;
glm::dvec2 const hit{camera.x + flat.x, camera.z + flat.y};
low = glm::min(low, hit);
high = glm::max(high, hit);
}
auto const count{m_orthophoto.hurry(low, high, camera)};
ui()->set_status(count > 0 ? format(STR_C("Orthophoto: %zu tiles of the view downloaded first (Ctrl+~)"), count)
: std::string{STR("Orthophoto: the view is all shown already (Ctrl+~)")});
}