-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnob.cpp
executable file
·150 lines (117 loc) · 3.1 KB
/
Knob.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Knob.cpp : implementation file
//
#include "stdafx.h"
#include "math.h"
#include "Knob.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CKnob
CKnob::CKnob()
: m_blFirstPaint(true)
, m_blMouseCapture(false)
, m_fAngle(0.0f)
, m_cxSize(1)
, m_cySize(1)
, m_iRadius(1)
, m_iPos(2048)
{
}
CKnob::~CKnob()
{
}
short CKnob::Position() const
{
return m_iPos;
}
BEGIN_MESSAGE_MAP(CKnob, CButton)
//{{AFX_MSG_MAP(CKnob)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CKnob::SetPosition(CPoint pos)
{
m_pos = pos;
const float PI = 3.1415926536f;
const float fMax = 120 * PI / 180;
const float fMin = 60 * PI / 180;
int dx = m_pos.x - m_ptCentre.x;
int dy = m_pos.y - m_ptCentre.y;
m_fAngle = float(atan2(dy,dx));
if(m_fAngle > fMin && m_fAngle < fMax)
{
if(m_fAngle > (fMin + fMax)/2)
m_fAngle = fMax;
else
m_fAngle = fMin;
}
// Offset to set 0 point
float fPos = m_fAngle + 2*PI - fMax;
if(fPos >= 2*PI)
fPos -= 2*PI;
fPos /= (2*PI - (fMax - fMin));
m_iPos = short(4096 * fPos);
}
/////////////////////////////////////////////////////////////////////////////
// CKnob message handlers
void CKnob::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CBrush* pBrushOld = static_cast<CBrush*>(pDC->SelectStockObject(DKGRAY_BRUSH));
CPen* pPenOld = static_cast<CPen*>(pDC->SelectStockObject(WHITE_PEN));
CRect r(lpDrawItemStruct->rcItem);
// First time we paint, note the size for later & center stick
if(m_blFirstPaint)
{
m_cxSize = r.Width();
m_cySize = r.Height();
m_iRadius = min(m_cxSize, m_cySize) /2;
m_ptCentre = r.CenterPoint();
SetPosition(CPoint(m_ptCentre.x, r.top));
m_blFirstPaint = false;
}
CBrush brBack;
if(brBack.CreateStockObject(LTGRAY_BRUSH))
{
pDC->FillRect(r,&brBack);
}
pDC->Ellipse(m_ptCentre.x-m_iRadius, m_ptCentre.y-m_iRadius
, m_ptCentre.x+m_iRadius, m_ptCentre.y+m_iRadius);
pDC->MoveTo(m_ptCentre);
CPoint ptEnd;
ptEnd.x = m_ptCentre.x + long(m_iRadius * cos(m_fAngle));
ptEnd.y = m_ptCentre.y + long(m_iRadius * sin(m_fAngle));
pDC->LineTo(ptEnd);
pDC->SelectObject(pPenOld);
pDC->SelectObject(pBrushOld);
}
void CKnob::OnLButtonDown(UINT nFlags, CPoint point)
{
SetPosition(point);
SetCapture();
m_blMouseCapture = true;
RedrawWindow(NULL,NULL,RDW_NOERASE | RDW_INVALIDATE);
}
void CKnob::OnLButtonUp(UINT nFlags, CPoint point)
{
if(m_blMouseCapture)
{
SetPosition(point);
ReleaseCapture();
m_blMouseCapture = false;
RedrawWindow(NULL,NULL,RDW_NOERASE | RDW_INVALIDATE);
}
}
void CKnob::OnMouseMove(UINT nFlags, CPoint point)
{
if(m_blMouseCapture)
{
SetPosition(point);
RedrawWindow(NULL,NULL,RDW_NOERASE | RDW_INVALIDATE);
}
}