From bbafb8f137bd9f8a18a7242e2628d309a1f8f991 Mon Sep 17 00:00:00 2001 From: nick_m Date: Sun, 17 May 2015 00:23:25 +1000 Subject: [PATCH] Cleanup relative snap code. --- gtk2_ardour/editor_drag.cc | 128 ++++++++++++------------------------- gtk2_ardour/editor_drag.h | 23 +++---- 2 files changed, 53 insertions(+), 98 deletions(-) diff --git a/gtk2_ardour/editor_drag.cc b/gtk2_ardour/editor_drag.cc index 065eecbf7d..f7f4cd2873 100644 --- a/gtk2_ardour/editor_drag.cc +++ b/gtk2_ardour/editor_drag.cc @@ -226,6 +226,7 @@ Drag::Drag (Editor* e, ArdourCanvas::Item* i, bool trackview_only) , _raw_grab_frame (0) , _grab_frame (0) , _last_pointer_frame (0) + , _snap_delta (0) { } @@ -360,6 +361,16 @@ Drag::current_pointer_y () const return _drags->current_pointer_y () - _editor->get_trackview_group()->canvas_origin().y; } +void +Drag::setup_snap_delta (framepos_t pos) +{ + if (_editor->snap_delta () == SnapRelative) { + framepos_t temp = pos; + _editor->snap_to (temp); + _snap_delta = temp - pos; + } +} + bool Drag::motion_handler (GdkEvent* event, bool from_autoscroll) { @@ -560,7 +571,6 @@ RegionMotionDrag::RegionMotionDrag (Editor* e, ArdourCanvas::Item* i, RegionView , _ndropzone (0) , _pdropzone (0) , _ddropzone (0) - , _snap_delta (0) { DEBUG_TRACE (DEBUG::Drags, "New RegionMotionDrag\n"); } @@ -569,12 +579,7 @@ void RegionMotionDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor) { Drag::start_grab (event, cursor); - - if (_editor->snap_delta () == SnapRelative) { - framepos_t temp = _last_frame_position; - _editor->snap_to_with_modifier (temp, event, RoundUpMaybe); - _snap_delta = temp - _last_frame_position; - } + setup_snap_delta (_last_frame_position); if (Keyboard::modifier_state_contains (event->button.state, Keyboard::TertiaryModifier)) { _single_axis = true; @@ -596,7 +601,7 @@ RegionMotionDrag::compute_x_delta (GdkEvent const * event, framepos_t* pending_r /* compute the amount of pointer motion in frames, and where the region would be if we moved it by that much. */ - *pending_region_position = adjusted_frame (_drags->current_pointer_frame () + _snap_delta, event, true); + *pending_region_position = adjusted_frame (_drags->current_pointer_frame () + snap_delta (), event, true); framepos_t sync_frame; framecnt_t sync_offset; @@ -612,7 +617,7 @@ RegionMotionDrag::compute_x_delta (GdkEvent const * event, framepos_t* pending_r _editor->snap_to_with_modifier (sync_frame, event); - *pending_region_position = _primary->region()->adjust_to_sync (sync_frame) - _snap_delta; + *pending_region_position = _primary->region()->adjust_to_sync (sync_frame) - snap_delta (); } else { *pending_region_position = _last_frame_position; @@ -2355,15 +2360,6 @@ NoteResizeDrag::start_grab (GdkEvent* event, Gdk::Cursor* /*ignored*/) double temp; temp = region->snap_to_pixel(cnote->x0 ()); _snap_delta = temp - cnote->x0 (); - /* - if (at_front) { - temp = region->snap_to_pixel(cnote->x0 ()); - _snap_delta = temp - cnote->x0 (); - } else { - temp = region->snap_to_pixel(cnote->x1 ()); - _snap_delta = temp - cnote->x1 (); - } - */ } _item->grab (); @@ -2601,7 +2597,6 @@ TrimDrag::TrimDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, listregion()->length() / speed); framepos_t const pf = adjusted_current_frame (event); - - if (_editor->snap_delta () == SnapRelative) { - framepos_t temp = region_start; - _editor->snap_to_with_modifier (temp, event, RoundUpMaybe); - _snap_delta = temp - region_start; - } + setup_snap_delta (region_start); if (Keyboard::modifier_state_equals (event->button.state, Keyboard::PrimaryModifier)) { /* Move the contents of the region around without changing the region bounds */ @@ -2693,8 +2683,8 @@ TrimDrag::motion (GdkEvent* event, bool first_move) if (tv && tv->is_track()) { speed = tv->track()->speed(); } - framecnt_t adj_frame = adjusted_frame (_drags->current_pointer_frame () + _snap_delta, event, true); - framecnt_t dt = adj_frame - raw_grab_frame () + _pointer_frame_offset - _snap_delta; + framecnt_t adj_frame = adjusted_frame (_drags->current_pointer_frame () + snap_delta (), event, true); + framecnt_t dt = adj_frame - raw_grab_frame () + _pointer_frame_offset - snap_delta (); if (first_move) { @@ -3244,7 +3234,6 @@ CursorDrag::CursorDrag (Editor* e, EditorCursor& c, bool s) : Drag (e, &c.track_canvas_item(), false) , _cursor (c) , _stop (s) - , _snap_delta (0) { DEBUG_TRACE (DEBUG::Drags, "New CursorDrag\n"); } @@ -3279,16 +3268,11 @@ void CursorDrag::start_grab (GdkEvent* event, Gdk::Cursor* c) { Drag::start_grab (event, c); - - if (_editor->snap_delta () == SnapRelative) { - framepos_t temp = _editor->playhead_cursor->current_frame (); - _editor->snap_to_with_modifier (temp, event); - _snap_delta = temp - _editor->playhead_cursor->current_frame (); - } + setup_snap_delta (_editor->playhead_cursor->current_frame ()); _grab_zoom = _editor->samples_per_pixel; - framepos_t where = _editor->canvas_event_sample (event) + _snap_delta; + framepos_t where = _editor->canvas_event_sample (event) + snap_delta (); _editor->snap_to_with_modifier (where, event); @@ -3326,16 +3310,16 @@ CursorDrag::start_grab (GdkEvent* event, Gdk::Cursor* c) } } - fake_locate (where - _snap_delta); + fake_locate (where - snap_delta ()); } void CursorDrag::motion (GdkEvent* event, bool) { - framepos_t where = _editor->canvas_event_sample (event) + _snap_delta; + framepos_t where = _editor->canvas_event_sample (event) + snap_delta (); _editor->snap_to_with_modifier (where, event); if (where != last_pointer_frame()) { - fake_locate (where - _snap_delta); + fake_locate (where - snap_delta ()); } } @@ -3375,7 +3359,6 @@ CursorDrag::aborted (bool) FadeInDrag::FadeInDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, list const & v) : RegionDrag (e, i, p, v) - , _snap_delta (0) { DEBUG_TRACE (DEBUG::Drags, "New FadeInDrag\n"); } @@ -3387,12 +3370,7 @@ FadeInDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor) AudioRegionView* arv = dynamic_cast (_primary); boost::shared_ptr const r = arv->audio_region (); - - if (_editor->snap_delta () == SnapRelative) { - framepos_t temp = r->position(); - _editor->snap_to_with_modifier (temp, event); - _snap_delta = temp - r->position(); - } + setup_snap_delta (r->position ()); show_verbose_cursor_duration (r->position(), r->position() + r->fade_in()->back()->when, 32); } @@ -3410,9 +3388,9 @@ FadeInDrag::motion (GdkEvent* event, bool) { framecnt_t fade_length; - framepos_t pos = _editor->canvas_event_sample (event) + _snap_delta; + framepos_t pos = _editor->canvas_event_sample (event) + snap_delta (); _editor->snap_to_with_modifier (pos, event); - pos -= _snap_delta; + pos -= snap_delta (); boost::shared_ptr region = boost::dynamic_pointer_cast (_primary->region ()); @@ -3446,9 +3424,9 @@ FadeInDrag::finished (GdkEvent* event, bool movement_occurred) } framecnt_t fade_length; - framepos_t pos = _editor->canvas_event_sample (event) + _snap_delta; + framepos_t pos = _editor->canvas_event_sample (event) + snap_delta (); _editor->snap_to_with_modifier (pos, event); - pos -= _snap_delta; + pos -= snap_delta (); boost::shared_ptr region = boost::dynamic_pointer_cast (_primary->region ()); @@ -3510,12 +3488,7 @@ FadeOutDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor) AudioRegionView* arv = dynamic_cast (_primary); boost::shared_ptr r = arv->audio_region (); - - if (_editor->snap_delta () == SnapRelative) { - framepos_t temp = r->last_frame (); - _editor->snap_to_with_modifier (temp, event); - _snap_delta = temp - r->last_frame(); - } + setup_snap_delta (r->last_frame ()); show_verbose_cursor_duration (r->last_frame() - r->fade_out()->back()->when, r->last_frame()); } @@ -3533,9 +3506,9 @@ FadeOutDrag::motion (GdkEvent* event, bool) { framecnt_t fade_length; - framepos_t pos = _editor->canvas_event_sample (event) + _snap_delta; + framepos_t pos = _editor->canvas_event_sample (event) + snap_delta (); _editor->snap_to_with_modifier (pos, event); - pos -= _snap_delta; + pos -= snap_delta (); boost::shared_ptr region = boost::dynamic_pointer_cast (_primary->region ()); @@ -3570,9 +3543,9 @@ FadeOutDrag::finished (GdkEvent* event, bool movement_occurred) framecnt_t fade_length; - framepos_t pos = _editor->canvas_event_sample (event) + _snap_delta; + framepos_t pos = _editor->canvas_event_sample (event) + snap_delta (); _editor->snap_to_with_modifier (pos, event); - pos -= _snap_delta; + pos -= snap_delta (); boost::shared_ptr region = boost::dynamic_pointer_cast (_primary->region ()); @@ -4027,14 +4000,7 @@ ControlPointDrag::start_grab (GdkEvent* event, Gdk::Cursor* /*cursor*/) _fixed_grab_y = _point->get_y(); framepos_t pos = _editor->pixel_to_sample (_fixed_grab_x); - - if (_editor->snap_delta () == SnapRelative) { - framepos_t temp = pos; - if (!_x_constrained) { - _editor->snap_to_with_modifier (temp, event); - } - _snap_delta = temp - pos; - } + setup_snap_delta (pos); float const fraction = 1 - (_point->get_y() / _point->line().height()); @@ -4088,13 +4054,13 @@ ControlPointDrag::motion (GdkEvent* event, bool) cy = max (0.0, cy); cy = min ((double) _point->line().height(), cy); - framepos_t cx_frames = _editor->pixel_to_sample (cx) + _snap_delta; + framepos_t cx_frames = _editor->pixel_to_sample (cx) + snap_delta (); if (!_x_constrained) { _editor->snap_to_with_modifier (cx_frames, event); } - cx_frames -= _snap_delta; + cx_frames -= snap_delta (); cx_frames = min (cx_frames, _point->line().maximum_time()); float const fraction = 1.0 - (cy / _point->line().height()); @@ -4495,7 +4461,6 @@ RubberbandSelectDrag::aborted (bool) TimeFXDrag::TimeFXDrag (Editor* e, ArdourCanvas::Item* i, RegionView* p, std::list const & v) : RegionDrag (e, i, p, v) - , _snap_delta (0) { DEBUG_TRACE (DEBUG::Drags, "New TimeFXDrag\n"); } @@ -4508,11 +4473,7 @@ TimeFXDrag::start_grab (GdkEvent* event, Gdk::Cursor* cursor) _editor->get_selection().add (_primary); framepos_t where = _primary->region()->position(); - if (_editor->snap_delta () == SnapRelative) { - framepos_t temp = where; - _editor->snap_to_with_modifier (temp, event); - _snap_delta = temp - where; - } + setup_snap_delta (where); show_verbose_cursor_duration (where, adjusted_current_frame (event), 0); } @@ -4526,9 +4487,9 @@ TimeFXDrag::motion (GdkEvent* event, bool) pair const tv = _editor->trackview_by_y_position (grab_y()); int layer = tv.first->layer_display() == Overlaid ? 0 : tv.second; int layers = tv.first->layer_display() == Overlaid ? 1 : cv->layers(); - framepos_t pf = _editor->canvas_event_sample (event) + _snap_delta; + framepos_t pf = _editor->canvas_event_sample (event) + snap_delta (); _editor->snap_to_with_modifier (pf, event); - pf -= _snap_delta; + pf -= snap_delta (); if (pf > rv->region()->position()) { rv->get_time_axis_view().show_timestretch (rv->region()->position(), pf, layers, layer); @@ -5200,7 +5161,6 @@ NoteDrag::NoteDrag (Editor* e, ArdourCanvas::Item* i) : Drag (e, i) , _cumulative_dx (0) , _cumulative_dy (0) - , _snap_delta (0) { DEBUG_TRACE (DEBUG::Drags, "New NoteDrag\n"); @@ -5214,13 +5174,7 @@ void NoteDrag::start_grab (GdkEvent* event, Gdk::Cursor *) { Drag::start_grab (event); - - if (_editor->snap_delta () == SnapRelative) { - framepos_t where = _region->source_beats_to_absolute_frames (_primary->note()->time ()); - framepos_t temp = where; - _editor->snap_to_with_modifier (temp, event); - _snap_delta = temp - where; - } + setup_snap_delta (_region->source_beats_to_absolute_frames (_primary->note()->time ())); if (!(_was_selected = _primary->selected())) { @@ -5257,7 +5211,7 @@ NoteDrag::total_dx () const frameoffset_t const n = _region->source_beats_to_absolute_frames (_primary->note()->time ()); /* new time of the primary note in session frames */ - frameoffset_t st = n + dx + _snap_delta; + frameoffset_t st = n + dx + snap_delta (); framepos_t const rp = _region->region()->position (); @@ -5265,7 +5219,7 @@ NoteDrag::total_dx () const st = max (st, rp); /* snap and return corresponding delta */ - return _region->snap_frame_to_frame (st - rp) + rp - n - _snap_delta; + return _region->snap_frame_to_frame (st - rp) + rp - n - snap_delta (); } /** @return Current total drag y change in note number */ diff --git a/gtk2_ardour/editor_drag.h b/gtk2_ardour/editor_drag.h index e5eecbb3ed..5fb183671f 100644 --- a/gtk2_ardour/editor_drag.h +++ b/gtk2_ardour/editor_drag.h @@ -217,9 +217,16 @@ protected: return _last_pointer_frame; } + framecnt_t snap_delta () const { + return _snap_delta; + } + double current_pointer_x () const; double current_pointer_y () const; + /* sets snap delta from unsnapped pos */ + void setup_snap_delta (framepos_t pos); + boost::shared_ptr add_midi_region (MidiTimeAxisView*); void show_verbose_cursor_time (framepos_t); @@ -248,6 +255,11 @@ private: ARDOUR::framepos_t _raw_grab_frame; ///< unsnapped frame that the mouse was at when start_grab was called, or 0 ARDOUR::framepos_t _grab_frame; ///< adjusted_frame that the mouse was at when start_grab was called, or 0 ARDOUR::framepos_t _last_pointer_frame; ///< adjusted_frame the last time a motion occurred + + /* difference between some key position's snapped and unsnapped + * framepos. used for relative snap. + */ + framecnt_t _snap_delta; CursorContext::Handle _cursor_ctx; ///< cursor change context }; @@ -341,7 +353,6 @@ private: uint32_t _ndropzone; uint32_t _pdropzone; uint32_t _ddropzone; - int32_t _snap_delta; ///< delta between the initial position and next snap point }; @@ -529,7 +540,6 @@ class NoteDrag : public Drag double _cumulative_dy; bool _was_selected; double _note_height; - int32_t _snap_delta; }; class NoteCreateDrag : public Drag @@ -649,7 +659,6 @@ private: bool _preserve_fade_anchor; bool _jump_position_when_done; - int32_t _snap_delta; }; /** Meter marker drag */ @@ -732,7 +741,6 @@ private: EditorCursor& _cursor; bool _stop; ///< true to stop the transport on starting the drag, otherwise false double _grab_zoom; ///< editor frames per unit when our grab started - int32_t _snap_delta; }; /** Region fade-in drag */ @@ -751,8 +759,6 @@ public: } void setup_pointer_frame_offset (); -private: - int32_t _snap_delta; }; /** Region fade-out drag */ @@ -771,8 +777,6 @@ public: } void setup_pointer_frame_offset (); -private: - int32_t _snap_delta; }; /** Marker drag */ @@ -836,7 +840,6 @@ private: double _cumulative_y_drag; bool _pushing; uint32_t _final_index; - int32_t _snap_delta; static double _zero_gain_fraction; }; @@ -961,8 +964,6 @@ public: void motion (GdkEvent *, bool); void finished (GdkEvent *, bool); void aborted (bool); -private: - int32_t _snap_delta; }; /** Scrub drag in audition mode */