Remove temporary pieces from the library after unloading a project.

This commit is contained in:
leo 2015-01-07 16:52:42 +00:00
parent 16415b136a
commit 06496f1cf0
10 changed files with 98 additions and 37 deletions

View file

@ -77,6 +77,7 @@ void lcApplication::SetProject(Project* Project)
for (int ViewIdx = 0; ViewIdx < Views.GetSize(); ViewIdx++)
Views[ViewIdx]->SetModel(lcGetActiveModel());
lcGetPiecesLibrary()->RemoveTemporaryPieces();
lcGetActiveModel()->UpdateInterface();
gMainWindow->UpdateAllViews();
}

View file

@ -60,6 +60,24 @@ void lcPiecesLibrary::Unload()
mZipFiles[LC_ZIPFILE_UNOFFICIAL] = NULL;
}
void lcPiecesLibrary::RemoveTemporaryPieces()
{
for (int PieceIdx = mPieces.GetSize() - 1; PieceIdx >= 0; PieceIdx--)
{
PieceInfo* Info = mPieces[PieceIdx];
if (!Info->IsTemporary())
break;
if (!Info->IsLoaded())
{
mPieces.RemoveIndex(PieceIdx);
delete Info;
}
}
}
PieceInfo* lcPiecesLibrary::FindPiece(const char* PieceName, bool CreatePlaceholder)
{
for (int PieceIdx = 0; PieceIdx < mPieces.GetSize(); PieceIdx++)

View file

@ -118,6 +118,7 @@ public:
bool Load(const char* LibraryPath, const char* CachePath);
void Unload();
void RemoveTemporaryPieces();
PieceInfo* FindPiece(const char* PieceName, bool CreatePlaceholder);
bool LoadPiece(PieceInfo* Info);

View file

@ -34,6 +34,9 @@ lcMainWindow::lcMainWindow()
lcMainWindow::~lcMainWindow()
{
delete mPreviewWidget;
mPreviewWidget = NULL;
for (int FileIdx = 0; FileIdx < LC_MAX_RECENT_FILES; FileIdx++)
lcSetProfileString((LC_PROFILE_KEY)(LC_PROFILE_RECENT_FILE1 + FileIdx), mRecentFiles[FileIdx]);

View file

@ -154,6 +154,15 @@ lcModel::lcModel(const QString& Name)
lcModel::~lcModel()
{
if (mPieceInfo)
{
if (gMainWindow->mPreviewWidget->GetCurrentPiece() == mPieceInfo)
gMainWindow->mPreviewWidget->SetCurrentPiece(NULL);
mPieceInfo->SetModel(NULL);
mPieceInfo->Release();
}
DeleteModel();
DeleteHistory();
}
@ -205,6 +214,7 @@ void lcModel::CreatePieceInfo()
{
QString PartID = mProperties.mName.toUpper();
mPieceInfo = lcGetPiecesLibrary()->FindPiece(PartID.toLatin1().constData(), true);
mPieceInfo->AddRef();
mPieceInfo->SetModel(this);
}

View file

@ -30,16 +30,13 @@ PieceInfo::~PieceInfo()
QString PieceInfo::GetSaveID() const
{
if (mFlags & LC_PIECE_MODEL)
return mModel->GetProperties().mName;
return QString::fromLatin1(m_strName);
return QString::fromLatin1(m_strName) + QLatin1String(".DAT");
}
void PieceInfo::SetModel(lcModel* Model)
{
m_strName[0] = 0;
m_strDescription[0] = 0;
mFlags = LC_PIECE_MODEL;
mModel = Model;
@ -72,7 +69,9 @@ void PieceInfo::CreatePlaceholder(const char* Name)
void PieceInfo::Load()
{
if (mFlags & LC_PIECE_PLACEHOLDER)
if (mFlags & LC_PIECE_MODEL)
return;
else if (mFlags & LC_PIECE_PLACEHOLDER)
{
mMesh = new lcMesh();
mMesh->CreateBox();
@ -105,6 +104,8 @@ void PieceInfo::Unload()
delete mMesh;
mMesh = NULL;
}
mModel = NULL;
}
bool PieceInfo::MinIntersectDist(const lcMatrix44& WorldMatrix, const lcVector3& WorldStart, const lcVector3& WorldEnd, float& MinDistance) const

View file

@ -54,6 +54,16 @@ public:
return mRefCount;
}
bool IsLoaded() const
{
return mRefCount != 0;
}
bool IsTemporary() const
{
return (mFlags & (LC_PIECE_PLACEHOLDER | LC_PIECE_MODEL)) != 0;
}
void SetZipFile(int ZipFileType, int ZipFileIndex)
{
mZipFileType = ZipFileType;

View file

@ -19,6 +19,8 @@ PiecePreview::PiecePreview()
PiecePreview::~PiecePreview()
{
if (m_PieceInfo)
m_PieceInfo->Release();
}
void PiecePreview::OnDraw()

View file

@ -183,6 +183,7 @@ void Project::ShowModelListDialog()
mModels = NewModels;
lcGetPiecesLibrary()->RemoveTemporaryPieces();
SetActiveModel(Dialog.mActiveModel);
gMainWindow->UpdateTitle();
}

View file

@ -357,14 +357,19 @@ void View::OnDraw()
if (DrawInterface && mTrackTool == LC_TRACKTOOL_INSERT)
{
lcVector3 Position;
lcVector4 Rotation;
GetPieceInsertPosition(Position, Rotation);
PieceInfo* Info = gMainWindow->mPreviewWidget->GetCurrentPiece();
lcMatrix44 WorldMatrix = lcMatrix44FromAxisAngle(lcVector3(Rotation[0], Rotation[1], Rotation[2]), Rotation[3] * LC_DTOR);
WorldMatrix.SetTranslation(Position);
if (Info)
{
lcVector3 Position;
lcVector4 Rotation;
GetPieceInsertPosition(Position, Rotation);
gMainWindow->mPreviewWidget->GetCurrentPiece()->AddRenderMeshes(Scene, WorldMatrix, gMainWindow->mColorIndex, true, true);
lcMatrix44 WorldMatrix = lcMatrix44FromAxisAngle(lcVector3(Rotation[0], Rotation[1], Rotation[2]), Rotation[3] * LC_DTOR);
WorldMatrix.SetTranslation(Position);
Info->AddRenderMeshes(Scene, WorldMatrix, gMainWindow->mColorIndex, true, true);
}
}
mContext->SetDefaultState();
@ -1099,39 +1104,43 @@ void View::DrawGrid()
if (mTrackTool == LC_TRACKTOOL_INSERT)
{
lcVector3 Position;
lcVector4 Rotation;
GetPieceInsertPosition(Position, Rotation);
PieceInfo* CurPiece = gMainWindow->mPreviewWidget->GetCurrentPiece();
lcVector3 Points[8] =
if (CurPiece)
{
lcVector3(CurPiece->m_fDimensions[0], CurPiece->m_fDimensions[1], CurPiece->m_fDimensions[5]),
lcVector3(CurPiece->m_fDimensions[3], CurPiece->m_fDimensions[1], CurPiece->m_fDimensions[5]),
lcVector3(CurPiece->m_fDimensions[0], CurPiece->m_fDimensions[1], CurPiece->m_fDimensions[2]),
lcVector3(CurPiece->m_fDimensions[3], CurPiece->m_fDimensions[4], CurPiece->m_fDimensions[5]),
lcVector3(CurPiece->m_fDimensions[3], CurPiece->m_fDimensions[4], CurPiece->m_fDimensions[2]),
lcVector3(CurPiece->m_fDimensions[0], CurPiece->m_fDimensions[4], CurPiece->m_fDimensions[2]),
lcVector3(CurPiece->m_fDimensions[0], CurPiece->m_fDimensions[4], CurPiece->m_fDimensions[5]),
lcVector3(CurPiece->m_fDimensions[3], CurPiece->m_fDimensions[1], CurPiece->m_fDimensions[2])
};
lcVector3 Position;
lcVector4 Rotation;
GetPieceInsertPosition(Position, Rotation);
lcMatrix44 ModelWorld = lcMatrix44FromAxisAngle(lcVector3(Rotation[0], Rotation[1], Rotation[2]), Rotation[3] * LC_DTOR);
ModelWorld.SetTranslation(Position);
lcVector3 Points[8] =
{
lcVector3(CurPiece->m_fDimensions[0], CurPiece->m_fDimensions[1], CurPiece->m_fDimensions[5]),
lcVector3(CurPiece->m_fDimensions[3], CurPiece->m_fDimensions[1], CurPiece->m_fDimensions[5]),
lcVector3(CurPiece->m_fDimensions[0], CurPiece->m_fDimensions[1], CurPiece->m_fDimensions[2]),
lcVector3(CurPiece->m_fDimensions[3], CurPiece->m_fDimensions[4], CurPiece->m_fDimensions[5]),
lcVector3(CurPiece->m_fDimensions[3], CurPiece->m_fDimensions[4], CurPiece->m_fDimensions[2]),
lcVector3(CurPiece->m_fDimensions[0], CurPiece->m_fDimensions[4], CurPiece->m_fDimensions[2]),
lcVector3(CurPiece->m_fDimensions[0], CurPiece->m_fDimensions[4], CurPiece->m_fDimensions[5]),
lcVector3(CurPiece->m_fDimensions[3], CurPiece->m_fDimensions[1], CurPiece->m_fDimensions[2])
};
for (int i = 0; i < 8; i++)
{
lcVector3 Point = lcMul31(Points[i], ModelWorld);
lcMatrix44 ModelWorld = lcMatrix44FromAxisAngle(lcVector3(Rotation[0], Rotation[1], Rotation[2]), Rotation[3] * LC_DTOR);
ModelWorld.SetTranslation(Position);
if (Point[0] < BoundingBox[0]) BoundingBox[0] = Point[0];
if (Point[1] < BoundingBox[1]) BoundingBox[1] = Point[1];
if (Point[2] < BoundingBox[2]) BoundingBox[2] = Point[2];
if (Point[0] > BoundingBox[3]) BoundingBox[3] = Point[0];
if (Point[1] > BoundingBox[4]) BoundingBox[4] = Point[1];
if (Point[2] > BoundingBox[5]) BoundingBox[5] = Point[2];
for (int i = 0; i < 8; i++)
{
lcVector3 Point = lcMul31(Points[i], ModelWorld);
if (Point[0] < BoundingBox[0]) BoundingBox[0] = Point[0];
if (Point[1] < BoundingBox[1]) BoundingBox[1] = Point[1];
if (Point[2] < BoundingBox[2]) BoundingBox[2] = Point[2];
if (Point[0] > BoundingBox[3]) BoundingBox[3] = Point[0];
if (Point[1] > BoundingBox[4]) BoundingBox[4] = Point[1];
if (Point[2] > BoundingBox[5]) BoundingBox[5] = Point[2];
}
GridSizeValid = true;
}
GridSizeValid = true;
}
if (GridSizeValid)
@ -1984,6 +1993,11 @@ void View::OnLeftButtonDown()
case LC_TRACKTOOL_INSERT:
{
PieceInfo* CurPiece = gMainWindow->mPreviewWidget->GetCurrentPiece();
if (!CurPiece)
break;
lcVector3 Position;
lcVector4 Rotation;
GetPieceInsertPosition(Position, Rotation);