forked from getdunne/Voxvu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrosshairsControl.cpp
95 lines (85 loc) · 2.88 KB
/
CrosshairsControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "stdafx.h"
#include "CrosshairsControl.h"
#include "Macros.h"
//--------------------------------------------------------------
// Constructor
//--------------------------------------------------------------
CrosshairsControl::CrosshairsControl ()
{
m_color = RGB(0,192,255); // default color is light blue
m_fx = m_fy = 0.5;
m_pDragProc = NULL;
m_pReleaseProc = NULL;
}
//--------------------------------------------------------------
// Draw
//--------------------------------------------------------------
void CrosshairsControl::Draw (CDC* pDC)
{
CRgn rgn;
rgn.CreateRectRgnIndirect(m_bbox);
pDC->SelectClipRgn(&rgn);
pDC->OffsetViewportOrg(m_bbox.left,m_bbox.top);
CPen pen(PS_SOLID,0,m_color);
CPen* pOldPen = pDC->SelectObject(&pen);
int x = ROUND(m_fx * m_bbox.Width());
int y = ROUND(m_fy * m_bbox.Height());
pDC->MoveTo(x,0); pDC->LineTo(x,m_bbox.Height());
pDC->MoveTo(0,y); pDC->LineTo(m_bbox.Width(),y);
pDC->SelectObject(pOldPen);
CBrush brush(m_color);
CRect mr(x,y,x,y);
mr.InflateRect(5,5);
pDC->FrameRect(mr,&brush);
pDC->SetViewportOrg(0,0);
pDC->SelectClipRgn(NULL);
rgn.DeleteObject();
}
//--------------------------------------------------------------
// Hit test: return true only if mouse in indicator area
//--------------------------------------------------------------
BOOL CrosshairsControl::Hit (const CPoint mouse)
{
int x = ROUND(m_fx * m_bbox.Width()) + m_bbox.left;
int y = ROUND(m_fy * m_bbox.Height()) + m_bbox.top;
CRect mr(x,y,x,y);
mr.InflateRect(5,5);
return mr.PtInRect(mouse);
}
//--------------------------------------------------------------
// Click
//--------------------------------------------------------------
void CrosshairsControl::Click (UINT nFlags, CPoint point)
{
m_lastMouse = point;
m_fx = (double)(point.x - m_bbox.left)/m_bbox.Width();
m_fy = (double)(point.y - m_bbox.top)/m_bbox.Height();
if (m_fx < 0.) m_fx = 0.;
if (m_fx > 1.) m_fx = 1.;
if (m_fy < 0.) m_fy = 0.;
if (m_fy > 1.) m_fy = 1.;
if (m_bVisible) m_pOwner->InvalidateRect(m_bbox,FALSE);
}
//--------------------------------------------------------------
// Drag
//--------------------------------------------------------------
void CrosshairsControl::Drag (UINT nFlags, CPoint point)
{
CSize diff = point - m_lastMouse;
m_fx = (double)(point.x - m_bbox.left)/m_bbox.Width();
m_fy = (double)(point.y - m_bbox.top)/m_bbox.Height();
if (m_fx < 0.) m_fx = 0.;
if (m_fx > 1.) m_fx = 1.;
if (m_fy < 0.) m_fy = 0.;
if (m_fy > 1.) m_fy = 1.;
if (m_bVisible) m_pOwner->InvalidateRect(m_bbox,FALSE);
if (m_pDragProc) (*m_pDragProc)(m_pOwner,nFlags,m_fx,m_fy);
m_lastMouse = point;
}
//--------------------------------------------------------------
// Release: pass on to owner
//--------------------------------------------------------------
void CrosshairsControl::Release (UINT nFlags, CPoint point)
{
if (m_pReleaseProc) (*m_pReleaseProc)(m_pOwner,nFlags,point);
}