Skip to content

Commit

Permalink
Added helper functions to encode/decode mTrackToolSection.
Browse files Browse the repository at this point in the history
  • Loading branch information
leozide committed Dec 17, 2024
1 parent 24b7c43 commit 5e0df56
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
7 changes: 3 additions & 4 deletions common/lc_traintrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
// auto replace cross when going over a straight section
// redo gizmo
// add cross to gizmo
// new rotate connection gizmo
// rotate around connections shortcut
// shortcuts for changing active connection
// move config to json
// add other track types
// macros to encode/decode mTrackToolSection
// better transform and connection selection when adding with the keyboard, keep the same curve direction and use straight for branches
// set focus connection after adding

void lcTrainTrackInit(lcPiecesLibrary* Library)
Expand Down Expand Up @@ -55,8 +54,8 @@ void lcTrainTrackInit(lcPiecesLibrary* Library)
{
lcTrainTrackInfo* TrainTrackInfo = new lcTrainTrackInfo();

TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(LC_PI), lcVector3(-320.0f, 0.0f, 0.0f))});
TrainTrackInfo->AddConnection({lcMatrix44Translation(lcVector3(320.0f, 0.0f, 0.0f))});
TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(LC_PI), lcVector3(-320.0f, 0.0f, 0.0f))});
TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(22.5f * LC_DTOR), lcVector3(BranchX, BranchY, 0.0f))});

Info->SetTrainTrackInfo(TrainTrackInfo);
Expand All @@ -68,8 +67,8 @@ void lcTrainTrackInit(lcPiecesLibrary* Library)
{
lcTrainTrackInfo* TrainTrackInfo = new lcTrainTrackInfo();

TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(LC_PI), lcVector3(-320.0f, 0.0f, 0.0f))});
TrainTrackInfo->AddConnection({lcMatrix44Translation(lcVector3(320.0f, 0.0f, 0.0f))});
TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(LC_PI), lcVector3(-320.0f, 0.0f, 0.0f))});
TrainTrackInfo->AddConnection({lcMatrix44(lcMatrix33RotationZ(-22.5f * LC_DTOR), lcVector3(BranchX, -BranchY, 0.0f))});

Info->SetTrainTrackInfo(TrainTrackInfo);
Expand Down
13 changes: 13 additions & 0 deletions common/lc_traintrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ class lcTrainTrackInfo
static std::optional<lcMatrix44> CalculateTransformToConnection(const lcMatrix44& ConnectionTransform, PieceInfo* Info, quint32 ConnectionIndex);
static int GetPieceConnectionIndex(const lcPiece* Piece1, int ConnectionIndex1, const lcPiece* Piece2);

static quint32 EncodeTrackToolSection(quint32 ConnectionIndex, lcTrainTrackType TrainTrackType)
{
return ConnectionIndex | (static_cast<quint32>(TrainTrackType) << 8);
}

static std::pair<quint32, lcTrainTrackType> DecodeTrackToolSection(quint32 TrackToolSection)
{
quint32 ConnectionIndex = TrackToolSection & 0xff;
lcTrainTrackType TrainTrackType = static_cast<lcTrainTrackType>((TrackToolSection >> 8) & 0xff);

return { ConnectionIndex, TrainTrackType };
}

void AddConnection(const lcTrainTrackConnection& TrainTrackConnection)
{
mConnections.emplace_back(TrainTrackConnection);
Expand Down
5 changes: 2 additions & 3 deletions common/lc_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,7 @@ void lcView::UpdatePiecePreview()

if (TrainTrackInfo)
{
quint32 ConnectionIndex = mTrackToolSection & 0xff;
lcTrainTrackType TrainTrackType = static_cast<lcTrainTrackType>((mTrackToolSection >> 8) & 0xff);
auto [ConnectionIndex, TrainTrackType] = lcTrainTrackInfo::DecodeTrackToolSection(mTrackToolSection);

std::tie(PreviewInfo, mPiecePreviewTransform) = TrainTrackInfo->GetPieceInsertTransform(Piece, ConnectionIndex, TrainTrackType);

Expand Down Expand Up @@ -2570,7 +2569,7 @@ void lcView::OnButtonDown(lcTrackButton TrackButton)

case lcTrackTool::RotateTrainTrack:
{
quint32 ConnectionIndex = mTrackToolSection & 0xff;
auto [ConnectionIndex, TrainTrackType] = lcTrainTrackInfo::DecodeTrackToolSection(mTrackToolSection);

ActiveModel->RotateTrainTrackToolClicked(ConnectionIndex);

Expand Down
2 changes: 1 addition & 1 deletion common/lc_viewmanipulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ std::pair<lcTrackTool, quint32> lcViewManipulator::UpdateSelectMove()

NewTrackTool = lcTrackTool::Insert;
ClosestIntersectionDistance = IntersectionDistance;
NewTrackSection = ConnectionIndex | (VertexIndex << 8);
NewTrackSection = lcTrainTrackInfo::EncodeTrackToolSection(ConnectionIndex, static_cast<lcTrainTrackType>(VertexIndex));
}
}
}
Expand Down

0 comments on commit 5e0df56

Please sign in to comment.