diff --git a/common/lc_library.cpp b/common/lc_library.cpp index 7685289c..1e8f7475 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -1808,7 +1808,7 @@ void lcPiecesLibrary::GetParts(std::vector& Parts) const Parts.emplace_back(PartIt.second); } -std::vector lcPiecesLibrary::GetTrainTrackParts(const lcTrainTrackConnectionType& ConnectionType) const +std::vector lcPiecesLibrary::GetVisibleTrainTrackParts(const lcTrainTrackConnectionType& ConnectionType) const { std::vector Parts; @@ -1816,7 +1816,7 @@ std::vector lcPiecesLibrary::GetTrainTrackParts(const lcTrainTrackCo { lcTrainTrackInfo* TrainTrackInfo = Info->GetTrainTrackInfo(); - if (TrainTrackInfo && TrainTrackInfo->CanConnectTo(ConnectionType)) + if (TrainTrackInfo && TrainTrackInfo->IsVisible() && TrainTrackInfo->CanConnectTo(ConnectionType)) Parts.emplace_back(Info); } diff --git a/common/lc_library.h b/common/lc_library.h index 8180070e..b569c98f 100644 --- a/common/lc_library.h +++ b/common/lc_library.h @@ -157,7 +157,7 @@ public: void GetCategoryEntries(int CategoryIndex, bool GroupPieces, std::vector& SinglePieces, std::vector& GroupedPieces); void GetCategoryEntries(const char* CategoryKeywords, bool GroupPieces, std::vector& SinglePieces, std::vector& GroupedPieces); void GetParts(std::vector& Parts) const; - std::vector GetTrainTrackParts(const lcTrainTrackConnectionType& ConnectionType) const; + std::vector GetVisibleTrainTrackParts(const lcTrainTrackConnectionType& ConnectionType) const; std::vector GetPartsFromSet(const std::vector& PartIds) const; std::string GetPartId(const PieceInfo* Info) const; diff --git a/common/lc_traintrack.cpp b/common/lc_traintrack.cpp index a9afac5b..94d2eb4d 100644 --- a/common/lc_traintrack.cpp +++ b/common/lc_traintrack.cpp @@ -6,8 +6,8 @@ #include "lc_application.h" // 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 +// add the rest of the 12v tracks, look into 4.5v +// hide some of the 12v tracks to avoid bloat // 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 @@ -22,56 +22,71 @@ void lcTrainTrackInfo::Initialize(lcPiecesLibrary* Library) if (!ConfigFile.open(QIODevice::ReadOnly)) return; - QJsonDocument Document = QJsonDocument::fromJson(ConfigFile.readAll()); + QJsonParseError Error; + QJsonDocument Document = QJsonDocument::fromJson(ConfigFile.readAll(), &Error); + + if (Error.error != QJsonParseError::NoError) + { + qDebug() << Error.errorString(); + } + QJsonObject Root = Document.object(); if (Root["Version"].toInt() != 1) return; - QJsonObject JsonPieces = Root["Pieces"].toObject(); + QJsonArray JsonPieces = Root["Pieces"].toArray(); - for (QJsonObject::const_iterator PiecesIt = JsonPieces.constBegin(); PiecesIt != JsonPieces.constEnd(); ++PiecesIt) + for (QJsonArray::const_iterator PiecesIt = JsonPieces.constBegin(); PiecesIt != JsonPieces.constEnd(); ++PiecesIt) { QJsonObject JsonPiece = PiecesIt->toObject(); - QJsonArray JsonParts = JsonPiece["Parts"].toArray(); - for (QJsonArray::const_iterator PartsIt = JsonParts.constBegin(); PartsIt != JsonParts.constEnd(); ++PartsIt) + QJsonArray JsonConnections = JsonPiece["Connections"].toArray(); + std::vector Connections; + + for (QJsonArray::const_iterator ConnectionIt = JsonConnections.constBegin(); ConnectionIt != JsonConnections.constEnd(); ++ConnectionIt) { - PieceInfo* Info = Library->FindPiece(PartsIt->toString().toLatin1(), nullptr, false, false); + QJsonObject JsonConnection = ConnectionIt->toObject(); - if (!Info) - continue; + QJsonArray JsonPosition = JsonConnection["Position"].toArray(); + lcVector3 Position(JsonPosition[0].toDouble(), JsonPosition[1].toDouble(), JsonPosition[2].toDouble()); - lcTrainTrackInfo* TrainTrackInfo = new lcTrainTrackInfo(); + float Rotation = JsonConnection["Rotation"].toDouble() * LC_DTOR; + QString ConnectionGroup = JsonConnection["Type"].toString(); + lcTrainTrackConnectionSleeper ConnectionSleeper = lcTrainTrackConnectionSleeper::None; - Info->SetTrainTrackInfo(TrainTrackInfo); - - QJsonArray JsonConnections = JsonPiece["Connections"].toArray(); - - for (QJsonArray::const_iterator ConnectionIt = JsonConnections.constBegin(); ConnectionIt != JsonConnections.constEnd(); ++ConnectionIt) + if (ConnectionGroup.startsWith('+')) { - QJsonObject JsonConnection = ConnectionIt->toObject(); - - QJsonArray JsonPosition = JsonConnection["Position"].toArray(); - lcVector3 Position(JsonPosition[0].toDouble(), JsonPosition[1].toDouble(), JsonPosition[2].toDouble()); - - float Rotation = JsonConnection["Rotation"].toDouble() * LC_DTOR; - QString ConnectionGroup = JsonConnection["Type"].toString(); - lcTrainTrackConnectionSleeper ConnectionSleeper = lcTrainTrackConnectionSleeper::None; - - if (ConnectionGroup.startsWith('+')) - { - ConnectionSleeper = lcTrainTrackConnectionSleeper::NeedsSleeper; - ConnectionGroup = ConnectionGroup.mid(1); - } - else if (ConnectionGroup.startsWith('-')) - { - ConnectionSleeper = lcTrainTrackConnectionSleeper::HasSleeper; - ConnectionGroup = ConnectionGroup.mid(1); - } - - TrainTrackInfo->AddConnection(lcMatrix44(lcMatrix33RotationZ(Rotation), Position), { qHash(ConnectionGroup), ConnectionSleeper } ); + ConnectionSleeper = lcTrainTrackConnectionSleeper::NeedsSleeper; + ConnectionGroup = ConnectionGroup.mid(1); } + else if (ConnectionGroup.startsWith('-')) + { + ConnectionSleeper = lcTrainTrackConnectionSleeper::HasSleeper; + ConnectionGroup = ConnectionGroup.mid(1); + } + + Connections.emplace_back(lcTrainTrackConnection{ lcMatrix44(lcMatrix33RotationZ(Rotation), Position), { qHash(ConnectionGroup), ConnectionSleeper } }); + } + + QJsonArray JsonIDs = JsonPiece["IDs"].toArray(); + + for (QJsonArray::const_iterator IDIt = JsonIDs.constBegin(); IDIt != JsonIDs.constEnd(); ++IDIt) + { + PieceInfo* Info = Library->FindPiece(IDIt->toString().toLatin1(), nullptr, false, false); + + if (Info) + Info->SetTrainTrackInfo(new lcTrainTrackInfo(Connections, true)); + } + + JsonIDs = JsonPiece["HiddenIDs"].toArray(); + + for (QJsonArray::const_iterator IDIt = JsonIDs.constBegin(); IDIt != JsonIDs.constEnd(); ++IDIt) + { + PieceInfo* Info = Library->FindPiece(IDIt->toString().toLatin1(), nullptr, false, false); + + if (Info) + Info->SetTrainTrackInfo(new lcTrainTrackInfo(Connections, false)); } } diff --git a/common/lc_traintrack.h b/common/lc_traintrack.h index cb58c41d..8b5fed94 100644 --- a/common/lc_traintrack.h +++ b/common/lc_traintrack.h @@ -35,7 +35,15 @@ struct lcTrainTrackInsert class lcTrainTrackInfo { public: - lcTrainTrackInfo() = default; + lcTrainTrackInfo(const std::vector &Connections, bool Visible) + : mConnections(Connections), mVisible(Visible) + { + } + + bool IsVisible() const + { + return mVisible; + } static void Initialize(lcPiecesLibrary* Library); static std::vector GetPieceInsertTransforms(lcPiece* CurrentPiece, PieceInfo* Info, quint32 Section); @@ -43,11 +51,6 @@ public: static std::optional CalculateTransformToConnection(const lcMatrix44& ConnectionTransform, PieceInfo* Info, quint32 ConnectionIndex); static int GetPieceConnectionIndex(const lcPiece* Piece1, int ConnectionIndex1, const lcPiece* Piece2); - void AddConnection(const lcMatrix44 &Transform, const lcTrainTrackConnectionType& Type) - { - mConnections.emplace_back(lcTrainTrackConnection{Transform, Type}); - } - const std::vector& GetConnections() const { return mConnections; @@ -84,5 +87,7 @@ protected: } std::vector mConnections; + bool mVisible = false; + static std::map mSleepers; }; diff --git a/qt/lc_qutils.cpp b/qt/lc_qutils.cpp index cc24618e..971e15f1 100644 --- a/qt/lc_qutils.cpp +++ b/qt/lc_qutils.cpp @@ -240,7 +240,7 @@ lcTrainTrackPickerPopup::lcTrainTrackPickerPopup(QWidget* Parent, const lcTrainT mPartSelectionListView->setMinimumWidth(450); mPartSelectionListView->setDragEnabled(false); - std::vector Parts = lcGetPiecesLibrary()->GetTrainTrackParts(ConnectionType); + std::vector Parts = lcGetPiecesLibrary()->GetVisibleTrainTrackParts(ConnectionType); mPartSelectionListView->SetCustomParts(Parts); diff --git a/resources/traintrack.json b/resources/traintrack.json index 340fa1b8..de704733 100644 --- a/resources/traintrack.json +++ b/resources/traintrack.json @@ -1,10 +1,11 @@ { "Version": 1, "Pieces": - { - "Train Track 9V Point Right": + [ { - "Parts": [ "2859c03.dat", "2859c04.dat" ], + "Description": "Train Track 9V Point Right", + "IDs": [ "2859c03.dat", "2859c04.dat" ], + "HiddenIDs": [ "2859c01.dat", "2859c02.dat" ], "Connections": [ { @@ -24,9 +25,10 @@ } ] }, - "Train Track 9V Point Left": { - "Parts": [ "2861c03.dat", "2861c04.dat" ], + "Description": "Train Track 9V Point Left", + "IDs": [ "2861c03.dat", "2861c04.dat" ], + "HiddenIDs": [ "2861c01.dat", "2861c02.dat" ], "Connections": [ { @@ -46,9 +48,9 @@ } ] }, - "Train Track 9V Crossing": { - "Parts": [ "32087.dat" ], + "Description": "Train Track 9V Crossing", + "IDs": [ "32087.dat" ], "Connections": [ { @@ -73,9 +75,9 @@ } ] }, - "Train Track 9V Straight": { - "Parts": [ "74746.dat" ], + "Description": "Train Track 9V Straight", + "IDs": [ "74746.dat" ], "Connections": [ { @@ -90,9 +92,9 @@ } ] }, - "Train Track 9V Curved": { - "Parts": [ "74747.dat" ], + "Description": "Train Track 9V Curved", + "IDs": [ "74747.dat" ], "Connections": [ { @@ -107,9 +109,26 @@ } ] }, - "Train Track 12V Slotted Curved": { - "Parts": [ "3241ac01.dat" ], + "Description": "Train Track 9V Curved (4 Segments)", + "HiddenIDs": [ "74747c04.dat" ], + "Connections": + [ + { + "Position": [ 0.0, 0.0, 0.0 ], + "Rotation": 180, + "Type": "9V" + }, + { + "Position": [ 800, -800, 0.0 ], + "Rotation": -90, + "Type": "9V" + } + ] + }, + { + "Description": "Train Track 12V Slotted Curved", + "IDs": [ "3241ac01.dat" ], "Connections": [ { @@ -124,9 +143,9 @@ } ] }, - "Train Track Sleeper Plate 2 x 8": { - "Parts": [ "4166a.dat", "4166b.dat" ], + "Description": "Train Track Sleeper Plate 2 x 8", + "IDs": [ "4166a.dat", "4166b.dat" ], "Connections": [ { @@ -141,9 +160,9 @@ } ] }, - "Train Track 12V Slotted Straight With End Sleepers": { - "Parts": [ "3240ac01.dat", "3240bc01.dat" ], + "Description": "Train Track 12V Slotted Straight With End Sleepers", + "IDs": [ "3240ac01.dat", "3240bc01.dat" ], "Connections": [ { @@ -158,9 +177,9 @@ } ] }, - "Train Track 12V Slotted Straight Without End Sleepers": { - "Parts": [ "3240ac02.dat", "3240bc02.dat" ], + "Description": "Train Track 12V Slotted Straight Without End Sleepers", + "IDs": [ "3240ac02.dat", "3240bc02.dat", "862ac01.dat" ], "Connections": [ { @@ -175,9 +194,9 @@ } ] }, - "Train Track 12V Slotted Straight With Signal": { - "Parts": [ "862ac02.dat" ], + "Description": "Train Track 12V Slotted Straight With Signal", + "IDs": [ "862ac02.dat" ], "Connections": [ { @@ -192,9 +211,9 @@ } ] }, - "Train Track 12V Slotted Decoupler": { - "Parts": [ "u9230c01.dat", "u9230c02.dat" ], + "Description": "Train Track 12V Slotted Decoupler", + "IDs": [ "u9230c01.dat", "u9230c02.dat" ], "Connections": [ { @@ -209,9 +228,9 @@ } ] }, - "Train Track 12V Slotted Point Left": { - "Parts": [ "73697ac01.dat", "73697ac02.dat", "73697ac03.dat", "73697ac04.dat" ], + "Description": "Train Track 12V Slotted Point Left", + "IDs": [ "73697ac01.dat", "73697ac02.dat", "73697ac03.dat", "73697ac04.dat" ], "Connections": [ { @@ -231,9 +250,9 @@ } ] }, - "Train Track 12V Slotted Point Right": { - "Parts": [ "73696ac01.dat", "73696ac02.dat", "73696ac03.dat", "73696ac04.dat" ], + "Description": "Train Track 12V Slotted Point Right", + "IDs": [ "73696ac01.dat", "73696ac02.dat", "73696ac03.dat", "73696ac04.dat" ], "Connections": [ { @@ -253,9 +272,9 @@ } ] }, - "Train Track 12V Slotted Crossing": { - "Parts": [ "73698.dat" ], + "Description": "Train Track 12V Slotted Crossing", + "IDs": [ "73698.dat" ], "Connections": [ { @@ -280,9 +299,9 @@ } ] }, - "Train Track 4.5V Slotted Straight Without End Sleepers": { - "Parts": [ "3228bc02.dat" ], + "Description": "Train Track 4.5V Slotted Straight Without End Sleepers", + "IDs": [ "3228bc02.dat" ], "Connections": [ { @@ -297,7 +316,7 @@ } ] } - }, + ], "Sleepers": { "12V": "4166a.dat"