Individual snap setting per animation

This commit is contained in:
JannisX11 2020-09-14 12:44:43 +02:00
parent 4abb94684a
commit 713d5a5724
3 changed files with 36 additions and 6 deletions

View File

@ -9,6 +9,7 @@ class Animation {
this.anim_time_update = '';
this.blend_weight = '';
this.length = 0;
this.snapping = settings.animation_snap.value;
this.animators = {};
this.markers = [];
for (var key in Animation.properties) {
@ -28,6 +29,7 @@ class Animation {
Merge.string(this, data, 'anim_time_update')
Merge.string(this, data, 'blend_weight')
Merge.number(this, data, 'length')
Merge.number(this, data, 'snapping')
if (typeof data.length == 'number') {
this.setLength(this.length)
}
@ -67,6 +69,7 @@ class Animation {
anim_time_update: this.anim_time_update,
blend_weight: this.blend_weight,
length: this.length,
snapping: this.snapping,
selected: this.selected,
}
for (var key in Animation.properties) {
@ -409,6 +412,31 @@ class Animation {
if (undo) Undo.finishEdit('change animation loop mode')
}
}
calculateSnappingFromKeyframes() {
let timecodes = [];
for (var key in this.animators) {
let animator = this.animators[key];
animator.keyframes.forEach(kf => {
timecodes.safePush(kf.time);
})
}
if (timecodes.length > 1) {
for (var i = 10; i <= 80; i++) {
let works = true;
for (var timecode of timecodes) {
let factor = (timecode * i) % 1;
if (factor > 0.01 && factor < 0.99) {
works = false;
break;
}
}
if (works) {
this.snapping = i;
return this.snapping;
}
}
}
}
}
Animation.all = [];
Animation.prototype.menu = new Menu([
@ -1128,6 +1156,7 @@ const Animator = {
})
}
}
animation.calculateSnappingFromKeyframes();
if (!Animator.selected) {
animation.select()
}

View File

@ -177,7 +177,7 @@ class Keyframe {
}
}
getTimecodeString() {
let timecode = trimFloatNumber(Timeline.snapTime(this.time)).toString();
let timecode = trimFloatNumber(Timeline.snapTime(this.time, this.animator.animation)).toString();
if (!timecode.includes('.')) {
timecode += '.0';
}
@ -517,7 +517,7 @@ BARS.defineActions(function() {
condition: () => Animator.open && Animator.selected,
getInterval(event) {
if (event && event.shiftKey) return 1;
return 1/Math.clamp(settings.animation_snap.value, 1, 120)
return Timeline.getStep()
},
get: function() {
return Animator.selected.length
@ -537,7 +537,7 @@ BARS.defineActions(function() {
condition: () => Animator.open && Timeline.selected.length,
getInterval(event) {
if (event && event.shiftKey) return 1;
return 1/Math.clamp(settings.animation_snap.value, 1, 120)
return Timeline.getStep()
},
get: function() {
return Timeline.selected[0] ? Timeline.selected[0].time : 0

View File

@ -190,16 +190,17 @@ const Timeline = {
if ((f+'').length === 1) {f = '0'+f}
$('#timeline_corner').text(m + ':' + s + ':' + f)
},
snapTime(time) {
snapTime(time, animation) {
//return time;
if (time == undefined || isNaN(time)) {
time = Timeline.time;
}
var fps = Math.clamp(settings.animation_snap.value, 1, 120);
if (!animation) animation = Animator.selected;
var fps = Math.clamp(animation.snapping, 1, 120);
return Math.clamp(Math.round(time*fps)/fps, 0);
},
getStep() {
return 1/Math.clamp(settings.animation_snap.value, 1, 120);
return 1/Math.clamp(Animator.selected.snapping, 1, 120);
},
setup() {
$('#timeline_body').mousedown(Timeline.selector.down)