Krotos Modules 3
Loading...
Searching...
No Matches
AreaSelectionComp.cpp
Go to the documentation of this file.
1namespace krotos
2{
3 AreaSelectionComp::AreaSelectionComp(bool drawBackground, bool drawMarqueeBackground, bool useEndPoint)
4 : m_drawBackground(drawBackground), m_drawMarqueeBackground(drawMarqueeBackground), m_useEndPoint(useEndPoint)
5 {
6 }
7
9
11
13
14 void AreaSelectionComp::paint(juce::Graphics& g)
15 {
16 if (!isEnabled())
17 return;
18
19 float transaprencyCoef = 0.1f;
20 float markerLineWidth = 1.f;
21
22 int width = getWidth();
23 int height = getHeight();
24
25 int drawSelectionStart = m_selectionStart;
26
28 {
29 if (drawSelectionStart >= width)
30 drawSelectionStart = width - 2; // keep the line in the drawable area
31
32 int drawSelectionEnd = m_selectionEnd;
33
35 g.fillAll(Colour(109, 109, 109));
36
37 if (inAreaSelectMode == true)
38 {
39 g.setColour(Colours::red);
40 drawZoomedLine(g, {float(drawSelectionStart), 0.f, float(drawSelectionStart), float(height)},
41 markerLineWidth);
42
43 if (m_useEndPoint == true)
44 {
45 g.setColour(Colours::green);
46 drawZoomedLine(g, {float(drawSelectionEnd), 0.f, float(drawSelectionEnd), float(height)},
47 markerLineWidth);
48
49 if (drawSelectionStart < drawSelectionEnd)
50 {
51 g.setColour(Colour::fromFloatRGBA(1.0f, 1.0f, 1.0f, transaprencyCoef));
52 Rectangle<int> zoomedRect =
53 Rectangle<int>(drawSelectionStart, 0, drawSelectionEnd - drawSelectionStart, height);
54 drawZoomedRect(g, zoomedRect.toFloat());
55 }
56
57 else
58 {
59 g.setColour(Colour::fromFloatRGBA(1.0f, 0.0f, 0.0f, transaprencyCoef));
60 Rectangle<int> zoomedRect =
61 Rectangle<int>(drawSelectionEnd, 0, drawSelectionStart - drawSelectionEnd, height);
62 // Rectangle<int> zoomedRect = Rectangle<int>(drawSelectionStart, 0, drawSelectionEnd -
63 // drawSelectionStart, height);
64 drawZoomedRect(g, zoomedRect.toFloat());
65 }
66 }
67 }
68 }
69 else if (m_waveformMode == WaveformMode::onlyStartLine) // granular
70 {
71 if (drawSelectionStart >= width)
72 drawSelectionStart = width - 2; // keep the line in the drawable area
73
75 g.fillAll(Colour(109, 109, 109));
76
77 if (inAreaSelectMode == true)
78 {
79 g.setColour(Colours::red);
80 drawZoomedLine(g, {float(drawSelectionStart), 0.f, float(drawSelectionStart), float(height)},
81 markerLineWidth);
82 }
83 }
84 else if (m_waveformMode == WaveformMode::noLine) // vehicle
85 {
87 g.fillAll(Colour(109, 109, 109));
88 }
89 }
90
91 void AreaSelectionComp::setStartPointInPercentage(float percentage, float nonModulatedPercentage)
92 {
93
94 float newValue = percentage;
95 if (m_dragging == true && nonModulatedPercentage != -1.0f)
96 {
97 newValue = nonModulatedPercentage;
98 }
99 newValue = jlimit<float>(0.f, 1.f, newValue);
100 m_startPosPercentage = newValue;
101 m_selectionStart = int(std::round(newValue * getWidth()));
102
103 if (m_useEndPoint == true)
104 {
106 }
107 repaint();
108 }
109
111 {
112 percentage = jlimit<float>(0.f, 1.f, percentage);
113 if (m_useEndPoint == true)
114 {
115 m_endPosPercentage = percentage;
116 m_selectionEnd = int(std::round(percentage * (getWidth() - m_lineWidth)));
118 }
119 else
120 {
122 }
123
124 repaint();
125 }
126
128 {
129 m_startPosPercentage = static_cast<float>(m_selectionStart) / (getWidth());
131 }
132
134 {
135 m_endPosPercentage = static_cast<float>(m_selectionEnd) / (getWidth() - m_lineWidth);
136 return m_endPosPercentage;
137 }
138
140 {
141 inAreaSelectMode = toggleState;
142
143 if (inAreaSelectMode == true)
144 {
145 setEnabled(true);
146 }
147 else
148 {
149 setEnabled(false);
150 }
151 repaint();
152 }
153
154 void AreaSelectionComp::determineMinSelectionWidth(float sampleRate, float numSamples)
155 {
156 int width = getWidth();
157
158 m_minSelectionWidth = int((width * sampleRate) / numSamples);
159 }
160
161 void AreaSelectionComp::modifierKeysChanged(const ModifierKeys& modifiers)
162 {
163 if (modifiers.isShiftDown())
164 m_shiftKeyPressed = true;
165 else
166 m_shiftKeyPressed = false;
167 }
168
169 void AreaSelectionComp::mouseDown(const MouseEvent& event)
170 {
171 auto zf = getHorizontalZoomFactor();
172 auto zc = getHorizontalZoomCenter();
173 auto width = float(getWidth());
174 auto x = float(event.x);
175 // Position of the mouse
176 int mx = static_cast<int>((x - zc * width) * zf + zc * width);
177 // Interval of grabbing at each side
178
179 if (isEnabled() == true)
180 {
181 Range<int> leftMarqueeRange(m_selectionStart - m_grabAreaWidth, m_selectionStart + m_grabAreaWidth);
182 Range<int> constraintRangeA(0, getWidth() - m_lineWidth);
183 leftMarqueeRange = constraintRangeA.constrainRange(leftMarqueeRange);
184
185 Range<int> rightMarqueeRange(m_selectionEnd - m_grabAreaWidth, m_selectionEnd + m_grabAreaWidth);
186 rightMarqueeRange = constraintRangeA.constrainRange(rightMarqueeRange);
187
188 if (leftMarqueeRange.contains(mx))
189 {
191 m_rightBarWasSelected = false;
192 }
193 else if (rightMarqueeRange.contains(mx))
194 {
195 m_leftBarWasSelected = false;
197 }
198 // Case of Indicators overlapping
199 if (leftMarqueeRange.contains(mx) && rightMarqueeRange.contains(mx))
200 {
201 if (mx < m_selectionStart)
202 {
204 m_rightBarWasSelected = false;
205 }
206 else
207 {
208 m_leftBarWasSelected = false;
210 }
211 }
212 }
213 }
214
215 void AreaSelectionComp::mouseDrag(const MouseEvent& event)
216 {
217 if (isEnabled() == true)
218 {
219 auto zf = getHorizontalZoomFactor();
220 auto zc = getHorizontalZoomCenter();
221 auto width = float(getWidth());
222 auto x = float(event.x);
223 // position of the mouse
224 int mx = static_cast<int>((x - zc * width) * zf + zc * width);
225
226 // you are holding the left bar
228 {
229 // Check start is not behind 0
230 if (mx < 0)
231 {
233 }
234 else
235 {
236 m_selectionStart = mx;
237
238 // dont allow start to exceed end , sampler mode
240 {
242 {
244 }
245 }
246 }
247 }
248 // you are holding the right bar, sampler mode
250 {
251 m_selectionEnd = mx;
252 // dont allow end to exceed right border
253 if (m_selectionEnd > getWidth() - m_lineWidth)
254 {
255 m_selectionEnd = getWidth() - m_lineWidth;
256 }
257 // dont allow end to exceed start
260 }
261 }
262
265 repaint();
266 }
267
268 void AreaSelectionComp::mouseUp(const MouseEvent&)
269 {
270 if (isEnabled() == true)
271 {
272 m_shiftDragging = false;
275 }
276
277 m_dragging = false;
278 m_leftBarWasSelected = false;
279 m_rightBarWasSelected = false;
280 }
281
283} // namespace krotos
Definition AreaSelectionComp.h:15
virtual void sampleAreaChanged(AreaSelectionComp *areaSelectionComp, float startPos, float endPos)=0
int m_selectionStart
Definition AreaSelectionComp.h:62
bool m_leftBarWasSelected
Definition AreaSelectionComp.h:75
~AreaSelectionComp() override
Definition AreaSelectionComp.cpp:8
void modifierKeysChanged(const ModifierKeys &modifiers) override
Definition AreaSelectionComp.cpp:161
void setStartPointInPercentage(float percentage, float nonModulatedPercentage=-1)
Definition AreaSelectionComp.cpp:91
ListenerList< Listener > m_listeners
Definition AreaSelectionComp.h:46
bool m_shiftDragging
Definition AreaSelectionComp.h:55
void setEndPointInPercentage(float percentage)
Definition AreaSelectionComp.cpp:110
float m_endPosPercentage
Definition AreaSelectionComp.h:66
AreaSelectionComp(bool drawBackground, bool drawMarqueeBackground, bool useEndPoint)
Definition AreaSelectionComp.cpp:3
int m_grabAreaWidth
Definition AreaSelectionComp.h:59
float getStartPosInPercentage()
Definition AreaSelectionComp.cpp:127
float getEndPosInPercentage()
Definition AreaSelectionComp.cpp:133
int m_selectionEnd
Definition AreaSelectionComp.h:63
int m_minSelectionWidth
Definition AreaSelectionComp.h:73
void addListener(Listener *Listener)
Definition AreaSelectionComp.cpp:10
const int m_lineWidth
Definition AreaSelectionComp.h:70
bool m_shiftKeyPressed
Definition AreaSelectionComp.h:54
bool m_drawBackground
Definition AreaSelectionComp.h:50
void setWaveformMode(WaveformMode mode)
Definition AreaSelectionComp.cpp:282
bool m_rightBarWasSelected
Definition AreaSelectionComp.h:76
bool m_useEndPoint
Definition AreaSelectionComp.h:52
WaveformMode m_waveformMode
Definition AreaSelectionComp.h:79
void mouseDown(const MouseEvent &event) override
Definition AreaSelectionComp.cpp:169
void toggleAreaOrControl(bool toggleState)
Definition AreaSelectionComp.cpp:139
void removeListener(Listener *Listener)
Definition AreaSelectionComp.cpp:12
void mouseUp(const MouseEvent &event) override
Definition AreaSelectionComp.cpp:268
WaveformMode
Definition AreaSelectionComp.h:7
void determineMinSelectionWidth(float sampleRate, float numSamples)
Definition AreaSelectionComp.cpp:154
bool m_dragging
Definition AreaSelectionComp.h:77
void mouseDrag(const MouseEvent &event) override
Definition AreaSelectionComp.cpp:215
void paint(Graphics &g) override
Definition AreaSelectionComp.cpp:14
int m_selectionWidth
Definition AreaSelectionComp.h:61
bool inAreaSelectMode
Definition AreaSelectionComp.h:56
float m_startPosPercentage
Definition AreaSelectionComp.h:65
void drawZoomedLine(Graphics &g, const Line< float > &line, float thickness) const
Definition ZoomableComponent.cpp:46
void drawZoomedRect(Graphics &g, const Rectangle< float > &rect) const
Definition ZoomableComponent.cpp:24
float getHorizontalZoomFactor() const
Definition ZoomableComponent.h:36
float getHorizontalZoomCenter() const
Definition ZoomableComponent.h:37
Definition AirAbsorptionFilter.cpp:2