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

fix(editor): a road point picked on the screen only when nothing is under the cursor, and not through it; Alt+drag dropped with Esc, the work area or the window, no undo in the middle of it

The click takes the point by the ground clicked, then the piece or the junction under the cursor; the point nearest on the
screen only when there's none, no farther than what is under the cursor and a bit beyond it, by whole pixels first and
the range then, so the order of the roads doesn't decide. The ends of the pieces are gone through without looking for the
repeated ones (editor_road::each_joint), joints() isn't needed. Esc, the road window closed, another tool or the track work
area drop the drag; Ctrl+Z and Ctrl+Y wait for its end.

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-11 11:38:15 +01:00
parent 6684baefc3
commit b8b6b3aa76
4 changed files with 44 additions and 38 deletions

View File

@@ -5351,7 +5351,7 @@ void editor_mode::on_key(int const Key, int const Scancode, int const Action, in
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))
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))
{

View File

@@ -219,6 +219,13 @@ glm::dvec3 path_direction(segment_data const &Path, double const T)
void editor_mode::update_road_tool()
{
{
// an Alt+drag of the heights is dropped once the window, the tool or the work area isn't its own any more, or its
// release went elsewhere
auto &lift{m_roadtool};
if (lift.lifting && (false == lift.window || lift.tool != 0 || ui()->mode() == nodebank_panel::TRACK || false == ImGui::IsMouseDown(ImGuiMouseButton_Left)))
lift.lifting = false;
}
auto &tool{m_roadtool};
if (tool.selected != nullptr && tool.selected->m_editorremoved)
tool.selected = nullptr;
@@ -903,6 +910,8 @@ void editor_mode::render_junction_layout(junction_node &Junction)
void editor_mode::road_cancel()
{
auto &tool{m_roadtool};
// Esc in the middle of Alt+drag: the heights stay as they were
tool.lifting = false;
tool.chain = false;
tool.branchroad = nullptr;
tool.branchjunction = nullptr;
@@ -1111,28 +1120,31 @@ 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())
{
// no farther than what is under the cursor and a bit beyond it (a point in a cutting, under an embankment), so nothing far
// behind the ground or a model shown there is picked through it
auto reach{kReach};
if (GfxRenderer->Mouse_Hit())
reach = std::min(reach, glm::length(GfxRenderer->Mouse_Position()) * 1.1 + 15.0);
// the nearest on the screen, by whole pixels; of those as near, the one nearer to the camera
auto best_pixels{std::numeric_limits<int>::max()};
auto best_range{std::numeric_limits<double>::max()};
bool found{false};
editor_road::each_joint([&](glm::dvec3 const &Point) {
auto const range{glm::distance(Point, camera)};
ImVec2 screen;
if (false == projection.project(joint, screen))
continue;
if (range > reach || false == projection.project(Point, screen))
return;
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;
}
}
auto const pixels{static_cast<int>(std::sqrt(dx * dx + dy * dy))};
if (pixels > 12 || pixels > best_pixels || (pixels == best_pixels && range >= best_range))
return;
best_pixels = pixels;
best_range = range;
Joint = Point;
found = true;
});
return found;
}
@@ -1217,8 +1229,11 @@ void editor_mode::road_click()
}
tool.marker = nullptr;
glm::dvec3 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))
// the point by the ground clicked first, then the piece or the junction under the cursor; the point nearest on the screen
// only when there's none of them (a point under the terrain, in a cutting, can't be got near by the ground clicked)
auto *under{editor_road::nearest(ground, 1.5)};
auto *junction_under{under == nullptr ? editor_road::nearest_junction(ground) : nullptr};
if (editor_road::nearest_joint(ground, kPointRadius, joint) || (under == nullptr && junction_under == nullptr && road_joint_on_screen(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; })};
@@ -1234,12 +1249,10 @@ void editor_mode::road_click()
return;
}
tool.points.clear();
auto *road{editor_road::nearest(ground, 1.5)};
auto *junction{road == nullptr ? editor_road::nearest_junction(ground) : nullptr};
if (road == nullptr && junction == nullptr)
if (under == nullptr && junction_under == nullptr)
tool.status = "No road here";
road_select(road);
junction_select(junction);
road_select(under);
junction_select(junction_under);
return;
}

View File

@@ -1621,22 +1621,15 @@ bool editor_road::nearest_joint(glm::dvec3 const &Point, double const Radius, gl
return found;
}
std::vector<glm::dvec3> editor_road::joints()
void editor_road::each_joint(std::function<void(glm::dvec3 const &)> const &Visit)
{
std::vector<glm::dvec3> result;
for (auto const *road : simulation::Roads.sequence())
{
if (road == nullptr || road->m_editorremoved)
continue;
for (auto const atend : {false, true})
{
auto const position{axis_end(road->definition(), atend)};
auto const same = [&position](glm::dvec3 const &Listed) { return glm::distance(Listed, position) < kSamePoint; };
if (std::none_of(result.begin(), result.end(), same))
result.push_back(position);
}
Visit(axis_end(road->definition(), false));
Visit(axis_end(road->definition(), true));
}
return result;
}
std::vector<editor_road::piece_end> editor_road::ends_at(glm::dvec3 const &Point)

View File

@@ -128,8 +128,8 @@ class editor_road
static junction_node *nearest_junction(glm::dvec3 const &Point);
// the point where pieces meet or end which is nearest to specified one, within specified distance from it. returns: false if there's none
static bool nearest_joint(glm::dvec3 const &Point, double const Radius, glm::dvec3 &Joint);
// every point where pieces meet or end, each once
static std::vector<glm::dvec3> joints();
// Visit called for the ends of every piece, a point where several meet as many times (no search for the repeated ones)
static void each_joint(std::function<void(glm::dvec3 const &)> const &Visit);
// ends of the pieces which meet at specified point
static std::vector<piece_end> ends_at(glm::dvec3 const &Point);
// junctions with an arm at specified point, each with the number of that arm