-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindicatorwindow.cpp
193 lines (160 loc) · 3.81 KB
/
indicatorwindow.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <FL/fl.h>
#include <FL/fl_draw.h>
#include <algorithm>
#include <float.h>
#ifdef _DEBUG
#include <assert.h>
#endif // _DEBUG
#include "indicatorwindow.h"
const float IndicatorWindow::ks_fIndicatorNotFound = -FLT_MAX;
IndicatorWindow::IndicatorWindow(int x, int y, int w, int h, const char* lable) :
Fl_Double_Window(x, y, w, h, lable),
m_fMin(0.0f), m_fMax(1.0f),
m_fRangeMarkerMin(0.0f), m_fRangeMarkerMax(0.0f),
m_bRangeMarkerEnabled(false),
m_fFloatingIndicator(0.0f),
m_iSelectedIndicator(-1)
{
box(FL_NO_BOX);
}
void IndicatorWindow::draw()
{
fl_color(255, 255, 255);
fl_rectf(0, 0, w(), h());
// draw range
if (m_bRangeMarkerEnabled) {
fl_color(200,200,200);
int min = toWindowX(m_fRangeMarkerMin);
int max = toWindowX(m_fRangeMarkerMax);
fl_rectf(min, 0, max - min, h());
}
// draw indicators
fl_color(0, 0, 255);
for (int i = 0; i < m_fvIndicators.size(); ++i) {
int x = toWindowX(m_fvIndicators[i]);
fl_line(x, 0, x, h());
}
// draw the floating indicator
if (m_iSelectedIndicator < 0)
fl_color(255, 0, 0);
else
fl_color(0, 255, 0);
int x = toWindowX(m_fFloatingIndicator);
fl_line(x, 0, x, h());
}
int IndicatorWindow::handle(int iEvent)
{
switch (iEvent) {
case FL_PUSH:
switch (Fl::event_button()) {
case 1: // left button
{
// find the indicator and move the floating indicator there
int x = Fl::event_x();
float fIndicator = findIndicator(x, 5);
if (fIndicator != ks_fIndicatorNotFound) {
floatingIndicator(fIndicator);
redraw();
}
}
break;
}
return 1;
case FL_RELEASE:
switch (Fl::event_button()) {
case 1: // left button
// do the callback function so that the parent can update other
// widgets accordingly
do_callback();
break;
}
return 1;
default:
break;
}
return Fl_Double_Window::handle(iEvent);
}
void IndicatorWindow::range(float fMin, float fMax)
{
m_fMin = fMin;
m_fMax = fMax;
#ifdef _DEBUG
assert(m_fMax > m_fMin);
#endif // _DEBUG
}
void IndicatorWindow::addIndicator(float f)
{
m_fvIndicators.push_back(f);
std::sort(m_fvIndicators.begin(), m_fvIndicators.end());
}
void IndicatorWindow::removeIndicator(float f)
{
for (std::vector<float>::iterator it = m_fvIndicators.begin(); it != m_fvIndicators.end(); ++it) {
if (*it == f) {
m_fvIndicators.erase(it);
break;
}
}
}
void IndicatorWindow::clearIndicators()
{
m_fvIndicators.clear();
}
void IndicatorWindow::floatingIndicator(float f)
{
m_fFloatingIndicator = f;
m_iSelectedIndicator = -1;
for (int i = 0; i < m_fvIndicators.size(); ++i) {
if (m_fFloatingIndicator == m_fvIndicators[i])
m_iSelectedIndicator = i;
}
}
float IndicatorWindow::floatingIndicator() const
{
return m_fFloatingIndicator;
}
bool IndicatorWindow::floatingIndicatorSnapped() const
{
return (m_iSelectedIndicator >= 0);
}
float IndicatorWindow::findIndicator(int x, int iPickWindowSize) const
{
int iMinDist = iPickWindowSize;
float fInd = ks_fIndicatorNotFound;
for (int i = 0; i < m_fvIndicators.size(); ++i) {
int iIndX = toWindowX(m_fvIndicators[i]);
int iDist = abs(x - iIndX);
if (iDist * 2 <= iPickWindowSize) {
if (iDist < iMinDist) {
iMinDist = iDist;
fInd = m_fvIndicators[i];
}
}
}
return fInd;
}
bool IndicatorWindow::rangeMarkerEnabled() const
{
return m_bRangeMarkerEnabled;
}
void IndicatorWindow::rangeMarkerEnabled(bool RangeMarkerEnabled)
{
m_bRangeMarkerEnabled = RangeMarkerEnabled;
}
void IndicatorWindow::rangeMarkerRange(float fRangeMin, float fRangeMax)
{
m_fRangeMarkerMin = fRangeMin;
m_fRangeMarkerMax = fRangeMax;
}
float IndicatorWindow::fromWindowX(int x) const
{
if (w() > 1)
return (float)x * (m_fMax - m_fMin) / (float)(w() - 1) + m_fMin;
return 0.0f;
}
int IndicatorWindow::toWindowX(float f) const
{
if (w() > 1)
return (int)((f - m_fMin) * (float)(w() - 1) / (m_fMax - m_fMin) + 0.5f);
return 0;
}