_commandEntries
|
#include "StdAfx.h"
|
#include "CHGlassDefectMap/GlassDefectMap.h"
|
|
#define SafeRelease(T) if(T) {T->Release(); T = 0; }
|
#define SafeDelete(T) if(T) {delete T; T = 0; }
|
|
|
int GetDirX(int nDir1, int nDir2)
|
{
|
if ( (nDir1==MODT_LeftTop) || (nDir1==MODT_LeftBottom) )
|
{
|
if ( (nDir2==MODT_LeftTop) || (nDir2==MODT_LeftBottom) )
|
{
|
return 1;
|
}
|
return 0;
|
}
|
|
if ( (nDir2==MODT_RightTop) || (nDir2==MODT_RightBottom) )
|
{
|
return 1;
|
}
|
return 0;
|
}
|
|
int GetDirY(int nDir1, int nDir2)
|
{
|
if ( (nDir1==MODT_LeftTop) || (nDir1==MODT_RightTop) )
|
{
|
if ( (nDir2==MODT_LeftTop) || (nDir2==MODT_RightTop) )
|
{
|
return 1;
|
}
|
return 0;
|
}
|
|
if ( (nDir2==MODT_LeftBottom) || (nDir2==MODT_RightBottom) )
|
{
|
return 1;
|
}
|
return 0;
|
}
|
|
|
// CGlassDefectMapWnd
|
|
IMPLEMENT_DYNAMIC(CGlassDefectMap, CWnd)
|
|
CGlassDefectMap::CGlassDefectMap(CWnd* pParentWnd) : m_pParentWnd(pParentWnd)
|
{
|
CoInitialize(0);
|
|
HRESULT hr = S_OK;
|
|
// create wic factory
|
hr = ::CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_pWICFactory));
|
if (hr!=S_OK)
|
{
|
}
|
|
// create d2d factory
|
hr = ::D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pD2DFactory);
|
if (hr!=S_OK)
|
{
|
}
|
|
// create direct write factory
|
hr = ::DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), (IUnknown**)&m_pDWFactroy);
|
if (hr!=S_OK)
|
{
|
}
|
|
m_pLayerDefect = nullptr;
|
m_pLayerCell = nullptr;
|
m_pLayerGlass = nullptr;
|
m_pRenderTargetHwnd = nullptr;
|
|
m_bDrawGlass = TRUE;
|
m_bDrawCell = TRUE;
|
m_bDrawDefect = TRUE;
|
|
m_nMapDrawType = MDT_Auto;
|
m_fMapZoomLevel = 0.9f;
|
m_ptMapPoint = CPoint(0,0);
|
m_nMapBackColor = MakeColor(0,255,255,255);
|
|
m_fMapTranslation = D2D1::SizeF(0.0f, 0.0f);
|
m_fMapScale = D2D1::SizeF(1.0f, 1.0f);
|
m_fMapPoint = D2D1::Point2F(0.0f, 0.0f);
|
|
m_vecDrawData_Cell.push_back(CDrawData_Cell());
|
|
m_matTransform = D2D1::Matrix3x2F::Identity();
|
}
|
|
CGlassDefectMap::~CGlassDefectMap(void)
|
{
|
SafeRelease(m_pLayerDefect);
|
SafeRelease(m_pLayerCell);
|
SafeRelease(m_pLayerGlass);
|
|
SafeRelease(m_pRenderTargetHwnd);
|
SafeRelease(m_pDWFactroy);
|
SafeRelease(m_pD2DFactory);
|
SafeRelease(m_pWICFactory);
|
|
CoUninitialize();
|
}
|
|
BEGIN_MESSAGE_MAP(CGlassDefectMap, CWnd)
|
ON_WM_PAINT()
|
ON_WM_CREATE()
|
ON_WM_LBUTTONDOWN()
|
ON_WM_LBUTTONUP()
|
ON_WM_MOUSEMOVE()
|
END_MESSAGE_MAP()
|
|
void CGlassDefectMap::InitGlassDefectMap()
|
{
|
if (m_pD2DFactory==nullptr || m_pWICFactory==nullptr) return;
|
|
CRect rect;
|
GetClientRect(rect);
|
|
// set size
|
m_uRenderTargetHwndSize = D2D1::SizeU(rect.Width(), rect.Height());
|
m_fRenderTargetHwndSize = D2D1::SizeF(FLOAT(rect.Width()), FLOAT(rect.Height()));
|
m_pxRenderTargetHwndSize = D2D1::SizeU(m_uRenderTargetHwndSize.width * 4, m_uRenderTargetHwndSize.height * 4);
|
m_rtRenderTargetHwndRect = D2D1::RectF( 0.f, 0.f, m_fRenderTargetHwndSize.width, m_fRenderTargetHwndSize.height );
|
|
// create hwnd render target
|
SafeRelease(m_pRenderTargetHwnd);
|
HRESULT hr = m_pD2DFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), ::D2D1::HwndRenderTargetProperties(this->GetSafeHwnd(), m_uRenderTargetHwndSize), &m_pRenderTargetHwnd);
|
if (hr!=S_OK)
|
{
|
return;
|
}
|
|
return;
|
}
|
|
void CGlassDefectMap::DrawGlassLayer(ID2D1RenderTarget *pRenderTarget, const D2D1_RECT_F* pDestRect)
|
{
|
if (pRenderTarget==nullptr) return;
|
|
if (m_pLayerGlass==nullptr) UpdateGlassLayer(pRenderTarget);
|
|
if (m_pLayerGlass==nullptr) return;
|
|
pRenderTarget->DrawBitmap(m_pLayerGlass, pDestRect);
|
}
|
|
void CGlassDefectMap::DrawCellLayer(ID2D1RenderTarget *pRenderTarget, const D2D1_RECT_F* pDestRect)
|
{
|
if (pRenderTarget==nullptr) return;
|
|
if (m_pLayerCell==nullptr) UpdateCellLayer(pRenderTarget);
|
|
if (m_pLayerCell==nullptr) return;
|
|
pRenderTarget->DrawBitmap(m_pLayerCell, pDestRect);
|
}
|
|
void CGlassDefectMap::DrawDefectLayer(ID2D1RenderTarget *pRenderTarget, const D2D1_RECT_F* pDestRect)
|
{
|
if (pRenderTarget==nullptr) return;
|
|
if (m_pLayerDefect==nullptr) UpdateDefectLayer(pRenderTarget);
|
|
if (m_pLayerDefect==nullptr) return;
|
|
pRenderTarget->DrawBitmap(m_pLayerDefect, pDestRect);
|
}
|
|
void CGlassDefectMap::UpdateDrawData(ID2D1RenderTarget *pRenderTarget, const CDrawData* pDrawData, float fScale)
|
{
|
// set rect
|
D2D1_RECT_F rect = D2D1::RectF(pDrawData->fLeft, pDrawData->fTop, pDrawData->fRight, pDrawData->fBottom);
|
|
// create pen
|
ID2D1SolidColorBrush *pPen = NULL;
|
HRESULT hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF(pDrawData->nPenColor, pDrawData->fPenOpacity)), &pPen);
|
if (hr!=S_OK)
|
{
|
return;
|
}
|
|
// create brush
|
ID2D1SolidColorBrush *pBrush = NULL;
|
hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF(pDrawData->nBrushColor, pDrawData->fBrushOpacity)), &pBrush);
|
if (hr!=S_OK)
|
{
|
SafeRelease(pPen);
|
return;
|
}
|
|
pRenderTarget->FillRectangle(rect, pBrush);
|
|
float fPenSize = pDrawData->fPenSize / fScale;
|
pRenderTarget->DrawRectangle(rect, pPen, fPenSize);
|
|
SafeRelease(pPen);
|
SafeRelease(pBrush);
|
}
|
|
|
void CGlassDefectMap::UpdateGlassCorner( ID2D1RenderTarget *pRenderTarget, const CDrawData_Glass* pDataGlass, const D2D1_SIZE_F& fScale)
|
{
|
ID2D1PathGeometry* pPathGeometry = nullptr;
|
HRESULT hr = m_pD2DFactory->CreatePathGeometry(&pPathGeometry);
|
if (hr!=S_OK)
|
{
|
return;
|
}
|
|
ID2D1GeometrySink* pGeometrySink = nullptr;
|
hr = pPathGeometry->Open(&pGeometrySink);
|
if (hr!=S_OK)
|
{
|
SafeRelease(pPathGeometry);
|
return;
|
}
|
|
int nDirX = GetDirX(pDataGlass->nOriginDir, pDataGlass->nCornerDir);
|
int nDirY = GetDirY(pDataGlass->nOriginDir, pDataGlass->nCornerDir);
|
|
float fCornerWidth = pDataGlass->fCornerSize / fabs(fScale.width);
|
float fCornerHeight = pDataGlass->fCornerSize / fabs(fScale.height);
|
|
// Use D2D1_FIGURE_BEGIN_FILLED for filled
|
D2D1_FIGURE_BEGIN fg = D2D1_FIGURE_BEGIN_FILLED;
|
D2D1_FIGURE_END fe = D2D1_FIGURE_END_CLOSED;
|
|
|
switch(pDataGlass->nCornerDir)
|
{
|
case MODT_LeftTop:
|
pGeometrySink->BeginFigure(D2D1::Point2F(0.0f, 0.0f), fg);
|
pGeometrySink->AddLine(D2D1::Point2F(fCornerWidth, 0.0f));
|
pGeometrySink->AddLine(D2D1::Point2F(0.0f, fCornerHeight));
|
break;
|
|
case MODT_RightTop:
|
pGeometrySink->BeginFigure(D2D1::Point2F(pDataGlass->fGlassWidth, 0.0f), fg);
|
pGeometrySink->AddLine(D2D1::Point2F(pDataGlass->fGlassWidth-fCornerWidth, 0.0f));
|
pGeometrySink->AddLine(D2D1::Point2F(pDataGlass->fGlassWidth, fCornerHeight));
|
break;
|
|
case MODT_LeftBottom:
|
pGeometrySink->BeginFigure(D2D1::Point2F(0.0f, pDataGlass->fGlassHeight), fg);
|
pGeometrySink->AddLine(D2D1::Point2F(fCornerWidth, pDataGlass->fGlassHeight));
|
pGeometrySink->AddLine(D2D1::Point2F(0.0f, pDataGlass->fGlassHeight-fCornerHeight));
|
break;
|
|
case MODT_RightBottom:
|
pGeometrySink->BeginFigure(D2D1::Point2F(pDataGlass->fGlassWidth, pDataGlass->fGlassHeight), fg);
|
pGeometrySink->AddLine(D2D1::Point2F(pDataGlass->fGlassWidth-fCornerWidth, pDataGlass->fGlassHeight));
|
pGeometrySink->AddLine(D2D1::Point2F(pDataGlass->fGlassWidth, pDataGlass->fGlassHeight-fCornerHeight));
|
break;
|
|
default:
|
SafeRelease(pPathGeometry);
|
SafeRelease(pGeometrySink);
|
return;
|
}
|
|
pGeometrySink->EndFigure(fe);
|
pGeometrySink->Close();
|
SafeRelease(pGeometrySink);
|
|
ID2D1SolidColorBrush *pBrush = NULL;
|
hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF(pDataGlass->nCornerColor, 1.0f)), &pBrush);
|
if (hr!=S_OK)
|
{
|
SafeRelease(pPathGeometry)
|
return;
|
}
|
|
pRenderTarget->FillGeometry(pPathGeometry, pBrush);
|
|
SafeRelease(pBrush);
|
SafeRelease(pPathGeometry);
|
}
|
|
void CGlassDefectMap::UpdateGlassRuler( ID2D1RenderTarget *pRenderTarget, const CDrawData_Glass* pDataGlass, const D2D1_SIZE_F& fScale)
|
{
|
int nWidthCount = int( (pDataGlass->fGlassWidth / pDataGlass->fRulerWidth) + 0.5f) + 1;
|
int nHeightCount = int( (pDataGlass->fGlassHeight / pDataGlass->fRulerHeight) + 0.5f) + 1;
|
|
ID2D1SolidColorBrush *pPenStart = NULL;
|
HRESULT hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF(MakeColor(0,0,0,0), 1.0f)), &pPenStart);
|
if (hr!=S_OK)
|
{
|
return;
|
}
|
|
ID2D1SolidColorBrush *pPenMiddle = NULL;
|
hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF(pDataGlass->nPenColor, 1.0f)), &pPenMiddle);
|
if (hr!=S_OK)
|
{
|
SafeRelease(pPenStart);
|
return;
|
}
|
|
|
D2D1_POINT_2F pt1, pt2, pt3, pt4;
|
CString str = _T("");
|
|
float fTextWidth = 14.0f / fabs(fScale.width);
|
float fTextHeight = 5.0f / fabs(fScale.height);
|
float fGabWidth = 5.0f / fabs(fScale.width);
|
float fGabHeight = 5.0f / fabs(fScale.height);
|
float fPenWidth = pDataGlass->fRulerStrokeWidth / fabs(fScale.width);
|
|
float fFontSize = 25.f;
|
IDWriteTextFormat *pTextFormat = NULL;
|
hr = m_pDWFactroy->CreateTextFormat(
|
L"Calibri",
|
0,
|
DWRITE_FONT_WEIGHT_REGULAR,
|
DWRITE_FONT_STYLE_NORMAL,
|
DWRITE_FONT_STRETCH_MEDIUM,
|
fFontSize,
|
L"en-us",
|
&pTextFormat
|
);
|
if (hr!=S_OK)
|
{
|
SafeRelease(pPenStart);
|
return;
|
}
|
|
// x dir
|
pt1.y = 0.f;
|
pt2.y = pDataGlass->fGlassHeight;
|
pt3.y = -fGabHeight;
|
|
for (int nIdx=0; nIdx<nWidthCount; nIdx++)
|
{
|
pt3.x = pt2.x = pt1.x = (FLOAT(nIdx) * pDataGlass->fRulerWidth);
|
|
if (pt1.x > pDataGlass->fGlassWidth) continue;
|
|
if (nIdx==0)
|
{
|
pRenderTarget->DrawLine(pt1, pt2, pPenStart, fPenWidth);
|
}
|
else
|
{
|
pRenderTarget->DrawLine(pt1, pt2, pPenMiddle, fPenWidth);
|
}
|
|
pRenderTarget->DrawLine(pt1, pt3, pPenStart, fPenWidth);
|
|
#ifdef X
|
str.Format(_T("%d"), (int)pt1.x);
|
IDWriteTextLayout *pTextLayout = NULL;
|
HRESULT hr = m_pDWFactroy->CreateTextLayout(str, wcslen(str), pTextFormat, fTextWidth*2.0f, fTextHeight, &pTextLayout);
|
#else
|
USES_CONVERSION;
|
str.Format(_T("%d"), (int)pt1.x);
|
WCHAR *wcstr = T2W(str.GetBuffer());
|
IDWriteTextLayout *pTextLayout = NULL;
|
HRESULT hr = m_pDWFactroy->CreateTextLayout(wcstr, wcslen(wcstr), pTextFormat, fTextWidth*2.0f, fTextHeight, &pTextLayout);
|
#endif
|
if (hr!=S_OK) continue;
|
|
pTextLayout->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
|
|
pt4 = pt3;
|
pt4.x -= fTextWidth;
|
pt4.y -= fTextHeight* 4.f;
|
|
pRenderTarget->DrawTextLayout(pt4, pTextLayout, pPenStart);
|
|
SafeRelease(pTextLayout);
|
}
|
|
// y dir
|
pt1.x = 0.0f;
|
pt2.x = pDataGlass->fGlassWidth;
|
pt3.x = -fGabWidth;
|
for (int nIdx=0; nIdx<nHeightCount; nIdx++)
|
{
|
pt3.y = pt2.y = pt1.y = (FLOAT(nIdx) * pDataGlass->fRulerHeight);
|
|
if (pt1.y > pDataGlass->fGlassHeight) continue;
|
|
if (nIdx==0)
|
{
|
pRenderTarget->DrawLine(pt1, pt2, pPenStart, fPenWidth);
|
}
|
else
|
{
|
pRenderTarget->DrawLine(pt1, pt2, pPenMiddle, fPenWidth);
|
}
|
|
pRenderTarget->DrawLine(pt1, pt3, pPenStart, fPenWidth);
|
|
#ifdef X
|
str.Format(_T("%d"), (int)pt1.y);
|
IDWriteTextLayout *pTextLayout = NULL;
|
HRESULT hr = m_pDWFactroy->CreateTextLayout(str, wcslen(str), pTextFormat, fTextWidth*2.0f, fTextHeight, &pTextLayout);
|
#else
|
USES_CONVERSION;
|
str.Format(_T("%d"), (int)pt1.y);
|
WCHAR *wcstr1 = T2W(str.GetBuffer());
|
IDWriteTextLayout *pTextLayout = NULL;
|
HRESULT hr = m_pDWFactroy->CreateTextLayout(wcstr1, wcslen(wcstr1), pTextFormat, fTextWidth*2.0f, fTextHeight, &pTextLayout);
|
#endif
|
if (hr!=S_OK) continue;
|
|
pTextLayout->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
|
|
pt4 = pt3;
|
pt4.x -= (fTextWidth * 2.f);
|
pt4.y -= fTextHeight * 1.5f;
|
|
pRenderTarget->DrawTextLayout(pt4, pTextLayout, pPenStart);
|
|
SafeRelease(pTextLayout);
|
|
}
|
|
SafeRelease(pTextFormat); // ¸É¹ö·Î Ãß°¡ÇÊ¿ä
|
SafeRelease(pPenStart);
|
SafeRelease(pPenMiddle);
|
}
|
|
|
void CGlassDefectMap::UpdateGlassLayer(ID2D1RenderTarget *pRenderTarget)
|
{
|
// create render target
|
ID2D1BitmapRenderTarget *pRenderTargetBitmap = nullptr;
|
HRESULT hr = pRenderTarget->CreateCompatibleRenderTarget(m_fRenderTargetHwndSize, m_pxRenderTargetHwndSize, &pRenderTargetBitmap);
|
if (hr!=S_OK)
|
{
|
return;
|
}
|
|
// begin
|
pRenderTargetBitmap->BeginDraw();
|
|
// transform
|
pRenderTargetBitmap->SetTransform(m_matTransform);
|
|
if (pRenderTargetBitmap->GetAntialiasMode() != D2D1_ANTIALIAS_MODE_PER_PRIMITIVE)
|
{
|
pRenderTargetBitmap->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
|
}
|
|
// update draw data
|
UpdateDrawData(pRenderTargetBitmap, &m_DrawData_Glass, m_fMapScale.width);
|
|
// update glass ruler
|
UpdateGlassRuler(pRenderTargetBitmap, &m_DrawData_Glass, m_fMapScale);
|
|
// update glass corner
|
D2D1_MATRIX_3X2_F matTransform;
|
CalCornerTransformMatrix(m_fMapTranslation, m_fMapScale, m_fMapPoint, m_fRenderTargetHwndSize, &m_DrawData_Glass, matTransform);
|
pRenderTargetBitmap->SetTransform(matTransform);
|
|
UpdateGlassCorner(pRenderTargetBitmap, &m_DrawData_Glass, m_fMapScale);
|
|
pRenderTargetBitmap->EndDraw();
|
|
// get bitmap
|
SafeRelease(m_pLayerGlass);
|
hr = pRenderTargetBitmap->GetBitmap(&m_pLayerGlass);
|
|
// release render target
|
SafeRelease(pRenderTargetBitmap);
|
}
|
|
|
|
void CGlassDefectMap::UpdateCellLayer(ID2D1RenderTarget *pRenderTarget)
|
{
|
// create render target
|
ID2D1BitmapRenderTarget *pRenderTargetBitmap = nullptr;
|
HRESULT hr = pRenderTarget->CreateCompatibleRenderTarget(m_fRenderTargetHwndSize, m_pxRenderTargetHwndSize, &pRenderTargetBitmap);
|
if (hr!=S_OK)
|
{
|
return;
|
}
|
|
// begin
|
pRenderTargetBitmap->BeginDraw();
|
|
// transform
|
pRenderTargetBitmap->SetTransform(m_matTransform);
|
|
if (pRenderTargetBitmap->GetAntialiasMode() == D2D1_ANTIALIAS_MODE_ALIASED)
|
{
|
pRenderTargetBitmap->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
|
}
|
|
// update draw data
|
for (constVectorDrawData_CellIt it=m_vecDrawData_Cell.begin(); it!=m_vecDrawData_Cell.end(); it++)
|
{
|
const CDrawData_Cell *pDrawData = static_cast<const CDrawData_Cell *>(&(*it));
|
if (pDrawData==NULL) continue;
|
|
UpdateDrawData(pRenderTargetBitmap, pDrawData, m_fMapScale.width);
|
}
|
|
// end
|
pRenderTargetBitmap->EndDraw();
|
|
// get bitmap
|
SafeRelease(m_pLayerCell);
|
pRenderTargetBitmap->GetBitmap(&m_pLayerCell);
|
|
// release render target
|
SafeRelease(pRenderTargetBitmap);
|
}
|
|
void CGlassDefectMap::UpdateDefectLayer(ID2D1RenderTarget *pRenderTarget)
|
{
|
// if (m_pRenderTargetBitmap==nullptr) return;
|
//
|
// // clear
|
// m_pRenderTargetBitmap->Clear();
|
//
|
// // transform
|
// m_pRenderTargetBitmap->SetTransform(m_matTransform);
|
//
|
// // draw
|
//
|
// // get
|
// SafeRelease(m_pLayerDefect);
|
// m_pRenderTargetBitmap->GetBitmap( &m_pLayerDefect );
|
}
|
|
/*
|
void CGlassDefectMap::CalculateMapScale(const D2D1_SIZE_F& fViewSize, const int nDrawType, const float fZoomLevel, const CDrawData_Glass *pDataGlass, D2D1_SIZE_F& fMapScale, D2D1_MATRIX_3X2_F& matMapScale)
|
{
|
matMapScale = D2D1::SizeF(0.0f, 0.0f);
|
|
float fScaleX = fViewSize.width / pDataGlass->fGlassWidth;
|
float fScaleY = fViewSize.height / pDataGlass->fGlassHeight;
|
|
// 1. cal scale
|
switch(nDrawType)
|
{
|
case MDT_Auto:
|
{
|
if (fScaleX < fScaleY)
|
{
|
fMapScale = D2D1::SizeF(fScaleX, fScaleX);
|
}
|
else
|
{
|
fMapScale = D2D1::SizeF(fScaleY, fScaleY);
|
}
|
}
|
break;
|
|
case MDT_Horiz:
|
{
|
fMapScale = D2D1::SizeF(fScaleX, fScaleX);
|
}
|
break;
|
|
case MDT_Vert:
|
{
|
fMapScale = D2D1::SizeF(fScaleY, fScaleY);
|
}
|
break;
|
|
case MDT_Fit:
|
fMapScale.width = fScaleX;
|
fMapScale.height = fScaleY;
|
break;
|
}
|
|
// 2. cal zoom
|
fMapScale.width *= fZoomLevel;
|
fMapScale.height *= fZoomLevel;
|
|
|
}
|
*/
|
|
void CGlassDefectMap::CalTransformMatrix()
|
{
|
m_matTransform = D2D1::Matrix3x2F::Identity();
|
|
m_fMapTranslation = D2D1::SizeF(0.0f, 0.0f);
|
m_fMapScale = D2D1::SizeF(1.0f, 1.0f);
|
|
float fViewWidth = m_fRenderTargetHwndSize.width;
|
float fViewHeight = m_fRenderTargetHwndSize.height;
|
|
// 1. cal scale
|
int nDrawType = m_nMapDrawType;
|
switch(nDrawType)
|
{
|
case MDT_Auto:
|
{
|
float fMapScaleX = fViewWidth / m_DrawData_Glass.fGlassWidth;
|
float fMapScaleY = fViewHeight / m_DrawData_Glass.fGlassHeight;
|
|
if (fMapScaleX < fMapScaleY)
|
{
|
nDrawType = MDT_Horiz;
|
m_fMapScale = D2D1::SizeF(fMapScaleX, fMapScaleX);
|
}
|
else
|
{
|
nDrawType = MDT_Vert;
|
m_fMapScale = D2D1::SizeF(fMapScaleY, fMapScaleY);
|
}
|
}
|
break;
|
|
case MDT_Horiz:
|
{
|
float fMapScaleX = fViewWidth / m_DrawData_Glass.fGlassWidth;
|
m_fMapScale = D2D1::SizeF(fMapScaleX, fMapScaleX);
|
}
|
break;
|
|
case MDT_Vert:
|
{
|
float fMapScaleY = fViewHeight / m_DrawData_Glass.fGlassHeight;
|
m_fMapScale = D2D1::SizeF(fMapScaleY, fMapScaleY);
|
}
|
break;
|
|
case MDT_Fit:
|
m_fMapScale.width = fViewWidth / m_DrawData_Glass.fGlassWidth;
|
m_fMapScale.height = fViewHeight / m_DrawData_Glass.fGlassHeight;
|
break;
|
}
|
|
// 2. cal zoom
|
m_fMapScale.width *= m_fMapZoomLevel;
|
m_fMapScale.height *= m_fMapZoomLevel;
|
|
// 3. cal trans
|
switch(m_DrawData_Glass.nOriginDir)
|
{
|
case MODT_LeftTop:
|
m_fMapTranslation.width = ( fViewWidth - (m_DrawData_Glass.fGlassWidth * m_fMapScale.width) ) / 2.f ;
|
m_fMapTranslation.height = ( fViewHeight - (m_DrawData_Glass.fGlassHeight * m_fMapScale.height) ) / 2.f ;
|
m_fMapScale.width *= 1.0f;
|
m_fMapScale.height *= 1.0f;
|
break;
|
|
case MODT_RightTop:
|
m_fMapTranslation.width = fViewWidth - ( fViewWidth - (m_DrawData_Glass.fGlassWidth * m_fMapScale.width) ) / 2.f ;
|
m_fMapTranslation.height= ( fViewHeight - (m_DrawData_Glass.fGlassHeight * m_fMapScale.height) ) / 2.f ;
|
m_fMapScale.width *= -1.0f;
|
m_fMapScale.height *= 1.0f;
|
break;
|
|
case MODT_LeftBottom:
|
m_fMapTranslation.width = ( fViewWidth - (m_DrawData_Glass.fGlassWidth * m_fMapScale.width) ) / 2.f ;
|
m_fMapTranslation.height = fViewHeight - ( fViewHeight - (m_DrawData_Glass.fGlassHeight * m_fMapScale.height) ) / 2.f ;
|
m_fMapScale.width *= 1.0f;
|
m_fMapScale.height *= -1.0f;
|
break;
|
|
case MODT_RightBottom:
|
m_fMapTranslation.width = fViewWidth - ( fViewWidth - (m_DrawData_Glass.fGlassWidth * m_fMapScale.width) ) / 2.f ;
|
m_fMapTranslation.height = fViewHeight - ( fViewHeight - (m_DrawData_Glass.fGlassHeight * m_fMapScale.height) ) / 2.f ;
|
m_fMapScale.width *= -1.0f;
|
m_fMapScale.height *= -1.0f;
|
break;
|
|
default:
|
return;
|
}
|
|
// 4. cal dx, dy
|
// m_fMapPoint.x -= FLOAT(m_ptMapPoint.x) / m_fMapScale.width;
|
// m_fMapPoint.y -= FLOAT(m_ptMapPoint.y) / m_fMapScale.height;
|
D2D1_MATRIX_3X2_F matTranslation = D2D1::Matrix3x2F::Translation(m_fMapTranslation);
|
D2D1_MATRIX_3X2_F matScale = D2D1::Matrix3x2F::Scale(m_fMapScale, m_fMapPoint);
|
|
// 5. cal transform matrix
|
m_matTransform = matScale * matTranslation;
|
}
|
|
void CGlassDefectMap::CalCornerTransformMatrix(const D2D1_SIZE_F& fTrans, const D2D1_SIZE_F& fScale, const D2D1_POINT_2F& fPoint, const D2D1_SIZE_F& fViewSize, const CDrawData_Glass *pDataGlass, D2D1_MATRIX_3X2_F& matTransform)
|
{
|
D2D1_SIZE_F fMapTranslation = fTrans;
|
D2D1_SIZE_F fMapScale = fScale;
|
D2D1_POINT_2F fMapPoint = fPoint;
|
|
// 1. abs scale
|
fMapScale.width = fabs(fMapScale.width);
|
fMapScale.height = fabs(fMapScale.height);
|
|
// 2. cal trans
|
fMapTranslation.width = ( fViewSize.width - (pDataGlass->fGlassWidth * fMapScale.width) ) / 2.f ;
|
fMapTranslation.height = ( fViewSize.height - (pDataGlass->fGlassHeight * fMapScale.height) ) / 2.f ;
|
|
|
// 3. cal dx, dy
|
// m_fMapPoint.x -= FLOAT(m_ptMapPoint.x) / m_fMapScale.width;
|
// m_fMapPoint.y -= FLOAT(m_ptMapPoint.y) / m_fMapScale.height;
|
D2D1_MATRIX_3X2_F matTranslation = D2D1::Matrix3x2F::Translation(fMapTranslation);
|
D2D1_MATRIX_3X2_F matScale = D2D1::Matrix3x2F::Scale(fMapScale, fMapPoint);
|
|
// 4. cal transform matrix
|
matTransform = matScale * matTranslation;
|
}
|
|
void CGlassDefectMap::OnPaint()
|
{
|
if (m_pRenderTargetHwnd==nullptr) return;
|
|
CalTransformMatrix();
|
|
m_pRenderTargetHwnd->BeginDraw();
|
m_pRenderTargetHwnd->SetTransform(D2D1::Matrix3x2F::Identity());
|
m_pRenderTargetHwnd->Clear(D2D1::ColorF(D2D1::ColorF(m_nMapBackColor, 1.0f)));
|
|
if (m_pRenderTargetHwnd->GetAntialiasMode() != D2D1_ANTIALIAS_MODE_PER_PRIMITIVE)
|
{
|
m_pRenderTargetHwnd->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
|
}
|
|
|
|
if (m_bDrawGlass) DrawGlassLayer(m_pRenderTargetHwnd, &m_rtRenderTargetHwndRect);
|
|
if (m_bDrawCell) DrawCellLayer(m_pRenderTargetHwnd, &m_rtRenderTargetHwndRect);
|
|
if (m_bDrawDefect) DrawDefectLayer(m_pRenderTargetHwnd, &m_rtRenderTargetHwndRect);
|
|
ID2D1SolidColorBrush *pPen = NULL;
|
HRESULT hr = m_pRenderTargetHwnd->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF(MakeColor(0,60,60,60), 1.0f)), &pPen);
|
if (hr==S_OK)
|
{
|
m_pRenderTargetHwnd->DrawRectangle(m_rtRenderTargetHwndRect, pPen, 1.0);
|
SafeRelease(pPen);
|
}
|
|
m_pRenderTargetHwnd->EndDraw();
|
}
|
|
void CGlassDefectMap::SetDrawData_Glass( const CDrawData_Glass& dataGlass )
|
{
|
m_DrawData_Glass = dataGlass;
|
}
|
|
void CGlassDefectMap::SetMapZoomLevel( float fLevel )
|
{
|
if (m_fMapZoomLevel == fLevel) return;
|
m_fMapZoomLevel = fLevel;
|
ReleaseAllLayers();
|
}
|
|
void CGlassDefectMap::SetMapCenterPoint( const CPoint& ptPoint )
|
{
|
if (m_ptMapPoint == ptPoint) return;
|
m_ptMapPoint = ptPoint;
|
ReleaseAllLayers();
|
}
|
|
void CGlassDefectMap::SetParentWnd( CWnd* pParentWnd )
|
{
|
m_pParentWnd = pParentWnd;
|
}
|
|
void CGlassDefectMap::SetMapDefaultValue()
|
{
|
if (m_fMapZoomLevel==1.0f && m_ptMapPoint==CPoint(0,0)) return;
|
m_fMapZoomLevel = 1.0f;
|
m_ptMapPoint = CPoint(0,0);
|
ReleaseAllLayers();
|
}
|
|
void CGlassDefectMap::ReleaseAllLayers()
|
{
|
SafeRelease(m_pLayerGlass);
|
SafeRelease(m_pLayerCell);
|
SafeRelease(m_pLayerDefect);
|
|
Invalidate(FALSE);
|
}
|
|
int CGlassDefectMap::OnCreate(LPCREATESTRUCT lpCreateStruct)
|
{
|
if (CWnd::OnCreate(lpCreateStruct) == -1)
|
return -1;
|
|
InitGlassDefectMap();
|
|
return 0;
|
}
|
|
|
void CGlassDefectMap::OnLButtonDown(UINT nFlags, CPoint point)
|
{
|
if (m_pParentWnd==NULL) return;
|
|
CRect rect;
|
this->GetClientRect(rect);
|
if (rect.PtInRect(point))
|
{
|
m_pParentWnd->SendMessage(WM_LBUTTONDOWN, static_cast<WPARAM>(nFlags), MAKELPARAM(point.x, point.y));
|
}
|
|
CWnd::OnLButtonDown(nFlags, point);
|
}
|
|
|
void CGlassDefectMap::OnLButtonUp(UINT nFlags, CPoint point)
|
{
|
CRect rect;
|
this->GetClientRect(rect);
|
if (rect.PtInRect(point))
|
{
|
m_pParentWnd->SendMessage(WM_LBUTTONUP, static_cast<WPARAM>(nFlags), MAKELPARAM(point.x, point.y));
|
}
|
|
CWnd::OnLButtonUp(nFlags, point);
|
}
|
|
|
void CGlassDefectMap::OnMouseMove(UINT nFlags, CPoint point)
|
{
|
CRect rect;
|
this->GetClientRect(rect);
|
if (rect.PtInRect(point))
|
{
|
m_pParentWnd->SendMessage(WM_MOUSEMOVE, static_cast<WPARAM>(nFlags), MAKELPARAM(point.x, point.y));
|
}
|
|
CWnd::OnMouseMove(nFlags, point);
|
}
|
|
void CGlassDefectMap::SetMapBackColor( UINT nColor )
|
{
|
m_nMapBackColor = nColor;
|
Invalidate(FALSE);
|
}
|