diff --git a/common/lc_library.cpp b/common/lc_library.cpp index aefcdfa0..7685289c 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -279,7 +279,7 @@ bool lcPiecesLibrary::Load(const QString& LibraryPath, bool ShowProgress) UpdateStudStyleSource(); lcLoadDefaultCategories(); lcSynthInit(); - lcTrainTrackInit(this); + lcTrainTrackInfo::Initialize(this); return true; } @@ -1888,7 +1888,7 @@ bool lcPiecesLibrary::LoadBuiltinPieces() lcLoadDefaultColors(lcStudStyle::Plain); lcLoadDefaultCategories(true); lcSynthInit(); - lcTrainTrackInit(this); + lcTrainTrackInfo::Initialize(this); return true; } diff --git a/common/lc_traintrack.cpp b/common/lc_traintrack.cpp index 1449aee0..a9afac5b 100644 --- a/common/lc_traintrack.cpp +++ b/common/lc_traintrack.cpp @@ -8,13 +8,14 @@ // todo: // add the rest of the 9v and 12v tracks, look into 4.5v // see if we should remove some of the 12v tracks to avoid bloat -// data driven sleepers // lcView::GetPieceInsertTransform should use track connections when dragging a new track over an existing one // better insert gizmo mouse detection // auto replace cross when going over a straight section // set focus connection after adding -void lcTrainTrackInit(lcPiecesLibrary* Library) +std::map lcTrainTrackInfo::mSleepers; + +void lcTrainTrackInfo::Initialize(lcPiecesLibrary* Library) { QFile ConfigFile(QLatin1String(":/resources/traintrack.json")); @@ -73,6 +74,21 @@ void lcTrainTrackInit(lcPiecesLibrary* Library) } } } + + QJsonObject JsonSleepers = Root["Sleepers"].toObject(); + + mSleepers.clear(); + + for (QJsonObject::const_iterator SleepersIt = JsonSleepers.constBegin(); SleepersIt != JsonSleepers.constEnd(); ++SleepersIt) + { + quint32 Group = qHash(SleepersIt.key()); + QString PartId = SleepersIt->toString(); + + PieceInfo* SleeperInfo = Library->FindPiece(PartId.toLatin1(), nullptr, false, false); + + if (SleeperInfo) + mSleepers[Group] = SleeperInfo; + } } int lcTrainTrackInfo::GetPieceConnectionIndex(const lcPiece* Piece1, int ConnectionIndex1, const lcPiece* Piece2) @@ -137,7 +153,12 @@ std::vector lcTrainTrackInfo::GetPieceInsertTransforms(lcPie if (CurrentConnectionType.Sleeper == lcTrainTrackConnectionSleeper::NeedsSleeper && NewConnections[NewConnectionIndex].Type.Sleeper == lcTrainTrackConnectionSleeper::NeedsSleeper) { - PieceInfo* SleeperInfo = lcGetPiecesLibrary()->FindPiece("4166a.dat", nullptr, false, false); // todo: data driven + std::map::const_iterator SleeperIt = mSleepers.find(CurrentConnectionType.Group); + + if (SleeperIt == mSleepers.end()) + return Pieces; + + PieceInfo* SleeperInfo = SleeperIt->second; if (!SleeperInfo) return Pieces; diff --git a/common/lc_traintrack.h b/common/lc_traintrack.h index 06c1f2cb..cb58c41d 100644 --- a/common/lc_traintrack.h +++ b/common/lc_traintrack.h @@ -37,6 +37,7 @@ class lcTrainTrackInfo public: lcTrainTrackInfo() = default; + static void Initialize(lcPiecesLibrary* Library); static std::vector GetPieceInsertTransforms(lcPiece* CurrentPiece, PieceInfo* Info, quint32 Section); static std::optional GetConnectionTransform(PieceInfo* CurrentInfo, const lcMatrix44& CurrentTransform, quint32 CurrentConnectionIndex, PieceInfo* Info, quint32 NewConnectionIndex); static std::optional CalculateTransformToConnection(const lcMatrix44& ConnectionTransform, PieceInfo* Info, quint32 ConnectionIndex); @@ -83,6 +84,5 @@ protected: } std::vector mConnections; + static std::map mSleepers; }; - -void lcTrainTrackInit(lcPiecesLibrary* Library); diff --git a/resources/traintrack.json b/resources/traintrack.json index c78d8fb9..340fa1b8 100644 --- a/resources/traintrack.json +++ b/resources/traintrack.json @@ -297,5 +297,9 @@ } ] } + }, + "Sleepers": + { + "12V": "4166a.dat" } }