Code cleanup.

This commit is contained in:
Leonardo Zide 2024-11-26 15:53:50 -08:00
parent dcfda1e22b
commit f80b5c95e1
5 changed files with 48 additions and 28 deletions

View file

@ -2289,7 +2289,20 @@ void lcModel::AddPiece()
if (Last)
{
if (!PieceInfo->GetTrainTrackInfo() || !Last->mPieceInfo->GetTrainTrackInfo())
bool TransformValid = false;
if (PieceInfo->GetTrainTrackInfo() && Last->mPieceInfo->GetTrainTrackInfo())
{
std::optional<lcMatrix44> TrainTrackTransform = lcTrainTrackInfo::GetPieceInsertTransform(Last, PieceInfo);
if (TrainTrackTransform)
{
TransformValid = true;
WorldMatrix = TrainTrackTransform.value();
}
}
if (!TransformValid)
{
const lcBoundingBox& LastBoundingBox = Last->GetBoundingBox();
lcVector3 Dist(0, 0, LastBoundingBox.Max.z - PieceInfoBoundingBox.Min.z);
@ -2298,14 +2311,6 @@ void lcModel::AddPiece()
WorldMatrix = Last->mModelWorld;
WorldMatrix.SetTranslation(lcMul31(Dist, Last->mModelWorld));
}
else
{
lcTrainTrackInfo* CurrentTrackInfo = Last->mPieceInfo->GetTrainTrackInfo();
std::optional<lcMatrix44> TrainTrackTransform = CurrentTrackInfo->GetPieceInsertPosition(Last, PieceInfo);
if (TrainTrackTransform)
WorldMatrix = TrainTrackTransform.value();
}
}
else
{

View file

@ -71,7 +71,7 @@ void lcTrainTrackInit(lcPiecesLibrary* Library)
}
}
std::pair<PieceInfo*, lcMatrix44> lcTrainTrackInfo::GetPieceInsertPosition(lcPiece* Piece, quint32 ConnectionIndex, lcTrainTrackType TrainTrackType) const
std::pair<PieceInfo*, lcMatrix44> lcTrainTrackInfo::GetPieceInsertTransform(lcPiece* Piece, quint32 ConnectionIndex, lcTrainTrackType TrainTrackType) const
{
if (ConnectionIndex >= mConnections.size())
return { nullptr, lcMatrix44Identity() };
@ -111,36 +111,51 @@ std::pair<PieceInfo*, lcMatrix44> lcTrainTrackInfo::GetPieceInsertPosition(lcPie
return { Info, Transform };
}
std::optional<lcMatrix44> lcTrainTrackInfo::GetPieceInsertPosition(lcPiece* Piece, PieceInfo* Info) const
std::optional<lcMatrix44> lcTrainTrackInfo::GetPieceInsertTransform(lcPiece* CurrentPiece, PieceInfo* Info)
{
quint32 FocusSection = Piece->GetFocusSection();
if (!CurrentPiece || !Info)
return std::nullopt;
const lcTrainTrackInfo* CurrentTrackInfo = CurrentPiece->mPieceInfo->GetTrainTrackInfo();
if (!CurrentTrackInfo || CurrentTrackInfo->GetConnections().empty())
return std::nullopt;
const quint32 FocusSection = CurrentPiece->GetFocusSection();
quint32 ConnectionIndex = 0;
if (FocusSection > LC_PIECE_SECTION_TRAIN_TRACK_CONNECTION_FIRST)
if (FocusSection == LC_PIECE_SECTION_POSITION || FocusSection == LC_PIECE_SECTION_INVALID)
{
// todo: search model for open connection
}
else
{
if (FocusSection < LC_PIECE_SECTION_TRAIN_TRACK_CONNECTION_FIRST)
return std::nullopt;
ConnectionIndex = FocusSection - LC_PIECE_SECTION_TRAIN_TRACK_CONNECTION_FIRST;
if (ConnectionIndex >= mConnections.size())
ConnectionIndex = 0;
if (ConnectionIndex >= CurrentTrackInfo->GetConnections().size())
return std::nullopt;
}
lcTrainTrackInfo* TrainTrackInfo = Info->GetTrainTrackInfo();
lcTrainTrackInfo* NewTrackInfo = Info->GetTrainTrackInfo();
if (!TrainTrackInfo || TrainTrackInfo->mConnections.empty())
if (!NewTrackInfo || NewTrackInfo->mConnections.empty())
return std::nullopt;
lcMatrix44 Transform;
// if (TrainTrackType != lcTrainTrackType::Left)
Transform = TrainTrackInfo->mConnections[0].Transform;
Transform = NewTrackInfo->mConnections[0].Transform;
// else
// {
// Transform = lcMatrix44AffineInverse(TrainTrackInfo->mConnections[0].Transform);
// Transform = lcMul(Transform, lcMatrix44RotationZ(LC_PI));
// }
Transform = lcMul(Transform, mConnections[ConnectionIndex].Transform);
Transform = lcMul(Transform, Piece->mModelWorld);
Transform = lcMul(Transform, CurrentTrackInfo->GetConnections()[ConnectionIndex].Transform);
Transform = lcMul(Transform, CurrentPiece->mModelWorld);
return Transform;
}

View file

@ -25,8 +25,8 @@ class lcTrainTrackInfo
public:
lcTrainTrackInfo() = default;
std::pair<PieceInfo*, lcMatrix44> GetPieceInsertPosition(lcPiece* Piece, quint32 ConnectionIndex, lcTrainTrackType TrainTrackType) const;
std::optional<lcMatrix44> GetPieceInsertPosition(lcPiece* Piece, PieceInfo* Info) const;
std::pair<PieceInfo*, lcMatrix44> GetPieceInsertTransform(lcPiece* Piece, quint32 ConnectionIndex, lcTrainTrackType TrainTrackType) const;
static std::optional<lcMatrix44> GetPieceInsertTransform(lcPiece* CurrentPiece, PieceInfo* Info);
void AddConnection(const lcTrainTrackConnection& TrainTrackConnection)
{

View file

@ -437,7 +437,7 @@ void lcView::UpdatePiecePreview()
quint32 ConnectionIndex = mTrackToolSection & 0xff;
lcTrainTrackType TrainTrackType = static_cast<lcTrainTrackType>((mTrackToolSection >> 8) & 0xff);
std::tie(PreviewInfo, mPiecePreviewTransform) = TrainTrackInfo->GetPieceInsertPosition(Piece, ConnectionIndex, TrainTrackType);
std::tie(PreviewInfo, mPiecePreviewTransform) = TrainTrackInfo->GetPieceInsertTransform(Piece, ConnectionIndex, TrainTrackType);
}
}
@ -447,7 +447,7 @@ void lcView::UpdatePiecePreview()
if (PreviewInfo)
{
mPiecePreviewTransform = GetPieceInsertPosition(false, PreviewInfo);
mPiecePreviewTransform = GetPieceInsertTransform(false, PreviewInfo);
if (GetActiveModel() != mModel)
mPiecePreviewTransform = lcMul(mPiecePreviewTransform, mActiveSubmodelTransform);
@ -468,7 +468,7 @@ void lcView::UpdatePiecePreview()
}
}
lcMatrix44 lcView::GetPieceInsertPosition(bool IgnoreSelected, PieceInfo* Info) const
lcMatrix44 lcView::GetPieceInsertTransform(bool IgnoreSelected, PieceInfo* Info) const
{
lcModel* ActiveModel = GetActiveModel();
@ -1705,7 +1705,7 @@ void lcView::EndDrag(bool Accept)
PieceInfo* Info = gMainWindow->GetCurrentPieceInfo();
if (Info)
ActiveModel->InsertPieceToolClicked(Info, GetPieceInsertPosition(false, Info));
ActiveModel->InsertPieceToolClicked(Info, GetPieceInsertTransform(false, Info));
} break;
case lcDragState::Color:
@ -2855,7 +2855,7 @@ void lcView::OnMouseMove()
}
else if (mTrackTool == lcTrackTool::MoveXYZ && mMouseDownPiece)
{
lcMatrix44 NewPosition = GetPieceInsertPosition(true, mMouseDownPiece);
lcMatrix44 NewPosition = GetPieceInsertTransform(true, mMouseDownPiece);
lcVector3 Distance = NewPosition.GetTranslation() - mMouseDownPosition;
ActiveModel->UpdateMoveTool(Distance, false, mTrackButton != lcTrackButton::Left);
}

View file

@ -260,7 +260,7 @@ public:
float GetOverlayScale() const;
lcVector3 GetMoveDirection(const lcVector3& Direction) const;
void UpdatePiecePreview();
lcMatrix44 GetPieceInsertPosition(bool IgnoreSelected, PieceInfo* Info) const;
lcMatrix44 GetPieceInsertTransform(bool IgnoreSelected, PieceInfo* Info) const;
lcVector3 GetCameraLightInsertPosition() const;
void GetRayUnderPointer(lcVector3& Start, lcVector3& End) const;
lcObjectSection FindObjectUnderPointer(bool PiecesOnly, bool IgnoreSelected) const;