comparison layer/NoteLayer.cpp @ 1432:5b9692768beb single-point

Further snap fixes
author Chris Cannam
date Wed, 20 Mar 2019 15:46:17 +0000
parents c9fa16e41664
children 696e569ff21b
comparison
equal deleted inserted replaced
1431:af824022bffd 1432:5b9692768beb
532 { 532 {
533 if (!m_model) { 533 if (!m_model) {
534 return Layer::snapToFeatureFrame(v, frame, resolution, snap); 534 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
535 } 535 }
536 536
537 // SnapLeft / SnapRight: return frame of nearest feature in that
538 // direction no matter how far away
539 //
540 // SnapNeighbouring: return frame of feature that would be used in
541 // an editing operation, i.e. closest feature in either direction
542 // but only if it is "close enough"
543
537 resolution = m_model->getResolution(); 544 resolution = m_model->getResolution();
538 EventVector points;
539 545
540 if (snap == SnapNeighbouring) { 546 if (snap == SnapNeighbouring) {
541 547 EventVector points = getLocalPoints(v, v->getXForFrame(frame));
542 points = getLocalPoints(v, v->getXForFrame(frame));
543 if (points.empty()) return false; 548 if (points.empty()) return false;
544 frame = points.begin()->getFrame(); 549 frame = points.begin()->getFrame();
545 return true; 550 return true;
546 } 551 }
547 552
548 //!!! I think this is not quite right - we want to be able to snap 553 Event e;
549 //!!! to events that are nearby but not covering the given frame, 554 if (m_model->getNearestEventMatching
550 //!!! and I think that worked with the old code. Compare and 555 (frame,
551 //!!! revise. 556 [](Event) { return true; },
552 557 snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward,
553 points = m_model->getEventsCovering(frame); 558 e)) {
554 sv_frame_t snapped = frame; 559 frame = e.getFrame();
555 bool found = false; 560 return true;
556 561 }
557 for (EventVector::const_iterator i = points.begin(); 562
558 i != points.end(); ++i) { 563 return false;
559
560 if (snap == SnapRight) {
561
562 if (i->getFrame() > frame) {
563 snapped = i->getFrame();
564 found = true;
565 break;
566 }
567
568 } else if (snap == SnapLeft) {
569
570 if (i->getFrame() <= frame) {
571 snapped = i->getFrame();
572 found = true; // don't break, as the next may be better
573 } else {
574 break;
575 }
576
577 } else { // nearest
578
579 EventVector::const_iterator j = i;
580 ++j;
581
582 if (j == points.end()) {
583
584 snapped = i->getFrame();
585 found = true;
586 break;
587
588 } else if (j->getFrame() >= frame) {
589
590 if (j->getFrame() - frame < frame - i->getFrame()) {
591 snapped = j->getFrame();
592 } else {
593 snapped = i->getFrame();
594 }
595 found = true;
596 break;
597 }
598 }
599 }
600
601 frame = snapped;
602 return found;
603 } 564 }
604 565
605 void 566 void
606 NoteLayer::getScaleExtents(LayerGeometryProvider *v, double &min, double &max, bool &log) const 567 NoteLayer::getScaleExtents(LayerGeometryProvider *v, double &min, double &max, bool &log) const
607 { 568 {