leocad/common/preview.cpp

175 lines
3.3 KiB
C++
Raw Normal View History

#include "lc_global.h"
2011-09-07 23:06:51 +02:00
#include "preview.h"
#include "project.h"
#include "lc_model.h"
2011-09-07 23:06:51 +02:00
#include "pieceinf.h"
#include "system.h"
#include "lc_application.h"
2013-08-16 03:25:51 +02:00
#include "lc_mainwindow.h"
#include "lc_library.h"
2011-09-07 23:06:51 +02:00
2013-08-09 06:57:18 +02:00
PiecePreview::PiecePreview()
2011-09-07 23:06:51 +02:00
{
m_PieceInfo = NULL;
m_RotateX = 60.0f;
m_RotateZ = 225.0f;
m_Distance = 10.0f;
m_AutoZoom = true;
m_Tracking = LC_TRACK_NONE;
}
PiecePreview::~PiecePreview()
{
if (m_PieceInfo)
m_PieceInfo->Release();
2011-09-07 23:06:51 +02:00
}
void PiecePreview::OnDraw()
{
if (m_PieceInfo == NULL)
return;
2015-01-12 05:49:30 +01:00
mContext->SetDefaultState();
2011-09-07 23:06:51 +02:00
2013-08-09 06:57:18 +02:00
float aspect = (float)mWidth/(float)mHeight;
mContext->SetViewport(0, 0, mWidth, mHeight);
2011-09-07 23:06:51 +02:00
lcGetActiveModel()->DrawBackground(mContext);
2012-06-07 00:34:38 +02:00
lcVector3 Eye(0.0f, 0.0f, 1.0f);
2011-09-07 23:06:51 +02:00
2012-06-07 00:34:38 +02:00
Eye = lcMul30(Eye, lcMatrix44RotationX(-m_RotateX * LC_DTOR));
Eye = lcMul30(Eye, lcMatrix44RotationZ(-m_RotateZ * LC_DTOR));
2011-09-07 23:06:51 +02:00
lcMatrix44 ProjectionMatrix = lcMatrix44Perspective(30.0f, aspect, 1.0f, 2500.0f);
lcMatrix44 ViewMatrix;
2011-09-07 23:06:51 +02:00
if (m_AutoZoom)
{
Eye = Eye * 100.0f;
m_PieceInfo->ZoomExtents(ProjectionMatrix, ViewMatrix, Eye);
2011-09-07 23:06:51 +02:00
// Update the new camera distance.
2012-06-07 00:34:38 +02:00
lcVector3 d = Eye - m_PieceInfo->GetCenter();
2011-09-07 23:06:51 +02:00
m_Distance = d.Length();
}
else
{
ViewMatrix = lcMatrix44LookAt(Eye * m_Distance, m_PieceInfo->GetCenter(), lcVector3(0, 0, 1));
2011-09-07 23:06:51 +02:00
}
mContext->SetProjectionMatrix(ProjectionMatrix);
lcScene Scene;
2015-02-08 19:54:51 +01:00
Scene.Begin(ViewMatrix);
m_PieceInfo->AddRenderMeshes(Scene, lcMatrix44Identity(), gMainWindow->mColorIndex, false, false);
2015-02-08 19:54:51 +01:00
Scene.End();
2015-02-08 19:54:51 +01:00
mContext->DrawOpaqueMeshes(ViewMatrix, Scene.mOpaqueMeshes);
mContext->DrawTranslucentMeshes(ViewMatrix, Scene.mTranslucentMeshes);
mContext->UnbindMesh(); // context remove
2011-09-07 23:06:51 +02:00
}
void PiecePreview::SetCurrentPiece(PieceInfo *pInfo)
{
MakeCurrent();
if (m_PieceInfo != NULL)
2012-10-12 01:55:55 +02:00
m_PieceInfo->Release();
2011-09-07 23:06:51 +02:00
m_PieceInfo = pInfo;
if (m_PieceInfo != NULL)
{
m_PieceInfo->AddRef();
Redraw();
}
}
void PiecePreview::SetDefaultPiece()
{
lcPiecesLibrary* Library = lcGetPiecesLibrary();
PieceInfo* Info = Library->FindPiece("3005", NULL, false);
if (!Info)
Info = Library->mPieces[0];
if (Info)
SetCurrentPiece(Info);
}
2013-08-09 06:57:18 +02:00
void PiecePreview::OnLeftButtonDown()
2011-09-07 23:06:51 +02:00
{
if (m_Tracking == LC_TRACK_NONE)
{
2013-08-09 06:57:18 +02:00
m_DownX = mInputState.x;
m_DownY = mInputState.y;
2011-09-07 23:06:51 +02:00
m_Tracking = LC_TRACK_LEFT;
}
}
2013-08-09 06:57:18 +02:00
void PiecePreview::OnLeftButtonUp()
2011-09-07 23:06:51 +02:00
{
if (m_Tracking == LC_TRACK_LEFT)
m_Tracking = LC_TRACK_NONE;
}
2013-08-09 06:57:18 +02:00
void PiecePreview::OnLeftButtonDoubleClick()
2011-09-07 23:06:51 +02:00
{
m_AutoZoom = true;
Redraw();
}
2013-08-09 06:57:18 +02:00
void PiecePreview::OnRightButtonDown()
2011-09-07 23:06:51 +02:00
{
if (m_Tracking == LC_TRACK_NONE)
{
2013-08-09 06:57:18 +02:00
m_DownX = mInputState.x;
m_DownY = mInputState.y;
2011-09-07 23:06:51 +02:00
m_Tracking = LC_TRACK_RIGHT;
}
}
2013-08-09 06:57:18 +02:00
void PiecePreview::OnRightButtonUp()
2011-09-07 23:06:51 +02:00
{
if (m_Tracking == LC_TRACK_RIGHT)
m_Tracking = LC_TRACK_NONE;
}
2013-08-09 06:57:18 +02:00
void PiecePreview::OnMouseMove()
2011-09-07 23:06:51 +02:00
{
if (m_Tracking == LC_TRACK_LEFT)
{
// Rotate.
2013-08-09 06:57:18 +02:00
m_RotateZ += mInputState.x - m_DownX;
m_RotateX += mInputState.y - m_DownY;
2011-09-07 23:06:51 +02:00
if (m_RotateX > 179.5f)
m_RotateX = 179.5f;
else if (m_RotateX < 0.5f)
m_RotateX = 0.5f;
2013-08-09 06:57:18 +02:00
m_DownX = mInputState.x;
m_DownY = mInputState.y;
2011-09-07 23:06:51 +02:00
Redraw();
}
else if (m_Tracking == LC_TRACK_RIGHT)
{
// Zoom.
2013-08-09 06:57:18 +02:00
m_Distance += (float)(m_DownY - mInputState.y) * 0.2f;
2011-09-07 23:06:51 +02:00
m_AutoZoom = false;
if (m_Distance < 0.5f)
m_Distance = 0.5f;
2013-08-09 06:57:18 +02:00
m_DownX = mInputState.x;
m_DownY = mInputState.y;
2011-09-07 23:06:51 +02:00
Redraw();
}
}