Debian Codegraph @

src/tracks/playabletrack/notetrack/ui/StretchHandle.cpp

raw
1 /**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 StretchHandle.cpp
6
7 Paul Licameli split from TrackPanel.cpp
8
9 **********************************************************************/
10
11
12
13 #ifdef USE_MIDI
14 #include "WrapAllegro.h"
15
16 #include "StretchHandle.h"
17
18 #include "../../../ui/CommonTrackPanelCell.h"
19 #include "../../../../HitTestResult.h"
20 #include "NoteTrack.h"
21 #include "ProjectAudioIO.h"
22 #include "ProjectHistory.h"
23 #include "../../../../RefreshCode.h"
24 #include "SyncLock.h"
25 #include "../../../../TrackPanelMouseEvent.h"
26 #include "UndoManager.h"
27 #include "ViewInfo.h"
28 #include "../../../../../images/Cursors.h"
29
30 #include <wx/event.h>
31 #include <algorithm>
32
33 StretchHandle::StretchHandle
34 ( const std::shared_ptr<NoteTrack> &pTrack, const StretchState &stretchState )
35 : mpTrack{ pTrack }
36 , mStretchState{ stretchState }
37 {}
38
39 HitTestPreview StretchHandle::HitPreview( StretchEnum stretchMode, bool unsafe )
40 {
41 static auto disabledCursor =
42 ::MakeCursor(wxCURSOR_NO_ENTRY, DisabledCursorXpm, 16, 16);
43 static auto stretchLeftCursor =
44 ::MakeCursor(wxCURSOR_BULLSEYE, StretchLeftCursorXpm, 16, 16);
45 static auto stretchRightCursor =
46 ::MakeCursor(wxCURSOR_BULLSEYE, StretchRightCursorXpm, 16, 16);
47 static auto stretchCursor =
48 ::MakeCursor(wxCURSOR_BULLSEYE, StretchCursorXpm, 16, 16);
49
50 if (unsafe) {
51 return { {}, &*disabledCursor };
52 }
53 else {
54 wxCursor *pCursor = NULL;
55 switch (stretchMode) {
56 default:
57 wxASSERT(false);
58 case stretchLeft:
59 pCursor = &*stretchLeftCursor; break;
60 case stretchCenter:
61 pCursor = &*stretchCursor; break;
62 case stretchRight:
63 pCursor = &*stretchRightCursor; break;
64 }
65 return {
66 XO("Click and drag to stretch selected region."),
67 pCursor
68 };
69 }
70 }
71
72 UIHandlePtr StretchHandle::HitTest
73 (std::weak_ptr<StretchHandle> &holder,
74 const TrackPanelMouseState &st, const AudacityProject *pProject,
75 const std::shared_ptr<NoteTrack> &pTrack)
76 {
77 StretchState stretchState;
78 const wxMouseState &state = st.state;
79
80 // later, we may want a different policy, but for now, stretch is
81 // selected when the cursor is near the center of the track and
82 // within the selection
83 auto &viewInfo = ViewInfo::Get( *pProject );
84
85 if (!pTrack || !pTrack->GetSelected())
86 return {};
87
88 const wxRect &rect = st.rect;
89 int center = rect.y + rect.height / 2;
90 int distance = abs(state.m_y - center);
91 const int yTolerance = 10;
92 wxInt64 leftSel = viewInfo.TimeToPosition(viewInfo.selectedRegion.t0(), rect.x);
93 wxInt64 rightSel = viewInfo.TimeToPosition(viewInfo.selectedRegion.t1(), rect.x);
94 // Something is wrong if right edge comes before left edge
95 wxASSERT(!(rightSel < leftSel));
96 if (!(leftSel <= state.m_x && state.m_x <= rightSel &&
97 distance < yTolerance))
98 return {};
99
100 // find nearest beat to sel0, sel1
101 static const double minPeriod = 0.05; // minimum beat period
102 stretchState.mBeatCenter = { 0, 0 };
103
104 auto t0 = GetT0(*pTrack, viewInfo);
105 auto t1 = GetT1(*pTrack, viewInfo);
106
107 if (t0 >= t1)
108 return {};
109
110 stretchState.mBeat0 = pTrack->NearestBeatTime( t0 );
111 stretchState.mOrigSel0Quantized = stretchState.mBeat0.first;
112
113 stretchState.mBeat1 = pTrack->NearestBeatTime( t1 );
114 stretchState.mOrigSel1Quantized = stretchState.mBeat1.first;
115
116 // If there is not (almost) a beat to stretch that is slower
117 // than 20 beats per second, don't stretch
118 if ( within( stretchState.mBeat0.second,
119 stretchState.mBeat1.second, 0.9 ) ||
120 ( stretchState.mBeat1.first - stretchState.mBeat0.first ) /
121 ( stretchState.mBeat1.second - stretchState.mBeat0.second )
122 < minPeriod )
123 return {};
124
125 auto selStart = viewInfo.PositionToTime( state.m_x, rect.x );
126 selStart = std::max(t0, std::min(t1, selStart));
127 stretchState.mBeatCenter = pTrack->NearestBeatTime( selStart );
128 if ( within( stretchState.mBeat0.second,
129 stretchState.mBeatCenter.second, 0.1 ) ) {
130 stretchState.mMode = stretchLeft;
131 stretchState.mLeftBeats = 0;
132 stretchState.mRightBeats =
133 stretchState.mBeat1.second - stretchState.mBeat0.second;
134 }
135 else if ( within( stretchState.mBeat1.second,
136 stretchState.mBeatCenter.second, 0.1 ) ) {
137 stretchState.mMode = stretchRight;
138 stretchState.mLeftBeats =
139 stretchState.mBeat1.second - stretchState.mBeat0.second;
140 stretchState.mRightBeats = 0;
141 }
142 else {
143 stretchState.mMode = stretchCenter;
144 stretchState.mLeftBeats =
145 stretchState.mBeat1.second - stretchState.mBeatCenter.second;
146 stretchState.mRightBeats =
147 stretchState.mBeatCenter.second - stretchState.mBeat0.second;
148 }
149
150 auto result = std::make_shared<StretchHandle>( pTrack, stretchState );
151 result = AssignUIHandlePtr(holder, result);
152 return result;
153 }
154
155 StretchHandle::~StretchHandle()
156 {
157 }
158
159 std::shared_ptr<const Track> StretchHandle::FindTrack() const
160 {
161 return mpTrack;
162 }
163
164 UIHandle::Result StretchHandle::Click
165 (const TrackPanelMouseEvent &evt, AudacityProject *pProject)
166 {
167 using namespace RefreshCode;
168 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
169 if ( unsafe )
170 return Cancelled;
171
172 const wxMouseEvent &event = evt.event;
173
174 if (event.LeftDClick() ||
175 !event.LeftDown() ||
176 evt.pCell == NULL)
177 return Cancelled;
178
179
180 mLeftEdge = evt.rect.GetLeft();
181 auto &viewInfo = ViewInfo::Get( *pProject );
182
183 viewInfo.selectedRegion.setTimes
184 ( mStretchState.mBeat0.first, mStretchState.mBeat1.first );
185
186 // Full refresh since the label area may need to indicate
187 // newly selected tracks. (I'm really not sure if the label area
188 // needs to be refreshed or how to just refresh non-label areas.-RBD)
189
190 return RefreshAll;
191 }
192
193 UIHandle::Result StretchHandle::Drag
194 (const TrackPanelMouseEvent &evt, AudacityProject *pProject)
195 {
196 using namespace RefreshCode;
197 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
198 if (unsafe) {
199 this->Cancel(pProject);
200 return RefreshAll | Cancelled;
201 }
202
203 const wxMouseEvent &event = evt.event;
204 const int x = event.m_x;
205
206 Channel *clickedChannel = nullptr;
207 if (evt.pCell)
208 clickedChannel =
209 static_cast<CommonChannelCell*>(evt.pCell.get())->FindChannel().get();
210
211 if (clickedChannel == nullptr && mpTrack != nullptr)
212 clickedChannel = mpTrack.get();
213 Stretch(pProject, x, mLeftEdge, clickedChannel);
214 return RefreshAll;
215 }
216
217 HitTestPreview StretchHandle::Preview
218 (const TrackPanelMouseState &, AudacityProject *pProject)
219 {
220 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
221 return HitPreview( mStretchState.mMode, unsafe );
222 }
223
224 UIHandle::Result StretchHandle::Release
225 (const TrackPanelMouseEvent &, AudacityProject *pProject,
226 wxWindow *)
227 {
228 using namespace RefreshCode;
229 if (!mpTrack)
230 return RefreshNone;
231
232 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
233 if (unsafe) {
234 this->Cancel(pProject);
235 return RefreshAll | Cancelled;
236 }
237
238 bool left = mStretchState.mMode == stretchLeft;
239 bool right = mStretchState.mMode == stretchRight;
240 auto &viewInfo = ViewInfo::Get( *pProject );
241 if (SyncLockState::Get(*pProject).IsSyncLocked() && (left || right)) {
242 for (auto track : SyncLock::Group(*mpTrack)) {
243 if (track != mpTrack.get()) {
244 if (left) {
245 auto origT0 = mStretchState.mOrigSel0Quantized;
246 auto diff = viewInfo.selectedRegion.t0() - origT0;
247 if (diff > 0)
248 track->SyncLockAdjust(origT0 + diff, origT0);
249 else
250 track->SyncLockAdjust(origT0, origT0 - diff);
251 track->ShiftBy(diff);
252 }
253 else {
254 auto origT1 = mStretchState.mOrigSel1Quantized;
255 auto diff = viewInfo.selectedRegion.t1() - origT1;
256 track->SyncLockAdjust(origT1, origT1 + diff);
257 }
258 }
259 }
260 }
261
262 /* i18n-hint: (noun) The track that is used for MIDI notes which can be
263 dragged to change their duration.*/
264 ProjectHistory::Get( *pProject ).PushState(XO("Stretch Note Track"),
265 /* i18n-hint: In the history list, indicates a MIDI note has
266 been dragged to change its duration (stretch it). Using either past
267 or present tense is fine here. If unsure, go for whichever is
268 shorter.*/
269 XO("Stretch"),
270 UndoPush::CONSOLIDATE);
271 return RefreshAll;
272 }
273
274 UIHandle::Result StretchHandle::Cancel(AudacityProject *pProject)
275 {
276 ProjectHistory::Get( *pProject ).RollbackState();
277 return RefreshCode::RefreshNone;
278 }
279
280 double StretchHandle::GetT0(const Track &track, const ViewInfo &viewInfo)
281 {
282 return std::max(track.GetStartTime(), viewInfo.selectedRegion.t0());
283 }
284
285 double StretchHandle::GetT1(const Track &track, const ViewInfo &viewInfo)
286 {
287 return std::min(track.GetEndTime(), viewInfo.selectedRegion.t1());
288 }
289
290 void StretchHandle::Stretch(AudacityProject *pProject, int mouseXCoordinate, int trackLeftEdge,
291 Channel *pChannel)
292 {
293 auto &viewInfo = ViewInfo::Get( *pProject );
294
295 if (pChannel == nullptr && mpTrack != nullptr)
296 pChannel = mpTrack.get();
297
298 if (const auto pNt = dynamic_cast<NoteTrack*>(pChannel)) {
299 auto &nt = *pNt;
300 double moveto =
301 std::max(0.0, viewInfo.PositionToTime(mouseXCoordinate, trackLeftEdge));
302
303 double dur, left_dur, right_dur;
304
305 // check to make sure tempo is not higher than 20 beats per second
306 // (In principle, tempo can be higher, but not infinity.)
307 double minPeriod = 0.05; // minimum beat period
308
309 // make sure target duration is not too short
310 // Take quick exit if so, without changing the selection.
311 auto t0 = mStretchState.mBeat0.first;
312 auto t1 = mStretchState.mBeat1.first;
313 switch ( mStretchState.mMode ) {
314 case stretchLeft: {
315 dur = t1 - moveto;
316 if (dur < mStretchState.mRightBeats * minPeriod)
317 return;
318 nt.StretchRegion
319 ( mStretchState.mBeat0, mStretchState.mBeat1, dur );
320 nt.ChannelGroup::ShiftBy(moveto - t0);
321 mStretchState.mBeat0.first = moveto;
322 viewInfo.selectedRegion.setT0(moveto);
323 break;
324 }
325 case stretchRight: {
326 dur = moveto - t0;
327 if (dur < mStretchState.mLeftBeats * minPeriod)
328 return;
329 nt.StretchRegion
330 ( mStretchState.mBeat0, mStretchState.mBeat1, dur );
331 viewInfo.selectedRegion.setT1(moveto);
332 mStretchState.mBeat1.first = moveto;
333 break;
334 }
335 case stretchCenter: {
336 moveto = std::max(t0, std::min(t1, moveto));
337 left_dur = moveto - t0;
338 right_dur = t1 - moveto;
339 if ( left_dur < mStretchState.mLeftBeats * minPeriod ||
340 right_dur < mStretchState.mRightBeats * minPeriod )
341 return;
342 nt.StretchRegion
343 ( mStretchState.mBeatCenter, mStretchState.mBeat1, right_dur );
344 nt.StretchRegion
345 ( mStretchState.mBeat0, mStretchState.mBeatCenter, left_dur );
346 mStretchState.mBeatCenter.first = moveto;
347 break;
348 }
349 default:
350 wxASSERT(false);
351 break;
352 }
353 };
354 }
355 #endif