//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: LgUIMapWnd.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgUIMapWnd.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "..\Version.h" #include "LgUIMapWnd.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CLgUIMapWnd::CLgUIMapWnd() { m_dScale = 1.0; m_dpShift.x = m_dpShift.y = 0.0; m_dMeterScale = 1.0; // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapWnd::CLgUIMapWnd" ); } CLgUIMapWnd::~CLgUIMapWnd() { m_BackgroundDrawThread.Destroy(); // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapWnd::~CLgUIMapWnd" ); } BEGIN_MESSAGE_MAP(CLgUIMapWnd, CWnd) //{{AFX_MSG_MAP(CLgUIMapWnd) ON_WM_PAINT() ON_WM_SIZE() //}}AFX_MSG_MAP ON_MESSAGE( WM_BACKGROUND_DRAW_THREAD, OnWmBackgroundDrawThread ) END_MESSAGE_MAP() BOOL CLgUIMapWnd::Create( CWnd *pParentWnd, CLgUIMapBackground *pBackground, int x, int y, int nWidth, int nHeight ) { if( !pBackground ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapWnd::Create"), _T("pBackground is NULL") ); return FALSE; } CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, ::LoadCursor( NULL, IDC_CROSS ), NULL ); CRect rect( x, y, x + nWidth, y + nHeight ); if( !CWnd::Create( strClassName, NULL, WS_CHILD | WS_DLGFRAME | WS_CLIPSIBLINGS, rect, pParentWnd, 0 ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapWnd::Create"), _T("CWnd::Create failed") ); return FALSE; } if( !m_BackgroundDrawThread.Create( this, pBackground ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapWnd::Create"), _T("m_BackgroundDrawThread.Create failed") ); return FALSE; } m_dMeterScale = pBackground ? pBackground->GetMeterScale() : 1.0; OnChangeViewParams(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapWnd::Create", _T("Succeeded") ); return TRUE; } void CLgUIMapWnd::OnPaint() { LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnPaint"), _T("Started...") ); CPaintDC dc(this); CRect rect; GetClientRect( &rect ); dc.BitBlt( 0, 0, rect.Width(), rect.Height(), &m_BackgroundDC, 0, 0, SRCCOPY ); OnDraw( &dc ); LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnPaint"), _T("Done") ); } void CLgUIMapWnd::OnSize( UINT nType, int cx, int cy ) { LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnSize"), _T("Started...") ); CWnd::OnSize( nType, cx, cy ); // reinitialize if( cx && cy ) { m_BackgroundDC.Create( cx, cy, RGB(0,0xFF,0) ); OnChangeViewParams(); } LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnSize"), _T("Done") ); } LRESULT CLgUIMapWnd::OnWmBackgroundDrawThread( WPARAM, LPARAM ) { // log info LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgUIMapWnd::OnWmBackgroundDrawThread", "Start..." ); if( m_BackgroundDrawThread.Draw( &m_BackgroundDC ) ) { OnDrawBackground( &m_BackgroundDC ); InvalidateRect( NULL, FALSE ); // log info LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgUIMapWnd::OnWmBackgroundDrawThread", "Done successfully" ); return TRUE; } // log info LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgUIMapWnd::OnWmBackgroundDrawThread", "Done unsuccessfully" ); return FALSE; /* CMemoryDC *pMemoryDC = m_BackgroundDrawThread.GetDC(); if( !pMemoryDC ) return 0; CSize size = m_BackgroundDC.GetSize(); m_BackgroundDC.BitBlt( 0, 0, size.cx, size.cy, pMemoryDC, 0, 0, SRCCOPY ); m_BackgroundDrawThread.ReleaseDC(); InvalidateRect( NULL, FALSE ); return 0; */ } void CLgUIMapWnd::OnDraw( CDC* ) { } void CLgUIMapWnd::OnDrawBackground( CDC* ) { } CLgDPoint CLgUIMapWnd::WindowToArea( CPoint *pPoint ) { CRect FitFrame = GetFitFrame(); return CLgDPoint( ( (double)( pPoint->x - FitFrame.left ) ) / ( FitFrame.Width() * m_dScale ) + m_dpShift.x, ( (double)( pPoint->y - FitFrame.top ) ) / ( FitFrame.Height() * m_dScale ) + m_dpShift.y ); } CLgDSize CLgUIMapWnd::WindowToArea( CSize *pSize ) { CRect FitFrame = GetFitFrame(); return CLgDSize( ( (double)pSize->cx ) / ( FitFrame.Width() * m_dScale ), ( (double)pSize->cy ) / ( FitFrame.Height() * m_dScale ) ); } CLgDRect CLgUIMapWnd::WindowToArea( CRect *pRect ) { CPoint pTopLeft( pRect->TopLeft() ); CPoint pBottomRight( pRect->BottomRight() ); CLgDPoint dTopLeft( WindowToArea( &pTopLeft ) ); CLgDPoint dBottomRight( WindowToArea( &pBottomRight ) ); return CLgDRect( dTopLeft.x, dTopLeft.y, dBottomRight.x, dBottomRight.y ); } CPoint CLgUIMapWnd::AreaToWindow( CLgDPoint *pDPoint ) { CRect FitFrame = GetFitFrame(); return CPoint( (int)( ( pDPoint->x - m_dpShift.x ) * FitFrame.Width() * m_dScale + 0.5 ) + FitFrame.left, (int)( ( pDPoint->y - m_dpShift.y ) * FitFrame.Height() * m_dScale + 0.5 ) + FitFrame.top ); } CSize CLgUIMapWnd::AreaToWindow( CLgDSize *pDSize ) { CRect FitFrame = GetFitFrame(); return CPoint( (int)( pDSize->cx * FitFrame.Width() * m_dScale + 0.5 ), (int)( pDSize->cy * FitFrame.Height() * m_dScale + 0.5 ) ); } CRect CLgUIMapWnd::AreaToWindow( CLgDRect *pDRect ) { CLgDPoint dTopLeft( pDRect->left, pDRect->top ); CLgDPoint dBottomRight( pDRect->right, pDRect->bottom ); CPoint pTopLeft( AreaToWindow( &dTopLeft ) ); CPoint pBottomRight( AreaToWindow( &dBottomRight ) ); return CRect( pTopLeft, pBottomRight ); } void CLgUIMapWnd::FitAreaToWindow() { m_dpShift.x = 0.0; m_dpShift.y = 0.0; m_dScale = 1.0; OnChangeViewParams(); } CRect CLgUIMapWnd::GetFitFrame() { if( !GetSafeHwnd() ) return CRect(0,0,0,0); CLgUIMapBackground *pBackground = m_BackgroundDrawThread; if( !pBackground ) return CRect(0,0,0,0); CRect ClientRect; GetClientRect( &ClientRect ); CSize size = pBackground->GetSize(); double dAspect = (((double)size.cx)/(double)size.cy); CRect FitRect; FitRectWithAspect( &ClientRect, &FitRect, dAspect ); return FitRect; } void CLgUIMapWnd::OnChangeViewParams() { if( !GetSafeHwnd() ) return; LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnChangeViewParams"), _T("Started...") ); CRect WndRect; GetClientRect( &WndRect ); CRect DstRect( AreaToWindow( &CLgDPoint(0,0) ), AreaToWindow( &CLgDPoint(1.0,1.0) ) ); m_BackgroundDrawThread.InitDraw( &WndRect, &DstRect ); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnChangeViewParams"), _T("Done") ); } void CLgUIMapWnd::SetCenter( CLgDPoint *pDPoint ) { CRect FitFrame = GetFitFrame(); if( !FitFrame.Width() || !FitFrame.Height() ) return; CPoint CenterPoint( FitFrame.left + FitFrame.Width() / 2, FitFrame.top + FitFrame.Height() / 2 ); m_dpShift.x = pDPoint->x - ( CenterPoint.x - FitFrame.left ) / ( FitFrame.Width() * m_dScale ); m_dpShift.y = pDPoint->y - ( CenterPoint.y - FitFrame.top ) / ( FitFrame.Height() * m_dScale ); OnChangeViewParams(); } void CLgUIMapWnd::SetScale( double dScale, CLgDPoint *pdPoint ) { if( dScale <= 0.0 ) return; CLgDPoint pdCenter; if( pdPoint == NULL ) { CRect FitFrame = GetFitFrame(); CPoint CenterPoint( FitFrame.left + FitFrame.Width() / 2, FitFrame.top + FitFrame.Height() / 2 ); pdCenter = WindowToArea( &CenterPoint ); pdPoint = &pdCenter; } CPoint p = AreaToWindow( pdPoint ); m_dScale = dScale; CLgDPoint dp = WindowToArea( &p ); m_dpShift += *pdPoint - dp; OnChangeViewParams(); } void CLgUIMapWnd::SetMeterScale( double dScale ) { m_dMeterScale = dScale; // redraw contents if( GetSafeHwnd() ) InvalidateRect( NULL ); } // meter to area conversions CLgDSize CLgUIMapWnd::MeterToArea( CLgDSize* pDSize ) { CRect FitFrame = GetFitFrame(); return CLgDSize( pDSize->cx / m_dMeterScale, pDSize->cy * ((double)FitFrame.Width()/(double)FitFrame.Height()) / m_dMeterScale ); } CLgDSize CLgUIMapWnd::AreaToMeter( CLgDSize* pDSize ) { return CLgDSize( pDSize->cx * m_dMeterScale, pDSize->cy * m_dMeterScale ); } CLgDPoint CLgUIMapWnd::MeterToArea( CLgDPoint* pDPoint ) { return CLgDPoint( pDPoint->x / m_dMeterScale, pDPoint->y / m_dMeterScale ); } CLgDPoint CLgUIMapWnd::AreaToMeter( CLgDPoint* pDPoint ) { return CLgDPoint( pDPoint->x * m_dMeterScale, pDPoint->y * m_dMeterScale ); } CLgDRect CLgUIMapWnd::MeterToArea( CLgDRect* pDRect ) { return CLgDRect( pDRect->left / m_dMeterScale, pDRect->top / m_dMeterScale, pDRect->right / m_dMeterScale, pDRect->bottom / m_dMeterScale ); } CLgDRect CLgUIMapWnd::AreaToMeter( CLgDRect* pDRect ) { return CLgDRect( pDRect->left * m_dMeterScale, pDRect->top * m_dMeterScale, pDRect->right * m_dMeterScale, pDRect->bottom * m_dMeterScale ); } double CLgUIMapWnd::GetMeterToPixelScale() { CSize m2p = AreaToWindow( &MeterToArea( &CLgDSize( 1000000.0, 1000000.0 ) ) ); return m2p.cx / 1000000.0; }