Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

483 lines
12 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LocatorWnd.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LocatorWnd.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LocatorWnd.h"
  16. #include <math.h>
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. CLocatorWnd::CLocatorWnd()
  23. {
  24. memset( &m_PTZ, 0, sizeof(m_PTZ) );
  25. memset( &m_dPTZ, 0, sizeof(m_dPTZ) );
  26. m_nTimer = 0;
  27. m_dwDirection = 0;
  28. m_bSynchronizedPan = FALSE;
  29. m_bSynchronizedTilt = FALSE;
  30. m_bChange = FALSE;
  31. m_nChangeTime = 0;
  32. m_bBackground = FALSE;
  33. }
  34. CLocatorWnd::~CLocatorWnd()
  35. {
  36. }
  37. BEGIN_MESSAGE_MAP(CLocatorWnd, CWnd)
  38. //{{AFX_MSG_MAP(CLocatorWnd)
  39. ON_WM_PAINT()
  40. ON_WM_SIZE()
  41. ON_WM_TIMER()
  42. //}}AFX_MSG_MAP
  43. END_MESSAGE_MAP()
  44. BOOL CLocatorWnd::Create( CWnd *pParentWnd, INT x, INT y, INT cx, INT cy )
  45. {
  46. CRect rect( x, y, x + cx, y + cy );
  47. return Create( pParentWnd, rect );
  48. }
  49. BOOL CLocatorWnd::Create( CWnd* pParentWnd, CRect &rect )
  50. {
  51. CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, LoadCursor( NULL, IDC_ARROW ), NULL, NULL );
  52. if( !CWnd::Create( strClassName, NULL, WS_CHILD | WS_VISIBLE, rect, pParentWnd, 0 ) )
  53. return FALSE;
  54. m_nTimer = CWnd::SetTimer( 0, 50, NULL );
  55. if( !m_nTimer )
  56. return FALSE;
  57. return TRUE;
  58. }
  59. void CLocatorWnd::OnPaint()
  60. {
  61. CPaintDC dc(this);
  62. Draw( &dc );
  63. }
  64. void CLocatorWnd::DrawCamera( CDC *pDC )
  65. {
  66. // paint video frame
  67. CRect rect;
  68. GetClientRect( &rect );
  69. // calculate positions
  70. double d = 3.0 + (1.0 - m_PTZ.dZoom) * 21.0;
  71. INT w = rect.Width() / 2;
  72. INT h = rect.Height() / 2;
  73. double r = h * ( 0.2 + m_PTZ.dZoom * 0.8 );
  74. COLORREF color = m_bSynchronizedPan ? RGB(0,255,0) : RGB(255,0,0);
  75. CBrush brush( color );
  76. CPen pen( PS_SOLID, 2, color );
  77. CBrush *pBrush = pDC->SelectObject( &brush );
  78. CPen *pPen = pDC->SelectObject( &pen );
  79. INT nOldROP = pDC->SetROP2( R2_XORPEN );
  80. INT x1 = (INT)( ( cos( ( m_PTZ.dPan + d ) / 360.0 * 6.2831 ) ) * r + w );
  81. INT y1 = (INT)( ( sin( ( m_PTZ.dPan + d ) / 360.0 * 6.2831 ) ) * r + h );
  82. INT x2 = (INT)( ( cos( ( m_PTZ.dPan - d ) / 360.0 * 6.2831 ) ) * r + w );
  83. INT y2 = (INT)( ( sin( ( m_PTZ.dPan - d ) / 360.0 * 6.2831 ) ) * r + h );
  84. POINT p[] = { { x1, y1 }, { x2, y2 }, { w, h } };
  85. rect.SetRect( (INT)(w-r), (INT)(h-r), (INT)(w+r), (INT)(h+r) );
  86. pDC->Pie( &rect, p[0], p[1] );
  87. /*
  88. pDC->MoveTo( w, h );
  89. pDC->AngleArc( w, h, r, 360 - (m_PTZ.dPan + d), d * 2 );
  90. pDC->LineTo( w, h );
  91. */
  92. pDC->SetROP2( nOldROP );
  93. pDC->SelectObject( pPen );
  94. pDC->SelectObject( pBrush );
  95. }
  96. void CLocatorWnd::Draw(CDC *pDC)
  97. {
  98. CRect rect;
  99. GetClientRect( &rect );
  100. m_MemoryDC.BitBlt( 0, 0, rect.Width(), rect.Height(), &m_FieldDC, 0, 0, SRCCOPY );
  101. // DrawCamera( &m_MemoryDC );
  102. BLENDFUNCTION bf;
  103. bf.AlphaFormat = 0;
  104. bf.BlendFlags = 0;
  105. bf.BlendOp = 0;
  106. bf.SourceConstantAlpha = 100;
  107. ::AlphaBlend( m_MemoryDC.GetSafeHdc(), 0, 0, rect.Width(), rect.Height(),
  108. m_CameraDC.GetSafeHdc(), 0, 0, rect.Width(), rect.Height(), bf );
  109. pDC->BitBlt( 0, 0, rect.Width(), rect.Height(), &m_MemoryDC, 0, 0, SRCCOPY );
  110. }
  111. BOOL CLocatorWnd::Clear()
  112. {
  113. CRect rect;
  114. GetClientRect( &rect );
  115. // prepare dimentions
  116. INT cx = rect.Width();
  117. INT cy = rect.Height();
  118. if( !m_bBackground )
  119. {
  120. CPen pen( PS_SOLID, 1, RGB(127 ,0,0) );
  121. CPen *pPen = m_FieldDC.SelectObject( &pen );
  122. CBrush brush( RGB(0,127,0) );
  123. CBrush *pBrush = m_FieldDC.SelectObject( &brush );
  124. // draw locator field
  125. INT W = cx / 2;
  126. INT H = cy / 2;
  127. for( INT i = 4; i >= 0; i-- )
  128. {
  129. INT w = W / 5 * (i + 1);
  130. INT h = H / 5 * (i + 1);
  131. m_FieldDC.Ellipse( W-w, H-h, W+w, H+h );
  132. }
  133. // draw lines
  134. m_FieldDC.MoveTo( 0, H );
  135. m_FieldDC.LineTo( cx, H );
  136. m_FieldDC.MoveTo( W, 0 );
  137. m_FieldDC.LineTo( W, cy );
  138. m_FieldDC.SelectObject( pBrush );
  139. m_FieldDC.SelectObject( pPen );
  140. }
  141. else
  142. m_FieldDC.BitBlt( 0, 0, cx, cy, &m_BackgroundDC, 0, 0, SRCCOPY );
  143. InvalidateRect( NULL, FALSE );
  144. return TRUE;
  145. }
  146. void CLocatorWnd::OnSize(UINT nType, int cx, int cy)
  147. {
  148. CWnd::OnSize(nType, cx, cy);
  149. m_MemoryDC.Create( cx, cy, RGB(0,0,0) );
  150. m_FieldDC.Create( cx, cy, RGB(0,0,0) );
  151. // draw locator field
  152. Clear();
  153. // create camera
  154. m_CameraDC.Create( cx, cy, RGB(0,0,0) );
  155. RedrawCamera();
  156. }
  157. void CLocatorWnd::RedrawCamera()
  158. {
  159. // return;
  160. // paint video frame
  161. CRect rect;
  162. GetClientRect( &rect );
  163. // calculate positions
  164. double d = 3.0 + (1.0 - m_PTZ.dZoom) * 21.0;
  165. INT w = rect.Width() / 2;
  166. INT h = rect.Height() / 2;
  167. double r = h * ( 0.2 + m_PTZ.dZoom * 0.8 );
  168. INT x1 = (INT)( ( cos( ( m_PTZ.dPan + d ) / 360.0 * 6.2831 ) ) * r + w );
  169. INT y1 = (INT)( ( sin( ( m_PTZ.dPan + d ) / 360.0 * 6.2831 ) ) * r + h );
  170. INT x2 = (INT)( ( cos( ( m_PTZ.dPan - d ) / 360.0 * 6.2831 ) ) * r + w );
  171. INT y2 = (INT)( ( sin( ( m_PTZ.dPan - d ) / 360.0 * 6.2831 ) ) * r + h );
  172. // redraw
  173. m_CameraDC.FillSolidRect( rect, RGB(0,0,0) );
  174. // draw
  175. POINT p[] = { { x1, y1 }, { x2, y2 }, { w, h } };
  176. // m_CameraDC.Polygon( p, 3 );
  177. // m_CameraDC.MoveTo( x1, y1 );
  178. // m_CameraDC.LineTo( w, h );
  179. // m_CameraDC.LineTo( x2, y2 );
  180. // POINT p[] = { { x1, y1 }, { x2, y2 } };
  181. COLORREF color = m_bSynchronizedPan ? RGB(0,255,0) : RGB(255,0,0);
  182. CBrush brush( color );
  183. CPen pen( PS_SOLID, 1, color );
  184. CBrush *pBrush = m_CameraDC.SelectObject( &brush );
  185. CPen *pPen = m_CameraDC.SelectObject( &pen );
  186. rect.SetRect( (INT)(w-r), (INT)(h-r), (INT)(w+r), (INT)(h+r) );
  187. m_CameraDC.Pie( &rect, p[0], p[1] );
  188. // select old pen
  189. // m_CameraDC.SelectObject( pPen );
  190. // CPen pen1( PS_SOLID, 10, RGB(0xFF,0xFF,0xFF) );
  191. // pPen = m_CameraDC.SelectObject( &pen1 );
  192. /*
  193. GetClientRect( &rect );
  194. double l = ( sqrt( pow( rect.Width(), 2 ) + pow( rect.Height(), 2 ) ) - rect.Width() ) / 4;
  195. w = l;
  196. h = l;
  197. r = h * ( 0.2 + m_PTZ.dZoom * 0.8 ) * 2;
  198. x1 = (INT)( ( cos( ( m_PTZ.dTilt + d ) / 360.0 * 6.2831 ) ) * r + w );
  199. y1 = (INT)( ( sin( ( m_PTZ.dTilt + d ) / 360.0 * 6.2831 ) ) * r + h );
  200. x2 = (INT)( ( cos( ( m_PTZ.dTilt - d ) / 360.0 * 6.2831 ) ) * r + w );
  201. y2 = (INT)( ( sin( ( m_PTZ.dTilt - d ) / 360.0 * 6.2831 ) ) * r + h );
  202. POINT p1[] = { { x1, y1 }, { x2, y2 }, { w, h } };
  203. rect.SetRect( (INT)(-r), (INT)(rect.Height()-r*2), (INT)(r), (INT)(rect.Height()) );
  204. m_CameraDC.Pie( &rect, p1[0], p1[1] );
  205. */
  206. /*
  207. m_CameraDC.MoveTo( 0, rect.bottom - (INT)l );
  208. x1 = (INT)( ( cos( m_PTZ.dTilt / 360.0 * 6.2831 ) ) * l );
  209. y1 = (INT)( ( sin( m_PTZ.dTilt / 360.0 * 6.2831 ) ) * l ) + rect.bottom - (INT)l;
  210. m_CameraDC.LineTo( x1, y1 );
  211. */
  212. m_CameraDC.SelectObject( pPen );
  213. m_CameraDC.SelectObject( pBrush );
  214. }
  215. BOOL CLocatorWnd::DestroyWindow()
  216. {
  217. CWnd::KillTimer( m_nTimer );
  218. return CWnd::DestroyWindow();
  219. }
  220. void CLocatorWnd::OnTimer( UINT nIDEvent )
  221. {
  222. BOOL bChange = FALSE;
  223. switch( m_dwDirection & PAN_MASK )
  224. {
  225. case PAN_LEFT:
  226. // perform acceleration
  227. if( m_dPTZ.dPan < 10 )
  228. m_dPTZ.dPan += 1;
  229. // calculate new position
  230. m_PTZ.dPan -= m_dPTZ.dPan;
  231. if( m_PTZ.dPan < 0.0 )
  232. m_PTZ.dPan += 360.0;
  233. // remember changes
  234. bChange = TRUE;
  235. break;
  236. case PAN_RIGTH:
  237. // perform acceleration
  238. if( m_dPTZ.dPan < 10 )
  239. m_dPTZ.dPan += 1;
  240. // calculate new position
  241. m_PTZ.dPan += m_dPTZ.dPan;
  242. if( m_PTZ.dPan >= 360.0 )
  243. m_PTZ.dPan -= 360.0;
  244. bChange = TRUE;
  245. break;
  246. case PAN_STOP:
  247. default:
  248. break;
  249. }
  250. switch( m_dwDirection & TILT_MASK )
  251. {
  252. case TILT_UP:
  253. // perform acceleration
  254. if( m_dPTZ.dTilt < 10 )
  255. m_dPTZ.dTilt += 1;
  256. // calculate new position
  257. m_PTZ.dTilt -= m_dPTZ.dTilt;
  258. if( m_PTZ.dTilt < -30.0 )
  259. m_PTZ.dTilt = -30.0;
  260. bChange = TRUE;
  261. break;
  262. case TILT_DOWN:
  263. // perform acceleration
  264. if( m_dPTZ.dTilt < 10 )
  265. m_dPTZ.dTilt += 1;
  266. // calculate new position
  267. m_PTZ.dTilt += m_dPTZ.dTilt;
  268. if( m_PTZ.dTilt > 90.0 )
  269. m_PTZ.dTilt = 90.0;
  270. bChange = TRUE;
  271. break;
  272. case TILT_STOP:
  273. default:
  274. break;
  275. }
  276. switch( m_dwDirection & ZOOM_MASK )
  277. {
  278. case ZOOM_TELE:
  279. // perform acceleration
  280. if( m_dPTZ.dZoom < 0.10 )
  281. m_dPTZ.dZoom += 0.01;
  282. // calculate new position
  283. m_PTZ.dZoom += m_dPTZ.dZoom;
  284. if( m_PTZ.dZoom > 1.0 )
  285. m_PTZ.dZoom = 1.0;
  286. bChange = TRUE;
  287. break;
  288. case ZOOM_WIDE:
  289. // perform acceleration
  290. if( m_dPTZ.dZoom < 0.10 )
  291. m_dPTZ.dZoom += 0.01;
  292. // calculate new position
  293. m_PTZ.dZoom -= m_dPTZ.dZoom;
  294. if( m_PTZ.dZoom < 0.0 )
  295. m_PTZ.dZoom = 0.0;
  296. bChange = TRUE;
  297. break;
  298. case ZOOM_STOP:
  299. default:
  300. break;
  301. }
  302. // apply changes
  303. if( bChange )
  304. {
  305. RedrawCamera();
  306. InvalidateRect( NULL, FALSE );
  307. m_bChange = TRUE;
  308. m_nChangeTime = 10;
  309. m_pCameraPositionWnd->SetPosition( m_PTZ.dPan, m_PTZ.dTilt, m_PTZ.dZoom, m_bSynchronizedTilt );
  310. }
  311. if( m_bChange )
  312. {
  313. if( m_nChangeTime-- <= 0 )
  314. {
  315. GetParent()->SendMessage( LG_CAMERA_POSITION_CHANGED );
  316. m_bChange = FALSE;
  317. }
  318. }
  319. /*
  320. if( rand()%2 )
  321. {
  322. AddTarget( rand() % 360, (rand() % 100) / 100.0, RGB(0xE0,0xE0,0) );
  323. }
  324. */
  325. CWnd::OnTimer(nIDEvent);
  326. }
  327. void CLocatorWnd::MovePTZ( DWORD dwDir )
  328. {
  329. if( dwDir & PAN_MASK )
  330. {
  331. if( ( m_dwDirection & PAN_MASK ) != ( dwDir & PAN_MASK ) )
  332. {
  333. m_dPTZ.dPan = 0;
  334. m_dwDirection = ( m_dwDirection & ~PAN_MASK ) | ( dwDir & PAN_MASK );
  335. }
  336. m_bSynchronizedPan = FALSE;
  337. }
  338. if( dwDir & TILT_MASK )
  339. {
  340. if( ( m_dwDirection & TILT_MASK ) != ( dwDir & TILT_MASK ) )
  341. {
  342. m_dPTZ.dTilt = 0;
  343. m_dwDirection = ( m_dwDirection & ~TILT_MASK ) | ( dwDir & TILT_MASK );
  344. }
  345. m_bSynchronizedTilt = FALSE;
  346. }
  347. if( dwDir & ZOOM_MASK )
  348. {
  349. if( ( m_dwDirection & ZOOM_MASK ) != ( dwDir & ZOOM_MASK ) )
  350. {
  351. m_dPTZ.dZoom = 0;
  352. m_dwDirection = ( m_dwDirection & ~ZOOM_MASK ) | ( dwDir & ZOOM_MASK );
  353. }
  354. m_bSynchronizedPan = FALSE;
  355. m_bSynchronizedTilt = FALSE;
  356. }
  357. }
  358. BOOL CLocatorWnd::AddTarget( double dAngle, double dRange, COLORREF color )
  359. {
  360. // paint video frame
  361. CRect rect;
  362. GetClientRect( &rect );
  363. // calculate positions
  364. INT w = rect.Width() / 2;
  365. INT h = rect.Height() / 2;
  366. INT x = (INT)( ( cos( dAngle / 360.0 * 6.2831 ) ) * dRange * w + w );
  367. INT y = (INT)( ( -sin( dAngle / 360.0 * 6.2831 ) ) * dRange * h + h );
  368. // draw
  369. CBrush brush( color );
  370. CPen pen( PS_SOLID, 1, color );
  371. CBrush *pBrush = m_FieldDC.SelectObject( &brush );
  372. CPen *pPen = m_FieldDC.SelectObject( &pen );
  373. m_FieldDC.Ellipse( x-2, y-2, x+2, y+2 );
  374. m_FieldDC.SelectObject( pPen );
  375. m_FieldDC.SelectObject( pBrush );
  376. CWnd::InvalidateRect( NULL, FALSE );
  377. return TRUE;
  378. }
  379. void CLocatorWnd::ValidatePTZ( double dPan, double dTilt, double dZoom )
  380. {
  381. double dP = fabs(m_PTZ.dPan - dPan);
  382. double dT = fabs(m_PTZ.dTilt - dTilt);
  383. double dZ = fabs(m_PTZ.dZoom - dZoom);
  384. BOOL bSynchronizedZoom = dZ < 0.1 || dZ > 0.8;
  385. m_bSynchronizedPan = ( dP < 10 || dP > 350 ) && bSynchronizedZoom;
  386. m_bSynchronizedTilt = ( dT < 10 || dT > 80 ) && bSynchronizedZoom;
  387. m_pCameraPositionWnd->SetPosition( m_PTZ.dPan, m_PTZ.dTilt, m_PTZ.dZoom, m_bSynchronizedTilt );
  388. RedrawCamera();
  389. CWnd::InvalidateRect( NULL, FALSE );
  390. }
  391. BOOL CLocatorWnd::SetBackgroundDIB( CLgImageDIB *pDIB )
  392. {
  393. CRect rect;
  394. GetClientRect( &rect );
  395. BITMAPINFOHEADER *pBmih = *pDIB;
  396. if( !pBmih || !m_BackgroundDC.Create( pBmih->biWidth, pBmih->biHeight ) )
  397. {
  398. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLocatorWnd::SetBackgroundDIB"), _T("m_BackgroundDC.Create failed") );
  399. }
  400. else
  401. if( pDIB->Draw( m_BackgroundDC, &rect ) && pDIB->Draw( m_FieldDC, &rect ) )
  402. {
  403. InvalidateRect( NULL, FALSE );
  404. }
  405. return m_bBackground = TRUE;
  406. }