18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 22:44:59 +01:00

feat(editor): the points of the roads picked on the screen, also under the terrain; Alt+drag their height

The points where the pieces of a road meet were picked only by the ground clicked: one more than 3 m above or below the
ground under the cursor (a road taken down into a cutting, under an embankment) couldn't be picked again. Now the point
nearest to the cursor on the screen (within 12 px, the one nearer to the camera of those about as near) is picked first,
the ground as before after it.

Alt+LMB on a point and dragging up or down changes the height of the selected points (Ctrl: ten times finer); the change
shows as yellow lines and a label by the cursor while dragging, and is applied, as one step of the history, when the button
is let go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ
This commit is contained in:
Claude
2026-11-09 23:16:38 +01:00
parent 823807201c
commit 6684baefc3
5 changed files with 148 additions and 3 deletions

View File

@@ -23,6 +23,7 @@ http://mozilla.org/MPL/2.0/.
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
namespace
{
@@ -1106,6 +1107,87 @@ void editor_mode::road_profile(std::vector<segment_data> &Pieces, double const *
}
}
bool editor_mode::road_joint_on_screen(glm::dvec3 &Joint) const
{
screen_projection const projection;
auto const mouse{ImGui::GetIO().MousePos};
float best{12.0f * 12.0f};
bool found{false};
glm::dvec3 const camera{Global.pCamera.Pos};
double nearest{std::numeric_limits<double>::max()};
for (auto const &joint : editor_road::joints())
{
ImVec2 screen;
if (false == projection.project(joint, screen))
continue;
auto const dx{screen.x - mouse.x};
auto const dy{screen.y - mouse.y};
auto const distance{dx * dx + dy * dy};
auto const range{glm::distance(joint, camera)};
// of the points about as near the cursor, the one nearer to the camera
if (distance < best - 9.0f || (distance <= best + 9.0f && range < nearest))
{
best = std::min(best, distance);
nearest = range;
Joint = joint;
found = true;
}
}
return found;
}
bool editor_mode::road_lift_start()
{
auto &tool{m_roadtool};
glm::dvec3 joint;
if (tool.tool != 0 || false == road_joint_on_screen(joint))
return false;
auto const listed{std::find_if(tool.points.begin(), tool.points.end(), [&joint](glm::dvec3 const &Point) { return glm::distance(Point, joint) < 0.25; })};
if (listed == tool.points.end())
tool.points.assign(1, joint);
tool.selected = nullptr;
tool.junction = nullptr;
tool.marker = nullptr;
auto const &io{ImGui::GetIO()};
auto const height{std::max(1.0f, io.DisplaySize.y)};
if (Global.EditorOrtho)
tool.lift_scale = 2.0 * static_cast<double>(Global.EditorOrthoExtent) / height;
else
tool.lift_scale = glm::distance(joint, glm::dvec3{Global.pCamera.Pos}) * 2.0 * std::tan(glm::radians(Global.FieldOfView / Global.ZoomFactor) * 0.5) / height;
tool.lift_mouse = io.MousePos.y;
tool.lift_from.clear();
for (auto const &point : tool.points)
tool.lift_from.push_back(point.y);
tool.lifting = true;
tool.status = "Drag up or down, let go to set the heights. Ctrl while dragging: by centimetres";
return true;
}
double editor_mode::road_lift_offset() const
{
auto const &tool{m_roadtool};
auto offset{(tool.lift_mouse - ImGui::GetIO().MousePos.y) * tool.lift_scale};
// with Ctrl it's slower, for the fine part
if (Global.ctrlState)
offset *= 0.1;
return std::round(offset * 100.0) / 100.0;
}
void editor_mode::road_lift_finish()
{
auto &tool{m_roadtool};
if (false == tool.lifting)
return;
tool.lifting = false;
auto const offset{road_lift_offset()};
if (std::abs(offset) < 0.005 || tool.lift_from.size() != tool.points.size())
return;
std::vector<double> heights;
for (auto const height : tool.lift_from)
heights.push_back(height + offset);
road_point_heights(heights);
}
void editor_mode::road_click()
{
auto &tool{m_roadtool};
@@ -1135,7 +1217,8 @@ void editor_mode::road_click()
}
tool.marker = nullptr;
glm::dvec3 joint;
if (editor_road::nearest_joint(ground, kPointRadius, joint))
// by the cursor on the screen first: a point under the terrain can't be got near by the ground clicked
if (road_joint_on_screen(joint) || editor_road::nearest_joint(ground, kPointRadius, joint))
{
// a point where pieces meet or end. with Shift it's added to the selected ones, or taken out of them
auto const listed{std::find_if(tool.points.begin(), tool.points.end(), [&joint](glm::dvec3 const &Point) { return glm::distance(Point, joint) < 0.25; })};
@@ -2808,8 +2891,26 @@ void editor_mode::draw_road_overlay() const
drawpoint(point, kSelectedColor, 6.0f, true);
drawpoint(point, kSelectedColor, 11.0f, false);
}
hint = tool.marker != nullptr ? "LMB: select a piece, a junction, a point or a marker Del: delete Ctrl+Z: undo" :
false == tool.points.empty() ? "LMB: select a point Shift+LMB: add a point or take it out Del: delete the points Ctrl+Z: undo" :
if (tool.lifting)
{
// where the points go: a line up or down to each, and the change by the cursor
auto const offset{road_lift_offset()};
ImU32 const lifted{IM_COL32(255, 210, 60, 255)};
for (auto const &point : tool.points)
{
glm::dvec3 const target{point.x, point.y + offset, point.z};
projection.line(drawlist, point, target, lifted, 2.0f);
drawpoint(target, lifted, 6.0f, true);
}
auto const mouse{ImGui::GetIO().MousePos};
char label[64];
std::snprintf(label, sizeof(label), "%+.2f m, to %.2f m", offset, tool.points.empty() ? 0.0 : tool.points.front().y + offset);
drawlist->AddRectFilled(ImVec2(mouse.x + 14.0f, mouse.y - 4.0f), ImVec2(mouse.x + 22.0f + ImGui::CalcTextSize(label).x, mouse.y + 18.0f), IM_COL32(0, 0, 0, 170), 3.0f);
drawlist->AddText(ImVec2(mouse.x + 18.0f, mouse.y - 2.0f), lifted, label);
}
hint = tool.lifting ? "Drag up or down: the height of the selected points Ctrl: finer let go: set" :
tool.marker != nullptr ? "LMB: select a piece, a junction, a point or a marker Del: delete Ctrl+Z: undo" :
false == tool.points.empty() ? "LMB: select a point (under the terrain too) Shift+LMB: add a point or take it out Alt+LMB drag: the height Del: delete the points Ctrl+Z: undo" :
tool.selected != nullptr ? "LMB: select a piece, a junction or a point (white dot) K: split at the cursor Del: delete Ctrl+Z: undo" :
tool.junction != nullptr ? "LMB: select a piece, a junction or a point (white dot) Del: delete Ctrl+Z: undo" :
"LMB: select a piece, a junction, a point (white dot) or a marker";