diff --git a/application/editormode.cpp b/application/editormode.cpp index 32f991c55..991040d3e 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -5518,6 +5518,18 @@ void editor_mode::on_mouse_button(int const Button, int const Action, int const // 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 diff --git a/application/editormode.h b/application/editormode.h index c7dbd3911..e06d79a46 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -1923,6 +1923,11 @@ class editor_mode : public application_mode, private editor_track::observer bool follow{true}; // built roads are led over the ground between the clicked points float crossingspeed{30.f}; // speed limit on the junctions being made float heightstep{0.1f}; // how much the selected points are raised or lowered at a time + // Alt+LMB on a point dragged up or down: the heights of the selected points changed by the drag, applied when let go + bool lifting{false}; + float lift_mouse{0.0f}; // where on the screen the drag began, pixels down + double lift_scale{0.0}; // metres for a pixel at the point picked + std::vector lift_from; // the heights of the selected points when the drag began bool ring{false}; // the build tool makes roundabouts float ringradius{15.f}; // of the middle of the road going around std::string island{"none"}; // what the middle of a roundabout is covered with @@ -1967,6 +1972,13 @@ class editor_mode : public application_mode, private editor_track::observer void draw_road_overlay() const; void update_road_tool(); void road_click(); + // the point where pieces meet or end nearest to the cursor on the screen, within a few pixels; it's found under the + // terrain too, where the click on the ground doesn't get near it + bool road_joint_on_screen(glm::dvec3 &Joint) const; + // Alt+LMB: the drag of the heights of the selected points begun on a point, and finished + bool road_lift_start(); + void road_lift_finish(); + double road_lift_offset() const; void road_select(road_node *Road); void junction_select(junction_node *Junction); void road_apply(); diff --git a/application/editormoderoad.cpp b/application/editormoderoad.cpp index 9b5dc64ac..1c3130697 100644 --- a/application/editormoderoad.cpp +++ b/application/editormoderoad.cpp @@ -23,6 +23,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include namespace { @@ -1106,6 +1107,87 @@ void editor_mode::road_profile(std::vector &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::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(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 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"; diff --git a/editor/editorRoad.cpp b/editor/editorRoad.cpp index 8e2113d17..0def167d9 100644 --- a/editor/editorRoad.cpp +++ b/editor/editorRoad.cpp @@ -1621,6 +1621,24 @@ bool editor_road::nearest_joint(glm::dvec3 const &Point, double const Radius, gl return found; } +std::vector editor_road::joints() +{ + std::vector 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); + } + } + return result; +} + std::vector editor_road::ends_at(glm::dvec3 const &Point) { std::vector result; diff --git a/editor/editorRoad.hpp b/editor/editorRoad.hpp index 20553b013..7c19b00f5 100644 --- a/editor/editorRoad.hpp +++ b/editor/editorRoad.hpp @@ -128,6 +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 joints(); // ends of the pieces which meet at specified point static std::vector ends_at(glm::dvec3 const &Point); // junctions with an arm at specified point, each with the number of that arm