mirror of
https://github.com/leozide/leocad.git
synced 2026-10-27 06:20:55 +00:00
Replace manual piece-data retain/release bookkeeping with shared copyable asset-reference handles. Pieces, models, selection, minifigs, thumbnails, and stored insertion previews retain their dependencies explicitly, while transient queries continue to borrow pointers. Separate registered lookup and acquisition from asynchronous load requests and explicit readiness waits. Keep loader-held references through work and notification delivery, resetting shared request handles on the UI thread and guarding callback-triggered library teardown. Validated with the Qt 6.10 debug build and 26 offscreen lifecycle checks, including cancellation, stale requests, copy/move operations, model/preview teardown, undo, project merge/export, and loader shutdown.
330 lines
12 KiB
C++
330 lines
12 KiB
C++
#include "lc_global.h"
|
|
#include "lc_traintrack.h"
|
|
#include "lc_library.h"
|
|
#include "lc_piecedata.h"
|
|
#include "piece.h"
|
|
#include "lc_model.h"
|
|
#include "lc_application.h"
|
|
|
|
// todo:
|
|
// when moving existing pieces:
|
|
// need to check for parts intersecting instead of mouse intersection because it's hard to connect tracks
|
|
// look into the snapping that happens when there are no available connections
|
|
// fix comments in UpdateFreeMoveTool
|
|
// hide some of the 12v tracks to avoid bloat
|
|
// auto replace cross when going over a straight section
|
|
// set focus connection after adding
|
|
|
|
std::map<size_t, lcPieceData*> lcTrainTrackInfo::mSleepers;
|
|
|
|
void lcTrainTrackInfo::Initialize(lcPiecesLibrary* Library)
|
|
{
|
|
QFile ConfigFile(QLatin1String(":/resources/traintrack.json"));
|
|
|
|
if (!ConfigFile.open(QIODevice::ReadOnly))
|
|
return;
|
|
|
|
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;
|
|
|
|
QJsonArray JsonPieces = Root["Pieces"].toArray();
|
|
|
|
for (QJsonArray::const_iterator PiecesIt = JsonPieces.constBegin(); PiecesIt != JsonPieces.constEnd(); ++PiecesIt)
|
|
{
|
|
QJsonObject JsonPiece = PiecesIt->toObject();
|
|
|
|
QJsonArray JsonConnections = JsonPiece["Connections"].toArray();
|
|
std::vector<lcTrainTrackConnection> Connections;
|
|
|
|
for (QJsonArray::const_iterator ConnectionIt = JsonConnections.constBegin(); ConnectionIt != JsonConnections.constEnd(); ++ConnectionIt)
|
|
{
|
|
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);
|
|
}
|
|
|
|
Connections.emplace_back(lcTrainTrackConnection{ lcMatrix44(lcMatrix33RotationZ(Rotation), Position), { qHash(ConnectionGroup), ConnectionSleeper } });
|
|
}
|
|
|
|
int Color = JsonPiece["Color"].toInt(16);
|
|
QJsonArray JsonIDs = JsonPiece["IDs"].toArray();
|
|
|
|
for (QJsonArray::const_iterator IDIt = JsonIDs.constBegin(); IDIt != JsonIDs.constEnd(); ++IDIt)
|
|
{
|
|
lcPieceData* PieceData = Library->FindPieceData(IDIt->toString().toLatin1(), nullptr);
|
|
|
|
if (PieceData)
|
|
PieceData->SetTrainTrackInfo(new lcTrainTrackInfo(Connections, Color, true));
|
|
}
|
|
|
|
JsonIDs = JsonPiece["HiddenIDs"].toArray();
|
|
|
|
for (QJsonArray::const_iterator IDIt = JsonIDs.constBegin(); IDIt != JsonIDs.constEnd(); ++IDIt)
|
|
{
|
|
lcPieceData* PieceData = Library->FindPieceData(IDIt->toString().toLatin1(), nullptr);
|
|
|
|
if (PieceData)
|
|
PieceData->SetTrainTrackInfo(new lcTrainTrackInfo(Connections, Color, false));
|
|
}
|
|
}
|
|
|
|
QJsonObject JsonSleepers = Root["Sleepers"].toObject();
|
|
|
|
mSleepers.clear();
|
|
|
|
for (QJsonObject::const_iterator SleepersIt = JsonSleepers.constBegin(); SleepersIt != JsonSleepers.constEnd(); ++SleepersIt)
|
|
{
|
|
size_t Group = qHash(SleepersIt.key());
|
|
QString PartId = SleepersIt->toString();
|
|
|
|
lcPieceData* SleeperPieceData = Library->FindPieceData(PartId.toLatin1(), nullptr);
|
|
|
|
if (SleeperPieceData)
|
|
mSleepers[Group] = SleeperPieceData;
|
|
}
|
|
}
|
|
|
|
int lcTrainTrackInfo::GetPieceConnectionIndex(const lcPiece* Piece1, int ConnectionIndex1, const lcPiece* Piece2)
|
|
{
|
|
const lcTrainTrackInfo* TrainTrackInfo1 = Piece1->mPieceData->GetTrainTrackInfo();
|
|
const lcTrainTrackInfo* TrainTrackInfo2 = Piece2->mPieceData->GetTrainTrackInfo();
|
|
|
|
const std::vector<lcTrainTrackConnection>& Connections2 = TrainTrackInfo2->GetConnections();
|
|
lcMatrix44 Transform1 = lcMul(TrainTrackInfo1->GetConnections()[ConnectionIndex1].Transform, Piece1->mModelWorld);
|
|
|
|
for (int ConnectionIndex2 = 0; ConnectionIndex2 < static_cast<int>(Connections2.size()); ConnectionIndex2++)
|
|
{
|
|
const lcTrainTrackConnection& Connection2 = Connections2[ConnectionIndex2];
|
|
const lcMatrix44 Transform2 = lcMul(Connection2.Transform, Piece2->mModelWorld);
|
|
|
|
if (lcLengthSquared(Transform1.GetTranslation() - Transform2.GetTranslation()) > 0.1f)
|
|
continue;
|
|
|
|
float Dot = lcDot3(Transform1[0], Transform2[0]);
|
|
|
|
if (Dot < -0.99f && Dot > -1.01f)
|
|
return ConnectionIndex2;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
std::vector<lcInsertPieceInfo> lcTrainTrackInfo::GetInsertPieceInfo(lcPiece* CurrentPiece, lcPieceData* PieceData, lcPiece* MovingPiece, int ColorIndex, quint32 PreferredSection, bool AllowNewPieces, std::optional<lcVector3> ClosestPoint)
|
|
{
|
|
std::vector<lcInsertPieceInfo> Pieces;
|
|
const lcTrainTrackInfo* CurrentTrackInfo = CurrentPiece->mPieceData->GetTrainTrackInfo();
|
|
|
|
if (!CurrentTrackInfo || CurrentTrackInfo->GetConnections().empty() || (MovingPiece && PieceData != MovingPiece->mPieceData))
|
|
return Pieces;
|
|
|
|
quint32 ConnectionIndex = 0;
|
|
|
|
if (ClosestPoint)
|
|
{
|
|
lcMatrix44 InverseMatrix = lcMatrix44AffineInverse(CurrentPiece->mModelWorld);
|
|
lcVector3 LocalClosestPoint = lcMul31(ClosestPoint.value(), InverseMatrix);
|
|
float BestDistance = FLT_MAX;
|
|
|
|
for (quint32 CheckIndex = 0; CheckIndex < CurrentTrackInfo->GetConnections().size(); CheckIndex++)
|
|
{
|
|
if (CurrentPiece->IsTrainTrackConnected(CheckIndex))
|
|
continue;
|
|
|
|
if (MovingPiece)
|
|
{
|
|
const std::vector<lcTrainTrackConnection>& MovingConnections = PieceData->GetTrainTrackInfo()->GetConnections();
|
|
bool CanConnect = false;
|
|
|
|
for (quint32 MovingConnectionIndex = 0; MovingConnectionIndex < MovingConnections.size(); MovingConnectionIndex++)
|
|
{
|
|
if (!MovingPiece->IsTrainTrackConnected(MovingConnectionIndex) && AreConnectionsCompatible(CurrentTrackInfo->GetConnections()[CheckIndex].Type, MovingConnections[MovingConnectionIndex].Type, AllowNewPieces))
|
|
{
|
|
CanConnect = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!CanConnect)
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
if (!PieceData->GetTrainTrackInfo()->CanConnectTo(CurrentTrackInfo->GetConnections()[CheckIndex].Type, AllowNewPieces))
|
|
continue;
|
|
}
|
|
|
|
float Distance = (CurrentTrackInfo->GetConnections()[CheckIndex].Transform.GetTranslation() - LocalClosestPoint).LengthSquared();
|
|
|
|
if (Distance < BestDistance)
|
|
{
|
|
ConnectionIndex = CheckIndex;
|
|
BestDistance = Distance;
|
|
}
|
|
}
|
|
|
|
if (BestDistance == FLT_MAX)
|
|
return Pieces;
|
|
}
|
|
else
|
|
{
|
|
if (PreferredSection != LC_PIECE_SECTION_INVALID && PreferredSection >= LC_PIECE_SECTION_TRAIN_TRACK_CONNECTION_FIRST)
|
|
ConnectionIndex = PreferredSection - LC_PIECE_SECTION_TRAIN_TRACK_CONNECTION_FIRST;
|
|
|
|
quint32 CheckIndex;
|
|
|
|
for (CheckIndex = 0; CheckIndex < CurrentTrackInfo->GetConnections().size(); CheckIndex++)
|
|
if (!CurrentPiece->IsTrainTrackConnected((ConnectionIndex + CheckIndex) % CurrentTrackInfo->GetConnections().size()))
|
|
break;
|
|
|
|
if (CheckIndex == CurrentTrackInfo->GetConnections().size())
|
|
return Pieces;
|
|
|
|
ConnectionIndex = (ConnectionIndex + CheckIndex) % CurrentTrackInfo->GetConnections().size();
|
|
}
|
|
|
|
const lcTrainTrackConnectionType& CurrentConnectionType = CurrentTrackInfo->GetConnections()[ConnectionIndex].Type;
|
|
const std::vector<lcTrainTrackConnection>& NewConnections = PieceData->GetTrainTrackInfo()->GetConnections();
|
|
quint32 NewConnectionIndex;// = ConnectionIndex ? 0 : 1;
|
|
|
|
for (NewConnectionIndex = 0; NewConnectionIndex < NewConnections.size(); NewConnectionIndex++)
|
|
{
|
|
if (MovingPiece)
|
|
{
|
|
if (MovingPiece->IsTrainTrackConnected(NewConnectionIndex))
|
|
continue;
|
|
|
|
const std::vector<lcTrainTrackConnection>& MovingConnections = PieceData->GetTrainTrackInfo()->GetConnections();
|
|
|
|
if (AreConnectionsCompatible(CurrentTrackInfo->GetConnections()[ConnectionIndex].Type, MovingConnections[NewConnectionIndex].Type, AllowNewPieces))
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
if (AreConnectionsCompatible(CurrentConnectionType, NewConnections[NewConnectionIndex].Type, AllowNewPieces))
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (NewConnectionIndex == NewConnections.size())
|
|
return Pieces;
|
|
|
|
if (CurrentConnectionType.Sleeper == lcTrainTrackConnectionSleeper::NeedsSleeper && NewConnections[NewConnectionIndex].Type.Sleeper == lcTrainTrackConnectionSleeper::NeedsSleeper)
|
|
{
|
|
std::map<size_t, lcPieceData*>::const_iterator SleeperIt = mSleepers.find(CurrentConnectionType.Group);
|
|
|
|
if (SleeperIt == mSleepers.end())
|
|
return Pieces;
|
|
|
|
lcPieceData* SleeperPieceData = SleeperIt->second;
|
|
|
|
if (!SleeperPieceData)
|
|
return Pieces;
|
|
|
|
std::optional<lcMatrix44> SleeperTransform = GetConnectionTransform(CurrentPiece->mPieceData.Get(), CurrentPiece->mModelWorld, ConnectionIndex, SleeperPieceData, 0);
|
|
|
|
if (!SleeperTransform)
|
|
return Pieces;
|
|
|
|
std::optional<lcMatrix44> Transform = GetConnectionTransform(SleeperPieceData, SleeperTransform.value(), 1, PieceData, NewConnectionIndex);
|
|
|
|
if (Transform)
|
|
{
|
|
Pieces.emplace_back(lcInsertPieceInfo{ SleeperPieceData, SleeperTransform.value(), lcGetColorIndex(SleeperPieceData->GetTrainTrackInfo()->GetColorCode()) });
|
|
Pieces.emplace_back(lcInsertPieceInfo{ PieceData, Transform.value(), ColorIndex });
|
|
}
|
|
}
|
|
else
|
|
{
|
|
std::optional<lcMatrix44> Transform = GetConnectionTransform(CurrentPiece->mPieceData.Get(), CurrentPiece->mModelWorld, ConnectionIndex, PieceData, NewConnectionIndex);
|
|
|
|
if (Transform)
|
|
Pieces.emplace_back(lcInsertPieceInfo{ PieceData, Transform.value(), ColorIndex });
|
|
}
|
|
|
|
return Pieces;
|
|
}
|
|
|
|
std::optional<lcMatrix44> lcTrainTrackInfo::GetConnectionTransform(lcPieceData* CurrentPieceData, const lcMatrix44& CurrentTransform, quint32 CurrentConnectionIndex, lcPieceData* PieceData, quint32 NewConnectionIndex)
|
|
{
|
|
if (!CurrentPieceData || !PieceData)
|
|
return std::nullopt;
|
|
|
|
const lcTrainTrackInfo* CurrentTrackInfo = CurrentPieceData->GetTrainTrackInfo();
|
|
|
|
if (!CurrentTrackInfo || CurrentTrackInfo->GetConnections().empty())
|
|
return std::nullopt;
|
|
|
|
if (CurrentConnectionIndex >= CurrentTrackInfo->GetConnections().size())
|
|
return std::nullopt;
|
|
|
|
lcTrainTrackInfo* NewTrackInfo = PieceData->GetTrainTrackInfo();
|
|
|
|
if (!NewTrackInfo || NewConnectionIndex >= NewTrackInfo->mConnections.size())
|
|
return std::nullopt;
|
|
|
|
lcMatrix44 Transform;
|
|
|
|
// if (TrainTrackType != lcTrainTrackType::Left)
|
|
// Transform = NewTrackInfo->mConnections[NewConnectionIndex].Transform;
|
|
// else
|
|
// {
|
|
Transform = lcMatrix44AffineInverse(NewTrackInfo->mConnections[NewConnectionIndex].Transform);
|
|
Transform = lcMul(Transform, lcMatrix44RotationZ(LC_PI));
|
|
// }
|
|
|
|
Transform = lcMul(Transform, CurrentTrackInfo->GetConnections()[CurrentConnectionIndex].Transform);
|
|
Transform = lcMul(Transform, CurrentTransform);
|
|
|
|
return Transform;
|
|
}
|
|
|
|
std::optional<lcMatrix44> lcTrainTrackInfo::CalculateTransformToConnection(const lcMatrix44& ConnectionTransform, lcPieceData* PieceData, quint32 ConnectionIndex)
|
|
{
|
|
lcTrainTrackInfo* TrackInfo = PieceData->GetTrainTrackInfo();
|
|
|
|
if (!TrackInfo || ConnectionIndex >= TrackInfo->mConnections.size())
|
|
return std::nullopt;
|
|
|
|
lcMatrix44 Transform;
|
|
|
|
// if (TrainTrackType != lcTrainTrackType::Left)
|
|
// Transform = NewTrackInfo->mConnections[NewConnectionIndex].Transform;
|
|
// else
|
|
// {
|
|
Transform = lcMatrix44AffineInverse(TrackInfo->mConnections[ConnectionIndex].Transform);
|
|
// Transform = lcMul(Transform, lcMatrix44RotationZ(LC_PI));
|
|
// }
|
|
|
|
Transform = lcMul(Transform, ConnectionTransform);
|
|
|
|
// Transform = lcMul(Transform, CurrentTrackInfo->GetConnections()[CurrentConnectionIndex].Transform);
|
|
// Transform = lcMul(Transform, CurrentPiece->mModelWorld);
|
|
|
|
return Transform;
|
|
}
|