|
1 | 1 | import { isCurrentFormat } from '../blueprintFormat' |
2 | 2 | import { PACKAGE } from '../constants' |
3 | 3 | import { events } from '../util/events' |
4 | | -import { ContextProperty, createBlockbenchMod } from '../util/moddingTools' |
5 | | -import { |
6 | | - EASING_DEFAULT, |
7 | | - EasingKey, |
8 | | - easingFunctions, |
9 | | - getEasingArgDefault, |
10 | | - hasArgs, |
11 | | -} from '../util/easing' |
12 | | - |
13 | | -interface IEasingProperties { |
14 | | - easing?: EasingKey |
15 | | - easingArgs?: any[] |
16 | | -} |
| 4 | +import { createBlockbenchMod } from '../util/moddingTools' |
17 | 5 |
|
18 | 6 | createBlockbenchMod( |
19 | 7 | `${PACKAGE.name}:keyframeSelectEventMod`, |
@@ -84,133 +72,3 @@ createBlockbenchMod( |
84 | 72 | context.barItem.change = context.originalChange |
85 | 73 | } |
86 | 74 | ) |
87 | | - |
88 | | -export function reverseEasing(easing?: EasingKey): EasingKey | undefined { |
89 | | - if (!easing) return easing |
90 | | - if (easing.startsWith('easeInOut')) return easing |
91 | | - if (easing.startsWith('easeIn')) return easing.replace('easeIn', 'easeOut') |
92 | | - if (easing.startsWith('easeOut')) return easing.replace('easeOut', 'easeIn') |
93 | | - return easing |
94 | | -} |
95 | | - |
96 | | -createBlockbenchMod( |
97 | | - `${PACKAGE.name}:reverseKeyframesMod`, |
98 | | - { |
99 | | - action: BarItems.reverse_keyframes as Action, |
100 | | - originalClick: (BarItems.reverse_keyframes as Action).click, |
101 | | - }, |
102 | | - context => { |
103 | | - context.action.click = function (event?: Event) { |
104 | | - context.originalClick.call(this, event) |
105 | | - // There's not really an easy way to merge our undo operation with the original one so we'll make a new one instead |
106 | | - Undo.initEdit({ keyframes: Timeline.selected || undefined }) |
107 | | - |
108 | | - const kfByAnimator: Record<string, _Keyframe[]> = {} |
109 | | - for (const kf of Timeline.selected || []) { |
110 | | - kfByAnimator[kf.animator.uuid] ??= [] |
111 | | - kfByAnimator[kf.animator.uuid].push(kf) |
112 | | - } |
113 | | - |
114 | | - const kfByAnimatorAndChannel: Record<string, Record<string, _Keyframe[]>> = {} |
115 | | - for (const [animatorUuid, keyframes] of Object.entries(kfByAnimator)) { |
116 | | - const channel: Record<string, _Keyframe[]> = {} |
117 | | - kfByAnimatorAndChannel[animatorUuid] = channel |
118 | | - for (const kf of keyframes) { |
119 | | - channel[kf.channel] ??= [] |
120 | | - channel[kf.channel].push(kf) |
121 | | - } |
122 | | - } |
123 | | - |
124 | | - for (const channelGroups of Object.values(kfByAnimatorAndChannel)) { |
125 | | - for (const keyframes of Object.values(channelGroups)) { |
126 | | - // Ensure keyframes are in temporal order. Not sure if this is already the case, but it couldn't hurt |
127 | | - keyframes.sort((a, b) => a.time - b.time) |
128 | | - // Reverse easing direction |
129 | | - const easingData: IEasingProperties[] = keyframes.map((kf: _Keyframe) => ({ |
130 | | - easing: reverseEasing(kf.easing), |
131 | | - easingArgs: kf.easingArgs, |
132 | | - })) |
133 | | - // Shift easing data to the right by one keyframe |
134 | | - keyframes.forEach((kf: _Keyframe, i: number) => { |
135 | | - if (i == 0) { |
136 | | - kf.easing = undefined |
137 | | - kf.easingArgs = undefined |
138 | | - return |
139 | | - } |
140 | | - const newEasingData = easingData[i - 1] |
141 | | - kf.easing = newEasingData.easing |
142 | | - kf.easingArgs = newEasingData.easingArgs |
143 | | - }) |
144 | | - } |
145 | | - } |
146 | | - |
147 | | - Undo.finishEdit('Reverse keyframe easing') |
148 | | - updateKeyframeSelection() |
149 | | - Animator.preview() |
150 | | - } |
151 | | - return context |
152 | | - }, |
153 | | - context => { |
154 | | - context.action.click = context.originalClick |
155 | | - } |
156 | | -) |
157 | | - |
158 | | -function lerp(start: number, stop: number, amt: number): number { |
159 | | - return amt * (stop - start) + start |
160 | | -} |
161 | | - |
162 | | -createBlockbenchMod( |
163 | | - `${PACKAGE.name}:keyframeEasingMod`, |
164 | | - { |
165 | | - originalGetLerp: Blockbench.Keyframe.prototype.getLerp, |
166 | | - easingProperty: undefined as ContextProperty<'string'>, |
167 | | - easingArgsProperty: undefined as ContextProperty<'array'>, |
168 | | - }, |
169 | | - context => { |
170 | | - context.easingProperty = new Property(Blockbench.Keyframe, 'string', 'easing', { |
171 | | - default: EASING_DEFAULT, |
172 | | - condition: isCurrentFormat(), |
173 | | - }) |
174 | | - context.easingArgsProperty = new Property(Blockbench.Keyframe, 'array', 'easingArgs', { |
175 | | - condition: isCurrentFormat(), |
176 | | - }) |
177 | | - |
178 | | - Blockbench.Keyframe.prototype.getLerp = function ( |
179 | | - this: _Keyframe, |
180 | | - other, |
181 | | - axis, |
182 | | - amount, |
183 | | - allowExpression |
184 | | - ): number { |
185 | | - if (!isCurrentFormat()) |
186 | | - return context.originalGetLerp.call(this, other, axis, amount, allowExpression) |
187 | | - |
188 | | - const easing = other.easing || 'linear' |
189 | | - let easingFunc = easingFunctions[easing] |
190 | | - if (hasArgs(easing)) { |
191 | | - const arg1 = |
192 | | - Array.isArray(other.easingArgs) && other.easingArgs.length > 0 |
193 | | - ? other.easingArgs[0] |
194 | | - : getEasingArgDefault(other) |
195 | | - |
196 | | - easingFunc = easingFunc.bind(null, arg1 || 0) |
197 | | - } |
198 | | - const easedAmount = easingFunc(amount) |
199 | | - const start = this.calc(axis) |
200 | | - const stop = other.calc(axis) |
201 | | - const result = lerp(start, stop, easedAmount) |
202 | | - |
203 | | - if (Number.isNaN(result)) { |
204 | | - throw new Error('Invalid easing function or arguments.') |
205 | | - } |
206 | | - return result |
207 | | - } |
208 | | - |
209 | | - return context |
210 | | - }, |
211 | | - context => { |
212 | | - context.easingProperty?.delete() |
213 | | - context.easingArgsProperty?.delete() |
214 | | - Blockbench.Keyframe.prototype.getLerp = context.originalGetLerp |
215 | | - } |
216 | | -) |
0 commit comments