Skip to content

Commit

Permalink
Added rotate train track gizmo.
Browse files Browse the repository at this point in the history
  • Loading branch information
leozide committed Dec 18, 2024
1 parent 5e0df56 commit 4f2782b
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 171 deletions.
2 changes: 1 addition & 1 deletion common/lc_mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2941,7 +2941,7 @@ void lcMainWindow::HandleCommand(lcCommandId CommandId)

case LC_PIECE_TRAIN_TRACK_ROTATE:
if (ActiveModel)
ActiveModel->RotateFocusedTrainTrack();
ActiveModel->RotateFocusedTrainTrack(1);
break;

case LC_PIECE_MOVE_PLUSX:
Expand Down
52 changes: 2 additions & 50 deletions common/lc_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,7 @@ void lcModel::FocusPreviousTrainTrack()
UpdateAllViews();
}

void lcModel::RotateFocusedTrainTrack()
void lcModel::RotateFocusedTrainTrack(int Direction)
{
const lcObject* Focus = GetFocusObject();

Expand Down Expand Up @@ -2472,7 +2472,7 @@ void lcModel::RotateFocusedTrainTrack()
}

lcMatrix44 ConnectionTransform = lcMul(TrainTrackInfo->GetConnections()[RotateConnectionIndex].Transform, FocusPiece->mModelWorld);
int NewConnectionIndex = (RotateConnectionIndex + 1) % TrainTrackInfo->GetConnections().size();
int NewConnectionIndex = (RotateConnectionIndex + Direction + static_cast<int>(TrainTrackInfo->GetConnections().size())) % TrainTrackInfo->GetConnections().size();

Transform = lcTrainTrackInfo::CalculateTransformToConnection(ConnectionTransform, FocusPiece->mPieceInfo, NewConnectionIndex);

Expand Down Expand Up @@ -4525,54 +4525,6 @@ void lcModel::UpdateRotateTool(const lcVector3& Angles, bool AlternateButtonDrag
UpdateAllViews();
}

void lcModel::RotateTrainTrackToolClicked(quint32 ConnectionIndex)
{
const lcObject* Focus = GetFocusObject();

if (!Focus || !Focus->IsPiece())
return;

lcPiece* FocusPiece = (lcPiece*)Focus;
const lcTrainTrackInfo* TrainTrackInfo = FocusPiece->mPieceInfo->GetTrainTrackInfo();

if (!TrainTrackInfo)
return;

lcPiece* ConnectedPiece = nullptr;
int ConnectedPieceConnectionIndex = -1;

for (const std::unique_ptr<lcPiece>& Piece : mPieces)
{
if (Piece.get() == FocusPiece || !Piece->mPieceInfo->GetTrainTrackInfo())
continue;

ConnectedPieceConnectionIndex = lcTrainTrackInfo::GetPieceConnectionIndex(FocusPiece, ConnectionIndex, Piece.get());

if (ConnectedPieceConnectionIndex != -1)
{
ConnectedPiece = Piece.get();
break;
}
}

if (!ConnectedPiece)
return;

quint32 NewConnectionIndex = (ConnectionIndex + 1) % TrainTrackInfo->GetConnections().size();
std::optional<lcMatrix44> Transform = lcTrainTrackInfo::GetConnectionTransform(ConnectedPiece, ConnectedPieceConnectionIndex, FocusPiece->mPieceInfo, NewConnectionIndex);

if (!Transform)
return;

FocusPiece->SetPosition(Transform.value().GetTranslation(), mCurrentStep, gMainWindow->GetAddKeys());
FocusPiece->SetRotation(lcMatrix33(Transform.value()), mCurrentStep, gMainWindow->GetAddKeys());
FocusPiece->UpdatePosition(mCurrentStep);

gMainWindow->UpdateSelectedObjects(true);
UpdateAllViews();
SaveCheckpoint(tr("Rotating"));
}

void lcModel::UpdateScaleTool(const float Scale)
{
ScaleSelectedPieces(Scale, true, false);
Expand Down
3 changes: 1 addition & 2 deletions common/lc_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class lcModel
void RemoveFocusedControlPoint();
void FocusNextTrainTrack();
void FocusPreviousTrainTrack();
void RotateFocusedTrainTrack();
void RotateFocusedTrainTrack(int Direction);
void ShowSelectedPiecesEarlier();
void ShowSelectedPiecesLater();
void SetPieceSteps(const std::vector<std::pair<lcPiece*, lcStep>>& PieceSteps);
Expand Down Expand Up @@ -339,7 +339,6 @@ class lcModel
void UpdateCameraTool(const lcVector3& Position);
void UpdateMoveTool(const lcVector3& Distance, bool AllowRelative, bool AlternateButtonDrag);
void UpdateRotateTool(const lcVector3& Angles, bool AlternateButtonDrag);
void RotateTrainTrackToolClicked(quint32 ConnectionIndex);
void UpdateScaleTool(const float Scale);
void EraserToolClicked(lcObject* Object);
void PaintToolClicked(lcObject* Object);
Expand Down
3 changes: 0 additions & 3 deletions common/lc_traintrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
// 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
// set focus connection after adding
Expand Down
30 changes: 21 additions & 9 deletions common/lc_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,8 @@ lcCursor lcView::GetCursor() const
lcCursor::Rotate, // lcTrackTool::RotateZ
lcCursor::Rotate, // lcTrackTool::RotateXY
lcCursor::Rotate, // lcTrackTool::RotateXYZ
lcCursor::Rotate, // lcTrackTool::RotateTrainTrack
lcCursor::Rotate, // lcTrackTool::RotateTrainTrackRight
lcCursor::Rotate, // lcTrackTool::RotateTrainTrackLeft
lcCursor::Move, // lcTrackTool::ScalePlus
lcCursor::Move, // lcTrackTool::ScaleMinus
lcCursor::Delete, // lcTrackTool::Eraser
Expand Down Expand Up @@ -2037,7 +2038,8 @@ lcTool lcView::GetCurrentTool() const
lcTool::Rotate, // lcTrackTool::RotateZ
lcTool::Rotate, // lcTrackTool::RotateXY
lcTool::Rotate, // lcTrackTool::RotateXYZ
lcTool::Rotate, // lcTrackTool::RotateTrainTrack
lcTool::Rotate, // lcTrackTool::RotateTrainTrackRight
lcTool::Rotate, // lcTrackTool::RotateTrainTrackLeft
lcTool::Move, // lcTrackTool::ScalePlus
lcTool::Move, // lcTrackTool::ScaleMinus
lcTool::Eraser, // lcTrackTool::Eraser
Expand Down Expand Up @@ -2567,18 +2569,16 @@ void lcView::OnButtonDown(lcTrackButton TrackButton)
StartTracking(TrackButton);
break;

case lcTrackTool::RotateTrainTrack:
case lcTrackTool::RotateTrainTrackRight:
case lcTrackTool::RotateTrainTrackLeft:
{
auto [ConnectionIndex, TrainTrackType] = lcTrainTrackInfo::DecodeTrackToolSection(mTrackToolSection);

ActiveModel->RotateTrainTrackToolClicked(ConnectionIndex);
ActiveModel->RotateFocusedTrainTrack(mTrackTool == lcTrackTool::RotateTrainTrackRight ? 1 : -1);

mToolClicked = true;
UpdateTrackTool();
}
break;


case lcTrackTool::ScalePlus:
case lcTrackTool::ScaleMinus:
if (ActiveModel->AnyPiecesSelected())
Expand Down Expand Up @@ -2653,9 +2653,20 @@ void lcView::OnLeftButtonDoubleClick()
return;
}

lcObjectSection ObjectSection = FindObjectUnderPointer(false, false);
lcModel* ActiveModel = GetActiveModel();

if (mTrackTool == lcTrackTool::RotateTrainTrackRight || mTrackTool == lcTrackTool::RotateTrainTrackLeft)
{
ActiveModel->RotateFocusedTrainTrack(mTrackTool == lcTrackTool::RotateTrainTrackRight ? 1 : -1);

mToolClicked = true;
UpdateTrackTool();

return;
}

lcObjectSection ObjectSection = FindObjectUnderPointer(false, false);

if (mMouseModifiers & Qt::ControlModifier)
ActiveModel->FocusOrDeselectObject(ObjectSection);
else if (mMouseModifiers & Qt::ShiftModifier)
Expand Down Expand Up @@ -3011,7 +3022,8 @@ void lcView::OnMouseMove()
}
break;

case lcTrackTool::RotateTrainTrack:
case lcTrackTool::RotateTrainTrackRight:
case lcTrackTool::RotateTrainTrackLeft:
case lcTrackTool::Eraser:
case lcTrackTool::Paint:
case lcTrackTool::ColorPicker:
Expand Down
3 changes: 2 additions & 1 deletion common/lc_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ enum class lcTrackTool
RotateZ,
RotateXY,
RotateXYZ,
RotateTrainTrack,
RotateTrainTrackRight,
RotateTrainTrackLeft,
ScalePlus,
ScaleMinus,
Eraser,
Expand Down
Loading

0 comments on commit 4f2782b

Please sign in to comment.