Skip to content

Commit

Permalink
Replaced train track gizmo with a popup list.
Browse files Browse the repository at this point in the history
  • Loading branch information
leozide committed Dec 19, 2024
1 parent 4f2782b commit b9dc71d
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 188 deletions.
12 changes: 12 additions & 0 deletions common/lc_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "lc_glextensions.h"
#include "lc_synth.h"
#include "lc_traintrack.h"
#include "lc_traintrack.h"
#include "project.h"
#include "lc_profile.h"
#include "lc_meshloader.h"
Expand Down Expand Up @@ -1807,6 +1808,17 @@ void lcPiecesLibrary::GetParts(std::vector<PieceInfo*>& Parts) const
Parts.emplace_back(PartIt.second);
}

std::vector<PieceInfo*> lcPiecesLibrary::GetTrainTrackParts(const lcTrainTrackInfo* TrainTrackInfo) const
{
std::vector<PieceInfo*> Parts;

for (const auto& [Name, Info] : mPieces)
if (Info->GetTrainTrackInfo())
Parts.emplace_back(Info);

return Parts;
}

std::vector<PieceInfo*> lcPiecesLibrary::GetPartsFromSet(const std::vector<std::string>& PartIds) const
{
std::vector<PieceInfo*> Parts;
Expand Down
2 changes: 2 additions & 0 deletions common/lc_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "lc_meshloader.h"

class PieceInfo;
class lcTrainTrackInfo;
class lcZipFile;
class lcLibraryMeshData;
class lcThumbnailManager;
Expand Down Expand Up @@ -155,6 +156,7 @@ class lcPiecesLibrary : public QObject
void GetCategoryEntries(int CategoryIndex, bool GroupPieces, std::vector<PieceInfo*>& SinglePieces, std::vector<PieceInfo*>& GroupedPieces);
void GetCategoryEntries(const char* CategoryKeywords, bool GroupPieces, std::vector<PieceInfo*>& SinglePieces, std::vector<PieceInfo*>& GroupedPieces);
void GetParts(std::vector<PieceInfo*>& Parts) const;
std::vector<PieceInfo*> GetTrainTrackParts(const lcTrainTrackInfo* TrainTrackInfo) const;

std::vector<PieceInfo*> GetPartsFromSet(const std::vector<std::string>& PartIds) const;
std::string GetPartId(const PieceInfo* Info) const;
Expand Down
2 changes: 1 addition & 1 deletion common/lc_mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2891,7 +2891,7 @@ void lcMainWindow::HandleCommand(lcCommandId CommandId)

case LC_PIECE_INSERT:
if (ActiveModel)
ActiveModel->AddPiece();
ActiveModel->AddPiece(static_cast<PieceInfo*>(nullptr));
break;

case LC_PIECE_DELETE:
Expand Down
5 changes: 3 additions & 2 deletions common/lc_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2281,9 +2281,10 @@ lcMatrix33 lcModel::GetRelativeRotation() const
return lcMatrix33Identity();
}

void lcModel::AddPiece()
void lcModel::AddPiece(PieceInfo* PieceInfo)
{
PieceInfo* PieceInfo = gMainWindow->GetCurrentPieceInfo();
if (!PieceInfo)
PieceInfo = gMainWindow->GetCurrentPieceInfo();

if (!PieceInfo)
return;
Expand Down
2 changes: 1 addition & 1 deletion common/lc_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class lcModel
void InsertStep(lcStep Step);
void RemoveStep(lcStep Step);

void AddPiece();
void AddPiece(PieceInfo* Info);
void DeleteAllCameras();
void DeleteSelectedObjects();
void ResetSelectedPiecesPivotPoint();
Expand Down
41 changes: 36 additions & 5 deletions common/lc_partselectionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,28 @@ void lcPartSelectionListModel::SetCurrentModelCategory()
SetFilter(mFilter);
}

void lcPartSelectionListModel::SetCustomParts(const std::vector<PieceInfo*>& Parts)
{
beginResetModel();

ReleaseThumbnails();
mParts.clear();

for (PieceInfo* Part : Parts)
mParts.emplace_back().Info = Part;

auto lcPartSortFunc = [](const lcPartSelectionListModelEntry& a, const lcPartSelectionListModelEntry& b)
{
return strcmp(a.Info->m_strDescription, b.Info->m_strDescription) < 0;
};

std::sort(mParts.begin(), mParts.end(), lcPartSortFunc);

endResetModel();

SetFilter(mFilter);
}

void lcPartSelectionListModel::SetFilter(const QString& Filter)
{
mFilter = Filter.toLatin1();
Expand Down Expand Up @@ -511,12 +533,8 @@ void lcPartSelectionListModel::SetShowPartNames(bool Show)
}

lcPartSelectionListView::lcPartSelectionListView(QWidget* Parent, lcPartSelectionWidget* PartSelectionWidget)
: QListView(Parent)
: QListView(Parent), mPartSelectionWidget(PartSelectionWidget)
{
mPartSelectionWidget = PartSelectionWidget;
mCategoryType = lcPartCategoryType::AllParts;
mCategoryIndex = 0;

setUniformItemSizes(true);
setResizeMode(QListView::Adjust);
setWordWrap(false);
Expand All @@ -535,6 +553,9 @@ lcPartSelectionListView::lcPartSelectionListView(QWidget* Parent, lcPartSelectio

void lcPartSelectionListView::CustomContextMenuRequested(QPoint Pos)
{
if (!mPartSelectionWidget)
return;

QMenu* Menu = new QMenu(this);

QModelIndex Index = indexAt(Pos);
Expand Down Expand Up @@ -584,13 +605,23 @@ void lcPartSelectionListView::SetCategory(lcPartCategoryType Type, int Index)
case lcPartCategoryType::Category:
mListModel->SetCategory(Index);
break;
case lcPartCategoryType::Custom:
case lcPartCategoryType::Count:
break;
}

setCurrentIndex(mListModel->index(0, 0));
}

void lcPartSelectionListView::SetCustomParts(const std::vector<PieceInfo*>& Parts)
{
mCategoryType = lcPartCategoryType::Custom;

mListModel->SetCustomParts(Parts);

setCurrentIndex(mListModel->index(0, 0));
}

void lcPartSelectionListView::SetCurrentPart(PieceInfo* Info)
{
QModelIndex Index = mListModel->GetPieceInfoIndex(Info);
Expand Down
13 changes: 8 additions & 5 deletions common/lc_partselectionwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum class lcPartCategoryType
Submodels,
Palette,
Category,
Custom,
Count
};

Expand Down Expand Up @@ -148,6 +149,7 @@ class lcPartSelectionListModel : public QAbstractListModel
void SetModelsCategory();
void SetPaletteCategory(int SetIndex);
void SetCurrentModelCategory();
void SetCustomParts(const std::vector<PieceInfo*>& Parts);
void SetFilter(const QString& Filter);
void RequestThumbnail(int PartIndex);
void SetShowDecoratedParts(bool Show);
Expand Down Expand Up @@ -191,6 +193,7 @@ class lcPartSelectionListView : public QListView
void startDrag(Qt::DropActions SupportedActions) override;

void SetCategory(lcPartCategoryType Type, int Index);
void SetCustomParts(const std::vector<PieceInfo*>& Parts);
void SetCurrentPart(PieceInfo* Info);

PieceInfo* GetCurrentPart() const
Expand Down Expand Up @@ -239,11 +242,11 @@ public slots:
void SetIconSize(int Size);
void SetPartFilterType(lcPartFilterType Option);

lcPartSelectionListModel* mListModel;
lcPartSelectionWidget* mPartSelectionWidget;
PieceInfo* mContextInfo;
lcPartCategoryType mCategoryType;
int mCategoryIndex;
lcPartSelectionListModel* mListModel = nullptr;
lcPartSelectionWidget* mPartSelectionWidget = nullptr;
PieceInfo* mContextInfo = nullptr;
lcPartCategoryType mCategoryType = lcPartCategoryType::AllParts;
int mCategoryIndex = 0;
};

class lcPartSelectionWidget : public QWidget
Expand Down
40 changes: 0 additions & 40 deletions common/lc_traintrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,46 +86,6 @@ void lcTrainTrackInit(lcPiecesLibrary* Library)
}
}

std::pair<PieceInfo*, lcMatrix44> lcTrainTrackInfo::GetPieceInsertTransform(lcPiece* Piece, quint32 ConnectionIndex, lcTrainTrackType TrainTrackType) const
{
if (ConnectionIndex >= 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<int>(TrainTrackType)], nullptr, false, false);

if (!Info)
return { nullptr, lcMatrix44Identity() };

lcTrainTrackInfo* TrainTrackInfo = Info->GetTrainTrackInfo();

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 = lcMul(Transform, mConnections[ConnectionIndex].Transform);
Transform = lcMul(Transform, Piece->mModelWorld);

return { Info, Transform };
}

int lcTrainTrackInfo::GetPieceConnectionIndex(const lcPiece* Piece1, int ConnectionIndex1, const lcPiece* Piece2)
{
const lcTrainTrackInfo* TrainTrackInfo1 = Piece1->mPieceInfo->GetTrainTrackInfo();
Expand Down
14 changes: 0 additions & 14 deletions common/lc_traintrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,11 @@ class lcTrainTrackInfo
public:
lcTrainTrackInfo() = default;

std::pair<PieceInfo*, lcMatrix44> GetPieceInsertTransform(lcPiece* Piece, quint32 ConnectionIndex, lcTrainTrackType TrainTrackType) const;
static std::optional<lcMatrix44> GetPieceInsertTransform(lcPiece* CurrentPiece, PieceInfo* Info);
static std::optional<lcMatrix44> GetConnectionTransform(lcPiece* CurrentPiece, quint32 CurrentConnectionIndex, PieceInfo* Info, quint32 NewConnectionIndex);
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
Loading

0 comments on commit b9dc71d

Please sign in to comment.