From f460e65856d91c645206b7014a58f98dbab3adae Mon Sep 17 00:00:00 2001 From: cambrialas Date: Sat, 9 Nov 2024 10:22:15 +0100 Subject: [PATCH] Rotate track while holding left mouse button down and move left or right --- common/lc_traintrack.cpp | 51 +++++++++++++++++++++-------------- common/lc_traintrack.h | 8 +++--- common/lc_view.cpp | 48 ++++++++++++++++++++++++++++++--- common/lc_view.h | 1 + common/lc_viewmanipulator.cpp | 5 +++- 5 files changed, 84 insertions(+), 29 deletions(-) diff --git a/common/lc_traintrack.cpp b/common/lc_traintrack.cpp index 6a7246dc..541765c1 100644 --- a/common/lc_traintrack.cpp +++ b/common/lc_traintrack.cpp @@ -13,41 +13,38 @@ // move config to json // add other track types -std::pair lcTrainTrackInfo::GetPieceInsertPosition(lcPiece* Piece, quint32 ConnectionIndex, lcTrainTrackType TrainTrackType) const +std::pair lcTrainTrackInfo::GetPieceInsertPosition(lcPiece* Piece, quint32 fromConnectionIdx, lcTrainTrackType TrainTrackType, quint32 toConnectionIdx) const { - if (ConnectionIndex >= mConnections.size()) + const char* TrainTrackPieceNames[] = + { + "74746.dat", + "2861c04.dat", + "2859c04.dat", + "74747.dat", + "32087.dat" + }; + + if (fromConnectionIdx >= mConnections.size()) return { nullptr, lcMatrix44Identity() }; - const char* PieceNames[] = - { - "74746.dat", - "74747.dat", - "74747.dat", - "2861c04.dat", - "2859c04.dat" - }; - - PieceInfo* Info = lcGetPiecesLibrary()->FindPiece(PieceNames[static_cast(TrainTrackType)], nullptr, false, false); + PieceInfo* Info = lcGetPiecesLibrary()->FindPiece(TrainTrackPieceNames[static_cast(TrainTrackType)], nullptr, false, false); if (!Info) return { nullptr, lcMatrix44Identity() }; lcTrainTrackInfo* TrainTrackInfo = Info->GetTrainTrackInfo(); + toConnectionIdx = toConnectionIdx % TrainTrackInfo->mConnections.size(); + if (!TrainTrackInfo || TrainTrackInfo->mConnections.empty()) return { nullptr, lcMatrix44Identity() }; lcMatrix44 Transform; - if (TrainTrackType != lcTrainTrackType::Left) - Transform = TrainTrackInfo->mConnections[0].Transform; - else - { - Transform = lcMatrix44AffineInverse(TrainTrackInfo->mConnections[0].Transform); - Transform = lcMul(Transform, lcMatrix44RotationZ(LC_PI)); - } + Transform = lcMatrix44AffineInverse(TrainTrackInfo->mConnections[toConnectionIdx].Transform); + Transform = lcMul(Transform, lcMatrix44RotationZ(LC_PI)); - Transform = lcMul(Transform, mConnections[ConnectionIndex].Transform); + Transform = lcMul(Transform, mConnections[fromConnectionIdx].Transform); Transform = lcMul(Transform, Piece->mModelWorld); return { Info, Transform }; @@ -108,6 +105,20 @@ void lcTrainTrackInit(lcPiecesLibrary* Library) TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(-22.5f * LC_DTOR), lcVector3(BranchX, -BranchY, 0.0f))}); TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(LC_PI), lcVector3(-320.0f, 0.0f, 0.0f))}); + Info->SetTrainTrackInfo(TrainTrackInfo); + } + + Info = Library->FindPiece("32087.dat", nullptr, false, false); + + if (Info) + { + lcTrainTrackInfo* TrainTrackInfo = new lcTrainTrackInfo(); + + TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(0.0f * LC_DTOR), lcVector3(160.0f, 0.0f, 0.0f))}); + TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(90.0f * LC_DTOR), lcVector3(0.0f, 160.0f, 0.0f))}); + TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(180.0f * LC_DTOR), lcVector3(-160.0f, 0.0f, 0.0f))}); + TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(-90.0f * LC_DTOR), lcVector3(0.0f, -160.0f, 0.0f))}); + Info->SetTrainTrackInfo(TrainTrackInfo); } } diff --git a/common/lc_traintrack.h b/common/lc_traintrack.h index 7a12d86d..6b0ba272 100644 --- a/common/lc_traintrack.h +++ b/common/lc_traintrack.h @@ -13,10 +13,10 @@ struct lcTrainTrackConnection enum class lcTrainTrackType { Straight, + BranchLeft, + BranchRight, Left, - Right, - BranchLeft, - BranchRight, + Cross, Count }; @@ -25,7 +25,7 @@ class lcTrainTrackInfo public: lcTrainTrackInfo() = default; - std::pair GetPieceInsertPosition(lcPiece* Piece, quint32 ConnectionIndex, lcTrainTrackType TrainTrackType) const; + std::pair GetPieceInsertPosition(lcPiece* Piece, quint32 fromConnectionIdx, lcTrainTrackType TrainTrackType, quint32 toConnectionIdx) const; void AddConnection(const lcTrainTrackConnection& TrainTrackConnection) { diff --git a/common/lc_view.cpp b/common/lc_view.cpp index 189b2df9..250b17ca 100644 --- a/common/lc_view.cpp +++ b/common/lc_view.cpp @@ -436,8 +436,9 @@ void lcView::UpdatePiecePreview() { quint32 ConnectionIndex = mTrackToolSection & 0xff; lcTrainTrackType TrainTrackType = static_cast((mTrackToolSection >> 8) & 0xff); + int toConnectionIdx = (mTrackToolSection >> 16) & 0xFF; - std::tie(PreviewInfo, mPiecePreviewTransform) = TrainTrackInfo->GetPieceInsertPosition(Piece, ConnectionIndex, TrainTrackType); + std::tie(PreviewInfo, mPiecePreviewTransform) = TrainTrackInfo->GetPieceInsertPosition(Piece, ConnectionIndex, TrainTrackType, toConnectionIdx); } } @@ -868,7 +869,7 @@ void lcView::OnDraw() mModel->GetScene(mScene.get(), mCamera, Preferences.mHighlightNewParts, Preferences.mFadeSteps); - if (DrawInterface && mTrackTool == lcTrackTool::Insert) + if (DrawInterface && mTrackTool == lcTrackTool::RotateTrack) { UpdatePiecePreview(); @@ -1931,7 +1932,8 @@ lcCursor lcView::GetCursor() const lcCursor::RotateY, // lcTrackTool::OrbitY lcCursor::RotateView, // lcTrackTool::OrbitXY lcCursor::Roll, // lcTrackTool::Roll - lcCursor::ZoomRegion // lcTrackTool::ZoomRegion + lcCursor::ZoomRegion, // lcTrackTool::ZoomRegion + lcCursor::Rotate // lcTrackTool::RotateTrack }; LC_ARRAY_SIZE_CHECK(CursorFromTrackTool, lcTrackTool::Count); @@ -2040,7 +2042,8 @@ lcTool lcView::GetCurrentTool() const lcTool::RotateView, // lcTrackTool::OrbitY lcTool::RotateView, // lcTrackTool::OrbitXY lcTool::Roll, // lcTrackTool::Roll - lcTool::ZoomRegion // lcTrackTool::ZoomRegion + lcTool::ZoomRegion, // lcTrackTool::ZoomRegion + lcTool::Rotate // lcTrackTool::RotateTrack }; LC_ARRAY_SIZE_CHECK(ToolFromTrackTool, lcTrackTool::Count); @@ -2395,6 +2398,24 @@ void lcView::StopTracking(bool Accept) case lcTool::Move: case lcTool::Rotate: + + if(mTrackTool == lcTrackTool::RotateTrack) { + + UpdatePiecePreview(); + + if (!mPiecePreviewInfo) + break; + + ActiveModel->InsertPieceToolClicked(mPiecePreviewInfo, mPiecePreviewTransform); + + if ((mMouseModifiers & Qt::ControlModifier) == 0) + gMainWindow->SetTool(lcTool::Select); + + mToolClicked = true; + UpdateTrackTool(); + + } + ActiveModel->EndMouseTool(Tool, Accept); break; @@ -2586,6 +2607,14 @@ void lcView::OnButtonDown(lcTrackButton TrackButton) StartTracking(TrackButton); break; + case lcTrackTool::RotateTrack: + { + lcObject* Focus = ActiveModel->GetFocusObject(); + if (Focus && Focus->IsPiece() && ((lcPiece*)Focus)->mPieceInfo->GetTrainTrackInfo()) + StartTracking(TrackButton); + } + break; + case lcTrackTool::Count: break; } @@ -3020,6 +3049,17 @@ void lcView::OnMouseMove() Redraw(); break; + case lcTrackTool::RotateTrack: + { + int trackRotation = (float)(mMouseX - mMouseDownX) * MouseSensitivity;; + trackRotation = abs(trackRotation); + mTrackToolSection &= 0xFF00FFFF; + mTrackToolSection += (trackRotation << 16); + + Redraw(); + } + break; + case lcTrackTool::Count: break; } diff --git a/common/lc_view.h b/common/lc_view.h index 6463e096..0ec1ae4e 100644 --- a/common/lc_view.h +++ b/common/lc_view.h @@ -82,6 +82,7 @@ enum class lcTrackTool OrbitXY, Roll, ZoomRegion, + RotateTrack, Count }; diff --git a/common/lc_viewmanipulator.cpp b/common/lc_viewmanipulator.cpp index b646c8b6..2a20d67f 100644 --- a/common/lc_viewmanipulator.cpp +++ b/common/lc_viewmanipulator.cpp @@ -772,6 +772,9 @@ bool lcViewManipulator::IsTrackToolAllowed(lcTrackTool TrackTool, quint32 Allowe case lcTrackTool::ZoomRegion: return true; + case lcTrackTool::RotateTrack: + return true; + case lcTrackTool::Count: return false; } @@ -979,7 +982,7 @@ std::pair lcViewManipulator::UpdateSelectMove() if (IntersectionDistance > ClosestIntersectionDistance) continue; - NewTrackTool = lcTrackTool::Insert; + NewTrackTool = lcTrackTool::RotateTrack; ClosestIntersectionDistance = IntersectionDistance; NewTrackSection = Section | (VertexIndex << 8); }