timeline: vastly improved snapping
This commit is contained in:
@@ -150,13 +150,13 @@ void TimelineWidget::AddTool::MouseMoveInternal(const rational &cursor_frame, bo
|
||||
rational movement = cursor_frame - drag_start_point_;
|
||||
|
||||
// Snap movement
|
||||
bool snapped = SnapPoint(snap_points_, &movement);
|
||||
bool snapped = parent()->SnapPoint(snap_points_, &movement);
|
||||
|
||||
// If alt is held, our movement goes both ways (outwards)
|
||||
if (!snapped && outwards) {
|
||||
// Snap backwards too
|
||||
movement = -movement;
|
||||
SnapPoint(snap_points_, &movement);
|
||||
parent()->SnapPoint(snap_points_, &movement);
|
||||
// We don't need to un-neg here because outwards means all future processing will be done both pos and neg
|
||||
}
|
||||
|
||||
|
||||
@@ -122,14 +122,17 @@ void TimelineWidget::ImportTool::DragMove(TimelineViewMouseEvent *event)
|
||||
rational time_movement = event->GetFrame() - drag_start_.GetFrame();
|
||||
int track_movement = event->GetTrack().index() - drag_start_.GetTrack().index();
|
||||
|
||||
// If snapping is enabled, check for snap points
|
||||
if (Core::instance()->snapping()) {
|
||||
SnapPoint(snap_points_, &time_movement);
|
||||
}
|
||||
|
||||
time_movement = ValidateTimeMovement(time_movement, parent()->ghost_items_);
|
||||
track_movement = ValidateTrackMovement(track_movement, parent()->ghost_items_);
|
||||
|
||||
// If snapping is enabled, check for snap points
|
||||
if (Core::instance()->snapping()) {
|
||||
parent()->SnapPoint(snap_points_, &time_movement);
|
||||
|
||||
time_movement = ValidateTimeMovement(time_movement, parent()->ghost_items_);
|
||||
track_movement = ValidateTrackMovement(track_movement, parent()->ghost_items_);
|
||||
}
|
||||
|
||||
rational earliest_ghost = RATIONAL_MAX;
|
||||
|
||||
// Move ghosts to the mouse cursor
|
||||
|
||||
@@ -314,17 +314,20 @@ void TimelineWidget::PointerTool::ProcessDrag(const TimelineCoordinate &mouse_po
|
||||
// Determine frame movement
|
||||
rational time_movement = mouse_pos.GetFrame() - drag_start_.GetFrame();
|
||||
|
||||
// Perform snapping if enabled (adjusts time_movement if it's close to any potential snap points)
|
||||
if (Core::instance()->snapping()) {
|
||||
SnapPoint(snap_points_, &time_movement);
|
||||
}
|
||||
|
||||
// Validate movement (enforce all ghosts moving in legal ways)
|
||||
// NOTE: Always do this after snapping to ensure the snap hasn't made an illegal movement.
|
||||
time_movement = ValidateTimeMovement(time_movement, parent()->ghost_items_);
|
||||
time_movement = ValidateInTrimming(time_movement, parent()->ghost_items_, !trim_overwrite_allowed_);
|
||||
time_movement = ValidateOutTrimming(time_movement, parent()->ghost_items_, !trim_overwrite_allowed_);
|
||||
|
||||
// Perform snapping if enabled (adjusts time_movement if it's close to any potential snap points)
|
||||
if (Core::instance()->snapping()) {
|
||||
parent()->SnapPoint(snap_points_, &time_movement);
|
||||
|
||||
time_movement = ValidateTimeMovement(time_movement, parent()->ghost_items_);
|
||||
time_movement = ValidateInTrimming(time_movement, parent()->ghost_items_, !trim_overwrite_allowed_);
|
||||
time_movement = ValidateOutTrimming(time_movement, parent()->ghost_items_, !trim_overwrite_allowed_);
|
||||
}
|
||||
|
||||
// Validate ghosts that are being moved (clips from other track types do NOT get moved)
|
||||
{
|
||||
QVector<TimelineViewGhostItem*> validate_track_ghosts = parent()->ghost_items_;
|
||||
|
||||
@@ -20,9 +20,6 @@
|
||||
|
||||
#include "widget/timelinewidget/timelinewidget.h"
|
||||
|
||||
#include <cfloat>
|
||||
|
||||
#include "common/range.h"
|
||||
#include "node/block/transition/transition.h"
|
||||
#include "widget/nodeview/nodeviewundo.h"
|
||||
|
||||
@@ -76,28 +73,6 @@ TimelineViewBlockItem *TimelineWidget::Tool::GetItemAtScenePos(const TimelineCoo
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void AttemptSnap(const QList<double>& proposed_pts,
|
||||
double compare_point,
|
||||
const QList<rational>& start_times,
|
||||
rational compare_time,
|
||||
rational* movement,
|
||||
double* diff) {
|
||||
const qreal kSnapRange = 10; // FIXME: Hardcoded number
|
||||
|
||||
for (int i=0;i<proposed_pts.size();i++) {
|
||||
// Attempt snapping to clip out point
|
||||
if (InRange(proposed_pts.at(i), compare_point, kSnapRange)) {
|
||||
double this_diff = qAbs(compare_point - proposed_pts.at(i));
|
||||
|
||||
if (this_diff < *diff
|
||||
&& start_times.at(i) + *movement >= 0) {
|
||||
*movement = compare_time - start_times.at(i);
|
||||
*diff = this_diff;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rational TimelineWidget::Tool::ValidateTimeMovement(rational movement, const QVector<TimelineViewGhostItem *> ghosts)
|
||||
{
|
||||
foreach (TimelineViewGhostItem* ghost, ghosts) {
|
||||
@@ -153,52 +128,6 @@ int TimelineWidget::Tool::ValidateTrackMovement(int movement, const QVector<Time
|
||||
return movement;
|
||||
}
|
||||
|
||||
bool TimelineWidget::Tool::SnapPoint(QList<rational> start_times, rational* movement, int snap_points)
|
||||
{
|
||||
double diff = DBL_MAX;
|
||||
|
||||
QList<double> proposed_pts;
|
||||
|
||||
foreach (rational s, start_times) {
|
||||
proposed_pts.append((s + *movement).toDouble() * parent()->GetScale());
|
||||
}
|
||||
|
||||
if (snap_points & kSnapToPlayhead) {
|
||||
|
||||
|
||||
rational playhead_abs_time = rational(parent()->GetTimestamp() * parent()->timebase().numerator(),
|
||||
parent()->timebase().denominator());
|
||||
|
||||
qreal playhead_pos = playhead_abs_time.toDouble() * parent()->GetScale();
|
||||
|
||||
AttemptSnap(proposed_pts, playhead_pos, start_times, playhead_abs_time, movement, &diff);
|
||||
}
|
||||
|
||||
if (snap_points & kSnapToClips) {
|
||||
QMapIterator<Block*, TimelineViewBlockItem*> iterator(parent()->block_items_);
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
iterator.next();
|
||||
|
||||
TimelineViewBlockItem* item = iterator.value();
|
||||
|
||||
if (item != nullptr) {
|
||||
qreal rect_left = item->x();
|
||||
qreal rect_right = rect_left + item->rect().width();
|
||||
|
||||
// Attempt snapping to clip in point
|
||||
AttemptSnap(proposed_pts, rect_left, start_times, item->block()->in(), movement, &diff);
|
||||
|
||||
// Attempt snapping to clip out point
|
||||
AttemptSnap(proposed_pts, rect_right, start_times, item->block()->out(), movement, &diff);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (diff < DBL_MAX);
|
||||
}
|
||||
|
||||
void TimelineWidget::Tool::GetGhostData(const QVector<TimelineViewGhostItem *> &ghosts, rational *earliest_point, rational *latest_point)
|
||||
{
|
||||
rational ep = RATIONAL_MAX;
|
||||
|
||||
Reference in New Issue
Block a user